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