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