P2P: More complete persistent group management over D-Bus
[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                 /*
784                  * In case the network object corresponds to a persistent group
785                  * then do not send out network disabled signal. In addition,
786                  * do not change disabled status of persistent network objects
787                  * from 2 to 1 should we connect to another network.
788                  */
789                 if (was_disabled != 2) {
790                         ssid->disabled = ssid != selected;
791                         if (was_disabled != ssid->disabled)
792                                 wpas_notify_network_enabled_changed(wpa_s,
793                                                                     ssid);
794                 }
795                 ssid = ssid->next;
796         }
797         wpa_s->disconnected = 0;
798         wpa_s->reassociate = 1;
799         wpa_s->scan_runs = 0;
800         wpa_s->wps_success = 0;
801         wpa_s->blacklist_cleared = 0;
802         wpa_supplicant_req_scan(wpa_s, 0, 0);
803 }
804
805
806 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
807                        int p2p_group)
808 {
809         struct wpa_ssid *ssid;
810         wpas_clear_wps(wpa_s);
811         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
812         if (ssid == NULL)
813                 return -1;
814         ssid->temporary = 1;
815         ssid->p2p_group = p2p_group;
816 #ifdef CONFIG_P2P
817         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
818                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
819                 if (ssid->ssid) {
820                         ssid->ssid_len = wpa_s->go_params->ssid_len;
821                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
822                                   ssid->ssid_len);
823                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
824                                           "SSID", ssid->ssid, ssid->ssid_len);
825                 }
826         }
827 #endif /* CONFIG_P2P */
828         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
829         if (wpa_s->wps_fragment_size)
830                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
831         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
832                                wpa_s, NULL);
833         wpas_wps_reassoc(wpa_s, ssid);
834         return 0;
835 }
836
837
838 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
839                        const char *pin, int p2p_group, u16 dev_pw_id)
840 {
841         struct wpa_ssid *ssid;
842         char val[128];
843         unsigned int rpin = 0;
844
845         wpas_clear_wps(wpa_s);
846         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
847         if (ssid == NULL)
848                 return -1;
849         ssid->temporary = 1;
850         ssid->p2p_group = p2p_group;
851 #ifdef CONFIG_P2P
852         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
853                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
854                 if (ssid->ssid) {
855                         ssid->ssid_len = wpa_s->go_params->ssid_len;
856                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
857                                   ssid->ssid_len);
858                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
859                                           "SSID", ssid->ssid, ssid->ssid_len);
860                 }
861         }
862 #endif /* CONFIG_P2P */
863         if (pin)
864                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
865                             pin, dev_pw_id);
866         else {
867                 rpin = wps_generate_pin();
868                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
869                             rpin, dev_pw_id);
870         }
871         wpa_config_set(ssid, "phase1", val, 0);
872         if (wpa_s->wps_fragment_size)
873                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
874         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
875                                wpa_s, NULL);
876         wpas_wps_reassoc(wpa_s, ssid);
877         return rpin;
878 }
879
880
881 /* Cancel the wps pbc/pin requests */
882 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
883 {
884 #ifdef CONFIG_AP
885         if (wpa_s->ap_iface) {
886                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
887                 return wpa_supplicant_ap_wps_cancel(wpa_s);
888         }
889 #endif /* CONFIG_AP */
890
891         if (wpa_s->wpa_state == WPA_SCANNING) {
892                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
893                 wpa_supplicant_cancel_scan(wpa_s);
894                 wpas_clear_wps(wpa_s);
895         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
896                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
897                            "deauthenticate");
898                 wpa_supplicant_deauthenticate(wpa_s,
899                                               WLAN_REASON_DEAUTH_LEAVING);
900                 wpas_clear_wps(wpa_s);
901         }
902
903         return 0;
904 }
905
906
907 #ifdef CONFIG_WPS_OOB
908 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
909                        char *path, char *method, char *name)
910 {
911         struct wps_context *wps = wpa_s->wps;
912         struct oob_device_data *oob_dev;
913
914         oob_dev = wps_get_oob_device(device_type);
915         if (oob_dev == NULL)
916                 return -1;
917         oob_dev->device_path = path;
918         oob_dev->device_name = name;
919         wps->oob_conf.oob_method = wps_get_oob_method(method);
920
921         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
922                 /*
923                  * Use pre-configured DH keys in order to be able to write the
924                  * key hash into the OOB file.
925                  */
926                 wpabuf_free(wps->dh_pubkey);
927                 wpabuf_free(wps->dh_privkey);
928                 wps->dh_privkey = NULL;
929                 wps->dh_pubkey = NULL;
930                 dh5_free(wps->dh_ctx);
931                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
932                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
933                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
934                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
935                                    "Diffie-Hellman handshake");
936                         return -1;
937                 }
938         }
939
940         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
941                 wpas_clear_wps(wpa_s);
942
943         if (wps_process_oob(wps, oob_dev, 0) < 0)
944                 return -1;
945
946         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
947              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
948             wpas_wps_start_pin(wpa_s, NULL,
949                                wpabuf_head(wps->oob_conf.dev_password), 0,
950                                DEV_PW_DEFAULT) < 0)
951                         return -1;
952
953         return 0;
954 }
955 #endif /* CONFIG_WPS_OOB */
956
957
958 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
959                        const char *pin, struct wps_new_ap_settings *settings)
960 {
961         struct wpa_ssid *ssid;
962         char val[200];
963         char *pos, *end;
964         int res;
965
966         if (!pin)
967                 return -1;
968         wpas_clear_wps(wpa_s);
969         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
970         if (ssid == NULL)
971                 return -1;
972         ssid->temporary = 1;
973         pos = val;
974         end = pos + sizeof(val);
975         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
976         if (res < 0 || res >= end - pos)
977                 return -1;
978         pos += res;
979         if (settings) {
980                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
981                                   "new_encr=%s new_key=%s",
982                                   settings->ssid_hex, settings->auth,
983                                   settings->encr, settings->key_hex);
984                 if (res < 0 || res >= end - pos)
985                         return -1;
986                 pos += res;
987         }
988         res = os_snprintf(pos, end - pos, "\"");
989         if (res < 0 || res >= end - pos)
990                 return -1;
991         wpa_config_set(ssid, "phase1", val, 0);
992         if (wpa_s->wps_fragment_size)
993                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
994         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
995                                wpa_s, NULL);
996         wpas_wps_reassoc(wpa_s, ssid);
997         return 0;
998 }
999
1000
1001 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
1002                                size_t psk_len)
1003 {
1004         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
1005                    "STA " MACSTR, MAC2STR(mac_addr));
1006         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1007
1008         /* TODO */
1009
1010         return 0;
1011 }
1012
1013
1014 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1015                                    const struct wps_device_data *dev)
1016 {
1017         char uuid[40], txt[400];
1018         int len;
1019         char devtype[WPS_DEV_TYPE_BUFSIZE];
1020         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1021                 return;
1022         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1023         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1024                           " [%s|%s|%s|%s|%s|%s]",
1025                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
1026                           dev->manufacturer, dev->model_name,
1027                           dev->model_number, dev->serial_number,
1028                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1029                                                sizeof(devtype)));
1030         if (len > 0 && len < (int) sizeof(txt))
1031                 wpa_printf(MSG_INFO, "%s", txt);
1032 }
1033
1034
1035 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1036                                     u16 sel_reg_config_methods)
1037 {
1038 #ifdef CONFIG_WPS_ER
1039         struct wpa_supplicant *wpa_s = ctx;
1040
1041         if (wpa_s->wps_er == NULL)
1042                 return;
1043         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1044                    "dev_password_id=%u sel_reg_config_methods=0x%x",
1045                    sel_reg, dev_passwd_id, sel_reg_config_methods);
1046         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1047                            sel_reg_config_methods);
1048 #endif /* CONFIG_WPS_ER */
1049 }
1050
1051
1052 static u16 wps_fix_config_methods(u16 config_methods)
1053 {
1054 #ifdef CONFIG_WPS2
1055         if ((config_methods &
1056              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1057               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1058                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1059                            "virtual_display for WPS 2.0 compliance");
1060                 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1061         }
1062         if ((config_methods &
1063              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1064               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1065                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1066                            "virtual_push_button for WPS 2.0 compliance");
1067                 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1068         }
1069 #endif /* CONFIG_WPS2 */
1070
1071         return config_methods;
1072 }
1073
1074
1075 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1076                               struct wps_context *wps)
1077 {
1078         wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1079         if (is_nil_uuid(wpa_s->conf->uuid)) {
1080                 struct wpa_supplicant *first;
1081                 first = wpa_s->global->ifaces;
1082                 while (first && first->next)
1083                         first = first->next;
1084                 if (first && first != wpa_s) {
1085                         os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
1086                                   WPS_UUID_LEN);
1087                         wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1088                                     "interface", wps->uuid, WPS_UUID_LEN);
1089                 } else {
1090                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1091                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1092                                     "address", wps->uuid, WPS_UUID_LEN);
1093                 }
1094         } else {
1095                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1096                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1097                             wps->uuid, WPS_UUID_LEN);
1098         }
1099 }
1100
1101
1102 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1103 {
1104         struct wps_context *wps;
1105         struct wps_registrar_config rcfg;
1106
1107         wps = os_zalloc(sizeof(*wps));
1108         if (wps == NULL)
1109                 return -1;
1110
1111         wps->cred_cb = wpa_supplicant_wps_cred;
1112         wps->event_cb = wpa_supplicant_wps_event;
1113         wps->cb_ctx = wpa_s;
1114
1115         wps->dev.device_name = wpa_s->conf->device_name;
1116         wps->dev.manufacturer = wpa_s->conf->manufacturer;
1117         wps->dev.model_name = wpa_s->conf->model_name;
1118         wps->dev.model_number = wpa_s->conf->model_number;
1119         wps->dev.serial_number = wpa_s->conf->serial_number;
1120         wps->config_methods =
1121                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1122         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1123             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1124                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1125                            "methods are not allowed at the same time");
1126                 os_free(wps);
1127                 return -1;
1128         }
1129         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1130         os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1131                   WPS_DEV_TYPE_LEN);
1132
1133         wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1134         os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1135                   WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1136
1137         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1138         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1139         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1140         wpas_wps_set_uuid(wpa_s, wps);
1141
1142         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1143         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1144
1145         os_memset(&rcfg, 0, sizeof(rcfg));
1146         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1147         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1148         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1149         rcfg.cb_ctx = wpa_s;
1150
1151         wps->registrar = wps_registrar_init(wps, &rcfg);
1152         if (wps->registrar == NULL) {
1153                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1154                 os_free(wps);
1155                 return -1;
1156         }
1157
1158         wpa_s->wps = wps;
1159
1160         return 0;
1161 }
1162
1163
1164 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1165 {
1166         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1167
1168         if (wpa_s->wps == NULL)
1169                 return;
1170
1171 #ifdef CONFIG_WPS_ER
1172         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1173         wpa_s->wps_er = NULL;
1174 #endif /* CONFIG_WPS_ER */
1175
1176         wps_registrar_deinit(wpa_s->wps->registrar);
1177         wpabuf_free(wpa_s->wps->dh_pubkey);
1178         wpabuf_free(wpa_s->wps->dh_privkey);
1179         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1180         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1181         os_free(wpa_s->wps->network_key);
1182         os_free(wpa_s->wps);
1183         wpa_s->wps = NULL;
1184 }
1185
1186
1187 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1188                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1189 {
1190         struct wpabuf *wps_ie;
1191
1192         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1193                 return -1;
1194
1195         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1196         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1197                 if (!wps_ie) {
1198                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1199                         return 0;
1200                 }
1201
1202                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1203                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1204                                    "without active PBC Registrar");
1205                         wpabuf_free(wps_ie);
1206                         return 0;
1207                 }
1208
1209                 /* TODO: overlap detection */
1210                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1211                            "(Active PBC)");
1212                 wpabuf_free(wps_ie);
1213                 return 1;
1214         }
1215
1216         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1217                 if (!wps_ie) {
1218                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1219                         return 0;
1220                 }
1221
1222                 /*
1223                  * Start with WPS APs that advertise our address as an
1224                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1225                  * allow any WPS AP after couple of scans since some APs do not
1226                  * set Selected Registrar attribute properly when using
1227                  * external Registrar.
1228                  */
1229                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1230                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1231                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1232                                            "without active PIN Registrar");
1233                                 wpabuf_free(wps_ie);
1234                                 return 0;
1235                         }
1236                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1237                 } else {
1238                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1239                                    "(Authorized MAC or Active PIN)");
1240                 }
1241                 wpabuf_free(wps_ie);
1242                 return 1;
1243         }
1244
1245         if (wps_ie) {
1246                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1247                 wpabuf_free(wps_ie);
1248                 return 1;
1249         }
1250
1251         return -1;
1252 }
1253
1254
1255 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1256                               struct wpa_ssid *ssid,
1257                               struct wpa_scan_res *bss)
1258 {
1259         struct wpabuf *wps_ie = NULL;
1260         int ret = 0;
1261
1262         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1263                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1264                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1265                         /* allow wildcard SSID for WPS PBC */
1266                         ret = 1;
1267                 }
1268         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1269                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1270                 if (wps_ie &&
1271                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1272                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1273                         /* allow wildcard SSID for WPS PIN */
1274                         ret = 1;
1275                 }
1276         }
1277
1278         if (!ret && ssid->bssid_set &&
1279             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1280                 /* allow wildcard SSID due to hardcoded BSSID match */
1281                 ret = 1;
1282         }
1283
1284 #ifdef CONFIG_WPS_STRICT
1285         if (wps_ie) {
1286                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1287                                                    0, bss->bssid) < 0)
1288                         ret = 0;
1289                 if (bss->beacon_ie_len) {
1290                         struct wpabuf *bcn_wps;
1291                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1292                                 bss, WPS_IE_VENDOR_TYPE);
1293                         if (bcn_wps == NULL) {
1294                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1295                                            "missing from AP Beacon");
1296                                 ret = 0;
1297                         } else {
1298                                 if (wps_validate_beacon(wps_ie) < 0)
1299                                         ret = 0;
1300                                 wpabuf_free(bcn_wps);
1301                         }
1302                 }
1303         }
1304 #endif /* CONFIG_WPS_STRICT */
1305
1306         wpabuf_free(wps_ie);
1307
1308         return ret;
1309 }
1310
1311
1312 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1313                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1314 {
1315         const u8 *sel_uuid, *uuid;
1316         struct wpabuf *wps_ie;
1317         int ret = 0;
1318         struct wpa_bss *bss;
1319
1320         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1321                 return 0;
1322
1323         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1324                    "present in scan results; selected BSSID " MACSTR,
1325                    MAC2STR(selected->bssid));
1326
1327         /* Make sure that only one AP is in active PBC mode */
1328         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1329         if (wps_ie) {
1330                 sel_uuid = wps_get_uuid_e(wps_ie);
1331                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1332                             sel_uuid, UUID_LEN);
1333         } else {
1334                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1335                            "WPS IE?!");
1336                 sel_uuid = NULL;
1337         }
1338
1339         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1340                 struct wpabuf *ie;
1341                 if (bss == selected)
1342                         continue;
1343                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1344                 if (!ie)
1345                         continue;
1346                 if (!wps_is_selected_pbc_registrar(ie)) {
1347                         wpabuf_free(ie);
1348                         continue;
1349                 }
1350                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1351                            MACSTR, MAC2STR(bss->bssid));
1352                 uuid = wps_get_uuid_e(ie);
1353                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1354                             uuid, UUID_LEN);
1355                 if (sel_uuid == NULL || uuid == NULL ||
1356                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1357                         ret = 1; /* PBC overlap */
1358                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1359                                 MACSTR " and " MACSTR,
1360                                 MAC2STR(selected->bssid),
1361                                 MAC2STR(bss->bssid));
1362                         wpabuf_free(ie);
1363                         break;
1364                 }
1365
1366                 /* TODO: verify that this is reasonable dual-band situation */
1367
1368                 wpabuf_free(ie);
1369         }
1370
1371         wpabuf_free(wps_ie);
1372
1373         return ret;
1374 }
1375
1376
1377 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1378 {
1379         struct wpa_bss *bss;
1380         unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1381
1382         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1383                 return;
1384
1385         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1386                 struct wpabuf *ie;
1387                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1388                 if (!ie)
1389                         continue;
1390                 if (wps_is_selected_pbc_registrar(ie))
1391                         pbc++;
1392                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1393                         auth++;
1394                 else if (wps_is_selected_pin_registrar(ie))
1395                         pin++;
1396                 else
1397                         wps++;
1398                 wpabuf_free(ie);
1399         }
1400
1401         if (pbc)
1402                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1403         else if (auth)
1404                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1405         else if (pin)
1406                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1407         else if (wps)
1408                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1409 }
1410
1411
1412 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1413 {
1414         struct wpa_ssid *ssid;
1415
1416         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1417                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1418                         return 1;
1419         }
1420
1421         return 0;
1422 }
1423
1424
1425 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1426                               char *end)
1427 {
1428         struct wpabuf *wps_ie;
1429         int ret;
1430
1431         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1432         if (wps_ie == NULL)
1433                 return 0;
1434
1435         ret = wps_attr_text(wps_ie, buf, end);
1436         wpabuf_free(wps_ie);
1437         return ret;
1438 }
1439
1440
1441 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1442 {
1443 #ifdef CONFIG_WPS_ER
1444         if (wpa_s->wps_er) {
1445                 wps_er_refresh(wpa_s->wps_er);
1446                 return 0;
1447         }
1448         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1449         if (wpa_s->wps_er == NULL)
1450                 return -1;
1451         return 0;
1452 #else /* CONFIG_WPS_ER */
1453         return 0;
1454 #endif /* CONFIG_WPS_ER */
1455 }
1456
1457
1458 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1459 {
1460 #ifdef CONFIG_WPS_ER
1461         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1462         wpa_s->wps_er = NULL;
1463 #endif /* CONFIG_WPS_ER */
1464         return 0;
1465 }
1466
1467
1468 #ifdef CONFIG_WPS_ER
1469 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1470                         const char *uuid, const char *pin)
1471 {
1472         u8 u[UUID_LEN];
1473         int any = 0;
1474
1475         if (os_strcmp(uuid, "any") == 0)
1476                 any = 1;
1477         else if (uuid_str2bin(uuid, u))
1478                 return -1;
1479         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1480                                      any ? NULL : u,
1481                                      (const u8 *) pin, os_strlen(pin), 300);
1482 }
1483
1484
1485 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1486 {
1487         u8 u[UUID_LEN];
1488
1489         if (uuid_str2bin(uuid, u))
1490                 return -1;
1491         return wps_er_pbc(wpa_s->wps_er, u);
1492 }
1493
1494
1495 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1496                       const char *pin)
1497 {
1498         u8 u[UUID_LEN];
1499
1500         if (uuid_str2bin(uuid, u))
1501                 return -1;
1502         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1503                             os_strlen(pin));
1504 }
1505
1506
1507 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1508                            int id)
1509 {
1510         u8 u[UUID_LEN];
1511         struct wpa_ssid *ssid;
1512         struct wps_credential cred;
1513
1514         if (uuid_str2bin(uuid, u))
1515                 return -1;
1516         ssid = wpa_config_get_network(wpa_s->conf, id);
1517         if (ssid == NULL || ssid->ssid == NULL)
1518                 return -1;
1519
1520         os_memset(&cred, 0, sizeof(cred));
1521         if (ssid->ssid_len > 32)
1522                 return -1;
1523         os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1524         cred.ssid_len = ssid->ssid_len;
1525         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1526                 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1527                         WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1528                 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1529                         cred.encr_type = WPS_ENCR_AES;
1530                 else
1531                         cred.encr_type = WPS_ENCR_TKIP;
1532                 if (ssid->passphrase) {
1533                         cred.key_len = os_strlen(ssid->passphrase);
1534                         if (cred.key_len >= 64)
1535                                 return -1;
1536                         os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1537                 } else if (ssid->psk_set) {
1538                         cred.key_len = 32;
1539                         os_memcpy(cred.key, ssid->psk, 32);
1540                 } else
1541                         return -1;
1542         } else {
1543                 cred.auth_type = WPS_AUTH_OPEN;
1544                 cred.encr_type = WPS_ENCR_NONE;
1545         }
1546         return wps_er_set_config(wpa_s->wps_er, u, &cred);
1547 }
1548
1549
1550 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1551                        const char *pin, struct wps_new_ap_settings *settings)
1552 {
1553         u8 u[UUID_LEN];
1554         struct wps_credential cred;
1555         size_t len;
1556
1557         if (uuid_str2bin(uuid, u))
1558                 return -1;
1559         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1560             settings->encr == NULL || settings->key_hex == NULL)
1561                 return -1;
1562
1563         os_memset(&cred, 0, sizeof(cred));
1564         len = os_strlen(settings->ssid_hex);
1565         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1566             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1567                 return -1;
1568         cred.ssid_len = len / 2;
1569
1570         len = os_strlen(settings->key_hex);
1571         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1572             hexstr2bin(settings->key_hex, cred.key, len / 2))
1573                 return -1;
1574         cred.key_len = len / 2;
1575
1576         if (os_strcmp(settings->auth, "OPEN") == 0)
1577                 cred.auth_type = WPS_AUTH_OPEN;
1578         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1579                 cred.auth_type = WPS_AUTH_WPAPSK;
1580         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1581                 cred.auth_type = WPS_AUTH_WPA2PSK;
1582         else
1583                 return -1;
1584
1585         if (os_strcmp(settings->encr, "NONE") == 0)
1586                 cred.encr_type = WPS_ENCR_NONE;
1587         else if (os_strcmp(settings->encr, "WEP") == 0)
1588                 cred.encr_type = WPS_ENCR_WEP;
1589         else if (os_strcmp(settings->encr, "TKIP") == 0)
1590                 cred.encr_type = WPS_ENCR_TKIP;
1591         else if (os_strcmp(settings->encr, "CCMP") == 0)
1592                 cred.encr_type = WPS_ENCR_AES;
1593         else
1594                 return -1;
1595
1596         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1597                              os_strlen(pin), &cred);
1598 }
1599
1600
1601 static int callbacks_pending = 0;
1602
1603 static void wpas_wps_terminate_cb(void *ctx)
1604 {
1605         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1606         if (--callbacks_pending <= 0)
1607                 eloop_terminate();
1608 }
1609 #endif /* CONFIG_WPS_ER */
1610
1611
1612 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1613 {
1614 #ifdef CONFIG_WPS_ER
1615         if (wpa_s->wps_er) {
1616                 callbacks_pending++;
1617                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1618                 wpa_s->wps_er = NULL;
1619                 return 1;
1620         }
1621 #endif /* CONFIG_WPS_ER */
1622         return 0;
1623 }
1624
1625
1626 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1627 {
1628         struct wpa_ssid *ssid;
1629
1630         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1631                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1632                         return 1;
1633         }
1634
1635         return 0;
1636 }
1637
1638
1639 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1640 {
1641         struct wps_context *wps = wpa_s->wps;
1642
1643         if (wps == NULL)
1644                 return;
1645
1646         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1647                 wps->config_methods = wps_config_methods_str2bin(
1648                         wpa_s->conf->config_methods);
1649                 if ((wps->config_methods &
1650                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1651                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1652                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1653                                    "config methods are not allowed at the "
1654                                    "same time");
1655                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1656                 }
1657         }
1658         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1659
1660         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1661                 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1662                           WPS_DEV_TYPE_LEN);
1663
1664         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1665                 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1666                 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1667                           wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1668         }
1669
1670         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1671                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1672
1673         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1674                 wpas_wps_set_uuid(wpa_s, wps);
1675
1676         if (wpa_s->conf->changed_parameters &
1677             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1678                 /* Update pointers to make sure they refer current values */
1679                 wps->dev.device_name = wpa_s->conf->device_name;
1680                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1681                 wps->dev.model_name = wpa_s->conf->model_name;
1682                 wps->dev.model_number = wpa_s->conf->model_number;
1683                 wps->dev.serial_number = wpa_s->conf->serial_number;
1684         }
1685 }