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