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