Merge commit 'garage/master'
[wpasupplicant] / 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 "sta_info.h"
24 #include "eapol_sm.h"
25 #include "wps/wps.h"
26 #include "wps/wps_defs.h"
27 #include "wps/wps_dev_attr.h"
28 #include "wps_hostapd.h"
29 #include "dh_groups.h"
30
31
32 #ifdef CONFIG_WPS_UPNP
33 #include "wps/wps_upnp.h"
34 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
35                                  struct wps_context *wps);
36 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
37 #endif /* CONFIG_WPS_UPNP */
38
39 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
40                                      const u8 *ie, size_t ie_len);
41
42
43 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
44                                   size_t psk_len)
45 {
46         struct hostapd_data *hapd = ctx;
47         struct hostapd_wpa_psk *p;
48         struct hostapd_ssid *ssid = &hapd->conf->ssid;
49
50         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
51                    MACSTR, MAC2STR(mac_addr));
52         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
53
54         if (psk_len != PMK_LEN) {
55                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
56                            (unsigned long) psk_len);
57                 return -1;
58         }
59
60         /* Add the new PSK to runtime PSK list */
61         p = os_zalloc(sizeof(*p));
62         if (p == NULL)
63                 return -1;
64         os_memcpy(p->addr, mac_addr, ETH_ALEN);
65         os_memcpy(p->psk, psk, PMK_LEN);
66
67         p->next = ssid->wpa_psk;
68         ssid->wpa_psk = p;
69
70         if (ssid->wpa_psk_file) {
71                 FILE *f;
72                 char hex[PMK_LEN * 2 + 1];
73                 /* Add the new PSK to PSK list file */
74                 f = fopen(ssid->wpa_psk_file, "a");
75                 if (f == NULL) {
76                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
77                                    "'%s'", ssid->wpa_psk_file);
78                         return -1;
79                 }
80
81                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
82                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
83                 fclose(f);
84         }
85
86         return 0;
87 }
88
89
90 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
91                                  size_t beacon_ie_len, const u8 *probe_resp_ie,
92                                  size_t probe_resp_ie_len)
93 {
94         struct hostapd_data *hapd = ctx;
95
96         os_free(hapd->wps_beacon_ie);
97         if (beacon_ie_len == 0) {
98                 hapd->wps_beacon_ie = NULL;
99                 hapd->wps_beacon_ie_len = 0;
100         } else {
101                 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
102                 if (hapd->wps_beacon_ie == NULL) {
103                         hapd->wps_beacon_ie_len = 0;
104                         return -1;
105                 }
106                 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
107                 hapd->wps_beacon_ie_len = beacon_ie_len;
108         }
109         hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
110                                   hapd->wps_beacon_ie_len);
111
112         os_free(hapd->wps_probe_resp_ie);
113         if (probe_resp_ie_len == 0) {
114                 hapd->wps_probe_resp_ie = NULL;
115                 hapd->wps_probe_resp_ie_len = 0;
116         } else {
117                 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
118                 if (hapd->wps_probe_resp_ie == NULL) {
119                         hapd->wps_probe_resp_ie_len = 0;
120                         return -1;
121                 }
122                 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
123                           probe_resp_ie_len);
124                 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
125         }
126         hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
127                                       hapd->wps_probe_resp_ie_len);
128
129         return 0;
130 }
131
132
133 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
134                                       const struct wps_device_data *dev)
135 {
136         struct hostapd_data *hapd = ctx;
137         char uuid[40], txt[400];
138         int len;
139         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
140                 return;
141         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
142         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
143                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
144                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
145                           dev->manufacturer, dev->model_name,
146                           dev->model_number, dev->serial_number,
147                           dev->categ, dev->oui, dev->sub_categ);
148         if (len > 0 && len < (int) sizeof(txt))
149                 wpa_msg(hapd, MSG_INFO, "%s", txt);
150
151         if (hapd->conf->wps_pin_requests) {
152                 FILE *f;
153                 struct os_time t;
154                 f = fopen(hapd->conf->wps_pin_requests, "a");
155                 if (f == NULL)
156                         return;
157                 os_get_time(&t);
158                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
159                         "\t%d-%08X-%d\n",
160                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
161                         dev->manufacturer, dev->model_name, dev->model_number,
162                         dev->serial_number,
163                         dev->categ, dev->oui, dev->sub_categ);
164                 fclose(f);
165         }
166 }
167
168
169 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
170                                        const u8 *uuid_e)
171 {
172         struct hostapd_data *hapd = ctx;
173         char uuid[40];
174         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
175                 return;
176         wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
177                 MAC2STR(mac_addr), uuid);
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 (hostapd_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_INFO, "%s%s",
231                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
232                         os_free(_buf);
233                 }
234         } else
235                 wpa_msg(hapd, 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 == NULL) {
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_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         os_free(hapd->wps_beacon_ie);
475         hapd->wps_beacon_ie = NULL;
476         hapd->wps_beacon_ie_len = 0;
477         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
478
479         os_free(hapd->wps_probe_resp_ie);
480         hapd->wps_probe_resp_ie = NULL;
481         hapd->wps_probe_resp_ie_len = 0;
482         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
483 }
484
485
486 int hostapd_init_wps(struct hostapd_data *hapd,
487                      struct hostapd_bss_config *conf)
488 {
489         struct wps_context *wps;
490         struct wps_registrar_config cfg;
491
492         if (conf->wps_state == 0) {
493                 hostapd_wps_clear_ies(hapd);
494                 return 0;
495         }
496
497         wps = os_zalloc(sizeof(*wps));
498         if (wps == NULL)
499                 return -1;
500
501         wps->cred_cb = hostapd_wps_cred_cb;
502         wps->event_cb = hostapd_wps_event_cb;
503         wps->cb_ctx = hapd;
504
505         os_memset(&cfg, 0, sizeof(cfg));
506         wps->wps_state = hapd->conf->wps_state;
507         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
508         if (is_nil_uuid(hapd->conf->uuid)) {
509                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
510                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
511                             wps->uuid, UUID_LEN);
512         } else
513                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
514         wps->ssid_len = hapd->conf->ssid.ssid_len;
515         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
516         wps->ap = 1;
517         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
518         wps->dev.device_name = hapd->conf->device_name ?
519                 os_strdup(hapd->conf->device_name) : NULL;
520         wps->dev.manufacturer = hapd->conf->manufacturer ?
521                 os_strdup(hapd->conf->manufacturer) : NULL;
522         wps->dev.model_name = hapd->conf->model_name ?
523                 os_strdup(hapd->conf->model_name) : NULL;
524         wps->dev.model_number = hapd->conf->model_number ?
525                 os_strdup(hapd->conf->model_number) : NULL;
526         wps->dev.serial_number = hapd->conf->serial_number ?
527                 os_strdup(hapd->conf->serial_number) : NULL;
528         if (hapd->conf->config_methods) {
529                 char *m = hapd->conf->config_methods;
530                 if (os_strstr(m, "label"))
531                         wps->config_methods |= WPS_CONFIG_LABEL;
532                 if (os_strstr(m, "display"))
533                         wps->config_methods |= WPS_CONFIG_DISPLAY;
534                 if (os_strstr(m, "push_button"))
535                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
536                 if (os_strstr(m, "keypad"))
537                         wps->config_methods |= WPS_CONFIG_KEYPAD;
538         }
539         if (hapd->conf->device_type) {
540                 char *pos;
541                 u8 oui[4];
542                 /* <categ>-<OUI>-<subcateg> */
543                 wps->dev.categ = atoi(hapd->conf->device_type);
544                 pos = os_strchr(hapd->conf->device_type, '-');
545                 if (pos == NULL) {
546                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
547                         os_free(wps);
548                         return -1;
549                 }
550                 pos++;
551                 if (hexstr2bin(pos, oui, 4)) {
552                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
553                         os_free(wps);
554                         return -1;
555                 }
556                 wps->dev.oui = WPA_GET_BE32(oui);
557                 pos = os_strchr(pos, '-');
558                 if (pos == NULL) {
559                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
560                         os_free(wps);
561                         return -1;
562                 }
563                 pos++;
564                 wps->dev.sub_categ = atoi(pos);
565         }
566         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
567         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
568                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
569
570         if (conf->wpa & WPA_PROTO_RSN) {
571                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
572                         wps->auth_types |= WPS_AUTH_WPA2PSK;
573                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
574                         wps->auth_types |= WPS_AUTH_WPA2;
575
576                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
577                         wps->encr_types |= WPS_ENCR_AES;
578                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
579                         wps->encr_types |= WPS_ENCR_TKIP;
580         }
581
582         if (conf->wpa & WPA_PROTO_WPA) {
583                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
584                         wps->auth_types |= WPS_AUTH_WPAPSK;
585                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
586                         wps->auth_types |= WPS_AUTH_WPA;
587
588                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
589                         wps->encr_types |= WPS_ENCR_AES;
590                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
591                         wps->encr_types |= WPS_ENCR_TKIP;
592         }
593
594         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
595                 wps->encr_types |= WPS_ENCR_NONE;
596                 wps->auth_types |= WPS_AUTH_OPEN;
597         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
598                 wps->encr_types |= WPS_ENCR_WEP;
599                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
600                         wps->auth_types |= WPS_AUTH_OPEN;
601                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
602                         wps->auth_types |= WPS_AUTH_SHARED;
603         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
604                 wps->auth_types |= WPS_AUTH_OPEN;
605                 if (conf->default_wep_key_len)
606                         wps->encr_types |= WPS_ENCR_WEP;
607                 else
608                         wps->encr_types |= WPS_ENCR_NONE;
609         }
610
611         if (conf->ssid.wpa_psk_file) {
612                 /* Use per-device PSKs */
613         } else if (conf->ssid.wpa_passphrase) {
614                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
615                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
616         } else if (conf->ssid.wpa_psk) {
617                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
618                 if (wps->network_key == NULL) {
619                         os_free(wps);
620                         return -1;
621                 }
622                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
623                                  conf->ssid.wpa_psk->psk, PMK_LEN);
624                 wps->network_key_len = 2 * PMK_LEN;
625         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
626                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
627                 if (wps->network_key == NULL) {
628                         os_free(wps);
629                         return -1;
630                 }
631                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
632                           conf->ssid.wep.len[0]);
633                 wps->network_key_len = conf->ssid.wep.len[0];
634         }
635
636         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
637                 /* Override parameters to enable security by default */
638                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
639                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
640         }
641
642         wps->ap_settings = conf->ap_settings;
643         wps->ap_settings_len = conf->ap_settings_len;
644
645         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
646         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
647         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
648         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
649         cfg.cb_ctx = hapd;
650         cfg.skip_cred_build = conf->skip_cred_build;
651         cfg.extra_cred = conf->extra_cred;
652         cfg.extra_cred_len = conf->extra_cred_len;
653         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
654                 conf->skip_cred_build;
655         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
656                 cfg.static_wep_only = 1;
657
658         wps->registrar = wps_registrar_init(wps, &cfg);
659         if (wps->registrar == NULL) {
660                 printf("Failed to initialize WPS Registrar\n");
661                 os_free(wps->network_key);
662                 os_free(wps);
663                 return -1;
664         }
665
666 #ifdef CONFIG_WPS_UPNP
667         wps->friendly_name = hapd->conf->friendly_name;
668         wps->manufacturer_url = hapd->conf->manufacturer_url;
669         wps->model_description = hapd->conf->model_description;
670         wps->model_url = hapd->conf->model_url;
671         wps->upc = hapd->conf->upc;
672
673         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
674                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
675                 wps_registrar_deinit(wps->registrar);
676                 os_free(wps->network_key);
677                 os_free(wps);
678                 return -1;
679         }
680 #endif /* CONFIG_WPS_UPNP */
681
682         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
683
684         hapd->wps = wps;
685
686         return 0;
687 }
688
689
690 void hostapd_deinit_wps(struct hostapd_data *hapd)
691 {
692         if (hapd->wps == NULL)
693                 return;
694 #ifdef CONFIG_WPS_UPNP
695         hostapd_wps_upnp_deinit(hapd);
696 #endif /* CONFIG_WPS_UPNP */
697         wps_registrar_deinit(hapd->wps->registrar);
698         os_free(hapd->wps->network_key);
699         wps_device_data_free(&hapd->wps->dev);
700         wpabuf_free(hapd->wps->dh_pubkey);
701         wpabuf_free(hapd->wps->dh_privkey);
702         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
703         wpabuf_free(hapd->wps->oob_conf.dev_password);
704         wps_free_pending_msgs(hapd->wps->upnp_msgs);
705         os_free(hapd->wps);
706         hapd->wps = NULL;
707         hostapd_wps_clear_ies(hapd);
708 }
709
710
711 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
712                         const char *pin, int timeout)
713 {
714         u8 u[UUID_LEN];
715         int any = 0;
716
717         if (hapd->wps == NULL)
718                 return -1;
719         if (os_strcmp(uuid, "any") == 0)
720                 any = 1;
721         else if (uuid_str2bin(uuid, u))
722                 return -1;
723         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
724                                      (const u8 *) pin, os_strlen(pin),
725                                      timeout);
726 }
727
728
729 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
730 {
731         if (hapd->wps == NULL)
732                 return -1;
733         return wps_registrar_button_pushed(hapd->wps->registrar);
734 }
735
736
737 #ifdef CONFIG_WPS_OOB
738 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
739                           char *path, char *method, char *name)
740 {
741         struct wps_context *wps = hapd->wps;
742         struct oob_device_data *oob_dev;
743
744         oob_dev = wps_get_oob_device(device_type);
745         if (oob_dev == NULL)
746                 return -1;
747         oob_dev->device_path = path;
748         oob_dev->device_name = name;
749         wps->oob_conf.oob_method = wps_get_oob_method(method);
750
751         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
752                 /*
753                  * Use pre-configured DH keys in order to be able to write the
754                  * key hash into the OOB file.
755                  */
756                 wpabuf_free(wps->dh_pubkey);
757                 wpabuf_free(wps->dh_privkey);
758                 wps->dh_privkey = NULL;
759                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
760                                          &wps->dh_privkey);
761                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
762                 if (wps->dh_pubkey == NULL) {
763                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
764                                    "Diffie-Hellman handshake");
765                         return -1;
766                 }
767         }
768
769         if (wps_process_oob(wps, oob_dev, 1) < 0)
770                 goto error;
771
772         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
773              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
774             hostapd_wps_add_pin(hapd, "any",
775                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
776             0)
777                 goto error;
778
779         return 0;
780
781 error:
782         wpabuf_free(wps->dh_pubkey);
783         wps->dh_pubkey = NULL;
784         wpabuf_free(wps->dh_privkey);
785         wps->dh_privkey = NULL;
786         return -1;
787 }
788 #endif /* CONFIG_WPS_OOB */
789
790
791 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
792                                      const u8 *ie, size_t ie_len)
793 {
794         struct hostapd_data *hapd = ctx;
795         struct wpabuf *wps_ie;
796         const u8 *end, *pos, *wps;
797
798         if (hapd->wps == NULL)
799                 return;
800
801         pos = ie;
802         end = ie + ie_len;
803         wps = NULL;
804
805         while (pos + 1 < end) {
806                 if (pos + 2 + pos[1] > end)
807                         return;
808                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
809                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
810                         wps = pos;
811                         break;
812                 }
813                 pos += 2 + pos[1];
814         }
815
816         if (wps == NULL)
817                 return; /* No WPS IE in Probe Request */
818
819         wps_ie = wpabuf_alloc(ie_len);
820         if (wps_ie == NULL)
821                 return;
822
823         /* There may be multiple WPS IEs in the message, so need to concatenate
824          * their WPS Data fields */
825         while (pos + 1 < end) {
826                 if (pos + 2 + pos[1] > end)
827                         break;
828                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
829                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
830                         wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
831                 pos += 2 + pos[1];
832         }
833
834         if (wpabuf_len(wps_ie) > 0) {
835                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
836 #ifdef CONFIG_WPS_UPNP
837                 /* FIX: what exactly should be included in the WLANEvent?
838                  * WPS attributes? Full ProbeReq frame? */
839                 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
840                                                 UPNP_WPS_WLANEVENT_TYPE_PROBE,
841                                                 wps_ie);
842 #endif /* CONFIG_WPS_UPNP */
843         }
844
845         wpabuf_free(wps_ie);
846 }
847
848
849 #ifdef CONFIG_WPS_UPNP
850
851 static struct wpabuf *
852 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
853 {
854         struct hostapd_data *hapd = priv;
855         struct wps_config cfg;
856         struct wps_data *wps;
857         enum wsc_op_code op_code;
858         struct wpabuf *m1;
859
860         /*
861          * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
862          * registration over UPnP with the AP acting as an Enrollee. It should
863          * be noted that this is frequently used just to get the device data,
864          * i.e., there may not be any intent to actually complete the
865          * registration.
866          */
867
868         if (peer->wps)
869                 wps_deinit(peer->wps);
870
871         os_memset(&cfg, 0, sizeof(cfg));
872         cfg.wps = hapd->wps;
873         cfg.pin = (u8 *) hapd->conf->ap_pin;
874         cfg.pin_len = os_strlen(hapd->conf->ap_pin);
875         wps = wps_init(&cfg);
876         if (wps == NULL)
877                 return NULL;
878
879         m1 = wps_get_msg(wps, &op_code);
880         if (m1 == NULL) {
881                 wps_deinit(wps);
882                 return NULL;
883         }
884
885         peer->wps = wps;
886
887         return m1;
888 }
889
890
891 static struct wpabuf *
892 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
893                            const struct wpabuf *msg)
894 {
895         enum wps_process_res res;
896         enum wsc_op_code op_code;
897
898         /* PutMessage: msg = InMessage, return OutMessage */
899         res = wps_process_msg(peer->wps, WSC_UPnP, msg);
900         if (res == WPS_FAILURE)
901                 return NULL;
902         return wps_get_msg(peer->wps, &op_code);
903 }
904
905
906 static struct wpabuf *
907 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
908 {
909         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
910         return NULL;
911 }
912
913
914 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
915 {
916         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
917         return -1;
918 }
919
920
921 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
922 {
923         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
924         return -1;
925 }
926
927
928 static struct wpabuf *
929 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
930 {
931         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
932         return NULL;
933 }
934
935
936 static int hostapd_rx_req_set_sta_settings(void *priv,
937                                            const struct wpabuf *msg)
938 {
939         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
940         return -1;
941 }
942
943
944 static int hostapd_rx_req_del_sta_settings(void *priv,
945                                            const struct wpabuf *msg)
946 {
947         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
948         return -1;
949 }
950
951
952 static int hostapd_rx_req_put_wlan_response(
953         void *priv, enum upnp_wps_wlanevent_type ev_type,
954         const u8 *mac_addr, const struct wpabuf *msg,
955         enum wps_msg_type msg_type)
956 {
957         struct hostapd_data *hapd = priv;
958         struct sta_info *sta;
959         struct upnp_pending_message *p;
960
961         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
962                    MACSTR, ev_type, MAC2STR(mac_addr));
963         wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
964                           wpabuf_head(msg), wpabuf_len(msg));
965         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
966                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
967                            "PutWLANResponse WLANEventType %d", ev_type);
968                 return -1;
969         }
970
971         /*
972          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
973          * server implementation for delivery to the peer.
974          */
975
976         sta = ap_get_sta(hapd, mac_addr);
977         if (!sta) {
978                 /*
979                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
980                  * Pick STA that is in an ongoing WPS registration without
981                  * checking the MAC address.
982                  */
983                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
984                            "on NewWLANEventMAC; try wildcard match");
985                 for (sta = hapd->sta_list; sta; sta = sta->next) {
986                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
987                                 break;
988                 }
989         }
990
991         if (!sta) {
992                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
993                 return 0;
994         }
995
996         p = os_zalloc(sizeof(*p));
997         if (p == NULL)
998                 return -1;
999         os_memcpy(p->addr, sta->addr, ETH_ALEN);
1000         p->msg = wpabuf_dup(msg);
1001         p->type = msg_type;
1002         p->next = hapd->wps->upnp_msgs;
1003         hapd->wps->upnp_msgs = p;
1004
1005         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1006 }
1007
1008
1009 static int hostapd_rx_req_set_selected_registrar(void *priv,
1010                                                  const struct wpabuf *msg)
1011 {
1012         struct hostapd_data *hapd = priv;
1013         return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
1014 }
1015
1016
1017 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
1018 {
1019         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1020         return -1;
1021 }
1022
1023
1024 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
1025 {
1026         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1027         return -1;
1028 }
1029
1030
1031 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
1032 {
1033         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1034         return -1;
1035 }
1036
1037
1038 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
1039 {
1040         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1041         return -1;
1042 }
1043
1044
1045 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1046                                  struct wps_context *wps)
1047 {
1048         struct upnp_wps_device_ctx *ctx;
1049
1050         if (!hapd->conf->upnp_iface)
1051                 return 0;
1052         ctx = os_zalloc(sizeof(*ctx));
1053         if (ctx == NULL)
1054                 return -1;
1055
1056         ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
1057         ctx->rx_req_put_message = hostapd_rx_req_put_message;
1058         ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
1059         ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
1060         ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
1061         ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
1062         ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
1063         ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
1064         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1065         ctx->rx_req_set_selected_registrar =
1066                 hostapd_rx_req_set_selected_registrar;
1067         ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1068         ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1069         ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1070         ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1071
1072         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1073         if (hapd->wps_upnp == NULL) {
1074                 os_free(ctx);
1075                 return -1;
1076         }
1077         wps->wps_upnp = hapd->wps_upnp;
1078
1079         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1080                 upnp_wps_device_deinit(hapd->wps_upnp);
1081                 hapd->wps_upnp = NULL;
1082                 return -1;
1083         }
1084
1085         return 0;
1086 }
1087
1088
1089 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1090 {
1091         upnp_wps_device_deinit(hapd->wps_upnp);
1092 }
1093
1094 #endif /* CONFIG_WPS_UPNP */