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