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