Home | History | Annotate | Download | only in ap
      1 /*
      2  * hostapd / WPS integration
      3  * Copyright (c) 2008-2012, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "utils/includes.h"
     10 
     11 #include "utils/common.h"
     12 #include "utils/eloop.h"
     13 #include "utils/uuid.h"
     14 #include "common/wpa_ctrl.h"
     15 #include "common/ieee802_11_defs.h"
     16 #include "common/ieee802_11_common.h"
     17 #include "eapol_auth/eapol_auth_sm.h"
     18 #include "eapol_auth/eapol_auth_sm_i.h"
     19 #include "wps/wps.h"
     20 #include "wps/wps_defs.h"
     21 #include "wps/wps_dev_attr.h"
     22 #include "wps/wps_attr_parse.h"
     23 #include "hostapd.h"
     24 #include "ap_config.h"
     25 #include "ap_drv_ops.h"
     26 #include "beacon.h"
     27 #include "sta_info.h"
     28 #include "wps_hostapd.h"
     29 
     30 
     31 #ifdef CONFIG_WPS_UPNP
     32 #include "wps/wps_upnp.h"
     33 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
     34 				 struct wps_context *wps);
     35 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
     36 #endif /* CONFIG_WPS_UPNP */
     37 
     38 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
     39 				    const u8 *bssid,
     40 				    const u8 *ie, size_t ie_len,
     41 				    int ssi_signal);
     42 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
     43 static void hostapd_wps_nfc_clear(struct wps_context *wps);
     44 
     45 
     46 struct wps_for_each_data {
     47 	int (*func)(struct hostapd_data *h, void *ctx);
     48 	void *ctx;
     49 	struct hostapd_data *calling_hapd;
     50 };
     51 
     52 
     53 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
     54 {
     55 	struct wps_for_each_data *data = ctx;
     56 	size_t j;
     57 
     58 	if (iface == NULL)
     59 		return 0;
     60 	for (j = 0; j < iface->num_bss; j++) {
     61 		struct hostapd_data *hapd = iface->bss[j];
     62 		int ret;
     63 
     64 		if (hapd != data->calling_hapd &&
     65 		    (hapd->conf->wps_independent ||
     66 		     data->calling_hapd->conf->wps_independent))
     67 			continue;
     68 
     69 		ret = data->func(hapd, data->ctx);
     70 		if (ret)
     71 			return ret;
     72 	}
     73 
     74 	return 0;
     75 }
     76 
     77 
     78 static int hostapd_wps_for_each(struct hostapd_data *hapd,
     79 				int (*func)(struct hostapd_data *h, void *ctx),
     80 				void *ctx)
     81 {
     82 	struct hostapd_iface *iface = hapd->iface;
     83 	struct wps_for_each_data data;
     84 	data.func = func;
     85 	data.ctx = ctx;
     86 	data.calling_hapd = hapd;
     87 	if (iface->interfaces == NULL ||
     88 	    iface->interfaces->for_each_interface == NULL)
     89 		return wps_for_each(iface, &data);
     90 	return iface->interfaces->for_each_interface(iface->interfaces,
     91 						     wps_for_each, &data);
     92 }
     93 
     94 
     95 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
     96 				  const u8 *p2p_dev_addr, const u8 *psk,
     97 				  size_t psk_len)
     98 {
     99 	struct hostapd_data *hapd = ctx;
    100 	struct hostapd_wpa_psk *p;
    101 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
    102 
    103 	if (is_zero_ether_addr(p2p_dev_addr)) {
    104 		wpa_printf(MSG_DEBUG,
    105 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
    106 			   MAC2STR(mac_addr));
    107 	} else {
    108 		wpa_printf(MSG_DEBUG,
    109 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
    110 			   " P2P Device Addr " MACSTR,
    111 			   MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
    112 	}
    113 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
    114 
    115 	if (psk_len != PMK_LEN) {
    116 		wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
    117 			   (unsigned long) psk_len);
    118 		return -1;
    119 	}
    120 
    121 	/* Add the new PSK to runtime PSK list */
    122 	p = os_zalloc(sizeof(*p));
    123 	if (p == NULL)
    124 		return -1;
    125 	os_memcpy(p->addr, mac_addr, ETH_ALEN);
    126 	os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
    127 	os_memcpy(p->psk, psk, PMK_LEN);
    128 
    129 	if (hapd->new_psk_cb) {
    130 		hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
    131 				 psk, psk_len);
    132 	}
    133 
    134 	p->next = ssid->wpa_psk;
    135 	ssid->wpa_psk = p;
    136 
    137 	if (ssid->wpa_psk_file) {
    138 		FILE *f;
    139 		char hex[PMK_LEN * 2 + 1];
    140 		/* Add the new PSK to PSK list file */
    141 		f = fopen(ssid->wpa_psk_file, "a");
    142 		if (f == NULL) {
    143 			wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
    144 				   "'%s'", ssid->wpa_psk_file);
    145 			return -1;
    146 		}
    147 
    148 		wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
    149 		fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
    150 		fclose(f);
    151 	}
    152 
    153 	return 0;
    154 }
    155 
    156 
    157 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
    158 				 struct wpabuf *probe_resp_ie)
    159 {
    160 	struct hostapd_data *hapd = ctx;
    161 	wpabuf_free(hapd->wps_beacon_ie);
    162 	hapd->wps_beacon_ie = beacon_ie;
    163 	wpabuf_free(hapd->wps_probe_resp_ie);
    164 	hapd->wps_probe_resp_ie = probe_resp_ie;
    165 	if (hapd->beacon_set_done)
    166 		ieee802_11_set_beacon(hapd);
    167 	return hostapd_set_ap_wps_ie(hapd);
    168 }
    169 
    170 
    171 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
    172 				      const struct wps_device_data *dev)
    173 {
    174 	struct hostapd_data *hapd = ctx;
    175 	char uuid[40], txt[400];
    176 	int len;
    177 	char devtype[WPS_DEV_TYPE_BUFSIZE];
    178 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
    179 		return;
    180 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
    181 	len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
    182 			  "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
    183 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
    184 			  dev->manufacturer, dev->model_name,
    185 			  dev->model_number, dev->serial_number,
    186 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
    187 					       sizeof(devtype)));
    188 	if (!os_snprintf_error(sizeof(txt), len))
    189 		wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
    190 
    191 	if (hapd->conf->wps_pin_requests) {
    192 		FILE *f;
    193 		struct os_time t;
    194 		f = fopen(hapd->conf->wps_pin_requests, "a");
    195 		if (f == NULL)
    196 			return;
    197 		os_get_time(&t);
    198 		fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
    199 			"\t%s\n",
    200 			t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
    201 			dev->manufacturer, dev->model_name, dev->model_number,
    202 			dev->serial_number,
    203 			wps_dev_type_bin2str(dev->pri_dev_type, devtype,
    204 					     sizeof(devtype)));
    205 		fclose(f);
    206 	}
    207 }
    208 
    209 
    210 struct wps_stop_reg_data {
    211 	struct hostapd_data *current_hapd;
    212 	const u8 *uuid_e;
    213 	const u8 *dev_pw;
    214 	size_t dev_pw_len;
    215 };
    216 
    217 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
    218 {
    219 	struct wps_stop_reg_data *data = ctx;
    220 	if (hapd != data->current_hapd && hapd->wps != NULL)
    221 		wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
    222 				       data->dev_pw, data->dev_pw_len);
    223 	return 0;
    224 }
    225 
    226 
    227 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
    228 				       const u8 *uuid_e, const u8 *dev_pw,
    229 				       size_t dev_pw_len)
    230 {
    231 	struct hostapd_data *hapd = ctx;
    232 	char uuid[40];
    233 	struct wps_stop_reg_data data;
    234 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
    235 		return;
    236 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
    237 		MAC2STR(mac_addr), uuid);
    238 	if (hapd->wps_reg_success_cb)
    239 		hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
    240 					 mac_addr, uuid_e);
    241 	data.current_hapd = hapd;
    242 	data.uuid_e = uuid_e;
    243 	data.dev_pw = dev_pw;
    244 	data.dev_pw_len = dev_pw_len;
    245 	hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
    246 }
    247 
    248 
    249 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
    250 					 const u8 *uuid_e,
    251 					 const u8 *pri_dev_type,
    252 					 u16 config_methods,
    253 					 u16 dev_password_id, u8 request_type,
    254 					 const char *dev_name)
    255 {
    256 	struct hostapd_data *hapd = ctx;
    257 	char uuid[40];
    258 	char devtype[WPS_DEV_TYPE_BUFSIZE];
    259 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
    260 		return;
    261 	if (dev_name == NULL)
    262 		dev_name = "";
    263 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
    264 		     " %s %s 0x%x %u %u [%s]",
    265 		     MAC2STR(addr), uuid,
    266 		     wps_dev_type_bin2str(pri_dev_type, devtype,
    267 					  sizeof(devtype)),
    268 		     config_methods, dev_password_id, request_type, dev_name);
    269 }
    270 
    271 
    272 static int str_starts(const char *str, const char *start)
    273 {
    274 	return os_strncmp(str, start, os_strlen(start)) == 0;
    275 }
    276 
    277 
    278 static void wps_reload_config(void *eloop_data, void *user_ctx)
    279 {
    280 	struct hostapd_iface *iface = eloop_data;
    281 
    282 	wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
    283 	if (iface->interfaces == NULL ||
    284 	    iface->interfaces->reload_config(iface) < 0) {
    285 		wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
    286 			   "configuration");
    287 	}
    288 }
    289 
    290 
    291 void hostapd_wps_eap_completed(struct hostapd_data *hapd)
    292 {
    293 	/*
    294 	 * Reduce race condition of the station trying to reconnect immediately
    295 	 * after AP reconfiguration through WPS by rescheduling the reload
    296 	 * timeout to happen after EAP completion rather than the originally
    297 	 * scheduled 100 ms after new configuration became known.
    298 	 */
    299 	if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
    300 	    1)
    301 		wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
    302 }
    303 
    304 
    305 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
    306 			      size_t attr_len)
    307 {
    308 	size_t blen = attr_len * 2 + 1;
    309 	char *buf = os_malloc(blen);
    310 	if (buf) {
    311 		wpa_snprintf_hex(buf, blen, attr, attr_len);
    312 		wpa_msg(hapd->msg_ctx, MSG_INFO,
    313 			WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
    314 		os_free(buf);
    315 	}
    316 }
    317 
    318 
    319 static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
    320 				       const struct wps_credential *cred)
    321 {
    322 	struct hostapd_bss_config *bss = hapd->conf;
    323 
    324 	wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
    325 
    326 	bss->wps_state = 2;
    327 	if (cred->ssid_len <= SSID_MAX_LEN) {
    328 		os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
    329 		bss->ssid.ssid_len = cred->ssid_len;
    330 		bss->ssid.ssid_set = 1;
    331 	}
    332 
    333 	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
    334 	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
    335 		bss->wpa = 3;
    336 	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
    337 		bss->wpa = 2;
    338 	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
    339 		bss->wpa = 1;
    340 	else
    341 		bss->wpa = 0;
    342 
    343 	if (bss->wpa) {
    344 		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
    345 			bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
    346 		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
    347 			bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
    348 
    349 		bss->wpa_pairwise = 0;
    350 		if (cred->encr_type & WPS_ENCR_AES) {
    351 			if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
    352 				bss->wpa_pairwise |= WPA_CIPHER_GCMP;
    353 			else
    354 				bss->wpa_pairwise |= WPA_CIPHER_CCMP;
    355 		}
    356 		if (cred->encr_type & WPS_ENCR_TKIP)
    357 			bss->wpa_pairwise |= WPA_CIPHER_TKIP;
    358 		bss->rsn_pairwise = bss->wpa_pairwise;
    359 		bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
    360 							    bss->wpa_pairwise,
    361 							    bss->rsn_pairwise);
    362 
    363 		if (cred->key_len >= 8 && cred->key_len < 64) {
    364 			os_free(bss->ssid.wpa_passphrase);
    365 			bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
    366 			if (bss->ssid.wpa_passphrase)
    367 				os_memcpy(bss->ssid.wpa_passphrase, cred->key,
    368 					  cred->key_len);
    369 			hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
    370 		} else if (cred->key_len == 64) {
    371 			hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
    372 			bss->ssid.wpa_psk =
    373 				os_zalloc(sizeof(struct hostapd_wpa_psk));
    374 			if (bss->ssid.wpa_psk &&
    375 			    hexstr2bin((const char *) cred->key,
    376 				       bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
    377 				bss->ssid.wpa_psk->group = 1;
    378 				os_free(bss->ssid.wpa_passphrase);
    379 				bss->ssid.wpa_passphrase = NULL;
    380 			}
    381 		}
    382 		bss->auth_algs = 1;
    383 	} else {
    384 		/*
    385 		 * WPS 2.0 does not allow WEP to be configured, so no need to
    386 		 * process that option here either.
    387 		 */
    388 		bss->auth_algs = 1;
    389 	}
    390 
    391 	/* Schedule configuration reload after short period of time to allow
    392 	 * EAP-WSC to be finished.
    393 	 */
    394 	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
    395 			       NULL);
    396 
    397 	return 0;
    398 }
    399 
    400 
    401 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
    402 {
    403 	const struct wps_credential *cred = ctx;
    404 	FILE *oconf, *nconf;
    405 	size_t len, i;
    406 	char *tmp_fname;
    407 	char buf[1024];
    408 	int multi_bss;
    409 	int wpa;
    410 
    411 	if (hapd->wps == NULL)
    412 		return 0;
    413 
    414 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
    415 			cred->cred_attr, cred->cred_attr_len);
    416 
    417 	wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
    418 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
    419 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
    420 		   cred->auth_type);
    421 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
    422 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
    423 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
    424 			cred->key, cred->key_len);
    425 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
    426 		   MAC2STR(cred->mac_addr));
    427 
    428 	if ((hapd->conf->wps_cred_processing == 1 ||
    429 	     hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
    430 		hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
    431 	} else if (hapd->conf->wps_cred_processing == 1 ||
    432 		   hapd->conf->wps_cred_processing == 2) {
    433 		struct wpabuf *attr;
    434 		attr = wpabuf_alloc(200);
    435 		if (attr && wps_build_credential_wrap(attr, cred) == 0)
    436 			hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
    437 					  wpabuf_len(attr));
    438 		wpabuf_free(attr);
    439 	} else
    440 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
    441 
    442 	if (hapd->conf->wps_cred_processing == 1)
    443 		return 0;
    444 
    445 	os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
    446 	hapd->wps->ssid_len = cred->ssid_len;
    447 	hapd->wps->encr_types = cred->encr_type;
    448 	hapd->wps->auth_types = cred->auth_type;
    449 	hapd->wps->ap_encr_type = cred->encr_type;
    450 	hapd->wps->ap_auth_type = cred->auth_type;
    451 	if (cred->key_len == 0) {
    452 		os_free(hapd->wps->network_key);
    453 		hapd->wps->network_key = NULL;
    454 		hapd->wps->network_key_len = 0;
    455 	} else {
    456 		if (hapd->wps->network_key == NULL ||
    457 		    hapd->wps->network_key_len < cred->key_len) {
    458 			hapd->wps->network_key_len = 0;
    459 			os_free(hapd->wps->network_key);
    460 			hapd->wps->network_key = os_malloc(cred->key_len);
    461 			if (hapd->wps->network_key == NULL)
    462 				return -1;
    463 		}
    464 		hapd->wps->network_key_len = cred->key_len;
    465 		os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
    466 	}
    467 	hapd->wps->wps_state = WPS_STATE_CONFIGURED;
    468 
    469 	if (hapd->iface->config_fname == NULL)
    470 		return hapd_wps_reconfig_in_memory(hapd, cred);
    471 	len = os_strlen(hapd->iface->config_fname) + 5;
    472 	tmp_fname = os_malloc(len);
    473 	if (tmp_fname == NULL)
    474 		return -1;
    475 	os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
    476 
    477 	oconf = fopen(hapd->iface->config_fname, "r");
    478 	if (oconf == NULL) {
    479 		wpa_printf(MSG_WARNING, "WPS: Could not open current "
    480 			   "configuration file");
    481 		os_free(tmp_fname);
    482 		return -1;
    483 	}
    484 
    485 	nconf = fopen(tmp_fname, "w");
    486 	if (nconf == NULL) {
    487 		wpa_printf(MSG_WARNING, "WPS: Could not write updated "
    488 			   "configuration file");
    489 		os_free(tmp_fname);
    490 		fclose(oconf);
    491 		return -1;
    492 	}
    493 
    494 	fprintf(nconf, "# WPS configuration - START\n");
    495 
    496 	fprintf(nconf, "wps_state=2\n");
    497 
    498 	if (is_hex(cred->ssid, cred->ssid_len)) {
    499 		fprintf(nconf, "ssid2=");
    500 		for (i = 0; i < cred->ssid_len; i++)
    501 			fprintf(nconf, "%02x", cred->ssid[i]);
    502 		fprintf(nconf, "\n");
    503 	} else {
    504 		fprintf(nconf, "ssid=");
    505 		for (i = 0; i < cred->ssid_len; i++)
    506 			fputc(cred->ssid[i], nconf);
    507 		fprintf(nconf, "\n");
    508 	}
    509 
    510 	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
    511 	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
    512 		wpa = 3;
    513 	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
    514 		wpa = 2;
    515 	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
    516 		wpa = 1;
    517 	else
    518 		wpa = 0;
    519 
    520 	if (wpa) {
    521 		char *prefix;
    522 		fprintf(nconf, "wpa=%d\n", wpa);
    523 
    524 		fprintf(nconf, "wpa_key_mgmt=");
    525 		prefix = "";
    526 		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
    527 			fprintf(nconf, "WPA-EAP");
    528 			prefix = " ";
    529 		}
    530 		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
    531 			fprintf(nconf, "%sWPA-PSK", prefix);
    532 		fprintf(nconf, "\n");
    533 
    534 		fprintf(nconf, "wpa_pairwise=");
    535 		prefix = "";
    536 		if (cred->encr_type & WPS_ENCR_AES) {
    537 			if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
    538 				fprintf(nconf, "GCMP");
    539 			else
    540 				fprintf(nconf, "CCMP");
    541 
    542 			prefix = " ";
    543 		}
    544 		if (cred->encr_type & WPS_ENCR_TKIP) {
    545 			fprintf(nconf, "%sTKIP", prefix);
    546 		}
    547 		fprintf(nconf, "\n");
    548 
    549 		if (cred->key_len >= 8 && cred->key_len < 64) {
    550 			fprintf(nconf, "wpa_passphrase=");
    551 			for (i = 0; i < cred->key_len; i++)
    552 				fputc(cred->key[i], nconf);
    553 			fprintf(nconf, "\n");
    554 		} else if (cred->key_len == 64) {
    555 			fprintf(nconf, "wpa_psk=");
    556 			for (i = 0; i < cred->key_len; i++)
    557 				fputc(cred->key[i], nconf);
    558 			fprintf(nconf, "\n");
    559 		} else {
    560 			wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
    561 				   "for WPA/WPA2",
    562 				   (unsigned long) cred->key_len);
    563 		}
    564 
    565 		fprintf(nconf, "auth_algs=1\n");
    566 	} else {
    567 		/*
    568 		 * WPS 2.0 does not allow WEP to be configured, so no need to
    569 		 * process that option here either.
    570 		 */
    571 		fprintf(nconf, "auth_algs=1\n");
    572 	}
    573 
    574 	fprintf(nconf, "# WPS configuration - END\n");
    575 
    576 	multi_bss = 0;
    577 	while (fgets(buf, sizeof(buf), oconf)) {
    578 		if (os_strncmp(buf, "bss=", 4) == 0)
    579 			multi_bss = 1;
    580 		if (!multi_bss &&
    581 		    (str_starts(buf, "ssid=") ||
    582 		     str_starts(buf, "ssid2=") ||
    583 		     str_starts(buf, "auth_algs=") ||
    584 		     str_starts(buf, "wep_default_key=") ||
    585 		     str_starts(buf, "wep_key") ||
    586 		     str_starts(buf, "wps_state=") ||
    587 		     str_starts(buf, "wpa=") ||
    588 		     str_starts(buf, "wpa_psk=") ||
    589 		     str_starts(buf, "wpa_pairwise=") ||
    590 		     str_starts(buf, "rsn_pairwise=") ||
    591 		     str_starts(buf, "wpa_key_mgmt=") ||
    592 		     str_starts(buf, "wpa_passphrase="))) {
    593 			fprintf(nconf, "#WPS# %s", buf);
    594 		} else
    595 			fprintf(nconf, "%s", buf);
    596 	}
    597 
    598 	fclose(nconf);
    599 	fclose(oconf);
    600 
    601 	if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
    602 		wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
    603 			   "configuration file: %s", strerror(errno));
    604 		os_free(tmp_fname);
    605 		return -1;
    606 	}
    607 
    608 	os_free(tmp_fname);
    609 
    610 	/* Schedule configuration reload after short period of time to allow
    611 	 * EAP-WSC to be finished.
    612 	 */
    613 	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
    614 			       NULL);
    615 
    616 	wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
    617 
    618 	return 0;
    619 }
    620 
    621 
    622 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
    623 {
    624 	struct hostapd_data *hapd = ctx;
    625 	return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
    626 }
    627 
    628 
    629 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
    630 {
    631 	struct hostapd_data *hapd = eloop_data;
    632 
    633 	if (hapd->conf->ap_setup_locked)
    634 		return;
    635 	if (hapd->ap_pin_failures_consecutive >= 10)
    636 		return;
    637 
    638 	wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
    639 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
    640 	hapd->wps->ap_setup_locked = 0;
    641 	wps_registrar_update_ie(hapd->wps->registrar);
    642 }
    643 
    644 
    645 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
    646 {
    647 	struct wps_event_pwd_auth_fail *data = ctx;
    648 
    649 	if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
    650 		return 0;
    651 
    652 	/*
    653 	 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
    654 	 * for some time if this happens multiple times to slow down brute
    655 	 * force attacks.
    656 	 */
    657 	hapd->ap_pin_failures++;
    658 	hapd->ap_pin_failures_consecutive++;
    659 	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
    660 		   "(%u consecutive)",
    661 		   hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
    662 	if (hapd->ap_pin_failures < 3)
    663 		return 0;
    664 
    665 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
    666 	hapd->wps->ap_setup_locked = 1;
    667 
    668 	wps_registrar_update_ie(hapd->wps->registrar);
    669 
    670 	if (!hapd->conf->ap_setup_locked &&
    671 	    hapd->ap_pin_failures_consecutive >= 10) {
    672 		/*
    673 		 * In indefinite lockdown - disable automatic AP PIN
    674 		 * reenablement.
    675 		 */
    676 		eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
    677 		wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
    678 	} else if (!hapd->conf->ap_setup_locked) {
    679 		if (hapd->ap_pin_lockout_time == 0)
    680 			hapd->ap_pin_lockout_time = 60;
    681 		else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
    682 			 (hapd->ap_pin_failures % 3) == 0)
    683 			hapd->ap_pin_lockout_time *= 2;
    684 
    685 		wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
    686 			   hapd->ap_pin_lockout_time);
    687 		eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
    688 		eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
    689 				       hostapd_wps_reenable_ap_pin, hapd,
    690 				       NULL);
    691 	}
    692 
    693 	return 0;
    694 }
    695 
    696 
    697 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
    698 				  struct wps_event_pwd_auth_fail *data)
    699 {
    700 	/* Update WPS Status - Authentication Failure */
    701 	wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
    702 	hapd->wps_stats.status = WPS_STATUS_FAILURE;
    703 	hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
    704 	os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
    705 
    706 	hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
    707 }
    708 
    709 
    710 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
    711 {
    712 	if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
    713 		return 0;
    714 
    715 	if (hapd->ap_pin_failures_consecutive == 0)
    716 		return 0;
    717 
    718 	wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
    719 		   "- total validation failures %u (%u consecutive)",
    720 		   hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
    721 	hapd->ap_pin_failures_consecutive = 0;
    722 
    723 	return 0;
    724 }
    725 
    726 
    727 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
    728 {
    729 	hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
    730 }
    731 
    732 
    733 static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
    734 {
    735 	/* Update WPS Status - PBC Overlap */
    736 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
    737 }
    738 
    739 
    740 static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
    741 {
    742 	/* Update WPS PBC Status:PBC Timeout */
    743 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
    744 }
    745 
    746 
    747 static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
    748 {
    749 	/* Update WPS PBC status - Active */
    750 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
    751 }
    752 
    753 
    754 static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
    755 {
    756 	/* Update WPS PBC status - Active */
    757 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
    758 }
    759 
    760 
    761 static void hostapd_wps_event_success(struct hostapd_data *hapd,
    762 				      struct wps_event_success *success)
    763 {
    764 	/* Update WPS status - Success */
    765 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
    766 	hapd->wps_stats.status = WPS_STATUS_SUCCESS;
    767 	os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
    768 }
    769 
    770 
    771 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
    772 				   struct wps_event_fail *fail)
    773 {
    774 	/* Update WPS status - Failure */
    775 	hapd->wps_stats.status = WPS_STATUS_FAILURE;
    776 	os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
    777 
    778 	hapd->wps_stats.failure_reason = fail->error_indication;
    779 
    780 	if (fail->error_indication > 0 &&
    781 	    fail->error_indication < NUM_WPS_EI_VALUES) {
    782 		wpa_msg(hapd->msg_ctx, MSG_INFO,
    783 			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
    784 			fail->msg, fail->config_error, fail->error_indication,
    785 			wps_ei_str(fail->error_indication));
    786 	} else {
    787 		wpa_msg(hapd->msg_ctx, MSG_INFO,
    788 			WPS_EVENT_FAIL "msg=%d config_error=%d",
    789 			fail->msg, fail->config_error);
    790 	}
    791 }
    792 
    793 
    794 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
    795 				 union wps_event_data *data)
    796 {
    797 	struct hostapd_data *hapd = ctx;
    798 
    799 	switch (event) {
    800 	case WPS_EV_M2D:
    801 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
    802 		break;
    803 	case WPS_EV_FAIL:
    804 		hostapd_wps_event_fail(hapd, &data->fail);
    805 		break;
    806 	case WPS_EV_SUCCESS:
    807 		hostapd_wps_event_success(hapd, &data->success);
    808 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
    809 		break;
    810 	case WPS_EV_PWD_AUTH_FAIL:
    811 		hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
    812 		break;
    813 	case WPS_EV_PBC_OVERLAP:
    814 		hostapd_wps_event_pbc_overlap(hapd);
    815 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
    816 		break;
    817 	case WPS_EV_PBC_TIMEOUT:
    818 		hostapd_wps_event_pbc_timeout(hapd);
    819 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
    820 		break;
    821 	case WPS_EV_PBC_ACTIVE:
    822 		hostapd_wps_event_pbc_active(hapd);
    823 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
    824 		break;
    825 	case WPS_EV_PBC_DISABLE:
    826 		hostapd_wps_event_pbc_disable(hapd);
    827 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
    828 		break;
    829 	case WPS_EV_ER_AP_ADD:
    830 		break;
    831 	case WPS_EV_ER_AP_REMOVE:
    832 		break;
    833 	case WPS_EV_ER_ENROLLEE_ADD:
    834 		break;
    835 	case WPS_EV_ER_ENROLLEE_REMOVE:
    836 		break;
    837 	case WPS_EV_ER_AP_SETTINGS:
    838 		break;
    839 	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
    840 		break;
    841 	case WPS_EV_AP_PIN_SUCCESS:
    842 		hostapd_wps_ap_pin_success(hapd);
    843 		break;
    844 	}
    845 	if (hapd->wps_event_cb)
    846 		hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
    847 }
    848 
    849 
    850 static int hostapd_wps_rf_band_cb(void *ctx)
    851 {
    852 	struct hostapd_data *hapd = ctx;
    853 
    854 	return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
    855 		WPS_RF_50GHZ :
    856 		hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
    857 		WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
    858 }
    859 
    860 
    861 static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
    862 {
    863 	wpabuf_free(hapd->wps_beacon_ie);
    864 	hapd->wps_beacon_ie = NULL;
    865 
    866 	wpabuf_free(hapd->wps_probe_resp_ie);
    867 	hapd->wps_probe_resp_ie = NULL;
    868 
    869 	if (deinit_only) {
    870 		hostapd_reset_ap_wps_ie(hapd);
    871 		return;
    872 	}
    873 
    874 	hostapd_set_ap_wps_ie(hapd);
    875 }
    876 
    877 
    878 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
    879 {
    880 	const u8 **uuid = ctx;
    881 	size_t j;
    882 
    883 	if (iface == NULL)
    884 		return 0;
    885 	for (j = 0; j < iface->num_bss; j++) {
    886 		struct hostapd_data *hapd = iface->bss[j];
    887 		if (hapd->wps && !hapd->conf->wps_independent &&
    888 		    !is_nil_uuid(hapd->wps->uuid)) {
    889 			*uuid = hapd->wps->uuid;
    890 			return 1;
    891 		}
    892 	}
    893 
    894 	return 0;
    895 }
    896 
    897 
    898 static const u8 * get_own_uuid(struct hostapd_iface *iface)
    899 {
    900 	const u8 *uuid;
    901 	if (iface->interfaces == NULL ||
    902 	    iface->interfaces->for_each_interface == NULL)
    903 		return NULL;
    904 	uuid = NULL;
    905 	iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
    906 					      &uuid);
    907 	return uuid;
    908 }
    909 
    910 
    911 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
    912 {
    913 	int *count= ctx;
    914 	(*count)++;
    915 	return 0;
    916 }
    917 
    918 
    919 static int interface_count(struct hostapd_iface *iface)
    920 {
    921 	int count = 0;
    922 	if (iface->interfaces == NULL ||
    923 	    iface->interfaces->for_each_interface == NULL)
    924 		return 0;
    925 	iface->interfaces->for_each_interface(iface->interfaces,
    926 					      count_interface_cb, &count);
    927 	return count;
    928 }
    929 
    930 
    931 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
    932 				      struct wps_context *wps)
    933 {
    934 	int i;
    935 
    936 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
    937 		wpabuf_free(wps->dev.vendor_ext[i]);
    938 		wps->dev.vendor_ext[i] = NULL;
    939 
    940 		if (hapd->conf->wps_vendor_ext[i] == NULL)
    941 			continue;
    942 
    943 		wps->dev.vendor_ext[i] =
    944 			wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
    945 		if (wps->dev.vendor_ext[i] == NULL) {
    946 			while (--i >= 0)
    947 				wpabuf_free(wps->dev.vendor_ext[i]);
    948 			return -1;
    949 		}
    950 	}
    951 
    952 	return 0;
    953 }
    954 
    955 
    956 static void hostapd_free_wps(struct wps_context *wps)
    957 {
    958 	int i;
    959 
    960 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
    961 		wpabuf_free(wps->dev.vendor_ext[i]);
    962 	wps_device_data_free(&wps->dev);
    963 	os_free(wps->network_key);
    964 	hostapd_wps_nfc_clear(wps);
    965 	wpabuf_free(wps->dh_pubkey);
    966 	wpabuf_free(wps->dh_privkey);
    967 	os_free(wps);
    968 }
    969 
    970 
    971 int hostapd_init_wps(struct hostapd_data *hapd,
    972 		     struct hostapd_bss_config *conf)
    973 {
    974 	struct wps_context *wps;
    975 	struct wps_registrar_config cfg;
    976 
    977 	if (conf->wps_state == 0) {
    978 		hostapd_wps_clear_ies(hapd, 0);
    979 		return 0;
    980 	}
    981 
    982 	wps = os_zalloc(sizeof(*wps));
    983 	if (wps == NULL)
    984 		return -1;
    985 
    986 	wps->cred_cb = hostapd_wps_cred_cb;
    987 	wps->event_cb = hostapd_wps_event_cb;
    988 	wps->rf_band_cb = hostapd_wps_rf_band_cb;
    989 	wps->cb_ctx = hapd;
    990 
    991 	os_memset(&cfg, 0, sizeof(cfg));
    992 	wps->wps_state = hapd->conf->wps_state;
    993 	wps->ap_setup_locked = hapd->conf->ap_setup_locked;
    994 	if (is_nil_uuid(hapd->conf->uuid)) {
    995 		const u8 *uuid;
    996 		uuid = get_own_uuid(hapd->iface);
    997 		if (uuid && !conf->wps_independent) {
    998 			os_memcpy(wps->uuid, uuid, UUID_LEN);
    999 			wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
   1000 				    "interface", wps->uuid, UUID_LEN);
   1001 		} else {
   1002 			uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
   1003 			wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
   1004 				    "address", wps->uuid, UUID_LEN);
   1005 		}
   1006 	} else {
   1007 		os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
   1008 		wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
   1009 			    wps->uuid, UUID_LEN);
   1010 	}
   1011 	wps->ssid_len = hapd->conf->ssid.ssid_len;
   1012 	os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
   1013 	wps->ap = 1;
   1014 	os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
   1015 	wps->dev.device_name = hapd->conf->device_name ?
   1016 		os_strdup(hapd->conf->device_name) : NULL;
   1017 	wps->dev.manufacturer = hapd->conf->manufacturer ?
   1018 		os_strdup(hapd->conf->manufacturer) : NULL;
   1019 	wps->dev.model_name = hapd->conf->model_name ?
   1020 		os_strdup(hapd->conf->model_name) : NULL;
   1021 	wps->dev.model_number = hapd->conf->model_number ?
   1022 		os_strdup(hapd->conf->model_number) : NULL;
   1023 	wps->dev.serial_number = hapd->conf->serial_number ?
   1024 		os_strdup(hapd->conf->serial_number) : NULL;
   1025 	wps->config_methods =
   1026 		wps_config_methods_str2bin(hapd->conf->config_methods);
   1027 	if ((wps->config_methods &
   1028 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
   1029 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
   1030 		wpa_printf(MSG_INFO, "WPS: Converting display to "
   1031 			   "virtual_display for WPS 2.0 compliance");
   1032 		wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
   1033 	}
   1034 	if ((wps->config_methods &
   1035 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
   1036 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
   1037 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
   1038 			   "virtual_push_button for WPS 2.0 compliance");
   1039 		wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
   1040 	}
   1041 	os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
   1042 		  WPS_DEV_TYPE_LEN);
   1043 
   1044 	if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
   1045 		goto fail;
   1046 
   1047 	wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
   1048 
   1049 	if (conf->wps_rf_bands) {
   1050 		wps->dev.rf_bands = conf->wps_rf_bands;
   1051 	} else {
   1052 		wps->dev.rf_bands =
   1053 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
   1054 			WPS_RF_50GHZ :
   1055 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
   1056 			WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
   1057 	}
   1058 
   1059 	if (conf->wpa & WPA_PROTO_RSN) {
   1060 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
   1061 			wps->auth_types |= WPS_AUTH_WPA2PSK;
   1062 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
   1063 			wps->auth_types |= WPS_AUTH_WPA2;
   1064 
   1065 		if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))
   1066 			wps->encr_types |= WPS_ENCR_AES;
   1067 		if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
   1068 			wps->encr_types |= WPS_ENCR_TKIP;
   1069 	}
   1070 
   1071 	if (conf->wpa & WPA_PROTO_WPA) {
   1072 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
   1073 			wps->auth_types |= WPS_AUTH_WPAPSK;
   1074 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
   1075 			wps->auth_types |= WPS_AUTH_WPA;
   1076 
   1077 		if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
   1078 			wps->encr_types |= WPS_ENCR_AES;
   1079 		if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
   1080 			wps->encr_types |= WPS_ENCR_TKIP;
   1081 	}
   1082 
   1083 	if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
   1084 		wps->encr_types |= WPS_ENCR_NONE;
   1085 		wps->auth_types |= WPS_AUTH_OPEN;
   1086 	}
   1087 
   1088 	if (conf->ssid.wpa_psk_file) {
   1089 		/* Use per-device PSKs */
   1090 	} else if (conf->ssid.wpa_passphrase) {
   1091 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
   1092 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
   1093 	} else if (conf->ssid.wpa_psk) {
   1094 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
   1095 		if (wps->network_key == NULL)
   1096 			goto fail;
   1097 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
   1098 				 conf->ssid.wpa_psk->psk, PMK_LEN);
   1099 		wps->network_key_len = 2 * PMK_LEN;
   1100 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
   1101 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
   1102 		if (wps->network_key == NULL)
   1103 			goto fail;
   1104 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
   1105 			  conf->ssid.wep.len[0]);
   1106 		wps->network_key_len = conf->ssid.wep.len[0];
   1107 	}
   1108 
   1109 	if (conf->ssid.wpa_psk) {
   1110 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
   1111 		wps->psk_set = 1;
   1112 	}
   1113 
   1114 	wps->ap_auth_type = wps->auth_types;
   1115 	wps->ap_encr_type = wps->encr_types;
   1116 	if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
   1117 		/* Override parameters to enable security by default */
   1118 		wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
   1119 		wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
   1120 	}
   1121 
   1122 	wps->ap_settings = conf->ap_settings;
   1123 	wps->ap_settings_len = conf->ap_settings_len;
   1124 
   1125 	cfg.new_psk_cb = hostapd_wps_new_psk_cb;
   1126 	cfg.set_ie_cb = hostapd_wps_set_ie_cb;
   1127 	cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
   1128 	cfg.reg_success_cb = hostapd_wps_reg_success_cb;
   1129 	cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
   1130 	cfg.cb_ctx = hapd;
   1131 	cfg.skip_cred_build = conf->skip_cred_build;
   1132 	cfg.extra_cred = conf->extra_cred;
   1133 	cfg.extra_cred_len = conf->extra_cred_len;
   1134 	cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
   1135 		conf->skip_cred_build;
   1136 	if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
   1137 		cfg.static_wep_only = 1;
   1138 	cfg.dualband = interface_count(hapd->iface) > 1;
   1139 	if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
   1140 	    (WPS_RF_50GHZ | WPS_RF_24GHZ))
   1141 		cfg.dualband = 1;
   1142 	if (cfg.dualband)
   1143 		wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
   1144 	cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
   1145 
   1146 	wps->registrar = wps_registrar_init(wps, &cfg);
   1147 	if (wps->registrar == NULL) {
   1148 		wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
   1149 		goto fail;
   1150 	}
   1151 
   1152 #ifdef CONFIG_WPS_UPNP
   1153 	wps->friendly_name = hapd->conf->friendly_name;
   1154 	wps->manufacturer_url = hapd->conf->manufacturer_url;
   1155 	wps->model_description = hapd->conf->model_description;
   1156 	wps->model_url = hapd->conf->model_url;
   1157 	wps->upc = hapd->conf->upc;
   1158 #endif /* CONFIG_WPS_UPNP */
   1159 
   1160 	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
   1161 
   1162 	hapd->wps = wps;
   1163 
   1164 	return 0;
   1165 
   1166 fail:
   1167 	hostapd_free_wps(wps);
   1168 	return -1;
   1169 }
   1170 
   1171 
   1172 int hostapd_init_wps_complete(struct hostapd_data *hapd)
   1173 {
   1174 	struct wps_context *wps = hapd->wps;
   1175 
   1176 	if (wps == NULL)
   1177 		return 0;
   1178 
   1179 #ifdef CONFIG_WPS_UPNP
   1180 	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
   1181 		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
   1182 		wps_registrar_deinit(wps->registrar);
   1183 		hostapd_free_wps(wps);
   1184 		hapd->wps = NULL;
   1185 		return -1;
   1186 	}
   1187 #endif /* CONFIG_WPS_UPNP */
   1188 
   1189 	return 0;
   1190 }
   1191 
   1192 
   1193 static void hostapd_wps_nfc_clear(struct wps_context *wps)
   1194 {
   1195 #ifdef CONFIG_WPS_NFC
   1196 	wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
   1197 	wps->ap_nfc_dev_pw_id = 0;
   1198 	wpabuf_free(wps->ap_nfc_dh_pubkey);
   1199 	wps->ap_nfc_dh_pubkey = NULL;
   1200 	wpabuf_free(wps->ap_nfc_dh_privkey);
   1201 	wps->ap_nfc_dh_privkey = NULL;
   1202 	wpabuf_free(wps->ap_nfc_dev_pw);
   1203 	wps->ap_nfc_dev_pw = NULL;
   1204 #endif /* CONFIG_WPS_NFC */
   1205 }
   1206 
   1207 
   1208 void hostapd_deinit_wps(struct hostapd_data *hapd)
   1209 {
   1210 	eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
   1211 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
   1212 	eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
   1213 	if (hapd->wps == NULL) {
   1214 		hostapd_wps_clear_ies(hapd, 1);
   1215 		return;
   1216 	}
   1217 #ifdef CONFIG_WPS_UPNP
   1218 	hostapd_wps_upnp_deinit(hapd);
   1219 #endif /* CONFIG_WPS_UPNP */
   1220 	wps_registrar_deinit(hapd->wps->registrar);
   1221 	wps_free_pending_msgs(hapd->wps->upnp_msgs);
   1222 	hostapd_free_wps(hapd->wps);
   1223 	hapd->wps = NULL;
   1224 	hostapd_wps_clear_ies(hapd, 1);
   1225 }
   1226 
   1227 
   1228 void hostapd_update_wps(struct hostapd_data *hapd)
   1229 {
   1230 	if (hapd->wps == NULL)
   1231 		return;
   1232 
   1233 #ifdef CONFIG_WPS_UPNP
   1234 	hapd->wps->friendly_name = hapd->conf->friendly_name;
   1235 	hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
   1236 	hapd->wps->model_description = hapd->conf->model_description;
   1237 	hapd->wps->model_url = hapd->conf->model_url;
   1238 	hapd->wps->upc = hapd->conf->upc;
   1239 #endif /* CONFIG_WPS_UPNP */
   1240 
   1241 	hostapd_wps_set_vendor_ext(hapd, hapd->wps);
   1242 
   1243 	if (hapd->conf->wps_state)
   1244 		wps_registrar_update_ie(hapd->wps->registrar);
   1245 	else
   1246 		hostapd_deinit_wps(hapd);
   1247 }
   1248 
   1249 
   1250 struct wps_add_pin_data {
   1251 	const u8 *addr;
   1252 	const u8 *uuid;
   1253 	const u8 *pin;
   1254 	size_t pin_len;
   1255 	int timeout;
   1256 	int added;
   1257 };
   1258 
   1259 
   1260 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
   1261 {
   1262 	struct wps_add_pin_data *data = ctx;
   1263 	int ret;
   1264 
   1265 	if (hapd->wps == NULL)
   1266 		return 0;
   1267 	ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
   1268 				    data->uuid, data->pin, data->pin_len,
   1269 				    data->timeout);
   1270 	if (ret == 0)
   1271 		data->added++;
   1272 	return ret;
   1273 }
   1274 
   1275 
   1276 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
   1277 			const char *uuid, const char *pin, int timeout)
   1278 {
   1279 	u8 u[UUID_LEN];
   1280 	struct wps_add_pin_data data;
   1281 
   1282 	data.addr = addr;
   1283 	data.uuid = u;
   1284 	data.pin = (const u8 *) pin;
   1285 	data.pin_len = os_strlen(pin);
   1286 	data.timeout = timeout;
   1287 	data.added = 0;
   1288 
   1289 	if (os_strcmp(uuid, "any") == 0)
   1290 		data.uuid = NULL;
   1291 	else {
   1292 		if (uuid_str2bin(uuid, u))
   1293 			return -1;
   1294 		data.uuid = u;
   1295 	}
   1296 	if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
   1297 		return -1;
   1298 	return data.added ? 0 : -1;
   1299 }
   1300 
   1301 
   1302 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
   1303 {
   1304 	const u8 *p2p_dev_addr = ctx;
   1305 	if (hapd->wps == NULL)
   1306 		return -1;
   1307 	return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
   1308 }
   1309 
   1310 
   1311 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
   1312 			      const u8 *p2p_dev_addr)
   1313 {
   1314 	return hostapd_wps_for_each(hapd, wps_button_pushed,
   1315 				    (void *) p2p_dev_addr);
   1316 }
   1317 
   1318 
   1319 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
   1320 {
   1321 	if (hapd->wps == NULL)
   1322 		return -1;
   1323 
   1324 	wps_registrar_wps_cancel(hapd->wps->registrar);
   1325 	ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
   1326 
   1327 	return 0;
   1328 }
   1329 
   1330 
   1331 int hostapd_wps_cancel(struct hostapd_data *hapd)
   1332 {
   1333 	return hostapd_wps_for_each(hapd, wps_cancel, NULL);
   1334 }
   1335 
   1336 
   1337 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
   1338 				    const u8 *bssid,
   1339 				    const u8 *ie, size_t ie_len,
   1340 				    int ssi_signal)
   1341 {
   1342 	struct hostapd_data *hapd = ctx;
   1343 	struct wpabuf *wps_ie;
   1344 	struct ieee802_11_elems elems;
   1345 
   1346 	if (hapd->wps == NULL)
   1347 		return 0;
   1348 
   1349 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
   1350 		wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
   1351 			   MACSTR, MAC2STR(addr));
   1352 		return 0;
   1353 	}
   1354 
   1355 	if (elems.ssid && elems.ssid_len > 0 &&
   1356 	    (elems.ssid_len != hapd->conf->ssid.ssid_len ||
   1357 	     os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
   1358 	     0))
   1359 		return 0; /* Not for us */
   1360 
   1361 	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
   1362 	if (wps_ie == NULL)
   1363 		return 0;
   1364 	if (wps_validate_probe_req(wps_ie, addr) < 0) {
   1365 		wpabuf_free(wps_ie);
   1366 		return 0;
   1367 	}
   1368 
   1369 	if (wpabuf_len(wps_ie) > 0) {
   1370 		int p2p_wildcard = 0;
   1371 #ifdef CONFIG_P2P
   1372 		if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
   1373 		    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
   1374 			      P2P_WILDCARD_SSID_LEN) == 0)
   1375 			p2p_wildcard = 1;
   1376 #endif /* CONFIG_P2P */
   1377 		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
   1378 					   p2p_wildcard);
   1379 #ifdef CONFIG_WPS_UPNP
   1380 		/* FIX: what exactly should be included in the WLANEvent?
   1381 		 * WPS attributes? Full ProbeReq frame? */
   1382 		if (!p2p_wildcard)
   1383 			upnp_wps_device_send_wlan_event(
   1384 				hapd->wps_upnp, addr,
   1385 				UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
   1386 #endif /* CONFIG_WPS_UPNP */
   1387 	}
   1388 
   1389 	wpabuf_free(wps_ie);
   1390 
   1391 	return 0;
   1392 }
   1393 
   1394 
   1395 #ifdef CONFIG_WPS_UPNP
   1396 
   1397 static int hostapd_rx_req_put_wlan_response(
   1398 	void *priv, enum upnp_wps_wlanevent_type ev_type,
   1399 	const u8 *mac_addr, const struct wpabuf *msg,
   1400 	enum wps_msg_type msg_type)
   1401 {
   1402 	struct hostapd_data *hapd = priv;
   1403 	struct sta_info *sta;
   1404 	struct upnp_pending_message *p;
   1405 
   1406 	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
   1407 		   MACSTR, ev_type, MAC2STR(mac_addr));
   1408 	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
   1409 		    wpabuf_head(msg), wpabuf_len(msg));
   1410 	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
   1411 		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
   1412 			   "PutWLANResponse WLANEventType %d", ev_type);
   1413 		return -1;
   1414 	}
   1415 
   1416 	/*
   1417 	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
   1418 	 * server implementation for delivery to the peer.
   1419 	 */
   1420 
   1421 	sta = ap_get_sta(hapd, mac_addr);
   1422 #ifndef CONFIG_WPS_STRICT
   1423 	if (!sta) {
   1424 		/*
   1425 		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
   1426 		 * Pick STA that is in an ongoing WPS registration without
   1427 		 * checking the MAC address.
   1428 		 */
   1429 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
   1430 			   "on NewWLANEventMAC; try wildcard match");
   1431 		for (sta = hapd->sta_list; sta; sta = sta->next) {
   1432 			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
   1433 				break;
   1434 		}
   1435 	}
   1436 #endif /* CONFIG_WPS_STRICT */
   1437 
   1438 	if (!sta || !(sta->flags & WLAN_STA_WPS)) {
   1439 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
   1440 		return 0;
   1441 	}
   1442 
   1443 	if (!sta->eapol_sm) {
   1444 		/*
   1445 		 * This can happen, e.g., if an ER sends an extra message after
   1446 		 * the station has disassociated (but not fully
   1447 		 * deauthenticated).
   1448 		 */
   1449 		wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
   1450 		return 0;
   1451 	}
   1452 
   1453 	p = os_zalloc(sizeof(*p));
   1454 	if (p == NULL)
   1455 		return -1;
   1456 	os_memcpy(p->addr, sta->addr, ETH_ALEN);
   1457 	p->msg = wpabuf_dup(msg);
   1458 	p->type = msg_type;
   1459 	p->next = hapd->wps->upnp_msgs;
   1460 	hapd->wps->upnp_msgs = p;
   1461 
   1462 	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
   1463 }
   1464 
   1465 
   1466 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
   1467 				 struct wps_context *wps)
   1468 {
   1469 	struct upnp_wps_device_ctx *ctx;
   1470 
   1471 	if (!hapd->conf->upnp_iface)
   1472 		return 0;
   1473 	ctx = os_zalloc(sizeof(*ctx));
   1474 	if (ctx == NULL)
   1475 		return -1;
   1476 
   1477 	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
   1478 	if (hapd->conf->ap_pin)
   1479 		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
   1480 
   1481 	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
   1482 					      hapd->conf->upnp_iface);
   1483 	if (hapd->wps_upnp == NULL)
   1484 		return -1;
   1485 	wps->wps_upnp = hapd->wps_upnp;
   1486 
   1487 	return 0;
   1488 }
   1489 
   1490 
   1491 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
   1492 {
   1493 	upnp_wps_device_deinit(hapd->wps_upnp, hapd);
   1494 }
   1495 
   1496 #endif /* CONFIG_WPS_UPNP */
   1497 
   1498 
   1499 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
   1500 			    char *buf, size_t buflen)
   1501 {
   1502 	if (hapd->wps == NULL)
   1503 		return 0;
   1504 	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
   1505 }
   1506 
   1507 
   1508 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
   1509 {
   1510 	struct hostapd_data *hapd = eloop_data;
   1511 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
   1512 	hostapd_wps_ap_pin_disable(hapd);
   1513 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
   1514 }
   1515 
   1516 
   1517 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
   1518 {
   1519 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
   1520 	hapd->ap_pin_failures = 0;
   1521 	hapd->ap_pin_failures_consecutive = 0;
   1522 	hapd->conf->ap_setup_locked = 0;
   1523 	if (hapd->wps->ap_setup_locked) {
   1524 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
   1525 		hapd->wps->ap_setup_locked = 0;
   1526 		wps_registrar_update_ie(hapd->wps->registrar);
   1527 	}
   1528 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
   1529 	if (timeout > 0)
   1530 		eloop_register_timeout(timeout, 0,
   1531 				       hostapd_wps_ap_pin_timeout, hapd, NULL);
   1532 }
   1533 
   1534 
   1535 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
   1536 {
   1537 	os_free(hapd->conf->ap_pin);
   1538 	hapd->conf->ap_pin = NULL;
   1539 #ifdef CONFIG_WPS_UPNP
   1540 	upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
   1541 #endif /* CONFIG_WPS_UPNP */
   1542 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
   1543 	return 0;
   1544 }
   1545 
   1546 
   1547 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
   1548 {
   1549 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
   1550 	hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
   1551 }
   1552 
   1553 
   1554 struct wps_ap_pin_data {
   1555 	char pin_txt[9];
   1556 	int timeout;
   1557 };
   1558 
   1559 
   1560 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
   1561 {
   1562 	struct wps_ap_pin_data *data = ctx;
   1563 	os_free(hapd->conf->ap_pin);
   1564 	hapd->conf->ap_pin = os_strdup(data->pin_txt);
   1565 #ifdef CONFIG_WPS_UPNP
   1566 	upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
   1567 #endif /* CONFIG_WPS_UPNP */
   1568 	hostapd_wps_ap_pin_enable(hapd, data->timeout);
   1569 	return 0;
   1570 }
   1571 
   1572 
   1573 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
   1574 {
   1575 	unsigned int pin;
   1576 	struct wps_ap_pin_data data;
   1577 
   1578 	pin = wps_generate_pin();
   1579 	os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
   1580 	data.timeout = timeout;
   1581 	hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
   1582 	return hapd->conf->ap_pin;
   1583 }
   1584 
   1585 
   1586 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
   1587 {
   1588 	return hapd->conf->ap_pin;
   1589 }
   1590 
   1591 
   1592 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
   1593 			   int timeout)
   1594 {
   1595 	struct wps_ap_pin_data data;
   1596 	int ret;
   1597 
   1598 	ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
   1599 	if (os_snprintf_error(sizeof(data.pin_txt), ret))
   1600 		return -1;
   1601 	data.timeout = timeout;
   1602 	return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
   1603 }
   1604 
   1605 
   1606 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
   1607 {
   1608 	if (hapd->wps)
   1609 		wps_registrar_update_ie(hapd->wps->registrar);
   1610 	return 0;
   1611 }
   1612 
   1613 
   1614 void hostapd_wps_update_ie(struct hostapd_data *hapd)
   1615 {
   1616 	hostapd_wps_for_each(hapd, wps_update_ie, NULL);
   1617 }
   1618 
   1619 
   1620 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
   1621 			  const char *auth, const char *encr, const char *key)
   1622 {
   1623 	struct wps_credential cred;
   1624 	size_t len;
   1625 
   1626 	os_memset(&cred, 0, sizeof(cred));
   1627 
   1628 	len = os_strlen(ssid);
   1629 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
   1630 	    hexstr2bin(ssid, cred.ssid, len / 2))
   1631 		return -1;
   1632 	cred.ssid_len = len / 2;
   1633 
   1634 	if (os_strncmp(auth, "OPEN", 4) == 0)
   1635 		cred.auth_type = WPS_AUTH_OPEN;
   1636 	else if (os_strncmp(auth, "WPAPSK", 6) == 0)
   1637 		cred.auth_type = WPS_AUTH_WPAPSK;
   1638 	else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
   1639 		cred.auth_type = WPS_AUTH_WPA2PSK;
   1640 	else
   1641 		return -1;
   1642 
   1643 	if (encr) {
   1644 		if (os_strncmp(encr, "NONE", 4) == 0)
   1645 			cred.encr_type = WPS_ENCR_NONE;
   1646 		else if (os_strncmp(encr, "TKIP", 4) == 0)
   1647 			cred.encr_type = WPS_ENCR_TKIP;
   1648 		else if (os_strncmp(encr, "CCMP", 4) == 0)
   1649 			cred.encr_type = WPS_ENCR_AES;
   1650 		else
   1651 			return -1;
   1652 	} else
   1653 		cred.encr_type = WPS_ENCR_NONE;
   1654 
   1655 	if (key) {
   1656 		len = os_strlen(key);
   1657 		if ((len & 1) || len > 2 * sizeof(cred.key) ||
   1658 		    hexstr2bin(key, cred.key, len / 2))
   1659 			return -1;
   1660 		cred.key_len = len / 2;
   1661 	}
   1662 
   1663 	return wps_registrar_config_ap(hapd->wps->registrar, &cred);
   1664 }
   1665 
   1666 
   1667 #ifdef CONFIG_WPS_NFC
   1668 
   1669 struct wps_nfc_password_token_data {
   1670 	const u8 *oob_dev_pw;
   1671 	size_t oob_dev_pw_len;
   1672 	int added;
   1673 };
   1674 
   1675 
   1676 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
   1677 {
   1678 	struct wps_nfc_password_token_data *data = ctx;
   1679 	int ret;
   1680 
   1681 	if (hapd->wps == NULL)
   1682 		return 0;
   1683 	ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
   1684 						   data->oob_dev_pw,
   1685 						   data->oob_dev_pw_len);
   1686 	if (ret == 0)
   1687 		data->added++;
   1688 	return ret;
   1689 }
   1690 
   1691 
   1692 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
   1693 					      struct wps_parse_attr *attr)
   1694 {
   1695 	struct wps_nfc_password_token_data data;
   1696 
   1697 	data.oob_dev_pw = attr->oob_dev_password;
   1698 	data.oob_dev_pw_len = attr->oob_dev_password_len;
   1699 	data.added = 0;
   1700 	if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
   1701 		return -1;
   1702 	return data.added ? 0 : -1;
   1703 }
   1704 
   1705 
   1706 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
   1707 				       const struct wpabuf *wps)
   1708 {
   1709 	struct wps_parse_attr attr;
   1710 
   1711 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
   1712 
   1713 	if (wps_parse_msg(wps, &attr)) {
   1714 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
   1715 		return -1;
   1716 	}
   1717 
   1718 	if (attr.oob_dev_password)
   1719 		return hostapd_wps_add_nfc_password_token(hapd, &attr);
   1720 
   1721 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
   1722 	return -1;
   1723 }
   1724 
   1725 
   1726 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
   1727 			     const struct wpabuf *data)
   1728 {
   1729 	const struct wpabuf *wps = data;
   1730 	struct wpabuf *tmp = NULL;
   1731 	int ret;
   1732 
   1733 	if (wpabuf_len(data) < 4)
   1734 		return -1;
   1735 
   1736 	if (*wpabuf_head_u8(data) != 0x10) {
   1737 		/* Assume this contains full NDEF record */
   1738 		tmp = ndef_parse_wifi(data);
   1739 		if (tmp == NULL) {
   1740 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
   1741 			return -1;
   1742 		}
   1743 		wps = tmp;
   1744 	}
   1745 
   1746 	ret = hostapd_wps_nfc_tag_process(hapd, wps);
   1747 	wpabuf_free(tmp);
   1748 	return ret;
   1749 }
   1750 
   1751 
   1752 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
   1753 					     int ndef)
   1754 {
   1755 	struct wpabuf *ret;
   1756 
   1757 	if (hapd->wps == NULL)
   1758 		return NULL;
   1759 
   1760 	ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
   1761 			       hapd->iconf->channel);
   1762 	if (ndef && ret) {
   1763 		struct wpabuf *tmp;
   1764 		tmp = ndef_build_wifi(ret);
   1765 		wpabuf_free(ret);
   1766 		if (tmp == NULL)
   1767 			return NULL;
   1768 		ret = tmp;
   1769 	}
   1770 
   1771 	return ret;
   1772 }
   1773 
   1774 
   1775 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
   1776 {
   1777 	struct wpabuf *ret;
   1778 
   1779 	if (hapd->wps == NULL)
   1780 		return NULL;
   1781 
   1782 	if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
   1783 		struct wps_context *wps = hapd->wps;
   1784 		if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
   1785 				   &hapd->conf->wps_nfc_dh_privkey) < 0)
   1786 			return NULL;
   1787 		hostapd_wps_nfc_clear(wps);
   1788 		wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
   1789 		wps->ap_nfc_dh_pubkey =
   1790 			wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
   1791 		wps->ap_nfc_dh_privkey =
   1792 			wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
   1793 		if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
   1794 			hostapd_wps_nfc_clear(wps);
   1795 			return NULL;
   1796 		}
   1797 	}
   1798 
   1799 	ret = wps_build_nfc_handover_sel(hapd->wps,
   1800 					 hapd->conf->wps_nfc_dh_pubkey,
   1801 					 hapd->own_addr, hapd->iface->freq);
   1802 
   1803 	if (ndef && ret) {
   1804 		struct wpabuf *tmp;
   1805 		tmp = ndef_build_wifi(ret);
   1806 		wpabuf_free(ret);
   1807 		if (tmp == NULL)
   1808 			return NULL;
   1809 		ret = tmp;
   1810 	}
   1811 
   1812 	return ret;
   1813 }
   1814 
   1815 
   1816 int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
   1817 				    const struct wpabuf *req,
   1818 				    const struct wpabuf *sel)
   1819 {
   1820 	struct wpabuf *wps;
   1821 	int ret = -1;
   1822 	u16 wsc_len;
   1823 	const u8 *pos;
   1824 	struct wpabuf msg;
   1825 	struct wps_parse_attr attr;
   1826 	u16 dev_pw_id;
   1827 
   1828 	/*
   1829 	 * Enrollee/station is always initiator of the NFC connection handover,
   1830 	 * so use the request message here to find Enrollee public key hash.
   1831 	 */
   1832 	wps = ndef_parse_wifi(req);
   1833 	if (wps == NULL)
   1834 		return -1;
   1835 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
   1836 		   "payload from NFC connection handover");
   1837 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
   1838 	if (wpabuf_len(wps) < 2) {
   1839 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
   1840 			   "Message");
   1841 		goto out;
   1842 	}
   1843 	pos = wpabuf_head(wps);
   1844 	wsc_len = WPA_GET_BE16(pos);
   1845 	if (wsc_len > wpabuf_len(wps) - 2) {
   1846 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
   1847 			   "in rt Wi-Fi Handover Request Message", wsc_len);
   1848 		goto out;
   1849 	}
   1850 	pos += 2;
   1851 
   1852 	wpa_hexdump(MSG_DEBUG,
   1853 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
   1854 		    pos, wsc_len);
   1855 	if (wsc_len < wpabuf_len(wps) - 2) {
   1856 		wpa_hexdump(MSG_DEBUG,
   1857 			    "WPS: Ignore extra data after WSC attributes",
   1858 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
   1859 	}
   1860 
   1861 	wpabuf_set(&msg, pos, wsc_len);
   1862 	ret = wps_parse_msg(&msg, &attr);
   1863 	if (ret < 0) {
   1864 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
   1865 			   "Wi-Fi Handover Request Message");
   1866 		goto out;
   1867 	}
   1868 
   1869 	if (attr.oob_dev_password == NULL ||
   1870 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
   1871 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
   1872 			   "included in Wi-Fi Handover Request Message");
   1873 		ret = -1;
   1874 		goto out;
   1875 	}
   1876 
   1877 	if (attr.uuid_e == NULL) {
   1878 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
   1879 			   "Handover Request Message");
   1880 		ret = -1;
   1881 		goto out;
   1882 	}
   1883 
   1884 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
   1885 
   1886 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
   1887 		    attr.oob_dev_password, attr.oob_dev_password_len);
   1888 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
   1889 				 WPS_OOB_PUBKEY_HASH_LEN);
   1890 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
   1891 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
   1892 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
   1893 		ret = -1;
   1894 		goto out;
   1895 	}
   1896 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
   1897 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
   1898 
   1899 	ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
   1900 					     attr.oob_dev_password,
   1901 					     DEV_PW_NFC_CONNECTION_HANDOVER,
   1902 					     NULL, 0, 1);
   1903 
   1904 out:
   1905 	wpabuf_free(wps);
   1906 	return ret;
   1907 }
   1908 
   1909 
   1910 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
   1911 {
   1912 	if (hapd->conf->wps_nfc_pw_from_config) {
   1913 		return wps_nfc_token_build(ndef,
   1914 					   hapd->conf->wps_nfc_dev_pw_id,
   1915 					   hapd->conf->wps_nfc_dh_pubkey,
   1916 					   hapd->conf->wps_nfc_dev_pw);
   1917 	}
   1918 
   1919 	return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
   1920 				 &hapd->conf->wps_nfc_dh_pubkey,
   1921 				 &hapd->conf->wps_nfc_dh_privkey,
   1922 				 &hapd->conf->wps_nfc_dev_pw);
   1923 }
   1924 
   1925 
   1926 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
   1927 {
   1928 	struct wps_context *wps = hapd->wps;
   1929 	struct wpabuf *pw;
   1930 
   1931 	if (wps == NULL)
   1932 		return -1;
   1933 
   1934 	if (!hapd->conf->wps_nfc_dh_pubkey ||
   1935 	    !hapd->conf->wps_nfc_dh_privkey ||
   1936 	    !hapd->conf->wps_nfc_dev_pw ||
   1937 	    !hapd->conf->wps_nfc_dev_pw_id)
   1938 		return -1;
   1939 
   1940 	hostapd_wps_nfc_clear(wps);
   1941 	wpa_printf(MSG_DEBUG,
   1942 		   "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
   1943 		   hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
   1944 	wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
   1945 	wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
   1946 	wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
   1947 	pw = hapd->conf->wps_nfc_dev_pw;
   1948 	wps->ap_nfc_dev_pw = wpabuf_alloc(
   1949 		wpabuf_len(pw) * 2 + 1);
   1950 	if (wps->ap_nfc_dev_pw) {
   1951 		wpa_snprintf_hex_uppercase(
   1952 			(char *) wpabuf_put(wps->ap_nfc_dev_pw,
   1953 					    wpabuf_len(pw) * 2),
   1954 			wpabuf_len(pw) * 2 + 1,
   1955 			wpabuf_head(pw), wpabuf_len(pw));
   1956 	}
   1957 
   1958 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
   1959 	    !wps->ap_nfc_dev_pw) {
   1960 		hostapd_wps_nfc_clear(wps);
   1961 		return -1;
   1962 	}
   1963 
   1964 	return 0;
   1965 }
   1966 
   1967 
   1968 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
   1969 {
   1970 	wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
   1971 		   hapd->conf->iface);
   1972 	hostapd_wps_nfc_clear(hapd->wps);
   1973 }
   1974 
   1975 #endif /* CONFIG_WPS_NFC */
   1976