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