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 void register_event_handler(struct wpa_ctrl *ctrl)
100 event_handler_registered =
101 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
108 static void unregister_event_handler(struct wpa_ctrl *ctrl)
112 if (interactive && event_handler_registered) {
113 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
114 event_handler_registered = 0;
119 static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
121 #ifndef CONFIG_CTRL_IFACE_UDP
124 #endif /* !CONFIG_CTRL_IFACE_UDP */
129 #ifdef CONFIG_CTRL_IFACE_UDP
130 ctrl_conn = wpa_ctrl_open(ifname);
132 #else /* CONFIG_CTRL_IFACE_UDP */
133 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
134 cfile = malloc(flen);
137 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
139 if (client_socket_dir && client_socket_dir[0] &&
140 access(client_socket_dir, F_OK) < 0) {
141 perror(client_socket_dir);
146 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
149 #endif /* CONFIG_CTRL_IFACE_UDP */
153 static void hostapd_cli_close_connection(void)
155 if (ctrl_conn == NULL)
158 unregister_event_handler(ctrl_conn);
159 if (hostapd_cli_attached) {
160 wpa_ctrl_detach(ctrl_conn);
161 hostapd_cli_attached = 0;
163 wpa_ctrl_close(ctrl_conn);
168 static void hostapd_cli_msg_cb(char *msg, size_t len)
174 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
180 if (ctrl_conn == NULL) {
181 printf("Not connected to hostapd - command dropped.\n");
184 len = sizeof(buf) - 1;
185 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
188 printf("'%s' command timed out.\n", cmd);
190 } else if (ret < 0) {
191 printf("'%s' command failed.\n", cmd);
202 static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
204 return _wpa_ctrl_command(ctrl, cmd, 1);
208 static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc,
217 res = os_snprintf(pos, end - pos, "%s", cmd);
218 if (os_snprintf_error(end - pos, res))
222 for (i = 0; i < argc; i++) {
223 res = os_snprintf(pos, end - pos, " %s", argv[i]);
224 if (os_snprintf_error(end - pos, res))
229 buf[buflen - 1] = '\0';
233 printf("Too long command\n");
238 static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
239 int min_args, int argc, char *argv[])
243 if (argc < min_args) {
244 printf("Invalid %s command - at least %d argument%s required.\n",
245 cmd, min_args, min_args > 1 ? "s are" : " is");
248 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
250 return wpa_ctrl_command(ctrl, buf);
254 static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
256 return wpa_ctrl_command(ctrl, "PING");
260 static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
262 return wpa_ctrl_command(ctrl, "RELOG");
266 static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
268 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
269 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
270 return wpa_ctrl_command(ctrl, "STATUS");
274 static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
278 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
279 return wpa_ctrl_command(ctrl, buf);
281 return wpa_ctrl_command(ctrl, "MIB");
285 static int hostapd_cli_exec(const char *program, const char *arg1,
292 len = os_strlen(arg1) + os_strlen(arg2) + 2;
293 arg = os_malloc(len);
296 os_snprintf(arg, len, "%s %s", arg1, arg2);
297 res = os_exec(program, arg, 1);
304 static void hostapd_cli_action_process(char *msg, size_t len)
310 pos = os_strchr(pos, '>');
317 hostapd_cli_exec(action_file, ctrl_ifname, pos);
321 static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
325 printf("Invalid 'sta' command - at least one argument, STA "
326 "address, is required.\n");
330 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
332 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
333 return wpa_ctrl_command(ctrl, buf);
337 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
342 printf("Invalid 'new_sta' command - exactly one argument, STA "
343 "address, is required.\n");
346 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
347 return wpa_ctrl_command(ctrl, buf);
351 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
356 printf("Invalid 'deauthenticate' command - exactly one "
357 "argument, STA address, is required.\n");
361 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
364 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
365 return wpa_ctrl_command(ctrl, buf);
369 static char ** hostapd_complete_deauthenticate(const char *str, int pos)
371 int arg = get_cmd_arg_num(str, pos);
376 res = cli_txt_list_array(&stations);
384 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
389 printf("Invalid 'disassociate' command - exactly one "
390 "argument, STA address, is required.\n");
394 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
397 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
398 return wpa_ctrl_command(ctrl, buf);
402 static char ** hostapd_complete_disassociate(const char *str, int pos)
404 int arg = get_cmd_arg_num(str, pos);
409 res = cli_txt_list_array(&stations);
417 #ifdef CONFIG_IEEE80211W
418 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
423 printf("Invalid 'sa_query' command - exactly one argument, "
424 "STA address, is required.\n");
427 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
428 return wpa_ctrl_command(ctrl, buf);
430 #endif /* CONFIG_IEEE80211W */
434 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
439 printf("Invalid 'wps_pin' command - at least two arguments, "
440 "UUID and PIN, are required.\n");
444 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
445 argv[0], argv[1], argv[2], argv[3]);
447 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
448 argv[0], argv[1], argv[2]);
450 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
451 return wpa_ctrl_command(ctrl, buf);
455 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
461 if (argc != 1 && argc != 2) {
462 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
463 "- PIN to be verified\n");
468 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
471 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
473 if (os_snprintf_error(sizeof(cmd), res)) {
474 printf("Too long WPS_CHECK_PIN command.\n");
477 return wpa_ctrl_command(ctrl, cmd);
481 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
484 return wpa_ctrl_command(ctrl, "WPS_PBC");
488 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
491 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
495 #ifdef CONFIG_WPS_NFC
496 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
504 printf("Invalid 'wps_nfc_tag_read' command - one argument "
509 buflen = 18 + os_strlen(argv[0]);
510 buf = os_malloc(buflen);
513 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
515 ret = wpa_ctrl_command(ctrl, buf);
522 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
523 int argc, char *argv[])
529 printf("Invalid 'wps_nfc_config_token' command - one argument "
534 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
536 if (os_snprintf_error(sizeof(cmd), res)) {
537 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
540 return wpa_ctrl_command(ctrl, cmd);
544 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
545 int argc, char *argv[])
551 printf("Invalid 'wps_nfc_token' command - one argument is "
556 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
557 if (os_snprintf_error(sizeof(cmd), res)) {
558 printf("Too long WPS_NFC_TOKEN command.\n");
561 return wpa_ctrl_command(ctrl, cmd);
565 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
566 int argc, char *argv[])
572 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
577 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
579 if (os_snprintf_error(sizeof(cmd), res)) {
580 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
583 return wpa_ctrl_command(ctrl, cmd);
586 #endif /* CONFIG_WPS_NFC */
589 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
594 printf("Invalid 'wps_ap_pin' command - at least one argument "
599 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
600 argv[0], argv[1], argv[2]);
602 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
605 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
606 return wpa_ctrl_command(ctrl, buf);
610 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
613 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
617 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
621 char ssid_hex[2 * SSID_MAX_LEN + 1];
622 char key_hex[2 * 64 + 1];
626 printf("Invalid 'wps_config' command - at least two arguments "
632 for (i = 0; i < SSID_MAX_LEN; i++) {
633 if (argv[0][i] == '\0')
635 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
640 for (i = 0; i < 64; i++) {
641 if (argv[3][i] == '\0')
643 os_snprintf(&key_hex[i * 2], 3, "%02x",
649 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
650 ssid_hex, argv[1], argv[2], key_hex);
652 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
653 ssid_hex, argv[1], argv[2]);
655 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
657 return wpa_ctrl_command(ctrl, buf);
659 #endif /* CONFIG_WPS */
662 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
669 printf("Invalid 'disassoc_imminent' command - two arguments "
670 "(STA addr and Disassociation Timer) are needed\n");
674 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
676 if (os_snprintf_error(sizeof(buf), res))
678 return wpa_ctrl_command(ctrl, buf);
682 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
689 printf("Invalid 'ess_disassoc' command - three arguments (STA "
690 "addr, disassoc timer, and URL) are needed\n");
694 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
695 argv[0], argv[1], argv[2]);
696 if (os_snprintf_error(sizeof(buf), res))
698 return wpa_ctrl_command(ctrl, buf);
702 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
705 char buf[2000], *tmp;
709 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
713 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
714 if (os_snprintf_error(sizeof(buf), res))
718 for (i = 1; i < argc; i++) {
720 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
721 if (os_snprintf_error(sizeof(buf) - total, res))
725 return wpa_ctrl_command(ctrl, buf);
729 static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
732 return wpa_ctrl_command(ctrl, "GET_CONFIG");
736 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
737 char *addr, size_t addr_len)
739 char buf[4096], *pos;
743 if (ctrl_conn == NULL) {
744 printf("Not connected to hostapd - command dropped.\n");
747 len = sizeof(buf) - 1;
748 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
751 printf("'%s' command timed out.\n", cmd);
753 } else if (ret < 0) {
754 printf("'%s' command failed.\n", cmd);
759 if (memcmp(buf, "FAIL", 4) == 0)
764 while (*pos != '\0' && *pos != '\n')
767 os_strlcpy(addr, buf, addr_len);
772 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
775 char addr[32], cmd[64];
777 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
780 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
781 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
787 static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
789 print_help(stdout, argc > 0 ? argv[0] : NULL);
794 static char ** hostapd_cli_complete_help(const char *str, int pos)
796 int arg = get_cmd_arg_num(str, pos);
801 res = list_cmd_list();
809 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
812 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
817 static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
818 int argc, char *argv[])
824 printf("Invalid 'set_qos_map_set' command - "
825 "one argument (comma delimited QoS map set) "
830 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
831 if (os_snprintf_error(sizeof(buf), res))
833 return wpa_ctrl_command(ctrl, buf);
837 static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
838 int argc, char *argv[])
844 printf("Invalid 'send_qos_map_conf' command - "
845 "one argument (STA addr) is needed\n");
849 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
850 if (os_snprintf_error(sizeof(buf), res))
852 return wpa_ctrl_command(ctrl, buf);
856 static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
863 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
864 "addr and URL) are needed\n");
868 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
870 if (os_snprintf_error(sizeof(buf), res))
872 return wpa_ctrl_command(ctrl, buf);
876 static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
883 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
888 res = os_snprintf(buf, sizeof(buf),
889 "HS20_DEAUTH_REQ %s %s %s %s",
890 argv[0], argv[1], argv[2], argv[3]);
892 res = os_snprintf(buf, sizeof(buf),
893 "HS20_DEAUTH_REQ %s %s %s",
894 argv[0], argv[1], argv[2]);
895 if (os_snprintf_error(sizeof(buf), res))
897 return wpa_ctrl_command(ctrl, buf);
901 static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
903 hostapd_cli_quit = 1;
910 static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
914 printf("Invalid LEVEL command: needs one argument (debug "
918 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
919 return wpa_ctrl_command(ctrl, cmd);
923 static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
924 struct dl_list *interfaces)
929 if (!ctrl || !interfaces)
931 dir = opendir(ctrl_iface_dir);
935 while ((dent = readdir(dir))) {
936 if (strcmp(dent->d_name, ".") == 0 ||
937 strcmp(dent->d_name, "..") == 0)
939 cli_txt_list_add(interfaces, dent->d_name);
945 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
950 dir = opendir(ctrl_iface_dir);
952 printf("Control interface directory '%s' could not be "
953 "openned.\n", ctrl_iface_dir);
957 printf("Available interfaces:\n");
958 while ((dent = readdir(dir))) {
959 if (strcmp(dent->d_name, ".") == 0 ||
960 strcmp(dent->d_name, "..") == 0)
962 printf("%s\n", dent->d_name);
968 static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
972 hostapd_cli_list_interfaces(ctrl);
976 hostapd_cli_close_connection();
977 os_free(ctrl_ifname);
978 ctrl_ifname = os_strdup(argv[0]);
979 if (ctrl_ifname == NULL)
982 if (hostapd_cli_open_connection(ctrl_ifname)) {
983 printf("Connected to interface '%s.\n", ctrl_ifname);
984 if (wpa_ctrl_attach(ctrl_conn) == 0) {
985 hostapd_cli_attached = 1;
986 register_event_handler(ctrl_conn);
988 printf("Warning: Failed to attach to "
992 printf("Could not connect to interface '%s' - re-trying\n",
999 static char ** hostapd_complete_interface(const char *str, int pos)
1001 int arg = get_cmd_arg_num(str, pos);
1003 DEFINE_DL_LIST(interfaces);
1007 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1008 res = cli_txt_list_array(&interfaces);
1009 cli_txt_list_flush(&interfaces);
1017 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1023 printf("Invalid SET command: needs two arguments (variable "
1024 "name and value)\n");
1028 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
1029 if (os_snprintf_error(sizeof(cmd), res)) {
1030 printf("Too long SET command.\n");
1033 return wpa_ctrl_command(ctrl, cmd);
1037 static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1043 printf("Invalid GET command: needs one argument (variable "
1048 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
1049 if (os_snprintf_error(sizeof(cmd), res)) {
1050 printf("Too long GET command.\n");
1053 return wpa_ctrl_command(ctrl, cmd);
1058 static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1066 printf("FST command: parameters are required.\n");
1070 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1072 for (i = 0; i < argc; i++) {
1073 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1075 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1076 printf("Too long fst command.\n");
1081 return wpa_ctrl_command(ctrl, cmd);
1083 #endif /* CONFIG_FST */
1086 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1087 int argc, char *argv[])
1096 printf("Invalid chan_switch command: needs at least two "
1097 "arguments (count and freq)\n"
1098 "usage: <cs_count> <freq> [sec_channel_offset=] "
1099 "[center_freq1=] [center_freq2=] [bandwidth=] "
1100 "[blocktx] [ht|vht]\n");
1104 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1106 if (os_snprintf_error(sizeof(cmd), res)) {
1107 printf("Too long CHAN_SWITCH command.\n");
1112 for (i = 2; i < argc; i++) {
1114 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
1115 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1116 printf("Too long CHAN_SWITCH command.\n");
1121 return wpa_ctrl_command(ctrl, cmd);
1125 static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1128 return wpa_ctrl_command(ctrl, "ENABLE");
1132 static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1135 return wpa_ctrl_command(ctrl, "RELOAD");
1139 static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1142 return wpa_ctrl_command(ctrl, "DISABLE");
1146 static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1151 if (argc < 2 || argc > 3) {
1152 printf("Invalid vendor command\n"
1153 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1157 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
1158 argc == 3 ? argv[2] : "");
1159 if (os_snprintf_error(sizeof(cmd), res)) {
1160 printf("Too long VENDOR command.\n");
1163 return wpa_ctrl_command(ctrl, cmd);
1167 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1170 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1174 static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1180 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1181 argc >= 1 ? " " : "",
1182 argc >= 1 ? argv[0] : "",
1183 argc == 2 ? " " : "",
1184 argc == 2 ? argv[1] : "");
1185 if (os_snprintf_error(sizeof(cmd), res)) {
1186 printf("Too long option\n");
1189 return wpa_ctrl_command(ctrl, cmd);
1193 static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1197 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1201 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1203 return wpa_ctrl_command(ctrl, "PMKSA");
1207 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1210 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1214 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1220 if (argc < 3 || argc > 5) {
1221 printf("Invalid set_neighbor command: needs 3-5 arguments\n");
1225 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s",
1226 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
1227 argc == 5 ? argv[4] : "");
1228 if (os_snprintf_error(sizeof(cmd), res)) {
1229 printf("Too long SET_NEIGHBOR command.\n");
1232 return wpa_ctrl_command(ctrl, cmd);
1236 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1243 printf("Invalid remove_neighbor command: needs 2 arguments\n");
1247 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
1249 if (os_snprintf_error(sizeof(cmd), res)) {
1250 printf("Too long REMOVE_NEIGHBOR command.\n");
1253 return wpa_ctrl_command(ctrl, cmd);
1257 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1264 printf("Invalid req_lci command - requires destination address\n");
1268 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1269 if (os_snprintf_error(sizeof(cmd), res)) {
1270 printf("Too long REQ_LCI command.\n");
1273 return wpa_ctrl_command(ctrl, cmd);
1277 static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1281 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1285 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1289 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1292 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1296 struct hostapd_cli_cmd {
1298 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
1299 char ** (*completion)(const char *str, int pos);
1303 static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
1304 { "ping", hostapd_cli_cmd_ping, NULL,
1305 "= pings hostapd" },
1306 { "mib", hostapd_cli_cmd_mib, NULL,
1307 "= get MIB variables (dot1x, dot11, radius)" },
1308 { "relog", hostapd_cli_cmd_relog, NULL, NULL },
1309 { "status", hostapd_cli_cmd_status, NULL, NULL },
1310 { "sta", hostapd_cli_cmd_sta, NULL,
1311 "<addr> = get MIB variables for one station" },
1312 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1313 "= get MIB variables for all stations" },
1314 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1315 "<addr> = add a new station" },
1316 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
1317 hostapd_complete_deauthenticate,
1318 "<addr> = deauthenticate a station" },
1319 { "disassociate", hostapd_cli_cmd_disassociate,
1320 hostapd_complete_disassociate,
1321 "<addr> = disassociate a station" },
1322 #ifdef CONFIG_IEEE80211W
1323 { "sa_query", hostapd_cli_cmd_sa_query, NULL,
1324 "<addr> = send SA Query to a station" },
1325 #endif /* CONFIG_IEEE80211W */
1327 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1328 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1329 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1330 "<PIN> = verify PIN checksum" },
1331 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1332 "= indicate button pushed to initiate PBC" },
1333 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1334 "= cancel the pending WPS operation" },
1335 #ifdef CONFIG_WPS_NFC
1336 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1337 "<hexdump> = report read NFC tag with WPS data" },
1338 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1339 "<WPS/NDEF> = build NFC configuration token" },
1340 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1341 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1342 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1344 #endif /* CONFIG_WPS_NFC */
1345 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1346 "<cmd> [params..] = enable/disable AP PIN" },
1347 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1348 "<SSID> <auth> <encr> <key> = configure AP" },
1349 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1350 "= show current WPS status" },
1351 #endif /* CONFIG_WPS */
1352 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, NULL },
1353 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, NULL },
1354 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, NULL },
1355 { "get_config", hostapd_cli_cmd_get_config, NULL,
1356 "= show current configuration" },
1357 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1358 "= show this usage help" },
1359 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1360 "[ifname] = show interfaces/select interface" },
1362 { "fst", hostapd_cli_cmd_fst, NULL, NULL },
1363 #endif /* CONFIG_FST */
1364 { "raw", hostapd_cli_cmd_raw, NULL, NULL },
1365 { "level", hostapd_cli_cmd_level, NULL,
1366 "<debug level> = change debug level" },
1367 { "license", hostapd_cli_cmd_license, NULL,
1368 "= show full hostapd_cli license" },
1369 { "quit", hostapd_cli_cmd_quit, NULL,
1370 "= exit hostapd_cli" },
1371 { "set", hostapd_cli_cmd_set, NULL, NULL },
1372 { "get", hostapd_cli_cmd_get, NULL, NULL },
1373 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, NULL },
1374 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, NULL, NULL },
1375 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, NULL },
1376 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, NULL },
1377 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, NULL },
1378 { "vendor", hostapd_cli_cmd_vendor, NULL, NULL },
1379 { "enable", hostapd_cli_cmd_enable, NULL, NULL },
1380 { "reload", hostapd_cli_cmd_reload, NULL, NULL },
1381 { "disable", hostapd_cli_cmd_disable, NULL, NULL },
1382 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, NULL },
1383 { "log_level", hostapd_cli_cmd_log_level, NULL, NULL },
1384 { "pmksa", hostapd_cli_cmd_pmksa, NULL, NULL },
1385 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, NULL },
1386 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, NULL },
1387 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, NULL },
1388 { "req_lci", hostapd_cli_cmd_req_lci, NULL, NULL },
1389 { "req_range", hostapd_cli_cmd_req_range, NULL, NULL },
1390 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, NULL },
1391 { NULL, NULL, NULL, NULL }
1396 * Prints command usage, lines are padded with the specified string.
1398 static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1404 if (cmd->usage == NULL)
1406 fprintf(stream, "%s%s ", pad, cmd->cmd);
1407 for (n = 0; (c = cmd->usage[n]); n++) {
1408 fprintf(stream, "%c", c);
1410 fprintf(stream, "%s", pad);
1412 fprintf(stream, "\n");
1416 static void print_help(FILE *stream, const char *cmd)
1420 fprintf(stream, "commands:\n");
1421 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1422 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1423 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1428 static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1430 const struct hostapd_cli_cmd *cmd, *match = NULL;
1434 cmd = hostapd_cli_commands;
1436 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1438 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1439 /* we have an exact match */
1449 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1450 cmd = hostapd_cli_commands;
1452 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1454 printf(" %s", cmd->cmd);
1459 } else if (count == 0) {
1460 printf("Unknown command '%s'\n", argv[0]);
1462 match->handler(ctrl, argc - 1, &argv[1]);
1467 static void cli_event(const char *str)
1469 const char *start, *s;
1471 start = os_strchr(str, '>');
1477 if (str_starts(start, AP_STA_CONNECTED)) {
1478 s = os_strchr(start, ' ');
1481 cli_txt_list_add(&stations, s + 1);
1485 if (str_starts(start, AP_STA_DISCONNECTED)) {
1486 s = os_strchr(start, ' ');
1489 cli_txt_list_del_addr(&stations, s + 1);
1495 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1499 if (ctrl_conn == NULL)
1501 while (wpa_ctrl_pending(ctrl)) {
1503 size_t len = sizeof(buf) - 1;
1504 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1507 hostapd_cli_action_process(buf, len);
1510 if (in_read && first)
1513 printf("%s\n", buf);
1516 printf("Could not read pending message.\n");
1523 static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
1525 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
1531 static int tokenize_cmd(char *cmd, char *argv[])
1544 if (argc == max_args)
1547 char *pos2 = os_strrchr(pos, '"');
1551 while (*pos != '\0' && *pos != ' ')
1561 static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
1563 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1564 printf("Connection to hostapd lost - trying to reconnect\n");
1565 hostapd_cli_close_connection();
1568 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1570 printf("Connection to hostapd re-established\n");
1571 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1572 hostapd_cli_attached = 1;
1573 register_event_handler(ctrl_conn);
1575 printf("Warning: Failed to attach to "
1581 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
1582 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1586 static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1592 static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1594 char *argv[max_args];
1596 argc = tokenize_cmd(cmd, argv);
1598 wpa_request(ctrl_conn, argc, argv);
1602 static void hostapd_cli_edit_eof_cb(void *ctx)
1608 static char ** list_cmd_list(void)
1613 count = ARRAY_SIZE(hostapd_cli_commands);
1614 res = os_calloc(count + 1, sizeof(char *));
1618 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1619 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1628 static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1633 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1634 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
1636 if (hostapd_cli_commands[i].completion)
1637 return hostapd_cli_commands[i].completion(str, pos);
1638 if (!hostapd_cli_commands[i].usage)
1641 printf("\r%s\n", hostapd_cli_commands[i].usage);
1650 static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
1657 end = os_strchr(str, ' ');
1658 if (end == NULL || str + pos < end)
1659 return list_cmd_list();
1661 cmd = os_malloc(pos + 1);
1664 os_memcpy(cmd, str, pos);
1665 cmd[end - str] = '\0';
1666 res = hostapd_cli_cmd_completion(cmd, str, pos);
1672 static void hostapd_cli_interactive(void)
1674 printf("\nInteractive mode\n\n");
1676 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
1677 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
1678 hostapd_cli_edit_completion_cb, NULL, NULL, NULL);
1679 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1683 cli_txt_list_flush(&stations);
1684 edit_deinit(NULL, NULL);
1685 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
1689 static void hostapd_cli_cleanup(void)
1691 hostapd_cli_close_connection();
1693 os_daemonize_terminate(pid_file);
1695 os_program_deinit();
1699 static void hostapd_cli_action(struct wpa_ctrl *ctrl)
1707 fd = wpa_ctrl_get_fd(ctrl);
1709 while (!hostapd_cli_quit) {
1712 tv.tv_sec = ping_interval;
1714 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1715 if (res < 0 && errno != EINTR) {
1720 if (FD_ISSET(fd, &rfds))
1721 hostapd_cli_recv_pending(ctrl, 0, 1);
1723 len = sizeof(buf) - 1;
1724 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1725 hostapd_cli_action_process) < 0 ||
1726 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1727 printf("hostapd did not reply to PING "
1728 "command - exiting\n");
1736 int main(int argc, char *argv[])
1738 int warning_displayed = 0;
1742 if (os_program_init())
1746 c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
1751 action_file = optarg;
1757 ping_interval = atoi(optarg);
1763 printf("%s\n", hostapd_cli_version);
1766 os_free(ctrl_ifname);
1767 ctrl_ifname = os_strdup(optarg);
1770 ctrl_iface_dir = optarg;
1776 client_socket_dir = optarg;
1784 interactive = (argc == optind) && (action_file == NULL);
1787 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
1794 if (ctrl_ifname == NULL) {
1795 struct dirent *dent;
1796 DIR *dir = opendir(ctrl_iface_dir);
1798 while ((dent = readdir(dir))) {
1799 if (os_strcmp(dent->d_name, ".") == 0
1801 os_strcmp(dent->d_name, "..") == 0)
1803 printf("Selected interface '%s'\n",
1805 ctrl_ifname = os_strdup(dent->d_name);
1811 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1813 if (warning_displayed)
1814 printf("Connection established.\n");
1819 perror("Failed to connect to hostapd - "
1824 if (!warning_displayed) {
1825 printf("Could not connect to hostapd - re-trying\n");
1826 warning_displayed = 1;
1832 if (interactive || action_file) {
1833 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1834 hostapd_cli_attached = 1;
1835 register_event_handler(ctrl_conn);
1837 printf("Warning: Failed to attach to hostapd.\n");
1843 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
1847 hostapd_cli_interactive();
1848 else if (action_file)
1849 hostapd_cli_action(ctrl_conn);
1851 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1853 unregister_event_handler(ctrl_conn);
1854 os_free(ctrl_ifname);
1856 hostapd_cli_cleanup();
1860 #else /* CONFIG_NO_CTRL_IFACE */
1862 int main(int argc, char *argv[])
1867 #endif /* CONFIG_NO_CTRL_IFACE */