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