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