1 /* 2 * hostapd / WPS integration 3 * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "utils/includes.h" 10 11 #include "utils/common.h" 12 #include "utils/eloop.h" 13 #include "utils/uuid.h" 14 #include "common/wpa_ctrl.h" 15 #include "common/ieee802_11_defs.h" 16 #include "common/ieee802_11_common.h" 17 #include "eapol_auth/eapol_auth_sm.h" 18 #include "eapol_auth/eapol_auth_sm_i.h" 19 #include "wps/wps.h" 20 #include "wps/wps_defs.h" 21 #include "wps/wps_dev_attr.h" 22 #include "wps/wps_attr_parse.h" 23 #include "hostapd.h" 24 #include "ap_config.h" 25 #include "ap_drv_ops.h" 26 #include "beacon.h" 27 #include "sta_info.h" 28 #include "wps_hostapd.h" 29 30 31 #ifdef CONFIG_WPS_UPNP 32 #include "wps/wps_upnp.h" 33 static int hostapd_wps_upnp_init(struct hostapd_data *hapd, 34 struct wps_context *wps); 35 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd); 36 #endif /* CONFIG_WPS_UPNP */ 37 38 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, 39 const u8 *bssid, 40 const u8 *ie, size_t ie_len, 41 int ssi_signal); 42 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx); 43 44 45 struct wps_for_each_data { 46 int (*func)(struct hostapd_data *h, void *ctx); 47 void *ctx; 48 }; 49 50 51 static int wps_for_each(struct hostapd_iface *iface, void *ctx) 52 { 53 struct wps_for_each_data *data = ctx; 54 size_t j; 55 56 if (iface == NULL) 57 return 0; 58 for (j = 0; j < iface->num_bss; j++) { 59 struct hostapd_data *hapd = iface->bss[j]; 60 int ret = data->func(hapd, data->ctx); 61 if (ret) 62 return ret; 63 } 64 65 return 0; 66 } 67 68 69 static int hostapd_wps_for_each(struct hostapd_data *hapd, 70 int (*func)(struct hostapd_data *h, void *ctx), 71 void *ctx) 72 { 73 struct hostapd_iface *iface = hapd->iface; 74 struct wps_for_each_data data; 75 data.func = func; 76 data.ctx = ctx; 77 if (iface->interfaces == NULL || 78 iface->interfaces->for_each_interface == NULL) 79 return wps_for_each(iface, &data); 80 return iface->interfaces->for_each_interface(iface->interfaces, 81 wps_for_each, &data); 82 } 83 84 85 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk, 86 size_t psk_len) 87 { 88 struct hostapd_data *hapd = ctx; 89 struct hostapd_wpa_psk *p; 90 struct hostapd_ssid *ssid = &hapd->conf->ssid; 91 92 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA " 93 MACSTR, MAC2STR(mac_addr)); 94 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len); 95 96 if (psk_len != PMK_LEN) { 97 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu", 98 (unsigned long) psk_len); 99 return -1; 100 } 101 102 /* Add the new PSK to runtime PSK list */ 103 p = os_zalloc(sizeof(*p)); 104 if (p == NULL) 105 return -1; 106 os_memcpy(p->addr, mac_addr, ETH_ALEN); 107 os_memcpy(p->psk, psk, PMK_LEN); 108 109 p->next = ssid->wpa_psk; 110 ssid->wpa_psk = p; 111 112 if (ssid->wpa_psk_file) { 113 FILE *f; 114 char hex[PMK_LEN * 2 + 1]; 115 /* Add the new PSK to PSK list file */ 116 f = fopen(ssid->wpa_psk_file, "a"); 117 if (f == NULL) { 118 wpa_printf(MSG_DEBUG, "Failed to add the PSK to " 119 "'%s'", ssid->wpa_psk_file); 120 return -1; 121 } 122 123 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len); 124 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex); 125 fclose(f); 126 } 127 128 return 0; 129 } 130 131 132 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie, 133 struct wpabuf *probe_resp_ie) 134 { 135 struct hostapd_data *hapd = ctx; 136 wpabuf_free(hapd->wps_beacon_ie); 137 hapd->wps_beacon_ie = beacon_ie; 138 wpabuf_free(hapd->wps_probe_resp_ie); 139 hapd->wps_probe_resp_ie = probe_resp_ie; 140 if (hapd->beacon_set_done) 141 ieee802_11_set_beacon(hapd); 142 return hostapd_set_ap_wps_ie(hapd); 143 } 144 145 146 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e, 147 const struct wps_device_data *dev) 148 { 149 struct hostapd_data *hapd = ctx; 150 char uuid[40], txt[400]; 151 int len; 152 char devtype[WPS_DEV_TYPE_BUFSIZE]; 153 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) 154 return; 155 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid); 156 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED 157 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]", 158 uuid, MAC2STR(dev->mac_addr), dev->device_name, 159 dev->manufacturer, dev->model_name, 160 dev->model_number, dev->serial_number, 161 wps_dev_type_bin2str(dev->pri_dev_type, devtype, 162 sizeof(devtype))); 163 if (len > 0 && len < (int) sizeof(txt)) 164 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt); 165 166 if (hapd->conf->wps_pin_requests) { 167 FILE *f; 168 struct os_time t; 169 f = fopen(hapd->conf->wps_pin_requests, "a"); 170 if (f == NULL) 171 return; 172 os_get_time(&t); 173 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s" 174 "\t%s\n", 175 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name, 176 dev->manufacturer, dev->model_name, dev->model_number, 177 dev->serial_number, 178 wps_dev_type_bin2str(dev->pri_dev_type, devtype, 179 sizeof(devtype))); 180 fclose(f); 181 } 182 } 183 184 185 struct wps_stop_reg_data { 186 struct hostapd_data *current_hapd; 187 const u8 *uuid_e; 188 const u8 *dev_pw; 189 size_t dev_pw_len; 190 }; 191 192 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx) 193 { 194 struct wps_stop_reg_data *data = ctx; 195 if (hapd != data->current_hapd && hapd->wps != NULL) 196 wps_registrar_complete(hapd->wps->registrar, data->uuid_e, 197 data->dev_pw, data->dev_pw_len); 198 return 0; 199 } 200 201 202 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr, 203 const u8 *uuid_e, const u8 *dev_pw, 204 size_t dev_pw_len) 205 { 206 struct hostapd_data *hapd = ctx; 207 char uuid[40]; 208 struct wps_stop_reg_data data; 209 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) 210 return; 211 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s", 212 MAC2STR(mac_addr), uuid); 213 if (hapd->wps_reg_success_cb) 214 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx, 215 mac_addr, uuid_e); 216 data.current_hapd = hapd; 217 data.uuid_e = uuid_e; 218 data.dev_pw = dev_pw; 219 data.dev_pw_len = dev_pw_len; 220 hostapd_wps_for_each(hapd, wps_stop_registrar, &data); 221 } 222 223 224 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr, 225 const u8 *uuid_e, 226 const u8 *pri_dev_type, 227 u16 config_methods, 228 u16 dev_password_id, u8 request_type, 229 const char *dev_name) 230 { 231 struct hostapd_data *hapd = ctx; 232 char uuid[40]; 233 char devtype[WPS_DEV_TYPE_BUFSIZE]; 234 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) 235 return; 236 if (dev_name == NULL) 237 dev_name = ""; 238 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR 239 " %s %s 0x%x %u %u [%s]", 240 MAC2STR(addr), uuid, 241 wps_dev_type_bin2str(pri_dev_type, devtype, 242 sizeof(devtype)), 243 config_methods, dev_password_id, request_type, dev_name); 244 } 245 246 247 static int str_starts(const char *str, const char *start) 248 { 249 return os_strncmp(str, start, os_strlen(start)) == 0; 250 } 251 252 253 static void wps_reload_config(void *eloop_data, void *user_ctx) 254 { 255 struct hostapd_iface *iface = eloop_data; 256 257 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data"); 258 if (iface->interfaces == NULL || 259 iface->interfaces->reload_config(iface) < 0) { 260 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated " 261 "configuration"); 262 } 263 } 264 265 266 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr, 267 size_t attr_len) 268 { 269 size_t blen = attr_len * 2 + 1; 270 char *buf = os_malloc(blen); 271 if (buf) { 272 wpa_snprintf_hex(buf, blen, attr, attr_len); 273 wpa_msg(hapd->msg_ctx, MSG_INFO, 274 WPS_EVENT_NEW_AP_SETTINGS "%s", buf); 275 os_free(buf); 276 } 277 } 278 279 280 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx) 281 { 282 const struct wps_credential *cred = ctx; 283 FILE *oconf, *nconf; 284 size_t len, i; 285 char *tmp_fname; 286 char buf[1024]; 287 int multi_bss; 288 int wpa; 289 290 if (hapd->wps == NULL) 291 return 0; 292 293 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute", 294 cred->cred_attr, cred->cred_attr_len); 295 296 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings"); 297 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len); 298 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x", 299 cred->auth_type); 300 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type); 301 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx); 302 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", 303 cred->key, cred->key_len); 304 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, 305 MAC2STR(cred->mac_addr)); 306 307 if ((hapd->conf->wps_cred_processing == 1 || 308 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) { 309 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len); 310 } else if (hapd->conf->wps_cred_processing == 1 || 311 hapd->conf->wps_cred_processing == 2) { 312 struct wpabuf *attr; 313 attr = wpabuf_alloc(200); 314 if (attr && wps_build_credential_wrap(attr, cred) == 0) 315 hapd_new_ap_event(hapd, wpabuf_head_u8(attr), 316 wpabuf_len(attr)); 317 wpabuf_free(attr); 318 } else 319 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS); 320 321 if (hapd->conf->wps_cred_processing == 1) 322 return 0; 323 324 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len); 325 hapd->wps->ssid_len = cred->ssid_len; 326 hapd->wps->encr_types = cred->encr_type; 327 hapd->wps->auth_types = cred->auth_type; 328 if (cred->key_len == 0) { 329 os_free(hapd->wps->network_key); 330 hapd->wps->network_key = NULL; 331 hapd->wps->network_key_len = 0; 332 } else { 333 if (hapd->wps->network_key == NULL || 334 hapd->wps->network_key_len < cred->key_len) { 335 hapd->wps->network_key_len = 0; 336 os_free(hapd->wps->network_key); 337 hapd->wps->network_key = os_malloc(cred->key_len); 338 if (hapd->wps->network_key == NULL) 339 return -1; 340 } 341 hapd->wps->network_key_len = cred->key_len; 342 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len); 343 } 344 hapd->wps->wps_state = WPS_STATE_CONFIGURED; 345 346 if (hapd->iface->config_fname == NULL) 347 return 0; 348 len = os_strlen(hapd->iface->config_fname) + 5; 349 tmp_fname = os_malloc(len); 350 if (tmp_fname == NULL) 351 return -1; 352 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname); 353 354 oconf = fopen(hapd->iface->config_fname, "r"); 355 if (oconf == NULL) { 356 wpa_printf(MSG_WARNING, "WPS: Could not open current " 357 "configuration file"); 358 os_free(tmp_fname); 359 return -1; 360 } 361 362 nconf = fopen(tmp_fname, "w"); 363 if (nconf == NULL) { 364 wpa_printf(MSG_WARNING, "WPS: Could not write updated " 365 "configuration file"); 366 os_free(tmp_fname); 367 fclose(oconf); 368 return -1; 369 } 370 371 fprintf(nconf, "# WPS configuration - START\n"); 372 373 fprintf(nconf, "wps_state=2\n"); 374 375 if (is_hex(cred->ssid, cred->ssid_len)) { 376 fprintf(nconf, "ssid2="); 377 for (i = 0; i < cred->ssid_len; i++) 378 fprintf(nconf, "%02x", cred->ssid[i]); 379 fprintf(nconf, "\n"); 380 } else { 381 fprintf(nconf, "ssid="); 382 for (i = 0; i < cred->ssid_len; i++) 383 fputc(cred->ssid[i], nconf); 384 fprintf(nconf, "\n"); 385 } 386 387 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && 388 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) 389 wpa = 3; 390 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) 391 wpa = 2; 392 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) 393 wpa = 1; 394 else 395 wpa = 0; 396 397 if (wpa) { 398 char *prefix; 399 fprintf(nconf, "wpa=%d\n", wpa); 400 401 fprintf(nconf, "wpa_key_mgmt="); 402 prefix = ""; 403 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) { 404 fprintf(nconf, "WPA-EAP"); 405 prefix = " "; 406 } 407 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) 408 fprintf(nconf, "%sWPA-PSK", prefix); 409 fprintf(nconf, "\n"); 410 411 fprintf(nconf, "wpa_pairwise="); 412 prefix = ""; 413 if (cred->encr_type & WPS_ENCR_AES) { 414 fprintf(nconf, "CCMP"); 415 prefix = " "; 416 } 417 if (cred->encr_type & WPS_ENCR_TKIP) { 418 fprintf(nconf, "%sTKIP", prefix); 419 } 420 fprintf(nconf, "\n"); 421 422 if (cred->key_len >= 8 && cred->key_len < 64) { 423 fprintf(nconf, "wpa_passphrase="); 424 for (i = 0; i < cred->key_len; i++) 425 fputc(cred->key[i], nconf); 426 fprintf(nconf, "\n"); 427 } else if (cred->key_len == 64) { 428 fprintf(nconf, "wpa_psk="); 429 for (i = 0; i < cred->key_len; i++) 430 fputc(cred->key[i], nconf); 431 fprintf(nconf, "\n"); 432 } else { 433 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu " 434 "for WPA/WPA2", 435 (unsigned long) cred->key_len); 436 } 437 438 fprintf(nconf, "auth_algs=1\n"); 439 } else { 440 if ((cred->auth_type & WPS_AUTH_OPEN) && 441 (cred->auth_type & WPS_AUTH_SHARED)) 442 fprintf(nconf, "auth_algs=3\n"); 443 else if (cred->auth_type & WPS_AUTH_SHARED) 444 fprintf(nconf, "auth_algs=2\n"); 445 else 446 fprintf(nconf, "auth_algs=1\n"); 447 448 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) { 449 int key_idx = cred->key_idx; 450 if (key_idx) 451 key_idx--; 452 fprintf(nconf, "wep_default_key=%d\n", key_idx); 453 fprintf(nconf, "wep_key%d=", key_idx); 454 if (cred->key_len == 10 || cred->key_len == 26) { 455 /* WEP key as a hex string */ 456 for (i = 0; i < cred->key_len; i++) 457 fputc(cred->key[i], nconf); 458 } else { 459 /* Raw WEP key; convert to hex */ 460 for (i = 0; i < cred->key_len; i++) 461 fprintf(nconf, "%02x", cred->key[i]); 462 } 463 fprintf(nconf, "\n"); 464 } 465 } 466 467 fprintf(nconf, "# WPS configuration - END\n"); 468 469 multi_bss = 0; 470 while (fgets(buf, sizeof(buf), oconf)) { 471 if (os_strncmp(buf, "bss=", 4) == 0) 472 multi_bss = 1; 473 if (!multi_bss && 474 (str_starts(buf, "ssid=") || 475 str_starts(buf, "ssid2=") || 476 str_starts(buf, "auth_algs=") || 477 str_starts(buf, "wep_default_key=") || 478 str_starts(buf, "wep_key") || 479 str_starts(buf, "wps_state=") || 480 str_starts(buf, "wpa=") || 481 str_starts(buf, "wpa_psk=") || 482 str_starts(buf, "wpa_pairwise=") || 483 str_starts(buf, "rsn_pairwise=") || 484 str_starts(buf, "wpa_key_mgmt=") || 485 str_starts(buf, "wpa_passphrase="))) { 486 fprintf(nconf, "#WPS# %s", buf); 487 } else 488 fprintf(nconf, "%s", buf); 489 } 490 491 fclose(nconf); 492 fclose(oconf); 493 494 if (rename(tmp_fname, hapd->iface->config_fname) < 0) { 495 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated " 496 "configuration file: %s", strerror(errno)); 497 os_free(tmp_fname); 498 return -1; 499 } 500 501 os_free(tmp_fname); 502 503 /* Schedule configuration reload after short period of time to allow 504 * EAP-WSC to be finished. 505 */ 506 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, 507 NULL); 508 509 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated"); 510 511 return 0; 512 } 513 514 515 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred) 516 { 517 struct hostapd_data *hapd = ctx; 518 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred); 519 } 520 521 522 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx) 523 { 524 struct hostapd_data *hapd = eloop_data; 525 526 if (hapd->conf->ap_setup_locked) 527 return; 528 if (hapd->ap_pin_failures_consecutive >= 10) 529 return; 530 531 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN"); 532 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); 533 hapd->wps->ap_setup_locked = 0; 534 wps_registrar_update_ie(hapd->wps->registrar); 535 } 536 537 538 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx) 539 { 540 struct wps_event_pwd_auth_fail *data = ctx; 541 542 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL) 543 return 0; 544 545 /* 546 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup 547 * for some time if this happens multiple times to slow down brute 548 * force attacks. 549 */ 550 hapd->ap_pin_failures++; 551 hapd->ap_pin_failures_consecutive++; 552 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u " 553 "(%u consecutive)", 554 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); 555 if (hapd->ap_pin_failures < 3) 556 return 0; 557 558 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED); 559 hapd->wps->ap_setup_locked = 1; 560 561 wps_registrar_update_ie(hapd->wps->registrar); 562 563 if (!hapd->conf->ap_setup_locked && 564 hapd->ap_pin_failures_consecutive >= 10) { 565 /* 566 * In indefinite lockdown - disable automatic AP PIN 567 * reenablement. 568 */ 569 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); 570 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely"); 571 } else if (!hapd->conf->ap_setup_locked) { 572 if (hapd->ap_pin_lockout_time == 0) 573 hapd->ap_pin_lockout_time = 60; 574 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 && 575 (hapd->ap_pin_failures % 3) == 0) 576 hapd->ap_pin_lockout_time *= 2; 577 578 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds", 579 hapd->ap_pin_lockout_time); 580 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); 581 eloop_register_timeout(hapd->ap_pin_lockout_time, 0, 582 hostapd_wps_reenable_ap_pin, hapd, 583 NULL); 584 } 585 586 return 0; 587 } 588 589 590 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd, 591 struct wps_event_pwd_auth_fail *data) 592 { 593 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data); 594 } 595 596 597 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx) 598 { 599 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL) 600 return 0; 601 602 if (hapd->ap_pin_failures_consecutive == 0) 603 return 0; 604 605 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter " 606 "- total validation failures %u (%u consecutive)", 607 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); 608 hapd->ap_pin_failures_consecutive = 0; 609 610 return 0; 611 } 612 613 614 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd) 615 { 616 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL); 617 } 618 619 620 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = { 621 "No Error", /* WPS_EI_NO_ERROR */ 622 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */ 623 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */ 624 }; 625 626 static void hostapd_wps_event_fail(struct hostapd_data *hapd, 627 struct wps_event_fail *fail) 628 { 629 if (fail->error_indication > 0 && 630 fail->error_indication < NUM_WPS_EI_VALUES) { 631 wpa_msg(hapd->msg_ctx, MSG_INFO, 632 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)", 633 fail->msg, fail->config_error, fail->error_indication, 634 wps_event_fail_reason[fail->error_indication]); 635 } else { 636 wpa_msg(hapd->msg_ctx, MSG_INFO, 637 WPS_EVENT_FAIL "msg=%d config_error=%d", 638 fail->msg, fail->config_error); 639 } 640 } 641 642 643 static void hostapd_wps_event_cb(void *ctx, enum wps_event event, 644 union wps_event_data *data) 645 { 646 struct hostapd_data *hapd = ctx; 647 648 switch (event) { 649 case WPS_EV_M2D: 650 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D); 651 break; 652 case WPS_EV_FAIL: 653 hostapd_wps_event_fail(hapd, &data->fail); 654 break; 655 case WPS_EV_SUCCESS: 656 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS); 657 break; 658 case WPS_EV_PWD_AUTH_FAIL: 659 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail); 660 break; 661 case WPS_EV_PBC_OVERLAP: 662 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP); 663 break; 664 case WPS_EV_PBC_TIMEOUT: 665 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT); 666 break; 667 case WPS_EV_ER_AP_ADD: 668 break; 669 case WPS_EV_ER_AP_REMOVE: 670 break; 671 case WPS_EV_ER_ENROLLEE_ADD: 672 break; 673 case WPS_EV_ER_ENROLLEE_REMOVE: 674 break; 675 case WPS_EV_ER_AP_SETTINGS: 676 break; 677 case WPS_EV_ER_SET_SELECTED_REGISTRAR: 678 break; 679 case WPS_EV_AP_PIN_SUCCESS: 680 hostapd_wps_ap_pin_success(hapd); 681 break; 682 } 683 if (hapd->wps_event_cb) 684 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data); 685 } 686 687 688 static void hostapd_wps_clear_ies(struct hostapd_data *hapd) 689 { 690 wpabuf_free(hapd->wps_beacon_ie); 691 hapd->wps_beacon_ie = NULL; 692 693 wpabuf_free(hapd->wps_probe_resp_ie); 694 hapd->wps_probe_resp_ie = NULL; 695 696 hostapd_set_ap_wps_ie(hapd); 697 } 698 699 700 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx) 701 { 702 const u8 **uuid = ctx; 703 size_t j; 704 705 if (iface == NULL) 706 return 0; 707 for (j = 0; j < iface->num_bss; j++) { 708 struct hostapd_data *hapd = iface->bss[j]; 709 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) { 710 *uuid = hapd->wps->uuid; 711 return 1; 712 } 713 } 714 715 return 0; 716 } 717 718 719 static const u8 * get_own_uuid(struct hostapd_iface *iface) 720 { 721 const u8 *uuid; 722 if (iface->interfaces == NULL || 723 iface->interfaces->for_each_interface == NULL) 724 return NULL; 725 uuid = NULL; 726 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb, 727 &uuid); 728 return uuid; 729 } 730 731 732 static int count_interface_cb(struct hostapd_iface *iface, void *ctx) 733 { 734 int *count= ctx; 735 (*count)++; 736 return 0; 737 } 738 739 740 static int interface_count(struct hostapd_iface *iface) 741 { 742 int count = 0; 743 if (iface->interfaces == NULL || 744 iface->interfaces->for_each_interface == NULL) 745 return 0; 746 iface->interfaces->for_each_interface(iface->interfaces, 747 count_interface_cb, &count); 748 return count; 749 } 750 751 752 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd, 753 struct wps_context *wps) 754 { 755 int i; 756 757 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { 758 wpabuf_free(wps->dev.vendor_ext[i]); 759 wps->dev.vendor_ext[i] = NULL; 760 761 if (hapd->conf->wps_vendor_ext[i] == NULL) 762 continue; 763 764 wps->dev.vendor_ext[i] = 765 wpabuf_dup(hapd->conf->wps_vendor_ext[i]); 766 if (wps->dev.vendor_ext[i] == NULL) { 767 while (--i >= 0) 768 wpabuf_free(wps->dev.vendor_ext[i]); 769 return -1; 770 } 771 } 772 773 return 0; 774 } 775 776 777 int hostapd_init_wps(struct hostapd_data *hapd, 778 struct hostapd_bss_config *conf) 779 { 780 struct wps_context *wps; 781 struct wps_registrar_config cfg; 782 783 if (conf->wps_state == 0) { 784 hostapd_wps_clear_ies(hapd); 785 return 0; 786 } 787 788 wps = os_zalloc(sizeof(*wps)); 789 if (wps == NULL) 790 return -1; 791 792 wps->cred_cb = hostapd_wps_cred_cb; 793 wps->event_cb = hostapd_wps_event_cb; 794 wps->cb_ctx = hapd; 795 796 os_memset(&cfg, 0, sizeof(cfg)); 797 wps->wps_state = hapd->conf->wps_state; 798 wps->ap_setup_locked = hapd->conf->ap_setup_locked; 799 if (is_nil_uuid(hapd->conf->uuid)) { 800 const u8 *uuid; 801 uuid = get_own_uuid(hapd->iface); 802 if (uuid) { 803 os_memcpy(wps->uuid, uuid, UUID_LEN); 804 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another " 805 "interface", wps->uuid, UUID_LEN); 806 } else { 807 uuid_gen_mac_addr(hapd->own_addr, wps->uuid); 808 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC " 809 "address", wps->uuid, UUID_LEN); 810 } 811 } else { 812 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN); 813 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID", 814 wps->uuid, UUID_LEN); 815 } 816 wps->ssid_len = hapd->conf->ssid.ssid_len; 817 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len); 818 wps->ap = 1; 819 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN); 820 wps->dev.device_name = hapd->conf->device_name ? 821 os_strdup(hapd->conf->device_name) : NULL; 822 wps->dev.manufacturer = hapd->conf->manufacturer ? 823 os_strdup(hapd->conf->manufacturer) : NULL; 824 wps->dev.model_name = hapd->conf->model_name ? 825 os_strdup(hapd->conf->model_name) : NULL; 826 wps->dev.model_number = hapd->conf->model_number ? 827 os_strdup(hapd->conf->model_number) : NULL; 828 wps->dev.serial_number = hapd->conf->serial_number ? 829 os_strdup(hapd->conf->serial_number) : NULL; 830 wps->config_methods = 831 wps_config_methods_str2bin(hapd->conf->config_methods); 832 #ifdef CONFIG_WPS2 833 if ((wps->config_methods & 834 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY | 835 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) { 836 wpa_printf(MSG_INFO, "WPS: Converting display to " 837 "virtual_display for WPS 2.0 compliance"); 838 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY; 839 } 840 if ((wps->config_methods & 841 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON | 842 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) { 843 wpa_printf(MSG_INFO, "WPS: Converting push_button to " 844 "virtual_push_button for WPS 2.0 compliance"); 845 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON; 846 } 847 #endif /* CONFIG_WPS2 */ 848 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type, 849 WPS_DEV_TYPE_LEN); 850 851 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) { 852 os_free(wps); 853 return -1; 854 } 855 856 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version); 857 858 if (conf->wps_rf_bands) { 859 wps->dev.rf_bands = conf->wps_rf_bands; 860 } else { 861 wps->dev.rf_bands = 862 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? 863 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ 864 } 865 866 if (conf->wpa & WPA_PROTO_RSN) { 867 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) 868 wps->auth_types |= WPS_AUTH_WPA2PSK; 869 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) 870 wps->auth_types |= WPS_AUTH_WPA2; 871 872 if (conf->rsn_pairwise & WPA_CIPHER_CCMP) 873 wps->encr_types |= WPS_ENCR_AES; 874 if (conf->rsn_pairwise & WPA_CIPHER_TKIP) 875 wps->encr_types |= WPS_ENCR_TKIP; 876 } 877 878 if (conf->wpa & WPA_PROTO_WPA) { 879 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) 880 wps->auth_types |= WPS_AUTH_WPAPSK; 881 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) 882 wps->auth_types |= WPS_AUTH_WPA; 883 884 if (conf->wpa_pairwise & WPA_CIPHER_CCMP) 885 wps->encr_types |= WPS_ENCR_AES; 886 if (conf->wpa_pairwise & WPA_CIPHER_TKIP) 887 wps->encr_types |= WPS_ENCR_TKIP; 888 } 889 890 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) { 891 wps->encr_types |= WPS_ENCR_NONE; 892 wps->auth_types |= WPS_AUTH_OPEN; 893 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) { 894 wps->encr_types |= WPS_ENCR_WEP; 895 if (conf->auth_algs & WPA_AUTH_ALG_OPEN) 896 wps->auth_types |= WPS_AUTH_OPEN; 897 if (conf->auth_algs & WPA_AUTH_ALG_SHARED) 898 wps->auth_types |= WPS_AUTH_SHARED; 899 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) { 900 wps->auth_types |= WPS_AUTH_OPEN; 901 if (conf->default_wep_key_len) 902 wps->encr_types |= WPS_ENCR_WEP; 903 else 904 wps->encr_types |= WPS_ENCR_NONE; 905 } 906 907 if (conf->ssid.wpa_psk_file) { 908 /* Use per-device PSKs */ 909 } else if (conf->ssid.wpa_passphrase) { 910 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase); 911 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase); 912 } else if (conf->ssid.wpa_psk) { 913 wps->network_key = os_malloc(2 * PMK_LEN + 1); 914 if (wps->network_key == NULL) { 915 os_free(wps); 916 return -1; 917 } 918 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1, 919 conf->ssid.wpa_psk->psk, PMK_LEN); 920 wps->network_key_len = 2 * PMK_LEN; 921 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) { 922 wps->network_key = os_malloc(conf->ssid.wep.len[0]); 923 if (wps->network_key == NULL) { 924 os_free(wps); 925 return -1; 926 } 927 os_memcpy(wps->network_key, conf->ssid.wep.key[0], 928 conf->ssid.wep.len[0]); 929 wps->network_key_len = conf->ssid.wep.len[0]; 930 } 931 932 if (conf->ssid.wpa_psk) { 933 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN); 934 wps->psk_set = 1; 935 } 936 937 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) { 938 /* Override parameters to enable security by default */ 939 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK; 940 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP; 941 } 942 943 wps->ap_settings = conf->ap_settings; 944 wps->ap_settings_len = conf->ap_settings_len; 945 946 cfg.new_psk_cb = hostapd_wps_new_psk_cb; 947 cfg.set_ie_cb = hostapd_wps_set_ie_cb; 948 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb; 949 cfg.reg_success_cb = hostapd_wps_reg_success_cb; 950 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb; 951 cfg.cb_ctx = hapd; 952 cfg.skip_cred_build = conf->skip_cred_build; 953 cfg.extra_cred = conf->extra_cred; 954 cfg.extra_cred_len = conf->extra_cred_len; 955 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) && 956 conf->skip_cred_build; 957 if (conf->ssid.security_policy == SECURITY_STATIC_WEP) 958 cfg.static_wep_only = 1; 959 cfg.dualband = interface_count(hapd->iface) > 1; 960 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) == 961 (WPS_RF_50GHZ | WPS_RF_24GHZ)) 962 cfg.dualband = 1; 963 if (cfg.dualband) 964 wpa_printf(MSG_DEBUG, "WPS: Dualband AP"); 965 966 wps->registrar = wps_registrar_init(wps, &cfg); 967 if (wps->registrar == NULL) { 968 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar"); 969 os_free(wps->network_key); 970 os_free(wps); 971 return -1; 972 } 973 974 #ifdef CONFIG_WPS_UPNP 975 wps->friendly_name = hapd->conf->friendly_name; 976 wps->manufacturer_url = hapd->conf->manufacturer_url; 977 wps->model_description = hapd->conf->model_description; 978 wps->model_url = hapd->conf->model_url; 979 wps->upc = hapd->conf->upc; 980 #endif /* CONFIG_WPS_UPNP */ 981 982 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd); 983 984 hapd->wps = wps; 985 986 return 0; 987 } 988 989 990 int hostapd_init_wps_complete(struct hostapd_data *hapd) 991 { 992 struct wps_context *wps = hapd->wps; 993 994 if (wps == NULL) 995 return 0; 996 997 #ifdef CONFIG_WPS_UPNP 998 if (hostapd_wps_upnp_init(hapd, wps) < 0) { 999 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP"); 1000 wps_registrar_deinit(wps->registrar); 1001 os_free(wps->network_key); 1002 os_free(wps); 1003 hapd->wps = NULL; 1004 return -1; 1005 } 1006 #endif /* CONFIG_WPS_UPNP */ 1007 1008 return 0; 1009 } 1010 1011 1012 static void hostapd_wps_nfc_clear(struct wps_context *wps) 1013 { 1014 #ifdef CONFIG_WPS_NFC 1015 wps->ap_nfc_dev_pw_id = 0; 1016 wpabuf_free(wps->ap_nfc_dh_pubkey); 1017 wps->ap_nfc_dh_pubkey = NULL; 1018 wpabuf_free(wps->ap_nfc_dh_privkey); 1019 wps->ap_nfc_dh_privkey = NULL; 1020 wpabuf_free(wps->ap_nfc_dev_pw); 1021 wps->ap_nfc_dev_pw = NULL; 1022 #endif /* CONFIG_WPS_NFC */ 1023 } 1024 1025 1026 void hostapd_deinit_wps(struct hostapd_data *hapd) 1027 { 1028 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); 1029 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1030 if (hapd->wps == NULL) 1031 return; 1032 #ifdef CONFIG_WPS_UPNP 1033 hostapd_wps_upnp_deinit(hapd); 1034 #endif /* CONFIG_WPS_UPNP */ 1035 wps_registrar_deinit(hapd->wps->registrar); 1036 os_free(hapd->wps->network_key); 1037 wps_device_data_free(&hapd->wps->dev); 1038 wpabuf_free(hapd->wps->dh_pubkey); 1039 wpabuf_free(hapd->wps->dh_privkey); 1040 wps_free_pending_msgs(hapd->wps->upnp_msgs); 1041 hostapd_wps_nfc_clear(hapd->wps); 1042 os_free(hapd->wps); 1043 hapd->wps = NULL; 1044 hostapd_wps_clear_ies(hapd); 1045 } 1046 1047 1048 void hostapd_update_wps(struct hostapd_data *hapd) 1049 { 1050 if (hapd->wps == NULL) 1051 return; 1052 1053 #ifdef CONFIG_WPS_UPNP 1054 hapd->wps->friendly_name = hapd->conf->friendly_name; 1055 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url; 1056 hapd->wps->model_description = hapd->conf->model_description; 1057 hapd->wps->model_url = hapd->conf->model_url; 1058 hapd->wps->upc = hapd->conf->upc; 1059 #endif /* CONFIG_WPS_UPNP */ 1060 1061 hostapd_wps_set_vendor_ext(hapd, hapd->wps); 1062 1063 if (hapd->conf->wps_state) 1064 wps_registrar_update_ie(hapd->wps->registrar); 1065 else 1066 hostapd_deinit_wps(hapd); 1067 } 1068 1069 1070 struct wps_add_pin_data { 1071 const u8 *addr; 1072 const u8 *uuid; 1073 const u8 *pin; 1074 size_t pin_len; 1075 int timeout; 1076 int added; 1077 }; 1078 1079 1080 static int wps_add_pin(struct hostapd_data *hapd, void *ctx) 1081 { 1082 struct wps_add_pin_data *data = ctx; 1083 int ret; 1084 1085 if (hapd->wps == NULL) 1086 return 0; 1087 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr, 1088 data->uuid, data->pin, data->pin_len, 1089 data->timeout); 1090 if (ret == 0) 1091 data->added++; 1092 return ret; 1093 } 1094 1095 1096 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr, 1097 const char *uuid, const char *pin, int timeout) 1098 { 1099 u8 u[UUID_LEN]; 1100 struct wps_add_pin_data data; 1101 1102 data.addr = addr; 1103 data.uuid = u; 1104 data.pin = (const u8 *) pin; 1105 data.pin_len = os_strlen(pin); 1106 data.timeout = timeout; 1107 data.added = 0; 1108 1109 if (os_strcmp(uuid, "any") == 0) 1110 data.uuid = NULL; 1111 else { 1112 if (uuid_str2bin(uuid, u)) 1113 return -1; 1114 data.uuid = u; 1115 } 1116 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0) 1117 return -1; 1118 return data.added ? 0 : -1; 1119 } 1120 1121 1122 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx) 1123 { 1124 const u8 *p2p_dev_addr = ctx; 1125 if (hapd->wps == NULL) 1126 return 0; 1127 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr); 1128 } 1129 1130 1131 int hostapd_wps_button_pushed(struct hostapd_data *hapd, 1132 const u8 *p2p_dev_addr) 1133 { 1134 return hostapd_wps_for_each(hapd, wps_button_pushed, 1135 (void *) p2p_dev_addr); 1136 } 1137 1138 1139 static int wps_cancel(struct hostapd_data *hapd, void *ctx) 1140 { 1141 if (hapd->wps == NULL) 1142 return 0; 1143 1144 wps_registrar_wps_cancel(hapd->wps->registrar); 1145 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL); 1146 1147 return 0; 1148 } 1149 1150 1151 int hostapd_wps_cancel(struct hostapd_data *hapd) 1152 { 1153 return hostapd_wps_for_each(hapd, wps_cancel, NULL); 1154 } 1155 1156 1157 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, 1158 const u8 *bssid, 1159 const u8 *ie, size_t ie_len, 1160 int ssi_signal) 1161 { 1162 struct hostapd_data *hapd = ctx; 1163 struct wpabuf *wps_ie; 1164 struct ieee802_11_elems elems; 1165 1166 if (hapd->wps == NULL) 1167 return 0; 1168 1169 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) { 1170 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from " 1171 MACSTR, MAC2STR(addr)); 1172 return 0; 1173 } 1174 1175 if (elems.ssid && elems.ssid_len > 0 && 1176 (elems.ssid_len != hapd->conf->ssid.ssid_len || 1177 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 1178 0)) 1179 return 0; /* Not for us */ 1180 1181 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); 1182 if (wps_ie == NULL) 1183 return 0; 1184 if (wps_validate_probe_req(wps_ie, addr) < 0) { 1185 wpabuf_free(wps_ie); 1186 return 0; 1187 } 1188 1189 if (wpabuf_len(wps_ie) > 0) { 1190 int p2p_wildcard = 0; 1191 #ifdef CONFIG_P2P 1192 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN && 1193 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, 1194 P2P_WILDCARD_SSID_LEN) == 0) 1195 p2p_wildcard = 1; 1196 #endif /* CONFIG_P2P */ 1197 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie, 1198 p2p_wildcard); 1199 #ifdef CONFIG_WPS_UPNP 1200 /* FIX: what exactly should be included in the WLANEvent? 1201 * WPS attributes? Full ProbeReq frame? */ 1202 if (!p2p_wildcard) 1203 upnp_wps_device_send_wlan_event( 1204 hapd->wps_upnp, addr, 1205 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie); 1206 #endif /* CONFIG_WPS_UPNP */ 1207 } 1208 1209 wpabuf_free(wps_ie); 1210 1211 return 0; 1212 } 1213 1214 1215 #ifdef CONFIG_WPS_UPNP 1216 1217 static int hostapd_rx_req_put_wlan_response( 1218 void *priv, enum upnp_wps_wlanevent_type ev_type, 1219 const u8 *mac_addr, const struct wpabuf *msg, 1220 enum wps_msg_type msg_type) 1221 { 1222 struct hostapd_data *hapd = priv; 1223 struct sta_info *sta; 1224 struct upnp_pending_message *p; 1225 1226 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr=" 1227 MACSTR, ev_type, MAC2STR(mac_addr)); 1228 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage", 1229 wpabuf_head(msg), wpabuf_len(msg)); 1230 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) { 1231 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected " 1232 "PutWLANResponse WLANEventType %d", ev_type); 1233 return -1; 1234 } 1235 1236 /* 1237 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC 1238 * server implementation for delivery to the peer. 1239 */ 1240 1241 sta = ap_get_sta(hapd, mac_addr); 1242 #ifndef CONFIG_WPS_STRICT 1243 if (!sta) { 1244 /* 1245 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC: 1246 * Pick STA that is in an ongoing WPS registration without 1247 * checking the MAC address. 1248 */ 1249 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based " 1250 "on NewWLANEventMAC; try wildcard match"); 1251 for (sta = hapd->sta_list; sta; sta = sta->next) { 1252 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS)) 1253 break; 1254 } 1255 } 1256 #endif /* CONFIG_WPS_STRICT */ 1257 1258 if (!sta || !(sta->flags & WLAN_STA_WPS)) { 1259 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found"); 1260 return 0; 1261 } 1262 1263 p = os_zalloc(sizeof(*p)); 1264 if (p == NULL) 1265 return -1; 1266 os_memcpy(p->addr, sta->addr, ETH_ALEN); 1267 p->msg = wpabuf_dup(msg); 1268 p->type = msg_type; 1269 p->next = hapd->wps->upnp_msgs; 1270 hapd->wps->upnp_msgs = p; 1271 1272 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap); 1273 } 1274 1275 1276 static int hostapd_wps_upnp_init(struct hostapd_data *hapd, 1277 struct wps_context *wps) 1278 { 1279 struct upnp_wps_device_ctx *ctx; 1280 1281 if (!hapd->conf->upnp_iface) 1282 return 0; 1283 ctx = os_zalloc(sizeof(*ctx)); 1284 if (ctx == NULL) 1285 return -1; 1286 1287 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response; 1288 if (hapd->conf->ap_pin) 1289 ctx->ap_pin = os_strdup(hapd->conf->ap_pin); 1290 1291 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd, 1292 hapd->conf->upnp_iface); 1293 if (hapd->wps_upnp == NULL) 1294 return -1; 1295 wps->wps_upnp = hapd->wps_upnp; 1296 1297 return 0; 1298 } 1299 1300 1301 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd) 1302 { 1303 upnp_wps_device_deinit(hapd->wps_upnp, hapd); 1304 } 1305 1306 #endif /* CONFIG_WPS_UPNP */ 1307 1308 1309 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr, 1310 char *buf, size_t buflen) 1311 { 1312 if (hapd->wps == NULL) 1313 return 0; 1314 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen); 1315 } 1316 1317 1318 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx) 1319 { 1320 struct hostapd_data *hapd = eloop_data; 1321 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out"); 1322 hostapd_wps_ap_pin_disable(hapd); 1323 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED); 1324 } 1325 1326 1327 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout) 1328 { 1329 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout); 1330 hapd->ap_pin_failures = 0; 1331 hapd->ap_pin_failures_consecutive = 0; 1332 hapd->conf->ap_setup_locked = 0; 1333 if (hapd->wps->ap_setup_locked) { 1334 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); 1335 hapd->wps->ap_setup_locked = 0; 1336 wps_registrar_update_ie(hapd->wps->registrar); 1337 } 1338 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1339 if (timeout > 0) 1340 eloop_register_timeout(timeout, 0, 1341 hostapd_wps_ap_pin_timeout, hapd, NULL); 1342 } 1343 1344 1345 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx) 1346 { 1347 os_free(hapd->conf->ap_pin); 1348 hapd->conf->ap_pin = NULL; 1349 #ifdef CONFIG_WPS_UPNP 1350 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL); 1351 #endif /* CONFIG_WPS_UPNP */ 1352 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1353 return 0; 1354 } 1355 1356 1357 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd) 1358 { 1359 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN"); 1360 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL); 1361 } 1362 1363 1364 struct wps_ap_pin_data { 1365 char pin_txt[9]; 1366 int timeout; 1367 }; 1368 1369 1370 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx) 1371 { 1372 struct wps_ap_pin_data *data = ctx; 1373 os_free(hapd->conf->ap_pin); 1374 hapd->conf->ap_pin = os_strdup(data->pin_txt); 1375 #ifdef CONFIG_WPS_UPNP 1376 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt); 1377 #endif /* CONFIG_WPS_UPNP */ 1378 hostapd_wps_ap_pin_enable(hapd, data->timeout); 1379 return 0; 1380 } 1381 1382 1383 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout) 1384 { 1385 unsigned int pin; 1386 struct wps_ap_pin_data data; 1387 1388 pin = wps_generate_pin(); 1389 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin); 1390 data.timeout = timeout; 1391 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); 1392 return hapd->conf->ap_pin; 1393 } 1394 1395 1396 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd) 1397 { 1398 return hapd->conf->ap_pin; 1399 } 1400 1401 1402 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin, 1403 int timeout) 1404 { 1405 struct wps_ap_pin_data data; 1406 int ret; 1407 1408 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin); 1409 if (ret < 0 || ret >= (int) sizeof(data.pin_txt)) 1410 return -1; 1411 data.timeout = timeout; 1412 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); 1413 } 1414 1415 1416 static int wps_update_ie(struct hostapd_data *hapd, void *ctx) 1417 { 1418 if (hapd->wps) 1419 wps_registrar_update_ie(hapd->wps->registrar); 1420 return 0; 1421 } 1422 1423 1424 void hostapd_wps_update_ie(struct hostapd_data *hapd) 1425 { 1426 hostapd_wps_for_each(hapd, wps_update_ie, NULL); 1427 } 1428 1429 1430 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid, 1431 const char *auth, const char *encr, const char *key) 1432 { 1433 struct wps_credential cred; 1434 size_t len; 1435 1436 os_memset(&cred, 0, sizeof(cred)); 1437 1438 len = os_strlen(ssid); 1439 if ((len & 1) || len > 2 * sizeof(cred.ssid) || 1440 hexstr2bin(ssid, cred.ssid, len / 2)) 1441 return -1; 1442 cred.ssid_len = len / 2; 1443 1444 if (os_strncmp(auth, "OPEN", 4) == 0) 1445 cred.auth_type = WPS_AUTH_OPEN; 1446 else if (os_strncmp(auth, "WPAPSK", 6) == 0) 1447 cred.auth_type = WPS_AUTH_WPAPSK; 1448 else if (os_strncmp(auth, "WPA2PSK", 7) == 0) 1449 cred.auth_type = WPS_AUTH_WPA2PSK; 1450 else 1451 return -1; 1452 1453 if (encr) { 1454 if (os_strncmp(encr, "NONE", 4) == 0) 1455 cred.encr_type = WPS_ENCR_NONE; 1456 else if (os_strncmp(encr, "WEP", 3) == 0) 1457 cred.encr_type = WPS_ENCR_WEP; 1458 else if (os_strncmp(encr, "TKIP", 4) == 0) 1459 cred.encr_type = WPS_ENCR_TKIP; 1460 else if (os_strncmp(encr, "CCMP", 4) == 0) 1461 cred.encr_type = WPS_ENCR_AES; 1462 else 1463 return -1; 1464 } else 1465 cred.encr_type = WPS_ENCR_NONE; 1466 1467 if (key) { 1468 len = os_strlen(key); 1469 if ((len & 1) || len > 2 * sizeof(cred.key) || 1470 hexstr2bin(key, cred.key, len / 2)) 1471 return -1; 1472 cred.key_len = len / 2; 1473 } 1474 1475 return wps_registrar_config_ap(hapd->wps->registrar, &cred); 1476 } 1477 1478 1479 #ifdef CONFIG_WPS_NFC 1480 1481 struct wps_nfc_password_token_data { 1482 const u8 *oob_dev_pw; 1483 size_t oob_dev_pw_len; 1484 int added; 1485 }; 1486 1487 1488 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx) 1489 { 1490 struct wps_nfc_password_token_data *data = ctx; 1491 int ret; 1492 1493 if (hapd->wps == NULL) 1494 return 0; 1495 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar, 1496 data->oob_dev_pw, 1497 data->oob_dev_pw_len); 1498 if (ret == 0) 1499 data->added++; 1500 return ret; 1501 } 1502 1503 1504 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd, 1505 struct wps_parse_attr *attr) 1506 { 1507 struct wps_nfc_password_token_data data; 1508 1509 data.oob_dev_pw = attr->oob_dev_password; 1510 data.oob_dev_pw_len = attr->oob_dev_password_len; 1511 data.added = 0; 1512 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0) 1513 return -1; 1514 return data.added ? 0 : -1; 1515 } 1516 1517 1518 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd, 1519 const struct wpabuf *wps) 1520 { 1521 struct wps_parse_attr attr; 1522 1523 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps); 1524 1525 if (wps_parse_msg(wps, &attr)) { 1526 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag"); 1527 return -1; 1528 } 1529 1530 if (attr.oob_dev_password) 1531 return hostapd_wps_add_nfc_password_token(hapd, &attr); 1532 1533 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag"); 1534 return -1; 1535 } 1536 1537 1538 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd, 1539 const struct wpabuf *data) 1540 { 1541 const struct wpabuf *wps = data; 1542 struct wpabuf *tmp = NULL; 1543 int ret; 1544 1545 if (wpabuf_len(data) < 4) 1546 return -1; 1547 1548 if (*wpabuf_head_u8(data) != 0x10) { 1549 /* Assume this contains full NDEF record */ 1550 tmp = ndef_parse_wifi(data); 1551 if (tmp == NULL) { 1552 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF"); 1553 return -1; 1554 } 1555 wps = tmp; 1556 } 1557 1558 ret = hostapd_wps_nfc_tag_process(hapd, wps); 1559 wpabuf_free(tmp); 1560 return ret; 1561 } 1562 1563 1564 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, 1565 int ndef) 1566 { 1567 struct wpabuf *ret; 1568 1569 if (hapd->wps == NULL) 1570 return NULL; 1571 1572 ret = wps_get_oob_cred(hapd->wps); 1573 if (ndef && ret) { 1574 struct wpabuf *tmp; 1575 tmp = ndef_build_wifi(ret); 1576 wpabuf_free(ret); 1577 if (tmp == NULL) 1578 return NULL; 1579 ret = tmp; 1580 } 1581 1582 return ret; 1583 } 1584 1585 1586 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) 1587 { 1588 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, 1589 &hapd->conf->wps_nfc_dh_pubkey, 1590 &hapd->conf->wps_nfc_dh_privkey, 1591 &hapd->conf->wps_nfc_dev_pw); 1592 } 1593 1594 1595 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd) 1596 { 1597 struct wps_context *wps = hapd->wps; 1598 1599 if (wps == NULL) 1600 return -1; 1601 1602 if (!hapd->conf->wps_nfc_dh_pubkey || 1603 !hapd->conf->wps_nfc_dh_privkey || 1604 !hapd->conf->wps_nfc_dev_pw || 1605 !hapd->conf->wps_nfc_dev_pw_id) 1606 return -1; 1607 1608 hostapd_wps_nfc_clear(wps); 1609 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id; 1610 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); 1611 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); 1612 wps->ap_nfc_dev_pw = wpabuf_dup(hapd->conf->wps_nfc_dev_pw); 1613 1614 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey || 1615 !wps->ap_nfc_dev_pw) { 1616 hostapd_wps_nfc_clear(wps); 1617 return -1; 1618 } 1619 1620 return 0; 1621 } 1622 1623 1624 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd) 1625 { 1626 hostapd_wps_nfc_clear(hapd->wps); 1627 } 1628 1629 #endif /* CONFIG_WPS_NFC */ 1630