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