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