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