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