wps_hostapd.c revision 8d520ff1dc2da35cdca849e982051b86468016d8
1/*
2 * hostapd / WPS integration
3 * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "utils/includes.h"
16
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "utils/uuid.h"
20#include "crypto/dh_groups.h"
21#include "common/wpa_ctrl.h"
22#include "common/ieee802_11_defs.h"
23#include "common/ieee802_11_common.h"
24#include "eapol_auth/eapol_auth_sm.h"
25#include "eapol_auth/eapol_auth_sm_i.h"
26#include "wps/wps.h"
27#include "wps/wps_defs.h"
28#include "wps/wps_dev_attr.h"
29#include "hostapd.h"
30#include "ap_config.h"
31#include "ap_drv_ops.h"
32#include "beacon.h"
33#include "sta_info.h"
34#include "wps_hostapd.h"
35
36
37#ifdef CONFIG_WPS_UPNP
38#include "wps/wps_upnp.h"
39static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
40				 struct wps_context *wps);
41static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
42#endif /* CONFIG_WPS_UPNP */
43
44static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
45				    const u8 *ie, size_t ie_len);
46static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
47
48
49struct wps_for_each_data {
50	int (*func)(struct hostapd_data *h, void *ctx);
51	void *ctx;
52};
53
54
55static int wps_for_each(struct hostapd_iface *iface, void *ctx)
56{
57	struct wps_for_each_data *data = ctx;
58	size_t j;
59
60	if (iface == NULL)
61		return 0;
62	for (j = 0; j < iface->num_bss; j++) {
63		struct hostapd_data *hapd = iface->bss[j];
64		int ret = data->func(hapd, data->ctx);
65		if (ret)
66			return ret;
67	}
68
69	return 0;
70}
71
72
73static int hostapd_wps_for_each(struct hostapd_data *hapd,
74				int (*func)(struct hostapd_data *h, void *ctx),
75				void *ctx)
76{
77	struct hostapd_iface *iface = hapd->iface;
78	struct wps_for_each_data data;
79	data.func = func;
80	data.ctx = ctx;
81	if (iface->for_each_interface == NULL)
82		return wps_for_each(iface, &data);
83	return iface->for_each_interface(iface->interfaces, wps_for_each,
84					 &data);
85}
86
87
88static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
89				  size_t psk_len)
90{
91	struct hostapd_data *hapd = ctx;
92	struct hostapd_wpa_psk *p;
93	struct hostapd_ssid *ssid = &hapd->conf->ssid;
94
95	wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
96		   MACSTR, MAC2STR(mac_addr));
97	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
98
99	if (psk_len != PMK_LEN) {
100		wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
101			   (unsigned long) psk_len);
102		return -1;
103	}
104
105	/* Add the new PSK to runtime PSK list */
106	p = os_zalloc(sizeof(*p));
107	if (p == NULL)
108		return -1;
109	os_memcpy(p->addr, mac_addr, ETH_ALEN);
110	os_memcpy(p->psk, psk, PMK_LEN);
111
112	p->next = ssid->wpa_psk;
113	ssid->wpa_psk = p;
114
115	if (ssid->wpa_psk_file) {
116		FILE *f;
117		char hex[PMK_LEN * 2 + 1];
118		/* Add the new PSK to PSK list file */
119		f = fopen(ssid->wpa_psk_file, "a");
120		if (f == NULL) {
121			wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
122				   "'%s'", ssid->wpa_psk_file);
123			return -1;
124		}
125
126		wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
127		fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
128		fclose(f);
129	}
130
131	return 0;
132}
133
134
135static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
136				 struct wpabuf *probe_resp_ie)
137{
138	struct hostapd_data *hapd = ctx;
139	wpabuf_free(hapd->wps_beacon_ie);
140	hapd->wps_beacon_ie = beacon_ie;
141	wpabuf_free(hapd->wps_probe_resp_ie);
142	hapd->wps_probe_resp_ie = probe_resp_ie;
143	ieee802_11_set_beacon(hapd);
144	return hostapd_set_ap_wps_ie(hapd);
145}
146
147
148static 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
187static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
188				       const u8 *uuid_e)
189{
190	struct hostapd_data *hapd = ctx;
191	char uuid[40];
192	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
193		return;
194	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
195		MAC2STR(mac_addr), uuid);
196	if (hapd->wps_reg_success_cb)
197		hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
198					 mac_addr, uuid_e);
199}
200
201
202static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
203					 const u8 *uuid_e,
204					 const u8 *pri_dev_type,
205					 u16 config_methods,
206					 u16 dev_password_id, u8 request_type,
207					 const char *dev_name)
208{
209	struct hostapd_data *hapd = ctx;
210	char uuid[40];
211	char devtype[WPS_DEV_TYPE_BUFSIZE];
212	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
213		return;
214	if (dev_name == NULL)
215		dev_name = "";
216	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
217		     " %s %s 0x%x %u %u [%s]",
218		     MAC2STR(addr), uuid,
219		     wps_dev_type_bin2str(pri_dev_type, devtype,
220					  sizeof(devtype)),
221		     config_methods, dev_password_id, request_type, dev_name);
222}
223
224
225static int str_starts(const char *str, const char *start)
226{
227	return os_strncmp(str, start, os_strlen(start)) == 0;
228}
229
230
231static void wps_reload_config(void *eloop_data, void *user_ctx)
232{
233	struct hostapd_iface *iface = eloop_data;
234
235	wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
236	if (iface->reload_config(iface) < 0) {
237		wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
238			   "configuration");
239	}
240}
241
242
243static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
244{
245	const struct wps_credential *cred = ctx;
246	FILE *oconf, *nconf;
247	size_t len, i;
248	char *tmp_fname;
249	char buf[1024];
250	int multi_bss;
251	int wpa;
252
253	if (hapd->wps == NULL)
254		return 0;
255
256	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
257			cred->cred_attr, cred->cred_attr_len);
258
259	wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
260	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
261	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
262		   cred->auth_type);
263	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
264	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
265	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
266			cred->key, cred->key_len);
267	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
268		   MAC2STR(cred->mac_addr));
269
270	if ((hapd->conf->wps_cred_processing == 1 ||
271	     hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
272		size_t blen = cred->cred_attr_len * 2 + 1;
273		char *_buf = os_malloc(blen);
274		if (_buf) {
275			wpa_snprintf_hex(_buf, blen,
276					 cred->cred_attr, cred->cred_attr_len);
277			wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
278				WPS_EVENT_NEW_AP_SETTINGS, _buf);
279			os_free(_buf);
280		}
281	} else
282		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
283
284	if (hapd->conf->wps_cred_processing == 1)
285		return 0;
286
287	os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
288	hapd->wps->ssid_len = cred->ssid_len;
289	hapd->wps->encr_types = cred->encr_type;
290	hapd->wps->auth_types = cred->auth_type;
291	if (cred->key_len == 0) {
292		os_free(hapd->wps->network_key);
293		hapd->wps->network_key = NULL;
294		hapd->wps->network_key_len = 0;
295	} else {
296		if (hapd->wps->network_key == NULL ||
297		    hapd->wps->network_key_len < cred->key_len) {
298			hapd->wps->network_key_len = 0;
299			os_free(hapd->wps->network_key);
300			hapd->wps->network_key = os_malloc(cred->key_len);
301			if (hapd->wps->network_key == NULL)
302				return -1;
303		}
304		hapd->wps->network_key_len = cred->key_len;
305		os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
306	}
307	hapd->wps->wps_state = WPS_STATE_CONFIGURED;
308
309	len = os_strlen(hapd->iface->config_fname) + 5;
310	tmp_fname = os_malloc(len);
311	if (tmp_fname == NULL)
312		return -1;
313	os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
314
315	oconf = fopen(hapd->iface->config_fname, "r");
316	if (oconf == NULL) {
317		wpa_printf(MSG_WARNING, "WPS: Could not open current "
318			   "configuration file");
319		os_free(tmp_fname);
320		return -1;
321	}
322
323	nconf = fopen(tmp_fname, "w");
324	if (nconf == NULL) {
325		wpa_printf(MSG_WARNING, "WPS: Could not write updated "
326			   "configuration file");
327		os_free(tmp_fname);
328		fclose(oconf);
329		return -1;
330	}
331
332	fprintf(nconf, "# WPS configuration - START\n");
333
334	fprintf(nconf, "wps_state=2\n");
335
336	fprintf(nconf, "ssid=");
337	for (i = 0; i < cred->ssid_len; i++)
338		fputc(cred->ssid[i], nconf);
339	fprintf(nconf, "\n");
340
341	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
342	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
343		wpa = 3;
344	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
345		wpa = 2;
346	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
347		wpa = 1;
348	else
349		wpa = 0;
350
351	if (wpa) {
352		char *prefix;
353		fprintf(nconf, "wpa=%d\n", wpa);
354
355		fprintf(nconf, "wpa_key_mgmt=");
356		prefix = "";
357		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
358			fprintf(nconf, "WPA-EAP");
359			prefix = " ";
360		}
361		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
362			fprintf(nconf, "%sWPA-PSK", prefix);
363		fprintf(nconf, "\n");
364
365		fprintf(nconf, "wpa_pairwise=");
366		prefix = "";
367		if (cred->encr_type & WPS_ENCR_AES) {
368			fprintf(nconf, "CCMP");
369			prefix = " ";
370		}
371		if (cred->encr_type & WPS_ENCR_TKIP) {
372			fprintf(nconf, "%sTKIP", prefix);
373		}
374		fprintf(nconf, "\n");
375
376		if (cred->key_len >= 8 && cred->key_len < 64) {
377			fprintf(nconf, "wpa_passphrase=");
378			for (i = 0; i < cred->key_len; i++)
379				fputc(cred->key[i], nconf);
380			fprintf(nconf, "\n");
381		} else if (cred->key_len == 64) {
382			fprintf(nconf, "wpa_psk=");
383			for (i = 0; i < cred->key_len; i++)
384				fputc(cred->key[i], nconf);
385			fprintf(nconf, "\n");
386		} else {
387			wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
388				   "for WPA/WPA2",
389				   (unsigned long) cred->key_len);
390		}
391
392		fprintf(nconf, "auth_algs=1\n");
393	} else {
394		if ((cred->auth_type & WPS_AUTH_OPEN) &&
395		    (cred->auth_type & WPS_AUTH_SHARED))
396			fprintf(nconf, "auth_algs=3\n");
397		else if (cred->auth_type & WPS_AUTH_SHARED)
398			fprintf(nconf, "auth_algs=2\n");
399		else
400			fprintf(nconf, "auth_algs=1\n");
401
402		if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
403			int key_idx = cred->key_idx;
404			if (key_idx)
405				key_idx--;
406			fprintf(nconf, "wep_default_key=%d\n", key_idx);
407			fprintf(nconf, "wep_key%d=", key_idx);
408			if (cred->key_len == 10 || cred->key_len == 26) {
409				/* WEP key as a hex string */
410				for (i = 0; i < cred->key_len; i++)
411					fputc(cred->key[i], nconf);
412			} else {
413				/* Raw WEP key; convert to hex */
414				for (i = 0; i < cred->key_len; i++)
415					fprintf(nconf, "%02x", cred->key[i]);
416			}
417			fprintf(nconf, "\n");
418		}
419	}
420
421	fprintf(nconf, "# WPS configuration - END\n");
422
423	multi_bss = 0;
424	while (fgets(buf, sizeof(buf), oconf)) {
425		if (os_strncmp(buf, "bss=", 4) == 0)
426			multi_bss = 1;
427		if (!multi_bss &&
428		    (str_starts(buf, "ssid=") ||
429		     str_starts(buf, "auth_algs=") ||
430		     str_starts(buf, "wep_default_key=") ||
431		     str_starts(buf, "wep_key") ||
432		     str_starts(buf, "wps_state=") ||
433		     str_starts(buf, "wpa=") ||
434		     str_starts(buf, "wpa_psk=") ||
435		     str_starts(buf, "wpa_pairwise=") ||
436		     str_starts(buf, "rsn_pairwise=") ||
437		     str_starts(buf, "wpa_key_mgmt=") ||
438		     str_starts(buf, "wpa_passphrase="))) {
439			fprintf(nconf, "#WPS# %s", buf);
440		} else
441			fprintf(nconf, "%s", buf);
442	}
443
444	fclose(nconf);
445	fclose(oconf);
446
447	if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
448		wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
449			   "configuration file: %s", strerror(errno));
450		os_free(tmp_fname);
451		return -1;
452	}
453
454	os_free(tmp_fname);
455
456	/* Schedule configuration reload after short period of time to allow
457	 * EAP-WSC to be finished.
458	 */
459	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
460			       NULL);
461
462	wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
463
464	return 0;
465}
466
467
468static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
469{
470	struct hostapd_data *hapd = ctx;
471	return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
472}
473
474
475static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
476{
477	struct hostapd_data *hapd = eloop_data;
478
479	if (hapd->conf->ap_setup_locked)
480		return;
481
482	wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
483	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
484	hapd->wps->ap_setup_locked = 0;
485	wps_registrar_update_ie(hapd->wps->registrar);
486}
487
488
489static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
490{
491	struct wps_event_pwd_auth_fail *data = ctx;
492
493	if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
494		return 0;
495
496	/*
497	 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
498	 * for some time if this happens multiple times to slow down brute
499	 * force attacks.
500	 */
501	hapd->ap_pin_failures++;
502	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
503		   hapd->ap_pin_failures);
504	if (hapd->ap_pin_failures < 3)
505		return 0;
506
507	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
508	hapd->wps->ap_setup_locked = 1;
509
510	wps_registrar_update_ie(hapd->wps->registrar);
511
512	if (!hapd->conf->ap_setup_locked) {
513		if (hapd->ap_pin_lockout_time == 0)
514			hapd->ap_pin_lockout_time = 60;
515		else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
516			 (hapd->ap_pin_failures % 3) == 0)
517			hapd->ap_pin_lockout_time *= 2;
518
519		wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
520			   hapd->ap_pin_lockout_time);
521		eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
522		eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
523				       hostapd_wps_reenable_ap_pin, hapd,
524				       NULL);
525	}
526
527	return 0;
528}
529
530
531static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
532				  struct wps_event_pwd_auth_fail *data)
533{
534	hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
535}
536
537
538static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
539	"No Error", /* WPS_EI_NO_ERROR */
540	"TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
541	"WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
542};
543
544static void hostapd_wps_event_fail(struct hostapd_data *hapd,
545				   struct wps_event_fail *fail)
546{
547	if (fail->error_indication > 0 &&
548	    fail->error_indication < NUM_WPS_EI_VALUES) {
549		wpa_msg(hapd->msg_ctx, MSG_INFO,
550			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
551			fail->msg, fail->config_error, fail->error_indication,
552			wps_event_fail_reason[fail->error_indication]);
553	} else {
554		wpa_msg(hapd->msg_ctx, MSG_INFO,
555			WPS_EVENT_FAIL "msg=%d config_error=%d",
556			fail->msg, fail->config_error);
557	}
558}
559
560
561static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
562				 union wps_event_data *data)
563{
564	struct hostapd_data *hapd = ctx;
565
566	switch (event) {
567	case WPS_EV_M2D:
568		break;
569	case WPS_EV_FAIL:
570		hostapd_wps_event_fail(hapd, &data->fail);
571		break;
572	case WPS_EV_SUCCESS:
573		break;
574	case WPS_EV_PWD_AUTH_FAIL:
575		hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
576		break;
577	case WPS_EV_PBC_OVERLAP:
578		break;
579	case WPS_EV_PBC_TIMEOUT:
580		break;
581	case WPS_EV_ER_AP_ADD:
582		break;
583	case WPS_EV_ER_AP_REMOVE:
584		break;
585	case WPS_EV_ER_ENROLLEE_ADD:
586		break;
587	case WPS_EV_ER_ENROLLEE_REMOVE:
588		break;
589	case WPS_EV_ER_AP_SETTINGS:
590		break;
591	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
592		break;
593	}
594	if (hapd->wps_event_cb)
595		hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
596}
597
598
599static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
600{
601	wpabuf_free(hapd->wps_beacon_ie);
602	hapd->wps_beacon_ie = NULL;
603
604	wpabuf_free(hapd->wps_probe_resp_ie);
605	hapd->wps_probe_resp_ie = NULL;
606
607	hostapd_set_ap_wps_ie(hapd);
608}
609
610
611static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
612{
613	const u8 **uuid = ctx;
614	size_t j;
615
616	if (iface == NULL)
617		return 0;
618	for (j = 0; j < iface->num_bss; j++) {
619		struct hostapd_data *hapd = iface->bss[j];
620		if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
621			*uuid = hapd->wps->uuid;
622			return 1;
623		}
624	}
625
626	return 0;
627}
628
629
630static const u8 * get_own_uuid(struct hostapd_iface *iface)
631{
632	const u8 *uuid;
633	if (iface->for_each_interface == NULL)
634		return NULL;
635	uuid = NULL;
636	iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
637	return uuid;
638}
639
640
641static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
642{
643	int *count= ctx;
644	(*count)++;
645	return 0;
646}
647
648
649static int interface_count(struct hostapd_iface *iface)
650{
651	int count = 0;
652	if (iface->for_each_interface == NULL)
653		return 0;
654	iface->for_each_interface(iface->interfaces, count_interface_cb,
655				  &count);
656	return count;
657}
658
659
660static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
661				      struct wps_context *wps)
662{
663	int i;
664
665	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
666		wpabuf_free(wps->dev.vendor_ext[i]);
667		wps->dev.vendor_ext[i] = NULL;
668
669		if (hapd->conf->wps_vendor_ext[i] == NULL)
670			continue;
671
672		wps->dev.vendor_ext[i] =
673			wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
674		if (wps->dev.vendor_ext[i] == NULL) {
675			while (--i >= 0)
676				wpabuf_free(wps->dev.vendor_ext[i]);
677			return -1;
678		}
679	}
680
681	return 0;
682}
683
684
685int hostapd_init_wps(struct hostapd_data *hapd,
686		     struct hostapd_bss_config *conf)
687{
688	struct wps_context *wps;
689	struct wps_registrar_config cfg;
690
691	if (conf->wps_state == 0) {
692		hostapd_wps_clear_ies(hapd);
693		return 0;
694	}
695
696	wps = os_zalloc(sizeof(*wps));
697	if (wps == NULL)
698		return -1;
699
700	wps->cred_cb = hostapd_wps_cred_cb;
701	wps->event_cb = hostapd_wps_event_cb;
702	wps->cb_ctx = hapd;
703
704	os_memset(&cfg, 0, sizeof(cfg));
705	wps->wps_state = hapd->conf->wps_state;
706	wps->ap_setup_locked = hapd->conf->ap_setup_locked;
707	if (is_nil_uuid(hapd->conf->uuid)) {
708		const u8 *uuid;
709		uuid = get_own_uuid(hapd->iface);
710		if (uuid) {
711			os_memcpy(wps->uuid, uuid, UUID_LEN);
712			wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
713				    "interface", wps->uuid, UUID_LEN);
714		} else {
715			uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
716			wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
717				    "address", wps->uuid, UUID_LEN);
718		}
719	} else {
720		os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
721		wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
722			    wps->uuid, UUID_LEN);
723	}
724	wps->ssid_len = hapd->conf->ssid.ssid_len;
725	os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
726	wps->ap = 1;
727	os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
728	wps->dev.device_name = hapd->conf->device_name ?
729		os_strdup(hapd->conf->device_name) : NULL;
730	wps->dev.manufacturer = hapd->conf->manufacturer ?
731		os_strdup(hapd->conf->manufacturer) : NULL;
732	wps->dev.model_name = hapd->conf->model_name ?
733		os_strdup(hapd->conf->model_name) : NULL;
734	wps->dev.model_number = hapd->conf->model_number ?
735		os_strdup(hapd->conf->model_number) : NULL;
736	wps->dev.serial_number = hapd->conf->serial_number ?
737		os_strdup(hapd->conf->serial_number) : NULL;
738	wps->config_methods =
739		wps_config_methods_str2bin(hapd->conf->config_methods);
740#ifdef CONFIG_WPS2
741	if ((wps->config_methods &
742	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
743	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
744		wpa_printf(MSG_INFO, "WPS: Converting display to "
745			   "virtual_display for WPS 2.0 compliance");
746		wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
747	}
748	if ((wps->config_methods &
749	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
750	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
751		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
752			   "virtual_push_button for WPS 2.0 compliance");
753		wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
754	}
755#endif /* CONFIG_WPS2 */
756	os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
757		  WPS_DEV_TYPE_LEN);
758
759	if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
760		os_free(wps);
761		return -1;
762	}
763
764	wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
765	wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
766		WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
767
768	if (conf->wpa & WPA_PROTO_RSN) {
769		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
770			wps->auth_types |= WPS_AUTH_WPA2PSK;
771		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
772			wps->auth_types |= WPS_AUTH_WPA2;
773
774		if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
775			wps->encr_types |= WPS_ENCR_AES;
776		if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
777			wps->encr_types |= WPS_ENCR_TKIP;
778	}
779
780	if (conf->wpa & WPA_PROTO_WPA) {
781		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
782			wps->auth_types |= WPS_AUTH_WPAPSK;
783		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
784			wps->auth_types |= WPS_AUTH_WPA;
785
786		if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
787			wps->encr_types |= WPS_ENCR_AES;
788		if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
789			wps->encr_types |= WPS_ENCR_TKIP;
790	}
791
792	if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
793		wps->encr_types |= WPS_ENCR_NONE;
794		wps->auth_types |= WPS_AUTH_OPEN;
795	} else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
796		wps->encr_types |= WPS_ENCR_WEP;
797		if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
798			wps->auth_types |= WPS_AUTH_OPEN;
799		if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
800			wps->auth_types |= WPS_AUTH_SHARED;
801	} else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
802		wps->auth_types |= WPS_AUTH_OPEN;
803		if (conf->default_wep_key_len)
804			wps->encr_types |= WPS_ENCR_WEP;
805		else
806			wps->encr_types |= WPS_ENCR_NONE;
807	}
808
809	if (conf->ssid.wpa_psk_file) {
810		/* Use per-device PSKs */
811	} else if (conf->ssid.wpa_passphrase) {
812		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
813		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
814	} else if (conf->ssid.wpa_psk) {
815		wps->network_key = os_malloc(2 * PMK_LEN + 1);
816		if (wps->network_key == NULL) {
817			os_free(wps);
818			return -1;
819		}
820		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
821				 conf->ssid.wpa_psk->psk, PMK_LEN);
822		wps->network_key_len = 2 * PMK_LEN;
823	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
824		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
825		if (wps->network_key == NULL) {
826			os_free(wps);
827			return -1;
828		}
829		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
830			  conf->ssid.wep.len[0]);
831		wps->network_key_len = conf->ssid.wep.len[0];
832	}
833
834	if (conf->ssid.wpa_psk) {
835		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
836		wps->psk_set = 1;
837	}
838
839	if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
840		/* Override parameters to enable security by default */
841		wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
842		wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
843	}
844
845	wps->ap_settings = conf->ap_settings;
846	wps->ap_settings_len = conf->ap_settings_len;
847
848	cfg.new_psk_cb = hostapd_wps_new_psk_cb;
849	cfg.set_ie_cb = hostapd_wps_set_ie_cb;
850	cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
851	cfg.reg_success_cb = hostapd_wps_reg_success_cb;
852	cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
853	cfg.cb_ctx = hapd;
854	cfg.skip_cred_build = conf->skip_cred_build;
855	cfg.extra_cred = conf->extra_cred;
856	cfg.extra_cred_len = conf->extra_cred_len;
857	cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
858		conf->skip_cred_build;
859	if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
860		cfg.static_wep_only = 1;
861	cfg.dualband = interface_count(hapd->iface) > 1;
862	if (cfg.dualband)
863		wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
864
865	wps->registrar = wps_registrar_init(wps, &cfg);
866	if (wps->registrar == NULL) {
867		wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
868		os_free(wps->network_key);
869		os_free(wps);
870		return -1;
871	}
872
873#ifdef CONFIG_WPS_UPNP
874	wps->friendly_name = hapd->conf->friendly_name;
875	wps->manufacturer_url = hapd->conf->manufacturer_url;
876	wps->model_description = hapd->conf->model_description;
877	wps->model_url = hapd->conf->model_url;
878	wps->upc = hapd->conf->upc;
879
880	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
881		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
882		wps_registrar_deinit(wps->registrar);
883		os_free(wps->network_key);
884		os_free(wps);
885		return -1;
886	}
887#endif /* CONFIG_WPS_UPNP */
888
889	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
890
891	hapd->wps = wps;
892
893	return 0;
894}
895
896
897void hostapd_deinit_wps(struct hostapd_data *hapd)
898{
899	eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
900	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
901	if (hapd->wps == NULL)
902		return;
903#ifdef CONFIG_WPS_UPNP
904	hostapd_wps_upnp_deinit(hapd);
905#endif /* CONFIG_WPS_UPNP */
906	wps_registrar_deinit(hapd->wps->registrar);
907	os_free(hapd->wps->network_key);
908	wps_device_data_free(&hapd->wps->dev);
909	wpabuf_free(hapd->wps->dh_pubkey);
910	wpabuf_free(hapd->wps->dh_privkey);
911	wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
912	wpabuf_free(hapd->wps->oob_conf.dev_password);
913	wps_free_pending_msgs(hapd->wps->upnp_msgs);
914	os_free(hapd->wps);
915	hapd->wps = NULL;
916	hostapd_wps_clear_ies(hapd);
917}
918
919
920void hostapd_update_wps(struct hostapd_data *hapd)
921{
922	if (hapd->wps == NULL)
923		return;
924
925#ifdef CONFIG_WPS_UPNP
926	hapd->wps->friendly_name = hapd->conf->friendly_name;
927	hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
928	hapd->wps->model_description = hapd->conf->model_description;
929	hapd->wps->model_url = hapd->conf->model_url;
930	hapd->wps->upc = hapd->conf->upc;
931#endif /* CONFIG_WPS_UPNP */
932
933	hostapd_wps_set_vendor_ext(hapd, hapd->wps);
934
935	if (hapd->conf->wps_state)
936		wps_registrar_update_ie(hapd->wps->registrar);
937	else
938		hostapd_deinit_wps(hapd);
939}
940
941
942struct wps_add_pin_data {
943	const u8 *addr;
944	const u8 *uuid;
945	const u8 *pin;
946	size_t pin_len;
947	int timeout;
948	int added;
949};
950
951
952static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
953{
954	struct wps_add_pin_data *data = ctx;
955	int ret;
956
957	if (hapd->wps == NULL)
958		return 0;
959	ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
960				    data->uuid, data->pin, data->pin_len,
961				    data->timeout);
962	if (ret == 0)
963		data->added++;
964	return ret;
965}
966
967
968int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
969			const char *uuid, const char *pin, int timeout)
970{
971	u8 u[UUID_LEN];
972	struct wps_add_pin_data data;
973
974	data.addr = addr;
975	data.uuid = u;
976	data.pin = (const u8 *) pin;
977	data.pin_len = os_strlen(pin);
978	data.timeout = timeout;
979	data.added = 0;
980
981	if (os_strcmp(uuid, "any") == 0)
982		data.uuid = NULL;
983	else {
984		if (uuid_str2bin(uuid, u))
985			return -1;
986		data.uuid = u;
987	}
988	if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
989		return -1;
990	return data.added ? 0 : -1;
991}
992
993
994static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
995{
996	const u8 *p2p_dev_addr = ctx;
997	if (hapd->wps == NULL)
998		return 0;
999	return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1000}
1001
1002
1003int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1004			      const u8 *p2p_dev_addr)
1005{
1006	return hostapd_wps_for_each(hapd, wps_button_pushed,
1007				    (void *) p2p_dev_addr);
1008}
1009
1010
1011#ifdef CONFIG_WPS_OOB
1012int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
1013			  char *path, char *method, char *name)
1014{
1015	struct wps_context *wps = hapd->wps;
1016	struct oob_device_data *oob_dev;
1017
1018	oob_dev = wps_get_oob_device(device_type);
1019	if (oob_dev == NULL)
1020		return -1;
1021	oob_dev->device_path = path;
1022	oob_dev->device_name = name;
1023	wps->oob_conf.oob_method = wps_get_oob_method(method);
1024
1025	if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
1026		/*
1027		 * Use pre-configured DH keys in order to be able to write the
1028		 * key hash into the OOB file.
1029		 */
1030		wpabuf_free(wps->dh_pubkey);
1031		wpabuf_free(wps->dh_privkey);
1032		wps->dh_privkey = NULL;
1033		wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1034					 &wps->dh_privkey);
1035		wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1036		if (wps->dh_pubkey == NULL) {
1037			wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1038				   "Diffie-Hellman handshake");
1039			return -1;
1040		}
1041	}
1042
1043	if (wps_process_oob(wps, oob_dev, 1) < 0)
1044		goto error;
1045
1046	if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1047	     wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1048	    hostapd_wps_add_pin(hapd, NULL, "any",
1049				wpabuf_head(wps->oob_conf.dev_password), 0) <
1050	    0)
1051		goto error;
1052
1053	return 0;
1054
1055error:
1056	wpabuf_free(wps->dh_pubkey);
1057	wps->dh_pubkey = NULL;
1058	wpabuf_free(wps->dh_privkey);
1059	wps->dh_privkey = NULL;
1060	return -1;
1061}
1062#endif /* CONFIG_WPS_OOB */
1063
1064
1065static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
1066				    const u8 *ie, size_t ie_len)
1067{
1068	struct hostapd_data *hapd = ctx;
1069	struct wpabuf *wps_ie;
1070	struct ieee802_11_elems elems;
1071
1072	if (hapd->wps == NULL)
1073		return 0;
1074
1075	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1076		wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1077			   MACSTR, MAC2STR(addr));
1078		return 0;
1079	}
1080
1081	if (elems.ssid && elems.ssid_len > 0 &&
1082	    (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1083	     os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1084	     0))
1085		return 0; /* Not for us */
1086
1087	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1088	if (wps_ie == NULL)
1089		return 0;
1090	if (wps_validate_probe_req(wps_ie, addr) < 0) {
1091		wpabuf_free(wps_ie);
1092		return 0;
1093	}
1094
1095	if (wpabuf_len(wps_ie) > 0) {
1096		int p2p_wildcard = 0;
1097#ifdef CONFIG_P2P
1098		if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1099		    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1100			      P2P_WILDCARD_SSID_LEN) == 0)
1101			p2p_wildcard = 1;
1102#endif /* CONFIG_P2P */
1103		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1104					   p2p_wildcard);
1105#ifdef CONFIG_WPS_UPNP
1106		/* FIX: what exactly should be included in the WLANEvent?
1107		 * WPS attributes? Full ProbeReq frame? */
1108		if (!p2p_wildcard)
1109			upnp_wps_device_send_wlan_event(
1110				hapd->wps_upnp, addr,
1111				UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1112#endif /* CONFIG_WPS_UPNP */
1113	}
1114
1115	wpabuf_free(wps_ie);
1116
1117	return 0;
1118}
1119
1120
1121#ifdef CONFIG_WPS_UPNP
1122
1123static int hostapd_rx_req_put_wlan_response(
1124	void *priv, enum upnp_wps_wlanevent_type ev_type,
1125	const u8 *mac_addr, const struct wpabuf *msg,
1126	enum wps_msg_type msg_type)
1127{
1128	struct hostapd_data *hapd = priv;
1129	struct sta_info *sta;
1130	struct upnp_pending_message *p;
1131
1132	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1133		   MACSTR, ev_type, MAC2STR(mac_addr));
1134	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1135		    wpabuf_head(msg), wpabuf_len(msg));
1136	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1137		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1138			   "PutWLANResponse WLANEventType %d", ev_type);
1139		return -1;
1140	}
1141
1142	/*
1143	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1144	 * server implementation for delivery to the peer.
1145	 */
1146
1147	sta = ap_get_sta(hapd, mac_addr);
1148#ifndef CONFIG_WPS_STRICT
1149	if (!sta) {
1150		/*
1151		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1152		 * Pick STA that is in an ongoing WPS registration without
1153		 * checking the MAC address.
1154		 */
1155		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1156			   "on NewWLANEventMAC; try wildcard match");
1157		for (sta = hapd->sta_list; sta; sta = sta->next) {
1158			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1159				break;
1160		}
1161	}
1162#endif /* CONFIG_WPS_STRICT */
1163
1164	if (!sta) {
1165		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1166		return 0;
1167	}
1168
1169	p = os_zalloc(sizeof(*p));
1170	if (p == NULL)
1171		return -1;
1172	os_memcpy(p->addr, sta->addr, ETH_ALEN);
1173	p->msg = wpabuf_dup(msg);
1174	p->type = msg_type;
1175	p->next = hapd->wps->upnp_msgs;
1176	hapd->wps->upnp_msgs = p;
1177
1178	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1179}
1180
1181
1182static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1183				 struct wps_context *wps)
1184{
1185	struct upnp_wps_device_ctx *ctx;
1186
1187	if (!hapd->conf->upnp_iface)
1188		return 0;
1189	ctx = os_zalloc(sizeof(*ctx));
1190	if (ctx == NULL)
1191		return -1;
1192
1193	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1194	if (hapd->conf->ap_pin)
1195		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1196
1197	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1198					      hapd->conf->upnp_iface);
1199	if (hapd->wps_upnp == NULL)
1200		return -1;
1201	wps->wps_upnp = hapd->wps_upnp;
1202
1203	return 0;
1204}
1205
1206
1207static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1208{
1209	upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1210}
1211
1212#endif /* CONFIG_WPS_UPNP */
1213
1214
1215int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1216			    char *buf, size_t buflen)
1217{
1218	if (hapd->wps == NULL)
1219		return 0;
1220	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1221}
1222
1223
1224static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1225{
1226	struct hostapd_data *hapd = eloop_data;
1227	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1228	hostapd_wps_ap_pin_disable(hapd);
1229}
1230
1231
1232static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1233{
1234	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1235	hapd->ap_pin_failures = 0;
1236	hapd->conf->ap_setup_locked = 0;
1237	if (hapd->wps->ap_setup_locked) {
1238		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1239		hapd->wps->ap_setup_locked = 0;
1240		wps_registrar_update_ie(hapd->wps->registrar);
1241	}
1242	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1243	if (timeout > 0)
1244		eloop_register_timeout(timeout, 0,
1245				       hostapd_wps_ap_pin_timeout, hapd, NULL);
1246}
1247
1248
1249static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1250{
1251	os_free(hapd->conf->ap_pin);
1252	hapd->conf->ap_pin = NULL;
1253#ifdef CONFIG_WPS_UPNP
1254	upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1255#endif /* CONFIG_WPS_UPNP */
1256	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1257	return 0;
1258}
1259
1260
1261void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1262{
1263	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1264	hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1265}
1266
1267
1268struct wps_ap_pin_data {
1269	char pin_txt[9];
1270	int timeout;
1271};
1272
1273
1274static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1275{
1276	struct wps_ap_pin_data *data = ctx;
1277	os_free(hapd->conf->ap_pin);
1278	hapd->conf->ap_pin = os_strdup(data->pin_txt);
1279#ifdef CONFIG_WPS_UPNP
1280	upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1281#endif /* CONFIG_WPS_UPNP */
1282	hostapd_wps_ap_pin_enable(hapd, data->timeout);
1283	return 0;
1284}
1285
1286
1287const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1288{
1289	unsigned int pin;
1290	struct wps_ap_pin_data data;
1291
1292	pin = wps_generate_pin();
1293	os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1294	data.timeout = timeout;
1295	hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1296	return hapd->conf->ap_pin;
1297}
1298
1299
1300const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1301{
1302	return hapd->conf->ap_pin;
1303}
1304
1305
1306int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1307			   int timeout)
1308{
1309	struct wps_ap_pin_data data;
1310	int ret;
1311
1312	ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1313	if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1314		return -1;
1315	data.timeout = timeout;
1316	return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1317}
1318
1319
1320static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1321{
1322	if (hapd->wps)
1323		wps_registrar_update_ie(hapd->wps->registrar);
1324	return 0;
1325}
1326
1327
1328void hostapd_wps_update_ie(struct hostapd_data *hapd)
1329{
1330	hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1331}
1332
1333
1334int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1335			  const char *auth, const char *encr, const char *key)
1336{
1337	struct wps_credential cred;
1338	size_t len;
1339
1340	os_memset(&cred, 0, sizeof(cred));
1341
1342	len = os_strlen(ssid);
1343	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1344	    hexstr2bin(ssid, cred.ssid, len / 2))
1345		return -1;
1346	cred.ssid_len = len / 2;
1347
1348	if (os_strncmp(auth, "OPEN", 4) == 0)
1349		cred.auth_type = WPS_AUTH_OPEN;
1350	else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1351		cred.auth_type = WPS_AUTH_WPAPSK;
1352	else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1353		cred.auth_type = WPS_AUTH_WPA2PSK;
1354	else
1355		return -1;
1356
1357	if (encr) {
1358		if (os_strncmp(encr, "NONE", 4) == 0)
1359			cred.encr_type = WPS_ENCR_NONE;
1360		else if (os_strncmp(encr, "WEP", 3) == 0)
1361			cred.encr_type = WPS_ENCR_WEP;
1362		else if (os_strncmp(encr, "TKIP", 4) == 0)
1363			cred.encr_type = WPS_ENCR_TKIP;
1364		else if (os_strncmp(encr, "CCMP", 4) == 0)
1365			cred.encr_type = WPS_ENCR_AES;
1366		else
1367			return -1;
1368	} else
1369		cred.encr_type = WPS_ENCR_NONE;
1370
1371	if (key) {
1372		len = os_strlen(key);
1373		if ((len & 1) || len > 2 * sizeof(cred.key) ||
1374		    hexstr2bin(key, cred.key, len / 2))
1375			return -1;
1376		cred.key_len = len / 2;
1377	}
1378
1379	return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1380}
1381