303786b461bf17c4c701683b2994b1d3bb4a0068
[mech_eap.git] / src / ap / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/hw_features_common.h"
16 #include "radius/radius_client.h"
17 #include "radius/radius_das.h"
18 #include "eap_server/tncs.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
21 #include "fst/fst.h"
22 #include "hostapd.h"
23 #include "authsrv.h"
24 #include "sta_info.h"
25 #include "accounting.h"
26 #include "ap_list.h"
27 #include "beacon.h"
28 #include "iapp.h"
29 #include "ieee802_1x.h"
30 #include "ieee802_11_auth.h"
31 #include "vlan_init.h"
32 #include "wpa_auth.h"
33 #include "wps_hostapd.h"
34 #include "hw_features.h"
35 #include "wpa_auth_glue.h"
36 #include "ap_drv_ops.h"
37 #include "ap_config.h"
38 #include "p2p_hostapd.h"
39 #include "gas_serv.h"
40 #include "dfs.h"
41 #include "ieee802_11.h"
42 #include "bss_load.h"
43 #include "x_snoop.h"
44 #include "dhcp_snoop.h"
45 #include "ndisc_snoop.h"
46
47
48 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
49 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
50 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
51 static int setup_interface2(struct hostapd_iface *iface);
52 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
53
54
55 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
56                                int (*cb)(struct hostapd_iface *iface,
57                                          void *ctx), void *ctx)
58 {
59         size_t i;
60         int ret;
61
62         for (i = 0; i < interfaces->count; i++) {
63                 ret = cb(interfaces->iface[i], ctx);
64                 if (ret)
65                         return ret;
66         }
67
68         return 0;
69 }
70
71
72 static void hostapd_reload_bss(struct hostapd_data *hapd)
73 {
74         struct hostapd_ssid *ssid;
75
76 #ifndef CONFIG_NO_RADIUS
77         radius_client_reconfig(hapd->radius, hapd->conf->radius);
78 #endif /* CONFIG_NO_RADIUS */
79
80         ssid = &hapd->conf->ssid;
81         if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
82             ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
83                 /*
84                  * Force PSK to be derived again since SSID or passphrase may
85                  * have changed.
86                  */
87                 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
88         }
89         if (hostapd_setup_wpa_psk(hapd->conf)) {
90                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
91                            "after reloading configuration");
92         }
93
94         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
95                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
96         else
97                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
98
99         if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
100                 hostapd_setup_wpa(hapd);
101                 if (hapd->wpa_auth)
102                         wpa_init_keys(hapd->wpa_auth);
103         } else if (hapd->conf->wpa) {
104                 const u8 *wpa_ie;
105                 size_t wpa_ie_len;
106                 hostapd_reconfig_wpa(hapd);
107                 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
108                 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
109                         wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
110                                    "the kernel driver.");
111         } else if (hapd->wpa_auth) {
112                 wpa_deinit(hapd->wpa_auth);
113                 hapd->wpa_auth = NULL;
114                 hostapd_set_privacy(hapd, 0);
115                 hostapd_setup_encryption(hapd->conf->iface, hapd);
116                 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
117         }
118
119         ieee802_11_set_beacon(hapd);
120         hostapd_update_wps(hapd);
121
122         if (hapd->conf->ssid.ssid_set &&
123             hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
124                              hapd->conf->ssid.ssid_len)) {
125                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
126                 /* try to continue */
127         }
128         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
129 }
130
131
132 static void hostapd_clear_old(struct hostapd_iface *iface)
133 {
134         size_t j;
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                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
143                 hostapd_broadcast_wep_clear(iface->bss[j]);
144
145 #ifndef CONFIG_NO_RADIUS
146                 /* TODO: update dynamic data based on changed configuration
147                  * items (e.g., open/close sockets, etc.) */
148                 radius_client_flush(iface->bss[j]->radius, 0);
149 #endif /* CONFIG_NO_RADIUS */
150         }
151 }
152
153
154 int hostapd_reload_config(struct hostapd_iface *iface)
155 {
156         struct hostapd_data *hapd = iface->bss[0];
157         struct hostapd_config *newconf, *oldconf;
158         size_t j;
159
160         if (iface->config_fname == NULL) {
161                 /* Only in-memory config in use - assume it has been updated */
162                 hostapd_clear_old(iface);
163                 for (j = 0; j < iface->num_bss; j++)
164                         hostapd_reload_bss(iface->bss[j]);
165                 return 0;
166         }
167
168         if (iface->interfaces == NULL ||
169             iface->interfaces->config_read_cb == NULL)
170                 return -1;
171         newconf = iface->interfaces->config_read_cb(iface->config_fname);
172         if (newconf == NULL)
173                 return -1;
174
175         hostapd_clear_old(iface);
176
177         oldconf = hapd->iconf;
178         iface->conf = newconf;
179
180         for (j = 0; j < iface->num_bss; j++) {
181                 hapd = iface->bss[j];
182                 hapd->iconf = newconf;
183                 hapd->iconf->channel = oldconf->channel;
184                 hapd->iconf->acs = oldconf->acs;
185                 hapd->iconf->secondary_channel = oldconf->secondary_channel;
186                 hapd->iconf->ieee80211n = oldconf->ieee80211n;
187                 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
188                 hapd->iconf->ht_capab = oldconf->ht_capab;
189                 hapd->iconf->vht_capab = oldconf->vht_capab;
190                 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
191                 hapd->iconf->vht_oper_centr_freq_seg0_idx =
192                         oldconf->vht_oper_centr_freq_seg0_idx;
193                 hapd->iconf->vht_oper_centr_freq_seg1_idx =
194                         oldconf->vht_oper_centr_freq_seg1_idx;
195                 hapd->conf = newconf->bss[j];
196                 hostapd_reload_bss(hapd);
197         }
198
199         hostapd_config_free(oldconf);
200
201
202         return 0;
203 }
204
205
206 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
207                                               char *ifname)
208 {
209         int i;
210
211         for (i = 0; i < NUM_WEP_KEYS; i++) {
212                 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
213                                         0, NULL, 0, NULL, 0)) {
214                         wpa_printf(MSG_DEBUG, "Failed to clear default "
215                                    "encryption keys (ifname=%s keyidx=%d)",
216                                    ifname, i);
217                 }
218         }
219 #ifdef CONFIG_IEEE80211W
220         if (hapd->conf->ieee80211w) {
221                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
222                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
223                                                 NULL, i, 0, NULL,
224                                                 0, NULL, 0)) {
225                                 wpa_printf(MSG_DEBUG, "Failed to clear "
226                                            "default mgmt encryption keys "
227                                            "(ifname=%s keyidx=%d)", ifname, i);
228                         }
229                 }
230         }
231 #endif /* CONFIG_IEEE80211W */
232 }
233
234
235 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
236 {
237         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
238         return 0;
239 }
240
241
242 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
243 {
244         int errors = 0, idx;
245         struct hostapd_ssid *ssid = &hapd->conf->ssid;
246
247         idx = ssid->wep.idx;
248         if (ssid->wep.default_len &&
249             hostapd_drv_set_key(hapd->conf->iface,
250                                 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
251                                 1, NULL, 0, ssid->wep.key[idx],
252                                 ssid->wep.len[idx])) {
253                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
254                 errors++;
255         }
256
257         return errors;
258 }
259
260
261 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
262 {
263         os_free(hapd->probereq_cb);
264         hapd->probereq_cb = NULL;
265         hapd->num_probereq_cb = 0;
266
267 #ifdef CONFIG_P2P
268         wpabuf_free(hapd->p2p_beacon_ie);
269         hapd->p2p_beacon_ie = NULL;
270         wpabuf_free(hapd->p2p_probe_resp_ie);
271         hapd->p2p_probe_resp_ie = NULL;
272 #endif /* CONFIG_P2P */
273
274         if (!hapd->started) {
275                 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
276                            __func__, hapd->conf->iface);
277                 return;
278         }
279         hapd->started = 0;
280
281         wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
282         iapp_deinit(hapd->iapp);
283         hapd->iapp = NULL;
284         accounting_deinit(hapd);
285         hostapd_deinit_wpa(hapd);
286         vlan_deinit(hapd);
287         hostapd_acl_deinit(hapd);
288 #ifndef CONFIG_NO_RADIUS
289         radius_client_deinit(hapd->radius);
290         hapd->radius = NULL;
291         radius_das_deinit(hapd->radius_das);
292         hapd->radius_das = NULL;
293 #endif /* CONFIG_NO_RADIUS */
294
295         hostapd_deinit_wps(hapd);
296
297         authsrv_deinit(hapd);
298
299         if (hapd->interface_added) {
300                 hapd->interface_added = 0;
301                 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
302                         wpa_printf(MSG_WARNING,
303                                    "Failed to remove BSS interface %s",
304                                    hapd->conf->iface);
305                         hapd->interface_added = 1;
306                 } else {
307                         /*
308                          * Since this was a dynamically added interface, the
309                          * driver wrapper may have removed its internal instance
310                          * and hapd->drv_priv is not valid anymore.
311                          */
312                         hapd->drv_priv = NULL;
313                 }
314         }
315
316         wpabuf_free(hapd->time_adv);
317
318 #ifdef CONFIG_INTERWORKING
319         gas_serv_deinit(hapd);
320 #endif /* CONFIG_INTERWORKING */
321
322         bss_load_update_deinit(hapd);
323         ndisc_snoop_deinit(hapd);
324         dhcp_snoop_deinit(hapd);
325         x_snoop_deinit(hapd);
326
327 #ifdef CONFIG_SQLITE
328         bin_clear_free(hapd->tmp_eap_user.identity,
329                        hapd->tmp_eap_user.identity_len);
330         bin_clear_free(hapd->tmp_eap_user.password,
331                        hapd->tmp_eap_user.password_len);
332 #endif /* CONFIG_SQLITE */
333
334 #ifdef CONFIG_MESH
335         wpabuf_free(hapd->mesh_pending_auth);
336         hapd->mesh_pending_auth = NULL;
337 #endif /* CONFIG_MESH */
338 }
339
340
341 /**
342  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
343  * @hapd: Pointer to BSS data
344  *
345  * This function is used to free all per-BSS data structures and resources.
346  * Most of the modules that are initialized in hostapd_setup_bss() are
347  * deinitialized here.
348  */
349 static void hostapd_cleanup(struct hostapd_data *hapd)
350 {
351         wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
352                    hapd->conf->iface);
353         if (hapd->iface->interfaces &&
354             hapd->iface->interfaces->ctrl_iface_deinit)
355                 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
356         hostapd_free_hapd_data(hapd);
357 }
358
359
360 static void sta_track_deinit(struct hostapd_iface *iface)
361 {
362         struct hostapd_sta_info *info;
363
364         if (!iface->num_sta_seen)
365                 return;
366
367         while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
368                                      list))) {
369                 dl_list_del(&info->list);
370                 iface->num_sta_seen--;
371                 os_free(info);
372         }
373 }
374
375
376 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
377 {
378         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
379 #ifdef CONFIG_IEEE80211N
380 #ifdef NEED_AP_MLME
381         hostapd_stop_setup_timers(iface);
382 #endif /* NEED_AP_MLME */
383 #endif /* CONFIG_IEEE80211N */
384         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
385         iface->hw_features = NULL;
386         os_free(iface->current_rates);
387         iface->current_rates = NULL;
388         os_free(iface->basic_rates);
389         iface->basic_rates = NULL;
390         ap_list_deinit(iface);
391         sta_track_deinit(iface);
392 }
393
394
395 /**
396  * hostapd_cleanup_iface - Complete per-interface cleanup
397  * @iface: Pointer to interface data
398  *
399  * This function is called after per-BSS data structures are deinitialized
400  * with hostapd_cleanup().
401  */
402 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
403 {
404         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
405         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
406
407         hostapd_cleanup_iface_partial(iface);
408         hostapd_config_free(iface->conf);
409         iface->conf = NULL;
410
411         os_free(iface->config_fname);
412         os_free(iface->bss);
413         wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
414         os_free(iface);
415 }
416
417
418 static void hostapd_clear_wep(struct hostapd_data *hapd)
419 {
420         if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
421                 hostapd_set_privacy(hapd, 0);
422                 hostapd_broadcast_wep_clear(hapd);
423         }
424 }
425
426
427 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
428 {
429         int i;
430
431         hostapd_broadcast_wep_set(hapd);
432
433         if (hapd->conf->ssid.wep.default_len) {
434                 hostapd_set_privacy(hapd, 1);
435                 return 0;
436         }
437
438         /*
439          * When IEEE 802.1X is not enabled, the driver may need to know how to
440          * set authentication algorithms for static WEP.
441          */
442         hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
443
444         for (i = 0; i < 4; i++) {
445                 if (hapd->conf->ssid.wep.key[i] &&
446                     hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
447                                         i == hapd->conf->ssid.wep.idx, NULL, 0,
448                                         hapd->conf->ssid.wep.key[i],
449                                         hapd->conf->ssid.wep.len[i])) {
450                         wpa_printf(MSG_WARNING, "Could not set WEP "
451                                    "encryption.");
452                         return -1;
453                 }
454                 if (hapd->conf->ssid.wep.key[i] &&
455                     i == hapd->conf->ssid.wep.idx)
456                         hostapd_set_privacy(hapd, 1);
457         }
458
459         return 0;
460 }
461
462
463 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
464 {
465         int ret = 0;
466         u8 addr[ETH_ALEN];
467
468         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
469                 return 0;
470
471         if (!hapd->iface->driver_ap_teardown) {
472                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
473                         "Flushing old station entries");
474
475                 if (hostapd_flush(hapd)) {
476                         wpa_msg(hapd->msg_ctx, MSG_WARNING,
477                                 "Could not connect to kernel driver");
478                         ret = -1;
479                 }
480         }
481         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
482         os_memset(addr, 0xff, ETH_ALEN);
483         hostapd_drv_sta_deauth(hapd, addr, reason);
484         hostapd_free_stas(hapd);
485
486         return ret;
487 }
488
489
490 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
491 {
492         hostapd_free_stas(hapd);
493         hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
494         hostapd_clear_wep(hapd);
495 }
496
497
498 /**
499  * hostapd_validate_bssid_configuration - Validate BSSID configuration
500  * @iface: Pointer to interface data
501  * Returns: 0 on success, -1 on failure
502  *
503  * This function is used to validate that the configured BSSIDs are valid.
504  */
505 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
506 {
507         u8 mask[ETH_ALEN] = { 0 };
508         struct hostapd_data *hapd = iface->bss[0];
509         unsigned int i = iface->conf->num_bss, bits = 0, j;
510         int auto_addr = 0;
511
512         if (hostapd_drv_none(hapd))
513                 return 0;
514
515         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
516
517         /* Determine the bits necessary to cover the number of BSSIDs. */
518         for (i--; i; i >>= 1)
519                 bits++;
520
521         /* Determine the bits necessary to any configured BSSIDs,
522            if they are higher than the number of BSSIDs. */
523         for (j = 0; j < iface->conf->num_bss; j++) {
524                 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
525                         if (j)
526                                 auto_addr++;
527                         continue;
528                 }
529
530                 for (i = 0; i < ETH_ALEN; i++) {
531                         mask[i] |=
532                                 iface->conf->bss[j]->bssid[i] ^
533                                 hapd->own_addr[i];
534                 }
535         }
536
537         if (!auto_addr)
538                 goto skip_mask_ext;
539
540         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
541                 ;
542         j = 0;
543         if (i < ETH_ALEN) {
544                 j = (5 - i) * 8;
545
546                 while (mask[i] != 0) {
547                         mask[i] >>= 1;
548                         j++;
549                 }
550         }
551
552         if (bits < j)
553                 bits = j;
554
555         if (bits > 40) {
556                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
557                            bits);
558                 return -1;
559         }
560
561         os_memset(mask, 0xff, ETH_ALEN);
562         j = bits / 8;
563         for (i = 5; i > 5 - j; i--)
564                 mask[i] = 0;
565         j = bits % 8;
566         while (j--)
567                 mask[i] <<= 1;
568
569 skip_mask_ext:
570         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
571                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
572
573         if (!auto_addr)
574                 return 0;
575
576         for (i = 0; i < ETH_ALEN; i++) {
577                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
578                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
579                                    " for start address " MACSTR ".",
580                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
581                         wpa_printf(MSG_ERROR, "Start address must be the "
582                                    "first address in the block (i.e., addr "
583                                    "AND mask == addr).");
584                         return -1;
585                 }
586         }
587
588         return 0;
589 }
590
591
592 static int mac_in_conf(struct hostapd_config *conf, const void *a)
593 {
594         size_t i;
595
596         for (i = 0; i < conf->num_bss; i++) {
597                 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
598                         return 1;
599                 }
600         }
601
602         return 0;
603 }
604
605
606 #ifndef CONFIG_NO_RADIUS
607
608 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
609                                     struct radius_das_attrs *attr)
610 {
611         if (attr->nas_identifier &&
612             (!hapd->conf->nas_identifier ||
613              os_strlen(hapd->conf->nas_identifier) !=
614              attr->nas_identifier_len ||
615              os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
616                        attr->nas_identifier_len) != 0)) {
617                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
618                 return 1;
619         }
620
621         if (attr->nas_ip_addr &&
622             (hapd->conf->own_ip_addr.af != AF_INET ||
623              os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
624              0)) {
625                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
626                 return 1;
627         }
628
629 #ifdef CONFIG_IPV6
630         if (attr->nas_ipv6_addr &&
631             (hapd->conf->own_ip_addr.af != AF_INET6 ||
632              os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
633              != 0)) {
634                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
635                 return 1;
636         }
637 #endif /* CONFIG_IPV6 */
638
639         return 0;
640 }
641
642
643 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
644                                               struct radius_das_attrs *attr,
645                                               int *multi)
646 {
647         struct sta_info *selected, *sta;
648         char buf[128];
649         int num_attr = 0;
650         int count;
651
652         *multi = 0;
653
654         for (sta = hapd->sta_list; sta; sta = sta->next)
655                 sta->radius_das_match = 1;
656
657         if (attr->sta_addr) {
658                 num_attr++;
659                 sta = ap_get_sta(hapd, attr->sta_addr);
660                 if (!sta) {
661                         wpa_printf(MSG_DEBUG,
662                                    "RADIUS DAS: No Calling-Station-Id match");
663                         return NULL;
664                 }
665
666                 selected = sta;
667                 for (sta = hapd->sta_list; sta; sta = sta->next) {
668                         if (sta != selected)
669                                 sta->radius_das_match = 0;
670                 }
671                 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
672         }
673
674         if (attr->acct_session_id) {
675                 num_attr++;
676                 if (attr->acct_session_id_len != 16) {
677                         wpa_printf(MSG_DEBUG,
678                                    "RADIUS DAS: Acct-Session-Id cannot match");
679                         return NULL;
680                 }
681                 count = 0;
682
683                 for (sta = hapd->sta_list; sta; sta = sta->next) {
684                         if (!sta->radius_das_match)
685                                 continue;
686                         os_snprintf(buf, sizeof(buf), "%016llX",
687                                     (unsigned long long) sta->acct_session_id);
688                         if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
689                                 sta->radius_das_match = 0;
690                         else
691                                 count++;
692                 }
693
694                 if (count == 0) {
695                         wpa_printf(MSG_DEBUG,
696                                    "RADIUS DAS: No matches remaining after Acct-Session-Id check");
697                         return NULL;
698                 }
699                 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
700         }
701
702         if (attr->acct_multi_session_id) {
703                 num_attr++;
704                 if (attr->acct_multi_session_id_len != 16) {
705                         wpa_printf(MSG_DEBUG,
706                                    "RADIUS DAS: Acct-Multi-Session-Id cannot match");
707                         return NULL;
708                 }
709                 count = 0;
710
711                 for (sta = hapd->sta_list; sta; sta = sta->next) {
712                         if (!sta->radius_das_match)
713                                 continue;
714                         if (!sta->eapol_sm ||
715                             !sta->eapol_sm->acct_multi_session_id) {
716                                 sta->radius_das_match = 0;
717                                 continue;
718                         }
719                         os_snprintf(buf, sizeof(buf), "%016llX",
720                                     (unsigned long long)
721                                     sta->eapol_sm->acct_multi_session_id);
722                         if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
723                             0)
724                                 sta->radius_das_match = 0;
725                         else
726                                 count++;
727                 }
728
729                 if (count == 0) {
730                         wpa_printf(MSG_DEBUG,
731                                    "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
732                         return NULL;
733                 }
734                 wpa_printf(MSG_DEBUG,
735                            "RADIUS DAS: Acct-Multi-Session-Id match");
736         }
737
738         if (attr->cui) {
739                 num_attr++;
740                 count = 0;
741
742                 for (sta = hapd->sta_list; sta; sta = sta->next) {
743                         struct wpabuf *cui;
744
745                         if (!sta->radius_das_match)
746                                 continue;
747                         cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
748                         if (!cui || wpabuf_len(cui) != attr->cui_len ||
749                             os_memcmp(wpabuf_head(cui), attr->cui,
750                                       attr->cui_len) != 0)
751                                 sta->radius_das_match = 0;
752                         else
753                                 count++;
754                 }
755
756                 if (count == 0) {
757                         wpa_printf(MSG_DEBUG,
758                                    "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
759                         return NULL;
760                 }
761                 wpa_printf(MSG_DEBUG,
762                            "RADIUS DAS: Chargeable-User-Identity match");
763         }
764
765         if (attr->user_name) {
766                 num_attr++;
767                 count = 0;
768
769                 for (sta = hapd->sta_list; sta; sta = sta->next) {
770                         u8 *identity;
771                         size_t identity_len;
772
773                         if (!sta->radius_das_match)
774                                 continue;
775                         identity = ieee802_1x_get_identity(sta->eapol_sm,
776                                                            &identity_len);
777                         if (!identity ||
778                             identity_len != attr->user_name_len ||
779                             os_memcmp(identity, attr->user_name, identity_len)
780                             != 0)
781                                 sta->radius_das_match = 0;
782                         else
783                                 count++;
784                 }
785
786                 if (count == 0) {
787                         wpa_printf(MSG_DEBUG,
788                                    "RADIUS DAS: No matches remaining after User-Name check");
789                         return NULL;
790                 }
791                 wpa_printf(MSG_DEBUG,
792                            "RADIUS DAS: User-Name match");
793         }
794
795         if (num_attr == 0) {
796                 /*
797                  * In theory, we could match all current associations, but it
798                  * seems safer to just reject requests that do not include any
799                  * session identification attributes.
800                  */
801                 wpa_printf(MSG_DEBUG,
802                            "RADIUS DAS: No session identification attributes included");
803                 return NULL;
804         }
805
806         selected = NULL;
807         for (sta = hapd->sta_list; sta; sta = sta->next) {
808                 if (sta->radius_das_match) {
809                         if (selected) {
810                                 *multi = 1;
811                                 return NULL;
812                         }
813                         selected = sta;
814                 }
815         }
816
817         return selected;
818 }
819
820
821 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
822                                         struct radius_das_attrs *attr)
823 {
824         if (!hapd->wpa_auth)
825                 return -1;
826         return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
827 }
828
829
830 static enum radius_das_res
831 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
832 {
833         struct hostapd_data *hapd = ctx;
834         struct sta_info *sta;
835         int multi;
836
837         if (hostapd_das_nas_mismatch(hapd, attr))
838                 return RADIUS_DAS_NAS_MISMATCH;
839
840         sta = hostapd_das_find_sta(hapd, attr, &multi);
841         if (sta == NULL) {
842                 if (multi) {
843                         wpa_printf(MSG_DEBUG,
844                                    "RADIUS DAS: Multiple sessions match - not supported");
845                         return RADIUS_DAS_MULTI_SESSION_MATCH;
846                 }
847                 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
848                         wpa_printf(MSG_DEBUG,
849                                    "RADIUS DAS: PMKSA cache entry matched");
850                         return RADIUS_DAS_SUCCESS;
851                 }
852                 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
853                 return RADIUS_DAS_SESSION_NOT_FOUND;
854         }
855
856         wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
857                    " - disconnecting", MAC2STR(sta->addr));
858         wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
859
860         hostapd_drv_sta_deauth(hapd, sta->addr,
861                                WLAN_REASON_PREV_AUTH_NOT_VALID);
862         ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
863
864         return RADIUS_DAS_SUCCESS;
865 }
866
867 #endif /* CONFIG_NO_RADIUS */
868
869
870 /**
871  * hostapd_setup_bss - Per-BSS setup (initialization)
872  * @hapd: Pointer to BSS data
873  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
874  *      but interface may exist
875  *
876  * This function is used to initialize all per-BSS data structures and
877  * resources. This gets called in a loop for each BSS when an interface is
878  * initialized. Most of the modules that are initialized here will be
879  * deinitialized in hostapd_cleanup().
880  */
881 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
882 {
883         struct hostapd_bss_config *conf = hapd->conf;
884         u8 ssid[SSID_MAX_LEN + 1];
885         int ssid_len, set_ssid;
886         char force_ifname[IFNAMSIZ];
887         u8 if_addr[ETH_ALEN];
888         int flush_old_stations = 1;
889
890         wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
891                    __func__, hapd, conf->iface, first);
892
893 #ifdef EAP_SERVER_TNC
894         if (conf->tnc && tncs_global_init() < 0) {
895                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
896                 return -1;
897         }
898 #endif /* EAP_SERVER_TNC */
899
900         if (hapd->started) {
901                 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
902                            __func__, conf->iface);
903                 return -1;
904         }
905         hapd->started = 1;
906
907         if (!first || first == -1) {
908                 if (is_zero_ether_addr(conf->bssid)) {
909                         /* Allocate the next available BSSID. */
910                         do {
911                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
912                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
913                 } else {
914                         /* Allocate the configured BSSID. */
915                         os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
916
917                         if (hostapd_mac_comp(hapd->own_addr,
918                                              hapd->iface->bss[0]->own_addr) ==
919                             0) {
920                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
921                                            "BSSID set to the MAC address of "
922                                            "the radio", conf->iface);
923                                 return -1;
924                         }
925                 }
926
927                 hapd->interface_added = 1;
928                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
929                                    conf->iface, hapd->own_addr, hapd,
930                                    &hapd->drv_priv, force_ifname, if_addr,
931                                    conf->bridge[0] ? conf->bridge : NULL,
932                                    first == -1)) {
933                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
934                                    MACSTR ")", MAC2STR(hapd->own_addr));
935                         hapd->interface_added = 0;
936                         return -1;
937                 }
938         }
939
940         if (conf->wmm_enabled < 0)
941                 conf->wmm_enabled = hapd->iconf->ieee80211n;
942
943 #ifdef CONFIG_IEEE80211R
944         if (is_zero_ether_addr(conf->r1_key_holder))
945                 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
946 #endif /* CONFIG_IEEE80211R */
947
948 #ifdef CONFIG_MESH
949         if (hapd->iface->mconf == NULL)
950                 flush_old_stations = 0;
951 #endif /* CONFIG_MESH */
952
953         if (flush_old_stations)
954                 hostapd_flush_old_stations(hapd,
955                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
956         hostapd_set_privacy(hapd, 0);
957
958         hostapd_broadcast_wep_clear(hapd);
959         if (hostapd_setup_encryption(conf->iface, hapd))
960                 return -1;
961
962         /*
963          * Fetch the SSID from the system and use it or,
964          * if one was specified in the config file, verify they
965          * match.
966          */
967         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
968         if (ssid_len < 0) {
969                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
970                 return -1;
971         }
972         if (conf->ssid.ssid_set) {
973                 /*
974                  * If SSID is specified in the config file and it differs
975                  * from what is being used then force installation of the
976                  * new SSID.
977                  */
978                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
979                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
980         } else {
981                 /*
982                  * No SSID in the config file; just use the one we got
983                  * from the system.
984                  */
985                 set_ssid = 0;
986                 conf->ssid.ssid_len = ssid_len;
987                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
988         }
989
990         if (!hostapd_drv_none(hapd)) {
991                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
992                            " and ssid \"%s\"",
993                            conf->iface, MAC2STR(hapd->own_addr),
994                            wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
995         }
996
997         if (hostapd_setup_wpa_psk(conf)) {
998                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
999                 return -1;
1000         }
1001
1002         /* Set SSID for the kernel driver (to be used in beacon and probe
1003          * response frames) */
1004         if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1005                                          conf->ssid.ssid_len)) {
1006                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1007                 return -1;
1008         }
1009
1010         if (wpa_debug_level <= MSG_MSGDUMP)
1011                 conf->radius->msg_dumps = 1;
1012 #ifndef CONFIG_NO_RADIUS
1013         hapd->radius = radius_client_init(hapd, conf->radius);
1014         if (hapd->radius == NULL) {
1015                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1016                 return -1;
1017         }
1018
1019         if (conf->radius_das_port) {
1020                 struct radius_das_conf das_conf;
1021                 os_memset(&das_conf, 0, sizeof(das_conf));
1022                 das_conf.port = conf->radius_das_port;
1023                 das_conf.shared_secret = conf->radius_das_shared_secret;
1024                 das_conf.shared_secret_len =
1025                         conf->radius_das_shared_secret_len;
1026                 das_conf.client_addr = &conf->radius_das_client_addr;
1027                 das_conf.time_window = conf->radius_das_time_window;
1028                 das_conf.require_event_timestamp =
1029                         conf->radius_das_require_event_timestamp;
1030                 das_conf.ctx = hapd;
1031                 das_conf.disconnect = hostapd_das_disconnect;
1032                 hapd->radius_das = radius_das_init(&das_conf);
1033                 if (hapd->radius_das == NULL) {
1034                         wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1035                                    "failed.");
1036                         return -1;
1037                 }
1038         }
1039 #endif /* CONFIG_NO_RADIUS */
1040
1041         if (hostapd_acl_init(hapd)) {
1042                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1043                 return -1;
1044         }
1045         if (hostapd_init_wps(hapd, conf))
1046                 return -1;
1047
1048         if (authsrv_init(hapd) < 0)
1049                 return -1;
1050
1051         if (ieee802_1x_init(hapd)) {
1052                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1053                 return -1;
1054         }
1055
1056         if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1057                 return -1;
1058
1059         if (accounting_init(hapd)) {
1060                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1061                 return -1;
1062         }
1063
1064         if (conf->ieee802_11f &&
1065             (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1066                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1067                            "failed.");
1068                 return -1;
1069         }
1070
1071 #ifdef CONFIG_INTERWORKING
1072         if (gas_serv_init(hapd)) {
1073                 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1074                 return -1;
1075         }
1076
1077         if (conf->qos_map_set_len &&
1078             hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1079                                     conf->qos_map_set_len)) {
1080                 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1081                 return -1;
1082         }
1083 #endif /* CONFIG_INTERWORKING */
1084
1085         if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1086                 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1087                 return -1;
1088         }
1089
1090         if (conf->proxy_arp) {
1091                 if (x_snoop_init(hapd)) {
1092                         wpa_printf(MSG_ERROR,
1093                                    "Generic snooping infrastructure initialization failed");
1094                         return -1;
1095                 }
1096
1097                 if (dhcp_snoop_init(hapd)) {
1098                         wpa_printf(MSG_ERROR,
1099                                    "DHCP snooping initialization failed");
1100                         return -1;
1101                 }
1102
1103                 if (ndisc_snoop_init(hapd)) {
1104                         wpa_printf(MSG_ERROR,
1105                                    "Neighbor Discovery snooping initialization failed");
1106                         return -1;
1107                 }
1108         }
1109
1110         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1111                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1112                 return -1;
1113         }
1114
1115         if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1116                 return -1;
1117
1118         if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1119                 return -1;
1120
1121         if (hapd->driver && hapd->driver->set_operstate)
1122                 hapd->driver->set_operstate(hapd->drv_priv, 1);
1123
1124         return 0;
1125 }
1126
1127
1128 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1129 {
1130         struct hostapd_data *hapd = iface->bss[0];
1131         int i;
1132         struct hostapd_tx_queue_params *p;
1133
1134 #ifdef CONFIG_MESH
1135         if (iface->mconf == NULL)
1136                 return;
1137 #endif /* CONFIG_MESH */
1138
1139         for (i = 0; i < NUM_TX_QUEUES; i++) {
1140                 p = &iface->conf->tx_queue[i];
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_set_acl_list(struct hostapd_data *hapd,
1153                                 struct mac_acl_entry *mac_acl,
1154                                 int n_entries, u8 accept_acl)
1155 {
1156         struct hostapd_acl_params *acl_params;
1157         int i, err;
1158
1159         acl_params = os_zalloc(sizeof(*acl_params) +
1160                                (n_entries * sizeof(acl_params->mac_acl[0])));
1161         if (!acl_params)
1162                 return -ENOMEM;
1163
1164         for (i = 0; i < n_entries; i++)
1165                 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1166                           ETH_ALEN);
1167
1168         acl_params->acl_policy = accept_acl;
1169         acl_params->num_mac_acl = n_entries;
1170
1171         err = hostapd_drv_set_acl(hapd, acl_params);
1172
1173         os_free(acl_params);
1174
1175         return err;
1176 }
1177
1178
1179 static void hostapd_set_acl(struct hostapd_data *hapd)
1180 {
1181         struct hostapd_config *conf = hapd->iconf;
1182         int err;
1183         u8 accept_acl;
1184
1185         if (hapd->iface->drv_max_acl_mac_addrs == 0)
1186                 return;
1187
1188         if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1189                 accept_acl = 1;
1190                 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1191                                            conf->bss[0]->num_accept_mac,
1192                                            accept_acl);
1193                 if (err) {
1194                         wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1195                         return;
1196                 }
1197         } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1198                 accept_acl = 0;
1199                 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1200                                            conf->bss[0]->num_deny_mac,
1201                                            accept_acl);
1202                 if (err) {
1203                         wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1204                         return;
1205                 }
1206         }
1207 }
1208
1209
1210 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1211 {
1212         if (!hapd->iface->interfaces ||
1213             !hapd->iface->interfaces->ctrl_iface_init)
1214                 return 0;
1215
1216         if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1217                 wpa_printf(MSG_ERROR,
1218                            "Failed to setup control interface for %s",
1219                            hapd->conf->iface);
1220                 return -1;
1221         }
1222
1223         return 0;
1224 }
1225
1226
1227 static int start_ctrl_iface(struct hostapd_iface *iface)
1228 {
1229         size_t i;
1230
1231         if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1232                 return 0;
1233
1234         for (i = 0; i < iface->num_bss; i++) {
1235                 struct hostapd_data *hapd = iface->bss[i];
1236                 if (iface->interfaces->ctrl_iface_init(hapd)) {
1237                         wpa_printf(MSG_ERROR,
1238                                    "Failed to setup control interface for %s",
1239                                    hapd->conf->iface);
1240                         return -1;
1241                 }
1242         }
1243
1244         return 0;
1245 }
1246
1247
1248 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1249 {
1250         struct hostapd_iface *iface = eloop_ctx;
1251
1252         if (!iface->wait_channel_update) {
1253                 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1254                 return;
1255         }
1256
1257         /*
1258          * It is possible that the existing channel list is acceptable, so try
1259          * to proceed.
1260          */
1261         wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1262         setup_interface2(iface);
1263 }
1264
1265
1266 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1267 {
1268         if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1269                 return;
1270
1271         wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1272         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1273         setup_interface2(iface);
1274 }
1275
1276
1277 static int setup_interface(struct hostapd_iface *iface)
1278 {
1279         struct hostapd_data *hapd = iface->bss[0];
1280         size_t i;
1281
1282         /*
1283          * It is possible that setup_interface() is called after the interface
1284          * was disabled etc., in which case driver_ap_teardown is possibly set
1285          * to 1. Clear it here so any other key/station deletion, which is not
1286          * part of a teardown flow, would also call the relevant driver
1287          * callbacks.
1288          */
1289         iface->driver_ap_teardown = 0;
1290
1291         if (!iface->phy[0]) {
1292                 const char *phy = hostapd_drv_get_radio_name(hapd);
1293                 if (phy) {
1294                         wpa_printf(MSG_DEBUG, "phy: %s", phy);
1295                         os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1296                 }
1297         }
1298
1299         /*
1300          * Make sure that all BSSes get configured with a pointer to the same
1301          * driver interface.
1302          */
1303         for (i = 1; i < iface->num_bss; i++) {
1304                 iface->bss[i]->driver = hapd->driver;
1305                 iface->bss[i]->drv_priv = hapd->drv_priv;
1306         }
1307
1308         if (hostapd_validate_bssid_configuration(iface))
1309                 return -1;
1310
1311         /*
1312          * Initialize control interfaces early to allow external monitoring of
1313          * channel setup operations that may take considerable amount of time
1314          * especially for DFS cases.
1315          */
1316         if (start_ctrl_iface(iface))
1317                 return -1;
1318
1319         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1320                 char country[4], previous_country[4];
1321
1322                 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1323                 if (hostapd_get_country(hapd, previous_country) < 0)
1324                         previous_country[0] = '\0';
1325
1326                 os_memcpy(country, hapd->iconf->country, 3);
1327                 country[3] = '\0';
1328                 if (hostapd_set_country(hapd, country) < 0) {
1329                         wpa_printf(MSG_ERROR, "Failed to set country code");
1330                         return -1;
1331                 }
1332
1333                 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1334                            previous_country, country);
1335
1336                 if (os_strncmp(previous_country, country, 2) != 0) {
1337                         wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1338                         iface->wait_channel_update = 1;
1339                         eloop_register_timeout(5, 0,
1340                                                channel_list_update_timeout,
1341                                                iface, NULL);
1342                         return 0;
1343                 }
1344         }
1345
1346         return setup_interface2(iface);
1347 }
1348
1349
1350 static int setup_interface2(struct hostapd_iface *iface)
1351 {
1352         iface->wait_channel_update = 0;
1353
1354         if (hostapd_get_hw_features(iface)) {
1355                 /* Not all drivers support this yet, so continue without hw
1356                  * feature data. */
1357         } else {
1358                 int ret = hostapd_select_hw_mode(iface);
1359                 if (ret < 0) {
1360                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1361                                    "channel. (%d)", ret);
1362                         goto fail;
1363                 }
1364                 if (ret == 1) {
1365                         wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1366                         return 0;
1367                 }
1368                 ret = hostapd_check_ht_capab(iface);
1369                 if (ret < 0)
1370                         goto fail;
1371                 if (ret == 1) {
1372                         wpa_printf(MSG_DEBUG, "Interface initialization will "
1373                                    "be completed in a callback");
1374                         return 0;
1375                 }
1376
1377                 if (iface->conf->ieee80211h)
1378                         wpa_printf(MSG_DEBUG, "DFS support is enabled");
1379         }
1380         return hostapd_setup_interface_complete(iface, 0);
1381
1382 fail:
1383         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1384         wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1385         if (iface->interfaces && iface->interfaces->terminate_on_error)
1386                 eloop_terminate();
1387         return -1;
1388 }
1389
1390
1391 #ifdef CONFIG_FST
1392
1393 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1394 {
1395         struct hostapd_data *hapd = ctx;
1396
1397         return hapd->own_addr;
1398 }
1399
1400
1401 static void fst_hostapd_get_channel_info_cb(void *ctx,
1402                                             enum hostapd_hw_mode *hw_mode,
1403                                             u8 *channel)
1404 {
1405         struct hostapd_data *hapd = ctx;
1406
1407         *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1408 }
1409
1410
1411 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1412 {
1413         struct hostapd_data *hapd = ctx;
1414
1415         if (hapd->iface->fst_ies != fst_ies) {
1416                 hapd->iface->fst_ies = fst_ies;
1417                 if (ieee802_11_set_beacon(hapd))
1418                         wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1419         }
1420 }
1421
1422
1423 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1424                                       struct wpabuf *buf)
1425 {
1426         struct hostapd_data *hapd = ctx;
1427
1428         return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1429                                        wpabuf_head(buf), wpabuf_len(buf));
1430 }
1431
1432
1433 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1434 {
1435         struct hostapd_data *hapd = ctx;
1436         struct sta_info *sta = ap_get_sta(hapd, addr);
1437
1438         return sta ? sta->mb_ies : NULL;
1439 }
1440
1441
1442 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1443                                         const u8 *buf, size_t size)
1444 {
1445         struct hostapd_data *hapd = ctx;
1446         struct sta_info *sta = ap_get_sta(hapd, addr);
1447
1448         if (sta) {
1449                 struct mb_ies_info info;
1450
1451                 if (!mb_ies_info_by_ies(&info, buf, size)) {
1452                         wpabuf_free(sta->mb_ies);
1453                         sta->mb_ies = mb_ies_by_info(&info);
1454                 }
1455         }
1456 }
1457
1458
1459 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1460                                       Boolean mb_only)
1461 {
1462         struct sta_info *s = (struct sta_info *) *get_ctx;
1463
1464         if (mb_only) {
1465                 for (; s && !s->mb_ies; s = s->next)
1466                         ;
1467         }
1468
1469         if (s) {
1470                 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1471
1472                 return s->addr;
1473         }
1474
1475         *get_ctx = NULL;
1476         return NULL;
1477 }
1478
1479
1480 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1481                                              struct fst_get_peer_ctx **get_ctx,
1482                                              Boolean mb_only)
1483 {
1484         struct hostapd_data *hapd = ctx;
1485
1486         *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1487
1488         return fst_hostapd_get_sta(get_ctx, mb_only);
1489 }
1490
1491
1492 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1493                                             struct fst_get_peer_ctx **get_ctx,
1494                                             Boolean mb_only)
1495 {
1496         return fst_hostapd_get_sta(get_ctx, mb_only);
1497 }
1498
1499
1500 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1501                                 struct fst_wpa_obj *iface_obj)
1502 {
1503         iface_obj->ctx = hapd;
1504         iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1505         iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1506         iface_obj->set_ies = fst_hostapd_set_ies_cb;
1507         iface_obj->send_action = fst_hostapd_send_action_cb;
1508         iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1509         iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1510         iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1511         iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1512 }
1513
1514 #endif /* CONFIG_FST */
1515
1516
1517 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1518                                                  int err)
1519 {
1520         struct hostapd_data *hapd = iface->bss[0];
1521         size_t j;
1522         u8 *prev_addr;
1523         int delay_apply_cfg = 0;
1524         int res_dfs_offload = 0;
1525
1526         if (err)
1527                 goto fail;
1528
1529         wpa_printf(MSG_DEBUG, "Completing interface initialization");
1530         if (iface->conf->channel) {
1531 #ifdef NEED_AP_MLME
1532                 int res;
1533 #endif /* NEED_AP_MLME */
1534
1535                 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1536                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1537                            "Frequency: %d MHz",
1538                            hostapd_hw_mode_txt(iface->conf->hw_mode),
1539                            iface->conf->channel, iface->freq);
1540
1541 #ifdef NEED_AP_MLME
1542                 /* Handle DFS only if it is not offloaded to the driver */
1543                 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1544                         /* Check DFS */
1545                         res = hostapd_handle_dfs(iface);
1546                         if (res <= 0) {
1547                                 if (res < 0)
1548                                         goto fail;
1549                                 return res;
1550                         }
1551                 } else {
1552                         /* If DFS is offloaded to the driver */
1553                         res_dfs_offload = hostapd_handle_dfs_offload(iface);
1554                         if (res_dfs_offload <= 0) {
1555                                 if (res_dfs_offload < 0)
1556                                         goto fail;
1557                         } else {
1558                                 wpa_printf(MSG_DEBUG,
1559                                            "Proceed with AP/channel setup");
1560                                 /*
1561                                  * If this is a DFS channel, move to completing
1562                                  * AP setup.
1563                                  */
1564                                 if (res_dfs_offload == 1)
1565                                         goto dfs_offload;
1566                                 /* Otherwise fall through. */
1567                         }
1568                 }
1569 #endif /* NEED_AP_MLME */
1570
1571 #ifdef CONFIG_MESH
1572                 if (iface->mconf != NULL) {
1573                         wpa_printf(MSG_DEBUG,
1574                                    "%s: Mesh configuration will be applied while joining the mesh network",
1575                                    iface->bss[0]->conf->iface);
1576                         delay_apply_cfg = 1;
1577                 }
1578 #endif /* CONFIG_MESH */
1579
1580                 if (!delay_apply_cfg &&
1581                     hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1582                                      hapd->iconf->channel,
1583                                      hapd->iconf->ieee80211n,
1584                                      hapd->iconf->ieee80211ac,
1585                                      hapd->iconf->secondary_channel,
1586                                      hapd->iconf->vht_oper_chwidth,
1587                                      hapd->iconf->vht_oper_centr_freq_seg0_idx,
1588                                      hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1589                         wpa_printf(MSG_ERROR, "Could not set channel for "
1590                                    "kernel driver");
1591                         goto fail;
1592                 }
1593         }
1594
1595         if (iface->current_mode) {
1596                 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1597                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
1598                                    "table.");
1599                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1600                                        HOSTAPD_LEVEL_WARNING,
1601                                        "Failed to prepare rates table.");
1602                         goto fail;
1603                 }
1604         }
1605
1606         if (hapd->iconf->rts_threshold > -1 &&
1607             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1608                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1609                            "kernel driver");
1610                 goto fail;
1611         }
1612
1613         if (hapd->iconf->fragm_threshold > -1 &&
1614             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1615                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1616                            "for kernel driver");
1617                 goto fail;
1618         }
1619
1620         prev_addr = hapd->own_addr;
1621
1622         for (j = 0; j < iface->num_bss; j++) {
1623                 hapd = iface->bss[j];
1624                 if (j)
1625                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1626                 if (hostapd_setup_bss(hapd, j == 0)) {
1627                         do {
1628                                 hapd = iface->bss[j];
1629                                 hostapd_bss_deinit_no_free(hapd);
1630                                 hostapd_free_hapd_data(hapd);
1631                         } while (j-- > 0);
1632                         goto fail;
1633                 }
1634                 if (is_zero_ether_addr(hapd->conf->bssid))
1635                         prev_addr = hapd->own_addr;
1636         }
1637         hapd = iface->bss[0];
1638
1639         hostapd_tx_queue_params(iface);
1640
1641         ap_list_init(iface);
1642         dl_list_init(&iface->sta_seen);
1643
1644         hostapd_set_acl(hapd);
1645
1646         if (hostapd_driver_commit(hapd) < 0) {
1647                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1648                            "configuration", __func__);
1649                 goto fail;
1650         }
1651
1652         /*
1653          * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1654          * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1655          * mode), the interface is up only after driver_commit, so initialize
1656          * WPS after driver_commit.
1657          */
1658         for (j = 0; j < iface->num_bss; j++) {
1659                 if (hostapd_init_wps_complete(iface->bss[j]))
1660                         goto fail;
1661         }
1662
1663         if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1664             !res_dfs_offload) {
1665                 /*
1666                  * If freq is DFS, and DFS is offloaded to the driver, then wait
1667                  * for CAC to complete.
1668                  */
1669                 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1670                 return res_dfs_offload;
1671         }
1672
1673 #ifdef NEED_AP_MLME
1674 dfs_offload:
1675 #endif /* NEED_AP_MLME */
1676
1677 #ifdef CONFIG_FST
1678         if (hapd->iconf->fst_cfg.group_id[0]) {
1679                 struct fst_wpa_obj iface_obj;
1680
1681                 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1682                 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1683                                         &iface_obj, &hapd->iconf->fst_cfg);
1684                 if (!iface->fst) {
1685                         wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1686                                    hapd->iconf->fst_cfg.group_id);
1687                         goto fail;
1688                 }
1689         }
1690 #endif /* CONFIG_FST */
1691
1692         hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1693         wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1694         if (hapd->setup_complete_cb)
1695                 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1696
1697         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1698                    iface->bss[0]->conf->iface);
1699         if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1700                 iface->interfaces->terminate_on_error--;
1701
1702         return 0;
1703
1704 fail:
1705         wpa_printf(MSG_ERROR, "Interface initialization failed");
1706         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1707         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1708 #ifdef CONFIG_FST
1709         if (iface->fst) {
1710                 fst_detach(iface->fst);
1711                 iface->fst = NULL;
1712         }
1713 #endif /* CONFIG_FST */
1714         if (iface->interfaces && iface->interfaces->terminate_on_error)
1715                 eloop_terminate();
1716         return -1;
1717 }
1718
1719
1720 /**
1721  * hostapd_setup_interface_complete - Complete interface setup
1722  *
1723  * This function is called when previous steps in the interface setup has been
1724  * completed. This can also start operations, e.g., DFS, that will require
1725  * additional processing before interface is ready to be enabled. Such
1726  * operations will call this function from eloop callbacks when finished.
1727  */
1728 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1729 {
1730         struct hapd_interfaces *interfaces = iface->interfaces;
1731         struct hostapd_data *hapd = iface->bss[0];
1732         unsigned int i;
1733         int not_ready_in_sync_ifaces = 0;
1734
1735         if (!iface->need_to_start_in_sync)
1736                 return hostapd_setup_interface_complete_sync(iface, err);
1737
1738         if (err) {
1739                 wpa_printf(MSG_ERROR, "Interface initialization failed");
1740                 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1741                 iface->need_to_start_in_sync = 0;
1742                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1743                 if (interfaces && interfaces->terminate_on_error)
1744                         eloop_terminate();
1745                 return -1;
1746         }
1747
1748         if (iface->ready_to_start_in_sync) {
1749                 /* Already in ready and waiting. should never happpen */
1750                 return 0;
1751         }
1752
1753         for (i = 0; i < interfaces->count; i++) {
1754                 if (interfaces->iface[i]->need_to_start_in_sync &&
1755                     !interfaces->iface[i]->ready_to_start_in_sync)
1756                         not_ready_in_sync_ifaces++;
1757         }
1758
1759         /*
1760          * Check if this is the last interface, if yes then start all the other
1761          * waiting interfaces. If not, add this interface to the waiting list.
1762          */
1763         if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1764                 /*
1765                  * If this interface went through CAC, do not synchronize, just
1766                  * start immediately.
1767                  */
1768                 iface->need_to_start_in_sync = 0;
1769                 wpa_printf(MSG_INFO,
1770                            "%s: Finished CAC - bypass sync and start interface",
1771                            iface->bss[0]->conf->iface);
1772                 return hostapd_setup_interface_complete_sync(iface, err);
1773         }
1774
1775         if (not_ready_in_sync_ifaces > 1) {
1776                 /* need to wait as there are other interfaces still coming up */
1777                 iface->ready_to_start_in_sync = 1;
1778                 wpa_printf(MSG_INFO,
1779                            "%s: Interface waiting to sync with other interfaces",
1780                            iface->bss[0]->conf->iface);
1781                 return 0;
1782         }
1783
1784         wpa_printf(MSG_INFO,
1785                    "%s: Last interface to sync - starting all interfaces",
1786                    iface->bss[0]->conf->iface);
1787         iface->need_to_start_in_sync = 0;
1788         hostapd_setup_interface_complete_sync(iface, err);
1789         for (i = 0; i < interfaces->count; i++) {
1790                 if (interfaces->iface[i]->need_to_start_in_sync &&
1791                     interfaces->iface[i]->ready_to_start_in_sync) {
1792                         hostapd_setup_interface_complete_sync(
1793                                 interfaces->iface[i], 0);
1794                         /* Only once the interfaces are sync started */
1795                         interfaces->iface[i]->need_to_start_in_sync = 0;
1796                 }
1797         }
1798
1799         return 0;
1800 }
1801
1802
1803 /**
1804  * hostapd_setup_interface - Setup of an interface
1805  * @iface: Pointer to interface data.
1806  * Returns: 0 on success, -1 on failure
1807  *
1808  * Initializes the driver interface, validates the configuration,
1809  * and sets driver parameters based on the configuration.
1810  * Flushes old stations, sets the channel, encryption,
1811  * beacons, and WDS links based on the configuration.
1812  *
1813  * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1814  * or DFS operations, this function returns 0 before such operations have been
1815  * completed. The pending operations are registered into eloop and will be
1816  * completed from eloop callbacks. Those callbacks end up calling
1817  * hostapd_setup_interface_complete() once setup has been completed.
1818  */
1819 int hostapd_setup_interface(struct hostapd_iface *iface)
1820 {
1821         int ret;
1822
1823         ret = setup_interface(iface);
1824         if (ret) {
1825                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1826                            iface->bss[0]->conf->iface);
1827                 return -1;
1828         }
1829
1830         return 0;
1831 }
1832
1833
1834 /**
1835  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1836  * @hapd_iface: Pointer to interface data
1837  * @conf: Pointer to per-interface configuration
1838  * @bss: Pointer to per-BSS configuration for this BSS
1839  * Returns: Pointer to allocated BSS data
1840  *
1841  * This function is used to allocate per-BSS data structure. This data will be
1842  * freed after hostapd_cleanup() is called for it during interface
1843  * deinitialization.
1844  */
1845 struct hostapd_data *
1846 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1847                        struct hostapd_config *conf,
1848                        struct hostapd_bss_config *bss)
1849 {
1850         struct hostapd_data *hapd;
1851
1852         hapd = os_zalloc(sizeof(*hapd));
1853         if (hapd == NULL)
1854                 return NULL;
1855
1856         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1857         hapd->iconf = conf;
1858         hapd->conf = bss;
1859         hapd->iface = hapd_iface;
1860         hapd->driver = hapd->iconf->driver;
1861         hapd->ctrl_sock = -1;
1862
1863         return hapd;
1864 }
1865
1866
1867 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1868 {
1869         wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1870                    hapd->conf->iface);
1871         hostapd_bss_deinit_no_free(hapd);
1872         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1873         hostapd_cleanup(hapd);
1874 }
1875
1876
1877 void hostapd_interface_deinit(struct hostapd_iface *iface)
1878 {
1879         int j;
1880
1881         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1882         if (iface == NULL)
1883                 return;
1884
1885         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1886
1887 #ifdef CONFIG_IEEE80211N
1888 #ifdef NEED_AP_MLME
1889         hostapd_stop_setup_timers(iface);
1890         eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1891 #endif /* NEED_AP_MLME */
1892 #endif /* CONFIG_IEEE80211N */
1893         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1894         iface->wait_channel_update = 0;
1895
1896 #ifdef CONFIG_FST
1897         if (iface->fst) {
1898                 fst_detach(iface->fst);
1899                 iface->fst = NULL;
1900         }
1901 #endif /* CONFIG_FST */
1902
1903         for (j = iface->num_bss - 1; j >= 0; j--)
1904                 hostapd_bss_deinit(iface->bss[j]);
1905 }
1906
1907
1908 void hostapd_interface_free(struct hostapd_iface *iface)
1909 {
1910         size_t j;
1911         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1912         for (j = 0; j < iface->num_bss; j++) {
1913                 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1914                            __func__, iface->bss[j]);
1915                 os_free(iface->bss[j]);
1916         }
1917         hostapd_cleanup_iface(iface);
1918 }
1919
1920
1921 /**
1922  * hostapd_init - Allocate and initialize per-interface data
1923  * @config_file: Path to the configuration file
1924  * Returns: Pointer to the allocated interface data or %NULL on failure
1925  *
1926  * This function is used to allocate main data structures for per-interface
1927  * data. The allocated data buffer will be freed by calling
1928  * hostapd_cleanup_iface().
1929  */
1930 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1931                                     const char *config_file)
1932 {
1933         struct hostapd_iface *hapd_iface = NULL;
1934         struct hostapd_config *conf = NULL;
1935         struct hostapd_data *hapd;
1936         size_t i;
1937
1938         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1939         if (hapd_iface == NULL)
1940                 goto fail;
1941
1942         hapd_iface->config_fname = os_strdup(config_file);
1943         if (hapd_iface->config_fname == NULL)
1944                 goto fail;
1945
1946         conf = interfaces->config_read_cb(hapd_iface->config_fname);
1947         if (conf == NULL)
1948                 goto fail;
1949         hapd_iface->conf = conf;
1950
1951         hapd_iface->num_bss = conf->num_bss;
1952         hapd_iface->bss = os_calloc(conf->num_bss,
1953                                     sizeof(struct hostapd_data *));
1954         if (hapd_iface->bss == NULL)
1955                 goto fail;
1956
1957         for (i = 0; i < conf->num_bss; i++) {
1958                 hapd = hapd_iface->bss[i] =
1959                         hostapd_alloc_bss_data(hapd_iface, conf,
1960                                                conf->bss[i]);
1961                 if (hapd == NULL)
1962                         goto fail;
1963                 hapd->msg_ctx = hapd;
1964         }
1965
1966         return hapd_iface;
1967
1968 fail:
1969         wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1970                    config_file);
1971         if (conf)
1972                 hostapd_config_free(conf);
1973         if (hapd_iface) {
1974                 os_free(hapd_iface->config_fname);
1975                 os_free(hapd_iface->bss);
1976                 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1977                            __func__, hapd_iface);
1978                 os_free(hapd_iface);
1979         }
1980         return NULL;
1981 }
1982
1983
1984 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1985 {
1986         size_t i, j;
1987
1988         for (i = 0; i < interfaces->count; i++) {
1989                 struct hostapd_iface *iface = interfaces->iface[i];
1990                 for (j = 0; j < iface->num_bss; j++) {
1991                         struct hostapd_data *hapd = iface->bss[j];
1992                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
1993                                 return 1;
1994                 }
1995         }
1996
1997         return 0;
1998 }
1999
2000
2001 /**
2002  * hostapd_interface_init_bss - Read configuration file and init BSS data
2003  *
2004  * This function is used to parse configuration file for a BSS. This BSS is
2005  * added to an existing interface sharing the same radio (if any) or a new
2006  * interface is created if this is the first interface on a radio. This
2007  * allocate memory for the BSS. No actual driver operations are started.
2008  *
2009  * This is similar to hostapd_interface_init(), but for a case where the
2010  * configuration is used to add a single BSS instead of all BSSes for a radio.
2011  */
2012 struct hostapd_iface *
2013 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2014                            const char *config_fname, int debug)
2015 {
2016         struct hostapd_iface *new_iface = NULL, *iface = NULL;
2017         struct hostapd_data *hapd;
2018         int k;
2019         size_t i, bss_idx;
2020
2021         if (!phy || !*phy)
2022                 return NULL;
2023
2024         for (i = 0; i < interfaces->count; i++) {
2025                 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2026                         iface = interfaces->iface[i];
2027                         break;
2028                 }
2029         }
2030
2031         wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2032                    config_fname, phy, iface ? "" : " --> new PHY");
2033         if (iface) {
2034                 struct hostapd_config *conf;
2035                 struct hostapd_bss_config **tmp_conf;
2036                 struct hostapd_data **tmp_bss;
2037                 struct hostapd_bss_config *bss;
2038                 const char *ifname;
2039
2040                 /* Add new BSS to existing iface */
2041                 conf = interfaces->config_read_cb(config_fname);
2042                 if (conf == NULL)
2043                         return NULL;
2044                 if (conf->num_bss > 1) {
2045                         wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2046                         hostapd_config_free(conf);
2047                         return NULL;
2048                 }
2049
2050                 ifname = conf->bss[0]->iface;
2051                 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2052                         wpa_printf(MSG_ERROR,
2053                                    "Interface name %s already in use", ifname);
2054                         hostapd_config_free(conf);
2055                         return NULL;
2056                 }
2057
2058                 tmp_conf = os_realloc_array(
2059                         iface->conf->bss, iface->conf->num_bss + 1,
2060                         sizeof(struct hostapd_bss_config *));
2061                 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2062                                            sizeof(struct hostapd_data *));
2063                 if (tmp_bss)
2064                         iface->bss = tmp_bss;
2065                 if (tmp_conf) {
2066                         iface->conf->bss = tmp_conf;
2067                         iface->conf->last_bss = tmp_conf[0];
2068                 }
2069                 if (tmp_bss == NULL || tmp_conf == NULL) {
2070                         hostapd_config_free(conf);
2071                         return NULL;
2072                 }
2073                 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2074                 iface->conf->num_bss++;
2075
2076                 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2077                 if (hapd == NULL) {
2078                         iface->conf->num_bss--;
2079                         hostapd_config_free(conf);
2080                         return NULL;
2081                 }
2082                 iface->conf->last_bss = bss;
2083                 iface->bss[iface->num_bss] = hapd;
2084                 hapd->msg_ctx = hapd;
2085
2086                 bss_idx = iface->num_bss++;
2087                 conf->num_bss--;
2088                 conf->bss[0] = NULL;
2089                 hostapd_config_free(conf);
2090         } else {
2091                 /* Add a new iface with the first BSS */
2092                 new_iface = iface = hostapd_init(interfaces, config_fname);
2093                 if (!iface)
2094                         return NULL;
2095                 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2096                 iface->interfaces = interfaces;
2097                 bss_idx = 0;
2098         }
2099
2100         for (k = 0; k < debug; k++) {
2101                 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2102                         iface->bss[bss_idx]->conf->logger_stdout_level--;
2103         }
2104
2105         if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2106             !hostapd_drv_none(iface->bss[bss_idx])) {
2107                 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2108                            config_fname);
2109                 if (new_iface)
2110                         hostapd_interface_deinit_free(new_iface);
2111                 return NULL;
2112         }
2113
2114         return iface;
2115 }
2116
2117
2118 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2119 {
2120         const struct wpa_driver_ops *driver;
2121         void *drv_priv;
2122
2123         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2124         if (iface == NULL)
2125                 return;
2126         wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2127                    __func__, (unsigned int) iface->num_bss,
2128                    (unsigned int) iface->conf->num_bss);
2129         driver = iface->bss[0]->driver;
2130         drv_priv = iface->bss[0]->drv_priv;
2131         hostapd_interface_deinit(iface);
2132         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2133                    __func__, driver, drv_priv);
2134         if (driver && driver->hapd_deinit && drv_priv) {
2135                 driver->hapd_deinit(drv_priv);
2136                 iface->bss[0]->drv_priv = NULL;
2137         }
2138         hostapd_interface_free(iface);
2139 }
2140
2141
2142 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2143                                   void *drv_priv,
2144                                   struct hostapd_iface *hapd_iface)
2145 {
2146         size_t j;
2147
2148         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2149                    __func__, driver, drv_priv);
2150         if (driver && driver->hapd_deinit && drv_priv) {
2151                 driver->hapd_deinit(drv_priv);
2152                 for (j = 0; j < hapd_iface->num_bss; j++) {
2153                         wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2154                                    __func__, (int) j,
2155                                    hapd_iface->bss[j]->drv_priv);
2156                         if (hapd_iface->bss[j]->drv_priv == drv_priv)
2157                                 hapd_iface->bss[j]->drv_priv = NULL;
2158                 }
2159         }
2160 }
2161
2162
2163 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2164 {
2165         size_t j;
2166
2167         if (hapd_iface->bss[0]->drv_priv != NULL) {
2168                 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2169                            hapd_iface->conf->bss[0]->iface);
2170                 return -1;
2171         }
2172
2173         wpa_printf(MSG_DEBUG, "Enable interface %s",
2174                    hapd_iface->conf->bss[0]->iface);
2175
2176         for (j = 0; j < hapd_iface->num_bss; j++)
2177                 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2178         if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2179                 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2180                 return -1;
2181         }
2182
2183         if (hapd_iface->interfaces == NULL ||
2184             hapd_iface->interfaces->driver_init == NULL ||
2185             hapd_iface->interfaces->driver_init(hapd_iface))
2186                 return -1;
2187
2188         if (hostapd_setup_interface(hapd_iface)) {
2189                 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2190                                       hapd_iface->bss[0]->drv_priv,
2191                                       hapd_iface);
2192                 return -1;
2193         }
2194
2195         return 0;
2196 }
2197
2198
2199 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2200 {
2201         size_t j;
2202
2203         wpa_printf(MSG_DEBUG, "Reload interface %s",
2204                    hapd_iface->conf->bss[0]->iface);
2205         for (j = 0; j < hapd_iface->num_bss; j++)
2206                 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2207         if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2208                 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2209                 return -1;
2210         }
2211         hostapd_clear_old(hapd_iface);
2212         for (j = 0; j < hapd_iface->num_bss; j++)
2213                 hostapd_reload_bss(hapd_iface->bss[j]);
2214
2215         return 0;
2216 }
2217
2218
2219 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2220 {
2221         size_t j;
2222         const struct wpa_driver_ops *driver;
2223         void *drv_priv;
2224
2225         if (hapd_iface == NULL)
2226                 return -1;
2227
2228         if (hapd_iface->bss[0]->drv_priv == NULL) {
2229                 wpa_printf(MSG_INFO, "Interface %s already disabled",
2230                            hapd_iface->conf->bss[0]->iface);
2231                 return -1;
2232         }
2233
2234         wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2235         driver = hapd_iface->bss[0]->driver;
2236         drv_priv = hapd_iface->bss[0]->drv_priv;
2237
2238         hapd_iface->driver_ap_teardown =
2239                 !!(hapd_iface->drv_flags &
2240                    WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2241
2242         /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2243         for (j = 0; j < hapd_iface->num_bss; j++) {
2244                 struct hostapd_data *hapd = hapd_iface->bss[j];
2245                 hostapd_bss_deinit_no_free(hapd);
2246                 hostapd_free_hapd_data(hapd);
2247         }
2248
2249         hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2250
2251         /* From hostapd_cleanup_iface: These were initialized in
2252          * hostapd_setup_interface and hostapd_setup_interface_complete
2253          */
2254         hostapd_cleanup_iface_partial(hapd_iface);
2255
2256         wpa_printf(MSG_DEBUG, "Interface %s disabled",
2257                    hapd_iface->bss[0]->conf->iface);
2258         hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2259         return 0;
2260 }
2261
2262
2263 static struct hostapd_iface *
2264 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2265 {
2266         struct hostapd_iface **iface, *hapd_iface;
2267
2268         iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2269                                  sizeof(struct hostapd_iface *));
2270         if (iface == NULL)
2271                 return NULL;
2272         interfaces->iface = iface;
2273         hapd_iface = interfaces->iface[interfaces->count] =
2274                 os_zalloc(sizeof(*hapd_iface));
2275         if (hapd_iface == NULL) {
2276                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2277                            "the interface", __func__);
2278                 return NULL;
2279         }
2280         interfaces->count++;
2281         hapd_iface->interfaces = interfaces;
2282
2283         return hapd_iface;
2284 }
2285
2286
2287 static struct hostapd_config *
2288 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2289                      const char *ctrl_iface, const char *driver)
2290 {
2291         struct hostapd_bss_config *bss;
2292         struct hostapd_config *conf;
2293
2294         /* Allocates memory for bss and conf */
2295         conf = hostapd_config_defaults();
2296         if (conf == NULL) {
2297                  wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2298                                 "configuration", __func__);
2299                 return NULL;
2300         }
2301
2302         if (driver) {
2303                 int j;
2304
2305                 for (j = 0; wpa_drivers[j]; j++) {
2306                         if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2307                                 conf->driver = wpa_drivers[j];
2308                                 goto skip;
2309                         }
2310                 }
2311
2312                 wpa_printf(MSG_ERROR,
2313                            "Invalid/unknown driver '%s' - registering the default driver",
2314                            driver);
2315         }
2316
2317         conf->driver = wpa_drivers[0];
2318         if (conf->driver == NULL) {
2319                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2320                 hostapd_config_free(conf);
2321                 return NULL;
2322         }
2323
2324 skip:
2325         bss = conf->last_bss = conf->bss[0];
2326
2327         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2328         bss->ctrl_interface = os_strdup(ctrl_iface);
2329         if (bss->ctrl_interface == NULL) {
2330                 hostapd_config_free(conf);
2331                 return NULL;
2332         }
2333
2334         /* Reading configuration file skipped, will be done in SET!
2335          * From reading the configuration till the end has to be done in
2336          * SET
2337          */
2338         return conf;
2339 }
2340
2341
2342 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2343                               struct hostapd_config *conf)
2344 {
2345         size_t i;
2346         struct hostapd_data *hapd;
2347
2348         hapd_iface->bss = os_calloc(conf->num_bss,
2349                                     sizeof(struct hostapd_data *));
2350         if (hapd_iface->bss == NULL)
2351                 return -1;
2352
2353         for (i = 0; i < conf->num_bss; i++) {
2354                 hapd = hapd_iface->bss[i] =
2355                         hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2356                 if (hapd == NULL) {
2357                         while (i > 0) {
2358                                 i--;
2359                                 os_free(hapd_iface->bss[i]);
2360                                 hapd_iface->bss[i] = NULL;
2361                         }
2362                         os_free(hapd_iface->bss);
2363                         hapd_iface->bss = NULL;
2364                         return -1;
2365                 }
2366                 hapd->msg_ctx = hapd;
2367         }
2368
2369         hapd_iface->conf = conf;
2370         hapd_iface->num_bss = conf->num_bss;
2371
2372         return 0;
2373 }
2374
2375
2376 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2377 {
2378         struct hostapd_config *conf = NULL;
2379         struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2380         struct hostapd_data *hapd;
2381         char *ptr;
2382         size_t i, j;
2383         const char *conf_file = NULL, *phy_name = NULL;
2384
2385         if (os_strncmp(buf, "bss_config=", 11) == 0) {
2386                 char *pos;
2387                 phy_name = buf + 11;
2388                 pos = os_strchr(phy_name, ':');
2389                 if (!pos)
2390                         return -1;
2391                 *pos++ = '\0';
2392                 conf_file = pos;
2393                 if (!os_strlen(conf_file))
2394                         return -1;
2395
2396                 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2397                                                         conf_file, 0);
2398                 if (!hapd_iface)
2399                         return -1;
2400                 for (j = 0; j < interfaces->count; j++) {
2401                         if (interfaces->iface[j] == hapd_iface)
2402                                 break;
2403                 }
2404                 if (j == interfaces->count) {
2405                         struct hostapd_iface **tmp;
2406                         tmp = os_realloc_array(interfaces->iface,
2407                                                interfaces->count + 1,
2408                                                sizeof(struct hostapd_iface *));
2409                         if (!tmp) {
2410                                 hostapd_interface_deinit_free(hapd_iface);
2411                                 return -1;
2412                         }
2413                         interfaces->iface = tmp;
2414                         interfaces->iface[interfaces->count++] = hapd_iface;
2415                         new_iface = hapd_iface;
2416                 }
2417
2418                 if (new_iface) {
2419                         if (interfaces->driver_init(hapd_iface))
2420                                 goto fail;
2421
2422                         if (hostapd_setup_interface(hapd_iface)) {
2423                                 hostapd_deinit_driver(
2424                                         hapd_iface->bss[0]->driver,
2425                                         hapd_iface->bss[0]->drv_priv,
2426                                         hapd_iface);
2427                                 goto fail;
2428                         }
2429                 } else {
2430                         /* Assign new BSS with bss[0]'s driver info */
2431                         hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2432                         hapd->driver = hapd_iface->bss[0]->driver;
2433                         hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2434                         os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2435                                   ETH_ALEN);
2436
2437                         if (start_ctrl_iface_bss(hapd) < 0 ||
2438                             (hapd_iface->state == HAPD_IFACE_ENABLED &&
2439                              hostapd_setup_bss(hapd, -1))) {
2440                                 hostapd_cleanup(hapd);
2441                                 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2442                                 hapd_iface->conf->num_bss--;
2443                                 hapd_iface->num_bss--;
2444                                 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2445                                            __func__, hapd, hapd->conf->iface);
2446                                 hostapd_config_free_bss(hapd->conf);
2447                                 hapd->conf = NULL;
2448                                 os_free(hapd);
2449                                 return -1;
2450                         }
2451                 }
2452                 return 0;
2453         }
2454
2455         ptr = os_strchr(buf, ' ');
2456         if (ptr == NULL)
2457                 return -1;
2458         *ptr++ = '\0';
2459
2460         if (os_strncmp(ptr, "config=", 7) == 0)
2461                 conf_file = ptr + 7;
2462
2463         for (i = 0; i < interfaces->count; i++) {
2464                 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2465                                buf)) {
2466                         wpa_printf(MSG_INFO, "Cannot add interface - it "
2467                                    "already exists");
2468                         return -1;
2469                 }
2470         }
2471
2472         hapd_iface = hostapd_iface_alloc(interfaces);
2473         if (hapd_iface == NULL) {
2474                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2475                            "for interface", __func__);
2476                 goto fail;
2477         }
2478         new_iface = hapd_iface;
2479
2480         if (conf_file && interfaces->config_read_cb) {
2481                 conf = interfaces->config_read_cb(conf_file);
2482                 if (conf && conf->bss)
2483                         os_strlcpy(conf->bss[0]->iface, buf,
2484                                    sizeof(conf->bss[0]->iface));
2485         } else {
2486                 char *driver = os_strchr(ptr, ' ');
2487
2488                 if (driver)
2489                         *driver++ = '\0';
2490                 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2491         }
2492
2493         if (conf == NULL || conf->bss == NULL) {
2494                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2495                            "for configuration", __func__);
2496                 goto fail;
2497         }
2498
2499         if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2500                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2501                            "for hostapd", __func__);
2502                 goto fail;
2503         }
2504         conf = NULL;
2505
2506         if (start_ctrl_iface(hapd_iface) < 0)
2507                 goto fail;
2508
2509         wpa_printf(MSG_INFO, "Add interface '%s'",
2510                    hapd_iface->conf->bss[0]->iface);
2511
2512         return 0;
2513
2514 fail:
2515         if (conf)
2516                 hostapd_config_free(conf);
2517         if (hapd_iface) {
2518                 if (hapd_iface->bss) {
2519                         for (i = 0; i < hapd_iface->num_bss; i++) {
2520                                 hapd = hapd_iface->bss[i];
2521                                 if (!hapd)
2522                                         continue;
2523                                 if (hapd_iface->interfaces &&
2524                                     hapd_iface->interfaces->ctrl_iface_deinit)
2525                                         hapd_iface->interfaces->
2526                                                 ctrl_iface_deinit(hapd);
2527                                 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2528                                            __func__, hapd_iface->bss[i],
2529                                            hapd->conf->iface);
2530                                 hostapd_cleanup(hapd);
2531                                 os_free(hapd);
2532                                 hapd_iface->bss[i] = NULL;
2533                         }
2534                         os_free(hapd_iface->bss);
2535                         hapd_iface->bss = NULL;
2536                 }
2537                 if (new_iface) {
2538                         interfaces->count--;
2539                         interfaces->iface[interfaces->count] = NULL;
2540                 }
2541                 hostapd_cleanup_iface(hapd_iface);
2542         }
2543         return -1;
2544 }
2545
2546
2547 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2548 {
2549         size_t i;
2550
2551         wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2552
2553         /* Remove hostapd_data only if it has already been initialized */
2554         if (idx < iface->num_bss) {
2555                 struct hostapd_data *hapd = iface->bss[idx];
2556
2557                 hostapd_bss_deinit(hapd);
2558                 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2559                            __func__, hapd, hapd->conf->iface);
2560                 hostapd_config_free_bss(hapd->conf);
2561                 hapd->conf = NULL;
2562                 os_free(hapd);
2563
2564                 iface->num_bss--;
2565
2566                 for (i = idx; i < iface->num_bss; i++)
2567                         iface->bss[i] = iface->bss[i + 1];
2568         } else {
2569                 hostapd_config_free_bss(iface->conf->bss[idx]);
2570                 iface->conf->bss[idx] = NULL;
2571         }
2572
2573         iface->conf->num_bss--;
2574         for (i = idx; i < iface->conf->num_bss; i++)
2575                 iface->conf->bss[i] = iface->conf->bss[i + 1];
2576
2577         return 0;
2578 }
2579
2580
2581 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2582 {
2583         struct hostapd_iface *hapd_iface;
2584         size_t i, j, k = 0;
2585
2586         for (i = 0; i < interfaces->count; i++) {
2587                 hapd_iface = interfaces->iface[i];
2588                 if (hapd_iface == NULL)
2589                         return -1;
2590                 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2591                         wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2592                         hapd_iface->driver_ap_teardown =
2593                                 !!(hapd_iface->drv_flags &
2594                                    WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2595
2596                         hostapd_interface_deinit_free(hapd_iface);
2597                         k = i;
2598                         while (k < (interfaces->count - 1)) {
2599                                 interfaces->iface[k] =
2600                                         interfaces->iface[k + 1];
2601                                 k++;
2602                         }
2603                         interfaces->count--;
2604                         return 0;
2605                 }
2606
2607                 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2608                         if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2609                                 hapd_iface->driver_ap_teardown =
2610                                         !(hapd_iface->drv_flags &
2611                                           WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2612                                 return hostapd_remove_bss(hapd_iface, j);
2613                         }
2614                 }
2615         }
2616         return -1;
2617 }
2618
2619
2620 /**
2621  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2622  * @hapd: Pointer to BSS data
2623  * @sta: Pointer to the associated STA data
2624  * @reassoc: 1 to indicate this was a re-association; 0 = first association
2625  *
2626  * This function will be called whenever a station associates with the AP. It
2627  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2628  * from drv_callbacks.c based on driver events for drivers that take care of
2629  * management frames (IEEE 802.11 authentication and association) internally.
2630  */
2631 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2632                            int reassoc)
2633 {
2634         if (hapd->tkip_countermeasures) {
2635                 hostapd_drv_sta_deauth(hapd, sta->addr,
2636                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
2637                 return;
2638         }
2639
2640         hostapd_prune_associations(hapd, sta->addr);
2641         ap_sta_clear_disconnect_timeouts(hapd, sta);
2642
2643         /* IEEE 802.11F (IAPP) */
2644         if (hapd->conf->ieee802_11f)
2645                 iapp_new_station(hapd->iapp, sta);
2646
2647 #ifdef CONFIG_P2P
2648         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2649                 sta->no_p2p_set = 1;
2650                 hapd->num_sta_no_p2p++;
2651                 if (hapd->num_sta_no_p2p == 1)
2652                         hostapd_p2p_non_p2p_sta_connected(hapd);
2653         }
2654 #endif /* CONFIG_P2P */
2655
2656         /* Start accounting here, if IEEE 802.1X and WPA are not used.
2657          * IEEE 802.1X/WPA code will start accounting after the station has
2658          * been authorized. */
2659         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2660                 ap_sta_set_authorized(hapd, sta, 1);
2661                 os_get_reltime(&sta->connected_time);
2662                 accounting_sta_start(hapd, sta);
2663         }
2664
2665         /* Start IEEE 802.1X authentication process for new stations */
2666         ieee802_1x_new_station(hapd, sta);
2667         if (reassoc) {
2668                 if (sta->auth_alg != WLAN_AUTH_FT &&
2669                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2670                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2671         } else
2672                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2673
2674         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2675                 wpa_printf(MSG_DEBUG,
2676                            "%s: %s: reschedule ap_handle_timer timeout for "
2677                            MACSTR " (%d seconds - ap_max_inactivity)",
2678                            hapd->conf->iface, __func__, MAC2STR(sta->addr),
2679                            hapd->conf->ap_max_inactivity);
2680                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2681                 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2682                                        ap_handle_timer, hapd, sta);
2683         }
2684 }
2685
2686
2687 const char * hostapd_state_text(enum hostapd_iface_state s)
2688 {
2689         switch (s) {
2690         case HAPD_IFACE_UNINITIALIZED:
2691                 return "UNINITIALIZED";
2692         case HAPD_IFACE_DISABLED:
2693                 return "DISABLED";
2694         case HAPD_IFACE_COUNTRY_UPDATE:
2695                 return "COUNTRY_UPDATE";
2696         case HAPD_IFACE_ACS:
2697                 return "ACS";
2698         case HAPD_IFACE_HT_SCAN:
2699                 return "HT_SCAN";
2700         case HAPD_IFACE_DFS:
2701                 return "DFS";
2702         case HAPD_IFACE_ENABLED:
2703                 return "ENABLED";
2704         }
2705
2706         return "UNKNOWN";
2707 }
2708
2709
2710 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2711 {
2712         wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2713                    iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2714                    hostapd_state_text(s));
2715         iface->state = s;
2716 }
2717
2718
2719 int hostapd_csa_in_progress(struct hostapd_iface *iface)
2720 {
2721         unsigned int i;
2722
2723         for (i = 0; i < iface->num_bss; i++)
2724                 if (iface->bss[i]->csa_in_progress)
2725                         return 1;
2726         return 0;
2727 }
2728
2729
2730 #ifdef NEED_AP_MLME
2731
2732 static void free_beacon_data(struct beacon_data *beacon)
2733 {
2734         os_free(beacon->head);
2735         beacon->head = NULL;
2736         os_free(beacon->tail);
2737         beacon->tail = NULL;
2738         os_free(beacon->probe_resp);
2739         beacon->probe_resp = NULL;
2740         os_free(beacon->beacon_ies);
2741         beacon->beacon_ies = NULL;
2742         os_free(beacon->proberesp_ies);
2743         beacon->proberesp_ies = NULL;
2744         os_free(beacon->assocresp_ies);
2745         beacon->assocresp_ies = NULL;
2746 }
2747
2748
2749 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2750                                      struct beacon_data *beacon)
2751 {
2752         struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2753         struct wpa_driver_ap_params params;
2754         int ret;
2755
2756         os_memset(beacon, 0, sizeof(*beacon));
2757         ret = ieee802_11_build_ap_params(hapd, &params);
2758         if (ret < 0)
2759                 return ret;
2760
2761         ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2762                                          &proberesp_extra,
2763                                          &assocresp_extra);
2764         if (ret)
2765                 goto free_ap_params;
2766
2767         ret = -1;
2768         beacon->head = os_malloc(params.head_len);
2769         if (!beacon->head)
2770                 goto free_ap_extra_ies;
2771
2772         os_memcpy(beacon->head, params.head, params.head_len);
2773         beacon->head_len = params.head_len;
2774
2775         beacon->tail = os_malloc(params.tail_len);
2776         if (!beacon->tail)
2777                 goto free_beacon;
2778
2779         os_memcpy(beacon->tail, params.tail, params.tail_len);
2780         beacon->tail_len = params.tail_len;
2781
2782         if (params.proberesp != NULL) {
2783                 beacon->probe_resp = os_malloc(params.proberesp_len);
2784                 if (!beacon->probe_resp)
2785                         goto free_beacon;
2786
2787                 os_memcpy(beacon->probe_resp, params.proberesp,
2788                           params.proberesp_len);
2789                 beacon->probe_resp_len = params.proberesp_len;
2790         }
2791
2792         /* copy the extra ies */
2793         if (beacon_extra) {
2794                 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2795                 if (!beacon->beacon_ies)
2796                         goto free_beacon;
2797
2798                 os_memcpy(beacon->beacon_ies,
2799                           beacon_extra->buf, wpabuf_len(beacon_extra));
2800                 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2801         }
2802
2803         if (proberesp_extra) {
2804                 beacon->proberesp_ies =
2805                         os_malloc(wpabuf_len(proberesp_extra));
2806                 if (!beacon->proberesp_ies)
2807                         goto free_beacon;
2808
2809                 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2810                           wpabuf_len(proberesp_extra));
2811                 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2812         }
2813
2814         if (assocresp_extra) {
2815                 beacon->assocresp_ies =
2816                         os_malloc(wpabuf_len(assocresp_extra));
2817                 if (!beacon->assocresp_ies)
2818                         goto free_beacon;
2819
2820                 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2821                           wpabuf_len(assocresp_extra));
2822                 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2823         }
2824
2825         ret = 0;
2826 free_beacon:
2827         /* if the function fails, the caller should not free beacon data */
2828         if (ret)
2829                 free_beacon_data(beacon);
2830
2831 free_ap_extra_ies:
2832         hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2833                                   assocresp_extra);
2834 free_ap_params:
2835         ieee802_11_free_ap_params(&params);
2836         return ret;
2837 }
2838
2839
2840 /*
2841  * TODO: This flow currently supports only changing channel and width within
2842  * the same hw_mode. Any other changes to MAC parameters or provided settings
2843  * are not supported.
2844  */
2845 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2846                                       struct hostapd_config *conf,
2847                                       struct hostapd_freq_params *params,
2848                                       struct hostapd_freq_params *old_params)
2849 {
2850         int channel;
2851
2852         if (!params->channel) {
2853                 /* check if the new channel is supported by hw */
2854                 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2855         }
2856
2857         channel = params->channel;
2858         if (!channel)
2859                 return -1;
2860
2861         /* if a pointer to old_params is provided we save previous state */
2862         if (old_params &&
2863             hostapd_set_freq_params(old_params, conf->hw_mode,
2864                                     hostapd_hw_get_freq(hapd, conf->channel),
2865                                     conf->channel, conf->ieee80211n,
2866                                     conf->ieee80211ac,
2867                                     conf->secondary_channel,
2868                                     conf->vht_oper_chwidth,
2869                                     conf->vht_oper_centr_freq_seg0_idx,
2870                                     conf->vht_oper_centr_freq_seg1_idx,
2871                                     conf->vht_capab))
2872                 return -1;
2873
2874         switch (params->bandwidth) {
2875         case 0:
2876         case 20:
2877         case 40:
2878                 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2879                 break;
2880         case 80:
2881                 if (params->center_freq2)
2882                         conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2883                 else
2884                         conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2885                 break;
2886         case 160:
2887                 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2888                 break;
2889         default:
2890                 return -1;
2891         }
2892
2893         conf->channel = channel;
2894         conf->ieee80211n = params->ht_enabled;
2895         conf->secondary_channel = params->sec_channel_offset;
2896         ieee80211_freq_to_chan(params->center_freq1,
2897                                &conf->vht_oper_centr_freq_seg0_idx);
2898         ieee80211_freq_to_chan(params->center_freq2,
2899                                &conf->vht_oper_centr_freq_seg1_idx);
2900
2901         /* TODO: maybe call here hostapd_config_check here? */
2902
2903         return 0;
2904 }
2905
2906
2907 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2908                                      struct csa_settings *settings)
2909 {
2910         struct hostapd_iface *iface = hapd->iface;
2911         struct hostapd_freq_params old_freq;
2912         int ret;
2913         u8 chan, vht_bandwidth;
2914
2915         os_memset(&old_freq, 0, sizeof(old_freq));
2916         if (!iface || !iface->freq || hapd->csa_in_progress)
2917                 return -1;
2918
2919         switch (settings->freq_params.bandwidth) {
2920         case 80:
2921                 if (settings->freq_params.center_freq2)
2922                         vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2923                 else
2924                         vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2925                 break;
2926         case 160:
2927                 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2928                 break;
2929         default:
2930                 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2931                 break;
2932         }
2933
2934         if (ieee80211_freq_to_channel_ext(
2935                     settings->freq_params.freq,
2936                     settings->freq_params.sec_channel_offset,
2937                     vht_bandwidth,
2938                     &hapd->iface->cs_oper_class,
2939                     &chan) == NUM_HOSTAPD_MODES) {
2940                 wpa_printf(MSG_DEBUG,
2941                            "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2942                            settings->freq_params.freq,
2943                            settings->freq_params.sec_channel_offset,
2944                            settings->freq_params.vht_enabled);
2945                 return -1;
2946         }
2947
2948         settings->freq_params.channel = chan;
2949
2950         ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2951                                          &settings->freq_params,
2952                                          &old_freq);
2953         if (ret)
2954                 return ret;
2955
2956         ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2957
2958         /* change back the configuration */
2959         hostapd_change_config_freq(iface->bss[0], iface->conf,
2960                                    &old_freq, NULL);
2961
2962         if (ret)
2963                 return ret;
2964
2965         /* set channel switch parameters for csa ie */
2966         hapd->cs_freq_params = settings->freq_params;
2967         hapd->cs_count = settings->cs_count;
2968         hapd->cs_block_tx = settings->block_tx;
2969
2970         ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2971         if (ret) {
2972                 free_beacon_data(&settings->beacon_after);
2973                 return ret;
2974         }
2975
2976         settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2977         settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2978         settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2979         settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
2980
2981         return 0;
2982 }
2983
2984
2985 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2986 {
2987         os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2988         hapd->cs_count = 0;
2989         hapd->cs_block_tx = 0;
2990         hapd->cs_c_off_beacon = 0;
2991         hapd->cs_c_off_proberesp = 0;
2992         hapd->csa_in_progress = 0;
2993         hapd->cs_c_off_ecsa_beacon = 0;
2994         hapd->cs_c_off_ecsa_proberesp = 0;
2995 }
2996
2997
2998 int hostapd_switch_channel(struct hostapd_data *hapd,
2999                            struct csa_settings *settings)
3000 {
3001         int ret;
3002
3003         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3004                 wpa_printf(MSG_INFO, "CSA is not supported");
3005                 return -1;
3006         }
3007
3008         ret = hostapd_fill_csa_settings(hapd, settings);
3009         if (ret)
3010                 return ret;
3011
3012         ret = hostapd_drv_switch_channel(hapd, settings);
3013         free_beacon_data(&settings->beacon_csa);
3014         free_beacon_data(&settings->beacon_after);
3015
3016         if (ret) {
3017                 /* if we failed, clean cs parameters */
3018                 hostapd_cleanup_cs_params(hapd);
3019                 return ret;
3020         }
3021
3022         hapd->csa_in_progress = 1;
3023         return 0;
3024 }
3025
3026
3027 void
3028 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3029                                 const struct hostapd_freq_params *freq_params)
3030 {
3031         int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3032         unsigned int i;
3033
3034         wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3035
3036         if (freq_params->center_freq1)
3037                 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3038         if (freq_params->center_freq2)
3039                 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3040
3041         switch (freq_params->bandwidth) {
3042         case 0:
3043         case 20:
3044         case 40:
3045                 vht_bw = VHT_CHANWIDTH_USE_HT;
3046                 break;
3047         case 80:
3048                 if (freq_params->center_freq2)
3049                         vht_bw = VHT_CHANWIDTH_80P80MHZ;
3050                 else
3051                         vht_bw = VHT_CHANWIDTH_80MHZ;
3052                 break;
3053         case 160:
3054                 vht_bw = VHT_CHANWIDTH_160MHZ;
3055                 break;
3056         default:
3057                 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3058                            freq_params->bandwidth);
3059                 break;
3060         }
3061
3062         iface->freq = freq_params->freq;
3063         iface->conf->channel = freq_params->channel;
3064         iface->conf->secondary_channel = freq_params->sec_channel_offset;
3065         iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3066         iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3067         iface->conf->vht_oper_chwidth = vht_bw;
3068         iface->conf->ieee80211n = freq_params->ht_enabled;
3069         iface->conf->ieee80211ac = freq_params->vht_enabled;
3070
3071         /*
3072          * cs_params must not be cleared earlier because the freq_params
3073          * argument may actually point to one of these.
3074          */
3075         for (i = 0; i < iface->num_bss; i++)
3076                 hostapd_cleanup_cs_params(iface->bss[i]);
3077
3078         hostapd_disable_iface(iface);
3079         hostapd_enable_iface(iface);
3080 }
3081
3082
3083 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3084                                         const char *ifname)
3085 {
3086         size_t i, j;
3087
3088         for (i = 0; i < interfaces->count; i++) {
3089                 struct hostapd_iface *iface = interfaces->iface[i];
3090
3091                 for (j = 0; j < iface->num_bss; j++) {
3092                         struct hostapd_data *hapd = iface->bss[j];
3093
3094                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
3095                                 return hapd;
3096                 }
3097         }
3098
3099         return NULL;
3100 }
3101
3102 #endif /* NEED_AP_MLME */
3103
3104
3105 void hostapd_periodic_iface(struct hostapd_iface *iface)
3106 {
3107         size_t i;
3108
3109         ap_list_timer(iface);
3110
3111         for (i = 0; i < iface->num_bss; i++) {
3112                 struct hostapd_data *hapd = iface->bss[i];
3113
3114                 if (!hapd->started)
3115                         continue;
3116
3117 #ifndef CONFIG_NO_RADIUS
3118                 hostapd_acl_expire(hapd);
3119 #endif /* CONFIG_NO_RADIUS */
3120         }
3121 }