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