2 * hostapd - command line interface for hostapd daemon
3 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
20 #ifndef CONFIG_NO_CTRL_IFACE
22 static const char *const hostapd_cli_version =
23 "hostapd_cli v" VERSION_STR "\n"
24 "Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi> and contributors";
26 static struct wpa_ctrl *ctrl_conn;
27 static int hostapd_cli_quit = 0;
28 static int hostapd_cli_attached = 0;
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;
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;
43 static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
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);
50 static void usage(void)
52 fprintf(stderr, "%s\n", hostapd_cli_version);
55 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvB] "
57 " [-P<pid file>] [-G<ping interval>] [command..]\n"
60 " -h help (show this usage text)\n"
61 " -v shown version information\n"
62 " -p<path> path to find control sockets (default: "
64 " -s<dir_path> dir path to open client sockets (default: "
65 CONFIG_CTRL_IFACE_DIR ")\n"
66 " -a<file> run in daemon mode executing the action file "
69 " -B run a daemon in the background\n"
70 " -i<ifname> Interface to listen on (default: first "
71 "interface found in the\n"
73 print_help(stderr, NULL);
77 static void register_event_handler(struct wpa_ctrl *ctrl)
82 event_handler_registered =
83 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
90 static void unregister_event_handler(struct wpa_ctrl *ctrl)
94 if (interactive && event_handler_registered) {
95 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
96 event_handler_registered = 0;
101 static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
103 #ifndef CONFIG_CTRL_IFACE_UDP
106 #endif /* !CONFIG_CTRL_IFACE_UDP */
111 #ifdef CONFIG_CTRL_IFACE_UDP
112 ctrl_conn = wpa_ctrl_open(ifname);
114 #else /* CONFIG_CTRL_IFACE_UDP */
115 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
116 cfile = malloc(flen);
119 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
121 if (client_socket_dir && client_socket_dir[0] &&
122 access(client_socket_dir, F_OK) < 0) {
123 perror(client_socket_dir);
128 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
131 #endif /* CONFIG_CTRL_IFACE_UDP */
135 static void hostapd_cli_close_connection(void)
137 if (ctrl_conn == NULL)
140 unregister_event_handler(ctrl_conn);
141 if (hostapd_cli_attached) {
142 wpa_ctrl_detach(ctrl_conn);
143 hostapd_cli_attached = 0;
145 wpa_ctrl_close(ctrl_conn);
150 static void hostapd_cli_msg_cb(char *msg, size_t len)
156 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
162 if (ctrl_conn == NULL) {
163 printf("Not connected to hostapd - command dropped.\n");
166 len = sizeof(buf) - 1;
167 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
170 printf("'%s' command timed out.\n", cmd);
172 } else if (ret < 0) {
173 printf("'%s' command failed.\n", cmd);
184 static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
186 return _wpa_ctrl_command(ctrl, cmd, 1);
190 static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
191 int min_args, int argc, char *argv[])
195 if (argc < min_args) {
196 printf("Invalid %s command - at least %d argument%s required.\n",
197 cmd, min_args, min_args > 1 ? "s are" : " is");
200 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
202 return wpa_ctrl_command(ctrl, buf);
206 static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
208 return wpa_ctrl_command(ctrl, "PING");
212 static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
214 return wpa_ctrl_command(ctrl, "RELOG");
218 static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
220 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
221 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
222 return wpa_ctrl_command(ctrl, "STATUS");
226 static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
230 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
231 return wpa_ctrl_command(ctrl, buf);
233 return wpa_ctrl_command(ctrl, "MIB");
237 static int hostapd_cli_exec(const char *program, const char *arg1,
244 len = os_strlen(arg1) + os_strlen(arg2) + 2;
245 arg = os_malloc(len);
248 os_snprintf(arg, len, "%s %s", arg1, arg2);
249 res = os_exec(program, arg, 1);
256 static void hostapd_cli_action_process(char *msg, size_t len)
262 pos = os_strchr(pos, '>');
269 hostapd_cli_exec(action_file, ctrl_ifname, pos);
273 static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
277 printf("Invalid 'sta' command - at least one argument, STA "
278 "address, is required.\n");
282 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
284 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
285 return wpa_ctrl_command(ctrl, buf);
289 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
294 printf("Invalid 'new_sta' command - exactly one argument, STA "
295 "address, is required.\n");
298 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
299 return wpa_ctrl_command(ctrl, buf);
303 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
308 printf("Invalid 'deauthenticate' command - exactly one "
309 "argument, STA address, is required.\n");
313 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
316 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
317 return wpa_ctrl_command(ctrl, buf);
321 static char ** hostapd_complete_deauthenticate(const char *str, int pos)
323 int arg = get_cmd_arg_num(str, pos);
328 res = cli_txt_list_array(&stations);
336 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
341 printf("Invalid 'disassociate' command - exactly one "
342 "argument, STA address, is required.\n");
346 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
349 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
350 return wpa_ctrl_command(ctrl, buf);
354 static char ** hostapd_complete_disassociate(const char *str, int pos)
356 int arg = get_cmd_arg_num(str, pos);
361 res = cli_txt_list_array(&stations);
369 #ifdef CONFIG_IEEE80211W
370 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
375 printf("Invalid 'sa_query' command - exactly one argument, "
376 "STA address, is required.\n");
379 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
380 return wpa_ctrl_command(ctrl, buf);
382 #endif /* CONFIG_IEEE80211W */
386 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
391 printf("Invalid 'wps_pin' command - at least two arguments, "
392 "UUID and PIN, are required.\n");
396 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
397 argv[0], argv[1], argv[2], argv[3]);
399 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
400 argv[0], argv[1], argv[2]);
402 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
403 return wpa_ctrl_command(ctrl, buf);
407 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
413 if (argc != 1 && argc != 2) {
414 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
415 "- PIN to be verified\n");
420 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
423 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
425 if (os_snprintf_error(sizeof(cmd), res)) {
426 printf("Too long WPS_CHECK_PIN command.\n");
429 return wpa_ctrl_command(ctrl, cmd);
433 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
436 return wpa_ctrl_command(ctrl, "WPS_PBC");
440 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
443 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
447 #ifdef CONFIG_WPS_NFC
448 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
456 printf("Invalid 'wps_nfc_tag_read' command - one argument "
461 buflen = 18 + os_strlen(argv[0]);
462 buf = os_malloc(buflen);
465 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
467 ret = wpa_ctrl_command(ctrl, buf);
474 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
475 int argc, char *argv[])
481 printf("Invalid 'wps_nfc_config_token' command - one argument "
486 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
488 if (os_snprintf_error(sizeof(cmd), res)) {
489 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
492 return wpa_ctrl_command(ctrl, cmd);
496 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
497 int argc, char *argv[])
503 printf("Invalid 'wps_nfc_token' command - one argument is "
508 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
509 if (os_snprintf_error(sizeof(cmd), res)) {
510 printf("Too long WPS_NFC_TOKEN command.\n");
513 return wpa_ctrl_command(ctrl, cmd);
517 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
518 int argc, char *argv[])
524 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
529 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
531 if (os_snprintf_error(sizeof(cmd), res)) {
532 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
535 return wpa_ctrl_command(ctrl, cmd);
538 #endif /* CONFIG_WPS_NFC */
541 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
546 printf("Invalid 'wps_ap_pin' command - at least one argument "
551 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
552 argv[0], argv[1], argv[2]);
554 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
557 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
558 return wpa_ctrl_command(ctrl, buf);
562 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
565 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
569 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
573 char ssid_hex[2 * SSID_MAX_LEN + 1];
574 char key_hex[2 * 64 + 1];
578 printf("Invalid 'wps_config' command - at least two arguments "
584 for (i = 0; i < SSID_MAX_LEN; i++) {
585 if (argv[0][i] == '\0')
587 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
592 for (i = 0; i < 64; i++) {
593 if (argv[3][i] == '\0')
595 os_snprintf(&key_hex[i * 2], 3, "%02x",
601 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
602 ssid_hex, argv[1], argv[2], key_hex);
604 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
605 ssid_hex, argv[1], argv[2]);
607 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
609 return wpa_ctrl_command(ctrl, buf);
611 #endif /* CONFIG_WPS */
614 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
621 printf("Invalid 'disassoc_imminent' command - two arguments "
622 "(STA addr and Disassociation Timer) are needed\n");
626 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
628 if (os_snprintf_error(sizeof(buf), res))
630 return wpa_ctrl_command(ctrl, buf);
634 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
641 printf("Invalid 'ess_disassoc' command - three arguments (STA "
642 "addr, disassoc timer, and URL) are needed\n");
646 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
647 argv[0], argv[1], argv[2]);
648 if (os_snprintf_error(sizeof(buf), res))
650 return wpa_ctrl_command(ctrl, buf);
654 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
657 char buf[2000], *tmp;
661 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
665 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
666 if (os_snprintf_error(sizeof(buf), res))
670 for (i = 1; i < argc; i++) {
672 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
673 if (os_snprintf_error(sizeof(buf) - total, res))
677 return wpa_ctrl_command(ctrl, buf);
681 static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
684 return wpa_ctrl_command(ctrl, "GET_CONFIG");
688 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
689 char *addr, size_t addr_len)
691 char buf[4096], *pos;
695 if (ctrl_conn == NULL) {
696 printf("Not connected to hostapd - command dropped.\n");
699 len = sizeof(buf) - 1;
700 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
703 printf("'%s' command timed out.\n", cmd);
705 } else if (ret < 0) {
706 printf("'%s' command failed.\n", cmd);
711 if (memcmp(buf, "FAIL", 4) == 0)
716 while (*pos != '\0' && *pos != '\n')
719 os_strlcpy(addr, buf, addr_len);
724 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
727 char addr[32], cmd[64];
729 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
732 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
733 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
739 static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
741 print_help(stdout, argc > 0 ? argv[0] : NULL);
746 static char ** hostapd_cli_complete_help(const char *str, int pos)
748 int arg = get_cmd_arg_num(str, pos);
753 res = list_cmd_list();
761 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
764 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
769 static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
770 int argc, char *argv[])
776 printf("Invalid 'set_qos_map_set' command - "
777 "one argument (comma delimited QoS map set) "
782 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
783 if (os_snprintf_error(sizeof(buf), res))
785 return wpa_ctrl_command(ctrl, buf);
789 static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
790 int argc, char *argv[])
796 printf("Invalid 'send_qos_map_conf' command - "
797 "one argument (STA addr) is needed\n");
801 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
802 if (os_snprintf_error(sizeof(buf), res))
804 return wpa_ctrl_command(ctrl, buf);
808 static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
815 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
816 "addr and URL) are needed\n");
820 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
822 if (os_snprintf_error(sizeof(buf), res))
824 return wpa_ctrl_command(ctrl, buf);
828 static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
835 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
840 res = os_snprintf(buf, sizeof(buf),
841 "HS20_DEAUTH_REQ %s %s %s %s",
842 argv[0], argv[1], argv[2], argv[3]);
844 res = os_snprintf(buf, sizeof(buf),
845 "HS20_DEAUTH_REQ %s %s %s",
846 argv[0], argv[1], argv[2]);
847 if (os_snprintf_error(sizeof(buf), res))
849 return wpa_ctrl_command(ctrl, buf);
853 static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
855 hostapd_cli_quit = 1;
862 static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
866 printf("Invalid LEVEL command: needs one argument (debug "
870 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
871 return wpa_ctrl_command(ctrl, cmd);
875 static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
876 struct dl_list *interfaces)
881 if (!ctrl || !interfaces)
883 dir = opendir(ctrl_iface_dir);
887 while ((dent = readdir(dir))) {
888 if (strcmp(dent->d_name, ".") == 0 ||
889 strcmp(dent->d_name, "..") == 0)
891 cli_txt_list_add(interfaces, dent->d_name);
897 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
902 dir = opendir(ctrl_iface_dir);
904 printf("Control interface directory '%s' could not be "
905 "openned.\n", ctrl_iface_dir);
909 printf("Available interfaces:\n");
910 while ((dent = readdir(dir))) {
911 if (strcmp(dent->d_name, ".") == 0 ||
912 strcmp(dent->d_name, "..") == 0)
914 printf("%s\n", dent->d_name);
920 static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
924 hostapd_cli_list_interfaces(ctrl);
928 hostapd_cli_close_connection();
929 os_free(ctrl_ifname);
930 ctrl_ifname = os_strdup(argv[0]);
931 if (ctrl_ifname == NULL)
934 if (hostapd_cli_open_connection(ctrl_ifname)) {
935 printf("Connected to interface '%s.\n", ctrl_ifname);
936 if (wpa_ctrl_attach(ctrl_conn) == 0) {
937 hostapd_cli_attached = 1;
938 register_event_handler(ctrl_conn);
940 printf("Warning: Failed to attach to "
944 printf("Could not connect to interface '%s' - re-trying\n",
951 static char ** hostapd_complete_interface(const char *str, int pos)
953 int arg = get_cmd_arg_num(str, pos);
955 DEFINE_DL_LIST(interfaces);
959 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
960 res = cli_txt_list_array(&interfaces);
961 cli_txt_list_flush(&interfaces);
969 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
975 printf("Invalid SET command: needs two arguments (variable "
976 "name and value)\n");
980 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
981 if (os_snprintf_error(sizeof(cmd), res)) {
982 printf("Too long SET command.\n");
985 return wpa_ctrl_command(ctrl, cmd);
989 static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
995 printf("Invalid GET command: needs one argument (variable "
1000 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
1001 if (os_snprintf_error(sizeof(cmd), res)) {
1002 printf("Too long GET command.\n");
1005 return wpa_ctrl_command(ctrl, cmd);
1010 static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1018 printf("FST command: parameters are required.\n");
1022 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1024 for (i = 0; i < argc; i++) {
1025 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1027 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1028 printf("Too long fst command.\n");
1033 return wpa_ctrl_command(ctrl, cmd);
1035 #endif /* CONFIG_FST */
1038 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1039 int argc, char *argv[])
1048 printf("Invalid chan_switch command: needs at least two "
1049 "arguments (count and freq)\n"
1050 "usage: <cs_count> <freq> [sec_channel_offset=] "
1051 "[center_freq1=] [center_freq2=] [bandwidth=] "
1052 "[blocktx] [ht|vht]\n");
1056 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1058 if (os_snprintf_error(sizeof(cmd), res)) {
1059 printf("Too long CHAN_SWITCH command.\n");
1064 for (i = 2; i < argc; i++) {
1066 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
1067 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1068 printf("Too long CHAN_SWITCH command.\n");
1073 return wpa_ctrl_command(ctrl, cmd);
1077 static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1080 return wpa_ctrl_command(ctrl, "ENABLE");
1084 static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1087 return wpa_ctrl_command(ctrl, "RELOAD");
1091 static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1094 return wpa_ctrl_command(ctrl, "DISABLE");
1098 static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1103 if (argc < 2 || argc > 3) {
1104 printf("Invalid vendor command\n"
1105 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1109 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
1110 argc == 3 ? argv[2] : "");
1111 if (os_snprintf_error(sizeof(cmd), res)) {
1112 printf("Too long VENDOR command.\n");
1115 return wpa_ctrl_command(ctrl, cmd);
1119 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1122 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1126 static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1132 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1133 argc >= 1 ? " " : "",
1134 argc >= 1 ? argv[0] : "",
1135 argc == 2 ? " " : "",
1136 argc == 2 ? argv[1] : "");
1137 if (os_snprintf_error(sizeof(cmd), res)) {
1138 printf("Too long option\n");
1141 return wpa_ctrl_command(ctrl, cmd);
1145 static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1149 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1153 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1155 return wpa_ctrl_command(ctrl, "PMKSA");
1159 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1162 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1166 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1172 if (argc < 3 || argc > 5) {
1173 printf("Invalid set_neighbor command: needs 3-5 arguments\n");
1177 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s",
1178 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
1179 argc == 5 ? argv[4] : "");
1180 if (os_snprintf_error(sizeof(cmd), res)) {
1181 printf("Too long SET_NEIGHBOR command.\n");
1184 return wpa_ctrl_command(ctrl, cmd);
1188 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1195 printf("Invalid remove_neighbor command: needs 2 arguments\n");
1199 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
1201 if (os_snprintf_error(sizeof(cmd), res)) {
1202 printf("Too long REMOVE_NEIGHBOR command.\n");
1205 return wpa_ctrl_command(ctrl, cmd);
1209 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1216 printf("Invalid req_lci command - requires destination address\n");
1220 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1221 if (os_snprintf_error(sizeof(cmd), res)) {
1222 printf("Too long REQ_LCI command.\n");
1225 return wpa_ctrl_command(ctrl, cmd);
1229 static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1233 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1237 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1241 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1244 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1248 struct hostapd_cli_cmd {
1250 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
1251 char ** (*completion)(const char *str, int pos);
1255 static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
1256 { "ping", hostapd_cli_cmd_ping, NULL,
1257 "= pings hostapd" },
1258 { "mib", hostapd_cli_cmd_mib, NULL,
1259 "= get MIB variables (dot1x, dot11, radius)" },
1260 { "relog", hostapd_cli_cmd_relog, NULL, NULL },
1261 { "status", hostapd_cli_cmd_status, NULL, NULL },
1262 { "sta", hostapd_cli_cmd_sta, NULL,
1263 "<addr> = get MIB variables for one station" },
1264 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1265 "= get MIB variables for all stations" },
1266 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1267 "<addr> = add a new station" },
1268 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
1269 hostapd_complete_deauthenticate,
1270 "<addr> = deauthenticate a station" },
1271 { "disassociate", hostapd_cli_cmd_disassociate,
1272 hostapd_complete_disassociate,
1273 "<addr> = disassociate a station" },
1274 #ifdef CONFIG_IEEE80211W
1275 { "sa_query", hostapd_cli_cmd_sa_query, NULL,
1276 "<addr> = send SA Query to a station" },
1277 #endif /* CONFIG_IEEE80211W */
1279 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1280 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1281 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1282 "<PIN> = verify PIN checksum" },
1283 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1284 "= indicate button pushed to initiate PBC" },
1285 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1286 "= cancel the pending WPS operation" },
1287 #ifdef CONFIG_WPS_NFC
1288 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1289 "<hexdump> = report read NFC tag with WPS data" },
1290 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1291 "<WPS/NDEF> = build NFC configuration token" },
1292 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1293 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1294 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1296 #endif /* CONFIG_WPS_NFC */
1297 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1298 "<cmd> [params..] = enable/disable AP PIN" },
1299 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1300 "<SSID> <auth> <encr> <key> = configure AP" },
1301 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1302 "= show current WPS status" },
1303 #endif /* CONFIG_WPS */
1304 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, NULL },
1305 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, NULL },
1306 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, NULL },
1307 { "get_config", hostapd_cli_cmd_get_config, NULL,
1308 "= show current configuration" },
1309 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1310 "= show this usage help" },
1311 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1312 "[ifname] = show interfaces/select interface" },
1314 { "fst", hostapd_cli_cmd_fst, NULL, NULL },
1315 #endif /* CONFIG_FST */
1316 { "raw", hostapd_cli_cmd_raw, NULL, NULL },
1317 { "level", hostapd_cli_cmd_level, NULL,
1318 "<debug level> = change debug level" },
1319 { "license", hostapd_cli_cmd_license, NULL,
1320 "= show full hostapd_cli license" },
1321 { "quit", hostapd_cli_cmd_quit, NULL,
1322 "= exit hostapd_cli" },
1323 { "set", hostapd_cli_cmd_set, NULL, NULL },
1324 { "get", hostapd_cli_cmd_get, NULL, NULL },
1325 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, NULL },
1326 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, NULL, NULL },
1327 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, NULL },
1328 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, NULL },
1329 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, NULL },
1330 { "vendor", hostapd_cli_cmd_vendor, NULL, NULL },
1331 { "enable", hostapd_cli_cmd_enable, NULL, NULL },
1332 { "reload", hostapd_cli_cmd_reload, NULL, NULL },
1333 { "disable", hostapd_cli_cmd_disable, NULL, NULL },
1334 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, NULL },
1335 { "log_level", hostapd_cli_cmd_log_level, NULL, NULL },
1336 { "pmksa", hostapd_cli_cmd_pmksa, NULL, NULL },
1337 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, NULL },
1338 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, NULL },
1339 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, NULL },
1340 { "req_lci", hostapd_cli_cmd_req_lci, NULL, NULL },
1341 { "req_range", hostapd_cli_cmd_req_range, NULL, NULL },
1342 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, NULL },
1343 { NULL, NULL, NULL, NULL }
1348 * Prints command usage, lines are padded with the specified string.
1350 static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1356 if (cmd->usage == NULL)
1358 fprintf(stream, "%s%s ", pad, cmd->cmd);
1359 for (n = 0; (c = cmd->usage[n]); n++) {
1360 fprintf(stream, "%c", c);
1362 fprintf(stream, "%s", pad);
1364 fprintf(stream, "\n");
1368 static void print_help(FILE *stream, const char *cmd)
1372 fprintf(stream, "commands:\n");
1373 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1374 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1375 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1380 static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1382 const struct hostapd_cli_cmd *cmd, *match = NULL;
1386 cmd = hostapd_cli_commands;
1388 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1390 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1391 /* we have an exact match */
1401 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1402 cmd = hostapd_cli_commands;
1404 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1406 printf(" %s", cmd->cmd);
1411 } else if (count == 0) {
1412 printf("Unknown command '%s'\n", argv[0]);
1414 match->handler(ctrl, argc - 1, &argv[1]);
1419 static void cli_event(const char *str)
1421 const char *start, *s;
1423 start = os_strchr(str, '>');
1429 if (str_starts(start, AP_STA_CONNECTED)) {
1430 s = os_strchr(start, ' ');
1433 cli_txt_list_add(&stations, s + 1);
1437 if (str_starts(start, AP_STA_DISCONNECTED)) {
1438 s = os_strchr(start, ' ');
1441 cli_txt_list_del_addr(&stations, s + 1);
1447 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1451 if (ctrl_conn == NULL)
1453 while (wpa_ctrl_pending(ctrl)) {
1455 size_t len = sizeof(buf) - 1;
1456 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1459 hostapd_cli_action_process(buf, len);
1462 if (in_read && first)
1465 printf("%s\n", buf);
1468 printf("Could not read pending message.\n");
1475 static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
1477 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
1481 static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
1483 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1484 printf("Connection to hostapd lost - trying to reconnect\n");
1485 hostapd_cli_close_connection();
1488 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1490 printf("Connection to hostapd re-established\n");
1491 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1492 hostapd_cli_attached = 1;
1493 register_event_handler(ctrl_conn);
1495 printf("Warning: Failed to attach to "
1501 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
1502 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1506 static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1512 static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1514 char *argv[max_args];
1516 argc = tokenize_cmd(cmd, argv);
1518 wpa_request(ctrl_conn, argc, argv);
1522 static void hostapd_cli_edit_eof_cb(void *ctx)
1528 static char ** list_cmd_list(void)
1533 count = ARRAY_SIZE(hostapd_cli_commands);
1534 res = os_calloc(count + 1, sizeof(char *));
1538 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1539 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1548 static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1553 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1554 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
1556 if (hostapd_cli_commands[i].completion)
1557 return hostapd_cli_commands[i].completion(str, pos);
1558 if (!hostapd_cli_commands[i].usage)
1561 printf("\r%s\n", hostapd_cli_commands[i].usage);
1570 static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
1577 end = os_strchr(str, ' ');
1578 if (end == NULL || str + pos < end)
1579 return list_cmd_list();
1581 cmd = os_malloc(pos + 1);
1584 os_memcpy(cmd, str, pos);
1585 cmd[end - str] = '\0';
1586 res = hostapd_cli_cmd_completion(cmd, str, pos);
1592 static void hostapd_cli_interactive(void)
1594 printf("\nInteractive mode\n\n");
1596 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
1597 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
1598 hostapd_cli_edit_completion_cb, NULL, NULL, NULL);
1599 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1603 cli_txt_list_flush(&stations);
1604 edit_deinit(NULL, NULL);
1605 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
1609 static void hostapd_cli_cleanup(void)
1611 hostapd_cli_close_connection();
1613 os_daemonize_terminate(pid_file);
1615 os_program_deinit();
1619 static void hostapd_cli_action(struct wpa_ctrl *ctrl)
1627 fd = wpa_ctrl_get_fd(ctrl);
1629 while (!hostapd_cli_quit) {
1632 tv.tv_sec = ping_interval;
1634 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1635 if (res < 0 && errno != EINTR) {
1640 if (FD_ISSET(fd, &rfds))
1641 hostapd_cli_recv_pending(ctrl, 0, 1);
1643 len = sizeof(buf) - 1;
1644 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1645 hostapd_cli_action_process) < 0 ||
1646 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1647 printf("hostapd did not reply to PING "
1648 "command - exiting\n");
1656 int main(int argc, char *argv[])
1658 int warning_displayed = 0;
1662 if (os_program_init())
1666 c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
1671 action_file = optarg;
1677 ping_interval = atoi(optarg);
1683 printf("%s\n", hostapd_cli_version);
1686 os_free(ctrl_ifname);
1687 ctrl_ifname = os_strdup(optarg);
1690 ctrl_iface_dir = optarg;
1696 client_socket_dir = optarg;
1704 interactive = (argc == optind) && (action_file == NULL);
1707 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
1714 if (ctrl_ifname == NULL) {
1715 struct dirent *dent;
1716 DIR *dir = opendir(ctrl_iface_dir);
1718 while ((dent = readdir(dir))) {
1719 if (os_strcmp(dent->d_name, ".") == 0
1721 os_strcmp(dent->d_name, "..") == 0)
1723 printf("Selected interface '%s'\n",
1725 ctrl_ifname = os_strdup(dent->d_name);
1731 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1733 if (warning_displayed)
1734 printf("Connection established.\n");
1739 perror("Failed to connect to hostapd - "
1744 if (!warning_displayed) {
1745 printf("Could not connect to hostapd - re-trying\n");
1746 warning_displayed = 1;
1752 if (interactive || action_file) {
1753 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1754 hostapd_cli_attached = 1;
1755 register_event_handler(ctrl_conn);
1757 printf("Warning: Failed to attach to hostapd.\n");
1763 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
1767 hostapd_cli_interactive();
1768 else if (action_file)
1769 hostapd_cli_action(ctrl_conn);
1771 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1773 unregister_event_handler(ctrl_conn);
1774 os_free(ctrl_ifname);
1776 hostapd_cli_cleanup();
1780 #else /* CONFIG_NO_CTRL_IFACE */
1782 int main(int argc, char *argv[])
1787 #endif /* CONFIG_NO_CTRL_IFACE */