blob: 8ecfffc9705051e649ec3ea8dcc563e3bdd7eec8 [file] [log] [blame]
Dmitry Shmidt04949592012-07-19 12:16:46 -07001/*
2 * RADIUS Dynamic Authorization Server (DAS) (RFC 5176)
3 * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include <net/if.h>
11
12#include "utils/common.h"
13#include "utils/eloop.h"
14#include "utils/ip_addr.h"
15#include "radius.h"
16#include "radius_das.h"
17
18
19extern int wpa_debug_level;
20
21
22struct radius_das_data {
23 int sock;
24 u8 *shared_secret;
25 size_t shared_secret_len;
26 struct hostapd_ip_addr client_addr;
27 unsigned int time_window;
28 int require_event_timestamp;
29 void *ctx;
30 enum radius_das_res (*disconnect)(void *ctx,
31 struct radius_das_attrs *attr);
32};
33
34
35static struct radius_msg * radius_das_disconnect(struct radius_das_data *das,
36 struct radius_msg *msg,
37 const char *abuf,
38 int from_port)
39{
40 struct radius_hdr *hdr;
41 struct radius_msg *reply;
42 u8 allowed[] = {
43 RADIUS_ATTR_USER_NAME,
44 RADIUS_ATTR_CALLING_STATION_ID,
45 RADIUS_ATTR_ACCT_SESSION_ID,
46 RADIUS_ATTR_EVENT_TIMESTAMP,
47 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
48 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
49 0
50 };
51 int error = 405;
52 u8 attr;
53 enum radius_das_res res;
54 struct radius_das_attrs attrs;
55 u8 *buf;
56 size_t len;
57 char tmp[100];
58 u8 sta_addr[ETH_ALEN];
59
60 hdr = radius_msg_get_hdr(msg);
61
62 attr = radius_msg_find_unlisted_attr(msg, allowed);
63 if (attr) {
64 wpa_printf(MSG_INFO, "DAS: Unsupported attribute %u in "
65 "Disconnect-Request from %s:%d", attr,
66 abuf, from_port);
67 error = 401;
68 goto fail;
69 }
70
71 os_memset(&attrs, 0, sizeof(attrs));
72
73 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CALLING_STATION_ID,
74 &buf, &len, NULL) == 0) {
75 if (len >= sizeof(tmp))
76 len = sizeof(tmp) - 1;
77 os_memcpy(tmp, buf, len);
78 tmp[len] = '\0';
79 if (hwaddr_aton2(tmp, sta_addr) < 0) {
80 wpa_printf(MSG_INFO, "DAS: Invalid Calling-Station-Id "
81 "'%s' from %s:%d", tmp, abuf, from_port);
82 error = 407;
83 goto fail;
84 }
85 attrs.sta_addr = sta_addr;
86 }
87
88 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
89 &buf, &len, NULL) == 0) {
90 attrs.user_name = buf;
91 attrs.user_name_len = len;
92 }
93
94 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
95 &buf, &len, NULL) == 0) {
96 attrs.acct_session_id = buf;
97 attrs.acct_session_id_len = len;
98 }
99
100 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
101 &buf, &len, NULL) == 0) {
102 attrs.cui = buf;
103 attrs.cui_len = len;
104 }
105
106 res = das->disconnect(das->ctx, &attrs);
107 switch (res) {
108 case RADIUS_DAS_NAS_MISMATCH:
109 wpa_printf(MSG_INFO, "DAS: NAS mismatch from %s:%d",
110 abuf, from_port);
111 error = 403;
112 break;
113 case RADIUS_DAS_SESSION_NOT_FOUND:
114 wpa_printf(MSG_INFO, "DAS: Session not found for request from "
115 "%s:%d", abuf, from_port);
116 error = 503;
117 break;
118 case RADIUS_DAS_SUCCESS:
119 error = 0;
120 break;
121 }
122
123fail:
124 reply = radius_msg_new(error ? RADIUS_CODE_DISCONNECT_NAK :
125 RADIUS_CODE_DISCONNECT_ACK, hdr->identifier);
126 if (reply == NULL)
127 return NULL;
128
129 if (error) {
130 radius_msg_add_attr_int32(reply, RADIUS_ATTR_ERROR_CAUSE,
131 error);
132 }
133
134 return reply;
135}
136
137
138static void radius_das_receive(int sock, void *eloop_ctx, void *sock_ctx)
139{
140 struct radius_das_data *das = eloop_ctx;
141 u8 buf[1500];
142 union {
143 struct sockaddr_storage ss;
144 struct sockaddr_in sin;
145#ifdef CONFIG_IPV6
146 struct sockaddr_in6 sin6;
147#endif /* CONFIG_IPV6 */
148 } from;
149 char abuf[50];
150 int from_port = 0;
151 socklen_t fromlen;
152 int len;
153 struct radius_msg *msg, *reply = NULL;
154 struct radius_hdr *hdr;
155 struct wpabuf *rbuf;
156 u32 val;
157 int res;
158 struct os_time now;
159
160 fromlen = sizeof(from);
161 len = recvfrom(sock, buf, sizeof(buf), 0,
162 (struct sockaddr *) &from.ss, &fromlen);
163 if (len < 0) {
164 wpa_printf(MSG_ERROR, "DAS: recvfrom: %s", strerror(errno));
165 return;
166 }
167
168 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
169 from_port = ntohs(from.sin.sin_port);
170
171 wpa_printf(MSG_DEBUG, "DAS: Received %d bytes from %s:%d",
172 len, abuf, from_port);
173 if (das->client_addr.u.v4.s_addr != from.sin.sin_addr.s_addr) {
174 wpa_printf(MSG_DEBUG, "DAS: Drop message from unknown client");
175 return;
176 }
177
178 msg = radius_msg_parse(buf, len);
179 if (msg == NULL) {
180 wpa_printf(MSG_DEBUG, "DAS: Parsing incoming RADIUS packet "
181 "from %s:%d failed", abuf, from_port);
182 return;
183 }
184
185 if (wpa_debug_level <= MSG_MSGDUMP)
186 radius_msg_dump(msg);
187
188 if (radius_msg_verify_das_req(msg, das->shared_secret,
189 das->shared_secret_len)) {
190 wpa_printf(MSG_DEBUG, "DAS: Invalid authenticator in packet "
191 "from %s:%d - drop", abuf, from_port);
192 goto fail;
193 }
194
195 os_get_time(&now);
196 res = radius_msg_get_attr(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
197 (u8 *) &val, 4);
198 if (res == 4) {
199 u32 timestamp = ntohl(val);
200 if (abs(now.sec - timestamp) > das->time_window) {
201 wpa_printf(MSG_DEBUG, "DAS: Unacceptable "
202 "Event-Timestamp (%u; local time %u) in "
203 "packet from %s:%d - drop",
204 timestamp, (unsigned int) now.sec,
205 abuf, from_port);
206 goto fail;
207 }
208 } else if (das->require_event_timestamp) {
209 wpa_printf(MSG_DEBUG, "DAS: Missing Event-Timestamp in packet "
210 "from %s:%d - drop", abuf, from_port);
211 goto fail;
212 }
213
214 hdr = radius_msg_get_hdr(msg);
215
216 switch (hdr->code) {
217 case RADIUS_CODE_DISCONNECT_REQUEST:
218 reply = radius_das_disconnect(das, msg, abuf, from_port);
219 break;
220 case RADIUS_CODE_COA_REQUEST:
221 /* TODO */
222 reply = radius_msg_new(RADIUS_CODE_COA_NAK,
223 hdr->identifier);
224 if (reply == NULL)
225 break;
226
227 /* Unsupported Service */
228 radius_msg_add_attr_int32(reply, RADIUS_ATTR_ERROR_CAUSE, 405);
229 break;
230 default:
231 wpa_printf(MSG_DEBUG, "DAS: Unexpected RADIUS code %u in "
232 "packet from %s:%d",
233 hdr->code, abuf, from_port);
234 }
235
236 if (reply) {
237 wpa_printf(MSG_DEBUG, "DAS: Reply to %s:%d", abuf, from_port);
238
239 if (!radius_msg_add_attr_int32(reply,
240 RADIUS_ATTR_EVENT_TIMESTAMP,
241 now.sec)) {
242 wpa_printf(MSG_DEBUG, "DAS: Failed to add "
243 "Event-Timestamp attribute");
244 }
245
246 if (radius_msg_finish_das_resp(reply, das->shared_secret,
247 das->shared_secret_len, hdr) <
248 0) {
249 wpa_printf(MSG_DEBUG, "DAS: Failed to add "
250 "Message-Authenticator attribute");
251 }
252
253 if (wpa_debug_level <= MSG_MSGDUMP)
254 radius_msg_dump(reply);
255
256 rbuf = radius_msg_get_buf(reply);
257 res = sendto(das->sock, wpabuf_head(rbuf),
258 wpabuf_len(rbuf), 0,
259 (struct sockaddr *) &from.ss, fromlen);
260 if (res < 0) {
261 wpa_printf(MSG_ERROR, "DAS: sendto(to %s:%d): %s",
262 abuf, from_port, strerror(errno));
263 }
264 }
265
266fail:
267 radius_msg_free(msg);
268 radius_msg_free(reply);
269}
270
271
272static int radius_das_open_socket(int port)
273{
274 int s;
275 struct sockaddr_in addr;
276
277 s = socket(PF_INET, SOCK_DGRAM, 0);
278 if (s < 0) {
279 perror("socket");
280 return -1;
281 }
282
283 os_memset(&addr, 0, sizeof(addr));
284 addr.sin_family = AF_INET;
285 addr.sin_port = htons(port);
286 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
287 perror("bind");
288 close(s);
289 return -1;
290 }
291
292 return s;
293}
294
295
296struct radius_das_data *
297radius_das_init(struct radius_das_conf *conf)
298{
299 struct radius_das_data *das;
300
301 if (conf->port == 0 || conf->shared_secret == NULL ||
302 conf->client_addr == NULL)
303 return NULL;
304
305 das = os_zalloc(sizeof(*das));
306 if (das == NULL)
307 return NULL;
308
309 das->time_window = conf->time_window;
310 das->require_event_timestamp = conf->require_event_timestamp;
311 das->ctx = conf->ctx;
312 das->disconnect = conf->disconnect;
313
314 os_memcpy(&das->client_addr, conf->client_addr,
315 sizeof(das->client_addr));
316
317 das->shared_secret = os_malloc(conf->shared_secret_len);
318 if (das->shared_secret == NULL) {
319 radius_das_deinit(das);
320 return NULL;
321 }
322 os_memcpy(das->shared_secret, conf->shared_secret,
323 conf->shared_secret_len);
324 das->shared_secret_len = conf->shared_secret_len;
325
326 das->sock = radius_das_open_socket(conf->port);
327 if (das->sock < 0) {
328 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS "
329 "DAS");
330 radius_das_deinit(das);
331 return NULL;
332 }
333
334 if (eloop_register_read_sock(das->sock, radius_das_receive, das, NULL))
335 {
336 radius_das_deinit(das);
337 return NULL;
338 }
339
340 return das;
341}
342
343
344void radius_das_deinit(struct radius_das_data *das)
345{
346 if (das == NULL)
347 return;
348
349 if (das->sock >= 0) {
350 eloop_unregister_read_sock(das->sock);
351 close(das->sock);
352 }
353
354 os_free(das->shared_secret);
355 os_free(das);
356}