EAP-FAST: Allow A-ID and A-ID-Info to be configured separately
[mech_eap.git] / src / radius / radius_server.c
1 /*
2  * hostapd / RADIUS authentication server
3  * Copyright (c) 2005-2008, 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 "defs.h"
22 #include "eap_server/eap.h"
23 #include "radius_server.h"
24
25 #define RADIUS_SESSION_TIMEOUT 60
26 #define RADIUS_MAX_SESSION 100
27 #define RADIUS_MAX_MSG_LEN 3000
28
29 static struct eapol_callbacks radius_server_eapol_cb;
30
31 struct radius_client;
32 struct radius_server_data;
33
34 struct radius_server_counters {
35         u32 access_requests;
36         u32 invalid_requests;
37         u32 dup_access_requests;
38         u32 access_accepts;
39         u32 access_rejects;
40         u32 access_challenges;
41         u32 malformed_access_requests;
42         u32 bad_authenticators;
43         u32 packets_dropped;
44         u32 unknown_types;
45 };
46
47 struct radius_session {
48         struct radius_session *next;
49         struct radius_client *client;
50         struct radius_server_data *server;
51         unsigned int sess_id;
52         struct eap_sm *eap;
53         struct eap_eapol_interface *eap_if;
54
55         struct radius_msg *last_msg;
56         char *last_from_addr;
57         int last_from_port;
58         struct sockaddr_storage last_from;
59         socklen_t last_fromlen;
60         u8 last_identifier;
61         struct radius_msg *last_reply;
62         u8 last_authenticator[16];
63 };
64
65 struct radius_client {
66         struct radius_client *next;
67         struct in_addr addr;
68         struct in_addr mask;
69 #ifdef CONFIG_IPV6
70         struct in6_addr addr6;
71         struct in6_addr mask6;
72 #endif /* CONFIG_IPV6 */
73         char *shared_secret;
74         int shared_secret_len;
75         struct radius_session *sessions;
76         struct radius_server_counters counters;
77 };
78
79 struct radius_server_data {
80         int auth_sock;
81         struct radius_client *clients;
82         unsigned int next_sess_id;
83         void *conf_ctx;
84         int num_sess;
85         void *eap_sim_db_priv;
86         void *ssl_ctx;
87         u8 *pac_opaque_encr_key;
88         u8 *eap_fast_a_id;
89         size_t eap_fast_a_id_len;
90         char *eap_fast_a_id_info;
91         int eap_fast_prov;
92         int pac_key_lifetime;
93         int pac_key_refresh_time;
94         int eap_sim_aka_result_ind;
95         int tnc;
96         int ipv6;
97         struct os_time start_time;
98         struct radius_server_counters counters;
99         int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
100                             int phase2, struct eap_user *user);
101 };
102
103
104 extern int wpa_debug_level;
105
106 #define RADIUS_DEBUG(args...) \
107 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
108 #define RADIUS_ERROR(args...) \
109 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
110 #define RADIUS_DUMP(args...) \
111 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
112 #define RADIUS_DUMP_ASCII(args...) \
113 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
114
115
116 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
117
118
119
120 static struct radius_client *
121 radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
122                          int ipv6)
123 {
124         struct radius_client *client = data->clients;
125
126         while (client) {
127 #ifdef CONFIG_IPV6
128                 if (ipv6) {
129                         struct in6_addr *addr6;
130                         int i;
131
132                         addr6 = (struct in6_addr *) addr;
133                         for (i = 0; i < 16; i++) {
134                                 if ((addr6->s6_addr[i] &
135                                      client->mask6.s6_addr[i]) !=
136                                     (client->addr6.s6_addr[i] &
137                                      client->mask6.s6_addr[i])) {
138                                         i = 17;
139                                         break;
140                                 }
141                         }
142                         if (i == 16) {
143                                 break;
144                         }
145                 }
146 #endif /* CONFIG_IPV6 */
147                 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
148                     (addr->s_addr & client->mask.s_addr)) {
149                         break;
150                 }
151
152                 client = client->next;
153         }
154
155         return client;
156 }
157
158
159 static struct radius_session *
160 radius_server_get_session(struct radius_client *client, unsigned int sess_id)
161 {
162         struct radius_session *sess = client->sessions;
163
164         while (sess) {
165                 if (sess->sess_id == sess_id) {
166                         break;
167                 }
168                 sess = sess->next;
169         }
170
171         return sess;
172 }
173
174
175 static void radius_server_session_free(struct radius_server_data *data,
176                                        struct radius_session *sess)
177 {
178         eloop_cancel_timeout(radius_server_session_timeout, data, sess);
179         eap_server_sm_deinit(sess->eap);
180         if (sess->last_msg) {
181                 radius_msg_free(sess->last_msg);
182                 os_free(sess->last_msg);
183         }
184         os_free(sess->last_from_addr);
185         if (sess->last_reply) {
186                 radius_msg_free(sess->last_reply);
187                 os_free(sess->last_reply);
188         }
189         os_free(sess);
190         data->num_sess--;
191 }
192
193
194 static void radius_server_session_remove_timeout(void *eloop_ctx,
195                                                  void *timeout_ctx);
196
197 static void radius_server_session_remove(struct radius_server_data *data,
198                                          struct radius_session *sess)
199 {
200         struct radius_client *client = sess->client;
201         struct radius_session *session, *prev;
202
203         eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
204
205         prev = NULL;
206         session = client->sessions;
207         while (session) {
208                 if (session == sess) {
209                         if (prev == NULL) {
210                                 client->sessions = sess->next;
211                         } else {
212                                 prev->next = sess->next;
213                         }
214                         radius_server_session_free(data, sess);
215                         break;
216                 }
217                 prev = session;
218                 session = session->next;
219         }
220 }
221
222
223 static void radius_server_session_remove_timeout(void *eloop_ctx,
224                                                  void *timeout_ctx)
225 {
226         struct radius_server_data *data = eloop_ctx;
227         struct radius_session *sess = timeout_ctx;
228         RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
229         radius_server_session_remove(data, sess);
230 }
231
232
233 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
234 {
235         struct radius_server_data *data = eloop_ctx;
236         struct radius_session *sess = timeout_ctx;
237
238         RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
239         radius_server_session_remove(data, sess);
240 }
241
242
243 static struct radius_session *
244 radius_server_new_session(struct radius_server_data *data,
245                           struct radius_client *client)
246 {
247         struct radius_session *sess;
248
249         if (data->num_sess >= RADIUS_MAX_SESSION) {
250                 RADIUS_DEBUG("Maximum number of existing session - no room "
251                              "for a new session");
252                 return NULL;
253         }
254
255         sess = os_zalloc(sizeof(*sess));
256         if (sess == NULL)
257                 return NULL;
258
259         sess->server = data;
260         sess->client = client;
261         sess->sess_id = data->next_sess_id++;
262         sess->next = client->sessions;
263         client->sessions = sess;
264         eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
265                                radius_server_session_timeout, data, sess);
266         data->num_sess++;
267         return sess;
268 }
269
270
271 static struct radius_session *
272 radius_server_get_new_session(struct radius_server_data *data,
273                               struct radius_client *client,
274                               struct radius_msg *msg)
275 {
276         u8 *user;
277         size_t user_len;
278         int res;
279         struct radius_session *sess;
280         struct eap_config eap_conf;
281
282         RADIUS_DEBUG("Creating a new session");
283
284         user = os_malloc(256);
285         if (user == NULL) {
286                 return NULL;
287         }
288         res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
289         if (res < 0 || res > 256) {
290                 RADIUS_DEBUG("Could not get User-Name");
291                 os_free(user);
292                 return NULL;
293         }
294         user_len = res;
295         RADIUS_DUMP_ASCII("User-Name", user, user_len);
296
297         res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
298         os_free(user);
299
300         if (res == 0) {
301                 RADIUS_DEBUG("Matching user entry found");
302                 sess = radius_server_new_session(data, client);
303                 if (sess == NULL) {
304                         RADIUS_DEBUG("Failed to create a new session");
305                         return NULL;
306                 }
307         } else {
308                 RADIUS_DEBUG("User-Name not found from user database");
309                 return NULL;
310         }
311
312         os_memset(&eap_conf, 0, sizeof(eap_conf));
313         eap_conf.ssl_ctx = data->ssl_ctx;
314         eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
315         eap_conf.backend_auth = TRUE;
316         eap_conf.eap_server = 1;
317         eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
318         eap_conf.eap_fast_a_id = data->eap_fast_a_id;
319         eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
320         eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
321         eap_conf.eap_fast_prov = data->eap_fast_prov;
322         eap_conf.pac_key_lifetime = data->pac_key_lifetime;
323         eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
324         eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
325         eap_conf.tnc = data->tnc;
326         sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
327                                        &eap_conf);
328         if (sess->eap == NULL) {
329                 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
330                              "new session");
331                 radius_server_session_free(data, sess);
332                 return NULL;
333         }
334         sess->eap_if = eap_get_interface(sess->eap);
335         sess->eap_if->eapRestart = TRUE;
336         sess->eap_if->portEnabled = TRUE;
337
338         RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
339
340         return sess;
341 }
342
343
344 static struct radius_msg *
345 radius_server_encapsulate_eap(struct radius_server_data *data,
346                               struct radius_client *client,
347                               struct radius_session *sess,
348                               struct radius_msg *request)
349 {
350         struct radius_msg *msg;
351         int code;
352         unsigned int sess_id;
353
354         if (sess->eap_if->eapFail) {
355                 sess->eap_if->eapFail = FALSE;
356                 code = RADIUS_CODE_ACCESS_REJECT;
357         } else if (sess->eap_if->eapSuccess) {
358                 sess->eap_if->eapSuccess = FALSE;
359                 code = RADIUS_CODE_ACCESS_ACCEPT;
360         } else {
361                 sess->eap_if->eapReq = FALSE;
362                 code = RADIUS_CODE_ACCESS_CHALLENGE;
363         }
364
365         msg = radius_msg_new(code, request->hdr->identifier);
366         if (msg == NULL) {
367                 RADIUS_DEBUG("Failed to allocate reply message");
368                 return NULL;
369         }
370
371         sess_id = htonl(sess->sess_id);
372         if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
373             !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
374                                  (u8 *) &sess_id, sizeof(sess_id))) {
375                 RADIUS_DEBUG("Failed to add State attribute");
376         }
377
378         if (sess->eap_if->eapReqData &&
379             !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
380                                 wpabuf_len(sess->eap_if->eapReqData))) {
381                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
382         }
383
384         if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
385                 int len;
386                 if (sess->eap_if->eapKeyDataLen > 64) {
387                         len = 32;
388                 } else {
389                         len = sess->eap_if->eapKeyDataLen / 2;
390                 }
391                 if (!radius_msg_add_mppe_keys(msg, request->hdr->authenticator,
392                                               (u8 *) client->shared_secret,
393                                               client->shared_secret_len,
394                                               sess->eap_if->eapKeyData + len,
395                                               len, sess->eap_if->eapKeyData,
396                                               len)) {
397                         RADIUS_DEBUG("Failed to add MPPE key attributes");
398                 }
399         }
400
401         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
402                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
403                 radius_msg_free(msg);
404                 os_free(msg);
405                 return NULL;
406         }
407
408         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
409                                   client->shared_secret_len,
410                                   request->hdr->authenticator) < 0) {
411                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
412         }
413
414         return msg;
415 }
416
417
418 static int radius_server_reject(struct radius_server_data *data,
419                                 struct radius_client *client,
420                                 struct radius_msg *request,
421                                 struct sockaddr *from, socklen_t fromlen,
422                                 const char *from_addr, int from_port)
423 {
424         struct radius_msg *msg;
425         int ret = 0;
426         struct eap_hdr eapfail;
427
428         RADIUS_DEBUG("Reject invalid request from %s:%d",
429                      from_addr, from_port);
430
431         msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT,
432                              request->hdr->identifier);
433         if (msg == NULL) {
434                 return -1;
435         }
436
437         os_memset(&eapfail, 0, sizeof(eapfail));
438         eapfail.code = EAP_CODE_FAILURE;
439         eapfail.identifier = 0;
440         eapfail.length = host_to_be16(sizeof(eapfail));
441
442         if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
443                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
444         }
445
446         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
447                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
448                 radius_msg_free(msg);
449                 os_free(msg);
450                 return -1;
451         }
452
453         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
454                                   client->shared_secret_len,
455                                   request->hdr->authenticator) < 0) {
456                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
457         }
458
459         if (wpa_debug_level <= MSG_MSGDUMP) {
460                 radius_msg_dump(msg);
461         }
462
463         data->counters.access_rejects++;
464         client->counters.access_rejects++;
465         if (sendto(data->auth_sock, msg->buf, msg->buf_used, 0,
466                    (struct sockaddr *) from, sizeof(*from)) < 0) {
467                 perror("sendto[RADIUS SRV]");
468                 ret = -1;
469         }
470
471         radius_msg_free(msg);
472         os_free(msg);
473
474         return ret;
475 }
476
477
478 static int radius_server_request(struct radius_server_data *data,
479                                  struct radius_msg *msg,
480                                  struct sockaddr *from, socklen_t fromlen,
481                                  struct radius_client *client,
482                                  const char *from_addr, int from_port,
483                                  struct radius_session *force_sess)
484 {
485         u8 *eap = NULL;
486         size_t eap_len;
487         int res, state_included = 0;
488         u8 statebuf[4];
489         unsigned int state;
490         struct radius_session *sess;
491         struct radius_msg *reply;
492
493         if (force_sess)
494                 sess = force_sess;
495         else {
496                 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
497                                           sizeof(statebuf));
498                 state_included = res >= 0;
499                 if (res == sizeof(statebuf)) {
500                         state = WPA_GET_BE32(statebuf);
501                         sess = radius_server_get_session(client, state);
502                 } else {
503                         sess = NULL;
504                 }
505         }
506
507         if (sess) {
508                 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
509         } else if (state_included) {
510                 RADIUS_DEBUG("State attribute included but no session found");
511                 radius_server_reject(data, client, msg, from, fromlen,
512                                      from_addr, from_port);
513                 return -1;
514         } else {
515                 sess = radius_server_get_new_session(data, client, msg);
516                 if (sess == NULL) {
517                         RADIUS_DEBUG("Could not create a new session");
518                         radius_server_reject(data, client, msg, from, fromlen,
519                                              from_addr, from_port);
520                         return -1;
521                 }
522         }
523
524         if (sess->last_from_port == from_port &&
525             sess->last_identifier == msg->hdr->identifier &&
526             os_memcmp(sess->last_authenticator, msg->hdr->authenticator, 16) ==
527             0) {
528                 RADIUS_DEBUG("Duplicate message from %s", from_addr);
529                 data->counters.dup_access_requests++;
530                 client->counters.dup_access_requests++;
531
532                 if (sess->last_reply) {
533                         res = sendto(data->auth_sock, sess->last_reply->buf,
534                                      sess->last_reply->buf_used, 0,
535                                      (struct sockaddr *) from, fromlen);
536                         if (res < 0) {
537                                 perror("sendto[RADIUS SRV]");
538                         }
539                         return 0;
540                 }
541
542                 RADIUS_DEBUG("No previous reply available for duplicate "
543                              "message");
544                 return -1;
545         }
546                       
547         eap = radius_msg_get_eap(msg, &eap_len);
548         if (eap == NULL) {
549                 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
550                              from_addr);
551                 data->counters.packets_dropped++;
552                 client->counters.packets_dropped++;
553                 return -1;
554         }
555
556         RADIUS_DUMP("Received EAP data", eap, eap_len);
557
558         /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
559          * RFC3579 Sect. 2.6.2.
560          * Include EAP-Response/Nak with no preferred method if
561          * code == request.
562          * If code is not 1-4, discard the packet silently.
563          * Or is this already done by the EAP state machine? */
564
565         wpabuf_free(sess->eap_if->eapRespData);
566         sess->eap_if->eapRespData = wpabuf_alloc_ext_data(eap, eap_len);
567         if (sess->eap_if->eapRespData == NULL)
568                 os_free(eap);
569         eap = NULL;
570         sess->eap_if->eapResp = TRUE;
571         eap_server_sm_step(sess->eap);
572
573         if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
574              sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
575                 RADIUS_DUMP("EAP data from the state machine",
576                             wpabuf_head(sess->eap_if->eapReqData),
577                             wpabuf_len(sess->eap_if->eapReqData));
578         } else if (sess->eap_if->eapFail) {
579                 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
580                              "set");
581         } else if (eap_sm_method_pending(sess->eap)) {
582                 if (sess->last_msg) {
583                         radius_msg_free(sess->last_msg);
584                         os_free(sess->last_msg);
585                 }
586                 sess->last_msg = msg;
587                 sess->last_from_port = from_port;
588                 os_free(sess->last_from_addr);
589                 sess->last_from_addr = os_strdup(from_addr);
590                 sess->last_fromlen = fromlen;
591                 os_memcpy(&sess->last_from, from, fromlen);
592                 return -2;
593         } else {
594                 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
595                              " Access-Request silently (assuming it was a "
596                              "duplicate)");
597                 data->counters.packets_dropped++;
598                 client->counters.packets_dropped++;
599                 return -1;
600         }
601
602         reply = radius_server_encapsulate_eap(data, client, sess, msg);
603
604         if (reply) {
605                 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
606                 if (wpa_debug_level <= MSG_MSGDUMP) {
607                         radius_msg_dump(reply);
608                 }
609
610                 switch (reply->hdr->code) {
611                 case RADIUS_CODE_ACCESS_ACCEPT:
612                         data->counters.access_accepts++;
613                         client->counters.access_accepts++;
614                         break;
615                 case RADIUS_CODE_ACCESS_REJECT:
616                         data->counters.access_rejects++;
617                         client->counters.access_rejects++;
618                         break;
619                 case RADIUS_CODE_ACCESS_CHALLENGE:
620                         data->counters.access_challenges++;
621                         client->counters.access_challenges++;
622                         break;
623                 }
624                 res = sendto(data->auth_sock, reply->buf, reply->buf_used, 0,
625                              (struct sockaddr *) from, fromlen);
626                 if (res < 0) {
627                         perror("sendto[RADIUS SRV]");
628                 }
629                 if (sess->last_reply) {
630                         radius_msg_free(sess->last_reply);
631                         os_free(sess->last_reply);
632                 }
633                 sess->last_reply = reply;
634                 sess->last_from_port = from_port;
635                 sess->last_identifier = msg->hdr->identifier;
636                 os_memcpy(sess->last_authenticator, msg->hdr->authenticator,
637                           16);
638         } else {
639                 data->counters.packets_dropped++;
640                 client->counters.packets_dropped++;
641         }
642
643         if (sess->eap_if->eapSuccess || sess->eap_if->eapFail) {
644                 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
645                              sess->sess_id);
646                 eloop_cancel_timeout(radius_server_session_remove_timeout,
647                                      data, sess);
648                 eloop_register_timeout(10, 0,
649                                        radius_server_session_remove_timeout,
650                                        data, sess);
651         }
652
653         return 0;
654 }
655
656
657 static void radius_server_receive_auth(int sock, void *eloop_ctx,
658                                        void *sock_ctx)
659 {
660         struct radius_server_data *data = eloop_ctx;
661         u8 *buf = NULL;
662         struct sockaddr_storage from;
663         socklen_t fromlen;
664         int len;
665         struct radius_client *client = NULL;
666         struct radius_msg *msg = NULL;
667         char abuf[50];
668         int from_port = 0;
669
670         buf = os_malloc(RADIUS_MAX_MSG_LEN);
671         if (buf == NULL) {
672                 goto fail;
673         }
674
675         fromlen = sizeof(from);
676         len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
677                        (struct sockaddr *) &from, &fromlen);
678         if (len < 0) {
679                 perror("recvfrom[radius_server]");
680                 goto fail;
681         }
682
683 #ifdef CONFIG_IPV6
684         if (data->ipv6) {
685                 struct sockaddr_in6 *from6 = (struct sockaddr_in6 *) &from;
686                 if (inet_ntop(AF_INET6, &from6->sin6_addr, abuf, sizeof(abuf))
687                     == NULL)
688                         abuf[0] = '\0';
689                 from_port = ntohs(from6->sin6_port);
690                 RADIUS_DEBUG("Received %d bytes from %s:%d",
691                              len, abuf, from_port);
692
693                 client = radius_server_get_client(data,
694                                                   (struct in_addr *)
695                                                   &from6->sin6_addr, 1);
696         }
697 #endif /* CONFIG_IPV6 */
698
699         if (!data->ipv6) {
700                 struct sockaddr_in *from4 = (struct sockaddr_in *) &from;
701                 os_strlcpy(abuf, inet_ntoa(from4->sin_addr), sizeof(abuf));
702                 from_port = ntohs(from4->sin_port);
703                 RADIUS_DEBUG("Received %d bytes from %s:%d",
704                              len, abuf, from_port);
705
706                 client = radius_server_get_client(data, &from4->sin_addr, 0);
707         }
708
709         RADIUS_DUMP("Received data", buf, len);
710
711         if (client == NULL) {
712                 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
713                 data->counters.invalid_requests++;
714                 goto fail;
715         }
716
717         msg = radius_msg_parse(buf, len);
718         if (msg == NULL) {
719                 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
720                 data->counters.malformed_access_requests++;
721                 client->counters.malformed_access_requests++;
722                 goto fail;
723         }
724
725         os_free(buf);
726         buf = NULL;
727
728         if (wpa_debug_level <= MSG_MSGDUMP) {
729                 radius_msg_dump(msg);
730         }
731
732         if (msg->hdr->code != RADIUS_CODE_ACCESS_REQUEST) {
733                 RADIUS_DEBUG("Unexpected RADIUS code %d", msg->hdr->code);
734                 data->counters.unknown_types++;
735                 client->counters.unknown_types++;
736                 goto fail;
737         }
738
739         data->counters.access_requests++;
740         client->counters.access_requests++;
741
742         if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
743                                        client->shared_secret_len, NULL)) {
744                 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
745                 data->counters.bad_authenticators++;
746                 client->counters.bad_authenticators++;
747                 goto fail;
748         }
749
750         if (radius_server_request(data, msg, (struct sockaddr *) &from,
751                                   fromlen, client, abuf, from_port, NULL) ==
752             -2)
753                 return; /* msg was stored with the session */
754
755 fail:
756         if (msg) {
757                 radius_msg_free(msg);
758                 os_free(msg);
759         }
760         os_free(buf);
761 }
762
763
764 static int radius_server_open_socket(int port)
765 {
766         int s;
767         struct sockaddr_in addr;
768
769         s = socket(PF_INET, SOCK_DGRAM, 0);
770         if (s < 0) {
771                 perror("socket");
772                 return -1;
773         }
774
775         os_memset(&addr, 0, sizeof(addr));
776         addr.sin_family = AF_INET;
777         addr.sin_port = htons(port);
778         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
779                 perror("bind");
780                 close(s);
781                 return -1;
782         }
783
784         return s;
785 }
786
787
788 #ifdef CONFIG_IPV6
789 static int radius_server_open_socket6(int port)
790 {
791         int s;
792         struct sockaddr_in6 addr;
793
794         s = socket(PF_INET6, SOCK_DGRAM, 0);
795         if (s < 0) {
796                 perror("socket[IPv6]");
797                 return -1;
798         }
799
800         os_memset(&addr, 0, sizeof(addr));
801         addr.sin6_family = AF_INET6;
802         os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
803         addr.sin6_port = htons(port);
804         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
805                 perror("bind");
806                 close(s);
807                 return -1;
808         }
809
810         return s;
811 }
812 #endif /* CONFIG_IPV6 */
813
814
815 static void radius_server_free_sessions(struct radius_server_data *data,
816                                         struct radius_session *sessions)
817 {
818         struct radius_session *session, *prev;
819
820         session = sessions;
821         while (session) {
822                 prev = session;
823                 session = session->next;
824                 radius_server_session_free(data, prev);
825         }
826 }
827
828
829 static void radius_server_free_clients(struct radius_server_data *data,
830                                        struct radius_client *clients)
831 {
832         struct radius_client *client, *prev;
833
834         client = clients;
835         while (client) {
836                 prev = client;
837                 client = client->next;
838
839                 radius_server_free_sessions(data, prev->sessions);
840                 os_free(prev->shared_secret);
841                 os_free(prev);
842         }
843 }
844
845
846 static struct radius_client *
847 radius_server_read_clients(const char *client_file, int ipv6)
848 {
849         FILE *f;
850         const int buf_size = 1024;
851         char *buf, *pos;
852         struct radius_client *clients, *tail, *entry;
853         int line = 0, mask, failed = 0, i;
854         struct in_addr addr;
855 #ifdef CONFIG_IPV6
856         struct in6_addr addr6;
857 #endif /* CONFIG_IPV6 */
858         unsigned int val;
859
860         f = fopen(client_file, "r");
861         if (f == NULL) {
862                 RADIUS_ERROR("Could not open client file '%s'", client_file);
863                 return NULL;
864         }
865
866         buf = os_malloc(buf_size);
867         if (buf == NULL) {
868                 fclose(f);
869                 return NULL;
870         }
871
872         clients = tail = NULL;
873         while (fgets(buf, buf_size, f)) {
874                 /* Configuration file format:
875                  * 192.168.1.0/24 secret
876                  * 192.168.1.2 secret
877                  * fe80::211:22ff:fe33:4455/64 secretipv6
878                  */
879                 line++;
880                 buf[buf_size - 1] = '\0';
881                 pos = buf;
882                 while (*pos != '\0' && *pos != '\n')
883                         pos++;
884                 if (*pos == '\n')
885                         *pos = '\0';
886                 if (*buf == '\0' || *buf == '#')
887                         continue;
888
889                 pos = buf;
890                 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
891                        (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
892                        (*pos >= 'A' && *pos <= 'F')) {
893                         pos++;
894                 }
895
896                 if (*pos == '\0') {
897                         failed = 1;
898                         break;
899                 }
900
901                 if (*pos == '/') {
902                         char *end;
903                         *pos++ = '\0';
904                         mask = strtol(pos, &end, 10);
905                         if ((pos == end) ||
906                             (mask < 0 || mask > (ipv6 ? 128 : 32))) {
907                                 failed = 1;
908                                 break;
909                         }
910                         pos = end;
911                 } else {
912                         mask = ipv6 ? 128 : 32;
913                         *pos++ = '\0';
914                 }
915
916                 if (!ipv6 && inet_aton(buf, &addr) == 0) {
917                         failed = 1;
918                         break;
919                 }
920 #ifdef CONFIG_IPV6
921                 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
922                         if (inet_pton(AF_INET, buf, &addr) <= 0) {
923                                 failed = 1;
924                                 break;
925                         }
926                         /* Convert IPv4 address to IPv6 */
927                         if (mask <= 32)
928                                 mask += (128 - 32);
929                         os_memset(addr6.s6_addr, 0, 10);
930                         addr6.s6_addr[10] = 0xff;
931                         addr6.s6_addr[11] = 0xff;
932                         os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
933                                   4);
934                 }
935 #endif /* CONFIG_IPV6 */
936
937                 while (*pos == ' ' || *pos == '\t') {
938                         pos++;
939                 }
940
941                 if (*pos == '\0') {
942                         failed = 1;
943                         break;
944                 }
945
946                 entry = os_zalloc(sizeof(*entry));
947                 if (entry == NULL) {
948                         failed = 1;
949                         break;
950                 }
951                 entry->shared_secret = os_strdup(pos);
952                 if (entry->shared_secret == NULL) {
953                         failed = 1;
954                         os_free(entry);
955                         break;
956                 }
957                 entry->shared_secret_len = os_strlen(entry->shared_secret);
958                 entry->addr.s_addr = addr.s_addr;
959                 if (!ipv6) {
960                         val = 0;
961                         for (i = 0; i < mask; i++)
962                                 val |= 1 << (31 - i);
963                         entry->mask.s_addr = htonl(val);
964                 }
965 #ifdef CONFIG_IPV6
966                 if (ipv6) {
967                         int offset = mask / 8;
968
969                         os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
970                         os_memset(entry->mask6.s6_addr, 0xff, offset);
971                         val = 0;
972                         for (i = 0; i < (mask % 8); i++)
973                                 val |= 1 << (7 - i);
974                         if (offset < 16)
975                                 entry->mask6.s6_addr[offset] = val;
976                 }
977 #endif /* CONFIG_IPV6 */
978
979                 if (tail == NULL) {
980                         clients = tail = entry;
981                 } else {
982                         tail->next = entry;
983                         tail = entry;
984                 }
985         }
986
987         if (failed) {
988                 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
989                 radius_server_free_clients(NULL, clients);
990                 clients = NULL;
991         }
992
993         os_free(buf);
994         fclose(f);
995
996         return clients;
997 }
998
999
1000 struct radius_server_data *
1001 radius_server_init(struct radius_server_conf *conf)
1002 {
1003         struct radius_server_data *data;
1004
1005 #ifndef CONFIG_IPV6
1006         if (conf->ipv6) {
1007                 fprintf(stderr, "RADIUS server compiled without IPv6 "
1008                         "support.\n");
1009                 return NULL;
1010         }
1011 #endif /* CONFIG_IPV6 */
1012
1013         data = os_zalloc(sizeof(*data));
1014         if (data == NULL)
1015                 return NULL;
1016
1017         os_get_time(&data->start_time);
1018         data->conf_ctx = conf->conf_ctx;
1019         data->eap_sim_db_priv = conf->eap_sim_db_priv;
1020         data->ssl_ctx = conf->ssl_ctx;
1021         data->ipv6 = conf->ipv6;
1022         if (conf->pac_opaque_encr_key) {
1023                 data->pac_opaque_encr_key = os_malloc(16);
1024                 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1025                           16);
1026         }
1027         if (conf->eap_fast_a_id) {
1028                 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1029                 if (data->eap_fast_a_id) {
1030                         os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1031                                   conf->eap_fast_a_id_len);
1032                         data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1033                 }
1034         }
1035         if (conf->eap_fast_a_id_info)
1036                 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1037         data->eap_fast_prov = conf->eap_fast_prov;
1038         data->pac_key_lifetime = conf->pac_key_lifetime;
1039         data->pac_key_refresh_time = conf->pac_key_refresh_time;
1040         data->get_eap_user = conf->get_eap_user;
1041         data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1042         data->tnc = conf->tnc;
1043
1044         data->clients = radius_server_read_clients(conf->client_file,
1045                                                    conf->ipv6);
1046         if (data->clients == NULL) {
1047                 printf("No RADIUS clients configured.\n");
1048                 radius_server_deinit(data);
1049                 return NULL;
1050         }
1051
1052 #ifdef CONFIG_IPV6
1053         if (conf->ipv6)
1054                 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1055         else
1056 #endif /* CONFIG_IPV6 */
1057         data->auth_sock = radius_server_open_socket(conf->auth_port);
1058         if (data->auth_sock < 0) {
1059                 printf("Failed to open UDP socket for RADIUS authentication "
1060                        "server\n");
1061                 radius_server_deinit(data);
1062                 return NULL;
1063         }
1064         if (eloop_register_read_sock(data->auth_sock,
1065                                      radius_server_receive_auth,
1066                                      data, NULL)) {
1067                 radius_server_deinit(data);
1068                 return NULL;
1069         }
1070
1071         return data;
1072 }
1073
1074
1075 void radius_server_deinit(struct radius_server_data *data)
1076 {
1077         if (data == NULL)
1078                 return;
1079
1080         if (data->auth_sock >= 0) {
1081                 eloop_unregister_read_sock(data->auth_sock);
1082                 close(data->auth_sock);
1083         }
1084
1085         radius_server_free_clients(data, data->clients);
1086
1087         os_free(data->pac_opaque_encr_key);
1088         os_free(data->eap_fast_a_id);
1089         os_free(data->eap_fast_a_id_info);
1090         os_free(data);
1091 }
1092
1093
1094 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1095                           size_t buflen)
1096 {
1097         int ret, uptime;
1098         unsigned int idx;
1099         char *end, *pos;
1100         struct os_time now;
1101         struct radius_client *cli;
1102
1103         /* RFC 2619 - RADIUS Authentication Server MIB */
1104
1105         if (data == NULL || buflen == 0)
1106                 return 0;
1107
1108         pos = buf;
1109         end = buf + buflen;
1110
1111         os_get_time(&now);
1112         uptime = (now.sec - data->start_time.sec) * 100 +
1113                 ((now.usec - data->start_time.usec) / 10000) % 100;
1114         ret = os_snprintf(pos, end - pos,
1115                           "RADIUS-AUTH-SERVER-MIB\n"
1116                           "radiusAuthServIdent=hostapd\n"
1117                           "radiusAuthServUpTime=%d\n"
1118                           "radiusAuthServResetTime=0\n"
1119                           "radiusAuthServConfigReset=4\n",
1120                           uptime);
1121         if (ret < 0 || ret >= end - pos) {
1122                 *pos = '\0';
1123                 return pos - buf;
1124         }
1125         pos += ret;
1126
1127         ret = os_snprintf(pos, end - pos,
1128                           "radiusAuthServTotalAccessRequests=%u\n"
1129                           "radiusAuthServTotalInvalidRequests=%u\n"
1130                           "radiusAuthServTotalDupAccessRequests=%u\n"
1131                           "radiusAuthServTotalAccessAccepts=%u\n"
1132                           "radiusAuthServTotalAccessRejects=%u\n"
1133                           "radiusAuthServTotalAccessChallenges=%u\n"
1134                           "radiusAuthServTotalMalformedAccessRequests=%u\n"
1135                           "radiusAuthServTotalBadAuthenticators=%u\n"
1136                           "radiusAuthServTotalPacketsDropped=%u\n"
1137                           "radiusAuthServTotalUnknownTypes=%u\n",
1138                           data->counters.access_requests,
1139                           data->counters.invalid_requests,
1140                           data->counters.dup_access_requests,
1141                           data->counters.access_accepts,
1142                           data->counters.access_rejects,
1143                           data->counters.access_challenges,
1144                           data->counters.malformed_access_requests,
1145                           data->counters.bad_authenticators,
1146                           data->counters.packets_dropped,
1147                           data->counters.unknown_types);
1148         if (ret < 0 || ret >= end - pos) {
1149                 *pos = '\0';
1150                 return pos - buf;
1151         }
1152         pos += ret;
1153
1154         for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1155                 char abuf[50], mbuf[50];
1156 #ifdef CONFIG_IPV6
1157                 if (data->ipv6) {
1158                         if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1159                                       sizeof(abuf)) == NULL)
1160                                 abuf[0] = '\0';
1161                         if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1162                                       sizeof(mbuf)) == NULL)
1163                                 mbuf[0] = '\0';
1164                 }
1165 #endif /* CONFIG_IPV6 */
1166                 if (!data->ipv6) {
1167                         os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1168                         os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1169                 }
1170
1171                 ret = os_snprintf(pos, end - pos,
1172                                   "radiusAuthClientIndex=%u\n"
1173                                   "radiusAuthClientAddress=%s/%s\n"
1174                                   "radiusAuthServAccessRequests=%u\n"
1175                                   "radiusAuthServDupAccessRequests=%u\n"
1176                                   "radiusAuthServAccessAccepts=%u\n"
1177                                   "radiusAuthServAccessRejects=%u\n"
1178                                   "radiusAuthServAccessChallenges=%u\n"
1179                                   "radiusAuthServMalformedAccessRequests=%u\n"
1180                                   "radiusAuthServBadAuthenticators=%u\n"
1181                                   "radiusAuthServPacketsDropped=%u\n"
1182                                   "radiusAuthServUnknownTypes=%u\n",
1183                                   idx,
1184                                   abuf, mbuf,
1185                                   cli->counters.access_requests,
1186                                   cli->counters.dup_access_requests,
1187                                   cli->counters.access_accepts,
1188                                   cli->counters.access_rejects,
1189                                   cli->counters.access_challenges,
1190                                   cli->counters.malformed_access_requests,
1191                                   cli->counters.bad_authenticators,
1192                                   cli->counters.packets_dropped,
1193                                   cli->counters.unknown_types);
1194                 if (ret < 0 || ret >= end - pos) {
1195                         *pos = '\0';
1196                         return pos - buf;
1197                 }
1198                 pos += ret;
1199         }
1200
1201         return pos - buf;
1202 }
1203
1204
1205 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1206                                       size_t identity_len, int phase2,
1207                                       struct eap_user *user)
1208 {
1209         struct radius_session *sess = ctx;
1210         struct radius_server_data *data = sess->server;
1211
1212         return data->get_eap_user(data->conf_ctx, identity, identity_len,
1213                                   phase2, user);
1214 }
1215
1216
1217 static struct eapol_callbacks radius_server_eapol_cb =
1218 {
1219         .get_eap_user = radius_server_get_eap_user,
1220 };
1221
1222
1223 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1224 {
1225         struct radius_client *cli;
1226         struct radius_session *s, *sess = NULL;
1227         struct radius_msg *msg;
1228
1229         if (data == NULL)
1230                 return;
1231
1232         for (cli = data->clients; cli; cli = cli->next) {
1233                 for (s = cli->sessions; s; s = s->next) {
1234                         if (s->eap == ctx && s->last_msg) {
1235                                 sess = s;
1236                                 break;
1237                         }
1238                         if (sess)
1239                                 break;
1240                 }
1241                 if (sess)
1242                         break;
1243         }
1244
1245         if (sess == NULL) {
1246                 RADIUS_DEBUG("No session matched callback ctx");
1247                 return;
1248         }
1249
1250         msg = sess->last_msg;
1251         sess->last_msg = NULL;
1252         eap_sm_pending_cb(sess->eap);
1253         if (radius_server_request(data, msg,
1254                                   (struct sockaddr *) &sess->last_from,
1255                                   sess->last_fromlen, cli,
1256                                   sess->last_from_addr,
1257                                   sess->last_from_port, sess) == -2)
1258                 return; /* msg was stored with the session */
1259
1260         radius_msg_free(msg);
1261         os_free(msg);
1262 }