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