Remove src/common from default header file path
[libeap.git] / hostapd / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-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
17 #include "eloop.h"
18 #include "hostapd.h"
19 #include "ieee802_1x.h"
20 #include "beacon.h"
21 #include "hw_features.h"
22 #include "accounting.h"
23 #include "eapol_sm.h"
24 #include "iapp.h"
25 #include "common/ieee802_11_defs.h"
26 #include "ieee802_11_auth.h"
27 #include "sta_info.h"
28 #include "ap_list.h"
29 #include "driver_i.h"
30 #include "radius/radius_client.h"
31 #include "radius/radius_server.h"
32 #include "wpa.h"
33 #include "preauth.h"
34 #include "vlan_init.h"
35 #include "ctrl_iface.h"
36 #include "tls.h"
37 #include "eap_server/eap_sim_db.h"
38 #include "eap_server/eap.h"
39 #include "eap_server/tncs.h"
40 #include "l2_packet/l2_packet.h"
41 #include "wps_hostapd.h"
42 #include "tkip_countermeasures.h"
43
44
45 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
46                                        size_t identity_len, int phase2,
47                                        struct eap_user *user);
48 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
49 static int hostapd_setup_wpa(struct hostapd_data *hapd);
50 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
51
52 extern int wpa_debug_level;
53
54
55 #ifdef EAP_SERVER
56 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
57                                  struct sta_info *sta, void *ctx)
58 {
59         if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
60                 return 1;
61         return 0;
62 }
63
64
65 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
66 {
67         struct hostapd_data *hapd = ctx;
68         if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
69                 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
70 }
71 #endif /* EAP_SERVER */
72
73
74 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
75                                   struct wpa_auth_config *wconf)
76 {
77         wconf->wpa = conf->wpa;
78         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
79         wconf->wpa_pairwise = conf->wpa_pairwise;
80         wconf->wpa_group = conf->wpa_group;
81         wconf->wpa_group_rekey = conf->wpa_group_rekey;
82         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
83         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
84         wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
85         wconf->rsn_pairwise = conf->rsn_pairwise;
86         wconf->rsn_preauth = conf->rsn_preauth;
87         wconf->eapol_version = conf->eapol_version;
88         wconf->peerkey = conf->peerkey;
89         wconf->wmm_enabled = conf->wmm_enabled;
90         wconf->okc = conf->okc;
91 #ifdef CONFIG_IEEE80211W
92         wconf->ieee80211w = conf->ieee80211w;
93 #endif /* CONFIG_IEEE80211W */
94 #ifdef CONFIG_IEEE80211R
95         wconf->ssid_len = conf->ssid.ssid_len;
96         if (wconf->ssid_len > SSID_LEN)
97                 wconf->ssid_len = SSID_LEN;
98         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
99         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
100                   MOBILITY_DOMAIN_ID_LEN);
101         if (conf->nas_identifier &&
102             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
103                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
104                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
105                           wconf->r0_key_holder_len);
106         }
107         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
108         wconf->r0_key_lifetime = conf->r0_key_lifetime;
109         wconf->reassociation_deadline = conf->reassociation_deadline;
110         wconf->r0kh_list = conf->r0kh_list;
111         wconf->r1kh_list = conf->r1kh_list;
112         wconf->pmk_r1_push = conf->pmk_r1_push;
113 #endif /* CONFIG_IEEE80211R */
114 }
115
116
117 int hostapd_reload_config(struct hostapd_iface *iface)
118 {
119         struct hostapd_data *hapd = iface->bss[0];
120         struct hostapd_config *newconf, *oldconf;
121         struct wpa_auth_config wpa_auth_conf;
122         size_t j;
123
124         newconf = hostapd_config_read(iface->config_fname);
125         if (newconf == NULL)
126                 return -1;
127
128         /*
129          * Deauthenticate all stations since the new configuration may not
130          * allow them to use the BSS anymore.
131          */
132         for (j = 0; j < iface->num_bss; j++)
133                 hostapd_flush_old_stations(iface->bss[j]);
134
135         /* TODO: update dynamic data based on changed configuration
136          * items (e.g., open/close sockets, etc.) */
137         radius_client_flush(hapd->radius, 0);
138
139         oldconf = hapd->iconf;
140         hapd->iconf = newconf;
141         hapd->conf = &newconf->bss[0];
142         iface->conf = newconf;
143
144         if (hostapd_setup_wpa_psk(hapd->conf)) {
145                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
146                            "after reloading configuration");
147         }
148
149         if (hapd->conf->wpa && hapd->wpa_auth == NULL)
150                 hostapd_setup_wpa(hapd);
151         else if (hapd->conf->wpa) {
152                 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
153                 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
154         } else if (hapd->wpa_auth) {
155                 wpa_deinit(hapd->wpa_auth);
156                 hapd->wpa_auth = NULL;
157                 hostapd_set_privacy(hapd, 0);
158                 hostapd_setup_encryption(hapd->conf->iface, hapd);
159         }
160
161         ieee802_11_set_beacon(hapd);
162
163         if (hapd->conf->ssid.ssid_set &&
164             hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
165                              hapd->conf->ssid.ssid_len)) {
166                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
167                 /* try to continue */
168         }
169
170         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
171                 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1);
172
173         hostapd_config_free(oldconf);
174
175         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
176
177         return 0;
178 }
179
180
181 int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
182 {
183         if (hostapd_reload_config(iface) < 0) {
184                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
185                            "file - continuing with old.");
186         }
187         return 0;
188 }
189
190
191 #ifdef HOSTAPD_DUMP_STATE
192 /**
193  * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
194  */
195 static void hostapd_dump_state(struct hostapd_data *hapd)
196 {
197         FILE *f;
198         time_t now;
199         struct sta_info *sta;
200         int i;
201         char *buf;
202
203         if (!hapd->conf->dump_log_name) {
204                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
205                            "request");
206                 return;
207         }
208
209         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
210                    hapd->conf->dump_log_name);
211         f = fopen(hapd->conf->dump_log_name, "w");
212         if (f == NULL) {
213                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
214                            "writing.", hapd->conf->dump_log_name);
215                 return;
216         }
217
218         time(&now);
219         fprintf(f, "hostapd state dump - %s", ctime(&now));
220         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
221                 "num_sta_no_short_slot_time=%d\n"
222                 "num_sta_no_short_preamble=%d\n",
223                 hapd->num_sta, hapd->iface->num_sta_non_erp,
224                 hapd->iface->num_sta_no_short_slot_time,
225                 hapd->iface->num_sta_no_short_preamble);
226
227         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
228                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
229
230                 fprintf(f,
231                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
232                         "  capability=0x%x listen_interval=%d\n",
233                         sta->aid,
234                         sta->flags,
235                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
236                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
237                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
238                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
239                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
240                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
241                          ""),
242                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
243                          ""),
244                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
245                          "[SHORT_PREAMBLE]" : ""),
246                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
247                         (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
248                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
249                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
250                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
251                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
252                         sta->capability,
253                         sta->listen_interval);
254
255                 fprintf(f, "  supported_rates=");
256                 for (i = 0; i < sta->supported_rates_len; i++)
257                         fprintf(f, "%02x ", sta->supported_rates[i]);
258                 fprintf(f, "\n");
259
260                 fprintf(f,
261                         "  timeout_next=%s\n",
262                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
263                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
264                           "DEAUTH")));
265
266                 ieee802_1x_dump_state(f, "  ", sta);
267         }
268
269         buf = os_malloc(4096);
270         if (buf) {
271                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
272                 if (count < 0)
273                         count = 0;
274                 else if (count > 4095)
275                         count = 4095;
276                 buf[count] = '\0';
277                 fprintf(f, "%s", buf);
278
279                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
280                 if (count < 0)
281                         count = 0;
282                 else if (count > 4095)
283                         count = 4095;
284                 buf[count] = '\0';
285                 fprintf(f, "%s", buf);
286                 os_free(buf);
287         }
288         fclose(f);
289 }
290
291
292 int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
293 {
294         size_t i;
295
296         for (i = 0; i < iface->num_bss; i++)
297                 hostapd_dump_state(iface->bss[i]);
298
299         return 0;
300 }
301 #endif /* HOSTAPD_DUMP_STATE */
302
303
304 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
305                                               char *ifname)
306 {
307         int i;
308
309         for (i = 0; i < NUM_WEP_KEYS; i++) {
310                 if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
311                                     i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
312                         wpa_printf(MSG_DEBUG, "Failed to clear default "
313                                    "encryption keys (ifname=%s keyidx=%d)",
314                                    ifname, i);
315                 }
316         }
317 #ifdef CONFIG_IEEE80211W
318         if (hapd->conf->ieee80211w) {
319                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
320                         if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL,
321                                             i, i == 0 ? 1 : 0, NULL, 0,
322                                             NULL, 0)) {
323                                 wpa_printf(MSG_DEBUG, "Failed to clear "
324                                            "default mgmt encryption keys "
325                                            "(ifname=%s keyidx=%d)", ifname, i);
326                         }
327                 }
328         }
329 #endif /* CONFIG_IEEE80211W */
330 }
331
332
333 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
334 {
335         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
336         return 0;
337 }
338
339
340 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
341 {
342         int errors = 0, idx;
343         struct hostapd_ssid *ssid = &hapd->conf->ssid;
344
345         idx = ssid->wep.idx;
346         if (ssid->wep.default_len &&
347             hostapd_set_key(hapd->conf->iface,
348                             hapd, WPA_ALG_WEP, NULL, idx, idx == ssid->wep.idx,
349                             NULL, 0, ssid->wep.key[idx], ssid->wep.len[idx])) {
350                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
351                 errors++;
352         }
353
354         if (ssid->dyn_vlan_keys) {
355                 size_t i;
356                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
357                         const char *ifname;
358                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
359                         if (key == NULL)
360                                 continue;
361                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
362                                                             i);
363                         if (ifname == NULL)
364                                 continue;
365
366                         idx = key->idx;
367                         if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL,
368                                             idx, idx == key->idx, NULL, 0,
369                                             key->key[idx], key->len[idx])) {
370                                 wpa_printf(MSG_WARNING, "Could not set "
371                                            "dynamic VLAN WEP encryption.");
372                                 errors++;
373                         }
374                 }
375         }
376
377         return errors;
378 }
379
380 /**
381  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
382  * @hapd: Pointer to BSS data
383  *
384  * This function is used to free all per-BSS data structures and resources.
385  * This gets called in a loop for each BSS between calls to
386  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
387  * is deinitialized. Most of the modules that are initialized in
388  * hostapd_setup_bss() are deinitialized here.
389  */
390 static void hostapd_cleanup(struct hostapd_data *hapd)
391 {
392         hostapd_ctrl_iface_deinit(hapd);
393
394         iapp_deinit(hapd->iapp);
395         hapd->iapp = NULL;
396         accounting_deinit(hapd);
397         rsn_preauth_iface_deinit(hapd);
398         if (hapd->wpa_auth) {
399                 wpa_deinit(hapd->wpa_auth);
400                 hapd->wpa_auth = NULL;
401
402                 if (hostapd_set_privacy(hapd, 0)) {
403                         wpa_printf(MSG_DEBUG, "Could not disable "
404                                    "PrivacyInvoked for interface %s",
405                                    hapd->conf->iface);
406                 }
407
408                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
409                         wpa_printf(MSG_DEBUG, "Could not remove generic "
410                                    "information element from interface %s",
411                                    hapd->conf->iface);
412                 }
413         }
414         ieee802_1x_deinit(hapd);
415         vlan_deinit(hapd);
416         hostapd_acl_deinit(hapd);
417         radius_client_deinit(hapd->radius);
418         hapd->radius = NULL;
419         radius_server_deinit(hapd->radius_srv);
420         hapd->radius_srv = NULL;
421
422 #ifdef CONFIG_IEEE80211R
423         l2_packet_deinit(hapd->l2);
424 #endif /* CONFIG_IEEE80211R */
425
426         hostapd_deinit_wps(hapd);
427
428 #ifdef EAP_TLS_FUNCS
429         if (hapd->ssl_ctx) {
430                 tls_deinit(hapd->ssl_ctx);
431                 hapd->ssl_ctx = NULL;
432         }
433 #endif /* EAP_TLS_FUNCS */
434
435 #ifdef EAP_SERVER
436         if (hapd->eap_sim_db_priv) {
437                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
438                 hapd->eap_sim_db_priv = NULL;
439         }
440 #endif /* EAP_SERVER */
441
442         if (hapd->interface_added &&
443             hostapd_bss_remove(hapd, hapd->conf->iface)) {
444                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
445                            hapd->conf->iface);
446         }
447
448         os_free(hapd->probereq_cb);
449         hapd->probereq_cb = NULL;
450 }
451
452
453 /**
454  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
455  * @iface: Pointer to interface data
456  *
457  * This function is called before per-BSS data structures are deinitialized
458  * with hostapd_cleanup().
459  */
460 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
461 {
462 }
463
464
465 /**
466  * hostapd_cleanup_iface - Complete per-interface cleanup
467  * @iface: Pointer to interface data
468  *
469  * This function is called after per-BSS data structures are deinitialized
470  * with hostapd_cleanup().
471  */
472 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
473 {
474         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
475         iface->hw_features = NULL;
476         os_free(iface->current_rates);
477         iface->current_rates = NULL;
478         ap_list_deinit(iface);
479         hostapd_config_free(iface->conf);
480         iface->conf = NULL;
481
482         os_free(iface->config_fname);
483         os_free(iface->bss);
484         os_free(iface);
485 }
486
487
488 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
489 {
490         int i;
491
492         hostapd_broadcast_wep_set(hapd);
493
494         if (hapd->conf->ssid.wep.default_len)
495                 return 0;
496
497         for (i = 0; i < 4; i++) {
498                 if (hapd->conf->ssid.wep.key[i] &&
499                     hostapd_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
500                                     i == hapd->conf->ssid.wep.idx, NULL, 0,
501                                     hapd->conf->ssid.wep.key[i],
502                                     hapd->conf->ssid.wep.len[i])) {
503                         wpa_printf(MSG_WARNING, "Could not set WEP "
504                                    "encryption.");
505                         return -1;
506                 }
507                 if (hapd->conf->ssid.wep.key[i] &&
508                     i == hapd->conf->ssid.wep.idx)
509                         hostapd_set_privacy(hapd, 1);
510         }
511
512         return 0;
513 }
514
515
516 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
517 {
518         int ret = 0;
519
520         if (hostapd_drv_none(hapd))
521                 return 0;
522
523         wpa_printf(MSG_DEBUG, "Flushing old station entries");
524         if (hostapd_flush(hapd)) {
525                 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
526                 ret = -1;
527         }
528         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
529
530         /* New Prism2.5/3 STA firmware versions seem to have issues with this
531          * broadcast deauth frame. This gets the firmware in odd state where
532          * nothing works correctly, so let's skip sending this for the hostap
533          * driver. */
534         if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
535                 u8 addr[ETH_ALEN];
536                 os_memset(addr, 0xff, ETH_ALEN);
537                 hostapd_sta_deauth(hapd, addr,
538                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
539         }
540
541         return ret;
542 }
543
544
545 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
546                                     logger_level level, const char *txt)
547 {
548 #ifndef CONFIG_NO_HOSTAPD_LOGGER
549         struct hostapd_data *hapd = ctx;
550         int hlevel;
551
552         switch (level) {
553         case LOGGER_WARNING:
554                 hlevel = HOSTAPD_LEVEL_WARNING;
555                 break;
556         case LOGGER_INFO:
557                 hlevel = HOSTAPD_LEVEL_INFO;
558                 break;
559         case LOGGER_DEBUG:
560         default:
561                 hlevel = HOSTAPD_LEVEL_DEBUG;
562                 break;
563         }
564
565         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
566 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
567 }
568
569
570 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
571                                         u16 reason)
572 {
573         struct hostapd_data *hapd = ctx;
574         struct sta_info *sta;
575
576         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
577                    "STA " MACSTR " reason %d",
578                    __func__, MAC2STR(addr), reason);
579
580         sta = ap_get_sta(hapd, addr);
581         hostapd_sta_deauth(hapd, addr, reason);
582         if (sta == NULL)
583                 return;
584         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
585         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
586         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
587         sta->timeout_next = STA_REMOVE;
588 }
589
590
591 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
592 {
593         struct hostapd_data *hapd = ctx;
594         michael_mic_failure(hapd, addr, 0);
595 }
596
597
598 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
599                                        wpa_eapol_variable var, int value)
600 {
601         struct hostapd_data *hapd = ctx;
602         struct sta_info *sta = ap_get_sta(hapd, addr);
603         if (sta == NULL)
604                 return;
605         switch (var) {
606         case WPA_EAPOL_portEnabled:
607                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
608                 break;
609         case WPA_EAPOL_portValid:
610                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
611                 break;
612         case WPA_EAPOL_authorized:
613                 ieee802_1x_set_sta_authorized(hapd, sta, value);
614                 break;
615         case WPA_EAPOL_portControl_Auto:
616                 if (sta->eapol_sm)
617                         sta->eapol_sm->portControl = Auto;
618                 break;
619         case WPA_EAPOL_keyRun:
620                 if (sta->eapol_sm)
621                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
622                 break;
623         case WPA_EAPOL_keyAvailable:
624                 if (sta->eapol_sm)
625                         sta->eapol_sm->eap_if->eapKeyAvailable =
626                                 value ? TRUE : FALSE;
627                 break;
628         case WPA_EAPOL_keyDone:
629                 if (sta->eapol_sm)
630                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
631                 break;
632         case WPA_EAPOL_inc_EapolFramesTx:
633                 if (sta->eapol_sm)
634                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
635                 break;
636         }
637 }
638
639
640 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
641                                       wpa_eapol_variable var)
642 {
643         struct hostapd_data *hapd = ctx;
644         struct sta_info *sta = ap_get_sta(hapd, addr);
645         if (sta == NULL || sta->eapol_sm == NULL)
646                 return -1;
647         switch (var) {
648         case WPA_EAPOL_keyRun:
649                 return sta->eapol_sm->keyRun;
650         case WPA_EAPOL_keyAvailable:
651                 return sta->eapol_sm->eap_if->eapKeyAvailable;
652         default:
653                 return -1;
654         }
655 }
656
657
658 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
659                                            const u8 *prev_psk)
660 {
661         struct hostapd_data *hapd = ctx;
662         return hostapd_get_psk(hapd->conf, addr, prev_psk);
663 }
664
665
666 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
667                                     size_t *len)
668 {
669         struct hostapd_data *hapd = ctx;
670         const u8 *key;
671         size_t keylen;
672         struct sta_info *sta;
673
674         sta = ap_get_sta(hapd, addr);
675         if (sta == NULL)
676                 return -1;
677
678         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
679         if (key == NULL)
680                 return -1;
681
682         if (keylen > *len)
683                 keylen = *len;
684         os_memcpy(msk, key, keylen);
685         *len = keylen;
686
687         return 0;
688 }
689
690
691 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
692                                     const u8 *addr, int idx, u8 *key,
693                                     size_t key_len)
694 {
695         struct hostapd_data *hapd = ctx;
696         const char *ifname = hapd->conf->iface;
697
698         if (vlan_id > 0) {
699                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
700                 if (ifname == NULL)
701                         return -1;
702         }
703
704         return hostapd_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
705                                key, key_len);
706 }
707
708
709 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
710                                        u8 *seq)
711 {
712         struct hostapd_data *hapd = ctx;
713         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
714 }
715
716
717 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
718                                             u8 *seq)
719 {
720         struct hostapd_data *hapd = ctx;
721         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
722                                        seq);
723 }
724
725
726 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
727                                        const u8 *data, size_t data_len,
728                                        int encrypt)
729 {
730         struct hostapd_data *hapd = ctx;
731         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
732 }
733
734
735 static int hostapd_wpa_auth_for_each_sta(
736         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
737         void *cb_ctx)
738 {
739         struct hostapd_data *hapd = ctx;
740         struct sta_info *sta;
741
742         for (sta = hapd->sta_list; sta; sta = sta->next) {
743                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
744                         return 1;
745         }
746         return 0;
747 }
748
749
750 struct wpa_auth_iface_iter_data {
751         int (*cb)(struct wpa_authenticator *sm, void *ctx);
752         void *cb_ctx;
753 };
754
755 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
756 {
757         struct wpa_auth_iface_iter_data *data = ctx;
758         size_t i;
759         for (i = 0; i < iface->num_bss; i++) {
760                 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
761                         return 1;
762         }
763         return 0;
764 }
765
766
767 static int hostapd_wpa_auth_for_each_auth(
768         void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
769         void *cb_ctx)
770 {
771         struct wpa_auth_iface_iter_data data;
772         data.cb = cb;
773         data.cb_ctx = cb_ctx;
774         return hostapd_for_each_interface(wpa_auth_iface_iter, &data);
775 }
776
777
778 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
779                                        const u8 *data, size_t data_len)
780 {
781         struct hostapd_data *hapd = ctx;
782
783         if (hapd->driver && hapd->driver->send_ether)
784                 return hapd->driver->send_ether(hapd->drv_priv, dst,
785                                                 hapd->own_addr, proto,
786                                                 data, data_len);
787         if (hapd->l2 == NULL)
788                 return -1;
789         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
790 }
791
792
793 #ifdef CONFIG_IEEE80211R
794
795 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
796                                            const u8 *data, size_t data_len)
797 {
798         struct hostapd_data *hapd = ctx;
799         int res;
800         struct ieee80211_mgmt *m;
801         size_t mlen;
802         struct sta_info *sta;
803
804         sta = ap_get_sta(hapd, dst);
805         if (sta == NULL || sta->wpa_sm == NULL)
806                 return -1;
807
808         m = os_zalloc(sizeof(*m) + data_len);
809         if (m == NULL)
810                 return -1;
811         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
812         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
813                                         WLAN_FC_STYPE_ACTION);
814         os_memcpy(m->da, dst, ETH_ALEN);
815         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
816         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
817         os_memcpy(&m->u, data, data_len);
818
819         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen);
820         os_free(m);
821         return res;
822 }
823
824
825 static struct wpa_state_machine *
826 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
827 {
828         struct hostapd_data *hapd = ctx;
829         struct sta_info *sta;
830
831         sta = ap_sta_add(hapd, sta_addr);
832         if (sta == NULL)
833                 return NULL;
834         if (sta->wpa_sm)
835                 return sta->wpa_sm;
836
837         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
838         if (sta->wpa_sm == NULL) {
839                 ap_free_sta(hapd, sta);
840                 return NULL;
841         }
842         sta->auth_alg = WLAN_AUTH_FT;
843
844         return sta->wpa_sm;
845 }
846
847
848 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
849                                 size_t len)
850 {
851         struct hostapd_data *hapd = ctx;
852         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
853 }
854
855 #endif /* CONFIG_IEEE80211R */
856
857
858 /**
859  * hostapd_validate_bssid_configuration - Validate BSSID configuration
860  * @iface: Pointer to interface data
861  * Returns: 0 on success, -1 on failure
862  *
863  * This function is used to validate that the configured BSSIDs are valid.
864  */
865 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
866 {
867         u8 mask[ETH_ALEN] = { 0 };
868         struct hostapd_data *hapd = iface->bss[0];
869         unsigned int i = iface->conf->num_bss, bits = 0, j;
870         int res;
871         int auto_addr = 0;
872
873         if (hostapd_drv_none(hapd))
874                 return 0;
875
876         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
877
878         /* Determine the bits necessary to cover the number of BSSIDs. */
879         for (i--; i; i >>= 1)
880                 bits++;
881
882         /* Determine the bits necessary to any configured BSSIDs,
883            if they are higher than the number of BSSIDs. */
884         for (j = 0; j < iface->conf->num_bss; j++) {
885                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
886                         if (j)
887                                 auto_addr++;
888                         continue;
889                 }
890
891                 for (i = 0; i < ETH_ALEN; i++) {
892                         mask[i] |=
893                                 iface->conf->bss[j].bssid[i] ^
894                                 hapd->own_addr[i];
895                 }
896         }
897
898         if (!auto_addr)
899                 goto skip_mask_ext;
900
901         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
902                 ;
903         j = 0;
904         if (i < ETH_ALEN) {
905                 j = (5 - i) * 8;
906
907                 while (mask[i] != 0) {
908                         mask[i] >>= 1;
909                         j++;
910                 }
911         }
912
913         if (bits < j)
914                 bits = j;
915
916         if (bits > 40) {
917                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
918                            bits);
919                 return -1;
920         }
921
922         os_memset(mask, 0xff, ETH_ALEN);
923         j = bits / 8;
924         for (i = 5; i > 5 - j; i--)
925                 mask[i] = 0;
926         j = bits % 8;
927         while (j--)
928                 mask[i] <<= 1;
929
930 skip_mask_ext:
931         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
932                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
933
934         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
935         if (res == 0)
936                 return 0;
937
938         if (res < 0) {
939                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
940                            MACSTR " for start address " MACSTR ".",
941                            MAC2STR(mask), MAC2STR(hapd->own_addr));
942                 return -1;
943         }
944
945         if (!auto_addr)
946                 return 0;
947
948         for (i = 0; i < ETH_ALEN; i++) {
949                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
950                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
951                                    " for start address " MACSTR ".",
952                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
953                         wpa_printf(MSG_ERROR, "Start address must be the "
954                                    "first address in the block (i.e., addr "
955                                    "AND mask == addr).");
956                         return -1;
957                 }
958         }
959
960         return 0;
961 }
962
963
964 static int mac_in_conf(struct hostapd_config *conf, const void *a)
965 {
966         size_t i;
967
968         for (i = 0; i < conf->num_bss; i++) {
969                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
970                         return 1;
971                 }
972         }
973
974         return 0;
975 }
976
977
978 static int hostapd_setup_wpa(struct hostapd_data *hapd)
979 {
980         struct wpa_auth_config _conf;
981         struct wpa_auth_callbacks cb;
982         const u8 *wpa_ie;
983         size_t wpa_ie_len;
984
985         hostapd_wpa_auth_conf(hapd->conf, &_conf);
986         os_memset(&cb, 0, sizeof(cb));
987         cb.ctx = hapd;
988         cb.logger = hostapd_wpa_auth_logger;
989         cb.disconnect = hostapd_wpa_auth_disconnect;
990         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
991         cb.set_eapol = hostapd_wpa_auth_set_eapol;
992         cb.get_eapol = hostapd_wpa_auth_get_eapol;
993         cb.get_psk = hostapd_wpa_auth_get_psk;
994         cb.get_msk = hostapd_wpa_auth_get_msk;
995         cb.set_key = hostapd_wpa_auth_set_key;
996         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
997         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
998         cb.send_eapol = hostapd_wpa_auth_send_eapol;
999         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1000         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1001         cb.send_ether = hostapd_wpa_auth_send_ether;
1002 #ifdef CONFIG_IEEE80211R
1003         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1004         cb.add_sta = hostapd_wpa_auth_add_sta;
1005 #endif /* CONFIG_IEEE80211R */
1006         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1007         if (hapd->wpa_auth == NULL) {
1008                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1009                 return -1;
1010         }
1011
1012         if (hostapd_set_privacy(hapd, 1)) {
1013                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1014                            "for interface %s", hapd->conf->iface);
1015                 return -1;
1016         }
1017
1018         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1019         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1020                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1021                            "the kernel driver.");
1022                 return -1;
1023         }
1024
1025         if (rsn_preauth_iface_init(hapd)) {
1026                 wpa_printf(MSG_ERROR, "Initialization of RSN "
1027                            "pre-authentication failed.");
1028                 return -1;
1029         }
1030
1031         return 0;
1032
1033 }
1034
1035
1036 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1037                                     struct hostapd_bss_config *conf)
1038 {
1039         struct radius_server_conf srv;
1040         os_memset(&srv, 0, sizeof(srv));
1041         srv.client_file = conf->radius_server_clients;
1042         srv.auth_port = conf->radius_server_auth_port;
1043         srv.conf_ctx = conf;
1044         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1045         srv.ssl_ctx = hapd->ssl_ctx;
1046         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1047         srv.eap_fast_a_id = conf->eap_fast_a_id;
1048         srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1049         srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1050         srv.eap_fast_prov = conf->eap_fast_prov;
1051         srv.pac_key_lifetime = conf->pac_key_lifetime;
1052         srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1053         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1054         srv.tnc = conf->tnc;
1055         srv.wps = hapd->wps;
1056         srv.ipv6 = conf->radius_server_ipv6;
1057         srv.get_eap_user = hostapd_radius_get_eap_user;
1058         srv.eap_req_id_text = conf->eap_req_id_text;
1059         srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1060
1061         hapd->radius_srv = radius_server_init(&srv);
1062         if (hapd->radius_srv == NULL) {
1063                 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1064                 return -1;
1065         }
1066
1067         return 0;
1068 }
1069
1070
1071 /**
1072  * hostapd_setup_bss - Per-BSS setup (initialization)
1073  * @hapd: Pointer to BSS data
1074  * @first: Whether this BSS is the first BSS of an interface
1075  *
1076  * This function is used to initialize all per-BSS data structures and
1077  * resources. This gets called in a loop for each BSS when an interface is
1078  * initialized. Most of the modules that are initialized here will be
1079  * deinitialized in hostapd_cleanup().
1080  */
1081 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1082 {
1083         struct hostapd_bss_config *conf = hapd->conf;
1084         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1085         int ssid_len, set_ssid;
1086
1087         if (!first) {
1088                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1089                         /* Allocate the next available BSSID. */
1090                         do {
1091                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1092                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1093                 } else {
1094                         /* Allocate the configured BSSID. */
1095                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1096
1097                         if (hostapd_mac_comp(hapd->own_addr,
1098                                              hapd->iface->bss[0]->own_addr) ==
1099                             0) {
1100                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1101                                            "BSSID set to the MAC address of "
1102                                            "the radio", hapd->conf->iface);
1103                                 return -1;
1104                         }
1105                 }
1106
1107                 hapd->interface_added = 1;
1108                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1109                                     hapd->own_addr)) {
1110                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1111                                    MACSTR ")", MAC2STR(hapd->own_addr));
1112                         return -1;
1113                 }
1114         }
1115
1116         hostapd_flush_old_stations(hapd);
1117         hostapd_set_privacy(hapd, 0);
1118
1119         hostapd_broadcast_wep_clear(hapd);
1120         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1121                 return -1;
1122
1123         /*
1124          * Fetch the SSID from the system and use it or,
1125          * if one was specified in the config file, verify they
1126          * match.
1127          */
1128         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1129         if (ssid_len < 0) {
1130                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1131                 return -1;
1132         }
1133         if (conf->ssid.ssid_set) {
1134                 /*
1135                  * If SSID is specified in the config file and it differs
1136                  * from what is being used then force installation of the
1137                  * new SSID.
1138                  */
1139                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1140                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1141         } else {
1142                 /*
1143                  * No SSID in the config file; just use the one we got
1144                  * from the system.
1145                  */
1146                 set_ssid = 0;
1147                 conf->ssid.ssid_len = ssid_len;
1148                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1149                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1150         }
1151
1152         if (!hostapd_drv_none(hapd)) {
1153                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1154                            " and ssid '%s'",
1155                            hapd->conf->iface, MAC2STR(hapd->own_addr),
1156                            hapd->conf->ssid.ssid);
1157         }
1158
1159         if (hostapd_setup_wpa_psk(conf)) {
1160                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1161                 return -1;
1162         }
1163
1164         /* Set SSID for the kernel driver (to be used in beacon and probe
1165          * response frames) */
1166         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1167                                          conf->ssid.ssid_len)) {
1168                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1169                 return -1;
1170         }
1171
1172         if (wpa_debug_level == MSG_MSGDUMP)
1173                 conf->radius->msg_dumps = 1;
1174         hapd->radius = radius_client_init(hapd, conf->radius);
1175         if (hapd->radius == NULL) {
1176                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1177                 return -1;
1178         }
1179
1180         if (hostapd_acl_init(hapd)) {
1181                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1182                 return -1;
1183         }
1184         if (hostapd_init_wps(hapd, conf))
1185                 return -1;
1186
1187         if (ieee802_1x_init(hapd)) {
1188                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1189                 return -1;
1190         }
1191
1192         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1193                 return -1;
1194
1195         if (accounting_init(hapd)) {
1196                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1197                 return -1;
1198         }
1199
1200         if (hapd->conf->ieee802_11f &&
1201             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1202                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1203                            "failed.");
1204                 return -1;
1205         }
1206
1207         if (hostapd_ctrl_iface_init(hapd)) {
1208                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1209                 return -1;
1210         }
1211
1212         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1213                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1214                 return -1;
1215         }
1216
1217 #ifdef CONFIG_IEEE80211R
1218         if (!hostapd_drv_none(hapd)) {
1219                 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1220                                           hostapd_rrb_receive, hapd, 0);
1221                 if (hapd->l2 == NULL &&
1222                     (hapd->driver == NULL ||
1223                      hapd->driver->send_ether == NULL)) {
1224                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1225                                    "interface");
1226                         return -1;
1227                 }
1228         }
1229 #endif /* CONFIG_IEEE80211R */
1230
1231         ieee802_11_set_beacon(hapd);
1232
1233         if (conf->radius_server_clients &&
1234             hostapd_setup_radius_srv(hapd, conf))
1235                 return -1;
1236
1237         return 0;
1238 }
1239
1240
1241 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1242 {
1243         struct hostapd_data *hapd = iface->bss[0];
1244         int i;
1245         struct hostapd_tx_queue_params *p;
1246
1247         for (i = 0; i < NUM_TX_QUEUES; i++) {
1248                 p = &iface->conf->tx_queue[i];
1249
1250                 if (!p->configured)
1251                         continue;
1252
1253                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1254                                                 p->cwmax, p->burst)) {
1255                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1256                                    "parameters for queue %d.", i);
1257                         /* Continue anyway */
1258                 }
1259         }
1260 }
1261
1262
1263 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1264                                        size_t identity_len, int phase2,
1265                                        struct eap_user *user)
1266 {
1267         const struct hostapd_eap_user *eap_user;
1268         int i, count;
1269
1270         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1271         if (eap_user == NULL)
1272                 return -1;
1273
1274         if (user == NULL)
1275                 return 0;
1276
1277         os_memset(user, 0, sizeof(*user));
1278         count = EAP_USER_MAX_METHODS;
1279         if (count > EAP_MAX_METHODS)
1280                 count = EAP_MAX_METHODS;
1281         for (i = 0; i < count; i++) {
1282                 user->methods[i].vendor = eap_user->methods[i].vendor;
1283                 user->methods[i].method = eap_user->methods[i].method;
1284         }
1285
1286         if (eap_user->password) {
1287                 user->password = os_malloc(eap_user->password_len);
1288                 if (user->password == NULL)
1289                         return -1;
1290                 os_memcpy(user->password, eap_user->password,
1291                           eap_user->password_len);
1292                 user->password_len = eap_user->password_len;
1293                 user->password_hash = eap_user->password_hash;
1294         }
1295         user->force_version = eap_user->force_version;
1296         user->ttls_auth = eap_user->ttls_auth;
1297
1298         return 0;
1299 }
1300
1301
1302 static int setup_interface(struct hostapd_iface *iface)
1303 {
1304         struct hostapd_data *hapd = iface->bss[0];
1305         struct hostapd_bss_config *conf = hapd->conf;
1306         size_t i;
1307         char country[4];
1308         u8 *b = conf->bssid;
1309
1310         /*
1311          * Initialize the driver interface and make sure that all BSSes get
1312          * configured with a pointer to this driver interface.
1313          */
1314         if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
1315                 b = NULL;
1316         hapd->drv_priv = hostapd_driver_init(hapd, b);
1317
1318         if (hapd->drv_priv == NULL) {
1319                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1320                            hapd->driver ? hapd->driver->name : "Unknown");
1321                 hapd->driver = NULL;
1322                 return -1;
1323         }
1324         for (i = 0; i < iface->num_bss; i++) {
1325                 iface->bss[i]->driver = hapd->driver;
1326                 iface->bss[i]->drv_priv = hapd->drv_priv;
1327         }
1328
1329         if (hostapd_validate_bssid_configuration(iface))
1330                 return -1;
1331
1332         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1333                 os_memcpy(country, hapd->iconf->country, 3);
1334                 country[3] = '\0';
1335                 if (hostapd_set_country(hapd, country) < 0) {
1336                         wpa_printf(MSG_ERROR, "Failed to set country code");
1337                         return -1;
1338                 }
1339         }
1340
1341         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1342             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1343                 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1344                            "kernel driver");
1345                 return -1;
1346         }
1347
1348         if (hostapd_get_hw_features(iface)) {
1349                 /* Not all drivers support this yet, so continue without hw
1350                  * feature data. */
1351         } else {
1352                 int ret = hostapd_select_hw_mode(iface);
1353                 if (ret < 0) {
1354                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1355                                    "channel. (%d)", ret);
1356                         return -1;
1357                 }
1358                 ret = hostapd_check_ht_capab(iface);
1359                 if (ret < 0)
1360                         return -1;
1361                 if (ret == 1) {
1362                         wpa_printf(MSG_DEBUG, "Interface initialization will "
1363                                    "be completed in a callback");
1364                         return 0;
1365                 }
1366         }
1367         return hostapd_setup_interface_complete(iface, 0);
1368 }
1369
1370
1371 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1372 {
1373         struct hostapd_data *hapd = iface->bss[0];
1374         int freq;
1375         size_t j;
1376         u8 *prev_addr;
1377
1378         if (err) {
1379                 wpa_printf(MSG_ERROR, "Interface initialization failed");
1380                 eloop_terminate();
1381                 return -1;
1382         }
1383
1384         wpa_printf(MSG_DEBUG, "Completing interface initialization");
1385         if (hapd->iconf->channel) {
1386                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1387                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1388                            "Frequency: %d MHz",
1389                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1390                            hapd->iconf->channel, freq);
1391
1392                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1393                                      hapd->iconf->channel,
1394                                      hapd->iconf->ieee80211n,
1395                                      hapd->iconf->secondary_channel)) {
1396                         wpa_printf(MSG_ERROR, "Could not set channel for "
1397                                    "kernel driver");
1398                         return -1;
1399                 }
1400         }
1401
1402         if (hapd->iconf->rts_threshold > -1 &&
1403             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1404                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1405                            "kernel driver");
1406                 return -1;
1407         }
1408
1409         if (hapd->iconf->fragm_threshold > -1 &&
1410             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1411                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1412                            "for kernel driver");
1413                 return -1;
1414         }
1415
1416         prev_addr = hapd->own_addr;
1417
1418         for (j = 0; j < iface->num_bss; j++) {
1419                 hapd = iface->bss[j];
1420                 if (j)
1421                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1422                 if (hostapd_setup_bss(hapd, j == 0))
1423                         return -1;
1424                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1425                         prev_addr = hapd->own_addr;
1426         }
1427
1428         hostapd_tx_queue_params(iface);
1429
1430         ap_list_init(iface);
1431
1432         if (hostapd_driver_commit(hapd) < 0) {
1433                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1434                            "configuration", __func__);
1435                 return -1;
1436         }
1437
1438         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1439                    iface->bss[0]->conf->iface);
1440
1441         return 0;
1442 }
1443
1444
1445 /**
1446  * hostapd_setup_interface - Setup of an interface
1447  * @iface: Pointer to interface data.
1448  * Returns: 0 on success, -1 on failure
1449  *
1450  * Initializes the driver interface, validates the configuration,
1451  * and sets driver parameters based on the configuration.
1452  * Flushes old stations, sets the channel, encryption,
1453  * beacons, and WDS links based on the configuration.
1454  */
1455 int hostapd_setup_interface(struct hostapd_iface *iface)
1456 {
1457         int ret;
1458
1459         ret = setup_interface(iface);
1460         if (ret) {
1461                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1462                            iface->bss[0]->conf->iface);
1463                 eloop_terminate();
1464                 return -1;
1465         }
1466
1467         return 0;
1468 }
1469
1470
1471 /**
1472  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1473  * @hapd_iface: Pointer to interface data
1474  * @conf: Pointer to per-interface configuration
1475  * @bss: Pointer to per-BSS configuration for this BSS
1476  * Returns: Pointer to allocated BSS data
1477  *
1478  * This function is used to allocate per-BSS data structure. This data will be
1479  * freed after hostapd_cleanup() is called for it during interface
1480  * deinitialization.
1481  */
1482 struct hostapd_data *
1483 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1484                        struct hostapd_config *conf,
1485                        struct hostapd_bss_config *bss)
1486 {
1487         struct hostapd_data *hapd;
1488
1489         hapd = os_zalloc(sizeof(*hapd));
1490         if (hapd == NULL)
1491                 return NULL;
1492
1493         hapd->iconf = conf;
1494         hapd->conf = bss;
1495         hapd->iface = hapd_iface;
1496
1497 #ifdef EAP_TLS_FUNCS
1498         if (hapd->conf->eap_server &&
1499             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1500              hapd->conf->dh_file)) {
1501                 struct tls_connection_params params;
1502
1503                 hapd->ssl_ctx = tls_init(NULL);
1504                 if (hapd->ssl_ctx == NULL) {
1505                         wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1506                         goto fail;
1507                 }
1508
1509                 os_memset(&params, 0, sizeof(params));
1510                 params.ca_cert = hapd->conf->ca_cert;
1511                 params.client_cert = hapd->conf->server_cert;
1512                 params.private_key = hapd->conf->private_key;
1513                 params.private_key_passwd = hapd->conf->private_key_passwd;
1514                 params.dh_file = hapd->conf->dh_file;
1515
1516                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1517                         wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1518                         goto fail;
1519                 }
1520
1521                 if (tls_global_set_verify(hapd->ssl_ctx,
1522                                           hapd->conf->check_crl)) {
1523                         wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1524                         goto fail;
1525                 }
1526         }
1527 #endif /* EAP_TLS_FUNCS */
1528
1529 #ifdef EAP_SERVER
1530         if (hapd->conf->eap_sim_db) {
1531                 hapd->eap_sim_db_priv =
1532                         eap_sim_db_init(hapd->conf->eap_sim_db,
1533                                         hostapd_sim_db_cb, hapd);
1534                 if (hapd->eap_sim_db_priv == NULL) {
1535                         wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1536                                    "database interface");
1537                         goto fail;
1538                 }
1539         }
1540 #endif /* EAP_SERVER */
1541
1542         hapd->driver = hapd->iconf->driver;
1543
1544         return hapd;
1545
1546 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1547 fail:
1548 #endif
1549         /* TODO: cleanup allocated resources(?) */
1550         os_free(hapd);
1551         return NULL;
1552 }
1553
1554
1555 void hostapd_interface_deinit(struct hostapd_iface *iface)
1556 {
1557         size_t j;
1558
1559         if (iface == NULL)
1560                 return;
1561
1562         hostapd_cleanup_iface_pre(iface);
1563         for (j = 0; j < iface->num_bss; j++) {
1564                 struct hostapd_data *hapd = iface->bss[j];
1565                 hostapd_free_stas(hapd);
1566                 hostapd_flush_old_stations(hapd);
1567                 hostapd_cleanup(hapd);
1568                 if (j == iface->num_bss - 1 && hapd->driver)
1569                         hostapd_driver_deinit(hapd);
1570         }
1571         for (j = 0; j < iface->num_bss; j++)
1572                 os_free(iface->bss[j]);
1573         hostapd_cleanup_iface(iface);
1574 }
1575
1576
1577 int hostapd_register_probereq_cb(struct hostapd_data *hapd,
1578                                  void (*cb)(void *ctx, const u8 *sa,
1579                                             const u8 *ie, size_t ie_len),
1580                                  void *ctx)
1581 {
1582         struct hostapd_probereq_cb *n;
1583
1584         n = os_realloc(hapd->probereq_cb, (hapd->num_probereq_cb + 1) *
1585                        sizeof(struct hostapd_probereq_cb));
1586         if (n == NULL)
1587                 return -1;
1588
1589         hapd->probereq_cb = n;
1590         n = &hapd->probereq_cb[hapd->num_probereq_cb];
1591         hapd->num_probereq_cb++;
1592
1593         n->cb = cb;
1594         n->ctx = ctx;
1595
1596         return 0;
1597 }