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