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