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 int get_cmd_arg_num(const char *str, int pos)
81 for (i = 0; i <= pos; i++) {
84 while (i <= pos && str[i] != ' ')
95 static int str_starts(const char *src, const char *match)
97 return os_strncmp(src, match, os_strlen(match)) == 0;
101 static void register_event_handler(struct wpa_ctrl *ctrl)
106 event_handler_registered =
107 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
114 static void unregister_event_handler(struct wpa_ctrl *ctrl)
118 if (interactive && event_handler_registered) {
119 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
120 event_handler_registered = 0;
125 static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
127 #ifndef CONFIG_CTRL_IFACE_UDP
130 #endif /* !CONFIG_CTRL_IFACE_UDP */
135 #ifdef CONFIG_CTRL_IFACE_UDP
136 ctrl_conn = wpa_ctrl_open(ifname);
138 #else /* CONFIG_CTRL_IFACE_UDP */
139 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
140 cfile = malloc(flen);
143 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
145 if (client_socket_dir && client_socket_dir[0] &&
146 access(client_socket_dir, F_OK) < 0) {
147 perror(client_socket_dir);
152 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
155 #endif /* CONFIG_CTRL_IFACE_UDP */
159 static void hostapd_cli_close_connection(void)
161 if (ctrl_conn == NULL)
164 unregister_event_handler(ctrl_conn);
165 if (hostapd_cli_attached) {
166 wpa_ctrl_detach(ctrl_conn);
167 hostapd_cli_attached = 0;
169 wpa_ctrl_close(ctrl_conn);
174 static void hostapd_cli_msg_cb(char *msg, size_t len)
180 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
186 if (ctrl_conn == NULL) {
187 printf("Not connected to hostapd - command dropped.\n");
190 len = sizeof(buf) - 1;
191 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
194 printf("'%s' command timed out.\n", cmd);
196 } else if (ret < 0) {
197 printf("'%s' command failed.\n", cmd);
208 static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
210 return _wpa_ctrl_command(ctrl, cmd, 1);
214 static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc,
223 res = os_snprintf(pos, end - pos, "%s", cmd);
224 if (os_snprintf_error(end - pos, res))
228 for (i = 0; i < argc; i++) {
229 res = os_snprintf(pos, end - pos, " %s", argv[i]);
230 if (os_snprintf_error(end - pos, res))
235 buf[buflen - 1] = '\0';
239 printf("Too long command\n");
244 static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
245 int min_args, int argc, char *argv[])
249 if (argc < min_args) {
250 printf("Invalid %s command - at least %d argument%s required.\n",
251 cmd, min_args, min_args > 1 ? "s are" : " is");
254 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
256 return wpa_ctrl_command(ctrl, buf);
260 static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
262 return wpa_ctrl_command(ctrl, "PING");
266 static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
268 return wpa_ctrl_command(ctrl, "RELOG");
272 static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
274 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
275 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
276 return wpa_ctrl_command(ctrl, "STATUS");
280 static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
284 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
285 return wpa_ctrl_command(ctrl, buf);
287 return wpa_ctrl_command(ctrl, "MIB");
291 static int hostapd_cli_exec(const char *program, const char *arg1,
298 len = os_strlen(arg1) + os_strlen(arg2) + 2;
299 arg = os_malloc(len);
302 os_snprintf(arg, len, "%s %s", arg1, arg2);
303 res = os_exec(program, arg, 1);
310 static void hostapd_cli_action_process(char *msg, size_t len)
316 pos = os_strchr(pos, '>');
323 hostapd_cli_exec(action_file, ctrl_ifname, pos);
327 static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
331 printf("Invalid 'sta' command - at least one argument, STA "
332 "address, is required.\n");
336 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
338 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
339 return wpa_ctrl_command(ctrl, buf);
343 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
348 printf("Invalid 'new_sta' command - exactly one argument, STA "
349 "address, is required.\n");
352 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
353 return wpa_ctrl_command(ctrl, buf);
357 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
362 printf("Invalid 'deauthenticate' command - exactly one "
363 "argument, STA address, is required.\n");
367 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
370 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
371 return wpa_ctrl_command(ctrl, buf);
375 static char ** hostapd_complete_deauthenticate(const char *str, int pos)
377 int arg = get_cmd_arg_num(str, pos);
382 res = cli_txt_list_array(&stations);
390 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
395 printf("Invalid 'disassociate' command - exactly one "
396 "argument, STA address, is required.\n");
400 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
403 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
404 return wpa_ctrl_command(ctrl, buf);
408 static char ** hostapd_complete_disassociate(const char *str, int pos)
410 int arg = get_cmd_arg_num(str, pos);
415 res = cli_txt_list_array(&stations);
423 #ifdef CONFIG_IEEE80211W
424 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
429 printf("Invalid 'sa_query' command - exactly one argument, "
430 "STA address, is required.\n");
433 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
434 return wpa_ctrl_command(ctrl, buf);
436 #endif /* CONFIG_IEEE80211W */
440 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
445 printf("Invalid 'wps_pin' command - at least two arguments, "
446 "UUID and PIN, are required.\n");
450 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
451 argv[0], argv[1], argv[2], argv[3]);
453 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
454 argv[0], argv[1], argv[2]);
456 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
457 return wpa_ctrl_command(ctrl, buf);
461 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
467 if (argc != 1 && argc != 2) {
468 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
469 "- PIN to be verified\n");
474 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
477 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
479 if (os_snprintf_error(sizeof(cmd), res)) {
480 printf("Too long WPS_CHECK_PIN command.\n");
483 return wpa_ctrl_command(ctrl, cmd);
487 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
490 return wpa_ctrl_command(ctrl, "WPS_PBC");
494 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
497 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
501 #ifdef CONFIG_WPS_NFC
502 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
510 printf("Invalid 'wps_nfc_tag_read' command - one argument "
515 buflen = 18 + os_strlen(argv[0]);
516 buf = os_malloc(buflen);
519 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
521 ret = wpa_ctrl_command(ctrl, buf);
528 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
529 int argc, char *argv[])
535 printf("Invalid 'wps_nfc_config_token' command - one argument "
540 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
542 if (os_snprintf_error(sizeof(cmd), res)) {
543 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
546 return wpa_ctrl_command(ctrl, cmd);
550 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
551 int argc, char *argv[])
557 printf("Invalid 'wps_nfc_token' command - one argument is "
562 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
563 if (os_snprintf_error(sizeof(cmd), res)) {
564 printf("Too long WPS_NFC_TOKEN command.\n");
567 return wpa_ctrl_command(ctrl, cmd);
571 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
572 int argc, char *argv[])
578 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
583 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
585 if (os_snprintf_error(sizeof(cmd), res)) {
586 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
589 return wpa_ctrl_command(ctrl, cmd);
592 #endif /* CONFIG_WPS_NFC */
595 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
600 printf("Invalid 'wps_ap_pin' command - at least one argument "
605 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
606 argv[0], argv[1], argv[2]);
608 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
611 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
612 return wpa_ctrl_command(ctrl, buf);
616 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
619 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
623 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
627 char ssid_hex[2 * SSID_MAX_LEN + 1];
628 char key_hex[2 * 64 + 1];
632 printf("Invalid 'wps_config' command - at least two arguments "
638 for (i = 0; i < SSID_MAX_LEN; i++) {
639 if (argv[0][i] == '\0')
641 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
646 for (i = 0; i < 64; i++) {
647 if (argv[3][i] == '\0')
649 os_snprintf(&key_hex[i * 2], 3, "%02x",
655 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
656 ssid_hex, argv[1], argv[2], key_hex);
658 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
659 ssid_hex, argv[1], argv[2]);
661 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
663 return wpa_ctrl_command(ctrl, buf);
665 #endif /* CONFIG_WPS */
668 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
675 printf("Invalid 'disassoc_imminent' command - two arguments "
676 "(STA addr and Disassociation Timer) are needed\n");
680 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
682 if (os_snprintf_error(sizeof(buf), res))
684 return wpa_ctrl_command(ctrl, buf);
688 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
695 printf("Invalid 'ess_disassoc' command - three arguments (STA "
696 "addr, disassoc timer, and URL) are needed\n");
700 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
701 argv[0], argv[1], argv[2]);
702 if (os_snprintf_error(sizeof(buf), res))
704 return wpa_ctrl_command(ctrl, buf);
708 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
711 char buf[2000], *tmp;
715 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
719 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
720 if (os_snprintf_error(sizeof(buf), res))
724 for (i = 1; i < argc; i++) {
726 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
727 if (os_snprintf_error(sizeof(buf) - total, res))
731 return wpa_ctrl_command(ctrl, buf);
735 static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
738 return wpa_ctrl_command(ctrl, "GET_CONFIG");
742 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
743 char *addr, size_t addr_len)
745 char buf[4096], *pos;
749 if (ctrl_conn == NULL) {
750 printf("Not connected to hostapd - command dropped.\n");
753 len = sizeof(buf) - 1;
754 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
757 printf("'%s' command timed out.\n", cmd);
759 } else if (ret < 0) {
760 printf("'%s' command failed.\n", cmd);
765 if (memcmp(buf, "FAIL", 4) == 0)
770 while (*pos != '\0' && *pos != '\n')
773 os_strlcpy(addr, buf, addr_len);
778 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
781 char addr[32], cmd[64];
783 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
786 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
787 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
793 static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
795 print_help(stdout, argc > 0 ? argv[0] : NULL);
800 static char ** hostapd_cli_complete_help(const char *str, int pos)
802 int arg = get_cmd_arg_num(str, pos);
807 res = list_cmd_list();
815 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
818 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
823 static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
824 int argc, char *argv[])
830 printf("Invalid 'set_qos_map_set' command - "
831 "one argument (comma delimited QoS map set) "
836 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
837 if (os_snprintf_error(sizeof(buf), res))
839 return wpa_ctrl_command(ctrl, buf);
843 static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
844 int argc, char *argv[])
850 printf("Invalid 'send_qos_map_conf' command - "
851 "one argument (STA addr) is needed\n");
855 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
856 if (os_snprintf_error(sizeof(buf), res))
858 return wpa_ctrl_command(ctrl, buf);
862 static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
869 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
870 "addr and URL) are needed\n");
874 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
876 if (os_snprintf_error(sizeof(buf), res))
878 return wpa_ctrl_command(ctrl, buf);
882 static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
889 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
894 res = os_snprintf(buf, sizeof(buf),
895 "HS20_DEAUTH_REQ %s %s %s %s",
896 argv[0], argv[1], argv[2], argv[3]);
898 res = os_snprintf(buf, sizeof(buf),
899 "HS20_DEAUTH_REQ %s %s %s",
900 argv[0], argv[1], argv[2]);
901 if (os_snprintf_error(sizeof(buf), res))
903 return wpa_ctrl_command(ctrl, buf);
907 static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
909 hostapd_cli_quit = 1;
916 static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
920 printf("Invalid LEVEL command: needs one argument (debug "
924 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
925 return wpa_ctrl_command(ctrl, cmd);
929 static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
930 struct dl_list *interfaces)
935 if (!ctrl || !interfaces)
937 dir = opendir(ctrl_iface_dir);
941 while ((dent = readdir(dir))) {
942 if (strcmp(dent->d_name, ".") == 0 ||
943 strcmp(dent->d_name, "..") == 0)
945 cli_txt_list_add(interfaces, dent->d_name);
951 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
956 dir = opendir(ctrl_iface_dir);
958 printf("Control interface directory '%s' could not be "
959 "openned.\n", ctrl_iface_dir);
963 printf("Available interfaces:\n");
964 while ((dent = readdir(dir))) {
965 if (strcmp(dent->d_name, ".") == 0 ||
966 strcmp(dent->d_name, "..") == 0)
968 printf("%s\n", dent->d_name);
974 static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
978 hostapd_cli_list_interfaces(ctrl);
982 hostapd_cli_close_connection();
983 os_free(ctrl_ifname);
984 ctrl_ifname = os_strdup(argv[0]);
985 if (ctrl_ifname == NULL)
988 if (hostapd_cli_open_connection(ctrl_ifname)) {
989 printf("Connected to interface '%s.\n", ctrl_ifname);
990 if (wpa_ctrl_attach(ctrl_conn) == 0) {
991 hostapd_cli_attached = 1;
992 register_event_handler(ctrl_conn);
994 printf("Warning: Failed to attach to "
998 printf("Could not connect to interface '%s' - re-trying\n",
1005 static char ** hostapd_complete_interface(const char *str, int pos)
1007 int arg = get_cmd_arg_num(str, pos);
1009 DEFINE_DL_LIST(interfaces);
1013 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1014 res = cli_txt_list_array(&interfaces);
1015 cli_txt_list_flush(&interfaces);
1023 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1029 printf("Invalid SET command: needs two arguments (variable "
1030 "name and value)\n");
1034 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
1035 if (os_snprintf_error(sizeof(cmd), res)) {
1036 printf("Too long SET command.\n");
1039 return wpa_ctrl_command(ctrl, cmd);
1043 static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1049 printf("Invalid GET command: needs one argument (variable "
1054 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
1055 if (os_snprintf_error(sizeof(cmd), res)) {
1056 printf("Too long GET command.\n");
1059 return wpa_ctrl_command(ctrl, cmd);
1064 static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1072 printf("FST command: parameters are required.\n");
1076 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1078 for (i = 0; i < argc; i++) {
1079 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1081 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1082 printf("Too long fst command.\n");
1087 return wpa_ctrl_command(ctrl, cmd);
1089 #endif /* CONFIG_FST */
1092 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1093 int argc, char *argv[])
1102 printf("Invalid chan_switch command: needs at least two "
1103 "arguments (count and freq)\n"
1104 "usage: <cs_count> <freq> [sec_channel_offset=] "
1105 "[center_freq1=] [center_freq2=] [bandwidth=] "
1106 "[blocktx] [ht|vht]\n");
1110 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1112 if (os_snprintf_error(sizeof(cmd), res)) {
1113 printf("Too long CHAN_SWITCH command.\n");
1118 for (i = 2; i < argc; i++) {
1120 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
1121 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1122 printf("Too long CHAN_SWITCH command.\n");
1127 return wpa_ctrl_command(ctrl, cmd);
1131 static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1134 return wpa_ctrl_command(ctrl, "ENABLE");
1138 static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1141 return wpa_ctrl_command(ctrl, "RELOAD");
1145 static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1148 return wpa_ctrl_command(ctrl, "DISABLE");
1152 static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1157 if (argc < 2 || argc > 3) {
1158 printf("Invalid vendor command\n"
1159 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1163 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
1164 argc == 3 ? argv[2] : "");
1165 if (os_snprintf_error(sizeof(cmd), res)) {
1166 printf("Too long VENDOR command.\n");
1169 return wpa_ctrl_command(ctrl, cmd);
1173 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1176 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1180 static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1186 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1187 argc >= 1 ? " " : "",
1188 argc >= 1 ? argv[0] : "",
1189 argc == 2 ? " " : "",
1190 argc == 2 ? argv[1] : "");
1191 if (os_snprintf_error(sizeof(cmd), res)) {
1192 printf("Too long option\n");
1195 return wpa_ctrl_command(ctrl, cmd);
1199 static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1203 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1207 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1209 return wpa_ctrl_command(ctrl, "PMKSA");
1213 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1216 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1220 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1226 if (argc < 3 || argc > 5) {
1227 printf("Invalid set_neighbor command: needs 3-5 arguments\n");
1231 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s",
1232 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
1233 argc == 5 ? argv[4] : "");
1234 if (os_snprintf_error(sizeof(cmd), res)) {
1235 printf("Too long SET_NEIGHBOR command.\n");
1238 return wpa_ctrl_command(ctrl, cmd);
1242 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1249 printf("Invalid remove_neighbor command: needs 2 arguments\n");
1253 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
1255 if (os_snprintf_error(sizeof(cmd), res)) {
1256 printf("Too long REMOVE_NEIGHBOR command.\n");
1259 return wpa_ctrl_command(ctrl, cmd);
1263 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1270 printf("Invalid req_lci command - requires destination address\n");
1274 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1275 if (os_snprintf_error(sizeof(cmd), res)) {
1276 printf("Too long REQ_LCI command.\n");
1279 return wpa_ctrl_command(ctrl, cmd);
1283 static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1287 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1291 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1295 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1298 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1302 struct hostapd_cli_cmd {
1304 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
1305 char ** (*completion)(const char *str, int pos);
1309 static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
1310 { "ping", hostapd_cli_cmd_ping, NULL,
1311 "= pings hostapd" },
1312 { "mib", hostapd_cli_cmd_mib, NULL,
1313 "= get MIB variables (dot1x, dot11, radius)" },
1314 { "relog", hostapd_cli_cmd_relog, NULL, NULL },
1315 { "status", hostapd_cli_cmd_status, NULL, NULL },
1316 { "sta", hostapd_cli_cmd_sta, NULL,
1317 "<addr> = get MIB variables for one station" },
1318 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1319 "= get MIB variables for all stations" },
1320 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1321 "<addr> = add a new station" },
1322 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
1323 hostapd_complete_deauthenticate,
1324 "<addr> = deauthenticate a station" },
1325 { "disassociate", hostapd_cli_cmd_disassociate,
1326 hostapd_complete_disassociate,
1327 "<addr> = disassociate a station" },
1328 #ifdef CONFIG_IEEE80211W
1329 { "sa_query", hostapd_cli_cmd_sa_query, NULL,
1330 "<addr> = send SA Query to a station" },
1331 #endif /* CONFIG_IEEE80211W */
1333 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1334 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1335 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1336 "<PIN> = verify PIN checksum" },
1337 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1338 "= indicate button pushed to initiate PBC" },
1339 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1340 "= cancel the pending WPS operation" },
1341 #ifdef CONFIG_WPS_NFC
1342 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1343 "<hexdump> = report read NFC tag with WPS data" },
1344 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1345 "<WPS/NDEF> = build NFC configuration token" },
1346 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1347 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1348 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1350 #endif /* CONFIG_WPS_NFC */
1351 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1352 "<cmd> [params..] = enable/disable AP PIN" },
1353 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1354 "<SSID> <auth> <encr> <key> = configure AP" },
1355 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1356 "= show current WPS status" },
1357 #endif /* CONFIG_WPS */
1358 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, NULL },
1359 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, NULL },
1360 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, NULL },
1361 { "get_config", hostapd_cli_cmd_get_config, NULL,
1362 "= show current configuration" },
1363 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1364 "= show this usage help" },
1365 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1366 "[ifname] = show interfaces/select interface" },
1368 { "fst", hostapd_cli_cmd_fst, NULL, NULL },
1369 #endif /* CONFIG_FST */
1370 { "raw", hostapd_cli_cmd_raw, NULL, NULL },
1371 { "level", hostapd_cli_cmd_level, NULL,
1372 "<debug level> = change debug level" },
1373 { "license", hostapd_cli_cmd_license, NULL,
1374 "= show full hostapd_cli license" },
1375 { "quit", hostapd_cli_cmd_quit, NULL,
1376 "= exit hostapd_cli" },
1377 { "set", hostapd_cli_cmd_set, NULL, NULL },
1378 { "get", hostapd_cli_cmd_get, NULL, NULL },
1379 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, NULL },
1380 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, NULL, NULL },
1381 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, NULL },
1382 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, NULL },
1383 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, NULL },
1384 { "vendor", hostapd_cli_cmd_vendor, NULL, NULL },
1385 { "enable", hostapd_cli_cmd_enable, NULL, NULL },
1386 { "reload", hostapd_cli_cmd_reload, NULL, NULL },
1387 { "disable", hostapd_cli_cmd_disable, NULL, NULL },
1388 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, NULL },
1389 { "log_level", hostapd_cli_cmd_log_level, NULL, NULL },
1390 { "pmksa", hostapd_cli_cmd_pmksa, NULL, NULL },
1391 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, NULL },
1392 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, NULL },
1393 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, NULL },
1394 { "req_lci", hostapd_cli_cmd_req_lci, NULL, NULL },
1395 { "req_range", hostapd_cli_cmd_req_range, NULL, NULL },
1396 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, NULL },
1397 { NULL, NULL, NULL, NULL }
1402 * Prints command usage, lines are padded with the specified string.
1404 static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1410 if (cmd->usage == NULL)
1412 fprintf(stream, "%s%s ", pad, cmd->cmd);
1413 for (n = 0; (c = cmd->usage[n]); n++) {
1414 fprintf(stream, "%c", c);
1416 fprintf(stream, "%s", pad);
1418 fprintf(stream, "\n");
1422 static void print_help(FILE *stream, const char *cmd)
1426 fprintf(stream, "commands:\n");
1427 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1428 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1429 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1434 static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1436 const struct hostapd_cli_cmd *cmd, *match = NULL;
1440 cmd = hostapd_cli_commands;
1442 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1444 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1445 /* we have an exact match */
1455 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1456 cmd = hostapd_cli_commands;
1458 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1460 printf(" %s", cmd->cmd);
1465 } else if (count == 0) {
1466 printf("Unknown command '%s'\n", argv[0]);
1468 match->handler(ctrl, argc - 1, &argv[1]);
1473 static void cli_event(const char *str)
1475 const char *start, *s;
1477 start = os_strchr(str, '>');
1483 if (str_starts(start, AP_STA_CONNECTED)) {
1484 s = os_strchr(start, ' ');
1487 cli_txt_list_add(&stations, s + 1);
1491 if (str_starts(start, AP_STA_DISCONNECTED)) {
1492 s = os_strchr(start, ' ');
1495 cli_txt_list_del_addr(&stations, s + 1);
1501 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1505 if (ctrl_conn == NULL)
1507 while (wpa_ctrl_pending(ctrl)) {
1509 size_t len = sizeof(buf) - 1;
1510 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1513 hostapd_cli_action_process(buf, len);
1516 if (in_read && first)
1519 printf("%s\n", buf);
1522 printf("Could not read pending message.\n");
1529 static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
1531 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
1537 static int tokenize_cmd(char *cmd, char *argv[])
1550 if (argc == max_args)
1553 char *pos2 = os_strrchr(pos, '"');
1557 while (*pos != '\0' && *pos != ' ')
1567 static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
1569 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1570 printf("Connection to hostapd lost - trying to reconnect\n");
1571 hostapd_cli_close_connection();
1574 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1576 printf("Connection to hostapd re-established\n");
1577 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1578 hostapd_cli_attached = 1;
1579 register_event_handler(ctrl_conn);
1581 printf("Warning: Failed to attach to "
1587 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
1588 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1592 static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1598 static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1600 char *argv[max_args];
1602 argc = tokenize_cmd(cmd, argv);
1604 wpa_request(ctrl_conn, argc, argv);
1608 static void hostapd_cli_edit_eof_cb(void *ctx)
1614 static char ** list_cmd_list(void)
1619 count = ARRAY_SIZE(hostapd_cli_commands);
1620 res = os_calloc(count + 1, sizeof(char *));
1624 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1625 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1634 static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1639 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1640 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
1642 if (hostapd_cli_commands[i].completion)
1643 return hostapd_cli_commands[i].completion(str, pos);
1644 if (!hostapd_cli_commands[i].usage)
1647 printf("\r%s\n", hostapd_cli_commands[i].usage);
1656 static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
1663 end = os_strchr(str, ' ');
1664 if (end == NULL || str + pos < end)
1665 return list_cmd_list();
1667 cmd = os_malloc(pos + 1);
1670 os_memcpy(cmd, str, pos);
1671 cmd[end - str] = '\0';
1672 res = hostapd_cli_cmd_completion(cmd, str, pos);
1678 static void hostapd_cli_interactive(void)
1680 printf("\nInteractive mode\n\n");
1682 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
1683 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
1684 hostapd_cli_edit_completion_cb, NULL, NULL, NULL);
1685 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1689 cli_txt_list_flush(&stations);
1690 edit_deinit(NULL, NULL);
1691 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
1695 static void hostapd_cli_cleanup(void)
1697 hostapd_cli_close_connection();
1699 os_daemonize_terminate(pid_file);
1701 os_program_deinit();
1705 static void hostapd_cli_action(struct wpa_ctrl *ctrl)
1713 fd = wpa_ctrl_get_fd(ctrl);
1715 while (!hostapd_cli_quit) {
1718 tv.tv_sec = ping_interval;
1720 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1721 if (res < 0 && errno != EINTR) {
1726 if (FD_ISSET(fd, &rfds))
1727 hostapd_cli_recv_pending(ctrl, 0, 1);
1729 len = sizeof(buf) - 1;
1730 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1731 hostapd_cli_action_process) < 0 ||
1732 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1733 printf("hostapd did not reply to PING "
1734 "command - exiting\n");
1742 int main(int argc, char *argv[])
1744 int warning_displayed = 0;
1748 if (os_program_init())
1752 c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
1757 action_file = optarg;
1763 ping_interval = atoi(optarg);
1769 printf("%s\n", hostapd_cli_version);
1772 os_free(ctrl_ifname);
1773 ctrl_ifname = os_strdup(optarg);
1776 ctrl_iface_dir = optarg;
1782 client_socket_dir = optarg;
1790 interactive = (argc == optind) && (action_file == NULL);
1793 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
1800 if (ctrl_ifname == NULL) {
1801 struct dirent *dent;
1802 DIR *dir = opendir(ctrl_iface_dir);
1804 while ((dent = readdir(dir))) {
1805 if (os_strcmp(dent->d_name, ".") == 0
1807 os_strcmp(dent->d_name, "..") == 0)
1809 printf("Selected interface '%s'\n",
1811 ctrl_ifname = os_strdup(dent->d_name);
1817 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1819 if (warning_displayed)
1820 printf("Connection established.\n");
1825 perror("Failed to connect to hostapd - "
1830 if (!warning_displayed) {
1831 printf("Could not connect to hostapd - re-trying\n");
1832 warning_displayed = 1;
1838 if (interactive || action_file) {
1839 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1840 hostapd_cli_attached = 1;
1841 register_event_handler(ctrl_conn);
1843 printf("Warning: Failed to attach to hostapd.\n");
1849 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
1853 hostapd_cli_interactive();
1854 else if (action_file)
1855 hostapd_cli_action(ctrl_conn);
1857 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1859 unregister_event_handler(ctrl_conn);
1860 os_free(ctrl_ifname);
1862 hostapd_cli_cleanup();
1866 #else /* CONFIG_NO_CTRL_IFACE */
1868 int main(int argc, char *argv[])
1873 #endif /* CONFIG_NO_CTRL_IFACE */