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>] [-hvB] "
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 " -B run a daemon in the background\n"
72 " -i<ifname> Interface to listen on (default: first "
73 "interface found in the\n"
75 print_help(stderr
, NULL
);
79 static void register_event_handler(struct wpa_ctrl
*ctrl
)
84 event_handler_registered
=
85 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl
),
92 static void unregister_event_handler(struct wpa_ctrl
*ctrl
)
96 if (interactive
&& event_handler_registered
) {
97 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl
));
98 event_handler_registered
= 0;
103 static struct wpa_ctrl
* hostapd_cli_open_connection(const char *ifname
)
105 #ifndef CONFIG_CTRL_IFACE_UDP
108 #endif /* !CONFIG_CTRL_IFACE_UDP */
113 #ifdef CONFIG_CTRL_IFACE_UDP
114 ctrl_conn
= wpa_ctrl_open(ifname
);
116 #else /* CONFIG_CTRL_IFACE_UDP */
117 flen
= strlen(ctrl_iface_dir
) + strlen(ifname
) + 2;
118 cfile
= malloc(flen
);
121 snprintf(cfile
, flen
, "%s/%s", ctrl_iface_dir
, ifname
);
123 if (client_socket_dir
&& client_socket_dir
[0] &&
124 access(client_socket_dir
, F_OK
) < 0) {
125 perror(client_socket_dir
);
130 ctrl_conn
= wpa_ctrl_open2(cfile
, client_socket_dir
);
133 #endif /* CONFIG_CTRL_IFACE_UDP */
137 static void hostapd_cli_close_connection(void)
139 if (ctrl_conn
== NULL
)
142 unregister_event_handler(ctrl_conn
);
143 if (hostapd_cli_attached
) {
144 wpa_ctrl_detach(ctrl_conn
);
145 hostapd_cli_attached
= 0;
147 wpa_ctrl_close(ctrl_conn
);
152 static int hostapd_cli_reconnect(const char *ifname
)
154 char *next_ctrl_ifname
;
156 hostapd_cli_close_connection();
161 next_ctrl_ifname
= os_strdup(ifname
);
162 os_free(ctrl_ifname
);
163 ctrl_ifname
= next_ctrl_ifname
;
167 ctrl_conn
= hostapd_cli_open_connection(ctrl_ifname
);
170 if (!interactive
&& !action_file
)
172 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
173 hostapd_cli_attached
= 1;
174 register_event_handler(ctrl_conn
);
175 update_stations(ctrl_conn
);
177 printf("Warning: Failed to attach to hostapd.\n");
183 static void hostapd_cli_msg_cb(char *msg
, size_t len
)
190 static int _wpa_ctrl_command(struct wpa_ctrl
*ctrl
, const char *cmd
, int print
)
196 if (ctrl_conn
== NULL
) {
197 printf("Not connected to hostapd - command dropped.\n");
200 len
= sizeof(buf
) - 1;
201 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
204 printf("'%s' command timed out.\n", cmd
);
206 } else if (ret
< 0) {
207 printf("'%s' command failed.\n", cmd
);
218 static inline int wpa_ctrl_command(struct wpa_ctrl
*ctrl
, const char *cmd
)
220 return _wpa_ctrl_command(ctrl
, cmd
, 1);
224 static int hostapd_cli_cmd(struct wpa_ctrl
*ctrl
, const char *cmd
,
225 int min_args
, int argc
, char *argv
[])
229 if (argc
< min_args
) {
230 printf("Invalid %s command - at least %d argument%s required.\n",
231 cmd
, min_args
, min_args
> 1 ? "s are" : " is");
234 if (write_cmd(buf
, sizeof(buf
), cmd
, argc
, argv
) < 0)
236 return wpa_ctrl_command(ctrl
, buf
);
240 static int hostapd_cli_cmd_ping(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
242 return wpa_ctrl_command(ctrl
, "PING");
246 static int hostapd_cli_cmd_relog(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
248 return wpa_ctrl_command(ctrl
, "RELOG");
252 static int hostapd_cli_cmd_status(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
254 if (argc
> 0 && os_strcmp(argv
[0], "driver") == 0)
255 return wpa_ctrl_command(ctrl
, "STATUS-DRIVER");
256 return wpa_ctrl_command(ctrl
, "STATUS");
260 static int hostapd_cli_cmd_mib(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
264 os_snprintf(buf
, sizeof(buf
), "MIB %s", argv
[0]);
265 return wpa_ctrl_command(ctrl
, buf
);
267 return wpa_ctrl_command(ctrl
, "MIB");
271 static int hostapd_cli_exec(const char *program
, const char *arg1
,
278 len
= os_strlen(arg1
) + os_strlen(arg2
) + 2;
279 arg
= os_malloc(len
);
282 os_snprintf(arg
, len
, "%s %s", arg1
, arg2
);
283 res
= os_exec(program
, arg
, 1);
290 static void hostapd_cli_action_process(char *msg
, size_t len
)
296 pos
= os_strchr(pos
, '>');
303 hostapd_cli_exec(action_file
, ctrl_ifname
, pos
);
307 static int hostapd_cli_cmd_sta(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
311 printf("Invalid 'sta' command - at least one argument, STA "
312 "address, is required.\n");
316 snprintf(buf
, sizeof(buf
), "STA %s %s", argv
[0], argv
[1]);
318 snprintf(buf
, sizeof(buf
), "STA %s", argv
[0]);
319 return wpa_ctrl_command(ctrl
, buf
);
323 static char ** hostapd_complete_stations(const char *str
, int pos
)
325 int arg
= get_cmd_arg_num(str
, pos
);
330 res
= cli_txt_list_array(&stations
);
338 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl
*ctrl
, int argc
,
343 printf("Invalid 'new_sta' command - exactly one argument, STA "
344 "address, is required.\n");
347 snprintf(buf
, sizeof(buf
), "NEW_STA %s", argv
[0]);
348 return wpa_ctrl_command(ctrl
, buf
);
352 static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl
*ctrl
, int argc
,
357 printf("Invalid 'deauthenticate' command - exactly one "
358 "argument, STA address, is required.\n");
362 os_snprintf(buf
, sizeof(buf
), "DEAUTHENTICATE %s %s",
365 os_snprintf(buf
, sizeof(buf
), "DEAUTHENTICATE %s", argv
[0]);
366 return wpa_ctrl_command(ctrl
, buf
);
370 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl
*ctrl
, int argc
,
375 printf("Invalid 'disassociate' command - exactly one "
376 "argument, STA address, is required.\n");
380 os_snprintf(buf
, sizeof(buf
), "DISASSOCIATE %s %s",
383 os_snprintf(buf
, sizeof(buf
), "DISASSOCIATE %s", argv
[0]);
384 return wpa_ctrl_command(ctrl
, buf
);
388 #ifdef CONFIG_TAXONOMY
389 static int hostapd_cli_cmd_signature(struct wpa_ctrl
*ctrl
, int argc
,
395 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
398 os_snprintf(buf
, sizeof(buf
), "SIGNATURE %s", argv
[0]);
399 return wpa_ctrl_command(ctrl
, buf
);
401 #endif /* CONFIG_TAXONOMY */
404 #ifdef CONFIG_IEEE80211W
405 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl
*ctrl
, int argc
,
410 printf("Invalid 'sa_query' command - exactly one argument, "
411 "STA address, is required.\n");
414 snprintf(buf
, sizeof(buf
), "SA_QUERY %s", argv
[0]);
415 return wpa_ctrl_command(ctrl
, buf
);
417 #endif /* CONFIG_IEEE80211W */
421 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl
*ctrl
, int argc
,
426 printf("Invalid 'wps_pin' command - at least two arguments, "
427 "UUID and PIN, are required.\n");
431 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s %s %s",
432 argv
[0], argv
[1], argv
[2], argv
[3]);
434 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s %s",
435 argv
[0], argv
[1], argv
[2]);
437 snprintf(buf
, sizeof(buf
), "WPS_PIN %s %s", argv
[0], argv
[1]);
438 return wpa_ctrl_command(ctrl
, buf
);
442 static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl
*ctrl
, int argc
,
448 if (argc
!= 1 && argc
!= 2) {
449 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
450 "- PIN to be verified\n");
455 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s %s",
458 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s",
460 if (os_snprintf_error(sizeof(cmd
), res
)) {
461 printf("Too long WPS_CHECK_PIN command.\n");
464 return wpa_ctrl_command(ctrl
, cmd
);
468 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl
*ctrl
, int argc
,
471 return wpa_ctrl_command(ctrl
, "WPS_PBC");
475 static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl
*ctrl
, int argc
,
478 return wpa_ctrl_command(ctrl
, "WPS_CANCEL");
482 #ifdef CONFIG_WPS_NFC
483 static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl
*ctrl
, int argc
,
491 printf("Invalid 'wps_nfc_tag_read' command - one argument "
496 buflen
= 18 + os_strlen(argv
[0]);
497 buf
= os_malloc(buflen
);
500 os_snprintf(buf
, buflen
, "WPS_NFC_TAG_READ %s", argv
[0]);
502 ret
= wpa_ctrl_command(ctrl
, buf
);
509 static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl
*ctrl
,
510 int argc
, char *argv
[])
516 printf("Invalid 'wps_nfc_config_token' command - one argument "
521 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_NFC_CONFIG_TOKEN %s",
523 if (os_snprintf_error(sizeof(cmd
), res
)) {
524 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
527 return wpa_ctrl_command(ctrl
, cmd
);
531 static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl
*ctrl
,
532 int argc
, char *argv
[])
538 printf("Invalid 'wps_nfc_token' command - one argument is "
543 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_NFC_TOKEN %s", argv
[0]);
544 if (os_snprintf_error(sizeof(cmd
), res
)) {
545 printf("Too long WPS_NFC_TOKEN command.\n");
548 return wpa_ctrl_command(ctrl
, cmd
);
552 static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl
*ctrl
,
553 int argc
, char *argv
[])
559 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
564 res
= os_snprintf(cmd
, sizeof(cmd
), "NFC_GET_HANDOVER_SEL %s %s",
566 if (os_snprintf_error(sizeof(cmd
), res
)) {
567 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
570 return wpa_ctrl_command(ctrl
, cmd
);
573 #endif /* CONFIG_WPS_NFC */
576 static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl
*ctrl
, int argc
,
581 printf("Invalid 'wps_ap_pin' command - at least one argument "
586 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s %s %s",
587 argv
[0], argv
[1], argv
[2]);
589 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s %s",
592 snprintf(buf
, sizeof(buf
), "WPS_AP_PIN %s", argv
[0]);
593 return wpa_ctrl_command(ctrl
, buf
);
597 static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl
*ctrl
, int argc
,
600 return wpa_ctrl_command(ctrl
, "WPS_GET_STATUS");
604 static int hostapd_cli_cmd_wps_config(struct wpa_ctrl
*ctrl
, int argc
,
608 char ssid_hex
[2 * SSID_MAX_LEN
+ 1];
609 char key_hex
[2 * 64 + 1];
613 printf("Invalid 'wps_config' command - at least two arguments "
619 for (i
= 0; i
< SSID_MAX_LEN
; i
++) {
620 if (argv
[0][i
] == '\0')
622 os_snprintf(&ssid_hex
[i
* 2], 3, "%02x", argv
[0][i
]);
627 for (i
= 0; i
< 64; i
++) {
628 if (argv
[3][i
] == '\0')
630 os_snprintf(&key_hex
[i
* 2], 3, "%02x",
636 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s %s %s",
637 ssid_hex
, argv
[1], argv
[2], key_hex
);
639 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s %s",
640 ssid_hex
, argv
[1], argv
[2]);
642 snprintf(buf
, sizeof(buf
), "WPS_CONFIG %s %s",
644 return wpa_ctrl_command(ctrl
, buf
);
646 #endif /* CONFIG_WPS */
649 static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl
*ctrl
, int argc
,
656 printf("Invalid 'disassoc_imminent' command - two arguments "
657 "(STA addr and Disassociation Timer) are needed\n");
661 res
= os_snprintf(buf
, sizeof(buf
), "DISASSOC_IMMINENT %s %s",
663 if (os_snprintf_error(sizeof(buf
), res
))
665 return wpa_ctrl_command(ctrl
, buf
);
669 static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl
*ctrl
, int argc
,
676 printf("Invalid 'ess_disassoc' command - three arguments (STA "
677 "addr, disassoc timer, and URL) are needed\n");
681 res
= os_snprintf(buf
, sizeof(buf
), "ESS_DISASSOC %s %s %s",
682 argv
[0], argv
[1], argv
[2]);
683 if (os_snprintf_error(sizeof(buf
), res
))
685 return wpa_ctrl_command(ctrl
, buf
);
689 static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl
*ctrl
, int argc
,
692 char buf
[2000], *tmp
;
696 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
700 res
= os_snprintf(buf
, sizeof(buf
), "BSS_TM_REQ %s", argv
[0]);
701 if (os_snprintf_error(sizeof(buf
), res
))
705 for (i
= 1; i
< argc
; i
++) {
707 res
= os_snprintf(tmp
, sizeof(buf
) - total
, " %s", argv
[i
]);
708 if (os_snprintf_error(sizeof(buf
) - total
, res
))
712 return wpa_ctrl_command(ctrl
, buf
);
716 static int hostapd_cli_cmd_get_config(struct wpa_ctrl
*ctrl
, int argc
,
719 return wpa_ctrl_command(ctrl
, "GET_CONFIG");
723 static int wpa_ctrl_command_sta(struct wpa_ctrl
*ctrl
, const char *cmd
,
724 char *addr
, size_t addr_len
, int print
)
726 char buf
[4096], *pos
;
730 if (ctrl_conn
== NULL
) {
731 printf("Not connected to hostapd - command dropped.\n");
734 len
= sizeof(buf
) - 1;
735 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
738 printf("'%s' command timed out.\n", cmd
);
740 } else if (ret
< 0) {
741 printf("'%s' command failed.\n", cmd
);
746 if (memcmp(buf
, "FAIL", 4) == 0)
752 while (*pos
!= '\0' && *pos
!= '\n')
755 os_strlcpy(addr
, buf
, addr_len
);
760 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl
*ctrl
, int argc
,
763 char addr
[32], cmd
[64];
765 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 1))
768 snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
769 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 1) == 0);
775 static int hostapd_cli_cmd_list_sta(struct wpa_ctrl
*ctrl
, int argc
,
778 char addr
[32], cmd
[64];
780 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 0))
783 if (os_strcmp(addr
, "") != 0)
784 printf("%s\n", addr
);
785 os_snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
786 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 0) == 0);
792 static int hostapd_cli_cmd_help(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
794 print_help(stdout
, argc
> 0 ? argv
[0] : NULL
);
799 static char ** hostapd_cli_complete_help(const char *str
, int pos
)
801 int arg
= get_cmd_arg_num(str
, pos
);
806 res
= list_cmd_list();
814 static int hostapd_cli_cmd_license(struct wpa_ctrl
*ctrl
, int argc
,
817 printf("%s\n\n%s\n", hostapd_cli_version
, cli_full_license
);
822 static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl
*ctrl
,
823 int argc
, char *argv
[])
829 printf("Invalid 'set_qos_map_set' command - "
830 "one argument (comma delimited QoS map set) "
835 res
= os_snprintf(buf
, sizeof(buf
), "SET_QOS_MAP_SET %s", argv
[0]);
836 if (os_snprintf_error(sizeof(buf
), res
))
838 return wpa_ctrl_command(ctrl
, buf
);
842 static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl
*ctrl
,
843 int argc
, char *argv
[])
849 printf("Invalid 'send_qos_map_conf' command - "
850 "one argument (STA addr) is needed\n");
854 res
= os_snprintf(buf
, sizeof(buf
), "SEND_QOS_MAP_CONF %s", argv
[0]);
855 if (os_snprintf_error(sizeof(buf
), res
))
857 return wpa_ctrl_command(ctrl
, buf
);
861 static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl
*ctrl
, int argc
,
868 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
869 "addr and URL) are needed\n");
873 res
= os_snprintf(buf
, sizeof(buf
), "HS20_WNM_NOTIF %s %s",
875 if (os_snprintf_error(sizeof(buf
), res
))
877 return wpa_ctrl_command(ctrl
, buf
);
881 static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl
*ctrl
, int argc
,
888 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
893 res
= os_snprintf(buf
, sizeof(buf
),
894 "HS20_DEAUTH_REQ %s %s %s %s",
895 argv
[0], argv
[1], argv
[2], argv
[3]);
897 res
= os_snprintf(buf
, sizeof(buf
),
898 "HS20_DEAUTH_REQ %s %s %s",
899 argv
[0], argv
[1], argv
[2]);
900 if (os_snprintf_error(sizeof(buf
), res
))
902 return wpa_ctrl_command(ctrl
, buf
);
906 static int hostapd_cli_cmd_quit(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
908 hostapd_cli_quit
= 1;
915 static int hostapd_cli_cmd_level(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
919 printf("Invalid LEVEL command: needs one argument (debug "
923 snprintf(cmd
, sizeof(cmd
), "LEVEL %s", argv
[0]);
924 return wpa_ctrl_command(ctrl
, cmd
);
928 static void update_stations(struct wpa_ctrl
*ctrl
)
930 char addr
[32], cmd
[64];
932 if (!ctrl
|| !interactive
)
935 cli_txt_list_flush(&stations
);
937 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
), 0))
940 if (os_strcmp(addr
, "") != 0)
941 cli_txt_list_add(&stations
, addr
);
942 os_snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
943 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
), 0) == 0);
947 static void hostapd_cli_get_interfaces(struct wpa_ctrl
*ctrl
,
948 struct dl_list
*interfaces
)
953 if (!ctrl
|| !interfaces
)
955 dir
= opendir(ctrl_iface_dir
);
959 while ((dent
= readdir(dir
))) {
960 if (strcmp(dent
->d_name
, ".") == 0 ||
961 strcmp(dent
->d_name
, "..") == 0)
963 cli_txt_list_add(interfaces
, dent
->d_name
);
969 static void hostapd_cli_list_interfaces(struct wpa_ctrl
*ctrl
)
974 dir
= opendir(ctrl_iface_dir
);
976 printf("Control interface directory '%s' could not be "
977 "openned.\n", ctrl_iface_dir
);
981 printf("Available interfaces:\n");
982 while ((dent
= readdir(dir
))) {
983 if (strcmp(dent
->d_name
, ".") == 0 ||
984 strcmp(dent
->d_name
, "..") == 0)
986 printf("%s\n", dent
->d_name
);
992 static int hostapd_cli_cmd_interface(struct wpa_ctrl
*ctrl
, int argc
,
996 hostapd_cli_list_interfaces(ctrl
);
999 if (hostapd_cli_reconnect(argv
[0]) != 0) {
1000 printf("Could not connect to interface '%s' - re-trying\n",
1007 static char ** hostapd_complete_interface(const char *str
, int pos
)
1009 int arg
= get_cmd_arg_num(str
, pos
);
1011 DEFINE_DL_LIST(interfaces
);
1015 hostapd_cli_get_interfaces(ctrl_conn
, &interfaces
);
1016 res
= cli_txt_list_array(&interfaces
);
1017 cli_txt_list_flush(&interfaces
);
1025 static int hostapd_cli_cmd_set(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1031 printf("Invalid SET command: needs two arguments (variable "
1032 "name and value)\n");
1036 res
= os_snprintf(cmd
, sizeof(cmd
), "SET %s %s", argv
[0], argv
[1]);
1037 if (os_snprintf_error(sizeof(cmd
), res
)) {
1038 printf("Too long SET command.\n");
1041 return wpa_ctrl_command(ctrl
, cmd
);
1045 static char ** hostapd_complete_set(const char *str
, int pos
)
1047 int arg
= get_cmd_arg_num(str
, pos
);
1048 const char *fields
[] = {
1049 #ifdef CONFIG_WPS_TESTING
1050 "wps_version_number", "wps_testing_dummy_cred",
1051 "wps_corrupt_pkhash",
1052 #endif /* CONFIG_WPS_TESTING */
1053 #ifdef CONFIG_INTERWORKING
1055 #endif /* CONFIG_INTERWORKING */
1056 #ifdef CONFIG_TESTING_OPTIONS
1057 "ext_mgmt_frame_handling", "ext_eapol_frame_io",
1058 #endif /* CONFIG_TESTING_OPTIONS */
1060 "mbo_assoc_disallow",
1061 #endif /* CONFIG_MBO */
1062 "deny_mac_file", "accept_mac_file",
1064 int i
, num_fields
= ARRAY_SIZE(fields
);
1069 res
= os_calloc(num_fields
+ 1, sizeof(char *));
1072 for (i
= 0; i
< num_fields
; i
++) {
1073 res
[i
] = os_strdup(fields
[i
]);
1083 static int hostapd_cli_cmd_get(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1089 printf("Invalid GET command: needs one argument (variable "
1094 res
= os_snprintf(cmd
, sizeof(cmd
), "GET %s", argv
[0]);
1095 if (os_snprintf_error(sizeof(cmd
), res
)) {
1096 printf("Too long GET command.\n");
1099 return wpa_ctrl_command(ctrl
, cmd
);
1103 static char ** hostapd_complete_get(const char *str
, int pos
)
1105 int arg
= get_cmd_arg_num(str
, pos
);
1106 const char *fields
[] = {
1107 "version", "tls_library",
1109 int i
, num_fields
= ARRAY_SIZE(fields
);
1114 res
= os_calloc(num_fields
+ 1, sizeof(char *));
1117 for (i
= 0; i
< num_fields
; i
++) {
1118 res
[i
] = os_strdup(fields
[i
]);
1129 static int hostapd_cli_cmd_fst(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1137 printf("FST command: parameters are required.\n");
1141 total
= os_snprintf(cmd
, sizeof(cmd
), "FST-MANAGER");
1143 for (i
= 0; i
< argc
; i
++) {
1144 res
= os_snprintf(cmd
+ total
, sizeof(cmd
) - total
, " %s",
1146 if (os_snprintf_error(sizeof(cmd
) - total
, res
)) {
1147 printf("Too long fst command.\n");
1152 return wpa_ctrl_command(ctrl
, cmd
);
1154 #endif /* CONFIG_FST */
1157 static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl
*ctrl
,
1158 int argc
, char *argv
[])
1167 printf("Invalid chan_switch command: needs at least two "
1168 "arguments (count and freq)\n"
1169 "usage: <cs_count> <freq> [sec_channel_offset=] "
1170 "[center_freq1=] [center_freq2=] [bandwidth=] "
1171 "[blocktx] [ht|vht]\n");
1175 res
= os_snprintf(cmd
, sizeof(cmd
), "CHAN_SWITCH %s %s",
1177 if (os_snprintf_error(sizeof(cmd
), res
)) {
1178 printf("Too long CHAN_SWITCH command.\n");
1183 for (i
= 2; i
< argc
; i
++) {
1185 res
= os_snprintf(tmp
, sizeof(cmd
) - total
, " %s", argv
[i
]);
1186 if (os_snprintf_error(sizeof(cmd
) - total
, res
)) {
1187 printf("Too long CHAN_SWITCH command.\n");
1192 return wpa_ctrl_command(ctrl
, cmd
);
1196 static int hostapd_cli_cmd_enable(struct wpa_ctrl
*ctrl
, int argc
,
1199 return wpa_ctrl_command(ctrl
, "ENABLE");
1203 static int hostapd_cli_cmd_reload(struct wpa_ctrl
*ctrl
, int argc
,
1206 return wpa_ctrl_command(ctrl
, "RELOAD");
1210 static int hostapd_cli_cmd_disable(struct wpa_ctrl
*ctrl
, int argc
,
1213 return wpa_ctrl_command(ctrl
, "DISABLE");
1217 static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl
*ctrl
, int argc
,
1220 return wpa_ctrl_command(ctrl
, "UPDATE_BEACON");
1224 static int hostapd_cli_cmd_vendor(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1229 if (argc
< 2 || argc
> 3) {
1230 printf("Invalid vendor command\n"
1231 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1235 res
= os_snprintf(cmd
, sizeof(cmd
), "VENDOR %s %s %s", argv
[0], argv
[1],
1236 argc
== 3 ? argv
[2] : "");
1237 if (os_snprintf_error(sizeof(cmd
), res
)) {
1238 printf("Too long VENDOR command.\n");
1241 return wpa_ctrl_command(ctrl
, cmd
);
1245 static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl
*ctrl
, int argc
,
1248 return wpa_ctrl_command(ctrl
, "ERP_FLUSH");
1252 static int hostapd_cli_cmd_log_level(struct wpa_ctrl
*ctrl
, int argc
,
1258 res
= os_snprintf(cmd
, sizeof(cmd
), "LOG_LEVEL%s%s%s%s",
1259 argc
>= 1 ? " " : "",
1260 argc
>= 1 ? argv
[0] : "",
1261 argc
== 2 ? " " : "",
1262 argc
== 2 ? argv
[1] : "");
1263 if (os_snprintf_error(sizeof(cmd
), res
)) {
1264 printf("Too long option\n");
1267 return wpa_ctrl_command(ctrl
, cmd
);
1271 static int hostapd_cli_cmd_raw(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1275 return hostapd_cli_cmd(ctrl
, argv
[0], 0, argc
- 1, &argv
[1]);
1279 static int hostapd_cli_cmd_pmksa(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1281 return wpa_ctrl_command(ctrl
, "PMKSA");
1285 static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl
*ctrl
, int argc
,
1288 return wpa_ctrl_command(ctrl
, "PMKSA_FLUSH");
1292 static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl
*ctrl
, int argc
,
1298 if (argc
< 3 || argc
> 6) {
1299 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
1303 res
= os_snprintf(cmd
, sizeof(cmd
), "SET_NEIGHBOR %s %s %s %s %s %s",
1304 argv
[0], argv
[1], argv
[2], argc
>= 4 ? argv
[3] : "",
1305 argc
>= 5 ? argv
[4] : "", argc
== 6 ? argv
[5] : "");
1306 if (os_snprintf_error(sizeof(cmd
), res
)) {
1307 printf("Too long SET_NEIGHBOR command.\n");
1310 return wpa_ctrl_command(ctrl
, cmd
);
1314 static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl
*ctrl
, int argc
,
1321 printf("Invalid remove_neighbor command: needs 2 arguments\n");
1325 res
= os_snprintf(cmd
, sizeof(cmd
), "REMOVE_NEIGHBOR %s %s",
1327 if (os_snprintf_error(sizeof(cmd
), res
)) {
1328 printf("Too long REMOVE_NEIGHBOR command.\n");
1331 return wpa_ctrl_command(ctrl
, cmd
);
1335 static int hostapd_cli_cmd_req_lci(struct wpa_ctrl
*ctrl
, int argc
,
1342 printf("Invalid req_lci command - requires destination address\n");
1346 res
= os_snprintf(cmd
, sizeof(cmd
), "REQ_LCI %s", argv
[0]);
1347 if (os_snprintf_error(sizeof(cmd
), res
)) {
1348 printf("Too long REQ_LCI command.\n");
1351 return wpa_ctrl_command(ctrl
, cmd
);
1355 static int hostapd_cli_cmd_req_range(struct wpa_ctrl
*ctrl
, int argc
,
1359 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1363 return hostapd_cli_cmd(ctrl
, "REQ_RANGE", 4, argc
, argv
);
1367 static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl
*ctrl
, int argc
,
1370 return wpa_ctrl_command(ctrl
, "DRIVER_FLAGS");
1376 static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl
*ctrl
, int argc
,
1379 return hostapd_cli_cmd(ctrl
, "DPP_QR_CODE", 1, argc
, argv
);
1383 static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl
*ctrl
, int argc
,
1386 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_GEN", 1, argc
, argv
);
1390 static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl
*ctrl
, int argc
,
1393 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_REMOVE", 1, argc
, argv
);
1397 static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl
*ctrl
,
1398 int argc
, char *argv
[])
1400 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_GET_URI", 1, argc
, argv
);
1404 static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl
*ctrl
, int argc
,
1407 return hostapd_cli_cmd(ctrl
, "DPP_BOOTSTRAP_INFO", 1, argc
, argv
);
1411 static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl
*ctrl
, int argc
,
1414 return hostapd_cli_cmd(ctrl
, "DPP_AUTH_INIT", 1, argc
, argv
);
1418 static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl
*ctrl
, int argc
,
1421 return hostapd_cli_cmd(ctrl
, "DPP_LISTEN", 1, argc
, argv
);
1425 static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl
*ctrl
, int argc
,
1428 return wpa_ctrl_command(ctrl
, "DPP_STOP_LISTEN");
1432 static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl
*ctrl
, int argc
,
1435 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_ADD", 0, argc
, argv
);
1439 static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl
*ctrl
,
1440 int argc
, char *argv
[])
1442 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_REMOVE", 1, argc
, argv
);
1446 static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl
*ctrl
,
1447 int argc
, char *argv
[])
1449 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_GET_KEY", 1, argc
, argv
);
1453 static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl
*ctrl
,
1454 int argc
, char *argv
[])
1456 return hostapd_cli_cmd(ctrl
, "DPP_CONFIGURATOR_SIGN", 1, argc
, argv
);
1460 static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl
*ctrl
, int argc
,
1463 return hostapd_cli_cmd(ctrl
, "DPP_PKEX_ADD", 1, argc
, argv
);
1467 static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl
*ctrl
, int argc
,
1470 return hostapd_cli_cmd(ctrl
, "DPP_PKEX_REMOVE", 1, argc
, argv
);
1473 #endif /* CONFIG_DPP */
1476 static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl
*ctrl
, int argc
,
1479 return hostapd_cli_cmd(ctrl
, "ACCEPT_ACL", 1, argc
, argv
);
1483 static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl
*ctrl
, int argc
,
1486 return hostapd_cli_cmd(ctrl
, "DENY_ACL", 1, argc
, argv
);
1490 static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl
*ctrl
, int argc
,
1493 return hostapd_cli_cmd(ctrl
, "POLL_STA", 1, argc
, argv
);
1497 static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl
*ctrl
, int argc
,
1500 return hostapd_cli_cmd(ctrl
, "REQ_BEACON", 2, argc
, argv
);
1504 static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl
*ctrl
, int argc
,
1507 return wpa_ctrl_command(ctrl
, "RELOAD_WPA_PSK");
1511 struct hostapd_cli_cmd
{
1513 int (*handler
)(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[]);
1514 char ** (*completion
)(const char *str
, int pos
);
1518 static const struct hostapd_cli_cmd hostapd_cli_commands
[] = {
1519 { "ping", hostapd_cli_cmd_ping
, NULL
,
1520 "= pings hostapd" },
1521 { "mib", hostapd_cli_cmd_mib
, NULL
,
1522 "= get MIB variables (dot1x, dot11, radius)" },
1523 { "relog", hostapd_cli_cmd_relog
, NULL
,
1524 "= reload/truncate debug log output file" },
1525 { "status", hostapd_cli_cmd_status
, NULL
,
1526 "= show interface status info" },
1527 { "sta", hostapd_cli_cmd_sta
, hostapd_complete_stations
,
1528 "<addr> = get MIB variables for one station" },
1529 { "all_sta", hostapd_cli_cmd_all_sta
, NULL
,
1530 "= get MIB variables for all stations" },
1531 { "list_sta", hostapd_cli_cmd_list_sta
, NULL
,
1532 "= list all stations" },
1533 { "new_sta", hostapd_cli_cmd_new_sta
, NULL
,
1534 "<addr> = add a new station" },
1535 { "deauthenticate", hostapd_cli_cmd_deauthenticate
,
1536 hostapd_complete_stations
,
1537 "<addr> = deauthenticate a station" },
1538 { "disassociate", hostapd_cli_cmd_disassociate
,
1539 hostapd_complete_stations
,
1540 "<addr> = disassociate a station" },
1541 #ifdef CONFIG_TAXONOMY
1542 { "signature", hostapd_cli_cmd_signature
, hostapd_complete_stations
,
1543 "<addr> = get taxonomy signature for a station" },
1544 #endif /* CONFIG_TAXONOMY */
1545 #ifdef CONFIG_IEEE80211W
1546 { "sa_query", hostapd_cli_cmd_sa_query
, hostapd_complete_stations
,
1547 "<addr> = send SA Query to a station" },
1548 #endif /* CONFIG_IEEE80211W */
1550 { "wps_pin", hostapd_cli_cmd_wps_pin
, NULL
,
1551 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1552 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin
, NULL
,
1553 "<PIN> = verify PIN checksum" },
1554 { "wps_pbc", hostapd_cli_cmd_wps_pbc
, NULL
,
1555 "= indicate button pushed to initiate PBC" },
1556 { "wps_cancel", hostapd_cli_cmd_wps_cancel
, NULL
,
1557 "= cancel the pending WPS operation" },
1558 #ifdef CONFIG_WPS_NFC
1559 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read
, NULL
,
1560 "<hexdump> = report read NFC tag with WPS data" },
1561 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token
, NULL
,
1562 "<WPS/NDEF> = build NFC configuration token" },
1563 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token
, NULL
,
1564 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1565 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel
, NULL
,
1567 #endif /* CONFIG_WPS_NFC */
1568 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin
, NULL
,
1569 "<cmd> [params..] = enable/disable AP PIN" },
1570 { "wps_config", hostapd_cli_cmd_wps_config
, NULL
,
1571 "<SSID> <auth> <encr> <key> = configure AP" },
1572 { "wps_get_status", hostapd_cli_cmd_wps_get_status
, NULL
,
1573 "= show current WPS status" },
1574 #endif /* CONFIG_WPS */
1575 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent
, NULL
,
1576 "= send Disassociation Imminent notification" },
1577 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc
, NULL
,
1578 "= send ESS Dissassociation Imminent notification" },
1579 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req
, NULL
,
1580 "= send BSS Transition Management Request" },
1581 { "get_config", hostapd_cli_cmd_get_config
, NULL
,
1582 "= show current configuration" },
1583 { "help", hostapd_cli_cmd_help
, hostapd_cli_complete_help
,
1584 "= show this usage help" },
1585 { "interface", hostapd_cli_cmd_interface
, hostapd_complete_interface
,
1586 "[ifname] = show interfaces/select interface" },
1588 { "fst", hostapd_cli_cmd_fst
, NULL
,
1589 "<params...> = send FST-MANAGER control interface command" },
1590 #endif /* CONFIG_FST */
1591 { "raw", hostapd_cli_cmd_raw
, NULL
,
1592 "<params..> = send unprocessed command" },
1593 { "level", hostapd_cli_cmd_level
, NULL
,
1594 "<debug level> = change debug level" },
1595 { "license", hostapd_cli_cmd_license
, NULL
,
1596 "= show full hostapd_cli license" },
1597 { "quit", hostapd_cli_cmd_quit
, NULL
,
1598 "= exit hostapd_cli" },
1599 { "set", hostapd_cli_cmd_set
, hostapd_complete_set
,
1600 "<name> <value> = set runtime variables" },
1601 { "get", hostapd_cli_cmd_get
, hostapd_complete_get
,
1602 "<name> = get runtime info" },
1603 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set
, NULL
,
1604 "<arg,arg,...> = set QoS Map set element" },
1605 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf
,
1606 hostapd_complete_stations
,
1607 "<addr> = send QoS Map Configure frame" },
1608 { "chan_switch", hostapd_cli_cmd_chan_switch
, NULL
,
1609 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1610 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1611 " = initiate channel switch announcement" },
1612 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif
, NULL
,
1614 " = send WNM-Notification Subscription Remediation Request" },
1615 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req
, NULL
,
1616 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1617 " = send WNM-Notification imminent deauthentication indication" },
1618 { "vendor", hostapd_cli_cmd_vendor
, NULL
,
1619 "<vendor id> <sub command id> [<hex formatted data>]\n"
1620 " = send vendor driver command" },
1621 { "enable", hostapd_cli_cmd_enable
, NULL
,
1622 "= enable hostapd on current interface" },
1623 { "reload", hostapd_cli_cmd_reload
, NULL
,
1624 "= reload configuration for current interface" },
1625 { "disable", hostapd_cli_cmd_disable
, NULL
,
1626 "= disable hostapd on current interface" },
1627 { "update_beacon", hostapd_cli_cmd_update_beacon
, NULL
,
1628 "= update Beacon frame contents\n"},
1629 { "erp_flush", hostapd_cli_cmd_erp_flush
, NULL
,
1630 "= drop all ERP keys"},
1631 { "log_level", hostapd_cli_cmd_log_level
, NULL
,
1632 "[level] = show/change log verbosity level" },
1633 { "pmksa", hostapd_cli_cmd_pmksa
, NULL
,
1634 " = show PMKSA cache entries" },
1635 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush
, NULL
,
1636 " = flush PMKSA cache" },
1637 { "set_neighbor", hostapd_cli_cmd_set_neighbor
, NULL
,
1638 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1639 " = add AP to neighbor database" },
1640 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor
, NULL
,
1641 "<addr> <ssid=> = remove AP from neighbor database" },
1642 { "req_lci", hostapd_cli_cmd_req_lci
, hostapd_complete_stations
,
1643 "<addr> = send LCI request to a station"},
1644 { "req_range", hostapd_cli_cmd_req_range
, NULL
,
1645 " = send FTM range request"},
1646 { "driver_flags", hostapd_cli_cmd_driver_flags
, NULL
,
1647 " = show supported driver flags"},
1649 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code
, NULL
,
1650 "report a scanned DPP URI from a QR Code" },
1651 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen
, NULL
,
1652 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1653 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove
, NULL
,
1654 "*|<id> = remove DPP bootstrap information" },
1655 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri
, NULL
,
1656 "<id> = get DPP bootstrap URI" },
1657 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info
, NULL
,
1658 "<id> = show DPP bootstrap information" },
1659 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init
, NULL
,
1660 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
1661 { "dpp_listen", hostapd_cli_cmd_dpp_listen
, NULL
,
1662 "<freq in MHz> = start DPP listen" },
1663 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen
, NULL
,
1664 "= stop DPP listen" },
1665 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add
, NULL
,
1666 "[curve=..] [key=..] = add DPP configurator" },
1667 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove
,
1669 "*|<id> = remove DPP configurator" },
1670 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key
,
1672 "<id> = Get DPP configurator's private key" },
1673 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign
, NULL
,
1674 "conf=<role> configurator=<id> = generate self DPP configuration" },
1675 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add
, NULL
,
1677 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove
, NULL
,
1678 "*|<id> = remove DPP pkex information" },
1679 #endif /* CONFIG_DPP */
1680 { "accept_acl", hostapd_cli_cmd_accept_macacl
, NULL
,
1681 "=Add/Delete/Show/Clear accept MAC ACL" },
1682 { "deny_acl", hostapd_cli_cmd_deny_macacl
, NULL
,
1683 "=Add/Delete/Show/Clear deny MAC ACL" },
1684 { "poll_sta", hostapd_cli_cmd_poll_sta
, hostapd_complete_stations
,
1685 "<addr> = poll a STA to check connectivity with a QoS null frame" },
1686 { "req_beacon", hostapd_cli_cmd_req_beacon
, NULL
,
1687 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
1688 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk
, NULL
,
1689 "= reload wpa_psk_file only" },
1690 { NULL
, NULL
, NULL
, NULL
}
1695 * Prints command usage, lines are padded with the specified string.
1697 static void print_cmd_help(FILE *stream
, const struct hostapd_cli_cmd
*cmd
,
1703 if (cmd
->usage
== NULL
)
1705 fprintf(stream
, "%s%s ", pad
, cmd
->cmd
);
1706 for (n
= 0; (c
= cmd
->usage
[n
]); n
++) {
1707 fprintf(stream
, "%c", c
);
1709 fprintf(stream
, "%s", pad
);
1711 fprintf(stream
, "\n");
1715 static void print_help(FILE *stream
, const char *cmd
)
1719 fprintf(stream
, "commands:\n");
1720 for (n
= 0; hostapd_cli_commands
[n
].cmd
; n
++) {
1721 if (cmd
== NULL
|| str_starts(hostapd_cli_commands
[n
].cmd
, cmd
))
1722 print_cmd_help(stream
, &hostapd_cli_commands
[n
], " ");
1727 static void wpa_request(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1729 const struct hostapd_cli_cmd
*cmd
, *match
= NULL
;
1733 cmd
= hostapd_cli_commands
;
1735 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) == 0) {
1737 if (os_strcasecmp(cmd
->cmd
, argv
[0]) == 0) {
1738 /* we have an exact match */
1748 printf("Ambiguous command '%s'; possible commands:", argv
[0]);
1749 cmd
= hostapd_cli_commands
;
1751 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) ==
1753 printf(" %s", cmd
->cmd
);
1758 } else if (count
== 0) {
1759 printf("Unknown command '%s'\n", argv
[0]);
1761 match
->handler(ctrl
, argc
- 1, &argv
[1]);
1766 static void cli_event(const char *str
)
1768 const char *start
, *s
;
1770 start
= os_strchr(str
, '>');
1776 if (str_starts(start
, AP_STA_CONNECTED
)) {
1777 s
= os_strchr(start
, ' ');
1780 cli_txt_list_add(&stations
, s
+ 1);
1784 if (str_starts(start
, AP_STA_DISCONNECTED
)) {
1785 s
= os_strchr(start
, ' ');
1788 cli_txt_list_del_addr(&stations
, s
+ 1);
1794 static void hostapd_cli_recv_pending(struct wpa_ctrl
*ctrl
, int in_read
,
1798 if (ctrl_conn
== NULL
)
1800 while (wpa_ctrl_pending(ctrl
)) {
1802 size_t len
= sizeof(buf
) - 1;
1803 if (wpa_ctrl_recv(ctrl
, buf
, &len
) == 0) {
1806 hostapd_cli_action_process(buf
, len
);
1809 if (in_read
&& first
)
1812 printf("%s\n", buf
);
1815 printf("Could not read pending message.\n");
1822 static void hostapd_cli_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
1824 hostapd_cli_recv_pending(ctrl_conn
, 0, 0);
1828 static void hostapd_cli_ping(void *eloop_ctx
, void *timeout_ctx
)
1830 if (ctrl_conn
&& _wpa_ctrl_command(ctrl_conn
, "PING", 0)) {
1831 printf("Connection to hostapd lost - trying to reconnect\n");
1832 hostapd_cli_close_connection();
1834 if (!ctrl_conn
&& hostapd_cli_reconnect(ctrl_ifname
) == 0)
1835 printf("Connection to hostapd re-established\n");
1837 hostapd_cli_recv_pending(ctrl_conn
, 1, 0);
1838 eloop_register_timeout(ping_interval
, 0, hostapd_cli_ping
, NULL
, NULL
);
1842 static void hostapd_cli_eloop_terminate(int sig
, void *signal_ctx
)
1848 static void hostapd_cli_edit_cmd_cb(void *ctx
, char *cmd
)
1850 char *argv
[max_args
];
1852 argc
= tokenize_cmd(cmd
, argv
);
1854 wpa_request(ctrl_conn
, argc
, argv
);
1858 static void hostapd_cli_edit_eof_cb(void *ctx
)
1864 static char ** list_cmd_list(void)
1869 count
= ARRAY_SIZE(hostapd_cli_commands
);
1870 res
= os_calloc(count
+ 1, sizeof(char *));
1874 for (i
= 0; hostapd_cli_commands
[i
].cmd
; i
++) {
1875 res
[i
] = os_strdup(hostapd_cli_commands
[i
].cmd
);
1884 static char ** hostapd_cli_cmd_completion(const char *cmd
, const char *str
,
1889 for (i
= 0; hostapd_cli_commands
[i
].cmd
; i
++) {
1890 if (os_strcasecmp(hostapd_cli_commands
[i
].cmd
, cmd
) != 0)
1892 if (hostapd_cli_commands
[i
].completion
)
1893 return hostapd_cli_commands
[i
].completion(str
, pos
);
1894 if (!hostapd_cli_commands
[i
].usage
)
1897 printf("\r%s\n", hostapd_cli_commands
[i
].usage
);
1906 static char ** hostapd_cli_edit_completion_cb(void *ctx
, const char *str
,
1913 end
= os_strchr(str
, ' ');
1914 if (end
== NULL
|| str
+ pos
< end
)
1915 return list_cmd_list();
1917 cmd
= os_malloc(pos
+ 1);
1920 os_memcpy(cmd
, str
, pos
);
1921 cmd
[end
- str
] = '\0';
1922 res
= hostapd_cli_cmd_completion(cmd
, str
, pos
);
1928 static void hostapd_cli_interactive(void)
1933 printf("\nInteractive mode\n\n");
1935 #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
1936 home
= CONFIG_HOSTAPD_CLI_HISTORY_DIR
;
1937 #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1938 home
= getenv("HOME");
1939 #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1941 const char *fname
= ".hostapd_cli_history";
1942 int hfile_len
= os_strlen(home
) + 1 + os_strlen(fname
) + 1;
1943 hfile
= os_malloc(hfile_len
);
1945 os_snprintf(hfile
, hfile_len
, "%s/%s", home
, fname
);
1948 eloop_register_signal_terminate(hostapd_cli_eloop_terminate
, NULL
);
1949 edit_init(hostapd_cli_edit_cmd_cb
, hostapd_cli_edit_eof_cb
,
1950 hostapd_cli_edit_completion_cb
, NULL
, hfile
, NULL
);
1951 eloop_register_timeout(ping_interval
, 0, hostapd_cli_ping
, NULL
, NULL
);
1955 cli_txt_list_flush(&stations
);
1956 edit_deinit(hfile
, NULL
);
1958 eloop_cancel_timeout(hostapd_cli_ping
, NULL
, NULL
);
1962 static void hostapd_cli_cleanup(void)
1964 hostapd_cli_close_connection();
1966 os_daemonize_terminate(pid_file
);
1968 os_program_deinit();
1972 static void hostapd_cli_action(struct wpa_ctrl
*ctrl
)
1980 fd
= wpa_ctrl_get_fd(ctrl
);
1982 while (!hostapd_cli_quit
) {
1985 tv
.tv_sec
= ping_interval
;
1987 res
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
1988 if (res
< 0 && errno
!= EINTR
) {
1993 if (FD_ISSET(fd
, &rfds
))
1994 hostapd_cli_recv_pending(ctrl
, 0, 1);
1996 len
= sizeof(buf
) - 1;
1997 if (wpa_ctrl_request(ctrl
, "PING", 4, buf
, &len
,
1998 hostapd_cli_action_process
) < 0 ||
1999 len
< 4 || os_memcmp(buf
, "PONG", 4) != 0) {
2000 printf("hostapd did not reply to PING "
2001 "command - exiting\n");
2009 int main(int argc
, char *argv
[])
2011 int warning_displayed
= 0;
2015 if (os_program_init())
2019 c
= getopt(argc
, argv
, "a:BhG:i:p:P:s:v");
2024 action_file
= optarg
;
2030 ping_interval
= atoi(optarg
);
2036 printf("%s\n", hostapd_cli_version
);
2039 os_free(ctrl_ifname
);
2040 ctrl_ifname
= os_strdup(optarg
);
2043 ctrl_iface_dir
= optarg
;
2049 client_socket_dir
= optarg
;
2057 interactive
= (argc
== optind
) && (action_file
== NULL
);
2060 printf("%s\n\n%s\n\n", hostapd_cli_version
, cli_license
);
2067 if (ctrl_ifname
== NULL
) {
2068 struct dirent
*dent
;
2069 DIR *dir
= opendir(ctrl_iface_dir
);
2071 while ((dent
= readdir(dir
))) {
2072 if (os_strcmp(dent
->d_name
, ".") == 0
2074 os_strcmp(dent
->d_name
, "..") == 0)
2076 printf("Selected interface '%s'\n",
2078 ctrl_ifname
= os_strdup(dent
->d_name
);
2084 hostapd_cli_reconnect(ctrl_ifname
);
2086 if (warning_displayed
)
2087 printf("Connection established.\n");
2092 perror("Failed to connect to hostapd - "
2097 if (!warning_displayed
) {
2098 printf("Could not connect to hostapd - re-trying\n");
2099 warning_displayed
= 1;
2105 if (action_file
&& !hostapd_cli_attached
)
2107 if (daemonize
&& os_daemonize(pid_file
) && eloop_sock_requeue())
2111 hostapd_cli_interactive();
2112 else if (action_file
)
2113 hostapd_cli_action(ctrl_conn
);
2115 wpa_request(ctrl_conn
, argc
- optind
, &argv
[optind
]);
2117 unregister_event_handler(ctrl_conn
);
2118 os_free(ctrl_ifname
);
2120 hostapd_cli_cleanup();
2124 #else /* CONFIG_NO_CTRL_IFACE */
2126 int main(int argc
, char *argv
[])
2131 #endif /* CONFIG_NO_CTRL_IFACE */