1 /*
2 * RADIUS client
3 * Copyright (c) 2002-2015, 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
11 #include "common.h"
12 #include "radius.h"
13 #include "radius_client.h"
14 #include "eloop.h"
15
16 /* Defaults for RADIUS retransmit values (exponential backoff) */
17
18 /**
19 * RADIUS_CLIENT_FIRST_WAIT - RADIUS client timeout for first retry in seconds
20 */
21 #define RADIUS_CLIENT_FIRST_WAIT 3
22
23 /**
24 * RADIUS_CLIENT_MAX_WAIT - RADIUS client maximum retry timeout in seconds
25 */
26 #define RADIUS_CLIENT_MAX_WAIT 120
27
28 /**
29 * RADIUS_CLIENT_MAX_FAILOVER - RADIUS client maximum retries
30 *
31 * Maximum number of server failovers before the entry is removed from
32 * retransmit list.
33 */
34 #define RADIUS_CLIENT_MAX_FAILOVER 3
35
36 /**
37 * RADIUS_CLIENT_MAX_ENTRIES - RADIUS client maximum pending messages
38 *
39 * Maximum number of entries in retransmit list (oldest entries will be
40 * removed, if this limit is exceeded).
41 */
42 #define RADIUS_CLIENT_MAX_ENTRIES 30
43
44 /**
45 * RADIUS_CLIENT_NUM_FAILOVER - RADIUS client failover point
46 *
47 * The number of failed retry attempts after which the RADIUS server will be
48 * changed (if one of more backup servers are configured).
49 */
50 #define RADIUS_CLIENT_NUM_FAILOVER 4
51
52
53 /**
54 * struct radius_rx_handler - RADIUS client RX handler
55 *
56 * This data structure is used internally inside the RADIUS client module to
57 * store registered RX handlers. These handlers are registered by calls to
58 * radius_client_register() and unregistered when the RADIUS client is
59 * deinitialized with a call to radius_client_deinit().
60 */
61 struct radius_rx_handler {
62 /**
63 * handler - Received RADIUS message handler
64 */
65 RadiusRxResult (*handler)(struct radius_msg *msg,
66 struct radius_msg *req,
67 const u8 *shared_secret,
68 size_t shared_secret_len,
69 void *data);
70
71 /**
72 * data - Context data for the handler
73 */
74 void *data;
75 };
76
77
78 /**
79 * struct radius_msg_list - RADIUS client message retransmit list
80 *
81 * This data structure is used internally inside the RADIUS client module to
82 * store pending RADIUS requests that may still need to be retransmitted.
83 */
84 struct radius_msg_list {
85 /**
86 * addr - STA/client address
87 *
88 * This is used to find RADIUS messages for the same STA.
89 */
90 u8 addr[ETH_ALEN];
91
92 /**
93 * msg - RADIUS message
94 */
95 struct radius_msg *msg;
96
97 /**
98 * msg_type - Message type
99 */
100 RadiusType msg_type;
101
102 /**
103 * first_try - Time of the first transmission attempt
104 */
105 os_time_t first_try;
106
107 /**
108 * next_try - Time for the next transmission attempt
109 */
110 os_time_t next_try;
111
112 /**
113 * attempts - Number of transmission attempts for one server
114 */
115 int attempts;
116
117 /**
118 * accu_attempts - Number of accumulated attempts
119 */
120 int accu_attempts;
121
122 /**
123 * next_wait - Next retransmission wait time in seconds
124 */
125 int next_wait;
126
127 /**
128 * last_attempt - Time of the last transmission attempt
129 */
130 struct os_reltime last_attempt;
131
132 /**
133 * shared_secret - Shared secret with the target RADIUS server
134 */
135 const u8 *shared_secret;
136
137 /**
138 * shared_secret_len - shared_secret length in octets
139 */
140 size_t shared_secret_len;
141
142 /* TODO: server config with failover to backup server(s) */
143
144 /**
145 * next - Next message in the list
146 */
147 struct radius_msg_list *next;
148 };
149
150
151 /**
152 * struct radius_client_data - Internal RADIUS client data
153 *
154 * This data structure is used internally inside the RADIUS client module.
155 * External users allocate this by calling radius_client_init() and free it by
156 * calling radius_client_deinit(). The pointer to this opaque data is used in
157 * calls to other functions as an identifier for the RADIUS client instance.
158 */
159 struct radius_client_data {
160 /**
161 * ctx - Context pointer for hostapd_logger() callbacks
162 */
163 void *ctx;
164
165 /**
166 * conf - RADIUS client configuration (list of RADIUS servers to use)
167 */
168 struct hostapd_radius_servers *conf;
169
170 /**
171 * auth_serv_sock - IPv4 socket for RADIUS authentication messages
172 */
173 int auth_serv_sock;
174
175 /**
176 * acct_serv_sock - IPv4 socket for RADIUS accounting messages
177 */
178 int acct_serv_sock;
179
180 /**
181 * auth_serv_sock6 - IPv6 socket for RADIUS authentication messages
182 */
183 int auth_serv_sock6;
184
185 /**
186 * acct_serv_sock6 - IPv6 socket for RADIUS accounting messages
187 */
188 int acct_serv_sock6;
189
190 /**
191 * auth_sock - Currently used socket for RADIUS authentication server
192 */
193 int auth_sock;
194
195 /**
196 * acct_sock - Currently used socket for RADIUS accounting server
197 */
198 int acct_sock;
199
200 /**
201 * auth_handlers - Authentication message handlers
202 */
203 struct radius_rx_handler *auth_handlers;
204
205 /**
206 * num_auth_handlers - Number of handlers in auth_handlers
207 */
208 size_t num_auth_handlers;
209
210 /**
211 * acct_handlers - Accounting message handlers
212 */
213 struct radius_rx_handler *acct_handlers;
214
215 /**
216 * num_acct_handlers - Number of handlers in acct_handlers
217 */
218 size_t num_acct_handlers;
219
220 /**
221 * msgs - Pending outgoing RADIUS messages
222 */
223 struct radius_msg_list *msgs;
224
225 /**
226 * num_msgs - Number of pending messages in the msgs list
227 */
228 size_t num_msgs;
229
230 /**
231 * next_radius_identifier - Next RADIUS message identifier to use
232 */
233 u8 next_radius_identifier;
234
235 /**
236 * interim_error_cb - Interim accounting error callback
237 */
238 void (*interim_error_cb)(const u8 *addr, void *ctx);
239
240 /**
241 * interim_error_cb_ctx - interim_error_cb() context data
242 */
243 void *interim_error_cb_ctx;
244 };
245
246
247 static int
248 radius_change_server(struct radius_client_data *radius,
249 struct hostapd_radius_server *nserv,
250 struct hostapd_radius_server *oserv,
251 int sock, int sock6, int auth);
252 static int radius_client_init_acct(struct radius_client_data *radius);
253 static int radius_client_init_auth(struct radius_client_data *radius);
254 static void radius_client_auth_failover(struct radius_client_data *radius);
255 static void radius_client_acct_failover(struct radius_client_data *radius);
256
257
radius_client_msg_free(struct radius_msg_list * req)258 static void radius_client_msg_free(struct radius_msg_list *req)
259 {
260 radius_msg_free(req->msg);
261 os_free(req);
262 }
263
264
265 /**
266 * radius_client_register - Register a RADIUS client RX handler
267 * @radius: RADIUS client context from radius_client_init()
268 * @msg_type: RADIUS client type (RADIUS_AUTH or RADIUS_ACCT)
269 * @handler: Handler for received RADIUS messages
270 * @data: Context pointer for handler callbacks
271 * Returns: 0 on success, -1 on failure
272 *
273 * This function is used to register a handler for processing received RADIUS
274 * authentication and accounting messages. The handler() callback function will
275 * be called whenever a RADIUS message is received from the active server.
276 *
277 * There can be multiple registered RADIUS message handlers. The handlers will
278 * be called in order until one of them indicates that it has processed or
279 * queued the message.
280 */
radius_client_register(struct radius_client_data * radius,RadiusType msg_type,RadiusRxResult (* handler)(struct radius_msg * msg,struct radius_msg * req,const u8 * shared_secret,size_t shared_secret_len,void * data),void * data)281 int radius_client_register(struct radius_client_data *radius,
282 RadiusType msg_type,
283 RadiusRxResult (*handler)(struct radius_msg *msg,
284 struct radius_msg *req,
285 const u8 *shared_secret,
286 size_t shared_secret_len,
287 void *data),
288 void *data)
289 {
290 struct radius_rx_handler **handlers, *newh;
291 size_t *num;
292
293 if (msg_type == RADIUS_ACCT) {
294 handlers = &radius->acct_handlers;
295 num = &radius->num_acct_handlers;
296 } else {
297 handlers = &radius->auth_handlers;
298 num = &radius->num_auth_handlers;
299 }
300
301 newh = os_realloc_array(*handlers, *num + 1,
302 sizeof(struct radius_rx_handler));
303 if (newh == NULL)
304 return -1;
305
306 newh[*num].handler = handler;
307 newh[*num].data = data;
308 (*num)++;
309 *handlers = newh;
310
311 return 0;
312 }
313
314
315 /**
316 * radius_client_set_interim_erro_cb - Register an interim acct error callback
317 * @radius: RADIUS client context from radius_client_init()
318 * @addr: Station address from the failed message
319 * @cb: Handler for interim accounting errors
320 * @ctx: Context pointer for handler callbacks
321 *
322 * This function is used to register a handler for processing failed
323 * transmission attempts of interim accounting update messages.
324 */
radius_client_set_interim_error_cb(struct radius_client_data * radius,void (* cb)(const u8 * addr,void * ctx),void * ctx)325 void radius_client_set_interim_error_cb(struct radius_client_data *radius,
326 void (*cb)(const u8 *addr, void *ctx),
327 void *ctx)
328 {
329 radius->interim_error_cb = cb;
330 radius->interim_error_cb_ctx = ctx;
331 }
332
333
334 /*
335 * Returns >0 if message queue was flushed (i.e., the message that triggered
336 * the error is not available anymore)
337 */
radius_client_handle_send_error(struct radius_client_data * radius,int s,RadiusType msg_type)338 static int radius_client_handle_send_error(struct radius_client_data *radius,
339 int s, RadiusType msg_type)
340 {
341 #ifndef CONFIG_NATIVE_WINDOWS
342 int _errno = errno;
343 wpa_printf(MSG_INFO, "send[RADIUS,s=%d]: %s", s, strerror(errno));
344 if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
345 _errno == EBADF || _errno == ENETUNREACH || _errno == EACCES) {
346 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
347 HOSTAPD_LEVEL_INFO,
348 "Send failed - maybe interface status changed -"
349 " try to connect again");
350 if (msg_type == RADIUS_ACCT ||
351 msg_type == RADIUS_ACCT_INTERIM) {
352 radius_client_init_acct(radius);
353 return 0;
354 } else {
355 radius_client_init_auth(radius);
356 return 1;
357 }
358 }
359 #endif /* CONFIG_NATIVE_WINDOWS */
360
361 return 0;
362 }
363
364
radius_client_retransmit(struct radius_client_data * radius,struct radius_msg_list * entry,os_time_t now)365 static int radius_client_retransmit(struct radius_client_data *radius,
366 struct radius_msg_list *entry,
367 os_time_t now)
368 {
369 struct hostapd_radius_servers *conf = radius->conf;
370 int s;
371 struct wpabuf *buf;
372 size_t prev_num_msgs;
373 u8 *acct_delay_time;
374 size_t acct_delay_time_len;
375 int num_servers;
376
377 if (entry->msg_type == RADIUS_ACCT ||
378 entry->msg_type == RADIUS_ACCT_INTERIM) {
379 num_servers = conf->num_acct_servers;
380 if (radius->acct_sock < 0)
381 radius_client_init_acct(radius);
382 if (radius->acct_sock < 0 && conf->num_acct_servers > 1) {
383 prev_num_msgs = radius->num_msgs;
384 radius_client_acct_failover(radius);
385 if (prev_num_msgs != radius->num_msgs)
386 return 0;
387 }
388 s = radius->acct_sock;
389 if (entry->attempts == 0)
390 conf->acct_server->requests++;
391 else {
392 conf->acct_server->timeouts++;
393 conf->acct_server->retransmissions++;
394 }
395 } else {
396 num_servers = conf->num_auth_servers;
397 if (radius->auth_sock < 0)
398 radius_client_init_auth(radius);
399 if (radius->auth_sock < 0 && conf->num_auth_servers > 1) {
400 prev_num_msgs = radius->num_msgs;
401 radius_client_auth_failover(radius);
402 if (prev_num_msgs != radius->num_msgs)
403 return 0;
404 }
405 s = radius->auth_sock;
406 if (entry->attempts == 0)
407 conf->auth_server->requests++;
408 else {
409 conf->auth_server->timeouts++;
410 conf->auth_server->retransmissions++;
411 }
412 }
413
414 if (entry->msg_type == RADIUS_ACCT_INTERIM) {
415 wpa_printf(MSG_DEBUG,
416 "RADIUS: Failed to transmit interim accounting update to "
417 MACSTR " - drop message and request a new update",
418 MAC2STR(entry->addr));
419 if (radius->interim_error_cb)
420 radius->interim_error_cb(entry->addr,
421 radius->interim_error_cb_ctx);
422 return 1;
423 }
424
425 if (s < 0) {
426 wpa_printf(MSG_INFO,
427 "RADIUS: No valid socket for retransmission");
428 return 1;
429 }
430
431 if (entry->msg_type == RADIUS_ACCT &&
432 radius_msg_get_attr_ptr(entry->msg, RADIUS_ATTR_ACCT_DELAY_TIME,
433 &acct_delay_time, &acct_delay_time_len,
434 NULL) == 0 &&
435 acct_delay_time_len == 4) {
436 struct radius_hdr *hdr;
437 u32 delay_time;
438
439 /*
440 * Need to assign a new identifier since attribute contents
441 * changes.
442 */
443 hdr = radius_msg_get_hdr(entry->msg);
444 hdr->identifier = radius_client_get_id(radius);
445
446 /* Update Acct-Delay-Time to show wait time in queue */
447 delay_time = now - entry->first_try;
448 WPA_PUT_BE32(acct_delay_time, delay_time);
449
450 wpa_printf(MSG_DEBUG,
451 "RADIUS: Updated Acct-Delay-Time to %u for retransmission",
452 delay_time);
453 radius_msg_finish_acct(entry->msg, entry->shared_secret,
454 entry->shared_secret_len);
455 if (radius->conf->msg_dumps)
456 radius_msg_dump(entry->msg);
457 }
458
459 /* retransmit; remove entry if too many attempts */
460 if (entry->accu_attempts >= RADIUS_CLIENT_MAX_FAILOVER *
461 RADIUS_CLIENT_NUM_FAILOVER * num_servers) {
462 wpa_printf(MSG_INFO,
463 "RADIUS: Removing un-ACKed message due to too many failed retransmit attempts");
464 return 1;
465 }
466
467 entry->attempts++;
468 entry->accu_attempts++;
469 hostapd_logger(radius->ctx, entry->addr, HOSTAPD_MODULE_RADIUS,
470 HOSTAPD_LEVEL_DEBUG, "Resending RADIUS message (id=%d)",
471 radius_msg_get_hdr(entry->msg)->identifier);
472
473 os_get_reltime(&entry->last_attempt);
474 buf = radius_msg_get_buf(entry->msg);
475 if (send(s, wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
476 if (radius_client_handle_send_error(radius, s, entry->msg_type)
477 > 0)
478 return 0;
479 }
480
481 entry->next_try = now + entry->next_wait;
482 entry->next_wait *= 2;
483 if (entry->next_wait > RADIUS_CLIENT_MAX_WAIT)
484 entry->next_wait = RADIUS_CLIENT_MAX_WAIT;
485
486 return 0;
487 }
488
489
radius_client_timer(void * eloop_ctx,void * timeout_ctx)490 static void radius_client_timer(void *eloop_ctx, void *timeout_ctx)
491 {
492 struct radius_client_data *radius = eloop_ctx;
493 struct os_reltime now;
494 os_time_t first;
495 struct radius_msg_list *entry, *prev, *tmp;
496 int auth_failover = 0, acct_failover = 0;
497 size_t prev_num_msgs;
498 int s;
499
500 entry = radius->msgs;
501 if (!entry)
502 return;
503
504 os_get_reltime(&now);
505
506 while (entry) {
507 if (now.sec >= entry->next_try) {
508 s = entry->msg_type == RADIUS_AUTH ? radius->auth_sock :
509 radius->acct_sock;
510 if (entry->attempts >= RADIUS_CLIENT_NUM_FAILOVER ||
511 (s < 0 && entry->attempts > 0)) {
512 if (entry->msg_type == RADIUS_ACCT ||
513 entry->msg_type == RADIUS_ACCT_INTERIM)
514 acct_failover++;
515 else
516 auth_failover++;
517 }
518 }
519 entry = entry->next;
520 }
521
522 if (auth_failover)
523 radius_client_auth_failover(radius);
524
525 if (acct_failover)
526 radius_client_acct_failover(radius);
527
528 entry = radius->msgs;
529 first = 0;
530
531 prev = NULL;
532 while (entry) {
533 prev_num_msgs = radius->num_msgs;
534 if (now.sec >= entry->next_try &&
535 radius_client_retransmit(radius, entry, now.sec)) {
536 if (prev)
537 prev->next = entry->next;
538 else
539 radius->msgs = entry->next;
540
541 tmp = entry;
542 entry = entry->next;
543 radius_client_msg_free(tmp);
544 radius->num_msgs--;
545 continue;
546 }
547
548 if (prev_num_msgs != radius->num_msgs) {
549 wpa_printf(MSG_DEBUG,
550 "RADIUS: Message removed from queue - restart from beginning");
551 entry = radius->msgs;
552 prev = NULL;
553 continue;
554 }
555
556 if (first == 0 || entry->next_try < first)
557 first = entry->next_try;
558
559 prev = entry;
560 entry = entry->next;
561 }
562
563 if (radius->msgs) {
564 if (first < now.sec)
565 first = now.sec;
566 eloop_cancel_timeout(radius_client_timer, radius, NULL);
567 eloop_register_timeout(first - now.sec, 0,
568 radius_client_timer, radius, NULL);
569 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
570 HOSTAPD_LEVEL_DEBUG, "Next RADIUS client "
571 "retransmit in %ld seconds",
572 (long int) (first - now.sec));
573 }
574 }
575
576
radius_client_auth_failover(struct radius_client_data * radius)577 static void radius_client_auth_failover(struct radius_client_data *radius)
578 {
579 struct hostapd_radius_servers *conf = radius->conf;
580 struct hostapd_radius_server *next, *old;
581 struct radius_msg_list *entry;
582 char abuf[50];
583
584 old = conf->auth_server;
585 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
586 HOSTAPD_LEVEL_NOTICE,
587 "No response from Authentication server %s:%d - failover",
588 hostapd_ip_txt(&old->addr, abuf, sizeof(abuf)),
589 old->port);
590
591 for (entry = radius->msgs; entry; entry = entry->next) {
592 if (entry->msg_type == RADIUS_AUTH)
593 old->timeouts++;
594 }
595
596 next = old + 1;
597 if (next > &(conf->auth_servers[conf->num_auth_servers - 1]))
598 next = conf->auth_servers;
599 conf->auth_server = next;
600 radius_change_server(radius, next, old,
601 radius->auth_serv_sock,
602 radius->auth_serv_sock6, 1);
603 }
604
605
radius_client_acct_failover(struct radius_client_data * radius)606 static void radius_client_acct_failover(struct radius_client_data *radius)
607 {
608 struct hostapd_radius_servers *conf = radius->conf;
609 struct hostapd_radius_server *next, *old;
610 struct radius_msg_list *entry;
611 char abuf[50];
612
613 old = conf->acct_server;
614 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
615 HOSTAPD_LEVEL_NOTICE,
616 "No response from Accounting server %s:%d - failover",
617 hostapd_ip_txt(&old->addr, abuf, sizeof(abuf)),
618 old->port);
619
620 for (entry = radius->msgs; entry; entry = entry->next) {
621 if (entry->msg_type == RADIUS_ACCT ||
622 entry->msg_type == RADIUS_ACCT_INTERIM)
623 old->timeouts++;
624 }
625
626 next = old + 1;
627 if (next > &conf->acct_servers[conf->num_acct_servers - 1])
628 next = conf->acct_servers;
629 conf->acct_server = next;
630 radius_change_server(radius, next, old,
631 radius->acct_serv_sock,
632 radius->acct_serv_sock6, 0);
633 }
634
635
radius_client_update_timeout(struct radius_client_data * radius)636 static void radius_client_update_timeout(struct radius_client_data *radius)
637 {
638 struct os_reltime now;
639 os_time_t first;
640 struct radius_msg_list *entry;
641
642 eloop_cancel_timeout(radius_client_timer, radius, NULL);
643
644 if (radius->msgs == NULL) {
645 return;
646 }
647
648 first = 0;
649 for (entry = radius->msgs; entry; entry = entry->next) {
650 if (first == 0 || entry->next_try < first)
651 first = entry->next_try;
652 }
653
654 os_get_reltime(&now);
655 if (first < now.sec)
656 first = now.sec;
657 eloop_register_timeout(first - now.sec, 0, radius_client_timer, radius,
658 NULL);
659 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
660 HOSTAPD_LEVEL_DEBUG, "Next RADIUS client retransmit in"
661 " %ld seconds", (long int) (first - now.sec));
662 }
663
664
radius_client_list_add(struct radius_client_data * radius,struct radius_msg * msg,RadiusType msg_type,const u8 * shared_secret,size_t shared_secret_len,const u8 * addr)665 static void radius_client_list_add(struct radius_client_data *radius,
666 struct radius_msg *msg,
667 RadiusType msg_type,
668 const u8 *shared_secret,
669 size_t shared_secret_len, const u8 *addr)
670 {
671 struct radius_msg_list *entry, *prev;
672
673 if (eloop_terminated()) {
674 /* No point in adding entries to retransmit queue since event
675 * loop has already been terminated. */
676 radius_msg_free(msg);
677 return;
678 }
679
680 entry = os_zalloc(sizeof(*entry));
681 if (entry == NULL) {
682 wpa_printf(MSG_INFO, "RADIUS: Failed to add packet into retransmit list");
683 radius_msg_free(msg);
684 return;
685 }
686
687 if (addr)
688 os_memcpy(entry->addr, addr, ETH_ALEN);
689 entry->msg = msg;
690 entry->msg_type = msg_type;
691 entry->shared_secret = shared_secret;
692 entry->shared_secret_len = shared_secret_len;
693 os_get_reltime(&entry->last_attempt);
694 entry->first_try = entry->last_attempt.sec;
695 entry->next_try = entry->first_try + RADIUS_CLIENT_FIRST_WAIT;
696 entry->attempts = 1;
697 entry->accu_attempts = 1;
698 entry->next_wait = RADIUS_CLIENT_FIRST_WAIT * 2;
699 if (entry->next_wait > RADIUS_CLIENT_MAX_WAIT)
700 entry->next_wait = RADIUS_CLIENT_MAX_WAIT;
701 entry->next = radius->msgs;
702 radius->msgs = entry;
703 radius_client_update_timeout(radius);
704
705 if (radius->num_msgs >= RADIUS_CLIENT_MAX_ENTRIES) {
706 wpa_printf(MSG_INFO, "RADIUS: Removing the oldest un-ACKed packet due to retransmit list limits");
707 prev = NULL;
708 while (entry->next) {
709 prev = entry;
710 entry = entry->next;
711 }
712 if (prev) {
713 prev->next = NULL;
714 radius_client_msg_free(entry);
715 }
716 } else
717 radius->num_msgs++;
718 }
719
720
721 /**
722 * radius_client_send - Send a RADIUS request
723 * @radius: RADIUS client context from radius_client_init()
724 * @msg: RADIUS message to be sent
725 * @msg_type: Message type (RADIUS_AUTH, RADIUS_ACCT, RADIUS_ACCT_INTERIM)
726 * @addr: MAC address of the device related to this message or %NULL
727 * Returns: 0 on success, -1 on failure
728 *
729 * This function is used to transmit a RADIUS authentication (RADIUS_AUTH) or
730 * accounting request (RADIUS_ACCT or RADIUS_ACCT_INTERIM). The only difference
731 * between accounting and interim accounting messages is that the interim
732 * message will not be retransmitted. Instead, a callback is used to indicate
733 * that the transmission failed for the specific station @addr so that a new
734 * interim accounting update message can be generated with up-to-date session
735 * data instead of trying to resend old information.
736 *
737 * The message is added on the retransmission queue and will be retransmitted
738 * automatically until a response is received or maximum number of retries
739 * (RADIUS_CLIENT_MAX_FAILOVER * RADIUS_CLIENT_NUM_FAILOVER) is reached. No
740 * such retries are used with RADIUS_ACCT_INTERIM, i.e., such a pending message
741 * is removed from the queue automatically on transmission failure.
742 *
743 * The related device MAC address can be used to identify pending messages that
744 * can be removed with radius_client_flush_auth().
745 */
radius_client_send(struct radius_client_data * radius,struct radius_msg * msg,RadiusType msg_type,const u8 * addr)746 int radius_client_send(struct radius_client_data *radius,
747 struct radius_msg *msg, RadiusType msg_type,
748 const u8 *addr)
749 {
750 struct hostapd_radius_servers *conf = radius->conf;
751 const u8 *shared_secret;
752 size_t shared_secret_len;
753 char *name;
754 int s, res;
755 struct wpabuf *buf;
756
757 if (msg_type == RADIUS_ACCT || msg_type == RADIUS_ACCT_INTERIM) {
758 if (conf->acct_server && radius->acct_sock < 0)
759 radius_client_init_acct(radius);
760
761 if (conf->acct_server == NULL || radius->acct_sock < 0 ||
762 conf->acct_server->shared_secret == NULL) {
763 hostapd_logger(radius->ctx, NULL,
764 HOSTAPD_MODULE_RADIUS,
765 HOSTAPD_LEVEL_INFO,
766 "No accounting server configured");
767 return -1;
768 }
769 shared_secret = conf->acct_server->shared_secret;
770 shared_secret_len = conf->acct_server->shared_secret_len;
771 radius_msg_finish_acct(msg, shared_secret, shared_secret_len);
772 name = "accounting";
773 s = radius->acct_sock;
774 conf->acct_server->requests++;
775 } else {
776 if (conf->auth_server && radius->auth_sock < 0)
777 radius_client_init_auth(radius);
778
779 if (conf->auth_server == NULL || radius->auth_sock < 0 ||
780 conf->auth_server->shared_secret == NULL) {
781 hostapd_logger(radius->ctx, NULL,
782 HOSTAPD_MODULE_RADIUS,
783 HOSTAPD_LEVEL_INFO,
784 "No authentication server configured");
785 return -1;
786 }
787 shared_secret = conf->auth_server->shared_secret;
788 shared_secret_len = conf->auth_server->shared_secret_len;
789 radius_msg_finish(msg, shared_secret, shared_secret_len);
790 name = "authentication";
791 s = radius->auth_sock;
792 conf->auth_server->requests++;
793 }
794
795 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
796 HOSTAPD_LEVEL_DEBUG, "Sending RADIUS message to %s "
797 "server", name);
798 if (conf->msg_dumps)
799 radius_msg_dump(msg);
800
801 buf = radius_msg_get_buf(msg);
802 res = send(s, wpabuf_head(buf), wpabuf_len(buf), 0);
803 if (res < 0)
804 radius_client_handle_send_error(radius, s, msg_type);
805
806 radius_client_list_add(radius, msg, msg_type, shared_secret,
807 shared_secret_len, addr);
808
809 return 0;
810 }
811
812
radius_client_receive(int sock,void * eloop_ctx,void * sock_ctx)813 static void radius_client_receive(int sock, void *eloop_ctx, void *sock_ctx)
814 {
815 struct radius_client_data *radius = eloop_ctx;
816 struct hostapd_radius_servers *conf = radius->conf;
817 RadiusType msg_type = (RadiusType) sock_ctx;
818 int len, roundtrip;
819 unsigned char buf[RADIUS_MAX_MSG_LEN];
820 struct msghdr msghdr = {0};
821 struct iovec iov;
822 struct radius_msg *msg;
823 struct radius_hdr *hdr;
824 struct radius_rx_handler *handlers;
825 size_t num_handlers, i;
826 struct radius_msg_list *req, *prev_req;
827 struct os_reltime now;
828 struct hostapd_radius_server *rconf;
829 int invalid_authenticator = 0;
830
831 if (msg_type == RADIUS_ACCT) {
832 handlers = radius->acct_handlers;
833 num_handlers = radius->num_acct_handlers;
834 rconf = conf->acct_server;
835 } else {
836 handlers = radius->auth_handlers;
837 num_handlers = radius->num_auth_handlers;
838 rconf = conf->auth_server;
839 }
840
841 iov.iov_base = buf;
842 iov.iov_len = RADIUS_MAX_MSG_LEN;
843 msghdr.msg_iov = &iov;
844 msghdr.msg_iovlen = 1;
845 msghdr.msg_flags = 0;
846 len = recvmsg(sock, &msghdr, MSG_DONTWAIT);
847 if (len < 0) {
848 wpa_printf(MSG_INFO, "recvmsg[RADIUS]: %s", strerror(errno));
849 return;
850 }
851
852 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
853 HOSTAPD_LEVEL_DEBUG, "Received %d bytes from RADIUS "
854 "server", len);
855
856 if (msghdr.msg_flags & MSG_TRUNC) {
857 wpa_printf(MSG_INFO, "RADIUS: Possibly too long UDP frame for our buffer - dropping it");
858 return;
859 }
860
861 msg = radius_msg_parse(buf, len);
862 if (msg == NULL) {
863 wpa_printf(MSG_INFO, "RADIUS: Parsing incoming frame failed");
864 rconf->malformed_responses++;
865 return;
866 }
867 hdr = radius_msg_get_hdr(msg);
868
869 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
870 HOSTAPD_LEVEL_DEBUG, "Received RADIUS message");
871 if (conf->msg_dumps)
872 radius_msg_dump(msg);
873
874 switch (hdr->code) {
875 case RADIUS_CODE_ACCESS_ACCEPT:
876 rconf->access_accepts++;
877 break;
878 case RADIUS_CODE_ACCESS_REJECT:
879 rconf->access_rejects++;
880 break;
881 case RADIUS_CODE_ACCESS_CHALLENGE:
882 rconf->access_challenges++;
883 break;
884 case RADIUS_CODE_ACCOUNTING_RESPONSE:
885 rconf->responses++;
886 break;
887 }
888
889 prev_req = NULL;
890 req = radius->msgs;
891 while (req) {
892 /* TODO: also match by src addr:port of the packet when using
893 * alternative RADIUS servers (?) */
894 if ((req->msg_type == msg_type ||
895 (req->msg_type == RADIUS_ACCT_INTERIM &&
896 msg_type == RADIUS_ACCT)) &&
897 radius_msg_get_hdr(req->msg)->identifier ==
898 hdr->identifier)
899 break;
900
901 prev_req = req;
902 req = req->next;
903 }
904
905 if (req == NULL) {
906 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
907 HOSTAPD_LEVEL_DEBUG,
908 "No matching RADIUS request found (type=%d "
909 "id=%d) - dropping packet",
910 msg_type, hdr->identifier);
911 goto fail;
912 }
913
914 os_get_reltime(&now);
915 roundtrip = (now.sec - req->last_attempt.sec) * 100 +
916 (now.usec - req->last_attempt.usec) / 10000;
917 hostapd_logger(radius->ctx, req->addr, HOSTAPD_MODULE_RADIUS,
918 HOSTAPD_LEVEL_DEBUG,
919 "Received RADIUS packet matched with a pending "
920 "request, round trip time %d.%02d sec",
921 roundtrip / 100, roundtrip % 100);
922 rconf->round_trip_time = roundtrip;
923
924 /* Remove ACKed RADIUS packet from retransmit list */
925 if (prev_req)
926 prev_req->next = req->next;
927 else
928 radius->msgs = req->next;
929 radius->num_msgs--;
930
931 for (i = 0; i < num_handlers; i++) {
932 RadiusRxResult res;
933 res = handlers[i].handler(msg, req->msg, req->shared_secret,
934 req->shared_secret_len,
935 handlers[i].data);
936 switch (res) {
937 case RADIUS_RX_PROCESSED:
938 radius_msg_free(msg);
939 /* fall through */
940 case RADIUS_RX_QUEUED:
941 radius_client_msg_free(req);
942 return;
943 case RADIUS_RX_INVALID_AUTHENTICATOR:
944 invalid_authenticator++;
945 /* fall through */
946 case RADIUS_RX_UNKNOWN:
947 /* continue with next handler */
948 break;
949 }
950 }
951
952 if (invalid_authenticator)
953 rconf->bad_authenticators++;
954 else
955 rconf->unknown_types++;
956 hostapd_logger(radius->ctx, req->addr, HOSTAPD_MODULE_RADIUS,
957 HOSTAPD_LEVEL_DEBUG, "No RADIUS RX handler found "
958 "(type=%d code=%d id=%d)%s - dropping packet",
959 msg_type, hdr->code, hdr->identifier,
960 invalid_authenticator ? " [INVALID AUTHENTICATOR]" :
961 "");
962 radius_client_msg_free(req);
963
964 fail:
965 radius_msg_free(msg);
966 }
967
968
969 /**
970 * radius_client_get_id - Get an identifier for a new RADIUS message
971 * @radius: RADIUS client context from radius_client_init()
972 * Returns: Allocated identifier
973 *
974 * This function is used to fetch a unique (among pending requests) identifier
975 * for a new RADIUS message.
976 */
radius_client_get_id(struct radius_client_data * radius)977 u8 radius_client_get_id(struct radius_client_data *radius)
978 {
979 struct radius_msg_list *entry, *prev, *_remove;
980 u8 id = radius->next_radius_identifier++;
981
982 /* remove entries with matching id from retransmit list to avoid
983 * using new reply from the RADIUS server with an old request */
984 entry = radius->msgs;
985 prev = NULL;
986 while (entry) {
987 if (radius_msg_get_hdr(entry->msg)->identifier == id) {
988 hostapd_logger(radius->ctx, entry->addr,
989 HOSTAPD_MODULE_RADIUS,
990 HOSTAPD_LEVEL_DEBUG,
991 "Removing pending RADIUS message, "
992 "since its id (%d) is reused", id);
993 if (prev)
994 prev->next = entry->next;
995 else
996 radius->msgs = entry->next;
997 _remove = entry;
998 } else {
999 _remove = NULL;
1000 prev = entry;
1001 }
1002 entry = entry->next;
1003
1004 if (_remove)
1005 radius_client_msg_free(_remove);
1006 }
1007
1008 return id;
1009 }
1010
1011
1012 /**
1013 * radius_client_flush - Flush all pending RADIUS client messages
1014 * @radius: RADIUS client context from radius_client_init()
1015 * @only_auth: Whether only authentication messages are removed
1016 */
radius_client_flush(struct radius_client_data * radius,int only_auth)1017 void radius_client_flush(struct radius_client_data *radius, int only_auth)
1018 {
1019 struct radius_msg_list *entry, *prev, *tmp;
1020
1021 if (!radius)
1022 return;
1023
1024 prev = NULL;
1025 entry = radius->msgs;
1026
1027 while (entry) {
1028 if (!only_auth || entry->msg_type == RADIUS_AUTH) {
1029 if (prev)
1030 prev->next = entry->next;
1031 else
1032 radius->msgs = entry->next;
1033
1034 tmp = entry;
1035 entry = entry->next;
1036 radius_client_msg_free(tmp);
1037 radius->num_msgs--;
1038 } else {
1039 prev = entry;
1040 entry = entry->next;
1041 }
1042 }
1043
1044 if (radius->msgs == NULL)
1045 eloop_cancel_timeout(radius_client_timer, radius, NULL);
1046 }
1047
1048
radius_client_update_acct_msgs(struct radius_client_data * radius,const u8 * shared_secret,size_t shared_secret_len)1049 static void radius_client_update_acct_msgs(struct radius_client_data *radius,
1050 const u8 *shared_secret,
1051 size_t shared_secret_len)
1052 {
1053 struct radius_msg_list *entry;
1054
1055 if (!radius)
1056 return;
1057
1058 for (entry = radius->msgs; entry; entry = entry->next) {
1059 if (entry->msg_type == RADIUS_ACCT) {
1060 entry->shared_secret = shared_secret;
1061 entry->shared_secret_len = shared_secret_len;
1062 radius_msg_finish_acct(entry->msg, shared_secret,
1063 shared_secret_len);
1064 }
1065 }
1066 }
1067
1068
1069 static int
radius_change_server(struct radius_client_data * radius,struct hostapd_radius_server * nserv,struct hostapd_radius_server * oserv,int sock,int sock6,int auth)1070 radius_change_server(struct radius_client_data *radius,
1071 struct hostapd_radius_server *nserv,
1072 struct hostapd_radius_server *oserv,
1073 int sock, int sock6, int auth)
1074 {
1075 struct sockaddr_in serv, claddr;
1076 #ifdef CONFIG_IPV6
1077 struct sockaddr_in6 serv6, claddr6;
1078 #endif /* CONFIG_IPV6 */
1079 struct sockaddr *addr, *cl_addr;
1080 socklen_t addrlen, claddrlen;
1081 char abuf[50];
1082 int sel_sock;
1083 struct radius_msg_list *entry;
1084 struct hostapd_radius_servers *conf = radius->conf;
1085 struct sockaddr_in disconnect_addr = {
1086 .sin_family = AF_UNSPEC,
1087 };
1088
1089 hostapd_logger(radius->ctx, NULL, HOSTAPD_MODULE_RADIUS,
1090 HOSTAPD_LEVEL_INFO,
1091 "%s server %s:%d",
1092 auth ? "Authentication" : "Accounting",
1093 hostapd_ip_txt(&nserv->addr, abuf, sizeof(abuf)),
1094 nserv->port);
1095
1096 if (oserv && oserv == nserv) {
1097 /* Reconnect to same server, flush */
1098 if (auth)
1099 radius_client_flush(radius, 1);
1100 }
1101
1102 if (oserv && oserv != nserv &&
1103 (nserv->shared_secret_len != oserv->shared_secret_len ||
1104 os_memcmp(nserv->shared_secret, oserv->shared_secret,
1105 nserv->shared_secret_len) != 0)) {
1106 /* Pending RADIUS packets used different shared secret, so
1107 * they need to be modified. Update accounting message
1108 * authenticators here. Authentication messages are removed
1109 * since they would require more changes and the new RADIUS
1110 * server may not be prepared to receive them anyway due to
1111 * missing state information. Client will likely retry
1112 * authentication, so this should not be an issue. */
1113 if (auth)
1114 radius_client_flush(radius, 1);
1115 else {
1116 radius_client_update_acct_msgs(
1117 radius, nserv->shared_secret,
1118 nserv->shared_secret_len);
1119 }
1120 }
1121
1122 /* Reset retry counters */
1123 for (entry = radius->msgs; oserv && entry; entry = entry->next) {
1124 if ((auth && entry->msg_type != RADIUS_AUTH) ||
1125 (!auth && entry->msg_type != RADIUS_ACCT))
1126 continue;
1127 entry->next_try = entry->first_try + RADIUS_CLIENT_FIRST_WAIT;
1128 entry->attempts = 0;
1129 entry->next_wait = RADIUS_CLIENT_FIRST_WAIT * 2;
1130 }
1131
1132 if (radius->msgs) {
1133 eloop_cancel_timeout(radius_client_timer, radius, NULL);
1134 eloop_register_timeout(RADIUS_CLIENT_FIRST_WAIT, 0,
1135 radius_client_timer, radius, NULL);
1136 }
1137
1138 switch (nserv->addr.af) {
1139 case AF_INET:
1140 os_memset(&serv, 0, sizeof(serv));
1141 serv.sin_family = AF_INET;
1142 serv.sin_addr.s_addr = nserv->addr.u.v4.s_addr;
1143 serv.sin_port = htons(nserv->port);
1144 addr = (struct sockaddr *) &serv;
1145 addrlen = sizeof(serv);
1146 sel_sock = sock;
1147 break;
1148 #ifdef CONFIG_IPV6
1149 case AF_INET6:
1150 os_memset(&serv6, 0, sizeof(serv6));
1151 serv6.sin6_family = AF_INET6;
1152 os_memcpy(&serv6.sin6_addr, &nserv->addr.u.v6,
1153 sizeof(struct in6_addr));
1154 serv6.sin6_port = htons(nserv->port);
1155 addr = (struct sockaddr *) &serv6;
1156 addrlen = sizeof(serv6);
1157 sel_sock = sock6;
1158 break;
1159 #endif /* CONFIG_IPV6 */
1160 default:
1161 return -1;
1162 }
1163
1164 if (sel_sock < 0) {
1165 wpa_printf(MSG_INFO,
1166 "RADIUS: No server socket available (af=%d sock=%d sock6=%d auth=%d",
1167 nserv->addr.af, sock, sock6, auth);
1168 return -1;
1169 }
1170
1171 if (conf->force_client_addr) {
1172 switch (conf->client_addr.af) {
1173 case AF_INET:
1174 os_memset(&claddr, 0, sizeof(claddr));
1175 claddr.sin_family = AF_INET;
1176 claddr.sin_addr.s_addr = conf->client_addr.u.v4.s_addr;
1177 claddr.sin_port = htons(0);
1178 cl_addr = (struct sockaddr *) &claddr;
1179 claddrlen = sizeof(claddr);
1180 break;
1181 #ifdef CONFIG_IPV6
1182 case AF_INET6:
1183 os_memset(&claddr6, 0, sizeof(claddr6));
1184 claddr6.sin6_family = AF_INET6;
1185 os_memcpy(&claddr6.sin6_addr, &conf->client_addr.u.v6,
1186 sizeof(struct in6_addr));
1187 claddr6.sin6_port = htons(0);
1188 cl_addr = (struct sockaddr *) &claddr6;
1189 claddrlen = sizeof(claddr6);
1190 break;
1191 #endif /* CONFIG_IPV6 */
1192 default:
1193 return -1;
1194 }
1195
1196 if (bind(sel_sock, cl_addr, claddrlen) < 0) {
1197 wpa_printf(MSG_INFO, "bind[radius]: %s",
1198 strerror(errno));
1199 return -1;
1200 }
1201 }
1202
1203 /* Force a reconnect by disconnecting the socket first */
1204 if (connect(sel_sock, (struct sockaddr *) &disconnect_addr,
1205 sizeof(disconnect_addr)) < 0)
1206 wpa_printf(MSG_INFO, "disconnect[radius]: %s", strerror(errno));
1207
1208 if (connect(sel_sock, addr, addrlen) < 0) {
1209 wpa_printf(MSG_INFO, "connect[radius]: %s", strerror(errno));
1210 return -1;
1211 }
1212
1213 #ifndef CONFIG_NATIVE_WINDOWS
1214 switch (nserv->addr.af) {
1215 case AF_INET:
1216 claddrlen = sizeof(claddr);
1217 if (getsockname(sel_sock, (struct sockaddr *) &claddr,
1218 &claddrlen) == 0) {
1219 wpa_printf(MSG_DEBUG, "RADIUS local address: %s:%u",
1220 inet_ntoa(claddr.sin_addr),
1221 ntohs(claddr.sin_port));
1222 }
1223 break;
1224 #ifdef CONFIG_IPV6
1225 case AF_INET6: {
1226 claddrlen = sizeof(claddr6);
1227 if (getsockname(sel_sock, (struct sockaddr *) &claddr6,
1228 &claddrlen) == 0) {
1229 wpa_printf(MSG_DEBUG, "RADIUS local address: %s:%u",
1230 inet_ntop(AF_INET6, &claddr6.sin6_addr,
1231 abuf, sizeof(abuf)),
1232 ntohs(claddr6.sin6_port));
1233 }
1234 break;
1235 }
1236 #endif /* CONFIG_IPV6 */
1237 }
1238 #endif /* CONFIG_NATIVE_WINDOWS */
1239
1240 if (auth)
1241 radius->auth_sock = sel_sock;
1242 else
1243 radius->acct_sock = sel_sock;
1244
1245 return 0;
1246 }
1247
1248
radius_retry_primary_timer(void * eloop_ctx,void * timeout_ctx)1249 static void radius_retry_primary_timer(void *eloop_ctx, void *timeout_ctx)
1250 {
1251 struct radius_client_data *radius = eloop_ctx;
1252 struct hostapd_radius_servers *conf = radius->conf;
1253 struct hostapd_radius_server *oserv;
1254
1255 if (radius->auth_sock >= 0 && conf->auth_servers &&
1256 conf->auth_server != conf->auth_servers) {
1257 oserv = conf->auth_server;
1258 conf->auth_server = conf->auth_servers;
1259 if (radius_change_server(radius, conf->auth_server, oserv,
1260 radius->auth_serv_sock,
1261 radius->auth_serv_sock6, 1) < 0) {
1262 conf->auth_server = oserv;
1263 radius_change_server(radius, oserv, conf->auth_server,
1264 radius->auth_serv_sock,
1265 radius->auth_serv_sock6, 1);
1266 }
1267 }
1268
1269 if (radius->acct_sock >= 0 && conf->acct_servers &&
1270 conf->acct_server != conf->acct_servers) {
1271 oserv = conf->acct_server;
1272 conf->acct_server = conf->acct_servers;
1273 if (radius_change_server(radius, conf->acct_server, oserv,
1274 radius->acct_serv_sock,
1275 radius->acct_serv_sock6, 0) < 0) {
1276 conf->acct_server = oserv;
1277 radius_change_server(radius, oserv, conf->acct_server,
1278 radius->acct_serv_sock,
1279 radius->acct_serv_sock6, 0);
1280 }
1281 }
1282
1283 if (conf->retry_primary_interval)
1284 eloop_register_timeout(conf->retry_primary_interval, 0,
1285 radius_retry_primary_timer, radius,
1286 NULL);
1287 }
1288
1289
radius_client_disable_pmtu_discovery(int s)1290 static int radius_client_disable_pmtu_discovery(int s)
1291 {
1292 int r = -1;
1293 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1294 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
1295 int action = IP_PMTUDISC_DONT;
1296 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
1297 sizeof(action));
1298 if (r == -1)
1299 wpa_printf(MSG_ERROR, "RADIUS: Failed to set IP_MTU_DISCOVER: %s",
1300 strerror(errno));
1301 #endif
1302 return r;
1303 }
1304
1305
radius_close_auth_sockets(struct radius_client_data * radius)1306 static void radius_close_auth_sockets(struct radius_client_data *radius)
1307 {
1308 radius->auth_sock = -1;
1309
1310 if (radius->auth_serv_sock >= 0) {
1311 eloop_unregister_read_sock(radius->auth_serv_sock);
1312 close(radius->auth_serv_sock);
1313 radius->auth_serv_sock = -1;
1314 }
1315 #ifdef CONFIG_IPV6
1316 if (radius->auth_serv_sock6 >= 0) {
1317 eloop_unregister_read_sock(radius->auth_serv_sock6);
1318 close(radius->auth_serv_sock6);
1319 radius->auth_serv_sock6 = -1;
1320 }
1321 #endif /* CONFIG_IPV6 */
1322 }
1323
1324
radius_close_acct_sockets(struct radius_client_data * radius)1325 static void radius_close_acct_sockets(struct radius_client_data *radius)
1326 {
1327 radius->acct_sock = -1;
1328
1329 if (radius->acct_serv_sock >= 0) {
1330 eloop_unregister_read_sock(radius->acct_serv_sock);
1331 close(radius->acct_serv_sock);
1332 radius->acct_serv_sock = -1;
1333 }
1334 #ifdef CONFIG_IPV6
1335 if (radius->acct_serv_sock6 >= 0) {
1336 eloop_unregister_read_sock(radius->acct_serv_sock6);
1337 close(radius->acct_serv_sock6);
1338 radius->acct_serv_sock6 = -1;
1339 }
1340 #endif /* CONFIG_IPV6 */
1341 }
1342
1343
radius_client_init_auth(struct radius_client_data * radius)1344 static int radius_client_init_auth(struct radius_client_data *radius)
1345 {
1346 struct hostapd_radius_servers *conf = radius->conf;
1347 int ok = 0;
1348
1349 radius_close_auth_sockets(radius);
1350
1351 radius->auth_serv_sock = socket(PF_INET, SOCK_DGRAM, 0);
1352 if (radius->auth_serv_sock < 0)
1353 wpa_printf(MSG_INFO, "RADIUS: socket[PF_INET,SOCK_DGRAM]: %s",
1354 strerror(errno));
1355 else {
1356 radius_client_disable_pmtu_discovery(radius->auth_serv_sock);
1357 ok++;
1358 }
1359
1360 #ifdef CONFIG_IPV6
1361 radius->auth_serv_sock6 = socket(PF_INET6, SOCK_DGRAM, 0);
1362 if (radius->auth_serv_sock6 < 0)
1363 wpa_printf(MSG_INFO, "RADIUS: socket[PF_INET6,SOCK_DGRAM]: %s",
1364 strerror(errno));
1365 else
1366 ok++;
1367 #endif /* CONFIG_IPV6 */
1368
1369 if (ok == 0)
1370 return -1;
1371
1372 radius_change_server(radius, conf->auth_server, NULL,
1373 radius->auth_serv_sock, radius->auth_serv_sock6,
1374 1);
1375
1376 if (radius->auth_serv_sock >= 0 &&
1377 eloop_register_read_sock(radius->auth_serv_sock,
1378 radius_client_receive, radius,
1379 (void *) RADIUS_AUTH)) {
1380 wpa_printf(MSG_INFO, "RADIUS: Could not register read socket for authentication server");
1381 radius_close_auth_sockets(radius);
1382 return -1;
1383 }
1384
1385 #ifdef CONFIG_IPV6
1386 if (radius->auth_serv_sock6 >= 0 &&
1387 eloop_register_read_sock(radius->auth_serv_sock6,
1388 radius_client_receive, radius,
1389 (void *) RADIUS_AUTH)) {
1390 wpa_printf(MSG_INFO, "RADIUS: Could not register read socket for authentication server");
1391 radius_close_auth_sockets(radius);
1392 return -1;
1393 }
1394 #endif /* CONFIG_IPV6 */
1395
1396 return 0;
1397 }
1398
1399
radius_client_init_acct(struct radius_client_data * radius)1400 static int radius_client_init_acct(struct radius_client_data *radius)
1401 {
1402 struct hostapd_radius_servers *conf = radius->conf;
1403 int ok = 0;
1404
1405 radius_close_acct_sockets(radius);
1406
1407 radius->acct_serv_sock = socket(PF_INET, SOCK_DGRAM, 0);
1408 if (radius->acct_serv_sock < 0)
1409 wpa_printf(MSG_INFO, "RADIUS: socket[PF_INET,SOCK_DGRAM]: %s",
1410 strerror(errno));
1411 else {
1412 radius_client_disable_pmtu_discovery(radius->acct_serv_sock);
1413 ok++;
1414 }
1415
1416 #ifdef CONFIG_IPV6
1417 radius->acct_serv_sock6 = socket(PF_INET6, SOCK_DGRAM, 0);
1418 if (radius->acct_serv_sock6 < 0)
1419 wpa_printf(MSG_INFO, "RADIUS: socket[PF_INET6,SOCK_DGRAM]: %s",
1420 strerror(errno));
1421 else
1422 ok++;
1423 #endif /* CONFIG_IPV6 */
1424
1425 if (ok == 0)
1426 return -1;
1427
1428 radius_change_server(radius, conf->acct_server, NULL,
1429 radius->acct_serv_sock, radius->acct_serv_sock6,
1430 0);
1431
1432 if (radius->acct_serv_sock >= 0 &&
1433 eloop_register_read_sock(radius->acct_serv_sock,
1434 radius_client_receive, radius,
1435 (void *) RADIUS_ACCT)) {
1436 wpa_printf(MSG_INFO, "RADIUS: Could not register read socket for accounting server");
1437 radius_close_acct_sockets(radius);
1438 return -1;
1439 }
1440
1441 #ifdef CONFIG_IPV6
1442 if (radius->acct_serv_sock6 >= 0 &&
1443 eloop_register_read_sock(radius->acct_serv_sock6,
1444 radius_client_receive, radius,
1445 (void *) RADIUS_ACCT)) {
1446 wpa_printf(MSG_INFO, "RADIUS: Could not register read socket for accounting server");
1447 radius_close_acct_sockets(radius);
1448 return -1;
1449 }
1450 #endif /* CONFIG_IPV6 */
1451
1452 return 0;
1453 }
1454
1455
1456 /**
1457 * radius_client_init - Initialize RADIUS client
1458 * @ctx: Callback context to be used in hostapd_logger() calls
1459 * @conf: RADIUS client configuration (RADIUS servers)
1460 * Returns: Pointer to private RADIUS client context or %NULL on failure
1461 *
1462 * The caller is responsible for keeping the configuration data available for
1463 * the lifetime of the RADIUS client, i.e., until radius_client_deinit() is
1464 * called for the returned context pointer.
1465 */
1466 struct radius_client_data *
radius_client_init(void * ctx,struct hostapd_radius_servers * conf)1467 radius_client_init(void *ctx, struct hostapd_radius_servers *conf)
1468 {
1469 struct radius_client_data *radius;
1470
1471 radius = os_zalloc(sizeof(struct radius_client_data));
1472 if (radius == NULL)
1473 return NULL;
1474
1475 radius->ctx = ctx;
1476 radius->conf = conf;
1477 radius->auth_serv_sock = radius->acct_serv_sock =
1478 radius->auth_serv_sock6 = radius->acct_serv_sock6 =
1479 radius->auth_sock = radius->acct_sock = -1;
1480
1481 if (conf->auth_server && radius_client_init_auth(radius)) {
1482 radius_client_deinit(radius);
1483 return NULL;
1484 }
1485
1486 if (conf->acct_server && radius_client_init_acct(radius)) {
1487 radius_client_deinit(radius);
1488 return NULL;
1489 }
1490
1491 if (conf->retry_primary_interval)
1492 eloop_register_timeout(conf->retry_primary_interval, 0,
1493 radius_retry_primary_timer, radius,
1494 NULL);
1495
1496 return radius;
1497 }
1498
1499
1500 /**
1501 * radius_client_deinit - Deinitialize RADIUS client
1502 * @radius: RADIUS client context from radius_client_init()
1503 */
radius_client_deinit(struct radius_client_data * radius)1504 void radius_client_deinit(struct radius_client_data *radius)
1505 {
1506 if (!radius)
1507 return;
1508
1509 radius_close_auth_sockets(radius);
1510 radius_close_acct_sockets(radius);
1511
1512 eloop_cancel_timeout(radius_retry_primary_timer, radius, NULL);
1513
1514 radius_client_flush(radius, 0);
1515 os_free(radius->auth_handlers);
1516 os_free(radius->acct_handlers);
1517 os_free(radius);
1518 }
1519
1520
1521 /**
1522 * radius_client_flush_auth - Flush pending RADIUS messages for an address
1523 * @radius: RADIUS client context from radius_client_init()
1524 * @addr: MAC address of the related device
1525 *
1526 * This function can be used to remove pending RADIUS authentication messages
1527 * that are related to a specific device. The addr parameter is matched with
1528 * the one used in radius_client_send() call that was used to transmit the
1529 * authentication request.
1530 */
radius_client_flush_auth(struct radius_client_data * radius,const u8 * addr)1531 void radius_client_flush_auth(struct radius_client_data *radius,
1532 const u8 *addr)
1533 {
1534 struct radius_msg_list *entry, *prev, *tmp;
1535
1536 prev = NULL;
1537 entry = radius->msgs;
1538 while (entry) {
1539 if (entry->msg_type == RADIUS_AUTH &&
1540 os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
1541 hostapd_logger(radius->ctx, addr,
1542 HOSTAPD_MODULE_RADIUS,
1543 HOSTAPD_LEVEL_DEBUG,
1544 "Removing pending RADIUS authentication"
1545 " message for removed client");
1546
1547 if (prev)
1548 prev->next = entry->next;
1549 else
1550 radius->msgs = entry->next;
1551
1552 tmp = entry;
1553 entry = entry->next;
1554 radius_client_msg_free(tmp);
1555 radius->num_msgs--;
1556 continue;
1557 }
1558
1559 prev = entry;
1560 entry = entry->next;
1561 }
1562 }
1563
1564
radius_client_dump_auth_server(char * buf,size_t buflen,struct hostapd_radius_server * serv,struct radius_client_data * cli)1565 static int radius_client_dump_auth_server(char *buf, size_t buflen,
1566 struct hostapd_radius_server *serv,
1567 struct radius_client_data *cli)
1568 {
1569 int pending = 0;
1570 struct radius_msg_list *msg;
1571 char abuf[50];
1572
1573 if (cli) {
1574 for (msg = cli->msgs; msg; msg = msg->next) {
1575 if (msg->msg_type == RADIUS_AUTH)
1576 pending++;
1577 }
1578 }
1579
1580 return os_snprintf(buf, buflen,
1581 "radiusAuthServerIndex=%d\n"
1582 "radiusAuthServerAddress=%s\n"
1583 "radiusAuthClientServerPortNumber=%d\n"
1584 "radiusAuthClientRoundTripTime=%d\n"
1585 "radiusAuthClientAccessRequests=%u\n"
1586 "radiusAuthClientAccessRetransmissions=%u\n"
1587 "radiusAuthClientAccessAccepts=%u\n"
1588 "radiusAuthClientAccessRejects=%u\n"
1589 "radiusAuthClientAccessChallenges=%u\n"
1590 "radiusAuthClientMalformedAccessResponses=%u\n"
1591 "radiusAuthClientBadAuthenticators=%u\n"
1592 "radiusAuthClientPendingRequests=%u\n"
1593 "radiusAuthClientTimeouts=%u\n"
1594 "radiusAuthClientUnknownTypes=%u\n"
1595 "radiusAuthClientPacketsDropped=%u\n",
1596 serv->index,
1597 hostapd_ip_txt(&serv->addr, abuf, sizeof(abuf)),
1598 serv->port,
1599 serv->round_trip_time,
1600 serv->requests,
1601 serv->retransmissions,
1602 serv->access_accepts,
1603 serv->access_rejects,
1604 serv->access_challenges,
1605 serv->malformed_responses,
1606 serv->bad_authenticators,
1607 pending,
1608 serv->timeouts,
1609 serv->unknown_types,
1610 serv->packets_dropped);
1611 }
1612
1613
radius_client_dump_acct_server(char * buf,size_t buflen,struct hostapd_radius_server * serv,struct radius_client_data * cli)1614 static int radius_client_dump_acct_server(char *buf, size_t buflen,
1615 struct hostapd_radius_server *serv,
1616 struct radius_client_data *cli)
1617 {
1618 int pending = 0;
1619 struct radius_msg_list *msg;
1620 char abuf[50];
1621
1622 if (cli) {
1623 for (msg = cli->msgs; msg; msg = msg->next) {
1624 if (msg->msg_type == RADIUS_ACCT ||
1625 msg->msg_type == RADIUS_ACCT_INTERIM)
1626 pending++;
1627 }
1628 }
1629
1630 return os_snprintf(buf, buflen,
1631 "radiusAccServerIndex=%d\n"
1632 "radiusAccServerAddress=%s\n"
1633 "radiusAccClientServerPortNumber=%d\n"
1634 "radiusAccClientRoundTripTime=%d\n"
1635 "radiusAccClientRequests=%u\n"
1636 "radiusAccClientRetransmissions=%u\n"
1637 "radiusAccClientResponses=%u\n"
1638 "radiusAccClientMalformedResponses=%u\n"
1639 "radiusAccClientBadAuthenticators=%u\n"
1640 "radiusAccClientPendingRequests=%u\n"
1641 "radiusAccClientTimeouts=%u\n"
1642 "radiusAccClientUnknownTypes=%u\n"
1643 "radiusAccClientPacketsDropped=%u\n",
1644 serv->index,
1645 hostapd_ip_txt(&serv->addr, abuf, sizeof(abuf)),
1646 serv->port,
1647 serv->round_trip_time,
1648 serv->requests,
1649 serv->retransmissions,
1650 serv->responses,
1651 serv->malformed_responses,
1652 serv->bad_authenticators,
1653 pending,
1654 serv->timeouts,
1655 serv->unknown_types,
1656 serv->packets_dropped);
1657 }
1658
1659
1660 /**
1661 * radius_client_get_mib - Get RADIUS client MIB information
1662 * @radius: RADIUS client context from radius_client_init()
1663 * @buf: Buffer for returning MIB data in text format
1664 * @buflen: Maximum buf length in octets
1665 * Returns: Number of octets written into the buffer
1666 */
radius_client_get_mib(struct radius_client_data * radius,char * buf,size_t buflen)1667 int radius_client_get_mib(struct radius_client_data *radius, char *buf,
1668 size_t buflen)
1669 {
1670 struct hostapd_radius_servers *conf;
1671 int i;
1672 struct hostapd_radius_server *serv;
1673 int count = 0;
1674
1675 if (!radius)
1676 return 0;
1677
1678 conf = radius->conf;
1679
1680 if (conf->auth_servers) {
1681 for (i = 0; i < conf->num_auth_servers; i++) {
1682 serv = &conf->auth_servers[i];
1683 count += radius_client_dump_auth_server(
1684 buf + count, buflen - count, serv,
1685 serv == conf->auth_server ?
1686 radius : NULL);
1687 }
1688 }
1689
1690 if (conf->acct_servers) {
1691 for (i = 0; i < conf->num_acct_servers; i++) {
1692 serv = &conf->acct_servers[i];
1693 count += radius_client_dump_acct_server(
1694 buf + count, buflen - count, serv,
1695 serv == conf->acct_server ?
1696 radius : NULL);
1697 }
1698 }
1699
1700 return count;
1701 }
1702
1703
radius_client_reconfig(struct radius_client_data * radius,struct hostapd_radius_servers * conf)1704 void radius_client_reconfig(struct radius_client_data *radius,
1705 struct hostapd_radius_servers *conf)
1706 {
1707 if (radius)
1708 radius->conf = conf;
1709 }
1710