WPS: Add MAC address to validation error message for Probe Request
[libeap.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 "beacon.h"
32 #include "sta_info.h"
33 #include "wps_hostapd.h"
34
35
36 #ifdef CONFIG_WPS_UPNP
37 #include "wps/wps_upnp.h"
38 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
39                                  struct wps_context *wps);
40 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
41 #endif /* CONFIG_WPS_UPNP */
42
43 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
44                                     const u8 *ie, size_t ie_len);
45 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
46
47
48 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
49                                   size_t psk_len)
50 {
51         struct hostapd_data *hapd = ctx;
52         struct hostapd_wpa_psk *p;
53         struct hostapd_ssid *ssid = &hapd->conf->ssid;
54
55         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
56                    MACSTR, MAC2STR(mac_addr));
57         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
58
59         if (psk_len != PMK_LEN) {
60                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
61                            (unsigned long) psk_len);
62                 return -1;
63         }
64
65         /* Add the new PSK to runtime PSK list */
66         p = os_zalloc(sizeof(*p));
67         if (p == NULL)
68                 return -1;
69         os_memcpy(p->addr, mac_addr, ETH_ALEN);
70         os_memcpy(p->psk, psk, PMK_LEN);
71
72         p->next = ssid->wpa_psk;
73         ssid->wpa_psk = p;
74
75         if (ssid->wpa_psk_file) {
76                 FILE *f;
77                 char hex[PMK_LEN * 2 + 1];
78                 /* Add the new PSK to PSK list file */
79                 f = fopen(ssid->wpa_psk_file, "a");
80                 if (f == NULL) {
81                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
82                                    "'%s'", ssid->wpa_psk_file);
83                         return -1;
84                 }
85
86                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
87                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
88                 fclose(f);
89         }
90
91         return 0;
92 }
93
94
95 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
96                                  struct wpabuf *probe_resp_ie)
97 {
98         struct hostapd_data *hapd = ctx;
99         wpabuf_free(hapd->wps_beacon_ie);
100         hapd->wps_beacon_ie = beacon_ie;
101         wpabuf_free(hapd->wps_probe_resp_ie);
102         hapd->wps_probe_resp_ie = probe_resp_ie;
103         ieee802_11_set_beacon(hapd);
104         return hapd->drv.set_ap_wps_ie(hapd);
105 }
106
107
108 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
109                                       const struct wps_device_data *dev)
110 {
111         struct hostapd_data *hapd = ctx;
112         char uuid[40], txt[400];
113         int len;
114         char devtype[WPS_DEV_TYPE_BUFSIZE];
115         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
116                 return;
117         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
118         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
119                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
120                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
121                           dev->manufacturer, dev->model_name,
122                           dev->model_number, dev->serial_number,
123                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
124                                                sizeof(devtype)));
125         if (len > 0 && len < (int) sizeof(txt))
126                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
127
128         if (hapd->conf->wps_pin_requests) {
129                 FILE *f;
130                 struct os_time t;
131                 f = fopen(hapd->conf->wps_pin_requests, "a");
132                 if (f == NULL)
133                         return;
134                 os_get_time(&t);
135                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
136                         "\t%s\n",
137                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
138                         dev->manufacturer, dev->model_name, dev->model_number,
139                         dev->serial_number,
140                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
141                                              sizeof(devtype)));
142                 fclose(f);
143         }
144 }
145
146
147 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
148                                        const u8 *uuid_e)
149 {
150         struct hostapd_data *hapd = ctx;
151         char uuid[40];
152         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
153                 return;
154         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
155                 MAC2STR(mac_addr), uuid);
156         if (hapd->wps_reg_success_cb)
157                 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
158                                          mac_addr, uuid_e);
159 }
160
161
162 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
163                                          const u8 *uuid_e,
164                                          const u8 *pri_dev_type,
165                                          u16 config_methods,
166                                          u16 dev_password_id, u8 request_type,
167                                          const char *dev_name)
168 {
169         struct hostapd_data *hapd = ctx;
170         char uuid[40];
171         char devtype[WPS_DEV_TYPE_BUFSIZE];
172         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
173                 return;
174         if (dev_name == NULL)
175                 dev_name = "";
176         wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
177                      " %s %s 0x%x %u %u [%s]",
178                      MAC2STR(addr), uuid,
179                      wps_dev_type_bin2str(pri_dev_type, devtype,
180                                           sizeof(devtype)),
181                      config_methods, dev_password_id, request_type, dev_name);
182 }
183
184
185 static int str_starts(const char *str, const char *start)
186 {
187         return os_strncmp(str, start, os_strlen(start)) == 0;
188 }
189
190
191 static void wps_reload_config(void *eloop_data, void *user_ctx)
192 {
193         struct hostapd_iface *iface = eloop_data;
194
195         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
196         if (iface->reload_config(iface) < 0) {
197                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
198                            "configuration");
199         }
200 }
201
202
203 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
204 {
205         struct hostapd_data *hapd = ctx;
206         FILE *oconf, *nconf;
207         size_t len, i;
208         char *tmp_fname;
209         char buf[1024];
210         int multi_bss;
211         int wpa;
212
213         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
214                         cred->cred_attr, cred->cred_attr_len);
215
216         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
217         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
218         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
219                    cred->auth_type);
220         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
221         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
222         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
223                         cred->key, cred->key_len);
224         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
225                    MAC2STR(cred->mac_addr));
226
227         if ((hapd->conf->wps_cred_processing == 1 ||
228              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
229                 size_t blen = cred->cred_attr_len * 2 + 1;
230                 char *_buf = os_malloc(blen);
231                 if (_buf) {
232                         wpa_snprintf_hex(_buf, blen,
233                                          cred->cred_attr, cred->cred_attr_len);
234                         wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
235                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
236                         os_free(_buf);
237                 }
238         } else
239                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
240
241         if (hapd->conf->wps_cred_processing == 1)
242                 return 0;
243
244         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
245         hapd->wps->ssid_len = cred->ssid_len;
246         hapd->wps->encr_types = cred->encr_type;
247         hapd->wps->auth_types = cred->auth_type;
248         if (cred->key_len == 0) {
249                 os_free(hapd->wps->network_key);
250                 hapd->wps->network_key = NULL;
251                 hapd->wps->network_key_len = 0;
252         } else {
253                 if (hapd->wps->network_key == NULL ||
254                     hapd->wps->network_key_len < cred->key_len) {
255                         hapd->wps->network_key_len = 0;
256                         os_free(hapd->wps->network_key);
257                         hapd->wps->network_key = os_malloc(cred->key_len);
258                         if (hapd->wps->network_key == NULL)
259                                 return -1;
260                 }
261                 hapd->wps->network_key_len = cred->key_len;
262                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
263         }
264         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
265
266         len = os_strlen(hapd->iface->config_fname) + 5;
267         tmp_fname = os_malloc(len);
268         if (tmp_fname == NULL)
269                 return -1;
270         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
271
272         oconf = fopen(hapd->iface->config_fname, "r");
273         if (oconf == NULL) {
274                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
275                            "configuration file");
276                 os_free(tmp_fname);
277                 return -1;
278         }
279
280         nconf = fopen(tmp_fname, "w");
281         if (nconf == NULL) {
282                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
283                            "configuration file");
284                 os_free(tmp_fname);
285                 fclose(oconf);
286                 return -1;
287         }
288
289         fprintf(nconf, "# WPS configuration - START\n");
290
291         fprintf(nconf, "wps_state=2\n");
292
293         fprintf(nconf, "ssid=");
294         for (i = 0; i < cred->ssid_len; i++)
295                 fputc(cred->ssid[i], nconf);
296         fprintf(nconf, "\n");
297
298         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
299             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
300                 wpa = 3;
301         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
302                 wpa = 2;
303         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
304                 wpa = 1;
305         else
306                 wpa = 0;
307
308         if (wpa) {
309                 char *prefix;
310                 fprintf(nconf, "wpa=%d\n", wpa);
311
312                 fprintf(nconf, "wpa_key_mgmt=");
313                 prefix = "";
314                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
315                         fprintf(nconf, "WPA-EAP");
316                         prefix = " ";
317                 }
318                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
319                         fprintf(nconf, "%sWPA-PSK", prefix);
320                 fprintf(nconf, "\n");
321
322                 fprintf(nconf, "wpa_pairwise=");
323                 prefix = "";
324                 if (cred->encr_type & WPS_ENCR_AES) {
325                         fprintf(nconf, "CCMP");
326                         prefix = " ";
327                 }
328                 if (cred->encr_type & WPS_ENCR_TKIP) {
329                         fprintf(nconf, "%sTKIP", prefix);
330                 }
331                 fprintf(nconf, "\n");
332
333                 if (cred->key_len >= 8 && cred->key_len < 64) {
334                         fprintf(nconf, "wpa_passphrase=");
335                         for (i = 0; i < cred->key_len; i++)
336                                 fputc(cred->key[i], nconf);
337                         fprintf(nconf, "\n");
338                 } else if (cred->key_len == 64) {
339                         fprintf(nconf, "wpa_psk=");
340                         for (i = 0; i < cred->key_len; i++)
341                                 fputc(cred->key[i], nconf);
342                         fprintf(nconf, "\n");
343                 } else {
344                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
345                                    "for WPA/WPA2",
346                                    (unsigned long) cred->key_len);
347                 }
348
349                 fprintf(nconf, "auth_algs=1\n");
350         } else {
351                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
352                     (cred->auth_type & WPS_AUTH_SHARED))
353                         fprintf(nconf, "auth_algs=3\n");
354                 else if (cred->auth_type & WPS_AUTH_SHARED)
355                         fprintf(nconf, "auth_algs=2\n");
356                 else
357                         fprintf(nconf, "auth_algs=1\n");
358
359                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
360                         int key_idx = cred->key_idx;
361                         if (key_idx)
362                                 key_idx--;
363                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
364                         fprintf(nconf, "wep_key%d=", key_idx);
365                         if (cred->key_len == 10 || cred->key_len == 26) {
366                                 /* WEP key as a hex string */
367                                 for (i = 0; i < cred->key_len; i++)
368                                         fputc(cred->key[i], nconf);
369                         } else {
370                                 /* Raw WEP key; convert to hex */
371                                 for (i = 0; i < cred->key_len; i++)
372                                         fprintf(nconf, "%02x", cred->key[i]);
373                         }
374                         fprintf(nconf, "\n");
375                 }
376         }
377
378         fprintf(nconf, "# WPS configuration - END\n");
379
380         multi_bss = 0;
381         while (fgets(buf, sizeof(buf), oconf)) {
382                 if (os_strncmp(buf, "bss=", 4) == 0)
383                         multi_bss = 1;
384                 if (!multi_bss &&
385                     (str_starts(buf, "ssid=") ||
386                      str_starts(buf, "auth_algs=") ||
387                      str_starts(buf, "wps_state=") ||
388                      str_starts(buf, "wpa=") ||
389                      str_starts(buf, "wpa_psk=") ||
390                      str_starts(buf, "wpa_pairwise=") ||
391                      str_starts(buf, "rsn_pairwise=") ||
392                      str_starts(buf, "wpa_key_mgmt=") ||
393                      str_starts(buf, "wpa_passphrase="))) {
394                         fprintf(nconf, "#WPS# %s", buf);
395                 } else
396                         fprintf(nconf, "%s", buf);
397         }
398
399         fclose(nconf);
400         fclose(oconf);
401
402         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
403                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
404                            "configuration file: %s", strerror(errno));
405                 os_free(tmp_fname);
406                 return -1;
407         }
408
409         os_free(tmp_fname);
410
411         /* Schedule configuration reload after short period of time to allow
412          * EAP-WSC to be finished.
413          */
414         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
415                                NULL);
416
417         /* TODO: dualband AP may need to update multiple configuration files */
418
419         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
420
421         return 0;
422 }
423
424
425 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
426 {
427         struct hostapd_data *hapd = eloop_data;
428
429         if (hapd->conf->ap_setup_locked)
430                 return;
431
432         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
433         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
434         hapd->wps->ap_setup_locked = 0;
435         wps_registrar_update_ie(hapd->wps->registrar);
436 }
437
438
439 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
440                                   struct wps_event_pwd_auth_fail *data)
441 {
442         if (!data->enrollee || hapd->conf->ap_pin == NULL)
443                 return;
444
445         /*
446          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
447          * for some time if this happens multiple times to slow down brute
448          * force attacks.
449          */
450         hapd->ap_pin_failures++;
451         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
452                    hapd->ap_pin_failures);
453         if (hapd->ap_pin_failures < 3)
454                 return;
455
456         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
457         hapd->wps->ap_setup_locked = 1;
458
459         wps_registrar_update_ie(hapd->wps->registrar);
460
461         if (!hapd->conf->ap_setup_locked) {
462                 if (hapd->ap_pin_lockout_time == 0)
463                         hapd->ap_pin_lockout_time = 60;
464                 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
465                          (hapd->ap_pin_failures % 3) == 0)
466                         hapd->ap_pin_lockout_time *= 2;
467
468                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
469                            hapd->ap_pin_lockout_time);
470                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
471                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
472                                        hostapd_wps_reenable_ap_pin, hapd,
473                                        NULL);
474         }
475
476         /* TODO: dualband AP may need to update other interfaces */
477 }
478
479
480 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
481                                  union wps_event_data *data)
482 {
483         struct hostapd_data *hapd = ctx;
484
485         if (event == WPS_EV_PWD_AUTH_FAIL)
486                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
487 }
488
489
490 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
491 {
492         wpabuf_free(hapd->wps_beacon_ie);
493         hapd->wps_beacon_ie = NULL;
494
495         wpabuf_free(hapd->wps_probe_resp_ie);
496         hapd->wps_probe_resp_ie = NULL;
497
498         hapd->drv.set_ap_wps_ie(hapd);
499 }
500
501
502 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
503 {
504         const u8 **uuid = ctx;
505         size_t j;
506
507         if (iface == NULL)
508                 return 0;
509         for (j = 0; j < iface->num_bss; j++) {
510                 struct hostapd_data *hapd = iface->bss[j];
511                 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
512                         *uuid = hapd->wps->uuid;
513                         return 1;
514                 }
515         }
516
517         return 0;
518 }
519
520
521 static const u8 * get_own_uuid(struct hostapd_iface *iface)
522 {
523         const u8 *uuid;
524         if (iface->for_each_interface == NULL)
525                 return NULL;
526         uuid = NULL;
527         iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
528         return uuid;
529 }
530
531
532 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
533 {
534         int *count= ctx;
535         (*count)++;
536         return 0;
537 }
538
539
540 static int interface_count(struct hostapd_iface *iface)
541 {
542         int count = 0;
543         iface->for_each_interface(iface->interfaces, count_interface_cb,
544                                   &count);
545         return count;
546 }
547
548
549 int hostapd_init_wps(struct hostapd_data *hapd,
550                      struct hostapd_bss_config *conf)
551 {
552         struct wps_context *wps;
553         struct wps_registrar_config cfg;
554
555         if (conf->wps_state == 0) {
556                 hostapd_wps_clear_ies(hapd);
557                 return 0;
558         }
559
560         wps = os_zalloc(sizeof(*wps));
561         if (wps == NULL)
562                 return -1;
563
564         wps->cred_cb = hostapd_wps_cred_cb;
565         wps->event_cb = hostapd_wps_event_cb;
566         wps->cb_ctx = hapd;
567
568         os_memset(&cfg, 0, sizeof(cfg));
569         wps->wps_state = hapd->conf->wps_state;
570         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
571         if (is_nil_uuid(hapd->conf->uuid)) {
572                 const u8 *uuid;
573                 uuid = get_own_uuid(hapd->iface);
574                 if (uuid) {
575                         os_memcpy(wps->uuid, uuid, UUID_LEN);
576                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
577                                     "interface", wps->uuid, UUID_LEN);
578                 } else {
579                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
580                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
581                                     "address", wps->uuid, UUID_LEN);
582                 }
583         } else {
584                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
585                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
586                             wps->uuid, UUID_LEN);
587         }
588         wps->ssid_len = hapd->conf->ssid.ssid_len;
589         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
590         wps->ap = 1;
591         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
592         wps->dev.device_name = hapd->conf->device_name ?
593                 os_strdup(hapd->conf->device_name) : NULL;
594         wps->dev.manufacturer = hapd->conf->manufacturer ?
595                 os_strdup(hapd->conf->manufacturer) : NULL;
596         wps->dev.model_name = hapd->conf->model_name ?
597                 os_strdup(hapd->conf->model_name) : NULL;
598         wps->dev.model_number = hapd->conf->model_number ?
599                 os_strdup(hapd->conf->model_number) : NULL;
600         wps->dev.serial_number = hapd->conf->serial_number ?
601                 os_strdup(hapd->conf->serial_number) : NULL;
602         wps->config_methods =
603                 wps_config_methods_str2bin(hapd->conf->config_methods);
604         if (hapd->conf->device_type &&
605             wps_dev_type_str2bin(hapd->conf->device_type,
606                                  wps->dev.pri_dev_type) < 0) {
607                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
608                 os_free(wps);
609                 return -1;
610         }
611         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
612         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
613                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
614
615         if (conf->wpa & WPA_PROTO_RSN) {
616                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
617                         wps->auth_types |= WPS_AUTH_WPA2PSK;
618                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
619                         wps->auth_types |= WPS_AUTH_WPA2;
620
621                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
622                         wps->encr_types |= WPS_ENCR_AES;
623                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
624                         wps->encr_types |= WPS_ENCR_TKIP;
625         }
626
627         if (conf->wpa & WPA_PROTO_WPA) {
628                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
629                         wps->auth_types |= WPS_AUTH_WPAPSK;
630                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
631                         wps->auth_types |= WPS_AUTH_WPA;
632
633                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
634                         wps->encr_types |= WPS_ENCR_AES;
635                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
636                         wps->encr_types |= WPS_ENCR_TKIP;
637         }
638
639         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
640                 wps->encr_types |= WPS_ENCR_NONE;
641                 wps->auth_types |= WPS_AUTH_OPEN;
642         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
643                 wps->encr_types |= WPS_ENCR_WEP;
644                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
645                         wps->auth_types |= WPS_AUTH_OPEN;
646                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
647                         wps->auth_types |= WPS_AUTH_SHARED;
648         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
649                 wps->auth_types |= WPS_AUTH_OPEN;
650                 if (conf->default_wep_key_len)
651                         wps->encr_types |= WPS_ENCR_WEP;
652                 else
653                         wps->encr_types |= WPS_ENCR_NONE;
654         }
655
656         if (conf->ssid.wpa_psk_file) {
657                 /* Use per-device PSKs */
658         } else if (conf->ssid.wpa_passphrase) {
659                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
660                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
661         } else if (conf->ssid.wpa_psk) {
662                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
663                 if (wps->network_key == NULL) {
664                         os_free(wps);
665                         return -1;
666                 }
667                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
668                                  conf->ssid.wpa_psk->psk, PMK_LEN);
669                 wps->network_key_len = 2 * PMK_LEN;
670         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
671                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
672                 if (wps->network_key == NULL) {
673                         os_free(wps);
674                         return -1;
675                 }
676                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
677                           conf->ssid.wep.len[0]);
678                 wps->network_key_len = conf->ssid.wep.len[0];
679         }
680
681         if (conf->ssid.wpa_psk) {
682                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
683                 wps->psk_set = 1;
684         }
685
686         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
687                 /* Override parameters to enable security by default */
688                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
689                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
690         }
691
692         wps->ap_settings = conf->ap_settings;
693         wps->ap_settings_len = conf->ap_settings_len;
694
695         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
696         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
697         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
698         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
699         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
700         cfg.cb_ctx = hapd;
701         cfg.skip_cred_build = conf->skip_cred_build;
702         cfg.extra_cred = conf->extra_cred;
703         cfg.extra_cred_len = conf->extra_cred_len;
704         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
705                 conf->skip_cred_build;
706         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
707                 cfg.static_wep_only = 1;
708         cfg.dualband = interface_count(hapd->iface) > 1;
709         if (cfg.dualband)
710                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
711
712         wps->registrar = wps_registrar_init(wps, &cfg);
713         if (wps->registrar == NULL) {
714                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
715                 os_free(wps->network_key);
716                 os_free(wps);
717                 return -1;
718         }
719
720 #ifdef CONFIG_WPS_UPNP
721         wps->friendly_name = hapd->conf->friendly_name;
722         wps->manufacturer_url = hapd->conf->manufacturer_url;
723         wps->model_description = hapd->conf->model_description;
724         wps->model_url = hapd->conf->model_url;
725         wps->upc = hapd->conf->upc;
726
727         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
728                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
729                 wps_registrar_deinit(wps->registrar);
730                 os_free(wps->network_key);
731                 os_free(wps);
732                 return -1;
733         }
734 #endif /* CONFIG_WPS_UPNP */
735
736         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
737
738         hapd->wps = wps;
739
740         return 0;
741 }
742
743
744 void hostapd_deinit_wps(struct hostapd_data *hapd)
745 {
746         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
747         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
748         if (hapd->wps == NULL)
749                 return;
750 #ifdef CONFIG_WPS_UPNP
751         hostapd_wps_upnp_deinit(hapd);
752 #endif /* CONFIG_WPS_UPNP */
753         wps_registrar_deinit(hapd->wps->registrar);
754         os_free(hapd->wps->network_key);
755         wps_device_data_free(&hapd->wps->dev);
756         wpabuf_free(hapd->wps->dh_pubkey);
757         wpabuf_free(hapd->wps->dh_privkey);
758         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
759         wpabuf_free(hapd->wps->oob_conf.dev_password);
760         wps_free_pending_msgs(hapd->wps->upnp_msgs);
761         os_free(hapd->wps);
762         hapd->wps = NULL;
763         hostapd_wps_clear_ies(hapd);
764 }
765
766
767 void hostapd_update_wps(struct hostapd_data *hapd)
768 {
769         if (hapd->wps == NULL)
770                 return;
771         if (hapd->conf->wps_state)
772                 wps_registrar_update_ie(hapd->wps->registrar);
773         else
774                 hostapd_deinit_wps(hapd);
775 }
776
777
778 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
779                         const char *uuid, const char *pin, int timeout)
780 {
781         u8 u[UUID_LEN];
782         int any = 0;
783
784         if (hapd->wps == NULL)
785                 return -1;
786         if (os_strcmp(uuid, "any") == 0)
787                 any = 1;
788         else if (uuid_str2bin(uuid, u))
789                 return -1;
790         return wps_registrar_add_pin(hapd->wps->registrar, addr,
791                                      any ? NULL : u,
792                                      (const u8 *) pin, os_strlen(pin),
793                                      timeout);
794 }
795
796
797 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
798 {
799         if (hapd->wps == NULL)
800                 return -1;
801         return wps_registrar_button_pushed(hapd->wps->registrar);
802 }
803
804
805 #ifdef CONFIG_WPS_OOB
806 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
807                           char *path, char *method, char *name)
808 {
809         struct wps_context *wps = hapd->wps;
810         struct oob_device_data *oob_dev;
811
812         oob_dev = wps_get_oob_device(device_type);
813         if (oob_dev == NULL)
814                 return -1;
815         oob_dev->device_path = path;
816         oob_dev->device_name = name;
817         wps->oob_conf.oob_method = wps_get_oob_method(method);
818
819         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
820                 /*
821                  * Use pre-configured DH keys in order to be able to write the
822                  * key hash into the OOB file.
823                  */
824                 wpabuf_free(wps->dh_pubkey);
825                 wpabuf_free(wps->dh_privkey);
826                 wps->dh_privkey = NULL;
827                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
828                                          &wps->dh_privkey);
829                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
830                 if (wps->dh_pubkey == NULL) {
831                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
832                                    "Diffie-Hellman handshake");
833                         return -1;
834                 }
835         }
836
837         if (wps_process_oob(wps, oob_dev, 1) < 0)
838                 goto error;
839
840         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
841              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
842             hostapd_wps_add_pin(hapd, NULL, "any",
843                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
844             0)
845                 goto error;
846
847         return 0;
848
849 error:
850         wpabuf_free(wps->dh_pubkey);
851         wps->dh_pubkey = NULL;
852         wpabuf_free(wps->dh_privkey);
853         wps->dh_privkey = NULL;
854         return -1;
855 }
856 #endif /* CONFIG_WPS_OOB */
857
858
859 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
860                                     const u8 *ie, size_t ie_len)
861 {
862         struct hostapd_data *hapd = ctx;
863         struct wpabuf *wps_ie;
864         struct ieee802_11_elems elems;
865
866         if (hapd->wps == NULL)
867                 return 0;
868
869         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
870                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
871                            MACSTR, MAC2STR(addr));
872                 return 0;
873         }
874
875         if (elems.ssid && elems.ssid_len > 0 &&
876             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
877              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
878              0))
879                 return 0; /* Not for us */
880
881         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
882         if (wps_ie == NULL)
883                 return 0;
884         if (wps_validate_probe_req(wps_ie, addr) < 0) {
885                 wpabuf_free(wps_ie);
886                 return 0;
887         }
888
889         if (wpabuf_len(wps_ie) > 0) {
890                 int p2p_wildcard = 0;
891 #ifdef CONFIG_P2P
892                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
893                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
894                               P2P_WILDCARD_SSID_LEN) == 0)
895                         p2p_wildcard = 1;
896 #endif /* CONFIG_P2P */
897                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
898                                            p2p_wildcard);
899 #ifdef CONFIG_WPS_UPNP
900                 /* FIX: what exactly should be included in the WLANEvent?
901                  * WPS attributes? Full ProbeReq frame? */
902                 if (!p2p_wildcard)
903                         upnp_wps_device_send_wlan_event(
904                                 hapd->wps_upnp, addr,
905                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
906 #endif /* CONFIG_WPS_UPNP */
907         }
908
909         wpabuf_free(wps_ie);
910
911         return 0;
912 }
913
914
915 #ifdef CONFIG_WPS_UPNP
916
917 static int hostapd_rx_req_put_wlan_response(
918         void *priv, enum upnp_wps_wlanevent_type ev_type,
919         const u8 *mac_addr, const struct wpabuf *msg,
920         enum wps_msg_type msg_type)
921 {
922         struct hostapd_data *hapd = priv;
923         struct sta_info *sta;
924         struct upnp_pending_message *p;
925
926         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
927                    MACSTR, ev_type, MAC2STR(mac_addr));
928         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
929                     wpabuf_head(msg), wpabuf_len(msg));
930         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
931                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
932                            "PutWLANResponse WLANEventType %d", ev_type);
933                 return -1;
934         }
935
936         /*
937          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
938          * server implementation for delivery to the peer.
939          */
940
941         sta = ap_get_sta(hapd, mac_addr);
942 #ifndef CONFIG_WPS_STRICT
943         if (!sta) {
944                 /*
945                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
946                  * Pick STA that is in an ongoing WPS registration without
947                  * checking the MAC address.
948                  */
949                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
950                            "on NewWLANEventMAC; try wildcard match");
951                 for (sta = hapd->sta_list; sta; sta = sta->next) {
952                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
953                                 break;
954                 }
955         }
956 #endif /* CONFIG_WPS_STRICT */
957
958         if (!sta) {
959                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
960                 return 0;
961         }
962
963         p = os_zalloc(sizeof(*p));
964         if (p == NULL)
965                 return -1;
966         os_memcpy(p->addr, sta->addr, ETH_ALEN);
967         p->msg = wpabuf_dup(msg);
968         p->type = msg_type;
969         p->next = hapd->wps->upnp_msgs;
970         hapd->wps->upnp_msgs = p;
971
972         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
973 }
974
975
976 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
977                                  struct wps_context *wps)
978 {
979         struct upnp_wps_device_ctx *ctx;
980
981         if (!hapd->conf->upnp_iface)
982                 return 0;
983         ctx = os_zalloc(sizeof(*ctx));
984         if (ctx == NULL)
985                 return -1;
986
987         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
988         if (hapd->conf->ap_pin)
989                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
990
991         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
992         if (hapd->wps_upnp == NULL) {
993                 os_free(ctx);
994                 return -1;
995         }
996         wps->wps_upnp = hapd->wps_upnp;
997
998         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
999                 upnp_wps_device_deinit(hapd->wps_upnp);
1000                 hapd->wps_upnp = NULL;
1001                 return -1;
1002         }
1003
1004         return 0;
1005 }
1006
1007
1008 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1009 {
1010         upnp_wps_device_deinit(hapd->wps_upnp);
1011 }
1012
1013 #endif /* CONFIG_WPS_UPNP */
1014
1015
1016 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1017                             char *buf, size_t buflen)
1018 {
1019         if (hapd->wps == NULL)
1020                 return 0;
1021         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1022 }
1023
1024
1025 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1026 {
1027         struct hostapd_data *hapd = eloop_data;
1028         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1029         hostapd_wps_ap_pin_disable(hapd);
1030 }
1031
1032
1033 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1034 {
1035         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1036         hapd->ap_pin_failures = 0;
1037         hapd->conf->ap_setup_locked = 0;
1038         if (hapd->wps->ap_setup_locked) {
1039                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1040                 hapd->wps->ap_setup_locked = 0;
1041                 wps_registrar_update_ie(hapd->wps->registrar);
1042         }
1043         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1044         if (timeout > 0)
1045                 eloop_register_timeout(timeout, 0,
1046                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
1047 }
1048
1049
1050 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1051 {
1052         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1053         os_free(hapd->conf->ap_pin);
1054         hapd->conf->ap_pin = NULL;
1055 #ifdef CONFIG_WPS_UPNP
1056         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1057 #endif /* CONFIG_WPS_UPNP */
1058         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1059 }
1060
1061
1062 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1063 {
1064         unsigned int pin;
1065         char pin_txt[9];
1066
1067         pin = wps_generate_pin();
1068         os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
1069         os_free(hapd->conf->ap_pin);
1070         hapd->conf->ap_pin = os_strdup(pin_txt);
1071 #ifdef CONFIG_WPS_UPNP
1072         upnp_wps_set_ap_pin(hapd->wps_upnp, pin_txt);
1073 #endif /* CONFIG_WPS_UPNP */
1074         hostapd_wps_ap_pin_enable(hapd, timeout);
1075         return hapd->conf->ap_pin;
1076 }
1077
1078
1079 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1080 {
1081         return hapd->conf->ap_pin;
1082 }
1083
1084
1085 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1086                            int timeout)
1087 {
1088         os_free(hapd->conf->ap_pin);
1089         hapd->conf->ap_pin = os_strdup(pin);
1090         if (hapd->conf->ap_pin == NULL)
1091                 return -1;
1092 #ifdef CONFIG_WPS_UPNP
1093         upnp_wps_set_ap_pin(hapd->wps_upnp, hapd->conf->ap_pin);
1094 #endif /* CONFIG_WPS_UPNP */
1095         hostapd_wps_ap_pin_enable(hapd, timeout);
1096         return 0;
1097 }