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