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