1 /* 2 * hostapd / UNIX domain socket -based control interface 3 * Copyright (c) 2004-2018, 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 #ifndef CONFIG_NATIVE_WINDOWS 12 13 #ifdef CONFIG_TESTING_OPTIONS 14 #ifdef __NetBSD__ 15 #include <net/if_ether.h> 16 #else 17 #include <net/ethernet.h> 18 #endif 19 #include <netinet/ip.h> 20 #endif /* CONFIG_TESTING_OPTIONS */ 21 22 #include <sys/un.h> 23 #include <sys/stat.h> 24 #include <stddef.h> 25 26 #ifdef CONFIG_CTRL_IFACE_UDP 27 #include <netdb.h> 28 #endif /* CONFIG_CTRL_IFACE_UDP */ 29 30 #include "utils/common.h" 31 #include "utils/eloop.h" 32 #include "utils/module_tests.h" 33 #include "common/version.h" 34 #include "common/ieee802_11_defs.h" 35 #include "common/ctrl_iface_common.h" 36 #ifdef CONFIG_DPP 37 #include "common/dpp.h" 38 #endif /* CONFIG_DPP */ 39 #include "common/wpa_ctrl.h" 40 #include "common/ptksa_cache.h" 41 #include "crypto/tls.h" 42 #include "drivers/driver.h" 43 #include "eapol_auth/eapol_auth_sm.h" 44 #include "radius/radius_client.h" 45 #include "radius/radius_server.h" 46 #include "l2_packet/l2_packet.h" 47 #include "ap/hostapd.h" 48 #include "ap/ap_config.h" 49 #include "ap/ieee802_1x.h" 50 #include "ap/wpa_auth.h" 51 #include "ap/pmksa_cache_auth.h" 52 #include "ap/ieee802_11.h" 53 #include "ap/sta_info.h" 54 #include "ap/wps_hostapd.h" 55 #include "ap/ctrl_iface_ap.h" 56 #include "ap/ap_drv_ops.h" 57 #include "ap/hs20.h" 58 #include "ap/wnm_ap.h" 59 #include "ap/wpa_auth.h" 60 #include "ap/beacon.h" 61 #include "ap/neighbor_db.h" 62 #include "ap/rrm.h" 63 #include "ap/dpp_hostapd.h" 64 #include "ap/dfs.h" 65 #include "wps/wps_defs.h" 66 #include "wps/wps.h" 67 #include "fst/fst_ctrl_iface.h" 68 #include "config_file.h" 69 #include "ctrl_iface.h" 70 71 72 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256 73 74 #ifdef CONFIG_CTRL_IFACE_UDP 75 #define HOSTAPD_CTRL_IFACE_PORT 8877 76 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50 77 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878 78 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50 79 #endif /* CONFIG_CTRL_IFACE_UDP */ 80 81 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, 82 enum wpa_msg_type type, 83 const char *buf, size_t len); 84 85 86 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd, 87 struct sockaddr_storage *from, 88 socklen_t fromlen, const char *input) 89 { 90 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input); 91 } 92 93 94 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd, 95 struct sockaddr_storage *from, 96 socklen_t fromlen) 97 { 98 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen); 99 } 100 101 102 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd, 103 struct sockaddr_storage *from, 104 socklen_t fromlen, 105 char *level) 106 { 107 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level); 108 } 109 110 111 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd, 112 const char *txtaddr) 113 { 114 u8 addr[ETH_ALEN]; 115 struct sta_info *sta; 116 117 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr); 118 119 if (hwaddr_aton(txtaddr, addr)) 120 return -1; 121 122 sta = ap_get_sta(hapd, addr); 123 if (sta) 124 return 0; 125 126 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface " 127 "notification", MAC2STR(addr)); 128 sta = ap_sta_add(hapd, addr); 129 if (sta == NULL) 130 return -1; 131 132 hostapd_new_assoc_sta(hapd, sta, 0); 133 return 0; 134 } 135 136 137 #ifdef NEED_AP_MLME 138 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd, 139 const char *txtaddr) 140 { 141 u8 addr[ETH_ALEN]; 142 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN]; 143 144 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr); 145 146 if (hwaddr_aton(txtaddr, addr) || 147 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) 148 return -1; 149 150 ieee802_11_send_sa_query_req(hapd, addr, trans_id); 151 152 return 0; 153 } 154 #endif /* NEED_AP_MLME */ 155 156 157 #ifdef CONFIG_WPS 158 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt) 159 { 160 char *pin = os_strchr(txt, ' '); 161 char *timeout_txt; 162 int timeout; 163 u8 addr_buf[ETH_ALEN], *addr = NULL; 164 char *pos; 165 166 if (pin == NULL) 167 return -1; 168 *pin++ = '\0'; 169 170 timeout_txt = os_strchr(pin, ' '); 171 if (timeout_txt) { 172 *timeout_txt++ = '\0'; 173 timeout = atoi(timeout_txt); 174 pos = os_strchr(timeout_txt, ' '); 175 if (pos) { 176 *pos++ = '\0'; 177 if (hwaddr_aton(pos, addr_buf) == 0) 178 addr = addr_buf; 179 } 180 } else 181 timeout = 0; 182 183 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout); 184 } 185 186 187 static int hostapd_ctrl_iface_wps_check_pin( 188 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen) 189 { 190 char pin[9]; 191 size_t len; 192 char *pos; 193 int ret; 194 195 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN", 196 (u8 *) cmd, os_strlen(cmd)); 197 for (pos = cmd, len = 0; *pos != '\0'; pos++) { 198 if (*pos < '0' || *pos > '9') 199 continue; 200 pin[len++] = *pos; 201 if (len == 9) { 202 wpa_printf(MSG_DEBUG, "WPS: Too long PIN"); 203 return -1; 204 } 205 } 206 if (len != 4 && len != 8) { 207 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len); 208 return -1; 209 } 210 pin[len] = '\0'; 211 212 if (len == 8) { 213 unsigned int pin_val; 214 pin_val = atoi(pin); 215 if (!wps_pin_valid(pin_val)) { 216 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit"); 217 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n"); 218 if (os_snprintf_error(buflen, ret)) 219 return -1; 220 return ret; 221 } 222 } 223 224 ret = os_snprintf(buf, buflen, "%s", pin); 225 if (os_snprintf_error(buflen, ret)) 226 return -1; 227 228 return ret; 229 } 230 231 232 #ifdef CONFIG_WPS_NFC 233 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd, 234 char *pos) 235 { 236 size_t len; 237 struct wpabuf *buf; 238 int ret; 239 240 len = os_strlen(pos); 241 if (len & 0x01) 242 return -1; 243 len /= 2; 244 245 buf = wpabuf_alloc(len); 246 if (buf == NULL) 247 return -1; 248 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) { 249 wpabuf_free(buf); 250 return -1; 251 } 252 253 ret = hostapd_wps_nfc_tag_read(hapd, buf); 254 wpabuf_free(buf); 255 256 return ret; 257 } 258 259 260 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd, 261 char *cmd, char *reply, 262 size_t max_len) 263 { 264 int ndef; 265 struct wpabuf *buf; 266 int res; 267 268 if (os_strcmp(cmd, "WPS") == 0) 269 ndef = 0; 270 else if (os_strcmp(cmd, "NDEF") == 0) 271 ndef = 1; 272 else 273 return -1; 274 275 buf = hostapd_wps_nfc_config_token(hapd, ndef); 276 if (buf == NULL) 277 return -1; 278 279 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), 280 wpabuf_len(buf)); 281 reply[res++] = '\n'; 282 reply[res] = '\0'; 283 284 wpabuf_free(buf); 285 286 return res; 287 } 288 289 290 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd, 291 char *reply, size_t max_len, 292 int ndef) 293 { 294 struct wpabuf *buf; 295 int res; 296 297 buf = hostapd_wps_nfc_token_gen(hapd, ndef); 298 if (buf == NULL) 299 return -1; 300 301 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), 302 wpabuf_len(buf)); 303 reply[res++] = '\n'; 304 reply[res] = '\0'; 305 306 wpabuf_free(buf); 307 308 return res; 309 } 310 311 312 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd, 313 char *cmd, char *reply, 314 size_t max_len) 315 { 316 if (os_strcmp(cmd, "WPS") == 0) 317 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, 318 max_len, 0); 319 320 if (os_strcmp(cmd, "NDEF") == 0) 321 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, 322 max_len, 1); 323 324 if (os_strcmp(cmd, "enable") == 0) 325 return hostapd_wps_nfc_token_enable(hapd); 326 327 if (os_strcmp(cmd, "disable") == 0) { 328 hostapd_wps_nfc_token_disable(hapd); 329 return 0; 330 } 331 332 return -1; 333 } 334 335 336 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd, 337 char *cmd, char *reply, 338 size_t max_len) 339 { 340 struct wpabuf *buf; 341 int res; 342 char *pos; 343 int ndef; 344 345 pos = os_strchr(cmd, ' '); 346 if (pos == NULL) 347 return -1; 348 *pos++ = '\0'; 349 350 if (os_strcmp(cmd, "WPS") == 0) 351 ndef = 0; 352 else if (os_strcmp(cmd, "NDEF") == 0) 353 ndef = 1; 354 else 355 return -1; 356 357 if (os_strcmp(pos, "WPS-CR") == 0) 358 buf = hostapd_wps_nfc_hs_cr(hapd, ndef); 359 else 360 buf = NULL; 361 if (buf == NULL) 362 return -1; 363 364 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), 365 wpabuf_len(buf)); 366 reply[res++] = '\n'; 367 reply[res] = '\0'; 368 369 wpabuf_free(buf); 370 371 return res; 372 } 373 374 375 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd, 376 char *cmd) 377 { 378 size_t len; 379 struct wpabuf *req, *sel; 380 int ret; 381 char *pos, *role, *type, *pos2; 382 383 role = cmd; 384 pos = os_strchr(role, ' '); 385 if (pos == NULL) 386 return -1; 387 *pos++ = '\0'; 388 389 type = pos; 390 pos = os_strchr(type, ' '); 391 if (pos == NULL) 392 return -1; 393 *pos++ = '\0'; 394 395 pos2 = os_strchr(pos, ' '); 396 if (pos2 == NULL) 397 return -1; 398 *pos2++ = '\0'; 399 400 len = os_strlen(pos); 401 if (len & 0x01) 402 return -1; 403 len /= 2; 404 405 req = wpabuf_alloc(len); 406 if (req == NULL) 407 return -1; 408 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) { 409 wpabuf_free(req); 410 return -1; 411 } 412 413 len = os_strlen(pos2); 414 if (len & 0x01) { 415 wpabuf_free(req); 416 return -1; 417 } 418 len /= 2; 419 420 sel = wpabuf_alloc(len); 421 if (sel == NULL) { 422 wpabuf_free(req); 423 return -1; 424 } 425 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) { 426 wpabuf_free(req); 427 wpabuf_free(sel); 428 return -1; 429 } 430 431 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) { 432 ret = hostapd_wps_nfc_report_handover(hapd, req, sel); 433 } else { 434 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover " 435 "reported: role=%s type=%s", role, type); 436 ret = -1; 437 } 438 wpabuf_free(req); 439 wpabuf_free(sel); 440 441 return ret; 442 } 443 444 #endif /* CONFIG_WPS_NFC */ 445 446 447 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, 448 char *buf, size_t buflen) 449 { 450 int timeout = 300; 451 char *pos; 452 const char *pin_txt; 453 454 pos = os_strchr(txt, ' '); 455 if (pos) 456 *pos++ = '\0'; 457 458 if (os_strcmp(txt, "disable") == 0) { 459 hostapd_wps_ap_pin_disable(hapd); 460 return os_snprintf(buf, buflen, "OK\n"); 461 } 462 463 if (os_strcmp(txt, "random") == 0) { 464 if (pos) 465 timeout = atoi(pos); 466 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); 467 if (pin_txt == NULL) 468 return -1; 469 return os_snprintf(buf, buflen, "%s", pin_txt); 470 } 471 472 if (os_strcmp(txt, "get") == 0) { 473 pin_txt = hostapd_wps_ap_pin_get(hapd); 474 if (pin_txt == NULL) 475 return -1; 476 return os_snprintf(buf, buflen, "%s", pin_txt); 477 } 478 479 if (os_strcmp(txt, "set") == 0) { 480 char *pin; 481 if (pos == NULL) 482 return -1; 483 pin = pos; 484 pos = os_strchr(pos, ' '); 485 if (pos) { 486 *pos++ = '\0'; 487 timeout = atoi(pos); 488 } 489 if (os_strlen(pin) > buflen) 490 return -1; 491 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) 492 return -1; 493 return os_snprintf(buf, buflen, "%s", pin); 494 } 495 496 return -1; 497 } 498 499 500 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt) 501 { 502 char *pos; 503 char *ssid, *auth, *encr = NULL, *key = NULL; 504 505 ssid = txt; 506 pos = os_strchr(txt, ' '); 507 if (!pos) 508 return -1; 509 *pos++ = '\0'; 510 511 auth = pos; 512 pos = os_strchr(pos, ' '); 513 if (pos) { 514 *pos++ = '\0'; 515 encr = pos; 516 pos = os_strchr(pos, ' '); 517 if (pos) { 518 *pos++ = '\0'; 519 key = pos; 520 } 521 } 522 523 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key); 524 } 525 526 527 static const char * pbc_status_str(enum pbc_status status) 528 { 529 switch (status) { 530 case WPS_PBC_STATUS_DISABLE: 531 return "Disabled"; 532 case WPS_PBC_STATUS_ACTIVE: 533 return "Active"; 534 case WPS_PBC_STATUS_TIMEOUT: 535 return "Timed-out"; 536 case WPS_PBC_STATUS_OVERLAP: 537 return "Overlap"; 538 default: 539 return "Unknown"; 540 } 541 } 542 543 544 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd, 545 char *buf, size_t buflen) 546 { 547 int ret; 548 char *pos, *end; 549 550 pos = buf; 551 end = buf + buflen; 552 553 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n", 554 pbc_status_str(hapd->wps_stats.pbc_status)); 555 556 if (os_snprintf_error(end - pos, ret)) 557 return pos - buf; 558 pos += ret; 559 560 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n", 561 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ? 562 "Success": 563 (hapd->wps_stats.status == WPS_STATUS_FAILURE ? 564 "Failed" : "None"))); 565 566 if (os_snprintf_error(end - pos, ret)) 567 return pos - buf; 568 pos += ret; 569 570 /* If status == Failure - Add possible Reasons */ 571 if(hapd->wps_stats.status == WPS_STATUS_FAILURE && 572 hapd->wps_stats.failure_reason > 0) { 573 ret = os_snprintf(pos, end - pos, 574 "Failure Reason: %s\n", 575 wps_ei_str(hapd->wps_stats.failure_reason)); 576 577 if (os_snprintf_error(end - pos, ret)) 578 return pos - buf; 579 pos += ret; 580 } 581 582 if (hapd->wps_stats.status) { 583 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n", 584 MAC2STR(hapd->wps_stats.peer_addr)); 585 586 if (os_snprintf_error(end - pos, ret)) 587 return pos - buf; 588 pos += ret; 589 } 590 591 return pos - buf; 592 } 593 594 #endif /* CONFIG_WPS */ 595 596 #ifdef CONFIG_HS20 597 598 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd, 599 const char *cmd) 600 { 601 u8 addr[ETH_ALEN]; 602 const char *url; 603 604 if (hwaddr_aton(cmd, addr)) 605 return -1; 606 url = cmd + 17; 607 if (*url == '\0') { 608 url = NULL; 609 } else { 610 if (*url != ' ') 611 return -1; 612 url++; 613 if (*url == '\0') 614 url = NULL; 615 } 616 617 return hs20_send_wnm_notification(hapd, addr, 1, url); 618 } 619 620 621 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd, 622 const char *cmd) 623 { 624 u8 addr[ETH_ALEN]; 625 int code, reauth_delay, ret; 626 const char *pos; 627 size_t url_len; 628 struct wpabuf *req; 629 630 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */ 631 if (hwaddr_aton(cmd, addr)) 632 return -1; 633 634 pos = os_strchr(cmd, ' '); 635 if (pos == NULL) 636 return -1; 637 pos++; 638 code = atoi(pos); 639 640 pos = os_strchr(pos, ' '); 641 if (pos == NULL) 642 return -1; 643 pos++; 644 reauth_delay = atoi(pos); 645 646 url_len = 0; 647 pos = os_strchr(pos, ' '); 648 if (pos) { 649 pos++; 650 url_len = os_strlen(pos); 651 } 652 653 req = wpabuf_alloc(4 + url_len); 654 if (req == NULL) 655 return -1; 656 wpabuf_put_u8(req, code); 657 wpabuf_put_le16(req, reauth_delay); 658 wpabuf_put_u8(req, url_len); 659 if (pos) 660 wpabuf_put_data(req, pos, url_len); 661 662 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR 663 " to indicate imminent deauthentication (code=%d " 664 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay); 665 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req); 666 wpabuf_free(req); 667 return ret; 668 } 669 670 #endif /* CONFIG_HS20 */ 671 672 673 #ifdef CONFIG_INTERWORKING 674 675 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd, 676 const char *cmd) 677 { 678 u8 qos_map_set[16 + 2 * 21], count = 0; 679 const char *pos = cmd; 680 int val, ret; 681 682 for (;;) { 683 if (count == sizeof(qos_map_set)) { 684 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters"); 685 return -1; 686 } 687 688 val = atoi(pos); 689 if (val < 0 || val > 255) { 690 wpa_printf(MSG_INFO, "Invalid QoS Map Set"); 691 return -1; 692 } 693 694 qos_map_set[count++] = val; 695 pos = os_strchr(pos, ','); 696 if (!pos) 697 break; 698 pos++; 699 } 700 701 if (count < 16 || count & 1) { 702 wpa_printf(MSG_INFO, "Invalid QoS Map Set"); 703 return -1; 704 } 705 706 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count); 707 if (ret) { 708 wpa_printf(MSG_INFO, "Failed to set QoS Map Set"); 709 return -1; 710 } 711 712 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count); 713 hapd->conf->qos_map_set_len = count; 714 715 return 0; 716 } 717 718 719 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd, 720 const char *cmd) 721 { 722 u8 addr[ETH_ALEN]; 723 struct sta_info *sta; 724 struct wpabuf *buf; 725 u8 *qos_map_set = hapd->conf->qos_map_set; 726 u8 qos_map_set_len = hapd->conf->qos_map_set_len; 727 int ret; 728 729 if (!qos_map_set_len) { 730 wpa_printf(MSG_INFO, "QoS Map Set is not set"); 731 return -1; 732 } 733 734 if (hwaddr_aton(cmd, addr)) 735 return -1; 736 737 sta = ap_get_sta(hapd, addr); 738 if (sta == NULL) { 739 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found " 740 "for QoS Map Configuration message", 741 MAC2STR(addr)); 742 return -1; 743 } 744 745 if (!sta->qos_map_enabled) { 746 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate " 747 "support for QoS Map", MAC2STR(addr)); 748 return -1; 749 } 750 751 buf = wpabuf_alloc(2 + 2 + qos_map_set_len); 752 if (buf == NULL) 753 return -1; 754 755 wpabuf_put_u8(buf, WLAN_ACTION_QOS); 756 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG); 757 758 /* QoS Map Set Element */ 759 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET); 760 wpabuf_put_u8(buf, qos_map_set_len); 761 wpabuf_put_data(buf, qos_map_set, qos_map_set_len); 762 763 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr, 764 wpabuf_head(buf), wpabuf_len(buf)); 765 wpabuf_free(buf); 766 767 return ret; 768 } 769 770 #endif /* CONFIG_INTERWORKING */ 771 772 773 #ifdef CONFIG_WNM_AP 774 775 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd, 776 const char *cmd) 777 { 778 u8 addr[ETH_ALEN]; 779 int disassoc_timer; 780 struct sta_info *sta; 781 782 if (hwaddr_aton(cmd, addr)) 783 return -1; 784 if (cmd[17] != ' ') 785 return -1; 786 disassoc_timer = atoi(cmd + 17); 787 788 sta = ap_get_sta(hapd, addr); 789 if (sta == NULL) { 790 wpa_printf(MSG_DEBUG, "Station " MACSTR 791 " not found for disassociation imminent message", 792 MAC2STR(addr)); 793 return -1; 794 } 795 796 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer); 797 } 798 799 800 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd, 801 const char *cmd) 802 { 803 u8 addr[ETH_ALEN]; 804 const char *url, *timerstr; 805 int disassoc_timer; 806 struct sta_info *sta; 807 808 if (hwaddr_aton(cmd, addr)) 809 return -1; 810 811 sta = ap_get_sta(hapd, addr); 812 if (sta == NULL) { 813 wpa_printf(MSG_DEBUG, "Station " MACSTR 814 " not found for ESS disassociation imminent message", 815 MAC2STR(addr)); 816 return -1; 817 } 818 819 timerstr = cmd + 17; 820 if (*timerstr != ' ') 821 return -1; 822 timerstr++; 823 disassoc_timer = atoi(timerstr); 824 if (disassoc_timer < 0 || disassoc_timer > 65535) 825 return -1; 826 827 url = os_strchr(timerstr, ' '); 828 if (url == NULL) 829 return -1; 830 url++; 831 832 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer); 833 } 834 835 836 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd, 837 const char *cmd) 838 { 839 u8 addr[ETH_ALEN]; 840 const char *pos, *end; 841 int disassoc_timer = 0; 842 struct sta_info *sta; 843 u8 req_mode = 0, valid_int = 0x01, dialog_token = 0x01; 844 u8 bss_term_dur[12]; 845 char *url = NULL; 846 int ret; 847 u8 nei_rep[1000]; 848 int nei_len; 849 u8 mbo[10]; 850 size_t mbo_len = 0; 851 852 if (hwaddr_aton(cmd, addr)) { 853 wpa_printf(MSG_DEBUG, "Invalid STA MAC address"); 854 return -1; 855 } 856 857 sta = ap_get_sta(hapd, addr); 858 if (sta == NULL) { 859 wpa_printf(MSG_DEBUG, "Station " MACSTR 860 " not found for BSS TM Request message", 861 MAC2STR(addr)); 862 return -1; 863 } 864 865 pos = os_strstr(cmd, " disassoc_timer="); 866 if (pos) { 867 pos += 16; 868 disassoc_timer = atoi(pos); 869 if (disassoc_timer < 0 || disassoc_timer > 65535) { 870 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer"); 871 return -1; 872 } 873 } 874 875 pos = os_strstr(cmd, " valid_int="); 876 if (pos) { 877 pos += 11; 878 valid_int = atoi(pos); 879 } 880 881 pos = os_strstr(cmd, " dialog_token="); 882 if (pos) { 883 pos += 14; 884 dialog_token = atoi(pos); 885 } 886 887 pos = os_strstr(cmd, " bss_term="); 888 if (pos) { 889 pos += 10; 890 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED; 891 /* TODO: TSF configurable/learnable */ 892 bss_term_dur[0] = 4; /* Subelement ID */ 893 bss_term_dur[1] = 10; /* Length */ 894 os_memset(&bss_term_dur[2], 0, 8); 895 end = os_strchr(pos, ','); 896 if (end == NULL) { 897 wpa_printf(MSG_DEBUG, "Invalid bss_term data"); 898 return -1; 899 } 900 end++; 901 WPA_PUT_LE16(&bss_term_dur[10], atoi(end)); 902 } 903 904 nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep, 905 sizeof(nei_rep)); 906 if (nei_len < 0) 907 return -1; 908 909 pos = os_strstr(cmd, " url="); 910 if (pos) { 911 size_t len; 912 pos += 5; 913 end = os_strchr(pos, ' '); 914 if (end) 915 len = end - pos; 916 else 917 len = os_strlen(pos); 918 url = os_malloc(len + 1); 919 if (url == NULL) 920 return -1; 921 os_memcpy(url, pos, len); 922 url[len] = '\0'; 923 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT; 924 } 925 926 if (os_strstr(cmd, " pref=1")) 927 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED; 928 if (os_strstr(cmd, " abridged=1")) 929 req_mode |= WNM_BSS_TM_REQ_ABRIDGED; 930 if (os_strstr(cmd, " disassoc_imminent=1")) 931 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT; 932 933 #ifdef CONFIG_MBO 934 pos = os_strstr(cmd, "mbo="); 935 if (pos) { 936 unsigned int mbo_reason, cell_pref, reassoc_delay; 937 u8 *mbo_pos = mbo; 938 939 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason, 940 &reassoc_delay, &cell_pref); 941 if (ret != 3) { 942 wpa_printf(MSG_DEBUG, 943 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>"); 944 ret = -1; 945 goto fail; 946 } 947 948 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) { 949 wpa_printf(MSG_DEBUG, 950 "Invalid MBO transition reason code %u", 951 mbo_reason); 952 ret = -1; 953 goto fail; 954 } 955 956 /* Valid values for Cellular preference are: 0, 1, 255 */ 957 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) { 958 wpa_printf(MSG_DEBUG, 959 "Invalid MBO cellular capability %u", 960 cell_pref); 961 ret = -1; 962 goto fail; 963 } 964 965 if (reassoc_delay > 65535 || 966 (reassoc_delay && 967 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) { 968 wpa_printf(MSG_DEBUG, 969 "MBO: Assoc retry delay is only valid in disassoc imminent mode"); 970 ret = -1; 971 goto fail; 972 } 973 974 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON; 975 *mbo_pos++ = 1; 976 *mbo_pos++ = mbo_reason; 977 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF; 978 *mbo_pos++ = 1; 979 *mbo_pos++ = cell_pref; 980 981 if (reassoc_delay) { 982 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY; 983 *mbo_pos++ = 2; 984 WPA_PUT_LE16(mbo_pos, reassoc_delay); 985 mbo_pos += 2; 986 } 987 988 mbo_len = mbo_pos - mbo; 989 } 990 #endif /* CONFIG_MBO */ 991 992 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer, 993 valid_int, bss_term_dur, dialog_token, url, 994 nei_len ? nei_rep : NULL, nei_len, 995 mbo_len ? mbo : NULL, mbo_len); 996 #ifdef CONFIG_MBO 997 fail: 998 #endif /* CONFIG_MBO */ 999 os_free(url); 1000 return ret; 1001 } 1002 1003 1004 static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd, 1005 const char *cmd) 1006 { 1007 u8 addr[ETH_ALEN]; 1008 struct sta_info *sta; 1009 const char *pos; 1010 unsigned int auto_report, timeout; 1011 1012 if (hwaddr_aton(cmd, addr)) { 1013 wpa_printf(MSG_DEBUG, "Invalid STA MAC address"); 1014 return -1; 1015 } 1016 1017 sta = ap_get_sta(hapd, addr); 1018 if (!sta) { 1019 wpa_printf(MSG_DEBUG, "Station " MACSTR 1020 " not found for Collocated Interference Request", 1021 MAC2STR(addr)); 1022 return -1; 1023 } 1024 1025 pos = cmd + 17; 1026 if (*pos != ' ') 1027 return -1; 1028 pos++; 1029 auto_report = atoi(pos); 1030 pos = os_strchr(pos, ' '); 1031 if (!pos) 1032 return -1; 1033 pos++; 1034 timeout = atoi(pos); 1035 1036 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout); 1037 } 1038 1039 #endif /* CONFIG_WNM_AP */ 1040 1041 1042 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd, 1043 char *buf, size_t buflen) 1044 { 1045 int ret = 0; 1046 char *pos, *end; 1047 1048 pos = buf; 1049 end = buf + buflen; 1050 1051 WPA_ASSERT(hapd->conf->wpa_key_mgmt); 1052 1053 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { 1054 ret = os_snprintf(pos, end - pos, "WPA-PSK "); 1055 if (os_snprintf_error(end - pos, ret)) 1056 return pos - buf; 1057 pos += ret; 1058 } 1059 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { 1060 ret = os_snprintf(pos, end - pos, "WPA-EAP "); 1061 if (os_snprintf_error(end - pos, ret)) 1062 return pos - buf; 1063 pos += ret; 1064 } 1065 #ifdef CONFIG_IEEE80211R_AP 1066 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { 1067 ret = os_snprintf(pos, end - pos, "FT-PSK "); 1068 if (os_snprintf_error(end - pos, ret)) 1069 return pos - buf; 1070 pos += ret; 1071 } 1072 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { 1073 ret = os_snprintf(pos, end - pos, "FT-EAP "); 1074 if (os_snprintf_error(end - pos, ret)) 1075 return pos - buf; 1076 pos += ret; 1077 } 1078 #ifdef CONFIG_SHA384 1079 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) { 1080 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 "); 1081 if (os_snprintf_error(end - pos, ret)) 1082 return pos - buf; 1083 pos += ret; 1084 } 1085 #endif /* CONFIG_SHA384 */ 1086 #ifdef CONFIG_SAE 1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) { 1088 ret = os_snprintf(pos, end - pos, "FT-SAE "); 1089 if (os_snprintf_error(end - pos, ret)) 1090 return pos - buf; 1091 pos += ret; 1092 } 1093 #endif /* CONFIG_SAE */ 1094 #ifdef CONFIG_FILS 1095 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) { 1096 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 "); 1097 if (os_snprintf_error(end - pos, ret)) 1098 return pos - buf; 1099 pos += ret; 1100 } 1101 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) { 1102 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 "); 1103 if (os_snprintf_error(end - pos, ret)) 1104 return pos - buf; 1105 pos += ret; 1106 } 1107 #endif /* CONFIG_FILS */ 1108 #endif /* CONFIG_IEEE80211R_AP */ 1109 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { 1110 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 "); 1111 if (os_snprintf_error(end - pos, ret)) 1112 return pos - buf; 1113 pos += ret; 1114 } 1115 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { 1116 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 "); 1117 if (os_snprintf_error(end - pos, ret)) 1118 return pos - buf; 1119 pos += ret; 1120 } 1121 #ifdef CONFIG_SAE 1122 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) { 1123 ret = os_snprintf(pos, end - pos, "SAE "); 1124 if (os_snprintf_error(end - pos, ret)) 1125 return pos - buf; 1126 pos += ret; 1127 } 1128 #endif /* CONFIG_SAE */ 1129 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) { 1130 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B "); 1131 if (os_snprintf_error(end - pos, ret)) 1132 return pos - buf; 1133 pos += ret; 1134 } 1135 if (hapd->conf->wpa_key_mgmt & 1136 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) { 1137 ret = os_snprintf(pos, end - pos, 1138 "WPA-EAP-SUITE-B-192 "); 1139 if (os_snprintf_error(end - pos, ret)) 1140 return pos - buf; 1141 pos += ret; 1142 } 1143 #ifdef CONFIG_FILS 1144 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) { 1145 ret = os_snprintf(pos, end - pos, "FILS-SHA256 "); 1146 if (os_snprintf_error(end - pos, ret)) 1147 return pos - buf; 1148 pos += ret; 1149 } 1150 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) { 1151 ret = os_snprintf(pos, end - pos, "FILS-SHA384 "); 1152 if (os_snprintf_error(end - pos, ret)) 1153 return pos - buf; 1154 pos += ret; 1155 } 1156 #endif /* CONFIG_FILS */ 1157 1158 #ifdef CONFIG_OWE 1159 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) { 1160 ret = os_snprintf(pos, end - pos, "OWE "); 1161 if (os_snprintf_error(end - pos, ret)) 1162 return pos - buf; 1163 pos += ret; 1164 } 1165 #endif /* CONFIG_OWE */ 1166 1167 #ifdef CONFIG_DPP 1168 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) { 1169 ret = os_snprintf(pos, end - pos, "DPP "); 1170 if (os_snprintf_error(end - pos, ret)) 1171 return pos - buf; 1172 pos += ret; 1173 } 1174 #endif /* CONFIG_DPP */ 1175 1176 if (pos > buf && *(pos - 1) == ' ') { 1177 *(pos - 1) = '\0'; 1178 pos--; 1179 } 1180 1181 return pos - buf; 1182 } 1183 1184 1185 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd, 1186 char *buf, size_t buflen) 1187 { 1188 int ret; 1189 char *pos, *end; 1190 1191 pos = buf; 1192 end = buf + buflen; 1193 1194 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n" 1195 "ssid=%s\n", 1196 MAC2STR(hapd->own_addr), 1197 wpa_ssid_txt(hapd->conf->ssid.ssid, 1198 hapd->conf->ssid.ssid_len)); 1199 if (os_snprintf_error(end - pos, ret)) 1200 return pos - buf; 1201 pos += ret; 1202 1203 #ifdef CONFIG_WPS 1204 ret = os_snprintf(pos, end - pos, "wps_state=%s\n", 1205 hapd->conf->wps_state == 0 ? "disabled" : 1206 (hapd->conf->wps_state == 1 ? "not configured" : 1207 "configured")); 1208 if (os_snprintf_error(end - pos, ret)) 1209 return pos - buf; 1210 pos += ret; 1211 1212 if (hapd->conf->wps_state && hapd->conf->wpa && 1213 hapd->conf->ssid.wpa_passphrase) { 1214 ret = os_snprintf(pos, end - pos, "passphrase=%s\n", 1215 hapd->conf->ssid.wpa_passphrase); 1216 if (os_snprintf_error(end - pos, ret)) 1217 return pos - buf; 1218 pos += ret; 1219 } 1220 1221 if (hapd->conf->wps_state && hapd->conf->wpa && 1222 hapd->conf->ssid.wpa_psk && 1223 hapd->conf->ssid.wpa_psk->group) { 1224 char hex[PMK_LEN * 2 + 1]; 1225 wpa_snprintf_hex(hex, sizeof(hex), 1226 hapd->conf->ssid.wpa_psk->psk, PMK_LEN); 1227 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex); 1228 if (os_snprintf_error(end - pos, ret)) 1229 return pos - buf; 1230 pos += ret; 1231 } 1232 1233 if (hapd->conf->multi_ap) { 1234 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid; 1235 1236 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n", 1237 hapd->conf->multi_ap); 1238 if (os_snprintf_error(end - pos, ret)) 1239 return pos - buf; 1240 pos += ret; 1241 1242 if (ssid->ssid_len) { 1243 ret = os_snprintf(pos, end - pos, 1244 "multi_ap_backhaul_ssid=%s\n", 1245 wpa_ssid_txt(ssid->ssid, 1246 ssid->ssid_len)); 1247 if (os_snprintf_error(end - pos, ret)) 1248 return pos - buf; 1249 pos += ret; 1250 } 1251 1252 if (hapd->conf->wps_state && hapd->conf->wpa && 1253 ssid->wpa_passphrase) { 1254 ret = os_snprintf(pos, end - pos, 1255 "multi_ap_backhaul_wpa_passphrase=%s\n", 1256 ssid->wpa_passphrase); 1257 if (os_snprintf_error(end - pos, ret)) 1258 return pos - buf; 1259 pos += ret; 1260 } 1261 1262 if (hapd->conf->wps_state && hapd->conf->wpa && 1263 ssid->wpa_psk && 1264 ssid->wpa_psk->group) { 1265 char hex[PMK_LEN * 2 + 1]; 1266 1267 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk, 1268 PMK_LEN); 1269 ret = os_snprintf(pos, end - pos, 1270 "multi_ap_backhaul_wpa_psk=%s\n", 1271 hex); 1272 forced_memzero(hex, sizeof(hex)); 1273 if (os_snprintf_error(end - pos, ret)) 1274 return pos - buf; 1275 pos += ret; 1276 } 1277 } 1278 #endif /* CONFIG_WPS */ 1279 1280 if (hapd->conf->wpa) { 1281 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa); 1282 if (os_snprintf_error(end - pos, ret)) 1283 return pos - buf; 1284 pos += ret; 1285 } 1286 1287 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) { 1288 ret = os_snprintf(pos, end - pos, "key_mgmt="); 1289 if (os_snprintf_error(end - pos, ret)) 1290 return pos - buf; 1291 pos += ret; 1292 1293 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos); 1294 1295 ret = os_snprintf(pos, end - pos, "\n"); 1296 if (os_snprintf_error(end - pos, ret)) 1297 return pos - buf; 1298 pos += ret; 1299 } 1300 1301 if (hapd->conf->wpa) { 1302 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n", 1303 wpa_cipher_txt(hapd->conf->wpa_group)); 1304 if (os_snprintf_error(end - pos, ret)) 1305 return pos - buf; 1306 pos += ret; 1307 } 1308 1309 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) { 1310 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher="); 1311 if (os_snprintf_error(end - pos, ret)) 1312 return pos - buf; 1313 pos += ret; 1314 1315 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise, 1316 " "); 1317 if (ret < 0) 1318 return pos - buf; 1319 pos += ret; 1320 1321 ret = os_snprintf(pos, end - pos, "\n"); 1322 if (os_snprintf_error(end - pos, ret)) 1323 return pos - buf; 1324 pos += ret; 1325 } 1326 1327 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) { 1328 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher="); 1329 if (os_snprintf_error(end - pos, ret)) 1330 return pos - buf; 1331 pos += ret; 1332 1333 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise, 1334 " "); 1335 if (ret < 0) 1336 return pos - buf; 1337 pos += ret; 1338 1339 ret = os_snprintf(pos, end - pos, "\n"); 1340 if (os_snprintf_error(end - pos, ret)) 1341 return pos - buf; 1342 pos += ret; 1343 } 1344 1345 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) { 1346 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n", 1347 hapd->conf->wpa_deny_ptk0_rekey); 1348 if (os_snprintf_error(end - pos, ret)) 1349 return pos - buf; 1350 pos += ret; 1351 } 1352 1353 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) { 1354 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n", 1355 hapd->conf->extended_key_id); 1356 if (os_snprintf_error(end - pos, ret)) 1357 return pos - buf; 1358 pos += ret; 1359 } 1360 1361 return pos - buf; 1362 } 1363 1364 1365 static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd) 1366 { 1367 struct sta_info *sta; 1368 struct vlan_description vlan_id; 1369 1370 if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED) 1371 return; 1372 1373 for (sta = hapd->sta_list; sta; sta = sta->next) { 1374 if (!hostapd_maclist_found(hapd->conf->accept_mac, 1375 hapd->conf->num_accept_mac, 1376 sta->addr, &vlan_id) || 1377 (vlan_id.notempty && 1378 vlan_compare(&vlan_id, sta->vlan_desc))) 1379 ap_sta_disconnect(hapd, sta, sta->addr, 1380 WLAN_REASON_UNSPECIFIED); 1381 } 1382 } 1383 1384 1385 static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd) 1386 { 1387 struct sta_info *sta; 1388 struct vlan_description vlan_id; 1389 1390 for (sta = hapd->sta_list; sta; sta = sta->next) { 1391 if (hostapd_maclist_found(hapd->conf->deny_mac, 1392 hapd->conf->num_deny_mac, sta->addr, 1393 &vlan_id) && 1394 (!vlan_id.notempty || 1395 !vlan_compare(&vlan_id, sta->vlan_desc))) 1396 ap_sta_disconnect(hapd, sta, sta->addr, 1397 WLAN_REASON_UNSPECIFIED); 1398 } 1399 } 1400 1401 1402 static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd, 1403 const char *bands) 1404 { 1405 union wpa_event_data event; 1406 u32 setband_mask = WPA_SETBAND_AUTO; 1407 1408 /* 1409 * For example: 1410 * SET setband 2G,6G 1411 * SET setband 5G 1412 * SET setband AUTO 1413 */ 1414 if (!os_strstr(bands, "AUTO")) { 1415 if (os_strstr(bands, "5G")) 1416 setband_mask |= WPA_SETBAND_5G; 1417 if (os_strstr(bands, "6G")) 1418 setband_mask |= WPA_SETBAND_6G; 1419 if (os_strstr(bands, "2G")) 1420 setband_mask |= WPA_SETBAND_2G; 1421 if (setband_mask == WPA_SETBAND_AUTO) 1422 return -1; 1423 } 1424 1425 if (hostapd_drv_set_band(hapd, setband_mask) == 0) { 1426 os_memset(&event, 0, sizeof(event)); 1427 event.channel_list_changed.initiator = REGDOM_SET_BY_USER; 1428 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN; 1429 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event); 1430 } 1431 1432 return 0; 1433 } 1434 1435 1436 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd) 1437 { 1438 char *value; 1439 int ret = 0; 1440 1441 value = os_strchr(cmd, ' '); 1442 if (value == NULL) 1443 return -1; 1444 *value++ = '\0'; 1445 1446 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); 1447 if (0) { 1448 #ifdef CONFIG_WPS_TESTING 1449 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { 1450 long int val; 1451 val = strtol(value, NULL, 0); 1452 if (val < 0 || val > 0xff) { 1453 ret = -1; 1454 wpa_printf(MSG_DEBUG, "WPS: Invalid " 1455 "wps_version_number %ld", val); 1456 } else { 1457 wps_version_number = val; 1458 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " 1459 "version %u.%u", 1460 (wps_version_number & 0xf0) >> 4, 1461 wps_version_number & 0x0f); 1462 hostapd_wps_update_ie(hapd); 1463 } 1464 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) { 1465 wps_testing_stub_cred = atoi(value); 1466 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d", 1467 wps_testing_stub_cred); 1468 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) { 1469 wps_corrupt_pkhash = atoi(value); 1470 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d", 1471 wps_corrupt_pkhash); 1472 #endif /* CONFIG_WPS_TESTING */ 1473 #ifdef CONFIG_TESTING_OPTIONS 1474 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) { 1475 hapd->ext_mgmt_frame_handling = atoi(value); 1476 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) { 1477 hapd->ext_eapol_frame_io = atoi(value); 1478 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) { 1479 hapd->force_backlog_bytes = atoi(value); 1480 #ifdef CONFIG_DPP 1481 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) { 1482 os_free(hapd->dpp_config_obj_override); 1483 hapd->dpp_config_obj_override = os_strdup(value); 1484 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) { 1485 os_free(hapd->dpp_discovery_override); 1486 hapd->dpp_discovery_override = os_strdup(value); 1487 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) { 1488 os_free(hapd->dpp_groups_override); 1489 hapd->dpp_groups_override = os_strdup(value); 1490 } else if (os_strcasecmp(cmd, 1491 "dpp_ignore_netaccesskey_mismatch") == 0) { 1492 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value); 1493 } else if (os_strcasecmp(cmd, "dpp_test") == 0) { 1494 dpp_test = atoi(value); 1495 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) { 1496 dpp_version_override = atoi(value); 1497 #endif /* CONFIG_DPP */ 1498 #endif /* CONFIG_TESTING_OPTIONS */ 1499 #ifdef CONFIG_MBO 1500 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) { 1501 int val; 1502 1503 if (!hapd->conf->mbo_enabled) 1504 return -1; 1505 1506 val = atoi(value); 1507 if (val < 0 || val > 1) 1508 return -1; 1509 1510 hapd->mbo_assoc_disallow = val; 1511 ieee802_11_update_beacons(hapd->iface); 1512 1513 /* 1514 * TODO: Need to configure drivers that do AP MLME offload with 1515 * disallowing station logic. 1516 */ 1517 #endif /* CONFIG_MBO */ 1518 #ifdef CONFIG_DPP 1519 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) { 1520 os_free(hapd->dpp_configurator_params); 1521 hapd->dpp_configurator_params = os_strdup(value); 1522 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) { 1523 hapd->dpp_init_max_tries = atoi(value); 1524 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) { 1525 hapd->dpp_init_retry_time = atoi(value); 1526 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) { 1527 hapd->dpp_resp_wait_time = atoi(value); 1528 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) { 1529 hapd->dpp_resp_max_tries = atoi(value); 1530 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) { 1531 hapd->dpp_resp_retry_time = atoi(value); 1532 #endif /* CONFIG_DPP */ 1533 } else if (os_strcasecmp(cmd, "setband") == 0) { 1534 ret = hostapd_ctrl_iface_set_band(hapd, value); 1535 } else { 1536 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value); 1537 if (ret) 1538 return ret; 1539 1540 if (os_strcasecmp(cmd, "deny_mac_file") == 0) { 1541 hostapd_disassoc_deny_mac(hapd); 1542 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) { 1543 hostapd_disassoc_accept_mac(hapd); 1544 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 || 1545 os_strncmp(cmd, "wmm_ac_", 7) == 0) { 1546 hapd->parameter_set_count++; 1547 if (ieee802_11_update_beacons(hapd->iface)) 1548 wpa_printf(MSG_DEBUG, 1549 "Failed to update beacons with WMM parameters"); 1550 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 || 1551 os_strcmp(cmd, "sae_password") == 0 || 1552 os_strcmp(cmd, "sae_pwe") == 0) { 1553 if (hapd->started) 1554 hostapd_setup_sae_pt(hapd->conf); 1555 } else if (os_strcasecmp(cmd, "transition_disable") == 0) { 1556 wpa_auth_set_transition_disable(hapd->wpa_auth, 1557 hapd->conf->transition_disable); 1558 } 1559 1560 #ifdef CONFIG_TESTING_OPTIONS 1561 if (os_strcmp(cmd, "ft_rsnxe_used") == 0) 1562 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth, 1563 hapd->conf->ft_rsnxe_used); 1564 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0) 1565 wpa_auth_set_ocv_override_freq( 1566 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3, 1567 atoi(value)); 1568 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0) 1569 wpa_auth_set_ocv_override_freq( 1570 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1, 1571 atoi(value)); 1572 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0) 1573 wpa_auth_set_ocv_override_freq( 1574 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC, 1575 atoi(value)); 1576 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0) 1577 wpa_auth_set_ocv_override_freq( 1578 hapd->wpa_auth, 1579 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value)); 1580 #endif /* CONFIG_TESTING_OPTIONS */ 1581 } 1582 1583 return ret; 1584 } 1585 1586 1587 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd, 1588 char *buf, size_t buflen) 1589 { 1590 int res; 1591 1592 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd); 1593 1594 if (os_strcmp(cmd, "version") == 0) { 1595 res = os_snprintf(buf, buflen, "%s", VERSION_STR); 1596 if (os_snprintf_error(buflen, res)) 1597 return -1; 1598 return res; 1599 } else if (os_strcmp(cmd, "tls_library") == 0) { 1600 res = tls_get_library_version(buf, buflen); 1601 if (os_snprintf_error(buflen, res)) 1602 return -1; 1603 return res; 1604 } 1605 1606 return -1; 1607 } 1608 1609 1610 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface) 1611 { 1612 if (hostapd_enable_iface(iface) < 0) { 1613 wpa_printf(MSG_ERROR, "Enabling of interface failed"); 1614 return -1; 1615 } 1616 return 0; 1617 } 1618 1619 1620 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface) 1621 { 1622 if (hostapd_reload_iface(iface) < 0) { 1623 wpa_printf(MSG_ERROR, "Reloading of interface failed"); 1624 return -1; 1625 } 1626 return 0; 1627 } 1628 1629 1630 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface) 1631 { 1632 if (hostapd_disable_iface(iface) < 0) { 1633 wpa_printf(MSG_ERROR, "Disabling of interface failed"); 1634 return -1; 1635 } 1636 return 0; 1637 } 1638 1639 1640 static int 1641 hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd, 1642 struct sta_info *sta, void *ctx) 1643 { 1644 struct hostapd_wpa_psk *psk; 1645 const u8 *pmk; 1646 int pmk_len; 1647 int pmk_match; 1648 int sta_match; 1649 int bss_match; 1650 int reason; 1651 1652 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len); 1653 1654 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) { 1655 pmk_match = PMK_LEN == pmk_len && 1656 os_memcmp(psk->psk, pmk, pmk_len) == 0; 1657 sta_match = psk->group == 0 && 1658 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0; 1659 bss_match = psk->group == 1; 1660 1661 if (pmk_match && (sta_match || bss_match)) 1662 return 0; 1663 } 1664 1665 wpa_printf(MSG_INFO, "STA " MACSTR 1666 " PSK/passphrase no longer valid - disconnect", 1667 MAC2STR(sta->addr)); 1668 reason = WLAN_REASON_PREV_AUTH_NOT_VALID; 1669 hostapd_drv_sta_deauth(hapd, sta->addr, reason); 1670 ap_sta_deauthenticate(hapd, sta, reason); 1671 1672 return 0; 1673 } 1674 1675 1676 static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd) 1677 { 1678 struct hostapd_bss_config *conf = hapd->conf; 1679 int err; 1680 1681 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk); 1682 1683 err = hostapd_setup_wpa_psk(conf); 1684 if (err < 0) { 1685 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d", 1686 err); 1687 return -1; 1688 } 1689 1690 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter, 1691 NULL); 1692 1693 return 0; 1694 } 1695 1696 1697 #ifdef CONFIG_TESTING_OPTIONS 1698 1699 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd) 1700 { 1701 union wpa_event_data data; 1702 char *pos, *param; 1703 enum wpa_event_type event; 1704 1705 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd); 1706 1707 os_memset(&data, 0, sizeof(data)); 1708 1709 param = os_strchr(cmd, ' '); 1710 if (param == NULL) 1711 return -1; 1712 *param++ = '\0'; 1713 1714 if (os_strcmp(cmd, "DETECTED") == 0) 1715 event = EVENT_DFS_RADAR_DETECTED; 1716 else if (os_strcmp(cmd, "CAC-FINISHED") == 0) 1717 event = EVENT_DFS_CAC_FINISHED; 1718 else if (os_strcmp(cmd, "CAC-ABORTED") == 0) 1719 event = EVENT_DFS_CAC_ABORTED; 1720 else if (os_strcmp(cmd, "NOP-FINISHED") == 0) 1721 event = EVENT_DFS_NOP_FINISHED; 1722 else { 1723 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s", 1724 cmd); 1725 return -1; 1726 } 1727 1728 pos = os_strstr(param, "freq="); 1729 if (pos) 1730 data.dfs_event.freq = atoi(pos + 5); 1731 1732 pos = os_strstr(param, "ht_enabled=1"); 1733 if (pos) 1734 data.dfs_event.ht_enabled = 1; 1735 1736 pos = os_strstr(param, "chan_offset="); 1737 if (pos) 1738 data.dfs_event.chan_offset = atoi(pos + 12); 1739 1740 pos = os_strstr(param, "chan_width="); 1741 if (pos) 1742 data.dfs_event.chan_width = atoi(pos + 11); 1743 1744 pos = os_strstr(param, "cf1="); 1745 if (pos) 1746 data.dfs_event.cf1 = atoi(pos + 4); 1747 1748 pos = os_strstr(param, "cf2="); 1749 if (pos) 1750 data.dfs_event.cf2 = atoi(pos + 4); 1751 1752 wpa_supplicant_event(hapd, event, &data); 1753 1754 return 0; 1755 } 1756 1757 1758 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd) 1759 { 1760 size_t len; 1761 u8 *buf; 1762 int res; 1763 1764 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd); 1765 1766 len = os_strlen(cmd); 1767 if (len & 1) 1768 return -1; 1769 len /= 2; 1770 1771 buf = os_malloc(len); 1772 if (buf == NULL) 1773 return -1; 1774 1775 if (hexstr2bin(cmd, buf, len) < 0) { 1776 os_free(buf); 1777 return -1; 1778 } 1779 1780 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0); 1781 os_free(buf); 1782 return res; 1783 } 1784 1785 1786 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd, 1787 char *cmd) 1788 { 1789 char *pos, *param; 1790 size_t len; 1791 u8 *buf; 1792 int stype = 0, ok = 0; 1793 union wpa_event_data event; 1794 1795 if (!hapd->ext_mgmt_frame_handling) 1796 return -1; 1797 1798 /* stype=<val> ok=<0/1> buf=<frame hexdump> */ 1799 1800 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd); 1801 1802 pos = cmd; 1803 param = os_strstr(pos, "stype="); 1804 if (param) { 1805 param += 6; 1806 stype = atoi(param); 1807 } 1808 1809 param = os_strstr(pos, " ok="); 1810 if (param) { 1811 param += 4; 1812 ok = atoi(param); 1813 } 1814 1815 param = os_strstr(pos, " buf="); 1816 if (!param) 1817 return -1; 1818 param += 5; 1819 1820 len = os_strlen(param); 1821 if (len & 1) 1822 return -1; 1823 len /= 2; 1824 1825 buf = os_malloc(len); 1826 if (!buf || hexstr2bin(param, buf, len) < 0) { 1827 os_free(buf); 1828 return -1; 1829 } 1830 1831 os_memset(&event, 0, sizeof(event)); 1832 event.tx_status.type = WLAN_FC_TYPE_MGMT; 1833 event.tx_status.data = buf; 1834 event.tx_status.data_len = len; 1835 event.tx_status.stype = stype; 1836 event.tx_status.ack = ok; 1837 hapd->ext_mgmt_frame_handling = 0; 1838 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event); 1839 hapd->ext_mgmt_frame_handling = 1; 1840 1841 os_free(buf); 1842 1843 return 0; 1844 } 1845 1846 1847 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd, 1848 char *cmd) 1849 { 1850 char *pos, *param; 1851 size_t len; 1852 u8 *buf; 1853 int freq = 0, datarate = 0, ssi_signal = 0; 1854 union wpa_event_data event; 1855 1856 if (!hapd->ext_mgmt_frame_handling) 1857 return -1; 1858 1859 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */ 1860 1861 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd); 1862 1863 pos = cmd; 1864 param = os_strstr(pos, "freq="); 1865 if (param) { 1866 param += 5; 1867 freq = atoi(param); 1868 } 1869 1870 param = os_strstr(pos, " datarate="); 1871 if (param) { 1872 param += 10; 1873 datarate = atoi(param); 1874 } 1875 1876 param = os_strstr(pos, " ssi_signal="); 1877 if (param) { 1878 param += 12; 1879 ssi_signal = atoi(param); 1880 } 1881 1882 param = os_strstr(pos, " frame="); 1883 if (param == NULL) 1884 return -1; 1885 param += 7; 1886 1887 len = os_strlen(param); 1888 if (len & 1) 1889 return -1; 1890 len /= 2; 1891 1892 buf = os_malloc(len); 1893 if (buf == NULL) 1894 return -1; 1895 1896 if (hexstr2bin(param, buf, len) < 0) { 1897 os_free(buf); 1898 return -1; 1899 } 1900 1901 os_memset(&event, 0, sizeof(event)); 1902 event.rx_mgmt.freq = freq; 1903 event.rx_mgmt.frame = buf; 1904 event.rx_mgmt.frame_len = len; 1905 event.rx_mgmt.ssi_signal = ssi_signal; 1906 event.rx_mgmt.datarate = datarate; 1907 hapd->ext_mgmt_frame_handling = 0; 1908 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event); 1909 hapd->ext_mgmt_frame_handling = 1; 1910 1911 os_free(buf); 1912 1913 return 0; 1914 } 1915 1916 1917 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd) 1918 { 1919 char *pos; 1920 u8 src[ETH_ALEN], *buf; 1921 int used; 1922 size_t len; 1923 1924 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd); 1925 1926 pos = cmd; 1927 used = hwaddr_aton2(pos, src); 1928 if (used < 0) 1929 return -1; 1930 pos += used; 1931 while (*pos == ' ') 1932 pos++; 1933 1934 len = os_strlen(pos); 1935 if (len & 1) 1936 return -1; 1937 len /= 2; 1938 1939 buf = os_malloc(len); 1940 if (buf == NULL) 1941 return -1; 1942 1943 if (hexstr2bin(pos, buf, len) < 0) { 1944 os_free(buf); 1945 return -1; 1946 } 1947 1948 ieee802_1x_receive(hapd, src, buf, len); 1949 os_free(buf); 1950 1951 return 0; 1952 } 1953 1954 1955 static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd) 1956 { 1957 char *pos, *pos2; 1958 u8 dst[ETH_ALEN], *buf; 1959 int used, ret; 1960 size_t len; 1961 unsigned int prev; 1962 int encrypt = 0; 1963 1964 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd); 1965 1966 pos = cmd; 1967 used = hwaddr_aton2(pos, dst); 1968 if (used < 0) 1969 return -1; 1970 pos += used; 1971 while (*pos == ' ') 1972 pos++; 1973 1974 pos2 = os_strchr(pos, ' '); 1975 if (pos2) { 1976 len = pos2 - pos; 1977 encrypt = os_strstr(pos2, "encrypt=1") != NULL; 1978 } else { 1979 len = os_strlen(pos); 1980 } 1981 if (len & 1) 1982 return -1; 1983 len /= 2; 1984 1985 buf = os_malloc(len); 1986 if (!buf || hexstr2bin(pos, buf, len) < 0) { 1987 os_free(buf); 1988 return -1; 1989 } 1990 1991 prev = hapd->ext_eapol_frame_io; 1992 hapd->ext_eapol_frame_io = 0; 1993 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt); 1994 hapd->ext_eapol_frame_io = prev; 1995 os_free(buf); 1996 1997 return ret; 1998 } 1999 2000 2001 static u16 ipv4_hdr_checksum(const void *buf, size_t len) 2002 { 2003 size_t i; 2004 u32 sum = 0; 2005 const u16 *pos = buf; 2006 2007 for (i = 0; i < len / 2; i++) 2008 sum += *pos++; 2009 2010 while (sum >> 16) 2011 sum = (sum & 0xffff) + (sum >> 16); 2012 2013 return sum ^ 0xffff; 2014 } 2015 2016 2017 #define HWSIM_PACKETLEN 1500 2018 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header)) 2019 2020 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, 2021 size_t len) 2022 { 2023 struct hostapd_data *hapd = ctx; 2024 const struct ether_header *eth; 2025 struct ip ip; 2026 const u8 *pos; 2027 unsigned int i; 2028 char extra[30]; 2029 2030 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) { 2031 wpa_printf(MSG_DEBUG, 2032 "test data: RX - ignore unexpected length %d", 2033 (int) len); 2034 return; 2035 } 2036 2037 eth = (const struct ether_header *) buf; 2038 os_memcpy(&ip, eth + 1, sizeof(ip)); 2039 pos = &buf[sizeof(*eth) + sizeof(ip)]; 2040 2041 if (ip.ip_hl != 5 || ip.ip_v != 4 || 2042 ntohs(ip.ip_len) > HWSIM_IP_LEN) { 2043 wpa_printf(MSG_DEBUG, 2044 "test data: RX - ignore unexpected IP header"); 2045 return; 2046 } 2047 2048 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) { 2049 if (*pos != (u8) i) { 2050 wpa_printf(MSG_DEBUG, 2051 "test data: RX - ignore mismatching payload"); 2052 return; 2053 } 2054 pos++; 2055 } 2056 2057 extra[0] = '\0'; 2058 if (ntohs(ip.ip_len) != HWSIM_IP_LEN) 2059 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len)); 2060 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s", 2061 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra); 2062 } 2063 2064 2065 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd, 2066 char *cmd) 2067 { 2068 int enabled = atoi(cmd); 2069 char *pos; 2070 const char *ifname; 2071 2072 if (!enabled) { 2073 if (hapd->l2_test) { 2074 l2_packet_deinit(hapd->l2_test); 2075 hapd->l2_test = NULL; 2076 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, 2077 "test data: Disabled"); 2078 } 2079 return 0; 2080 } 2081 2082 if (hapd->l2_test) 2083 return 0; 2084 2085 pos = os_strstr(cmd, " ifname="); 2086 if (pos) 2087 ifname = pos + 8; 2088 else 2089 ifname = hapd->conf->iface; 2090 2091 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr, 2092 ETHERTYPE_IP, hostapd_data_test_rx, 2093 hapd, 1); 2094 if (hapd->l2_test == NULL) 2095 return -1; 2096 2097 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled"); 2098 2099 return 0; 2100 } 2101 2102 2103 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd) 2104 { 2105 u8 dst[ETH_ALEN], src[ETH_ALEN]; 2106 char *pos, *pos2; 2107 int used; 2108 long int val; 2109 u8 tos; 2110 u8 buf[2 + HWSIM_PACKETLEN]; 2111 struct ether_header *eth; 2112 struct ip *ip; 2113 u8 *dpos; 2114 unsigned int i; 2115 size_t send_len = HWSIM_IP_LEN; 2116 2117 if (hapd->l2_test == NULL) 2118 return -1; 2119 2120 /* format: <dst> <src> <tos> [len=<length>] */ 2121 2122 pos = cmd; 2123 used = hwaddr_aton2(pos, dst); 2124 if (used < 0) 2125 return -1; 2126 pos += used; 2127 while (*pos == ' ') 2128 pos++; 2129 used = hwaddr_aton2(pos, src); 2130 if (used < 0) 2131 return -1; 2132 pos += used; 2133 2134 val = strtol(pos, &pos2, 0); 2135 if (val < 0 || val > 0xff) 2136 return -1; 2137 tos = val; 2138 2139 pos = os_strstr(pos2, " len="); 2140 if (pos) { 2141 i = atoi(pos + 5); 2142 if (i < sizeof(*ip) || i > HWSIM_IP_LEN) 2143 return -1; 2144 send_len = i; 2145 } 2146 2147 eth = (struct ether_header *) &buf[2]; 2148 os_memcpy(eth->ether_dhost, dst, ETH_ALEN); 2149 os_memcpy(eth->ether_shost, src, ETH_ALEN); 2150 eth->ether_type = htons(ETHERTYPE_IP); 2151 ip = (struct ip *) (eth + 1); 2152 os_memset(ip, 0, sizeof(*ip)); 2153 ip->ip_hl = 5; 2154 ip->ip_v = 4; 2155 ip->ip_ttl = 64; 2156 ip->ip_tos = tos; 2157 ip->ip_len = htons(send_len); 2158 ip->ip_p = 1; 2159 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1); 2160 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2); 2161 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip)); 2162 dpos = (u8 *) (ip + 1); 2163 for (i = 0; i < send_len - sizeof(*ip); i++) 2164 *dpos++ = i; 2165 2166 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2], 2167 sizeof(struct ether_header) + send_len) < 0) 2168 return -1; 2169 2170 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR 2171 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos); 2172 2173 return 0; 2174 } 2175 2176 2177 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd, 2178 char *cmd) 2179 { 2180 u8 *buf; 2181 struct ether_header *eth; 2182 struct l2_packet_data *l2 = NULL; 2183 size_t len; 2184 u16 ethertype; 2185 int res = -1; 2186 const char *ifname = hapd->conf->iface; 2187 2188 if (os_strncmp(cmd, "ifname=", 7) == 0) { 2189 cmd += 7; 2190 ifname = cmd; 2191 cmd = os_strchr(cmd, ' '); 2192 if (cmd == NULL) 2193 return -1; 2194 *cmd++ = '\0'; 2195 } 2196 2197 len = os_strlen(cmd); 2198 if (len & 1 || len < ETH_HLEN * 2) 2199 return -1; 2200 len /= 2; 2201 2202 buf = os_malloc(len); 2203 if (buf == NULL) 2204 return -1; 2205 2206 if (hexstr2bin(cmd, buf, len) < 0) 2207 goto done; 2208 2209 eth = (struct ether_header *) buf; 2210 ethertype = ntohs(eth->ether_type); 2211 2212 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype, 2213 hostapd_data_test_rx, hapd, 1); 2214 if (l2 == NULL) 2215 goto done; 2216 2217 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len); 2218 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res); 2219 done: 2220 if (l2) 2221 l2_packet_deinit(l2); 2222 os_free(buf); 2223 2224 return res < 0 ? -1 : 0; 2225 } 2226 2227 2228 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd) 2229 { 2230 #ifdef WPA_TRACE_BFD 2231 char *pos; 2232 2233 wpa_trace_fail_after = atoi(cmd); 2234 pos = os_strchr(cmd, ':'); 2235 if (pos) { 2236 pos++; 2237 os_strlcpy(wpa_trace_fail_func, pos, 2238 sizeof(wpa_trace_fail_func)); 2239 } else { 2240 wpa_trace_fail_after = 0; 2241 } 2242 2243 return 0; 2244 #else /* WPA_TRACE_BFD */ 2245 return -1; 2246 #endif /* WPA_TRACE_BFD */ 2247 } 2248 2249 2250 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd, 2251 char *buf, size_t buflen) 2252 { 2253 #ifdef WPA_TRACE_BFD 2254 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, 2255 wpa_trace_fail_func); 2256 #else /* WPA_TRACE_BFD */ 2257 return -1; 2258 #endif /* WPA_TRACE_BFD */ 2259 } 2260 2261 2262 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd) 2263 { 2264 #ifdef WPA_TRACE_BFD 2265 char *pos; 2266 2267 wpa_trace_test_fail_after = atoi(cmd); 2268 pos = os_strchr(cmd, ':'); 2269 if (pos) { 2270 pos++; 2271 os_strlcpy(wpa_trace_test_fail_func, pos, 2272 sizeof(wpa_trace_test_fail_func)); 2273 } else { 2274 wpa_trace_test_fail_after = 0; 2275 } 2276 2277 return 0; 2278 #else /* WPA_TRACE_BFD */ 2279 return -1; 2280 #endif /* WPA_TRACE_BFD */ 2281 } 2282 2283 2284 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd, 2285 char *buf, size_t buflen) 2286 { 2287 #ifdef WPA_TRACE_BFD 2288 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, 2289 wpa_trace_test_fail_func); 2290 #else /* WPA_TRACE_BFD */ 2291 return -1; 2292 #endif /* WPA_TRACE_BFD */ 2293 } 2294 2295 2296 static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd) 2297 { 2298 struct sta_info *sta; 2299 u8 addr[ETH_ALEN]; 2300 u8 zero[WPA_TK_MAX_LEN]; 2301 2302 os_memset(zero, 0, sizeof(zero)); 2303 2304 if (hwaddr_aton(cmd, addr)) 2305 return -1; 2306 2307 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) { 2308 if (hapd->last_bigtk_alg == WPA_ALG_NONE) 2309 return -1; 2310 2311 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK"); 2312 2313 /* First, use a zero key to avoid any possible duplicate key 2314 * avoidance in the driver. */ 2315 if (hostapd_drv_set_key(hapd->conf->iface, hapd, 2316 hapd->last_bigtk_alg, 2317 broadcast_ether_addr, 2318 hapd->last_bigtk_key_idx, 0, 1, NULL, 0, 2319 zero, hapd->last_bigtk_len, 2320 KEY_FLAG_GROUP_TX_DEFAULT) < 0) 2321 return -1; 2322 2323 /* Set the previously configured key to reset its TSC */ 2324 return hostapd_drv_set_key(hapd->conf->iface, hapd, 2325 hapd->last_bigtk_alg, 2326 broadcast_ether_addr, 2327 hapd->last_bigtk_key_idx, 0, 1, NULL, 2328 0, hapd->last_bigtk, 2329 hapd->last_bigtk_len, 2330 KEY_FLAG_GROUP_TX_DEFAULT); 2331 } 2332 2333 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) { 2334 if (hapd->last_igtk_alg == WPA_ALG_NONE) 2335 return -1; 2336 2337 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK"); 2338 2339 /* First, use a zero key to avoid any possible duplicate key 2340 * avoidance in the driver. */ 2341 if (hostapd_drv_set_key(hapd->conf->iface, hapd, 2342 hapd->last_igtk_alg, 2343 broadcast_ether_addr, 2344 hapd->last_igtk_key_idx, 0, 1, NULL, 0, 2345 zero, hapd->last_igtk_len, 2346 KEY_FLAG_GROUP_TX_DEFAULT) < 0) 2347 return -1; 2348 2349 /* Set the previously configured key to reset its TSC */ 2350 return hostapd_drv_set_key(hapd->conf->iface, hapd, 2351 hapd->last_igtk_alg, 2352 broadcast_ether_addr, 2353 hapd->last_igtk_key_idx, 0, 1, NULL, 2354 0, hapd->last_igtk, 2355 hapd->last_igtk_len, 2356 KEY_FLAG_GROUP_TX_DEFAULT); 2357 } 2358 2359 if (is_broadcast_ether_addr(addr)) { 2360 if (hapd->last_gtk_alg == WPA_ALG_NONE) 2361 return -1; 2362 2363 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK"); 2364 2365 /* First, use a zero key to avoid any possible duplicate key 2366 * avoidance in the driver. */ 2367 if (hostapd_drv_set_key(hapd->conf->iface, hapd, 2368 hapd->last_gtk_alg, 2369 broadcast_ether_addr, 2370 hapd->last_gtk_key_idx, 0, 1, NULL, 0, 2371 zero, hapd->last_gtk_len, 2372 KEY_FLAG_GROUP_TX_DEFAULT) < 0) 2373 return -1; 2374 2375 /* Set the previously configured key to reset its TSC */ 2376 return hostapd_drv_set_key(hapd->conf->iface, hapd, 2377 hapd->last_gtk_alg, 2378 broadcast_ether_addr, 2379 hapd->last_gtk_key_idx, 0, 1, NULL, 2380 0, hapd->last_gtk, 2381 hapd->last_gtk_len, 2382 KEY_FLAG_GROUP_TX_DEFAULT); 2383 } 2384 2385 sta = ap_get_sta(hapd, addr); 2386 if (!sta) 2387 return -1; 2388 2389 if (sta->last_tk_alg == WPA_ALG_NONE) 2390 return -1; 2391 2392 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR, 2393 MAC2STR(sta->addr)); 2394 2395 /* First, use a zero key to avoid any possible duplicate key avoidance 2396 * in the driver. */ 2397 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, 2398 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0, 2399 zero, sta->last_tk_len, 2400 KEY_FLAG_PAIRWISE_RX_TX) < 0) 2401 return -1; 2402 2403 /* Set the previously configured key to reset its TSC/RSC */ 2404 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, 2405 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 2406 0, sta->last_tk, sta->last_tk_len, 2407 KEY_FLAG_PAIRWISE_RX_TX); 2408 } 2409 2410 2411 static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd) 2412 { 2413 u8 addr[ETH_ALEN]; 2414 const char *pos = cmd; 2415 enum wpa_alg alg; 2416 enum key_flag key_flag; 2417 int idx, set_tx; 2418 u8 seq[6], key[WPA_TK_MAX_LEN]; 2419 size_t key_len; 2420 2421 /* parameters: alg addr idx set_tx seq key key_flag */ 2422 2423 alg = atoi(pos); 2424 pos = os_strchr(pos, ' '); 2425 if (!pos) 2426 return -1; 2427 pos++; 2428 if (hwaddr_aton(pos, addr)) 2429 return -1; 2430 pos += 17; 2431 if (*pos != ' ') 2432 return -1; 2433 pos++; 2434 idx = atoi(pos); 2435 pos = os_strchr(pos, ' '); 2436 if (!pos) 2437 return -1; 2438 pos++; 2439 set_tx = atoi(pos); 2440 pos = os_strchr(pos, ' '); 2441 if (!pos) 2442 return -1; 2443 pos++; 2444 if (hexstr2bin(pos, seq, sizeof(seq)) < 0) 2445 return -1; 2446 pos += 2 * 6; 2447 if (*pos != ' ') 2448 return -1; 2449 pos++; 2450 if (!os_strchr(pos, ' ')) 2451 return -1; 2452 key_len = (os_strchr(pos, ' ') - pos) / 2; 2453 if (hexstr2bin(pos, key, key_len) < 0) 2454 return -1; 2455 pos += 2 * key_len; 2456 if (*pos != ' ') 2457 return -1; 2458 2459 pos++; 2460 key_flag = atoi(pos); 2461 pos = os_strchr(pos, ' '); 2462 if (pos) 2463 return -1; 2464 2465 wpa_printf(MSG_INFO, "TESTING: Set key"); 2466 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0, 2467 set_tx, seq, 6, key, key_len, key_flag); 2468 } 2469 2470 2471 static void restore_tk(void *ctx1, void *ctx2) 2472 { 2473 struct hostapd_data *hapd = ctx1; 2474 struct sta_info *sta = ctx2; 2475 2476 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR, 2477 MAC2STR(sta->addr)); 2478 /* This does not really restore the TSC properly, so this will result 2479 * in replay protection issues for now since there is no clean way of 2480 * preventing encryption of a single EAPOL frame. */ 2481 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, 2482 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0, 2483 sta->last_tk, sta->last_tk_len, 2484 KEY_FLAG_PAIRWISE_RX_TX); 2485 } 2486 2487 2488 static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd) 2489 { 2490 struct sta_info *sta; 2491 u8 addr[ETH_ALEN]; 2492 int plain = os_strstr(cmd, "plaintext") != NULL; 2493 2494 if (hwaddr_aton(cmd, addr)) 2495 return -1; 2496 2497 sta = ap_get_sta(hapd, addr); 2498 if (!sta || !sta->wpa_sm) 2499 return -1; 2500 2501 if (plain && sta->last_tk_alg == WPA_ALG_NONE) 2502 plain = 0; /* no need for special processing */ 2503 if (plain) { 2504 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, 2505 MAC2STR(sta->addr)); 2506 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, 2507 sta->addr, sta->last_tk_key_idx, 0, 0, NULL, 2508 0, NULL, 0, KEY_FLAG_PAIRWISE); 2509 } 2510 2511 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr)); 2512 return wpa_auth_resend_m1(sta->wpa_sm, 2513 os_strstr(cmd, "change-anonce") != NULL, 2514 plain ? restore_tk : NULL, hapd, sta); 2515 } 2516 2517 2518 static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd) 2519 { 2520 struct sta_info *sta; 2521 u8 addr[ETH_ALEN]; 2522 int plain = os_strstr(cmd, "plaintext") != NULL; 2523 2524 if (hwaddr_aton(cmd, addr)) 2525 return -1; 2526 2527 sta = ap_get_sta(hapd, addr); 2528 if (!sta || !sta->wpa_sm) 2529 return -1; 2530 2531 if (plain && sta->last_tk_alg == WPA_ALG_NONE) 2532 plain = 0; /* no need for special processing */ 2533 if (plain) { 2534 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, 2535 MAC2STR(sta->addr)); 2536 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, 2537 sta->addr, sta->last_tk_key_idx, 0, 0, NULL, 2538 0, NULL, 0, KEY_FLAG_PAIRWISE); 2539 } 2540 2541 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr)); 2542 return wpa_auth_resend_m3(sta->wpa_sm, 2543 plain ? restore_tk : NULL, hapd, sta); 2544 } 2545 2546 2547 static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd, 2548 const char *cmd) 2549 { 2550 struct sta_info *sta; 2551 u8 addr[ETH_ALEN]; 2552 int plain = os_strstr(cmd, "plaintext") != NULL; 2553 2554 if (hwaddr_aton(cmd, addr)) 2555 return -1; 2556 2557 sta = ap_get_sta(hapd, addr); 2558 if (!sta || !sta->wpa_sm) 2559 return -1; 2560 2561 if (plain && sta->last_tk_alg == WPA_ALG_NONE) 2562 plain = 0; /* no need for special processing */ 2563 if (plain) { 2564 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, 2565 MAC2STR(sta->addr)); 2566 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, 2567 sta->addr, sta->last_tk_key_idx, 0, 0, NULL, 2568 0, NULL, 0, KEY_FLAG_PAIRWISE); 2569 } 2570 2571 wpa_printf(MSG_INFO, 2572 "TESTING: Send group M1 for the same GTK and zero RSC to " 2573 MACSTR, MAC2STR(sta->addr)); 2574 return wpa_auth_resend_group_m1(sta->wpa_sm, 2575 plain ? restore_tk : NULL, hapd, sta); 2576 } 2577 2578 2579 static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd) 2580 { 2581 struct sta_info *sta; 2582 u8 addr[ETH_ALEN]; 2583 2584 if (hwaddr_aton(cmd, addr)) 2585 return -1; 2586 2587 sta = ap_get_sta(hapd, addr); 2588 if (!sta || !sta->wpa_sm) 2589 return -1; 2590 2591 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm); 2592 } 2593 2594 2595 static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr, 2596 char *buf, size_t buflen) 2597 { 2598 struct rsn_pmksa_cache_entry *pmksa; 2599 2600 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL); 2601 if (!pmksa) 2602 return -1; 2603 2604 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len); 2605 } 2606 2607 2608 static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd, 2609 char *buf, size_t buflen) 2610 { 2611 struct sta_info *sta; 2612 u8 addr[ETH_ALEN]; 2613 const u8 *pmk; 2614 int pmk_len; 2615 2616 if (hwaddr_aton(cmd, addr)) 2617 return -1; 2618 2619 sta = ap_get_sta(hapd, addr); 2620 if (!sta || !sta->wpa_sm) { 2621 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR, 2622 MAC2STR(addr)); 2623 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen); 2624 } 2625 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len); 2626 if (!pmk || !pmk_len) { 2627 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR, 2628 MAC2STR(addr)); 2629 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen); 2630 } 2631 2632 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len); 2633 } 2634 2635 2636 static int hostapd_ctrl_register_frame(struct hostapd_data *hapd, 2637 const char *cmd) 2638 { 2639 u16 type; 2640 char *pos, *end; 2641 u8 match[10]; 2642 size_t match_len; 2643 bool multicast = false; 2644 2645 type = strtol(cmd, &pos, 16); 2646 if (*pos != ' ') 2647 return -1; 2648 pos++; 2649 end = os_strchr(pos, ' '); 2650 if (end) { 2651 match_len = end - pos; 2652 multicast = os_strstr(end, "multicast") != NULL; 2653 } else { 2654 match_len = os_strlen(pos) / 2; 2655 } 2656 if (hexstr2bin(pos, match, match_len)) 2657 return -1; 2658 2659 return hostapd_drv_register_frame(hapd, type, match, match_len, 2660 multicast); 2661 } 2662 2663 #endif /* CONFIG_TESTING_OPTIONS */ 2664 2665 2666 #ifdef NEED_AP_MLME 2667 static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params) 2668 { 2669 switch (params->bandwidth) { 2670 case 0: 2671 /* bandwidth not specified: use 20 MHz by default */ 2672 /* fall-through */ 2673 case 20: 2674 if (params->center_freq1 && 2675 params->center_freq1 != params->freq) 2676 return -1; 2677 2678 if (params->center_freq2 || params->sec_channel_offset) 2679 return -1; 2680 break; 2681 case 40: 2682 if (params->center_freq2 || !params->sec_channel_offset) 2683 return -1; 2684 2685 if (!params->center_freq1) 2686 break; 2687 switch (params->sec_channel_offset) { 2688 case 1: 2689 if (params->freq + 10 != params->center_freq1) 2690 return -1; 2691 break; 2692 case -1: 2693 if (params->freq - 10 != params->center_freq1) 2694 return -1; 2695 break; 2696 default: 2697 return -1; 2698 } 2699 break; 2700 case 80: 2701 if (!params->center_freq1 || !params->sec_channel_offset) 2702 return 1; 2703 2704 switch (params->sec_channel_offset) { 2705 case 1: 2706 if (params->freq - 10 != params->center_freq1 && 2707 params->freq + 30 != params->center_freq1) 2708 return 1; 2709 break; 2710 case -1: 2711 if (params->freq + 10 != params->center_freq1 && 2712 params->freq - 30 != params->center_freq1) 2713 return -1; 2714 break; 2715 default: 2716 return -1; 2717 } 2718 2719 /* Adjacent and overlapped are not allowed for 80+80 */ 2720 if (params->center_freq2 && 2721 params->center_freq1 - params->center_freq2 <= 80 && 2722 params->center_freq2 - params->center_freq1 <= 80) 2723 return 1; 2724 break; 2725 case 160: 2726 if (!params->center_freq1 || params->center_freq2 || 2727 !params->sec_channel_offset) 2728 return -1; 2729 2730 switch (params->sec_channel_offset) { 2731 case 1: 2732 if (params->freq + 70 != params->center_freq1 && 2733 params->freq + 30 != params->center_freq1 && 2734 params->freq - 10 != params->center_freq1 && 2735 params->freq - 50 != params->center_freq1) 2736 return -1; 2737 break; 2738 case -1: 2739 if (params->freq + 50 != params->center_freq1 && 2740 params->freq + 10 != params->center_freq1 && 2741 params->freq - 30 != params->center_freq1 && 2742 params->freq - 70 != params->center_freq1) 2743 return -1; 2744 break; 2745 default: 2746 return -1; 2747 } 2748 break; 2749 default: 2750 return -1; 2751 } 2752 2753 return 0; 2754 } 2755 #endif /* NEED_AP_MLME */ 2756 2757 2758 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface, 2759 char *pos) 2760 { 2761 #ifdef NEED_AP_MLME 2762 struct csa_settings settings; 2763 int ret; 2764 int dfs_range = 0; 2765 unsigned int i; 2766 int bandwidth; 2767 u8 chan; 2768 2769 ret = hostapd_parse_csa_settings(pos, &settings); 2770 if (ret) 2771 return ret; 2772 2773 ret = hostapd_ctrl_check_freq_params(&settings.freq_params); 2774 if (ret) { 2775 wpa_printf(MSG_INFO, 2776 "chanswitch: invalid frequency settings provided"); 2777 return ret; 2778 } 2779 2780 switch (settings.freq_params.bandwidth) { 2781 case 40: 2782 bandwidth = CHAN_WIDTH_40; 2783 break; 2784 case 80: 2785 if (settings.freq_params.center_freq2) 2786 bandwidth = CHAN_WIDTH_80P80; 2787 else 2788 bandwidth = CHAN_WIDTH_80; 2789 break; 2790 case 160: 2791 bandwidth = CHAN_WIDTH_160; 2792 break; 2793 default: 2794 bandwidth = CHAN_WIDTH_20; 2795 break; 2796 } 2797 2798 if (settings.freq_params.center_freq1) 2799 dfs_range += hostapd_is_dfs_overlap( 2800 iface, bandwidth, settings.freq_params.center_freq1); 2801 else 2802 dfs_range += hostapd_is_dfs_overlap( 2803 iface, bandwidth, settings.freq_params.freq); 2804 2805 if (settings.freq_params.center_freq2) 2806 dfs_range += hostapd_is_dfs_overlap( 2807 iface, bandwidth, settings.freq_params.center_freq2); 2808 2809 if (dfs_range) { 2810 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan); 2811 if (ret == NUM_HOSTAPD_MODES) { 2812 wpa_printf(MSG_ERROR, 2813 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)", 2814 settings.freq_params.freq, 2815 settings.freq_params.sec_channel_offset, 2816 settings.freq_params.bandwidth); 2817 return -1; 2818 } 2819 2820 settings.freq_params.channel = chan; 2821 2822 wpa_printf(MSG_DEBUG, 2823 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)", 2824 settings.freq_params.channel, 2825 settings.freq_params.freq, 2826 settings.freq_params.sec_channel_offset, 2827 settings.freq_params.bandwidth, 2828 settings.freq_params.center_freq1); 2829 2830 /* Perform CAC and switch channel */ 2831 hostapd_switch_channel_fallback(iface, &settings.freq_params); 2832 return 0; 2833 } 2834 2835 for (i = 0; i < iface->num_bss; i++) { 2836 2837 /* Save CHAN_SWITCH VHT and HE config */ 2838 hostapd_chan_switch_config(iface->bss[i], 2839 &settings.freq_params); 2840 2841 ret = hostapd_switch_channel(iface->bss[i], &settings); 2842 if (ret) { 2843 /* FIX: What do we do if CSA fails in the middle of 2844 * submitting multi-BSS CSA requests? */ 2845 return ret; 2846 } 2847 } 2848 2849 return 0; 2850 #else /* NEED_AP_MLME */ 2851 return -1; 2852 #endif /* NEED_AP_MLME */ 2853 } 2854 2855 2856 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply, 2857 int reply_size, const char *param) 2858 { 2859 #ifdef RADIUS_SERVER 2860 if (os_strcmp(param, "radius_server") == 0) { 2861 return radius_server_get_mib(hapd->radius_srv, reply, 2862 reply_size); 2863 } 2864 #endif /* RADIUS_SERVER */ 2865 return -1; 2866 } 2867 2868 2869 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd, 2870 char *buf, size_t buflen) 2871 { 2872 int ret; 2873 char *pos, *temp = NULL; 2874 u8 *data = NULL; 2875 unsigned int vendor_id, subcmd; 2876 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED; 2877 struct wpabuf *reply; 2878 size_t data_len = 0; 2879 2880 /** 2881 * cmd: <vendor id> <subcommand id> [<hex formatted data>] 2882 * [nested=<0|1>] 2883 */ 2884 vendor_id = strtoul(cmd, &pos, 16); 2885 if (!isblank((unsigned char) *pos)) 2886 return -EINVAL; 2887 2888 subcmd = strtoul(pos, &pos, 10); 2889 2890 if (*pos != '\0') { 2891 if (!isblank((unsigned char) *pos++)) 2892 return -EINVAL; 2893 2894 temp = os_strchr(pos, ' '); 2895 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos); 2896 } 2897 2898 if (data_len) { 2899 data_len /= 2; 2900 data = os_malloc(data_len); 2901 if (!data) 2902 return -ENOBUFS; 2903 2904 if (hexstr2bin(pos, data, data_len)) { 2905 wpa_printf(MSG_DEBUG, 2906 "Vendor command: wrong parameter format"); 2907 os_free(data); 2908 return -EINVAL; 2909 } 2910 } 2911 2912 pos = os_strstr(cmd, "nested="); 2913 if (pos) 2914 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED : 2915 NESTED_ATTR_NOT_USED; 2916 2917 reply = wpabuf_alloc((buflen - 1) / 2); 2918 if (!reply) { 2919 os_free(data); 2920 return -ENOBUFS; 2921 } 2922 2923 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len, 2924 nested_attr_flag, reply); 2925 2926 if (ret == 0) 2927 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply), 2928 wpabuf_len(reply)); 2929 2930 wpabuf_free(reply); 2931 os_free(data); 2932 2933 return ret; 2934 } 2935 2936 2937 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd, 2938 const char *cmd) 2939 { 2940 u8 addr[ETH_ALEN]; 2941 struct sta_info *sta; 2942 2943 if (hwaddr_aton(cmd, addr)) 2944 return -1; 2945 2946 sta = ap_get_sta(hapd, addr); 2947 if (!sta || !sta->eapol_sm) 2948 return -1; 2949 2950 eapol_auth_reauthenticate(sta->eapol_sm); 2951 return 0; 2952 } 2953 2954 2955 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd) 2956 { 2957 u8 addr[ETH_ALEN]; 2958 struct sta_info *sta; 2959 char *pos = cmd, *param; 2960 2961 if (hwaddr_aton(pos, addr) || pos[17] != ' ') 2962 return -1; 2963 pos += 18; 2964 param = pos; 2965 pos = os_strchr(pos, ' '); 2966 if (!pos) 2967 return -1; 2968 *pos++ = '\0'; 2969 2970 sta = ap_get_sta(hapd, addr); 2971 if (!sta || !sta->eapol_sm) 2972 return -1; 2973 2974 return eapol_auth_set_conf(sta->eapol_sm, param, pos); 2975 } 2976 2977 2978 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd, 2979 char *buf, size_t buflen) 2980 { 2981 char *pos, *end, *stamp; 2982 int ret; 2983 2984 /* cmd: "LOG_LEVEL [<level>]" */ 2985 if (*cmd == '\0') { 2986 pos = buf; 2987 end = buf + buflen; 2988 ret = os_snprintf(pos, end - pos, "Current level: %s\n" 2989 "Timestamp: %d\n", 2990 debug_level_str(wpa_debug_level), 2991 wpa_debug_timestamp); 2992 if (os_snprintf_error(end - pos, ret)) 2993 ret = 0; 2994 2995 return ret; 2996 } 2997 2998 while (*cmd == ' ') 2999 cmd++; 3000 3001 stamp = os_strchr(cmd, ' '); 3002 if (stamp) { 3003 *stamp++ = '\0'; 3004 while (*stamp == ' ') { 3005 stamp++; 3006 } 3007 } 3008 3009 if (os_strlen(cmd)) { 3010 int level = str_to_debug_level(cmd); 3011 if (level < 0) 3012 return -1; 3013 wpa_debug_level = level; 3014 } 3015 3016 if (stamp && os_strlen(stamp)) 3017 wpa_debug_timestamp = atoi(stamp); 3018 3019 os_memcpy(buf, "OK\n", 3); 3020 return 3; 3021 } 3022 3023 3024 #ifdef NEED_AP_MLME 3025 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd, 3026 char *buf, size_t buflen) 3027 { 3028 struct hostapd_iface *iface = hapd->iface; 3029 char *pos, *end; 3030 struct hostapd_sta_info *info; 3031 struct os_reltime now; 3032 3033 if (!iface->num_sta_seen) 3034 return 0; 3035 3036 sta_track_expire(iface, 0); 3037 3038 pos = buf; 3039 end = buf + buflen; 3040 3041 os_get_reltime(&now); 3042 dl_list_for_each_reverse(info, &iface->sta_seen, 3043 struct hostapd_sta_info, list) { 3044 struct os_reltime age; 3045 int ret; 3046 3047 os_reltime_sub(&now, &info->last_seen, &age); 3048 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n", 3049 MAC2STR(info->addr), (unsigned int) age.sec, 3050 info->ssi_signal); 3051 if (os_snprintf_error(end - pos, ret)) 3052 break; 3053 pos += ret; 3054 } 3055 3056 return pos - buf; 3057 } 3058 #endif /* NEED_AP_MLME */ 3059 3060 3061 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd, 3062 const char *cmd) 3063 { 3064 u8 addr[ETH_ALEN]; 3065 3066 if (hwaddr_aton(cmd, addr)) { 3067 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address"); 3068 return -1; 3069 } 3070 3071 return hostapd_send_lci_req(hapd, addr); 3072 } 3073 3074 3075 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd) 3076 { 3077 u8 addr[ETH_ALEN]; 3078 char *token, *context = NULL; 3079 int random_interval, min_ap; 3080 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS]; 3081 unsigned int n_responders; 3082 3083 token = str_token(cmd, " ", &context); 3084 if (!token || hwaddr_aton(token, addr)) { 3085 wpa_printf(MSG_INFO, 3086 "CTRL: REQ_RANGE - Bad destination address"); 3087 return -1; 3088 } 3089 3090 token = str_token(cmd, " ", &context); 3091 if (!token) 3092 return -1; 3093 3094 random_interval = atoi(token); 3095 if (random_interval < 0 || random_interval > 0xffff) 3096 return -1; 3097 3098 token = str_token(cmd, " ", &context); 3099 if (!token) 3100 return -1; 3101 3102 min_ap = atoi(token); 3103 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP) 3104 return -1; 3105 3106 n_responders = 0; 3107 while ((token = str_token(cmd, " ", &context))) { 3108 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) { 3109 wpa_printf(MSG_INFO, 3110 "CTRL: REQ_RANGE: Too many responders"); 3111 return -1; 3112 } 3113 3114 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) { 3115 wpa_printf(MSG_INFO, 3116 "CTRL: REQ_RANGE: Bad responder address"); 3117 return -1; 3118 } 3119 3120 n_responders++; 3121 } 3122 3123 if (!n_responders) { 3124 wpa_printf(MSG_INFO, 3125 "CTRL: REQ_RANGE - No FTM responder address"); 3126 return -1; 3127 } 3128 3129 return hostapd_send_range_req(hapd, addr, random_interval, min_ap, 3130 responders, n_responders); 3131 } 3132 3133 3134 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd, 3135 const char *cmd, char *reply, 3136 size_t reply_size) 3137 { 3138 u8 addr[ETH_ALEN]; 3139 const char *pos; 3140 struct wpabuf *req; 3141 int ret; 3142 u8 req_mode = 0; 3143 3144 if (hwaddr_aton(cmd, addr)) 3145 return -1; 3146 pos = os_strchr(cmd, ' '); 3147 if (!pos) 3148 return -1; 3149 pos++; 3150 if (os_strncmp(pos, "req_mode=", 9) == 0) { 3151 int val = hex2byte(pos + 9); 3152 3153 if (val < 0) 3154 return -1; 3155 req_mode = val; 3156 pos += 11; 3157 pos = os_strchr(pos, ' '); 3158 if (!pos) 3159 return -1; 3160 pos++; 3161 } 3162 req = wpabuf_parse_bin(pos); 3163 if (!req) 3164 return -1; 3165 3166 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req); 3167 wpabuf_free(req); 3168 if (ret >= 0) 3169 ret = os_snprintf(reply, reply_size, "%d", ret); 3170 return ret; 3171 } 3172 3173 3174 static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd, 3175 char *buf, size_t buflen) 3176 { 3177 if (!(hapd->conf->radio_measurements[0] & 3178 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { 3179 wpa_printf(MSG_ERROR, 3180 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled"); 3181 return -1; 3182 } 3183 3184 return hostapd_neighbor_show(hapd, buf, buflen); 3185 } 3186 3187 3188 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf) 3189 { 3190 struct wpa_ssid_value ssid; 3191 u8 bssid[ETH_ALEN]; 3192 struct wpabuf *nr, *lci = NULL, *civic = NULL; 3193 int stationary = 0; 3194 int bss_parameters = 0; 3195 char *tmp; 3196 int ret = -1; 3197 3198 if (!(hapd->conf->radio_measurements[0] & 3199 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { 3200 wpa_printf(MSG_ERROR, 3201 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled"); 3202 return -1; 3203 } 3204 3205 if (hwaddr_aton(buf, bssid)) { 3206 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID"); 3207 return -1; 3208 } 3209 3210 tmp = os_strstr(buf, "ssid="); 3211 if (!tmp || ssid_parse(tmp + 5, &ssid)) { 3212 wpa_printf(MSG_ERROR, 3213 "CTRL: SET_NEIGHBOR: Bad or missing SSID"); 3214 return -1; 3215 } 3216 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' '); 3217 if (!buf) 3218 return -1; 3219 3220 tmp = os_strstr(buf, "nr="); 3221 if (!tmp) { 3222 wpa_printf(MSG_ERROR, 3223 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element"); 3224 return -1; 3225 } 3226 3227 buf = os_strchr(tmp, ' '); 3228 if (buf) 3229 *buf++ = '\0'; 3230 3231 nr = wpabuf_parse_bin(tmp + 3); 3232 if (!nr) { 3233 wpa_printf(MSG_ERROR, 3234 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element"); 3235 return -1; 3236 } 3237 3238 if (!buf) 3239 goto set; 3240 3241 tmp = os_strstr(buf, "lci="); 3242 if (tmp) { 3243 buf = os_strchr(tmp, ' '); 3244 if (buf) 3245 *buf++ = '\0'; 3246 lci = wpabuf_parse_bin(tmp + 4); 3247 if (!lci) { 3248 wpa_printf(MSG_ERROR, 3249 "CTRL: SET_NEIGHBOR: Bad LCI subelement"); 3250 goto fail; 3251 } 3252 } 3253 3254 if (!buf) 3255 goto set; 3256 3257 tmp = os_strstr(buf, "civic="); 3258 if (tmp) { 3259 buf = os_strchr(tmp, ' '); 3260 if (buf) 3261 *buf++ = '\0'; 3262 civic = wpabuf_parse_bin(tmp + 6); 3263 if (!civic) { 3264 wpa_printf(MSG_ERROR, 3265 "CTRL: SET_NEIGHBOR: Bad civic subelement"); 3266 goto fail; 3267 } 3268 } 3269 3270 if (!buf) 3271 goto set; 3272 3273 if (os_strstr(buf, "stat")) 3274 stationary = 1; 3275 3276 tmp = os_strstr(buf, "bss_parameter="); 3277 if (tmp) { 3278 bss_parameters = atoi(tmp + 14); 3279 if (bss_parameters < 0 || bss_parameters > 0xff) { 3280 wpa_printf(MSG_ERROR, 3281 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement"); 3282 goto fail; 3283 } 3284 } 3285 3286 set: 3287 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic, 3288 stationary, bss_parameters); 3289 3290 fail: 3291 wpabuf_free(nr); 3292 wpabuf_free(lci); 3293 wpabuf_free(civic); 3294 3295 return ret; 3296 } 3297 3298 3299 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd, 3300 char *buf) 3301 { 3302 struct wpa_ssid_value ssid; 3303 struct wpa_ssid_value *ssidp = NULL; 3304 u8 bssid[ETH_ALEN]; 3305 char *tmp; 3306 3307 if (hwaddr_aton(buf, bssid)) { 3308 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID"); 3309 return -1; 3310 } 3311 3312 tmp = os_strstr(buf, "ssid="); 3313 if (tmp) { 3314 ssidp = &ssid; 3315 if (ssid_parse(tmp + 5, &ssid)) { 3316 wpa_printf(MSG_ERROR, 3317 "CTRL: REMOVE_NEIGHBOR: Bad SSID"); 3318 return -1; 3319 } 3320 } 3321 3322 return hostapd_neighbor_remove(hapd, bssid, ssidp); 3323 } 3324 3325 3326 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf, 3327 size_t buflen) 3328 { 3329 int ret, i; 3330 char *pos, *end; 3331 3332 ret = os_snprintf(buf, buflen, "%016llX:\n", 3333 (long long unsigned) iface->drv_flags); 3334 if (os_snprintf_error(buflen, ret)) 3335 return -1; 3336 3337 pos = buf + ret; 3338 end = buf + buflen; 3339 3340 for (i = 0; i < 64; i++) { 3341 if (iface->drv_flags & (1LLU << i)) { 3342 ret = os_snprintf(pos, end - pos, "%s\n", 3343 driver_flag_to_string(1LLU << i)); 3344 if (os_snprintf_error(end - pos, ret)) 3345 return -1; 3346 pos += ret; 3347 } 3348 } 3349 3350 return pos - buf; 3351 } 3352 3353 3354 static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf, 3355 size_t buflen) 3356 { 3357 int ret, i; 3358 char *pos, *end; 3359 3360 ret = os_snprintf(buf, buflen, "%016llX:\n", 3361 (long long unsigned) iface->drv_flags2); 3362 if (os_snprintf_error(buflen, ret)) 3363 return -1; 3364 3365 pos = buf + ret; 3366 end = buf + buflen; 3367 3368 for (i = 0; i < 64; i++) { 3369 if (iface->drv_flags2 & (1LLU << i)) { 3370 ret = os_snprintf(pos, end - pos, "%s\n", 3371 driver_flag2_to_string(1LLU << i)); 3372 if (os_snprintf_error(end - pos, ret)) 3373 return -1; 3374 pos += ret; 3375 } 3376 } 3377 3378 return pos - buf; 3379 } 3380 3381 3382 static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num, 3383 const char *txtaddr) 3384 { 3385 u8 addr[ETH_ALEN]; 3386 struct vlan_description vlan_id; 3387 3388 if (!(*num)) 3389 return 0; 3390 3391 if (hwaddr_aton(txtaddr, addr)) 3392 return -1; 3393 3394 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id)) 3395 hostapd_remove_acl_mac(acl, num, addr); 3396 3397 return 0; 3398 } 3399 3400 3401 static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl, 3402 int *num) 3403 { 3404 while (*num) 3405 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr); 3406 } 3407 3408 3409 static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num, 3410 char *buf, size_t buflen) 3411 { 3412 int i = 0, len = 0, ret = 0; 3413 3414 if (!acl) 3415 return 0; 3416 3417 while (i < num) { 3418 ret = os_snprintf(buf + len, buflen - len, 3419 MACSTR " VLAN_ID=%d\n", 3420 MAC2STR(acl[i].addr), 3421 acl[i].vlan_id.untagged); 3422 if (ret < 0 || (size_t) ret >= buflen - len) 3423 return len; 3424 i++; 3425 len += ret; 3426 } 3427 return len; 3428 } 3429 3430 3431 static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num, 3432 const char *cmd) 3433 { 3434 u8 addr[ETH_ALEN]; 3435 struct vlan_description vlan_id; 3436 int ret = 0, vlanid = 0; 3437 const char *pos; 3438 3439 if (hwaddr_aton(cmd, addr)) 3440 return -1; 3441 3442 pos = os_strstr(cmd, "VLAN_ID="); 3443 if (pos) 3444 vlanid = atoi(pos + 8); 3445 3446 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) { 3447 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr); 3448 if (ret != -1 && *acl) 3449 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp); 3450 } 3451 3452 return ret < 0 ? -1 : 0; 3453 } 3454 3455 3456 static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd, 3457 const char *field, char *buf, 3458 size_t buflen) 3459 { 3460 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field); 3461 3462 #ifdef CONFIG_DPP 3463 if (os_strcmp(field, "dpp") == 0) { 3464 int res; 3465 3466 #ifdef CONFIG_DPP2 3467 res = os_snprintf(buf, buflen, "DPP=2"); 3468 #else /* CONFIG_DPP2 */ 3469 res = os_snprintf(buf, buflen, "DPP=1"); 3470 #endif /* CONFIG_DPP2 */ 3471 if (os_snprintf_error(buflen, res)) 3472 return -1; 3473 return res; 3474 } 3475 #endif /* CONFIG_DPP */ 3476 3477 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'", 3478 field); 3479 3480 return -1; 3481 } 3482 3483 3484 #ifdef ANDROID 3485 static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd, 3486 char *buf, size_t buflen) 3487 { 3488 int ret; 3489 3490 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen); 3491 if (ret == 0) { 3492 ret = os_snprintf(buf, buflen, "%s\n", "OK"); 3493 if (os_snprintf_error(buflen, ret)) 3494 ret = -1; 3495 } 3496 return ret; 3497 } 3498 #endif /* ANDROID */ 3499 3500 3501 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd, 3502 char *buf, char *reply, 3503 int reply_size, 3504 struct sockaddr_storage *from, 3505 socklen_t fromlen) 3506 { 3507 int reply_len, res; 3508 3509 os_memcpy(reply, "OK\n", 3); 3510 reply_len = 3; 3511 3512 if (os_strcmp(buf, "PING") == 0) { 3513 os_memcpy(reply, "PONG\n", 5); 3514 reply_len = 5; 3515 } else if (os_strncmp(buf, "RELOG", 5) == 0) { 3516 if (wpa_debug_reopen_file() < 0) 3517 reply_len = -1; 3518 } else if (os_strncmp(buf, "NOTE ", 5) == 0) { 3519 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5); 3520 } else if (os_strcmp(buf, "STATUS") == 0) { 3521 reply_len = hostapd_ctrl_iface_status(hapd, reply, 3522 reply_size); 3523 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) { 3524 reply_len = hostapd_drv_status(hapd, reply, reply_size); 3525 } else if (os_strcmp(buf, "MIB") == 0) { 3526 reply_len = ieee802_11_get_mib(hapd, reply, reply_size); 3527 if (reply_len >= 0) { 3528 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, 3529 reply_size - reply_len); 3530 if (res < 0) 3531 reply_len = -1; 3532 else 3533 reply_len += res; 3534 } 3535 if (reply_len >= 0) { 3536 res = ieee802_1x_get_mib(hapd, reply + reply_len, 3537 reply_size - reply_len); 3538 if (res < 0) 3539 reply_len = -1; 3540 else 3541 reply_len += res; 3542 } 3543 #ifndef CONFIG_NO_RADIUS 3544 if (reply_len >= 0) { 3545 res = radius_client_get_mib(hapd->radius, 3546 reply + reply_len, 3547 reply_size - reply_len); 3548 if (res < 0) 3549 reply_len = -1; 3550 else 3551 reply_len += res; 3552 } 3553 #endif /* CONFIG_NO_RADIUS */ 3554 } else if (os_strncmp(buf, "MIB ", 4) == 0) { 3555 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size, 3556 buf + 4); 3557 } else if (os_strcmp(buf, "STA-FIRST") == 0) { 3558 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, 3559 reply_size); 3560 } else if (os_strncmp(buf, "STA ", 4) == 0) { 3561 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, 3562 reply_size); 3563 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { 3564 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, 3565 reply_size); 3566 } else if (os_strcmp(buf, "ATTACH") == 0) { 3567 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL)) 3568 reply_len = -1; 3569 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { 3570 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7)) 3571 reply_len = -1; 3572 } else if (os_strcmp(buf, "DETACH") == 0) { 3573 if (hostapd_ctrl_iface_detach(hapd, from, fromlen)) 3574 reply_len = -1; 3575 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { 3576 if (hostapd_ctrl_iface_level(hapd, from, fromlen, 3577 buf + 6)) 3578 reply_len = -1; 3579 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { 3580 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) 3581 reply_len = -1; 3582 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { 3583 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) 3584 reply_len = -1; 3585 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { 3586 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) 3587 reply_len = -1; 3588 #ifdef CONFIG_TAXONOMY 3589 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) { 3590 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10, 3591 reply, reply_size); 3592 #endif /* CONFIG_TAXONOMY */ 3593 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) { 3594 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9)) 3595 reply_len = -1; 3596 } else if (os_strcmp(buf, "STOP_AP") == 0) { 3597 if (hostapd_ctrl_iface_stop_ap(hapd)) 3598 reply_len = -1; 3599 #ifdef NEED_AP_MLME 3600 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { 3601 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) 3602 reply_len = -1; 3603 #endif /* NEED_AP_MLME */ 3604 #ifdef CONFIG_WPS 3605 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { 3606 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) 3607 reply_len = -1; 3608 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) { 3609 reply_len = hostapd_ctrl_iface_wps_check_pin( 3610 hapd, buf + 14, reply, reply_size); 3611 } else if (os_strcmp(buf, "WPS_PBC") == 0) { 3612 if (hostapd_wps_button_pushed(hapd, NULL)) 3613 reply_len = -1; 3614 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) { 3615 if (hostapd_wps_cancel(hapd)) 3616 reply_len = -1; 3617 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { 3618 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, 3619 reply, reply_size); 3620 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) { 3621 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0) 3622 reply_len = -1; 3623 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) { 3624 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply, 3625 reply_size); 3626 #ifdef CONFIG_WPS_NFC 3627 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) { 3628 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17)) 3629 reply_len = -1; 3630 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) { 3631 reply_len = hostapd_ctrl_iface_wps_nfc_config_token( 3632 hapd, buf + 21, reply, reply_size); 3633 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) { 3634 reply_len = hostapd_ctrl_iface_wps_nfc_token( 3635 hapd, buf + 14, reply, reply_size); 3636 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) { 3637 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel( 3638 hapd, buf + 21, reply, reply_size); 3639 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) { 3640 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20)) 3641 reply_len = -1; 3642 #endif /* CONFIG_WPS_NFC */ 3643 #endif /* CONFIG_WPS */ 3644 #ifdef CONFIG_INTERWORKING 3645 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) { 3646 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16)) 3647 reply_len = -1; 3648 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) { 3649 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18)) 3650 reply_len = -1; 3651 #endif /* CONFIG_INTERWORKING */ 3652 #ifdef CONFIG_HS20 3653 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) { 3654 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15)) 3655 reply_len = -1; 3656 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) { 3657 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16)) 3658 reply_len = -1; 3659 #endif /* CONFIG_HS20 */ 3660 #ifdef CONFIG_WNM_AP 3661 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) { 3662 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18)) 3663 reply_len = -1; 3664 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) { 3665 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13)) 3666 reply_len = -1; 3667 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) { 3668 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11)) 3669 reply_len = -1; 3670 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) { 3671 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15)) 3672 reply_len = -1; 3673 #endif /* CONFIG_WNM_AP */ 3674 } else if (os_strcmp(buf, "GET_CONFIG") == 0) { 3675 reply_len = hostapd_ctrl_iface_get_config(hapd, reply, 3676 reply_size); 3677 } else if (os_strncmp(buf, "SET ", 4) == 0) { 3678 if (hostapd_ctrl_iface_set(hapd, buf + 4)) 3679 reply_len = -1; 3680 } else if (os_strncmp(buf, "GET ", 4) == 0) { 3681 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply, 3682 reply_size); 3683 } else if (os_strncmp(buf, "ENABLE", 6) == 0) { 3684 if (hostapd_ctrl_iface_enable(hapd->iface)) 3685 reply_len = -1; 3686 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) { 3687 if (hostapd_ctrl_iface_reload_wpa_psk(hapd)) 3688 reply_len = -1; 3689 } else if (os_strncmp(buf, "RELOAD", 6) == 0) { 3690 if (hostapd_ctrl_iface_reload(hapd->iface)) 3691 reply_len = -1; 3692 } else if (os_strncmp(buf, "DISABLE", 7) == 0) { 3693 if (hostapd_ctrl_iface_disable(hapd->iface)) 3694 reply_len = -1; 3695 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) { 3696 if (ieee802_11_set_beacon(hapd)) 3697 reply_len = -1; 3698 #ifdef CONFIG_TESTING_OPTIONS 3699 } else if (os_strncmp(buf, "RADAR ", 6) == 0) { 3700 if (hostapd_ctrl_iface_radar(hapd, buf + 6)) 3701 reply_len = -1; 3702 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) { 3703 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8)) 3704 reply_len = -1; 3705 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) { 3706 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd, 3707 buf + 23) < 0) 3708 reply_len = -1; 3709 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) { 3710 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0) 3711 reply_len = -1; 3712 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) { 3713 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0) 3714 reply_len = -1; 3715 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) { 3716 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0) 3717 reply_len = -1; 3718 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) { 3719 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0) 3720 reply_len = -1; 3721 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) { 3722 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0) 3723 reply_len = -1; 3724 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) { 3725 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0) 3726 reply_len = -1; 3727 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) { 3728 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0) 3729 reply_len = -1; 3730 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) { 3731 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply, 3732 reply_size); 3733 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) { 3734 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0) 3735 reply_len = -1; 3736 } else if (os_strcmp(buf, "GET_FAIL") == 0) { 3737 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size); 3738 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) { 3739 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0) 3740 reply_len = -1; 3741 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) { 3742 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0) 3743 reply_len = -1; 3744 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) { 3745 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0) 3746 reply_len = -1; 3747 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) { 3748 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0) 3749 reply_len = -1; 3750 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) { 3751 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0) 3752 reply_len = -1; 3753 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) { 3754 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0) 3755 reply_len = -1; 3756 } else if (os_strcmp(buf, "REKEY_GTK") == 0) { 3757 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0) 3758 reply_len = -1; 3759 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) { 3760 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply, 3761 reply_size); 3762 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) { 3763 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0) 3764 reply_len = -1; 3765 #endif /* CONFIG_TESTING_OPTIONS */ 3766 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) { 3767 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12)) 3768 reply_len = -1; 3769 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) { 3770 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply, 3771 reply_size); 3772 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) { 3773 ieee802_1x_erp_flush(hapd); 3774 #ifdef RADIUS_SERVER 3775 radius_server_erp_flush(hapd->radius_srv); 3776 #endif /* RADIUS_SERVER */ 3777 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) { 3778 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13)) 3779 reply_len = -1; 3780 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) { 3781 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10)) 3782 reply_len = -1; 3783 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) { 3784 reply_len = hostapd_ctrl_iface_log_level( 3785 hapd, buf + 9, reply, reply_size); 3786 #ifdef NEED_AP_MLME 3787 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) { 3788 reply_len = hostapd_ctrl_iface_track_sta_list( 3789 hapd, reply, reply_size); 3790 #endif /* NEED_AP_MLME */ 3791 } else if (os_strcmp(buf, "PMKSA") == 0) { 3792 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply, 3793 reply_size); 3794 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) { 3795 hostapd_ctrl_iface_pmksa_flush(hapd); 3796 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) { 3797 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0) 3798 reply_len = -1; 3799 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) { 3800 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13)) 3801 reply_len = -1; 3802 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) { 3803 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply, 3804 reply_size); 3805 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) { 3806 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16)) 3807 reply_len = -1; 3808 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) { 3809 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8)) 3810 reply_len = -1; 3811 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) { 3812 if (hostapd_ctrl_iface_req_range(hapd, buf + 10)) 3813 reply_len = -1; 3814 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) { 3815 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11, 3816 reply, reply_size); 3817 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) { 3818 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply, 3819 reply_size); 3820 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) { 3821 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply, 3822 reply_size); 3823 } else if (os_strcmp(buf, "TERMINATE") == 0) { 3824 eloop_terminate(); 3825 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) { 3826 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) { 3827 if (hostapd_ctrl_iface_acl_add_mac( 3828 &hapd->conf->accept_mac, 3829 &hapd->conf->num_accept_mac, buf + 19)) 3830 reply_len = -1; 3831 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) { 3832 if (!hostapd_ctrl_iface_acl_del_mac( 3833 &hapd->conf->accept_mac, 3834 &hapd->conf->num_accept_mac, buf + 19)) 3835 hostapd_disassoc_accept_mac(hapd); 3836 else 3837 reply_len = -1; 3838 } else if (os_strcmp(buf + 11, "SHOW") == 0) { 3839 reply_len = hostapd_ctrl_iface_acl_show_mac( 3840 hapd->conf->accept_mac, 3841 hapd->conf->num_accept_mac, reply, reply_size); 3842 } else if (os_strcmp(buf + 11, "CLEAR") == 0) { 3843 hostapd_ctrl_iface_acl_clear_list( 3844 &hapd->conf->accept_mac, 3845 &hapd->conf->num_accept_mac); 3846 hostapd_disassoc_accept_mac(hapd); 3847 } 3848 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) { 3849 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) { 3850 if (!hostapd_ctrl_iface_acl_add_mac( 3851 &hapd->conf->deny_mac, 3852 &hapd->conf->num_deny_mac, buf + 17)) 3853 hostapd_disassoc_deny_mac(hapd); 3854 else 3855 reply_len = -1; 3856 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) { 3857 if (hostapd_ctrl_iface_acl_del_mac( 3858 &hapd->conf->deny_mac, 3859 &hapd->conf->num_deny_mac, buf + 17)) 3860 reply_len = -1; 3861 } else if (os_strcmp(buf + 9, "SHOW") == 0) { 3862 reply_len = hostapd_ctrl_iface_acl_show_mac( 3863 hapd->conf->deny_mac, 3864 hapd->conf->num_deny_mac, reply, reply_size); 3865 } else if (os_strcmp(buf + 9, "CLEAR") == 0) { 3866 hostapd_ctrl_iface_acl_clear_list( 3867 &hapd->conf->deny_mac, 3868 &hapd->conf->num_deny_mac); 3869 } 3870 #ifdef CONFIG_DPP 3871 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) { 3872 res = hostapd_dpp_qr_code(hapd, buf + 12); 3873 if (res < 0) { 3874 reply_len = -1; 3875 } else { 3876 reply_len = os_snprintf(reply, reply_size, "%d", res); 3877 if (os_snprintf_error(reply_size, reply_len)) 3878 reply_len = -1; 3879 } 3880 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) { 3881 res = hostapd_dpp_nfc_uri(hapd, buf + 12); 3882 if (res < 0) { 3883 reply_len = -1; 3884 } else { 3885 reply_len = os_snprintf(reply, reply_size, "%d", res); 3886 if (os_snprintf_error(reply_size, reply_len)) 3887 reply_len = -1; 3888 } 3889 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) { 3890 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20); 3891 if (res < 0) { 3892 reply_len = -1; 3893 } else { 3894 reply_len = os_snprintf(reply, reply_size, "%d", res); 3895 if (os_snprintf_error(reply_size, reply_len)) 3896 reply_len = -1; 3897 } 3898 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) { 3899 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20); 3900 if (res < 0) { 3901 reply_len = -1; 3902 } else { 3903 reply_len = os_snprintf(reply, reply_size, "%d", res); 3904 if (os_snprintf_error(reply_size, reply_len)) 3905 reply_len = -1; 3906 } 3907 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) { 3908 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18); 3909 if (res < 0) { 3910 reply_len = -1; 3911 } else { 3912 reply_len = os_snprintf(reply, reply_size, "%d", res); 3913 if (os_snprintf_error(reply_size, reply_len)) 3914 reply_len = -1; 3915 } 3916 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) { 3917 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp, 3918 buf + 21) < 0) 3919 reply_len = -1; 3920 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) { 3921 const char *uri; 3922 3923 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp, 3924 atoi(buf + 22)); 3925 if (!uri) { 3926 reply_len = -1; 3927 } else { 3928 reply_len = os_snprintf(reply, reply_size, "%s", uri); 3929 if (os_snprintf_error(reply_size, reply_len)) 3930 reply_len = -1; 3931 } 3932 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) { 3933 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp, 3934 atoi(buf + 19), 3935 reply, reply_size); 3936 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) { 3937 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp, 3938 atoi(buf + 18), 3939 os_strchr(buf + 18, ' ')) < 0) 3940 reply_len = -1; 3941 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) { 3942 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0) 3943 reply_len = -1; 3944 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) { 3945 if (hostapd_dpp_listen(hapd, buf + 11) < 0) 3946 reply_len = -1; 3947 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) { 3948 hostapd_dpp_stop(hapd); 3949 hostapd_dpp_listen_stop(hapd); 3950 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) { 3951 res = dpp_configurator_add(hapd->iface->interfaces->dpp, 3952 buf + 20); 3953 if (res < 0) { 3954 reply_len = -1; 3955 } else { 3956 reply_len = os_snprintf(reply, reply_size, "%d", res); 3957 if (os_snprintf_error(reply_size, reply_len)) 3958 reply_len = -1; 3959 } 3960 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) { 3961 if (dpp_configurator_remove(hapd->iface->interfaces->dpp, 3962 buf + 24) < 0) 3963 reply_len = -1; 3964 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) { 3965 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0) 3966 reply_len = -1; 3967 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) { 3968 reply_len = dpp_configurator_get_key_id( 3969 hapd->iface->interfaces->dpp, 3970 atoi(buf + 25), 3971 reply, reply_size); 3972 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) { 3973 res = hostapd_dpp_pkex_add(hapd, buf + 12); 3974 if (res < 0) { 3975 reply_len = -1; 3976 } else { 3977 reply_len = os_snprintf(reply, reply_size, "%d", res); 3978 if (os_snprintf_error(reply_size, reply_len)) 3979 reply_len = -1; 3980 } 3981 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) { 3982 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0) 3983 reply_len = -1; 3984 #ifdef CONFIG_DPP2 3985 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) { 3986 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0) 3987 reply_len = -1; 3988 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) { 3989 if (hostapd_dpp_controller_start(hapd, NULL) < 0) 3990 reply_len = -1; 3991 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) { 3992 dpp_controller_stop(hapd->iface->interfaces->dpp); 3993 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) { 3994 if (hostapd_dpp_chirp(hapd, buf + 9) < 0) 3995 reply_len = -1; 3996 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) { 3997 hostapd_dpp_chirp_stop(hapd); 3998 #endif /* CONFIG_DPP2 */ 3999 #endif /* CONFIG_DPP */ 4000 #ifdef RADIUS_SERVER 4001 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) { 4002 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0) 4003 reply_len = -1; 4004 #endif /* RADIUS_SERVER */ 4005 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) { 4006 reply_len = hostapd_ctrl_iface_get_capability( 4007 hapd, buf + 15, reply, reply_size); 4008 #ifdef CONFIG_PASN 4009 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) { 4010 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size); 4011 #endif /* CONFIG_PASN */ 4012 #ifdef ANDROID 4013 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) { 4014 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply, 4015 reply_size); 4016 #endif /* ANDROID */ 4017 } else { 4018 os_memcpy(reply, "UNKNOWN COMMAND\n", 16); 4019 reply_len = 16; 4020 } 4021 4022 if (reply_len < 0) { 4023 os_memcpy(reply, "FAIL\n", 5); 4024 reply_len = 5; 4025 } 4026 4027 return reply_len; 4028 } 4029 4030 4031 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, 4032 void *sock_ctx) 4033 { 4034 struct hostapd_data *hapd = eloop_ctx; 4035 char buf[4096]; 4036 int res; 4037 struct sockaddr_storage from; 4038 socklen_t fromlen = sizeof(from); 4039 char *reply, *pos = buf; 4040 const int reply_size = 4096; 4041 int reply_len; 4042 int level = MSG_DEBUG; 4043 #ifdef CONFIG_CTRL_IFACE_UDP 4044 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN]; 4045 #endif /* CONFIG_CTRL_IFACE_UDP */ 4046 4047 res = recvfrom(sock, buf, sizeof(buf) - 1, 0, 4048 (struct sockaddr *) &from, &fromlen); 4049 if (res < 0) { 4050 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", 4051 strerror(errno)); 4052 return; 4053 } 4054 buf[res] = '\0'; 4055 4056 reply = os_malloc(reply_size); 4057 if (reply == NULL) { 4058 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, 4059 fromlen) < 0) { 4060 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", 4061 strerror(errno)); 4062 } 4063 return; 4064 } 4065 4066 #ifdef CONFIG_CTRL_IFACE_UDP 4067 if (os_strcmp(buf, "GET_COOKIE") == 0) { 4068 os_memcpy(reply, "COOKIE=", 7); 4069 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1, 4070 hapd->ctrl_iface_cookie, 4071 CTRL_IFACE_COOKIE_LEN); 4072 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN; 4073 goto done; 4074 } 4075 4076 if (os_strncmp(buf, "COOKIE=", 7) != 0 || 4077 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) { 4078 wpa_printf(MSG_DEBUG, 4079 "CTRL: No cookie in the request - drop request"); 4080 os_free(reply); 4081 return; 4082 } 4083 4084 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie, 4085 CTRL_IFACE_COOKIE_LEN) != 0) { 4086 wpa_printf(MSG_DEBUG, 4087 "CTRL: Invalid cookie in the request - drop request"); 4088 os_free(reply); 4089 return; 4090 } 4091 4092 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN; 4093 while (*pos == ' ') 4094 pos++; 4095 #endif /* CONFIG_CTRL_IFACE_UDP */ 4096 4097 if (os_strcmp(pos, "PING") == 0) 4098 level = MSG_EXCESSIVE; 4099 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res); 4100 4101 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos, 4102 reply, reply_size, 4103 &from, fromlen); 4104 4105 #ifdef CONFIG_CTRL_IFACE_UDP 4106 done: 4107 #endif /* CONFIG_CTRL_IFACE_UDP */ 4108 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, 4109 fromlen) < 0) { 4110 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", 4111 strerror(errno)); 4112 } 4113 os_free(reply); 4114 } 4115 4116 4117 #ifndef CONFIG_CTRL_IFACE_UDP 4118 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) 4119 { 4120 char *buf; 4121 size_t len; 4122 4123 if (hapd->conf->ctrl_interface == NULL) 4124 return NULL; 4125 4126 len = os_strlen(hapd->conf->ctrl_interface) + 4127 os_strlen(hapd->conf->iface) + 2; 4128 buf = os_malloc(len); 4129 if (buf == NULL) 4130 return NULL; 4131 4132 os_snprintf(buf, len, "%s/%s", 4133 hapd->conf->ctrl_interface, hapd->conf->iface); 4134 buf[len - 1] = '\0'; 4135 return buf; 4136 } 4137 #endif /* CONFIG_CTRL_IFACE_UDP */ 4138 4139 4140 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, 4141 enum wpa_msg_type type, 4142 const char *txt, size_t len) 4143 { 4144 struct hostapd_data *hapd = ctx; 4145 if (hapd == NULL) 4146 return; 4147 hostapd_ctrl_iface_send(hapd, level, type, txt, len); 4148 } 4149 4150 4151 int hostapd_ctrl_iface_init(struct hostapd_data *hapd) 4152 { 4153 #ifdef CONFIG_CTRL_IFACE_UDP 4154 int port = HOSTAPD_CTRL_IFACE_PORT; 4155 char p[32] = { 0 }; 4156 char port_str[40], *tmp; 4157 char *pos; 4158 struct addrinfo hints = { 0 }, *res, *saveres; 4159 int n; 4160 4161 if (hapd->ctrl_sock > -1) { 4162 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); 4163 return 0; 4164 } 4165 4166 if (hapd->conf->ctrl_interface == NULL) 4167 return 0; 4168 4169 pos = os_strstr(hapd->conf->ctrl_interface, "udp:"); 4170 if (pos) { 4171 pos += 4; 4172 port = atoi(pos); 4173 if (port <= 0) { 4174 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port"); 4175 goto fail; 4176 } 4177 } 4178 4179 dl_list_init(&hapd->ctrl_dst); 4180 hapd->ctrl_sock = -1; 4181 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN); 4182 4183 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 4184 hints.ai_flags = AI_PASSIVE; 4185 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 4186 4187 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 4188 hints.ai_family = AF_INET6; 4189 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 4190 hints.ai_family = AF_INET; 4191 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 4192 hints.ai_socktype = SOCK_DGRAM; 4193 4194 try_again: 4195 os_snprintf(p, sizeof(p), "%d", port); 4196 n = getaddrinfo(NULL, p, &hints, &res); 4197 if (n) { 4198 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); 4199 goto fail; 4200 } 4201 4202 saveres = res; 4203 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype, 4204 res->ai_protocol); 4205 if (hapd->ctrl_sock < 0) { 4206 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); 4207 goto fail; 4208 } 4209 4210 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) { 4211 port--; 4212 if ((HOSTAPD_CTRL_IFACE_PORT - port) < 4213 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos) 4214 goto try_again; 4215 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); 4216 goto fail; 4217 } 4218 4219 freeaddrinfo(saveres); 4220 4221 os_snprintf(port_str, sizeof(port_str), "udp:%d", port); 4222 tmp = os_strdup(port_str); 4223 if (tmp) { 4224 os_free(hapd->conf->ctrl_interface); 4225 hapd->conf->ctrl_interface = tmp; 4226 } 4227 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port); 4228 4229 if (eloop_register_read_sock(hapd->ctrl_sock, 4230 hostapd_ctrl_iface_receive, hapd, NULL) < 4231 0) { 4232 hostapd_ctrl_iface_deinit(hapd); 4233 return -1; 4234 } 4235 4236 hapd->msg_ctx = hapd; 4237 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); 4238 4239 return 0; 4240 4241 fail: 4242 if (hapd->ctrl_sock >= 0) 4243 close(hapd->ctrl_sock); 4244 return -1; 4245 #else /* CONFIG_CTRL_IFACE_UDP */ 4246 struct sockaddr_un addr; 4247 int s = -1; 4248 char *fname = NULL; 4249 4250 if (hapd->ctrl_sock > -1) { 4251 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); 4252 return 0; 4253 } 4254 4255 dl_list_init(&hapd->ctrl_dst); 4256 4257 if (hapd->conf->ctrl_interface == NULL) 4258 return 0; 4259 4260 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { 4261 if (errno == EEXIST) { 4262 wpa_printf(MSG_DEBUG, "Using existing control " 4263 "interface directory."); 4264 } else { 4265 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", 4266 strerror(errno)); 4267 goto fail; 4268 } 4269 } 4270 4271 if (hapd->conf->ctrl_interface_gid_set && 4272 lchown(hapd->conf->ctrl_interface, -1, 4273 hapd->conf->ctrl_interface_gid) < 0) { 4274 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", 4275 strerror(errno)); 4276 return -1; 4277 } 4278 4279 if (!hapd->conf->ctrl_interface_gid_set && 4280 hapd->iface->interfaces->ctrl_iface_group && 4281 lchown(hapd->conf->ctrl_interface, -1, 4282 hapd->iface->interfaces->ctrl_iface_group) < 0) { 4283 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", 4284 strerror(errno)); 4285 return -1; 4286 } 4287 4288 #ifdef ANDROID 4289 /* 4290 * Android is using umask 0077 which would leave the control interface 4291 * directory without group access. This breaks things since Wi-Fi 4292 * framework assumes that this directory can be accessed by other 4293 * applications in the wifi group. Fix this by adding group access even 4294 * if umask value would prevent this. 4295 */ 4296 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { 4297 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", 4298 strerror(errno)); 4299 /* Try to continue anyway */ 4300 } 4301 #endif /* ANDROID */ 4302 4303 if (os_strlen(hapd->conf->ctrl_interface) + 1 + 4304 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) 4305 goto fail; 4306 4307 s = socket(PF_UNIX, SOCK_DGRAM, 0); 4308 if (s < 0) { 4309 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); 4310 goto fail; 4311 } 4312 4313 os_memset(&addr, 0, sizeof(addr)); 4314 #ifdef __FreeBSD__ 4315 addr.sun_len = sizeof(addr); 4316 #endif /* __FreeBSD__ */ 4317 addr.sun_family = AF_UNIX; 4318 fname = hostapd_ctrl_iface_path(hapd); 4319 if (fname == NULL) 4320 goto fail; 4321 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); 4322 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 4323 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", 4324 strerror(errno)); 4325 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 4326 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" 4327 " allow connections - assuming it was left" 4328 "over from forced program termination"); 4329 if (unlink(fname) < 0) { 4330 wpa_printf(MSG_ERROR, 4331 "Could not unlink existing ctrl_iface socket '%s': %s", 4332 fname, strerror(errno)); 4333 goto fail; 4334 } 4335 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 4336 0) { 4337 wpa_printf(MSG_ERROR, 4338 "hostapd-ctrl-iface: bind(PF_UNIX): %s", 4339 strerror(errno)); 4340 goto fail; 4341 } 4342 wpa_printf(MSG_DEBUG, "Successfully replaced leftover " 4343 "ctrl_iface socket '%s'", fname); 4344 } else { 4345 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " 4346 "be in use - cannot override it"); 4347 wpa_printf(MSG_INFO, "Delete '%s' manually if it is " 4348 "not used anymore", fname); 4349 os_free(fname); 4350 fname = NULL; 4351 goto fail; 4352 } 4353 } 4354 4355 if (hapd->conf->ctrl_interface_gid_set && 4356 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) { 4357 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", 4358 strerror(errno)); 4359 goto fail; 4360 } 4361 4362 if (!hapd->conf->ctrl_interface_gid_set && 4363 hapd->iface->interfaces->ctrl_iface_group && 4364 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) { 4365 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", 4366 strerror(errno)); 4367 goto fail; 4368 } 4369 4370 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { 4371 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", 4372 strerror(errno)); 4373 goto fail; 4374 } 4375 os_free(fname); 4376 4377 hapd->ctrl_sock = s; 4378 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, 4379 NULL) < 0) { 4380 hostapd_ctrl_iface_deinit(hapd); 4381 return -1; 4382 } 4383 hapd->msg_ctx = hapd; 4384 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); 4385 4386 return 0; 4387 4388 fail: 4389 if (s >= 0) 4390 close(s); 4391 if (fname) { 4392 unlink(fname); 4393 os_free(fname); 4394 } 4395 return -1; 4396 #endif /* CONFIG_CTRL_IFACE_UDP */ 4397 } 4398 4399 4400 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) 4401 { 4402 struct wpa_ctrl_dst *dst, *prev; 4403 4404 if (hapd->ctrl_sock > -1) { 4405 #ifndef CONFIG_CTRL_IFACE_UDP 4406 char *fname; 4407 #endif /* !CONFIG_CTRL_IFACE_UDP */ 4408 4409 eloop_unregister_read_sock(hapd->ctrl_sock); 4410 close(hapd->ctrl_sock); 4411 hapd->ctrl_sock = -1; 4412 #ifndef CONFIG_CTRL_IFACE_UDP 4413 fname = hostapd_ctrl_iface_path(hapd); 4414 if (fname) 4415 unlink(fname); 4416 os_free(fname); 4417 4418 if (hapd->conf->ctrl_interface && 4419 rmdir(hapd->conf->ctrl_interface) < 0) { 4420 if (errno == ENOTEMPTY) { 4421 wpa_printf(MSG_DEBUG, "Control interface " 4422 "directory not empty - leaving it " 4423 "behind"); 4424 } else { 4425 wpa_printf(MSG_ERROR, 4426 "rmdir[ctrl_interface=%s]: %s", 4427 hapd->conf->ctrl_interface, 4428 strerror(errno)); 4429 } 4430 } 4431 #endif /* !CONFIG_CTRL_IFACE_UDP */ 4432 } 4433 4434 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst, 4435 list) 4436 os_free(dst); 4437 4438 #ifdef CONFIG_TESTING_OPTIONS 4439 l2_packet_deinit(hapd->l2_test); 4440 hapd->l2_test = NULL; 4441 #endif /* CONFIG_TESTING_OPTIONS */ 4442 } 4443 4444 4445 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces, 4446 char *buf) 4447 { 4448 if (hostapd_add_iface(interfaces, buf) < 0) { 4449 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf); 4450 return -1; 4451 } 4452 return 0; 4453 } 4454 4455 4456 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces, 4457 char *buf) 4458 { 4459 if (hostapd_remove_iface(interfaces, buf) < 0) { 4460 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf); 4461 return -1; 4462 } 4463 return 0; 4464 } 4465 4466 4467 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces, 4468 struct sockaddr_storage *from, 4469 socklen_t fromlen, char *input) 4470 { 4471 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen, 4472 input); 4473 } 4474 4475 4476 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces, 4477 struct sockaddr_storage *from, 4478 socklen_t fromlen) 4479 { 4480 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen); 4481 } 4482 4483 4484 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces) 4485 { 4486 #ifdef CONFIG_WPS_TESTING 4487 wps_version_number = 0x20; 4488 wps_testing_stub_cred = 0; 4489 wps_corrupt_pkhash = 0; 4490 #endif /* CONFIG_WPS_TESTING */ 4491 4492 #ifdef CONFIG_TESTING_OPTIONS 4493 #ifdef CONFIG_DPP 4494 dpp_test = DPP_TEST_DISABLED; 4495 #ifdef CONFIG_DPP2 4496 dpp_version_override = 2; 4497 #else /* CONFIG_DPP2 */ 4498 dpp_version_override = 1; 4499 #endif /* CONFIG_DPP2 */ 4500 #endif /* CONFIG_DPP */ 4501 #endif /* CONFIG_TESTING_OPTIONS */ 4502 4503 #ifdef CONFIG_DPP 4504 dpp_global_clear(interfaces->dpp); 4505 #endif /* CONFIG_DPP */ 4506 } 4507 4508 4509 #ifdef CONFIG_FST 4510 4511 static int 4512 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces, 4513 const char *cmd) 4514 { 4515 char ifname[IFNAMSIZ + 1]; 4516 struct fst_iface_cfg cfg; 4517 struct hostapd_data *hapd; 4518 struct fst_wpa_obj iface_obj; 4519 4520 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) { 4521 hapd = hostapd_get_iface(interfaces, ifname); 4522 if (hapd) { 4523 if (hapd->iface->fst) { 4524 wpa_printf(MSG_INFO, "FST: Already attached"); 4525 return -1; 4526 } 4527 fst_hostapd_fill_iface_obj(hapd, &iface_obj); 4528 hapd->iface->fst = fst_attach(ifname, hapd->own_addr, 4529 &iface_obj, &cfg); 4530 if (hapd->iface->fst) 4531 return 0; 4532 } 4533 } 4534 4535 return -EINVAL; 4536 } 4537 4538 4539 static int 4540 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces, 4541 const char *cmd) 4542 { 4543 char ifname[IFNAMSIZ + 1]; 4544 struct hostapd_data * hapd; 4545 4546 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) { 4547 hapd = hostapd_get_iface(interfaces, ifname); 4548 if (hapd) { 4549 if (!fst_iface_detach(ifname)) { 4550 hapd->iface->fst = NULL; 4551 hapd->iface->fst_ies = NULL; 4552 return 0; 4553 } 4554 } 4555 } 4556 4557 return -EINVAL; 4558 } 4559 4560 #endif /* CONFIG_FST */ 4561 4562 4563 static struct hostapd_data * 4564 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces, 4565 const char *ifname) 4566 { 4567 size_t i, j; 4568 4569 for (i = 0; i < interfaces->count; i++) { 4570 struct hostapd_iface *iface = interfaces->iface[i]; 4571 4572 for (j = 0; j < iface->num_bss; j++) { 4573 struct hostapd_data *hapd; 4574 4575 hapd = iface->bss[j]; 4576 if (os_strcmp(ifname, hapd->conf->iface) == 0) 4577 return hapd; 4578 } 4579 } 4580 4581 return NULL; 4582 } 4583 4584 4585 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd, 4586 struct hostapd_data *dst_hapd, 4587 const char *param) 4588 { 4589 int res; 4590 char *value; 4591 4592 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN); 4593 if (!value) { 4594 wpa_printf(MSG_ERROR, 4595 "DUP: cannot allocate buffer to stringify %s", 4596 param); 4597 goto error_return; 4598 } 4599 4600 if (os_strcmp(param, "wpa") == 0) { 4601 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d", 4602 src_hapd->conf->wpa); 4603 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 && 4604 src_hapd->conf->wpa_key_mgmt) { 4605 res = hostapd_ctrl_iface_get_key_mgmt( 4606 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN); 4607 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res)) 4608 goto error_stringify; 4609 } else if (os_strcmp(param, "wpa_pairwise") == 0 && 4610 src_hapd->conf->wpa_pairwise) { 4611 res = wpa_write_ciphers(value, 4612 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, 4613 src_hapd->conf->wpa_pairwise, " "); 4614 if (res < 0) 4615 goto error_stringify; 4616 } else if (os_strcmp(param, "rsn_pairwise") == 0 && 4617 src_hapd->conf->rsn_pairwise) { 4618 res = wpa_write_ciphers(value, 4619 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, 4620 src_hapd->conf->rsn_pairwise, " "); 4621 if (res < 0) 4622 goto error_stringify; 4623 } else if (os_strcmp(param, "wpa_passphrase") == 0 && 4624 src_hapd->conf->ssid.wpa_passphrase) { 4625 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s", 4626 src_hapd->conf->ssid.wpa_passphrase); 4627 } else if (os_strcmp(param, "wpa_psk") == 0 && 4628 src_hapd->conf->ssid.wpa_psk_set) { 4629 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, 4630 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN); 4631 } else { 4632 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param); 4633 goto error_return; 4634 } 4635 4636 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value); 4637 os_free(value); 4638 return res; 4639 4640 error_stringify: 4641 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param); 4642 error_return: 4643 os_free(value); 4644 return -1; 4645 } 4646 4647 4648 static int 4649 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces, 4650 const char *input, 4651 char *reply, int reply_size) 4652 { 4653 size_t i, j; 4654 int res; 4655 char *pos, *end; 4656 struct hostapd_iface *iface; 4657 int show_ctrl = 0; 4658 4659 if (input) 4660 show_ctrl = !!os_strstr(input, "ctrl"); 4661 4662 pos = reply; 4663 end = reply + reply_size; 4664 4665 for (i = 0; i < interfaces->count; i++) { 4666 iface = interfaces->iface[i]; 4667 4668 for (j = 0; j < iface->num_bss; j++) { 4669 struct hostapd_bss_config *conf; 4670 4671 conf = iface->conf->bss[j]; 4672 if (show_ctrl) 4673 res = os_snprintf(pos, end - pos, 4674 "%s ctrl_iface=%s\n", 4675 conf->iface, 4676 conf->ctrl_interface ? 4677 conf->ctrl_interface : "N/A"); 4678 else 4679 res = os_snprintf(pos, end - pos, "%s\n", 4680 conf->iface); 4681 if (os_snprintf_error(end - pos, res)) { 4682 *pos = '\0'; 4683 return pos - reply; 4684 } 4685 pos += res; 4686 } 4687 } 4688 4689 return pos - reply; 4690 } 4691 4692 4693 static int 4694 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces, 4695 char *cmd) 4696 { 4697 char *p_start = cmd, *p_end; 4698 struct hostapd_data *src_hapd, *dst_hapd; 4699 4700 /* cmd: "<src ifname> <dst ifname> <variable name> */ 4701 4702 p_end = os_strchr(p_start, ' '); 4703 if (!p_end) { 4704 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'", 4705 cmd); 4706 return -1; 4707 } 4708 4709 *p_end = '\0'; 4710 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); 4711 if (!src_hapd) { 4712 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'", 4713 p_start); 4714 return -1; 4715 } 4716 4717 p_start = p_end + 1; 4718 p_end = os_strchr(p_start, ' '); 4719 if (!p_end) { 4720 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'", 4721 cmd); 4722 return -1; 4723 } 4724 4725 *p_end = '\0'; 4726 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); 4727 if (!dst_hapd) { 4728 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'", 4729 p_start); 4730 return -1; 4731 } 4732 4733 p_start = p_end + 1; 4734 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start); 4735 } 4736 4737 4738 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces, 4739 const char *ifname, 4740 char *buf, char *reply, 4741 int reply_size, 4742 struct sockaddr_storage *from, 4743 socklen_t fromlen) 4744 { 4745 struct hostapd_data *hapd; 4746 4747 hapd = hostapd_interfaces_get_hapd(interfaces, ifname); 4748 if (hapd == NULL) { 4749 int res; 4750 4751 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n"); 4752 if (os_snprintf_error(reply_size, res)) 4753 return -1; 4754 return res; 4755 } 4756 4757 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size, 4758 from, fromlen); 4759 } 4760 4761 4762 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx, 4763 void *sock_ctx) 4764 { 4765 struct hapd_interfaces *interfaces = eloop_ctx; 4766 char buffer[256], *buf = buffer; 4767 int res; 4768 struct sockaddr_storage from; 4769 socklen_t fromlen = sizeof(from); 4770 char *reply; 4771 int reply_len; 4772 const int reply_size = 4096; 4773 #ifdef CONFIG_CTRL_IFACE_UDP 4774 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN]; 4775 #endif /* CONFIG_CTRL_IFACE_UDP */ 4776 4777 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, 4778 (struct sockaddr *) &from, &fromlen); 4779 if (res < 0) { 4780 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", 4781 strerror(errno)); 4782 return; 4783 } 4784 buf[res] = '\0'; 4785 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf); 4786 4787 reply = os_malloc(reply_size); 4788 if (reply == NULL) { 4789 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, 4790 fromlen) < 0) { 4791 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", 4792 strerror(errno)); 4793 } 4794 return; 4795 } 4796 4797 os_memcpy(reply, "OK\n", 3); 4798 reply_len = 3; 4799 4800 #ifdef CONFIG_CTRL_IFACE_UDP 4801 if (os_strcmp(buf, "GET_COOKIE") == 0) { 4802 os_memcpy(reply, "COOKIE=", 7); 4803 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1, 4804 interfaces->ctrl_iface_cookie, 4805 CTRL_IFACE_COOKIE_LEN); 4806 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN; 4807 goto send_reply; 4808 } 4809 4810 if (os_strncmp(buf, "COOKIE=", 7) != 0 || 4811 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) { 4812 wpa_printf(MSG_DEBUG, 4813 "CTRL: No cookie in the request - drop request"); 4814 os_free(reply); 4815 return; 4816 } 4817 4818 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie, 4819 CTRL_IFACE_COOKIE_LEN) != 0) { 4820 wpa_printf(MSG_DEBUG, 4821 "CTRL: Invalid cookie in the request - drop request"); 4822 os_free(reply); 4823 return; 4824 } 4825 4826 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN; 4827 while (*buf == ' ') 4828 buf++; 4829 #endif /* CONFIG_CTRL_IFACE_UDP */ 4830 4831 if (os_strncmp(buf, "IFNAME=", 7) == 0) { 4832 char *pos = os_strchr(buf + 7, ' '); 4833 4834 if (pos) { 4835 *pos++ = '\0'; 4836 reply_len = hostapd_global_ctrl_iface_ifname( 4837 interfaces, buf + 7, pos, reply, reply_size, 4838 &from, fromlen); 4839 goto send_reply; 4840 } 4841 } 4842 4843 if (os_strcmp(buf, "PING") == 0) { 4844 os_memcpy(reply, "PONG\n", 5); 4845 reply_len = 5; 4846 } else if (os_strncmp(buf, "RELOG", 5) == 0) { 4847 if (wpa_debug_reopen_file() < 0) 4848 reply_len = -1; 4849 } else if (os_strcmp(buf, "FLUSH") == 0) { 4850 hostapd_ctrl_iface_flush(interfaces); 4851 } else if (os_strncmp(buf, "ADD ", 4) == 0) { 4852 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0) 4853 reply_len = -1; 4854 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) { 4855 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0) 4856 reply_len = -1; 4857 } else if (os_strcmp(buf, "ATTACH") == 0) { 4858 if (hostapd_global_ctrl_iface_attach(interfaces, &from, 4859 fromlen, NULL)) 4860 reply_len = -1; 4861 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { 4862 if (hostapd_global_ctrl_iface_attach(interfaces, &from, 4863 fromlen, buf + 7)) 4864 reply_len = -1; 4865 } else if (os_strcmp(buf, "DETACH") == 0) { 4866 if (hostapd_global_ctrl_iface_detach(interfaces, &from, 4867 fromlen)) 4868 reply_len = -1; 4869 #ifdef CONFIG_MODULE_TESTS 4870 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) { 4871 if (hapd_module_tests() < 0) 4872 reply_len = -1; 4873 #endif /* CONFIG_MODULE_TESTS */ 4874 #ifdef CONFIG_FST 4875 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) { 4876 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11)) 4877 reply_len = os_snprintf(reply, reply_size, "OK\n"); 4878 else 4879 reply_len = -1; 4880 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) { 4881 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11)) 4882 reply_len = os_snprintf(reply, reply_size, "OK\n"); 4883 else 4884 reply_len = -1; 4885 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) { 4886 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size); 4887 #endif /* CONFIG_FST */ 4888 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) { 4889 if (!hostapd_global_ctrl_iface_dup_network(interfaces, 4890 buf + 12)) 4891 reply_len = os_snprintf(reply, reply_size, "OK\n"); 4892 else 4893 reply_len = -1; 4894 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) { 4895 reply_len = hostapd_global_ctrl_iface_interfaces( 4896 interfaces, buf + 10, reply, sizeof(buffer)); 4897 } else if (os_strcmp(buf, "TERMINATE") == 0) { 4898 eloop_terminate(); 4899 } else { 4900 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command " 4901 "ignored"); 4902 reply_len = -1; 4903 } 4904 4905 send_reply: 4906 if (reply_len < 0) { 4907 os_memcpy(reply, "FAIL\n", 5); 4908 reply_len = 5; 4909 } 4910 4911 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, 4912 fromlen) < 0) { 4913 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", 4914 strerror(errno)); 4915 } 4916 os_free(reply); 4917 } 4918 4919 4920 #ifndef CONFIG_CTRL_IFACE_UDP 4921 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface) 4922 { 4923 char *buf; 4924 size_t len; 4925 4926 if (interface->global_iface_path == NULL) 4927 return NULL; 4928 4929 len = os_strlen(interface->global_iface_path) + 4930 os_strlen(interface->global_iface_name) + 2; 4931 buf = os_malloc(len); 4932 if (buf == NULL) 4933 return NULL; 4934 4935 os_snprintf(buf, len, "%s/%s", interface->global_iface_path, 4936 interface->global_iface_name); 4937 buf[len - 1] = '\0'; 4938 return buf; 4939 } 4940 #endif /* CONFIG_CTRL_IFACE_UDP */ 4941 4942 4943 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface) 4944 { 4945 #ifdef CONFIG_CTRL_IFACE_UDP 4946 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT; 4947 char p[32] = { 0 }; 4948 char *pos; 4949 struct addrinfo hints = { 0 }, *res, *saveres; 4950 int n; 4951 4952 if (interface->global_ctrl_sock > -1) { 4953 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); 4954 return 0; 4955 } 4956 4957 if (interface->global_iface_path == NULL) 4958 return 0; 4959 4960 pos = os_strstr(interface->global_iface_path, "udp:"); 4961 if (pos) { 4962 pos += 4; 4963 port = atoi(pos); 4964 if (port <= 0) { 4965 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port"); 4966 goto fail; 4967 } 4968 } 4969 4970 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN); 4971 4972 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 4973 hints.ai_flags = AI_PASSIVE; 4974 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 4975 4976 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 4977 hints.ai_family = AF_INET6; 4978 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 4979 hints.ai_family = AF_INET; 4980 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 4981 hints.ai_socktype = SOCK_DGRAM; 4982 4983 try_again: 4984 os_snprintf(p, sizeof(p), "%d", port); 4985 n = getaddrinfo(NULL, p, &hints, &res); 4986 if (n) { 4987 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); 4988 goto fail; 4989 } 4990 4991 saveres = res; 4992 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype, 4993 res->ai_protocol); 4994 if (interface->global_ctrl_sock < 0) { 4995 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); 4996 goto fail; 4997 } 4998 4999 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) < 5000 0) { 5001 port++; 5002 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) < 5003 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos) 5004 goto try_again; 5005 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); 5006 goto fail; 5007 } 5008 5009 freeaddrinfo(saveres); 5010 5011 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port); 5012 5013 if (eloop_register_read_sock(interface->global_ctrl_sock, 5014 hostapd_global_ctrl_iface_receive, 5015 interface, NULL) < 0) { 5016 hostapd_global_ctrl_iface_deinit(interface); 5017 return -1; 5018 } 5019 5020 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); 5021 5022 return 0; 5023 5024 fail: 5025 if (interface->global_ctrl_sock >= 0) 5026 close(interface->global_ctrl_sock); 5027 return -1; 5028 #else /* CONFIG_CTRL_IFACE_UDP */ 5029 struct sockaddr_un addr; 5030 int s = -1; 5031 char *fname = NULL; 5032 5033 if (interface->global_iface_path == NULL) { 5034 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!"); 5035 return 0; 5036 } 5037 5038 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) { 5039 if (errno == EEXIST) { 5040 wpa_printf(MSG_DEBUG, "Using existing control " 5041 "interface directory."); 5042 } else { 5043 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", 5044 strerror(errno)); 5045 goto fail; 5046 } 5047 } else if (interface->ctrl_iface_group && 5048 lchown(interface->global_iface_path, -1, 5049 interface->ctrl_iface_group) < 0) { 5050 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", 5051 strerror(errno)); 5052 goto fail; 5053 } 5054 5055 if (os_strlen(interface->global_iface_path) + 1 + 5056 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path)) 5057 goto fail; 5058 5059 s = socket(PF_UNIX, SOCK_DGRAM, 0); 5060 if (s < 0) { 5061 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); 5062 goto fail; 5063 } 5064 5065 os_memset(&addr, 0, sizeof(addr)); 5066 #ifdef __FreeBSD__ 5067 addr.sun_len = sizeof(addr); 5068 #endif /* __FreeBSD__ */ 5069 addr.sun_family = AF_UNIX; 5070 fname = hostapd_global_ctrl_iface_path(interface); 5071 if (fname == NULL) 5072 goto fail; 5073 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); 5074 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 5075 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", 5076 strerror(errno)); 5077 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 5078 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" 5079 " allow connections - assuming it was left" 5080 "over from forced program termination"); 5081 if (unlink(fname) < 0) { 5082 wpa_printf(MSG_ERROR, 5083 "Could not unlink existing ctrl_iface socket '%s': %s", 5084 fname, strerror(errno)); 5085 goto fail; 5086 } 5087 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 5088 0) { 5089 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s", 5090 strerror(errno)); 5091 goto fail; 5092 } 5093 wpa_printf(MSG_DEBUG, "Successfully replaced leftover " 5094 "ctrl_iface socket '%s'", fname); 5095 } else { 5096 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " 5097 "be in use - cannot override it"); 5098 wpa_printf(MSG_INFO, "Delete '%s' manually if it is " 5099 "not used anymore", fname); 5100 os_free(fname); 5101 fname = NULL; 5102 goto fail; 5103 } 5104 } 5105 5106 if (interface->ctrl_iface_group && 5107 lchown(fname, -1, interface->ctrl_iface_group) < 0) { 5108 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", 5109 strerror(errno)); 5110 goto fail; 5111 } 5112 5113 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { 5114 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", 5115 strerror(errno)); 5116 goto fail; 5117 } 5118 os_free(fname); 5119 5120 interface->global_ctrl_sock = s; 5121 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive, 5122 interface, NULL); 5123 5124 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); 5125 5126 return 0; 5127 5128 fail: 5129 if (s >= 0) 5130 close(s); 5131 if (fname) { 5132 unlink(fname); 5133 os_free(fname); 5134 } 5135 return -1; 5136 #endif /* CONFIG_CTRL_IFACE_UDP */ 5137 } 5138 5139 5140 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces) 5141 { 5142 #ifndef CONFIG_CTRL_IFACE_UDP 5143 char *fname = NULL; 5144 #endif /* CONFIG_CTRL_IFACE_UDP */ 5145 struct wpa_ctrl_dst *dst, *prev; 5146 5147 if (interfaces->global_ctrl_sock > -1) { 5148 eloop_unregister_read_sock(interfaces->global_ctrl_sock); 5149 close(interfaces->global_ctrl_sock); 5150 interfaces->global_ctrl_sock = -1; 5151 #ifndef CONFIG_CTRL_IFACE_UDP 5152 fname = hostapd_global_ctrl_iface_path(interfaces); 5153 if (fname) { 5154 unlink(fname); 5155 os_free(fname); 5156 } 5157 5158 if (interfaces->global_iface_path && 5159 rmdir(interfaces->global_iface_path) < 0) { 5160 if (errno == ENOTEMPTY) { 5161 wpa_printf(MSG_DEBUG, "Control interface " 5162 "directory not empty - leaving it " 5163 "behind"); 5164 } else { 5165 wpa_printf(MSG_ERROR, 5166 "rmdir[ctrl_interface=%s]: %s", 5167 interfaces->global_iface_path, 5168 strerror(errno)); 5169 } 5170 } 5171 #endif /* CONFIG_CTRL_IFACE_UDP */ 5172 } 5173 5174 os_free(interfaces->global_iface_path); 5175 interfaces->global_iface_path = NULL; 5176 5177 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst, 5178 struct wpa_ctrl_dst, list) 5179 os_free(dst); 5180 } 5181 5182 5183 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst, 5184 const char *buf) 5185 { 5186 /* Enable Probe Request events based on explicit request. 5187 * Other events are enabled by default. 5188 */ 5189 if (str_starts(buf, RX_PROBE_REQUEST)) 5190 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST); 5191 return 1; 5192 } 5193 5194 5195 static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst, 5196 const char *ifname, int level, 5197 const char *buf, size_t len) 5198 { 5199 struct wpa_ctrl_dst *dst, *next; 5200 struct msghdr msg; 5201 int idx, res; 5202 struct iovec io[5]; 5203 char levelstr[10]; 5204 5205 if (sock < 0 || dl_list_empty(ctrl_dst)) 5206 return; 5207 5208 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); 5209 if (os_snprintf_error(sizeof(levelstr), res)) 5210 return; 5211 idx = 0; 5212 if (ifname) { 5213 io[idx].iov_base = "IFNAME="; 5214 io[idx].iov_len = 7; 5215 idx++; 5216 io[idx].iov_base = (char *) ifname; 5217 io[idx].iov_len = os_strlen(ifname); 5218 idx++; 5219 io[idx].iov_base = " "; 5220 io[idx].iov_len = 1; 5221 idx++; 5222 } 5223 io[idx].iov_base = levelstr; 5224 io[idx].iov_len = os_strlen(levelstr); 5225 idx++; 5226 io[idx].iov_base = (char *) buf; 5227 io[idx].iov_len = len; 5228 idx++; 5229 os_memset(&msg, 0, sizeof(msg)); 5230 msg.msg_iov = io; 5231 msg.msg_iovlen = idx; 5232 5233 idx = 0; 5234 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { 5235 if ((level >= dst->debug_level) && 5236 hostapd_ctrl_check_event_enabled(dst, buf)) { 5237 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send", 5238 &dst->addr, dst->addrlen); 5239 msg.msg_name = &dst->addr; 5240 msg.msg_namelen = dst->addrlen; 5241 if (sendmsg(sock, &msg, 0) < 0) { 5242 int _errno = errno; 5243 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " 5244 "%d - %s", 5245 idx, errno, strerror(errno)); 5246 dst->errors++; 5247 if (dst->errors > 10 || _errno == ENOENT) { 5248 ctrl_iface_detach(ctrl_dst, 5249 &dst->addr, 5250 dst->addrlen); 5251 } 5252 } else 5253 dst->errors = 0; 5254 } 5255 idx++; 5256 } 5257 } 5258 5259 5260 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, 5261 enum wpa_msg_type type, 5262 const char *buf, size_t len) 5263 { 5264 if (type != WPA_MSG_NO_GLOBAL) { 5265 hostapd_ctrl_iface_send_internal( 5266 hapd->iface->interfaces->global_ctrl_sock, 5267 &hapd->iface->interfaces->global_ctrl_dst, 5268 type != WPA_MSG_PER_INTERFACE ? 5269 NULL : hapd->conf->iface, 5270 level, buf, len); 5271 } 5272 5273 if (type != WPA_MSG_ONLY_GLOBAL) { 5274 hostapd_ctrl_iface_send_internal( 5275 hapd->ctrl_sock, &hapd->ctrl_dst, 5276 NULL, level, buf, len); 5277 } 5278 } 5279 5280 #endif /* CONFIG_NATIVE_WINDOWS */ 5281