wps_hostapd.c revision d5e4923d04122f81300fa68fb07d64ede28fd44d
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" 33static int hostapd_wps_upnp_init(struct hostapd_data *hapd, 34 struct wps_context *wps); 35static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd); 36#endif /* CONFIG_WPS_UPNP */ 37 38static 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); 42static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx); 43 44 45struct wps_for_each_data { 46 int (*func)(struct hostapd_data *h, void *ctx); 47 void *ctx; 48}; 49 50 51static 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 69static 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 85static 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 132static 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 146static 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 185struct 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 192static 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 202static 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 224static 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 247static int str_starts(const char *str, const char *start) 248{ 249 return os_strncmp(str, start, os_strlen(start)) == 0; 250} 251 252 253static 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 266static 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 280static 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 515static 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 522static 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 538static 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 590static 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 597static 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 614static 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 620static 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 626static 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 643static 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 688static 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 700static 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 719static 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 732static int count_interface_cb(struct hostapd_iface *iface, void *ctx) 733{ 734 int *count= ctx; 735 (*count)++; 736 return 0; 737} 738 739 740static 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 752static 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 777int 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 (cfg.dualband) 961 wpa_printf(MSG_DEBUG, "WPS: Dualband AP"); 962 963 wps->registrar = wps_registrar_init(wps, &cfg); 964 if (wps->registrar == NULL) { 965 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar"); 966 os_free(wps->network_key); 967 os_free(wps); 968 return -1; 969 } 970 971#ifdef CONFIG_WPS_UPNP 972 wps->friendly_name = hapd->conf->friendly_name; 973 wps->manufacturer_url = hapd->conf->manufacturer_url; 974 wps->model_description = hapd->conf->model_description; 975 wps->model_url = hapd->conf->model_url; 976 wps->upc = hapd->conf->upc; 977#endif /* CONFIG_WPS_UPNP */ 978 979 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd); 980 981 hapd->wps = wps; 982 983 return 0; 984} 985 986 987int hostapd_init_wps_complete(struct hostapd_data *hapd) 988{ 989 struct wps_context *wps = hapd->wps; 990 991 if (wps == NULL) 992 return 0; 993 994#ifdef CONFIG_WPS_UPNP 995 if (hostapd_wps_upnp_init(hapd, wps) < 0) { 996 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP"); 997 wps_registrar_deinit(wps->registrar); 998 os_free(wps->network_key); 999 os_free(wps); 1000 hapd->wps = NULL; 1001 return -1; 1002 } 1003#endif /* CONFIG_WPS_UPNP */ 1004 1005 return 0; 1006} 1007 1008 1009static void hostapd_wps_nfc_clear(struct wps_context *wps) 1010{ 1011#ifdef CONFIG_WPS_NFC 1012 wps->ap_nfc_dev_pw_id = 0; 1013 wpabuf_free(wps->ap_nfc_dh_pubkey); 1014 wps->ap_nfc_dh_pubkey = NULL; 1015 wpabuf_free(wps->ap_nfc_dh_privkey); 1016 wps->ap_nfc_dh_privkey = NULL; 1017 wpabuf_free(wps->ap_nfc_dev_pw); 1018 wps->ap_nfc_dev_pw = NULL; 1019#endif /* CONFIG_WPS_NFC */ 1020} 1021 1022 1023void hostapd_deinit_wps(struct hostapd_data *hapd) 1024{ 1025 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); 1026 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1027 if (hapd->wps == NULL) 1028 return; 1029#ifdef CONFIG_WPS_UPNP 1030 hostapd_wps_upnp_deinit(hapd); 1031#endif /* CONFIG_WPS_UPNP */ 1032 wps_registrar_deinit(hapd->wps->registrar); 1033 os_free(hapd->wps->network_key); 1034 wps_device_data_free(&hapd->wps->dev); 1035 wpabuf_free(hapd->wps->dh_pubkey); 1036 wpabuf_free(hapd->wps->dh_privkey); 1037 wps_free_pending_msgs(hapd->wps->upnp_msgs); 1038 hostapd_wps_nfc_clear(hapd->wps); 1039 os_free(hapd->wps); 1040 hapd->wps = NULL; 1041 hostapd_wps_clear_ies(hapd); 1042} 1043 1044 1045void hostapd_update_wps(struct hostapd_data *hapd) 1046{ 1047 if (hapd->wps == NULL) 1048 return; 1049 1050#ifdef CONFIG_WPS_UPNP 1051 hapd->wps->friendly_name = hapd->conf->friendly_name; 1052 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url; 1053 hapd->wps->model_description = hapd->conf->model_description; 1054 hapd->wps->model_url = hapd->conf->model_url; 1055 hapd->wps->upc = hapd->conf->upc; 1056#endif /* CONFIG_WPS_UPNP */ 1057 1058 hostapd_wps_set_vendor_ext(hapd, hapd->wps); 1059 1060 if (hapd->conf->wps_state) 1061 wps_registrar_update_ie(hapd->wps->registrar); 1062 else 1063 hostapd_deinit_wps(hapd); 1064} 1065 1066 1067struct wps_add_pin_data { 1068 const u8 *addr; 1069 const u8 *uuid; 1070 const u8 *pin; 1071 size_t pin_len; 1072 int timeout; 1073 int added; 1074}; 1075 1076 1077static int wps_add_pin(struct hostapd_data *hapd, void *ctx) 1078{ 1079 struct wps_add_pin_data *data = ctx; 1080 int ret; 1081 1082 if (hapd->wps == NULL) 1083 return 0; 1084 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr, 1085 data->uuid, data->pin, data->pin_len, 1086 data->timeout); 1087 if (ret == 0) 1088 data->added++; 1089 return ret; 1090} 1091 1092 1093int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr, 1094 const char *uuid, const char *pin, int timeout) 1095{ 1096 u8 u[UUID_LEN]; 1097 struct wps_add_pin_data data; 1098 1099 data.addr = addr; 1100 data.uuid = u; 1101 data.pin = (const u8 *) pin; 1102 data.pin_len = os_strlen(pin); 1103 data.timeout = timeout; 1104 data.added = 0; 1105 1106 if (os_strcmp(uuid, "any") == 0) 1107 data.uuid = NULL; 1108 else { 1109 if (uuid_str2bin(uuid, u)) 1110 return -1; 1111 data.uuid = u; 1112 } 1113 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0) 1114 return -1; 1115 return data.added ? 0 : -1; 1116} 1117 1118 1119static int wps_button_pushed(struct hostapd_data *hapd, void *ctx) 1120{ 1121 const u8 *p2p_dev_addr = ctx; 1122 if (hapd->wps == NULL) 1123 return 0; 1124 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr); 1125} 1126 1127 1128int hostapd_wps_button_pushed(struct hostapd_data *hapd, 1129 const u8 *p2p_dev_addr) 1130{ 1131 return hostapd_wps_for_each(hapd, wps_button_pushed, 1132 (void *) p2p_dev_addr); 1133} 1134 1135 1136static int wps_cancel(struct hostapd_data *hapd, void *ctx) 1137{ 1138 if (hapd->wps == NULL) 1139 return 0; 1140 1141 wps_registrar_wps_cancel(hapd->wps->registrar); 1142 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL); 1143 1144 return 0; 1145} 1146 1147 1148int hostapd_wps_cancel(struct hostapd_data *hapd) 1149{ 1150 return hostapd_wps_for_each(hapd, wps_cancel, NULL); 1151} 1152 1153 1154static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, 1155 const u8 *bssid, 1156 const u8 *ie, size_t ie_len, 1157 int ssi_signal) 1158{ 1159 struct hostapd_data *hapd = ctx; 1160 struct wpabuf *wps_ie; 1161 struct ieee802_11_elems elems; 1162 1163 if (hapd->wps == NULL) 1164 return 0; 1165 1166 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) { 1167 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from " 1168 MACSTR, MAC2STR(addr)); 1169 return 0; 1170 } 1171 1172 if (elems.ssid && elems.ssid_len > 0 && 1173 (elems.ssid_len != hapd->conf->ssid.ssid_len || 1174 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 1175 0)) 1176 return 0; /* Not for us */ 1177 1178 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); 1179 if (wps_ie == NULL) 1180 return 0; 1181 if (wps_validate_probe_req(wps_ie, addr) < 0) { 1182 wpabuf_free(wps_ie); 1183 return 0; 1184 } 1185 1186 if (wpabuf_len(wps_ie) > 0) { 1187 int p2p_wildcard = 0; 1188#ifdef CONFIG_P2P 1189 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN && 1190 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, 1191 P2P_WILDCARD_SSID_LEN) == 0) 1192 p2p_wildcard = 1; 1193#endif /* CONFIG_P2P */ 1194 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie, 1195 p2p_wildcard); 1196#ifdef CONFIG_WPS_UPNP 1197 /* FIX: what exactly should be included in the WLANEvent? 1198 * WPS attributes? Full ProbeReq frame? */ 1199 if (!p2p_wildcard) 1200 upnp_wps_device_send_wlan_event( 1201 hapd->wps_upnp, addr, 1202 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie); 1203#endif /* CONFIG_WPS_UPNP */ 1204 } 1205 1206 wpabuf_free(wps_ie); 1207 1208 return 0; 1209} 1210 1211 1212#ifdef CONFIG_WPS_UPNP 1213 1214static int hostapd_rx_req_put_wlan_response( 1215 void *priv, enum upnp_wps_wlanevent_type ev_type, 1216 const u8 *mac_addr, const struct wpabuf *msg, 1217 enum wps_msg_type msg_type) 1218{ 1219 struct hostapd_data *hapd = priv; 1220 struct sta_info *sta; 1221 struct upnp_pending_message *p; 1222 1223 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr=" 1224 MACSTR, ev_type, MAC2STR(mac_addr)); 1225 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage", 1226 wpabuf_head(msg), wpabuf_len(msg)); 1227 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) { 1228 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected " 1229 "PutWLANResponse WLANEventType %d", ev_type); 1230 return -1; 1231 } 1232 1233 /* 1234 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC 1235 * server implementation for delivery to the peer. 1236 */ 1237 1238 sta = ap_get_sta(hapd, mac_addr); 1239#ifndef CONFIG_WPS_STRICT 1240 if (!sta) { 1241 /* 1242 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC: 1243 * Pick STA that is in an ongoing WPS registration without 1244 * checking the MAC address. 1245 */ 1246 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based " 1247 "on NewWLANEventMAC; try wildcard match"); 1248 for (sta = hapd->sta_list; sta; sta = sta->next) { 1249 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS)) 1250 break; 1251 } 1252 } 1253#endif /* CONFIG_WPS_STRICT */ 1254 1255 if (!sta || !(sta->flags & WLAN_STA_WPS)) { 1256 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found"); 1257 return 0; 1258 } 1259 1260 p = os_zalloc(sizeof(*p)); 1261 if (p == NULL) 1262 return -1; 1263 os_memcpy(p->addr, sta->addr, ETH_ALEN); 1264 p->msg = wpabuf_dup(msg); 1265 p->type = msg_type; 1266 p->next = hapd->wps->upnp_msgs; 1267 hapd->wps->upnp_msgs = p; 1268 1269 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap); 1270} 1271 1272 1273static int hostapd_wps_upnp_init(struct hostapd_data *hapd, 1274 struct wps_context *wps) 1275{ 1276 struct upnp_wps_device_ctx *ctx; 1277 1278 if (!hapd->conf->upnp_iface) 1279 return 0; 1280 ctx = os_zalloc(sizeof(*ctx)); 1281 if (ctx == NULL) 1282 return -1; 1283 1284 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response; 1285 if (hapd->conf->ap_pin) 1286 ctx->ap_pin = os_strdup(hapd->conf->ap_pin); 1287 1288 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd, 1289 hapd->conf->upnp_iface); 1290 if (hapd->wps_upnp == NULL) 1291 return -1; 1292 wps->wps_upnp = hapd->wps_upnp; 1293 1294 return 0; 1295} 1296 1297 1298static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd) 1299{ 1300 upnp_wps_device_deinit(hapd->wps_upnp, hapd); 1301} 1302 1303#endif /* CONFIG_WPS_UPNP */ 1304 1305 1306int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr, 1307 char *buf, size_t buflen) 1308{ 1309 if (hapd->wps == NULL) 1310 return 0; 1311 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen); 1312} 1313 1314 1315static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx) 1316{ 1317 struct hostapd_data *hapd = eloop_data; 1318 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out"); 1319 hostapd_wps_ap_pin_disable(hapd); 1320 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED); 1321} 1322 1323 1324static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout) 1325{ 1326 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout); 1327 hapd->ap_pin_failures = 0; 1328 hapd->ap_pin_failures_consecutive = 0; 1329 hapd->conf->ap_setup_locked = 0; 1330 if (hapd->wps->ap_setup_locked) { 1331 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); 1332 hapd->wps->ap_setup_locked = 0; 1333 wps_registrar_update_ie(hapd->wps->registrar); 1334 } 1335 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1336 if (timeout > 0) 1337 eloop_register_timeout(timeout, 0, 1338 hostapd_wps_ap_pin_timeout, hapd, NULL); 1339} 1340 1341 1342static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx) 1343{ 1344 os_free(hapd->conf->ap_pin); 1345 hapd->conf->ap_pin = NULL; 1346#ifdef CONFIG_WPS_UPNP 1347 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL); 1348#endif /* CONFIG_WPS_UPNP */ 1349 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); 1350 return 0; 1351} 1352 1353 1354void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd) 1355{ 1356 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN"); 1357 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL); 1358} 1359 1360 1361struct wps_ap_pin_data { 1362 char pin_txt[9]; 1363 int timeout; 1364}; 1365 1366 1367static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx) 1368{ 1369 struct wps_ap_pin_data *data = ctx; 1370 os_free(hapd->conf->ap_pin); 1371 hapd->conf->ap_pin = os_strdup(data->pin_txt); 1372#ifdef CONFIG_WPS_UPNP 1373 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt); 1374#endif /* CONFIG_WPS_UPNP */ 1375 hostapd_wps_ap_pin_enable(hapd, data->timeout); 1376 return 0; 1377} 1378 1379 1380const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout) 1381{ 1382 unsigned int pin; 1383 struct wps_ap_pin_data data; 1384 1385 pin = wps_generate_pin(); 1386 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin); 1387 data.timeout = timeout; 1388 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); 1389 return hapd->conf->ap_pin; 1390} 1391 1392 1393const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd) 1394{ 1395 return hapd->conf->ap_pin; 1396} 1397 1398 1399int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin, 1400 int timeout) 1401{ 1402 struct wps_ap_pin_data data; 1403 int ret; 1404 1405 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin); 1406 if (ret < 0 || ret >= (int) sizeof(data.pin_txt)) 1407 return -1; 1408 data.timeout = timeout; 1409 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); 1410} 1411 1412 1413static int wps_update_ie(struct hostapd_data *hapd, void *ctx) 1414{ 1415 if (hapd->wps) 1416 wps_registrar_update_ie(hapd->wps->registrar); 1417 return 0; 1418} 1419 1420 1421void hostapd_wps_update_ie(struct hostapd_data *hapd) 1422{ 1423 hostapd_wps_for_each(hapd, wps_update_ie, NULL); 1424} 1425 1426 1427int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid, 1428 const char *auth, const char *encr, const char *key) 1429{ 1430 struct wps_credential cred; 1431 size_t len; 1432 1433 os_memset(&cred, 0, sizeof(cred)); 1434 1435 len = os_strlen(ssid); 1436 if ((len & 1) || len > 2 * sizeof(cred.ssid) || 1437 hexstr2bin(ssid, cred.ssid, len / 2)) 1438 return -1; 1439 cred.ssid_len = len / 2; 1440 1441 if (os_strncmp(auth, "OPEN", 4) == 0) 1442 cred.auth_type = WPS_AUTH_OPEN; 1443 else if (os_strncmp(auth, "WPAPSK", 6) == 0) 1444 cred.auth_type = WPS_AUTH_WPAPSK; 1445 else if (os_strncmp(auth, "WPA2PSK", 7) == 0) 1446 cred.auth_type = WPS_AUTH_WPA2PSK; 1447 else 1448 return -1; 1449 1450 if (encr) { 1451 if (os_strncmp(encr, "NONE", 4) == 0) 1452 cred.encr_type = WPS_ENCR_NONE; 1453 else if (os_strncmp(encr, "WEP", 3) == 0) 1454 cred.encr_type = WPS_ENCR_WEP; 1455 else if (os_strncmp(encr, "TKIP", 4) == 0) 1456 cred.encr_type = WPS_ENCR_TKIP; 1457 else if (os_strncmp(encr, "CCMP", 4) == 0) 1458 cred.encr_type = WPS_ENCR_AES; 1459 else 1460 return -1; 1461 } else 1462 cred.encr_type = WPS_ENCR_NONE; 1463 1464 if (key) { 1465 len = os_strlen(key); 1466 if ((len & 1) || len > 2 * sizeof(cred.key) || 1467 hexstr2bin(key, cred.key, len / 2)) 1468 return -1; 1469 cred.key_len = len / 2; 1470 } 1471 1472 return wps_registrar_config_ap(hapd->wps->registrar, &cred); 1473} 1474 1475 1476#ifdef CONFIG_WPS_NFC 1477 1478struct wps_nfc_password_token_data { 1479 const u8 *oob_dev_pw; 1480 size_t oob_dev_pw_len; 1481 int added; 1482}; 1483 1484 1485static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx) 1486{ 1487 struct wps_nfc_password_token_data *data = ctx; 1488 int ret; 1489 1490 if (hapd->wps == NULL) 1491 return 0; 1492 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar, 1493 data->oob_dev_pw, 1494 data->oob_dev_pw_len); 1495 if (ret == 0) 1496 data->added++; 1497 return ret; 1498} 1499 1500 1501static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd, 1502 struct wps_parse_attr *attr) 1503{ 1504 struct wps_nfc_password_token_data data; 1505 1506 data.oob_dev_pw = attr->oob_dev_password; 1507 data.oob_dev_pw_len = attr->oob_dev_password_len; 1508 data.added = 0; 1509 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0) 1510 return -1; 1511 return data.added ? 0 : -1; 1512} 1513 1514 1515static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd, 1516 const struct wpabuf *wps) 1517{ 1518 struct wps_parse_attr attr; 1519 1520 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps); 1521 1522 if (wps_parse_msg(wps, &attr)) { 1523 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag"); 1524 return -1; 1525 } 1526 1527 if (attr.oob_dev_password) 1528 return hostapd_wps_add_nfc_password_token(hapd, &attr); 1529 1530 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag"); 1531 return -1; 1532} 1533 1534 1535int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd, 1536 const struct wpabuf *data) 1537{ 1538 const struct wpabuf *wps = data; 1539 struct wpabuf *tmp = NULL; 1540 int ret; 1541 1542 if (wpabuf_len(data) < 4) 1543 return -1; 1544 1545 if (*wpabuf_head_u8(data) != 0x10) { 1546 /* Assume this contains full NDEF record */ 1547 tmp = ndef_parse_wifi(data); 1548 if (tmp == NULL) { 1549 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF"); 1550 return -1; 1551 } 1552 wps = tmp; 1553 } 1554 1555 ret = hostapd_wps_nfc_tag_process(hapd, wps); 1556 wpabuf_free(tmp); 1557 return ret; 1558} 1559 1560 1561struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, 1562 int ndef) 1563{ 1564 struct wpabuf *ret; 1565 1566 if (hapd->wps == NULL) 1567 return NULL; 1568 1569 ret = wps_get_oob_cred(hapd->wps); 1570 if (ndef && ret) { 1571 struct wpabuf *tmp; 1572 tmp = ndef_build_wifi(ret); 1573 wpabuf_free(ret); 1574 if (tmp == NULL) 1575 return NULL; 1576 ret = tmp; 1577 } 1578 1579 return ret; 1580} 1581 1582 1583struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) 1584{ 1585 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, 1586 &hapd->conf->wps_nfc_dh_pubkey, 1587 &hapd->conf->wps_nfc_dh_privkey, 1588 &hapd->conf->wps_nfc_dev_pw); 1589} 1590 1591 1592int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd) 1593{ 1594 struct wps_context *wps = hapd->wps; 1595 1596 if (wps == NULL) 1597 return -1; 1598 1599 if (!hapd->conf->wps_nfc_dh_pubkey || 1600 !hapd->conf->wps_nfc_dh_privkey || 1601 !hapd->conf->wps_nfc_dev_pw || 1602 !hapd->conf->wps_nfc_dev_pw_id) 1603 return -1; 1604 1605 hostapd_wps_nfc_clear(wps); 1606 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id; 1607 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); 1608 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); 1609 wps->ap_nfc_dev_pw = wpabuf_dup(hapd->conf->wps_nfc_dev_pw); 1610 1611 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey || 1612 !wps->ap_nfc_dev_pw) { 1613 hostapd_wps_nfc_clear(wps); 1614 return -1; 1615 } 1616 1617 return 0; 1618} 1619 1620 1621void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd) 1622{ 1623 hostapd_wps_nfc_clear(hapd->wps); 1624} 1625 1626#endif /* CONFIG_WPS_NFC */ 1627