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