]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/wpa/src/radius/radius_server.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / wpa / src / radius / radius_server.c
1 /*
2  * RADIUS authentication server
3  * Copyright (c) 2005-2009, 2011, 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 "eloop.h"
15 #include "eap_server/eap.h"
16 #include "radius_server.h"
17
18 /**
19  * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
20  */
21 #define RADIUS_SESSION_TIMEOUT 60
22
23 /**
24  * RADIUS_MAX_SESSION - Maximum number of active sessions
25  */
26 #define RADIUS_MAX_SESSION 100
27
28 /**
29  * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
30  */
31 #define RADIUS_MAX_MSG_LEN 3000
32
33 static struct eapol_callbacks radius_server_eapol_cb;
34
35 struct radius_client;
36 struct radius_server_data;
37
38 /**
39  * struct radius_server_counters - RADIUS server statistics counters
40  */
41 struct radius_server_counters {
42         u32 access_requests;
43         u32 invalid_requests;
44         u32 dup_access_requests;
45         u32 access_accepts;
46         u32 access_rejects;
47         u32 access_challenges;
48         u32 malformed_access_requests;
49         u32 bad_authenticators;
50         u32 packets_dropped;
51         u32 unknown_types;
52 };
53
54 /**
55  * struct radius_session - Internal RADIUS server data for a session
56  */
57 struct radius_session {
58         struct radius_session *next;
59         struct radius_client *client;
60         struct radius_server_data *server;
61         unsigned int sess_id;
62         struct eap_sm *eap;
63         struct eap_eapol_interface *eap_if;
64
65         struct radius_msg *last_msg;
66         char *last_from_addr;
67         int last_from_port;
68         struct sockaddr_storage last_from;
69         socklen_t last_fromlen;
70         u8 last_identifier;
71         struct radius_msg *last_reply;
72         u8 last_authenticator[16];
73 };
74
75 /**
76  * struct radius_client - Internal RADIUS server data for a client
77  */
78 struct radius_client {
79         struct radius_client *next;
80         struct in_addr addr;
81         struct in_addr mask;
82 #ifdef CONFIG_IPV6
83         struct in6_addr addr6;
84         struct in6_addr mask6;
85 #endif /* CONFIG_IPV6 */
86         char *shared_secret;
87         int shared_secret_len;
88         struct radius_session *sessions;
89         struct radius_server_counters counters;
90 };
91
92 /**
93  * struct radius_server_data - Internal RADIUS server data
94  */
95 struct radius_server_data {
96         /**
97          * auth_sock - Socket for RADIUS authentication messages
98          */
99         int auth_sock;
100
101         /**
102          * clients - List of authorized RADIUS clients
103          */
104         struct radius_client *clients;
105
106         /**
107          * next_sess_id - Next session identifier
108          */
109         unsigned int next_sess_id;
110
111         /**
112          * conf_ctx - Context pointer for callbacks
113          *
114          * This is used as the ctx argument in get_eap_user() calls.
115          */
116         void *conf_ctx;
117
118         /**
119          * num_sess - Number of active sessions
120          */
121         int num_sess;
122
123         /**
124          * eap_sim_db_priv - EAP-SIM/AKA database context
125          *
126          * This is passed to the EAP-SIM/AKA server implementation as a
127          * callback context.
128          */
129         void *eap_sim_db_priv;
130
131         /**
132          * ssl_ctx - TLS context
133          *
134          * This is passed to the EAP server implementation as a callback
135          * context for TLS operations.
136          */
137         void *ssl_ctx;
138
139         /**
140          * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
141          *
142          * This parameter is used to set a key for EAP-FAST to encrypt the
143          * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
144          * set, must point to a 16-octet key.
145          */
146         u8 *pac_opaque_encr_key;
147
148         /**
149          * eap_fast_a_id - EAP-FAST authority identity (A-ID)
150          *
151          * If EAP-FAST is not used, this can be set to %NULL. In theory, this
152          * is a variable length field, but due to some existing implementations
153          * requiring A-ID to be 16 octets in length, it is recommended to use
154          * that length for the field to provide interoperability with deployed
155          * peer implementations.
156          */
157         u8 *eap_fast_a_id;
158
159         /**
160          * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
161          */
162         size_t eap_fast_a_id_len;
163
164         /**
165          * eap_fast_a_id_info - EAP-FAST authority identifier information
166          *
167          * This A-ID-Info contains a user-friendly name for the A-ID. For
168          * example, this could be the enterprise and server names in
169          * human-readable format. This field is encoded as UTF-8. If EAP-FAST
170          * is not used, this can be set to %NULL.
171          */
172         char *eap_fast_a_id_info;
173
174         /**
175          * eap_fast_prov - EAP-FAST provisioning modes
176          *
177          * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
178          * 2 = only authenticated provisioning allowed, 3 = both provisioning
179          * modes allowed.
180          */
181         int eap_fast_prov;
182
183         /**
184          * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
185          *
186          * This is the hard limit on how long a provisioned PAC-Key can be
187          * used.
188          */
189         int pac_key_lifetime;
190
191         /**
192          * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
193          *
194          * This is a soft limit on the PAC-Key. The server will automatically
195          * generate a new PAC-Key when this number of seconds (or fewer) of the
196          * lifetime remains.
197          */
198         int pac_key_refresh_time;
199
200         /**
201          * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
202          *
203          * This controls whether the protected success/failure indication
204          * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
205          */
206         int eap_sim_aka_result_ind;
207
208         /**
209          * tnc - Trusted Network Connect (TNC)
210          *
211          * This controls whether TNC is enabled and will be required before the
212          * peer is allowed to connect. Note: This is only used with EAP-TTLS
213          * and EAP-FAST. If any other EAP method is enabled, the peer will be
214          * allowed to connect without TNC.
215          */
216         int tnc;
217
218         /**
219          * pwd_group - The D-H group assigned for EAP-pwd
220          *
221          * If EAP-pwd is not used it can be set to zero.
222          */
223         u16 pwd_group;
224
225         /**
226          * wps - Wi-Fi Protected Setup context
227          *
228          * If WPS is used with an external RADIUS server (which is quite
229          * unlikely configuration), this is used to provide a pointer to WPS
230          * context data. Normally, this can be set to %NULL.
231          */
232         struct wps_context *wps;
233
234         /**
235          * ipv6 - Whether to enable IPv6 support in the RADIUS server
236          */
237         int ipv6;
238
239         /**
240          * start_time - Timestamp of server start
241          */
242         struct os_time start_time;
243
244         /**
245          * counters - Statistics counters for server operations
246          *
247          * These counters are the sum over all clients.
248          */
249         struct radius_server_counters counters;
250
251         /**
252          * get_eap_user - Callback for fetching EAP user information
253          * @ctx: Context data from conf_ctx
254          * @identity: User identity
255          * @identity_len: identity buffer length in octets
256          * @phase2: Whether this is for Phase 2 identity
257          * @user: Data structure for filling in the user information
258          * Returns: 0 on success, -1 on failure
259          *
260          * This is used to fetch information from user database. The callback
261          * will fill in information about allowed EAP methods and the user
262          * password. The password field will be an allocated copy of the
263          * password data and RADIUS server will free it after use.
264          */
265         int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
266                             int phase2, struct eap_user *user);
267
268         /**
269          * eap_req_id_text - Optional data for EAP-Request/Identity
270          *
271          * This can be used to configure an optional, displayable message that
272          * will be sent in EAP-Request/Identity. This string can contain an
273          * ASCII-0 character (nul) to separate network infromation per RFC
274          * 4284. The actual string length is explicit provided in
275          * eap_req_id_text_len since nul character will not be used as a string
276          * terminator.
277          */
278         char *eap_req_id_text;
279
280         /**
281          * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
282          */
283         size_t eap_req_id_text_len;
284
285         /*
286          * msg_ctx - Context data for wpa_msg() calls
287          */
288         void *msg_ctx;
289
290 #ifdef CONFIG_RADIUS_TEST
291         char *dump_msk_file;
292 #endif /* CONFIG_RADIUS_TEST */
293 };
294
295
296 extern int wpa_debug_level;
297
298 #define RADIUS_DEBUG(args...) \
299 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
300 #define RADIUS_ERROR(args...) \
301 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
302 #define RADIUS_DUMP(args...) \
303 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
304 #define RADIUS_DUMP_ASCII(args...) \
305 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
306
307
308 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
309 static void radius_server_session_remove_timeout(void *eloop_ctx,
310                                                  void *timeout_ctx);
311
312
313 static struct radius_client *
314 radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
315                          int ipv6)
316 {
317         struct radius_client *client = data->clients;
318
319         while (client) {
320 #ifdef CONFIG_IPV6
321                 if (ipv6) {
322                         struct in6_addr *addr6;
323                         int i;
324
325                         addr6 = (struct in6_addr *) addr;
326                         for (i = 0; i < 16; i++) {
327                                 if ((addr6->s6_addr[i] &
328                                      client->mask6.s6_addr[i]) !=
329                                     (client->addr6.s6_addr[i] &
330                                      client->mask6.s6_addr[i])) {
331                                         i = 17;
332                                         break;
333                                 }
334                         }
335                         if (i == 16) {
336                                 break;
337                         }
338                 }
339 #endif /* CONFIG_IPV6 */
340                 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
341                     (addr->s_addr & client->mask.s_addr)) {
342                         break;
343                 }
344
345                 client = client->next;
346         }
347
348         return client;
349 }
350
351
352 static struct radius_session *
353 radius_server_get_session(struct radius_client *client, unsigned int sess_id)
354 {
355         struct radius_session *sess = client->sessions;
356
357         while (sess) {
358                 if (sess->sess_id == sess_id) {
359                         break;
360                 }
361                 sess = sess->next;
362         }
363
364         return sess;
365 }
366
367
368 static void radius_server_session_free(struct radius_server_data *data,
369                                        struct radius_session *sess)
370 {
371         eloop_cancel_timeout(radius_server_session_timeout, data, sess);
372         eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
373         eap_server_sm_deinit(sess->eap);
374         radius_msg_free(sess->last_msg);
375         os_free(sess->last_from_addr);
376         radius_msg_free(sess->last_reply);
377         os_free(sess);
378         data->num_sess--;
379 }
380
381
382 static void radius_server_session_remove(struct radius_server_data *data,
383                                          struct radius_session *sess)
384 {
385         struct radius_client *client = sess->client;
386         struct radius_session *session, *prev;
387
388         eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
389
390         prev = NULL;
391         session = client->sessions;
392         while (session) {
393                 if (session == sess) {
394                         if (prev == NULL) {
395                                 client->sessions = sess->next;
396                         } else {
397                                 prev->next = sess->next;
398                         }
399                         radius_server_session_free(data, sess);
400                         break;
401                 }
402                 prev = session;
403                 session = session->next;
404         }
405 }
406
407
408 static void radius_server_session_remove_timeout(void *eloop_ctx,
409                                                  void *timeout_ctx)
410 {
411         struct radius_server_data *data = eloop_ctx;
412         struct radius_session *sess = timeout_ctx;
413         RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
414         radius_server_session_remove(data, sess);
415 }
416
417
418 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
419 {
420         struct radius_server_data *data = eloop_ctx;
421         struct radius_session *sess = timeout_ctx;
422
423         RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
424         radius_server_session_remove(data, sess);
425 }
426
427
428 static struct radius_session *
429 radius_server_new_session(struct radius_server_data *data,
430                           struct radius_client *client)
431 {
432         struct radius_session *sess;
433
434         if (data->num_sess >= RADIUS_MAX_SESSION) {
435                 RADIUS_DEBUG("Maximum number of existing session - no room "
436                              "for a new session");
437                 return NULL;
438         }
439
440         sess = os_zalloc(sizeof(*sess));
441         if (sess == NULL)
442                 return NULL;
443
444         sess->server = data;
445         sess->client = client;
446         sess->sess_id = data->next_sess_id++;
447         sess->next = client->sessions;
448         client->sessions = sess;
449         eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
450                                radius_server_session_timeout, data, sess);
451         data->num_sess++;
452         return sess;
453 }
454
455
456 static struct radius_session *
457 radius_server_get_new_session(struct radius_server_data *data,
458                               struct radius_client *client,
459                               struct radius_msg *msg)
460 {
461         u8 *user;
462         size_t user_len;
463         int res;
464         struct radius_session *sess;
465         struct eap_config eap_conf;
466
467         RADIUS_DEBUG("Creating a new session");
468
469         user = os_malloc(256);
470         if (user == NULL) {
471                 return NULL;
472         }
473         res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
474         if (res < 0 || res > 256) {
475                 RADIUS_DEBUG("Could not get User-Name");
476                 os_free(user);
477                 return NULL;
478         }
479         user_len = res;
480         RADIUS_DUMP_ASCII("User-Name", user, user_len);
481
482         res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
483         os_free(user);
484
485         if (res == 0) {
486                 RADIUS_DEBUG("Matching user entry found");
487                 sess = radius_server_new_session(data, client);
488                 if (sess == NULL) {
489                         RADIUS_DEBUG("Failed to create a new session");
490                         return NULL;
491                 }
492         } else {
493                 RADIUS_DEBUG("User-Name not found from user database");
494                 return NULL;
495         }
496
497         os_memset(&eap_conf, 0, sizeof(eap_conf));
498         eap_conf.ssl_ctx = data->ssl_ctx;
499         eap_conf.msg_ctx = data->msg_ctx;
500         eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
501         eap_conf.backend_auth = TRUE;
502         eap_conf.eap_server = 1;
503         eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
504         eap_conf.eap_fast_a_id = data->eap_fast_a_id;
505         eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
506         eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
507         eap_conf.eap_fast_prov = data->eap_fast_prov;
508         eap_conf.pac_key_lifetime = data->pac_key_lifetime;
509         eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
510         eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
511         eap_conf.tnc = data->tnc;
512         eap_conf.wps = data->wps;
513         eap_conf.pwd_group = data->pwd_group;
514         sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
515                                        &eap_conf);
516         if (sess->eap == NULL) {
517                 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
518                              "new session");
519                 radius_server_session_free(data, sess);
520                 return NULL;
521         }
522         sess->eap_if = eap_get_interface(sess->eap);
523         sess->eap_if->eapRestart = TRUE;
524         sess->eap_if->portEnabled = TRUE;
525
526         RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
527
528         return sess;
529 }
530
531
532 static struct radius_msg *
533 radius_server_encapsulate_eap(struct radius_server_data *data,
534                               struct radius_client *client,
535                               struct radius_session *sess,
536                               struct radius_msg *request)
537 {
538         struct radius_msg *msg;
539         int code;
540         unsigned int sess_id;
541         struct radius_hdr *hdr = radius_msg_get_hdr(request);
542
543         if (sess->eap_if->eapFail) {
544                 sess->eap_if->eapFail = FALSE;
545                 code = RADIUS_CODE_ACCESS_REJECT;
546         } else if (sess->eap_if->eapSuccess) {
547                 sess->eap_if->eapSuccess = FALSE;
548                 code = RADIUS_CODE_ACCESS_ACCEPT;
549         } else {
550                 sess->eap_if->eapReq = FALSE;
551                 code = RADIUS_CODE_ACCESS_CHALLENGE;
552         }
553
554         msg = radius_msg_new(code, hdr->identifier);
555         if (msg == NULL) {
556                 RADIUS_DEBUG("Failed to allocate reply message");
557                 return NULL;
558         }
559
560         sess_id = htonl(sess->sess_id);
561         if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
562             !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
563                                  (u8 *) &sess_id, sizeof(sess_id))) {
564                 RADIUS_DEBUG("Failed to add State attribute");
565         }
566
567         if (sess->eap_if->eapReqData &&
568             !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
569                                 wpabuf_len(sess->eap_if->eapReqData))) {
570                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
571         }
572
573         if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
574                 int len;
575 #ifdef CONFIG_RADIUS_TEST
576                 if (data->dump_msk_file) {
577                         FILE *f;
578                         char buf[2 * 64 + 1];
579                         f = fopen(data->dump_msk_file, "a");
580                         if (f) {
581                                 len = sess->eap_if->eapKeyDataLen;
582                                 if (len > 64)
583                                         len = 64;
584                                 len = wpa_snprintf_hex(
585                                         buf, sizeof(buf),
586                                         sess->eap_if->eapKeyData, len);
587                                 buf[len] = '\0';
588                                 fprintf(f, "%s\n", buf);
589                                 fclose(f);
590                         }
591                 }
592 #endif /* CONFIG_RADIUS_TEST */
593                 if (sess->eap_if->eapKeyDataLen > 64) {
594                         len = 32;
595                 } else {
596                         len = sess->eap_if->eapKeyDataLen / 2;
597                 }
598                 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
599                                               (u8 *) client->shared_secret,
600                                               client->shared_secret_len,
601                                               sess->eap_if->eapKeyData + len,
602                                               len, sess->eap_if->eapKeyData,
603                                               len)) {
604                         RADIUS_DEBUG("Failed to add MPPE key attributes");
605                 }
606         }
607
608         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
609                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
610                 radius_msg_free(msg);
611                 return NULL;
612         }
613
614         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
615                                   client->shared_secret_len,
616                                   hdr->authenticator) < 0) {
617                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
618         }
619
620         return msg;
621 }
622
623
624 static int radius_server_reject(struct radius_server_data *data,
625                                 struct radius_client *client,
626                                 struct radius_msg *request,
627                                 struct sockaddr *from, socklen_t fromlen,
628                                 const char *from_addr, int from_port)
629 {
630         struct radius_msg *msg;
631         int ret = 0;
632         struct eap_hdr eapfail;
633         struct wpabuf *buf;
634         struct radius_hdr *hdr = radius_msg_get_hdr(request);
635
636         RADIUS_DEBUG("Reject invalid request from %s:%d",
637                      from_addr, from_port);
638
639         msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
640         if (msg == NULL) {
641                 return -1;
642         }
643
644         os_memset(&eapfail, 0, sizeof(eapfail));
645         eapfail.code = EAP_CODE_FAILURE;
646         eapfail.identifier = 0;
647         eapfail.length = host_to_be16(sizeof(eapfail));
648
649         if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
650                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
651         }
652
653         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
654                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
655                 radius_msg_free(msg);
656                 return -1;
657         }
658
659         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
660                                   client->shared_secret_len,
661                                   hdr->authenticator) <
662             0) {
663                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
664         }
665
666         if (wpa_debug_level <= MSG_MSGDUMP) {
667                 radius_msg_dump(msg);
668         }
669
670         data->counters.access_rejects++;
671         client->counters.access_rejects++;
672         buf = radius_msg_get_buf(msg);
673         if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
674                    (struct sockaddr *) from, sizeof(*from)) < 0) {
675                 perror("sendto[RADIUS SRV]");
676                 ret = -1;
677         }
678
679         radius_msg_free(msg);
680
681         return ret;
682 }
683
684
685 static int radius_server_request(struct radius_server_data *data,
686                                  struct radius_msg *msg,
687                                  struct sockaddr *from, socklen_t fromlen,
688                                  struct radius_client *client,
689                                  const char *from_addr, int from_port,
690                                  struct radius_session *force_sess)
691 {
692         struct wpabuf *eap = NULL;
693         int res, state_included = 0;
694         u8 statebuf[4];
695         unsigned int state;
696         struct radius_session *sess;
697         struct radius_msg *reply;
698         int is_complete = 0;
699
700         if (force_sess)
701                 sess = force_sess;
702         else {
703                 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
704                                           sizeof(statebuf));
705                 state_included = res >= 0;
706                 if (res == sizeof(statebuf)) {
707                         state = WPA_GET_BE32(statebuf);
708                         sess = radius_server_get_session(client, state);
709                 } else {
710                         sess = NULL;
711                 }
712         }
713
714         if (sess) {
715                 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
716         } else if (state_included) {
717                 RADIUS_DEBUG("State attribute included but no session found");
718                 radius_server_reject(data, client, msg, from, fromlen,
719                                      from_addr, from_port);
720                 return -1;
721         } else {
722                 sess = radius_server_get_new_session(data, client, msg);
723                 if (sess == NULL) {
724                         RADIUS_DEBUG("Could not create a new session");
725                         radius_server_reject(data, client, msg, from, fromlen,
726                                              from_addr, from_port);
727                         return -1;
728                 }
729         }
730
731         if (sess->last_from_port == from_port &&
732             sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
733             os_memcmp(sess->last_authenticator,
734                       radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
735                 RADIUS_DEBUG("Duplicate message from %s", from_addr);
736                 data->counters.dup_access_requests++;
737                 client->counters.dup_access_requests++;
738
739                 if (sess->last_reply) {
740                         struct wpabuf *buf;
741                         buf = radius_msg_get_buf(sess->last_reply);
742                         res = sendto(data->auth_sock, wpabuf_head(buf),
743                                      wpabuf_len(buf), 0,
744                                      (struct sockaddr *) from, fromlen);
745                         if (res < 0) {
746                                 perror("sendto[RADIUS SRV]");
747                         }
748                         return 0;
749                 }
750
751                 RADIUS_DEBUG("No previous reply available for duplicate "
752                              "message");
753                 return -1;
754         }
755                       
756         eap = radius_msg_get_eap(msg);
757         if (eap == NULL) {
758                 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
759                              from_addr);
760                 data->counters.packets_dropped++;
761                 client->counters.packets_dropped++;
762                 return -1;
763         }
764
765         RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
766
767         /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
768          * RFC3579 Sect. 2.6.2.
769          * Include EAP-Response/Nak with no preferred method if
770          * code == request.
771          * If code is not 1-4, discard the packet silently.
772          * Or is this already done by the EAP state machine? */
773
774         wpabuf_free(sess->eap_if->eapRespData);
775         sess->eap_if->eapRespData = eap;
776         sess->eap_if->eapResp = TRUE;
777         eap_server_sm_step(sess->eap);
778
779         if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
780              sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
781                 RADIUS_DUMP("EAP data from the state machine",
782                             wpabuf_head(sess->eap_if->eapReqData),
783                             wpabuf_len(sess->eap_if->eapReqData));
784         } else if (sess->eap_if->eapFail) {
785                 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
786                              "set");
787         } else if (eap_sm_method_pending(sess->eap)) {
788                 radius_msg_free(sess->last_msg);
789                 sess->last_msg = msg;
790                 sess->last_from_port = from_port;
791                 os_free(sess->last_from_addr);
792                 sess->last_from_addr = os_strdup(from_addr);
793                 sess->last_fromlen = fromlen;
794                 os_memcpy(&sess->last_from, from, fromlen);
795                 return -2;
796         } else {
797                 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
798                              " Access-Request silently (assuming it was a "
799                              "duplicate)");
800                 data->counters.packets_dropped++;
801                 client->counters.packets_dropped++;
802                 return -1;
803         }
804
805         if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
806                 is_complete = 1;
807
808         reply = radius_server_encapsulate_eap(data, client, sess, msg);
809
810         if (reply) {
811                 struct wpabuf *buf;
812                 struct radius_hdr *hdr;
813
814                 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
815                 if (wpa_debug_level <= MSG_MSGDUMP) {
816                         radius_msg_dump(reply);
817                 }
818
819                 switch (radius_msg_get_hdr(reply)->code) {
820                 case RADIUS_CODE_ACCESS_ACCEPT:
821                         data->counters.access_accepts++;
822                         client->counters.access_accepts++;
823                         break;
824                 case RADIUS_CODE_ACCESS_REJECT:
825                         data->counters.access_rejects++;
826                         client->counters.access_rejects++;
827                         break;
828                 case RADIUS_CODE_ACCESS_CHALLENGE:
829                         data->counters.access_challenges++;
830                         client->counters.access_challenges++;
831                         break;
832                 }
833                 buf = radius_msg_get_buf(reply);
834                 res = sendto(data->auth_sock, wpabuf_head(buf),
835                              wpabuf_len(buf), 0,
836                              (struct sockaddr *) from, fromlen);
837                 if (res < 0) {
838                         perror("sendto[RADIUS SRV]");
839                 }
840                 radius_msg_free(sess->last_reply);
841                 sess->last_reply = reply;
842                 sess->last_from_port = from_port;
843                 hdr = radius_msg_get_hdr(msg);
844                 sess->last_identifier = hdr->identifier;
845                 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
846         } else {
847                 data->counters.packets_dropped++;
848                 client->counters.packets_dropped++;
849         }
850
851         if (is_complete) {
852                 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
853                              sess->sess_id);
854                 eloop_cancel_timeout(radius_server_session_remove_timeout,
855                                      data, sess);
856                 eloop_register_timeout(10, 0,
857                                        radius_server_session_remove_timeout,
858                                        data, sess);
859         }
860
861         return 0;
862 }
863
864
865 static void radius_server_receive_auth(int sock, void *eloop_ctx,
866                                        void *sock_ctx)
867 {
868         struct radius_server_data *data = eloop_ctx;
869         u8 *buf = NULL;
870         union {
871                 struct sockaddr_storage ss;
872                 struct sockaddr_in sin;
873 #ifdef CONFIG_IPV6
874                 struct sockaddr_in6 sin6;
875 #endif /* CONFIG_IPV6 */
876         } from;
877         socklen_t fromlen;
878         int len;
879         struct radius_client *client = NULL;
880         struct radius_msg *msg = NULL;
881         char abuf[50];
882         int from_port = 0;
883
884         buf = os_malloc(RADIUS_MAX_MSG_LEN);
885         if (buf == NULL) {
886                 goto fail;
887         }
888
889         fromlen = sizeof(from);
890         len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
891                        (struct sockaddr *) &from.ss, &fromlen);
892         if (len < 0) {
893                 perror("recvfrom[radius_server]");
894                 goto fail;
895         }
896
897 #ifdef CONFIG_IPV6
898         if (data->ipv6) {
899                 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
900                               sizeof(abuf)) == NULL)
901                         abuf[0] = '\0';
902                 from_port = ntohs(from.sin6.sin6_port);
903                 RADIUS_DEBUG("Received %d bytes from %s:%d",
904                              len, abuf, from_port);
905
906                 client = radius_server_get_client(data,
907                                                   (struct in_addr *)
908                                                   &from.sin6.sin6_addr, 1);
909         }
910 #endif /* CONFIG_IPV6 */
911
912         if (!data->ipv6) {
913                 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
914                 from_port = ntohs(from.sin.sin_port);
915                 RADIUS_DEBUG("Received %d bytes from %s:%d",
916                              len, abuf, from_port);
917
918                 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
919         }
920
921         RADIUS_DUMP("Received data", buf, len);
922
923         if (client == NULL) {
924                 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
925                 data->counters.invalid_requests++;
926                 goto fail;
927         }
928
929         msg = radius_msg_parse(buf, len);
930         if (msg == NULL) {
931                 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
932                 data->counters.malformed_access_requests++;
933                 client->counters.malformed_access_requests++;
934                 goto fail;
935         }
936
937         os_free(buf);
938         buf = NULL;
939
940         if (wpa_debug_level <= MSG_MSGDUMP) {
941                 radius_msg_dump(msg);
942         }
943
944         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
945                 RADIUS_DEBUG("Unexpected RADIUS code %d",
946                              radius_msg_get_hdr(msg)->code);
947                 data->counters.unknown_types++;
948                 client->counters.unknown_types++;
949                 goto fail;
950         }
951
952         data->counters.access_requests++;
953         client->counters.access_requests++;
954
955         if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
956                                        client->shared_secret_len, NULL)) {
957                 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
958                 data->counters.bad_authenticators++;
959                 client->counters.bad_authenticators++;
960                 goto fail;
961         }
962
963         if (radius_server_request(data, msg, (struct sockaddr *) &from,
964                                   fromlen, client, abuf, from_port, NULL) ==
965             -2)
966                 return; /* msg was stored with the session */
967
968 fail:
969         radius_msg_free(msg);
970         os_free(buf);
971 }
972
973
974 static int radius_server_disable_pmtu_discovery(int s)
975 {
976         int r = -1;
977 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
978         /* Turn off Path MTU discovery on IPv4/UDP sockets. */
979         int action = IP_PMTUDISC_DONT;
980         r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
981                        sizeof(action));
982         if (r == -1)
983                 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
984                            "%s", strerror(errno));
985 #endif
986         return r;
987 }
988
989
990 static int radius_server_open_socket(int port)
991 {
992         int s;
993         struct sockaddr_in addr;
994
995         s = socket(PF_INET, SOCK_DGRAM, 0);
996         if (s < 0) {
997                 perror("socket");
998                 return -1;
999         }
1000
1001         radius_server_disable_pmtu_discovery(s);
1002
1003         os_memset(&addr, 0, sizeof(addr));
1004         addr.sin_family = AF_INET;
1005         addr.sin_port = htons(port);
1006         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1007                 perror("bind");
1008                 close(s);
1009                 return -1;
1010         }
1011
1012         return s;
1013 }
1014
1015
1016 #ifdef CONFIG_IPV6
1017 static int radius_server_open_socket6(int port)
1018 {
1019         int s;
1020         struct sockaddr_in6 addr;
1021
1022         s = socket(PF_INET6, SOCK_DGRAM, 0);
1023         if (s < 0) {
1024                 perror("socket[IPv6]");
1025                 return -1;
1026         }
1027
1028         os_memset(&addr, 0, sizeof(addr));
1029         addr.sin6_family = AF_INET6;
1030         os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1031         addr.sin6_port = htons(port);
1032         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1033                 perror("bind");
1034                 close(s);
1035                 return -1;
1036         }
1037
1038         return s;
1039 }
1040 #endif /* CONFIG_IPV6 */
1041
1042
1043 static void radius_server_free_sessions(struct radius_server_data *data,
1044                                         struct radius_session *sessions)
1045 {
1046         struct radius_session *session, *prev;
1047
1048         session = sessions;
1049         while (session) {
1050                 prev = session;
1051                 session = session->next;
1052                 radius_server_session_free(data, prev);
1053         }
1054 }
1055
1056
1057 static void radius_server_free_clients(struct radius_server_data *data,
1058                                        struct radius_client *clients)
1059 {
1060         struct radius_client *client, *prev;
1061
1062         client = clients;
1063         while (client) {
1064                 prev = client;
1065                 client = client->next;
1066
1067                 radius_server_free_sessions(data, prev->sessions);
1068                 os_free(prev->shared_secret);
1069                 os_free(prev);
1070         }
1071 }
1072
1073
1074 static struct radius_client *
1075 radius_server_read_clients(const char *client_file, int ipv6)
1076 {
1077         FILE *f;
1078         const int buf_size = 1024;
1079         char *buf, *pos;
1080         struct radius_client *clients, *tail, *entry;
1081         int line = 0, mask, failed = 0, i;
1082         struct in_addr addr;
1083 #ifdef CONFIG_IPV6
1084         struct in6_addr addr6;
1085 #endif /* CONFIG_IPV6 */
1086         unsigned int val;
1087
1088         f = fopen(client_file, "r");
1089         if (f == NULL) {
1090                 RADIUS_ERROR("Could not open client file '%s'", client_file);
1091                 return NULL;
1092         }
1093
1094         buf = os_malloc(buf_size);
1095         if (buf == NULL) {
1096                 fclose(f);
1097                 return NULL;
1098         }
1099
1100         clients = tail = NULL;
1101         while (fgets(buf, buf_size, f)) {
1102                 /* Configuration file format:
1103                  * 192.168.1.0/24 secret
1104                  * 192.168.1.2 secret
1105                  * fe80::211:22ff:fe33:4455/64 secretipv6
1106                  */
1107                 line++;
1108                 buf[buf_size - 1] = '\0';
1109                 pos = buf;
1110                 while (*pos != '\0' && *pos != '\n')
1111                         pos++;
1112                 if (*pos == '\n')
1113                         *pos = '\0';
1114                 if (*buf == '\0' || *buf == '#')
1115                         continue;
1116
1117                 pos = buf;
1118                 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1119                        (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1120                        (*pos >= 'A' && *pos <= 'F')) {
1121                         pos++;
1122                 }
1123
1124                 if (*pos == '\0') {
1125                         failed = 1;
1126                         break;
1127                 }
1128
1129                 if (*pos == '/') {
1130                         char *end;
1131                         *pos++ = '\0';
1132                         mask = strtol(pos, &end, 10);
1133                         if ((pos == end) ||
1134                             (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1135                                 failed = 1;
1136                                 break;
1137                         }
1138                         pos = end;
1139                 } else {
1140                         mask = ipv6 ? 128 : 32;
1141                         *pos++ = '\0';
1142                 }
1143
1144                 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1145                         failed = 1;
1146                         break;
1147                 }
1148 #ifdef CONFIG_IPV6
1149                 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1150                         if (inet_pton(AF_INET, buf, &addr) <= 0) {
1151                                 failed = 1;
1152                                 break;
1153                         }
1154                         /* Convert IPv4 address to IPv6 */
1155                         if (mask <= 32)
1156                                 mask += (128 - 32);
1157                         os_memset(addr6.s6_addr, 0, 10);
1158                         addr6.s6_addr[10] = 0xff;
1159                         addr6.s6_addr[11] = 0xff;
1160                         os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1161                                   4);
1162                 }
1163 #endif /* CONFIG_IPV6 */
1164
1165                 while (*pos == ' ' || *pos == '\t') {
1166                         pos++;
1167                 }
1168
1169                 if (*pos == '\0') {
1170                         failed = 1;
1171                         break;
1172                 }
1173
1174                 entry = os_zalloc(sizeof(*entry));
1175                 if (entry == NULL) {
1176                         failed = 1;
1177                         break;
1178                 }
1179                 entry->shared_secret = os_strdup(pos);
1180                 if (entry->shared_secret == NULL) {
1181                         failed = 1;
1182                         os_free(entry);
1183                         break;
1184                 }
1185                 entry->shared_secret_len = os_strlen(entry->shared_secret);
1186                 entry->addr.s_addr = addr.s_addr;
1187                 if (!ipv6) {
1188                         val = 0;
1189                         for (i = 0; i < mask; i++)
1190                                 val |= 1 << (31 - i);
1191                         entry->mask.s_addr = htonl(val);
1192                 }
1193 #ifdef CONFIG_IPV6
1194                 if (ipv6) {
1195                         int offset = mask / 8;
1196
1197                         os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1198                         os_memset(entry->mask6.s6_addr, 0xff, offset);
1199                         val = 0;
1200                         for (i = 0; i < (mask % 8); i++)
1201                                 val |= 1 << (7 - i);
1202                         if (offset < 16)
1203                                 entry->mask6.s6_addr[offset] = val;
1204                 }
1205 #endif /* CONFIG_IPV6 */
1206
1207                 if (tail == NULL) {
1208                         clients = tail = entry;
1209                 } else {
1210                         tail->next = entry;
1211                         tail = entry;
1212                 }
1213         }
1214
1215         if (failed) {
1216                 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1217                 radius_server_free_clients(NULL, clients);
1218                 clients = NULL;
1219         }
1220
1221         os_free(buf);
1222         fclose(f);
1223
1224         return clients;
1225 }
1226
1227
1228 /**
1229  * radius_server_init - Initialize RADIUS server
1230  * @conf: Configuration for the RADIUS server
1231  * Returns: Pointer to private RADIUS server context or %NULL on failure
1232  *
1233  * This initializes a RADIUS server instance and returns a context pointer that
1234  * will be used in other calls to the RADIUS server module. The server can be
1235  * deinitialize by calling radius_server_deinit().
1236  */
1237 struct radius_server_data *
1238 radius_server_init(struct radius_server_conf *conf)
1239 {
1240         struct radius_server_data *data;
1241
1242 #ifndef CONFIG_IPV6
1243         if (conf->ipv6) {
1244                 fprintf(stderr, "RADIUS server compiled without IPv6 "
1245                         "support.\n");
1246                 return NULL;
1247         }
1248 #endif /* CONFIG_IPV6 */
1249
1250         data = os_zalloc(sizeof(*data));
1251         if (data == NULL)
1252                 return NULL;
1253
1254         os_get_time(&data->start_time);
1255         data->conf_ctx = conf->conf_ctx;
1256         data->eap_sim_db_priv = conf->eap_sim_db_priv;
1257         data->ssl_ctx = conf->ssl_ctx;
1258         data->msg_ctx = conf->msg_ctx;
1259         data->ipv6 = conf->ipv6;
1260         if (conf->pac_opaque_encr_key) {
1261                 data->pac_opaque_encr_key = os_malloc(16);
1262                 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1263                           16);
1264         }
1265         if (conf->eap_fast_a_id) {
1266                 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1267                 if (data->eap_fast_a_id) {
1268                         os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1269                                   conf->eap_fast_a_id_len);
1270                         data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1271                 }
1272         }
1273         if (conf->eap_fast_a_id_info)
1274                 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1275         data->eap_fast_prov = conf->eap_fast_prov;
1276         data->pac_key_lifetime = conf->pac_key_lifetime;
1277         data->pac_key_refresh_time = conf->pac_key_refresh_time;
1278         data->get_eap_user = conf->get_eap_user;
1279         data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1280         data->tnc = conf->tnc;
1281         data->wps = conf->wps;
1282         data->pwd_group = conf->pwd_group;
1283         if (conf->eap_req_id_text) {
1284                 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1285                 if (data->eap_req_id_text) {
1286                         os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1287                                   conf->eap_req_id_text_len);
1288                         data->eap_req_id_text_len = conf->eap_req_id_text_len;
1289                 }
1290         }
1291
1292 #ifdef CONFIG_RADIUS_TEST
1293         if (conf->dump_msk_file)
1294                 data->dump_msk_file = os_strdup(conf->dump_msk_file);
1295 #endif /* CONFIG_RADIUS_TEST */
1296
1297         data->clients = radius_server_read_clients(conf->client_file,
1298                                                    conf->ipv6);
1299         if (data->clients == NULL) {
1300                 printf("No RADIUS clients configured.\n");
1301                 radius_server_deinit(data);
1302                 return NULL;
1303         }
1304
1305 #ifdef CONFIG_IPV6
1306         if (conf->ipv6)
1307                 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1308         else
1309 #endif /* CONFIG_IPV6 */
1310         data->auth_sock = radius_server_open_socket(conf->auth_port);
1311         if (data->auth_sock < 0) {
1312                 printf("Failed to open UDP socket for RADIUS authentication "
1313                        "server\n");
1314                 radius_server_deinit(data);
1315                 return NULL;
1316         }
1317         if (eloop_register_read_sock(data->auth_sock,
1318                                      radius_server_receive_auth,
1319                                      data, NULL)) {
1320                 radius_server_deinit(data);
1321                 return NULL;
1322         }
1323
1324         return data;
1325 }
1326
1327
1328 /**
1329  * radius_server_deinit - Deinitialize RADIUS server
1330  * @data: RADIUS server context from radius_server_init()
1331  */
1332 void radius_server_deinit(struct radius_server_data *data)
1333 {
1334         if (data == NULL)
1335                 return;
1336
1337         if (data->auth_sock >= 0) {
1338                 eloop_unregister_read_sock(data->auth_sock);
1339                 close(data->auth_sock);
1340         }
1341
1342         radius_server_free_clients(data, data->clients);
1343
1344         os_free(data->pac_opaque_encr_key);
1345         os_free(data->eap_fast_a_id);
1346         os_free(data->eap_fast_a_id_info);
1347         os_free(data->eap_req_id_text);
1348 #ifdef CONFIG_RADIUS_TEST
1349         os_free(data->dump_msk_file);
1350 #endif /* CONFIG_RADIUS_TEST */
1351         os_free(data);
1352 }
1353
1354
1355 /**
1356  * radius_server_get_mib - Get RADIUS server MIB information
1357  * @data: RADIUS server context from radius_server_init()
1358  * @buf: Buffer for returning the MIB data in text format
1359  * @buflen: buf length in octets
1360  * Returns: Number of octets written into buf
1361  */
1362 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1363                           size_t buflen)
1364 {
1365         int ret, uptime;
1366         unsigned int idx;
1367         char *end, *pos;
1368         struct os_time now;
1369         struct radius_client *cli;
1370
1371         /* RFC 2619 - RADIUS Authentication Server MIB */
1372
1373         if (data == NULL || buflen == 0)
1374                 return 0;
1375
1376         pos = buf;
1377         end = buf + buflen;
1378
1379         os_get_time(&now);
1380         uptime = (now.sec - data->start_time.sec) * 100 +
1381                 ((now.usec - data->start_time.usec) / 10000) % 100;
1382         ret = os_snprintf(pos, end - pos,
1383                           "RADIUS-AUTH-SERVER-MIB\n"
1384                           "radiusAuthServIdent=hostapd\n"
1385                           "radiusAuthServUpTime=%d\n"
1386                           "radiusAuthServResetTime=0\n"
1387                           "radiusAuthServConfigReset=4\n",
1388                           uptime);
1389         if (ret < 0 || ret >= end - pos) {
1390                 *pos = '\0';
1391                 return pos - buf;
1392         }
1393         pos += ret;
1394
1395         ret = os_snprintf(pos, end - pos,
1396                           "radiusAuthServTotalAccessRequests=%u\n"
1397                           "radiusAuthServTotalInvalidRequests=%u\n"
1398                           "radiusAuthServTotalDupAccessRequests=%u\n"
1399                           "radiusAuthServTotalAccessAccepts=%u\n"
1400                           "radiusAuthServTotalAccessRejects=%u\n"
1401                           "radiusAuthServTotalAccessChallenges=%u\n"
1402                           "radiusAuthServTotalMalformedAccessRequests=%u\n"
1403                           "radiusAuthServTotalBadAuthenticators=%u\n"
1404                           "radiusAuthServTotalPacketsDropped=%u\n"
1405                           "radiusAuthServTotalUnknownTypes=%u\n",
1406                           data->counters.access_requests,
1407                           data->counters.invalid_requests,
1408                           data->counters.dup_access_requests,
1409                           data->counters.access_accepts,
1410                           data->counters.access_rejects,
1411                           data->counters.access_challenges,
1412                           data->counters.malformed_access_requests,
1413                           data->counters.bad_authenticators,
1414                           data->counters.packets_dropped,
1415                           data->counters.unknown_types);
1416         if (ret < 0 || ret >= end - pos) {
1417                 *pos = '\0';
1418                 return pos - buf;
1419         }
1420         pos += ret;
1421
1422         for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1423                 char abuf[50], mbuf[50];
1424 #ifdef CONFIG_IPV6
1425                 if (data->ipv6) {
1426                         if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1427                                       sizeof(abuf)) == NULL)
1428                                 abuf[0] = '\0';
1429                         if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1430                                       sizeof(mbuf)) == NULL)
1431                                 mbuf[0] = '\0';
1432                 }
1433 #endif /* CONFIG_IPV6 */
1434                 if (!data->ipv6) {
1435                         os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1436                         os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1437                 }
1438
1439                 ret = os_snprintf(pos, end - pos,
1440                                   "radiusAuthClientIndex=%u\n"
1441                                   "radiusAuthClientAddress=%s/%s\n"
1442                                   "radiusAuthServAccessRequests=%u\n"
1443                                   "radiusAuthServDupAccessRequests=%u\n"
1444                                   "radiusAuthServAccessAccepts=%u\n"
1445                                   "radiusAuthServAccessRejects=%u\n"
1446                                   "radiusAuthServAccessChallenges=%u\n"
1447                                   "radiusAuthServMalformedAccessRequests=%u\n"
1448                                   "radiusAuthServBadAuthenticators=%u\n"
1449                                   "radiusAuthServPacketsDropped=%u\n"
1450                                   "radiusAuthServUnknownTypes=%u\n",
1451                                   idx,
1452                                   abuf, mbuf,
1453                                   cli->counters.access_requests,
1454                                   cli->counters.dup_access_requests,
1455                                   cli->counters.access_accepts,
1456                                   cli->counters.access_rejects,
1457                                   cli->counters.access_challenges,
1458                                   cli->counters.malformed_access_requests,
1459                                   cli->counters.bad_authenticators,
1460                                   cli->counters.packets_dropped,
1461                                   cli->counters.unknown_types);
1462                 if (ret < 0 || ret >= end - pos) {
1463                         *pos = '\0';
1464                         return pos - buf;
1465                 }
1466                 pos += ret;
1467         }
1468
1469         return pos - buf;
1470 }
1471
1472
1473 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1474                                       size_t identity_len, int phase2,
1475                                       struct eap_user *user)
1476 {
1477         struct radius_session *sess = ctx;
1478         struct radius_server_data *data = sess->server;
1479
1480         return data->get_eap_user(data->conf_ctx, identity, identity_len,
1481                                   phase2, user);
1482 }
1483
1484
1485 static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1486 {
1487         struct radius_session *sess = ctx;
1488         struct radius_server_data *data = sess->server;
1489         *len = data->eap_req_id_text_len;
1490         return data->eap_req_id_text;
1491 }
1492
1493
1494 static struct eapol_callbacks radius_server_eapol_cb =
1495 {
1496         .get_eap_user = radius_server_get_eap_user,
1497         .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1498 };
1499
1500
1501 /**
1502  * radius_server_eap_pending_cb - Pending EAP data notification
1503  * @data: RADIUS server context from radius_server_init()
1504  * @ctx: Pending EAP context pointer
1505  *
1506  * This function is used to notify EAP server module that a pending operation
1507  * has been completed and processing of the EAP session can proceed.
1508  */
1509 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1510 {
1511         struct radius_client *cli;
1512         struct radius_session *s, *sess = NULL;
1513         struct radius_msg *msg;
1514
1515         if (data == NULL)
1516                 return;
1517
1518         for (cli = data->clients; cli; cli = cli->next) {
1519                 for (s = cli->sessions; s; s = s->next) {
1520                         if (s->eap == ctx && s->last_msg) {
1521                                 sess = s;
1522                                 break;
1523                         }
1524                         if (sess)
1525                                 break;
1526                 }
1527                 if (sess)
1528                         break;
1529         }
1530
1531         if (sess == NULL) {
1532                 RADIUS_DEBUG("No session matched callback ctx");
1533                 return;
1534         }
1535
1536         msg = sess->last_msg;
1537         sess->last_msg = NULL;
1538         eap_sm_pending_cb(sess->eap);
1539         if (radius_server_request(data, msg,
1540                                   (struct sockaddr *) &sess->last_from,
1541                                   sess->last_fromlen, cli,
1542                                   sess->last_from_addr,
1543                                   sess->last_from_port, sess) == -2)
1544                 return; /* msg was stored with the session */
1545
1546         radius_msg_free(msg);
1547 }