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