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