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