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