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