wps_hostapd.c revision 75ecf5267604f166b85a7ee2cf0d9cb682966680
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#endif /* CONFIG_WPS_UPNP */
880
881	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
882
883	hapd->wps = wps;
884
885	return 0;
886}
887
888
889int hostapd_init_wps_complete(struct hostapd_data *hapd)
890{
891	struct wps_context *wps = hapd->wps;
892
893	if (wps == NULL)
894		return 0;
895
896#ifdef CONFIG_WPS_UPNP
897	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
898		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
899		wps_registrar_deinit(wps->registrar);
900		os_free(wps->network_key);
901		os_free(wps);
902		hapd->wps = NULL;
903		return -1;
904	}
905#endif /* CONFIG_WPS_UPNP */
906
907	return 0;
908}
909
910
911void hostapd_deinit_wps(struct hostapd_data *hapd)
912{
913	eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
914	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
915	if (hapd->wps == NULL)
916		return;
917#ifdef CONFIG_WPS_UPNP
918	hostapd_wps_upnp_deinit(hapd);
919#endif /* CONFIG_WPS_UPNP */
920	wps_registrar_deinit(hapd->wps->registrar);
921	os_free(hapd->wps->network_key);
922	wps_device_data_free(&hapd->wps->dev);
923	wpabuf_free(hapd->wps->dh_pubkey);
924	wpabuf_free(hapd->wps->dh_privkey);
925	wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
926	wpabuf_free(hapd->wps->oob_conf.dev_password);
927	wps_free_pending_msgs(hapd->wps->upnp_msgs);
928	os_free(hapd->wps);
929	hapd->wps = NULL;
930	hostapd_wps_clear_ies(hapd);
931}
932
933
934void hostapd_update_wps(struct hostapd_data *hapd)
935{
936	if (hapd->wps == NULL)
937		return;
938
939#ifdef CONFIG_WPS_UPNP
940	hapd->wps->friendly_name = hapd->conf->friendly_name;
941	hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
942	hapd->wps->model_description = hapd->conf->model_description;
943	hapd->wps->model_url = hapd->conf->model_url;
944	hapd->wps->upc = hapd->conf->upc;
945#endif /* CONFIG_WPS_UPNP */
946
947	hostapd_wps_set_vendor_ext(hapd, hapd->wps);
948
949	if (hapd->conf->wps_state)
950		wps_registrar_update_ie(hapd->wps->registrar);
951	else
952		hostapd_deinit_wps(hapd);
953}
954
955
956struct wps_add_pin_data {
957	const u8 *addr;
958	const u8 *uuid;
959	const u8 *pin;
960	size_t pin_len;
961	int timeout;
962	int added;
963};
964
965
966static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
967{
968	struct wps_add_pin_data *data = ctx;
969	int ret;
970
971	if (hapd->wps == NULL)
972		return 0;
973	ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
974				    data->uuid, data->pin, data->pin_len,
975				    data->timeout);
976	if (ret == 0)
977		data->added++;
978	return ret;
979}
980
981
982int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
983			const char *uuid, const char *pin, int timeout)
984{
985	u8 u[UUID_LEN];
986	struct wps_add_pin_data data;
987
988	data.addr = addr;
989	data.uuid = u;
990	data.pin = (const u8 *) pin;
991	data.pin_len = os_strlen(pin);
992	data.timeout = timeout;
993	data.added = 0;
994
995	if (os_strcmp(uuid, "any") == 0)
996		data.uuid = NULL;
997	else {
998		if (uuid_str2bin(uuid, u))
999			return -1;
1000		data.uuid = u;
1001	}
1002	if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1003		return -1;
1004	return data.added ? 0 : -1;
1005}
1006
1007
1008static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1009{
1010	const u8 *p2p_dev_addr = ctx;
1011	if (hapd->wps == NULL)
1012		return 0;
1013	return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1014}
1015
1016
1017int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1018			      const u8 *p2p_dev_addr)
1019{
1020	return hostapd_wps_for_each(hapd, wps_button_pushed,
1021				    (void *) p2p_dev_addr);
1022}
1023
1024
1025#ifdef CONFIG_WPS_OOB
1026int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
1027			  char *path, char *method, char *name)
1028{
1029	struct wps_context *wps = hapd->wps;
1030	struct oob_device_data *oob_dev;
1031
1032	oob_dev = wps_get_oob_device(device_type);
1033	if (oob_dev == NULL)
1034		return -1;
1035	oob_dev->device_path = path;
1036	oob_dev->device_name = name;
1037	wps->oob_conf.oob_method = wps_get_oob_method(method);
1038
1039	if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
1040		/*
1041		 * Use pre-configured DH keys in order to be able to write the
1042		 * key hash into the OOB file.
1043		 */
1044		wpabuf_free(wps->dh_pubkey);
1045		wpabuf_free(wps->dh_privkey);
1046		wps->dh_privkey = NULL;
1047		wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1048					 &wps->dh_privkey);
1049		wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1050		if (wps->dh_pubkey == NULL) {
1051			wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1052				   "Diffie-Hellman handshake");
1053			return -1;
1054		}
1055	}
1056
1057	if (wps_process_oob(wps, oob_dev, 1) < 0)
1058		goto error;
1059
1060	if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1061	     wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1062	    hostapd_wps_add_pin(hapd, NULL, "any",
1063				wpabuf_head(wps->oob_conf.dev_password), 0) <
1064	    0)
1065		goto error;
1066
1067	return 0;
1068
1069error:
1070	wpabuf_free(wps->dh_pubkey);
1071	wps->dh_pubkey = NULL;
1072	wpabuf_free(wps->dh_privkey);
1073	wps->dh_privkey = NULL;
1074	return -1;
1075}
1076#endif /* CONFIG_WPS_OOB */
1077
1078
1079static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
1080				    const u8 *ie, size_t ie_len)
1081{
1082	struct hostapd_data *hapd = ctx;
1083	struct wpabuf *wps_ie;
1084	struct ieee802_11_elems elems;
1085
1086	if (hapd->wps == NULL)
1087		return 0;
1088
1089	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1090		wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1091			   MACSTR, MAC2STR(addr));
1092		return 0;
1093	}
1094
1095	if (elems.ssid && elems.ssid_len > 0 &&
1096	    (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1097	     os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1098	     0))
1099		return 0; /* Not for us */
1100
1101	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1102	if (wps_ie == NULL)
1103		return 0;
1104	if (wps_validate_probe_req(wps_ie, addr) < 0) {
1105		wpabuf_free(wps_ie);
1106		return 0;
1107	}
1108
1109	if (wpabuf_len(wps_ie) > 0) {
1110		int p2p_wildcard = 0;
1111#ifdef CONFIG_P2P
1112		if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1113		    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1114			      P2P_WILDCARD_SSID_LEN) == 0)
1115			p2p_wildcard = 1;
1116#endif /* CONFIG_P2P */
1117		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1118					   p2p_wildcard);
1119#ifdef CONFIG_WPS_UPNP
1120		/* FIX: what exactly should be included in the WLANEvent?
1121		 * WPS attributes? Full ProbeReq frame? */
1122		if (!p2p_wildcard)
1123			upnp_wps_device_send_wlan_event(
1124				hapd->wps_upnp, addr,
1125				UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1126#endif /* CONFIG_WPS_UPNP */
1127	}
1128
1129	wpabuf_free(wps_ie);
1130
1131	return 0;
1132}
1133
1134
1135#ifdef CONFIG_WPS_UPNP
1136
1137static int hostapd_rx_req_put_wlan_response(
1138	void *priv, enum upnp_wps_wlanevent_type ev_type,
1139	const u8 *mac_addr, const struct wpabuf *msg,
1140	enum wps_msg_type msg_type)
1141{
1142	struct hostapd_data *hapd = priv;
1143	struct sta_info *sta;
1144	struct upnp_pending_message *p;
1145
1146	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1147		   MACSTR, ev_type, MAC2STR(mac_addr));
1148	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1149		    wpabuf_head(msg), wpabuf_len(msg));
1150	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1151		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1152			   "PutWLANResponse WLANEventType %d", ev_type);
1153		return -1;
1154	}
1155
1156	/*
1157	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1158	 * server implementation for delivery to the peer.
1159	 */
1160
1161	sta = ap_get_sta(hapd, mac_addr);
1162#ifndef CONFIG_WPS_STRICT
1163	if (!sta) {
1164		/*
1165		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1166		 * Pick STA that is in an ongoing WPS registration without
1167		 * checking the MAC address.
1168		 */
1169		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1170			   "on NewWLANEventMAC; try wildcard match");
1171		for (sta = hapd->sta_list; sta; sta = sta->next) {
1172			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1173				break;
1174		}
1175	}
1176#endif /* CONFIG_WPS_STRICT */
1177
1178	if (!sta) {
1179		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1180		return 0;
1181	}
1182
1183	p = os_zalloc(sizeof(*p));
1184	if (p == NULL)
1185		return -1;
1186	os_memcpy(p->addr, sta->addr, ETH_ALEN);
1187	p->msg = wpabuf_dup(msg);
1188	p->type = msg_type;
1189	p->next = hapd->wps->upnp_msgs;
1190	hapd->wps->upnp_msgs = p;
1191
1192	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1193}
1194
1195
1196static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1197				 struct wps_context *wps)
1198{
1199	struct upnp_wps_device_ctx *ctx;
1200
1201	if (!hapd->conf->upnp_iface)
1202		return 0;
1203	ctx = os_zalloc(sizeof(*ctx));
1204	if (ctx == NULL)
1205		return -1;
1206
1207	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1208	if (hapd->conf->ap_pin)
1209		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1210
1211	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1212					      hapd->conf->upnp_iface);
1213	if (hapd->wps_upnp == NULL)
1214		return -1;
1215	wps->wps_upnp = hapd->wps_upnp;
1216
1217	return 0;
1218}
1219
1220
1221static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1222{
1223	upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1224}
1225
1226#endif /* CONFIG_WPS_UPNP */
1227
1228
1229int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1230			    char *buf, size_t buflen)
1231{
1232	if (hapd->wps == NULL)
1233		return 0;
1234	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1235}
1236
1237
1238static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1239{
1240	struct hostapd_data *hapd = eloop_data;
1241	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1242	hostapd_wps_ap_pin_disable(hapd);
1243}
1244
1245
1246static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1247{
1248	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1249	hapd->ap_pin_failures = 0;
1250	hapd->conf->ap_setup_locked = 0;
1251	if (hapd->wps->ap_setup_locked) {
1252		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1253		hapd->wps->ap_setup_locked = 0;
1254		wps_registrar_update_ie(hapd->wps->registrar);
1255	}
1256	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1257	if (timeout > 0)
1258		eloop_register_timeout(timeout, 0,
1259				       hostapd_wps_ap_pin_timeout, hapd, NULL);
1260}
1261
1262
1263static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1264{
1265	os_free(hapd->conf->ap_pin);
1266	hapd->conf->ap_pin = NULL;
1267#ifdef CONFIG_WPS_UPNP
1268	upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1269#endif /* CONFIG_WPS_UPNP */
1270	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1271	return 0;
1272}
1273
1274
1275void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1276{
1277	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1278	hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1279}
1280
1281
1282struct wps_ap_pin_data {
1283	char pin_txt[9];
1284	int timeout;
1285};
1286
1287
1288static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1289{
1290	struct wps_ap_pin_data *data = ctx;
1291	os_free(hapd->conf->ap_pin);
1292	hapd->conf->ap_pin = os_strdup(data->pin_txt);
1293#ifdef CONFIG_WPS_UPNP
1294	upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1295#endif /* CONFIG_WPS_UPNP */
1296	hostapd_wps_ap_pin_enable(hapd, data->timeout);
1297	return 0;
1298}
1299
1300
1301const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1302{
1303	unsigned int pin;
1304	struct wps_ap_pin_data data;
1305
1306	pin = wps_generate_pin();
1307	os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1308	data.timeout = timeout;
1309	hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1310	return hapd->conf->ap_pin;
1311}
1312
1313
1314const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1315{
1316	return hapd->conf->ap_pin;
1317}
1318
1319
1320int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1321			   int timeout)
1322{
1323	struct wps_ap_pin_data data;
1324	int ret;
1325
1326	ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1327	if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1328		return -1;
1329	data.timeout = timeout;
1330	return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1331}
1332
1333
1334static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1335{
1336	if (hapd->wps)
1337		wps_registrar_update_ie(hapd->wps->registrar);
1338	return 0;
1339}
1340
1341
1342void hostapd_wps_update_ie(struct hostapd_data *hapd)
1343{
1344	hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1345}
1346
1347
1348int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1349			  const char *auth, const char *encr, const char *key)
1350{
1351	struct wps_credential cred;
1352	size_t len;
1353
1354	os_memset(&cred, 0, sizeof(cred));
1355
1356	len = os_strlen(ssid);
1357	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1358	    hexstr2bin(ssid, cred.ssid, len / 2))
1359		return -1;
1360	cred.ssid_len = len / 2;
1361
1362	if (os_strncmp(auth, "OPEN", 4) == 0)
1363		cred.auth_type = WPS_AUTH_OPEN;
1364	else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1365		cred.auth_type = WPS_AUTH_WPAPSK;
1366	else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1367		cred.auth_type = WPS_AUTH_WPA2PSK;
1368	else
1369		return -1;
1370
1371	if (encr) {
1372		if (os_strncmp(encr, "NONE", 4) == 0)
1373			cred.encr_type = WPS_ENCR_NONE;
1374		else if (os_strncmp(encr, "WEP", 3) == 0)
1375			cred.encr_type = WPS_ENCR_WEP;
1376		else if (os_strncmp(encr, "TKIP", 4) == 0)
1377			cred.encr_type = WPS_ENCR_TKIP;
1378		else if (os_strncmp(encr, "CCMP", 4) == 0)
1379			cred.encr_type = WPS_ENCR_AES;
1380		else
1381			return -1;
1382	} else
1383		cred.encr_type = WPS_ENCR_NONE;
1384
1385	if (key) {
1386		len = os_strlen(key);
1387		if ((len & 1) || len > 2 * sizeof(cred.key) ||
1388		    hexstr2bin(key, cred.key, len / 2))
1389			return -1;
1390		cred.key_len = len / 2;
1391	}
1392
1393	return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1394}
1395