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