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