00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "includes.h"
00016
00017 #include "common.h"
00018 #include "eloop.h"
00019 #include "uuid.h"
00020 #include "crypto/dh_group5.h"
00021 #include "common/ieee802_11_defs.h"
00022 #include "common/ieee802_11_common.h"
00023 #include "common/wpa_common.h"
00024 #include "common/wpa_ctrl.h"
00025 #include "eap_common/eap_wsc_common.h"
00026 #include "eap_peer/eap.h"
00027 #include "rsn_supp/wpa.h"
00028 #include "config.h"
00029 #include "wpa_supplicant_i.h"
00030 #include "driver_i.h"
00031 #include "notify.h"
00032 #include "blacklist.h"
00033 #include "bss.h"
00034 #include "scan.h"
00035 #include "wps_supplicant.h"
00036
00037
00038 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
00039
00040 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
00041 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
00042
00043
00044 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
00045 {
00046 if (!wpa_s->wps_success &&
00047 wpa_s->current_ssid &&
00048 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
00049 const u8 *bssid = wpa_s->bssid;
00050 if (is_zero_ether_addr(bssid))
00051 bssid = wpa_s->pending_bssid;
00052
00053 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
00054 " did not succeed - continue trying to find "
00055 "suitable AP", MAC2STR(bssid));
00056 wpa_blacklist_add(wpa_s, bssid);
00057
00058 wpa_supplicant_deauthenticate(wpa_s,
00059 WLAN_REASON_DEAUTH_LEAVING);
00060 wpa_s->reassociate = 1;
00061 wpa_supplicant_req_scan(wpa_s,
00062 wpa_s->blacklist_cleared ? 5 : 0, 0);
00063 wpa_s->blacklist_cleared = 0;
00064 return 1;
00065 }
00066
00067 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
00068
00069 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
00070 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
00071 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
00072 "try to associate with the received credential");
00073 wpa_supplicant_deauthenticate(wpa_s,
00074 WLAN_REASON_DEAUTH_LEAVING);
00075 wpa_s->after_wps = 5;
00076 wpa_s->wps_freq = wpa_s->assoc_freq;
00077 wpa_s->reassociate = 1;
00078 wpa_supplicant_req_scan(wpa_s, 0, 0);
00079 return 1;
00080 }
00081
00082 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
00083 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
00084 "for external credential processing");
00085 wpas_clear_wps(wpa_s);
00086 wpa_supplicant_deauthenticate(wpa_s,
00087 WLAN_REASON_DEAUTH_LEAVING);
00088 return 1;
00089 }
00090
00091 return 0;
00092 }
00093
00094
00095 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
00096 struct wpa_ssid *ssid,
00097 const struct wps_credential *cred)
00098 {
00099 struct wpa_driver_capa capa;
00100 struct wpa_bss *bss;
00101 const u8 *ie;
00102 struct wpa_ie_data adv;
00103 int wpa2 = 0, ccmp = 0;
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 if (wpa_drv_get_capa(wpa_s, &capa))
00114 return;
00115
00116 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
00117 if (bss == NULL) {
00118 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
00119 "table - use credential as-is");
00120 return;
00121 }
00122
00123 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
00124
00125 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
00126 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
00127 wpa2 = 1;
00128 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
00129 ccmp = 1;
00130 } else {
00131 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
00132 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
00133 adv.pairwise_cipher & WPA_CIPHER_CCMP)
00134 ccmp = 1;
00135 }
00136
00137 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
00138 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
00139
00140
00141
00142
00143
00144
00145 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
00146 "support - use credential as-is");
00147 return;
00148 }
00149
00150 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
00151 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
00152 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
00153 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
00154 "based on scan results");
00155 if (wpa_s->conf->ap_scan == 1)
00156 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
00157 else
00158 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
00159 }
00160
00161 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
00162 (ssid->proto & WPA_PROTO_WPA) &&
00163 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
00164 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
00165 "based on scan results");
00166 if (wpa_s->conf->ap_scan == 1)
00167 ssid->proto |= WPA_PROTO_RSN;
00168 else
00169 ssid->proto = WPA_PROTO_RSN;
00170 }
00171 }
00172
00173
00174 static int wpa_supplicant_wps_cred(void *ctx,
00175 const struct wps_credential *cred)
00176 {
00177 struct wpa_supplicant *wpa_s = ctx;
00178 struct wpa_ssid *ssid = wpa_s->current_ssid;
00179 u8 key_idx = 0;
00180 u16 auth_type;
00181
00182 if ((wpa_s->conf->wps_cred_processing == 1 ||
00183 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
00184 size_t blen = cred->cred_attr_len * 2 + 1;
00185 char *buf = os_malloc(blen);
00186 if (buf) {
00187 wpa_snprintf_hex(buf, blen,
00188 cred->cred_attr, cred->cred_attr_len);
00189 wpa_msg(wpa_s, MSG_INFO, "%s%s",
00190 WPS_EVENT_CRED_RECEIVED, buf);
00191 os_free(buf);
00192 }
00193
00194 wpas_notify_wps_credential(wpa_s, cred);
00195 } else
00196 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
00197
00198 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
00199 cred->cred_attr, cred->cred_attr_len);
00200
00201 if (wpa_s->conf->wps_cred_processing == 1)
00202 return 0;
00203
00204 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
00205 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
00206 cred->auth_type);
00207 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
00208 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
00209 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
00210 cred->key, cred->key_len);
00211 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
00212 MAC2STR(cred->mac_addr));
00213
00214 auth_type = cred->auth_type;
00215 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
00216 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
00217 "auth_type into WPA2PSK");
00218 auth_type = WPS_AUTH_WPA2PSK;
00219 }
00220
00221 if (auth_type != WPS_AUTH_OPEN &&
00222 auth_type != WPS_AUTH_SHARED &&
00223 auth_type != WPS_AUTH_WPAPSK &&
00224 auth_type != WPS_AUTH_WPA2PSK) {
00225 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
00226 "unsupported authentication type 0x%x",
00227 auth_type);
00228 return 0;
00229 }
00230
00231 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
00232 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
00233 "on the received credential");
00234 os_free(ssid->eap.identity);
00235 ssid->eap.identity = NULL;
00236 ssid->eap.identity_len = 0;
00237 os_free(ssid->eap.phase1);
00238 ssid->eap.phase1 = NULL;
00239 os_free(ssid->eap.eap_methods);
00240 ssid->eap.eap_methods = NULL;
00241 } else {
00242 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
00243 "received credential");
00244 ssid = wpa_config_add_network(wpa_s->conf);
00245 if (ssid == NULL)
00246 return -1;
00247 wpas_notify_network_added(wpa_s, ssid);
00248 }
00249
00250 wpa_config_set_network_defaults(ssid);
00251
00252 os_free(ssid->ssid);
00253 ssid->ssid = os_malloc(cred->ssid_len);
00254 if (ssid->ssid) {
00255 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
00256 ssid->ssid_len = cred->ssid_len;
00257 }
00258
00259 switch (cred->encr_type) {
00260 case WPS_ENCR_NONE:
00261 break;
00262 case WPS_ENCR_WEP:
00263 if (cred->key_len <= 0)
00264 break;
00265 if (cred->key_len != 5 && cred->key_len != 13 &&
00266 cred->key_len != 10 && cred->key_len != 26) {
00267 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
00268 "%lu", (unsigned long) cred->key_len);
00269 return -1;
00270 }
00271 if (cred->key_idx > NUM_WEP_KEYS) {
00272 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
00273 cred->key_idx);
00274 return -1;
00275 }
00276 if (cred->key_idx)
00277 key_idx = cred->key_idx - 1;
00278 if (cred->key_len == 10 || cred->key_len == 26) {
00279 if (hexstr2bin((char *) cred->key,
00280 ssid->wep_key[key_idx],
00281 cred->key_len / 2) < 0) {
00282 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
00283 "%d", key_idx);
00284 return -1;
00285 }
00286 ssid->wep_key_len[key_idx] = cred->key_len / 2;
00287 } else {
00288 os_memcpy(ssid->wep_key[key_idx], cred->key,
00289 cred->key_len);
00290 ssid->wep_key_len[key_idx] = cred->key_len;
00291 }
00292 ssid->wep_tx_keyidx = key_idx;
00293 break;
00294 case WPS_ENCR_TKIP:
00295 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
00296 break;
00297 case WPS_ENCR_AES:
00298 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
00299 break;
00300 }
00301
00302 switch (auth_type) {
00303 case WPS_AUTH_OPEN:
00304 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
00305 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
00306 ssid->proto = 0;
00307 break;
00308 case WPS_AUTH_SHARED:
00309 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
00310 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
00311 ssid->proto = 0;
00312 break;
00313 case WPS_AUTH_WPAPSK:
00314 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
00315 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
00316 ssid->proto = WPA_PROTO_WPA;
00317 break;
00318 case WPS_AUTH_WPA:
00319 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
00320 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
00321 ssid->proto = WPA_PROTO_WPA;
00322 break;
00323 case WPS_AUTH_WPA2:
00324 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
00325 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
00326 ssid->proto = WPA_PROTO_RSN;
00327 break;
00328 case WPS_AUTH_WPA2PSK:
00329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
00330 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
00331 ssid->proto = WPA_PROTO_RSN;
00332 break;
00333 }
00334
00335 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
00336 if (cred->key_len == 2 * PMK_LEN) {
00337 if (hexstr2bin((const char *) cred->key, ssid->psk,
00338 PMK_LEN)) {
00339 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
00340 "Key");
00341 return -1;
00342 }
00343 ssid->psk_set = 1;
00344 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
00345 os_free(ssid->passphrase);
00346 ssid->passphrase = os_malloc(cred->key_len + 1);
00347 if (ssid->passphrase == NULL)
00348 return -1;
00349 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
00350 ssid->passphrase[cred->key_len] = '\0';
00351 wpa_config_update_psk(ssid);
00352 } else {
00353 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
00354 "length %lu",
00355 (unsigned long) cred->key_len);
00356 return -1;
00357 }
00358 }
00359
00360 wpas_wps_security_workaround(wpa_s, ssid, cred);
00361
00362 #ifndef CONFIG_NO_CONFIG_WRITE
00363 if (wpa_s->conf->update_config &&
00364 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
00365 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
00366 return -1;
00367 }
00368 #endif
00369
00370 return 0;
00371 }
00372
00373
00374 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
00375 struct wps_event_m2d *m2d)
00376 {
00377 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
00378 "dev_password_id=%d config_error=%d",
00379 m2d->dev_password_id, m2d->config_error);
00380 wpas_notify_wps_event_m2d(wpa_s, m2d);
00381 }
00382
00383
00384 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
00385 struct wps_event_fail *fail)
00386 {
00387 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
00388 wpas_clear_wps(wpa_s);
00389 wpas_notify_wps_event_fail(wpa_s, fail);
00390 }
00391
00392
00393 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
00394 {
00395 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
00396 wpa_s->wps_success = 1;
00397 wpas_notify_wps_event_success(wpa_s);
00398 }
00399
00400
00401 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
00402 struct wps_event_er_ap *ap)
00403 {
00404 char uuid_str[100];
00405 char dev_type[WPS_DEV_TYPE_BUFSIZE];
00406
00407 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
00408 if (ap->pri_dev_type)
00409 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
00410 sizeof(dev_type));
00411 else
00412 dev_type[0] = '\0';
00413
00414 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
00415 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
00416 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
00417 ap->friendly_name ? ap->friendly_name : "",
00418 ap->manufacturer ? ap->manufacturer : "",
00419 ap->model_description ? ap->model_description : "",
00420 ap->model_name ? ap->model_name : "",
00421 ap->manufacturer_url ? ap->manufacturer_url : "",
00422 ap->model_url ? ap->model_url : "");
00423 }
00424
00425
00426 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
00427 struct wps_event_er_ap *ap)
00428 {
00429 char uuid_str[100];
00430 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
00431 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
00432 }
00433
00434
00435 static void wpa_supplicant_wps_event_er_enrollee_add(
00436 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
00437 {
00438 char uuid_str[100];
00439 char dev_type[WPS_DEV_TYPE_BUFSIZE];
00440
00441 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
00442 if (enrollee->pri_dev_type)
00443 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
00444 sizeof(dev_type));
00445 else
00446 dev_type[0] = '\0';
00447
00448 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
00449 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
00450 "|%s|%s|%s|%s|%s|",
00451 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
00452 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
00453 enrollee->dev_name ? enrollee->dev_name : "",
00454 enrollee->manufacturer ? enrollee->manufacturer : "",
00455 enrollee->model_name ? enrollee->model_name : "",
00456 enrollee->model_number ? enrollee->model_number : "",
00457 enrollee->serial_number ? enrollee->serial_number : "");
00458 }
00459
00460
00461 static void wpa_supplicant_wps_event_er_enrollee_remove(
00462 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
00463 {
00464 char uuid_str[100];
00465 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
00466 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
00467 uuid_str, MAC2STR(enrollee->mac_addr));
00468 }
00469
00470
00471 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
00472 union wps_event_data *data)
00473 {
00474 struct wpa_supplicant *wpa_s = ctx;
00475 switch (event) {
00476 case WPS_EV_M2D:
00477 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
00478 break;
00479 case WPS_EV_FAIL:
00480 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
00481 break;
00482 case WPS_EV_SUCCESS:
00483 wpa_supplicant_wps_event_success(wpa_s);
00484 break;
00485 case WPS_EV_PWD_AUTH_FAIL:
00486 break;
00487 case WPS_EV_PBC_OVERLAP:
00488 break;
00489 case WPS_EV_PBC_TIMEOUT:
00490 break;
00491 case WPS_EV_ER_AP_ADD:
00492 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
00493 break;
00494 case WPS_EV_ER_AP_REMOVE:
00495 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
00496 break;
00497 case WPS_EV_ER_ENROLLEE_ADD:
00498 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
00499 &data->enrollee);
00500 break;
00501 case WPS_EV_ER_ENROLLEE_REMOVE:
00502 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
00503 &data->enrollee);
00504 break;
00505 }
00506 }
00507
00508
00509 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
00510 {
00511 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
00512 eap_is_wps_pin_enrollee(&ssid->eap))
00513 return WPS_REQ_ENROLLEE;
00514 else
00515 return WPS_REQ_REGISTRAR;
00516 }
00517
00518
00519 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
00520 {
00521 int id;
00522 struct wpa_ssid *ssid, *remove_ssid = NULL;
00523
00524 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
00525
00526
00527 ssid = wpa_s->conf->ssid;
00528 while (ssid) {
00529 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
00530 if (ssid == wpa_s->current_ssid) {
00531 wpa_s->current_ssid = NULL;
00532 if (ssid != NULL)
00533 wpas_notify_network_changed(wpa_s);
00534 }
00535 id = ssid->id;
00536 remove_ssid = ssid;
00537 } else
00538 id = -1;
00539 ssid = ssid->next;
00540 if (id >= 0) {
00541 wpas_notify_network_removed(wpa_s, remove_ssid);
00542 wpa_config_remove_network(wpa_s->conf, id);
00543 }
00544 }
00545 }
00546
00547
00548 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
00549 {
00550 struct wpa_supplicant *wpa_s = eloop_ctx;
00551 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
00552 "out");
00553 wpas_clear_wps(wpa_s);
00554 }
00555
00556
00557 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
00558 int registrar, const u8 *bssid)
00559 {
00560 struct wpa_ssid *ssid;
00561
00562 ssid = wpa_config_add_network(wpa_s->conf);
00563 if (ssid == NULL)
00564 return NULL;
00565 wpas_notify_network_added(wpa_s, ssid);
00566 wpa_config_set_network_defaults(ssid);
00567 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
00568 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
00569 wpa_config_set(ssid, "identity", registrar ?
00570 "\"" WSC_ID_REGISTRAR "\"" :
00571 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
00572 wpas_notify_network_removed(wpa_s, ssid);
00573 wpa_config_remove_network(wpa_s->conf, ssid->id);
00574 return NULL;
00575 }
00576
00577 if (bssid) {
00578 struct wpa_bss *bss;
00579 int count = 0;
00580
00581 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
00582 ssid->bssid_set = 1;
00583
00584 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
00585 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
00586 continue;
00587
00588 os_free(ssid->ssid);
00589 ssid->ssid = os_malloc(bss->ssid_len);
00590 if (ssid->ssid == NULL)
00591 break;
00592 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
00593 ssid->ssid_len = bss->ssid_len;
00594 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
00595 "scan results",
00596 ssid->ssid, ssid->ssid_len);
00597 count++;
00598 }
00599
00600 if (count > 1) {
00601 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
00602 "for the AP; use wildcard");
00603 os_free(ssid->ssid);
00604 ssid->ssid = NULL;
00605 ssid->ssid_len = 0;
00606 }
00607 }
00608
00609 return ssid;
00610 }
00611
00612
00613 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
00614 struct wpa_ssid *selected)
00615 {
00616 struct wpa_ssid *ssid;
00617
00618
00619 ssid = wpa_s->conf->ssid;
00620 while (ssid) {
00621 int was_disabled = ssid->disabled;
00622 ssid->disabled = ssid != selected;
00623 if (was_disabled != ssid->disabled)
00624 wpas_notify_network_enabled_changed(wpa_s, ssid);
00625 ssid = ssid->next;
00626 }
00627 wpa_s->disconnected = 0;
00628 wpa_s->reassociate = 1;
00629 wpa_s->scan_runs = 0;
00630 wpa_s->wps_success = 0;
00631 wpa_s->blacklist_cleared = 0;
00632 wpa_supplicant_req_scan(wpa_s, 0, 0);
00633 }
00634
00635
00636 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
00637 {
00638 struct wpa_ssid *ssid;
00639 wpas_clear_wps(wpa_s);
00640 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
00641 if (ssid == NULL)
00642 return -1;
00643 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
00644 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
00645 wpa_s, NULL);
00646 wpas_wps_reassoc(wpa_s, ssid);
00647 return 0;
00648 }
00649
00650
00651 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
00652 const char *pin)
00653 {
00654 struct wpa_ssid *ssid;
00655 char val[128];
00656 unsigned int rpin = 0;
00657
00658 wpas_clear_wps(wpa_s);
00659 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
00660 if (ssid == NULL)
00661 return -1;
00662 if (pin)
00663 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
00664 else {
00665 rpin = wps_generate_pin();
00666 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
00667 }
00668 wpa_config_set(ssid, "phase1", val, 0);
00669 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
00670 wpa_s, NULL);
00671 wpas_wps_reassoc(wpa_s, ssid);
00672 return rpin;
00673 }
00674
00675
00676 #ifdef CONFIG_WPS_OOB
00677 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
00678 char *path, char *method, char *name)
00679 {
00680 struct wps_context *wps = wpa_s->wps;
00681 struct oob_device_data *oob_dev;
00682
00683 oob_dev = wps_get_oob_device(device_type);
00684 if (oob_dev == NULL)
00685 return -1;
00686 oob_dev->device_path = path;
00687 oob_dev->device_name = name;
00688 wps->oob_conf.oob_method = wps_get_oob_method(method);
00689
00690 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
00691
00692
00693
00694
00695 wpabuf_free(wps->dh_pubkey);
00696 wpabuf_free(wps->dh_privkey);
00697 wps->dh_privkey = NULL;
00698 wps->dh_pubkey = NULL;
00699 dh5_free(wps->dh_ctx);
00700 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
00701 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
00702 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
00703 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
00704 "Diffie-Hellman handshake");
00705 return -1;
00706 }
00707 }
00708
00709 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
00710 wpas_clear_wps(wpa_s);
00711
00712 if (wps_process_oob(wps, oob_dev, 0) < 0)
00713 return -1;
00714
00715 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
00716 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
00717 wpas_wps_start_pin(wpa_s, NULL,
00718 wpabuf_head(wps->oob_conf.dev_password)) < 0)
00719 return -1;
00720
00721 return 0;
00722 }
00723 #endif
00724
00725
00726 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
00727 const char *pin, struct wps_new_ap_settings *settings)
00728 {
00729 struct wpa_ssid *ssid;
00730 char val[200];
00731 char *pos, *end;
00732 int res;
00733
00734 if (!pin)
00735 return -1;
00736 wpas_clear_wps(wpa_s);
00737 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
00738 if (ssid == NULL)
00739 return -1;
00740 pos = val;
00741 end = pos + sizeof(val);
00742 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
00743 if (res < 0 || res >= end - pos)
00744 return -1;
00745 pos += res;
00746 if (settings) {
00747 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
00748 "new_encr=%s new_key=%s",
00749 settings->ssid_hex, settings->auth,
00750 settings->encr, settings->key_hex);
00751 if (res < 0 || res >= end - pos)
00752 return -1;
00753 pos += res;
00754 }
00755 res = os_snprintf(pos, end - pos, "\"");
00756 if (res < 0 || res >= end - pos)
00757 return -1;
00758 wpa_config_set(ssid, "phase1", val, 0);
00759 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
00760 wpa_s, NULL);
00761 wpas_wps_reassoc(wpa_s, ssid);
00762 return 0;
00763 }
00764
00765
00766 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
00767 size_t psk_len)
00768 {
00769 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
00770 "STA " MACSTR, MAC2STR(mac_addr));
00771 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
00772
00773
00774
00775 return 0;
00776 }
00777
00778
00779 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
00780 const struct wps_device_data *dev)
00781 {
00782 char uuid[40], txt[400];
00783 int len;
00784 char devtype[WPS_DEV_TYPE_BUFSIZE];
00785 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
00786 return;
00787 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
00788 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
00789 " [%s|%s|%s|%s|%s|%s]",
00790 uuid, MAC2STR(dev->mac_addr), dev->device_name,
00791 dev->manufacturer, dev->model_name,
00792 dev->model_number, dev->serial_number,
00793 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
00794 sizeof(devtype)));
00795 if (len > 0 && len < (int) sizeof(txt))
00796 wpa_printf(MSG_INFO, "%s", txt);
00797 }
00798
00799
00800 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
00801 u16 sel_reg_config_methods)
00802 {
00803 #ifdef CONFIG_WPS_ER
00804 struct wpa_supplicant *wpa_s = ctx;
00805
00806 if (wpa_s->wps_er == NULL)
00807 return;
00808 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
00809 sel_reg_config_methods);
00810 #endif
00811 }
00812
00813
00814 int wpas_wps_init(struct wpa_supplicant *wpa_s)
00815 {
00816 struct wps_context *wps;
00817 struct wps_registrar_config rcfg;
00818
00819 wps = os_zalloc(sizeof(*wps));
00820 if (wps == NULL)
00821 return -1;
00822
00823 wps->cred_cb = wpa_supplicant_wps_cred;
00824 wps->event_cb = wpa_supplicant_wps_event;
00825 wps->cb_ctx = wpa_s;
00826
00827 wps->dev.device_name = wpa_s->conf->device_name;
00828 wps->dev.manufacturer = wpa_s->conf->manufacturer;
00829 wps->dev.model_name = wpa_s->conf->model_name;
00830 wps->dev.model_number = wpa_s->conf->model_number;
00831 wps->dev.serial_number = wpa_s->conf->serial_number;
00832 wps->config_methods =
00833 wps_config_methods_str2bin(wpa_s->conf->config_methods);
00834 if (wpa_s->conf->device_type &&
00835 wps_dev_type_str2bin(wpa_s->conf->device_type,
00836 wps->dev.pri_dev_type) < 0) {
00837 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
00838 os_free(wps);
00839 return -1;
00840 }
00841 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
00842 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
00843 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
00844 if (is_nil_uuid(wpa_s->conf->uuid)) {
00845 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
00846 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
00847 wps->uuid, WPS_UUID_LEN);
00848 } else
00849 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
00850
00851 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
00852 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
00853
00854 os_memset(&rcfg, 0, sizeof(rcfg));
00855 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
00856 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
00857 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
00858 rcfg.cb_ctx = wpa_s;
00859
00860 wps->registrar = wps_registrar_init(wps, &rcfg);
00861 if (wps->registrar == NULL) {
00862 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
00863 os_free(wps);
00864 return -1;
00865 }
00866
00867 wpa_s->wps = wps;
00868
00869 return 0;
00870 }
00871
00872
00873 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
00874 {
00875 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
00876
00877 if (wpa_s->wps == NULL)
00878 return;
00879
00880 #ifdef CONFIG_WPS_ER
00881 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
00882 wpa_s->wps_er = NULL;
00883 #endif
00884
00885 wps_registrar_deinit(wpa_s->wps->registrar);
00886 wpabuf_free(wpa_s->wps->dh_pubkey);
00887 wpabuf_free(wpa_s->wps->dh_privkey);
00888 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
00889 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
00890 os_free(wpa_s->wps->network_key);
00891 os_free(wpa_s->wps);
00892 wpa_s->wps = NULL;
00893 }
00894
00895
00896 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
00897 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
00898 {
00899 struct wpabuf *wps_ie;
00900
00901 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
00902 return -1;
00903
00904 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
00905 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
00906 if (!wps_ie) {
00907 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
00908 return 0;
00909 }
00910
00911 if (!wps_is_selected_pbc_registrar(wps_ie)) {
00912 wpa_printf(MSG_DEBUG, " skip - WPS AP "
00913 "without active PBC Registrar");
00914 wpabuf_free(wps_ie);
00915 return 0;
00916 }
00917
00918
00919 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
00920 "(Active PBC)");
00921 wpabuf_free(wps_ie);
00922 return 1;
00923 }
00924
00925 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
00926 if (!wps_ie) {
00927 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
00928 return 0;
00929 }
00930
00931
00932
00933
00934
00935
00936
00937 if (!wps_is_selected_pin_registrar(wps_ie)) {
00938 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
00939 wpa_printf(MSG_DEBUG, " skip - WPS AP "
00940 "without active PIN Registrar");
00941 wpabuf_free(wps_ie);
00942 return 0;
00943 }
00944 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
00945 } else {
00946 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
00947 "(Active PIN)");
00948 }
00949 wpabuf_free(wps_ie);
00950 return 1;
00951 }
00952
00953 if (wps_ie) {
00954 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
00955 wpabuf_free(wps_ie);
00956 return 1;
00957 }
00958
00959 return -1;
00960 }
00961
00962
00963 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
00964 struct wpa_ssid *ssid,
00965 struct wpa_scan_res *bss)
00966 {
00967 struct wpabuf *wps_ie = NULL;
00968 int ret = 0;
00969
00970 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
00971 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
00972 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
00973
00974 ret = 1;
00975 }
00976 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
00977 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
00978 if (wps_ie &&
00979 (wps_is_selected_pin_registrar(wps_ie) ||
00980 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
00981
00982 ret = 1;
00983 }
00984 }
00985
00986 if (!ret && ssid->bssid_set &&
00987 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
00988
00989 ret = 1;
00990 }
00991
00992 wpabuf_free(wps_ie);
00993
00994 return ret;
00995 }
00996
00997
00998 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
00999 struct wpa_bss *selected, struct wpa_ssid *ssid)
01000 {
01001 const u8 *sel_uuid, *uuid;
01002 struct wpabuf *wps_ie;
01003 int ret = 0;
01004 struct wpa_bss *bss;
01005
01006 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
01007 return 0;
01008
01009
01010 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
01011 if (wps_ie)
01012 sel_uuid = wps_get_uuid_e(wps_ie);
01013 else
01014 sel_uuid = NULL;
01015
01016 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
01017 struct wpabuf *ie;
01018 if (bss == selected)
01019 continue;
01020 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
01021 if (!ie)
01022 continue;
01023 if (!wps_is_selected_pbc_registrar(ie)) {
01024 wpabuf_free(ie);
01025 continue;
01026 }
01027 uuid = wps_get_uuid_e(ie);
01028 if (sel_uuid == NULL || uuid == NULL ||
01029 os_memcmp(sel_uuid, uuid, 16) != 0) {
01030 ret = 1;
01031 wpabuf_free(ie);
01032 break;
01033 }
01034
01035
01036
01037 wpabuf_free(ie);
01038 }
01039
01040 wpabuf_free(wps_ie);
01041
01042 return ret;
01043 }
01044
01045
01046 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
01047 {
01048 struct wpa_bss *bss;
01049
01050 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
01051 return;
01052
01053 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
01054 struct wpabuf *ie;
01055 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
01056 if (!ie)
01057 continue;
01058 if (wps_is_selected_pbc_registrar(ie))
01059 wpa_msg_ctrl(wpa_s, MSG_INFO,
01060 WPS_EVENT_AP_AVAILABLE_PBC);
01061 else if (wps_is_selected_pin_registrar(ie))
01062 wpa_msg_ctrl(wpa_s, MSG_INFO,
01063 WPS_EVENT_AP_AVAILABLE_PIN);
01064 else
01065 wpa_msg_ctrl(wpa_s, MSG_INFO,
01066 WPS_EVENT_AP_AVAILABLE);
01067 wpabuf_free(ie);
01068 break;
01069 }
01070 }
01071
01072
01073 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
01074 {
01075 struct wpa_ssid *ssid;
01076
01077 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
01078 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
01079 return 1;
01080 }
01081
01082 return 0;
01083 }
01084
01085
01086 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
01087 char *end)
01088 {
01089 struct wpabuf *wps_ie;
01090 int ret;
01091
01092 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
01093 if (wps_ie == NULL)
01094 return 0;
01095
01096 ret = wps_attr_text(wps_ie, buf, end);
01097 wpabuf_free(wps_ie);
01098 return ret;
01099 }
01100
01101
01102 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
01103 {
01104 #ifdef CONFIG_WPS_ER
01105 if (wpa_s->wps_er) {
01106 wps_er_refresh(wpa_s->wps_er);
01107 return 0;
01108 }
01109 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
01110 if (wpa_s->wps_er == NULL)
01111 return -1;
01112 return 0;
01113 #else
01114 return 0;
01115 #endif
01116 }
01117
01118
01119 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
01120 {
01121 #ifdef CONFIG_WPS_ER
01122 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
01123 wpa_s->wps_er = NULL;
01124 #endif
01125 return 0;
01126 }
01127
01128
01129 #ifdef CONFIG_WPS_ER
01130 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
01131 const char *pin)
01132 {
01133 u8 u[UUID_LEN];
01134 int any = 0;
01135
01136 if (os_strcmp(uuid, "any") == 0)
01137 any = 1;
01138 else if (uuid_str2bin(uuid, u))
01139 return -1;
01140 return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
01141 (const u8 *) pin, os_strlen(pin), 300);
01142 }
01143
01144
01145 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
01146 {
01147 u8 u[UUID_LEN];
01148
01149 if (uuid_str2bin(uuid, u))
01150 return -1;
01151 return wps_er_pbc(wpa_s->wps_er, u);
01152 }
01153
01154
01155 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
01156 const char *pin)
01157 {
01158 u8 u[UUID_LEN];
01159
01160 if (uuid_str2bin(uuid, u))
01161 return -1;
01162 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
01163 os_strlen(pin));
01164 }
01165
01166
01167 static void wpas_wps_terminate_cb(void *ctx)
01168 {
01169 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
01170 eloop_terminate();
01171 }
01172 #endif
01173
01174
01175 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
01176 {
01177 #ifdef CONFIG_WPS_ER
01178 if (wpa_s->wps_er) {
01179 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
01180 wpa_s->wps_er = NULL;
01181 return 1;
01182 }
01183 #endif
01184 return 0;
01185 }