WPS: Enable WSC 2.0 support unconditionally
[mech_eap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "uuid.h"
14 #include "crypto/random.h"
15 #include "crypto/dh_group5.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/wpa_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eap_peer/eap.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wps/wps_attr_parse.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "notify.h"
29 #include "blacklist.h"
30 #include "bss.h"
31 #include "scan.h"
32 #include "ap.h"
33 #include "p2p/p2p.h"
34 #include "p2p_supplicant.h"
35 #include "wps_supplicant.h"
36
37
38 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41
42 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
44
45
46 static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
47 {
48         os_free(wpa_s->wps_ap);
49         wpa_s->wps_ap = NULL;
50         wpa_s->num_wps_ap = 0;
51         wpa_s->wps_ap_iter = 0;
52 }
53
54
55 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
56 {
57 #ifdef CONFIG_P2P
58         if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
59                 return 1;
60 #endif /* CONFIG_P2P */
61
62         if (!wpa_s->wps_success &&
63             wpa_s->current_ssid &&
64             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
65                 const u8 *bssid = wpa_s->bssid;
66                 if (is_zero_ether_addr(bssid))
67                         bssid = wpa_s->pending_bssid;
68
69                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
70                            " did not succeed - continue trying to find "
71                            "suitable AP", MAC2STR(bssid));
72                 wpa_blacklist_add(wpa_s, bssid);
73
74                 wpa_supplicant_deauthenticate(wpa_s,
75                                               WLAN_REASON_DEAUTH_LEAVING);
76                 wpa_s->reassociate = 1;
77                 wpa_supplicant_req_scan(wpa_s,
78                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
79                 wpa_s->blacklist_cleared = 0;
80                 return 1;
81         }
82
83         wpas_wps_clear_ap_info(wpa_s);
84         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
85         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
86                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
87
88         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
89             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
90                 int disabled = wpa_s->current_ssid->disabled;
91                 unsigned int freq = wpa_s->assoc_freq;
92                 struct wpa_bss *bss;
93                 struct wpa_ssid *ssid = NULL;
94                 int use_fast_assoc = 0;
95
96                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
97                            "try to associate with the received credential "
98                            "(freq=%u)", freq);
99                 wpa_supplicant_deauthenticate(wpa_s,
100                                               WLAN_REASON_DEAUTH_LEAVING);
101                 if (disabled) {
102                         wpa_printf(MSG_DEBUG, "WPS: Current network is "
103                                    "disabled - wait for user to enable");
104                         return 1;
105                 }
106                 wpa_s->after_wps = 5;
107                 wpa_s->wps_freq = freq;
108                 wpa_s->normal_scans = 0;
109                 wpa_s->reassociate = 1;
110
111                 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
112                            "without a new scan can be used");
113                 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
114                 if (bss) {
115                         struct wpabuf *wps;
116                         struct wps_parse_attr attr;
117
118                         wps = wpa_bss_get_vendor_ie_multi(bss,
119                                                           WPS_IE_VENDOR_TYPE);
120                         if (wps && wps_parse_msg(wps, &attr) == 0 &&
121                             attr.wps_state &&
122                             *attr.wps_state == WPS_STATE_CONFIGURED)
123                                 use_fast_assoc = 1;
124                         wpabuf_free(wps);
125                 }
126
127                 if (!use_fast_assoc ||
128                     wpa_supplicant_fast_associate(wpa_s) != 1)
129                         wpa_supplicant_req_scan(wpa_s, 0, 0);
130                 return 1;
131         }
132
133         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
134                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
135                            "for external credential processing");
136                 wpas_clear_wps(wpa_s);
137                 wpa_supplicant_deauthenticate(wpa_s,
138                                               WLAN_REASON_DEAUTH_LEAVING);
139                 return 1;
140         }
141
142         return 0;
143 }
144
145
146 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
147                                          struct wpa_ssid *ssid,
148                                          const struct wps_credential *cred)
149 {
150         struct wpa_driver_capa capa;
151         struct wpa_bss *bss;
152         const u8 *ie;
153         struct wpa_ie_data adv;
154         int wpa2 = 0, ccmp = 0;
155
156         /*
157          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
158          * case they are configured for mixed mode operation (WPA+WPA2 and
159          * TKIP+CCMP). Try to use scan results to figure out whether the AP
160          * actually supports stronger security and select that if the client
161          * has support for it, too.
162          */
163
164         if (wpa_drv_get_capa(wpa_s, &capa))
165                 return; /* Unknown what driver supports */
166
167         if (ssid->ssid == NULL)
168                 return;
169         bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
170         if (bss == NULL) {
171                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
172                            "table - use credential as-is");
173                 return;
174         }
175
176         wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
177
178         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
179         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
180                 wpa2 = 1;
181                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
182                         ccmp = 1;
183         } else {
184                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
185                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
186                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
187                         ccmp = 1;
188         }
189
190         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
191             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
192                 /*
193                  * TODO: This could be the initial AP configuration and the
194                  * Beacon contents could change shortly. Should request a new
195                  * scan and delay addition of the network until the updated
196                  * scan results are available.
197                  */
198                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
199                            "support - use credential as-is");
200                 return;
201         }
202
203         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
204             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
205             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
206                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
207                            "based on scan results");
208                 if (wpa_s->conf->ap_scan == 1)
209                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
210                 else
211                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
212         }
213
214         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
215             (ssid->proto & WPA_PROTO_WPA) &&
216             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
217                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
218                            "based on scan results");
219                 if (wpa_s->conf->ap_scan == 1)
220                         ssid->proto |= WPA_PROTO_RSN;
221                 else
222                         ssid->proto = WPA_PROTO_RSN;
223         }
224 }
225
226
227 static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
228                                         struct wpa_ssid *new_ssid)
229 {
230         struct wpa_ssid *ssid, *next;
231
232         for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
233              ssid = next, next = ssid ? ssid->next : NULL) {
234                 /*
235                  * new_ssid has already been added to the list in
236                  * wpas_wps_add_network(), so skip it.
237                  */
238                 if (ssid == new_ssid)
239                         continue;
240
241                 if (ssid->bssid_set || new_ssid->bssid_set) {
242                         if (ssid->bssid_set != new_ssid->bssid_set)
243                                 continue;
244                         if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
245                             0)
246                                 continue;
247                 }
248
249                 /* compare SSID */
250                 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
251                         continue;
252
253                 if (ssid->ssid && new_ssid->ssid) {
254                         if (os_memcmp(ssid->ssid, new_ssid->ssid,
255                                       ssid->ssid_len) != 0)
256                                 continue;
257                 } else if (ssid->ssid || new_ssid->ssid)
258                         continue;
259
260                 /* compare security parameters */
261                 if (ssid->auth_alg != new_ssid->auth_alg ||
262                     ssid->key_mgmt != new_ssid->key_mgmt ||
263                     ssid->proto != new_ssid->proto ||
264                     ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
265                     ssid->group_cipher != new_ssid->group_cipher)
266                         continue;
267
268                 /* Remove the duplicated older network entry. */
269                 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
270                 wpas_notify_network_removed(wpa_s, ssid);
271                 wpa_config_remove_network(wpa_s->conf, ssid->id);
272         }
273 }
274
275
276 static int wpa_supplicant_wps_cred(void *ctx,
277                                    const struct wps_credential *cred)
278 {
279         struct wpa_supplicant *wpa_s = ctx;
280         struct wpa_ssid *ssid = wpa_s->current_ssid;
281         u8 key_idx = 0;
282         u16 auth_type;
283 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
284         int registrar = 0;
285 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
286
287         if ((wpa_s->conf->wps_cred_processing == 1 ||
288              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
289                 size_t blen = cred->cred_attr_len * 2 + 1;
290                 char *buf = os_malloc(blen);
291                 if (buf) {
292                         wpa_snprintf_hex(buf, blen,
293                                          cred->cred_attr, cred->cred_attr_len);
294                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
295                                 WPS_EVENT_CRED_RECEIVED, buf);
296                         os_free(buf);
297                 }
298
299                 wpas_notify_wps_credential(wpa_s, cred);
300         } else
301                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
302
303         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
304                         cred->cred_attr, cred->cred_attr_len);
305
306         if (wpa_s->conf->wps_cred_processing == 1)
307                 return 0;
308
309         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
310         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
311                    cred->auth_type);
312         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
313         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
314         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
315                         cred->key, cred->key_len);
316         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
317                    MAC2STR(cred->mac_addr));
318
319         auth_type = cred->auth_type;
320         if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
321                 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
322                            "auth_type into WPA2PSK");
323                 auth_type = WPS_AUTH_WPA2PSK;
324         }
325
326         if (auth_type != WPS_AUTH_OPEN &&
327             auth_type != WPS_AUTH_SHARED &&
328             auth_type != WPS_AUTH_WPAPSK &&
329             auth_type != WPS_AUTH_WPA2PSK) {
330                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
331                            "unsupported authentication type 0x%x",
332                            auth_type);
333                 return 0;
334         }
335
336         if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
337                 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
338                         wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
339                                    "invalid Network Key length %lu",
340                                    (unsigned long) cred->key_len);
341                         return -1;
342                 }
343         }
344
345         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
346                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
347                            "on the received credential");
348 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
349                 if (ssid->eap.identity &&
350                     ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
351                     os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
352                               WSC_ID_REGISTRAR_LEN) == 0)
353                         registrar = 1;
354 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
355                 os_free(ssid->eap.identity);
356                 ssid->eap.identity = NULL;
357                 ssid->eap.identity_len = 0;
358                 os_free(ssid->eap.phase1);
359                 ssid->eap.phase1 = NULL;
360                 os_free(ssid->eap.eap_methods);
361                 ssid->eap.eap_methods = NULL;
362                 if (!ssid->p2p_group) {
363                         ssid->temporary = 0;
364                         ssid->bssid_set = 0;
365                 }
366                 ssid->disabled_until.sec = 0;
367                 ssid->disabled_until.usec = 0;
368                 ssid->auth_failures = 0;
369         } else {
370                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
371                            "received credential");
372                 ssid = wpa_config_add_network(wpa_s->conf);
373                 if (ssid == NULL)
374                         return -1;
375                 wpas_notify_network_added(wpa_s, ssid);
376         }
377
378         wpa_config_set_network_defaults(ssid);
379
380         os_free(ssid->ssid);
381         ssid->ssid = os_malloc(cred->ssid_len);
382         if (ssid->ssid) {
383                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
384                 ssid->ssid_len = cred->ssid_len;
385         }
386
387         switch (cred->encr_type) {
388         case WPS_ENCR_NONE:
389                 break;
390         case WPS_ENCR_WEP:
391                 if (cred->key_len <= 0)
392                         break;
393                 if (cred->key_len != 5 && cred->key_len != 13 &&
394                     cred->key_len != 10 && cred->key_len != 26) {
395                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
396                                    "%lu", (unsigned long) cred->key_len);
397                         return -1;
398                 }
399                 if (cred->key_idx > NUM_WEP_KEYS) {
400                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
401                                    cred->key_idx);
402                         return -1;
403                 }
404                 if (cred->key_idx)
405                         key_idx = cred->key_idx - 1;
406                 if (cred->key_len == 10 || cred->key_len == 26) {
407                         if (hexstr2bin((char *) cred->key,
408                                        ssid->wep_key[key_idx],
409                                        cred->key_len / 2) < 0) {
410                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
411                                            "%d", key_idx);
412                                 return -1;
413                         }
414                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
415                 } else {
416                         os_memcpy(ssid->wep_key[key_idx], cred->key,
417                                   cred->key_len);
418                         ssid->wep_key_len[key_idx] = cred->key_len;
419                 }
420                 ssid->wep_tx_keyidx = key_idx;
421                 break;
422         case WPS_ENCR_TKIP:
423                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
424                 break;
425         case WPS_ENCR_AES:
426                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
427                 break;
428         }
429
430         switch (auth_type) {
431         case WPS_AUTH_OPEN:
432                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
433                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
434                 ssid->proto = 0;
435 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
436                 if (registrar) {
437                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
438                                 "id=%d - Credentials for an open "
439                                 "network disabled by default - use "
440                                 "'select_network %d' to enable",
441                                 ssid->id, ssid->id);
442                         ssid->disabled = 1;
443                 }
444 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
445                 break;
446         case WPS_AUTH_SHARED:
447                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
448                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
449                 ssid->proto = 0;
450                 break;
451         case WPS_AUTH_WPAPSK:
452                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
453                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
454                 ssid->proto = WPA_PROTO_WPA;
455                 break;
456         case WPS_AUTH_WPA2PSK:
457                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
458                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
459                 ssid->proto = WPA_PROTO_RSN;
460                 break;
461         }
462
463         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
464                 if (cred->key_len == 2 * PMK_LEN) {
465                         if (hexstr2bin((const char *) cred->key, ssid->psk,
466                                        PMK_LEN)) {
467                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
468                                            "Key");
469                                 return -1;
470                         }
471                         ssid->psk_set = 1;
472                         ssid->export_keys = 1;
473                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
474                         os_free(ssid->passphrase);
475                         ssid->passphrase = os_malloc(cred->key_len + 1);
476                         if (ssid->passphrase == NULL)
477                                 return -1;
478                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
479                         ssid->passphrase[cred->key_len] = '\0';
480                         wpa_config_update_psk(ssid);
481                         ssid->export_keys = 1;
482                 } else {
483                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
484                                    "length %lu",
485                                    (unsigned long) cred->key_len);
486                         return -1;
487                 }
488         }
489
490         wpas_wps_security_workaround(wpa_s, ssid, cred);
491
492         wpas_wps_remove_dup_network(wpa_s, ssid);
493
494 #ifndef CONFIG_NO_CONFIG_WRITE
495         if (wpa_s->conf->update_config &&
496             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
497                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
498                 return -1;
499         }
500 #endif /* CONFIG_NO_CONFIG_WRITE */
501
502         /*
503          * Optimize the post-WPS scan based on the channel used during
504          * the provisioning in case EAP-Failure is not received.
505          */
506         wpa_s->after_wps = 5;
507         wpa_s->wps_freq = wpa_s->assoc_freq;
508
509         return 0;
510 }
511
512
513 #ifdef CONFIG_P2P
514 static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
515 {
516         struct wpa_supplicant *wpa_s = eloop_ctx;
517         wpas_p2p_notif_pbc_overlap(wpa_s);
518 }
519 #endif /* CONFIG_P2P */
520
521
522 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
523                                          struct wps_event_m2d *m2d)
524 {
525         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
526                 "dev_password_id=%d config_error=%d",
527                 m2d->dev_password_id, m2d->config_error);
528         wpas_notify_wps_event_m2d(wpa_s, m2d);
529 #ifdef CONFIG_P2P
530         if (wpa_s->parent && wpa_s->parent != wpa_s) {
531                 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
532                         "dev_password_id=%d config_error=%d",
533                         m2d->dev_password_id, m2d->config_error);
534         }
535         if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
536                 /*
537                  * Notify P2P from eloop timeout to avoid issues with the
538                  * interface getting removed while processing a message.
539                  */
540                 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
541                                        NULL);
542         }
543 #endif /* CONFIG_P2P */
544 }
545
546
547 static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
548 {
549         struct wpa_supplicant *wpa_s = eloop_ctx;
550         wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
551         wpas_clear_wps(wpa_s);
552 }
553
554
555 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
556                                           struct wps_event_fail *fail)
557 {
558         if (fail->error_indication > 0 &&
559             fail->error_indication < NUM_WPS_EI_VALUES) {
560                 wpa_msg(wpa_s, MSG_INFO,
561                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
562                         fail->msg, fail->config_error, fail->error_indication,
563                         wps_ei_str(fail->error_indication));
564                 if (wpa_s->parent && wpa_s->parent != wpa_s)
565                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
566                                 "msg=%d config_error=%d reason=%d (%s)",
567                                 fail->msg, fail->config_error,
568                                 fail->error_indication,
569                                 wps_ei_str(fail->error_indication));
570         } else {
571                 wpa_msg(wpa_s, MSG_INFO,
572                         WPS_EVENT_FAIL "msg=%d config_error=%d",
573                         fail->msg, fail->config_error);
574                 if (wpa_s->parent && wpa_s->parent != wpa_s)
575                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
576                                 "msg=%d config_error=%d",
577                                 fail->msg, fail->config_error);
578         }
579
580         /*
581          * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
582          */
583         wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
584         eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
585         eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
586
587         wpas_notify_wps_event_fail(wpa_s, fail);
588 #ifdef CONFIG_P2P
589         wpas_p2p_wps_failed(wpa_s, fail);
590 #endif /* CONFIG_P2P */
591 }
592
593
594 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
595
596 static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
597 {
598         struct wpa_ssid *ssid;
599         int changed = 0;
600
601         eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
602
603         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
604                 if (ssid->disabled_for_connect && ssid->disabled) {
605                         ssid->disabled_for_connect = 0;
606                         ssid->disabled = 0;
607                         wpas_notify_network_enabled_changed(wpa_s, ssid);
608                         changed++;
609                 }
610         }
611
612         if (changed) {
613 #ifndef CONFIG_NO_CONFIG_WRITE
614                 if (wpa_s->conf->update_config &&
615                     wpa_config_write(wpa_s->confname, wpa_s->conf)) {
616                         wpa_printf(MSG_DEBUG, "WPS: Failed to update "
617                                    "configuration");
618                 }
619 #endif /* CONFIG_NO_CONFIG_WRITE */
620         }
621 }
622
623
624 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
625 {
626         struct wpa_supplicant *wpa_s = eloop_ctx;
627         /* Enable the networks disabled during wpas_wps_reassoc */
628         wpas_wps_reenable_networks(wpa_s);
629 }
630
631
632 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
633 {
634         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
635         wpa_s->wps_success = 1;
636         wpas_notify_wps_event_success(wpa_s);
637         if (wpa_s->current_ssid)
638                 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
639         wpa_s->extra_blacklist_count = 0;
640
641         /*
642          * Enable the networks disabled during wpas_wps_reassoc after 10
643          * seconds. The 10 seconds timer is to allow the data connection to be
644          * formed before allowing other networks to be selected.
645          */
646         eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
647                                NULL);
648
649 #ifdef CONFIG_P2P
650         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
651 #endif /* CONFIG_P2P */
652 }
653
654
655 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
656                                                struct wps_event_er_ap *ap)
657 {
658         char uuid_str[100];
659         char dev_type[WPS_DEV_TYPE_BUFSIZE];
660
661         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
662         if (ap->pri_dev_type)
663                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
664                                      sizeof(dev_type));
665         else
666                 dev_type[0] = '\0';
667
668         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
669                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
670                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
671                 ap->friendly_name ? ap->friendly_name : "",
672                 ap->manufacturer ? ap->manufacturer : "",
673                 ap->model_description ? ap->model_description : "",
674                 ap->model_name ? ap->model_name : "",
675                 ap->manufacturer_url ? ap->manufacturer_url : "",
676                 ap->model_url ? ap->model_url : "");
677 }
678
679
680 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
681                                                   struct wps_event_er_ap *ap)
682 {
683         char uuid_str[100];
684         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
685         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
686 }
687
688
689 static void wpa_supplicant_wps_event_er_enrollee_add(
690         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
691 {
692         char uuid_str[100];
693         char dev_type[WPS_DEV_TYPE_BUFSIZE];
694
695         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
696         if (enrollee->pri_dev_type)
697                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
698                                      sizeof(dev_type));
699         else
700                 dev_type[0] = '\0';
701
702         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
703                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
704                 "|%s|%s|%s|%s|%s|",
705                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
706                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
707                 enrollee->dev_name ? enrollee->dev_name : "",
708                 enrollee->manufacturer ? enrollee->manufacturer : "",
709                 enrollee->model_name ? enrollee->model_name : "",
710                 enrollee->model_number ? enrollee->model_number : "",
711                 enrollee->serial_number ? enrollee->serial_number : "");
712 }
713
714
715 static void wpa_supplicant_wps_event_er_enrollee_remove(
716         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
717 {
718         char uuid_str[100];
719         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
720         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
721                 uuid_str, MAC2STR(enrollee->mac_addr));
722 }
723
724
725 static void wpa_supplicant_wps_event_er_ap_settings(
726         struct wpa_supplicant *wpa_s,
727         struct wps_event_er_ap_settings *ap_settings)
728 {
729         char uuid_str[100];
730         char key_str[65];
731         const struct wps_credential *cred = ap_settings->cred;
732
733         key_str[0] = '\0';
734         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
735                 if (cred->key_len >= 8 && cred->key_len <= 64) {
736                         os_memcpy(key_str, cred->key, cred->key_len);
737                         key_str[cred->key_len] = '\0';
738                 }
739         }
740
741         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
742         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
743         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
744                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
745                      "key=%s",
746                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
747                      cred->auth_type, cred->encr_type, key_str);
748 }
749
750
751 static void wpa_supplicant_wps_event_er_set_sel_reg(
752         struct wpa_supplicant *wpa_s,
753         struct wps_event_er_set_selected_registrar *ev)
754 {
755         char uuid_str[100];
756
757         uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
758         switch (ev->state) {
759         case WPS_ER_SET_SEL_REG_START:
760                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
761                         "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
762                         "sel_reg_config_methods=0x%x",
763                         uuid_str, ev->sel_reg, ev->dev_passwd_id,
764                         ev->sel_reg_config_methods);
765                 break;
766         case WPS_ER_SET_SEL_REG_DONE:
767                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
768                         "uuid=%s state=DONE", uuid_str);
769                 break;
770         case WPS_ER_SET_SEL_REG_FAILED:
771                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
772                         "uuid=%s state=FAILED", uuid_str);
773                 break;
774         }
775 }
776
777
778 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
779                                      union wps_event_data *data)
780 {
781         struct wpa_supplicant *wpa_s = ctx;
782         switch (event) {
783         case WPS_EV_M2D:
784                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
785                 break;
786         case WPS_EV_FAIL:
787                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
788                 break;
789         case WPS_EV_SUCCESS:
790                 wpa_supplicant_wps_event_success(wpa_s);
791                 break;
792         case WPS_EV_PWD_AUTH_FAIL:
793 #ifdef CONFIG_AP
794                 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
795                         wpa_supplicant_ap_pwd_auth_fail(wpa_s);
796 #endif /* CONFIG_AP */
797                 break;
798         case WPS_EV_PBC_OVERLAP:
799                 break;
800         case WPS_EV_PBC_TIMEOUT:
801                 break;
802         case WPS_EV_PBC_ACTIVE:
803                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
804                 break;
805         case WPS_EV_PBC_DISABLE:
806                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
807                 break;
808         case WPS_EV_ER_AP_ADD:
809                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
810                 break;
811         case WPS_EV_ER_AP_REMOVE:
812                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
813                 break;
814         case WPS_EV_ER_ENROLLEE_ADD:
815                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
816                                                          &data->enrollee);
817                 break;
818         case WPS_EV_ER_ENROLLEE_REMOVE:
819                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
820                                                             &data->enrollee);
821                 break;
822         case WPS_EV_ER_AP_SETTINGS:
823                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
824                                                         &data->ap_settings);
825                 break;
826         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
827                 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
828                                                         &data->set_sel_reg);
829                 break;
830         case WPS_EV_AP_PIN_SUCCESS:
831                 break;
832         }
833 }
834
835
836 static int wpa_supplicant_wps_rf_band(void *ctx)
837 {
838         struct wpa_supplicant *wpa_s = ctx;
839
840         if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
841                 return 0;
842
843         return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
844 }
845
846
847 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
848 {
849         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
850             eap_is_wps_pin_enrollee(&ssid->eap))
851                 return WPS_REQ_ENROLLEE;
852         else
853                 return WPS_REQ_REGISTRAR;
854 }
855
856
857 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
858 {
859         int id;
860         struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
861
862         wpa_s->after_wps = 0;
863         wpa_s->known_wps_freq = 0;
864
865         prev_current = wpa_s->current_ssid;
866
867         /* Enable the networks disabled during wpas_wps_reassoc */
868         wpas_wps_reenable_networks(wpa_s);
869
870         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
871         eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
872
873         /* Remove any existing WPS network from configuration */
874         ssid = wpa_s->conf->ssid;
875         while (ssid) {
876                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
877                         if (ssid == wpa_s->current_ssid) {
878                                 wpa_supplicant_deauthenticate(
879                                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
880                         }
881                         id = ssid->id;
882                         remove_ssid = ssid;
883                 } else
884                         id = -1;
885                 ssid = ssid->next;
886                 if (id >= 0) {
887                         if (prev_current == remove_ssid) {
888                                 wpa_sm_set_config(wpa_s->wpa, NULL);
889                                 eapol_sm_notify_config(wpa_s->eapol, NULL,
890                                                        NULL);
891                         }
892                         wpas_notify_network_removed(wpa_s, remove_ssid);
893                         wpa_config_remove_network(wpa_s->conf, id);
894                 }
895         }
896
897         wpas_wps_clear_ap_info(wpa_s);
898 }
899
900
901 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
902 {
903         struct wpa_supplicant *wpa_s = eloop_ctx;
904         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
905                 "out");
906         wpas_clear_wps(wpa_s);
907 }
908
909
910 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
911                                               int registrar, const u8 *dev_addr,
912                                               const u8 *bssid)
913 {
914         struct wpa_ssid *ssid;
915
916         ssid = wpa_config_add_network(wpa_s->conf);
917         if (ssid == NULL)
918                 return NULL;
919         wpas_notify_network_added(wpa_s, ssid);
920         wpa_config_set_network_defaults(ssid);
921         ssid->temporary = 1;
922         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
923             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
924             wpa_config_set(ssid, "identity", registrar ?
925                            "\"" WSC_ID_REGISTRAR "\"" :
926                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
927                 wpas_notify_network_removed(wpa_s, ssid);
928                 wpa_config_remove_network(wpa_s->conf, ssid->id);
929                 return NULL;
930         }
931
932 #ifdef CONFIG_P2P
933         if (dev_addr)
934                 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
935 #endif /* CONFIG_P2P */
936
937         if (bssid) {
938 #ifndef CONFIG_P2P
939                 struct wpa_bss *bss;
940                 int count = 0;
941 #endif /* CONFIG_P2P */
942
943                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
944                 ssid->bssid_set = 1;
945
946                 /*
947                  * Note: With P2P, the SSID may change at the time the WPS
948                  * provisioning is started, so better not filter the AP based
949                  * on the current SSID in the scan results.
950                  */
951 #ifndef CONFIG_P2P
952                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
953                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
954                                 continue;
955
956                         os_free(ssid->ssid);
957                         ssid->ssid = os_malloc(bss->ssid_len);
958                         if (ssid->ssid == NULL)
959                                 break;
960                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
961                         ssid->ssid_len = bss->ssid_len;
962                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
963                                           "scan results",
964                                           ssid->ssid, ssid->ssid_len);
965                         count++;
966                 }
967
968                 if (count > 1) {
969                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
970                                    "for the AP; use wildcard");
971                         os_free(ssid->ssid);
972                         ssid->ssid = NULL;
973                         ssid->ssid_len = 0;
974                 }
975 #endif /* CONFIG_P2P */
976         }
977
978         return ssid;
979 }
980
981
982 static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
983                                   struct wpa_ssid *selected)
984 {
985         struct wpa_ssid *ssid;
986
987         if (wpa_s->current_ssid)
988                 wpa_supplicant_deauthenticate(
989                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
990
991         /* Mark all other networks disabled and trigger reassociation */
992         ssid = wpa_s->conf->ssid;
993         while (ssid) {
994                 int was_disabled = ssid->disabled;
995                 ssid->disabled_for_connect = 0;
996                 /*
997                  * In case the network object corresponds to a persistent group
998                  * then do not send out network disabled signal. In addition,
999                  * do not change disabled status of persistent network objects
1000                  * from 2 to 1 should we connect to another network.
1001                  */
1002                 if (was_disabled != 2) {
1003                         ssid->disabled = ssid != selected;
1004                         if (was_disabled != ssid->disabled) {
1005                                 if (ssid->disabled)
1006                                         ssid->disabled_for_connect = 1;
1007                                 wpas_notify_network_enabled_changed(wpa_s,
1008                                                                     ssid);
1009                         }
1010                 }
1011                 ssid = ssid->next;
1012         }
1013 }
1014
1015
1016 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1017                              struct wpa_ssid *selected, const u8 *bssid,
1018                              int freq)
1019 {
1020         struct wpa_bss *bss;
1021
1022         wpa_s->after_wps = 0;
1023         wpa_s->known_wps_freq = 0;
1024         if (freq) {
1025                 wpa_s->after_wps = 5;
1026                 wpa_s->wps_freq = freq;
1027         } else if (bssid) {
1028                 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1029                 if (bss && bss->freq > 0) {
1030                         wpa_s->known_wps_freq = 1;
1031                         wpa_s->wps_freq = bss->freq;
1032                 }
1033         }
1034
1035         wpas_wps_temp_disable(wpa_s, selected);
1036
1037         wpa_s->disconnected = 0;
1038         wpa_s->reassociate = 1;
1039         wpa_s->scan_runs = 0;
1040         wpa_s->normal_scans = 0;
1041         wpa_s->wps_success = 0;
1042         wpa_s->blacklist_cleared = 0;
1043
1044         wpa_supplicant_cancel_sched_scan(wpa_s);
1045         wpa_supplicant_req_scan(wpa_s, 0, 0);
1046 }
1047
1048
1049 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1050                        int p2p_group)
1051 {
1052         struct wpa_ssid *ssid;
1053         wpas_clear_wps(wpa_s);
1054         ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
1055         if (ssid == NULL)
1056                 return -1;
1057         ssid->temporary = 1;
1058         ssid->p2p_group = p2p_group;
1059 #ifdef CONFIG_P2P
1060         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1061                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1062                 if (ssid->ssid) {
1063                         ssid->ssid_len = wpa_s->go_params->ssid_len;
1064                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1065                                   ssid->ssid_len);
1066                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1067                                           "SSID", ssid->ssid, ssid->ssid_len);
1068                 }
1069         }
1070 #endif /* CONFIG_P2P */
1071         if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1072                 return -1;
1073         if (wpa_s->wps_fragment_size)
1074                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1075         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1076                                wpa_s, NULL);
1077         wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1078         return 0;
1079 }
1080
1081
1082 static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1083                                  const u8 *dev_addr, const u8 *bssid,
1084                                  const char *pin, int p2p_group, u16 dev_pw_id,
1085                                  const u8 *peer_pubkey_hash,
1086                                  const u8 *ssid_val, size_t ssid_len, int freq)
1087 {
1088         struct wpa_ssid *ssid;
1089         char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
1090         unsigned int rpin = 0;
1091         char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
1092
1093         wpas_clear_wps(wpa_s);
1094         if (bssid && is_zero_ether_addr(bssid))
1095                 bssid = NULL;
1096         ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1097         if (ssid == NULL) {
1098                 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
1099                 return -1;
1100         }
1101         ssid->temporary = 1;
1102         ssid->p2p_group = p2p_group;
1103         if (ssid_val) {
1104                 ssid->ssid = os_malloc(ssid_len);
1105                 if (ssid->ssid) {
1106                         os_memcpy(ssid->ssid, ssid_val, ssid_len);
1107                         ssid->ssid_len = ssid_len;
1108                 }
1109         }
1110         if (peer_pubkey_hash) {
1111                 os_memcpy(hash, " pkhash=", 8);
1112                 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1113                                            peer_pubkey_hash,
1114                                            WPS_OOB_PUBKEY_HASH_LEN);
1115         } else {
1116                 hash[0] = '\0';
1117         }
1118 #ifdef CONFIG_P2P
1119         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1120                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1121                 if (ssid->ssid) {
1122                         ssid->ssid_len = wpa_s->go_params->ssid_len;
1123                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1124                                   ssid->ssid_len);
1125                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1126                                           "SSID", ssid->ssid, ssid->ssid_len);
1127                 }
1128         }
1129 #endif /* CONFIG_P2P */
1130         if (pin)
1131                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1132                             pin, dev_pw_id, hash);
1133         else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1134                 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1135                             dev_pw_id, hash);
1136         } else {
1137                 rpin = wps_generate_pin();
1138                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1139                             rpin, dev_pw_id, hash);
1140         }
1141         if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1142                 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
1143                 return -1;
1144         }
1145         if (wpa_s->wps_fragment_size)
1146                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1147         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1148                                wpa_s, NULL);
1149         wpa_s->wps_ap_iter = 1;
1150         wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
1151         return rpin;
1152 }
1153
1154
1155 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1156                        const char *pin, int p2p_group, u16 dev_pw_id)
1157 {
1158         return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1159                                      dev_pw_id, NULL, NULL, 0, 0);
1160 }
1161
1162
1163 /* Cancel the wps pbc/pin requests */
1164 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1165 {
1166 #ifdef CONFIG_AP
1167         if (wpa_s->ap_iface) {
1168                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1169                 return wpa_supplicant_ap_wps_cancel(wpa_s);
1170         }
1171 #endif /* CONFIG_AP */
1172
1173         if (wpa_s->wpa_state == WPA_SCANNING ||
1174             wpa_s->wpa_state == WPA_DISCONNECTED) {
1175                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1176                 wpa_supplicant_cancel_scan(wpa_s);
1177                 wpas_clear_wps(wpa_s);
1178         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1179                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1180                            "deauthenticate");
1181                 wpa_supplicant_deauthenticate(wpa_s,
1182                                               WLAN_REASON_DEAUTH_LEAVING);
1183                 wpas_clear_wps(wpa_s);
1184         } else {
1185                 wpas_wps_reenable_networks(wpa_s);
1186                 wpas_wps_clear_ap_info(wpa_s);
1187                 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1188                     0)
1189                         wpas_clear_wps(wpa_s);
1190         }
1191
1192         wpa_s->after_wps = 0;
1193
1194         return 0;
1195 }
1196
1197
1198 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1199                        const char *pin, struct wps_new_ap_settings *settings)
1200 {
1201         struct wpa_ssid *ssid;
1202         char val[200];
1203         char *pos, *end;
1204         int res;
1205
1206         if (!pin)
1207                 return -1;
1208         wpas_clear_wps(wpa_s);
1209         ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
1210         if (ssid == NULL)
1211                 return -1;
1212         ssid->temporary = 1;
1213         pos = val;
1214         end = pos + sizeof(val);
1215         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1216         if (res < 0 || res >= end - pos)
1217                 return -1;
1218         pos += res;
1219         if (settings) {
1220                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1221                                   "new_encr=%s new_key=%s",
1222                                   settings->ssid_hex, settings->auth,
1223                                   settings->encr, settings->key_hex);
1224                 if (res < 0 || res >= end - pos)
1225                         return -1;
1226                 pos += res;
1227         }
1228         res = os_snprintf(pos, end - pos, "\"");
1229         if (res < 0 || res >= end - pos)
1230                 return -1;
1231         if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1232                 return -1;
1233         if (wpa_s->wps_fragment_size)
1234                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1235         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1236                                wpa_s, NULL);
1237         wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1238         return 0;
1239 }
1240
1241
1242 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1243                                const u8 *p2p_dev_addr, const u8 *psk,
1244                                size_t psk_len)
1245 {
1246         if (is_zero_ether_addr(p2p_dev_addr)) {
1247                 wpa_printf(MSG_DEBUG,
1248                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1249                            MAC2STR(mac_addr));
1250         } else {
1251                 wpa_printf(MSG_DEBUG,
1252                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1253                            " P2P Device Addr " MACSTR,
1254                            MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1255         }
1256         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1257
1258         /* TODO */
1259
1260         return 0;
1261 }
1262
1263
1264 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1265                                    const struct wps_device_data *dev)
1266 {
1267         char uuid[40], txt[400];
1268         int len;
1269         char devtype[WPS_DEV_TYPE_BUFSIZE];
1270         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1271                 return;
1272         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1273         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1274                           " [%s|%s|%s|%s|%s|%s]",
1275                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
1276                           dev->manufacturer, dev->model_name,
1277                           dev->model_number, dev->serial_number,
1278                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1279                                                sizeof(devtype)));
1280         if (len > 0 && len < (int) sizeof(txt))
1281                 wpa_printf(MSG_INFO, "%s", txt);
1282 }
1283
1284
1285 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1286                                     u16 sel_reg_config_methods)
1287 {
1288 #ifdef CONFIG_WPS_ER
1289         struct wpa_supplicant *wpa_s = ctx;
1290
1291         if (wpa_s->wps_er == NULL)
1292                 return;
1293         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1294                    "dev_password_id=%u sel_reg_config_methods=0x%x",
1295                    sel_reg, dev_passwd_id, sel_reg_config_methods);
1296         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1297                            sel_reg_config_methods);
1298 #endif /* CONFIG_WPS_ER */
1299 }
1300
1301
1302 static u16 wps_fix_config_methods(u16 config_methods)
1303 {
1304         if ((config_methods &
1305              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1306               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1307                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1308                            "virtual_display for WPS 2.0 compliance");
1309                 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1310         }
1311         if ((config_methods &
1312              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1313               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1314                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1315                            "virtual_push_button for WPS 2.0 compliance");
1316                 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1317         }
1318
1319         return config_methods;
1320 }
1321
1322
1323 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1324                               struct wps_context *wps)
1325 {
1326         char buf[50];
1327         const char *src;
1328
1329         if (is_nil_uuid(wpa_s->conf->uuid)) {
1330                 struct wpa_supplicant *first;
1331                 first = wpa_s->global->ifaces;
1332                 while (first && first->next)
1333                         first = first->next;
1334                 if (first && first != wpa_s) {
1335                         if (wps != wpa_s->global->ifaces->wps)
1336                                 os_memcpy(wps->uuid,
1337                                           wpa_s->global->ifaces->wps->uuid,
1338                                           WPS_UUID_LEN);
1339                         src = "from the first interface";
1340                 } else {
1341                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1342                         src = "based on MAC address";
1343                 }
1344         } else {
1345                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1346                 src = "based on configuration";
1347         }
1348
1349         uuid_bin2str(wps->uuid, buf, sizeof(buf));
1350         wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
1351 }
1352
1353
1354 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1355                                        struct wps_context *wps)
1356 {
1357         wpabuf_free(wps->dev.vendor_ext_m1);
1358         wps->dev.vendor_ext_m1 = NULL;
1359
1360         if (wpa_s->conf->wps_vendor_ext_m1) {
1361                 wps->dev.vendor_ext_m1 =
1362                         wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1363                 if (!wps->dev.vendor_ext_m1) {
1364                         wpa_printf(MSG_ERROR, "WPS: Cannot "
1365                                    "allocate memory for vendor_ext_m1");
1366                 }
1367         }
1368 }
1369
1370
1371 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1372 {
1373         struct wps_context *wps;
1374         struct wps_registrar_config rcfg;
1375         struct hostapd_hw_modes *modes;
1376         u16 m;
1377
1378         wps = os_zalloc(sizeof(*wps));
1379         if (wps == NULL)
1380                 return -1;
1381
1382         wps->cred_cb = wpa_supplicant_wps_cred;
1383         wps->event_cb = wpa_supplicant_wps_event;
1384         wps->rf_band_cb = wpa_supplicant_wps_rf_band;
1385         wps->cb_ctx = wpa_s;
1386
1387         wps->dev.device_name = wpa_s->conf->device_name;
1388         wps->dev.manufacturer = wpa_s->conf->manufacturer;
1389         wps->dev.model_name = wpa_s->conf->model_name;
1390         wps->dev.model_number = wpa_s->conf->model_number;
1391         wps->dev.serial_number = wpa_s->conf->serial_number;
1392         wps->config_methods =
1393                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1394         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1395             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1396                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1397                            "methods are not allowed at the same time");
1398                 os_free(wps);
1399                 return -1;
1400         }
1401         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1402         wps->dev.config_methods = wps->config_methods;
1403         os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1404                   WPS_DEV_TYPE_LEN);
1405
1406         wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1407         os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1408                   WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1409
1410         wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1411
1412         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1413         modes = wpa_s->hw.modes;
1414         if (modes) {
1415                 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1416                         if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1417                             modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1418                                 wps->dev.rf_bands |= WPS_RF_24GHZ;
1419                         else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1420                                 wps->dev.rf_bands |= WPS_RF_50GHZ;
1421                 }
1422         }
1423         if (wps->dev.rf_bands == 0) {
1424                 /*
1425                  * Default to claiming support for both bands if the driver
1426                  * does not provide support for fetching supported bands.
1427                  */
1428                 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1429         }
1430         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1431         wpas_wps_set_uuid(wpa_s, wps);
1432
1433         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1434         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1435
1436         os_memset(&rcfg, 0, sizeof(rcfg));
1437         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1438         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1439         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1440         rcfg.cb_ctx = wpa_s;
1441
1442         wps->registrar = wps_registrar_init(wps, &rcfg);
1443         if (wps->registrar == NULL) {
1444                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1445                 os_free(wps);
1446                 return -1;
1447         }
1448
1449         wpa_s->wps = wps;
1450
1451         return 0;
1452 }
1453
1454
1455 #ifdef CONFIG_WPS_ER
1456 static void wpas_wps_nfc_clear(struct wps_context *wps)
1457 {
1458         wps->ap_nfc_dev_pw_id = 0;
1459         wpabuf_free(wps->ap_nfc_dh_pubkey);
1460         wps->ap_nfc_dh_pubkey = NULL;
1461         wpabuf_free(wps->ap_nfc_dh_privkey);
1462         wps->ap_nfc_dh_privkey = NULL;
1463         wpabuf_free(wps->ap_nfc_dev_pw);
1464         wps->ap_nfc_dev_pw = NULL;
1465 }
1466 #endif /* CONFIG_WPS_ER */
1467
1468
1469 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1470 {
1471         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1472         eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
1473         eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
1474         wpas_wps_clear_ap_info(wpa_s);
1475
1476         if (wpa_s->wps == NULL)
1477                 return;
1478
1479 #ifdef CONFIG_WPS_ER
1480         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1481         wpa_s->wps_er = NULL;
1482         wpas_wps_nfc_clear(wpa_s->wps);
1483 #endif /* CONFIG_WPS_ER */
1484
1485         wps_registrar_deinit(wpa_s->wps->registrar);
1486         wpabuf_free(wpa_s->wps->dh_pubkey);
1487         wpabuf_free(wpa_s->wps->dh_privkey);
1488         wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
1489         os_free(wpa_s->wps->network_key);
1490         os_free(wpa_s->wps);
1491         wpa_s->wps = NULL;
1492 }
1493
1494
1495 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1496                             struct wpa_ssid *ssid, struct wpa_bss *bss)
1497 {
1498         struct wpabuf *wps_ie;
1499
1500         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1501                 return -1;
1502
1503         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1504         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1505                 if (!wps_ie) {
1506                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1507                         return 0;
1508                 }
1509
1510                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1511                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1512                                    "without active PBC Registrar");
1513                         wpabuf_free(wps_ie);
1514                         return 0;
1515                 }
1516
1517                 /* TODO: overlap detection */
1518                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1519                            "(Active PBC)");
1520                 wpabuf_free(wps_ie);
1521                 return 1;
1522         }
1523
1524         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1525                 if (!wps_ie) {
1526                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1527                         return 0;
1528                 }
1529
1530                 /*
1531                  * Start with WPS APs that advertise our address as an
1532                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1533                  * allow any WPS AP after couple of scans since some APs do not
1534                  * set Selected Registrar attribute properly when using
1535                  * external Registrar.
1536                  */
1537                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1538                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1539                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1540                                            "without active PIN Registrar");
1541                                 wpabuf_free(wps_ie);
1542                                 return 0;
1543                         }
1544                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1545                 } else {
1546                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1547                                    "(Authorized MAC or Active PIN)");
1548                 }
1549                 wpabuf_free(wps_ie);
1550                 return 1;
1551         }
1552
1553         if (wps_ie) {
1554                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1555                 wpabuf_free(wps_ie);
1556                 return 1;
1557         }
1558
1559         return -1;
1560 }
1561
1562
1563 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1564                               struct wpa_ssid *ssid,
1565                               struct wpa_bss *bss)
1566 {
1567         struct wpabuf *wps_ie = NULL;
1568         int ret = 0;
1569
1570         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1571                 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1572                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1573                         /* allow wildcard SSID for WPS PBC */
1574                         ret = 1;
1575                 }
1576         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1577                 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1578                 if (wps_ie &&
1579                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1580                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1581                         /* allow wildcard SSID for WPS PIN */
1582                         ret = 1;
1583                 }
1584         }
1585
1586         if (!ret && ssid->bssid_set &&
1587             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1588                 /* allow wildcard SSID due to hardcoded BSSID match */
1589                 ret = 1;
1590         }
1591
1592 #ifdef CONFIG_WPS_STRICT
1593         if (wps_ie) {
1594                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1595                                                    0, bss->bssid) < 0)
1596                         ret = 0;
1597                 if (bss->beacon_ie_len) {
1598                         struct wpabuf *bcn_wps;
1599                         bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
1600                                 bss, WPS_IE_VENDOR_TYPE);
1601                         if (bcn_wps == NULL) {
1602                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1603                                            "missing from AP Beacon");
1604                                 ret = 0;
1605                         } else {
1606                                 if (wps_validate_beacon(wps_ie) < 0)
1607                                         ret = 0;
1608                                 wpabuf_free(bcn_wps);
1609                         }
1610                 }
1611         }
1612 #endif /* CONFIG_WPS_STRICT */
1613
1614         wpabuf_free(wps_ie);
1615
1616         return ret;
1617 }
1618
1619
1620 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1621                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1622 {
1623         const u8 *sel_uuid, *uuid;
1624         struct wpabuf *wps_ie;
1625         int ret = 0;
1626         struct wpa_bss *bss;
1627
1628         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1629                 return 0;
1630
1631         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1632                    "present in scan results; selected BSSID " MACSTR,
1633                    MAC2STR(selected->bssid));
1634
1635         /* Make sure that only one AP is in active PBC mode */
1636         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1637         if (wps_ie) {
1638                 sel_uuid = wps_get_uuid_e(wps_ie);
1639                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1640                             sel_uuid, UUID_LEN);
1641         } else {
1642                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1643                            "WPS IE?!");
1644                 sel_uuid = NULL;
1645         }
1646
1647         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1648                 struct wpabuf *ie;
1649                 if (bss == selected)
1650                         continue;
1651                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1652                 if (!ie)
1653                         continue;
1654                 if (!wps_is_selected_pbc_registrar(ie)) {
1655                         wpabuf_free(ie);
1656                         continue;
1657                 }
1658                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1659                            MACSTR, MAC2STR(bss->bssid));
1660                 uuid = wps_get_uuid_e(ie);
1661                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1662                             uuid, UUID_LEN);
1663                 if (sel_uuid == NULL || uuid == NULL ||
1664                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1665                         ret = 1; /* PBC overlap */
1666                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1667                                 MACSTR " and " MACSTR,
1668                                 MAC2STR(selected->bssid),
1669                                 MAC2STR(bss->bssid));
1670                         wpabuf_free(ie);
1671                         break;
1672                 }
1673
1674                 /* TODO: verify that this is reasonable dual-band situation */
1675
1676                 wpabuf_free(ie);
1677         }
1678
1679         wpabuf_free(wps_ie);
1680
1681         return ret;
1682 }
1683
1684
1685 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1686 {
1687         struct wpa_bss *bss;
1688         unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1689
1690         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1691                 return;
1692
1693         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1694                 struct wpabuf *ie;
1695                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1696                 if (!ie)
1697                         continue;
1698                 if (wps_is_selected_pbc_registrar(ie))
1699                         pbc++;
1700                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1701                         auth++;
1702                 else if (wps_is_selected_pin_registrar(ie))
1703                         pin++;
1704                 else
1705                         wps++;
1706                 wpabuf_free(ie);
1707         }
1708
1709         if (pbc)
1710                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1711         else if (auth)
1712                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1713         else if (pin)
1714                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1715         else if (wps)
1716                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1717 }
1718
1719
1720 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1721 {
1722         struct wpa_ssid *ssid;
1723
1724         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1725                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1726                         return 1;
1727         }
1728
1729         return 0;
1730 }
1731
1732
1733 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1734                               char *end)
1735 {
1736         struct wpabuf *wps_ie;
1737         int ret;
1738
1739         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1740         if (wps_ie == NULL)
1741                 return 0;
1742
1743         ret = wps_attr_text(wps_ie, buf, end);
1744         wpabuf_free(wps_ie);
1745         return ret;
1746 }
1747
1748
1749 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1750 {
1751 #ifdef CONFIG_WPS_ER
1752         if (wpa_s->wps_er) {
1753                 wps_er_refresh(wpa_s->wps_er);
1754                 return 0;
1755         }
1756         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1757         if (wpa_s->wps_er == NULL)
1758                 return -1;
1759         return 0;
1760 #else /* CONFIG_WPS_ER */
1761         return 0;
1762 #endif /* CONFIG_WPS_ER */
1763 }
1764
1765
1766 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1767 {
1768 #ifdef CONFIG_WPS_ER
1769         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1770         wpa_s->wps_er = NULL;
1771 #endif /* CONFIG_WPS_ER */
1772         return 0;
1773 }
1774
1775
1776 #ifdef CONFIG_WPS_ER
1777 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1778                         const char *uuid, const char *pin)
1779 {
1780         u8 u[UUID_LEN];
1781         const u8 *use_uuid = NULL;
1782         u8 addr_buf[ETH_ALEN];
1783
1784         if (os_strcmp(uuid, "any") == 0) {
1785         } else if (uuid_str2bin(uuid, u) == 0) {
1786                 use_uuid = u;
1787         } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1788                 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1789                 if (use_uuid == NULL)
1790                         return -1;
1791         } else
1792                 return -1;
1793         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1794                                      use_uuid,
1795                                      (const u8 *) pin, os_strlen(pin), 300);
1796 }
1797
1798
1799 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1800 {
1801         u8 u[UUID_LEN], *use_uuid = NULL;
1802         u8 addr[ETH_ALEN], *use_addr = NULL;
1803
1804         if (uuid_str2bin(uuid, u) == 0)
1805                 use_uuid = u;
1806         else if (hwaddr_aton(uuid, addr) == 0)
1807                 use_addr = addr;
1808         else
1809                 return -1;
1810         return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
1811 }
1812
1813
1814 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1815                       const char *pin)
1816 {
1817         u8 u[UUID_LEN], *use_uuid = NULL;
1818         u8 addr[ETH_ALEN], *use_addr = NULL;
1819
1820         if (uuid_str2bin(uuid, u) == 0)
1821                 use_uuid = u;
1822         else if (hwaddr_aton(uuid, addr) == 0)
1823                 use_addr = addr;
1824         else
1825                 return -1;
1826
1827         return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
1828                             os_strlen(pin));
1829 }
1830
1831
1832 static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1833                                     struct wps_credential *cred)
1834 {
1835         os_memset(cred, 0, sizeof(*cred));
1836         if (ssid->ssid_len > 32)
1837                 return -1;
1838         os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1839         cred->ssid_len = ssid->ssid_len;
1840         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1841                 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1842                         WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1843                 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1844                         cred->encr_type = WPS_ENCR_AES;
1845                 else
1846                         cred->encr_type = WPS_ENCR_TKIP;
1847                 if (ssid->passphrase) {
1848                         cred->key_len = os_strlen(ssid->passphrase);
1849                         if (cred->key_len >= 64)
1850                                 return -1;
1851                         os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1852                 } else if (ssid->psk_set) {
1853                         cred->key_len = 32;
1854                         os_memcpy(cred->key, ssid->psk, 32);
1855                 } else
1856                         return -1;
1857         } else {
1858                 cred->auth_type = WPS_AUTH_OPEN;
1859                 cred->encr_type = WPS_ENCR_NONE;
1860         }
1861
1862         return 0;
1863 }
1864
1865
1866 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1867                            int id)
1868 {
1869         u8 u[UUID_LEN], *use_uuid = NULL;
1870         u8 addr[ETH_ALEN], *use_addr = NULL;
1871         struct wpa_ssid *ssid;
1872         struct wps_credential cred;
1873
1874         if (uuid_str2bin(uuid, u) == 0)
1875                 use_uuid = u;
1876         else if (hwaddr_aton(uuid, addr) == 0)
1877                 use_addr = addr;
1878         else
1879                 return -1;
1880         ssid = wpa_config_get_network(wpa_s->conf, id);
1881         if (ssid == NULL || ssid->ssid == NULL)
1882                 return -1;
1883
1884         if (wpas_wps_network_to_cred(ssid, &cred) < 0)
1885                 return -1;
1886         return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
1887 }
1888
1889
1890 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1891                        const char *pin, struct wps_new_ap_settings *settings)
1892 {
1893         u8 u[UUID_LEN], *use_uuid = NULL;
1894         u8 addr[ETH_ALEN], *use_addr = NULL;
1895         struct wps_credential cred;
1896         size_t len;
1897
1898         if (uuid_str2bin(uuid, u) == 0)
1899                 use_uuid = u;
1900         else if (hwaddr_aton(uuid, addr) == 0)
1901                 use_addr = addr;
1902         else
1903                 return -1;
1904         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1905             settings->encr == NULL || settings->key_hex == NULL)
1906                 return -1;
1907
1908         os_memset(&cred, 0, sizeof(cred));
1909         len = os_strlen(settings->ssid_hex);
1910         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1911             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1912                 return -1;
1913         cred.ssid_len = len / 2;
1914
1915         len = os_strlen(settings->key_hex);
1916         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1917             hexstr2bin(settings->key_hex, cred.key, len / 2))
1918                 return -1;
1919         cred.key_len = len / 2;
1920
1921         if (os_strcmp(settings->auth, "OPEN") == 0)
1922                 cred.auth_type = WPS_AUTH_OPEN;
1923         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1924                 cred.auth_type = WPS_AUTH_WPAPSK;
1925         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1926                 cred.auth_type = WPS_AUTH_WPA2PSK;
1927         else
1928                 return -1;
1929
1930         if (os_strcmp(settings->encr, "NONE") == 0)
1931                 cred.encr_type = WPS_ENCR_NONE;
1932         else if (os_strcmp(settings->encr, "WEP") == 0)
1933                 cred.encr_type = WPS_ENCR_WEP;
1934         else if (os_strcmp(settings->encr, "TKIP") == 0)
1935                 cred.encr_type = WPS_ENCR_TKIP;
1936         else if (os_strcmp(settings->encr, "CCMP") == 0)
1937                 cred.encr_type = WPS_ENCR_AES;
1938         else
1939                 return -1;
1940
1941         return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1942                              (const u8 *) pin, os_strlen(pin), &cred);
1943 }
1944
1945
1946 #ifdef CONFIG_WPS_NFC
1947 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1948                                              int ndef, const char *uuid)
1949 {
1950         struct wpabuf *ret;
1951         u8 u[UUID_LEN], *use_uuid = NULL;
1952         u8 addr[ETH_ALEN], *use_addr = NULL;
1953
1954         if (!wpa_s->wps_er)
1955                 return NULL;
1956
1957         if (uuid_str2bin(uuid, u) == 0)
1958                 use_uuid = u;
1959         else if (hwaddr_aton(uuid, addr) == 0)
1960                 use_addr = addr;
1961         else
1962                 return NULL;
1963
1964         ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
1965         if (ndef && ret) {
1966                 struct wpabuf *tmp;
1967                 tmp = ndef_build_wifi(ret);
1968                 wpabuf_free(ret);
1969                 if (tmp == NULL)
1970                         return NULL;
1971                 ret = tmp;
1972         }
1973
1974         return ret;
1975 }
1976 #endif /* CONFIG_WPS_NFC */
1977
1978
1979 static int callbacks_pending = 0;
1980
1981 static void wpas_wps_terminate_cb(void *ctx)
1982 {
1983         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1984         if (--callbacks_pending <= 0)
1985                 eloop_terminate();
1986 }
1987 #endif /* CONFIG_WPS_ER */
1988
1989
1990 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1991 {
1992 #ifdef CONFIG_WPS_ER
1993         if (wpa_s->wps_er) {
1994                 callbacks_pending++;
1995                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1996                 wpa_s->wps_er = NULL;
1997                 return 1;
1998         }
1999 #endif /* CONFIG_WPS_ER */
2000         return 0;
2001 }
2002
2003
2004 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2005 {
2006         struct wps_context *wps = wpa_s->wps;
2007
2008         if (wps == NULL)
2009                 return;
2010
2011         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2012                 wps->config_methods = wps_config_methods_str2bin(
2013                         wpa_s->conf->config_methods);
2014                 if ((wps->config_methods &
2015                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2016                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2017                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2018                                    "config methods are not allowed at the "
2019                                    "same time");
2020                         wps->config_methods &= ~WPS_CONFIG_LABEL;
2021                 }
2022         }
2023         wps->config_methods = wps_fix_config_methods(wps->config_methods);
2024         wps->dev.config_methods = wps->config_methods;
2025
2026         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2027                 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2028                           WPS_DEV_TYPE_LEN);
2029
2030         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2031                 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2032                 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2033                           wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2034         }
2035
2036         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2037                 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2038
2039         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2040                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2041
2042         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2043                 wpas_wps_set_uuid(wpa_s, wps);
2044
2045         if (wpa_s->conf->changed_parameters &
2046             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2047                 /* Update pointers to make sure they refer current values */
2048                 wps->dev.device_name = wpa_s->conf->device_name;
2049                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2050                 wps->dev.model_name = wpa_s->conf->model_name;
2051                 wps->dev.model_number = wpa_s->conf->model_number;
2052                 wps->dev.serial_number = wpa_s->conf->serial_number;
2053         }
2054 }
2055
2056
2057 #ifdef CONFIG_WPS_NFC
2058
2059 #ifdef CONFIG_WPS_ER
2060 static struct wpabuf *
2061 wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2062                               struct wpa_ssid *ssid)
2063 {
2064         struct wpabuf *ret;
2065         struct wps_credential cred;
2066
2067         if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2068                 return NULL;
2069
2070         ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2071
2072         if (ndef && ret) {
2073                 struct wpabuf *tmp;
2074                 tmp = ndef_build_wifi(ret);
2075                 wpabuf_free(ret);
2076                 if (tmp == NULL)
2077                         return NULL;
2078                 ret = tmp;
2079         }
2080
2081         return ret;
2082 }
2083 #endif /* CONFIG_WPS_ER */
2084
2085
2086 struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2087                                           int ndef, const char *id_str)
2088 {
2089 #ifdef CONFIG_WPS_ER
2090         if (id_str) {
2091                 int id;
2092                 char *end = NULL;
2093                 struct wpa_ssid *ssid;
2094
2095                 id = strtol(id_str, &end, 10);
2096                 if (end && *end)
2097                         return NULL;
2098
2099                 ssid = wpa_config_get_network(wpa_s->conf, id);
2100                 if (ssid == NULL)
2101                         return NULL;
2102                 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2103         }
2104 #endif /* CONFIG_WPS_ER */
2105 #ifdef CONFIG_AP
2106         if (wpa_s->ap_iface)
2107                 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2108 #endif /* CONFIG_AP */
2109         return NULL;
2110 }
2111
2112
2113 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2114 {
2115         if (wpa_s->conf->wps_nfc_pw_from_config) {
2116                 return wps_nfc_token_build(ndef,
2117                                            wpa_s->conf->wps_nfc_dev_pw_id,
2118                                            wpa_s->conf->wps_nfc_dh_pubkey,
2119                                            wpa_s->conf->wps_nfc_dev_pw);
2120         }
2121
2122         return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2123                                  &wpa_s->conf->wps_nfc_dh_pubkey,
2124                                  &wpa_s->conf->wps_nfc_dh_privkey,
2125                                  &wpa_s->conf->wps_nfc_dev_pw);
2126 }
2127
2128
2129 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2130                        const u8 *bssid,
2131                        const struct wpabuf *dev_pw, u16 dev_pw_id,
2132                        int p2p_group, const u8 *peer_pubkey_hash,
2133                        const u8 *ssid, size_t ssid_len, int freq)
2134 {
2135         struct wps_context *wps = wpa_s->wps;
2136         char pw[32 * 2 + 1];
2137
2138         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2139                 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2140                 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2141         }
2142
2143         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2144             wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2145                 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2146                            "cannot start NFC-triggered connection");
2147                 return -1;
2148         }
2149
2150         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2151                 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2152                            "cannot start NFC-triggered connection", dev_pw_id);
2153                 return -1;
2154         }
2155
2156         dh5_free(wps->dh_ctx);
2157         wpabuf_free(wps->dh_pubkey);
2158         wpabuf_free(wps->dh_privkey);
2159         wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2160         wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2161         if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2162                 wps->dh_ctx = NULL;
2163                 wpabuf_free(wps->dh_pubkey);
2164                 wps->dh_pubkey = NULL;
2165                 wpabuf_free(wps->dh_privkey);
2166                 wps->dh_privkey = NULL;
2167                 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
2168                 return -1;
2169         }
2170         wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
2171         if (wps->dh_ctx == NULL) {
2172                 wpabuf_free(wps->dh_pubkey);
2173                 wps->dh_pubkey = NULL;
2174                 wpabuf_free(wps->dh_privkey);
2175                 wps->dh_privkey = NULL;
2176                 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
2177                 return -1;
2178         }
2179
2180         if (dev_pw) {
2181                 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2182                                            wpabuf_head(dev_pw),
2183                                            wpabuf_len(dev_pw));
2184         }
2185         return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2186                                      dev_pw ? pw : NULL,
2187                                      p2p_group, dev_pw_id, peer_pubkey_hash,
2188                                      ssid, ssid_len, freq);
2189 }
2190
2191
2192 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2193                              struct wps_parse_attr *attr)
2194 {
2195         /*
2196          * Disable existing networks temporarily to allow the newly learned
2197          * credential to be preferred. Enable the temporarily disabled networks
2198          * after 10 seconds.
2199          */
2200         wpas_wps_temp_disable(wpa_s, NULL);
2201         eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2202                                NULL);
2203
2204         if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2205                 return -1;
2206
2207         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2208                 return 0;
2209
2210         if (attr->ap_channel) {
2211                 u16 chan = WPA_GET_BE16(attr->ap_channel);
2212                 int freq = 0;
2213
2214                 if (chan >= 1 && chan <= 13)
2215                         freq = 2407 + 5 * chan;
2216                 else if (chan == 14)
2217                         freq = 2484;
2218                 else if (chan >= 30)
2219                         freq = 5000 + 5 * chan;
2220
2221                 if (freq) {
2222                         wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2223                                    chan, freq);
2224                         wpa_s->after_wps = 5;
2225                         wpa_s->wps_freq = freq;
2226                 }
2227         }
2228
2229         wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2230                    "based on the received credential added");
2231         wpa_s->normal_scans = 0;
2232         wpa_supplicant_reinit_autoscan(wpa_s);
2233         wpa_s->disconnected = 0;
2234         wpa_s->reassociate = 1;
2235
2236         wpa_supplicant_cancel_sched_scan(wpa_s);
2237         wpa_supplicant_req_scan(wpa_s, 0, 0);
2238
2239         return 0;
2240 }
2241
2242
2243 #ifdef CONFIG_WPS_ER
2244 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2245                                            struct wps_parse_attr *attr)
2246 {
2247         return wps_registrar_add_nfc_password_token(
2248                 wpa_s->wps->registrar, attr->oob_dev_password,
2249                 attr->oob_dev_password_len);
2250 }
2251 #endif /* CONFIG_WPS_ER */
2252
2253
2254 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2255                                     const struct wpabuf *wps)
2256 {
2257         struct wps_parse_attr attr;
2258
2259         wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2260
2261         if (wps_parse_msg(wps, &attr)) {
2262                 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2263                 return -1;
2264         }
2265
2266         if (attr.num_cred)
2267                 return wpas_wps_use_cred(wpa_s, &attr);
2268
2269 #ifdef CONFIG_WPS_ER
2270         if (attr.oob_dev_password)
2271                 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2272 #endif /* CONFIG_WPS_ER */
2273
2274         wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2275         return -1;
2276 }
2277
2278
2279 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2280                           const struct wpabuf *data, int forced_freq)
2281 {
2282         const struct wpabuf *wps = data;
2283         struct wpabuf *tmp = NULL;
2284         int ret;
2285
2286         if (wpabuf_len(data) < 4)
2287                 return -1;
2288
2289         if (*wpabuf_head_u8(data) != 0x10) {
2290                 /* Assume this contains full NDEF record */
2291                 tmp = ndef_parse_wifi(data);
2292                 if (tmp == NULL) {
2293 #ifdef CONFIG_P2P
2294                         tmp = ndef_parse_p2p(data);
2295                         if (tmp) {
2296                                 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2297                                                                forced_freq);
2298                                 wpabuf_free(tmp);
2299                                 return ret;
2300                         }
2301 #endif /* CONFIG_P2P */
2302                         wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2303                         return -1;
2304                 }
2305                 wps = tmp;
2306         }
2307
2308         ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2309         wpabuf_free(tmp);
2310         return ret;
2311 }
2312
2313
2314 struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2315                                           int ndef)
2316 {
2317         struct wpabuf *ret;
2318
2319         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2320             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2321                            &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2322                 return NULL;
2323
2324         ret = wps_build_nfc_handover_req(wpa_s->wps,
2325                                          wpa_s->conf->wps_nfc_dh_pubkey);
2326
2327         if (ndef && ret) {
2328                 struct wpabuf *tmp;
2329                 tmp = ndef_build_wifi(ret);
2330                 wpabuf_free(ret);
2331                 if (tmp == NULL)
2332                         return NULL;
2333                 ret = tmp;
2334         }
2335
2336         return ret;
2337 }
2338
2339
2340 #ifdef CONFIG_WPS_NFC
2341
2342 static struct wpabuf *
2343 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2344                              const char *uuid)
2345 {
2346 #ifdef CONFIG_WPS_ER
2347         struct wpabuf *ret;
2348         u8 u[UUID_LEN], *use_uuid = NULL;
2349         u8 addr[ETH_ALEN], *use_addr = NULL;
2350         struct wps_context *wps = wpa_s->wps;
2351
2352         if (wps == NULL)
2353                 return NULL;
2354
2355         if (uuid == NULL)
2356                 return NULL;
2357         if (uuid_str2bin(uuid, u) == 0)
2358                 use_uuid = u;
2359         else if (hwaddr_aton(uuid, addr) == 0)
2360                 use_addr = addr;
2361         else
2362                 return NULL;
2363
2364         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2365                 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2366                                    &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2367                         return NULL;
2368         }
2369
2370         wpas_wps_nfc_clear(wps);
2371         wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2372         wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2373         wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2374         if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2375                 wpas_wps_nfc_clear(wps);
2376                 return NULL;
2377         }
2378
2379         ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2380                                       use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
2381         if (ndef && ret) {
2382                 struct wpabuf *tmp;
2383                 tmp = ndef_build_wifi(ret);
2384                 wpabuf_free(ret);
2385                 if (tmp == NULL)
2386                         return NULL;
2387                 ret = tmp;
2388         }
2389
2390         return ret;
2391 #else /* CONFIG_WPS_ER */
2392         return NULL;
2393 #endif /* CONFIG_WPS_ER */
2394 }
2395 #endif /* CONFIG_WPS_NFC */
2396
2397
2398 struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2399                                           int ndef, int cr, const char *uuid)
2400 {
2401         struct wpabuf *ret;
2402         if (!cr)
2403                 return NULL;
2404         ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2405         if (ret)
2406                 return ret;
2407         return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
2408 }
2409
2410
2411 int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2412                                  const struct wpabuf *data)
2413 {
2414         struct wpabuf *wps;
2415         int ret = -1;
2416         u16 wsc_len;
2417         const u8 *pos;
2418         struct wpabuf msg;
2419         struct wps_parse_attr attr;
2420         u16 dev_pw_id;
2421         const u8 *bssid = NULL;
2422         int freq = 0;
2423
2424         wps = ndef_parse_wifi(data);
2425         if (wps == NULL)
2426                 return -1;
2427         wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2428                    "payload from NFC connection handover");
2429         wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2430         if (wpabuf_len(wps) < 2) {
2431                 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2432                            "Message");
2433                 goto out;
2434         }
2435         pos = wpabuf_head(wps);
2436         wsc_len = WPA_GET_BE16(pos);
2437         if (wsc_len > wpabuf_len(wps) - 2) {
2438                 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2439                            "in Wi-Fi Handover Select Message", wsc_len);
2440                 goto out;
2441         }
2442         pos += 2;
2443
2444         wpa_hexdump(MSG_DEBUG,
2445                     "WPS: WSC attributes in Wi-Fi Handover Select Message",
2446                     pos, wsc_len);
2447         if (wsc_len < wpabuf_len(wps) - 2) {
2448                 wpa_hexdump(MSG_DEBUG,
2449                             "WPS: Ignore extra data after WSC attributes",
2450                             pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2451         }
2452
2453         wpabuf_set(&msg, pos, wsc_len);
2454         ret = wps_parse_msg(&msg, &attr);
2455         if (ret < 0) {
2456                 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2457                            "Wi-Fi Handover Select Message");
2458                 goto out;
2459         }
2460
2461         if (attr.oob_dev_password == NULL ||
2462             attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2463                 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2464                            "included in Wi-Fi Handover Select Message");
2465                 ret = -1;
2466                 goto out;
2467         }
2468
2469         if (attr.ssid == NULL) {
2470                 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2471                            "Select Message");
2472                 ret = -1;
2473                 goto out;
2474         }
2475
2476         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2477
2478         if (attr.mac_addr) {
2479                 bssid = attr.mac_addr;
2480                 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2481                            MAC2STR(bssid));
2482         }
2483
2484         if (attr.rf_bands)
2485                 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2486
2487         if (attr.ap_channel) {
2488                 u16 chan = WPA_GET_BE16(attr.ap_channel);
2489
2490                 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2491
2492                 if (chan >= 1 && chan <= 13 &&
2493                     (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2494                         freq = 2407 + 5 * chan;
2495                 else if (chan == 14 &&
2496                          (attr.rf_bands == NULL ||
2497                           *attr.rf_bands & WPS_RF_24GHZ))
2498                         freq = 2484;
2499                 else if (chan >= 30 &&
2500                          (attr.rf_bands == NULL ||
2501                           *attr.rf_bands & WPS_RF_50GHZ))
2502                         freq = 5000 + 5 * chan;
2503
2504                 if (freq) {
2505                         wpa_printf(MSG_DEBUG,
2506                                    "WPS: AP indicated channel %u -> %u MHz",
2507                                    chan, freq);
2508                 }
2509         }
2510
2511         wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2512                     attr.oob_dev_password, attr.oob_dev_password_len);
2513         dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2514                                  WPS_OOB_PUBKEY_HASH_LEN);
2515         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2516                 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2517                            "%u in Wi-Fi Handover Select Message", dev_pw_id);
2518                 ret = -1;
2519                 goto out;
2520         }
2521         wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2522                     attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2523
2524         ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2525                                  attr.oob_dev_password,
2526                                  attr.ssid, attr.ssid_len, freq);
2527
2528 out:
2529         wpabuf_free(wps);
2530         return ret;
2531 }
2532
2533
2534 int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2535                                  const struct wpabuf *req,
2536                                  const struct wpabuf *sel)
2537 {
2538         wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2539         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2540         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2541         return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2542 }
2543
2544
2545 int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2546                                     const struct wpabuf *req,
2547                                     const struct wpabuf *sel)
2548 {
2549         struct wpabuf *wps;
2550         int ret = -1;
2551         u16 wsc_len;
2552         const u8 *pos;
2553         struct wpabuf msg;
2554         struct wps_parse_attr attr;
2555         u16 dev_pw_id;
2556
2557         /*
2558          * Enrollee/station is always initiator of the NFC connection handover,
2559          * so use the request message here to find Enrollee public key hash.
2560          */
2561         wps = ndef_parse_wifi(req);
2562         if (wps == NULL)
2563                 return -1;
2564         wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2565                    "payload from NFC connection handover");
2566         wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2567         if (wpabuf_len(wps) < 2) {
2568                 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2569                            "Message");
2570                 goto out;
2571         }
2572         pos = wpabuf_head(wps);
2573         wsc_len = WPA_GET_BE16(pos);
2574         if (wsc_len > wpabuf_len(wps) - 2) {
2575                 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2576                            "in rt Wi-Fi Handover Request Message", wsc_len);
2577                 goto out;
2578         }
2579         pos += 2;
2580
2581         wpa_hexdump(MSG_DEBUG,
2582                     "WPS: WSC attributes in Wi-Fi Handover Request Message",
2583                     pos, wsc_len);
2584         if (wsc_len < wpabuf_len(wps) - 2) {
2585                 wpa_hexdump(MSG_DEBUG,
2586                             "WPS: Ignore extra data after WSC attributes",
2587                             pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2588         }
2589
2590         wpabuf_set(&msg, pos, wsc_len);
2591         ret = wps_parse_msg(&msg, &attr);
2592         if (ret < 0) {
2593                 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2594                            "Wi-Fi Handover Request Message");
2595                 goto out;
2596         }
2597
2598         if (attr.oob_dev_password == NULL ||
2599             attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2600                 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2601                            "included in Wi-Fi Handover Request Message");
2602                 ret = -1;
2603                 goto out;
2604         }
2605
2606         if (attr.uuid_e == NULL) {
2607                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2608                            "Handover Request Message");
2609                 ret = -1;
2610                 goto out;
2611         }
2612
2613         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2614
2615         wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2616                     attr.oob_dev_password, attr.oob_dev_password_len);
2617         dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2618                                  WPS_OOB_PUBKEY_HASH_LEN);
2619         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2620                 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2621                            "%u in Wi-Fi Handover Request Message", dev_pw_id);
2622                 ret = -1;
2623                 goto out;
2624         }
2625         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2626                     attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2627
2628         ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2629                                              attr.oob_dev_password,
2630                                              DEV_PW_NFC_CONNECTION_HANDOVER,
2631                                              NULL, 0, 1);
2632
2633 out:
2634         wpabuf_free(wps);
2635         return ret;
2636 }
2637
2638 #endif /* CONFIG_WPS_NFC */
2639
2640
2641 static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2642 {
2643         size_t i;
2644         struct os_reltime now;
2645
2646         if (wpa_debug_level > MSG_DEBUG)
2647                 return;
2648
2649         if (wpa_s->wps_ap == NULL)
2650                 return;
2651
2652         os_get_reltime(&now);
2653
2654         for (i = 0; i < wpa_s->num_wps_ap; i++) {
2655                 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2656                 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2657
2658                 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2659                            "tries=%d last_attempt=%d sec ago blacklist=%d",
2660                            (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2661                            ap->last_attempt.sec > 0 ?
2662                            (int) now.sec - (int) ap->last_attempt.sec : -1,
2663                            e ? e->count : 0);
2664         }
2665 }
2666
2667
2668 static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2669                                                  const u8 *bssid)
2670 {
2671         size_t i;
2672
2673         if (wpa_s->wps_ap == NULL)
2674                 return NULL;
2675
2676         for (i = 0; i < wpa_s->num_wps_ap; i++) {
2677                 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2678                 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2679                         return ap;
2680         }
2681
2682         return NULL;
2683 }
2684
2685
2686 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2687                                         struct wpa_scan_res *res)
2688 {
2689         struct wpabuf *wps;
2690         enum wps_ap_info_type type;
2691         struct wps_ap_info *ap;
2692         int r;
2693
2694         if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2695                 return;
2696
2697         wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2698         if (wps == NULL)
2699                 return;
2700
2701         r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2702         if (r == 2)
2703                 type = WPS_AP_SEL_REG_OUR;
2704         else if (r == 1)
2705                 type = WPS_AP_SEL_REG;
2706         else
2707                 type = WPS_AP_NOT_SEL_REG;
2708
2709         wpabuf_free(wps);
2710
2711         ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2712         if (ap) {
2713                 if (ap->type != type) {
2714                         wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2715                                    " changed type %d -> %d",
2716                                    MAC2STR(res->bssid), ap->type, type);
2717                         ap->type = type;
2718                         if (type != WPS_AP_NOT_SEL_REG)
2719                                 wpa_blacklist_del(wpa_s, ap->bssid);
2720                 }
2721                 return;
2722         }
2723
2724         ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2725                               sizeof(struct wps_ap_info));
2726         if (ap == NULL)
2727                 return;
2728
2729         wpa_s->wps_ap = ap;
2730         ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2731         wpa_s->num_wps_ap++;
2732
2733         os_memset(ap, 0, sizeof(*ap));
2734         os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2735         ap->type = type;
2736         wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2737                    MAC2STR(ap->bssid), ap->type);
2738 }
2739
2740
2741 void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2742                              struct wpa_scan_results *scan_res)
2743 {
2744         size_t i;
2745
2746         for (i = 0; i < scan_res->num; i++)
2747                 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2748
2749         wpas_wps_dump_ap_info(wpa_s);
2750 }
2751
2752
2753 void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2754 {
2755         struct wps_ap_info *ap;
2756
2757         wpa_s->after_wps = 0;
2758
2759         if (!wpa_s->wps_ap_iter)
2760                 return;
2761         ap = wpas_wps_get_ap_info(wpa_s, bssid);
2762         if (ap == NULL)
2763                 return;
2764         ap->tries++;
2765         os_get_reltime(&ap->last_attempt);
2766 }