1 /* 2 * hostapd - command line interface for hostapd daemon 3 * Copyright (c) 2004-2022, 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 "includes.h" 10 #include <dirent.h> 11 12 #include "common/wpa_ctrl.h" 13 #include "common/ieee802_11_defs.h" 14 #include "utils/common.h" 15 #include "utils/eloop.h" 16 #include "utils/edit.h" 17 #include "common/version.h" 18 #include "common/cli.h" 19 20 #ifndef CONFIG_NO_CTRL_IFACE 21 22 static const char *const hostapd_cli_version = 23 "hostapd_cli v" VERSION_STR "\n" 24 "Copyright (c) 2004-2024, Jouni Malinen <j@w1.fi> and contributors"; 25 26 static struct wpa_ctrl *ctrl_conn; 27 static int hostapd_cli_quit = 0; 28 static int hostapd_cli_attached = 0; 29 30 #ifndef CONFIG_CTRL_IFACE_DIR 31 #define CONFIG_CTRL_IFACE_DIR "/var/run/hostapd" 32 #endif /* CONFIG_CTRL_IFACE_DIR */ 33 static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR; 34 static const char *client_socket_dir = NULL; 35 36 static char *ctrl_ifname = NULL; 37 static const char *pid_file = NULL; 38 static const char *action_file = NULL; 39 static int ping_interval = 5; 40 static int interactive = 0; 41 static int event_handler_registered = 0; 42 43 static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */ 44 45 static void print_help(FILE *stream, const char *cmd); 46 static char ** list_cmd_list(void); 47 static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx); 48 static void update_stations(struct wpa_ctrl *ctrl); 49 static void cli_event(const char *str); 50 51 52 static void usage(void) 53 { 54 fprintf(stderr, "%s\n", hostapd_cli_version); 55 fprintf(stderr, 56 "\n" 57 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvBr] " 58 "[-a<path>] \\\n" 59 " [-P<pid file>] [-G<ping interval>] [command..]\n" 60 "\n" 61 "Options:\n" 62 " -h help (show this usage text)\n" 63 " -v shown version information\n" 64 " -p<path> path to find control sockets (default: " 65 "/var/run/hostapd)\n" 66 " -s<dir_path> dir path to open client sockets (default: " 67 CONFIG_CTRL_IFACE_DIR ")\n" 68 " -a<file> run in daemon mode executing the action file " 69 "based on events\n" 70 " from hostapd\n" 71 " -r try to reconnect when client socket is " 72 "disconnected.\n" 73 " This is useful only when used with -a.\n" 74 " -B run a daemon in the background\n" 75 " -i<ifname> Interface to listen on (default: first " 76 "interface found in the\n" 77 " socket path)\n\n"); 78 print_help(stderr, NULL); 79 } 80 81 82 static void register_event_handler(struct wpa_ctrl *ctrl) 83 { 84 if (!ctrl_conn) 85 return; 86 if (interactive) { 87 event_handler_registered = 88 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl), 89 hostapd_cli_receive, 90 NULL, NULL); 91 } 92 } 93 94 95 static void unregister_event_handler(struct wpa_ctrl *ctrl) 96 { 97 if (!ctrl_conn) 98 return; 99 if (interactive && event_handler_registered) { 100 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl)); 101 event_handler_registered = 0; 102 } 103 } 104 105 106 static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname) 107 { 108 #ifndef CONFIG_CTRL_IFACE_UDP 109 char *cfile; 110 int flen; 111 #endif /* !CONFIG_CTRL_IFACE_UDP */ 112 113 if (ifname == NULL) 114 return NULL; 115 116 #ifdef CONFIG_CTRL_IFACE_UDP 117 ctrl_conn = wpa_ctrl_open(ifname); 118 return ctrl_conn; 119 #else /* CONFIG_CTRL_IFACE_UDP */ 120 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; 121 cfile = malloc(flen); 122 if (cfile == NULL) 123 return NULL; 124 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); 125 126 if (client_socket_dir && client_socket_dir[0] && 127 access(client_socket_dir, F_OK) < 0) { 128 perror(client_socket_dir); 129 free(cfile); 130 return NULL; 131 } 132 133 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir); 134 free(cfile); 135 return ctrl_conn; 136 #endif /* CONFIG_CTRL_IFACE_UDP */ 137 } 138 139 140 static void hostapd_cli_close_connection(void) 141 { 142 if (ctrl_conn == NULL) 143 return; 144 145 unregister_event_handler(ctrl_conn); 146 if (hostapd_cli_attached) { 147 wpa_ctrl_detach(ctrl_conn); 148 hostapd_cli_attached = 0; 149 } 150 wpa_ctrl_close(ctrl_conn); 151 ctrl_conn = NULL; 152 } 153 154 155 static int hostapd_cli_reconnect(const char *ifname) 156 { 157 char *next_ctrl_ifname; 158 159 hostapd_cli_close_connection(); 160 161 if (!ifname) 162 return -1; 163 164 next_ctrl_ifname = os_strdup(ifname); 165 os_free(ctrl_ifname); 166 ctrl_ifname = next_ctrl_ifname; 167 if (!ctrl_ifname) 168 return -1; 169 170 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname); 171 if (!ctrl_conn) 172 return -1; 173 if (!interactive && !action_file) 174 return 0; 175 if (wpa_ctrl_attach(ctrl_conn) == 0) { 176 hostapd_cli_attached = 1; 177 register_event_handler(ctrl_conn); 178 update_stations(ctrl_conn); 179 } else { 180 printf("Warning: Failed to attach to hostapd.\n"); 181 } 182 return 0; 183 } 184 185 186 static void hostapd_cli_msg_cb(char *msg, size_t len) 187 { 188 cli_event(msg); 189 printf("%s\n", msg); 190 } 191 192 193 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print) 194 { 195 char buf[4096]; 196 size_t len; 197 int ret; 198 199 if (ctrl_conn == NULL) { 200 printf("Not connected to hostapd - command dropped.\n"); 201 return -1; 202 } 203 len = sizeof(buf) - 1; 204 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, 205 hostapd_cli_msg_cb); 206 if (ret == -2) { 207 printf("'%s' command timed out.\n", cmd); 208 return -2; 209 } else if (ret < 0) { 210 printf("'%s' command failed.\n", cmd); 211 return -1; 212 } 213 if (print) { 214 buf[len] = '\0'; 215 printf("%s", buf); 216 } 217 return 0; 218 } 219 220 221 static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd) 222 { 223 return _wpa_ctrl_command(ctrl, cmd, 1); 224 } 225 226 227 static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, 228 int min_args, int argc, char *argv[]) 229 { 230 char buf[4096]; 231 232 if (argc < min_args) { 233 printf("Invalid %s command - at least %d argument%s required.\n", 234 cmd, min_args, min_args > 1 ? "s are" : " is"); 235 return -1; 236 } 237 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0) 238 return -1; 239 return wpa_ctrl_command(ctrl, buf); 240 } 241 242 243 static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[]) 244 { 245 return wpa_ctrl_command(ctrl, "PING"); 246 } 247 248 249 static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[]) 250 { 251 return wpa_ctrl_command(ctrl, "RELOG"); 252 } 253 254 255 static int hostapd_cli_cmd_close_log(struct wpa_ctrl *ctrl, int argc, 256 char *argv[]) 257 { 258 return wpa_ctrl_command(ctrl, "CLOSE_LOG"); 259 } 260 261 262 static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[]) 263 { 264 if (argc > 0 && os_strcmp(argv[0], "driver") == 0) 265 return wpa_ctrl_command(ctrl, "STATUS-DRIVER"); 266 return wpa_ctrl_command(ctrl, "STATUS"); 267 } 268 269 270 static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[]) 271 { 272 if (argc > 0) { 273 char buf[100]; 274 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]); 275 return wpa_ctrl_command(ctrl, buf); 276 } 277 return wpa_ctrl_command(ctrl, "MIB"); 278 } 279 280 281 static int hostapd_cli_exec(const char *program, const char *arg1, 282 const char *arg2) 283 { 284 char *arg; 285 size_t len; 286 int res; 287 288 len = os_strlen(arg1) + os_strlen(arg2) + 2; 289 arg = os_malloc(len); 290 if (arg == NULL) 291 return -1; 292 os_snprintf(arg, len, "%s %s", arg1, arg2); 293 res = os_exec(program, arg, 1); 294 os_free(arg); 295 296 return res; 297 } 298 299 300 static void hostapd_cli_action_process(char *msg, size_t len) 301 { 302 const char *pos; 303 304 pos = msg; 305 if (*pos == '<') { 306 pos = os_strchr(pos, '>'); 307 if (pos) 308 pos++; 309 else 310 pos = msg; 311 } 312 313 hostapd_cli_exec(action_file, ctrl_ifname, pos); 314 } 315 316 317 static void hostapd_cli_action_cb(char *msg, size_t len) 318 { 319 hostapd_cli_action_process(msg, len); 320 } 321 322 323 static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[]) 324 { 325 char buf[64]; 326 if (argc < 1) { 327 printf("Invalid 'sta' command - at least one argument, STA " 328 "address, is required.\n"); 329 return -1; 330 } 331 if (argc > 1) 332 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]); 333 else 334 snprintf(buf, sizeof(buf), "STA %s", argv[0]); 335 return wpa_ctrl_command(ctrl, buf); 336 } 337 338 339 static char ** hostapd_complete_stations(const char *str, int pos) 340 { 341 int arg = get_cmd_arg_num(str, pos); 342 char **res = NULL; 343 344 switch (arg) { 345 case 1: 346 res = cli_txt_list_array(&stations); 347 break; 348 } 349 350 return res; 351 } 352 353 354 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc, 355 char *argv[]) 356 { 357 char buf[64]; 358 if (argc != 1) { 359 printf("Invalid 'new_sta' command - exactly one argument, STA " 360 "address, is required.\n"); 361 return -1; 362 } 363 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]); 364 return wpa_ctrl_command(ctrl, buf); 365 } 366 367 368 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc, 369 char *argv[]) 370 { 371 char buf[64]; 372 if (argc < 1) { 373 printf("Invalid 'deauthenticate' command - exactly one " 374 "argument, STA address, is required.\n"); 375 return -1; 376 } 377 if (argc > 1) 378 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s", 379 argv[0], argv[1]); 380 else 381 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]); 382 return wpa_ctrl_command(ctrl, buf); 383 } 384 385 386 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc, 387 char *argv[]) 388 { 389 char buf[64]; 390 if (argc < 1) { 391 printf("Invalid 'disassociate' command - exactly one " 392 "argument, STA address, is required.\n"); 393 return -1; 394 } 395 if (argc > 1) 396 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s", 397 argv[0], argv[1]); 398 else 399 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]); 400 return wpa_ctrl_command(ctrl, buf); 401 } 402 403 404 #ifdef CONFIG_TAXONOMY 405 static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc, 406 char *argv[]) 407 { 408 char buf[64]; 409 410 if (argc != 1) { 411 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n"); 412 return -1; 413 } 414 os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]); 415 return wpa_ctrl_command(ctrl, buf); 416 } 417 #endif /* CONFIG_TAXONOMY */ 418 419 420 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc, 421 char *argv[]) 422 { 423 char buf[64]; 424 if (argc != 1) { 425 printf("Invalid 'sa_query' command - exactly one argument, " 426 "STA address, is required.\n"); 427 return -1; 428 } 429 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]); 430 return wpa_ctrl_command(ctrl, buf); 431 } 432 433 434 #ifdef CONFIG_WPS 435 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, 436 char *argv[]) 437 { 438 char buf[256]; 439 if (argc < 2) { 440 printf("Invalid 'wps_pin' command - at least two arguments, " 441 "UUID and PIN, are required.\n"); 442 return -1; 443 } 444 if (argc > 3) 445 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s", 446 argv[0], argv[1], argv[2], argv[3]); 447 else if (argc > 2) 448 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s", 449 argv[0], argv[1], argv[2]); 450 else 451 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]); 452 return wpa_ctrl_command(ctrl, buf); 453 } 454 455 456 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc, 457 char *argv[]) 458 { 459 char cmd[256]; 460 int res; 461 462 if (argc != 1 && argc != 2) { 463 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n" 464 "- PIN to be verified\n"); 465 return -1; 466 } 467 468 if (argc == 2) 469 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s", 470 argv[0], argv[1]); 471 else 472 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s", 473 argv[0]); 474 if (os_snprintf_error(sizeof(cmd), res)) { 475 printf("Too long WPS_CHECK_PIN command.\n"); 476 return -1; 477 } 478 return wpa_ctrl_command(ctrl, cmd); 479 } 480 481 482 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, 483 char *argv[]) 484 { 485 return wpa_ctrl_command(ctrl, "WPS_PBC"); 486 } 487 488 489 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc, 490 char *argv[]) 491 { 492 return wpa_ctrl_command(ctrl, "WPS_CANCEL"); 493 } 494 495 496 #ifdef CONFIG_WPS_NFC 497 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc, 498 char *argv[]) 499 { 500 int ret; 501 char *buf; 502 size_t buflen; 503 504 if (argc != 1) { 505 printf("Invalid 'wps_nfc_tag_read' command - one argument " 506 "is required.\n"); 507 return -1; 508 } 509 510 buflen = 18 + os_strlen(argv[0]); 511 buf = os_malloc(buflen); 512 if (buf == NULL) 513 return -1; 514 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]); 515 516 ret = wpa_ctrl_command(ctrl, buf); 517 os_free(buf); 518 519 return ret; 520 } 521 522 523 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, 524 int argc, char *argv[]) 525 { 526 char cmd[64]; 527 int res; 528 529 if (argc != 1) { 530 printf("Invalid 'wps_nfc_config_token' command - one argument " 531 "is required.\n"); 532 return -1; 533 } 534 535 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s", 536 argv[0]); 537 if (os_snprintf_error(sizeof(cmd), res)) { 538 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n"); 539 return -1; 540 } 541 return wpa_ctrl_command(ctrl, cmd); 542 } 543 544 545 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, 546 int argc, char *argv[]) 547 { 548 char cmd[64]; 549 int res; 550 551 if (argc != 1) { 552 printf("Invalid 'wps_nfc_token' command - one argument is " 553 "required.\n"); 554 return -1; 555 } 556 557 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]); 558 if (os_snprintf_error(sizeof(cmd), res)) { 559 printf("Too long WPS_NFC_TOKEN command.\n"); 560 return -1; 561 } 562 return wpa_ctrl_command(ctrl, cmd); 563 } 564 565 566 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, 567 int argc, char *argv[]) 568 { 569 char cmd[64]; 570 int res; 571 572 if (argc != 2) { 573 printf("Invalid 'nfc_get_handover_sel' command - two arguments " 574 "are required.\n"); 575 return -1; 576 } 577 578 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s", 579 argv[0], argv[1]); 580 if (os_snprintf_error(sizeof(cmd), res)) { 581 printf("Too long NFC_GET_HANDOVER_SEL command.\n"); 582 return -1; 583 } 584 return wpa_ctrl_command(ctrl, cmd); 585 } 586 587 #endif /* CONFIG_WPS_NFC */ 588 589 590 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc, 591 char *argv[]) 592 { 593 char buf[64]; 594 if (argc < 1) { 595 printf("Invalid 'wps_ap_pin' command - at least one argument " 596 "is required.\n"); 597 return -1; 598 } 599 if (argc > 2) 600 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s", 601 argv[0], argv[1], argv[2]); 602 else if (argc > 1) 603 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s", 604 argv[0], argv[1]); 605 else 606 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]); 607 return wpa_ctrl_command(ctrl, buf); 608 } 609 610 611 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc, 612 char *argv[]) 613 { 614 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS"); 615 } 616 617 618 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc, 619 char *argv[]) 620 { 621 char buf[256]; 622 char ssid_hex[2 * SSID_MAX_LEN + 1]; 623 char key_hex[2 * 64 + 1]; 624 int i; 625 626 if (argc < 1) { 627 printf("Invalid 'wps_config' command - at least two arguments " 628 "are required.\n"); 629 return -1; 630 } 631 632 ssid_hex[0] = '\0'; 633 for (i = 0; i < SSID_MAX_LEN; i++) { 634 if (argv[0][i] == '\0') 635 break; 636 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]); 637 } 638 639 key_hex[0] = '\0'; 640 if (argc > 3) { 641 for (i = 0; i < 64; i++) { 642 if (argv[3][i] == '\0') 643 break; 644 os_snprintf(&key_hex[i * 2], 3, "%02x", 645 argv[3][i]); 646 } 647 } 648 649 if (argc > 3) 650 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s", 651 ssid_hex, argv[1], argv[2], key_hex); 652 else if (argc > 2) 653 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s", 654 ssid_hex, argv[1], argv[2]); 655 else 656 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s", 657 ssid_hex, argv[1]); 658 return wpa_ctrl_command(ctrl, buf); 659 } 660 #endif /* CONFIG_WPS */ 661 662 663 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc, 664 char *argv[]) 665 { 666 char buf[300]; 667 int res; 668 669 if (argc < 2) { 670 printf("Invalid 'disassoc_imminent' command - two arguments " 671 "(STA addr and Disassociation Timer) are needed\n"); 672 return -1; 673 } 674 675 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s", 676 argv[0], argv[1]); 677 if (os_snprintf_error(sizeof(buf), res)) 678 return -1; 679 return wpa_ctrl_command(ctrl, buf); 680 } 681 682 683 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc, 684 char *argv[]) 685 { 686 char buf[300]; 687 int res; 688 689 if (argc < 3) { 690 printf("Invalid 'ess_disassoc' command - three arguments (STA " 691 "addr, disassoc timer, and URL) are needed\n"); 692 return -1; 693 } 694 695 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s", 696 argv[0], argv[1], argv[2]); 697 if (os_snprintf_error(sizeof(buf), res)) 698 return -1; 699 return wpa_ctrl_command(ctrl, buf); 700 } 701 702 703 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc, 704 char *argv[]) 705 { 706 char buf[2000], *tmp; 707 int res, i, total; 708 709 if (argc < 1) { 710 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n"); 711 return -1; 712 } 713 714 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]); 715 if (os_snprintf_error(sizeof(buf), res)) 716 return -1; 717 718 total = res; 719 for (i = 1; i < argc; i++) { 720 tmp = &buf[total]; 721 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]); 722 if (os_snprintf_error(sizeof(buf) - total, res)) 723 return -1; 724 total += res; 725 } 726 return wpa_ctrl_command(ctrl, buf); 727 } 728 729 730 static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc, 731 char *argv[]) 732 { 733 return wpa_ctrl_command(ctrl, "GET_CONFIG"); 734 } 735 736 737 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd, 738 char *addr, size_t addr_len, int print) 739 { 740 char buf[4096], *pos; 741 size_t len; 742 int ret; 743 744 if (ctrl_conn == NULL) { 745 printf("Not connected to hostapd - command dropped.\n"); 746 return -1; 747 } 748 len = sizeof(buf) - 1; 749 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, 750 hostapd_cli_msg_cb); 751 if (ret == -2) { 752 printf("'%s' command timed out.\n", cmd); 753 return -2; 754 } else if (ret < 0) { 755 printf("'%s' command failed.\n", cmd); 756 return -1; 757 } 758 759 buf[len] = '\0'; 760 if (memcmp(buf, "FAIL", 4) == 0) 761 return -1; 762 if (print) 763 printf("%s", buf); 764 765 pos = buf; 766 while (*pos != '\0' && *pos != '\n') 767 pos++; 768 *pos = '\0'; 769 os_strlcpy(addr, buf, addr_len); 770 return 0; 771 } 772 773 774 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, 775 char *argv[]) 776 { 777 char addr[32], cmd[64]; 778 779 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1)) 780 return 0; 781 do { 782 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); 783 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0); 784 785 return -1; 786 } 787 788 789 static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc, 790 char *argv[]) 791 { 792 char addr[32], cmd[64]; 793 794 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0)) 795 return 0; 796 do { 797 if (os_strcmp(addr, "") != 0) 798 printf("%s\n", addr); 799 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); 800 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0); 801 802 return 0; 803 } 804 805 806 static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[]) 807 { 808 print_help(stdout, argc > 0 ? argv[0] : NULL); 809 return 0; 810 } 811 812 813 static char ** hostapd_cli_complete_help(const char *str, int pos) 814 { 815 int arg = get_cmd_arg_num(str, pos); 816 char **res = NULL; 817 818 switch (arg) { 819 case 1: 820 res = list_cmd_list(); 821 break; 822 } 823 824 return res; 825 } 826 827 828 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, 829 char *argv[]) 830 { 831 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license); 832 return 0; 833 } 834 835 836 static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl, 837 int argc, char *argv[]) 838 { 839 char buf[200]; 840 int res; 841 842 if (argc != 1) { 843 printf("Invalid 'set_qos_map_set' command - " 844 "one argument (comma delimited QoS map set) " 845 "is needed\n"); 846 return -1; 847 } 848 849 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]); 850 if (os_snprintf_error(sizeof(buf), res)) 851 return -1; 852 return wpa_ctrl_command(ctrl, buf); 853 } 854 855 856 static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl, 857 int argc, char *argv[]) 858 { 859 char buf[50]; 860 int res; 861 862 if (argc != 1) { 863 printf("Invalid 'send_qos_map_conf' command - " 864 "one argument (STA addr) is needed\n"); 865 return -1; 866 } 867 868 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]); 869 if (os_snprintf_error(sizeof(buf), res)) 870 return -1; 871 return wpa_ctrl_command(ctrl, buf); 872 } 873 874 875 static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc, 876 char *argv[]) 877 { 878 char buf[300]; 879 int res; 880 881 if (argc < 2) { 882 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA " 883 "addr and URL) are needed\n"); 884 return -1; 885 } 886 887 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s", 888 argv[0], argv[1]); 889 if (os_snprintf_error(sizeof(buf), res)) 890 return -1; 891 return wpa_ctrl_command(ctrl, buf); 892 } 893 894 895 static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc, 896 char *argv[]) 897 { 898 char buf[300]; 899 int res; 900 901 if (argc < 3) { 902 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n"); 903 return -1; 904 } 905 906 if (argc > 3) 907 res = os_snprintf(buf, sizeof(buf), 908 "HS20_DEAUTH_REQ %s %s %s %s", 909 argv[0], argv[1], argv[2], argv[3]); 910 else 911 res = os_snprintf(buf, sizeof(buf), 912 "HS20_DEAUTH_REQ %s %s %s", 913 argv[0], argv[1], argv[2]); 914 if (os_snprintf_error(sizeof(buf), res)) 915 return -1; 916 return wpa_ctrl_command(ctrl, buf); 917 } 918 919 920 static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[]) 921 { 922 hostapd_cli_quit = 1; 923 if (interactive) 924 eloop_terminate(); 925 return 0; 926 } 927 928 929 static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[]) 930 { 931 char cmd[256]; 932 if (argc != 1) { 933 printf("Invalid LEVEL command: needs one argument (debug " 934 "level)\n"); 935 return 0; 936 } 937 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]); 938 return wpa_ctrl_command(ctrl, cmd); 939 } 940 941 942 static void update_stations(struct wpa_ctrl *ctrl) 943 { 944 char addr[32], cmd[64]; 945 946 if (!ctrl || !interactive) 947 return; 948 949 cli_txt_list_flush(&stations); 950 951 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0)) 952 return; 953 do { 954 if (os_strcmp(addr, "") != 0) 955 cli_txt_list_add(&stations, addr); 956 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); 957 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0); 958 } 959 960 961 static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl, 962 struct dl_list *interfaces) 963 { 964 struct dirent *dent; 965 DIR *dir; 966 967 if (!ctrl || !interfaces) 968 return; 969 dir = opendir(ctrl_iface_dir); 970 if (dir == NULL) 971 return; 972 973 while ((dent = readdir(dir))) { 974 if (strcmp(dent->d_name, ".") == 0 || 975 strcmp(dent->d_name, "..") == 0) 976 continue; 977 cli_txt_list_add(interfaces, dent->d_name); 978 } 979 closedir(dir); 980 } 981 982 983 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl) 984 { 985 struct dirent *dent; 986 DIR *dir; 987 988 dir = opendir(ctrl_iface_dir); 989 if (dir == NULL) { 990 printf("Control interface directory '%s' could not be " 991 "opened.\n", ctrl_iface_dir); 992 return; 993 } 994 995 printf("Available interfaces:\n"); 996 while ((dent = readdir(dir))) { 997 if (strcmp(dent->d_name, ".") == 0 || 998 strcmp(dent->d_name, "..") == 0) 999 continue; 1000 printf("%s\n", dent->d_name); 1001 } 1002 closedir(dir); 1003 } 1004 1005 1006 static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, 1007 char *argv[]) 1008 { 1009 if (argc < 1) { 1010 hostapd_cli_list_interfaces(ctrl); 1011 return 0; 1012 } 1013 if (hostapd_cli_reconnect(argv[0]) != 0) { 1014 printf("Could not connect to interface '%s' - re-trying\n", 1015 ctrl_ifname); 1016 } 1017 return 0; 1018 } 1019 1020 1021 static char ** hostapd_complete_interface(const char *str, int pos) 1022 { 1023 int arg = get_cmd_arg_num(str, pos); 1024 char **res = NULL; 1025 DEFINE_DL_LIST(interfaces); 1026 1027 switch (arg) { 1028 case 1: 1029 hostapd_cli_get_interfaces(ctrl_conn, &interfaces); 1030 res = cli_txt_list_array(&interfaces); 1031 cli_txt_list_flush(&interfaces); 1032 break; 1033 } 1034 1035 return res; 1036 } 1037 1038 1039 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1040 { 1041 char cmd[2048]; 1042 int res; 1043 1044 if (argc != 2) { 1045 printf("Invalid SET command: needs two arguments (variable " 1046 "name and value)\n"); 1047 return -1; 1048 } 1049 1050 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]); 1051 if (os_snprintf_error(sizeof(cmd), res)) { 1052 printf("Too long SET command.\n"); 1053 return -1; 1054 } 1055 return wpa_ctrl_command(ctrl, cmd); 1056 } 1057 1058 1059 static char ** hostapd_complete_set(const char *str, int pos) 1060 { 1061 int arg = get_cmd_arg_num(str, pos); 1062 const char *fields[] = { 1063 #ifdef CONFIG_WPS_TESTING 1064 "wps_version_number", "wps_testing_stub_cred", 1065 "wps_corrupt_pkhash", 1066 #endif /* CONFIG_WPS_TESTING */ 1067 #ifdef CONFIG_INTERWORKING 1068 "gas_frag_limit", 1069 #endif /* CONFIG_INTERWORKING */ 1070 #ifdef CONFIG_TESTING_OPTIONS 1071 "ext_mgmt_frame_handling", "ext_eapol_frame_io", 1072 #endif /* CONFIG_TESTING_OPTIONS */ 1073 #ifdef CONFIG_MBO 1074 "mbo_assoc_disallow", 1075 #endif /* CONFIG_MBO */ 1076 "deny_mac_file", "accept_mac_file", 1077 }; 1078 int i, num_fields = ARRAY_SIZE(fields); 1079 1080 if (arg == 1) { 1081 char **res; 1082 1083 res = os_calloc(num_fields + 1, sizeof(char *)); 1084 if (!res) 1085 return NULL; 1086 for (i = 0; i < num_fields; i++) { 1087 res[i] = os_strdup(fields[i]); 1088 if (!res[i]) 1089 return res; 1090 } 1091 return res; 1092 } 1093 return NULL; 1094 } 1095 1096 1097 static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1098 { 1099 char cmd[256]; 1100 int res; 1101 1102 if (argc != 1) { 1103 printf("Invalid GET command: needs one argument (variable " 1104 "name)\n"); 1105 return -1; 1106 } 1107 1108 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]); 1109 if (os_snprintf_error(sizeof(cmd), res)) { 1110 printf("Too long GET command.\n"); 1111 return -1; 1112 } 1113 return wpa_ctrl_command(ctrl, cmd); 1114 } 1115 1116 1117 static char ** hostapd_complete_get(const char *str, int pos) 1118 { 1119 int arg = get_cmd_arg_num(str, pos); 1120 const char *fields[] = { 1121 "version", "tls_library", 1122 }; 1123 int i, num_fields = ARRAY_SIZE(fields); 1124 1125 if (arg == 1) { 1126 char **res; 1127 1128 res = os_calloc(num_fields + 1, sizeof(char *)); 1129 if (!res) 1130 return NULL; 1131 for (i = 0; i < num_fields; i++) { 1132 res[i] = os_strdup(fields[i]); 1133 if (!res[i]) 1134 return res; 1135 } 1136 return res; 1137 } 1138 return NULL; 1139 } 1140 1141 1142 #ifdef CONFIG_FST 1143 static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1144 { 1145 char cmd[256]; 1146 int res; 1147 int i; 1148 int total; 1149 1150 if (argc <= 0) { 1151 printf("FST command: parameters are required.\n"); 1152 return -1; 1153 } 1154 1155 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER"); 1156 1157 for (i = 0; i < argc; i++) { 1158 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s", 1159 argv[i]); 1160 if (os_snprintf_error(sizeof(cmd) - total, res)) { 1161 printf("Too long fst command.\n"); 1162 return -1; 1163 } 1164 total += res; 1165 } 1166 return wpa_ctrl_command(ctrl, cmd); 1167 } 1168 #endif /* CONFIG_FST */ 1169 1170 1171 #ifdef CONFIG_IEEE80211AX 1172 static int hostapd_cli_cmd_color_change(struct wpa_ctrl *ctrl, 1173 int argc, char *argv[]) 1174 { 1175 return hostapd_cli_cmd(ctrl, "COLOR_CHANGE", 1, argc, argv); 1176 } 1177 #endif /* CONFIG_IEEE80211AX */ 1178 1179 1180 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl, 1181 int argc, char *argv[]) 1182 { 1183 char cmd[256]; 1184 int res; 1185 int i; 1186 char *tmp; 1187 int total; 1188 1189 if (argc < 2) { 1190 printf("Invalid chan_switch command: needs at least two " 1191 "arguments (count and freq)\n" 1192 "usage: <cs_count> <freq> [sec_channel_offset=] " 1193 "[center_freq1=] [center_freq2=] [bandwidth=] " 1194 "[blocktx] [ht|vht|he|eht]\n"); 1195 return -1; 1196 } 1197 1198 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s", 1199 argv[0], argv[1]); 1200 if (os_snprintf_error(sizeof(cmd), res)) { 1201 printf("Too long CHAN_SWITCH command.\n"); 1202 return -1; 1203 } 1204 1205 total = res; 1206 for (i = 2; i < argc; i++) { 1207 tmp = cmd + total; 1208 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]); 1209 if (os_snprintf_error(sizeof(cmd) - total, res)) { 1210 printf("Too long CHAN_SWITCH command.\n"); 1211 return -1; 1212 } 1213 total += res; 1214 } 1215 return wpa_ctrl_command(ctrl, cmd); 1216 } 1217 1218 1219 static int hostapd_cli_cmd_notify_cw_change(struct wpa_ctrl *ctrl, 1220 int argc, char *argv[]) 1221 { 1222 return hostapd_cli_cmd(ctrl, "NOTIFY_CW_CHANGE", 1, argc, argv); 1223 } 1224 1225 1226 static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc, 1227 char *argv[]) 1228 { 1229 return wpa_ctrl_command(ctrl, "ENABLE"); 1230 } 1231 1232 1233 static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc, 1234 char *argv[]) 1235 { 1236 return wpa_ctrl_command(ctrl, "RELOAD"); 1237 } 1238 1239 1240 static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc, 1241 char *argv[]) 1242 { 1243 return wpa_ctrl_command(ctrl, "RELOAD_BSS"); 1244 } 1245 1246 1247 static int hostapd_cli_cmd_reload_config(struct wpa_ctrl *ctrl, int argc, 1248 char *argv[]) 1249 { 1250 return wpa_ctrl_command(ctrl, "RELOAD_CONFIG"); 1251 } 1252 1253 1254 static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc, 1255 char *argv[]) 1256 { 1257 return wpa_ctrl_command(ctrl, "DISABLE"); 1258 } 1259 1260 1261 static int hostapd_cli_cmd_enable_mld(struct wpa_ctrl *ctrl, int argc, 1262 char *argv[]) 1263 { 1264 return wpa_ctrl_command(ctrl, "ENABLE_MLD"); 1265 } 1266 1267 1268 static int hostapd_cli_cmd_disable_mld(struct wpa_ctrl *ctrl, int argc, 1269 char *argv[]) 1270 { 1271 return wpa_ctrl_command(ctrl, "DISABLE_MLD"); 1272 } 1273 1274 1275 static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc, 1276 char *argv[]) 1277 { 1278 return wpa_ctrl_command(ctrl, "UPDATE_BEACON"); 1279 } 1280 1281 1282 static int hostapd_cli_cmd_stop_ap(struct wpa_ctrl *ctrl, int argc, 1283 char *argv[]) 1284 { 1285 return wpa_ctrl_command(ctrl, "STOP_AP"); 1286 } 1287 1288 1289 static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1290 { 1291 char cmd[256]; 1292 int res; 1293 1294 if (argc < 2 || argc > 4) { 1295 printf("Invalid vendor command\n" 1296 "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n"); 1297 return -1; 1298 } 1299 1300 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0], 1301 argv[1], argc >= 3 ? argv[2] : "", 1302 argc == 4 ? " " : "", argc == 4 ? argv[3] : ""); 1303 if (os_snprintf_error(sizeof(cmd), res)) { 1304 printf("Too long VENDOR command.\n"); 1305 return -1; 1306 } 1307 return wpa_ctrl_command(ctrl, cmd); 1308 } 1309 1310 1311 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc, 1312 char *argv[]) 1313 { 1314 return wpa_ctrl_command(ctrl, "ERP_FLUSH"); 1315 } 1316 1317 1318 static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, 1319 char *argv[]) 1320 { 1321 char cmd[256]; 1322 int res; 1323 1324 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s", 1325 argc >= 1 ? " " : "", 1326 argc >= 1 ? argv[0] : "", 1327 argc == 2 ? " " : "", 1328 argc == 2 ? argv[1] : ""); 1329 if (os_snprintf_error(sizeof(cmd), res)) { 1330 printf("Too long option\n"); 1331 return -1; 1332 } 1333 return wpa_ctrl_command(ctrl, cmd); 1334 } 1335 1336 1337 static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1338 { 1339 if (argc == 0) 1340 return -1; 1341 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]); 1342 } 1343 1344 1345 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1346 { 1347 return wpa_ctrl_command(ctrl, "PMKSA"); 1348 } 1349 1350 1351 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc, 1352 char *argv[]) 1353 { 1354 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH"); 1355 } 1356 1357 1358 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc, 1359 char *argv[]) 1360 { 1361 char cmd[2048]; 1362 int res; 1363 1364 if (argc < 3 || argc > 6) { 1365 printf("Invalid set_neighbor command: needs 3-6 arguments\n"); 1366 return -1; 1367 } 1368 1369 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s", 1370 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "", 1371 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : ""); 1372 if (os_snprintf_error(sizeof(cmd), res)) { 1373 printf("Too long SET_NEIGHBOR command.\n"); 1374 return -1; 1375 } 1376 return wpa_ctrl_command(ctrl, cmd); 1377 } 1378 1379 1380 static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc, 1381 char *argv[]) 1382 { 1383 return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR"); 1384 } 1385 1386 1387 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc, 1388 char *argv[]) 1389 { 1390 return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv); 1391 } 1392 1393 1394 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc, 1395 char *argv[]) 1396 { 1397 char cmd[256]; 1398 int res; 1399 1400 if (argc != 1) { 1401 printf("Invalid req_lci command - requires destination address\n"); 1402 return -1; 1403 } 1404 1405 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]); 1406 if (os_snprintf_error(sizeof(cmd), res)) { 1407 printf("Too long REQ_LCI command.\n"); 1408 return -1; 1409 } 1410 return wpa_ctrl_command(ctrl, cmd); 1411 } 1412 1413 1414 static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc, 1415 char *argv[]) 1416 { 1417 if (argc < 4) { 1418 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n"); 1419 return -1; 1420 } 1421 1422 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv); 1423 } 1424 1425 1426 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc, 1427 char *argv[]) 1428 { 1429 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS"); 1430 } 1431 1432 1433 static int hostapd_cli_cmd_driver_flags2(struct wpa_ctrl *ctrl, int argc, 1434 char *argv[]) 1435 { 1436 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS2"); 1437 } 1438 1439 1440 #ifdef CONFIG_DPP 1441 1442 static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc, 1443 char *argv[]) 1444 { 1445 return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv); 1446 } 1447 1448 1449 static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc, 1450 char *argv[]) 1451 { 1452 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv); 1453 } 1454 1455 1456 static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc, 1457 char *argv[]) 1458 { 1459 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv); 1460 } 1461 1462 1463 static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl, 1464 int argc, char *argv[]) 1465 { 1466 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv); 1467 } 1468 1469 1470 static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc, 1471 char *argv[]) 1472 { 1473 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv); 1474 } 1475 1476 1477 static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc, 1478 char *argv[]) 1479 { 1480 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv); 1481 } 1482 1483 1484 static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc, 1485 char *argv[]) 1486 { 1487 return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv); 1488 } 1489 1490 1491 static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc, 1492 char *argv[]) 1493 { 1494 return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv); 1495 } 1496 1497 1498 static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc, 1499 char *argv[]) 1500 { 1501 return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN"); 1502 } 1503 1504 1505 static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc, 1506 char *argv[]) 1507 { 1508 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv); 1509 } 1510 1511 1512 static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl, 1513 int argc, char *argv[]) 1514 { 1515 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv); 1516 } 1517 1518 1519 static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl, 1520 int argc, char *argv[]) 1521 { 1522 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv); 1523 } 1524 1525 1526 static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl, 1527 int argc, char *argv[]) 1528 { 1529 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv); 1530 } 1531 1532 1533 static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc, 1534 char *argv[]) 1535 { 1536 return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv); 1537 } 1538 1539 1540 static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc, 1541 char *argv[]) 1542 { 1543 return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv); 1544 } 1545 1546 1547 #ifdef CONFIG_DPP2 1548 1549 static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc, 1550 char *argv[]) 1551 { 1552 return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv); 1553 } 1554 1555 1556 static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc, 1557 char *argv[]) 1558 { 1559 return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP"); 1560 } 1561 1562 1563 static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc, 1564 char *argv[]) 1565 { 1566 return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv); 1567 } 1568 1569 1570 static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc, 1571 char *argv[]) 1572 { 1573 return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP"); 1574 } 1575 1576 #endif /* CONFIG_DPP2 */ 1577 1578 1579 #ifdef CONFIG_DPP3 1580 static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc, 1581 char *argv[]) 1582 { 1583 return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 0, argc, argv); 1584 } 1585 #endif /* CONFIG_DPP3 */ 1586 #endif /* CONFIG_DPP */ 1587 1588 1589 static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc, 1590 char *argv[]) 1591 { 1592 return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv); 1593 } 1594 1595 1596 static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc, 1597 char *argv[]) 1598 { 1599 return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv); 1600 } 1601 1602 1603 static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc, 1604 char *argv[]) 1605 { 1606 return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv); 1607 } 1608 1609 1610 static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc, 1611 char *argv[]) 1612 { 1613 return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv); 1614 } 1615 1616 1617 static int hostapd_cli_cmd_req_link_measurement(struct wpa_ctrl *ctrl, int argc, 1618 char *argv[]) 1619 { 1620 return hostapd_cli_cmd(ctrl, "REQ_LINK_MEASUREMENT", 1, argc, argv); 1621 } 1622 1623 1624 static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc, 1625 char *argv[]) 1626 { 1627 return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK"); 1628 } 1629 1630 1631 #ifdef CONFIG_IEEE80211R_AP 1632 1633 static int hostapd_cli_cmd_get_rxkhs(struct wpa_ctrl *ctrl, int argc, 1634 char *argv[]) 1635 { 1636 return wpa_ctrl_command(ctrl, "GET_RXKHS"); 1637 } 1638 1639 1640 static int hostapd_cli_cmd_reload_rxkhs(struct wpa_ctrl *ctrl, int argc, 1641 char *argv[]) 1642 { 1643 return wpa_ctrl_command(ctrl, "RELOAD_RXKHS"); 1644 } 1645 1646 #endif /* CONFIG_IEEE80211R_AP */ 1647 1648 1649 #ifdef ANDROID 1650 static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1651 { 1652 return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv); 1653 } 1654 #endif /* ANDROID */ 1655 1656 1657 struct hostapd_cli_cmd { 1658 const char *cmd; 1659 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]); 1660 char ** (*completion)(const char *str, int pos); 1661 const char *usage; 1662 }; 1663 1664 static const struct hostapd_cli_cmd hostapd_cli_commands[] = { 1665 { "ping", hostapd_cli_cmd_ping, NULL, 1666 "= pings hostapd" }, 1667 { "mib", hostapd_cli_cmd_mib, NULL, 1668 "= get MIB variables (dot1x, dot11, radius)" }, 1669 { "relog", hostapd_cli_cmd_relog, NULL, 1670 "= reload/truncate debug log output file" }, 1671 { "close_log", hostapd_cli_cmd_close_log, NULL, 1672 "= disable debug log output file" }, 1673 { "status", hostapd_cli_cmd_status, NULL, 1674 "= show interface status info" }, 1675 { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations, 1676 "<addr> = get MIB variables for one station" }, 1677 { "all_sta", hostapd_cli_cmd_all_sta, NULL, 1678 "= get MIB variables for all stations" }, 1679 { "list_sta", hostapd_cli_cmd_list_sta, NULL, 1680 "= list all stations" }, 1681 { "new_sta", hostapd_cli_cmd_new_sta, NULL, 1682 "<addr> = add a new station" }, 1683 { "deauthenticate", hostapd_cli_cmd_deauthenticate, 1684 hostapd_complete_stations, 1685 "<addr> = deauthenticate a station" }, 1686 { "disassociate", hostapd_cli_cmd_disassociate, 1687 hostapd_complete_stations, 1688 "<addr> = disassociate a station" }, 1689 #ifdef CONFIG_TAXONOMY 1690 { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations, 1691 "<addr> = get taxonomy signature for a station" }, 1692 #endif /* CONFIG_TAXONOMY */ 1693 { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations, 1694 "<addr> = send SA Query to a station" }, 1695 #ifdef CONFIG_WPS 1696 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL, 1697 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" }, 1698 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL, 1699 "<PIN> = verify PIN checksum" }, 1700 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL, 1701 "= indicate button pushed to initiate PBC" }, 1702 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL, 1703 "= cancel the pending WPS operation" }, 1704 #ifdef CONFIG_WPS_NFC 1705 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL, 1706 "<hexdump> = report read NFC tag with WPS data" }, 1707 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL, 1708 "<WPS/NDEF> = build NFC configuration token" }, 1709 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL, 1710 "<WPS/NDEF/enable/disable> = manager NFC password token" }, 1711 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL, 1712 NULL }, 1713 #endif /* CONFIG_WPS_NFC */ 1714 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL, 1715 "<cmd> [params..] = enable/disable AP PIN" }, 1716 { "wps_config", hostapd_cli_cmd_wps_config, NULL, 1717 "<SSID> <auth> <encr> <key> = configure AP" }, 1718 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL, 1719 "= show current WPS status" }, 1720 #endif /* CONFIG_WPS */ 1721 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, 1722 "= send Disassociation Imminent notification" }, 1723 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, 1724 "= send ESS Dissassociation Imminent notification" }, 1725 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, 1726 "= send BSS Transition Management Request" }, 1727 { "get_config", hostapd_cli_cmd_get_config, NULL, 1728 "= show current configuration" }, 1729 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help, 1730 "= show this usage help" }, 1731 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface, 1732 "[ifname] = show interfaces/select interface" }, 1733 #ifdef CONFIG_FST 1734 { "fst", hostapd_cli_cmd_fst, NULL, 1735 "<params...> = send FST-MANAGER control interface command" }, 1736 #endif /* CONFIG_FST */ 1737 { "raw", hostapd_cli_cmd_raw, NULL, 1738 "<params..> = send unprocessed command" }, 1739 { "level", hostapd_cli_cmd_level, NULL, 1740 "<debug level> = change debug level" }, 1741 { "license", hostapd_cli_cmd_license, NULL, 1742 "= show full hostapd_cli license" }, 1743 { "quit", hostapd_cli_cmd_quit, NULL, 1744 "= exit hostapd_cli" }, 1745 { "set", hostapd_cli_cmd_set, hostapd_complete_set, 1746 "<name> <value> = set runtime variables" }, 1747 { "get", hostapd_cli_cmd_get, hostapd_complete_get, 1748 "<name> = get runtime info" }, 1749 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, 1750 "<arg,arg,...> = set QoS Map set element" }, 1751 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, 1752 hostapd_complete_stations, 1753 "<addr> = send QoS Map Configure frame" }, 1754 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, 1755 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n" 1756 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n" 1757 " = initiate channel switch announcement" }, 1758 #ifdef CONFIG_IEEE80211AX 1759 { "color_change", hostapd_cli_cmd_color_change, NULL, 1760 "<color> = initiate BSS color change to set the specified color\n" 1761 "Value 0 will disable the color.\n"}, 1762 #endif /* CONFIG_IEEE80211AX */ 1763 { "notify_cw_change", hostapd_cli_cmd_notify_cw_change, NULL, 1764 "<channel_width> = 0 - 20 MHz, 1 - 40 MHz, 2 - 80 MHz, 3 - 160 MHz" }, 1765 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, 1766 "<addr> <url>\n" 1767 " = send WNM-Notification Subscription Remediation Request" }, 1768 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, 1769 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n" 1770 " = send WNM-Notification imminent deauthentication indication" }, 1771 { "vendor", hostapd_cli_cmd_vendor, NULL, 1772 "<vendor id> <sub command id> [<hex formatted data>]\n" 1773 " = send vendor driver command" }, 1774 { "enable", hostapd_cli_cmd_enable, NULL, 1775 "= enable hostapd on current interface" }, 1776 { "reload", hostapd_cli_cmd_reload, NULL, 1777 "= reload configuration for current interface" }, 1778 { "reload_bss", hostapd_cli_cmd_reload_bss, NULL, 1779 "= reload configuration for current BSS" }, 1780 { "reload_config", hostapd_cli_cmd_reload_config, NULL, 1781 "= reload configuration for current interface" }, 1782 { "disable", hostapd_cli_cmd_disable, NULL, 1783 "= disable hostapd on current interface" }, 1784 { "enable_mld", hostapd_cli_cmd_enable_mld, NULL, 1785 "= enable AP MLD to which the interface is affiliated" }, 1786 { "disable_mld", hostapd_cli_cmd_disable_mld, NULL, 1787 "= disable AP MLD to which the interface is affiliated" }, 1788 { "update_beacon", hostapd_cli_cmd_update_beacon, NULL, 1789 "= update Beacon frame contents\n"}, 1790 { "stop_ap", hostapd_cli_cmd_stop_ap, NULL, 1791 "= stop AP\n"}, 1792 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, 1793 "= drop all ERP keys"}, 1794 { "log_level", hostapd_cli_cmd_log_level, NULL, 1795 "[level] = show/change log verbosity level" }, 1796 { "pmksa", hostapd_cli_cmd_pmksa, NULL, 1797 " = show PMKSA cache entries" }, 1798 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, 1799 " = flush PMKSA cache" }, 1800 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, 1801 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n" 1802 " = add AP to neighbor database" }, 1803 { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL, 1804 " = show neighbor database entries" }, 1805 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, 1806 "<addr> [ssid=<hex>] = remove AP from neighbor database" }, 1807 { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations, 1808 "<addr> = send LCI request to a station"}, 1809 { "req_range", hostapd_cli_cmd_req_range, NULL, 1810 " = send FTM range request"}, 1811 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, 1812 " = show supported driver flags"}, 1813 { "driver_flags2", hostapd_cli_cmd_driver_flags2, NULL, 1814 " = show supported driver flags2"}, 1815 #ifdef CONFIG_DPP 1816 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL, 1817 "report a scanned DPP URI from a QR Code" }, 1818 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL, 1819 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" }, 1820 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL, 1821 "*|<id> = remove DPP bootstrap information" }, 1822 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL, 1823 "<id> = get DPP bootstrap URI" }, 1824 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL, 1825 "<id> = show DPP bootstrap information" }, 1826 { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL, 1827 "<id> [conf=..] [ssid=<SSID>] [ssid_charset=#] [psk=<PSK>] [pass=<passphrase>] [configurator=<id>] [conn_status=#] [akm_use_selector=<0|1>] [group_id=..] [expiry=#] [csrattrs=..] = set DPP configurator parameters" }, 1828 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL, 1829 "peer=<id> [own=<id>] = initiate DPP bootstrapping" }, 1830 { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL, 1831 "<freq in MHz> = start DPP listen" }, 1832 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL, 1833 "= stop DPP listen" }, 1834 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL, 1835 "[curve=..] [key=..] = add DPP configurator" }, 1836 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove, 1837 NULL, 1838 "*|<id> = remove DPP configurator" }, 1839 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key, 1840 NULL, 1841 "<id> = Get DPP configurator's private key" }, 1842 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL, 1843 "conf=<role> configurator=<id> = generate self DPP configuration" }, 1844 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL, 1845 "add PKEX code" }, 1846 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL, 1847 "*|<id> = remove DPP pkex information" }, 1848 #ifdef CONFIG_DPP2 1849 { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL, 1850 "[tcp_port=<port>] [role=..] = start DPP controller" }, 1851 { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL, 1852 "= stop DPP controller" }, 1853 { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL, 1854 "own=<BI ID> iter=<count> = start DPP chirp" }, 1855 { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL, 1856 "= stop DPP chirp" }, 1857 #endif /* CONFIG_DPP2 */ 1858 #ifdef CONFIG_DPP3 1859 { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL, 1860 "= press DPP push button" }, 1861 #endif /* CONFIG_DPP3 */ 1862 #endif /* CONFIG_DPP */ 1863 { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL, 1864 "=Add/Delete/Show/Clear accept MAC ACL" }, 1865 { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL, 1866 "=Add/Delete/Show/Clear deny MAC ACL" }, 1867 { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations, 1868 "<addr> = poll a STA to check connectivity with a QoS null frame" }, 1869 { "req_beacon", hostapd_cli_cmd_req_beacon, NULL, 1870 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" }, 1871 { "req_link_measurement", hostapd_cli_cmd_req_link_measurement, NULL, 1872 "<addr> = send a link measurement report request to a station"}, 1873 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL, 1874 "= reload wpa_psk_file only" }, 1875 #ifdef CONFIG_IEEE80211R_AP 1876 { "reload_rxkhs", hostapd_cli_cmd_reload_rxkhs, NULL, 1877 "= reload R0KHs and R1KHs" }, 1878 { "get_rxkhs", hostapd_cli_cmd_get_rxkhs, NULL, 1879 "= get R0KHs and R1KHs" }, 1880 #endif /* CONFIG_IEEE80211R_AP */ 1881 #ifdef ANDROID 1882 { "driver", hostapd_cli_cmd_driver, NULL, 1883 "<driver sub command> [<hex formatted data>] = send driver command data" }, 1884 #endif /* ANDROID */ 1885 { NULL, NULL, NULL, NULL } 1886 }; 1887 1888 1889 /* 1890 * Prints command usage, lines are padded with the specified string. 1891 */ 1892 static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd, 1893 const char *pad) 1894 { 1895 char c; 1896 size_t n; 1897 1898 if (cmd->usage == NULL) 1899 return; 1900 fprintf(stream, "%s%s ", pad, cmd->cmd); 1901 for (n = 0; (c = cmd->usage[n]); n++) { 1902 fprintf(stream, "%c", c); 1903 if (c == '\n') 1904 fprintf(stream, "%s", pad); 1905 } 1906 fprintf(stream, "\n"); 1907 } 1908 1909 1910 static void print_help(FILE *stream, const char *cmd) 1911 { 1912 int n; 1913 1914 fprintf(stream, "commands:\n"); 1915 for (n = 0; hostapd_cli_commands[n].cmd; n++) { 1916 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd)) 1917 print_cmd_help(stream, &hostapd_cli_commands[n], " "); 1918 } 1919 } 1920 1921 1922 static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[]) 1923 { 1924 const struct hostapd_cli_cmd *cmd, *match = NULL; 1925 int count; 1926 1927 count = 0; 1928 cmd = hostapd_cli_commands; 1929 while (cmd->cmd) { 1930 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) { 1931 match = cmd; 1932 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) { 1933 /* we have an exact match */ 1934 count = 1; 1935 break; 1936 } 1937 count++; 1938 } 1939 cmd++; 1940 } 1941 1942 if (count > 1) { 1943 printf("Ambiguous command '%s'; possible commands:", argv[0]); 1944 cmd = hostapd_cli_commands; 1945 while (cmd->cmd) { 1946 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 1947 0) { 1948 printf(" %s", cmd->cmd); 1949 } 1950 cmd++; 1951 } 1952 printf("\n"); 1953 } else if (count == 0) { 1954 printf("Unknown command '%s'\n", argv[0]); 1955 } else { 1956 match->handler(ctrl, argc - 1, &argv[1]); 1957 } 1958 } 1959 1960 1961 static void cli_event(const char *str) 1962 { 1963 const char *start, *s; 1964 1965 start = os_strchr(str, '>'); 1966 if (start == NULL) 1967 return; 1968 1969 start++; 1970 1971 if (str_starts(start, AP_STA_CONNECTED)) { 1972 s = os_strchr(start, ' '); 1973 if (s == NULL) 1974 return; 1975 cli_txt_list_add(&stations, s + 1); 1976 return; 1977 } 1978 1979 if (str_starts(start, AP_STA_DISCONNECTED)) { 1980 s = os_strchr(start, ' '); 1981 if (s == NULL) 1982 return; 1983 cli_txt_list_del_addr(&stations, s + 1); 1984 return; 1985 } 1986 } 1987 1988 1989 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read, 1990 int action_monitor) 1991 { 1992 int first = 1; 1993 if (ctrl_conn == NULL) 1994 return; 1995 while (wpa_ctrl_pending(ctrl)) { 1996 char buf[4096]; 1997 size_t len = sizeof(buf) - 1; 1998 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) { 1999 buf[len] = '\0'; 2000 if (action_monitor) 2001 hostapd_cli_action_process(buf, len); 2002 else { 2003 cli_event(buf); 2004 if (in_read && first) 2005 printf("\n"); 2006 first = 0; 2007 printf("%s\n", buf); 2008 } 2009 } else { 2010 printf("Could not read pending message.\n"); 2011 break; 2012 } 2013 } 2014 } 2015 2016 2017 static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx) 2018 { 2019 hostapd_cli_recv_pending(ctrl_conn, 0, 0); 2020 } 2021 2022 2023 static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx) 2024 { 2025 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) { 2026 printf("Connection to hostapd lost - trying to reconnect\n"); 2027 hostapd_cli_close_connection(); 2028 } 2029 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0) 2030 printf("Connection to hostapd re-established\n"); 2031 if (ctrl_conn) 2032 hostapd_cli_recv_pending(ctrl_conn, 1, 0); 2033 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL); 2034 } 2035 2036 2037 static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx) 2038 { 2039 eloop_terminate(); 2040 } 2041 2042 2043 static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd) 2044 { 2045 char *argv[max_args]; 2046 int argc; 2047 argc = tokenize_cmd(cmd, argv); 2048 if (argc) 2049 wpa_request(ctrl_conn, argc, argv); 2050 } 2051 2052 2053 static void hostapd_cli_edit_eof_cb(void *ctx) 2054 { 2055 eloop_terminate(); 2056 } 2057 2058 2059 static char ** list_cmd_list(void) 2060 { 2061 char **res; 2062 int i, count; 2063 2064 count = ARRAY_SIZE(hostapd_cli_commands); 2065 res = os_calloc(count + 1, sizeof(char *)); 2066 if (res == NULL) 2067 return NULL; 2068 2069 for (i = 0; hostapd_cli_commands[i].cmd; i++) { 2070 res[i] = os_strdup(hostapd_cli_commands[i].cmd); 2071 if (res[i] == NULL) 2072 break; 2073 } 2074 2075 return res; 2076 } 2077 2078 2079 static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str, 2080 int pos) 2081 { 2082 int i; 2083 2084 for (i = 0; hostapd_cli_commands[i].cmd; i++) { 2085 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0) 2086 continue; 2087 if (hostapd_cli_commands[i].completion) 2088 return hostapd_cli_commands[i].completion(str, pos); 2089 if (!hostapd_cli_commands[i].usage) 2090 return NULL; 2091 edit_clear_line(); 2092 printf("\r%s\n", hostapd_cli_commands[i].usage); 2093 edit_redraw(); 2094 break; 2095 } 2096 2097 return NULL; 2098 } 2099 2100 2101 static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str, 2102 int pos) 2103 { 2104 char **res; 2105 const char *end; 2106 char *cmd; 2107 2108 end = os_strchr(str, ' '); 2109 if (end == NULL || str + pos < end) 2110 return list_cmd_list(); 2111 2112 cmd = os_malloc(pos + 1); 2113 if (cmd == NULL) 2114 return NULL; 2115 os_memcpy(cmd, str, pos); 2116 cmd[end - str] = '\0'; 2117 res = hostapd_cli_cmd_completion(cmd, str, pos); 2118 os_free(cmd); 2119 return res; 2120 } 2121 2122 2123 static void hostapd_cli_interactive(void) 2124 { 2125 char *hfile = NULL; 2126 char *home; 2127 2128 printf("\nInteractive mode\n\n"); 2129 2130 #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR 2131 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR; 2132 #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */ 2133 home = getenv("HOME"); 2134 #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */ 2135 if (home) { 2136 const char *fname = ".hostapd_cli_history"; 2137 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1; 2138 hfile = os_malloc(hfile_len); 2139 if (hfile) 2140 os_snprintf(hfile, hfile_len, "%s/%s", home, fname); 2141 } 2142 2143 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb, 2144 hostapd_cli_edit_completion_cb, NULL, hfile, NULL); 2145 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL); 2146 2147 eloop_run(); 2148 2149 cli_txt_list_flush(&stations); 2150 edit_deinit(hfile, NULL); 2151 os_free(hfile); 2152 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL); 2153 } 2154 2155 2156 static void hostapd_cli_cleanup(void) 2157 { 2158 hostapd_cli_close_connection(); 2159 if (pid_file) 2160 os_daemonize_terminate(pid_file); 2161 2162 os_program_deinit(); 2163 } 2164 2165 2166 static void hostapd_cli_action_ping(void *eloop_ctx, void *timeout_ctx) 2167 { 2168 struct wpa_ctrl *ctrl = eloop_ctx; 2169 char buf[256]; 2170 size_t len; 2171 2172 /* verify that connection is still working */ 2173 len = sizeof(buf) - 1; 2174 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len, 2175 hostapd_cli_action_cb) < 0 || 2176 len < 4 || os_memcmp(buf, "PONG", 4) != 0) { 2177 printf("hostapd did not reply to PING command - exiting\n"); 2178 eloop_terminate(); 2179 return; 2180 } 2181 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping, 2182 ctrl, NULL); 2183 } 2184 2185 2186 static void hostapd_cli_action_receive(int sock, void *eloop_ctx, 2187 void *sock_ctx) 2188 { 2189 struct wpa_ctrl *ctrl = eloop_ctx; 2190 2191 hostapd_cli_recv_pending(ctrl, 0, 1); 2192 } 2193 2194 2195 static void hostapd_cli_action(struct wpa_ctrl *ctrl) 2196 { 2197 int fd; 2198 2199 fd = wpa_ctrl_get_fd(ctrl); 2200 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping, 2201 ctrl, NULL); 2202 eloop_register_read_sock(fd, hostapd_cli_action_receive, ctrl, NULL); 2203 eloop_run(); 2204 eloop_cancel_timeout(hostapd_cli_action_ping, ctrl, NULL); 2205 eloop_unregister_read_sock(fd); 2206 } 2207 2208 2209 int main(int argc, char *argv[]) 2210 { 2211 int warning_displayed = 0; 2212 int c; 2213 int daemonize = 0; 2214 int reconnect = 0; 2215 2216 if (os_program_init()) 2217 return -1; 2218 2219 for (;;) { 2220 c = getopt(argc, argv, "a:BhG:i:p:P:rs:v"); 2221 if (c < 0) 2222 break; 2223 switch (c) { 2224 case 'a': 2225 action_file = optarg; 2226 break; 2227 case 'B': 2228 daemonize = 1; 2229 break; 2230 case 'G': 2231 ping_interval = atoi(optarg); 2232 break; 2233 case 'h': 2234 usage(); 2235 return 0; 2236 case 'v': 2237 printf("%s\n", hostapd_cli_version); 2238 return 0; 2239 case 'i': 2240 os_free(ctrl_ifname); 2241 ctrl_ifname = os_strdup(optarg); 2242 break; 2243 case 'p': 2244 ctrl_iface_dir = optarg; 2245 break; 2246 case 'P': 2247 pid_file = optarg; 2248 break; 2249 case 'r': 2250 reconnect = 1; 2251 break; 2252 case 's': 2253 client_socket_dir = optarg; 2254 break; 2255 default: 2256 usage(); 2257 return -1; 2258 } 2259 } 2260 2261 interactive = (argc == optind) && (action_file == NULL); 2262 2263 if (interactive) { 2264 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license); 2265 } 2266 2267 if (eloop_init()) 2268 return -1; 2269 2270 for (;;) { 2271 if (ctrl_ifname == NULL) { 2272 struct dirent *dent; 2273 DIR *dir = opendir(ctrl_iface_dir); 2274 if (dir) { 2275 while ((dent = readdir(dir))) { 2276 if (os_strcmp(dent->d_name, ".") == 0 2277 || 2278 os_strcmp(dent->d_name, "..") == 0) 2279 continue; 2280 printf("Selected interface '%s'\n", 2281 dent->d_name); 2282 ctrl_ifname = os_strdup(dent->d_name); 2283 break; 2284 } 2285 closedir(dir); 2286 } 2287 } 2288 hostapd_cli_reconnect(ctrl_ifname); 2289 if (ctrl_conn) { 2290 if (warning_displayed) 2291 printf("Connection established.\n"); 2292 break; 2293 } 2294 if (!interactive && !reconnect) { 2295 perror("Failed to connect to hostapd - " 2296 "wpa_ctrl_open"); 2297 return -1; 2298 } 2299 2300 if (!warning_displayed) { 2301 printf("Could not connect to hostapd - re-trying\n"); 2302 warning_displayed = 1; 2303 } 2304 os_sleep(1, 0); 2305 continue; 2306 } 2307 2308 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL); 2309 2310 if (action_file && !hostapd_cli_attached) 2311 return -1; 2312 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue()) 2313 return -1; 2314 if (reconnect && action_file && ctrl_ifname) { 2315 while (!hostapd_cli_quit) { 2316 if (ctrl_conn) 2317 hostapd_cli_action(ctrl_conn); 2318 os_sleep(1, 0); 2319 hostapd_cli_reconnect(ctrl_ifname); 2320 } 2321 } else if (interactive) 2322 hostapd_cli_interactive(); 2323 else if (action_file) 2324 hostapd_cli_action(ctrl_conn); 2325 else 2326 wpa_request(ctrl_conn, argc - optind, &argv[optind]); 2327 2328 unregister_event_handler(ctrl_conn); 2329 os_free(ctrl_ifname); 2330 eloop_destroy(); 2331 hostapd_cli_cleanup(); 2332 return 0; 2333 } 2334 2335 #else /* CONFIG_NO_CTRL_IFACE */ 2336 2337 int main(int argc, char *argv[]) 2338 { 2339 return -1; 2340 } 2341 2342 #endif /* CONFIG_NO_CTRL_IFACE */ 2343