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