P2P: Use SSID from GO Negotiation to limit WPS provisioning step
[mech_eap.orig] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2010, 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 "eloop.h"
19 #include "uuid.h"
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
28 #include "config.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "notify.h"
32 #include "blacklist.h"
33 #include "bss.h"
34 #include "scan.h"
35 #include "p2p/p2p.h"
36 #include "p2p_supplicant.h"
37 #include "wps_supplicant.h"
38
39
40 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
41 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
42 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
43
44 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
45 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
46
47
48 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
49 {
50         if (!wpa_s->wps_success &&
51             wpa_s->current_ssid &&
52             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
53                 const u8 *bssid = wpa_s->bssid;
54                 if (is_zero_ether_addr(bssid))
55                         bssid = wpa_s->pending_bssid;
56
57                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
58                            " did not succeed - continue trying to find "
59                            "suitable AP", MAC2STR(bssid));
60                 wpa_blacklist_add(wpa_s, bssid);
61
62                 wpa_supplicant_deauthenticate(wpa_s,
63                                               WLAN_REASON_DEAUTH_LEAVING);
64                 wpa_s->reassociate = 1;
65                 wpa_supplicant_req_scan(wpa_s,
66                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
67                 wpa_s->blacklist_cleared = 0;
68                 return 1;
69         }
70
71         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
72
73         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
74             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
75                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
76                            "try to associate with the received credential");
77                 wpa_supplicant_deauthenticate(wpa_s,
78                                               WLAN_REASON_DEAUTH_LEAVING);
79                 wpa_s->after_wps = 5;
80                 wpa_s->wps_freq = wpa_s->assoc_freq;
81                 wpa_s->reassociate = 1;
82                 wpa_supplicant_req_scan(wpa_s, 0, 0);
83                 return 1;
84         }
85
86         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
87                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
88                            "for external credential processing");
89                 wpas_clear_wps(wpa_s);
90                 wpa_supplicant_deauthenticate(wpa_s,
91                                               WLAN_REASON_DEAUTH_LEAVING);
92                 return 1;
93         }
94
95         return 0;
96 }
97
98
99 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
100                                          struct wpa_ssid *ssid,
101                                          const struct wps_credential *cred)
102 {
103         struct wpa_driver_capa capa;
104         struct wpa_bss *bss;
105         const u8 *ie;
106         struct wpa_ie_data adv;
107         int wpa2 = 0, ccmp = 0;
108
109         /*
110          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
111          * case they are configured for mixed mode operation (WPA+WPA2 and
112          * TKIP+CCMP). Try to use scan results to figure out whether the AP
113          * actually supports stronger security and select that if the client
114          * has support for it, too.
115          */
116
117         if (wpa_drv_get_capa(wpa_s, &capa))
118                 return; /* Unknown what driver supports */
119
120         bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
121         if (bss == NULL) {
122                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
123                            "table - use credential as-is");
124                 return;
125         }
126
127         wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
128
129         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
130         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
131                 wpa2 = 1;
132                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
133                         ccmp = 1;
134         } else {
135                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
136                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
137                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
138                         ccmp = 1;
139         }
140
141         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
142             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
143                 /*
144                  * TODO: This could be the initial AP configuration and the
145                  * Beacon contents could change shortly. Should request a new
146                  * scan and delay addition of the network until the updated
147                  * scan results are available.
148                  */
149                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
150                            "support - use credential as-is");
151                 return;
152         }
153
154         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
155             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
156             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
157                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
158                            "based on scan results");
159                 if (wpa_s->conf->ap_scan == 1)
160                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
161                 else
162                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
163         }
164
165         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
166             (ssid->proto & WPA_PROTO_WPA) &&
167             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
168                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
169                            "based on scan results");
170                 if (wpa_s->conf->ap_scan == 1)
171                         ssid->proto |= WPA_PROTO_RSN;
172                 else
173                         ssid->proto = WPA_PROTO_RSN;
174         }
175 }
176
177
178 static int wpa_supplicant_wps_cred(void *ctx,
179                                    const struct wps_credential *cred)
180 {
181         struct wpa_supplicant *wpa_s = ctx;
182         struct wpa_ssid *ssid = wpa_s->current_ssid;
183         u8 key_idx = 0;
184         u16 auth_type;
185
186         if ((wpa_s->conf->wps_cred_processing == 1 ||
187              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
188                 size_t blen = cred->cred_attr_len * 2 + 1;
189                 char *buf = os_malloc(blen);
190                 if (buf) {
191                         wpa_snprintf_hex(buf, blen,
192                                          cred->cred_attr, cred->cred_attr_len);
193                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
194                                 WPS_EVENT_CRED_RECEIVED, buf);
195                         os_free(buf);
196                 }
197
198                 wpas_notify_wps_credential(wpa_s, cred);
199         } else
200                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
201
202         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
203                         cred->cred_attr, cred->cred_attr_len);
204
205         if (wpa_s->conf->wps_cred_processing == 1)
206                 return 0;
207
208         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
209         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
210                    cred->auth_type);
211         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
212         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
213         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
214                         cred->key, cred->key_len);
215         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
216                    MAC2STR(cred->mac_addr));
217
218         auth_type = cred->auth_type;
219         if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
220                 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
221                            "auth_type into WPA2PSK");
222                 auth_type = WPS_AUTH_WPA2PSK;
223         }
224
225         if (auth_type != WPS_AUTH_OPEN &&
226             auth_type != WPS_AUTH_SHARED &&
227             auth_type != WPS_AUTH_WPAPSK &&
228             auth_type != WPS_AUTH_WPA2PSK) {
229                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
230                            "unsupported authentication type 0x%x",
231                            auth_type);
232                 return 0;
233         }
234
235         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
236                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
237                            "on the received credential");
238                 os_free(ssid->eap.identity);
239                 ssid->eap.identity = NULL;
240                 ssid->eap.identity_len = 0;
241                 os_free(ssid->eap.phase1);
242                 ssid->eap.phase1 = NULL;
243                 os_free(ssid->eap.eap_methods);
244                 ssid->eap.eap_methods = NULL;
245                 if (!ssid->p2p_group)
246                         ssid->temporary = 0;
247         } else {
248                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
249                            "received credential");
250                 ssid = wpa_config_add_network(wpa_s->conf);
251                 if (ssid == NULL)
252                         return -1;
253                 wpas_notify_network_added(wpa_s, ssid);
254         }
255
256         wpa_config_set_network_defaults(ssid);
257
258         os_free(ssid->ssid);
259         ssid->ssid = os_malloc(cred->ssid_len);
260         if (ssid->ssid) {
261                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
262                 ssid->ssid_len = cred->ssid_len;
263         }
264
265         switch (cred->encr_type) {
266         case WPS_ENCR_NONE:
267                 break;
268         case WPS_ENCR_WEP:
269                 if (cred->key_len <= 0)
270                         break;
271                 if (cred->key_len != 5 && cred->key_len != 13 &&
272                     cred->key_len != 10 && cred->key_len != 26) {
273                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
274                                    "%lu", (unsigned long) cred->key_len);
275                         return -1;
276                 }
277                 if (cred->key_idx > NUM_WEP_KEYS) {
278                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
279                                    cred->key_idx);
280                         return -1;
281                 }
282                 if (cred->key_idx)
283                         key_idx = cred->key_idx - 1;
284                 if (cred->key_len == 10 || cred->key_len == 26) {
285                         if (hexstr2bin((char *) cred->key,
286                                        ssid->wep_key[key_idx],
287                                        cred->key_len / 2) < 0) {
288                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
289                                            "%d", key_idx);
290                                 return -1;
291                         }
292                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
293                 } else {
294                         os_memcpy(ssid->wep_key[key_idx], cred->key,
295                                   cred->key_len);
296                         ssid->wep_key_len[key_idx] = cred->key_len;
297                 }
298                 ssid->wep_tx_keyidx = key_idx;
299                 break;
300         case WPS_ENCR_TKIP:
301                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
302                 break;
303         case WPS_ENCR_AES:
304                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
305                 break;
306         }
307
308         switch (auth_type) {
309         case WPS_AUTH_OPEN:
310                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
311                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
312                 ssid->proto = 0;
313                 break;
314         case WPS_AUTH_SHARED:
315                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
316                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
317                 ssid->proto = 0;
318                 break;
319         case WPS_AUTH_WPAPSK:
320                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
321                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
322                 ssid->proto = WPA_PROTO_WPA;
323                 break;
324         case WPS_AUTH_WPA:
325                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
326                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
327                 ssid->proto = WPA_PROTO_WPA;
328                 break;
329         case WPS_AUTH_WPA2:
330                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
331                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
332                 ssid->proto = WPA_PROTO_RSN;
333                 break;
334         case WPS_AUTH_WPA2PSK:
335                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
336                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
337                 ssid->proto = WPA_PROTO_RSN;
338                 break;
339         }
340
341         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
342                 if (cred->key_len == 2 * PMK_LEN) {
343                         if (hexstr2bin((const char *) cred->key, ssid->psk,
344                                        PMK_LEN)) {
345                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
346                                            "Key");
347                                 return -1;
348                         }
349                         ssid->psk_set = 1;
350                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
351                         os_free(ssid->passphrase);
352                         ssid->passphrase = os_malloc(cred->key_len + 1);
353                         if (ssid->passphrase == NULL)
354                                 return -1;
355                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
356                         ssid->passphrase[cred->key_len] = '\0';
357                         wpa_config_update_psk(ssid);
358                 } else {
359                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
360                                    "length %lu",
361                                    (unsigned long) cred->key_len);
362                         return -1;
363                 }
364         }
365
366         wpas_wps_security_workaround(wpa_s, ssid, cred);
367
368 #ifndef CONFIG_NO_CONFIG_WRITE
369         if (wpa_s->conf->update_config &&
370             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
371                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
372                 return -1;
373         }
374 #endif /* CONFIG_NO_CONFIG_WRITE */
375
376         return 0;
377 }
378
379
380 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
381                                          struct wps_event_m2d *m2d)
382 {
383         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
384                 "dev_password_id=%d config_error=%d",
385                 m2d->dev_password_id, m2d->config_error);
386         wpas_notify_wps_event_m2d(wpa_s, m2d);
387 }
388
389
390 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
391                                           struct wps_event_fail *fail)
392 {
393         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
394         wpas_clear_wps(wpa_s);
395         wpas_notify_wps_event_fail(wpa_s, fail);
396 }
397
398
399 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
400 {
401         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
402         wpa_s->wps_success = 1;
403         wpas_notify_wps_event_success(wpa_s);
404 #ifdef CONFIG_P2P
405         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
406 #endif /* CONFIG_P2P */
407 }
408
409
410 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
411                                                struct wps_event_er_ap *ap)
412 {
413         char uuid_str[100];
414         char dev_type[WPS_DEV_TYPE_BUFSIZE];
415
416         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
417         if (ap->pri_dev_type)
418                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
419                                      sizeof(dev_type));
420         else
421                 dev_type[0] = '\0';
422
423         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
424                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
425                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
426                 ap->friendly_name ? ap->friendly_name : "",
427                 ap->manufacturer ? ap->manufacturer : "",
428                 ap->model_description ? ap->model_description : "",
429                 ap->model_name ? ap->model_name : "",
430                 ap->manufacturer_url ? ap->manufacturer_url : "",
431                 ap->model_url ? ap->model_url : "");
432 }
433
434
435 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
436                                                   struct wps_event_er_ap *ap)
437 {
438         char uuid_str[100];
439         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
440         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
441 }
442
443
444 static void wpa_supplicant_wps_event_er_enrollee_add(
445         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
446 {
447         char uuid_str[100];
448         char dev_type[WPS_DEV_TYPE_BUFSIZE];
449
450         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
451         if (enrollee->pri_dev_type)
452                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
453                                      sizeof(dev_type));
454         else
455                 dev_type[0] = '\0';
456
457         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
458                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
459                 "|%s|%s|%s|%s|%s|",
460                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
461                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
462                 enrollee->dev_name ? enrollee->dev_name : "",
463                 enrollee->manufacturer ? enrollee->manufacturer : "",
464                 enrollee->model_name ? enrollee->model_name : "",
465                 enrollee->model_number ? enrollee->model_number : "",
466                 enrollee->serial_number ? enrollee->serial_number : "");
467 }
468
469
470 static void wpa_supplicant_wps_event_er_enrollee_remove(
471         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
472 {
473         char uuid_str[100];
474         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
475         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
476                 uuid_str, MAC2STR(enrollee->mac_addr));
477 }
478
479
480 static void wpa_supplicant_wps_event_er_ap_settings(
481         struct wpa_supplicant *wpa_s,
482         struct wps_event_er_ap_settings *ap_settings)
483 {
484         char uuid_str[100];
485         char key_str[65];
486         const struct wps_credential *cred = ap_settings->cred;
487
488         key_str[0] = '\0';
489         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
490                 if (cred->key_len >= 8 && cred->key_len <= 64) {
491                         os_memcpy(key_str, cred->key, cred->key_len);
492                         key_str[cred->key_len] = '\0';
493                 }
494         }
495
496         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
497         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
498         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
499                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
500                      "key=%s",
501                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
502                      cred->auth_type, cred->encr_type, key_str);
503 }
504
505
506 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
507                                      union wps_event_data *data)
508 {
509         struct wpa_supplicant *wpa_s = ctx;
510         switch (event) {
511         case WPS_EV_M2D:
512                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
513                 break;
514         case WPS_EV_FAIL:
515                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
516                 break;
517         case WPS_EV_SUCCESS:
518                 wpa_supplicant_wps_event_success(wpa_s);
519                 break;
520         case WPS_EV_PWD_AUTH_FAIL:
521                 break;
522         case WPS_EV_PBC_OVERLAP:
523                 break;
524         case WPS_EV_PBC_TIMEOUT:
525                 break;
526         case WPS_EV_ER_AP_ADD:
527                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
528                 break;
529         case WPS_EV_ER_AP_REMOVE:
530                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
531                 break;
532         case WPS_EV_ER_ENROLLEE_ADD:
533                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
534                                                          &data->enrollee);
535                 break;
536         case WPS_EV_ER_ENROLLEE_REMOVE:
537                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
538                                                             &data->enrollee);
539                 break;
540         case WPS_EV_ER_AP_SETTINGS:
541                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
542                                                         &data->ap_settings);
543                 break;
544         }
545 }
546
547
548 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
549 {
550         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
551             eap_is_wps_pin_enrollee(&ssid->eap))
552                 return WPS_REQ_ENROLLEE;
553         else
554                 return WPS_REQ_REGISTRAR;
555 }
556
557
558 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
559 {
560         int id;
561         struct wpa_ssid *ssid, *remove_ssid = NULL;
562
563         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
564
565         /* Remove any existing WPS network from configuration */
566         ssid = wpa_s->conf->ssid;
567         while (ssid) {
568                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
569                         if (ssid == wpa_s->current_ssid) {
570                                 wpa_s->current_ssid = NULL;
571                                 if (ssid != NULL)
572                                         wpas_notify_network_changed(wpa_s);
573                         }
574                         id = ssid->id;
575                         remove_ssid = ssid;
576                 } else
577                         id = -1;
578                 ssid = ssid->next;
579                 if (id >= 0) {
580                         wpas_notify_network_removed(wpa_s, remove_ssid);
581                         wpa_config_remove_network(wpa_s->conf, id);
582                 }
583         }
584 }
585
586
587 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
588 {
589         struct wpa_supplicant *wpa_s = eloop_ctx;
590         wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
591                    "out");
592         wpas_clear_wps(wpa_s);
593 }
594
595
596 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
597                                               int registrar, const u8 *bssid)
598 {
599         struct wpa_ssid *ssid;
600
601         ssid = wpa_config_add_network(wpa_s->conf);
602         if (ssid == NULL)
603                 return NULL;
604         wpas_notify_network_added(wpa_s, ssid);
605         wpa_config_set_network_defaults(ssid);
606         ssid->temporary = 1;
607         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
608             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
609             wpa_config_set(ssid, "identity", registrar ?
610                            "\"" WSC_ID_REGISTRAR "\"" :
611                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
612                 wpas_notify_network_removed(wpa_s, ssid);
613                 wpa_config_remove_network(wpa_s->conf, ssid->id);
614                 return NULL;
615         }
616
617         if (bssid) {
618 #ifndef CONFIG_P2P
619                 struct wpa_bss *bss;
620                 int count = 0;
621 #endif /* CONFIG_P2P */
622
623                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
624                 ssid->bssid_set = 1;
625
626                 /*
627                  * Note: With P2P, the SSID may change at the time the WPS
628                  * provisioning is started, so better not filter the AP based
629                  * on the current SSID in the scan results.
630                  */
631 #ifndef CONFIG_P2P
632                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
633                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
634                                 continue;
635
636                         os_free(ssid->ssid);
637                         ssid->ssid = os_malloc(bss->ssid_len);
638                         if (ssid->ssid == NULL)
639                                 break;
640                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
641                         ssid->ssid_len = bss->ssid_len;
642                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
643                                           "scan results",
644                                           ssid->ssid, ssid->ssid_len);
645                         count++;
646                 }
647
648                 if (count > 1) {
649                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
650                                    "for the AP; use wildcard");
651                         os_free(ssid->ssid);
652                         ssid->ssid = NULL;
653                         ssid->ssid_len = 0;
654                 }
655 #endif /* CONFIG_P2P */
656         }
657
658         return ssid;
659 }
660
661
662 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
663                              struct wpa_ssid *selected)
664 {
665         struct wpa_ssid *ssid;
666
667         /* Mark all other networks disabled and trigger reassociation */
668         ssid = wpa_s->conf->ssid;
669         while (ssid) {
670                 int was_disabled = ssid->disabled;
671                 ssid->disabled = ssid != selected;
672                 if (was_disabled != ssid->disabled)
673                         wpas_notify_network_enabled_changed(wpa_s, ssid);
674                 ssid = ssid->next;
675         }
676         wpa_s->disconnected = 0;
677         wpa_s->reassociate = 1;
678         wpa_s->scan_runs = 0;
679         wpa_s->wps_success = 0;
680         wpa_s->blacklist_cleared = 0;
681         wpa_supplicant_req_scan(wpa_s, 0, 0);
682 }
683
684
685 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
686                        int p2p_group)
687 {
688         struct wpa_ssid *ssid;
689         wpas_clear_wps(wpa_s);
690         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
691         if (ssid == NULL)
692                 return -1;
693         ssid->temporary = 1;
694         ssid->p2p_group = p2p_group;
695         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
696                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
697                 if (ssid->ssid) {
698                         ssid->ssid_len = wpa_s->go_params->ssid_len;
699                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
700                                   ssid->ssid_len);
701                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
702                                           "SSID", ssid->ssid, ssid->ssid_len);
703                 }
704         }
705         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
706         if (wpa_s->wps_fragment_size)
707                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
708         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
709                                wpa_s, NULL);
710         wpas_wps_reassoc(wpa_s, ssid);
711         return 0;
712 }
713
714
715 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
716                        const char *pin, int p2p_group, u16 dev_pw_id)
717 {
718         struct wpa_ssid *ssid;
719         char val[128];
720         unsigned int rpin = 0;
721
722         wpas_clear_wps(wpa_s);
723         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
724         if (ssid == NULL)
725                 return -1;
726         ssid->temporary = 1;
727         ssid->p2p_group = p2p_group;
728         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
729                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
730                 if (ssid->ssid) {
731                         ssid->ssid_len = wpa_s->go_params->ssid_len;
732                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
733                                   ssid->ssid_len);
734                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
735                                           "SSID", ssid->ssid, ssid->ssid_len);
736                 }
737         }
738         if (pin)
739                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
740                             pin, dev_pw_id);
741         else {
742                 rpin = wps_generate_pin();
743                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
744                             rpin, dev_pw_id);
745         }
746         wpa_config_set(ssid, "phase1", val, 0);
747         if (wpa_s->wps_fragment_size)
748                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
749         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
750                                wpa_s, NULL);
751         wpas_wps_reassoc(wpa_s, ssid);
752         return rpin;
753 }
754
755
756 #ifdef CONFIG_WPS_OOB
757 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
758                        char *path, char *method, char *name)
759 {
760         struct wps_context *wps = wpa_s->wps;
761         struct oob_device_data *oob_dev;
762
763         oob_dev = wps_get_oob_device(device_type);
764         if (oob_dev == NULL)
765                 return -1;
766         oob_dev->device_path = path;
767         oob_dev->device_name = name;
768         wps->oob_conf.oob_method = wps_get_oob_method(method);
769
770         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
771                 /*
772                  * Use pre-configured DH keys in order to be able to write the
773                  * key hash into the OOB file.
774                  */
775                 wpabuf_free(wps->dh_pubkey);
776                 wpabuf_free(wps->dh_privkey);
777                 wps->dh_privkey = NULL;
778                 wps->dh_pubkey = NULL;
779                 dh5_free(wps->dh_ctx);
780                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
781                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
782                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
783                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
784                                    "Diffie-Hellman handshake");
785                         return -1;
786                 }
787         }
788
789         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
790                 wpas_clear_wps(wpa_s);
791
792         if (wps_process_oob(wps, oob_dev, 0) < 0)
793                 return -1;
794
795         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
796              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
797             wpas_wps_start_pin(wpa_s, NULL,
798                                wpabuf_head(wps->oob_conf.dev_password), 0) < 0)
799                         return -1;
800
801         return 0;
802 }
803 #endif /* CONFIG_WPS_OOB */
804
805
806 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
807                        const char *pin, struct wps_new_ap_settings *settings)
808 {
809         struct wpa_ssid *ssid;
810         char val[200];
811         char *pos, *end;
812         int res;
813
814         if (!pin)
815                 return -1;
816         wpas_clear_wps(wpa_s);
817         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
818         if (ssid == NULL)
819                 return -1;
820         ssid->temporary = 1;
821         pos = val;
822         end = pos + sizeof(val);
823         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
824         if (res < 0 || res >= end - pos)
825                 return -1;
826         pos += res;
827         if (settings) {
828                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
829                                   "new_encr=%s new_key=%s",
830                                   settings->ssid_hex, settings->auth,
831                                   settings->encr, settings->key_hex);
832                 if (res < 0 || res >= end - pos)
833                         return -1;
834                 pos += res;
835         }
836         res = os_snprintf(pos, end - pos, "\"");
837         if (res < 0 || res >= end - pos)
838                 return -1;
839         wpa_config_set(ssid, "phase1", val, 0);
840         if (wpa_s->wps_fragment_size)
841                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
842         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
843                                wpa_s, NULL);
844         wpas_wps_reassoc(wpa_s, ssid);
845         return 0;
846 }
847
848
849 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
850                                size_t psk_len)
851 {
852         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
853                    "STA " MACSTR, MAC2STR(mac_addr));
854         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
855
856         /* TODO */
857
858         return 0;
859 }
860
861
862 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
863                                    const struct wps_device_data *dev)
864 {
865         char uuid[40], txt[400];
866         int len;
867         char devtype[WPS_DEV_TYPE_BUFSIZE];
868         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
869                 return;
870         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
871         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
872                           " [%s|%s|%s|%s|%s|%s]",
873                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
874                           dev->manufacturer, dev->model_name,
875                           dev->model_number, dev->serial_number,
876                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
877                                                sizeof(devtype)));
878         if (len > 0 && len < (int) sizeof(txt))
879                 wpa_printf(MSG_INFO, "%s", txt);
880 }
881
882
883 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
884                                     u16 sel_reg_config_methods)
885 {
886 #ifdef CONFIG_WPS_ER
887         struct wpa_supplicant *wpa_s = ctx;
888
889         if (wpa_s->wps_er == NULL)
890                 return;
891         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
892                    "dev_password_id=%u sel_reg_config_methods=0x%x",
893                    sel_reg, dev_passwd_id, sel_reg_config_methods);
894         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
895                            sel_reg_config_methods);
896 #endif /* CONFIG_WPS_ER */
897 }
898
899
900 int wpas_wps_init(struct wpa_supplicant *wpa_s)
901 {
902         struct wps_context *wps;
903         struct wps_registrar_config rcfg;
904
905         wps = os_zalloc(sizeof(*wps));
906         if (wps == NULL)
907                 return -1;
908
909         wps->cred_cb = wpa_supplicant_wps_cred;
910         wps->event_cb = wpa_supplicant_wps_event;
911         wps->cb_ctx = wpa_s;
912
913         wps->dev.device_name = wpa_s->conf->device_name;
914         wps->dev.manufacturer = wpa_s->conf->manufacturer;
915         wps->dev.model_name = wpa_s->conf->model_name;
916         wps->dev.model_number = wpa_s->conf->model_number;
917         wps->dev.serial_number = wpa_s->conf->serial_number;
918         wps->config_methods =
919                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
920         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
921             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
922                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
923                            "methods are not allowed at the same time");
924                 os_free(wps);
925                 return -1;
926         }
927         if (wpa_s->conf->device_type &&
928             wps_dev_type_str2bin(wpa_s->conf->device_type,
929                                  wps->dev.pri_dev_type) < 0) {
930                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
931                 os_free(wps);
932                 return -1;
933         }
934         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
935         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
936         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
937         if (is_nil_uuid(wpa_s->conf->uuid)) {
938                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
939                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
940                             wps->uuid, WPS_UUID_LEN);
941         } else
942                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
943
944         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
945         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
946
947         os_memset(&rcfg, 0, sizeof(rcfg));
948         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
949         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
950         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
951         rcfg.cb_ctx = wpa_s;
952
953         wps->registrar = wps_registrar_init(wps, &rcfg);
954         if (wps->registrar == NULL) {
955                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
956                 os_free(wps);
957                 return -1;
958         }
959
960         wpa_s->wps = wps;
961
962         return 0;
963 }
964
965
966 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
967 {
968         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
969
970         if (wpa_s->wps == NULL)
971                 return;
972
973 #ifdef CONFIG_WPS_ER
974         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
975         wpa_s->wps_er = NULL;
976 #endif /* CONFIG_WPS_ER */
977
978         wps_registrar_deinit(wpa_s->wps->registrar);
979         wpabuf_free(wpa_s->wps->dh_pubkey);
980         wpabuf_free(wpa_s->wps->dh_privkey);
981         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
982         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
983         os_free(wpa_s->wps->network_key);
984         os_free(wpa_s->wps);
985         wpa_s->wps = NULL;
986 }
987
988
989 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
990                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
991 {
992         struct wpabuf *wps_ie;
993
994         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
995                 return -1;
996
997         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
998         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
999                 if (!wps_ie) {
1000                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1001                         return 0;
1002                 }
1003
1004                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1005                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1006                                    "without active PBC Registrar");
1007                         wpabuf_free(wps_ie);
1008                         return 0;
1009                 }
1010
1011                 /* TODO: overlap detection */
1012                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1013                            "(Active PBC)");
1014                 wpabuf_free(wps_ie);
1015                 return 1;
1016         }
1017
1018         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1019                 if (!wps_ie) {
1020                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1021                         return 0;
1022                 }
1023
1024                 /*
1025                  * Start with WPS APs that advertise our address as an
1026                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1027                  * allow any WPS AP after couple of scans since some APs do not
1028                  * set Selected Registrar attribute properly when using
1029                  * external Registrar.
1030                  */
1031                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1032                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1033                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1034                                            "without active PIN Registrar");
1035                                 wpabuf_free(wps_ie);
1036                                 return 0;
1037                         }
1038                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1039                 } else {
1040                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1041                                    "(Authorized MAC or Active PIN)");
1042                 }
1043                 wpabuf_free(wps_ie);
1044                 return 1;
1045         }
1046
1047         if (wps_ie) {
1048                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1049                 wpabuf_free(wps_ie);
1050                 return 1;
1051         }
1052
1053         return -1;
1054 }
1055
1056
1057 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1058                               struct wpa_ssid *ssid,
1059                               struct wpa_scan_res *bss)
1060 {
1061         struct wpabuf *wps_ie = NULL;
1062         int ret = 0;
1063
1064         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1065                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1066                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1067                         /* allow wildcard SSID for WPS PBC */
1068                         ret = 1;
1069                 }
1070         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1071                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1072                 if (wps_ie &&
1073                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1074                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1075                         /* allow wildcard SSID for WPS PIN */
1076                         ret = 1;
1077                 }
1078         }
1079
1080         if (!ret && ssid->bssid_set &&
1081             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1082                 /* allow wildcard SSID due to hardcoded BSSID match */
1083                 ret = 1;
1084         }
1085
1086 #ifdef CONFIG_WPS_STRICT
1087         if (wps_ie) {
1088                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1089                                                    0) < 0)
1090                         ret = 0;
1091                 if (bss->beacon_ie_len) {
1092                         struct wpabuf *bcn_wps;
1093                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1094                                 bss, WPS_IE_VENDOR_TYPE);
1095                         if (bcn_wps == NULL) {
1096                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1097                                            "missing from AP Beacon");
1098                                 ret = 0;
1099                         } else {
1100                                 if (wps_validate_beacon(wps_ie) < 0)
1101                                         ret = 0;
1102                                 wpabuf_free(bcn_wps);
1103                         }
1104                 }
1105         }
1106 #endif /* CONFIG_WPS_STRICT */
1107
1108         wpabuf_free(wps_ie);
1109
1110         return ret;
1111 }
1112
1113
1114 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1115                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1116 {
1117         const u8 *sel_uuid, *uuid;
1118         struct wpabuf *wps_ie;
1119         int ret = 0;
1120         struct wpa_bss *bss;
1121
1122         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1123                 return 0;
1124
1125         /* Make sure that only one AP is in active PBC mode */
1126         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1127         if (wps_ie)
1128                 sel_uuid = wps_get_uuid_e(wps_ie);
1129         else
1130                 sel_uuid = NULL;
1131
1132         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1133                 struct wpabuf *ie;
1134                 if (bss == selected)
1135                         continue;
1136                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1137                 if (!ie)
1138                         continue;
1139                 if (!wps_is_selected_pbc_registrar(ie)) {
1140                         wpabuf_free(ie);
1141                         continue;
1142                 }
1143                 uuid = wps_get_uuid_e(ie);
1144                 if (sel_uuid == NULL || uuid == NULL ||
1145                     os_memcmp(sel_uuid, uuid, 16) != 0) {
1146                         ret = 1; /* PBC overlap */
1147                         wpabuf_free(ie);
1148                         break;
1149                 }
1150
1151                 /* TODO: verify that this is reasonable dual-band situation */
1152
1153                 wpabuf_free(ie);
1154         }
1155
1156         wpabuf_free(wps_ie);
1157
1158         return ret;
1159 }
1160
1161
1162 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1163 {
1164         struct wpa_bss *bss;
1165
1166         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1167                 return;
1168
1169         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1170                 struct wpabuf *ie;
1171                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1172                 if (!ie)
1173                         continue;
1174                 if (wps_is_selected_pbc_registrar(ie))
1175                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1176                                      WPS_EVENT_AP_AVAILABLE_PBC);
1177                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1178                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1179                                      WPS_EVENT_AP_AVAILABLE_AUTH);
1180                 else if (wps_is_selected_pin_registrar(ie))
1181                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1182                                      WPS_EVENT_AP_AVAILABLE_PIN);
1183                 else
1184                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1185                                      WPS_EVENT_AP_AVAILABLE);
1186                 wpabuf_free(ie);
1187                 break;
1188         }
1189 }
1190
1191
1192 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1193 {
1194         struct wpa_ssid *ssid;
1195
1196         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1197                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1198                         return 1;
1199         }
1200
1201         return 0;
1202 }
1203
1204
1205 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1206                               char *end)
1207 {
1208         struct wpabuf *wps_ie;
1209         int ret;
1210
1211         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1212         if (wps_ie == NULL)
1213                 return 0;
1214
1215         ret = wps_attr_text(wps_ie, buf, end);
1216         wpabuf_free(wps_ie);
1217         return ret;
1218 }
1219
1220
1221 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1222 {
1223 #ifdef CONFIG_WPS_ER
1224         if (wpa_s->wps_er) {
1225                 wps_er_refresh(wpa_s->wps_er);
1226                 return 0;
1227         }
1228         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1229         if (wpa_s->wps_er == NULL)
1230                 return -1;
1231         return 0;
1232 #else /* CONFIG_WPS_ER */
1233         return 0;
1234 #endif /* CONFIG_WPS_ER */
1235 }
1236
1237
1238 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1239 {
1240 #ifdef CONFIG_WPS_ER
1241         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1242         wpa_s->wps_er = NULL;
1243 #endif /* CONFIG_WPS_ER */
1244         return 0;
1245 }
1246
1247
1248 #ifdef CONFIG_WPS_ER
1249 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1250                         const char *uuid, const char *pin)
1251 {
1252         u8 u[UUID_LEN];
1253         int any = 0;
1254
1255         if (os_strcmp(uuid, "any") == 0)
1256                 any = 1;
1257         else if (uuid_str2bin(uuid, u))
1258                 return -1;
1259         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1260                                      any ? NULL : u,
1261                                      (const u8 *) pin, os_strlen(pin), 300);
1262 }
1263
1264
1265 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1266 {
1267         u8 u[UUID_LEN];
1268
1269         if (uuid_str2bin(uuid, u))
1270                 return -1;
1271         return wps_er_pbc(wpa_s->wps_er, u);
1272 }
1273
1274
1275 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1276                       const char *pin)
1277 {
1278         u8 u[UUID_LEN];
1279
1280         if (uuid_str2bin(uuid, u))
1281                 return -1;
1282         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1283                             os_strlen(pin));
1284 }
1285
1286
1287 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1288                        const char *pin, struct wps_new_ap_settings *settings)
1289 {
1290         u8 u[UUID_LEN];
1291         struct wps_credential cred;
1292         size_t len;
1293
1294         if (uuid_str2bin(uuid, u))
1295                 return -1;
1296         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1297             settings->encr == NULL || settings->key_hex == NULL)
1298                 return -1;
1299
1300         os_memset(&cred, 0, sizeof(cred));
1301         len = os_strlen(settings->ssid_hex);
1302         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1303             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1304                 return -1;
1305         cred.ssid_len = len / 2;
1306
1307         len = os_strlen(settings->key_hex);
1308         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1309             hexstr2bin(settings->key_hex, cred.key, len / 2))
1310                 return -1;
1311         cred.key_len = len / 2;
1312
1313         if (os_strcmp(settings->auth, "OPEN") == 0)
1314                 cred.auth_type = WPS_AUTH_OPEN;
1315         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1316                 cred.auth_type = WPS_AUTH_WPAPSK;
1317         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1318                 cred.auth_type = WPS_AUTH_WPA2PSK;
1319         else
1320                 return -1;
1321
1322         if (os_strcmp(settings->encr, "NONE") == 0)
1323                 cred.encr_type = WPS_ENCR_NONE;
1324         else if (os_strcmp(settings->encr, "WEP") == 0)
1325                 cred.encr_type = WPS_ENCR_WEP;
1326         else if (os_strcmp(settings->encr, "TKIP") == 0)
1327                 cred.encr_type = WPS_ENCR_TKIP;
1328         else if (os_strcmp(settings->encr, "CCMP") == 0)
1329                 cred.encr_type = WPS_ENCR_AES;
1330         else
1331                 return -1;
1332
1333         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1334                              os_strlen(pin), &cred);
1335 }
1336
1337
1338 static void wpas_wps_terminate_cb(void *ctx)
1339 {
1340         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1341         eloop_terminate();
1342 }
1343 #endif /* CONFIG_WPS_ER */
1344
1345
1346 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1347 {
1348 #ifdef CONFIG_WPS_ER
1349         if (wpa_s->wps_er) {
1350                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1351                 wpa_s->wps_er = NULL;
1352                 return 1;
1353         }
1354 #endif /* CONFIG_WPS_ER */
1355         return 0;
1356 }
1357
1358
1359 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1360 {
1361         struct wpa_ssid *ssid;
1362
1363         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1364                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1365                         return 1;
1366         }
1367
1368         return 0;
1369 }
1370
1371
1372 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1373 {
1374         struct wps_context *wps = wpa_s->wps;
1375
1376         if (wps == NULL)
1377                 return;
1378
1379         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1380                 wps->config_methods = wps_config_methods_str2bin(
1381                         wpa_s->conf->config_methods);
1382                 if ((wps->config_methods &
1383                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1384                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1385                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1386                                    "config methods are not allowed at the "
1387                                    "same time");
1388                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1389                 }
1390         }
1391
1392         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1393                 if (wpa_s->conf->device_type &&
1394                     wps_dev_type_str2bin(wpa_s->conf->device_type,
1395                                          wps->dev.pri_dev_type) < 0)
1396                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1397         }
1398
1399         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1400                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1401
1402         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1403                 if (is_nil_uuid(wpa_s->conf->uuid)) {
1404                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1405                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1406                                     "address", wps->uuid, WPS_UUID_LEN);
1407                 } else
1408                         os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1409         }
1410
1411         if (wpa_s->conf->changed_parameters &
1412             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1413                 /* Update pointers to make sure they refer current values */
1414                 wps->dev.device_name = wpa_s->conf->device_name;
1415                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1416                 wps->dev.model_name = wpa_s->conf->model_name;
1417                 wps->dev.model_number = wpa_s->conf->model_number;
1418                 wps->dev.serial_number = wpa_s->conf->serial_number;
1419         }
1420 }