Make struct radius_msg private to radius.c
[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         struct radius_hdr *hdr = radius_msg_get_hdr(request);
531
532         if (sess->eap_if->eapFail) {
533                 sess->eap_if->eapFail = FALSE;
534                 code = RADIUS_CODE_ACCESS_REJECT;
535         } else if (sess->eap_if->eapSuccess) {
536                 sess->eap_if->eapSuccess = FALSE;
537                 code = RADIUS_CODE_ACCESS_ACCEPT;
538         } else {
539                 sess->eap_if->eapReq = FALSE;
540                 code = RADIUS_CODE_ACCESS_CHALLENGE;
541         }
542
543         msg = radius_msg_new(code, hdr->identifier);
544         if (msg == NULL) {
545                 RADIUS_DEBUG("Failed to allocate reply message");
546                 return NULL;
547         }
548
549         sess_id = htonl(sess->sess_id);
550         if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
551             !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
552                                  (u8 *) &sess_id, sizeof(sess_id))) {
553                 RADIUS_DEBUG("Failed to add State attribute");
554         }
555
556         if (sess->eap_if->eapReqData &&
557             !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
558                                 wpabuf_len(sess->eap_if->eapReqData))) {
559                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
560         }
561
562         if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
563                 int len;
564                 if (sess->eap_if->eapKeyDataLen > 64) {
565                         len = 32;
566                 } else {
567                         len = sess->eap_if->eapKeyDataLen / 2;
568                 }
569                 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
570                                               (u8 *) client->shared_secret,
571                                               client->shared_secret_len,
572                                               sess->eap_if->eapKeyData + len,
573                                               len, sess->eap_if->eapKeyData,
574                                               len)) {
575                         RADIUS_DEBUG("Failed to add MPPE key attributes");
576                 }
577         }
578
579         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
580                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
581                 radius_msg_free(msg);
582                 return NULL;
583         }
584
585         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
586                                   client->shared_secret_len,
587                                   hdr->authenticator) < 0) {
588                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
589         }
590
591         return msg;
592 }
593
594
595 static int radius_server_reject(struct radius_server_data *data,
596                                 struct radius_client *client,
597                                 struct radius_msg *request,
598                                 struct sockaddr *from, socklen_t fromlen,
599                                 const char *from_addr, int from_port)
600 {
601         struct radius_msg *msg;
602         int ret = 0;
603         struct eap_hdr eapfail;
604         struct wpabuf *buf;
605         struct radius_hdr *hdr = radius_msg_get_hdr(request);
606
607         RADIUS_DEBUG("Reject invalid request from %s:%d",
608                      from_addr, from_port);
609
610         msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
611         if (msg == NULL) {
612                 return -1;
613         }
614
615         os_memset(&eapfail, 0, sizeof(eapfail));
616         eapfail.code = EAP_CODE_FAILURE;
617         eapfail.identifier = 0;
618         eapfail.length = host_to_be16(sizeof(eapfail));
619
620         if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
621                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
622         }
623
624         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
625                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
626                 radius_msg_free(msg);
627                 return -1;
628         }
629
630         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
631                                   client->shared_secret_len,
632                                   hdr->authenticator) <
633             0) {
634                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
635         }
636
637         if (wpa_debug_level <= MSG_MSGDUMP) {
638                 radius_msg_dump(msg);
639         }
640
641         data->counters.access_rejects++;
642         client->counters.access_rejects++;
643         buf = radius_msg_get_buf(msg);
644         if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
645                    (struct sockaddr *) from, sizeof(*from)) < 0) {
646                 perror("sendto[RADIUS SRV]");
647                 ret = -1;
648         }
649
650         radius_msg_free(msg);
651
652         return ret;
653 }
654
655
656 static int radius_server_request(struct radius_server_data *data,
657                                  struct radius_msg *msg,
658                                  struct sockaddr *from, socklen_t fromlen,
659                                  struct radius_client *client,
660                                  const char *from_addr, int from_port,
661                                  struct radius_session *force_sess)
662 {
663         u8 *eap = NULL;
664         size_t eap_len;
665         int res, state_included = 0;
666         u8 statebuf[4];
667         unsigned int state;
668         struct radius_session *sess;
669         struct radius_msg *reply;
670         int is_complete = 0;
671
672         if (force_sess)
673                 sess = force_sess;
674         else {
675                 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
676                                           sizeof(statebuf));
677                 state_included = res >= 0;
678                 if (res == sizeof(statebuf)) {
679                         state = WPA_GET_BE32(statebuf);
680                         sess = radius_server_get_session(client, state);
681                 } else {
682                         sess = NULL;
683                 }
684         }
685
686         if (sess) {
687                 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
688         } else if (state_included) {
689                 RADIUS_DEBUG("State attribute included but no session found");
690                 radius_server_reject(data, client, msg, from, fromlen,
691                                      from_addr, from_port);
692                 return -1;
693         } else {
694                 sess = radius_server_get_new_session(data, client, msg);
695                 if (sess == NULL) {
696                         RADIUS_DEBUG("Could not create a new session");
697                         radius_server_reject(data, client, msg, from, fromlen,
698                                              from_addr, from_port);
699                         return -1;
700                 }
701         }
702
703         if (sess->last_from_port == from_port &&
704             sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
705             os_memcmp(sess->last_authenticator,
706                       radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
707                 RADIUS_DEBUG("Duplicate message from %s", from_addr);
708                 data->counters.dup_access_requests++;
709                 client->counters.dup_access_requests++;
710
711                 if (sess->last_reply) {
712                         struct wpabuf *buf;
713                         buf = radius_msg_get_buf(sess->last_reply);
714                         res = sendto(data->auth_sock, wpabuf_head(buf),
715                                      wpabuf_len(buf), 0,
716                                      (struct sockaddr *) from, fromlen);
717                         if (res < 0) {
718                                 perror("sendto[RADIUS SRV]");
719                         }
720                         return 0;
721                 }
722
723                 RADIUS_DEBUG("No previous reply available for duplicate "
724                              "message");
725                 return -1;
726         }
727                       
728         eap = radius_msg_get_eap(msg, &eap_len);
729         if (eap == NULL) {
730                 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
731                              from_addr);
732                 data->counters.packets_dropped++;
733                 client->counters.packets_dropped++;
734                 return -1;
735         }
736
737         RADIUS_DUMP("Received EAP data", eap, eap_len);
738
739         /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
740          * RFC3579 Sect. 2.6.2.
741          * Include EAP-Response/Nak with no preferred method if
742          * code == request.
743          * If code is not 1-4, discard the packet silently.
744          * Or is this already done by the EAP state machine? */
745
746         wpabuf_free(sess->eap_if->eapRespData);
747         sess->eap_if->eapRespData = wpabuf_alloc_ext_data(eap, eap_len);
748         if (sess->eap_if->eapRespData == NULL)
749                 os_free(eap);
750         eap = NULL;
751         sess->eap_if->eapResp = TRUE;
752         eap_server_sm_step(sess->eap);
753
754         if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
755              sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
756                 RADIUS_DUMP("EAP data from the state machine",
757                             wpabuf_head(sess->eap_if->eapReqData),
758                             wpabuf_len(sess->eap_if->eapReqData));
759         } else if (sess->eap_if->eapFail) {
760                 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
761                              "set");
762         } else if (eap_sm_method_pending(sess->eap)) {
763                 radius_msg_free(sess->last_msg);
764                 sess->last_msg = msg;
765                 sess->last_from_port = from_port;
766                 os_free(sess->last_from_addr);
767                 sess->last_from_addr = os_strdup(from_addr);
768                 sess->last_fromlen = fromlen;
769                 os_memcpy(&sess->last_from, from, fromlen);
770                 return -2;
771         } else {
772                 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
773                              " Access-Request silently (assuming it was a "
774                              "duplicate)");
775                 data->counters.packets_dropped++;
776                 client->counters.packets_dropped++;
777                 return -1;
778         }
779
780         if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
781                 is_complete = 1;
782
783         reply = radius_server_encapsulate_eap(data, client, sess, msg);
784
785         if (reply) {
786                 struct wpabuf *buf;
787                 struct radius_hdr *hdr;
788
789                 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
790                 if (wpa_debug_level <= MSG_MSGDUMP) {
791                         radius_msg_dump(reply);
792                 }
793
794                 switch (radius_msg_get_hdr(reply)->code) {
795                 case RADIUS_CODE_ACCESS_ACCEPT:
796                         data->counters.access_accepts++;
797                         client->counters.access_accepts++;
798                         break;
799                 case RADIUS_CODE_ACCESS_REJECT:
800                         data->counters.access_rejects++;
801                         client->counters.access_rejects++;
802                         break;
803                 case RADIUS_CODE_ACCESS_CHALLENGE:
804                         data->counters.access_challenges++;
805                         client->counters.access_challenges++;
806                         break;
807                 }
808                 buf = radius_msg_get_buf(reply);
809                 res = sendto(data->auth_sock, wpabuf_head(buf),
810                              wpabuf_len(buf), 0,
811                              (struct sockaddr *) from, fromlen);
812                 if (res < 0) {
813                         perror("sendto[RADIUS SRV]");
814                 }
815                 radius_msg_free(sess->last_reply);
816                 sess->last_reply = reply;
817                 sess->last_from_port = from_port;
818                 hdr = radius_msg_get_hdr(msg);
819                 sess->last_identifier = hdr->identifier;
820                 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
821         } else {
822                 data->counters.packets_dropped++;
823                 client->counters.packets_dropped++;
824         }
825
826         if (is_complete) {
827                 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
828                              sess->sess_id);
829                 eloop_cancel_timeout(radius_server_session_remove_timeout,
830                                      data, sess);
831                 eloop_register_timeout(10, 0,
832                                        radius_server_session_remove_timeout,
833                                        data, sess);
834         }
835
836         return 0;
837 }
838
839
840 static void radius_server_receive_auth(int sock, void *eloop_ctx,
841                                        void *sock_ctx)
842 {
843         struct radius_server_data *data = eloop_ctx;
844         u8 *buf = NULL;
845         union {
846                 struct sockaddr_storage ss;
847                 struct sockaddr_in sin;
848 #ifdef CONFIG_IPV6
849                 struct sockaddr_in6 sin6;
850 #endif /* CONFIG_IPV6 */
851         } from;
852         socklen_t fromlen;
853         int len;
854         struct radius_client *client = NULL;
855         struct radius_msg *msg = NULL;
856         char abuf[50];
857         int from_port = 0;
858
859         buf = os_malloc(RADIUS_MAX_MSG_LEN);
860         if (buf == NULL) {
861                 goto fail;
862         }
863
864         fromlen = sizeof(from);
865         len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
866                        (struct sockaddr *) &from.ss, &fromlen);
867         if (len < 0) {
868                 perror("recvfrom[radius_server]");
869                 goto fail;
870         }
871
872 #ifdef CONFIG_IPV6
873         if (data->ipv6) {
874                 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
875                               sizeof(abuf)) == NULL)
876                         abuf[0] = '\0';
877                 from_port = ntohs(from.sin6.sin6_port);
878                 RADIUS_DEBUG("Received %d bytes from %s:%d",
879                              len, abuf, from_port);
880
881                 client = radius_server_get_client(data,
882                                                   (struct in_addr *)
883                                                   &from.sin6.sin6_addr, 1);
884         }
885 #endif /* CONFIG_IPV6 */
886
887         if (!data->ipv6) {
888                 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
889                 from_port = ntohs(from.sin.sin_port);
890                 RADIUS_DEBUG("Received %d bytes from %s:%d",
891                              len, abuf, from_port);
892
893                 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
894         }
895
896         RADIUS_DUMP("Received data", buf, len);
897
898         if (client == NULL) {
899                 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
900                 data->counters.invalid_requests++;
901                 goto fail;
902         }
903
904         msg = radius_msg_parse(buf, len);
905         if (msg == NULL) {
906                 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
907                 data->counters.malformed_access_requests++;
908                 client->counters.malformed_access_requests++;
909                 goto fail;
910         }
911
912         os_free(buf);
913         buf = NULL;
914
915         if (wpa_debug_level <= MSG_MSGDUMP) {
916                 radius_msg_dump(msg);
917         }
918
919         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
920                 RADIUS_DEBUG("Unexpected RADIUS code %d",
921                              radius_msg_get_hdr(msg)->code);
922                 data->counters.unknown_types++;
923                 client->counters.unknown_types++;
924                 goto fail;
925         }
926
927         data->counters.access_requests++;
928         client->counters.access_requests++;
929
930         if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
931                                        client->shared_secret_len, NULL)) {
932                 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
933                 data->counters.bad_authenticators++;
934                 client->counters.bad_authenticators++;
935                 goto fail;
936         }
937
938         if (radius_server_request(data, msg, (struct sockaddr *) &from,
939                                   fromlen, client, abuf, from_port, NULL) ==
940             -2)
941                 return; /* msg was stored with the session */
942
943 fail:
944         radius_msg_free(msg);
945         os_free(buf);
946 }
947
948
949 static int radius_server_disable_pmtu_discovery(int s)
950 {
951         int r = -1;
952 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
953         /* Turn off Path MTU discovery on IPv4/UDP sockets. */
954         int action = IP_PMTUDISC_DONT;
955         r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
956                        sizeof(action));
957         if (r == -1)
958                 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
959                            "%s", strerror(errno));
960 #endif
961         return r;
962 }
963
964
965 static int radius_server_open_socket(int port)
966 {
967         int s;
968         struct sockaddr_in addr;
969
970         s = socket(PF_INET, SOCK_DGRAM, 0);
971         if (s < 0) {
972                 perror("socket");
973                 return -1;
974         }
975
976         radius_server_disable_pmtu_discovery(s);
977
978         os_memset(&addr, 0, sizeof(addr));
979         addr.sin_family = AF_INET;
980         addr.sin_port = htons(port);
981         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
982                 perror("bind");
983                 close(s);
984                 return -1;
985         }
986
987         return s;
988 }
989
990
991 #ifdef CONFIG_IPV6
992 static int radius_server_open_socket6(int port)
993 {
994         int s;
995         struct sockaddr_in6 addr;
996
997         s = socket(PF_INET6, SOCK_DGRAM, 0);
998         if (s < 0) {
999                 perror("socket[IPv6]");
1000                 return -1;
1001         }
1002
1003         os_memset(&addr, 0, sizeof(addr));
1004         addr.sin6_family = AF_INET6;
1005         os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1006         addr.sin6_port = htons(port);
1007         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1008                 perror("bind");
1009                 close(s);
1010                 return -1;
1011         }
1012
1013         return s;
1014 }
1015 #endif /* CONFIG_IPV6 */
1016
1017
1018 static void radius_server_free_sessions(struct radius_server_data *data,
1019                                         struct radius_session *sessions)
1020 {
1021         struct radius_session *session, *prev;
1022
1023         session = sessions;
1024         while (session) {
1025                 prev = session;
1026                 session = session->next;
1027                 radius_server_session_free(data, prev);
1028         }
1029 }
1030
1031
1032 static void radius_server_free_clients(struct radius_server_data *data,
1033                                        struct radius_client *clients)
1034 {
1035         struct radius_client *client, *prev;
1036
1037         client = clients;
1038         while (client) {
1039                 prev = client;
1040                 client = client->next;
1041
1042                 radius_server_free_sessions(data, prev->sessions);
1043                 os_free(prev->shared_secret);
1044                 os_free(prev);
1045         }
1046 }
1047
1048
1049 static struct radius_client *
1050 radius_server_read_clients(const char *client_file, int ipv6)
1051 {
1052         FILE *f;
1053         const int buf_size = 1024;
1054         char *buf, *pos;
1055         struct radius_client *clients, *tail, *entry;
1056         int line = 0, mask, failed = 0, i;
1057         struct in_addr addr;
1058 #ifdef CONFIG_IPV6
1059         struct in6_addr addr6;
1060 #endif /* CONFIG_IPV6 */
1061         unsigned int val;
1062
1063         f = fopen(client_file, "r");
1064         if (f == NULL) {
1065                 RADIUS_ERROR("Could not open client file '%s'", client_file);
1066                 return NULL;
1067         }
1068
1069         buf = os_malloc(buf_size);
1070         if (buf == NULL) {
1071                 fclose(f);
1072                 return NULL;
1073         }
1074
1075         clients = tail = NULL;
1076         while (fgets(buf, buf_size, f)) {
1077                 /* Configuration file format:
1078                  * 192.168.1.0/24 secret
1079                  * 192.168.1.2 secret
1080                  * fe80::211:22ff:fe33:4455/64 secretipv6
1081                  */
1082                 line++;
1083                 buf[buf_size - 1] = '\0';
1084                 pos = buf;
1085                 while (*pos != '\0' && *pos != '\n')
1086                         pos++;
1087                 if (*pos == '\n')
1088                         *pos = '\0';
1089                 if (*buf == '\0' || *buf == '#')
1090                         continue;
1091
1092                 pos = buf;
1093                 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1094                        (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1095                        (*pos >= 'A' && *pos <= 'F')) {
1096                         pos++;
1097                 }
1098
1099                 if (*pos == '\0') {
1100                         failed = 1;
1101                         break;
1102                 }
1103
1104                 if (*pos == '/') {
1105                         char *end;
1106                         *pos++ = '\0';
1107                         mask = strtol(pos, &end, 10);
1108                         if ((pos == end) ||
1109                             (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1110                                 failed = 1;
1111                                 break;
1112                         }
1113                         pos = end;
1114                 } else {
1115                         mask = ipv6 ? 128 : 32;
1116                         *pos++ = '\0';
1117                 }
1118
1119                 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1120                         failed = 1;
1121                         break;
1122                 }
1123 #ifdef CONFIG_IPV6
1124                 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1125                         if (inet_pton(AF_INET, buf, &addr) <= 0) {
1126                                 failed = 1;
1127                                 break;
1128                         }
1129                         /* Convert IPv4 address to IPv6 */
1130                         if (mask <= 32)
1131                                 mask += (128 - 32);
1132                         os_memset(addr6.s6_addr, 0, 10);
1133                         addr6.s6_addr[10] = 0xff;
1134                         addr6.s6_addr[11] = 0xff;
1135                         os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1136                                   4);
1137                 }
1138 #endif /* CONFIG_IPV6 */
1139
1140                 while (*pos == ' ' || *pos == '\t') {
1141                         pos++;
1142                 }
1143
1144                 if (*pos == '\0') {
1145                         failed = 1;
1146                         break;
1147                 }
1148
1149                 entry = os_zalloc(sizeof(*entry));
1150                 if (entry == NULL) {
1151                         failed = 1;
1152                         break;
1153                 }
1154                 entry->shared_secret = os_strdup(pos);
1155                 if (entry->shared_secret == NULL) {
1156                         failed = 1;
1157                         os_free(entry);
1158                         break;
1159                 }
1160                 entry->shared_secret_len = os_strlen(entry->shared_secret);
1161                 entry->addr.s_addr = addr.s_addr;
1162                 if (!ipv6) {
1163                         val = 0;
1164                         for (i = 0; i < mask; i++)
1165                                 val |= 1 << (31 - i);
1166                         entry->mask.s_addr = htonl(val);
1167                 }
1168 #ifdef CONFIG_IPV6
1169                 if (ipv6) {
1170                         int offset = mask / 8;
1171
1172                         os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1173                         os_memset(entry->mask6.s6_addr, 0xff, offset);
1174                         val = 0;
1175                         for (i = 0; i < (mask % 8); i++)
1176                                 val |= 1 << (7 - i);
1177                         if (offset < 16)
1178                                 entry->mask6.s6_addr[offset] = val;
1179                 }
1180 #endif /* CONFIG_IPV6 */
1181
1182                 if (tail == NULL) {
1183                         clients = tail = entry;
1184                 } else {
1185                         tail->next = entry;
1186                         tail = entry;
1187                 }
1188         }
1189
1190         if (failed) {
1191                 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1192                 radius_server_free_clients(NULL, clients);
1193                 clients = NULL;
1194         }
1195
1196         os_free(buf);
1197         fclose(f);
1198
1199         return clients;
1200 }
1201
1202
1203 /**
1204  * radius_server_init - Initialize RADIUS server
1205  * @conf: Configuration for the RADIUS server
1206  * Returns: Pointer to private RADIUS server context or %NULL on failure
1207  *
1208  * This initializes a RADIUS server instance and returns a context pointer that
1209  * will be used in other calls to the RADIUS server module. The server can be
1210  * deinitialize by calling radius_server_deinit().
1211  */
1212 struct radius_server_data *
1213 radius_server_init(struct radius_server_conf *conf)
1214 {
1215         struct radius_server_data *data;
1216
1217 #ifndef CONFIG_IPV6
1218         if (conf->ipv6) {
1219                 fprintf(stderr, "RADIUS server compiled without IPv6 "
1220                         "support.\n");
1221                 return NULL;
1222         }
1223 #endif /* CONFIG_IPV6 */
1224
1225         data = os_zalloc(sizeof(*data));
1226         if (data == NULL)
1227                 return NULL;
1228
1229         os_get_time(&data->start_time);
1230         data->conf_ctx = conf->conf_ctx;
1231         data->eap_sim_db_priv = conf->eap_sim_db_priv;
1232         data->ssl_ctx = conf->ssl_ctx;
1233         data->ipv6 = conf->ipv6;
1234         if (conf->pac_opaque_encr_key) {
1235                 data->pac_opaque_encr_key = os_malloc(16);
1236                 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1237                           16);
1238         }
1239         if (conf->eap_fast_a_id) {
1240                 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1241                 if (data->eap_fast_a_id) {
1242                         os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1243                                   conf->eap_fast_a_id_len);
1244                         data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1245                 }
1246         }
1247         if (conf->eap_fast_a_id_info)
1248                 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1249         data->eap_fast_prov = conf->eap_fast_prov;
1250         data->pac_key_lifetime = conf->pac_key_lifetime;
1251         data->pac_key_refresh_time = conf->pac_key_refresh_time;
1252         data->get_eap_user = conf->get_eap_user;
1253         data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1254         data->tnc = conf->tnc;
1255         data->wps = conf->wps;
1256         if (conf->eap_req_id_text) {
1257                 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1258                 if (data->eap_req_id_text) {
1259                         os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1260                                   conf->eap_req_id_text_len);
1261                         data->eap_req_id_text_len = conf->eap_req_id_text_len;
1262                 }
1263         }
1264
1265         data->clients = radius_server_read_clients(conf->client_file,
1266                                                    conf->ipv6);
1267         if (data->clients == NULL) {
1268                 printf("No RADIUS clients configured.\n");
1269                 radius_server_deinit(data);
1270                 return NULL;
1271         }
1272
1273 #ifdef CONFIG_IPV6
1274         if (conf->ipv6)
1275                 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1276         else
1277 #endif /* CONFIG_IPV6 */
1278         data->auth_sock = radius_server_open_socket(conf->auth_port);
1279         if (data->auth_sock < 0) {
1280                 printf("Failed to open UDP socket for RADIUS authentication "
1281                        "server\n");
1282                 radius_server_deinit(data);
1283                 return NULL;
1284         }
1285         if (eloop_register_read_sock(data->auth_sock,
1286                                      radius_server_receive_auth,
1287                                      data, NULL)) {
1288                 radius_server_deinit(data);
1289                 return NULL;
1290         }
1291
1292         return data;
1293 }
1294
1295
1296 /**
1297  * radius_server_deinit - Deinitialize RADIUS server
1298  * @data: RADIUS server context from radius_server_init()
1299  */
1300 void radius_server_deinit(struct radius_server_data *data)
1301 {
1302         if (data == NULL)
1303                 return;
1304
1305         if (data->auth_sock >= 0) {
1306                 eloop_unregister_read_sock(data->auth_sock);
1307                 close(data->auth_sock);
1308         }
1309
1310         radius_server_free_clients(data, data->clients);
1311
1312         os_free(data->pac_opaque_encr_key);
1313         os_free(data->eap_fast_a_id);
1314         os_free(data->eap_fast_a_id_info);
1315         os_free(data->eap_req_id_text);
1316         os_free(data);
1317 }
1318
1319
1320 /**
1321  * radius_server_get_mib - Get RADIUS server MIB information
1322  * @data: RADIUS server context from radius_server_init()
1323  * @buf: Buffer for returning the MIB data in text format
1324  * @buflen: buf length in octets
1325  * Returns: Number of octets written into buf
1326  */
1327 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1328                           size_t buflen)
1329 {
1330         int ret, uptime;
1331         unsigned int idx;
1332         char *end, *pos;
1333         struct os_time now;
1334         struct radius_client *cli;
1335
1336         /* RFC 2619 - RADIUS Authentication Server MIB */
1337
1338         if (data == NULL || buflen == 0)
1339                 return 0;
1340
1341         pos = buf;
1342         end = buf + buflen;
1343
1344         os_get_time(&now);
1345         uptime = (now.sec - data->start_time.sec) * 100 +
1346                 ((now.usec - data->start_time.usec) / 10000) % 100;
1347         ret = os_snprintf(pos, end - pos,
1348                           "RADIUS-AUTH-SERVER-MIB\n"
1349                           "radiusAuthServIdent=hostapd\n"
1350                           "radiusAuthServUpTime=%d\n"
1351                           "radiusAuthServResetTime=0\n"
1352                           "radiusAuthServConfigReset=4\n",
1353                           uptime);
1354         if (ret < 0 || ret >= end - pos) {
1355                 *pos = '\0';
1356                 return pos - buf;
1357         }
1358         pos += ret;
1359
1360         ret = os_snprintf(pos, end - pos,
1361                           "radiusAuthServTotalAccessRequests=%u\n"
1362                           "radiusAuthServTotalInvalidRequests=%u\n"
1363                           "radiusAuthServTotalDupAccessRequests=%u\n"
1364                           "radiusAuthServTotalAccessAccepts=%u\n"
1365                           "radiusAuthServTotalAccessRejects=%u\n"
1366                           "radiusAuthServTotalAccessChallenges=%u\n"
1367                           "radiusAuthServTotalMalformedAccessRequests=%u\n"
1368                           "radiusAuthServTotalBadAuthenticators=%u\n"
1369                           "radiusAuthServTotalPacketsDropped=%u\n"
1370                           "radiusAuthServTotalUnknownTypes=%u\n",
1371                           data->counters.access_requests,
1372                           data->counters.invalid_requests,
1373                           data->counters.dup_access_requests,
1374                           data->counters.access_accepts,
1375                           data->counters.access_rejects,
1376                           data->counters.access_challenges,
1377                           data->counters.malformed_access_requests,
1378                           data->counters.bad_authenticators,
1379                           data->counters.packets_dropped,
1380                           data->counters.unknown_types);
1381         if (ret < 0 || ret >= end - pos) {
1382                 *pos = '\0';
1383                 return pos - buf;
1384         }
1385         pos += ret;
1386
1387         for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1388                 char abuf[50], mbuf[50];
1389 #ifdef CONFIG_IPV6
1390                 if (data->ipv6) {
1391                         if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1392                                       sizeof(abuf)) == NULL)
1393                                 abuf[0] = '\0';
1394                         if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1395                                       sizeof(mbuf)) == NULL)
1396                                 mbuf[0] = '\0';
1397                 }
1398 #endif /* CONFIG_IPV6 */
1399                 if (!data->ipv6) {
1400                         os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1401                         os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1402                 }
1403
1404                 ret = os_snprintf(pos, end - pos,
1405                                   "radiusAuthClientIndex=%u\n"
1406                                   "radiusAuthClientAddress=%s/%s\n"
1407                                   "radiusAuthServAccessRequests=%u\n"
1408                                   "radiusAuthServDupAccessRequests=%u\n"
1409                                   "radiusAuthServAccessAccepts=%u\n"
1410                                   "radiusAuthServAccessRejects=%u\n"
1411                                   "radiusAuthServAccessChallenges=%u\n"
1412                                   "radiusAuthServMalformedAccessRequests=%u\n"
1413                                   "radiusAuthServBadAuthenticators=%u\n"
1414                                   "radiusAuthServPacketsDropped=%u\n"
1415                                   "radiusAuthServUnknownTypes=%u\n",
1416                                   idx,
1417                                   abuf, mbuf,
1418                                   cli->counters.access_requests,
1419                                   cli->counters.dup_access_requests,
1420                                   cli->counters.access_accepts,
1421                                   cli->counters.access_rejects,
1422                                   cli->counters.access_challenges,
1423                                   cli->counters.malformed_access_requests,
1424                                   cli->counters.bad_authenticators,
1425                                   cli->counters.packets_dropped,
1426                                   cli->counters.unknown_types);
1427                 if (ret < 0 || ret >= end - pos) {
1428                         *pos = '\0';
1429                         return pos - buf;
1430                 }
1431                 pos += ret;
1432         }
1433
1434         return pos - buf;
1435 }
1436
1437
1438 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1439                                       size_t identity_len, int phase2,
1440                                       struct eap_user *user)
1441 {
1442         struct radius_session *sess = ctx;
1443         struct radius_server_data *data = sess->server;
1444
1445         return data->get_eap_user(data->conf_ctx, identity, identity_len,
1446                                   phase2, user);
1447 }
1448
1449
1450 static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1451 {
1452         struct radius_session *sess = ctx;
1453         struct radius_server_data *data = sess->server;
1454         *len = data->eap_req_id_text_len;
1455         return data->eap_req_id_text;
1456 }
1457
1458
1459 static struct eapol_callbacks radius_server_eapol_cb =
1460 {
1461         .get_eap_user = radius_server_get_eap_user,
1462         .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1463 };
1464
1465
1466 /**
1467  * radius_server_eap_pending_cb - Pending EAP data notification
1468  * @data: RADIUS server context from radius_server_init()
1469  * @ctx: Pending EAP context pointer
1470  *
1471  * This function is used to notify EAP server module that a pending operation
1472  * has been completed and processing of the EAP session can proceed.
1473  */
1474 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1475 {
1476         struct radius_client *cli;
1477         struct radius_session *s, *sess = NULL;
1478         struct radius_msg *msg;
1479
1480         if (data == NULL)
1481                 return;
1482
1483         for (cli = data->clients; cli; cli = cli->next) {
1484                 for (s = cli->sessions; s; s = s->next) {
1485                         if (s->eap == ctx && s->last_msg) {
1486                                 sess = s;
1487                                 break;
1488                         }
1489                         if (sess)
1490                                 break;
1491                 }
1492                 if (sess)
1493                         break;
1494         }
1495
1496         if (sess == NULL) {
1497                 RADIUS_DEBUG("No session matched callback ctx");
1498                 return;
1499         }
1500
1501         msg = sess->last_msg;
1502         sess->last_msg = NULL;
1503         eap_sm_pending_cb(sess->eap);
1504         if (radius_server_request(data, msg,
1505                                   (struct sockaddr *) &sess->last_from,
1506                                   sess->last_fromlen, cli,
1507                                   sess->last_from_addr,
1508                                   sess->last_from_port, sess) == -2)
1509                 return; /* msg was stored with the session */
1510
1511         radius_msg_free(msg);
1512 }