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