Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd - command line interface for hostapd daemon |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3 | * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | #include <dirent.h> |
| 11 | |
| 12 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 13 | #include "common/ieee802_11_defs.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 14 | #include "utils/common.h" |
| 15 | #include "utils/eloop.h" |
| 16 | #include "utils/edit.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #include "common/version.h" |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 18 | #include "common/cli.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 19 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 20 | #ifndef CONFIG_NO_CTRL_IFACE |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 21 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 22 | static const char *const hostapd_cli_version = |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 23 | "hostapd_cli v" VERSION_STR "\n" |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 24 | "Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi> and contributors"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 25 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | static struct wpa_ctrl *ctrl_conn; |
| 27 | static int hostapd_cli_quit = 0; |
| 28 | static int hostapd_cli_attached = 0; |
Jeff Johnson | 205f214 | 2012-09-03 22:12:17 -0700 | [diff] [blame] | 29 | |
| 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; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 34 | static const char *client_socket_dir = NULL; |
Jeff Johnson | 205f214 | 2012-09-03 22:12:17 -0700 | [diff] [blame] | 35 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 40 | static int interactive = 0; |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 41 | static int event_handler_registered = 0; |
| 42 | |
| 43 | static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */ |
| 44 | |
| 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); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 48 | static void update_stations(struct wpa_ctrl *ctrl); |
| 49 | static void cli_event(const char *str); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | |
| 51 | |
| 52 | static void usage(void) |
| 53 | { |
| 54 | fprintf(stderr, "%s\n", hostapd_cli_version); |
| 55 | fprintf(stderr, |
| 56 | "\n" |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 57 | "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvBr] " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 58 | "[-a<path>] \\\n" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 59 | " [-P<pid file>] [-G<ping interval>] [command..]\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 60 | "\n" |
| 61 | "Options:\n" |
| 62 | " -h help (show this usage text)\n" |
| 63 | " -v shown version information\n" |
| 64 | " -p<path> path to find control sockets (default: " |
| 65 | "/var/run/hostapd)\n" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 66 | " -s<dir_path> dir path to open client sockets (default: " |
| 67 | CONFIG_CTRL_IFACE_DIR ")\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 68 | " -a<file> run in daemon mode executing the action file " |
| 69 | "based on events\n" |
| 70 | " from hostapd\n" |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 71 | " -r try to reconnect when client socket is " |
| 72 | "disconnected.\n" |
| 73 | " This is useful only when used with -a.\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 74 | " -B run a daemon in the background\n" |
| 75 | " -i<ifname> Interface to listen on (default: first " |
| 76 | "interface found in the\n" |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 77 | " socket path)\n\n"); |
| 78 | print_help(stderr, NULL); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void register_event_handler(struct wpa_ctrl *ctrl) |
| 83 | { |
| 84 | if (!ctrl_conn) |
| 85 | return; |
| 86 | if (interactive) { |
| 87 | event_handler_registered = |
| 88 | !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl), |
| 89 | hostapd_cli_receive, |
| 90 | NULL, NULL); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void unregister_event_handler(struct wpa_ctrl *ctrl) |
| 96 | { |
| 97 | if (!ctrl_conn) |
| 98 | return; |
| 99 | if (interactive && event_handler_registered) { |
| 100 | eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl)); |
| 101 | event_handler_registered = 0; |
| 102 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname) |
| 107 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 108 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 109 | char *cfile; |
| 110 | int flen; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 111 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | |
| 113 | if (ifname == NULL) |
| 114 | return NULL; |
| 115 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 116 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 117 | ctrl_conn = wpa_ctrl_open(ifname); |
| 118 | return ctrl_conn; |
| 119 | #else /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 120 | flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; |
| 121 | cfile = malloc(flen); |
| 122 | if (cfile == NULL) |
| 123 | return NULL; |
| 124 | snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); |
| 125 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 126 | if (client_socket_dir && client_socket_dir[0] && |
| 127 | access(client_socket_dir, F_OK) < 0) { |
| 128 | perror(client_socket_dir); |
| 129 | free(cfile); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 134 | free(cfile); |
| 135 | return ctrl_conn; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 136 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | static void hostapd_cli_close_connection(void) |
| 141 | { |
| 142 | if (ctrl_conn == NULL) |
| 143 | return; |
| 144 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 145 | unregister_event_handler(ctrl_conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 146 | if (hostapd_cli_attached) { |
| 147 | wpa_ctrl_detach(ctrl_conn); |
| 148 | hostapd_cli_attached = 0; |
| 149 | } |
| 150 | wpa_ctrl_close(ctrl_conn); |
| 151 | ctrl_conn = NULL; |
| 152 | } |
| 153 | |
| 154 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 155 | static int hostapd_cli_reconnect(const char *ifname) |
| 156 | { |
| 157 | char *next_ctrl_ifname; |
| 158 | |
| 159 | hostapd_cli_close_connection(); |
| 160 | |
| 161 | if (!ifname) |
| 162 | return -1; |
| 163 | |
| 164 | next_ctrl_ifname = os_strdup(ifname); |
| 165 | os_free(ctrl_ifname); |
| 166 | ctrl_ifname = next_ctrl_ifname; |
| 167 | if (!ctrl_ifname) |
| 168 | return -1; |
| 169 | |
| 170 | ctrl_conn = hostapd_cli_open_connection(ctrl_ifname); |
| 171 | if (!ctrl_conn) |
| 172 | return -1; |
| 173 | if (!interactive && !action_file) |
| 174 | return 0; |
| 175 | if (wpa_ctrl_attach(ctrl_conn) == 0) { |
| 176 | hostapd_cli_attached = 1; |
| 177 | register_event_handler(ctrl_conn); |
| 178 | update_stations(ctrl_conn); |
| 179 | } else { |
| 180 | printf("Warning: Failed to attach to hostapd.\n"); |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 186 | static void hostapd_cli_msg_cb(char *msg, size_t len) |
| 187 | { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 188 | cli_event(msg); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 189 | printf("%s\n", msg); |
| 190 | } |
| 191 | |
| 192 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 193 | static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 194 | { |
| 195 | char buf[4096]; |
| 196 | size_t len; |
| 197 | int ret; |
| 198 | |
| 199 | if (ctrl_conn == NULL) { |
| 200 | printf("Not connected to hostapd - command dropped.\n"); |
| 201 | return -1; |
| 202 | } |
| 203 | len = sizeof(buf) - 1; |
| 204 | ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, |
| 205 | hostapd_cli_msg_cb); |
| 206 | if (ret == -2) { |
| 207 | printf("'%s' command timed out.\n", cmd); |
| 208 | return -2; |
| 209 | } else if (ret < 0) { |
| 210 | printf("'%s' command failed.\n", cmd); |
| 211 | return -1; |
| 212 | } |
| 213 | if (print) { |
| 214 | buf[len] = '\0'; |
| 215 | printf("%s", buf); |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 221 | static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 222 | { |
| 223 | return _wpa_ctrl_command(ctrl, cmd, 1); |
| 224 | } |
| 225 | |
| 226 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 227 | static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, |
| 228 | int min_args, int argc, char *argv[]) |
| 229 | { |
| 230 | char buf[4096]; |
| 231 | |
| 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"); |
| 235 | return -1; |
| 236 | } |
| 237 | if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0) |
| 238 | return -1; |
| 239 | return wpa_ctrl_command(ctrl, buf); |
| 240 | } |
| 241 | |
| 242 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 243 | static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 244 | { |
| 245 | return wpa_ctrl_command(ctrl, "PING"); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 250 | { |
| 251 | return wpa_ctrl_command(ctrl, "RELOG"); |
| 252 | } |
| 253 | |
| 254 | |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 255 | static int hostapd_cli_cmd_close_log(struct wpa_ctrl *ctrl, int argc, |
| 256 | char *argv[]) |
| 257 | { |
| 258 | return wpa_ctrl_command(ctrl, "CLOSE_LOG"); |
| 259 | } |
| 260 | |
| 261 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 262 | static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 263 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 264 | if (argc > 0 && os_strcmp(argv[0], "driver") == 0) |
| 265 | return wpa_ctrl_command(ctrl, "STATUS-DRIVER"); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 266 | return wpa_ctrl_command(ctrl, "STATUS"); |
| 267 | } |
| 268 | |
| 269 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 270 | static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 271 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 272 | if (argc > 0) { |
| 273 | char buf[100]; |
| 274 | os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]); |
| 275 | return wpa_ctrl_command(ctrl, buf); |
| 276 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 277 | return wpa_ctrl_command(ctrl, "MIB"); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | static int hostapd_cli_exec(const char *program, const char *arg1, |
| 282 | const char *arg2) |
| 283 | { |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 284 | char *arg; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | size_t len; |
| 286 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 287 | |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 288 | len = os_strlen(arg1) + os_strlen(arg2) + 2; |
| 289 | arg = os_malloc(len); |
| 290 | if (arg == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | return -1; |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 292 | os_snprintf(arg, len, "%s %s", arg1, arg2); |
| 293 | res = os_exec(program, arg, 1); |
| 294 | os_free(arg); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 295 | |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 296 | return res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
| 300 | static void hostapd_cli_action_process(char *msg, size_t len) |
| 301 | { |
| 302 | const char *pos; |
| 303 | |
| 304 | pos = msg; |
| 305 | if (*pos == '<') { |
| 306 | pos = os_strchr(pos, '>'); |
| 307 | if (pos) |
| 308 | pos++; |
| 309 | else |
| 310 | pos = msg; |
| 311 | } |
| 312 | |
| 313 | hostapd_cli_exec(action_file, ctrl_ifname, pos); |
| 314 | } |
| 315 | |
| 316 | |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 317 | static void hostapd_cli_action_cb(char *msg, size_t len) |
| 318 | { |
| 319 | hostapd_cli_action_process(msg, len); |
| 320 | } |
| 321 | |
| 322 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 323 | static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 324 | { |
| 325 | char buf[64]; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 326 | if (argc < 1) { |
| 327 | printf("Invalid 'sta' command - at least one argument, STA " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 328 | "address, is required.\n"); |
| 329 | return -1; |
| 330 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 331 | if (argc > 1) |
| 332 | snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]); |
| 333 | else |
| 334 | snprintf(buf, sizeof(buf), "STA %s", argv[0]); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 335 | return wpa_ctrl_command(ctrl, buf); |
| 336 | } |
| 337 | |
| 338 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 339 | static char ** hostapd_complete_stations(const char *str, int pos) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 340 | { |
| 341 | int arg = get_cmd_arg_num(str, pos); |
| 342 | char **res = NULL; |
| 343 | |
| 344 | switch (arg) { |
| 345 | case 1: |
| 346 | res = cli_txt_list_array(&stations); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | return res; |
| 351 | } |
| 352 | |
| 353 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 354 | static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc, |
| 355 | char *argv[]) |
| 356 | { |
| 357 | char buf[64]; |
| 358 | if (argc != 1) { |
| 359 | printf("Invalid 'new_sta' command - exactly one argument, STA " |
| 360 | "address, is required.\n"); |
| 361 | return -1; |
| 362 | } |
| 363 | snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]); |
| 364 | return wpa_ctrl_command(ctrl, buf); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc, |
| 369 | char *argv[]) |
| 370 | { |
| 371 | char buf[64]; |
| 372 | if (argc < 1) { |
| 373 | printf("Invalid 'deauthenticate' command - exactly one " |
| 374 | "argument, STA address, is required.\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | if (argc > 1) |
| 378 | os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s", |
| 379 | argv[0], argv[1]); |
| 380 | else |
| 381 | os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]); |
| 382 | return wpa_ctrl_command(ctrl, buf); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc, |
| 387 | char *argv[]) |
| 388 | { |
| 389 | char buf[64]; |
| 390 | if (argc < 1) { |
| 391 | printf("Invalid 'disassociate' command - exactly one " |
| 392 | "argument, STA address, is required.\n"); |
| 393 | return -1; |
| 394 | } |
| 395 | if (argc > 1) |
| 396 | os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s", |
| 397 | argv[0], argv[1]); |
| 398 | else |
| 399 | os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]); |
| 400 | return wpa_ctrl_command(ctrl, buf); |
| 401 | } |
| 402 | |
| 403 | |
Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 404 | #ifdef CONFIG_TAXONOMY |
| 405 | static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc, |
| 406 | char *argv[]) |
| 407 | { |
| 408 | char buf[64]; |
| 409 | |
| 410 | if (argc != 1) { |
| 411 | printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n"); |
| 412 | return -1; |
| 413 | } |
| 414 | os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]); |
| 415 | return wpa_ctrl_command(ctrl, buf); |
| 416 | } |
| 417 | #endif /* CONFIG_TAXONOMY */ |
| 418 | |
| 419 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 420 | static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc, |
| 421 | char *argv[]) |
| 422 | { |
| 423 | char buf[64]; |
| 424 | if (argc != 1) { |
| 425 | printf("Invalid 'sa_query' command - exactly one argument, " |
| 426 | "STA address, is required.\n"); |
| 427 | return -1; |
| 428 | } |
| 429 | snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]); |
| 430 | return wpa_ctrl_command(ctrl, buf); |
| 431 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 432 | |
| 433 | |
| 434 | #ifdef CONFIG_WPS |
| 435 | static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, |
| 436 | char *argv[]) |
| 437 | { |
| 438 | char buf[256]; |
| 439 | if (argc < 2) { |
| 440 | printf("Invalid 'wps_pin' command - at least two arguments, " |
| 441 | "UUID and PIN, are required.\n"); |
| 442 | return -1; |
| 443 | } |
| 444 | if (argc > 3) |
| 445 | snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s", |
| 446 | argv[0], argv[1], argv[2], argv[3]); |
| 447 | else if (argc > 2) |
| 448 | snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s", |
| 449 | argv[0], argv[1], argv[2]); |
| 450 | else |
| 451 | snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]); |
| 452 | return wpa_ctrl_command(ctrl, buf); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc, |
| 457 | char *argv[]) |
| 458 | { |
| 459 | char cmd[256]; |
| 460 | int res; |
| 461 | |
| 462 | if (argc != 1 && argc != 2) { |
| 463 | printf("Invalid WPS_CHECK_PIN command: needs one argument:\n" |
| 464 | "- PIN to be verified\n"); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | if (argc == 2) |
| 469 | res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s", |
| 470 | argv[0], argv[1]); |
| 471 | else |
| 472 | res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s", |
| 473 | argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 474 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 475 | printf("Too long WPS_CHECK_PIN command.\n"); |
| 476 | return -1; |
| 477 | } |
| 478 | return wpa_ctrl_command(ctrl, cmd); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, |
| 483 | char *argv[]) |
| 484 | { |
| 485 | return wpa_ctrl_command(ctrl, "WPS_PBC"); |
| 486 | } |
| 487 | |
| 488 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 489 | static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc, |
| 490 | char *argv[]) |
| 491 | { |
| 492 | return wpa_ctrl_command(ctrl, "WPS_CANCEL"); |
| 493 | } |
| 494 | |
| 495 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 496 | #ifdef CONFIG_WPS_NFC |
| 497 | static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc, |
| 498 | char *argv[]) |
| 499 | { |
| 500 | int ret; |
| 501 | char *buf; |
| 502 | size_t buflen; |
| 503 | |
| 504 | if (argc != 1) { |
| 505 | printf("Invalid 'wps_nfc_tag_read' command - one argument " |
| 506 | "is required.\n"); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | buflen = 18 + os_strlen(argv[0]); |
| 511 | buf = os_malloc(buflen); |
| 512 | if (buf == NULL) |
| 513 | return -1; |
| 514 | os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]); |
| 515 | |
| 516 | ret = wpa_ctrl_command(ctrl, buf); |
| 517 | os_free(buf); |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, |
| 524 | int argc, char *argv[]) |
| 525 | { |
| 526 | char cmd[64]; |
| 527 | int res; |
| 528 | |
| 529 | if (argc != 1) { |
| 530 | printf("Invalid 'wps_nfc_config_token' command - one argument " |
| 531 | "is required.\n"); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s", |
| 536 | argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 537 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 538 | printf("Too long WPS_NFC_CONFIG_TOKEN command.\n"); |
| 539 | return -1; |
| 540 | } |
| 541 | return wpa_ctrl_command(ctrl, cmd); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, |
| 546 | int argc, char *argv[]) |
| 547 | { |
| 548 | char cmd[64]; |
| 549 | int res; |
| 550 | |
| 551 | if (argc != 1) { |
| 552 | printf("Invalid 'wps_nfc_token' command - one argument is " |
| 553 | "required.\n"); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 558 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 559 | printf("Too long WPS_NFC_TOKEN command.\n"); |
| 560 | return -1; |
| 561 | } |
| 562 | return wpa_ctrl_command(ctrl, cmd); |
| 563 | } |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 564 | |
| 565 | |
| 566 | static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, |
| 567 | int argc, char *argv[]) |
| 568 | { |
| 569 | char cmd[64]; |
| 570 | int res; |
| 571 | |
| 572 | if (argc != 2) { |
| 573 | printf("Invalid 'nfc_get_handover_sel' command - two arguments " |
| 574 | "are required.\n"); |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s", |
| 579 | argv[0], argv[1]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 580 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 581 | printf("Too long NFC_GET_HANDOVER_SEL command.\n"); |
| 582 | return -1; |
| 583 | } |
| 584 | return wpa_ctrl_command(ctrl, cmd); |
| 585 | } |
| 586 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 587 | #endif /* CONFIG_WPS_NFC */ |
| 588 | |
| 589 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 590 | static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc, |
| 591 | char *argv[]) |
| 592 | { |
| 593 | char buf[64]; |
| 594 | if (argc < 1) { |
| 595 | printf("Invalid 'wps_ap_pin' command - at least one argument " |
| 596 | "is required.\n"); |
| 597 | return -1; |
| 598 | } |
| 599 | if (argc > 2) |
| 600 | snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s", |
| 601 | argv[0], argv[1], argv[2]); |
| 602 | else if (argc > 1) |
| 603 | snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s", |
| 604 | argv[0], argv[1]); |
| 605 | else |
| 606 | snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]); |
| 607 | return wpa_ctrl_command(ctrl, buf); |
| 608 | } |
| 609 | |
| 610 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 611 | static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc, |
| 612 | char *argv[]) |
| 613 | { |
| 614 | return wpa_ctrl_command(ctrl, "WPS_GET_STATUS"); |
| 615 | } |
| 616 | |
| 617 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 618 | static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc, |
| 619 | char *argv[]) |
| 620 | { |
| 621 | char buf[256]; |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 622 | char ssid_hex[2 * SSID_MAX_LEN + 1]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 623 | char key_hex[2 * 64 + 1]; |
| 624 | int i; |
| 625 | |
| 626 | if (argc < 1) { |
| 627 | printf("Invalid 'wps_config' command - at least two arguments " |
| 628 | "are required.\n"); |
| 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | ssid_hex[0] = '\0'; |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 633 | for (i = 0; i < SSID_MAX_LEN; i++) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 634 | if (argv[0][i] == '\0') |
| 635 | break; |
| 636 | os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]); |
| 637 | } |
| 638 | |
| 639 | key_hex[0] = '\0'; |
| 640 | if (argc > 3) { |
| 641 | for (i = 0; i < 64; i++) { |
| 642 | if (argv[3][i] == '\0') |
| 643 | break; |
| 644 | os_snprintf(&key_hex[i * 2], 3, "%02x", |
| 645 | argv[3][i]); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | if (argc > 3) |
| 650 | snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s", |
| 651 | ssid_hex, argv[1], argv[2], key_hex); |
| 652 | else if (argc > 2) |
| 653 | snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s", |
| 654 | ssid_hex, argv[1], argv[2]); |
| 655 | else |
| 656 | snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s", |
| 657 | ssid_hex, argv[1]); |
| 658 | return wpa_ctrl_command(ctrl, buf); |
| 659 | } |
| 660 | #endif /* CONFIG_WPS */ |
| 661 | |
| 662 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 663 | static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc, |
| 664 | char *argv[]) |
| 665 | { |
| 666 | char buf[300]; |
| 667 | int res; |
| 668 | |
| 669 | if (argc < 2) { |
| 670 | printf("Invalid 'disassoc_imminent' command - two arguments " |
| 671 | "(STA addr and Disassociation Timer) are needed\n"); |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s", |
| 676 | argv[0], argv[1]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 677 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 678 | return -1; |
| 679 | return wpa_ctrl_command(ctrl, buf); |
| 680 | } |
| 681 | |
| 682 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 683 | static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc, |
| 684 | char *argv[]) |
| 685 | { |
| 686 | char buf[300]; |
| 687 | int res; |
| 688 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 689 | if (argc < 3) { |
| 690 | printf("Invalid 'ess_disassoc' command - three arguments (STA " |
| 691 | "addr, disassoc timer, and URL) are needed\n"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 692 | return -1; |
| 693 | } |
| 694 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 695 | res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s", |
| 696 | argv[0], argv[1], argv[2]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 697 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 698 | return -1; |
| 699 | return wpa_ctrl_command(ctrl, buf); |
| 700 | } |
| 701 | |
| 702 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 703 | static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc, |
| 704 | char *argv[]) |
| 705 | { |
| 706 | char buf[2000], *tmp; |
| 707 | int res, i, total; |
| 708 | |
| 709 | if (argc < 1) { |
| 710 | printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n"); |
| 711 | return -1; |
| 712 | } |
| 713 | |
| 714 | res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]); |
| 715 | if (os_snprintf_error(sizeof(buf), res)) |
| 716 | return -1; |
| 717 | |
| 718 | total = res; |
| 719 | for (i = 1; i < argc; i++) { |
| 720 | tmp = &buf[total]; |
| 721 | res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]); |
| 722 | if (os_snprintf_error(sizeof(buf) - total, res)) |
| 723 | return -1; |
| 724 | total += res; |
| 725 | } |
| 726 | return wpa_ctrl_command(ctrl, buf); |
| 727 | } |
| 728 | |
| 729 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 730 | static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc, |
| 731 | char *argv[]) |
| 732 | { |
| 733 | return wpa_ctrl_command(ctrl, "GET_CONFIG"); |
| 734 | } |
| 735 | |
| 736 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 737 | static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd, |
| 738 | char *addr, size_t addr_len, int print) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 739 | { |
| 740 | char buf[4096], *pos; |
| 741 | size_t len; |
| 742 | int ret; |
| 743 | |
| 744 | if (ctrl_conn == NULL) { |
| 745 | printf("Not connected to hostapd - command dropped.\n"); |
| 746 | return -1; |
| 747 | } |
| 748 | len = sizeof(buf) - 1; |
| 749 | ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, |
| 750 | hostapd_cli_msg_cb); |
| 751 | if (ret == -2) { |
| 752 | printf("'%s' command timed out.\n", cmd); |
| 753 | return -2; |
| 754 | } else if (ret < 0) { |
| 755 | printf("'%s' command failed.\n", cmd); |
| 756 | return -1; |
| 757 | } |
| 758 | |
| 759 | buf[len] = '\0'; |
| 760 | if (memcmp(buf, "FAIL", 4) == 0) |
| 761 | return -1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 762 | if (print) |
| 763 | printf("%s", buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 764 | |
| 765 | pos = buf; |
| 766 | while (*pos != '\0' && *pos != '\n') |
| 767 | pos++; |
| 768 | *pos = '\0'; |
| 769 | os_strlcpy(addr, buf, addr_len); |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, |
| 775 | char *argv[]) |
| 776 | { |
| 777 | char addr[32], cmd[64]; |
| 778 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 779 | if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 780 | return 0; |
| 781 | do { |
| 782 | snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 783 | } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 784 | |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 789 | static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc, |
| 790 | char *argv[]) |
| 791 | { |
| 792 | char addr[32], cmd[64]; |
| 793 | |
| 794 | if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0)) |
| 795 | return 0; |
| 796 | do { |
| 797 | if (os_strcmp(addr, "") != 0) |
| 798 | printf("%s\n", addr); |
| 799 | os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); |
| 800 | } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 806 | static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 807 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 808 | print_help(stdout, argc > 0 ? argv[0] : NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 813 | static char ** hostapd_cli_complete_help(const char *str, int pos) |
| 814 | { |
| 815 | int arg = get_cmd_arg_num(str, pos); |
| 816 | char **res = NULL; |
| 817 | |
| 818 | switch (arg) { |
| 819 | case 1: |
| 820 | res = list_cmd_list(); |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | return res; |
| 825 | } |
| 826 | |
| 827 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 828 | static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, |
| 829 | char *argv[]) |
| 830 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 831 | printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 836 | static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl, |
| 837 | int argc, char *argv[]) |
| 838 | { |
| 839 | char buf[200]; |
| 840 | int res; |
| 841 | |
| 842 | if (argc != 1) { |
| 843 | printf("Invalid 'set_qos_map_set' command - " |
| 844 | "one argument (comma delimited QoS map set) " |
| 845 | "is needed\n"); |
| 846 | return -1; |
| 847 | } |
| 848 | |
| 849 | res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 850 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 851 | return -1; |
| 852 | return wpa_ctrl_command(ctrl, buf); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl, |
| 857 | int argc, char *argv[]) |
| 858 | { |
| 859 | char buf[50]; |
| 860 | int res; |
| 861 | |
| 862 | if (argc != 1) { |
| 863 | printf("Invalid 'send_qos_map_conf' command - " |
| 864 | "one argument (STA addr) is needed\n"); |
| 865 | return -1; |
| 866 | } |
| 867 | |
| 868 | res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 869 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 870 | return -1; |
| 871 | return wpa_ctrl_command(ctrl, buf); |
| 872 | } |
| 873 | |
| 874 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 875 | static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc, |
| 876 | char *argv[]) |
| 877 | { |
| 878 | char buf[300]; |
| 879 | int res; |
| 880 | |
| 881 | if (argc < 2) { |
| 882 | printf("Invalid 'hs20_wnm_notif' command - two arguments (STA " |
| 883 | "addr and URL) are needed\n"); |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s", |
| 888 | argv[0], argv[1]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 889 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 890 | return -1; |
| 891 | return wpa_ctrl_command(ctrl, buf); |
| 892 | } |
| 893 | |
| 894 | |
| 895 | static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc, |
| 896 | char *argv[]) |
| 897 | { |
| 898 | char buf[300]; |
| 899 | int res; |
| 900 | |
| 901 | if (argc < 3) { |
| 902 | printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n"); |
| 903 | return -1; |
| 904 | } |
| 905 | |
| 906 | if (argc > 3) |
| 907 | res = os_snprintf(buf, sizeof(buf), |
| 908 | "HS20_DEAUTH_REQ %s %s %s %s", |
| 909 | argv[0], argv[1], argv[2], argv[3]); |
| 910 | else |
| 911 | res = os_snprintf(buf, sizeof(buf), |
| 912 | "HS20_DEAUTH_REQ %s %s %s", |
| 913 | argv[0], argv[1], argv[2]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 914 | if (os_snprintf_error(sizeof(buf), res)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 915 | return -1; |
| 916 | return wpa_ctrl_command(ctrl, buf); |
| 917 | } |
| 918 | |
| 919 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 920 | static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 921 | { |
| 922 | hostapd_cli_quit = 1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 923 | if (interactive) |
| 924 | eloop_terminate(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 930 | { |
| 931 | char cmd[256]; |
| 932 | if (argc != 1) { |
| 933 | printf("Invalid LEVEL command: needs one argument (debug " |
| 934 | "level)\n"); |
| 935 | return 0; |
| 936 | } |
| 937 | snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]); |
| 938 | return wpa_ctrl_command(ctrl, cmd); |
| 939 | } |
| 940 | |
| 941 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 942 | static void update_stations(struct wpa_ctrl *ctrl) |
| 943 | { |
| 944 | char addr[32], cmd[64]; |
| 945 | |
| 946 | if (!ctrl || !interactive) |
| 947 | return; |
| 948 | |
| 949 | cli_txt_list_flush(&stations); |
| 950 | |
| 951 | if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0)) |
| 952 | return; |
| 953 | do { |
| 954 | if (os_strcmp(addr, "") != 0) |
| 955 | cli_txt_list_add(&stations, addr); |
| 956 | os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); |
| 957 | } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0); |
| 958 | } |
| 959 | |
| 960 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 961 | static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl, |
| 962 | struct dl_list *interfaces) |
| 963 | { |
| 964 | struct dirent *dent; |
| 965 | DIR *dir; |
| 966 | |
| 967 | if (!ctrl || !interfaces) |
| 968 | return; |
| 969 | dir = opendir(ctrl_iface_dir); |
| 970 | if (dir == NULL) |
| 971 | return; |
| 972 | |
| 973 | while ((dent = readdir(dir))) { |
| 974 | if (strcmp(dent->d_name, ".") == 0 || |
| 975 | strcmp(dent->d_name, "..") == 0) |
| 976 | continue; |
| 977 | cli_txt_list_add(interfaces, dent->d_name); |
| 978 | } |
| 979 | closedir(dir); |
| 980 | } |
| 981 | |
| 982 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 983 | static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl) |
| 984 | { |
| 985 | struct dirent *dent; |
| 986 | DIR *dir; |
| 987 | |
| 988 | dir = opendir(ctrl_iface_dir); |
| 989 | if (dir == NULL) { |
| 990 | printf("Control interface directory '%s' could not be " |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 991 | "opened.\n", ctrl_iface_dir); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 992 | return; |
| 993 | } |
| 994 | |
| 995 | printf("Available interfaces:\n"); |
| 996 | while ((dent = readdir(dir))) { |
| 997 | if (strcmp(dent->d_name, ".") == 0 || |
| 998 | strcmp(dent->d_name, "..") == 0) |
| 999 | continue; |
| 1000 | printf("%s\n", dent->d_name); |
| 1001 | } |
| 1002 | closedir(dir); |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, |
| 1007 | char *argv[]) |
| 1008 | { |
| 1009 | if (argc < 1) { |
| 1010 | hostapd_cli_list_interfaces(ctrl); |
| 1011 | return 0; |
| 1012 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1013 | if (hostapd_cli_reconnect(argv[0]) != 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1014 | printf("Could not connect to interface '%s' - re-trying\n", |
| 1015 | ctrl_ifname); |
| 1016 | } |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1021 | static char ** hostapd_complete_interface(const char *str, int pos) |
| 1022 | { |
| 1023 | int arg = get_cmd_arg_num(str, pos); |
| 1024 | char **res = NULL; |
| 1025 | DEFINE_DL_LIST(interfaces); |
| 1026 | |
| 1027 | switch (arg) { |
| 1028 | case 1: |
| 1029 | hostapd_cli_get_interfaces(ctrl_conn, &interfaces); |
| 1030 | res = cli_txt_list_array(&interfaces); |
| 1031 | cli_txt_list_flush(&interfaces); |
| 1032 | break; |
| 1033 | } |
| 1034 | |
| 1035 | return res; |
| 1036 | } |
| 1037 | |
| 1038 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1039 | static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1040 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1041 | char cmd[2048]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1042 | int res; |
| 1043 | |
| 1044 | if (argc != 2) { |
| 1045 | printf("Invalid SET command: needs two arguments (variable " |
| 1046 | "name and value)\n"); |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
| 1050 | res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1051 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1052 | printf("Too long SET command.\n"); |
| 1053 | return -1; |
| 1054 | } |
| 1055 | return wpa_ctrl_command(ctrl, cmd); |
| 1056 | } |
| 1057 | |
| 1058 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1059 | static char ** hostapd_complete_set(const char *str, int pos) |
| 1060 | { |
| 1061 | int arg = get_cmd_arg_num(str, pos); |
| 1062 | const char *fields[] = { |
| 1063 | #ifdef CONFIG_WPS_TESTING |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1064 | "wps_version_number", "wps_testing_stub_cred", |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1065 | "wps_corrupt_pkhash", |
| 1066 | #endif /* CONFIG_WPS_TESTING */ |
| 1067 | #ifdef CONFIG_INTERWORKING |
| 1068 | "gas_frag_limit", |
| 1069 | #endif /* CONFIG_INTERWORKING */ |
| 1070 | #ifdef CONFIG_TESTING_OPTIONS |
| 1071 | "ext_mgmt_frame_handling", "ext_eapol_frame_io", |
| 1072 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 1073 | #ifdef CONFIG_MBO |
| 1074 | "mbo_assoc_disallow", |
| 1075 | #endif /* CONFIG_MBO */ |
| 1076 | "deny_mac_file", "accept_mac_file", |
| 1077 | }; |
| 1078 | int i, num_fields = ARRAY_SIZE(fields); |
| 1079 | |
| 1080 | if (arg == 1) { |
| 1081 | char **res; |
| 1082 | |
| 1083 | res = os_calloc(num_fields + 1, sizeof(char *)); |
| 1084 | if (!res) |
| 1085 | return NULL; |
| 1086 | for (i = 0; i < num_fields; i++) { |
| 1087 | res[i] = os_strdup(fields[i]); |
| 1088 | if (!res[i]) |
| 1089 | return res; |
| 1090 | } |
| 1091 | return res; |
| 1092 | } |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1097 | static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1098 | { |
| 1099 | char cmd[256]; |
| 1100 | int res; |
| 1101 | |
| 1102 | if (argc != 1) { |
| 1103 | printf("Invalid GET command: needs one argument (variable " |
| 1104 | "name)\n"); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | |
| 1108 | res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1109 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1110 | printf("Too long GET command.\n"); |
| 1111 | return -1; |
| 1112 | } |
| 1113 | return wpa_ctrl_command(ctrl, cmd); |
| 1114 | } |
| 1115 | |
| 1116 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1117 | static char ** hostapd_complete_get(const char *str, int pos) |
| 1118 | { |
| 1119 | int arg = get_cmd_arg_num(str, pos); |
| 1120 | const char *fields[] = { |
| 1121 | "version", "tls_library", |
| 1122 | }; |
| 1123 | int i, num_fields = ARRAY_SIZE(fields); |
| 1124 | |
| 1125 | if (arg == 1) { |
| 1126 | char **res; |
| 1127 | |
| 1128 | res = os_calloc(num_fields + 1, sizeof(char *)); |
| 1129 | if (!res) |
| 1130 | return NULL; |
| 1131 | for (i = 0; i < num_fields; i++) { |
| 1132 | res[i] = os_strdup(fields[i]); |
| 1133 | if (!res[i]) |
| 1134 | return res; |
| 1135 | } |
| 1136 | return res; |
| 1137 | } |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | |
| 1141 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1142 | #ifdef CONFIG_FST |
| 1143 | static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1144 | { |
| 1145 | char cmd[256]; |
| 1146 | int res; |
| 1147 | int i; |
| 1148 | int total; |
| 1149 | |
| 1150 | if (argc <= 0) { |
| 1151 | printf("FST command: parameters are required.\n"); |
| 1152 | return -1; |
| 1153 | } |
| 1154 | |
| 1155 | total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER"); |
| 1156 | |
| 1157 | for (i = 0; i < argc; i++) { |
| 1158 | res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s", |
| 1159 | argv[i]); |
| 1160 | if (os_snprintf_error(sizeof(cmd) - total, res)) { |
| 1161 | printf("Too long fst command.\n"); |
| 1162 | return -1; |
| 1163 | } |
| 1164 | total += res; |
| 1165 | } |
| 1166 | return wpa_ctrl_command(ctrl, cmd); |
| 1167 | } |
| 1168 | #endif /* CONFIG_FST */ |
| 1169 | |
| 1170 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 1171 | static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl, |
| 1172 | int argc, char *argv[]) |
| 1173 | { |
| 1174 | char cmd[256]; |
| 1175 | int res; |
| 1176 | int i; |
| 1177 | char *tmp; |
| 1178 | int total; |
| 1179 | |
| 1180 | if (argc < 2) { |
| 1181 | printf("Invalid chan_switch command: needs at least two " |
| 1182 | "arguments (count and freq)\n" |
| 1183 | "usage: <cs_count> <freq> [sec_channel_offset=] " |
| 1184 | "[center_freq1=] [center_freq2=] [bandwidth=] " |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1185 | "[blocktx] [ht|vht|he|eht]\n"); |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s", |
| 1190 | argv[0], argv[1]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1191 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 1192 | printf("Too long CHAN_SWITCH command.\n"); |
| 1193 | return -1; |
| 1194 | } |
| 1195 | |
| 1196 | total = res; |
| 1197 | for (i = 2; i < argc; i++) { |
| 1198 | tmp = cmd + total; |
| 1199 | res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1200 | if (os_snprintf_error(sizeof(cmd) - total, res)) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 1201 | printf("Too long CHAN_SWITCH command.\n"); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | total += res; |
| 1205 | } |
| 1206 | return wpa_ctrl_command(ctrl, cmd); |
| 1207 | } |
| 1208 | |
| 1209 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1210 | static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc, |
| 1211 | char *argv[]) |
| 1212 | { |
| 1213 | return wpa_ctrl_command(ctrl, "ENABLE"); |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc, |
| 1218 | char *argv[]) |
| 1219 | { |
| 1220 | return wpa_ctrl_command(ctrl, "RELOAD"); |
| 1221 | } |
| 1222 | |
| 1223 | |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1224 | static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc, |
| 1225 | char *argv[]) |
| 1226 | { |
| 1227 | return wpa_ctrl_command(ctrl, "RELOAD_BSS"); |
| 1228 | } |
| 1229 | |
| 1230 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1231 | static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc, |
| 1232 | char *argv[]) |
| 1233 | { |
| 1234 | return wpa_ctrl_command(ctrl, "DISABLE"); |
| 1235 | } |
| 1236 | |
| 1237 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1238 | static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc, |
| 1239 | char *argv[]) |
| 1240 | { |
| 1241 | return wpa_ctrl_command(ctrl, "UPDATE_BEACON"); |
| 1242 | } |
| 1243 | |
| 1244 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1245 | static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1246 | { |
| 1247 | char cmd[256]; |
| 1248 | int res; |
| 1249 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1250 | if (argc < 2 || argc > 4) { |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1251 | printf("Invalid vendor command\n" |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1252 | "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n"); |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1253 | return -1; |
| 1254 | } |
| 1255 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1256 | res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0], |
| 1257 | argv[1], argc >= 3 ? argv[2] : "", |
| 1258 | argc == 4 ? " " : "", argc == 4 ? argv[3] : ""); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1259 | if (os_snprintf_error(sizeof(cmd), res)) { |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1260 | printf("Too long VENDOR command.\n"); |
| 1261 | return -1; |
| 1262 | } |
| 1263 | return wpa_ctrl_command(ctrl, cmd); |
| 1264 | } |
| 1265 | |
| 1266 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1267 | static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc, |
| 1268 | char *argv[]) |
| 1269 | { |
| 1270 | return wpa_ctrl_command(ctrl, "ERP_FLUSH"); |
| 1271 | } |
| 1272 | |
| 1273 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1274 | static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, |
| 1275 | char *argv[]) |
| 1276 | { |
| 1277 | char cmd[256]; |
| 1278 | int res; |
| 1279 | |
| 1280 | res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s", |
| 1281 | argc >= 1 ? " " : "", |
| 1282 | argc >= 1 ? argv[0] : "", |
| 1283 | argc == 2 ? " " : "", |
| 1284 | argc == 2 ? argv[1] : ""); |
| 1285 | if (os_snprintf_error(sizeof(cmd), res)) { |
| 1286 | printf("Too long option\n"); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | return wpa_ctrl_command(ctrl, cmd); |
| 1290 | } |
| 1291 | |
| 1292 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1293 | static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1294 | { |
| 1295 | if (argc == 0) |
| 1296 | return -1; |
| 1297 | return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]); |
| 1298 | } |
| 1299 | |
| 1300 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1301 | static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1302 | { |
| 1303 | return wpa_ctrl_command(ctrl, "PMKSA"); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc, |
| 1308 | char *argv[]) |
| 1309 | { |
| 1310 | return wpa_ctrl_command(ctrl, "PMKSA_FLUSH"); |
| 1311 | } |
| 1312 | |
| 1313 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1314 | static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc, |
| 1315 | char *argv[]) |
| 1316 | { |
| 1317 | char cmd[2048]; |
| 1318 | int res; |
| 1319 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1320 | if (argc < 3 || argc > 6) { |
| 1321 | printf("Invalid set_neighbor command: needs 3-6 arguments\n"); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1322 | return -1; |
| 1323 | } |
| 1324 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1325 | res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s", |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1326 | argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "", |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1327 | argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : ""); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1328 | if (os_snprintf_error(sizeof(cmd), res)) { |
| 1329 | printf("Too long SET_NEIGHBOR command.\n"); |
| 1330 | return -1; |
| 1331 | } |
| 1332 | return wpa_ctrl_command(ctrl, cmd); |
| 1333 | } |
| 1334 | |
| 1335 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1336 | static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc, |
| 1337 | char *argv[]) |
| 1338 | { |
| 1339 | return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR"); |
| 1340 | } |
| 1341 | |
| 1342 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1343 | static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc, |
| 1344 | char *argv[]) |
| 1345 | { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1346 | return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc, |
| 1351 | char *argv[]) |
| 1352 | { |
| 1353 | char cmd[256]; |
| 1354 | int res; |
| 1355 | |
| 1356 | if (argc != 1) { |
| 1357 | printf("Invalid req_lci command - requires destination address\n"); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | |
| 1361 | res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]); |
| 1362 | if (os_snprintf_error(sizeof(cmd), res)) { |
| 1363 | printf("Too long REQ_LCI command.\n"); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | return wpa_ctrl_command(ctrl, cmd); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc, |
| 1371 | char *argv[]) |
| 1372 | { |
| 1373 | if (argc < 4) { |
| 1374 | printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n"); |
| 1375 | return -1; |
| 1376 | } |
| 1377 | |
| 1378 | return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv); |
| 1379 | } |
| 1380 | |
| 1381 | |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1382 | static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc, |
| 1383 | char *argv[]) |
| 1384 | { |
| 1385 | return wpa_ctrl_command(ctrl, "DRIVER_FLAGS"); |
| 1386 | } |
| 1387 | |
| 1388 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1389 | #ifdef CONFIG_DPP |
| 1390 | |
| 1391 | static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc, |
| 1392 | char *argv[]) |
| 1393 | { |
| 1394 | return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv); |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc, |
| 1399 | char *argv[]) |
| 1400 | { |
| 1401 | return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc, |
| 1406 | char *argv[]) |
| 1407 | { |
| 1408 | return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv); |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl, |
| 1413 | int argc, char *argv[]) |
| 1414 | { |
| 1415 | return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv); |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc, |
| 1420 | char *argv[]) |
| 1421 | { |
| 1422 | return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv); |
| 1423 | } |
| 1424 | |
| 1425 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1426 | static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc, |
| 1427 | char *argv[]) |
| 1428 | { |
| 1429 | return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv); |
| 1430 | } |
| 1431 | |
| 1432 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1433 | static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc, |
| 1434 | char *argv[]) |
| 1435 | { |
| 1436 | return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv); |
| 1437 | } |
| 1438 | |
| 1439 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1440 | static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc, |
| 1441 | char *argv[]) |
| 1442 | { |
| 1443 | return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv); |
| 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc, |
| 1448 | char *argv[]) |
| 1449 | { |
| 1450 | return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN"); |
| 1451 | } |
| 1452 | |
| 1453 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1454 | static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc, |
| 1455 | char *argv[]) |
| 1456 | { |
| 1457 | return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv); |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl, |
| 1462 | int argc, char *argv[]) |
| 1463 | { |
| 1464 | return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv); |
| 1465 | } |
| 1466 | |
| 1467 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1468 | static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl, |
| 1469 | int argc, char *argv[]) |
| 1470 | { |
| 1471 | return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv); |
| 1472 | } |
| 1473 | |
| 1474 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1475 | static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl, |
| 1476 | int argc, char *argv[]) |
| 1477 | { |
| 1478 | return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv); |
| 1479 | } |
| 1480 | |
| 1481 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1482 | static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc, |
| 1483 | char *argv[]) |
| 1484 | { |
| 1485 | return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv); |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc, |
| 1490 | char *argv[]) |
| 1491 | { |
| 1492 | return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv); |
| 1493 | } |
| 1494 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1495 | |
| 1496 | #ifdef CONFIG_DPP2 |
| 1497 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1498 | static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc, |
| 1499 | char *argv[]) |
| 1500 | { |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1501 | return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc, |
| 1506 | char *argv[]) |
| 1507 | { |
| 1508 | return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP"); |
| 1509 | } |
| 1510 | |
| 1511 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1512 | static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc, |
| 1513 | char *argv[]) |
| 1514 | { |
| 1515 | return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv); |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc, |
| 1520 | char *argv[]) |
| 1521 | { |
| 1522 | return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP"); |
| 1523 | } |
| 1524 | |
| 1525 | #endif /* CONFIG_DPP2 */ |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1526 | |
| 1527 | |
| 1528 | #ifdef CONFIG_DPP3 |
| 1529 | static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc, |
| 1530 | char *argv[]) |
| 1531 | { |
| 1532 | return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 1, argc, argv); |
| 1533 | } |
| 1534 | #endif /* CONFIG_DPP3 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1535 | #endif /* CONFIG_DPP */ |
| 1536 | |
| 1537 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1538 | static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc, |
| 1539 | char *argv[]) |
| 1540 | { |
| 1541 | return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv); |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc, |
| 1546 | char *argv[]) |
| 1547 | { |
| 1548 | return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv); |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc, |
| 1553 | char *argv[]) |
| 1554 | { |
| 1555 | return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv); |
| 1556 | } |
| 1557 | |
| 1558 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1559 | static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc, |
| 1560 | char *argv[]) |
| 1561 | { |
| 1562 | return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv); |
| 1563 | } |
| 1564 | |
| 1565 | |
| 1566 | static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc, |
| 1567 | char *argv[]) |
| 1568 | { |
| 1569 | return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK"); |
| 1570 | } |
| 1571 | |
| 1572 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1573 | #ifdef ANDROID |
| 1574 | static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1575 | { |
| 1576 | return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv); |
| 1577 | } |
| 1578 | #endif /* ANDROID */ |
| 1579 | |
| 1580 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1581 | struct hostapd_cli_cmd { |
| 1582 | const char *cmd; |
| 1583 | int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]); |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1584 | char ** (*completion)(const char *str, int pos); |
| 1585 | const char *usage; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1586 | }; |
| 1587 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1588 | static const struct hostapd_cli_cmd hostapd_cli_commands[] = { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1589 | { "ping", hostapd_cli_cmd_ping, NULL, |
| 1590 | "= pings hostapd" }, |
| 1591 | { "mib", hostapd_cli_cmd_mib, NULL, |
| 1592 | "= get MIB variables (dot1x, dot11, radius)" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1593 | { "relog", hostapd_cli_cmd_relog, NULL, |
| 1594 | "= reload/truncate debug log output file" }, |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1595 | { "close_log", hostapd_cli_cmd_close_log, NULL, |
| 1596 | "= disable debug log output file" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1597 | { "status", hostapd_cli_cmd_status, NULL, |
| 1598 | "= show interface status info" }, |
| 1599 | { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1600 | "<addr> = get MIB variables for one station" }, |
| 1601 | { "all_sta", hostapd_cli_cmd_all_sta, NULL, |
| 1602 | "= get MIB variables for all stations" }, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1603 | { "list_sta", hostapd_cli_cmd_list_sta, NULL, |
| 1604 | "= list all stations" }, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1605 | { "new_sta", hostapd_cli_cmd_new_sta, NULL, |
| 1606 | "<addr> = add a new station" }, |
| 1607 | { "deauthenticate", hostapd_cli_cmd_deauthenticate, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1608 | hostapd_complete_stations, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1609 | "<addr> = deauthenticate a station" }, |
| 1610 | { "disassociate", hostapd_cli_cmd_disassociate, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1611 | hostapd_complete_stations, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1612 | "<addr> = disassociate a station" }, |
Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 1613 | #ifdef CONFIG_TAXONOMY |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1614 | { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations, |
Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 1615 | "<addr> = get taxonomy signature for a station" }, |
| 1616 | #endif /* CONFIG_TAXONOMY */ |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1617 | { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1618 | "<addr> = send SA Query to a station" }, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1619 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1620 | { "wps_pin", hostapd_cli_cmd_wps_pin, NULL, |
| 1621 | "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" }, |
| 1622 | { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL, |
| 1623 | "<PIN> = verify PIN checksum" }, |
| 1624 | { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL, |
| 1625 | "= indicate button pushed to initiate PBC" }, |
| 1626 | { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL, |
| 1627 | "= cancel the pending WPS operation" }, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1628 | #ifdef CONFIG_WPS_NFC |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1629 | { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL, |
| 1630 | "<hexdump> = report read NFC tag with WPS data" }, |
| 1631 | { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL, |
| 1632 | "<WPS/NDEF> = build NFC configuration token" }, |
| 1633 | { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL, |
| 1634 | "<WPS/NDEF/enable/disable> = manager NFC password token" }, |
| 1635 | { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL, |
| 1636 | NULL }, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1637 | #endif /* CONFIG_WPS_NFC */ |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1638 | { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL, |
| 1639 | "<cmd> [params..] = enable/disable AP PIN" }, |
| 1640 | { "wps_config", hostapd_cli_cmd_wps_config, NULL, |
| 1641 | "<SSID> <auth> <encr> <key> = configure AP" }, |
| 1642 | { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL, |
| 1643 | "= show current WPS status" }, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1644 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1645 | { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, |
| 1646 | "= send Disassociation Imminent notification" }, |
| 1647 | { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, |
| 1648 | "= send ESS Dissassociation Imminent notification" }, |
| 1649 | { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, |
| 1650 | "= send BSS Transition Management Request" }, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1651 | { "get_config", hostapd_cli_cmd_get_config, NULL, |
| 1652 | "= show current configuration" }, |
| 1653 | { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help, |
| 1654 | "= show this usage help" }, |
| 1655 | { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface, |
| 1656 | "[ifname] = show interfaces/select interface" }, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1657 | #ifdef CONFIG_FST |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1658 | { "fst", hostapd_cli_cmd_fst, NULL, |
| 1659 | "<params...> = send FST-MANAGER control interface command" }, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1660 | #endif /* CONFIG_FST */ |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1661 | { "raw", hostapd_cli_cmd_raw, NULL, |
| 1662 | "<params..> = send unprocessed command" }, |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1663 | { "level", hostapd_cli_cmd_level, NULL, |
| 1664 | "<debug level> = change debug level" }, |
| 1665 | { "license", hostapd_cli_cmd_license, NULL, |
| 1666 | "= show full hostapd_cli license" }, |
| 1667 | { "quit", hostapd_cli_cmd_quit, NULL, |
| 1668 | "= exit hostapd_cli" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1669 | { "set", hostapd_cli_cmd_set, hostapd_complete_set, |
| 1670 | "<name> <value> = set runtime variables" }, |
| 1671 | { "get", hostapd_cli_cmd_get, hostapd_complete_get, |
| 1672 | "<name> = get runtime info" }, |
| 1673 | { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, |
| 1674 | "<arg,arg,...> = set QoS Map set element" }, |
| 1675 | { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, |
| 1676 | hostapd_complete_stations, |
| 1677 | "<addr> = send QoS Map Configure frame" }, |
| 1678 | { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, |
| 1679 | "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n" |
| 1680 | " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n" |
| 1681 | " = initiate channel switch announcement" }, |
| 1682 | { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, |
| 1683 | "<addr> <url>\n" |
| 1684 | " = send WNM-Notification Subscription Remediation Request" }, |
| 1685 | { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, |
| 1686 | "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n" |
| 1687 | " = send WNM-Notification imminent deauthentication indication" }, |
| 1688 | { "vendor", hostapd_cli_cmd_vendor, NULL, |
| 1689 | "<vendor id> <sub command id> [<hex formatted data>]\n" |
| 1690 | " = send vendor driver command" }, |
| 1691 | { "enable", hostapd_cli_cmd_enable, NULL, |
| 1692 | "= enable hostapd on current interface" }, |
| 1693 | { "reload", hostapd_cli_cmd_reload, NULL, |
| 1694 | "= reload configuration for current interface" }, |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1695 | { "reload_bss", hostapd_cli_cmd_reload_bss, NULL, |
| 1696 | "= reload configuration for current BSS" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1697 | { "disable", hostapd_cli_cmd_disable, NULL, |
| 1698 | "= disable hostapd on current interface" }, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1699 | { "update_beacon", hostapd_cli_cmd_update_beacon, NULL, |
| 1700 | "= update Beacon frame contents\n"}, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1701 | { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, |
| 1702 | "= drop all ERP keys"}, |
| 1703 | { "log_level", hostapd_cli_cmd_log_level, NULL, |
| 1704 | "[level] = show/change log verbosity level" }, |
| 1705 | { "pmksa", hostapd_cli_cmd_pmksa, NULL, |
| 1706 | " = show PMKSA cache entries" }, |
| 1707 | { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, |
| 1708 | " = flush PMKSA cache" }, |
| 1709 | { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, |
| 1710 | "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n" |
| 1711 | " = add AP to neighbor database" }, |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1712 | { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL, |
| 1713 | " = show neighbor database entries" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1714 | { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1715 | "<addr> [ssid=<hex>] = remove AP from neighbor database" }, |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1716 | { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations, |
| 1717 | "<addr> = send LCI request to a station"}, |
| 1718 | { "req_range", hostapd_cli_cmd_req_range, NULL, |
| 1719 | " = send FTM range request"}, |
| 1720 | { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, |
| 1721 | " = show supported driver flags"}, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1722 | #ifdef CONFIG_DPP |
| 1723 | { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL, |
| 1724 | "report a scanned DPP URI from a QR Code" }, |
| 1725 | { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL, |
| 1726 | "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" }, |
| 1727 | { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL, |
| 1728 | "*|<id> = remove DPP bootstrap information" }, |
| 1729 | { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL, |
| 1730 | "<id> = get DPP bootstrap URI" }, |
| 1731 | { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL, |
| 1732 | "<id> = show DPP bootstrap information" }, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1733 | { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL, |
| 1734 | "<id> [conf=..] [ssid=<SSID>] [ssid_charset=#] [psk=<PSK>] [pass=<passphrase>] [configurator=<id>] [conn_status=#] [akm_use_selector=<0|1>] [group_id=..] [expiry=#] [csrattrs=..] = set DPP configurator parameters" }, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1735 | { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL, |
| 1736 | "peer=<id> [own=<id>] = initiate DPP bootstrapping" }, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1737 | { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL, |
| 1738 | "<freq in MHz> = start DPP listen" }, |
| 1739 | { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL, |
| 1740 | "= stop DPP listen" }, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1741 | { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL, |
| 1742 | "[curve=..] [key=..] = add DPP configurator" }, |
| 1743 | { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove, |
| 1744 | NULL, |
| 1745 | "*|<id> = remove DPP configurator" }, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1746 | { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1747 | NULL, |
| 1748 | "<id> = Get DPP configurator's private key" }, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1749 | { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL, |
| 1750 | "conf=<role> configurator=<id> = generate self DPP configuration" }, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1751 | { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL, |
| 1752 | "add PKEX code" }, |
| 1753 | { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL, |
| 1754 | "*|<id> = remove DPP pkex information" }, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1755 | #ifdef CONFIG_DPP2 |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1756 | { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL, |
| 1757 | "[tcp_port=<port>] [role=..] = start DPP controller" }, |
| 1758 | { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL, |
| 1759 | "= stop DPP controller" }, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1760 | { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL, |
| 1761 | "own=<BI ID> iter=<count> = start DPP chirp" }, |
| 1762 | { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL, |
| 1763 | "= stop DPP chirp" }, |
| 1764 | #endif /* CONFIG_DPP2 */ |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1765 | #ifdef CONFIG_DPP3 |
| 1766 | { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL, |
| 1767 | "= press DPP push button" }, |
| 1768 | #endif /* CONFIG_DPP3 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1769 | #endif /* CONFIG_DPP */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1770 | { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL, |
| 1771 | "=Add/Delete/Show/Clear accept MAC ACL" }, |
| 1772 | { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL, |
| 1773 | "=Add/Delete/Show/Clear deny MAC ACL" }, |
| 1774 | { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations, |
| 1775 | "<addr> = poll a STA to check connectivity with a QoS null frame" }, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1776 | { "req_beacon", hostapd_cli_cmd_req_beacon, NULL, |
| 1777 | "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" }, |
| 1778 | { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL, |
| 1779 | "= reload wpa_psk_file only" }, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1780 | #ifdef ANDROID |
| 1781 | { "driver", hostapd_cli_cmd_driver, NULL, |
| 1782 | "<driver sub command> [<hex formatted data>] = send driver command data" }, |
| 1783 | #endif /* ANDROID */ |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1784 | { NULL, NULL, NULL, NULL } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1785 | }; |
| 1786 | |
| 1787 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1788 | /* |
| 1789 | * Prints command usage, lines are padded with the specified string. |
| 1790 | */ |
| 1791 | static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd, |
| 1792 | const char *pad) |
| 1793 | { |
| 1794 | char c; |
| 1795 | size_t n; |
| 1796 | |
| 1797 | if (cmd->usage == NULL) |
| 1798 | return; |
| 1799 | fprintf(stream, "%s%s ", pad, cmd->cmd); |
| 1800 | for (n = 0; (c = cmd->usage[n]); n++) { |
| 1801 | fprintf(stream, "%c", c); |
| 1802 | if (c == '\n') |
| 1803 | fprintf(stream, "%s", pad); |
| 1804 | } |
| 1805 | fprintf(stream, "\n"); |
| 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | static void print_help(FILE *stream, const char *cmd) |
| 1810 | { |
| 1811 | int n; |
| 1812 | |
| 1813 | fprintf(stream, "commands:\n"); |
| 1814 | for (n = 0; hostapd_cli_commands[n].cmd; n++) { |
| 1815 | if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd)) |
| 1816 | print_cmd_help(stream, &hostapd_cli_commands[n], " "); |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1821 | static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
| 1822 | { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1823 | const struct hostapd_cli_cmd *cmd, *match = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1824 | int count; |
| 1825 | |
| 1826 | count = 0; |
| 1827 | cmd = hostapd_cli_commands; |
| 1828 | while (cmd->cmd) { |
| 1829 | if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) { |
| 1830 | match = cmd; |
| 1831 | if (os_strcasecmp(cmd->cmd, argv[0]) == 0) { |
| 1832 | /* we have an exact match */ |
| 1833 | count = 1; |
| 1834 | break; |
| 1835 | } |
| 1836 | count++; |
| 1837 | } |
| 1838 | cmd++; |
| 1839 | } |
| 1840 | |
| 1841 | if (count > 1) { |
| 1842 | printf("Ambiguous command '%s'; possible commands:", argv[0]); |
| 1843 | cmd = hostapd_cli_commands; |
| 1844 | while (cmd->cmd) { |
| 1845 | if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == |
| 1846 | 0) { |
| 1847 | printf(" %s", cmd->cmd); |
| 1848 | } |
| 1849 | cmd++; |
| 1850 | } |
| 1851 | printf("\n"); |
| 1852 | } else if (count == 0) { |
| 1853 | printf("Unknown command '%s'\n", argv[0]); |
| 1854 | } else { |
| 1855 | match->handler(ctrl, argc - 1, &argv[1]); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1860 | static void cli_event(const char *str) |
| 1861 | { |
| 1862 | const char *start, *s; |
| 1863 | |
| 1864 | start = os_strchr(str, '>'); |
| 1865 | if (start == NULL) |
| 1866 | return; |
| 1867 | |
| 1868 | start++; |
| 1869 | |
| 1870 | if (str_starts(start, AP_STA_CONNECTED)) { |
| 1871 | s = os_strchr(start, ' '); |
| 1872 | if (s == NULL) |
| 1873 | return; |
| 1874 | cli_txt_list_add(&stations, s + 1); |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | if (str_starts(start, AP_STA_DISCONNECTED)) { |
| 1879 | s = os_strchr(start, ' '); |
| 1880 | if (s == NULL) |
| 1881 | return; |
| 1882 | cli_txt_list_del_addr(&stations, s + 1); |
| 1883 | return; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1888 | static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read, |
| 1889 | int action_monitor) |
| 1890 | { |
| 1891 | int first = 1; |
| 1892 | if (ctrl_conn == NULL) |
| 1893 | return; |
| 1894 | while (wpa_ctrl_pending(ctrl)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1895 | char buf[4096]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1896 | size_t len = sizeof(buf) - 1; |
| 1897 | if (wpa_ctrl_recv(ctrl, buf, &len) == 0) { |
| 1898 | buf[len] = '\0'; |
| 1899 | if (action_monitor) |
| 1900 | hostapd_cli_action_process(buf, len); |
| 1901 | else { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1902 | cli_event(buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1903 | if (in_read && first) |
| 1904 | printf("\n"); |
| 1905 | first = 0; |
| 1906 | printf("%s\n", buf); |
| 1907 | } |
| 1908 | } else { |
| 1909 | printf("Could not read pending message.\n"); |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1916 | static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1917 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1918 | hostapd_cli_recv_pending(ctrl_conn, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1922 | static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1923 | { |
| 1924 | if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) { |
| 1925 | printf("Connection to hostapd lost - trying to reconnect\n"); |
| 1926 | hostapd_cli_close_connection(); |
| 1927 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1928 | if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0) |
| 1929 | printf("Connection to hostapd re-established\n"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1930 | if (ctrl_conn) |
| 1931 | hostapd_cli_recv_pending(ctrl_conn, 1, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1932 | eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL); |
| 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx) |
| 1937 | { |
| 1938 | eloop_terminate(); |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd) |
| 1943 | { |
| 1944 | char *argv[max_args]; |
| 1945 | int argc; |
| 1946 | argc = tokenize_cmd(cmd, argv); |
| 1947 | if (argc) |
| 1948 | wpa_request(ctrl_conn, argc, argv); |
| 1949 | } |
| 1950 | |
| 1951 | |
| 1952 | static void hostapd_cli_edit_eof_cb(void *ctx) |
| 1953 | { |
| 1954 | eloop_terminate(); |
| 1955 | } |
| 1956 | |
| 1957 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1958 | static char ** list_cmd_list(void) |
| 1959 | { |
| 1960 | char **res; |
| 1961 | int i, count; |
| 1962 | |
| 1963 | count = ARRAY_SIZE(hostapd_cli_commands); |
| 1964 | res = os_calloc(count + 1, sizeof(char *)); |
| 1965 | if (res == NULL) |
| 1966 | return NULL; |
| 1967 | |
| 1968 | for (i = 0; hostapd_cli_commands[i].cmd; i++) { |
| 1969 | res[i] = os_strdup(hostapd_cli_commands[i].cmd); |
| 1970 | if (res[i] == NULL) |
| 1971 | break; |
| 1972 | } |
| 1973 | |
| 1974 | return res; |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str, |
| 1979 | int pos) |
| 1980 | { |
| 1981 | int i; |
| 1982 | |
| 1983 | for (i = 0; hostapd_cli_commands[i].cmd; i++) { |
| 1984 | if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0) |
| 1985 | continue; |
| 1986 | if (hostapd_cli_commands[i].completion) |
| 1987 | return hostapd_cli_commands[i].completion(str, pos); |
| 1988 | if (!hostapd_cli_commands[i].usage) |
| 1989 | return NULL; |
| 1990 | edit_clear_line(); |
| 1991 | printf("\r%s\n", hostapd_cli_commands[i].usage); |
| 1992 | edit_redraw(); |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | return NULL; |
| 1997 | } |
| 1998 | |
| 1999 | |
| 2000 | static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str, |
| 2001 | int pos) |
| 2002 | { |
| 2003 | char **res; |
| 2004 | const char *end; |
| 2005 | char *cmd; |
| 2006 | |
| 2007 | end = os_strchr(str, ' '); |
| 2008 | if (end == NULL || str + pos < end) |
| 2009 | return list_cmd_list(); |
| 2010 | |
| 2011 | cmd = os_malloc(pos + 1); |
| 2012 | if (cmd == NULL) |
| 2013 | return NULL; |
| 2014 | os_memcpy(cmd, str, pos); |
| 2015 | cmd[end - str] = '\0'; |
| 2016 | res = hostapd_cli_cmd_completion(cmd, str, pos); |
| 2017 | os_free(cmd); |
| 2018 | return res; |
| 2019 | } |
| 2020 | |
| 2021 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2022 | static void hostapd_cli_interactive(void) |
| 2023 | { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2024 | char *hfile = NULL; |
| 2025 | char *home; |
| 2026 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2027 | printf("\nInteractive mode\n\n"); |
| 2028 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2029 | #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR |
| 2030 | home = CONFIG_HOSTAPD_CLI_HISTORY_DIR; |
| 2031 | #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */ |
| 2032 | home = getenv("HOME"); |
| 2033 | #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */ |
| 2034 | if (home) { |
| 2035 | const char *fname = ".hostapd_cli_history"; |
| 2036 | int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1; |
| 2037 | hfile = os_malloc(hfile_len); |
| 2038 | if (hfile) |
| 2039 | os_snprintf(hfile, hfile_len, "%s/%s", home, fname); |
| 2040 | } |
| 2041 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2042 | edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2043 | hostapd_cli_edit_completion_cb, NULL, hfile, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2044 | eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL); |
| 2045 | |
| 2046 | eloop_run(); |
| 2047 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 2048 | cli_txt_list_flush(&stations); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2049 | edit_deinit(hfile, NULL); |
| 2050 | os_free(hfile); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2051 | eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL); |
| 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | static void hostapd_cli_cleanup(void) |
| 2056 | { |
| 2057 | hostapd_cli_close_connection(); |
| 2058 | if (pid_file) |
| 2059 | os_daemonize_terminate(pid_file); |
| 2060 | |
| 2061 | os_program_deinit(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 2065 | static void hostapd_cli_action_ping(void *eloop_ctx, void *timeout_ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2066 | { |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 2067 | struct wpa_ctrl *ctrl = eloop_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2068 | char buf[256]; |
| 2069 | size_t len; |
| 2070 | |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 2071 | /* verify that connection is still working */ |
| 2072 | len = sizeof(buf) - 1; |
| 2073 | if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len, |
| 2074 | hostapd_cli_action_cb) < 0 || |
| 2075 | len < 4 || os_memcmp(buf, "PONG", 4) != 0) { |
| 2076 | printf("hostapd did not reply to PING command - exiting\n"); |
| 2077 | eloop_terminate(); |
| 2078 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2079 | } |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 2080 | eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping, |
| 2081 | ctrl, NULL); |
| 2082 | } |
| 2083 | |
| 2084 | |
| 2085 | static void hostapd_cli_action_receive(int sock, void *eloop_ctx, |
| 2086 | void *sock_ctx) |
| 2087 | { |
| 2088 | struct wpa_ctrl *ctrl = eloop_ctx; |
| 2089 | |
| 2090 | hostapd_cli_recv_pending(ctrl, 0, 1); |
| 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | static void hostapd_cli_action(struct wpa_ctrl *ctrl) |
| 2095 | { |
| 2096 | int fd; |
| 2097 | |
| 2098 | fd = wpa_ctrl_get_fd(ctrl); |
| 2099 | eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping, |
| 2100 | ctrl, NULL); |
| 2101 | eloop_register_read_sock(fd, hostapd_cli_action_receive, ctrl, NULL); |
| 2102 | eloop_run(); |
| 2103 | eloop_cancel_timeout(hostapd_cli_action_ping, ctrl, NULL); |
| 2104 | eloop_unregister_read_sock(fd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | int main(int argc, char *argv[]) |
| 2109 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2110 | int warning_displayed = 0; |
| 2111 | int c; |
| 2112 | int daemonize = 0; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2113 | int reconnect = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2114 | |
| 2115 | if (os_program_init()) |
| 2116 | return -1; |
| 2117 | |
| 2118 | for (;;) { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2119 | c = getopt(argc, argv, "a:BhG:i:p:P:rs:v"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2120 | if (c < 0) |
| 2121 | break; |
| 2122 | switch (c) { |
| 2123 | case 'a': |
| 2124 | action_file = optarg; |
| 2125 | break; |
| 2126 | case 'B': |
| 2127 | daemonize = 1; |
| 2128 | break; |
| 2129 | case 'G': |
| 2130 | ping_interval = atoi(optarg); |
| 2131 | break; |
| 2132 | case 'h': |
| 2133 | usage(); |
| 2134 | return 0; |
| 2135 | case 'v': |
| 2136 | printf("%s\n", hostapd_cli_version); |
| 2137 | return 0; |
| 2138 | case 'i': |
| 2139 | os_free(ctrl_ifname); |
| 2140 | ctrl_ifname = os_strdup(optarg); |
| 2141 | break; |
| 2142 | case 'p': |
| 2143 | ctrl_iface_dir = optarg; |
| 2144 | break; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2145 | case 'P': |
| 2146 | pid_file = optarg; |
| 2147 | break; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2148 | case 'r': |
| 2149 | reconnect = 1; |
| 2150 | break; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2151 | case 's': |
| 2152 | client_socket_dir = optarg; |
| 2153 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2154 | default: |
| 2155 | usage(); |
| 2156 | return -1; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | interactive = (argc == optind) && (action_file == NULL); |
| 2161 | |
| 2162 | if (interactive) { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 2163 | printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2166 | if (eloop_init()) |
| 2167 | return -1; |
| 2168 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2169 | for (;;) { |
| 2170 | if (ctrl_ifname == NULL) { |
| 2171 | struct dirent *dent; |
| 2172 | DIR *dir = opendir(ctrl_iface_dir); |
| 2173 | if (dir) { |
| 2174 | while ((dent = readdir(dir))) { |
| 2175 | if (os_strcmp(dent->d_name, ".") == 0 |
| 2176 | || |
| 2177 | os_strcmp(dent->d_name, "..") == 0) |
| 2178 | continue; |
| 2179 | printf("Selected interface '%s'\n", |
| 2180 | dent->d_name); |
| 2181 | ctrl_ifname = os_strdup(dent->d_name); |
| 2182 | break; |
| 2183 | } |
| 2184 | closedir(dir); |
| 2185 | } |
| 2186 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2187 | hostapd_cli_reconnect(ctrl_ifname); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2188 | if (ctrl_conn) { |
| 2189 | if (warning_displayed) |
| 2190 | printf("Connection established.\n"); |
| 2191 | break; |
| 2192 | } |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2193 | if (!interactive && !reconnect) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2194 | perror("Failed to connect to hostapd - " |
| 2195 | "wpa_ctrl_open"); |
| 2196 | return -1; |
| 2197 | } |
| 2198 | |
| 2199 | if (!warning_displayed) { |
| 2200 | printf("Could not connect to hostapd - re-trying\n"); |
| 2201 | warning_displayed = 1; |
| 2202 | } |
| 2203 | os_sleep(1, 0); |
| 2204 | continue; |
| 2205 | } |
| 2206 | |
Sunil Ravi | 036cec5 | 2023-03-29 11:35:17 -0700 | [diff] [blame^] | 2207 | eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL); |
| 2208 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2209 | if (action_file && !hostapd_cli_attached) |
| 2210 | return -1; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2211 | if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue()) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2212 | return -1; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2213 | if (reconnect && action_file && ctrl_ifname) { |
| 2214 | while (!hostapd_cli_quit) { |
| 2215 | if (ctrl_conn) |
| 2216 | hostapd_cli_action(ctrl_conn); |
| 2217 | os_sleep(1, 0); |
| 2218 | hostapd_cli_reconnect(ctrl_ifname); |
| 2219 | } |
| 2220 | } else if (interactive) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2221 | hostapd_cli_interactive(); |
| 2222 | else if (action_file) |
| 2223 | hostapd_cli_action(ctrl_conn); |
| 2224 | else |
| 2225 | wpa_request(ctrl_conn, argc - optind, &argv[optind]); |
| 2226 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 2227 | unregister_event_handler(ctrl_conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2228 | os_free(ctrl_ifname); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2229 | eloop_destroy(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2230 | hostapd_cli_cleanup(); |
| 2231 | return 0; |
| 2232 | } |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 2233 | |
| 2234 | #else /* CONFIG_NO_CTRL_IFACE */ |
| 2235 | |
| 2236 | int main(int argc, char *argv[]) |
| 2237 | { |
| 2238 | return -1; |
| 2239 | } |
| 2240 | |
| 2241 | #endif /* CONFIG_NO_CTRL_IFACE */ |