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