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