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