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