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