xref: /freebsd/contrib/wpa/wpa_supplicant/ap.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
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 "ap/hostapd.h"
18 #include "ap/ap_config.h"
19 #include "ap/ap_drv_ops.h"
20 #ifdef NEED_AP_MLME
21 #include "ap/ieee802_11.h"
22 #endif /* NEED_AP_MLME */
23 #include "ap/beacon.h"
24 #include "ap/ieee802_1x.h"
25 #include "ap/wps_hostapd.h"
26 #include "ap/ctrl_iface_ap.h"
27 #include "wps/wps.h"
28 #include "common/ieee802_11_defs.h"
29 #include "config_ssid.h"
30 #include "config.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "p2p_supplicant.h"
34 #include "ap.h"
35 #include "ap/sta_info.h"
36 #include "notify.h"
37 
38 
39 #ifdef CONFIG_WPS
40 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
41 #endif /* CONFIG_WPS */
42 
43 
44 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
45 				  struct wpa_ssid *ssid,
46 				  struct hostapd_config *conf)
47 {
48 	struct hostapd_bss_config *bss = &conf->bss[0];
49 	int pairwise;
50 
51 	conf->driver = wpa_s->driver;
52 
53 	os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
54 
55 	if (ssid->frequency == 0) {
56 		/* default channel 11 */
57 		conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
58 		conf->channel = 11;
59 	} else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
60 		conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
61 		conf->channel = (ssid->frequency - 2407) / 5;
62 	} else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
63 		   (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
64 		conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
65 		conf->channel = (ssid->frequency - 5000) / 5;
66 	} else if (ssid->frequency >= 56160 + 2160 * 1 &&
67 		   ssid->frequency <= 56160 + 2160 * 4) {
68 		conf->hw_mode = HOSTAPD_MODE_IEEE80211AD;
69 		conf->channel = (ssid->frequency - 56160) / 2160;
70 	} else {
71 		wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
72 			   ssid->frequency);
73 		return -1;
74 	}
75 
76 	/* TODO: enable HT40 if driver supports it;
77 	 * drop to 11b if driver does not support 11g */
78 
79 #ifdef CONFIG_IEEE80211N
80 	/*
81 	 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
82 	 * and a mask of allowed capabilities within conf->ht_capab.
83 	 * Using default config settings for: conf->ht_op_mode_fixed,
84 	 * conf->secondary_channel, conf->require_ht
85 	 */
86 	if (wpa_s->hw.modes) {
87 		struct hostapd_hw_modes *mode = NULL;
88 		int i, no_ht = 0;
89 		for (i = 0; i < wpa_s->hw.num_modes; i++) {
90 			if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
91 				mode = &wpa_s->hw.modes[i];
92 				break;
93 			}
94 		}
95 
96 #ifdef CONFIG_HT_OVERRIDES
97 		if (ssid->disable_ht) {
98 			conf->ieee80211n = 0;
99 			conf->ht_capab = 0;
100 			no_ht = 1;
101 		}
102 #endif /* CONFIG_HT_OVERRIDES */
103 
104 		if (!no_ht && mode && mode->ht_capab) {
105 			conf->ieee80211n = 1;
106 #ifdef CONFIG_P2P
107 			if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
108 			    (mode->ht_capab &
109 			     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
110 			    ssid->ht40)
111 				conf->secondary_channel =
112 					wpas_p2p_get_ht40_mode(wpa_s, mode,
113 							       conf->channel);
114 			if (conf->secondary_channel)
115 				conf->ht_capab |=
116 					HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
117 #endif /* CONFIG_P2P */
118 
119 			/*
120 			 * white-list capabilities that won't cause issues
121 			 * to connecting stations, while leaving the current
122 			 * capabilities intact (currently disabled SMPS).
123 			 */
124 			conf->ht_capab |= mode->ht_capab &
125 				(HT_CAP_INFO_GREEN_FIELD |
126 				 HT_CAP_INFO_SHORT_GI20MHZ |
127 				 HT_CAP_INFO_SHORT_GI40MHZ |
128 				 HT_CAP_INFO_RX_STBC_MASK |
129 				 HT_CAP_INFO_MAX_AMSDU_SIZE);
130 		}
131 	}
132 #endif /* CONFIG_IEEE80211N */
133 
134 #ifdef CONFIG_P2P
135 	if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
136 		/* Remove 802.11b rates from supported and basic rate sets */
137 		int *list = os_malloc(4 * sizeof(int));
138 		if (list) {
139 			list[0] = 60;
140 			list[1] = 120;
141 			list[2] = 240;
142 			list[3] = -1;
143 		}
144 		conf->basic_rates = list;
145 
146 		list = os_malloc(9 * sizeof(int));
147 		if (list) {
148 			list[0] = 60;
149 			list[1] = 90;
150 			list[2] = 120;
151 			list[3] = 180;
152 			list[4] = 240;
153 			list[5] = 360;
154 			list[6] = 480;
155 			list[7] = 540;
156 			list[8] = -1;
157 		}
158 		conf->supported_rates = list;
159 	}
160 
161 	bss->isolate = !wpa_s->conf->p2p_intra_bss;
162 #endif /* CONFIG_P2P */
163 
164 	if (ssid->ssid_len == 0) {
165 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
166 		return -1;
167 	}
168 	os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
169 	bss->ssid.ssid_len = ssid->ssid_len;
170 	bss->ssid.ssid_set = 1;
171 
172 	bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
173 
174 	if (ssid->auth_alg)
175 		bss->auth_algs = ssid->auth_alg;
176 
177 	if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
178 		bss->wpa = ssid->proto;
179 	bss->wpa_key_mgmt = ssid->key_mgmt;
180 	bss->wpa_pairwise = ssid->pairwise_cipher;
181 	if (ssid->psk_set) {
182 		os_free(bss->ssid.wpa_psk);
183 		bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
184 		if (bss->ssid.wpa_psk == NULL)
185 			return -1;
186 		os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
187 		bss->ssid.wpa_psk->group = 1;
188 	} else if (ssid->passphrase) {
189 		bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
190 	} else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
191 		   ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
192 		struct hostapd_wep_keys *wep = &bss->ssid.wep;
193 		int i;
194 		for (i = 0; i < NUM_WEP_KEYS; i++) {
195 			if (ssid->wep_key_len[i] == 0)
196 				continue;
197 			wep->key[i] = os_malloc(ssid->wep_key_len[i]);
198 			if (wep->key[i] == NULL)
199 				return -1;
200 			os_memcpy(wep->key[i], ssid->wep_key[i],
201 				  ssid->wep_key_len[i]);
202 			wep->len[i] = ssid->wep_key_len[i];
203 		}
204 		wep->idx = ssid->wep_tx_keyidx;
205 		wep->keys_set = 1;
206 	}
207 
208 	if (ssid->ap_max_inactivity)
209 		bss->ap_max_inactivity = ssid->ap_max_inactivity;
210 
211 	if (ssid->dtim_period)
212 		bss->dtim_period = ssid->dtim_period;
213 
214 	/* Select group cipher based on the enabled pairwise cipher suites */
215 	pairwise = 0;
216 	if (bss->wpa & 1)
217 		pairwise |= bss->wpa_pairwise;
218 	if (bss->wpa & 2) {
219 		if (bss->rsn_pairwise == 0)
220 			bss->rsn_pairwise = bss->wpa_pairwise;
221 		pairwise |= bss->rsn_pairwise;
222 	}
223 	if (pairwise & WPA_CIPHER_TKIP)
224 		bss->wpa_group = WPA_CIPHER_TKIP;
225 	else if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ==
226 		 WPA_CIPHER_GCMP)
227 		bss->wpa_group = WPA_CIPHER_GCMP;
228 	else
229 		bss->wpa_group = WPA_CIPHER_CCMP;
230 
231 	if (bss->wpa && bss->ieee802_1x)
232 		bss->ssid.security_policy = SECURITY_WPA;
233 	else if (bss->wpa)
234 		bss->ssid.security_policy = SECURITY_WPA_PSK;
235 	else if (bss->ieee802_1x) {
236 		int cipher = WPA_CIPHER_NONE;
237 		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
238 		bss->ssid.wep.default_len = bss->default_wep_key_len;
239 		if (bss->default_wep_key_len)
240 			cipher = bss->default_wep_key_len >= 13 ?
241 				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
242 		bss->wpa_group = cipher;
243 		bss->wpa_pairwise = cipher;
244 		bss->rsn_pairwise = cipher;
245 	} else if (bss->ssid.wep.keys_set) {
246 		int cipher = WPA_CIPHER_WEP40;
247 		if (bss->ssid.wep.len[0] >= 13)
248 			cipher = WPA_CIPHER_WEP104;
249 		bss->ssid.security_policy = SECURITY_STATIC_WEP;
250 		bss->wpa_group = cipher;
251 		bss->wpa_pairwise = cipher;
252 		bss->rsn_pairwise = cipher;
253 	} else {
254 		bss->ssid.security_policy = SECURITY_PLAINTEXT;
255 		bss->wpa_group = WPA_CIPHER_NONE;
256 		bss->wpa_pairwise = WPA_CIPHER_NONE;
257 		bss->rsn_pairwise = WPA_CIPHER_NONE;
258 	}
259 
260 #ifdef CONFIG_WPS
261 	/*
262 	 * Enable WPS by default for open and WPA/WPA2-Personal network, but
263 	 * require user interaction to actually use it. Only the internal
264 	 * Registrar is supported.
265 	 */
266 	if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
267 	    bss->ssid.security_policy != SECURITY_PLAINTEXT)
268 		goto no_wps;
269 #ifdef CONFIG_WPS2
270 	if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
271 	    (!(pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
272 		goto no_wps; /* WPS2 does not allow WPA/TKIP-only
273 			      * configuration */
274 #endif /* CONFIG_WPS2 */
275 	bss->eap_server = 1;
276 
277 	if (!ssid->ignore_broadcast_ssid)
278 		bss->wps_state = 2;
279 
280 	bss->ap_setup_locked = 2;
281 	if (wpa_s->conf->config_methods)
282 		bss->config_methods = os_strdup(wpa_s->conf->config_methods);
283 	os_memcpy(bss->device_type, wpa_s->conf->device_type,
284 		  WPS_DEV_TYPE_LEN);
285 	if (wpa_s->conf->device_name) {
286 		bss->device_name = os_strdup(wpa_s->conf->device_name);
287 		bss->friendly_name = os_strdup(wpa_s->conf->device_name);
288 	}
289 	if (wpa_s->conf->manufacturer)
290 		bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
291 	if (wpa_s->conf->model_name)
292 		bss->model_name = os_strdup(wpa_s->conf->model_name);
293 	if (wpa_s->conf->model_number)
294 		bss->model_number = os_strdup(wpa_s->conf->model_number);
295 	if (wpa_s->conf->serial_number)
296 		bss->serial_number = os_strdup(wpa_s->conf->serial_number);
297 	if (is_nil_uuid(wpa_s->conf->uuid))
298 		os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
299 	else
300 		os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
301 	os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
302 	bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
303 no_wps:
304 #endif /* CONFIG_WPS */
305 
306 	if (wpa_s->max_stations &&
307 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
308 		bss->max_num_sta = wpa_s->max_stations;
309 	else
310 		bss->max_num_sta = wpa_s->conf->max_num_sta;
311 
312 	bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
313 
314 	return 0;
315 }
316 
317 
318 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
319 {
320 #ifdef CONFIG_P2P
321 	struct wpa_supplicant *wpa_s = ctx;
322 	const struct ieee80211_mgmt *mgmt;
323 	size_t hdr_len;
324 
325 	mgmt = (const struct ieee80211_mgmt *) buf;
326 	hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
327 	if (hdr_len > len)
328 		return;
329 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
330 			   mgmt->u.action.category,
331 			   &mgmt->u.action.u.vs_public_action.action,
332 			   len - hdr_len, freq);
333 #endif /* CONFIG_P2P */
334 }
335 
336 
337 static void ap_wps_event_cb(void *ctx, enum wps_event event,
338 			    union wps_event_data *data)
339 {
340 #ifdef CONFIG_P2P
341 	struct wpa_supplicant *wpa_s = ctx;
342 
343 	if (event == WPS_EV_FAIL) {
344 		struct wps_event_fail *fail = &data->fail;
345 
346 		if (wpa_s->parent && wpa_s->parent != wpa_s &&
347 		    wpa_s == wpa_s->global->p2p_group_formation) {
348 			/*
349 			 * src/ap/wps_hostapd.c has already sent this on the
350 			 * main interface, so only send on the parent interface
351 			 * here if needed.
352 			 */
353 			wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
354 				"msg=%d config_error=%d",
355 				fail->msg, fail->config_error);
356 		}
357 		wpas_p2p_wps_failed(wpa_s, fail);
358 	}
359 #endif /* CONFIG_P2P */
360 }
361 
362 
363 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
364 				 int authorized, const u8 *p2p_dev_addr)
365 {
366 	wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
367 }
368 
369 
370 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
371 {
372 #ifdef CONFIG_P2P
373 	struct wpa_supplicant *wpa_s = ctx;
374 	const struct ieee80211_mgmt *mgmt;
375 	size_t hdr_len;
376 
377 	mgmt = (const struct ieee80211_mgmt *) buf;
378 	hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
379 	if (hdr_len > len)
380 		return -1;
381 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
382 			   mgmt->u.action.category,
383 			   &mgmt->u.action.u.vs_public_action.action,
384 			   len - hdr_len, freq);
385 #endif /* CONFIG_P2P */
386 	return 0;
387 }
388 
389 
390 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
391 			   const u8 *bssid, const u8 *ie, size_t ie_len,
392 			   int ssi_signal)
393 {
394 #ifdef CONFIG_P2P
395 	struct wpa_supplicant *wpa_s = ctx;
396 	return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
397 				     ssi_signal);
398 #else /* CONFIG_P2P */
399 	return 0;
400 #endif /* CONFIG_P2P */
401 }
402 
403 
404 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
405 				  const u8 *uuid_e)
406 {
407 #ifdef CONFIG_P2P
408 	struct wpa_supplicant *wpa_s = ctx;
409 	wpas_p2p_wps_success(wpa_s, mac_addr, 1);
410 #endif /* CONFIG_P2P */
411 }
412 
413 
414 static void wpas_ap_configured_cb(void *ctx)
415 {
416 	struct wpa_supplicant *wpa_s = ctx;
417 
418 	wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
419 
420 	if (wpa_s->ap_configured_cb)
421 		wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
422 					wpa_s->ap_configured_cb_data);
423 }
424 
425 
426 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
427 			     struct wpa_ssid *ssid)
428 {
429 	struct wpa_driver_associate_params params;
430 	struct hostapd_iface *hapd_iface;
431 	struct hostapd_config *conf;
432 	size_t i;
433 
434 	if (ssid->ssid == NULL || ssid->ssid_len == 0) {
435 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
436 		return -1;
437 	}
438 
439 	wpa_supplicant_ap_deinit(wpa_s);
440 
441 	wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
442 		   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
443 
444 	os_memset(&params, 0, sizeof(params));
445 	params.ssid = ssid->ssid;
446 	params.ssid_len = ssid->ssid_len;
447 	switch (ssid->mode) {
448 	case WPAS_MODE_INFRA:
449 		params.mode = IEEE80211_MODE_INFRA;
450 		break;
451 	case WPAS_MODE_IBSS:
452 		params.mode = IEEE80211_MODE_IBSS;
453 		break;
454 	case WPAS_MODE_AP:
455 	case WPAS_MODE_P2P_GO:
456 	case WPAS_MODE_P2P_GROUP_FORMATION:
457 		params.mode = IEEE80211_MODE_AP;
458 		break;
459 	}
460 	params.freq = ssid->frequency;
461 
462 	params.wpa_proto = ssid->proto;
463 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
464 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
465 	else
466 		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
467 	params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
468 
469 	if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
470 		wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
471 	else if (ssid->pairwise_cipher & WPA_CIPHER_GCMP)
472 		wpa_s->pairwise_cipher = WPA_CIPHER_GCMP;
473 	else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
474 		wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
475 	else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
476 		wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
477 	else {
478 		wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
479 			   "cipher.");
480 		return -1;
481 	}
482 	params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
483 	params.group_suite = params.pairwise_suite;
484 
485 #ifdef CONFIG_P2P
486 	if (ssid->mode == WPAS_MODE_P2P_GO ||
487 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
488 		params.p2p = 1;
489 #endif /* CONFIG_P2P */
490 
491 	if (wpa_s->parent->set_ap_uapsd)
492 		params.uapsd = wpa_s->parent->ap_uapsd;
493 	else
494 		params.uapsd = -1;
495 
496 	if (wpa_drv_associate(wpa_s, &params) < 0) {
497 		wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
498 		return -1;
499 	}
500 
501 	wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
502 	if (hapd_iface == NULL)
503 		return -1;
504 	hapd_iface->owner = wpa_s;
505 	hapd_iface->drv_flags = wpa_s->drv_flags;
506 	hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
507 
508 	wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
509 	if (conf == NULL) {
510 		wpa_supplicant_ap_deinit(wpa_s);
511 		return -1;
512 	}
513 
514 	os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
515 		  wpa_s->conf->wmm_ac_params,
516 		  sizeof(wpa_s->conf->wmm_ac_params));
517 
518 	if (params.uapsd > 0) {
519 		conf->bss->wmm_enabled = 1;
520 		conf->bss->wmm_uapsd = 1;
521 	}
522 
523 	if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
524 		wpa_printf(MSG_ERROR, "Failed to create AP configuration");
525 		wpa_supplicant_ap_deinit(wpa_s);
526 		return -1;
527 	}
528 
529 #ifdef CONFIG_P2P
530 	if (ssid->mode == WPAS_MODE_P2P_GO)
531 		conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
532 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
533 		conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
534 			P2P_GROUP_FORMATION;
535 #endif /* CONFIG_P2P */
536 
537 	hapd_iface->num_bss = conf->num_bss;
538 	hapd_iface->bss = os_calloc(conf->num_bss,
539 				    sizeof(struct hostapd_data *));
540 	if (hapd_iface->bss == NULL) {
541 		wpa_supplicant_ap_deinit(wpa_s);
542 		return -1;
543 	}
544 
545 	for (i = 0; i < conf->num_bss; i++) {
546 		hapd_iface->bss[i] =
547 			hostapd_alloc_bss_data(hapd_iface, conf,
548 					       &conf->bss[i]);
549 		if (hapd_iface->bss[i] == NULL) {
550 			wpa_supplicant_ap_deinit(wpa_s);
551 			return -1;
552 		}
553 
554 		hapd_iface->bss[i]->msg_ctx = wpa_s;
555 		hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
556 		hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
557 		hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
558 		hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
559 		hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
560 		hostapd_register_probereq_cb(hapd_iface->bss[i],
561 					     ap_probe_req_rx, wpa_s);
562 		hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
563 		hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
564 		hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
565 		hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
566 		hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
567 		hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
568 #ifdef CONFIG_P2P
569 		hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
570 		hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
571 								    ssid);
572 #endif /* CONFIG_P2P */
573 		hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
574 		hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
575 	}
576 
577 	os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
578 	hapd_iface->bss[0]->driver = wpa_s->driver;
579 	hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
580 
581 	wpa_s->current_ssid = ssid;
582 	os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
583 	wpa_s->assoc_freq = ssid->frequency;
584 
585 	if (hostapd_setup_interface(wpa_s->ap_iface)) {
586 		wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
587 		wpa_supplicant_ap_deinit(wpa_s);
588 		return -1;
589 	}
590 
591 	return 0;
592 }
593 
594 
595 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
596 {
597 #ifdef CONFIG_WPS
598 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
599 #endif /* CONFIG_WPS */
600 
601 	if (wpa_s->ap_iface == NULL)
602 		return;
603 
604 	wpa_s->current_ssid = NULL;
605 	wpa_s->assoc_freq = 0;
606 #ifdef CONFIG_P2P
607 	if (wpa_s->ap_iface->bss)
608 		wpa_s->ap_iface->bss[0]->p2p_group = NULL;
609 	wpas_p2p_group_deinit(wpa_s);
610 #endif /* CONFIG_P2P */
611 	hostapd_interface_deinit(wpa_s->ap_iface);
612 	hostapd_interface_free(wpa_s->ap_iface);
613 	wpa_s->ap_iface = NULL;
614 	wpa_drv_deinit_ap(wpa_s);
615 }
616 
617 
618 void ap_tx_status(void *ctx, const u8 *addr,
619 		  const u8 *buf, size_t len, int ack)
620 {
621 #ifdef NEED_AP_MLME
622 	struct wpa_supplicant *wpa_s = ctx;
623 	hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
624 #endif /* NEED_AP_MLME */
625 }
626 
627 
628 void ap_eapol_tx_status(void *ctx, const u8 *dst,
629 			const u8 *data, size_t len, int ack)
630 {
631 #ifdef NEED_AP_MLME
632 	struct wpa_supplicant *wpa_s = ctx;
633 	hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
634 #endif /* NEED_AP_MLME */
635 }
636 
637 
638 void ap_client_poll_ok(void *ctx, const u8 *addr)
639 {
640 #ifdef NEED_AP_MLME
641 	struct wpa_supplicant *wpa_s = ctx;
642 	if (wpa_s->ap_iface)
643 		hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
644 #endif /* NEED_AP_MLME */
645 }
646 
647 
648 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
649 {
650 #ifdef NEED_AP_MLME
651 	struct wpa_supplicant *wpa_s = ctx;
652 	ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
653 #endif /* NEED_AP_MLME */
654 }
655 
656 
657 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
658 {
659 #ifdef NEED_AP_MLME
660 	struct wpa_supplicant *wpa_s = ctx;
661 	struct hostapd_frame_info fi;
662 	os_memset(&fi, 0, sizeof(fi));
663 	fi.datarate = rx_mgmt->datarate;
664 	fi.ssi_signal = rx_mgmt->ssi_signal;
665 	ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
666 			rx_mgmt->frame_len, &fi);
667 #endif /* NEED_AP_MLME */
668 }
669 
670 
671 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
672 {
673 #ifdef NEED_AP_MLME
674 	struct wpa_supplicant *wpa_s = ctx;
675 	ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
676 #endif /* NEED_AP_MLME */
677 }
678 
679 
680 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
681 				const u8 *src_addr, const u8 *buf, size_t len)
682 {
683 	ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
684 }
685 
686 
687 #ifdef CONFIG_WPS
688 
689 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
690 			      const u8 *p2p_dev_addr)
691 {
692 	if (!wpa_s->ap_iface)
693 		return -1;
694 	return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
695 					 p2p_dev_addr);
696 }
697 
698 
699 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
700 {
701 	struct wps_registrar *reg;
702 	int reg_sel = 0, wps_sta = 0;
703 
704 	if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
705 		return -1;
706 
707 	reg = wpa_s->ap_iface->bss[0]->wps->registrar;
708 	reg_sel = wps_registrar_wps_cancel(reg);
709 	wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
710 				  ap_sta_wps_cancel, NULL);
711 
712 	if (!reg_sel && !wps_sta) {
713 		wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
714 			   "time");
715 		return -1;
716 	}
717 
718 	/*
719 	 * There are 2 cases to return wps cancel as success:
720 	 * 1. When wps cancel was initiated but no connection has been
721 	 *    established with client yet.
722 	 * 2. Client is in the middle of exchanging WPS messages.
723 	 */
724 
725 	return 0;
726 }
727 
728 
729 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
730 			      const char *pin, char *buf, size_t buflen,
731 			      int timeout)
732 {
733 	int ret, ret_len = 0;
734 
735 	if (!wpa_s->ap_iface)
736 		return -1;
737 
738 	if (pin == NULL) {
739 		unsigned int rpin = wps_generate_pin();
740 		ret_len = os_snprintf(buf, buflen, "%08d", rpin);
741 		pin = buf;
742 	} else
743 		ret_len = os_snprintf(buf, buflen, "%s", pin);
744 
745 	ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
746 				  timeout);
747 	if (ret)
748 		return -1;
749 	return ret_len;
750 }
751 
752 
753 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
754 {
755 	struct wpa_supplicant *wpa_s = eloop_data;
756 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
757 	wpas_wps_ap_pin_disable(wpa_s);
758 }
759 
760 
761 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
762 {
763 	struct hostapd_data *hapd;
764 
765 	if (wpa_s->ap_iface == NULL)
766 		return;
767 	hapd = wpa_s->ap_iface->bss[0];
768 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
769 	hapd->ap_pin_failures = 0;
770 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
771 	if (timeout > 0)
772 		eloop_register_timeout(timeout, 0,
773 				       wpas_wps_ap_pin_timeout, wpa_s, NULL);
774 }
775 
776 
777 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
778 {
779 	struct hostapd_data *hapd;
780 
781 	if (wpa_s->ap_iface == NULL)
782 		return;
783 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
784 	hapd = wpa_s->ap_iface->bss[0];
785 	os_free(hapd->conf->ap_pin);
786 	hapd->conf->ap_pin = NULL;
787 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
788 }
789 
790 
791 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
792 {
793 	struct hostapd_data *hapd;
794 	unsigned int pin;
795 	char pin_txt[9];
796 
797 	if (wpa_s->ap_iface == NULL)
798 		return NULL;
799 	hapd = wpa_s->ap_iface->bss[0];
800 	pin = wps_generate_pin();
801 	os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
802 	os_free(hapd->conf->ap_pin);
803 	hapd->conf->ap_pin = os_strdup(pin_txt);
804 	if (hapd->conf->ap_pin == NULL)
805 		return NULL;
806 	wpas_wps_ap_pin_enable(wpa_s, timeout);
807 
808 	return hapd->conf->ap_pin;
809 }
810 
811 
812 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
813 {
814 	struct hostapd_data *hapd;
815 	if (wpa_s->ap_iface == NULL)
816 		return NULL;
817 	hapd = wpa_s->ap_iface->bss[0];
818 	return hapd->conf->ap_pin;
819 }
820 
821 
822 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
823 			int timeout)
824 {
825 	struct hostapd_data *hapd;
826 	char pin_txt[9];
827 	int ret;
828 
829 	if (wpa_s->ap_iface == NULL)
830 		return -1;
831 	hapd = wpa_s->ap_iface->bss[0];
832 	ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
833 	if (ret < 0 || ret >= (int) sizeof(pin_txt))
834 		return -1;
835 	os_free(hapd->conf->ap_pin);
836 	hapd->conf->ap_pin = os_strdup(pin_txt);
837 	if (hapd->conf->ap_pin == NULL)
838 		return -1;
839 	wpas_wps_ap_pin_enable(wpa_s, timeout);
840 
841 	return 0;
842 }
843 
844 
845 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
846 {
847 	struct hostapd_data *hapd;
848 
849 	if (wpa_s->ap_iface == NULL)
850 		return;
851 	hapd = wpa_s->ap_iface->bss[0];
852 
853 	/*
854 	 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
855 	 * PIN if this happens multiple times to slow down brute force attacks.
856 	 */
857 	hapd->ap_pin_failures++;
858 	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
859 		   hapd->ap_pin_failures);
860 	if (hapd->ap_pin_failures < 3)
861 		return;
862 
863 	wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
864 	hapd->ap_pin_failures = 0;
865 	os_free(hapd->conf->ap_pin);
866 	hapd->conf->ap_pin = NULL;
867 }
868 
869 #endif /* CONFIG_WPS */
870 
871 
872 #ifdef CONFIG_CTRL_IFACE
873 
874 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
875 			    char *buf, size_t buflen)
876 {
877 	if (wpa_s->ap_iface == NULL)
878 		return -1;
879 	return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
880 					    buf, buflen);
881 }
882 
883 
884 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
885 		      char *buf, size_t buflen)
886 {
887 	if (wpa_s->ap_iface == NULL)
888 		return -1;
889 	return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
890 				      buf, buflen);
891 }
892 
893 
894 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
895 			   char *buf, size_t buflen)
896 {
897 	if (wpa_s->ap_iface == NULL)
898 		return -1;
899 	return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
900 					   buf, buflen);
901 }
902 
903 
904 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
905 				   const char *txtaddr)
906 {
907 	if (wpa_s->ap_iface == NULL)
908 		return -1;
909 	return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
910 					       txtaddr);
911 }
912 
913 
914 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
915 				     const char *txtaddr)
916 {
917 	if (wpa_s->ap_iface == NULL)
918 		return -1;
919 	return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
920 						 txtaddr);
921 }
922 
923 
924 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
925 				 size_t buflen, int verbose)
926 {
927 	char *pos = buf, *end = buf + buflen;
928 	int ret;
929 	struct hostapd_bss_config *conf;
930 
931 	if (wpa_s->ap_iface == NULL)
932 		return -1;
933 
934 	conf = wpa_s->ap_iface->bss[0]->conf;
935 	if (conf->wpa == 0)
936 		return 0;
937 
938 	ret = os_snprintf(pos, end - pos,
939 			  "pairwise_cipher=%s\n"
940 			  "group_cipher=%s\n"
941 			  "key_mgmt=%s\n",
942 			  wpa_cipher_txt(conf->rsn_pairwise),
943 			  wpa_cipher_txt(conf->wpa_group),
944 			  wpa_key_mgmt_txt(conf->wpa_key_mgmt,
945 					   conf->wpa));
946 	if (ret < 0 || ret >= end - pos)
947 		return pos - buf;
948 	pos += ret;
949 	return pos - buf;
950 }
951 
952 #endif /* CONFIG_CTRL_IFACE */
953 
954 
955 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
956 {
957 	struct hostapd_iface *iface = wpa_s->ap_iface;
958 	struct wpa_ssid *ssid = wpa_s->current_ssid;
959 	struct hostapd_data *hapd;
960 
961 	if (ssid == NULL || wpa_s->ap_iface == NULL ||
962 	    ssid->mode == WPAS_MODE_INFRA ||
963 	    ssid->mode == WPAS_MODE_IBSS)
964 		return -1;
965 
966 #ifdef CONFIG_P2P
967 	if (ssid->mode == WPAS_MODE_P2P_GO)
968 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
969 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
970 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
971 			P2P_GROUP_FORMATION;
972 #endif /* CONFIG_P2P */
973 
974 	hapd = iface->bss[0];
975 	if (hapd->drv_priv == NULL)
976 		return -1;
977 	ieee802_11_set_beacons(iface);
978 	hostapd_set_ap_wps_ie(hapd);
979 
980 	return 0;
981 }
982 
983 
984 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
985 		       int offset)
986 {
987 	if (!wpa_s->ap_iface)
988 		return;
989 
990 	wpa_s->assoc_freq = freq;
991 	hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
992 }
993 
994 
995 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
996 				      const u8 *addr)
997 {
998 	struct hostapd_data *hapd;
999 	struct hostapd_bss_config *conf;
1000 
1001 	if (!wpa_s->ap_iface)
1002 		return -1;
1003 
1004 	if (addr)
1005 		wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1006 			   MAC2STR(addr));
1007 	else
1008 		wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1009 
1010 	hapd = wpa_s->ap_iface->bss[0];
1011 	conf = hapd->conf;
1012 
1013 	os_free(conf->accept_mac);
1014 	conf->accept_mac = NULL;
1015 	conf->num_accept_mac = 0;
1016 	os_free(conf->deny_mac);
1017 	conf->deny_mac = NULL;
1018 	conf->num_deny_mac = 0;
1019 
1020 	if (addr == NULL) {
1021 		conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1022 		return 0;
1023 	}
1024 
1025 	conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1026 	conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1027 	if (conf->accept_mac == NULL)
1028 		return -1;
1029 	os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1030 	conf->num_accept_mac = 1;
1031 
1032 	return 0;
1033 }
1034