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