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