2 * hostapd - command line interface for hostapd daemon
3 * Copyright (c) 2004-2019, 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-2019, 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
);
48 static void update_stations(struct wpa_ctrl
*ctrl
);
49 static void cli_event(const char *str
);
52 static void usage(void)
54 fprintf(stderr
, "%s\n", hostapd_cli_version
);
57 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvBr] "
59 " [-P<pid file>] [-G<ping interval>] [command..]\n"
62 " -h help (show this usage text)\n"
63 " -v shown version information\n"
64 " -p<path> path to find control sockets (default: "
66 " -s<dir_path> dir path to open client sockets (default: "
67 CONFIG_CTRL_IFACE_DIR
")\n"
68 " -a<file> run in daemon mode executing the action file "
71 " -r try to reconnect when client socket is "
73 " This is useful only when used with -a.\n"
74 " -B run a daemon in the background\n"
75 " -i<ifname> Interface to listen on (default: first "
76 "interface found in the\n"
78 print_help(stderr
, NULL
);
82 static void register_event_handler(struct wpa_ctrl
*ctrl
)
87 event_handler_registered
=
88 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl
),
95 static void unregister_event_handler(struct wpa_ctrl
*ctrl
)
99 if (interactive
&& event_handler_registered
) {
100 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl
));
101 event_handler_registered
= 0;
106 static struct wpa_ctrl
* hostapd_cli_open_connection(const char *ifname
)
108 #ifndef CONFIG_CTRL_IFACE_UDP
111 #endif /* !CONFIG_CTRL_IFACE_UDP */
116 #ifdef CONFIG_CTRL_IFACE_UDP
117 ctrl_conn
= wpa_ctrl_open(ifname
);
119 #else /* CONFIG_CTRL_IFACE_UDP */
120 flen
= strlen(ctrl_iface_dir
) + strlen(ifname
) + 2;
121 cfile
= malloc(flen
);
124 snprintf(cfile
, flen
, "%s/%s", ctrl_iface_dir
, ifname
);
126 if (client_socket_dir
&& client_socket_dir
[0] &&
127 access(client_socket_dir
, F_OK
) < 0) {
128 perror(client_socket_dir
);
133 ctrl_conn
= wpa_ctrl_open2(cfile
, client_socket_dir
);
136 #endif /* CONFIG_CTRL_IFACE_UDP */
140 static void hostapd_cli_close_connection(void)
142 if (ctrl_conn
== NULL
)
145 unregister_event_handler(ctrl_conn
);
146 if (hostapd_cli_attached
) {
147 wpa_ctrl_detach(ctrl_conn
);
148 hostapd_cli_attached
= 0;
150 wpa_ctrl_close(ctrl_conn
);
155 static int hostapd_cli_reconnect(const char *ifname
)
157 char *next_ctrl_ifname
;
159 hostapd_cli_close_connection();
164 next_ctrl_ifname
= os_strdup(ifname
);
165 os_free(ctrl_ifname
);
166 ctrl_ifname
= next_ctrl_ifname
;
170 ctrl_conn
= hostapd_cli_open_connection(ctrl_ifname
);
173 if (!interactive
&& !action_file
)
175 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
176 hostapd_cli_attached
= 1;
177 register_event_handler(ctrl_conn
);
178 update_stations(ctrl_conn
);
180 printf("Warning: Failed to attach to hostapd.\n");
186 static void hostapd_cli_msg_cb(char *msg
, size_t len
)
193 static int _wpa_ctrl_command(struct wpa_ctrl
*ctrl
, const char *cmd
, int print
)
199 if (ctrl_conn
== NULL
) {
200 printf("Not connected to hostapd - command dropped.\n");
203 len
= sizeof(buf
) - 1;
204 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
207 printf("'%s' command timed out.\n", cmd
);
209 } else if (ret
< 0) {
210 printf("'%s' command failed.\n", cmd
);
221 static inline int wpa_ctrl_command(struct wpa_ctrl
*ctrl
, const char *cmd
)
223 return _wpa_ctrl_command(ctrl
, cmd
, 1);
227 static int hostapd_cli_cmd(struct wpa_ctrl
*ctrl
, const char *cmd
,
228 int min_args
, int argc
, char *argv
[])
232 if (argc
< min_args
) {
233 printf("Invalid %s command - at least %d argument%s required.\n",
234 cmd
, min_args
, min_args
> 1 ? "s are" : " is");
237 if (write_cmd(buf
, sizeof(buf
), cmd
, argc
, argv
) < 0)
239 return wpa_ctrl_command(ctrl
, buf
);
243 static int hostapd_cli_cmd_ping(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
245 return wpa_ctrl_command(ctrl
, "PING");
249 static int hostapd_cli_cmd_relog(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
251 return wpa_ctrl_command(ctrl
, "RELOG");
255 static int hostapd_cli_cmd_status(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
257 if (argc
> 0 && os_strcmp(argv
[0], "driver") == 0)
258 return wpa_ctrl_command(ctrl
, "STATUS-DRIVER");
259 return wpa_ctrl_command(ctrl
, "STATUS");
263 static int hostapd_cli_cmd_mib(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
267 os_snprintf(buf
, sizeof(buf
), "MIB %s", argv
[0]);
268 return wpa_ctrl_command(ctrl
, buf
);
270 return wpa_ctrl_command(ctrl
, "MIB");
274 static int hostapd_cli_exec(const char *program
, const char *arg1
,
281 len
= os_strlen(arg1
) + os_strlen(arg2
) + 2;
282 arg
= os_malloc(len
);
285 os_snprintf(arg
, len
, "%s %s", arg1
, arg2
);
286 res
= os_exec(program
, arg
, 1);
293 static void hostapd_cli_action_process(char *msg
, size_t len
)
299 pos
= os_strchr(pos
, '>');
306 hostapd_cli_exec(action_file
, ctrl_ifname
, pos
);
310 static int hostapd_cli_cmd_sta(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
314 printf("Invalid 'sta' command - at least one argument, STA "
315 "address, is required.\n");
319 snprintf(buf
, sizeof(buf
), "STA %s %s", argv
[0], argv
[1]);
321 snprintf(buf
, sizeof(buf
), "STA %s", argv
[0]);
322 return wpa_ctrl_command(ctrl
, buf
);
326 static char ** hostapd_complete_stations(const char *str
, int pos
)
328 int arg
= get_cmd_arg_num(str
, pos
);
333 res
= cli_txt_list_array(&stations
);
341 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl
*ctrl
, int argc
,
346 printf("Invalid 'new_sta' command - exactly one argument, STA "
347 "address, is required.\n");
350 snprintf(buf
, sizeof(buf
), "NEW_STA %s", argv
[0]);
351 return wpa_ctrl_command(ctrl
, buf
);
355 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl
*ctrl
, int argc
,
360 printf("Invalid 'deauthenticate' command - exactly one "
361 "argument, STA address, is required.\n");
365 os_snprintf(buf
, sizeof(buf
), "DEAUTHENTICATE %s %s",
368 os_snprintf(buf
, sizeof(buf
), "DEAUTHENTICATE %s", argv
[0]);
369 return wpa_ctrl_command(ctrl
, buf
);
373 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl
*ctrl
, int argc
,
378 printf("Invalid 'disassociate' command - exactly one "
379 "argument, STA address, is required.\n");
383 os_snprintf(buf
, sizeof(buf
), "DISASSOCIATE %s %s",
386 os_snprintf(buf
, sizeof(buf
), "DISASSOCIATE %s", argv
[0]);
387 return wpa_ctrl_command(ctrl
, buf
);
391 #ifdef CONFIG_TAXONOMY
392 static int hostapd_cli_cmd_signature(struct wpa_ctrl
*ctrl
, int argc
,
398 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
401 os_snprintf(buf
, sizeof(buf
), "SIGNATURE %s", argv
[0]);
402 return wpa_ctrl_command(ctrl
, buf
);
404 #endif /* CONFIG_TAXONOMY */
407 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl
*ctrl
, int argc
,
412 printf("Invalid 'sa_query' command - exactly one argument, "
413 "STA address, is required.\n");
416 snprintf(buf
, sizeof(buf
), "SA_QUERY %s", argv
[0]);
417 return wpa_ctrl_command(ctrl
, buf
);
422 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl
*ctrl
, int argc
,
427 printf("Invalid 'wps_pin' command - at least two arguments, "
428 "UUID and PIN, are required.\n");
432 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s %s %s",
433 argv
[0], argv
[1], argv
[2], argv
[3]);
435 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s %s",
436 argv
[0], argv
[1], argv
[2]);
438 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s", argv
[0], argv
[1]);
439 return wpa_ctrl_command(ctrl
, buf
);
443 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl
*ctrl
, int argc
,
449 if (argc
!= 1 && argc
!= 2) {
450 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
451 "- PIN to be verified\n");
456 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s %s",
459 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s",
461 if (os_snprintf_error(sizeof(cmd
), res
)) {
462 printf("Too long WPS_CHECK_PIN command.\n");
465 return wpa_ctrl_command(ctrl
, cmd
);
469 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl
*ctrl
, int argc
,
472 return wpa_ctrl_command(ctrl
, "WPS_PBC");
476 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl
*ctrl
, int argc
,
479 return wpa_ctrl_command(ctrl
, "WPS_CANCEL");
483 #ifdef CONFIG_WPS_NFC
484 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl
*ctrl
, int argc
,
492 printf("Invalid 'wps_nfc_tag_read' command - one argument "
497 buflen
= 18 + os_strlen(argv
[0]);
498 buf
= os_malloc(buflen
);
501 os_snprintf(buf
, buflen
, "WPS_NFC_TAG_READ %s", argv
[0]);
503 ret
= wpa_ctrl_command(ctrl
, buf
);
510 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl
*ctrl
,
511 int argc
, char *argv
[])
517 printf("Invalid 'wps_nfc_config_token' command - one argument "
522 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_NFC_CONFIG_TOKEN %s",
524 if (os_snprintf_error(sizeof(cmd
), res
)) {
525 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
528 return wpa_ctrl_command(ctrl
, cmd
);
532 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl
*ctrl
,
533 int argc
, char *argv
[])
539 printf("Invalid 'wps_nfc_token' command - one argument is "
544 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_NFC_TOKEN %s", argv
[0]);
545 if (os_snprintf_error(sizeof(cmd
), res
)) {
546 printf("Too long WPS_NFC_TOKEN command.\n");
549 return wpa_ctrl_command(ctrl
, cmd
);
553 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl
*ctrl
,
554 int argc
, char *argv
[])
560 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
565 res
= os_snprintf(cmd
, sizeof(cmd
), "NFC_GET_HANDOVER_SEL %s %s",
567 if (os_snprintf_error(sizeof(cmd
), res
)) {
568 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
571 return wpa_ctrl_command(ctrl
, cmd
);
574 #endif /* CONFIG_WPS_NFC */
577 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl
*ctrl
, int argc
,
582 printf("Invalid 'wps_ap_pin' command - at least one argument "
587 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s %s %s",
588 argv
[0], argv
[1], argv
[2]);
590 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s %s",
593 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s", argv
[0]);
594 return wpa_ctrl_command(ctrl
, buf
);
598 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl
*ctrl
, int argc
,
601 return wpa_ctrl_command(ctrl
, "WPS_GET_STATUS");
605 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl
*ctrl
, int argc
,
609 char ssid_hex
[2 * SSID_MAX_LEN
+ 1];
610 char key_hex
[2 * 64 + 1];
614 printf("Invalid 'wps_config' command - at least two arguments "
620 for (i
= 0; i
< SSID_MAX_LEN
; i
++) {
621 if (argv
[0][i
] == '\0')
623 os_snprintf(&ssid_hex
[i
* 2], 3, "%02x", argv
[0][i
]);
628 for (i
= 0; i
< 64; i
++) {
629 if (argv
[3][i
] == '\0')
631 os_snprintf(&key_hex
[i
* 2], 3, "%02x",
637 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s %s %s",
638 ssid_hex
, argv
[1], argv
[2], key_hex
);
640 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s %s",
641 ssid_hex
, argv
[1], argv
[2]);
643 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s",
645 return wpa_ctrl_command(ctrl
, buf
);
647 #endif /* CONFIG_WPS */
650 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl
*ctrl
, int argc
,
657 printf("Invalid 'disassoc_imminent' command - two arguments "
658 "(STA addr and Disassociation Timer) are needed\n");
662 res
= os_snprintf(buf
, sizeof(buf
), "DISASSOC_IMMINENT %s %s",
664 if (os_snprintf_error(sizeof(buf
), res
))
666 return wpa_ctrl_command(ctrl
, buf
);
670 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl
*ctrl
, int argc
,
677 printf("Invalid 'ess_disassoc' command - three arguments (STA "
678 "addr, disassoc timer, and URL) are needed\n");
682 res
= os_snprintf(buf
, sizeof(buf
), "ESS_DISASSOC %s %s %s",
683 argv
[0], argv
[1], argv
[2]);
684 if (os_snprintf_error(sizeof(buf
), res
))
686 return wpa_ctrl_command(ctrl
, buf
);
690 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl
*ctrl
, int argc
,
693 char buf
[2000], *tmp
;
697 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
701 res
= os_snprintf(buf
, sizeof(buf
), "BSS_TM_REQ %s", argv
[0]);
702 if (os_snprintf_error(sizeof(buf
), res
))
706 for (i
= 1; i
< argc
; i
++) {
708 res
= os_snprintf(tmp
, sizeof(buf
) - total
, " %s", argv
[i
]);
709 if (os_snprintf_error(sizeof(buf
) - total
, res
))
713 return wpa_ctrl_command(ctrl
, buf
);
717 static int hostapd_cli_cmd_get_config(struct wpa_ctrl
*ctrl
, int argc
,
720 return wpa_ctrl_command(ctrl
, "GET_CONFIG");
724 static int wpa_ctrl_command_sta(struct wpa_ctrl
*ctrl
, const char *cmd
,
725 char *addr
, size_t addr_len
, int print
)
727 char buf
[4096], *pos
;
731 if (ctrl_conn
== NULL
) {
732 printf("Not connected to hostapd - command dropped.\n");
735 len
= sizeof(buf
) - 1;
736 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
739 printf("'%s' command timed out.\n", cmd
);
741 } else if (ret
< 0) {
742 printf("'%s' command failed.\n", cmd
);
747 if (memcmp(buf
, "FAIL", 4) == 0)
753 while (*pos
!= '\0' && *pos
!= '\n')
756 os_strlcpy(addr
, buf
, addr_len
);
761 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl
*ctrl
, int argc
,
764 char addr
[32], cmd
[64];
766 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 1))
769 snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
770 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 1) == 0);
776 static int hostapd_cli_cmd_list_sta(struct wpa_ctrl
*ctrl
, int argc
,
779 char addr
[32], cmd
[64];
781 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 0))
784 if (os_strcmp(addr
, "") != 0)
785 printf("%s\n", addr
);
786 os_snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
787 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 0) == 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 update_stations(struct wpa_ctrl
*ctrl
)
931 char addr
[32], cmd
[64];
933 if (!ctrl
|| !interactive
)
936 cli_txt_list_flush(&stations
);
938 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 0))
941 if (os_strcmp(addr
, "") != 0)
942 cli_txt_list_add(&stations
, addr
);
943 os_snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
944 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 0) == 0);
948 static void hostapd_cli_get_interfaces(struct wpa_ctrl
*ctrl
,
949 struct dl_list
*interfaces
)
954 if (!ctrl
|| !interfaces
)
956 dir
= opendir(ctrl_iface_dir
);
960 while ((dent
= readdir(dir
))) {
961 if (strcmp(dent
->d_name
, ".") == 0 ||
962 strcmp(dent
->d_name
, "..") == 0)
964 cli_txt_list_add(interfaces
, dent
->d_name
);
970 static void hostapd_cli_list_interfaces(struct wpa_ctrl
*ctrl
)
975 dir
= opendir(ctrl_iface_dir
);
977 printf("Control interface directory '%s' could not be "
978 "openned.\n", ctrl_iface_dir
);
982 printf("Available interfaces:\n");
983 while ((dent
= readdir(dir
))) {
984 if (strcmp(dent
->d_name
, ".") == 0 ||
985 strcmp(dent
->d_name
, "..") == 0)
987 printf("%s\n", dent
->d_name
);
993 static int hostapd_cli_cmd_interface(struct wpa_ctrl
*ctrl
, int argc
,
997 hostapd_cli_list_interfaces(ctrl
);
1000 if (hostapd_cli_reconnect(argv
[0]) != 0) {
1001 printf("Could not connect to interface '%s' - re-trying\n",
1008 static char ** hostapd_complete_interface(const char *str
, int pos
)
1010 int arg
= get_cmd_arg_num(str
, pos
);
1012 DEFINE_DL_LIST(interfaces
);
1016 hostapd_cli_get_interfaces(ctrl_conn
, &interfaces
);
1017 res
= cli_txt_list_array(&interfaces
);
1018 cli_txt_list_flush(&interfaces
);
1026 static int hostapd_cli_cmd_set(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1032 printf("Invalid SET command: needs two arguments (variable "
1033 "name and value)\n");
1037 res
= os_snprintf(cmd
, sizeof(cmd
), "SET %s %s", argv
[0], argv
[1]);
1038 if (os_snprintf_error(sizeof(cmd
), res
)) {
1039 printf("Too long SET command.\n");
1042 return wpa_ctrl_command(ctrl
, cmd
);
1046 static char ** hostapd_complete_set(const char *str
, int pos
)
1048 int arg
= get_cmd_arg_num(str
, pos
);
1049 const char *fields
[] = {
1050 #ifdef CONFIG_WPS_TESTING
1051 "wps_version_number", "wps_testing_dummy_cred",
1052 "wps_corrupt_pkhash",
1053 #endif /* CONFIG_WPS_TESTING */
1054 #ifdef CONFIG_INTERWORKING
1056 #endif /* CONFIG_INTERWORKING */
1057 #ifdef CONFIG_TESTING_OPTIONS
1058 "ext_mgmt_frame_handling", "ext_eapol_frame_io",
1059 #endif /* CONFIG_TESTING_OPTIONS */
1061 "mbo_assoc_disallow",
1062 #endif /* CONFIG_MBO */
1063 "deny_mac_file", "accept_mac_file",
1065 int i
, num_fields
= ARRAY_SIZE(fields
);
1070 res
= os_calloc(num_fields
+ 1, sizeof(char *));
1073 for (i
= 0; i
< num_fields
; i
++) {
1074 res
[i
] = os_strdup(fields
[i
]);
1084 static int hostapd_cli_cmd_get(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1090 printf("Invalid GET command: needs one argument (variable "
1095 res
= os_snprintf(cmd
, sizeof(cmd
), "GET %s", argv
[0]);
1096 if (os_snprintf_error(sizeof(cmd
), res
)) {
1097 printf("Too long GET command.\n");
1100 return wpa_ctrl_command(ctrl
, cmd
);
1104 static char ** hostapd_complete_get(const char *str
, int pos
)
1106 int arg
= get_cmd_arg_num(str
, pos
);
1107 const char *fields
[] = {
1108 "version", "tls_library",
1110 int i
, num_fields
= ARRAY_SIZE(fields
);
1115 res
= os_calloc(num_fields
+ 1, sizeof(char *));
1118 for (i
= 0; i
< num_fields
; i
++) {
1119 res
[i
] = os_strdup(fields
[i
]);
1130 static int hostapd_cli_cmd_fst(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1138 printf("FST command: parameters are required.\n");
1142 total
= os_snprintf(cmd
, sizeof(cmd
), "FST-MANAGER");
1144 for (i
= 0; i
< argc
; i
++) {
1145 res
= os_snprintf(cmd
+ total
, sizeof(cmd
) - total
, " %s",
1147 if (os_snprintf_error(sizeof(cmd
) - total
, res
)) {
1148 printf("Too long fst command.\n");
1153 return wpa_ctrl_command(ctrl
, cmd
);
1155 #endif /* CONFIG_FST */
1158 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl
*ctrl
,
1159 int argc
, char *argv
[])
1168 printf("Invalid chan_switch command: needs at least two "
1169 "arguments (count and freq)\n"
1170 "usage: <cs_count> <freq> [sec_channel_offset=] "
1171 "[center_freq1=] [center_freq2=] [bandwidth=] "
1172 "[blocktx] [ht|vht]\n");
1176 res
= os_snprintf(cmd
, sizeof(cmd
), "CHAN_SWITCH %s %s",
1178 if (os_snprintf_error(sizeof(cmd
), res
)) {
1179 printf("Too long CHAN_SWITCH command.\n");
1184 for (i
= 2; i
< argc
; i
++) {
1186 res
= os_snprintf(tmp
, sizeof(cmd
) - total
, " %s", argv
[i
]);
1187 if (os_snprintf_error(sizeof(cmd
) - total
, res
)) {
1188 printf("Too long CHAN_SWITCH command.\n");
1193 return wpa_ctrl_command(ctrl
, cmd
);
1197 static int hostapd_cli_cmd_enable(struct wpa_ctrl
*ctrl
, int argc
,
1200 return wpa_ctrl_command(ctrl
, "ENABLE");
1204 static int hostapd_cli_cmd_reload(struct wpa_ctrl
*ctrl
, int argc
,
1207 return wpa_ctrl_command(ctrl
, "RELOAD");
1211 static int hostapd_cli_cmd_disable(struct wpa_ctrl
*ctrl
, int argc
,
1214 return wpa_ctrl_command(ctrl
, "DISABLE");
1218 static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl
*ctrl
, int argc
,
1221 return wpa_ctrl_command(ctrl
, "UPDATE_BEACON");
1225 static int hostapd_cli_cmd_vendor(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1230 if (argc
< 2 || argc
> 3) {
1231 printf("Invalid vendor command\n"
1232 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1236 res
= os_snprintf(cmd
, sizeof(cmd
), "VENDOR %s %s %s", argv
[0], argv
[1],
1237 argc
== 3 ? argv
[2] : "");
1238 if (os_snprintf_error(sizeof(cmd
), res
)) {
1239 printf("Too long VENDOR command.\n");
1242 return wpa_ctrl_command(ctrl
, cmd
);
1246 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl
*ctrl
, int argc
,
1249 return wpa_ctrl_command(ctrl
, "ERP_FLUSH");
1253 static int hostapd_cli_cmd_log_level(struct wpa_ctrl
*ctrl
, int argc
,
1259 res
= os_snprintf(cmd
, sizeof(cmd
), "LOG_LEVEL%s%s%s%s",
1260 argc
>= 1 ? " " : "",
1261 argc
>= 1 ? argv
[0] : "",
1262 argc
== 2 ? " " : "",
1263 argc
== 2 ? argv
[1] : "");
1264 if (os_snprintf_error(sizeof(cmd
), res
)) {
1265 printf("Too long option\n");
1268 return wpa_ctrl_command(ctrl
, cmd
);
1272 static int hostapd_cli_cmd_raw(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1276 return hostapd_cli_cmd(ctrl
, argv
[0], 0, argc
- 1, &argv
[1]);
1280 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1282 return wpa_ctrl_command(ctrl
, "PMKSA");
1286 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl
*ctrl
, int argc
,
1289 return wpa_ctrl_command(ctrl
, "PMKSA_FLUSH");
1293 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl
*ctrl
, int argc
,
1299 if (argc
< 3 || argc
> 6) {
1300 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
1304 res
= os_snprintf(cmd
, sizeof(cmd
), "SET_NEIGHBOR %s %s %s %s %s %s",
1305 argv
[0], argv
[1], argv
[2], argc
>= 4 ? argv
[3] : "",
1306 argc
>= 5 ? argv
[4] : "", argc
== 6 ? argv
[5] : "");
1307 if (os_snprintf_error(sizeof(cmd
), res
)) {
1308 printf("Too long SET_NEIGHBOR command.\n");
1311 return wpa_ctrl_command(ctrl
, cmd
);
1315 static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl
*ctrl
, int argc
,
1318 return wpa_ctrl_command(ctrl
, "SHOW_NEIGHBOR");
1322 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl
*ctrl
, int argc
,
1325 return hostapd_cli_cmd(ctrl
, "REMOVE_NEIGHBOR", 1, argc
, argv
);
1329 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl
*ctrl
, int argc
,
1336 printf("Invalid req_lci command - requires destination address\n");
1340 res
= os_snprintf(cmd
, sizeof(cmd
), "REQ_LCI %s", argv
[0]);
1341 if (os_snprintf_error(sizeof(cmd
), res
)) {
1342 printf("Too long REQ_LCI command.\n");
1345 return wpa_ctrl_command(ctrl
, cmd
);
1349 static int hostapd_cli_cmd_req_range(struct wpa_ctrl
*ctrl
, int argc
,
1353 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1357 return hostapd_cli_cmd(ctrl
, "REQ_RANGE", 4, argc
, argv
);
1361 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl
*ctrl
, int argc
,
1364 return wpa_ctrl_command(ctrl
, "DRIVER_FLAGS");
1370 static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl
*ctrl
, int argc
,
1373 return hostapd_cli_cmd(ctrl
, "DPP_QR_CODE", 1, argc
, argv
);
1377 static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl
*ctrl
, int argc
,
1380 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_GEN", 1, argc
, argv
);
1384 static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl
*ctrl
, int argc
,
1387 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_REMOVE", 1, argc
, argv
);
1391 static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl
*ctrl
,
1392 int argc
, char *argv
[])
1394 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_GET_URI", 1, argc
, argv
);
1398 static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl
*ctrl
, int argc
,
1401 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_INFO", 1, argc
, argv
);
1405 static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl
*ctrl
, int argc
,
1408 return hostapd_cli_cmd(ctrl
, "DPP_AUTH_INIT", 1, argc
, argv
);
1412 static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl
*ctrl
, int argc
,
1415 return hostapd_cli_cmd(ctrl
, "DPP_LISTEN", 1, argc
, argv
);
1419 static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl
*ctrl
, int argc
,
1422 return wpa_ctrl_command(ctrl
, "DPP_STOP_LISTEN");
1426 static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl
*ctrl
, int argc
,
1429 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_ADD", 0, argc
, argv
);
1433 static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl
*ctrl
,
1434 int argc
, char *argv
[])
1436 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_REMOVE", 1, argc
, argv
);
1440 static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl
*ctrl
,
1441 int argc
, char *argv
[])
1443 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_GET_KEY", 1, argc
, argv
);
1447 static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl
*ctrl
,
1448 int argc
, char *argv
[])
1450 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_SIGN", 1, argc
, argv
);
1454 static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl
*ctrl
, int argc
,
1457 return hostapd_cli_cmd(ctrl
, "DPP_PKEX_ADD", 1, argc
, argv
);
1461 static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl
*ctrl
, int argc
,
1464 return hostapd_cli_cmd(ctrl
, "DPP_PKEX_REMOVE", 1, argc
, argv
);
1467 #endif /* CONFIG_DPP */
1470 static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl
*ctrl
, int argc
,
1473 return hostapd_cli_cmd(ctrl
, "ACCEPT_ACL", 1, argc
, argv
);
1477 static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl
*ctrl
, int argc
,
1480 return hostapd_cli_cmd(ctrl
, "DENY_ACL", 1, argc
, argv
);
1484 static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl
*ctrl
, int argc
,
1487 return hostapd_cli_cmd(ctrl
, "POLL_STA", 1, argc
, argv
);
1491 static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl
*ctrl
, int argc
,
1494 return hostapd_cli_cmd(ctrl
, "REQ_BEACON", 2, argc
, argv
);
1498 static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl
*ctrl
, int argc
,
1501 return wpa_ctrl_command(ctrl
, "RELOAD_WPA_PSK");
1505 struct hostapd_cli_cmd
{
1507 int (*handler
)(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[]);
1508 char ** (*completion
)(const char *str
, int pos
);
1512 static const struct hostapd_cli_cmd hostapd_cli_commands
[] = {
1513 { "ping", hostapd_cli_cmd_ping
, NULL
,
1514 "= pings hostapd" },
1515 { "mib", hostapd_cli_cmd_mib
, NULL
,
1516 "= get MIB variables (dot1x, dot11, radius)" },
1517 { "relog", hostapd_cli_cmd_relog
, NULL
,
1518 "= reload/truncate debug log output file" },
1519 { "status", hostapd_cli_cmd_status
, NULL
,
1520 "= show interface status info" },
1521 { "sta", hostapd_cli_cmd_sta
, hostapd_complete_stations
,
1522 "<addr> = get MIB variables for one station" },
1523 { "all_sta", hostapd_cli_cmd_all_sta
, NULL
,
1524 "= get MIB variables for all stations" },
1525 { "list_sta", hostapd_cli_cmd_list_sta
, NULL
,
1526 "= list all stations" },
1527 { "new_sta", hostapd_cli_cmd_new_sta
, NULL
,
1528 "<addr> = add a new station" },
1529 { "deauthenticate", hostapd_cli_cmd_deauthenticate
,
1530 hostapd_complete_stations
,
1531 "<addr> = deauthenticate a station" },
1532 { "disassociate", hostapd_cli_cmd_disassociate
,
1533 hostapd_complete_stations
,
1534 "<addr> = disassociate a station" },
1535 #ifdef CONFIG_TAXONOMY
1536 { "signature", hostapd_cli_cmd_signature
, hostapd_complete_stations
,
1537 "<addr> = get taxonomy signature for a station" },
1538 #endif /* CONFIG_TAXONOMY */
1539 { "sa_query", hostapd_cli_cmd_sa_query
, hostapd_complete_stations
,
1540 "<addr> = send SA Query to a station" },
1542 { "wps_pin", hostapd_cli_cmd_wps_pin
, NULL
,
1543 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1544 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin
, NULL
,
1545 "<PIN> = verify PIN checksum" },
1546 { "wps_pbc", hostapd_cli_cmd_wps_pbc
, NULL
,
1547 "= indicate button pushed to initiate PBC" },
1548 { "wps_cancel", hostapd_cli_cmd_wps_cancel
, NULL
,
1549 "= cancel the pending WPS operation" },
1550 #ifdef CONFIG_WPS_NFC
1551 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read
, NULL
,
1552 "<hexdump> = report read NFC tag with WPS data" },
1553 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token
, NULL
,
1554 "<WPS/NDEF> = build NFC configuration token" },
1555 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token
, NULL
,
1556 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1557 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel
, NULL
,
1559 #endif /* CONFIG_WPS_NFC */
1560 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin
, NULL
,
1561 "<cmd> [params..] = enable/disable AP PIN" },
1562 { "wps_config", hostapd_cli_cmd_wps_config
, NULL
,
1563 "<SSID> <auth> <encr> <key> = configure AP" },
1564 { "wps_get_status", hostapd_cli_cmd_wps_get_status
, NULL
,
1565 "= show current WPS status" },
1566 #endif /* CONFIG_WPS */
1567 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent
, NULL
,
1568 "= send Disassociation Imminent notification" },
1569 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc
, NULL
,
1570 "= send ESS Dissassociation Imminent notification" },
1571 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req
, NULL
,
1572 "= send BSS Transition Management Request" },
1573 { "get_config", hostapd_cli_cmd_get_config
, NULL
,
1574 "= show current configuration" },
1575 { "help", hostapd_cli_cmd_help
, hostapd_cli_complete_help
,
1576 "= show this usage help" },
1577 { "interface", hostapd_cli_cmd_interface
, hostapd_complete_interface
,
1578 "[ifname] = show interfaces/select interface" },
1580 { "fst", hostapd_cli_cmd_fst
, NULL
,
1581 "<params...> = send FST-MANAGER control interface command" },
1582 #endif /* CONFIG_FST */
1583 { "raw", hostapd_cli_cmd_raw
, NULL
,
1584 "<params..> = send unprocessed command" },
1585 { "level", hostapd_cli_cmd_level
, NULL
,
1586 "<debug level> = change debug level" },
1587 { "license", hostapd_cli_cmd_license
, NULL
,
1588 "= show full hostapd_cli license" },
1589 { "quit", hostapd_cli_cmd_quit
, NULL
,
1590 "= exit hostapd_cli" },
1591 { "set", hostapd_cli_cmd_set
, hostapd_complete_set
,
1592 "<name> <value> = set runtime variables" },
1593 { "get", hostapd_cli_cmd_get
, hostapd_complete_get
,
1594 "<name> = get runtime info" },
1595 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set
, NULL
,
1596 "<arg,arg,...> = set QoS Map set element" },
1597 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf
,
1598 hostapd_complete_stations
,
1599 "<addr> = send QoS Map Configure frame" },
1600 { "chan_switch", hostapd_cli_cmd_chan_switch
, NULL
,
1601 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1602 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1603 " = initiate channel switch announcement" },
1604 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif
, NULL
,
1606 " = send WNM-Notification Subscription Remediation Request" },
1607 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req
, NULL
,
1608 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1609 " = send WNM-Notification imminent deauthentication indication" },
1610 { "vendor", hostapd_cli_cmd_vendor
, NULL
,
1611 "<vendor id> <sub command id> [<hex formatted data>]\n"
1612 " = send vendor driver command" },
1613 { "enable", hostapd_cli_cmd_enable
, NULL
,
1614 "= enable hostapd on current interface" },
1615 { "reload", hostapd_cli_cmd_reload
, NULL
,
1616 "= reload configuration for current interface" },
1617 { "disable", hostapd_cli_cmd_disable
, NULL
,
1618 "= disable hostapd on current interface" },
1619 { "update_beacon", hostapd_cli_cmd_update_beacon
, NULL
,
1620 "= update Beacon frame contents\n"},
1621 { "erp_flush", hostapd_cli_cmd_erp_flush
, NULL
,
1622 "= drop all ERP keys"},
1623 { "log_level", hostapd_cli_cmd_log_level
, NULL
,
1624 "[level] = show/change log verbosity level" },
1625 { "pmksa", hostapd_cli_cmd_pmksa
, NULL
,
1626 " = show PMKSA cache entries" },
1627 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush
, NULL
,
1628 " = flush PMKSA cache" },
1629 { "set_neighbor", hostapd_cli_cmd_set_neighbor
, NULL
,
1630 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1631 " = add AP to neighbor database" },
1632 { "show_neighbor", hostapd_cli_cmd_show_neighbor
, NULL
,
1633 " = show neighbor database entries" },
1634 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor
, NULL
,
1635 "<addr> [ssid=<hex>] = remove AP from neighbor database" },
1636 { "req_lci", hostapd_cli_cmd_req_lci
, hostapd_complete_stations
,
1637 "<addr> = send LCI request to a station"},
1638 { "req_range", hostapd_cli_cmd_req_range
, NULL
,
1639 " = send FTM range request"},
1640 { "driver_flags", hostapd_cli_cmd_driver_flags
, NULL
,
1641 " = show supported driver flags"},
1643 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code
, NULL
,
1644 "report a scanned DPP URI from a QR Code" },
1645 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen
, NULL
,
1646 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1647 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove
, NULL
,
1648 "*|<id> = remove DPP bootstrap information" },
1649 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri
, NULL
,
1650 "<id> = get DPP bootstrap URI" },
1651 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info
, NULL
,
1652 "<id> = show DPP bootstrap information" },
1653 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init
, NULL
,
1654 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
1655 { "dpp_listen", hostapd_cli_cmd_dpp_listen
, NULL
,
1656 "<freq in MHz> = start DPP listen" },
1657 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen
, NULL
,
1658 "= stop DPP listen" },
1659 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add
, NULL
,
1660 "[curve=..] [key=..] = add DPP configurator" },
1661 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove
,
1663 "*|<id> = remove DPP configurator" },
1664 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key
,
1666 "<id> = Get DPP configurator's private key" },
1667 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign
, NULL
,
1668 "conf=<role> configurator=<id> = generate self DPP configuration" },
1669 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add
, NULL
,
1671 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove
, NULL
,
1672 "*|<id> = remove DPP pkex information" },
1673 #endif /* CONFIG_DPP */
1674 { "accept_acl", hostapd_cli_cmd_accept_macacl
, NULL
,
1675 "=Add/Delete/Show/Clear accept MAC ACL" },
1676 { "deny_acl", hostapd_cli_cmd_deny_macacl
, NULL
,
1677 "=Add/Delete/Show/Clear deny MAC ACL" },
1678 { "poll_sta", hostapd_cli_cmd_poll_sta
, hostapd_complete_stations
,
1679 "<addr> = poll a STA to check connectivity with a QoS null frame" },
1680 { "req_beacon", hostapd_cli_cmd_req_beacon
, NULL
,
1681 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
1682 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk
, NULL
,
1683 "= reload wpa_psk_file only" },
1684 { NULL
, NULL
, NULL
, NULL
}
1689 * Prints command usage, lines are padded with the specified string.
1691 static void print_cmd_help(FILE *stream
, const struct hostapd_cli_cmd
*cmd
,
1697 if (cmd
->usage
== NULL
)
1699 fprintf(stream
, "%s%s ", pad
, cmd
->cmd
);
1700 for (n
= 0; (c
= cmd
->usage
[n
]); n
++) {
1701 fprintf(stream
, "%c", c
);
1703 fprintf(stream
, "%s", pad
);
1705 fprintf(stream
, "\n");
1709 static void print_help(FILE *stream
, const char *cmd
)
1713 fprintf(stream
, "commands:\n");
1714 for (n
= 0; hostapd_cli_commands
[n
].cmd
; n
++) {
1715 if (cmd
== NULL
|| str_starts(hostapd_cli_commands
[n
].cmd
, cmd
))
1716 print_cmd_help(stream
, &hostapd_cli_commands
[n
], " ");
1721 static void wpa_request(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1723 const struct hostapd_cli_cmd
*cmd
, *match
= NULL
;
1727 cmd
= hostapd_cli_commands
;
1729 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) == 0) {
1731 if (os_strcasecmp(cmd
->cmd
, argv
[0]) == 0) {
1732 /* we have an exact match */
1742 printf("Ambiguous command '%s'; possible commands:", argv
[0]);
1743 cmd
= hostapd_cli_commands
;
1745 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) ==
1747 printf(" %s", cmd
->cmd
);
1752 } else if (count
== 0) {
1753 printf("Unknown command '%s'\n", argv
[0]);
1755 match
->handler(ctrl
, argc
- 1, &argv
[1]);
1760 static void cli_event(const char *str
)
1762 const char *start
, *s
;
1764 start
= os_strchr(str
, '>');
1770 if (str_starts(start
, AP_STA_CONNECTED
)) {
1771 s
= os_strchr(start
, ' ');
1774 cli_txt_list_add(&stations
, s
+ 1);
1778 if (str_starts(start
, AP_STA_DISCONNECTED
)) {
1779 s
= os_strchr(start
, ' ');
1782 cli_txt_list_del_addr(&stations
, s
+ 1);
1788 static void hostapd_cli_recv_pending(struct wpa_ctrl
*ctrl
, int in_read
,
1792 if (ctrl_conn
== NULL
)
1794 while (wpa_ctrl_pending(ctrl
)) {
1796 size_t len
= sizeof(buf
) - 1;
1797 if (wpa_ctrl_recv(ctrl
, buf
, &len
) == 0) {
1800 hostapd_cli_action_process(buf
, len
);
1803 if (in_read
&& first
)
1806 printf("%s\n", buf
);
1809 printf("Could not read pending message.\n");
1816 static void hostapd_cli_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
1818 hostapd_cli_recv_pending(ctrl_conn
, 0, 0);
1822 static void hostapd_cli_ping(void *eloop_ctx
, void *timeout_ctx
)
1824 if (ctrl_conn
&& _wpa_ctrl_command(ctrl_conn
, "PING", 0)) {
1825 printf("Connection to hostapd lost - trying to reconnect\n");
1826 hostapd_cli_close_connection();
1828 if (!ctrl_conn
&& hostapd_cli_reconnect(ctrl_ifname
) == 0)
1829 printf("Connection to hostapd re-established\n");
1831 hostapd_cli_recv_pending(ctrl_conn
, 1, 0);
1832 eloop_register_timeout(ping_interval
, 0, hostapd_cli_ping
, NULL
, NULL
);
1836 static void hostapd_cli_eloop_terminate(int sig
, void *signal_ctx
)
1842 static void hostapd_cli_edit_cmd_cb(void *ctx
, char *cmd
)
1844 char *argv
[max_args
];
1846 argc
= tokenize_cmd(cmd
, argv
);
1848 wpa_request(ctrl_conn
, argc
, argv
);
1852 static void hostapd_cli_edit_eof_cb(void *ctx
)
1858 static char ** list_cmd_list(void)
1863 count
= ARRAY_SIZE(hostapd_cli_commands
);
1864 res
= os_calloc(count
+ 1, sizeof(char *));
1868 for (i
= 0; hostapd_cli_commands
[i
].cmd
; i
++) {
1869 res
[i
] = os_strdup(hostapd_cli_commands
[i
].cmd
);
1878 static char ** hostapd_cli_cmd_completion(const char *cmd
, const char *str
,
1883 for (i
= 0; hostapd_cli_commands
[i
].cmd
; i
++) {
1884 if (os_strcasecmp(hostapd_cli_commands
[i
].cmd
, cmd
) != 0)
1886 if (hostapd_cli_commands
[i
].completion
)
1887 return hostapd_cli_commands
[i
].completion(str
, pos
);
1888 if (!hostapd_cli_commands
[i
].usage
)
1891 printf("\r%s\n", hostapd_cli_commands
[i
].usage
);
1900 static char ** hostapd_cli_edit_completion_cb(void *ctx
, const char *str
,
1907 end
= os_strchr(str
, ' ');
1908 if (end
== NULL
|| str
+ pos
< end
)
1909 return list_cmd_list();
1911 cmd
= os_malloc(pos
+ 1);
1914 os_memcpy(cmd
, str
, pos
);
1915 cmd
[end
- str
] = '\0';
1916 res
= hostapd_cli_cmd_completion(cmd
, str
, pos
);
1922 static void hostapd_cli_interactive(void)
1927 printf("\nInteractive mode\n\n");
1929 #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
1930 home
= CONFIG_HOSTAPD_CLI_HISTORY_DIR
;
1931 #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1932 home
= getenv("HOME");
1933 #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1935 const char *fname
= ".hostapd_cli_history";
1936 int hfile_len
= os_strlen(home
) + 1 + os_strlen(fname
) + 1;
1937 hfile
= os_malloc(hfile_len
);
1939 os_snprintf(hfile
, hfile_len
, "%s/%s", home
, fname
);
1942 eloop_register_signal_terminate(hostapd_cli_eloop_terminate
, NULL
);
1943 edit_init(hostapd_cli_edit_cmd_cb
, hostapd_cli_edit_eof_cb
,
1944 hostapd_cli_edit_completion_cb
, NULL
, hfile
, NULL
);
1945 eloop_register_timeout(ping_interval
, 0, hostapd_cli_ping
, NULL
, NULL
);
1949 cli_txt_list_flush(&stations
);
1950 edit_deinit(hfile
, NULL
);
1952 eloop_cancel_timeout(hostapd_cli_ping
, NULL
, NULL
);
1956 static void hostapd_cli_cleanup(void)
1958 hostapd_cli_close_connection();
1960 os_daemonize_terminate(pid_file
);
1962 os_program_deinit();
1966 static void hostapd_cli_action(struct wpa_ctrl
*ctrl
)
1974 fd
= wpa_ctrl_get_fd(ctrl
);
1976 while (!hostapd_cli_quit
) {
1979 tv
.tv_sec
= ping_interval
;
1981 res
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
1982 if (res
< 0 && errno
!= EINTR
) {
1987 if (FD_ISSET(fd
, &rfds
))
1988 hostapd_cli_recv_pending(ctrl
, 0, 1);
1990 len
= sizeof(buf
) - 1;
1991 if (wpa_ctrl_request(ctrl
, "PING", 4, buf
, &len
,
1992 hostapd_cli_action_process
) < 0 ||
1993 len
< 4 || os_memcmp(buf
, "PONG", 4) != 0) {
1994 printf("hostapd did not reply to PING "
1995 "command - exiting\n");
2003 int main(int argc
, char *argv
[])
2005 int warning_displayed
= 0;
2010 if (os_program_init())
2014 c
= getopt(argc
, argv
, "a:BhG:i:p:P:rs:v");
2019 action_file
= optarg
;
2025 ping_interval
= atoi(optarg
);
2031 printf("%s\n", hostapd_cli_version
);
2034 os_free(ctrl_ifname
);
2035 ctrl_ifname
= os_strdup(optarg
);
2038 ctrl_iface_dir
= optarg
;
2047 client_socket_dir
= optarg
;
2055 interactive
= (argc
== optind
) && (action_file
== NULL
);
2058 printf("%s\n\n%s\n\n", hostapd_cli_version
, cli_license
);
2065 if (ctrl_ifname
== NULL
) {
2066 struct dirent
*dent
;
2067 DIR *dir
= opendir(ctrl_iface_dir
);
2069 while ((dent
= readdir(dir
))) {
2070 if (os_strcmp(dent
->d_name
, ".") == 0
2072 os_strcmp(dent
->d_name
, "..") == 0)
2074 printf("Selected interface '%s'\n",
2076 ctrl_ifname
= os_strdup(dent
->d_name
);
2082 hostapd_cli_reconnect(ctrl_ifname
);
2084 if (warning_displayed
)
2085 printf("Connection established.\n");
2088 if (!interactive
&& !reconnect
) {
2089 perror("Failed to connect to hostapd - "
2094 if (!warning_displayed
) {
2095 printf("Could not connect to hostapd - re-trying\n");
2096 warning_displayed
= 1;
2102 if (action_file
&& !hostapd_cli_attached
)
2104 if (daemonize
&& os_daemonize(pid_file
) && eloop_sock_requeue())
2106 if (reconnect
&& action_file
&& ctrl_ifname
) {
2107 while (!hostapd_cli_quit
) {
2109 hostapd_cli_action(ctrl_conn
);
2111 hostapd_cli_reconnect(ctrl_ifname
);
2113 } else if (interactive
)
2114 hostapd_cli_interactive();
2115 else if (action_file
)
2116 hostapd_cli_action(ctrl_conn
);
2118 wpa_request(ctrl_conn
, argc
- optind
, &argv
[optind
]);
2120 unregister_event_handler(ctrl_conn
);
2121 os_free(ctrl_ifname
);
2123 hostapd_cli_cleanup();
2127 #else /* CONFIG_NO_CTRL_IFACE */
2129 int main(int argc
, char *argv
[])
2134 #endif /* CONFIG_NO_CTRL_IFACE */