• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
wpa_supplicant_conf_ap(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct hostapd_config * conf)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 
50 	conf->driver = wpa_s->driver;
51 
52 	os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
53 
54 	if (ssid->frequency == 0) {
55 		/* default channel 11 */
56 		conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
57 		conf->channel = 11;
58 	} else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
59 		conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
60 		conf->channel = (ssid->frequency - 2407) / 5;
61 	} else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
62 		   (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
63 		conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
64 		conf->channel = (ssid->frequency - 5000) / 5;
65 	} else if (ssid->frequency >= 56160 + 2160 * 1 &&
66 		   ssid->frequency <= 56160 + 2160 * 4) {
67 		conf->hw_mode = HOSTAPD_MODE_IEEE80211AD;
68 		conf->channel = (ssid->frequency - 56160) / 2160;
69 	} else {
70 		wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
71 			   ssid->frequency);
72 		return -1;
73 	}
74 
75 	/* TODO: enable HT40 if driver supports it;
76 	 * drop to 11b if driver does not support 11g */
77 
78 #ifdef CONFIG_IEEE80211N
79 	/*
80 	 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
81 	 * and a mask of allowed capabilities within conf->ht_capab.
82 	 * Using default config settings for: conf->ht_op_mode_fixed,
83 	 * conf->secondary_channel, conf->require_ht
84 	 */
85 	if (wpa_s->hw.modes) {
86 		struct hostapd_hw_modes *mode = NULL;
87 		int i, no_ht = 0;
88 		for (i = 0; i < wpa_s->hw.num_modes; i++) {
89 			if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
90 				mode = &wpa_s->hw.modes[i];
91 				break;
92 			}
93 		}
94 
95 #ifdef CONFIG_HT_OVERRIDES
96 		if (ssid->disable_ht) {
97 			conf->ieee80211n = 0;
98 			conf->ht_capab = 0;
99 			no_ht = 1;
100 		}
101 #endif /* CONFIG_HT_OVERRIDES */
102 
103 		if (!no_ht && mode && mode->ht_capab) {
104 			conf->ieee80211n = 1;
105 #ifdef CONFIG_P2P
106 			if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
107 			    (mode->ht_capab &
108 			     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
109 			    ssid->ht40)
110 				conf->secondary_channel =
111 					wpas_p2p_get_ht40_mode(wpa_s, mode,
112 							       conf->channel);
113 			if (conf->secondary_channel)
114 				conf->ht_capab |=
115 					HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
116 #endif /* CONFIG_P2P */
117 
118 			/*
119 			 * white-list capabilities that won't cause issues
120 			 * to connecting stations, while leaving the current
121 			 * capabilities intact (currently disabled SMPS).
122 			 */
123 			conf->ht_capab |= mode->ht_capab &
124 				(HT_CAP_INFO_GREEN_FIELD |
125 				 HT_CAP_INFO_SHORT_GI20MHZ |
126 				 HT_CAP_INFO_SHORT_GI40MHZ |
127 				 HT_CAP_INFO_RX_STBC_MASK |
128 				 HT_CAP_INFO_MAX_AMSDU_SIZE);
129 		}
130 	}
131 #endif /* CONFIG_IEEE80211N */
132 
133 #ifdef CONFIG_P2P
134 	if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
135 		/* Remove 802.11b rates from supported and basic rate sets */
136 		int *list = os_malloc(4 * sizeof(int));
137 		if (list) {
138 			list[0] = 60;
139 			list[1] = 120;
140 			list[2] = 240;
141 			list[3] = -1;
142 		}
143 		conf->basic_rates = list;
144 
145 		list = os_malloc(9 * sizeof(int));
146 		if (list) {
147 			list[0] = 60;
148 			list[1] = 90;
149 			list[2] = 120;
150 			list[3] = 180;
151 			list[4] = 240;
152 			list[5] = 360;
153 			list[6] = 480;
154 			list[7] = 540;
155 			list[8] = -1;
156 		}
157 		conf->supported_rates = list;
158 	}
159 
160 	bss->isolate = !wpa_s->conf->p2p_intra_bss;
161 #endif /* CONFIG_P2P */
162 
163 	if (ssid->ssid_len == 0) {
164 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
165 		return -1;
166 	}
167 	os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
168 	bss->ssid.ssid_len = ssid->ssid_len;
169 	bss->ssid.ssid_set = 1;
170 
171 	bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
172 
173 	if (ssid->auth_alg)
174 		bss->auth_algs = ssid->auth_alg;
175 
176 	if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
177 		bss->wpa = ssid->proto;
178 	bss->wpa_key_mgmt = ssid->key_mgmt;
179 	bss->wpa_pairwise = ssid->pairwise_cipher;
180 	if (ssid->psk_set) {
181 		os_free(bss->ssid.wpa_psk);
182 		bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
183 		if (bss->ssid.wpa_psk == NULL)
184 			return -1;
185 		os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
186 		bss->ssid.wpa_psk->group = 1;
187 	} else if (ssid->passphrase) {
188 		bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
189 	} else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
190 		   ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
191 		struct hostapd_wep_keys *wep = &bss->ssid.wep;
192 		int i;
193 		for (i = 0; i < NUM_WEP_KEYS; i++) {
194 			if (ssid->wep_key_len[i] == 0)
195 				continue;
196 			wep->key[i] = os_malloc(ssid->wep_key_len[i]);
197 			if (wep->key[i] == NULL)
198 				return -1;
199 			os_memcpy(wep->key[i], ssid->wep_key[i],
200 				  ssid->wep_key_len[i]);
201 			wep->len[i] = ssid->wep_key_len[i];
202 		}
203 		wep->idx = ssid->wep_tx_keyidx;
204 		wep->keys_set = 1;
205 	}
206 
207 	if (ssid->ap_max_inactivity)
208 		bss->ap_max_inactivity = ssid->ap_max_inactivity;
209 
210 	if (ssid->dtim_period)
211 		bss->dtim_period = ssid->dtim_period;
212 	else if (wpa_s->conf->dtim_period)
213 		bss->dtim_period = wpa_s->conf->dtim_period;
214 
215 	if (ssid->beacon_int)
216 		conf->beacon_int = ssid->beacon_int;
217 	else if (wpa_s->conf->beacon_int)
218 		conf->beacon_int = wpa_s->conf->beacon_int;
219 
220 	if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
221 		bss->rsn_pairwise = bss->wpa_pairwise;
222 	bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
223 						    bss->rsn_pairwise);
224 
225 	if (bss->wpa && bss->ieee802_1x)
226 		bss->ssid.security_policy = SECURITY_WPA;
227 	else if (bss->wpa)
228 		bss->ssid.security_policy = SECURITY_WPA_PSK;
229 	else if (bss->ieee802_1x) {
230 		int cipher = WPA_CIPHER_NONE;
231 		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
232 		bss->ssid.wep.default_len = bss->default_wep_key_len;
233 		if (bss->default_wep_key_len)
234 			cipher = bss->default_wep_key_len >= 13 ?
235 				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
236 		bss->wpa_group = cipher;
237 		bss->wpa_pairwise = cipher;
238 		bss->rsn_pairwise = cipher;
239 	} else if (bss->ssid.wep.keys_set) {
240 		int cipher = WPA_CIPHER_WEP40;
241 		if (bss->ssid.wep.len[0] >= 13)
242 			cipher = WPA_CIPHER_WEP104;
243 		bss->ssid.security_policy = SECURITY_STATIC_WEP;
244 		bss->wpa_group = cipher;
245 		bss->wpa_pairwise = cipher;
246 		bss->rsn_pairwise = cipher;
247 	} else {
248 		bss->ssid.security_policy = SECURITY_PLAINTEXT;
249 		bss->wpa_group = WPA_CIPHER_NONE;
250 		bss->wpa_pairwise = WPA_CIPHER_NONE;
251 		bss->rsn_pairwise = WPA_CIPHER_NONE;
252 	}
253 
254 #ifdef CONFIG_WPS
255 	/*
256 	 * Enable WPS by default for open and WPA/WPA2-Personal network, but
257 	 * require user interaction to actually use it. Only the internal
258 	 * Registrar is supported.
259 	 */
260 	if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
261 	    bss->ssid.security_policy != SECURITY_PLAINTEXT)
262 		goto no_wps;
263 #ifdef CONFIG_WPS2
264 	if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
265 	    (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
266 		goto no_wps; /* WPS2 does not allow WPA/TKIP-only
267 			      * configuration */
268 #endif /* CONFIG_WPS2 */
269 	bss->eap_server = 1;
270 
271 	if (!ssid->ignore_broadcast_ssid)
272 		bss->wps_state = 2;
273 
274 	bss->ap_setup_locked = 2;
275 	if (wpa_s->conf->config_methods)
276 		bss->config_methods = os_strdup(wpa_s->conf->config_methods);
277 	os_memcpy(bss->device_type, wpa_s->conf->device_type,
278 		  WPS_DEV_TYPE_LEN);
279 	if (wpa_s->conf->device_name) {
280 		bss->device_name = os_strdup(wpa_s->conf->device_name);
281 		bss->friendly_name = os_strdup(wpa_s->conf->device_name);
282 	}
283 	if (wpa_s->conf->manufacturer)
284 		bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
285 	if (wpa_s->conf->model_name)
286 		bss->model_name = os_strdup(wpa_s->conf->model_name);
287 	if (wpa_s->conf->model_number)
288 		bss->model_number = os_strdup(wpa_s->conf->model_number);
289 	if (wpa_s->conf->serial_number)
290 		bss->serial_number = os_strdup(wpa_s->conf->serial_number);
291 	if (is_nil_uuid(wpa_s->conf->uuid))
292 		os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
293 	else
294 		os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
295 	os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
296 	bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
297 no_wps:
298 #endif /* CONFIG_WPS */
299 
300 	if (wpa_s->max_stations &&
301 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
302 		bss->max_num_sta = wpa_s->max_stations;
303 	else
304 		bss->max_num_sta = wpa_s->conf->max_num_sta;
305 
306 	bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
307 
308 	if (wpa_s->conf->ap_vendor_elements) {
309 		bss->vendor_elements =
310 			wpabuf_dup(wpa_s->conf->ap_vendor_elements);
311 	}
312 
313 	return 0;
314 }
315 
316 
ap_public_action_rx(void * ctx,const u8 * buf,size_t len,int freq)317 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
318 {
319 #ifdef CONFIG_P2P
320 	struct wpa_supplicant *wpa_s = ctx;
321 	const struct ieee80211_mgmt *mgmt;
322 	size_t hdr_len;
323 
324 	mgmt = (const struct ieee80211_mgmt *) buf;
325 	hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
326 	if (hdr_len > len)
327 		return;
328 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
329 			   mgmt->u.action.category,
330 			   &mgmt->u.action.u.vs_public_action.action,
331 			   len - hdr_len, freq);
332 #endif /* CONFIG_P2P */
333 }
334 
335 
ap_wps_event_cb(void * ctx,enum wps_event event,union wps_event_data * data)336 static void ap_wps_event_cb(void *ctx, enum wps_event event,
337 			    union wps_event_data *data)
338 {
339 #ifdef CONFIG_P2P
340 	struct wpa_supplicant *wpa_s = ctx;
341 
342 	if (event == WPS_EV_FAIL) {
343 		struct wps_event_fail *fail = &data->fail;
344 
345 		if (wpa_s->parent && wpa_s->parent != wpa_s &&
346 		    wpa_s == wpa_s->global->p2p_group_formation) {
347 			/*
348 			 * src/ap/wps_hostapd.c has already sent this on the
349 			 * main interface, so only send on the parent interface
350 			 * here if needed.
351 			 */
352 			wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
353 				"msg=%d config_error=%d",
354 				fail->msg, fail->config_error);
355 		}
356 		wpas_p2p_wps_failed(wpa_s, fail);
357 	}
358 #endif /* CONFIG_P2P */
359 }
360 
361 
ap_sta_authorized_cb(void * ctx,const u8 * mac_addr,int authorized,const u8 * p2p_dev_addr)362 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
363 				 int authorized, const u8 *p2p_dev_addr)
364 {
365 	wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
366 }
367 
368 
ap_vendor_action_rx(void * ctx,const u8 * buf,size_t len,int freq)369 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
370 {
371 #ifdef CONFIG_P2P
372 	struct wpa_supplicant *wpa_s = ctx;
373 	const struct ieee80211_mgmt *mgmt;
374 	size_t hdr_len;
375 
376 	mgmt = (const struct ieee80211_mgmt *) buf;
377 	hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
378 	if (hdr_len > len)
379 		return -1;
380 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
381 			   mgmt->u.action.category,
382 			   &mgmt->u.action.u.vs_public_action.action,
383 			   len - hdr_len, freq);
384 #endif /* CONFIG_P2P */
385 	return 0;
386 }
387 
388 
ap_probe_req_rx(void * ctx,const u8 * sa,const u8 * da,const u8 * bssid,const u8 * ie,size_t ie_len,int ssi_signal)389 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
390 			   const u8 *bssid, const u8 *ie, size_t ie_len,
391 			   int ssi_signal)
392 {
393 #ifdef CONFIG_P2P
394 	struct wpa_supplicant *wpa_s = ctx;
395 	return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
396 				     ssi_signal);
397 #else /* CONFIG_P2P */
398 	return 0;
399 #endif /* CONFIG_P2P */
400 }
401 
402 
ap_wps_reg_success_cb(void * ctx,const u8 * mac_addr,const u8 * uuid_e)403 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
404 				  const u8 *uuid_e)
405 {
406 #ifdef CONFIG_P2P
407 	struct wpa_supplicant *wpa_s = ctx;
408 	wpas_p2p_wps_success(wpa_s, mac_addr, 1);
409 #endif /* CONFIG_P2P */
410 }
411 
412 
wpas_ap_configured_cb(void * ctx)413 static void wpas_ap_configured_cb(void *ctx)
414 {
415 	struct wpa_supplicant *wpa_s = ctx;
416 
417 	wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
418 
419 	if (wpa_s->ap_configured_cb)
420 		wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
421 					wpa_s->ap_configured_cb_data);
422 }
423 
424 
wpa_supplicant_create_ap(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)425 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
426 			     struct wpa_ssid *ssid)
427 {
428 	struct wpa_driver_associate_params params;
429 	struct hostapd_iface *hapd_iface;
430 	struct hostapd_config *conf;
431 	size_t i;
432 
433 	if (ssid->ssid == NULL || ssid->ssid_len == 0) {
434 		wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
435 		return -1;
436 	}
437 
438 	wpa_supplicant_ap_deinit(wpa_s);
439 
440 	wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
441 		   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
442 
443 	os_memset(&params, 0, sizeof(params));
444 	params.ssid = ssid->ssid;
445 	params.ssid_len = ssid->ssid_len;
446 	switch (ssid->mode) {
447 	case WPAS_MODE_INFRA:
448 		params.mode = IEEE80211_MODE_INFRA;
449 		break;
450 	case WPAS_MODE_IBSS:
451 		params.mode = IEEE80211_MODE_IBSS;
452 		break;
453 	case WPAS_MODE_AP:
454 	case WPAS_MODE_P2P_GO:
455 	case WPAS_MODE_P2P_GROUP_FORMATION:
456 		params.mode = IEEE80211_MODE_AP;
457 		break;
458 	}
459 	params.freq = ssid->frequency;
460 
461 	params.wpa_proto = ssid->proto;
462 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
463 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
464 	else
465 		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
466 	params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
467 
468 	wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
469 							  1);
470 	if (wpa_s->pairwise_cipher < 0) {
471 		wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
472 			   "cipher.");
473 		return -1;
474 	}
475 	params.pairwise_suite =
476 		wpa_cipher_to_suite_driver(wpa_s->pairwise_cipher);
477 	params.group_suite = params.pairwise_suite;
478 
479 #ifdef CONFIG_P2P
480 	if (ssid->mode == WPAS_MODE_P2P_GO ||
481 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
482 		params.p2p = 1;
483 #endif /* CONFIG_P2P */
484 
485 	if (wpa_s->parent->set_ap_uapsd)
486 		params.uapsd = wpa_s->parent->ap_uapsd;
487 	else
488 		params.uapsd = -1;
489 
490 	if (wpa_drv_associate(wpa_s, &params) < 0) {
491 		wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
492 		return -1;
493 	}
494 
495 	wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
496 	if (hapd_iface == NULL)
497 		return -1;
498 	hapd_iface->owner = wpa_s;
499 	hapd_iface->drv_flags = wpa_s->drv_flags;
500 	hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
501 	hapd_iface->extended_capa = wpa_s->extended_capa;
502 	hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
503 	hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
504 
505 	wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
506 	if (conf == NULL) {
507 		wpa_supplicant_ap_deinit(wpa_s);
508 		return -1;
509 	}
510 
511 	os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
512 		  wpa_s->conf->wmm_ac_params,
513 		  sizeof(wpa_s->conf->wmm_ac_params));
514 
515 	if (params.uapsd > 0) {
516 		conf->bss->wmm_enabled = 1;
517 		conf->bss->wmm_uapsd = 1;
518 	}
519 
520 	if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
521 		wpa_printf(MSG_ERROR, "Failed to create AP configuration");
522 		wpa_supplicant_ap_deinit(wpa_s);
523 		return -1;
524 	}
525 
526 #ifdef CONFIG_P2P
527 	if (ssid->mode == WPAS_MODE_P2P_GO)
528 		conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
529 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
530 		conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
531 			P2P_GROUP_FORMATION;
532 #endif /* CONFIG_P2P */
533 
534 	hapd_iface->num_bss = conf->num_bss;
535 	hapd_iface->bss = os_calloc(conf->num_bss,
536 				    sizeof(struct hostapd_data *));
537 	if (hapd_iface->bss == NULL) {
538 		wpa_supplicant_ap_deinit(wpa_s);
539 		return -1;
540 	}
541 
542 	for (i = 0; i < conf->num_bss; i++) {
543 		hapd_iface->bss[i] =
544 			hostapd_alloc_bss_data(hapd_iface, conf,
545 					       &conf->bss[i]);
546 		if (hapd_iface->bss[i] == NULL) {
547 			wpa_supplicant_ap_deinit(wpa_s);
548 			return -1;
549 		}
550 
551 		hapd_iface->bss[i]->msg_ctx = wpa_s;
552 		hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
553 		hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
554 		hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
555 		hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
556 		hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
557 		hostapd_register_probereq_cb(hapd_iface->bss[i],
558 					     ap_probe_req_rx, wpa_s);
559 		hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
560 		hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
561 		hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
562 		hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
563 		hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
564 		hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
565 #ifdef CONFIG_P2P
566 		hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
567 		hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
568 								    ssid);
569 #endif /* CONFIG_P2P */
570 		hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
571 		hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
572 	}
573 
574 	os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
575 	hapd_iface->bss[0]->driver = wpa_s->driver;
576 	hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
577 
578 	wpa_s->current_ssid = ssid;
579 	os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
580 	wpa_s->assoc_freq = ssid->frequency;
581 
582 	if (hostapd_setup_interface(wpa_s->ap_iface)) {
583 		wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
584 		wpa_supplicant_ap_deinit(wpa_s);
585 		return -1;
586 	}
587 
588 	return 0;
589 }
590 
591 
wpa_supplicant_ap_deinit(struct wpa_supplicant * wpa_s)592 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
593 {
594 #ifdef CONFIG_WPS
595 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
596 #endif /* CONFIG_WPS */
597 
598 	if (wpa_s->ap_iface == NULL)
599 		return;
600 
601 	wpa_s->current_ssid = NULL;
602 	wpa_s->assoc_freq = 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 
ap_tx_status(void * ctx,const u8 * addr,const u8 * buf,size_t len,int ack)615 void 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 
ap_eapol_tx_status(void * ctx,const u8 * dst,const u8 * data,size_t len,int ack)625 void 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 
ap_client_poll_ok(void * ctx,const u8 * addr)635 void 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 
ap_rx_from_unknown_sta(void * ctx,const u8 * addr,int wds)645 void 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 
ap_mgmt_rx(void * ctx,struct rx_mgmt * rx_mgmt)654 void 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 
ap_mgmt_tx_cb(void * ctx,const u8 * buf,size_t len,u16 stype,int ok)668 void 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 
wpa_supplicant_ap_rx_eapol(struct wpa_supplicant * wpa_s,const u8 * src_addr,const u8 * buf,size_t len)677 void 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 
wpa_supplicant_ap_wps_pbc(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * p2p_dev_addr)686 int 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 
wpa_supplicant_ap_wps_cancel(struct wpa_supplicant * wpa_s)696 int 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 
wpa_supplicant_ap_wps_pin(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,char * buf,size_t buflen,int timeout)726 int 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 
wpas_wps_ap_pin_timeout(void * eloop_data,void * user_ctx)750 static 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 
wpas_wps_ap_pin_enable(struct wpa_supplicant * wpa_s,int timeout)758 static 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 
wpas_wps_ap_pin_disable(struct wpa_supplicant * wpa_s)774 void 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 
wpas_wps_ap_pin_random(struct wpa_supplicant * wpa_s,int timeout)788 const 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 
wpas_wps_ap_pin_get(struct wpa_supplicant * wpa_s)809 const 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 
wpas_wps_ap_pin_set(struct wpa_supplicant * wpa_s,const char * pin,int timeout)819 int 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 
wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant * wpa_s)842 void 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 
867 #ifdef CONFIG_WPS_NFC
868 
wpas_ap_wps_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef)869 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
870 					     int ndef)
871 {
872 	struct hostapd_data *hapd;
873 
874 	if (wpa_s->ap_iface == NULL)
875 		return NULL;
876 	hapd = wpa_s->ap_iface->bss[0];
877 	return hostapd_wps_nfc_config_token(hapd, ndef);
878 }
879 
880 
wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef)881 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
882 					     int ndef)
883 {
884 	struct hostapd_data *hapd;
885 
886 	if (wpa_s->ap_iface == NULL)
887 		return NULL;
888 	hapd = wpa_s->ap_iface->bss[0];
889 	return hostapd_wps_nfc_hs_cr(hapd, ndef);
890 }
891 
892 #endif /* CONFIG_WPS_NFC */
893 
894 #endif /* CONFIG_WPS */
895 
896 
897 #ifdef CONFIG_CTRL_IFACE
898 
ap_ctrl_iface_sta_first(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)899 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
900 			    char *buf, size_t buflen)
901 {
902 	if (wpa_s->ap_iface == NULL)
903 		return -1;
904 	return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
905 					    buf, buflen);
906 }
907 
908 
ap_ctrl_iface_sta(struct wpa_supplicant * wpa_s,const char * txtaddr,char * buf,size_t buflen)909 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
910 		      char *buf, size_t buflen)
911 {
912 	if (wpa_s->ap_iface == NULL)
913 		return -1;
914 	return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
915 				      buf, buflen);
916 }
917 
918 
ap_ctrl_iface_sta_next(struct wpa_supplicant * wpa_s,const char * txtaddr,char * buf,size_t buflen)919 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
920 			   char *buf, size_t buflen)
921 {
922 	if (wpa_s->ap_iface == NULL)
923 		return -1;
924 	return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
925 					   buf, buflen);
926 }
927 
928 
ap_ctrl_iface_sta_disassociate(struct wpa_supplicant * wpa_s,const char * txtaddr)929 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
930 				   const char *txtaddr)
931 {
932 	if (wpa_s->ap_iface == NULL)
933 		return -1;
934 	return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
935 					       txtaddr);
936 }
937 
938 
ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant * wpa_s,const char * txtaddr)939 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
940 				     const char *txtaddr)
941 {
942 	if (wpa_s->ap_iface == NULL)
943 		return -1;
944 	return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
945 						 txtaddr);
946 }
947 
948 
ap_ctrl_iface_wpa_get_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen,int verbose)949 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
950 				 size_t buflen, int verbose)
951 {
952 	char *pos = buf, *end = buf + buflen;
953 	int ret;
954 	struct hostapd_bss_config *conf;
955 
956 	if (wpa_s->ap_iface == NULL)
957 		return -1;
958 
959 	conf = wpa_s->ap_iface->bss[0]->conf;
960 	if (conf->wpa == 0)
961 		return 0;
962 
963 	ret = os_snprintf(pos, end - pos,
964 			  "pairwise_cipher=%s\n"
965 			  "group_cipher=%s\n"
966 			  "key_mgmt=%s\n",
967 			  wpa_cipher_txt(conf->rsn_pairwise),
968 			  wpa_cipher_txt(conf->wpa_group),
969 			  wpa_key_mgmt_txt(conf->wpa_key_mgmt,
970 					   conf->wpa));
971 	if (ret < 0 || ret >= end - pos)
972 		return pos - buf;
973 	pos += ret;
974 	return pos - buf;
975 }
976 
977 #endif /* CONFIG_CTRL_IFACE */
978 
979 
wpa_supplicant_ap_update_beacon(struct wpa_supplicant * wpa_s)980 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
981 {
982 	struct hostapd_iface *iface = wpa_s->ap_iface;
983 	struct wpa_ssid *ssid = wpa_s->current_ssid;
984 	struct hostapd_data *hapd;
985 
986 	if (ssid == NULL || wpa_s->ap_iface == NULL ||
987 	    ssid->mode == WPAS_MODE_INFRA ||
988 	    ssid->mode == WPAS_MODE_IBSS)
989 		return -1;
990 
991 #ifdef CONFIG_P2P
992 	if (ssid->mode == WPAS_MODE_P2P_GO)
993 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
994 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
995 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
996 			P2P_GROUP_FORMATION;
997 #endif /* CONFIG_P2P */
998 
999 	hapd = iface->bss[0];
1000 	if (hapd->drv_priv == NULL)
1001 		return -1;
1002 	ieee802_11_set_beacons(iface);
1003 	hostapd_set_ap_wps_ie(hapd);
1004 
1005 	return 0;
1006 }
1007 
1008 
wpas_ap_ch_switch(struct wpa_supplicant * wpa_s,int freq,int ht,int offset)1009 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1010 		       int offset)
1011 {
1012 	if (!wpa_s->ap_iface)
1013 		return;
1014 
1015 	wpa_s->assoc_freq = freq;
1016 	hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
1017 }
1018 
1019 
wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant * wpa_s,const u8 * addr)1020 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1021 				      const u8 *addr)
1022 {
1023 	struct hostapd_data *hapd;
1024 	struct hostapd_bss_config *conf;
1025 
1026 	if (!wpa_s->ap_iface)
1027 		return -1;
1028 
1029 	if (addr)
1030 		wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1031 			   MAC2STR(addr));
1032 	else
1033 		wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1034 
1035 	hapd = wpa_s->ap_iface->bss[0];
1036 	conf = hapd->conf;
1037 
1038 	os_free(conf->accept_mac);
1039 	conf->accept_mac = NULL;
1040 	conf->num_accept_mac = 0;
1041 	os_free(conf->deny_mac);
1042 	conf->deny_mac = NULL;
1043 	conf->num_deny_mac = 0;
1044 
1045 	if (addr == NULL) {
1046 		conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1047 		return 0;
1048 	}
1049 
1050 	conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1051 	conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1052 	if (conf->accept_mac == NULL)
1053 		return -1;
1054 	os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1055 	conf->num_accept_mac = 1;
1056 
1057 	return 0;
1058 }
1059