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