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