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