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