WPS ER: Add ctrl_iface notifications for AP/Enrollee add/remove
[libeap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008, 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 "ieee802_11_defs.h"
19 #include "ieee802_11_common.h"
20 #include "wpa_common.h"
21 #include "config.h"
22 #include "eap_peer/eap.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "eloop.h"
26 #include "uuid.h"
27 #include "wpa_ctrl.h"
28 #include "notify.h"
29 #include "eap_common/eap_wsc_common.h"
30 #include "blacklist.h"
31 #include "wpa.h"
32 #include "wps_supplicant.h"
33 #include "dh_group5.h"
34
35 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
36
37 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
38 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
39
40
41 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
42 {
43         if (!wpa_s->wps_success &&
44             wpa_s->current_ssid &&
45             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
46                 const u8 *bssid = wpa_s->bssid;
47                 if (is_zero_ether_addr(bssid))
48                         bssid = wpa_s->pending_bssid;
49
50                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
51                            " did not succeed - continue trying to find "
52                            "suitable AP", MAC2STR(bssid));
53                 wpa_blacklist_add(wpa_s, bssid);
54
55                 wpa_supplicant_deauthenticate(wpa_s,
56                                               WLAN_REASON_DEAUTH_LEAVING);
57                 wpa_s->reassociate = 1;
58                 wpa_supplicant_req_scan(wpa_s,
59                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
60                 wpa_s->blacklist_cleared = 0;
61                 return 1;
62         }
63
64         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
65
66         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
67             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
68                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
69                            "try to associate with the received credential");
70                 wpa_supplicant_deauthenticate(wpa_s,
71                                               WLAN_REASON_DEAUTH_LEAVING);
72                 wpa_s->reassociate = 1;
73                 wpa_supplicant_req_scan(wpa_s, 0, 0);
74                 return 1;
75         }
76
77         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
78                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
79                            "for external credential processing");
80                 wpas_clear_wps(wpa_s);
81                 wpa_supplicant_deauthenticate(wpa_s,
82                                               WLAN_REASON_DEAUTH_LEAVING);
83                 return 1;
84         }
85
86         return 0;
87 }
88
89
90 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
91                                          struct wpa_ssid *ssid,
92                                          const struct wps_credential *cred)
93 {
94         struct wpa_driver_capa capa;
95         size_t i;
96         struct wpa_scan_res *bss;
97         const u8 *ie;
98         struct wpa_ie_data adv;
99         int wpa2 = 0, ccmp = 0;
100
101         /*
102          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
103          * case they are configured for mixed mode operation (WPA+WPA2 and
104          * TKIP+CCMP). Try to use scan results to figure out whether the AP
105          * actually supports stronger security and select that if the client
106          * has support for it, too.
107          */
108
109         if (wpa_drv_get_capa(wpa_s, &capa))
110                 return; /* Unknown what driver supports */
111
112         if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
113                 return; /* Could not get scan results for checking advertised
114                          * parameters */
115
116         for (i = 0; i < wpa_s->scan_res->num; i++) {
117                 bss = wpa_s->scan_res->res[i];
118                 if (os_memcmp(bss->bssid, cred->mac_addr, ETH_ALEN) != 0)
119                         continue;
120                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
121                 if (ie == NULL)
122                         continue;
123                 if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
124                     os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
125                         continue;
126
127                 wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
128                 break;
129         }
130
131         if (i == wpa_s->scan_res->num) {
132                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from scan "
133                            "results - use credential as-is");
134                 return;
135         }
136
137         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
138         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
139                 wpa2 = 1;
140                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
141                         ccmp = 1;
142         } else {
143                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
144                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
145                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
146                         ccmp = 1;
147         }
148
149         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
150             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
151                 /*
152                  * TODO: This could be the initial AP configuration and the
153                  * Beacon contents could change shortly. Should request a new
154                  * scan and delay addition of the network until the updated
155                  * scan results are available.
156                  */
157                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
158                            "support - use credential as-is");
159                 return;
160         }
161
162         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
163             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
164             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
165                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
166                            "based on scan results");
167                 if (wpa_s->conf->ap_scan == 1)
168                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
169                 else
170                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
171         }
172
173         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
174             (ssid->proto & WPA_PROTO_WPA) &&
175             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
176                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
177                            "based on scan results");
178                 if (wpa_s->conf->ap_scan == 1)
179                         ssid->proto |= WPA_PROTO_RSN;
180                 else
181                         ssid->proto = WPA_PROTO_RSN;
182         }
183 }
184
185
186 static int wpa_supplicant_wps_cred(void *ctx,
187                                    const struct wps_credential *cred)
188 {
189         struct wpa_supplicant *wpa_s = ctx;
190         struct wpa_ssid *ssid = wpa_s->current_ssid;
191         u8 key_idx = 0;
192         u16 auth_type;
193
194         if ((wpa_s->conf->wps_cred_processing == 1 ||
195              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
196                 size_t blen = cred->cred_attr_len * 2 + 1;
197                 char *buf = os_malloc(blen);
198                 if (buf) {
199                         wpa_snprintf_hex(buf, blen,
200                                          cred->cred_attr, cred->cred_attr_len);
201                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
202                                 WPS_EVENT_CRED_RECEIVED, buf);
203                         os_free(buf);
204                 }
205
206                 wpas_notify_wps_credential(wpa_s, cred);
207         } else
208                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
209
210         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
211                         cred->cred_attr, cred->cred_attr_len);
212
213         if (wpa_s->conf->wps_cred_processing == 1)
214                 return 0;
215
216         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
217         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
218                    cred->auth_type);
219         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
220         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
221         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
222                         cred->key, cred->key_len);
223         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
224                    MAC2STR(cred->mac_addr));
225
226         auth_type = cred->auth_type;
227         if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
228                 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
229                            "auth_type into WPA2PSK");
230                 auth_type = WPS_AUTH_WPA2PSK;
231         }
232
233         if (auth_type != WPS_AUTH_OPEN &&
234             auth_type != WPS_AUTH_SHARED &&
235             auth_type != WPS_AUTH_WPAPSK &&
236             auth_type != WPS_AUTH_WPA2PSK) {
237                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
238                            "unsupported authentication type 0x%x",
239                            auth_type);
240                 return 0;
241         }
242
243         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
244                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
245                            "on the received credential");
246                 os_free(ssid->eap.identity);
247                 ssid->eap.identity = NULL;
248                 ssid->eap.identity_len = 0;
249                 os_free(ssid->eap.phase1);
250                 ssid->eap.phase1 = NULL;
251                 os_free(ssid->eap.eap_methods);
252                 ssid->eap.eap_methods = NULL;
253         } else {
254                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
255                            "received credential");
256                 ssid = wpa_config_add_network(wpa_s->conf);
257                 if (ssid == NULL)
258                         return -1;
259                 wpas_notify_network_added(wpa_s, ssid);
260         }
261
262         wpa_config_set_network_defaults(ssid);
263
264         os_free(ssid->ssid);
265         ssid->ssid = os_malloc(cred->ssid_len);
266         if (ssid->ssid) {
267                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
268                 ssid->ssid_len = cred->ssid_len;
269         }
270
271         switch (cred->encr_type) {
272         case WPS_ENCR_NONE:
273                 break;
274         case WPS_ENCR_WEP:
275                 if (cred->key_len <= 0)
276                         break;
277                 if (cred->key_len != 5 && cred->key_len != 13 &&
278                     cred->key_len != 10 && cred->key_len != 26) {
279                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
280                                    "%lu", (unsigned long) cred->key_len);
281                         return -1;
282                 }
283                 if (cred->key_idx > NUM_WEP_KEYS) {
284                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
285                                    cred->key_idx);
286                         return -1;
287                 }
288                 if (cred->key_idx)
289                         key_idx = cred->key_idx - 1;
290                 if (cred->key_len == 10 || cred->key_len == 26) {
291                         if (hexstr2bin((char *) cred->key,
292                                        ssid->wep_key[key_idx],
293                                        cred->key_len / 2) < 0) {
294                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
295                                            "%d", key_idx);
296                                 return -1;
297                         }
298                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
299                 } else {
300                         os_memcpy(ssid->wep_key[key_idx], cred->key,
301                                   cred->key_len);
302                         ssid->wep_key_len[key_idx] = cred->key_len;
303                 }
304                 ssid->wep_tx_keyidx = key_idx;
305                 break;
306         case WPS_ENCR_TKIP:
307                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
308                 break;
309         case WPS_ENCR_AES:
310                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
311                 break;
312         }
313
314         switch (auth_type) {
315         case WPS_AUTH_OPEN:
316                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
317                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
318                 ssid->proto = 0;
319                 break;
320         case WPS_AUTH_SHARED:
321                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
322                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
323                 ssid->proto = 0;
324                 break;
325         case WPS_AUTH_WPAPSK:
326                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
327                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
328                 ssid->proto = WPA_PROTO_WPA;
329                 break;
330         case WPS_AUTH_WPA:
331                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
332                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
333                 ssid->proto = WPA_PROTO_WPA;
334                 break;
335         case WPS_AUTH_WPA2:
336                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
337                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
338                 ssid->proto = WPA_PROTO_RSN;
339                 break;
340         case WPS_AUTH_WPA2PSK:
341                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
342                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
343                 ssid->proto = WPA_PROTO_RSN;
344                 break;
345         }
346
347         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
348                 if (cred->key_len == 2 * PMK_LEN) {
349                         if (hexstr2bin((const char *) cred->key, ssid->psk,
350                                        PMK_LEN)) {
351                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
352                                            "Key");
353                                 return -1;
354                         }
355                         ssid->psk_set = 1;
356                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
357                         os_free(ssid->passphrase);
358                         ssid->passphrase = os_malloc(cred->key_len + 1);
359                         if (ssid->passphrase == NULL)
360                                 return -1;
361                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
362                         ssid->passphrase[cred->key_len] = '\0';
363                         wpa_config_update_psk(ssid);
364                 } else {
365                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
366                                    "length %lu",
367                                    (unsigned long) cred->key_len);
368                         return -1;
369                 }
370         }
371
372         wpas_wps_security_workaround(wpa_s, ssid, cred);
373
374 #ifndef CONFIG_NO_CONFIG_WRITE
375         if (wpa_s->conf->update_config &&
376             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
377                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
378                 return -1;
379         }
380 #endif /* CONFIG_NO_CONFIG_WRITE */
381
382         return 0;
383 }
384
385
386 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
387                                          struct wps_event_m2d *m2d)
388 {
389         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
390                 "dev_password_id=%d config_error=%d",
391                 m2d->dev_password_id, m2d->config_error);
392         wpas_notify_wps_event_m2d(wpa_s, m2d);
393 }
394
395
396 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
397                                           struct wps_event_fail *fail)
398 {
399         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
400         wpas_clear_wps(wpa_s);
401         wpas_notify_wps_event_fail(wpa_s, fail);
402 }
403
404
405 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
406 {
407         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
408         wpa_s->wps_success = 1;
409         wpas_notify_wps_event_success(wpa_s);
410 }
411
412
413 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
414                                                struct wps_event_er_ap *ap)
415 {
416         char uuid_str[100];
417         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
418         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s|%s|%s|%s|%s|%s|%s",
419                 uuid_str,
420                 ap->friendly_name ? ap->friendly_name : "",
421                 ap->manufacturer ? ap->manufacturer : "",
422                 ap->model_description ? ap->model_description : "",
423                 ap->model_name ? ap->model_name : "",
424                 ap->manufacturer_url ? ap->manufacturer_url : "",
425                 ap->model_url ? ap->model_url : "");
426 }
427
428
429 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
430                                                   struct wps_event_er_ap *ap)
431 {
432         char uuid_str[100];
433         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
434         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
435 }
436
437
438 static void wpa_supplicant_wps_event_er_enrollee_add(
439         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
440 {
441         char uuid_str[100];
442         char dev_type[20];
443
444         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
445         if (enrollee->pri_dev_type)
446                 os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
447                             WPA_GET_BE16(enrollee->pri_dev_type),
448                             WPA_GET_BE32(enrollee->pri_dev_type + 2),
449                             WPA_GET_BE16(enrollee->pri_dev_type + 6));
450         else
451                 dev_type[0] = '\0';
452
453         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
454                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
455                 "|%s|%s|%s|%s|%s|",
456                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
457                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
458                 enrollee->dev_name ? enrollee->dev_name : "",
459                 enrollee->manufacturer ? enrollee->manufacturer : "",
460                 enrollee->model_name ? enrollee->model_name : "",
461                 enrollee->model_number ? enrollee->model_number : "",
462                 enrollee->serial_number ? enrollee->serial_number : "");
463 }
464
465
466 static void wpa_supplicant_wps_event_er_enrollee_remove(
467         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
468 {
469         char uuid_str[100];
470         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
471         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
472                 uuid_str, MAC2STR(enrollee->mac_addr));
473 }
474
475
476 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
477                                      union wps_event_data *data)
478 {
479         struct wpa_supplicant *wpa_s = ctx;
480         switch (event) {
481         case WPS_EV_M2D:
482                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
483                 break;
484         case WPS_EV_FAIL:
485                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
486                 break;
487         case WPS_EV_SUCCESS:
488                 wpa_supplicant_wps_event_success(wpa_s);
489                 break;
490         case WPS_EV_PWD_AUTH_FAIL:
491                 break;
492         case WPS_EV_PBC_OVERLAP:
493                 break;
494         case WPS_EV_PBC_TIMEOUT:
495                 break;
496         case WPS_EV_ER_AP_ADD:
497                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
498                 break;
499         case WPS_EV_ER_AP_REMOVE:
500                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
501                 break;
502         case WPS_EV_ER_ENROLLEE_ADD:
503                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
504                                                          &data->enrollee);
505                 break;
506         case WPS_EV_ER_ENROLLEE_REMOVE:
507                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
508                                                             &data->enrollee);
509                 break;
510         }
511 }
512
513
514 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
515 {
516         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
517             eap_is_wps_pin_enrollee(&ssid->eap))
518                 return WPS_REQ_ENROLLEE;
519         else
520                 return WPS_REQ_REGISTRAR;
521 }
522
523
524 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
525 {
526         int id;
527         struct wpa_ssid *ssid, *remove_ssid = NULL;
528
529         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
530
531         /* Remove any existing WPS network from configuration */
532         ssid = wpa_s->conf->ssid;
533         while (ssid) {
534                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
535                         if (ssid == wpa_s->current_ssid) {
536                                 wpa_s->current_ssid = NULL;
537                                 if (ssid != NULL)
538                                         wpas_notify_network_changed(wpa_s);
539                         }
540                         id = ssid->id;
541                         remove_ssid = ssid;
542                 } else
543                         id = -1;
544                 ssid = ssid->next;
545                 if (id >= 0) {
546                         wpas_notify_network_removed(wpa_s, remove_ssid);
547                         wpa_config_remove_network(wpa_s->conf, id);
548                 }
549         }
550 }
551
552
553 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
554 {
555         struct wpa_supplicant *wpa_s = eloop_ctx;
556         wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
557                    "out");
558         wpas_clear_wps(wpa_s);
559 }
560
561
562 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
563                                               int registrar, const u8 *bssid)
564 {
565         struct wpa_ssid *ssid;
566
567         ssid = wpa_config_add_network(wpa_s->conf);
568         if (ssid == NULL)
569                 return NULL;
570         wpas_notify_network_added(wpa_s, ssid);
571         wpa_config_set_network_defaults(ssid);
572         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
573             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
574             wpa_config_set(ssid, "identity", registrar ?
575                            "\"" WSC_ID_REGISTRAR "\"" :
576                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
577                 wpas_notify_network_removed(wpa_s, ssid);
578                 wpa_config_remove_network(wpa_s->conf, ssid->id);
579                 return NULL;
580         }
581
582         if (bssid) {
583                 size_t i;
584                 struct wpa_scan_res *res;
585
586                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
587                 ssid->bssid_set = 1;
588
589                 /* Try to get SSID from scan results */
590                 if (wpa_s->scan_res == NULL &&
591                     wpa_supplicant_get_scan_results(wpa_s) < 0)
592                         return ssid; /* Could not find any scan results */
593
594                 for (i = 0; i < wpa_s->scan_res->num; i++) {
595                         const u8 *ie;
596
597                         res = wpa_s->scan_res->res[i];
598                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
599                                 continue;
600
601                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
602                         if (ie == NULL)
603                                 break;
604                         os_free(ssid->ssid);
605                         ssid->ssid = os_malloc(ie[1]);
606                         if (ssid->ssid == NULL)
607                                 break;
608                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
609                         ssid->ssid_len = ie[1];
610                         break;
611                 }
612         }
613
614         return ssid;
615 }
616
617
618 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
619                              struct wpa_ssid *selected)
620 {
621         struct wpa_ssid *ssid;
622
623         /* Mark all other networks disabled and trigger reassociation */
624         ssid = wpa_s->conf->ssid;
625         while (ssid) {
626                 int was_disabled = ssid->disabled;
627                 ssid->disabled = ssid != selected;
628                 if (was_disabled != ssid->disabled)
629                         wpas_notify_network_enabled_changed(wpa_s, ssid);
630                 ssid = ssid->next;
631         }
632         wpa_s->disconnected = 0;
633         wpa_s->reassociate = 1;
634         wpa_s->scan_runs = 0;
635         wpa_s->wps_success = 0;
636         wpa_s->blacklist_cleared = 0;
637         wpa_supplicant_req_scan(wpa_s, 0, 0);
638 }
639
640
641 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
642 {
643         struct wpa_ssid *ssid;
644         wpas_clear_wps(wpa_s);
645         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
646         if (ssid == NULL)
647                 return -1;
648         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
649         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
650                                wpa_s, NULL);
651         wpas_wps_reassoc(wpa_s, ssid);
652         return 0;
653 }
654
655
656 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
657                        const char *pin)
658 {
659         struct wpa_ssid *ssid;
660         char val[128];
661         unsigned int rpin = 0;
662
663         wpas_clear_wps(wpa_s);
664         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
665         if (ssid == NULL)
666                 return -1;
667         if (pin)
668                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
669         else {
670                 rpin = wps_generate_pin();
671                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
672         }
673         wpa_config_set(ssid, "phase1", val, 0);
674         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
675                                wpa_s, NULL);
676         wpas_wps_reassoc(wpa_s, ssid);
677         return rpin;
678 }
679
680
681 #ifdef CONFIG_WPS_OOB
682 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
683                        char *path, char *method, char *name)
684 {
685         struct wps_context *wps = wpa_s->wps;
686         struct oob_device_data *oob_dev;
687
688         oob_dev = wps_get_oob_device(device_type);
689         if (oob_dev == NULL)
690                 return -1;
691         oob_dev->device_path = path;
692         oob_dev->device_name = name;
693         wps->oob_conf.oob_method = wps_get_oob_method(method);
694
695         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
696                 /*
697                  * Use pre-configured DH keys in order to be able to write the
698                  * key hash into the OOB file.
699                  */
700                 wpabuf_free(wps->dh_pubkey);
701                 wpabuf_free(wps->dh_privkey);
702                 wps->dh_privkey = NULL;
703                 wps->dh_pubkey = NULL;
704                 dh5_free(wps->dh_ctx);
705                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
706                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
707                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
708                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
709                                    "Diffie-Hellman handshake");
710                         return -1;
711                 }
712         }
713
714         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
715                 wpas_clear_wps(wpa_s);
716
717         if (wps_process_oob(wps, oob_dev, 0) < 0)
718                 return -1;
719
720         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
721              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
722             wpas_wps_start_pin(wpa_s, NULL,
723                                wpabuf_head(wps->oob_conf.dev_password)) < 0)
724                         return -1;
725
726         return 0;
727 }
728 #endif /* CONFIG_WPS_OOB */
729
730
731 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
732                        const char *pin, struct wps_new_ap_settings *settings)
733 {
734         struct wpa_ssid *ssid;
735         char val[200];
736         char *pos, *end;
737         int res;
738
739         if (!pin)
740                 return -1;
741         wpas_clear_wps(wpa_s);
742         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
743         if (ssid == NULL)
744                 return -1;
745         pos = val;
746         end = pos + sizeof(val);
747         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
748         if (res < 0 || res >= end - pos)
749                 return -1;
750         pos += res;
751         if (settings) {
752                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
753                                   "new_encr=%s new_key=%s",
754                                   settings->ssid_hex, settings->auth,
755                                   settings->encr, settings->key_hex);
756                 if (res < 0 || res >= end - pos)
757                         return -1;
758                 pos += res;
759         }
760         res = os_snprintf(pos, end - pos, "\"");
761         if (res < 0 || res >= end - pos)
762                 return -1;
763         wpa_config_set(ssid, "phase1", val, 0);
764         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
765                                wpa_s, NULL);
766         wpas_wps_reassoc(wpa_s, ssid);
767         return 0;
768 }
769
770
771 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
772                                size_t psk_len)
773 {
774         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
775                    "STA " MACSTR, MAC2STR(mac_addr));
776         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
777
778         /* TODO */
779
780         return 0;
781 }
782
783
784 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
785                                    const struct wps_device_data *dev)
786 {
787         char uuid[40], txt[400];
788         int len;
789         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
790                 return;
791         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
792         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
793                           " [%s|%s|%s|%s|%s|%d-%08X-%d]",
794                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
795                           dev->manufacturer, dev->model_name,
796                           dev->model_number, dev->serial_number,
797                           dev->categ, dev->oui, dev->sub_categ);
798         if (len > 0 && len < (int) sizeof(txt))
799                 wpa_printf(MSG_INFO, "%s", txt);
800 }
801
802
803 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
804                                     u16 sel_reg_config_methods)
805 {
806 #ifdef CONFIG_WPS_ER
807         struct wpa_supplicant *wpa_s = ctx;
808
809         if (wpa_s->wps_er == NULL)
810                 return;
811         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
812                            sel_reg_config_methods);
813 #endif /* CONFIG_WPS_ER */
814 }
815
816
817 int wpas_wps_init(struct wpa_supplicant *wpa_s)
818 {
819         struct wps_context *wps;
820         struct wps_registrar_config rcfg;
821
822         wps = os_zalloc(sizeof(*wps));
823         if (wps == NULL)
824                 return -1;
825
826         wps->cred_cb = wpa_supplicant_wps_cred;
827         wps->event_cb = wpa_supplicant_wps_event;
828         wps->cb_ctx = wpa_s;
829
830         wps->dev.device_name = wpa_s->conf->device_name;
831         wps->dev.manufacturer = wpa_s->conf->manufacturer;
832         wps->dev.model_name = wpa_s->conf->model_name;
833         wps->dev.model_number = wpa_s->conf->model_number;
834         wps->dev.serial_number = wpa_s->conf->serial_number;
835         if (wpa_s->conf->device_type) {
836                 char *pos;
837                 u8 oui[4];
838                 /* <categ>-<OUI>-<subcateg> */
839                 wps->dev.categ = atoi(wpa_s->conf->device_type);
840                 pos = os_strchr(wpa_s->conf->device_type, '-');
841                 if (pos == NULL) {
842                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
843                         os_free(wps);
844                         return -1;
845                 }
846                 pos++;
847                 if (hexstr2bin(pos, oui, 4)) {
848                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
849                         os_free(wps);
850                         return -1;
851                 }
852                 wps->dev.oui = WPA_GET_BE32(oui);
853                 pos = os_strchr(pos, '-');
854                 if (pos == NULL) {
855                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
856                         os_free(wps);
857                         return -1;
858                 }
859                 pos++;
860                 wps->dev.sub_categ = atoi(pos);
861         }
862         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
863         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
864         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
865         if (is_nil_uuid(wpa_s->conf->uuid)) {
866                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
867                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
868                             wps->uuid, WPS_UUID_LEN);
869         } else
870                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
871
872         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
873         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
874
875         os_memset(&rcfg, 0, sizeof(rcfg));
876         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
877         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
878         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
879         rcfg.cb_ctx = wpa_s;
880
881         wps->registrar = wps_registrar_init(wps, &rcfg);
882         if (wps->registrar == NULL) {
883                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
884                 os_free(wps);
885                 return -1;
886         }
887
888         wpa_s->wps = wps;
889
890         return 0;
891 }
892
893
894 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
895 {
896         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
897
898         if (wpa_s->wps == NULL)
899                 return;
900
901         wps_registrar_deinit(wpa_s->wps->registrar);
902         wpabuf_free(wpa_s->wps->dh_pubkey);
903         wpabuf_free(wpa_s->wps->dh_privkey);
904         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
905         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
906         os_free(wpa_s->wps->network_key);
907         os_free(wpa_s->wps);
908         wpa_s->wps = NULL;
909
910 #ifdef CONFIG_WPS_ER
911         wps_er_deinit(wpa_s->wps_er);
912         wpa_s->wps_er = NULL;
913 #endif /* CONFIG_WPS_ER */
914 }
915
916
917 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
918                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
919 {
920         struct wpabuf *wps_ie;
921
922         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
923                 return -1;
924
925         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
926         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
927                 if (!wps_ie) {
928                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
929                         return 0;
930                 }
931
932                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
933                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
934                                    "without active PBC Registrar");
935                         wpabuf_free(wps_ie);
936                         return 0;
937                 }
938
939                 /* TODO: overlap detection */
940                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
941                            "(Active PBC)");
942                 wpabuf_free(wps_ie);
943                 return 1;
944         }
945
946         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
947                 if (!wps_ie) {
948                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
949                         return 0;
950                 }
951
952                 /*
953                  * Start with WPS APs that advertise active PIN Registrar and
954                  * allow any WPS AP after third scan since some APs do not set
955                  * Selected Registrar attribute properly when using external
956                  * Registrar.
957                  */
958                 if (!wps_is_selected_pin_registrar(wps_ie)) {
959                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
960                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
961                                            "without active PIN Registrar");
962                                 wpabuf_free(wps_ie);
963                                 return 0;
964                         }
965                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
966                 } else {
967                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
968                                    "(Active PIN)");
969                 }
970                 wpabuf_free(wps_ie);
971                 return 1;
972         }
973
974         if (wps_ie) {
975                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
976                 wpabuf_free(wps_ie);
977                 return 1;
978         }
979
980         return -1;
981 }
982
983
984 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
985                               struct wpa_ssid *ssid,
986                               struct wpa_scan_res *bss)
987 {
988         struct wpabuf *wps_ie = NULL;
989         int ret = 0;
990
991         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
992                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
993                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
994                         /* allow wildcard SSID for WPS PBC */
995                         ret = 1;
996                 }
997         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
998                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
999                 if (wps_ie &&
1000                     (wps_is_selected_pin_registrar(wps_ie) ||
1001                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1002                         /* allow wildcard SSID for WPS PIN */
1003                         ret = 1;
1004                 }
1005         }
1006
1007         if (!ret && ssid->bssid_set &&
1008             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1009                 /* allow wildcard SSID due to hardcoded BSSID match */
1010                 ret = 1;
1011         }
1012
1013         wpabuf_free(wps_ie);
1014
1015         return ret;
1016 }
1017
1018
1019 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1020                               struct wpa_scan_res *selected,
1021                               struct wpa_ssid *ssid)
1022 {
1023         const u8 *sel_uuid, *uuid;
1024         size_t i;
1025         struct wpabuf *wps_ie;
1026         int ret = 0;
1027
1028         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1029                 return 0;
1030
1031         /* Make sure that only one AP is in active PBC mode */
1032         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1033         if (wps_ie)
1034                 sel_uuid = wps_get_uuid_e(wps_ie);
1035         else
1036                 sel_uuid = NULL;
1037
1038         for (i = 0; i < wpa_s->scan_res->num; i++) {
1039                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1040                 struct wpabuf *ie;
1041                 if (bss == selected)
1042                         continue;
1043                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1044                 if (!ie)
1045                         continue;
1046                 if (!wps_is_selected_pbc_registrar(ie)) {
1047                         wpabuf_free(ie);
1048                         continue;
1049                 }
1050                 uuid = wps_get_uuid_e(ie);
1051                 if (sel_uuid == NULL || uuid == NULL ||
1052                     os_memcmp(sel_uuid, uuid, 16) != 0) {
1053                         ret = 1; /* PBC overlap */
1054                         wpabuf_free(ie);
1055                         break;
1056                 }
1057
1058                 /* TODO: verify that this is reasonable dual-band situation */
1059
1060                 wpabuf_free(ie);
1061         }
1062
1063         wpabuf_free(wps_ie);
1064
1065         return ret;
1066 }
1067
1068
1069 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1070 {
1071         size_t i;
1072
1073         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1074                 return;
1075
1076         for (i = 0; i < wpa_s->scan_res->num; i++) {
1077                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1078                 struct wpabuf *ie;
1079                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1080                 if (!ie)
1081                         continue;
1082                 if (wps_is_selected_pbc_registrar(ie))
1083                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1084                 else if (wps_is_selected_pin_registrar(ie))
1085                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1086                 else
1087                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1088                 wpabuf_free(ie);
1089                 break;
1090         }
1091 }
1092
1093
1094 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1095 {
1096         struct wpa_ssid *ssid;
1097
1098         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1099                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1100                         return 1;
1101         }
1102
1103         return 0;
1104 }
1105
1106
1107 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1108                               char *end)
1109 {
1110         struct wpabuf *wps_ie;
1111         int ret;
1112
1113         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1114         if (wps_ie == NULL)
1115                 return 0;
1116
1117         ret = wps_attr_text(wps_ie, buf, end);
1118         wpabuf_free(wps_ie);
1119         return ret;
1120 }
1121
1122
1123 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
1124 {
1125 #ifdef CONFIG_WPS_ER
1126         if (wpa_s->wps_er) {
1127                 /* TODO: re-send ctrl_iface events for current data? */
1128                 return 0;
1129         }
1130         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
1131         if (wpa_s->wps_er == NULL)
1132                 return -1;
1133         return 0;
1134 #else /* CONFIG_WPS_ER */
1135         return 0;
1136 #endif /* CONFIG_WPS_ER */
1137 }
1138
1139
1140 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1141 {
1142 #ifdef CONFIG_WPS_ER
1143         wps_er_deinit(wpa_s->wps_er);
1144         wpa_s->wps_er = NULL;
1145 #endif /* CONFIG_WPS_ER */
1146         return 0;
1147 }
1148
1149
1150 #ifdef CONFIG_WPS_ER
1151 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
1152                         const char *pin)
1153 {
1154         u8 u[UUID_LEN];
1155         int any = 0;
1156
1157         if (os_strcmp(uuid, "any") == 0)
1158                 any = 1;
1159         else if (uuid_str2bin(uuid, u))
1160                 return -1;
1161         return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
1162                                      (const u8 *) pin, os_strlen(pin), 300);
1163 }
1164 #endif /* CONFIG_WPS_ER */