blob: 294a39d6fa1f17f4523640a86c6891d4b8d5d667 [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 Shmidt1f69aa52012-01-24 16:10:04 -0800290static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
291 size_t attr_len)
292{
293 size_t blen = attr_len * 2 + 1;
294 char *buf = os_malloc(blen);
295 if (buf) {
296 wpa_snprintf_hex(buf, blen, attr, attr_len);
297 wpa_msg(hapd->msg_ctx, MSG_INFO,
298 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
299 os_free(buf);
300 }
301}
302
303
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700304static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
305 const struct wps_credential *cred)
306{
307 struct hostapd_bss_config *bss = hapd->conf;
308
309 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
310
311 bss->wps_state = 2;
312 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
313 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
314 bss->ssid.ssid_len = cred->ssid_len;
315 bss->ssid.ssid_set = 1;
316 }
317
318 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
319 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
320 bss->wpa = 3;
321 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
322 bss->wpa = 2;
323 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
324 bss->wpa = 1;
325 else
326 bss->wpa = 0;
327
328 if (bss->wpa) {
329 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
330 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
331 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
332 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
333
334 bss->wpa_pairwise = 0;
335 if (cred->encr_type & WPS_ENCR_AES)
336 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
337 if (cred->encr_type & WPS_ENCR_TKIP)
338 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
339 bss->rsn_pairwise = bss->wpa_pairwise;
340 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
341 bss->wpa_pairwise,
342 bss->rsn_pairwise);
343
344 if (cred->key_len >= 8 && cred->key_len < 64) {
345 os_free(bss->ssid.wpa_passphrase);
346 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
347 if (bss->ssid.wpa_passphrase)
348 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
349 cred->key_len);
350 os_free(bss->ssid.wpa_psk);
351 bss->ssid.wpa_psk = NULL;
352 } else if (cred->key_len == 64) {
353 os_free(bss->ssid.wpa_psk);
354 bss->ssid.wpa_psk =
355 os_zalloc(sizeof(struct hostapd_wpa_psk));
356 if (bss->ssid.wpa_psk &&
357 hexstr2bin((const char *) cred->key,
358 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
359 bss->ssid.wpa_psk->group = 1;
360 os_free(bss->ssid.wpa_passphrase);
361 bss->ssid.wpa_passphrase = NULL;
362 }
363 }
364 bss->auth_algs = 1;
365 } else {
366 if ((cred->auth_type & WPS_AUTH_OPEN) &&
367 (cred->auth_type & WPS_AUTH_SHARED))
368 bss->auth_algs = 3;
369 else if (cred->auth_type & WPS_AUTH_SHARED)
370 bss->auth_algs = 2;
371 else
372 bss->auth_algs = 1;
373 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
374 cred->key_idx <= 4) {
375 struct hostapd_wep_keys *wep = &bss->ssid.wep;
376 int idx = cred->key_idx;
377 if (idx)
378 idx--;
379 wep->idx = idx;
380 if (cred->key_len == 10 || cred->key_len == 26) {
381 os_free(wep->key[idx]);
382 wep->key[idx] = os_malloc(cred->key_len / 2);
383 if (wep->key[idx] == NULL ||
384 hexstr2bin((const char *) cred->key,
385 wep->key[idx],
386 cred->key_len / 2))
387 return -1;
388 wep->len[idx] = cred->key_len / 2;
389 } else {
390 os_free(wep->key[idx]);
391 wep->key[idx] = os_malloc(cred->key_len);
392 if (wep->key[idx] == NULL)
393 return -1;
394 os_memcpy(wep->key[idx], cred->key,
395 cred->key_len);
396 wep->len[idx] = cred->key_len;
397 }
398 wep->keys_set = 1;
399 }
400 }
401
402 /* Schedule configuration reload after short period of time to allow
403 * EAP-WSC to be finished.
404 */
405 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
406 NULL);
407
408 return 0;
409}
410
411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
413{
414 const struct wps_credential *cred = ctx;
415 FILE *oconf, *nconf;
416 size_t len, i;
417 char *tmp_fname;
418 char buf[1024];
419 int multi_bss;
420 int wpa;
421
422 if (hapd->wps == NULL)
423 return 0;
424
425 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
426 cred->cred_attr, cred->cred_attr_len);
427
428 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
429 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
430 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
431 cred->auth_type);
432 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
433 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
434 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
435 cred->key, cred->key_len);
436 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
437 MAC2STR(cred->mac_addr));
438
439 if ((hapd->conf->wps_cred_processing == 1 ||
440 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800441 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
442 } else if (hapd->conf->wps_cred_processing == 1 ||
443 hapd->conf->wps_cred_processing == 2) {
444 struct wpabuf *attr;
445 attr = wpabuf_alloc(200);
446 if (attr && wps_build_credential_wrap(attr, cred) == 0)
447 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
448 wpabuf_len(attr));
449 wpabuf_free(attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 } else
451 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
452
453 if (hapd->conf->wps_cred_processing == 1)
454 return 0;
455
456 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
457 hapd->wps->ssid_len = cred->ssid_len;
458 hapd->wps->encr_types = cred->encr_type;
459 hapd->wps->auth_types = cred->auth_type;
460 if (cred->key_len == 0) {
461 os_free(hapd->wps->network_key);
462 hapd->wps->network_key = NULL;
463 hapd->wps->network_key_len = 0;
464 } else {
465 if (hapd->wps->network_key == NULL ||
466 hapd->wps->network_key_len < cred->key_len) {
467 hapd->wps->network_key_len = 0;
468 os_free(hapd->wps->network_key);
469 hapd->wps->network_key = os_malloc(cred->key_len);
470 if (hapd->wps->network_key == NULL)
471 return -1;
472 }
473 hapd->wps->network_key_len = cred->key_len;
474 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
475 }
476 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700478 if (hapd->iface->config_fname == NULL)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700479 return hapd_wps_reconfig_in_memory(hapd, cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 len = os_strlen(hapd->iface->config_fname) + 5;
481 tmp_fname = os_malloc(len);
482 if (tmp_fname == NULL)
483 return -1;
484 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
485
486 oconf = fopen(hapd->iface->config_fname, "r");
487 if (oconf == NULL) {
488 wpa_printf(MSG_WARNING, "WPS: Could not open current "
489 "configuration file");
490 os_free(tmp_fname);
491 return -1;
492 }
493
494 nconf = fopen(tmp_fname, "w");
495 if (nconf == NULL) {
496 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
497 "configuration file");
498 os_free(tmp_fname);
499 fclose(oconf);
500 return -1;
501 }
502
503 fprintf(nconf, "# WPS configuration - START\n");
504
505 fprintf(nconf, "wps_state=2\n");
506
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700507 if (is_hex(cred->ssid, cred->ssid_len)) {
508 fprintf(nconf, "ssid2=");
509 for (i = 0; i < cred->ssid_len; i++)
510 fprintf(nconf, "%02x", cred->ssid[i]);
511 fprintf(nconf, "\n");
512 } else {
513 fprintf(nconf, "ssid=");
514 for (i = 0; i < cred->ssid_len; i++)
515 fputc(cred->ssid[i], nconf);
516 fprintf(nconf, "\n");
517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518
519 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
520 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
521 wpa = 3;
522 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
523 wpa = 2;
524 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
525 wpa = 1;
526 else
527 wpa = 0;
528
529 if (wpa) {
530 char *prefix;
531 fprintf(nconf, "wpa=%d\n", wpa);
532
533 fprintf(nconf, "wpa_key_mgmt=");
534 prefix = "";
535 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
536 fprintf(nconf, "WPA-EAP");
537 prefix = " ";
538 }
539 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
540 fprintf(nconf, "%sWPA-PSK", prefix);
541 fprintf(nconf, "\n");
542
543 fprintf(nconf, "wpa_pairwise=");
544 prefix = "";
545 if (cred->encr_type & WPS_ENCR_AES) {
546 fprintf(nconf, "CCMP");
547 prefix = " ";
548 }
549 if (cred->encr_type & WPS_ENCR_TKIP) {
550 fprintf(nconf, "%sTKIP", prefix);
551 }
552 fprintf(nconf, "\n");
553
554 if (cred->key_len >= 8 && cred->key_len < 64) {
555 fprintf(nconf, "wpa_passphrase=");
556 for (i = 0; i < cred->key_len; i++)
557 fputc(cred->key[i], nconf);
558 fprintf(nconf, "\n");
559 } else if (cred->key_len == 64) {
560 fprintf(nconf, "wpa_psk=");
561 for (i = 0; i < cred->key_len; i++)
562 fputc(cred->key[i], nconf);
563 fprintf(nconf, "\n");
564 } else {
565 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
566 "for WPA/WPA2",
567 (unsigned long) cred->key_len);
568 }
569
570 fprintf(nconf, "auth_algs=1\n");
571 } else {
572 if ((cred->auth_type & WPS_AUTH_OPEN) &&
573 (cred->auth_type & WPS_AUTH_SHARED))
574 fprintf(nconf, "auth_algs=3\n");
575 else if (cred->auth_type & WPS_AUTH_SHARED)
576 fprintf(nconf, "auth_algs=2\n");
577 else
578 fprintf(nconf, "auth_algs=1\n");
579
580 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
581 int key_idx = cred->key_idx;
582 if (key_idx)
583 key_idx--;
584 fprintf(nconf, "wep_default_key=%d\n", key_idx);
585 fprintf(nconf, "wep_key%d=", key_idx);
586 if (cred->key_len == 10 || cred->key_len == 26) {
587 /* WEP key as a hex string */
588 for (i = 0; i < cred->key_len; i++)
589 fputc(cred->key[i], nconf);
590 } else {
591 /* Raw WEP key; convert to hex */
592 for (i = 0; i < cred->key_len; i++)
593 fprintf(nconf, "%02x", cred->key[i]);
594 }
595 fprintf(nconf, "\n");
596 }
597 }
598
599 fprintf(nconf, "# WPS configuration - END\n");
600
601 multi_bss = 0;
602 while (fgets(buf, sizeof(buf), oconf)) {
603 if (os_strncmp(buf, "bss=", 4) == 0)
604 multi_bss = 1;
605 if (!multi_bss &&
606 (str_starts(buf, "ssid=") ||
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700607 str_starts(buf, "ssid2=") ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 str_starts(buf, "auth_algs=") ||
609 str_starts(buf, "wep_default_key=") ||
610 str_starts(buf, "wep_key") ||
611 str_starts(buf, "wps_state=") ||
612 str_starts(buf, "wpa=") ||
613 str_starts(buf, "wpa_psk=") ||
614 str_starts(buf, "wpa_pairwise=") ||
615 str_starts(buf, "rsn_pairwise=") ||
616 str_starts(buf, "wpa_key_mgmt=") ||
617 str_starts(buf, "wpa_passphrase="))) {
618 fprintf(nconf, "#WPS# %s", buf);
619 } else
620 fprintf(nconf, "%s", buf);
621 }
622
623 fclose(nconf);
624 fclose(oconf);
625
626 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
627 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
628 "configuration file: %s", strerror(errno));
629 os_free(tmp_fname);
630 return -1;
631 }
632
633 os_free(tmp_fname);
634
635 /* Schedule configuration reload after short period of time to allow
636 * EAP-WSC to be finished.
637 */
638 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
639 NULL);
640
641 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
642
643 return 0;
644}
645
646
647static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
648{
649 struct hostapd_data *hapd = ctx;
650 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
651}
652
653
654static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
655{
656 struct hostapd_data *hapd = eloop_data;
657
658 if (hapd->conf->ap_setup_locked)
659 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800660 if (hapd->ap_pin_failures_consecutive >= 10)
661 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662
663 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
664 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
665 hapd->wps->ap_setup_locked = 0;
666 wps_registrar_update_ie(hapd->wps->registrar);
667}
668
669
670static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
671{
672 struct wps_event_pwd_auth_fail *data = ctx;
673
674 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
675 return 0;
676
677 /*
678 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
679 * for some time if this happens multiple times to slow down brute
680 * force attacks.
681 */
682 hapd->ap_pin_failures++;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800683 hapd->ap_pin_failures_consecutive++;
684 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
685 "(%u consecutive)",
686 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 if (hapd->ap_pin_failures < 3)
688 return 0;
689
690 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
691 hapd->wps->ap_setup_locked = 1;
692
693 wps_registrar_update_ie(hapd->wps->registrar);
694
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800695 if (!hapd->conf->ap_setup_locked &&
696 hapd->ap_pin_failures_consecutive >= 10) {
697 /*
698 * In indefinite lockdown - disable automatic AP PIN
699 * reenablement.
700 */
701 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
702 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
703 } else if (!hapd->conf->ap_setup_locked) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 if (hapd->ap_pin_lockout_time == 0)
705 hapd->ap_pin_lockout_time = 60;
706 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
707 (hapd->ap_pin_failures % 3) == 0)
708 hapd->ap_pin_lockout_time *= 2;
709
710 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
711 hapd->ap_pin_lockout_time);
712 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
713 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
714 hostapd_wps_reenable_ap_pin, hapd,
715 NULL);
716 }
717
718 return 0;
719}
720
721
722static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
723 struct wps_event_pwd_auth_fail *data)
724{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700725 /* Update WPS Status - Authentication Failure */
726 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
727 hapd->wps_stats.status = WPS_STATUS_FAILURE;
728 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
729 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
732}
733
734
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800735static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
736{
737 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
738 return 0;
739
740 if (hapd->ap_pin_failures_consecutive == 0)
741 return 0;
742
743 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
744 "- total validation failures %u (%u consecutive)",
745 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
746 hapd->ap_pin_failures_consecutive = 0;
747
748 return 0;
749}
750
751
752static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
753{
754 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
755}
756
757
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700758static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
759{
760 /* Update WPS Status - PBC Overlap */
761 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
762}
763
764
765static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
766{
767 /* Update WPS PBC Status:PBC Timeout */
768 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
769}
770
771
772static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
773{
774 /* Update WPS PBC status - Active */
775 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
776}
777
778
779static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
780{
781 /* Update WPS PBC status - Active */
782 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
783}
784
785
786static void hostapd_wps_event_success(struct hostapd_data *hapd,
787 struct wps_event_success *success)
788{
789 /* Update WPS status - Success */
790 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
791 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
792 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
793}
794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795
796static void hostapd_wps_event_fail(struct hostapd_data *hapd,
797 struct wps_event_fail *fail)
798{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700799 /* Update WPS status - Failure */
800 hapd->wps_stats.status = WPS_STATUS_FAILURE;
801 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
802
803 hapd->wps_stats.failure_reason = fail->error_indication;
804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 if (fail->error_indication > 0 &&
806 fail->error_indication < NUM_WPS_EI_VALUES) {
807 wpa_msg(hapd->msg_ctx, MSG_INFO,
808 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
809 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700810 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 } else {
812 wpa_msg(hapd->msg_ctx, MSG_INFO,
813 WPS_EVENT_FAIL "msg=%d config_error=%d",
814 fail->msg, fail->config_error);
815 }
816}
817
818
819static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
820 union wps_event_data *data)
821{
822 struct hostapd_data *hapd = ctx;
823
824 switch (event) {
825 case WPS_EV_M2D:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 break;
828 case WPS_EV_FAIL:
829 hostapd_wps_event_fail(hapd, &data->fail);
830 break;
831 case WPS_EV_SUCCESS:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700832 hostapd_wps_event_success(hapd, &data->success);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800833 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 break;
835 case WPS_EV_PWD_AUTH_FAIL:
836 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
837 break;
838 case WPS_EV_PBC_OVERLAP:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700839 hostapd_wps_event_pbc_overlap(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 break;
842 case WPS_EV_PBC_TIMEOUT:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700843 hostapd_wps_event_pbc_timeout(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700846 case WPS_EV_PBC_ACTIVE:
847 hostapd_wps_event_pbc_active(hapd);
848 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
849 break;
850 case WPS_EV_PBC_DISABLE:
851 hostapd_wps_event_pbc_disable(hapd);
852 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
853 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 case WPS_EV_ER_AP_ADD:
855 break;
856 case WPS_EV_ER_AP_REMOVE:
857 break;
858 case WPS_EV_ER_ENROLLEE_ADD:
859 break;
860 case WPS_EV_ER_ENROLLEE_REMOVE:
861 break;
862 case WPS_EV_ER_AP_SETTINGS:
863 break;
864 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
865 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800866 case WPS_EV_AP_PIN_SUCCESS:
867 hostapd_wps_ap_pin_success(hapd);
868 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 }
870 if (hapd->wps_event_cb)
871 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
872}
873
874
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700875static int hostapd_wps_rf_band_cb(void *ctx)
876{
877 struct hostapd_data *hapd = ctx;
878
879 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
880 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
881}
882
883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
885{
886 wpabuf_free(hapd->wps_beacon_ie);
887 hapd->wps_beacon_ie = NULL;
888
889 wpabuf_free(hapd->wps_probe_resp_ie);
890 hapd->wps_probe_resp_ie = NULL;
891
892 hostapd_set_ap_wps_ie(hapd);
893}
894
895
896static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
897{
898 const u8 **uuid = ctx;
899 size_t j;
900
901 if (iface == NULL)
902 return 0;
903 for (j = 0; j < iface->num_bss; j++) {
904 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700905 if (hapd->wps && !hapd->conf->wps_independent &&
906 !is_nil_uuid(hapd->wps->uuid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 *uuid = hapd->wps->uuid;
908 return 1;
909 }
910 }
911
912 return 0;
913}
914
915
916static const u8 * get_own_uuid(struct hostapd_iface *iface)
917{
918 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919 if (iface->interfaces == NULL ||
920 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 return NULL;
922 uuid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700923 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
924 &uuid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 return uuid;
926}
927
928
929static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
930{
931 int *count= ctx;
932 (*count)++;
933 return 0;
934}
935
936
937static int interface_count(struct hostapd_iface *iface)
938{
939 int count = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 if (iface->interfaces == NULL ||
941 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700943 iface->interfaces->for_each_interface(iface->interfaces,
944 count_interface_cb, &count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 return count;
946}
947
948
949static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
950 struct wps_context *wps)
951{
952 int i;
953
954 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
955 wpabuf_free(wps->dev.vendor_ext[i]);
956 wps->dev.vendor_ext[i] = NULL;
957
958 if (hapd->conf->wps_vendor_ext[i] == NULL)
959 continue;
960
961 wps->dev.vendor_ext[i] =
962 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
963 if (wps->dev.vendor_ext[i] == NULL) {
964 while (--i >= 0)
965 wpabuf_free(wps->dev.vendor_ext[i]);
966 return -1;
967 }
968 }
969
970 return 0;
971}
972
973
974int hostapd_init_wps(struct hostapd_data *hapd,
975 struct hostapd_bss_config *conf)
976{
977 struct wps_context *wps;
978 struct wps_registrar_config cfg;
979
980 if (conf->wps_state == 0) {
981 hostapd_wps_clear_ies(hapd);
982 return 0;
983 }
984
985 wps = os_zalloc(sizeof(*wps));
986 if (wps == NULL)
987 return -1;
988
989 wps->cred_cb = hostapd_wps_cred_cb;
990 wps->event_cb = hostapd_wps_event_cb;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700991 wps->rf_band_cb = hostapd_wps_rf_band_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 wps->cb_ctx = hapd;
993
994 os_memset(&cfg, 0, sizeof(cfg));
995 wps->wps_state = hapd->conf->wps_state;
996 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
997 if (is_nil_uuid(hapd->conf->uuid)) {
998 const u8 *uuid;
999 uuid = get_own_uuid(hapd->iface);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001000 if (uuid && !conf->wps_independent) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 os_memcpy(wps->uuid, uuid, UUID_LEN);
1002 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1003 "interface", wps->uuid, UUID_LEN);
1004 } else {
1005 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1006 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1007 "address", wps->uuid, UUID_LEN);
1008 }
1009 } else {
1010 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1011 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1012 wps->uuid, UUID_LEN);
1013 }
1014 wps->ssid_len = hapd->conf->ssid.ssid_len;
1015 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1016 wps->ap = 1;
1017 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1018 wps->dev.device_name = hapd->conf->device_name ?
1019 os_strdup(hapd->conf->device_name) : NULL;
1020 wps->dev.manufacturer = hapd->conf->manufacturer ?
1021 os_strdup(hapd->conf->manufacturer) : NULL;
1022 wps->dev.model_name = hapd->conf->model_name ?
1023 os_strdup(hapd->conf->model_name) : NULL;
1024 wps->dev.model_number = hapd->conf->model_number ?
1025 os_strdup(hapd->conf->model_number) : NULL;
1026 wps->dev.serial_number = hapd->conf->serial_number ?
1027 os_strdup(hapd->conf->serial_number) : NULL;
1028 wps->config_methods =
1029 wps_config_methods_str2bin(hapd->conf->config_methods);
1030#ifdef CONFIG_WPS2
1031 if ((wps->config_methods &
1032 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1033 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1034 wpa_printf(MSG_INFO, "WPS: Converting display to "
1035 "virtual_display for WPS 2.0 compliance");
1036 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1037 }
1038 if ((wps->config_methods &
1039 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1040 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1041 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1042 "virtual_push_button for WPS 2.0 compliance");
1043 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1044 }
1045#endif /* CONFIG_WPS2 */
1046 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1047 WPS_DEV_TYPE_LEN);
1048
1049 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
1050 os_free(wps);
1051 return -1;
1052 }
1053
1054 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055
1056 if (conf->wps_rf_bands) {
1057 wps->dev.rf_bands = conf->wps_rf_bands;
1058 } else {
1059 wps->dev.rf_bands =
1060 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1061 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063
1064 if (conf->wpa & WPA_PROTO_RSN) {
1065 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1066 wps->auth_types |= WPS_AUTH_WPA2PSK;
1067 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1068 wps->auth_types |= WPS_AUTH_WPA2;
1069
1070 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
1071 wps->encr_types |= WPS_ENCR_AES;
1072 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1073 wps->encr_types |= WPS_ENCR_TKIP;
1074 }
1075
1076 if (conf->wpa & WPA_PROTO_WPA) {
1077 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1078 wps->auth_types |= WPS_AUTH_WPAPSK;
1079 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1080 wps->auth_types |= WPS_AUTH_WPA;
1081
1082 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1083 wps->encr_types |= WPS_ENCR_AES;
1084 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1085 wps->encr_types |= WPS_ENCR_TKIP;
1086 }
1087
1088 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1089 wps->encr_types |= WPS_ENCR_NONE;
1090 wps->auth_types |= WPS_AUTH_OPEN;
1091 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
1092 wps->encr_types |= WPS_ENCR_WEP;
1093 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
1094 wps->auth_types |= WPS_AUTH_OPEN;
1095 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
1096 wps->auth_types |= WPS_AUTH_SHARED;
1097 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
1098 wps->auth_types |= WPS_AUTH_OPEN;
1099 if (conf->default_wep_key_len)
1100 wps->encr_types |= WPS_ENCR_WEP;
1101 else
1102 wps->encr_types |= WPS_ENCR_NONE;
1103 }
1104
1105 if (conf->ssid.wpa_psk_file) {
1106 /* Use per-device PSKs */
1107 } else if (conf->ssid.wpa_passphrase) {
1108 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1109 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1110 } else if (conf->ssid.wpa_psk) {
1111 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1112 if (wps->network_key == NULL) {
1113 os_free(wps);
1114 return -1;
1115 }
1116 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1117 conf->ssid.wpa_psk->psk, PMK_LEN);
1118 wps->network_key_len = 2 * PMK_LEN;
1119 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1120 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1121 if (wps->network_key == NULL) {
1122 os_free(wps);
1123 return -1;
1124 }
1125 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1126 conf->ssid.wep.len[0]);
1127 wps->network_key_len = conf->ssid.wep.len[0];
1128 }
1129
1130 if (conf->ssid.wpa_psk) {
1131 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1132 wps->psk_set = 1;
1133 }
1134
1135 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1136 /* Override parameters to enable security by default */
1137 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1138 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1139 }
1140
1141 wps->ap_settings = conf->ap_settings;
1142 wps->ap_settings_len = conf->ap_settings_len;
1143
1144 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1145 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1146 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1147 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1148 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1149 cfg.cb_ctx = hapd;
1150 cfg.skip_cred_build = conf->skip_cred_build;
1151 cfg.extra_cred = conf->extra_cred;
1152 cfg.extra_cred_len = conf->extra_cred_len;
1153 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1154 conf->skip_cred_build;
1155 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1156 cfg.static_wep_only = 1;
1157 cfg.dualband = interface_count(hapd->iface) > 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001158 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1159 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1160 cfg.dualband = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (cfg.dualband)
1162 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001163 cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164
1165 wps->registrar = wps_registrar_init(wps, &cfg);
1166 if (wps->registrar == NULL) {
1167 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1168 os_free(wps->network_key);
1169 os_free(wps);
1170 return -1;
1171 }
1172
1173#ifdef CONFIG_WPS_UPNP
1174 wps->friendly_name = hapd->conf->friendly_name;
1175 wps->manufacturer_url = hapd->conf->manufacturer_url;
1176 wps->model_description = hapd->conf->model_description;
1177 wps->model_url = hapd->conf->model_url;
1178 wps->upc = hapd->conf->upc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179#endif /* CONFIG_WPS_UPNP */
1180
1181 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1182
1183 hapd->wps = wps;
1184
1185 return 0;
1186}
1187
1188
Jouni Malinen87fd2792011-05-16 18:35:42 +03001189int hostapd_init_wps_complete(struct hostapd_data *hapd)
1190{
1191 struct wps_context *wps = hapd->wps;
1192
Jouni Malinen75ecf522011-06-27 15:19:46 -07001193 if (wps == NULL)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001194 return 0;
1195
1196#ifdef CONFIG_WPS_UPNP
1197 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1198 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1199 wps_registrar_deinit(wps->registrar);
1200 os_free(wps->network_key);
1201 os_free(wps);
1202 hapd->wps = NULL;
1203 return -1;
1204 }
1205#endif /* CONFIG_WPS_UPNP */
1206
1207 return 0;
1208}
1209
1210
Dmitry Shmidt04949592012-07-19 12:16:46 -07001211static void hostapd_wps_nfc_clear(struct wps_context *wps)
1212{
1213#ifdef CONFIG_WPS_NFC
Dmitry Shmidt54605472013-11-08 11:10:19 -08001214 wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001215 wps->ap_nfc_dev_pw_id = 0;
1216 wpabuf_free(wps->ap_nfc_dh_pubkey);
1217 wps->ap_nfc_dh_pubkey = NULL;
1218 wpabuf_free(wps->ap_nfc_dh_privkey);
1219 wps->ap_nfc_dh_privkey = NULL;
1220 wpabuf_free(wps->ap_nfc_dev_pw);
1221 wps->ap_nfc_dev_pw = NULL;
1222#endif /* CONFIG_WPS_NFC */
1223}
1224
1225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226void hostapd_deinit_wps(struct hostapd_data *hapd)
1227{
1228 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1229 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1230 if (hapd->wps == NULL)
1231 return;
1232#ifdef CONFIG_WPS_UPNP
1233 hostapd_wps_upnp_deinit(hapd);
1234#endif /* CONFIG_WPS_UPNP */
1235 wps_registrar_deinit(hapd->wps->registrar);
1236 os_free(hapd->wps->network_key);
1237 wps_device_data_free(&hapd->wps->dev);
1238 wpabuf_free(hapd->wps->dh_pubkey);
1239 wpabuf_free(hapd->wps->dh_privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 wps_free_pending_msgs(hapd->wps->upnp_msgs);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001241 hostapd_wps_nfc_clear(hapd->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 os_free(hapd->wps);
1243 hapd->wps = NULL;
1244 hostapd_wps_clear_ies(hapd);
1245}
1246
1247
1248void hostapd_update_wps(struct hostapd_data *hapd)
1249{
1250 if (hapd->wps == NULL)
1251 return;
1252
1253#ifdef CONFIG_WPS_UPNP
1254 hapd->wps->friendly_name = hapd->conf->friendly_name;
1255 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1256 hapd->wps->model_description = hapd->conf->model_description;
1257 hapd->wps->model_url = hapd->conf->model_url;
1258 hapd->wps->upc = hapd->conf->upc;
1259#endif /* CONFIG_WPS_UPNP */
1260
1261 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1262
1263 if (hapd->conf->wps_state)
1264 wps_registrar_update_ie(hapd->wps->registrar);
1265 else
1266 hostapd_deinit_wps(hapd);
1267}
1268
1269
1270struct wps_add_pin_data {
1271 const u8 *addr;
1272 const u8 *uuid;
1273 const u8 *pin;
1274 size_t pin_len;
1275 int timeout;
1276 int added;
1277};
1278
1279
1280static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1281{
1282 struct wps_add_pin_data *data = ctx;
1283 int ret;
1284
1285 if (hapd->wps == NULL)
1286 return 0;
1287 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1288 data->uuid, data->pin, data->pin_len,
1289 data->timeout);
1290 if (ret == 0)
1291 data->added++;
1292 return ret;
1293}
1294
1295
1296int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1297 const char *uuid, const char *pin, int timeout)
1298{
1299 u8 u[UUID_LEN];
1300 struct wps_add_pin_data data;
1301
1302 data.addr = addr;
1303 data.uuid = u;
1304 data.pin = (const u8 *) pin;
1305 data.pin_len = os_strlen(pin);
1306 data.timeout = timeout;
1307 data.added = 0;
1308
1309 if (os_strcmp(uuid, "any") == 0)
1310 data.uuid = NULL;
1311 else {
1312 if (uuid_str2bin(uuid, u))
1313 return -1;
1314 data.uuid = u;
1315 }
1316 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1317 return -1;
1318 return data.added ? 0 : -1;
1319}
1320
1321
1322static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1323{
1324 const u8 *p2p_dev_addr = ctx;
1325 if (hapd->wps == NULL)
1326 return 0;
1327 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1328}
1329
1330
1331int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1332 const u8 *p2p_dev_addr)
1333{
1334 return hostapd_wps_for_each(hapd, wps_button_pushed,
1335 (void *) p2p_dev_addr);
1336}
1337
1338
Dmitry Shmidt04949592012-07-19 12:16:46 -07001339static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1340{
1341 if (hapd->wps == NULL)
1342 return 0;
1343
1344 wps_registrar_wps_cancel(hapd->wps->registrar);
1345 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1346
1347 return 0;
1348}
1349
1350
1351int hostapd_wps_cancel(struct hostapd_data *hapd)
1352{
1353 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1354}
1355
1356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001357static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1358 const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001359 const u8 *ie, size_t ie_len,
1360 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361{
1362 struct hostapd_data *hapd = ctx;
1363 struct wpabuf *wps_ie;
1364 struct ieee802_11_elems elems;
1365
1366 if (hapd->wps == NULL)
1367 return 0;
1368
1369 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1370 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1371 MACSTR, MAC2STR(addr));
1372 return 0;
1373 }
1374
1375 if (elems.ssid && elems.ssid_len > 0 &&
1376 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1377 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1378 0))
1379 return 0; /* Not for us */
1380
1381 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1382 if (wps_ie == NULL)
1383 return 0;
1384 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1385 wpabuf_free(wps_ie);
1386 return 0;
1387 }
1388
1389 if (wpabuf_len(wps_ie) > 0) {
1390 int p2p_wildcard = 0;
1391#ifdef CONFIG_P2P
1392 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1393 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1394 P2P_WILDCARD_SSID_LEN) == 0)
1395 p2p_wildcard = 1;
1396#endif /* CONFIG_P2P */
1397 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1398 p2p_wildcard);
1399#ifdef CONFIG_WPS_UPNP
1400 /* FIX: what exactly should be included in the WLANEvent?
1401 * WPS attributes? Full ProbeReq frame? */
1402 if (!p2p_wildcard)
1403 upnp_wps_device_send_wlan_event(
1404 hapd->wps_upnp, addr,
1405 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1406#endif /* CONFIG_WPS_UPNP */
1407 }
1408
1409 wpabuf_free(wps_ie);
1410
1411 return 0;
1412}
1413
1414
1415#ifdef CONFIG_WPS_UPNP
1416
1417static int hostapd_rx_req_put_wlan_response(
1418 void *priv, enum upnp_wps_wlanevent_type ev_type,
1419 const u8 *mac_addr, const struct wpabuf *msg,
1420 enum wps_msg_type msg_type)
1421{
1422 struct hostapd_data *hapd = priv;
1423 struct sta_info *sta;
1424 struct upnp_pending_message *p;
1425
1426 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1427 MACSTR, ev_type, MAC2STR(mac_addr));
1428 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1429 wpabuf_head(msg), wpabuf_len(msg));
1430 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1431 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1432 "PutWLANResponse WLANEventType %d", ev_type);
1433 return -1;
1434 }
1435
1436 /*
1437 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1438 * server implementation for delivery to the peer.
1439 */
1440
1441 sta = ap_get_sta(hapd, mac_addr);
1442#ifndef CONFIG_WPS_STRICT
1443 if (!sta) {
1444 /*
1445 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1446 * Pick STA that is in an ongoing WPS registration without
1447 * checking the MAC address.
1448 */
1449 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1450 "on NewWLANEventMAC; try wildcard match");
1451 for (sta = hapd->sta_list; sta; sta = sta->next) {
1452 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1453 break;
1454 }
1455 }
1456#endif /* CONFIG_WPS_STRICT */
1457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001458 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1460 return 0;
1461 }
1462
1463 p = os_zalloc(sizeof(*p));
1464 if (p == NULL)
1465 return -1;
1466 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1467 p->msg = wpabuf_dup(msg);
1468 p->type = msg_type;
1469 p->next = hapd->wps->upnp_msgs;
1470 hapd->wps->upnp_msgs = p;
1471
1472 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1473}
1474
1475
1476static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1477 struct wps_context *wps)
1478{
1479 struct upnp_wps_device_ctx *ctx;
1480
1481 if (!hapd->conf->upnp_iface)
1482 return 0;
1483 ctx = os_zalloc(sizeof(*ctx));
1484 if (ctx == NULL)
1485 return -1;
1486
1487 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1488 if (hapd->conf->ap_pin)
1489 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1490
1491 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1492 hapd->conf->upnp_iface);
1493 if (hapd->wps_upnp == NULL)
1494 return -1;
1495 wps->wps_upnp = hapd->wps_upnp;
1496
1497 return 0;
1498}
1499
1500
1501static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1502{
1503 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1504}
1505
1506#endif /* CONFIG_WPS_UPNP */
1507
1508
1509int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1510 char *buf, size_t buflen)
1511{
1512 if (hapd->wps == NULL)
1513 return 0;
1514 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1515}
1516
1517
1518static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1519{
1520 struct hostapd_data *hapd = eloop_data;
1521 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1522 hostapd_wps_ap_pin_disable(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524}
1525
1526
1527static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1528{
1529 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1530 hapd->ap_pin_failures = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001531 hapd->ap_pin_failures_consecutive = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 hapd->conf->ap_setup_locked = 0;
1533 if (hapd->wps->ap_setup_locked) {
1534 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1535 hapd->wps->ap_setup_locked = 0;
1536 wps_registrar_update_ie(hapd->wps->registrar);
1537 }
1538 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1539 if (timeout > 0)
1540 eloop_register_timeout(timeout, 0,
1541 hostapd_wps_ap_pin_timeout, hapd, NULL);
1542}
1543
1544
1545static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1546{
1547 os_free(hapd->conf->ap_pin);
1548 hapd->conf->ap_pin = NULL;
1549#ifdef CONFIG_WPS_UPNP
1550 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1551#endif /* CONFIG_WPS_UPNP */
1552 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1553 return 0;
1554}
1555
1556
1557void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1558{
1559 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1560 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1561}
1562
1563
1564struct wps_ap_pin_data {
1565 char pin_txt[9];
1566 int timeout;
1567};
1568
1569
1570static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1571{
1572 struct wps_ap_pin_data *data = ctx;
1573 os_free(hapd->conf->ap_pin);
1574 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1575#ifdef CONFIG_WPS_UPNP
1576 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1577#endif /* CONFIG_WPS_UPNP */
1578 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1579 return 0;
1580}
1581
1582
1583const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1584{
1585 unsigned int pin;
1586 struct wps_ap_pin_data data;
1587
1588 pin = wps_generate_pin();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 data.timeout = timeout;
1591 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1592 return hapd->conf->ap_pin;
1593}
1594
1595
1596const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1597{
1598 return hapd->conf->ap_pin;
1599}
1600
1601
1602int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1603 int timeout)
1604{
1605 struct wps_ap_pin_data data;
1606 int ret;
1607
1608 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1609 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1610 return -1;
1611 data.timeout = timeout;
1612 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1613}
1614
1615
1616static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1617{
1618 if (hapd->wps)
1619 wps_registrar_update_ie(hapd->wps->registrar);
1620 return 0;
1621}
1622
1623
1624void hostapd_wps_update_ie(struct hostapd_data *hapd)
1625{
1626 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1627}
1628
1629
1630int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1631 const char *auth, const char *encr, const char *key)
1632{
1633 struct wps_credential cred;
1634 size_t len;
1635
1636 os_memset(&cred, 0, sizeof(cred));
1637
1638 len = os_strlen(ssid);
1639 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1640 hexstr2bin(ssid, cred.ssid, len / 2))
1641 return -1;
1642 cred.ssid_len = len / 2;
1643
1644 if (os_strncmp(auth, "OPEN", 4) == 0)
1645 cred.auth_type = WPS_AUTH_OPEN;
1646 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1647 cred.auth_type = WPS_AUTH_WPAPSK;
1648 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1649 cred.auth_type = WPS_AUTH_WPA2PSK;
1650 else
1651 return -1;
1652
1653 if (encr) {
1654 if (os_strncmp(encr, "NONE", 4) == 0)
1655 cred.encr_type = WPS_ENCR_NONE;
1656 else if (os_strncmp(encr, "WEP", 3) == 0)
1657 cred.encr_type = WPS_ENCR_WEP;
1658 else if (os_strncmp(encr, "TKIP", 4) == 0)
1659 cred.encr_type = WPS_ENCR_TKIP;
1660 else if (os_strncmp(encr, "CCMP", 4) == 0)
1661 cred.encr_type = WPS_ENCR_AES;
1662 else
1663 return -1;
1664 } else
1665 cred.encr_type = WPS_ENCR_NONE;
1666
1667 if (key) {
1668 len = os_strlen(key);
1669 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1670 hexstr2bin(key, cred.key, len / 2))
1671 return -1;
1672 cred.key_len = len / 2;
1673 }
1674
1675 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1676}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001677
1678
1679#ifdef CONFIG_WPS_NFC
1680
1681struct wps_nfc_password_token_data {
1682 const u8 *oob_dev_pw;
1683 size_t oob_dev_pw_len;
1684 int added;
1685};
1686
1687
1688static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1689{
1690 struct wps_nfc_password_token_data *data = ctx;
1691 int ret;
1692
1693 if (hapd->wps == NULL)
1694 return 0;
1695 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1696 data->oob_dev_pw,
1697 data->oob_dev_pw_len);
1698 if (ret == 0)
1699 data->added++;
1700 return ret;
1701}
1702
1703
1704static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1705 struct wps_parse_attr *attr)
1706{
1707 struct wps_nfc_password_token_data data;
1708
1709 data.oob_dev_pw = attr->oob_dev_password;
1710 data.oob_dev_pw_len = attr->oob_dev_password_len;
1711 data.added = 0;
1712 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1713 return -1;
1714 return data.added ? 0 : -1;
1715}
1716
1717
1718static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1719 const struct wpabuf *wps)
1720{
1721 struct wps_parse_attr attr;
1722
1723 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1724
1725 if (wps_parse_msg(wps, &attr)) {
1726 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1727 return -1;
1728 }
1729
1730 if (attr.oob_dev_password)
1731 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1732
1733 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1734 return -1;
1735}
1736
1737
1738int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1739 const struct wpabuf *data)
1740{
1741 const struct wpabuf *wps = data;
1742 struct wpabuf *tmp = NULL;
1743 int ret;
1744
1745 if (wpabuf_len(data) < 4)
1746 return -1;
1747
1748 if (*wpabuf_head_u8(data) != 0x10) {
1749 /* Assume this contains full NDEF record */
1750 tmp = ndef_parse_wifi(data);
1751 if (tmp == NULL) {
1752 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1753 return -1;
1754 }
1755 wps = tmp;
1756 }
1757
1758 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1759 wpabuf_free(tmp);
1760 return ret;
1761}
1762
1763
1764struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1765 int ndef)
1766{
1767 struct wpabuf *ret;
1768
1769 if (hapd->wps == NULL)
1770 return NULL;
1771
1772 ret = wps_get_oob_cred(hapd->wps);
1773 if (ndef && ret) {
1774 struct wpabuf *tmp;
1775 tmp = ndef_build_wifi(ret);
1776 wpabuf_free(ret);
1777 if (tmp == NULL)
1778 return NULL;
1779 ret = tmp;
1780 }
1781
1782 return ret;
1783}
1784
1785
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001786struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1787{
1788 /*
1789 * Handover Select carrier record for WPS uses the same format as
1790 * configuration token.
1791 */
1792 return hostapd_wps_nfc_config_token(hapd, ndef);
1793}
1794
1795
Dmitry Shmidt04949592012-07-19 12:16:46 -07001796struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1797{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001798 if (hapd->conf->wps_nfc_pw_from_config) {
1799 return wps_nfc_token_build(ndef,
1800 hapd->conf->wps_nfc_dev_pw_id,
1801 hapd->conf->wps_nfc_dh_pubkey,
1802 hapd->conf->wps_nfc_dev_pw);
1803 }
1804
Dmitry Shmidt04949592012-07-19 12:16:46 -07001805 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1806 &hapd->conf->wps_nfc_dh_pubkey,
1807 &hapd->conf->wps_nfc_dh_privkey,
1808 &hapd->conf->wps_nfc_dev_pw);
1809}
1810
1811
1812int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1813{
1814 struct wps_context *wps = hapd->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001815 struct wpabuf *pw;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001816
1817 if (wps == NULL)
1818 return -1;
1819
1820 if (!hapd->conf->wps_nfc_dh_pubkey ||
1821 !hapd->conf->wps_nfc_dh_privkey ||
1822 !hapd->conf->wps_nfc_dev_pw ||
1823 !hapd->conf->wps_nfc_dev_pw_id)
1824 return -1;
1825
1826 hostapd_wps_nfc_clear(wps);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001827 wpa_printf(MSG_DEBUG,
1828 "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
1829 hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001830 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1831 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1832 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001833 pw = hapd->conf->wps_nfc_dev_pw;
1834 wps->ap_nfc_dev_pw = wpabuf_alloc(
1835 wpabuf_len(pw) * 2 + 1);
1836 if (wps->ap_nfc_dev_pw) {
1837 wpa_snprintf_hex_uppercase(
1838 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1839 wpabuf_len(pw) * 2),
1840 wpabuf_len(pw) * 2 + 1,
1841 wpabuf_head(pw), wpabuf_len(pw));
1842 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001843
1844 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1845 !wps->ap_nfc_dev_pw) {
1846 hostapd_wps_nfc_clear(wps);
1847 return -1;
1848 }
1849
1850 return 0;
1851}
1852
1853
1854void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1855{
Dmitry Shmidt54605472013-11-08 11:10:19 -08001856 wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
1857 hapd->conf->iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001858 hostapd_wps_nfc_clear(hapd->wps);
1859}
1860
1861#endif /* CONFIG_WPS_NFC */