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