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