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