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