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