1 /*
2  * WPA Supplicant - Basic AP mode support routines
3  * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2009, Atheros Communications
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "utils/includes.h"
11 
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "crypto/dh_group5.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/ap_drv_ops.h"
22 #ifdef NEED_AP_MLME
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
29 #include "ap/dfs.h"
30 #include "wps/wps.h"
31 #include "common/ieee802_11_defs.h"
32 #include "config_ssid.h"
33 #include "config.h"
34 #include "wpa_supplicant_i.h"
35 #include "driver_i.h"
36 #include "p2p_supplicant.h"
37 #include "ap.h"
38 #include "ap/sta_info.h"
39 #include "notify.h"
40 
41 
42 #ifdef CONFIG_WPS
43 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
44 #endif /* CONFIG_WPS */
45 
46 
47 #ifdef CONFIG_P2P
is_chanwidth160_supported(struct hostapd_hw_modes * mode,struct hostapd_config * conf)48 static bool is_chanwidth160_supported(struct hostapd_hw_modes *mode,
49 				      struct hostapd_config *conf)
50 {
51 #ifdef CONFIG_IEEE80211AX
52 	if (conf->ieee80211ax) {
53 		struct he_capabilities *he_cap;
54 
55 		he_cap = &mode->he_capab[IEEE80211_MODE_AP];
56 		if (he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
57 		    (HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G |
58 		     HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))
59 			return true;
60 	}
61 #endif /* CONFIG_IEEE80211AX */
62 	if (mode->vht_capab & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
63 			       VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
64 		return true;
65 	return false;
66 }
67 #endif /* CONFIG_P2P */
68 
69 
wpas_conf_ap_vht(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct hostapd_config * conf,struct hostapd_hw_modes * mode)70 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
71 			     struct wpa_ssid *ssid,
72 			     struct hostapd_config *conf,
73 			     struct hostapd_hw_modes *mode)
74 {
75 #ifdef CONFIG_P2P
76 	u8 center_chan = 0;
77 	u8 channel = conf->channel;
78 #endif /* CONFIG_P2P */
79 	u8 freq_seg_idx;
80 
81 	if (!conf->secondary_channel)
82 		goto no_vht;
83 
84 	/* Use the maximum oper channel width if it's given. */
85 	if (ssid->max_oper_chwidth)
86 		hostapd_set_oper_chwidth(conf, ssid->max_oper_chwidth);
87 
88 	if (hostapd_get_oper_chwidth(conf) == CHANWIDTH_80P80MHZ) {
89 		ieee80211_freq_to_chan(ssid->vht_center_freq2,
90 				       &freq_seg_idx);
91 		hostapd_set_oper_centr_freq_seg1_idx(conf, freq_seg_idx);
92 	}
93 
94 	if (!ssid->p2p_group) {
95 		if (!ssid->vht_center_freq1)
96 			goto no_vht;
97 		ieee80211_freq_to_chan(ssid->vht_center_freq1,
98 				       &freq_seg_idx);
99 		hostapd_set_oper_centr_freq_seg0_idx(conf, freq_seg_idx);
100 
101 		wpa_printf(MSG_DEBUG,
102 			   "VHT seg0 index %d and seg1 index %d for AP",
103 			   hostapd_get_oper_centr_freq_seg0_idx(conf),
104 			   hostapd_get_oper_centr_freq_seg1_idx(conf));
105 		return;
106 	}
107 
108 #ifdef CONFIG_P2P
109 	switch (hostapd_get_oper_chwidth(conf)) {
110 	case CHANWIDTH_80MHZ:
111 	case CHANWIDTH_80P80MHZ:
112 		center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
113 		wpa_printf(MSG_DEBUG,
114 			   "VHT center channel %u for 80 or 80+80 MHz bandwidth",
115 			   center_chan);
116 		break;
117 	case CHANWIDTH_160MHZ:
118 		center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
119 		wpa_printf(MSG_DEBUG,
120 			   "VHT center channel %u for 160 MHz bandwidth",
121 			   center_chan);
122 		break;
123 	default:
124 		/*
125 		 * conf->vht_oper_chwidth might not be set for non-P2P GO cases,
126 		 * try oper_cwidth 160 MHz first then VHT 80 MHz, if 160 MHz is
127 		 * not supported.
128 		 */
129 		hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
130 		center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
131 		if (center_chan && is_chanwidth160_supported(mode, conf)) {
132 			wpa_printf(MSG_DEBUG,
133 				   "VHT center channel %u for auto-selected 160 MHz bandwidth",
134 				   center_chan);
135 		} else {
136 			hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
137 			center_chan = wpas_p2p_get_vht80_center(wpa_s, mode,
138 								channel);
139 			wpa_printf(MSG_DEBUG,
140 				   "VHT center channel %u for auto-selected 80 MHz bandwidth",
141 				   center_chan);
142 		}
143 		break;
144 	}
145 	if (!center_chan)
146 		goto no_vht;
147 
148 	hostapd_set_oper_centr_freq_seg0_idx(conf, center_chan);
149 	wpa_printf(MSG_DEBUG, "VHT seg0 index %d for P2P GO",
150 		   hostapd_get_oper_centr_freq_seg0_idx(conf));
151 	return;
152 #endif /* CONFIG_P2P */
153 
154 no_vht:
155 	wpa_printf(MSG_DEBUG,
156 		   "No VHT higher bandwidth support for the selected channel %d",
157 		   conf->channel);
158 	hostapd_set_oper_centr_freq_seg0_idx(
159 		conf, conf->channel + conf->secondary_channel * 2);
160 	hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
161 }
162 
163 
164 static struct hostapd_hw_modes *
wpa_supplicant_find_hw_mode(struct wpa_supplicant * wpa_s,enum hostapd_hw_mode hw_mode)165 wpa_supplicant_find_hw_mode(struct wpa_supplicant *wpa_s,
166 			    enum hostapd_hw_mode hw_mode)
167 {
168 	struct hostapd_hw_modes *mode = NULL;
169 	int i;
170 
171 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
172 		if (wpa_s->hw.modes[i].mode == hw_mode) {
173 			mode = &wpa_s->hw.modes[i];
174 			break;
175 		}
176 	}
177 
178 	return mode;
179 }
180 
181 
wpa_supplicant_conf_ap_ht(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct hostapd_config * conf)182 int wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
183 			      struct wpa_ssid *ssid,
184 			      struct hostapd_config *conf)
185 {
186 	conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
187 					       &conf->channel);
188 
189 	if (conf->hw_mode == NUM_HOSTAPD_MODES) {
190 		wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
191 			   ssid->frequency);
192 		return -1;
193 	}
194 
195 	/*
196 	 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
197 	 * and a mask of allowed capabilities within conf->ht_capab.
198 	 * Using default config settings for: conf->ht_op_mode_fixed,
199 	 * conf->secondary_channel, conf->require_ht
200 	 */
201 	if (wpa_s->hw.modes) {
202 		struct hostapd_hw_modes *mode = NULL;
203 		int no_ht = 0;
204 
205 		wpa_printf(MSG_DEBUG,
206 			   "Determining HT/VHT options based on driver capabilities (freq=%u chan=%u)",
207 			   ssid->frequency, conf->channel);
208 
209 		mode = wpa_supplicant_find_hw_mode(wpa_s, conf->hw_mode);
210 
211 		/* May drop to IEEE 802.11b if the driver does not support IEEE
212 		 * 802.11g */
213 		if (!mode && conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
214 			conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
215 			wpa_printf(MSG_INFO,
216 				   "Try downgrade to IEEE 802.11b as 802.11g is not supported by the current hardware");
217 			mode = wpa_supplicant_find_hw_mode(wpa_s,
218 							   conf->hw_mode);
219 		}
220 
221 		if (!mode) {
222 			wpa_printf(MSG_ERROR,
223 				   "No match between requested and supported hw modes found");
224 			return -1;
225 		}
226 
227 #ifdef CONFIG_HT_OVERRIDES
228 		if (ssid->disable_ht)
229 			ssid->ht = 0;
230 #endif /* CONFIG_HT_OVERRIDES */
231 
232 		if (!ssid->ht) {
233 			wpa_printf(MSG_DEBUG,
234 				   "HT not enabled in network profile");
235 			conf->ieee80211n = 0;
236 			conf->ht_capab = 0;
237 			no_ht = 1;
238 		}
239 
240 		if (!no_ht && mode && mode->ht_capab) {
241 			wpa_printf(MSG_DEBUG,
242 				   "Enable HT support (p2p_group=%d 11a=%d ht40_hw_capab=%d ssid->ht40=%d)",
243 				   ssid->p2p_group,
244 				   conf->hw_mode == HOSTAPD_MODE_IEEE80211A,
245 				   !!(mode->ht_capab &
246 				      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET),
247 				   ssid->ht40);
248 			conf->ieee80211n = 1;
249 
250 			if (ssid->ht40 &&
251 			    (mode->ht_capab &
252 			     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
253 				conf->secondary_channel = ssid->ht40;
254 			else
255 				conf->secondary_channel = 0;
256 
257 #ifdef CONFIG_P2P
258 			if (ssid->p2p_group &&
259 			    conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
260 			    (mode->ht_capab &
261 			     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
262 			    ssid->ht40) {
263 				conf->secondary_channel =
264 					wpas_p2p_get_ht40_mode(wpa_s, mode,
265 							       conf->channel);
266 				wpa_printf(MSG_DEBUG,
267 					   "HT secondary channel offset %d for P2P group",
268 					   conf->secondary_channel);
269 			} else if (ssid->p2p_group && conf->secondary_channel &&
270 				   conf->hw_mode != HOSTAPD_MODE_IEEE80211A) {
271 				/* This ended up trying to configure invalid
272 				 * 2.4 GHz channels (e.g., HT40+ on channel 11)
273 				 * in some cases, so clear the secondary channel
274 				 * configuration now to avoid such cases that
275 				 * would lead to group formation failures. */
276 				wpa_printf(MSG_DEBUG,
277 					   "Disable HT secondary channel for P2P group on 2.4 GHz");
278 				conf->secondary_channel = 0;
279 			}
280 #endif /* CONFIG_P2P */
281 
282 			if (!ssid->p2p_group &&
283 			    (mode->ht_capab &
284 			     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
285 				conf->secondary_channel = ssid->ht40;
286 				wpa_printf(MSG_DEBUG,
287 					   "HT secondary channel offset %d for AP",
288 					   conf->secondary_channel);
289 			}
290 
291 			if (conf->secondary_channel)
292 				conf->ht_capab |=
293 					HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
294 
295 			/*
296 			 * white-list capabilities that won't cause issues
297 			 * to connecting stations, while leaving the current
298 			 * capabilities intact (currently disabled SMPS).
299 			 */
300 			conf->ht_capab |= mode->ht_capab &
301 				(HT_CAP_INFO_GREEN_FIELD |
302 				 HT_CAP_INFO_SHORT_GI20MHZ |
303 				 HT_CAP_INFO_SHORT_GI40MHZ |
304 				 HT_CAP_INFO_RX_STBC_MASK |
305 				 HT_CAP_INFO_TX_STBC |
306 				 HT_CAP_INFO_MAX_AMSDU_SIZE);
307 
308 			/* check this before VHT, because setting oper chan
309 			 * width and friends is the same call for HE and VHT
310 			 * and checks if conf->ieee8021ax == 1 */
311 			if (mode->he_capab[wpas_mode_to_ieee80211_mode(
312 					    ssid->mode)].he_supported &&
313 			    ssid->he)
314 				conf->ieee80211ax = 1;
315 
316 			if (mode->vht_capab && ssid->vht) {
317 				conf->ieee80211ac = 1;
318 				conf->vht_capab |= mode->vht_capab;
319 				wpas_conf_ap_vht(wpa_s, ssid, conf, mode);
320 			}
321 		}
322 	}
323 
324 	if (conf->secondary_channel) {
325 		struct wpa_supplicant *iface;
326 
327 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
328 		{
329 			if (iface == wpa_s ||
330 			    iface->wpa_state < WPA_AUTHENTICATING ||
331 			    (int) iface->assoc_freq != ssid->frequency)
332 				continue;
333 
334 			/*
335 			 * Do not allow 40 MHz co-ex PRI/SEC switch to force us
336 			 * to change our PRI channel since we have an existing,
337 			 * concurrent connection on that channel and doing
338 			 * multi-channel concurrency is likely to cause more
339 			 * harm than using different PRI/SEC selection in
340 			 * environment with multiple BSSes on these two channels
341 			 * with mixed 20 MHz or PRI channel selection.
342 			 */
343 			conf->no_pri_sec_switch = 1;
344 		}
345 	}
346 
347 	return 0;
348 }
349 
350 
wpa_supplicant_conf_ap(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct hostapd_config * conf)351 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
352 				  struct wpa_ssid *ssid,
353 				  struct hostapd_config *conf)
354 {
355 	struct hostapd_bss_config *bss = conf->bss[0];
356 
357 	conf->driver = wpa_s->driver;
358 
359 	os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
360 
361 	if (wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf))
362 		return -1;
363 
364 	if (ssid->pbss > 1) {
365 		wpa_printf(MSG_ERROR, "Invalid pbss value(%d) for AP mode",
366 			   ssid->pbss);
367 		return -1;
368 	}
369 	bss->pbss = ssid->pbss;
370 
371 #ifdef CONFIG_ACS
372 	if (ssid->acs) {
373 		/* Setting channel to 0 in order to enable ACS */
374 		conf->channel = 0;
375 		wpa_printf(MSG_DEBUG, "Use automatic channel selection");
376 	}
377 #endif /* CONFIG_ACS */
378 
379 	if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
380 			     wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
381 		conf->ieee80211h = 1;
382 		conf->ieee80211d = 1;
383 		conf->country[0] = wpa_s->conf->country[0];
384 		conf->country[1] = wpa_s->conf->country[1];
385 		conf->country[2] = ' ';
386 	}
387 
388 #ifdef CONFIG_P2P
389 	if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
390 	    (ssid->mode == WPAS_MODE_P2P_GO ||
391 	     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
392 		/* Remove 802.11b rates from supported and basic rate sets */
393 		int *list = os_malloc(4 * sizeof(int));
394 		if (list) {
395 			list[0] = 60;
396 			list[1] = 120;
397 			list[2] = 240;
398 			list[3] = -1;
399 		}
400 		conf->basic_rates = list;
401 
402 		list = os_malloc(9 * sizeof(int));
403 		if (list) {
404 			list[0] = 60;
405 			list[1] = 90;
406 			list[2] = 120;
407 			list[3] = 180;
408 			list[4] = 240;
409 			list[5] = 360;
410 			list[6] = 480;
411 			list[7] = 540;
412 			list[8] = -1;
413 		}
414 		conf->supported_rates = list;
415 	}
416 
417 #ifdef CONFIG_IEEE80211AX
418 	if (ssid->mode == WPAS_MODE_P2P_GO ||
419 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
420 		conf->ieee80211ax = ssid->he;
421 #endif /* CONFIG_IEEE80211AX */
422 
423 	bss->isolate = !wpa_s->conf->p2p_intra_bss;
424 	bss->extended_key_id = wpa_s->conf->extended_key_id;
425 	bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
426 	bss->wpa_deny_ptk0_rekey = ssid->wpa_deny_ptk0_rekey;
427 
428 	if (ssid->p2p_group) {
429 		os_memcpy(bss->ip_addr_go, wpa_s->p2pdev->conf->ip_addr_go, 4);
430 		os_memcpy(bss->ip_addr_mask, wpa_s->p2pdev->conf->ip_addr_mask,
431 			  4);
432 		os_memcpy(bss->ip_addr_start,
433 			  wpa_s->p2pdev->conf->ip_addr_start, 4);
434 		os_memcpy(bss->ip_addr_end, wpa_s->p2pdev->conf->ip_addr_end,
435 			  4);
436 	}
437 #endif /* CONFIG_P2P */
438 
439 	if (ssid->ssid_len == 0) {
440 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
441 		return -1;
442 	}
443 	os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
444 	bss->ssid.ssid_len = ssid->ssid_len;
445 	bss->ssid.ssid_set = 1;
446 
447 	bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
448 
449 	if (ssid->auth_alg)
450 		bss->auth_algs = ssid->auth_alg;
451 
452 	if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
453 		bss->wpa = ssid->proto;
454 	if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
455 		bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
456 	else
457 		bss->wpa_key_mgmt = ssid->key_mgmt;
458 	bss->wpa_pairwise = ssid->pairwise_cipher;
459 	if (wpa_key_mgmt_sae(bss->wpa_key_mgmt) && ssid->passphrase) {
460 		bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
461 	} else if (ssid->psk_set) {
462 		bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
463 		bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
464 		if (bss->ssid.wpa_psk == NULL)
465 			return -1;
466 		os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
467 		bss->ssid.wpa_psk->group = 1;
468 		bss->ssid.wpa_psk_set = 1;
469 	} else if (ssid->passphrase) {
470 		bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
471 #ifdef CONFIG_WEP
472 	} else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
473 		   ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
474 		struct hostapd_wep_keys *wep = &bss->ssid.wep;
475 		int i;
476 		for (i = 0; i < NUM_WEP_KEYS; i++) {
477 			if (ssid->wep_key_len[i] == 0)
478 				continue;
479 			wep->key[i] = os_memdup(ssid->wep_key[i],
480 						ssid->wep_key_len[i]);
481 			if (wep->key[i] == NULL)
482 				return -1;
483 			wep->len[i] = ssid->wep_key_len[i];
484 		}
485 		wep->idx = ssid->wep_tx_keyidx;
486 		wep->keys_set = 1;
487 #endif /* CONFIG_WEP */
488 	}
489 #ifdef CONFIG_SAE
490 	if (ssid->sae_password) {
491 		struct sae_password_entry *pw;
492 
493 		pw = os_zalloc(sizeof(*pw));
494 		if (!pw)
495 			return -1;
496 		os_memset(pw->peer_addr, 0xff, ETH_ALEN);
497 		pw->password = os_strdup(ssid->sae_password);
498 		if (!pw->password) {
499 			os_free(pw);
500 			return -1;
501 		}
502 		if (ssid->sae_password_id) {
503 			pw->identifier = os_strdup(ssid->sae_password_id);
504 			if (!pw->identifier) {
505 				str_clear_free(pw->password);
506 				os_free(pw);
507 				return -1;
508 			}
509 		}
510 
511 		pw->next = bss->sae_passwords;
512 		bss->sae_passwords = pw;
513 	}
514 
515 	bss->sae_pwe = wpa_s->conf->sae_pwe;
516 #endif /* CONFIG_SAE */
517 
518 	if (wpa_s->conf->go_interworking) {
519 		wpa_printf(MSG_DEBUG,
520 			   "P2P: Enable Interworking with access_network_type: %d",
521 			   wpa_s->conf->go_access_network_type);
522 		bss->interworking = wpa_s->conf->go_interworking;
523 		bss->access_network_type = wpa_s->conf->go_access_network_type;
524 		bss->internet = wpa_s->conf->go_internet;
525 		if (wpa_s->conf->go_venue_group) {
526 			wpa_printf(MSG_DEBUG,
527 				   "P2P: Venue group: %d  Venue type: %d",
528 				   wpa_s->conf->go_venue_group,
529 				   wpa_s->conf->go_venue_type);
530 			bss->venue_group = wpa_s->conf->go_venue_group;
531 			bss->venue_type = wpa_s->conf->go_venue_type;
532 			bss->venue_info_set = 1;
533 		}
534 	}
535 
536 	if (ssid->ap_max_inactivity)
537 		bss->ap_max_inactivity = ssid->ap_max_inactivity;
538 
539 	if (ssid->dtim_period)
540 		bss->dtim_period = ssid->dtim_period;
541 	else if (wpa_s->conf->dtim_period)
542 		bss->dtim_period = wpa_s->conf->dtim_period;
543 
544 	if (ssid->beacon_int)
545 		conf->beacon_int = ssid->beacon_int;
546 	else if (wpa_s->conf->beacon_int)
547 		conf->beacon_int = wpa_s->conf->beacon_int;
548 
549 #ifdef CONFIG_P2P
550 	if (ssid->mode == WPAS_MODE_P2P_GO ||
551 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
552 		if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
553 			wpa_printf(MSG_INFO,
554 				   "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
555 				   wpa_s->conf->p2p_go_ctwindow,
556 				   conf->beacon_int);
557 			conf->p2p_go_ctwindow = 0;
558 		} else {
559 			conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
560 		}
561 	}
562 #endif /* CONFIG_P2P */
563 
564 	if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
565 		bss->rsn_pairwise = bss->wpa_pairwise;
566 	bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
567 						    bss->rsn_pairwise);
568 
569 	if (bss->wpa && bss->ieee802_1x) {
570 		bss->ssid.security_policy = SECURITY_WPA;
571 	} else if (bss->wpa) {
572 		bss->ssid.security_policy = SECURITY_WPA_PSK;
573 #ifdef CONFIG_WEP
574 	} else if (bss->ieee802_1x) {
575 		int cipher = WPA_CIPHER_NONE;
576 		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
577 		bss->ssid.wep.default_len = bss->default_wep_key_len;
578 		if (bss->default_wep_key_len)
579 			cipher = bss->default_wep_key_len >= 13 ?
580 				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
581 		bss->wpa_group = cipher;
582 		bss->wpa_pairwise = cipher;
583 		bss->rsn_pairwise = cipher;
584 	} else if (bss->ssid.wep.keys_set) {
585 		int cipher = WPA_CIPHER_WEP40;
586 		if (bss->ssid.wep.len[0] >= 13)
587 			cipher = WPA_CIPHER_WEP104;
588 		bss->ssid.security_policy = SECURITY_STATIC_WEP;
589 		bss->wpa_group = cipher;
590 		bss->wpa_pairwise = cipher;
591 		bss->rsn_pairwise = cipher;
592 #endif /* CONFIG_WEP */
593 	} else {
594 		bss->ssid.security_policy = SECURITY_PLAINTEXT;
595 		bss->wpa_group = WPA_CIPHER_NONE;
596 		bss->wpa_pairwise = WPA_CIPHER_NONE;
597 		bss->rsn_pairwise = WPA_CIPHER_NONE;
598 	}
599 
600 	if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
601 	    (bss->wpa_group == WPA_CIPHER_CCMP ||
602 	     bss->wpa_group == WPA_CIPHER_GCMP ||
603 	     bss->wpa_group == WPA_CIPHER_CCMP_256 ||
604 	     bss->wpa_group == WPA_CIPHER_GCMP_256)) {
605 		/*
606 		 * Strong ciphers do not need frequent rekeying, so increase
607 		 * the default GTK rekeying period to 24 hours.
608 		 */
609 		bss->wpa_group_rekey = 86400;
610 	}
611 
612 	if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
613 		bss->ieee80211w = ssid->ieee80211w;
614 
615 #ifdef CONFIG_OCV
616 	bss->ocv = ssid->ocv;
617 #endif /* CONFIG_OCV */
618 
619 #ifdef CONFIG_WPS
620 	/*
621 	 * Enable WPS by default for open and WPA/WPA2-Personal network, but
622 	 * require user interaction to actually use it. Only the internal
623 	 * Registrar is supported.
624 	 */
625 	if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
626 	    bss->ssid.security_policy != SECURITY_PLAINTEXT)
627 		goto no_wps;
628 	if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
629 	    (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
630 	     !(bss->wpa & 2)))
631 		goto no_wps; /* WPS2 does not allow WPA/TKIP-only
632 			      * configuration */
633 	if (ssid->wps_disabled)
634 		goto no_wps;
635 	bss->eap_server = 1;
636 
637 	if (!ssid->ignore_broadcast_ssid)
638 		bss->wps_state = 2;
639 
640 	bss->ap_setup_locked = 2;
641 	if (wpa_s->conf->config_methods)
642 		bss->config_methods = os_strdup(wpa_s->conf->config_methods);
643 	os_memcpy(bss->device_type, wpa_s->conf->device_type,
644 		  WPS_DEV_TYPE_LEN);
645 	if (wpa_s->conf->device_name) {
646 		bss->device_name = os_strdup(wpa_s->conf->device_name);
647 		bss->friendly_name = os_strdup(wpa_s->conf->device_name);
648 	}
649 	if (wpa_s->conf->manufacturer)
650 		bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
651 	if (wpa_s->conf->model_name)
652 		bss->model_name = os_strdup(wpa_s->conf->model_name);
653 	if (wpa_s->conf->model_number)
654 		bss->model_number = os_strdup(wpa_s->conf->model_number);
655 	if (wpa_s->conf->serial_number)
656 		bss->serial_number = os_strdup(wpa_s->conf->serial_number);
657 	if (is_nil_uuid(wpa_s->conf->uuid))
658 		os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
659 	else
660 		os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
661 	os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
662 	bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
663 	if (ssid->eap.fragment_size != DEFAULT_FRAGMENT_SIZE)
664 		bss->fragment_size = ssid->eap.fragment_size;
665 no_wps:
666 #endif /* CONFIG_WPS */
667 
668 	if (wpa_s->max_stations &&
669 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
670 		bss->max_num_sta = wpa_s->max_stations;
671 	else
672 		bss->max_num_sta = wpa_s->conf->max_num_sta;
673 
674 	if (!bss->isolate)
675 		bss->isolate = wpa_s->conf->ap_isolate;
676 
677 	bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
678 
679 	if (wpa_s->conf->ap_vendor_elements) {
680 		bss->vendor_elements =
681 			wpabuf_dup(wpa_s->conf->ap_vendor_elements);
682 	}
683 
684 	bss->ftm_responder = wpa_s->conf->ftm_responder;
685 	bss->ftm_initiator = wpa_s->conf->ftm_initiator;
686 
687 	bss->transition_disable = ssid->transition_disable;
688 
689 	return 0;
690 }
691 
692 
ap_public_action_rx(void * ctx,const u8 * buf,size_t len,int freq)693 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
694 {
695 #ifdef CONFIG_P2P
696 	struct wpa_supplicant *wpa_s = ctx;
697 	const struct ieee80211_mgmt *mgmt;
698 
699 	mgmt = (const struct ieee80211_mgmt *) buf;
700 	if (len < IEEE80211_HDRLEN + 1)
701 		return;
702 	if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
703 		return;
704 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
705 			   mgmt->u.action.category,
706 			   buf + IEEE80211_HDRLEN + 1,
707 			   len - IEEE80211_HDRLEN - 1, freq);
708 #endif /* CONFIG_P2P */
709 }
710 
711 
ap_wps_event_cb(void * ctx,enum wps_event event,union wps_event_data * data)712 static void ap_wps_event_cb(void *ctx, enum wps_event event,
713 			    union wps_event_data *data)
714 {
715 #ifdef CONFIG_P2P
716 	struct wpa_supplicant *wpa_s = ctx;
717 
718 	if (event == WPS_EV_FAIL) {
719 		struct wps_event_fail *fail = &data->fail;
720 
721 		if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s &&
722 		    wpa_s == wpa_s->global->p2p_group_formation) {
723 			/*
724 			 * src/ap/wps_hostapd.c has already sent this on the
725 			 * main interface, so only send on the parent interface
726 			 * here if needed.
727 			 */
728 			wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
729 				"msg=%d config_error=%d",
730 				fail->msg, fail->config_error);
731 		}
732 		wpas_p2p_wps_failed(wpa_s, fail);
733 	}
734 #endif /* CONFIG_P2P */
735 }
736 
737 
ap_sta_authorized_cb(void * ctx,const u8 * mac_addr,int authorized,const u8 * p2p_dev_addr)738 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
739 				 int authorized, const u8 *p2p_dev_addr)
740 {
741 	wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
742 }
743 
744 
745 #ifdef CONFIG_P2P
ap_new_psk_cb(void * ctx,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)746 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
747 			  const u8 *psk, size_t psk_len)
748 {
749 
750 	struct wpa_supplicant *wpa_s = ctx;
751 	if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
752 		return;
753 	wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
754 }
755 #endif /* CONFIG_P2P */
756 
757 
ap_vendor_action_rx(void * ctx,const u8 * buf,size_t len,int freq)758 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
759 {
760 #ifdef CONFIG_P2P
761 	struct wpa_supplicant *wpa_s = ctx;
762 	const struct ieee80211_mgmt *mgmt;
763 
764 	mgmt = (const struct ieee80211_mgmt *) buf;
765 	if (len < IEEE80211_HDRLEN + 1)
766 		return -1;
767 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
768 			   mgmt->u.action.category,
769 			   buf + IEEE80211_HDRLEN + 1,
770 			   len - IEEE80211_HDRLEN - 1, freq);
771 #endif /* CONFIG_P2P */
772 	return 0;
773 }
774 
775 
ap_probe_req_rx(void * ctx,const u8 * sa,const u8 * da,const u8 * bssid,const u8 * ie,size_t ie_len,int ssi_signal)776 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
777 			   const u8 *bssid, const u8 *ie, size_t ie_len,
778 			   int ssi_signal)
779 {
780 	struct wpa_supplicant *wpa_s = ctx;
781 	unsigned int freq = 0;
782 
783 	if (wpa_s->ap_iface)
784 		freq = wpa_s->ap_iface->freq;
785 
786 	return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
787 				     freq, ssi_signal);
788 }
789 
790 
ap_wps_reg_success_cb(void * ctx,const u8 * mac_addr,const u8 * uuid_e)791 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
792 				  const u8 *uuid_e)
793 {
794 	struct wpa_supplicant *wpa_s = ctx;
795 	wpas_p2p_wps_success(wpa_s, mac_addr, 1);
796 }
797 
798 
wpas_ap_configured_cb(void * ctx)799 static void wpas_ap_configured_cb(void *ctx)
800 {
801 	struct wpa_supplicant *wpa_s = ctx;
802 
803 	wpa_printf(MSG_DEBUG, "AP interface setup completed - state %s",
804 		   hostapd_state_text(wpa_s->ap_iface->state));
805 	if (wpa_s->ap_iface->state == HAPD_IFACE_DISABLED) {
806 		wpa_supplicant_ap_deinit(wpa_s);
807 		return;
808 	}
809 
810 	if (wpa_s->current_ssid) {
811 		int acs = 0;
812 #ifdef CONFIG_ACS
813 		acs = wpa_s->current_ssid->acs;
814 #endif
815 		if (acs || (wpa_s->assoc_freq && wpa_s->ap_iface->freq &&
816 			    wpa_s->assoc_freq != wpa_s->ap_iface->freq)) {
817 			wpa_s->assoc_freq = wpa_s->ap_iface->freq;
818 			wpa_s->current_ssid->frequency = wpa_s->ap_iface->freq;
819 		}
820 	}
821 
822 	wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
823 
824 	if (wpa_s->ap_configured_cb)
825 		wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
826 					wpa_s->ap_configured_cb_data);
827 }
828 
829 
wpa_supplicant_create_ap(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)830 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
831 			     struct wpa_ssid *ssid)
832 {
833 	struct wpa_driver_associate_params params;
834 	struct hostapd_iface *hapd_iface;
835 	struct hostapd_config *conf;
836 	size_t i;
837 
838 	if (ssid->ssid == NULL || ssid->ssid_len == 0) {
839 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
840 		return -1;
841 	}
842 
843 	wpa_supplicant_ap_deinit(wpa_s);
844 
845 	wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
846 		   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
847 
848 	os_memset(&params, 0, sizeof(params));
849 	params.ssid = ssid->ssid;
850 	params.ssid_len = ssid->ssid_len;
851 	switch (ssid->mode) {
852 	case WPAS_MODE_AP:
853 	case WPAS_MODE_P2P_GO:
854 	case WPAS_MODE_P2P_GROUP_FORMATION:
855 		params.mode = IEEE80211_MODE_AP;
856 		break;
857 	default:
858 		return -1;
859 	}
860 	if (ssid->frequency == 0)
861 		ssid->frequency = 2462; /* default channel 11 */
862 	params.freq.freq = ssid->frequency;
863 
864 	if ((ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO) &&
865 	    ssid->enable_edmg) {
866 		u8 primary_channel;
867 
868 		if (ieee80211_freq_to_chan(ssid->frequency, &primary_channel) ==
869 		    NUM_HOSTAPD_MODES) {
870 			wpa_printf(MSG_WARNING,
871 				   "EDMG: Failed to get the primary channel");
872 			return -1;
873 		}
874 
875 		hostapd_encode_edmg_chan(ssid->enable_edmg, ssid->edmg_channel,
876 					 primary_channel, &params.freq.edmg);
877 	}
878 
879 	params.wpa_proto = ssid->proto;
880 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
881 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
882 	else
883 		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
884 	params.key_mgmt_suite = wpa_s->key_mgmt;
885 
886 	wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
887 							  1);
888 	if (wpa_s->pairwise_cipher < 0) {
889 		wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
890 			   "cipher.");
891 		return -1;
892 	}
893 	params.pairwise_suite = wpa_s->pairwise_cipher;
894 	params.group_suite = params.pairwise_suite;
895 
896 #ifdef CONFIG_P2P
897 	if (ssid->mode == WPAS_MODE_P2P_GO ||
898 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
899 		params.p2p = 1;
900 #endif /* CONFIG_P2P */
901 
902 	if (wpa_s->p2pdev->set_ap_uapsd)
903 		params.uapsd = wpa_s->p2pdev->ap_uapsd;
904 	else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
905 		params.uapsd = 1; /* mandatory for P2P GO */
906 	else
907 		params.uapsd = -1;
908 
909 	if (ieee80211_is_dfs(params.freq.freq, wpa_s->hw.modes,
910 			     wpa_s->hw.num_modes))
911 		params.freq.freq = 0; /* set channel after CAC */
912 
913 	if (params.p2p)
914 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_GO);
915 	else
916 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_AP_BSS);
917 
918 	if (wpa_drv_associate(wpa_s, &params) < 0) {
919 		wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
920 		return -1;
921 	}
922 
923 	wpa_s->ap_iface = hapd_iface = hostapd_alloc_iface();
924 	if (hapd_iface == NULL)
925 		return -1;
926 	hapd_iface->owner = wpa_s;
927 	hapd_iface->drv_flags = wpa_s->drv_flags;
928 	hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
929 	hapd_iface->extended_capa = wpa_s->extended_capa;
930 	hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
931 	hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
932 
933 	wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
934 	if (conf == NULL) {
935 		wpa_supplicant_ap_deinit(wpa_s);
936 		return -1;
937 	}
938 
939 	os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
940 		  wpa_s->conf->wmm_ac_params,
941 		  sizeof(wpa_s->conf->wmm_ac_params));
942 
943 	os_memcpy(wpa_s->ap_iface->conf->tx_queue, wpa_s->conf->tx_queue,
944 		  sizeof(wpa_s->conf->tx_queue));
945 
946 	if (params.uapsd > 0) {
947 		conf->bss[0]->wmm_enabled = 1;
948 		conf->bss[0]->wmm_uapsd = 1;
949 	}
950 
951 	if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
952 		wpa_printf(MSG_ERROR, "Failed to create AP configuration");
953 		wpa_supplicant_ap_deinit(wpa_s);
954 		return -1;
955 	}
956 
957 #ifdef CONFIG_P2P
958 	if (ssid->mode == WPAS_MODE_P2P_GO)
959 		conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
960 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
961 		conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
962 			P2P_GROUP_FORMATION;
963 #endif /* CONFIG_P2P */
964 
965 	hapd_iface->num_bss = conf->num_bss;
966 	hapd_iface->bss = os_calloc(conf->num_bss,
967 				    sizeof(struct hostapd_data *));
968 	if (hapd_iface->bss == NULL) {
969 		wpa_supplicant_ap_deinit(wpa_s);
970 		return -1;
971 	}
972 
973 	for (i = 0; i < conf->num_bss; i++) {
974 		hapd_iface->bss[i] =
975 			hostapd_alloc_bss_data(hapd_iface, conf,
976 					       conf->bss[i]);
977 		if (hapd_iface->bss[i] == NULL) {
978 			wpa_supplicant_ap_deinit(wpa_s);
979 			return -1;
980 		}
981 
982 		hapd_iface->bss[i]->msg_ctx = wpa_s;
983 		hapd_iface->bss[i]->msg_ctx_parent = wpa_s->p2pdev;
984 		hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
985 		hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
986 		hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
987 		hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
988 		hostapd_register_probereq_cb(hapd_iface->bss[i],
989 					     ap_probe_req_rx, wpa_s);
990 		hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
991 		hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
992 		hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
993 		hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
994 		hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
995 		hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
996 #ifdef CONFIG_P2P
997 		hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
998 		hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
999 		hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
1000 		hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
1001 								    ssid);
1002 #endif /* CONFIG_P2P */
1003 		hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
1004 		hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
1005 #ifdef CONFIG_TESTING_OPTIONS
1006 		hapd_iface->bss[i]->ext_eapol_frame_io =
1007 			wpa_s->ext_eapol_frame_io;
1008 #endif /* CONFIG_TESTING_OPTIONS */
1009 	}
1010 
1011 	os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
1012 	hapd_iface->bss[0]->driver = wpa_s->driver;
1013 	hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
1014 
1015 	wpa_s->current_ssid = ssid;
1016 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1017 	os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
1018 	wpa_s->assoc_freq = ssid->frequency;
1019 	wpa_s->ap_iface->conf->enable_edmg = ssid->enable_edmg;
1020 	wpa_s->ap_iface->conf->edmg_channel = ssid->edmg_channel;
1021 
1022 #if defined(CONFIG_P2P) && defined(CONFIG_ACS)
1023 	if (wpa_s->p2p_go_do_acs) {
1024 		wpa_s->ap_iface->conf->channel = 0;
1025 		wpa_s->ap_iface->conf->hw_mode = wpa_s->p2p_go_acs_band;
1026 		ssid->acs = 1;
1027 	}
1028 #endif /* CONFIG_P2P && CONFIG_ACS */
1029 
1030 	if (hostapd_setup_interface(wpa_s->ap_iface)) {
1031 		wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
1032 		wpa_supplicant_ap_deinit(wpa_s);
1033 		return -1;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 
wpa_supplicant_ap_deinit(struct wpa_supplicant * wpa_s)1040 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
1041 {
1042 #ifdef CONFIG_WPS
1043 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
1044 #endif /* CONFIG_WPS */
1045 
1046 	if (wpa_s->ap_iface == NULL)
1047 		return;
1048 
1049 	wpa_s->current_ssid = NULL;
1050 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1051 	wpa_s->assoc_freq = 0;
1052 	wpas_p2p_ap_deinit(wpa_s);
1053 	wpa_s->ap_iface->driver_ap_teardown =
1054 		!!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1055 
1056 	hostapd_interface_deinit(wpa_s->ap_iface);
1057 	hostapd_interface_free(wpa_s->ap_iface);
1058 	wpa_s->ap_iface = NULL;
1059 	wpa_drv_deinit_ap(wpa_s);
1060 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1061 		" reason=%d locally_generated=1",
1062 		MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
1063 }
1064 
1065 
ap_tx_status(void * ctx,const u8 * addr,const u8 * buf,size_t len,int ack)1066 void ap_tx_status(void *ctx, const u8 *addr,
1067 		  const u8 *buf, size_t len, int ack)
1068 {
1069 #ifdef NEED_AP_MLME
1070 	struct wpa_supplicant *wpa_s = ctx;
1071 	hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
1072 #endif /* NEED_AP_MLME */
1073 }
1074 
1075 
ap_eapol_tx_status(void * ctx,const u8 * dst,const u8 * data,size_t len,int ack)1076 void ap_eapol_tx_status(void *ctx, const u8 *dst,
1077 			const u8 *data, size_t len, int ack)
1078 {
1079 #ifdef NEED_AP_MLME
1080 	struct wpa_supplicant *wpa_s = ctx;
1081 	if (!wpa_s->ap_iface)
1082 		return;
1083 	hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
1084 #endif /* NEED_AP_MLME */
1085 }
1086 
1087 
ap_client_poll_ok(void * ctx,const u8 * addr)1088 void ap_client_poll_ok(void *ctx, const u8 *addr)
1089 {
1090 #ifdef NEED_AP_MLME
1091 	struct wpa_supplicant *wpa_s = ctx;
1092 	if (wpa_s->ap_iface)
1093 		hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
1094 #endif /* NEED_AP_MLME */
1095 }
1096 
1097 
ap_rx_from_unknown_sta(void * ctx,const u8 * addr,int wds)1098 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
1099 {
1100 #ifdef NEED_AP_MLME
1101 	struct wpa_supplicant *wpa_s = ctx;
1102 	ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
1103 #endif /* NEED_AP_MLME */
1104 }
1105 
1106 
ap_mgmt_rx(void * ctx,struct rx_mgmt * rx_mgmt)1107 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
1108 {
1109 #ifdef NEED_AP_MLME
1110 	struct wpa_supplicant *wpa_s = ctx;
1111 	struct hostapd_frame_info fi;
1112 	os_memset(&fi, 0, sizeof(fi));
1113 	fi.datarate = rx_mgmt->datarate;
1114 	fi.ssi_signal = rx_mgmt->ssi_signal;
1115 	ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
1116 			rx_mgmt->frame_len, &fi);
1117 #endif /* NEED_AP_MLME */
1118 }
1119 
1120 
ap_mgmt_tx_cb(void * ctx,const u8 * buf,size_t len,u16 stype,int ok)1121 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
1122 {
1123 #ifdef NEED_AP_MLME
1124 	struct wpa_supplicant *wpa_s = ctx;
1125 	ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
1126 #endif /* NEED_AP_MLME */
1127 }
1128 
1129 
wpa_supplicant_ap_rx_eapol(struct wpa_supplicant * wpa_s,const u8 * src_addr,const u8 * buf,size_t len)1130 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
1131 				const u8 *src_addr, const u8 *buf, size_t len)
1132 {
1133 	ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
1134 }
1135 
1136 
1137 #ifdef CONFIG_WPS
1138 
wpa_supplicant_ap_wps_pbc(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * p2p_dev_addr)1139 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1140 			      const u8 *p2p_dev_addr)
1141 {
1142 	if (!wpa_s->ap_iface)
1143 		return -1;
1144 	return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
1145 					 p2p_dev_addr);
1146 }
1147 
1148 
wpa_supplicant_ap_wps_cancel(struct wpa_supplicant * wpa_s)1149 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
1150 {
1151 	struct wps_registrar *reg;
1152 	int reg_sel = 0, wps_sta = 0;
1153 
1154 	if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
1155 		return -1;
1156 
1157 	reg = wpa_s->ap_iface->bss[0]->wps->registrar;
1158 	reg_sel = wps_registrar_wps_cancel(reg);
1159 	wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
1160 				  ap_sta_wps_cancel, NULL);
1161 
1162 	if (!reg_sel && !wps_sta) {
1163 		wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
1164 			   "time");
1165 		return -1;
1166 	}
1167 
1168 	/*
1169 	 * There are 2 cases to return wps cancel as success:
1170 	 * 1. When wps cancel was initiated but no connection has been
1171 	 *    established with client yet.
1172 	 * 2. Client is in the middle of exchanging WPS messages.
1173 	 */
1174 
1175 	return 0;
1176 }
1177 
1178 
wpa_supplicant_ap_wps_pin(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,char * buf,size_t buflen,int timeout)1179 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1180 			      const char *pin, char *buf, size_t buflen,
1181 			      int timeout)
1182 {
1183 	int ret, ret_len = 0;
1184 
1185 	if (!wpa_s->ap_iface)
1186 		return -1;
1187 
1188 	if (pin == NULL) {
1189 		unsigned int rpin;
1190 
1191 		if (wps_generate_pin(&rpin) < 0)
1192 			return -1;
1193 		ret_len = os_snprintf(buf, buflen, "%08d", rpin);
1194 		if (os_snprintf_error(buflen, ret_len))
1195 			return -1;
1196 		pin = buf;
1197 	} else if (buf) {
1198 		ret_len = os_snprintf(buf, buflen, "%s", pin);
1199 		if (os_snprintf_error(buflen, ret_len))
1200 			return -1;
1201 	}
1202 
1203 	ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
1204 				  timeout);
1205 	if (ret)
1206 		return -1;
1207 	return ret_len;
1208 }
1209 
1210 
wpas_wps_ap_pin_timeout(void * eloop_data,void * user_ctx)1211 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1212 {
1213 	struct wpa_supplicant *wpa_s = eloop_data;
1214 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1215 	wpas_wps_ap_pin_disable(wpa_s);
1216 }
1217 
1218 
wpas_wps_ap_pin_enable(struct wpa_supplicant * wpa_s,int timeout)1219 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
1220 {
1221 	struct hostapd_data *hapd;
1222 
1223 	if (wpa_s->ap_iface == NULL)
1224 		return;
1225 	hapd = wpa_s->ap_iface->bss[0];
1226 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1227 	hapd->ap_pin_failures = 0;
1228 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
1229 	if (timeout > 0)
1230 		eloop_register_timeout(timeout, 0,
1231 				       wpas_wps_ap_pin_timeout, wpa_s, NULL);
1232 }
1233 
1234 
wpas_wps_ap_pin_disable(struct wpa_supplicant * wpa_s)1235 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
1236 {
1237 	struct hostapd_data *hapd;
1238 
1239 	if (wpa_s->ap_iface == NULL)
1240 		return;
1241 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1242 	hapd = wpa_s->ap_iface->bss[0];
1243 	os_free(hapd->conf->ap_pin);
1244 	hapd->conf->ap_pin = NULL;
1245 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
1246 }
1247 
1248 
wpas_wps_ap_pin_random(struct wpa_supplicant * wpa_s,int timeout)1249 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
1250 {
1251 	struct hostapd_data *hapd;
1252 	unsigned int pin;
1253 	char pin_txt[9];
1254 
1255 	if (wpa_s->ap_iface == NULL)
1256 		return NULL;
1257 	hapd = wpa_s->ap_iface->bss[0];
1258 	if (wps_generate_pin(&pin) < 0)
1259 		return NULL;
1260 	os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
1261 	os_free(hapd->conf->ap_pin);
1262 	hapd->conf->ap_pin = os_strdup(pin_txt);
1263 	if (hapd->conf->ap_pin == NULL)
1264 		return NULL;
1265 	wpas_wps_ap_pin_enable(wpa_s, timeout);
1266 
1267 	return hapd->conf->ap_pin;
1268 }
1269 
1270 
wpas_wps_ap_pin_get(struct wpa_supplicant * wpa_s)1271 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
1272 {
1273 	struct hostapd_data *hapd;
1274 	if (wpa_s->ap_iface == NULL)
1275 		return NULL;
1276 	hapd = wpa_s->ap_iface->bss[0];
1277 	return hapd->conf->ap_pin;
1278 }
1279 
1280 
wpas_wps_ap_pin_set(struct wpa_supplicant * wpa_s,const char * pin,int timeout)1281 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
1282 			int timeout)
1283 {
1284 	struct hostapd_data *hapd;
1285 	char pin_txt[9];
1286 	int ret;
1287 
1288 	if (wpa_s->ap_iface == NULL)
1289 		return -1;
1290 	hapd = wpa_s->ap_iface->bss[0];
1291 	ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
1292 	if (os_snprintf_error(sizeof(pin_txt), ret))
1293 		return -1;
1294 	os_free(hapd->conf->ap_pin);
1295 	hapd->conf->ap_pin = os_strdup(pin_txt);
1296 	if (hapd->conf->ap_pin == NULL)
1297 		return -1;
1298 	wpas_wps_ap_pin_enable(wpa_s, timeout);
1299 
1300 	return 0;
1301 }
1302 
1303 
wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant * wpa_s)1304 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
1305 {
1306 	struct hostapd_data *hapd;
1307 
1308 	if (wpa_s->ap_iface == NULL)
1309 		return;
1310 	hapd = wpa_s->ap_iface->bss[0];
1311 
1312 	/*
1313 	 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
1314 	 * PIN if this happens multiple times to slow down brute force attacks.
1315 	 */
1316 	hapd->ap_pin_failures++;
1317 	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
1318 		   hapd->ap_pin_failures);
1319 	if (hapd->ap_pin_failures < 3)
1320 		return;
1321 
1322 	wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
1323 	hapd->ap_pin_failures = 0;
1324 	os_free(hapd->conf->ap_pin);
1325 	hapd->conf->ap_pin = NULL;
1326 }
1327 
1328 
1329 #ifdef CONFIG_WPS_NFC
1330 
wpas_ap_wps_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef)1331 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
1332 					     int ndef)
1333 {
1334 	struct hostapd_data *hapd;
1335 
1336 	if (wpa_s->ap_iface == NULL)
1337 		return NULL;
1338 	hapd = wpa_s->ap_iface->bss[0];
1339 	return hostapd_wps_nfc_config_token(hapd, ndef);
1340 }
1341 
1342 
wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef)1343 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
1344 					     int ndef)
1345 {
1346 	struct hostapd_data *hapd;
1347 
1348 	if (wpa_s->ap_iface == NULL)
1349 		return NULL;
1350 	hapd = wpa_s->ap_iface->bss[0];
1351 	return hostapd_wps_nfc_hs_cr(hapd, ndef);
1352 }
1353 
1354 
wpas_ap_wps_nfc_report_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * req,const struct wpabuf * sel)1355 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
1356 				    const struct wpabuf *req,
1357 				    const struct wpabuf *sel)
1358 {
1359 	struct hostapd_data *hapd;
1360 
1361 	if (wpa_s->ap_iface == NULL)
1362 		return -1;
1363 	hapd = wpa_s->ap_iface->bss[0];
1364 	return hostapd_wps_nfc_report_handover(hapd, req, sel);
1365 }
1366 
1367 #endif /* CONFIG_WPS_NFC */
1368 
1369 #endif /* CONFIG_WPS */
1370 
1371 
1372 #ifdef CONFIG_CTRL_IFACE
1373 
ap_ctrl_iface_sta_first(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1374 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
1375 			    char *buf, size_t buflen)
1376 {
1377 	struct hostapd_data *hapd;
1378 
1379 	if (wpa_s->ap_iface)
1380 		hapd = wpa_s->ap_iface->bss[0];
1381 	else if (wpa_s->ifmsh)
1382 		hapd = wpa_s->ifmsh->bss[0];
1383 	else
1384 		return -1;
1385 	return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
1386 }
1387 
1388 
ap_ctrl_iface_sta(struct wpa_supplicant * wpa_s,const char * txtaddr,char * buf,size_t buflen)1389 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
1390 		      char *buf, size_t buflen)
1391 {
1392 	struct hostapd_data *hapd;
1393 
1394 	if (wpa_s->ap_iface)
1395 		hapd = wpa_s->ap_iface->bss[0];
1396 	else if (wpa_s->ifmsh)
1397 		hapd = wpa_s->ifmsh->bss[0];
1398 	else
1399 		return -1;
1400 	return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
1401 }
1402 
1403 
ap_ctrl_iface_sta_next(struct wpa_supplicant * wpa_s,const char * txtaddr,char * buf,size_t buflen)1404 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1405 			   char *buf, size_t buflen)
1406 {
1407 	struct hostapd_data *hapd;
1408 
1409 	if (wpa_s->ap_iface)
1410 		hapd = wpa_s->ap_iface->bss[0];
1411 	else if (wpa_s->ifmsh)
1412 		hapd = wpa_s->ifmsh->bss[0];
1413 	else
1414 		return -1;
1415 	return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
1416 }
1417 
1418 
ap_ctrl_iface_sta_disassociate(struct wpa_supplicant * wpa_s,const char * txtaddr)1419 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1420 				   const char *txtaddr)
1421 {
1422 	if (wpa_s->ap_iface == NULL)
1423 		return -1;
1424 	return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1425 					       txtaddr);
1426 }
1427 
1428 
ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant * wpa_s,const char * txtaddr)1429 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1430 				     const char *txtaddr)
1431 {
1432 	if (wpa_s->ap_iface == NULL)
1433 		return -1;
1434 	return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1435 						 txtaddr);
1436 }
1437 
1438 
ap_ctrl_iface_wpa_get_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen,int verbose)1439 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1440 				 size_t buflen, int verbose)
1441 {
1442 	char *pos = buf, *end = buf + buflen;
1443 	int ret;
1444 	struct hostapd_bss_config *conf;
1445 
1446 	if (wpa_s->ap_iface == NULL)
1447 		return -1;
1448 
1449 	conf = wpa_s->ap_iface->bss[0]->conf;
1450 	if (conf->wpa == 0)
1451 		return 0;
1452 
1453 	ret = os_snprintf(pos, end - pos,
1454 			  "pairwise_cipher=%s\n"
1455 			  "group_cipher=%s\n"
1456 			  "key_mgmt=%s\n",
1457 			  wpa_cipher_txt(conf->rsn_pairwise),
1458 			  wpa_cipher_txt(conf->wpa_group),
1459 			  wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1460 					   conf->wpa));
1461 	if (os_snprintf_error(end - pos, ret))
1462 		return pos - buf;
1463 	pos += ret;
1464 	return pos - buf;
1465 }
1466 
1467 #endif /* CONFIG_CTRL_IFACE */
1468 
1469 
wpa_supplicant_ap_update_beacon(struct wpa_supplicant * wpa_s)1470 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1471 {
1472 	struct hostapd_iface *iface = wpa_s->ap_iface;
1473 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1474 	struct hostapd_data *hapd;
1475 
1476 	if (ssid == NULL || wpa_s->ap_iface == NULL ||
1477 	    ssid->mode == WPAS_MODE_INFRA ||
1478 	    ssid->mode == WPAS_MODE_IBSS)
1479 		return -1;
1480 
1481 #ifdef CONFIG_P2P
1482 	if (ssid->mode == WPAS_MODE_P2P_GO)
1483 		iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1484 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1485 		iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1486 			P2P_GROUP_FORMATION;
1487 #endif /* CONFIG_P2P */
1488 
1489 	hapd = iface->bss[0];
1490 	if (hapd->drv_priv == NULL)
1491 		return -1;
1492 	ieee802_11_set_beacons(iface);
1493 	hostapd_set_ap_wps_ie(hapd);
1494 
1495 	return 0;
1496 }
1497 
1498 
ap_switch_channel(struct wpa_supplicant * wpa_s,struct csa_settings * settings)1499 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1500 		      struct csa_settings *settings)
1501 {
1502 #ifdef NEED_AP_MLME
1503 	struct hostapd_iface *iface = NULL;
1504 
1505 	if (wpa_s->ap_iface)
1506 		iface = wpa_s->ap_iface;
1507 	else if (wpa_s->ifmsh)
1508 		iface = wpa_s->ifmsh;
1509 
1510 	if (!iface || !iface->bss[0])
1511 		return -1;
1512 
1513 	return hostapd_switch_channel(iface->bss[0], settings);
1514 #else /* NEED_AP_MLME */
1515 	return -1;
1516 #endif /* NEED_AP_MLME */
1517 }
1518 
1519 
1520 #ifdef CONFIG_CTRL_IFACE
ap_ctrl_iface_chanswitch(struct wpa_supplicant * wpa_s,const char * pos)1521 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1522 {
1523 	struct csa_settings settings;
1524 	int ret = hostapd_parse_csa_settings(pos, &settings);
1525 
1526 	if (ret)
1527 		return ret;
1528 
1529 	return ap_switch_channel(wpa_s, &settings);
1530 }
1531 #endif /* CONFIG_CTRL_IFACE */
1532 
1533 
wpas_ap_ch_switch(struct wpa_supplicant * wpa_s,int freq,int ht,int offset,int width,int cf1,int cf2,int finished)1534 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1535 		       int offset, int width, int cf1, int cf2, int finished)
1536 {
1537 	struct hostapd_iface *iface = wpa_s->ap_iface;
1538 
1539 	if (!iface)
1540 		iface = wpa_s->ifmsh;
1541 	if (!iface)
1542 		return;
1543 	wpa_s->assoc_freq = freq;
1544 	if (wpa_s->current_ssid)
1545 		wpa_s->current_ssid->frequency = freq;
1546 	hostapd_event_ch_switch(iface->bss[0], freq, ht,
1547 				offset, width, cf1, cf2, finished);
1548 }
1549 
1550 
wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant * wpa_s,const u8 * addr)1551 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1552 				      const u8 *addr)
1553 {
1554 	struct hostapd_data *hapd;
1555 	struct hostapd_bss_config *conf;
1556 
1557 	if (!wpa_s->ap_iface)
1558 		return -1;
1559 
1560 	if (addr)
1561 		wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1562 			   MAC2STR(addr));
1563 	else
1564 		wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1565 
1566 	hapd = wpa_s->ap_iface->bss[0];
1567 	conf = hapd->conf;
1568 
1569 	os_free(conf->accept_mac);
1570 	conf->accept_mac = NULL;
1571 	conf->num_accept_mac = 0;
1572 	os_free(conf->deny_mac);
1573 	conf->deny_mac = NULL;
1574 	conf->num_deny_mac = 0;
1575 
1576 	if (addr == NULL) {
1577 		conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1578 		return 0;
1579 	}
1580 
1581 	conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1582 	conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1583 	if (conf->accept_mac == NULL)
1584 		return -1;
1585 	os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1586 	conf->num_accept_mac = 1;
1587 
1588 	return 0;
1589 }
1590 
1591 
1592 #ifdef CONFIG_WPS_NFC
wpas_ap_wps_add_nfc_pw(struct wpa_supplicant * wpa_s,u16 pw_id,const struct wpabuf * pw,const u8 * pubkey_hash)1593 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1594 			   const struct wpabuf *pw, const u8 *pubkey_hash)
1595 {
1596 	struct hostapd_data *hapd;
1597 	struct wps_context *wps;
1598 
1599 	if (!wpa_s->ap_iface)
1600 		return -1;
1601 	hapd = wpa_s->ap_iface->bss[0];
1602 	wps = hapd->wps;
1603 
1604 	if (wpa_s->p2pdev->conf->wps_nfc_dh_pubkey == NULL ||
1605 	    wpa_s->p2pdev->conf->wps_nfc_dh_privkey == NULL) {
1606 		wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1607 		return -1;
1608 	}
1609 
1610 	dh5_free(wps->dh_ctx);
1611 	wpabuf_free(wps->dh_pubkey);
1612 	wpabuf_free(wps->dh_privkey);
1613 	wps->dh_privkey = wpabuf_dup(
1614 		wpa_s->p2pdev->conf->wps_nfc_dh_privkey);
1615 	wps->dh_pubkey = wpabuf_dup(
1616 		wpa_s->p2pdev->conf->wps_nfc_dh_pubkey);
1617 	if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1618 		wps->dh_ctx = NULL;
1619 		wpabuf_free(wps->dh_pubkey);
1620 		wps->dh_pubkey = NULL;
1621 		wpabuf_free(wps->dh_privkey);
1622 		wps->dh_privkey = NULL;
1623 		return -1;
1624 	}
1625 	wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1626 	if (wps->dh_ctx == NULL)
1627 		return -1;
1628 
1629 	return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1630 					      pw_id,
1631 					      pw ? wpabuf_head(pw) : NULL,
1632 					      pw ? wpabuf_len(pw) : 0, 1);
1633 }
1634 #endif /* CONFIG_WPS_NFC */
1635 
1636 
1637 #ifdef CONFIG_CTRL_IFACE
wpas_ap_stop_ap(struct wpa_supplicant * wpa_s)1638 int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
1639 {
1640 	struct hostapd_data *hapd;
1641 
1642 	if (!wpa_s->ap_iface)
1643 		return -1;
1644 	hapd = wpa_s->ap_iface->bss[0];
1645 	return hostapd_ctrl_iface_stop_ap(hapd);
1646 }
1647 
1648 
wpas_ap_pmksa_cache_list(struct wpa_supplicant * wpa_s,char * buf,size_t len)1649 int wpas_ap_pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf,
1650 			     size_t len)
1651 {
1652 	size_t reply_len = 0, i;
1653 	char ap_delimiter[] = "---- AP ----\n";
1654 	char mesh_delimiter[] = "---- mesh ----\n";
1655 	size_t dlen;
1656 
1657 	if (wpa_s->ap_iface) {
1658 		dlen = os_strlen(ap_delimiter);
1659 		if (dlen > len - reply_len)
1660 			return reply_len;
1661 		os_memcpy(&buf[reply_len], ap_delimiter, dlen);
1662 		reply_len += dlen;
1663 
1664 		for (i = 0; i < wpa_s->ap_iface->num_bss; i++) {
1665 			reply_len += hostapd_ctrl_iface_pmksa_list(
1666 				wpa_s->ap_iface->bss[i],
1667 				&buf[reply_len], len - reply_len);
1668 		}
1669 	}
1670 
1671 	if (wpa_s->ifmsh) {
1672 		dlen = os_strlen(mesh_delimiter);
1673 		if (dlen > len - reply_len)
1674 			return reply_len;
1675 		os_memcpy(&buf[reply_len], mesh_delimiter, dlen);
1676 		reply_len += dlen;
1677 
1678 		reply_len += hostapd_ctrl_iface_pmksa_list(
1679 			wpa_s->ifmsh->bss[0], &buf[reply_len],
1680 			len - reply_len);
1681 	}
1682 
1683 	return reply_len;
1684 }
1685 
1686 
wpas_ap_pmksa_cache_flush(struct wpa_supplicant * wpa_s)1687 void wpas_ap_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
1688 {
1689 	size_t i;
1690 
1691 	if (wpa_s->ap_iface) {
1692 		for (i = 0; i < wpa_s->ap_iface->num_bss; i++)
1693 			hostapd_ctrl_iface_pmksa_flush(wpa_s->ap_iface->bss[i]);
1694 	}
1695 
1696 	if (wpa_s->ifmsh)
1697 		hostapd_ctrl_iface_pmksa_flush(wpa_s->ifmsh->bss[0]);
1698 }
1699 
1700 
1701 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
1702 #ifdef CONFIG_MESH
1703 
wpas_ap_pmksa_cache_list_mesh(struct wpa_supplicant * wpa_s,const u8 * addr,char * buf,size_t len)1704 int wpas_ap_pmksa_cache_list_mesh(struct wpa_supplicant *wpa_s, const u8 *addr,
1705 				  char *buf, size_t len)
1706 {
1707 	return hostapd_ctrl_iface_pmksa_list_mesh(wpa_s->ifmsh->bss[0], addr,
1708 						  &buf[0], len);
1709 }
1710 
1711 
wpas_ap_pmksa_cache_add_external(struct wpa_supplicant * wpa_s,char * cmd)1712 int wpas_ap_pmksa_cache_add_external(struct wpa_supplicant *wpa_s, char *cmd)
1713 {
1714 	struct external_pmksa_cache *entry;
1715 	void *pmksa_cache;
1716 
1717 	pmksa_cache = hostapd_ctrl_iface_pmksa_create_entry(wpa_s->own_addr,
1718 							    cmd);
1719 	if (!pmksa_cache)
1720 		return -1;
1721 
1722 	entry = os_zalloc(sizeof(struct external_pmksa_cache));
1723 	if (!entry)
1724 		return -1;
1725 
1726 	entry->pmksa_cache = pmksa_cache;
1727 
1728 	dl_list_add(&wpa_s->mesh_external_pmksa_cache, &entry->list);
1729 
1730 	return 0;
1731 }
1732 
1733 #endif /* CONFIG_MESH */
1734 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
1735 
1736 #endif /* CONFIG_CTRL_IFACE */
1737 
1738 
1739 #ifdef NEED_AP_MLME
wpas_ap_event_dfs_radar_detected(struct wpa_supplicant * wpa_s,struct dfs_event * radar)1740 void wpas_ap_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
1741 				      struct dfs_event *radar)
1742 {
1743 	struct hostapd_iface *iface = wpa_s->ap_iface;
1744 
1745 	if (!iface)
1746 		iface = wpa_s->ifmsh;
1747 	if (!iface || !iface->bss[0])
1748 		return;
1749 	wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1750 	hostapd_dfs_radar_detected(iface, radar->freq,
1751 				   radar->ht_enabled, radar->chan_offset,
1752 				   radar->chan_width,
1753 				   radar->cf1, radar->cf2);
1754 }
1755 
1756 
wpas_ap_event_dfs_cac_started(struct wpa_supplicant * wpa_s,struct dfs_event * radar)1757 void wpas_ap_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
1758 				   struct dfs_event *radar)
1759 {
1760 	struct hostapd_iface *iface = wpa_s->ap_iface;
1761 
1762 	if (!iface)
1763 		iface = wpa_s->ifmsh;
1764 	if (!iface || !iface->bss[0])
1765 		return;
1766 	wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
1767 	hostapd_dfs_start_cac(iface, radar->freq,
1768 			      radar->ht_enabled, radar->chan_offset,
1769 			      radar->chan_width, radar->cf1, radar->cf2);
1770 }
1771 
1772 
wpas_ap_event_dfs_cac_finished(struct wpa_supplicant * wpa_s,struct dfs_event * radar)1773 void wpas_ap_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
1774 				    struct dfs_event *radar)
1775 {
1776 	struct hostapd_iface *iface = wpa_s->ap_iface;
1777 
1778 	if (!iface)
1779 		iface = wpa_s->ifmsh;
1780 	if (!iface || !iface->bss[0])
1781 		return;
1782 	wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1783 	hostapd_dfs_complete_cac(iface, 1, radar->freq,
1784 				 radar->ht_enabled, radar->chan_offset,
1785 				 radar->chan_width, radar->cf1, radar->cf2);
1786 }
1787 
1788 
wpas_ap_event_dfs_cac_aborted(struct wpa_supplicant * wpa_s,struct dfs_event * radar)1789 void wpas_ap_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
1790 				   struct dfs_event *radar)
1791 {
1792 	struct hostapd_iface *iface = wpa_s->ap_iface;
1793 
1794 	if (!iface)
1795 		iface = wpa_s->ifmsh;
1796 	if (!iface || !iface->bss[0])
1797 		return;
1798 	wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1799 	hostapd_dfs_complete_cac(iface, 0, radar->freq,
1800 				 radar->ht_enabled, radar->chan_offset,
1801 				 radar->chan_width, radar->cf1, radar->cf2);
1802 }
1803 
1804 
wpas_ap_event_dfs_cac_nop_finished(struct wpa_supplicant * wpa_s,struct dfs_event * radar)1805 void wpas_ap_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
1806 					struct dfs_event *radar)
1807 {
1808 	struct hostapd_iface *iface = wpa_s->ap_iface;
1809 
1810 	if (!iface)
1811 		iface = wpa_s->ifmsh;
1812 	if (!iface || !iface->bss[0])
1813 		return;
1814 	wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1815 	hostapd_dfs_nop_finished(iface, radar->freq,
1816 				 radar->ht_enabled, radar->chan_offset,
1817 				 radar->chan_width, radar->cf1, radar->cf2);
1818 }
1819 #endif /* NEED_AP_MLME */
1820 
1821 
ap_periodic(struct wpa_supplicant * wpa_s)1822 void ap_periodic(struct wpa_supplicant *wpa_s)
1823 {
1824 	if (wpa_s->ap_iface)
1825 		hostapd_periodic_iface(wpa_s->ap_iface);
1826 }
1827