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