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