Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant - Basic AP mode support routines
      3  * Copyright (c) 2003-2009, Jouni Malinen <j (at) 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 {
     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->passphrase) {
    178 		bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
    179 	} else if (ssid->psk_set) {
    180 		os_free(bss->ssid.wpa_psk);
    181 		bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
    182 		if (bss->ssid.wpa_psk == NULL)
    183 			return -1;
    184 		os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
    185 		bss->ssid.wpa_psk->group = 1;
    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;
    299 no_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 
    314 static 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 
    333 static 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 
    359 static 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 
    366 static 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 
    386 static 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 
    400 static 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 
    410 static 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 
    422 int 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 
    591 void 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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 {
    729 	int ret, ret_len = 0;
    730 
    731 	if (!wpa_s->ap_iface)
    732 		return -1;
    733 
    734 	if (pin == NULL) {
    735 		unsigned int rpin = wps_generate_pin();
    736 		ret_len = os_snprintf(buf, buflen, "%08d", rpin);
    737 		pin = buf;
    738 	} else
    739 		ret_len = os_snprintf(buf, buflen, "%s", pin);
    740 
    741 	ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
    742 				  0);
    743 	if (ret)
    744 		return -1;
    745 	return ret_len;
    746 }
    747 
    748 
    749 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
    750 {
    751 	struct wpa_supplicant *wpa_s = eloop_data;
    752 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
    753 	wpas_wps_ap_pin_disable(wpa_s);
    754 }
    755 
    756 
    757 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
    758 {
    759 	struct hostapd_data *hapd;
    760 
    761 	if (wpa_s->ap_iface == NULL)
    762 		return;
    763 	hapd = wpa_s->ap_iface->bss[0];
    764 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
    765 	hapd->ap_pin_failures = 0;
    766 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
    767 	if (timeout > 0)
    768 		eloop_register_timeout(timeout, 0,
    769 				       wpas_wps_ap_pin_timeout, wpa_s, NULL);
    770 }
    771 
    772 
    773 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
    774 {
    775 	struct hostapd_data *hapd;
    776 
    777 	if (wpa_s->ap_iface == NULL)
    778 		return;
    779 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
    780 	hapd = wpa_s->ap_iface->bss[0];
    781 	os_free(hapd->conf->ap_pin);
    782 	hapd->conf->ap_pin = NULL;
    783 	eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
    784 }
    785 
    786 
    787 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
    788 {
    789 	struct hostapd_data *hapd;
    790 	unsigned int pin;
    791 	char pin_txt[9];
    792 
    793 	if (wpa_s->ap_iface == NULL)
    794 		return NULL;
    795 	hapd = wpa_s->ap_iface->bss[0];
    796 	pin = wps_generate_pin();
    797 	os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
    798 	os_free(hapd->conf->ap_pin);
    799 	hapd->conf->ap_pin = os_strdup(pin_txt);
    800 	if (hapd->conf->ap_pin == NULL)
    801 		return NULL;
    802 	wpas_wps_ap_pin_enable(wpa_s, timeout);
    803 
    804 	return hapd->conf->ap_pin;
    805 }
    806 
    807 
    808 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
    809 {
    810 	struct hostapd_data *hapd;
    811 	if (wpa_s->ap_iface == NULL)
    812 		return NULL;
    813 	hapd = wpa_s->ap_iface->bss[0];
    814 	return hapd->conf->ap_pin;
    815 }
    816 
    817 
    818 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
    819 			int timeout)
    820 {
    821 	struct hostapd_data *hapd;
    822 	char pin_txt[9];
    823 	int ret;
    824 
    825 	if (wpa_s->ap_iface == NULL)
    826 		return -1;
    827 	hapd = wpa_s->ap_iface->bss[0];
    828 	ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
    829 	if (ret < 0 || ret >= (int) sizeof(pin_txt))
    830 		return -1;
    831 	os_free(hapd->conf->ap_pin);
    832 	hapd->conf->ap_pin = os_strdup(pin_txt);
    833 	if (hapd->conf->ap_pin == NULL)
    834 		return -1;
    835 	wpas_wps_ap_pin_enable(wpa_s, timeout);
    836 
    837 	return 0;
    838 }
    839 
    840 
    841 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
    842 {
    843 	struct hostapd_data *hapd;
    844 
    845 	if (wpa_s->ap_iface == NULL)
    846 		return;
    847 	hapd = wpa_s->ap_iface->bss[0];
    848 
    849 	/*
    850 	 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
    851 	 * PIN if this happens multiple times to slow down brute force attacks.
    852 	 */
    853 	hapd->ap_pin_failures++;
    854 	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
    855 		   hapd->ap_pin_failures);
    856 	if (hapd->ap_pin_failures < 3)
    857 		return;
    858 
    859 	wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
    860 	hapd->ap_pin_failures = 0;
    861 	os_free(hapd->conf->ap_pin);
    862 	hapd->conf->ap_pin = NULL;
    863 }
    864 
    865 #endif /* CONFIG_WPS */
    866 
    867 
    868 #ifdef CONFIG_CTRL_IFACE
    869 
    870 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
    871 			    char *buf, size_t buflen)
    872 {
    873 	if (wpa_s->ap_iface == NULL)
    874 		return -1;
    875 	return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
    876 					    buf, buflen);
    877 }
    878 
    879 
    880 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
    881 		      char *buf, size_t buflen)
    882 {
    883 	if (wpa_s->ap_iface == NULL)
    884 		return -1;
    885 	return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
    886 				      buf, buflen);
    887 }
    888 
    889 
    890 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
    891 			   char *buf, size_t buflen)
    892 {
    893 	if (wpa_s->ap_iface == NULL)
    894 		return -1;
    895 	return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
    896 					   buf, buflen);
    897 }
    898 
    899 
    900 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
    901 				   const char *txtaddr)
    902 {
    903 	if (wpa_s->ap_iface == NULL)
    904 		return -1;
    905 	return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
    906 					       txtaddr);
    907 }
    908 
    909 
    910 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
    911 				     const char *txtaddr)
    912 {
    913 	if (wpa_s->ap_iface == NULL)
    914 		return -1;
    915 	return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
    916 						 txtaddr);
    917 }
    918 
    919 
    920 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
    921 				 size_t buflen, int verbose)
    922 {
    923 	char *pos = buf, *end = buf + buflen;
    924 	int ret;
    925 	struct hostapd_bss_config *conf;
    926 
    927 	if (wpa_s->ap_iface == NULL)
    928 		return -1;
    929 
    930 	conf = wpa_s->ap_iface->bss[0]->conf;
    931 	if (conf->wpa == 0)
    932 		return 0;
    933 
    934 	ret = os_snprintf(pos, end - pos,
    935 			  "pairwise_cipher=%s\n"
    936 			  "group_cipher=%s\n"
    937 			  "key_mgmt=%s\n",
    938 			  wpa_cipher_txt(conf->rsn_pairwise),
    939 			  wpa_cipher_txt(conf->wpa_group),
    940 			  wpa_key_mgmt_txt(conf->wpa_key_mgmt,
    941 					   conf->wpa));
    942 	if (ret < 0 || ret >= end - pos)
    943 		return pos - buf;
    944 	pos += ret;
    945 	return pos - buf;
    946 }
    947 
    948 #endif /* CONFIG_CTRL_IFACE */
    949 
    950 
    951 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
    952 {
    953 	struct hostapd_iface *iface = wpa_s->ap_iface;
    954 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    955 	struct hostapd_data *hapd;
    956 
    957 	if (ssid == NULL || wpa_s->ap_iface == NULL ||
    958 	    ssid->mode == WPAS_MODE_INFRA ||
    959 	    ssid->mode == WPAS_MODE_IBSS)
    960 		return -1;
    961 
    962 #ifdef CONFIG_P2P
    963 	if (ssid->mode == WPAS_MODE_P2P_GO)
    964 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
    965 	else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
    966 		iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
    967 			P2P_GROUP_FORMATION;
    968 #endif /* CONFIG_P2P */
    969 
    970 	hapd = iface->bss[0];
    971 	if (hapd->drv_priv == NULL)
    972 		return -1;
    973 	ieee802_11_set_beacons(iface);
    974 	hostapd_set_ap_wps_ie(hapd);
    975 
    976 	return 0;
    977 }
    978 
    979 
    980 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
    981 		       int offset)
    982 {
    983 	if (!wpa_s->ap_iface)
    984 		return;
    985 
    986 	wpa_s->assoc_freq = freq;
    987 	hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
    988 }
    989 
    990 
    991 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
    992 				      const u8 *addr)
    993 {
    994 	struct hostapd_data *hapd;
    995 	struct hostapd_bss_config *conf;
    996 
    997 	if (!wpa_s->ap_iface)
    998 		return -1;
    999 
   1000 	if (addr)
   1001 		wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
   1002 			   MAC2STR(addr));
   1003 	else
   1004 		wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
   1005 
   1006 	hapd = wpa_s->ap_iface->bss[0];
   1007 	conf = hapd->conf;
   1008 
   1009 	os_free(conf->accept_mac);
   1010 	conf->accept_mac = NULL;
   1011 	conf->num_accept_mac = 0;
   1012 	os_free(conf->deny_mac);
   1013 	conf->deny_mac = NULL;
   1014 	conf->num_deny_mac = 0;
   1015 
   1016 	if (addr == NULL) {
   1017 		conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
   1018 		return 0;
   1019 	}
   1020 
   1021 	conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
   1022 	conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
   1023 	if (conf->accept_mac == NULL)
   1024 		return -1;
   1025 	os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
   1026 	conf->num_accept_mac = 1;
   1027 
   1028 	return 0;
   1029 }
   1030