WPS: Fix WEP key index to use 1..4 instead of 0..3
[libeap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / 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 "common.h"
18 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
20 #include "config.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
23 #include "eloop.h"
24 #include "uuid.h"
25 #include "wpa_ctrl.h"
26 #include "ctrl_iface_dbus.h"
27 #include "eap_common/eap_wsc_common.h"
28 #include "blacklist.h"
29 #include "wps_supplicant.h"
30
31 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
32
33 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
34 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
35
36
37 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
38 {
39         if (!wpa_s->wps_success &&
40             wpa_s->current_ssid &&
41             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
42                 const u8 *bssid = wpa_s->bssid;
43                 if (is_zero_ether_addr(bssid))
44                         bssid = wpa_s->pending_bssid;
45
46                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
47                            " did not succeed - continue trying to find "
48                            "suitable AP", MAC2STR(bssid));
49                 wpa_blacklist_add(wpa_s, bssid);
50
51                 wpa_supplicant_deauthenticate(wpa_s,
52                                               WLAN_REASON_DEAUTH_LEAVING);
53                 wpa_s->reassociate = 1;
54                 wpa_supplicant_req_scan(wpa_s,
55                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
56                 wpa_s->blacklist_cleared = 0;
57                 return 1;
58         }
59
60         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
61
62         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
63             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
64                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
65                            "try to associate with the received credential");
66                 wpa_supplicant_deauthenticate(wpa_s,
67                                               WLAN_REASON_DEAUTH_LEAVING);
68                 wpa_s->reassociate = 1;
69                 wpa_supplicant_req_scan(wpa_s, 0, 0);
70                 return 1;
71         }
72
73         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
74                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
75                            "for external credential processing");
76                 wpas_clear_wps(wpa_s);
77                 wpa_supplicant_deauthenticate(wpa_s,
78                                               WLAN_REASON_DEAUTH_LEAVING);
79                 return 1;
80         }
81
82         return 0;
83 }
84
85
86 static int wpa_supplicant_wps_cred(void *ctx,
87                                    const struct wps_credential *cred)
88 {
89         struct wpa_supplicant *wpa_s = ctx;
90         struct wpa_ssid *ssid = wpa_s->current_ssid;
91         u8 key_idx = 0;
92
93         if ((wpa_s->conf->wps_cred_processing == 1 ||
94              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
95                 size_t blen = cred->cred_attr_len * 2 + 1;
96                 char *buf = os_malloc(blen);
97                 if (buf) {
98                         wpa_snprintf_hex(buf, blen,
99                                          cred->cred_attr, cred->cred_attr_len);
100                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
101                                 WPS_EVENT_CRED_RECEIVED, buf);
102                         os_free(buf);
103                 }
104                 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
105         } else
106                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
107
108         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
109                         cred->cred_attr, cred->cred_attr_len);
110
111         if (wpa_s->conf->wps_cred_processing == 1)
112                 return 0;
113
114         if (cred->auth_type != WPS_AUTH_OPEN &&
115             cred->auth_type != WPS_AUTH_SHARED &&
116             cred->auth_type != WPS_AUTH_WPAPSK &&
117             cred->auth_type != WPS_AUTH_WPA2PSK) {
118                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
119                            "unsupported authentication type %d",
120                            cred->auth_type);
121                 return 0;
122         }
123
124         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
125                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
126                            "on the received credential");
127                 os_free(ssid->eap.identity);
128                 ssid->eap.identity = NULL;
129                 ssid->eap.identity_len = 0;
130                 os_free(ssid->eap.phase1);
131                 ssid->eap.phase1 = NULL;
132                 os_free(ssid->eap.eap_methods);
133                 ssid->eap.eap_methods = NULL;
134         } else {
135                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
136                            "received credential");
137                 ssid = wpa_config_add_network(wpa_s->conf);
138                 if (ssid == NULL)
139                         return -1;
140         }
141
142         wpa_config_set_network_defaults(ssid);
143
144         os_free(ssid->ssid);
145         ssid->ssid = os_malloc(cred->ssid_len);
146         if (ssid->ssid) {
147                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
148                 ssid->ssid_len = cred->ssid_len;
149         }
150
151         switch (cred->encr_type) {
152         case WPS_ENCR_NONE:
153                 break;
154         case WPS_ENCR_WEP:
155                 if (cred->key_len <= 0)
156                         break;
157                 if (cred->key_len != 5 && cred->key_len != 13 &&
158                     cred->key_len != 10 && cred->key_len != 26) {
159                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
160                                    "%lu", (unsigned long) cred->key_len);
161                         return -1;
162                 }
163                 if (cred->key_idx > NUM_WEP_KEYS) {
164                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
165                                    cred->key_idx);
166                         return -1;
167                 }
168                 if (cred->key_idx)
169                         key_idx = cred->key_idx - 1;
170                 if (cred->key_len == 10 || cred->key_len == 26) {
171                         if (hexstr2bin((char *) cred->key,
172                                        ssid->wep_key[key_idx],
173                                        cred->key_len / 2) < 0) {
174                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
175                                            "%d", key_idx);
176                                 return -1;
177                         }
178                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
179                 } else {
180                         os_memcpy(ssid->wep_key[key_idx], cred->key,
181                                   cred->key_len);
182                         ssid->wep_key_len[key_idx] = cred->key_len;
183                 }
184                 ssid->wep_tx_keyidx = key_idx;
185                 break;
186         case WPS_ENCR_TKIP:
187                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
188                 break;
189         case WPS_ENCR_AES:
190                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
191                 break;
192         }
193
194         switch (cred->auth_type) {
195         case WPS_AUTH_OPEN:
196                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
197                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
198                 ssid->proto = 0;
199                 break;
200         case WPS_AUTH_SHARED:
201                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
202                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
203                 ssid->proto = 0;
204                 break;
205         case WPS_AUTH_WPAPSK:
206                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
207                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
208                 ssid->proto = WPA_PROTO_WPA;
209                 break;
210         case WPS_AUTH_WPA:
211                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
212                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
213                 ssid->proto = WPA_PROTO_WPA;
214                 break;
215         case WPS_AUTH_WPA2:
216                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
217                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
218                 ssid->proto = WPA_PROTO_RSN;
219                 break;
220         case WPS_AUTH_WPA2PSK:
221                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
222                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
223                 ssid->proto = WPA_PROTO_RSN;
224                 break;
225         }
226
227         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
228                 if (cred->key_len == 2 * PMK_LEN) {
229                         if (hexstr2bin((const char *) cred->key, ssid->psk,
230                                        PMK_LEN)) {
231                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
232                                            "Key");
233                                 return -1;
234                         }
235                         ssid->psk_set = 1;
236                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
237                         os_free(ssid->passphrase);
238                         ssid->passphrase = os_malloc(cred->key_len + 1);
239                         if (ssid->passphrase == NULL)
240                                 return -1;
241                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
242                         ssid->passphrase[cred->key_len] = '\0';
243                         wpa_config_update_psk(ssid);
244                 } else {
245                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
246                                    "length %lu",
247                                    (unsigned long) cred->key_len);
248                         return -1;
249                 }
250         }
251
252 #ifndef CONFIG_NO_CONFIG_WRITE
253         if (wpa_s->conf->update_config &&
254             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
255                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
256                 return -1;
257         }
258 #endif /* CONFIG_NO_CONFIG_WRITE */
259
260         return 0;
261 }
262
263
264 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
265                                          struct wps_event_m2d *m2d)
266 {
267         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
268                 "dev_password_id=%d config_error=%d",
269                 m2d->dev_password_id, m2d->config_error);
270 }
271
272
273 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
274                                           struct wps_event_fail *fail)
275 {
276         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
277         wpas_clear_wps(wpa_s);
278 }
279
280
281 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
282 {
283         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
284         wpa_s->wps_success = 1;
285 }
286
287
288 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
289                                      union wps_event_data *data)
290 {
291         struct wpa_supplicant *wpa_s = ctx;
292         switch (event) {
293         case WPS_EV_M2D:
294                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
295                 break;
296         case WPS_EV_FAIL:
297                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
298                 break;
299         case WPS_EV_SUCCESS:
300                 wpa_supplicant_wps_event_success(wpa_s);
301                 break;
302         case WPS_EV_PWD_AUTH_FAIL:
303                 break;
304         }
305 }
306
307
308 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
309 {
310         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
311             eap_is_wps_pin_enrollee(&ssid->eap))
312                 return WPS_REQ_ENROLLEE;
313         else
314                 return WPS_REQ_REGISTRAR;
315 }
316
317
318 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
319 {
320         int id;
321         struct wpa_ssid *ssid;
322
323         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
324
325         /* Remove any existing WPS network from configuration */
326         ssid = wpa_s->conf->ssid;
327         while (ssid) {
328                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
329                         if (ssid == wpa_s->current_ssid)
330                                 wpa_s->current_ssid = NULL;
331                         id = ssid->id;
332                 } else
333                         id = -1;
334                 ssid = ssid->next;
335                 if (id >= 0)
336                         wpa_config_remove_network(wpa_s->conf, id);
337         }
338 }
339
340
341 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
342 {
343         struct wpa_supplicant *wpa_s = eloop_ctx;
344         wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
345                    "out");
346         wpas_clear_wps(wpa_s);
347 }
348
349
350 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
351                                               int registrar, const u8 *bssid)
352 {
353         struct wpa_ssid *ssid;
354
355         ssid = wpa_config_add_network(wpa_s->conf);
356         if (ssid == NULL)
357                 return NULL;
358         wpa_config_set_network_defaults(ssid);
359         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
360             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
361             wpa_config_set(ssid, "identity", registrar ?
362                            "\"" WSC_ID_REGISTRAR "\"" :
363                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
364                 wpa_config_remove_network(wpa_s->conf, ssid->id);
365                 return NULL;
366         }
367
368         if (bssid) {
369                 size_t i;
370                 struct wpa_scan_res *res;
371
372                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
373                 ssid->bssid_set = 1;
374
375                 /* Try to get SSID from scan results */
376                 if (wpa_s->scan_res == NULL &&
377                     wpa_supplicant_get_scan_results(wpa_s) < 0)
378                         return ssid; /* Could not find any scan results */
379
380                 for (i = 0; i < wpa_s->scan_res->num; i++) {
381                         const u8 *ie;
382
383                         res = wpa_s->scan_res->res[i];
384                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
385                                 continue;
386
387                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
388                         if (ie == NULL)
389                                 break;
390                         os_free(ssid->ssid);
391                         ssid->ssid = os_malloc(ie[1]);
392                         if (ssid->ssid == NULL)
393                                 break;
394                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
395                         ssid->ssid_len = ie[1];
396                         break;
397                 }
398         }
399
400         return ssid;
401 }
402
403
404 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
405                              struct wpa_ssid *selected)
406 {
407         struct wpa_ssid *ssid;
408
409         /* Mark all other networks disabled and trigger reassociation */
410         ssid = wpa_s->conf->ssid;
411         while (ssid) {
412                 ssid->disabled = ssid != selected;
413                 ssid = ssid->next;
414         }
415         wpa_s->disconnected = 0;
416         wpa_s->reassociate = 1;
417         wpa_s->scan_runs = 0;
418         wpa_s->wps_success = 0;
419         wpa_s->blacklist_cleared = 0;
420         wpa_supplicant_req_scan(wpa_s, 0, 0);
421 }
422
423
424 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
425 {
426         struct wpa_ssid *ssid;
427         wpas_clear_wps(wpa_s);
428         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
429         if (ssid == NULL)
430                 return -1;
431         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
432         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
433                                wpa_s, NULL);
434         wpas_wps_reassoc(wpa_s, ssid);
435         return 0;
436 }
437
438
439 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
440                        const char *pin)
441 {
442         struct wpa_ssid *ssid;
443         char val[30];
444         unsigned int rpin = 0;
445
446         wpas_clear_wps(wpa_s);
447         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
448         if (ssid == NULL)
449                 return -1;
450         if (pin)
451                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
452         else {
453                 rpin = wps_generate_pin();
454                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
455         }
456         wpa_config_set(ssid, "phase1", val, 0);
457         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
458                                wpa_s, NULL);
459         wpas_wps_reassoc(wpa_s, ssid);
460         return rpin;
461 }
462
463
464 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
465                        const char *pin)
466 {
467         struct wpa_ssid *ssid;
468         char val[30];
469
470         if (!pin)
471                 return -1;
472         wpas_clear_wps(wpa_s);
473         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
474         if (ssid == NULL)
475                 return -1;
476         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
477         wpa_config_set(ssid, "phase1", val, 0);
478         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
479                                wpa_s, NULL);
480         wpas_wps_reassoc(wpa_s, ssid);
481         return 0;
482 }
483
484
485 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
486                                size_t psk_len)
487 {
488         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
489                    "STA " MACSTR, MAC2STR(mac_addr));
490         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
491
492         /* TODO */
493
494         return 0;
495 }
496
497
498 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
499                                    const struct wps_device_data *dev)
500 {
501         char uuid[40], txt[400];
502         int len;
503         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
504                 return;
505         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
506         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
507                           " [%s|%s|%s|%s|%s|%d-%08X-%d]",
508                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
509                           dev->manufacturer, dev->model_name,
510                           dev->model_number, dev->serial_number,
511                           dev->categ, dev->oui, dev->sub_categ);
512         if (len > 0 && len < (int) sizeof(txt))
513                 wpa_printf(MSG_INFO, "%s", txt);
514 }
515
516
517 int wpas_wps_init(struct wpa_supplicant *wpa_s)
518 {
519         struct wps_context *wps;
520         struct wps_registrar_config rcfg;
521
522         wps = os_zalloc(sizeof(*wps));
523         if (wps == NULL)
524                 return -1;
525
526         wps->cred_cb = wpa_supplicant_wps_cred;
527         wps->event_cb = wpa_supplicant_wps_event;
528         wps->cb_ctx = wpa_s;
529
530         wps->dev.device_name = wpa_s->conf->device_name;
531         wps->dev.manufacturer = wpa_s->conf->manufacturer;
532         wps->dev.model_name = wpa_s->conf->model_name;
533         wps->dev.model_number = wpa_s->conf->model_number;
534         wps->dev.serial_number = wpa_s->conf->serial_number;
535         if (wpa_s->conf->device_type) {
536                 char *pos;
537                 u8 oui[4];
538                 /* <categ>-<OUI>-<subcateg> */
539                 wps->dev.categ = atoi(wpa_s->conf->device_type);
540                 pos = os_strchr(wpa_s->conf->device_type, '-');
541                 if (pos == NULL) {
542                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
543                         os_free(wps);
544                         return -1;
545                 }
546                 pos++;
547                 if (hexstr2bin(pos, oui, 4)) {
548                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
549                         os_free(wps);
550                         return -1;
551                 }
552                 wps->dev.oui = WPA_GET_BE32(oui);
553                 pos = os_strchr(pos, '-');
554                 if (pos == NULL) {
555                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
556                         os_free(wps);
557                         return -1;
558                 }
559                 pos++;
560                 wps->dev.sub_categ = atoi(pos);
561         }
562         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
563         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
564         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
565         if (is_nil_uuid(wpa_s->conf->uuid)) {
566                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
567                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
568                             wps->uuid, WPS_UUID_LEN);
569         } else
570                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
571
572         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
573         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
574
575         os_memset(&rcfg, 0, sizeof(rcfg));
576         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
577         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
578         rcfg.cb_ctx = wpa_s;
579
580         wps->registrar = wps_registrar_init(wps, &rcfg);
581         if (wps->registrar == NULL) {
582                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
583                 os_free(wps);
584                 return -1;
585         }
586
587         wpa_s->wps = wps;
588
589         return 0;
590 }
591
592
593 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
594 {
595         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
596
597         if (wpa_s->wps == NULL)
598                 return;
599
600         wps_registrar_deinit(wpa_s->wps->registrar);
601         os_free(wpa_s->wps->network_key);
602         os_free(wpa_s->wps);
603         wpa_s->wps = NULL;
604 }
605
606
607 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
608                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
609 {
610         struct wpabuf *wps_ie;
611
612         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
613                 return -1;
614
615         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
616         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
617                 if (!wps_ie) {
618                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
619                         return 0;
620                 }
621
622                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
623                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
624                                    "without active PBC Registrar");
625                         wpabuf_free(wps_ie);
626                         return 0;
627                 }
628
629                 /* TODO: overlap detection */
630                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
631                            "(Active PBC)");
632                 wpabuf_free(wps_ie);
633                 return 1;
634         }
635
636         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
637                 if (!wps_ie) {
638                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
639                         return 0;
640                 }
641
642                 /*
643                  * Start with WPS APs that advertise active PIN Registrar and
644                  * allow any WPS AP after third scan since some APs do not set
645                  * Selected Registrar attribute properly when using external
646                  * Registrar.
647                  */
648                 if (!wps_is_selected_pin_registrar(wps_ie)) {
649                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
650                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
651                                            "without active PIN Registrar");
652                                 wpabuf_free(wps_ie);
653                                 return 0;
654                         }
655                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
656                 } else {
657                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
658                                    "(Active PIN)");
659                 }
660                 wpabuf_free(wps_ie);
661                 return 1;
662         }
663
664         if (wps_ie) {
665                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
666                 wpabuf_free(wps_ie);
667                 return 1;
668         }
669
670         return -1;
671 }
672
673
674 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
675                               struct wpa_ssid *ssid,
676                               struct wpa_scan_res *bss)
677 {
678         struct wpabuf *wps_ie = NULL;
679         int ret = 0;
680
681         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
682                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
683                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
684                         /* allow wildcard SSID for WPS PBC */
685                         ret = 1;
686                 }
687         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
688                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
689                 if (wps_ie &&
690                     (wps_is_selected_pin_registrar(wps_ie) ||
691                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
692                         /* allow wildcard SSID for WPS PIN */
693                         ret = 1;
694                 }
695         }
696
697         if (!ret && ssid->bssid_set &&
698             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
699                 /* allow wildcard SSID due to hardcoded BSSID match */
700                 ret = 1;
701         }
702
703         wpabuf_free(wps_ie);
704
705         return ret;
706 }
707
708
709 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
710                               struct wpa_scan_res *selected,
711                               struct wpa_ssid *ssid)
712 {
713         const u8 *sel_uuid, *uuid;
714         size_t i;
715         struct wpabuf *wps_ie;
716         int ret = 0;
717
718         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
719                 return 0;
720
721         /* Make sure that only one AP is in active PBC mode */
722         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
723         if (wps_ie)
724                 sel_uuid = wps_get_uuid_e(wps_ie);
725         else
726                 sel_uuid = NULL;
727
728         for (i = 0; i < wpa_s->scan_res->num; i++) {
729                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
730                 struct wpabuf *ie;
731                 if (bss == selected)
732                         continue;
733                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
734                 if (!ie)
735                         continue;
736                 if (!wps_is_selected_pbc_registrar(ie)) {
737                         wpabuf_free(ie);
738                         continue;
739                 }
740                 uuid = wps_get_uuid_e(ie);
741                 if (sel_uuid == NULL || uuid == NULL ||
742                     os_memcmp(sel_uuid, uuid, 16) != 0) {
743                         ret = 1; /* PBC overlap */
744                         wpabuf_free(ie);
745                         break;
746                 }
747
748                 /* TODO: verify that this is reasonable dual-band situation */
749
750                 wpabuf_free(ie);
751         }
752
753         wpabuf_free(wps_ie);
754
755         return ret;
756 }
757
758
759 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
760 {
761         size_t i;
762
763         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
764                 return;
765
766         for (i = 0; i < wpa_s->scan_res->num; i++) {
767                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
768                 struct wpabuf *ie;
769                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
770                 if (!ie)
771                         continue;
772                 if (wps_is_selected_pbc_registrar(ie))
773                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
774                 else if (wps_is_selected_pin_registrar(ie))
775                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
776                 else
777                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
778                 wpabuf_free(ie);
779                 break;
780         }
781 }
782
783
784 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
785 {
786         struct wpa_ssid *ssid;
787
788         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
789                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
790                         return 1;
791         }
792
793         return 0;
794 }