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