blob: eb8a38350bd112da7cccec7a7077229a6258b32a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd - command line interface for hostapd daemon
Hai Shaloma20dcd72022-02-04 13:43:00 -08003 * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <dirent.h>
11
12#include "common/wpa_ctrl.h"
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -070013#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014#include "utils/common.h"
15#include "utils/eloop.h"
16#include "utils/edit.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/version.h"
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070018#include "common/cli.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080020#ifndef CONFIG_NO_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070022static const char *const hostapd_cli_version =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023"hostapd_cli v" VERSION_STR "\n"
Sunil Ravi7f769292024-07-23 22:21:32 +000024"Copyright (c) 2004-2024, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026static struct wpa_ctrl *ctrl_conn;
27static int hostapd_cli_quit = 0;
28static int hostapd_cli_attached = 0;
Jeff Johnson205f2142012-09-03 22:12:17 -070029
30#ifndef CONFIG_CTRL_IFACE_DIR
31#define CONFIG_CTRL_IFACE_DIR "/var/run/hostapd"
32#endif /* CONFIG_CTRL_IFACE_DIR */
33static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080034static const char *client_socket_dir = NULL;
Jeff Johnson205f2142012-09-03 22:12:17 -070035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036static char *ctrl_ifname = NULL;
37static const char *pid_file = NULL;
38static const char *action_file = NULL;
39static int ping_interval = 5;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080040static int interactive = 0;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070041static int event_handler_registered = 0;
42
43static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
44
45static void print_help(FILE *stream, const char *cmd);
46static char ** list_cmd_list(void);
47static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048static void update_stations(struct wpa_ctrl *ctrl);
49static void cli_event(const char *str);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
51
52static void usage(void)
53{
54 fprintf(stderr, "%s\n", hostapd_cli_version);
55 fprintf(stderr,
56 "\n"
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080057 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvBr] "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 "[-a<path>] \\\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080059 " [-P<pid file>] [-G<ping interval>] [command..]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 "\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 Shmidtd80a4012015-11-05 16:35:40 -080066 " -s<dir_path> dir path to open client sockets (default: "
67 CONFIG_CTRL_IFACE_DIR ")\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 " -a<file> run in daemon mode executing the action file "
69 "based on events\n"
70 " from hostapd\n"
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080071 " -r try to reconnect when client socket is "
72 "disconnected.\n"
73 " This is useful only when used with -a.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 " -B run a daemon in the background\n"
75 " -i<ifname> Interface to listen on (default: first "
76 "interface found in the\n"
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070077 " socket path)\n\n");
78 print_help(stderr, NULL);
79}
80
81
82static 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
95static 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 Shmidt8d520ff2011-05-09 14:06:53 -0700103}
104
105
106static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
107{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800108#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 char *cfile;
110 int flen;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800111#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112
113 if (ifname == NULL)
114 return NULL;
115
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800116#ifdef CONFIG_CTRL_IFACE_UDP
117 ctrl_conn = wpa_ctrl_open(ifname);
118 return ctrl_conn;
119#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 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 Shmidtd80a4012015-11-05 16:35:40 -0800126 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 Shmidt8d520ff2011-05-09 14:06:53 -0700134 free(cfile);
135 return ctrl_conn;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800136#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137}
138
139
140static void hostapd_cli_close_connection(void)
141{
142 if (ctrl_conn == NULL)
143 return;
144
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700145 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146 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 Shmidt9839ecd2016-11-07 11:05:47 -0800155static 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 Shmidt8d520ff2011-05-09 14:06:53 -0700186static void hostapd_cli_msg_cb(char *msg, size_t len)
187{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800188 cli_event(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 printf("%s\n", msg);
190}
191
192
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800193static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194{
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 Shmidt9839ecd2016-11-07 11:05:47 -0800221static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222{
223 return _wpa_ctrl_command(ctrl, cmd, 1);
224}
225
226
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800227static 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 Shmidt8d520ff2011-05-09 14:06:53 -0700243static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
244{
245 return wpa_ctrl_command(ctrl, "PING");
246}
247
248
249static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
250{
251 return wpa_ctrl_command(ctrl, "RELOG");
252}
253
254
Sunil Ravi77d572f2023-01-17 23:58:31 +0000255static int hostapd_cli_cmd_close_log(struct wpa_ctrl *ctrl, int argc,
256 char *argv[])
257{
258 return wpa_ctrl_command(ctrl, "CLOSE_LOG");
259}
260
261
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800262static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
263{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
265 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800266 return wpa_ctrl_command(ctrl, "STATUS");
267}
268
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
271{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800272 if (argc > 0) {
273 char buf[100];
274 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
275 return wpa_ctrl_command(ctrl, buf);
276 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 return wpa_ctrl_command(ctrl, "MIB");
278}
279
280
281static int hostapd_cli_exec(const char *program, const char *arg1,
282 const char *arg2)
283{
Jouni Malinen772e12c2014-10-07 10:29:35 -0700284 char *arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 size_t len;
286 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Jouni Malinen772e12c2014-10-07 10:29:35 -0700288 len = os_strlen(arg1) + os_strlen(arg2) + 2;
289 arg = os_malloc(len);
290 if (arg == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 return -1;
Jouni Malinen772e12c2014-10-07 10:29:35 -0700292 os_snprintf(arg, len, "%s %s", arg1, arg2);
293 res = os_exec(program, arg, 1);
294 os_free(arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
Jouni Malinen772e12c2014-10-07 10:29:35 -0700296 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297}
298
299
300static void hostapd_cli_action_process(char *msg, size_t len)
301{
302 const char *pos;
303
304 pos = msg;
305 if (*pos == '<') {
306 pos = os_strchr(pos, '>');
307 if (pos)
308 pos++;
309 else
310 pos = msg;
311 }
312
313 hostapd_cli_exec(action_file, ctrl_ifname, pos);
314}
315
316
Sunil Ravi036cec52023-03-29 11:35:17 -0700317static void hostapd_cli_action_cb(char *msg, size_t len)
318{
319 hostapd_cli_action_process(msg, len);
320}
321
322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
324{
325 char buf[64];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800326 if (argc < 1) {
327 printf("Invalid 'sta' command - at least one argument, STA "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 "address, is required.\n");
329 return -1;
330 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 if (argc > 1)
332 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
333 else
334 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 return wpa_ctrl_command(ctrl, buf);
336}
337
338
Dmitry Shmidt29333592017-01-09 12:27:11 -0800339static char ** hostapd_complete_stations(const char *str, int pos)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800340{
341 int arg = get_cmd_arg_num(str, pos);
342 char **res = NULL;
343
344 switch (arg) {
345 case 1:
346 res = cli_txt_list_array(&stations);
347 break;
348 }
349
350 return res;
351}
352
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
355 char *argv[])
356{
357 char buf[64];
358 if (argc != 1) {
359 printf("Invalid 'new_sta' command - exactly one argument, STA "
360 "address, is required.\n");
361 return -1;
362 }
363 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
364 return wpa_ctrl_command(ctrl, buf);
365}
366
367
368static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
369 char *argv[])
370{
371 char buf[64];
372 if (argc < 1) {
373 printf("Invalid 'deauthenticate' command - exactly one "
374 "argument, STA address, is required.\n");
375 return -1;
376 }
377 if (argc > 1)
378 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
379 argv[0], argv[1]);
380 else
381 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
382 return wpa_ctrl_command(ctrl, buf);
383}
384
385
386static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
387 char *argv[])
388{
389 char buf[64];
390 if (argc < 1) {
391 printf("Invalid 'disassociate' command - exactly one "
392 "argument, STA address, is required.\n");
393 return -1;
394 }
395 if (argc > 1)
396 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
397 argv[0], argv[1]);
398 else
399 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
400 return wpa_ctrl_command(ctrl, buf);
401}
402
403
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700404#ifdef CONFIG_TAXONOMY
405static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
406 char *argv[])
407{
408 char buf[64];
409
410 if (argc != 1) {
411 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
412 return -1;
413 }
414 os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
415 return wpa_ctrl_command(ctrl, buf);
416}
417#endif /* CONFIG_TAXONOMY */
418
419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
421 char *argv[])
422{
423 char buf[64];
424 if (argc != 1) {
425 printf("Invalid 'sa_query' command - exactly one argument, "
426 "STA address, is required.\n");
427 return -1;
428 }
429 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
430 return wpa_ctrl_command(ctrl, buf);
431}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432
433
434#ifdef CONFIG_WPS
435static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
436 char *argv[])
437{
438 char buf[256];
439 if (argc < 2) {
440 printf("Invalid 'wps_pin' command - at least two arguments, "
441 "UUID and PIN, are required.\n");
442 return -1;
443 }
444 if (argc > 3)
445 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
446 argv[0], argv[1], argv[2], argv[3]);
447 else if (argc > 2)
448 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
449 argv[0], argv[1], argv[2]);
450 else
451 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
452 return wpa_ctrl_command(ctrl, buf);
453}
454
455
456static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
457 char *argv[])
458{
459 char cmd[256];
460 int res;
461
462 if (argc != 1 && argc != 2) {
463 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
464 "- PIN to be verified\n");
465 return -1;
466 }
467
468 if (argc == 2)
469 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
470 argv[0], argv[1]);
471 else
472 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
473 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800474 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 printf("Too long WPS_CHECK_PIN command.\n");
476 return -1;
477 }
478 return wpa_ctrl_command(ctrl, cmd);
479}
480
481
482static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
483 char *argv[])
484{
485 return wpa_ctrl_command(ctrl, "WPS_PBC");
486}
487
488
Dmitry Shmidt04949592012-07-19 12:16:46 -0700489static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
490 char *argv[])
491{
492 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
493}
494
495
Dmitry Shmidt04949592012-07-19 12:16:46 -0700496#ifdef CONFIG_WPS_NFC
497static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
498 char *argv[])
499{
500 int ret;
501 char *buf;
502 size_t buflen;
503
504 if (argc != 1) {
505 printf("Invalid 'wps_nfc_tag_read' command - one argument "
506 "is required.\n");
507 return -1;
508 }
509
510 buflen = 18 + os_strlen(argv[0]);
511 buf = os_malloc(buflen);
512 if (buf == NULL)
513 return -1;
514 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
515
516 ret = wpa_ctrl_command(ctrl, buf);
517 os_free(buf);
518
519 return ret;
520}
521
522
523static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
524 int argc, char *argv[])
525{
526 char cmd[64];
527 int res;
528
529 if (argc != 1) {
530 printf("Invalid 'wps_nfc_config_token' command - one argument "
531 "is required.\n");
532 return -1;
533 }
534
535 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
536 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700538 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
539 return -1;
540 }
541 return wpa_ctrl_command(ctrl, cmd);
542}
543
544
545static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
546 int argc, char *argv[])
547{
548 char cmd[64];
549 int res;
550
551 if (argc != 1) {
552 printf("Invalid 'wps_nfc_token' command - one argument is "
553 "required.\n");
554 return -1;
555 }
556
557 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800558 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700559 printf("Too long WPS_NFC_TOKEN command.\n");
560 return -1;
561 }
562 return wpa_ctrl_command(ctrl, cmd);
563}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800564
565
566static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
567 int argc, char *argv[])
568{
569 char cmd[64];
570 int res;
571
572 if (argc != 2) {
573 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
574 "are required.\n");
575 return -1;
576 }
577
578 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
579 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800581 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
582 return -1;
583 }
584 return wpa_ctrl_command(ctrl, cmd);
585}
586
Dmitry Shmidt04949592012-07-19 12:16:46 -0700587#endif /* CONFIG_WPS_NFC */
588
589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
591 char *argv[])
592{
593 char buf[64];
594 if (argc < 1) {
595 printf("Invalid 'wps_ap_pin' command - at least one argument "
596 "is required.\n");
597 return -1;
598 }
599 if (argc > 2)
600 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
601 argv[0], argv[1], argv[2]);
602 else if (argc > 1)
603 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
604 argv[0], argv[1]);
605 else
606 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
607 return wpa_ctrl_command(ctrl, buf);
608}
609
610
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700611static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
612 char *argv[])
613{
614 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
615}
616
617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
619 char *argv[])
620{
621 char buf[256];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700622 char ssid_hex[2 * SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 char key_hex[2 * 64 + 1];
624 int i;
625
626 if (argc < 1) {
627 printf("Invalid 'wps_config' command - at least two arguments "
628 "are required.\n");
629 return -1;
630 }
631
632 ssid_hex[0] = '\0';
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700633 for (i = 0; i < SSID_MAX_LEN; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 if (argv[0][i] == '\0')
635 break;
636 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
637 }
638
639 key_hex[0] = '\0';
640 if (argc > 3) {
641 for (i = 0; i < 64; i++) {
642 if (argv[3][i] == '\0')
643 break;
644 os_snprintf(&key_hex[i * 2], 3, "%02x",
645 argv[3][i]);
646 }
647 }
648
649 if (argc > 3)
650 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
651 ssid_hex, argv[1], argv[2], key_hex);
652 else if (argc > 2)
653 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
654 ssid_hex, argv[1], argv[2]);
655 else
656 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
657 ssid_hex, argv[1]);
658 return wpa_ctrl_command(ctrl, buf);
659}
660#endif /* CONFIG_WPS */
661
662
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800663static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
664 char *argv[])
665{
666 char buf[300];
667 int res;
668
669 if (argc < 2) {
670 printf("Invalid 'disassoc_imminent' command - two arguments "
671 "(STA addr and Disassociation Timer) are needed\n");
672 return -1;
673 }
674
675 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
676 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800677 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800678 return -1;
679 return wpa_ctrl_command(ctrl, buf);
680}
681
682
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
684 char *argv[])
685{
686 char buf[300];
687 int res;
688
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700689 if (argc < 3) {
690 printf("Invalid 'ess_disassoc' command - three arguments (STA "
691 "addr, disassoc timer, and URL) are needed\n");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800692 return -1;
693 }
694
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700695 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
696 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800697 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800698 return -1;
699 return wpa_ctrl_command(ctrl, buf);
700}
701
702
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800703static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
704 char *argv[])
705{
706 char buf[2000], *tmp;
707 int res, i, total;
708
709 if (argc < 1) {
710 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
711 return -1;
712 }
713
714 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
715 if (os_snprintf_error(sizeof(buf), res))
716 return -1;
717
718 total = res;
719 for (i = 1; i < argc; i++) {
720 tmp = &buf[total];
721 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
722 if (os_snprintf_error(sizeof(buf) - total, res))
723 return -1;
724 total += res;
725 }
726 return wpa_ctrl_command(ctrl, buf);
727}
728
729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
731 char *argv[])
732{
733 return wpa_ctrl_command(ctrl, "GET_CONFIG");
734}
735
736
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800737static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
738 char *addr, size_t addr_len, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739{
740 char buf[4096], *pos;
741 size_t len;
742 int ret;
743
744 if (ctrl_conn == NULL) {
745 printf("Not connected to hostapd - command dropped.\n");
746 return -1;
747 }
748 len = sizeof(buf) - 1;
749 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
750 hostapd_cli_msg_cb);
751 if (ret == -2) {
752 printf("'%s' command timed out.\n", cmd);
753 return -2;
754 } else if (ret < 0) {
755 printf("'%s' command failed.\n", cmd);
756 return -1;
757 }
758
759 buf[len] = '\0';
760 if (memcmp(buf, "FAIL", 4) == 0)
761 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800762 if (print)
763 printf("%s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764
765 pos = buf;
766 while (*pos != '\0' && *pos != '\n')
767 pos++;
768 *pos = '\0';
769 os_strlcpy(addr, buf, addr_len);
770 return 0;
771}
772
773
774static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
775 char *argv[])
776{
777 char addr[32], cmd[64];
778
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800779 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 return 0;
781 do {
782 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800783 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784
785 return -1;
786}
787
788
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800789static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
790 char *argv[])
791{
792 char addr[32], cmd[64];
793
794 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
795 return 0;
796 do {
797 if (os_strcmp(addr, "") != 0)
798 printf("%s\n", addr);
799 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
800 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
801
802 return 0;
803}
804
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
807{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700808 print_help(stdout, argc > 0 ? argv[0] : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 return 0;
810}
811
812
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700813static char ** hostapd_cli_complete_help(const char *str, int pos)
814{
815 int arg = get_cmd_arg_num(str, pos);
816 char **res = NULL;
817
818 switch (arg) {
819 case 1:
820 res = list_cmd_list();
821 break;
822 }
823
824 return res;
825}
826
827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
829 char *argv[])
830{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700831 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 return 0;
833}
834
835
Dmitry Shmidt051af732013-10-22 13:52:46 -0700836static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
837 int argc, char *argv[])
838{
839 char buf[200];
840 int res;
841
842 if (argc != 1) {
843 printf("Invalid 'set_qos_map_set' command - "
844 "one argument (comma delimited QoS map set) "
845 "is needed\n");
846 return -1;
847 }
848
849 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800850 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700851 return -1;
852 return wpa_ctrl_command(ctrl, buf);
853}
854
855
856static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
857 int argc, char *argv[])
858{
859 char buf[50];
860 int res;
861
862 if (argc != 1) {
863 printf("Invalid 'send_qos_map_conf' command - "
864 "one argument (STA addr) is needed\n");
865 return -1;
866 }
867
868 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800869 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700870 return -1;
871 return wpa_ctrl_command(ctrl, buf);
872}
873
874
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800875static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
876 char *argv[])
877{
878 char buf[300];
879 int res;
880
881 if (argc < 2) {
882 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
883 "addr and URL) are needed\n");
884 return -1;
885 }
886
887 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
888 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800889 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800890 return -1;
891 return wpa_ctrl_command(ctrl, buf);
892}
893
894
895static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
896 char *argv[])
897{
898 char buf[300];
899 int res;
900
901 if (argc < 3) {
902 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
903 return -1;
904 }
905
906 if (argc > 3)
907 res = os_snprintf(buf, sizeof(buf),
908 "HS20_DEAUTH_REQ %s %s %s %s",
909 argv[0], argv[1], argv[2], argv[3]);
910 else
911 res = os_snprintf(buf, sizeof(buf),
912 "HS20_DEAUTH_REQ %s %s %s",
913 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800914 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800915 return -1;
916 return wpa_ctrl_command(ctrl, buf);
917}
918
919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
921{
922 hostapd_cli_quit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800923 if (interactive)
924 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 return 0;
926}
927
928
929static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
930{
931 char cmd[256];
932 if (argc != 1) {
933 printf("Invalid LEVEL command: needs one argument (debug "
934 "level)\n");
935 return 0;
936 }
937 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
938 return wpa_ctrl_command(ctrl, cmd);
939}
940
941
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800942static void update_stations(struct wpa_ctrl *ctrl)
943{
944 char addr[32], cmd[64];
945
946 if (!ctrl || !interactive)
947 return;
948
949 cli_txt_list_flush(&stations);
950
951 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
952 return;
953 do {
954 if (os_strcmp(addr, "") != 0)
955 cli_txt_list_add(&stations, addr);
956 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
957 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
958}
959
960
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700961static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
962 struct dl_list *interfaces)
963{
964 struct dirent *dent;
965 DIR *dir;
966
967 if (!ctrl || !interfaces)
968 return;
969 dir = opendir(ctrl_iface_dir);
970 if (dir == NULL)
971 return;
972
973 while ((dent = readdir(dir))) {
974 if (strcmp(dent->d_name, ".") == 0 ||
975 strcmp(dent->d_name, "..") == 0)
976 continue;
977 cli_txt_list_add(interfaces, dent->d_name);
978 }
979 closedir(dir);
980}
981
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
984{
985 struct dirent *dent;
986 DIR *dir;
987
988 dir = opendir(ctrl_iface_dir);
989 if (dir == NULL) {
990 printf("Control interface directory '%s' could not be "
Hai Shalom899fcc72020-10-19 14:38:18 -0700991 "opened.\n", ctrl_iface_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 return;
993 }
994
995 printf("Available interfaces:\n");
996 while ((dent = readdir(dir))) {
997 if (strcmp(dent->d_name, ".") == 0 ||
998 strcmp(dent->d_name, "..") == 0)
999 continue;
1000 printf("%s\n", dent->d_name);
1001 }
1002 closedir(dir);
1003}
1004
1005
1006static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
1007 char *argv[])
1008{
1009 if (argc < 1) {
1010 hostapd_cli_list_interfaces(ctrl);
1011 return 0;
1012 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001013 if (hostapd_cli_reconnect(argv[0]) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 printf("Could not connect to interface '%s' - re-trying\n",
1015 ctrl_ifname);
1016 }
1017 return 0;
1018}
1019
1020
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001021static char ** hostapd_complete_interface(const char *str, int pos)
1022{
1023 int arg = get_cmd_arg_num(str, pos);
1024 char **res = NULL;
1025 DEFINE_DL_LIST(interfaces);
1026
1027 switch (arg) {
1028 case 1:
1029 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1030 res = cli_txt_list_array(&interfaces);
1031 cli_txt_list_flush(&interfaces);
1032 break;
1033 }
1034
1035 return res;
1036}
1037
1038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1040{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001041 char cmd[2048];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 int res;
1043
1044 if (argc != 2) {
1045 printf("Invalid SET command: needs two arguments (variable "
1046 "name and value)\n");
1047 return -1;
1048 }
1049
1050 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001051 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 printf("Too long SET command.\n");
1053 return -1;
1054 }
1055 return wpa_ctrl_command(ctrl, cmd);
1056}
1057
1058
Dmitry Shmidt29333592017-01-09 12:27:11 -08001059static char ** hostapd_complete_set(const char *str, int pos)
1060{
1061 int arg = get_cmd_arg_num(str, pos);
1062 const char *fields[] = {
1063#ifdef CONFIG_WPS_TESTING
Hai Shaloma20dcd72022-02-04 13:43:00 -08001064 "wps_version_number", "wps_testing_stub_cred",
Dmitry Shmidt29333592017-01-09 12:27:11 -08001065 "wps_corrupt_pkhash",
1066#endif /* CONFIG_WPS_TESTING */
1067#ifdef CONFIG_INTERWORKING
1068 "gas_frag_limit",
1069#endif /* CONFIG_INTERWORKING */
1070#ifdef CONFIG_TESTING_OPTIONS
1071 "ext_mgmt_frame_handling", "ext_eapol_frame_io",
1072#endif /* CONFIG_TESTING_OPTIONS */
1073#ifdef CONFIG_MBO
1074 "mbo_assoc_disallow",
1075#endif /* CONFIG_MBO */
1076 "deny_mac_file", "accept_mac_file",
1077 };
1078 int i, num_fields = ARRAY_SIZE(fields);
1079
1080 if (arg == 1) {
1081 char **res;
1082
1083 res = os_calloc(num_fields + 1, sizeof(char *));
1084 if (!res)
1085 return NULL;
1086 for (i = 0; i < num_fields; i++) {
1087 res[i] = os_strdup(fields[i]);
1088 if (!res[i])
1089 return res;
1090 }
1091 return res;
1092 }
1093 return NULL;
1094}
1095
1096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1098{
1099 char cmd[256];
1100 int res;
1101
1102 if (argc != 1) {
1103 printf("Invalid GET command: needs one argument (variable "
1104 "name)\n");
1105 return -1;
1106 }
1107
1108 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001109 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 printf("Too long GET command.\n");
1111 return -1;
1112 }
1113 return wpa_ctrl_command(ctrl, cmd);
1114}
1115
1116
Dmitry Shmidt29333592017-01-09 12:27:11 -08001117static char ** hostapd_complete_get(const char *str, int pos)
1118{
1119 int arg = get_cmd_arg_num(str, pos);
1120 const char *fields[] = {
1121 "version", "tls_library",
1122 };
1123 int i, num_fields = ARRAY_SIZE(fields);
1124
1125 if (arg == 1) {
1126 char **res;
1127
1128 res = os_calloc(num_fields + 1, sizeof(char *));
1129 if (!res)
1130 return NULL;
1131 for (i = 0; i < num_fields; i++) {
1132 res[i] = os_strdup(fields[i]);
1133 if (!res[i])
1134 return res;
1135 }
1136 return res;
1137 }
1138 return NULL;
1139}
1140
1141
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001142#ifdef CONFIG_FST
1143static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1144{
1145 char cmd[256];
1146 int res;
1147 int i;
1148 int total;
1149
1150 if (argc <= 0) {
1151 printf("FST command: parameters are required.\n");
1152 return -1;
1153 }
1154
1155 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1156
1157 for (i = 0; i < argc; i++) {
1158 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1159 argv[i]);
1160 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1161 printf("Too long fst command.\n");
1162 return -1;
1163 }
1164 total += res;
1165 }
1166 return wpa_ctrl_command(ctrl, cmd);
1167}
1168#endif /* CONFIG_FST */
1169
1170
Sunil Ravi7f769292024-07-23 22:21:32 +00001171#ifdef CONFIG_IEEE80211AX
1172static int hostapd_cli_cmd_color_change(struct wpa_ctrl *ctrl,
1173 int argc, char *argv[])
1174{
1175 return hostapd_cli_cmd(ctrl, "COLOR_CHANGE", 1, argc, argv);
1176}
1177#endif /* CONFIG_IEEE80211AX */
1178
1179
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001180static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1181 int argc, char *argv[])
1182{
1183 char cmd[256];
1184 int res;
1185 int i;
1186 char *tmp;
1187 int total;
1188
1189 if (argc < 2) {
1190 printf("Invalid chan_switch command: needs at least two "
1191 "arguments (count and freq)\n"
1192 "usage: <cs_count> <freq> [sec_channel_offset=] "
1193 "[center_freq1=] [center_freq2=] [bandwidth=] "
Sunil Ravia04bd252022-05-02 22:54:18 -07001194 "[blocktx] [ht|vht|he|eht]\n");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001195 return -1;
1196 }
1197
1198 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1199 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001200 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001201 printf("Too long CHAN_SWITCH command.\n");
1202 return -1;
1203 }
1204
1205 total = res;
1206 for (i = 2; i < argc; i++) {
1207 tmp = cmd + total;
1208 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001209 if (os_snprintf_error(sizeof(cmd) - total, res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001210 printf("Too long CHAN_SWITCH command.\n");
1211 return -1;
1212 }
1213 total += res;
1214 }
1215 return wpa_ctrl_command(ctrl, cmd);
1216}
1217
1218
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001219static int hostapd_cli_cmd_notify_cw_change(struct wpa_ctrl *ctrl,
1220 int argc, char *argv[])
1221{
1222 return hostapd_cli_cmd(ctrl, "NOTIFY_CW_CHANGE", 1, argc, argv);
1223}
1224
1225
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001226static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001227 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001228{
1229 return wpa_ctrl_command(ctrl, "ENABLE");
1230}
1231
1232
1233static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001234 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001235{
1236 return wpa_ctrl_command(ctrl, "RELOAD");
1237}
1238
1239
Sunil Ravi77d572f2023-01-17 23:58:31 +00001240static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc,
1241 char *argv[])
1242{
1243 return wpa_ctrl_command(ctrl, "RELOAD_BSS");
1244}
1245
1246
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001247static int hostapd_cli_cmd_reload_config(struct wpa_ctrl *ctrl, int argc,
1248 char *argv[])
1249{
1250 return wpa_ctrl_command(ctrl, "RELOAD_CONFIG");
1251}
1252
1253
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001254static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001255 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001256{
1257 return wpa_ctrl_command(ctrl, "DISABLE");
1258}
1259
1260
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001261static int hostapd_cli_cmd_enable_mld(struct wpa_ctrl *ctrl, int argc,
1262 char *argv[])
1263{
1264 return wpa_ctrl_command(ctrl, "ENABLE_MLD");
1265}
1266
1267
1268static int hostapd_cli_cmd_disable_mld(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001269 char *argv[])
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001270{
1271 return wpa_ctrl_command(ctrl, "DISABLE_MLD");
1272}
1273
1274
Hai Shalom81f62d82019-07-22 12:10:00 -07001275static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001276 char *argv[])
Hai Shalom81f62d82019-07-22 12:10:00 -07001277{
1278 return wpa_ctrl_command(ctrl, "UPDATE_BEACON");
1279}
1280
1281
Sunil Ravi99c035e2024-07-12 01:42:03 +00001282static int hostapd_cli_cmd_stop_ap(struct wpa_ctrl *ctrl, int argc,
1283 char *argv[])
1284{
1285 return wpa_ctrl_command(ctrl, "STOP_AP");
1286}
1287
1288
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001289static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1290{
1291 char cmd[256];
1292 int res;
1293
Hai Shalom60840252021-02-19 19:02:11 -08001294 if (argc < 2 || argc > 4) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001295 printf("Invalid vendor command\n"
Hai Shalom60840252021-02-19 19:02:11 -08001296 "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001297 return -1;
1298 }
1299
Hai Shalom60840252021-02-19 19:02:11 -08001300 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0],
1301 argv[1], argc >= 3 ? argv[2] : "",
1302 argc == 4 ? " " : "", argc == 4 ? argv[3] : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001303 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001304 printf("Too long VENDOR command.\n");
1305 return -1;
1306 }
1307 return wpa_ctrl_command(ctrl, cmd);
1308}
1309
1310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1312 char *argv[])
1313{
1314 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1315}
1316
1317
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001318static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1319 char *argv[])
1320{
1321 char cmd[256];
1322 int res;
1323
1324 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1325 argc >= 1 ? " " : "",
1326 argc >= 1 ? argv[0] : "",
1327 argc == 2 ? " " : "",
1328 argc == 2 ? argv[1] : "");
1329 if (os_snprintf_error(sizeof(cmd), res)) {
1330 printf("Too long option\n");
1331 return -1;
1332 }
1333 return wpa_ctrl_command(ctrl, cmd);
1334}
1335
1336
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001337static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1338{
1339 if (argc == 0)
1340 return -1;
1341 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1342}
1343
1344
Dmitry Shmidte4663042016-04-04 10:07:49 -07001345static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1346{
1347 return wpa_ctrl_command(ctrl, "PMKSA");
1348}
1349
1350
1351static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1352 char *argv[])
1353{
1354 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1355}
1356
1357
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001358static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1359 char *argv[])
1360{
1361 char cmd[2048];
1362 int res;
1363
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001364 if (argc < 3 || argc > 6) {
1365 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001366 return -1;
1367 }
1368
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001369 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001370 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001371 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001372 if (os_snprintf_error(sizeof(cmd), res)) {
1373 printf("Too long SET_NEIGHBOR command.\n");
1374 return -1;
1375 }
1376 return wpa_ctrl_command(ctrl, cmd);
1377}
1378
1379
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001380static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc,
1381 char *argv[])
1382{
1383 return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR");
1384}
1385
1386
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001387static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1388 char *argv[])
1389{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001390 return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001391}
1392
1393
1394static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1395 char *argv[])
1396{
1397 char cmd[256];
1398 int res;
1399
1400 if (argc != 1) {
1401 printf("Invalid req_lci command - requires destination address\n");
1402 return -1;
1403 }
1404
1405 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1406 if (os_snprintf_error(sizeof(cmd), res)) {
1407 printf("Too long REQ_LCI command.\n");
1408 return -1;
1409 }
1410 return wpa_ctrl_command(ctrl, cmd);
1411}
1412
1413
1414static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1415 char *argv[])
1416{
1417 if (argc < 4) {
1418 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1419 return -1;
1420 }
1421
1422 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1423}
1424
1425
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001426static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1427 char *argv[])
1428{
1429 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1430}
1431
1432
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001433static int hostapd_cli_cmd_driver_flags2(struct wpa_ctrl *ctrl, int argc,
1434 char *argv[])
1435{
1436 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS2");
1437}
1438
1439
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001440#ifdef CONFIG_DPP
1441
1442static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
1443 char *argv[])
1444{
1445 return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
1446}
1447
1448
1449static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
1450 char *argv[])
1451{
1452 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
1453}
1454
1455
1456static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
1457 char *argv[])
1458{
1459 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
1460}
1461
1462
1463static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
1464 int argc, char *argv[])
1465{
1466 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
1467}
1468
1469
1470static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
1471 char *argv[])
1472{
1473 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
1474}
1475
1476
Hai Shalom899fcc72020-10-19 14:38:18 -07001477static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc,
1478 char *argv[])
1479{
1480 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv);
1481}
1482
1483
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001484static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
1485 char *argv[])
1486{
1487 return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
1488}
1489
1490
Roshan Pius3a1667e2018-07-03 15:17:14 -07001491static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
1492 char *argv[])
1493{
1494 return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
1495}
1496
1497
1498static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
1499 char *argv[])
1500{
1501 return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
1502}
1503
1504
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001505static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
1506 char *argv[])
1507{
1508 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
1509}
1510
1511
1512static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
1513 int argc, char *argv[])
1514{
1515 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
1516}
1517
1518
Roshan Pius3a1667e2018-07-03 15:17:14 -07001519static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl,
1520 int argc, char *argv[])
1521{
1522 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
1523}
1524
1525
Hai Shalom74f70d42019-02-11 14:42:39 -08001526static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl,
1527 int argc, char *argv[])
1528{
1529 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv);
1530}
1531
1532
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001533static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
1534 char *argv[])
1535{
1536 return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
1537}
1538
1539
1540static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
1541 char *argv[])
1542{
1543 return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
1544}
1545
Hai Shalom899fcc72020-10-19 14:38:18 -07001546
1547#ifdef CONFIG_DPP2
1548
Hai Shalom60840252021-02-19 19:02:11 -08001549static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc,
1550 char *argv[])
1551{
Sunil Ravi89eba102022-09-13 21:04:37 -07001552 return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv);
Hai Shalom60840252021-02-19 19:02:11 -08001553}
1554
1555
1556static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc,
1557 char *argv[])
1558{
1559 return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP");
1560}
1561
1562
Hai Shalom899fcc72020-10-19 14:38:18 -07001563static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc,
1564 char *argv[])
1565{
1566 return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv);
1567}
1568
1569
1570static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc,
1571 char *argv[])
1572{
1573 return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP");
1574}
1575
1576#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001577
1578
1579#ifdef CONFIG_DPP3
1580static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc,
1581 char *argv[])
1582{
Sunil Ravi640215c2023-06-28 23:08:09 +00001583 return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 0, argc, argv);
Sunil Ravi89eba102022-09-13 21:04:37 -07001584}
1585#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001586#endif /* CONFIG_DPP */
1587
1588
Roshan Pius3a1667e2018-07-03 15:17:14 -07001589static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc,
1590 char *argv[])
1591{
1592 return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv);
1593}
1594
1595
1596static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc,
1597 char *argv[])
1598{
1599 return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv);
1600}
1601
1602
1603static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc,
1604 char *argv[])
1605{
1606 return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv);
1607}
1608
1609
Hai Shalom74f70d42019-02-11 14:42:39 -08001610static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc,
1611 char *argv[])
1612{
1613 return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv);
1614}
1615
1616
Sunil Ravi99c035e2024-07-12 01:42:03 +00001617static int hostapd_cli_cmd_req_link_measurement(struct wpa_ctrl *ctrl, int argc,
1618 char *argv[])
1619{
1620 return hostapd_cli_cmd(ctrl, "REQ_LINK_MEASUREMENT", 1, argc, argv);
1621}
1622
1623
Hai Shalom74f70d42019-02-11 14:42:39 -08001624static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc,
1625 char *argv[])
1626{
1627 return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK");
1628}
1629
1630
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001631#ifdef CONFIG_IEEE80211R_AP
1632
1633static int hostapd_cli_cmd_get_rxkhs(struct wpa_ctrl *ctrl, int argc,
1634 char *argv[])
1635{
1636 return wpa_ctrl_command(ctrl, "GET_RXKHS");
1637}
1638
1639
1640static int hostapd_cli_cmd_reload_rxkhs(struct wpa_ctrl *ctrl, int argc,
1641 char *argv[])
1642{
1643 return wpa_ctrl_command(ctrl, "RELOAD_RXKHS");
1644}
1645
1646#endif /* CONFIG_IEEE80211R_AP */
1647
1648
Hai Shaloma20dcd72022-02-04 13:43:00 -08001649#ifdef ANDROID
1650static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
1651{
1652 return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
1653}
1654#endif /* ANDROID */
1655
1656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657struct hostapd_cli_cmd {
1658 const char *cmd;
1659 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001660 char ** (*completion)(const char *str, int pos);
1661 const char *usage;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662};
1663
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001664static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001665 { "ping", hostapd_cli_cmd_ping, NULL,
1666 "= pings hostapd" },
1667 { "mib", hostapd_cli_cmd_mib, NULL,
1668 "= get MIB variables (dot1x, dot11, radius)" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001669 { "relog", hostapd_cli_cmd_relog, NULL,
1670 "= reload/truncate debug log output file" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001671 { "close_log", hostapd_cli_cmd_close_log, NULL,
1672 "= disable debug log output file" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001673 { "status", hostapd_cli_cmd_status, NULL,
1674 "= show interface status info" },
1675 { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001676 "<addr> = get MIB variables for one station" },
1677 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1678 "= get MIB variables for all stations" },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001679 { "list_sta", hostapd_cli_cmd_list_sta, NULL,
1680 "= list all stations" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001681 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1682 "<addr> = add a new station" },
1683 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001684 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001685 "<addr> = deauthenticate a station" },
1686 { "disassociate", hostapd_cli_cmd_disassociate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001687 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001688 "<addr> = disassociate a station" },
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001689#ifdef CONFIG_TAXONOMY
Dmitry Shmidt29333592017-01-09 12:27:11 -08001690 { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001691 "<addr> = get taxonomy signature for a station" },
1692#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001693 { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001694 "<addr> = send SA Query to a station" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695#ifdef CONFIG_WPS
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001696 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1697 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1698 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1699 "<PIN> = verify PIN checksum" },
1700 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1701 "= indicate button pushed to initiate PBC" },
1702 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1703 "= cancel the pending WPS operation" },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001704#ifdef CONFIG_WPS_NFC
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001705 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1706 "<hexdump> = report read NFC tag with WPS data" },
1707 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1708 "<WPS/NDEF> = build NFC configuration token" },
1709 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1710 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1711 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1712 NULL },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001713#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001714 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1715 "<cmd> [params..] = enable/disable AP PIN" },
1716 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1717 "<SSID> <auth> <encr> <key> = configure AP" },
1718 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1719 "= show current WPS status" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720#endif /* CONFIG_WPS */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001721 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
1722 "= send Disassociation Imminent notification" },
1723 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
1724 "= send ESS Dissassociation Imminent notification" },
1725 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
1726 "= send BSS Transition Management Request" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001727 { "get_config", hostapd_cli_cmd_get_config, NULL,
1728 "= show current configuration" },
1729 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1730 "= show this usage help" },
1731 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1732 "[ifname] = show interfaces/select interface" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001733#ifdef CONFIG_FST
Dmitry Shmidt29333592017-01-09 12:27:11 -08001734 { "fst", hostapd_cli_cmd_fst, NULL,
1735 "<params...> = send FST-MANAGER control interface command" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001736#endif /* CONFIG_FST */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001737 { "raw", hostapd_cli_cmd_raw, NULL,
1738 "<params..> = send unprocessed command" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001739 { "level", hostapd_cli_cmd_level, NULL,
1740 "<debug level> = change debug level" },
1741 { "license", hostapd_cli_cmd_license, NULL,
1742 "= show full hostapd_cli license" },
1743 { "quit", hostapd_cli_cmd_quit, NULL,
1744 "= exit hostapd_cli" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001745 { "set", hostapd_cli_cmd_set, hostapd_complete_set,
1746 "<name> <value> = set runtime variables" },
1747 { "get", hostapd_cli_cmd_get, hostapd_complete_get,
1748 "<name> = get runtime info" },
1749 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
1750 "<arg,arg,...> = set QoS Map set element" },
1751 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
1752 hostapd_complete_stations,
1753 "<addr> = send QoS Map Configure frame" },
1754 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
1755 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1756 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1757 " = initiate channel switch announcement" },
Sunil Ravi7f769292024-07-23 22:21:32 +00001758#ifdef CONFIG_IEEE80211AX
1759 { "color_change", hostapd_cli_cmd_color_change, NULL,
1760 "<color> = initiate BSS color change to set the specified color\n"
1761 "Value 0 will disable the color.\n"},
1762#endif /* CONFIG_IEEE80211AX */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001763 { "notify_cw_change", hostapd_cli_cmd_notify_cw_change, NULL,
1764 "<channel_width> = 0 - 20 MHz, 1 - 40 MHz, 2 - 80 MHz, 3 - 160 MHz" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001765 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
1766 "<addr> <url>\n"
1767 " = send WNM-Notification Subscription Remediation Request" },
1768 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
1769 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1770 " = send WNM-Notification imminent deauthentication indication" },
1771 { "vendor", hostapd_cli_cmd_vendor, NULL,
1772 "<vendor id> <sub command id> [<hex formatted data>]\n"
1773 " = send vendor driver command" },
1774 { "enable", hostapd_cli_cmd_enable, NULL,
1775 "= enable hostapd on current interface" },
1776 { "reload", hostapd_cli_cmd_reload, NULL,
1777 "= reload configuration for current interface" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001778 { "reload_bss", hostapd_cli_cmd_reload_bss, NULL,
1779 "= reload configuration for current BSS" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001780 { "reload_config", hostapd_cli_cmd_reload_config, NULL,
1781 "= reload configuration for current interface" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001782 { "disable", hostapd_cli_cmd_disable, NULL,
1783 "= disable hostapd on current interface" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001784 { "enable_mld", hostapd_cli_cmd_enable_mld, NULL,
1785 "= enable AP MLD to which the interface is affiliated" },
1786 { "disable_mld", hostapd_cli_cmd_disable_mld, NULL,
1787 "= disable AP MLD to which the interface is affiliated" },
Hai Shalom81f62d82019-07-22 12:10:00 -07001788 { "update_beacon", hostapd_cli_cmd_update_beacon, NULL,
1789 "= update Beacon frame contents\n"},
Sunil Ravi99c035e2024-07-12 01:42:03 +00001790 { "stop_ap", hostapd_cli_cmd_stop_ap, NULL,
1791 "= stop AP\n"},
Dmitry Shmidt29333592017-01-09 12:27:11 -08001792 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
1793 "= drop all ERP keys"},
1794 { "log_level", hostapd_cli_cmd_log_level, NULL,
1795 "[level] = show/change log verbosity level" },
1796 { "pmksa", hostapd_cli_cmd_pmksa, NULL,
1797 " = show PMKSA cache entries" },
1798 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
1799 " = flush PMKSA cache" },
1800 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
1801 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1802 " = add AP to neighbor database" },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001803 { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL,
1804 " = show neighbor database entries" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001805 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001806 "<addr> [ssid=<hex>] = remove AP from neighbor database" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001807 { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
1808 "<addr> = send LCI request to a station"},
1809 { "req_range", hostapd_cli_cmd_req_range, NULL,
1810 " = send FTM range request"},
1811 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
1812 " = show supported driver flags"},
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001813 { "driver_flags2", hostapd_cli_cmd_driver_flags2, NULL,
1814 " = show supported driver flags2"},
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001815#ifdef CONFIG_DPP
1816 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
1817 "report a scanned DPP URI from a QR Code" },
1818 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
1819 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1820 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
1821 "*|<id> = remove DPP bootstrap information" },
1822 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
1823 "<id> = get DPP bootstrap URI" },
1824 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
1825 "<id> = show DPP bootstrap information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001826 { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL,
1827 "<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 Shmidtd2986c22017-10-23 14:22:09 -07001828 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
1829 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
Roshan Pius3a1667e2018-07-03 15:17:14 -07001830 { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
1831 "<freq in MHz> = start DPP listen" },
1832 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
1833 "= stop DPP listen" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001834 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
1835 "[curve=..] [key=..] = add DPP configurator" },
1836 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
1837 NULL,
1838 "*|<id> = remove DPP configurator" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001839 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001840 NULL,
1841 "<id> = Get DPP configurator's private key" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001842 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL,
1843 "conf=<role> configurator=<id> = generate self DPP configuration" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001844 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
1845 "add PKEX code" },
1846 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
1847 "*|<id> = remove DPP pkex information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001848#ifdef CONFIG_DPP2
Hai Shalom60840252021-02-19 19:02:11 -08001849 { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL,
1850 "[tcp_port=<port>] [role=..] = start DPP controller" },
1851 { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL,
1852 "= stop DPP controller" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001853 { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL,
1854 "own=<BI ID> iter=<count> = start DPP chirp" },
1855 { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL,
1856 "= stop DPP chirp" },
1857#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001858#ifdef CONFIG_DPP3
1859 { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL,
1860 "= press DPP push button" },
1861#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001862#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001863 { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL,
1864 "=Add/Delete/Show/Clear accept MAC ACL" },
1865 { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL,
1866 "=Add/Delete/Show/Clear deny MAC ACL" },
1867 { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations,
1868 "<addr> = poll a STA to check connectivity with a QoS null frame" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001869 { "req_beacon", hostapd_cli_cmd_req_beacon, NULL,
1870 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
Sunil Ravi99c035e2024-07-12 01:42:03 +00001871 { "req_link_measurement", hostapd_cli_cmd_req_link_measurement, NULL,
1872 "<addr> = send a link measurement report request to a station"},
Hai Shalom74f70d42019-02-11 14:42:39 -08001873 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL,
1874 "= reload wpa_psk_file only" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001875#ifdef CONFIG_IEEE80211R_AP
1876 { "reload_rxkhs", hostapd_cli_cmd_reload_rxkhs, NULL,
1877 "= reload R0KHs and R1KHs" },
1878 { "get_rxkhs", hostapd_cli_cmd_get_rxkhs, NULL,
1879 "= get R0KHs and R1KHs" },
1880#endif /* CONFIG_IEEE80211R_AP */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001881#ifdef ANDROID
1882 { "driver", hostapd_cli_cmd_driver, NULL,
1883 "<driver sub command> [<hex formatted data>] = send driver command data" },
1884#endif /* ANDROID */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001885 { NULL, NULL, NULL, NULL }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886};
1887
1888
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001889/*
1890 * Prints command usage, lines are padded with the specified string.
1891 */
1892static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1893 const char *pad)
1894{
1895 char c;
1896 size_t n;
1897
1898 if (cmd->usage == NULL)
1899 return;
1900 fprintf(stream, "%s%s ", pad, cmd->cmd);
1901 for (n = 0; (c = cmd->usage[n]); n++) {
1902 fprintf(stream, "%c", c);
1903 if (c == '\n')
1904 fprintf(stream, "%s", pad);
1905 }
1906 fprintf(stream, "\n");
1907}
1908
1909
1910static void print_help(FILE *stream, const char *cmd)
1911{
1912 int n;
1913
1914 fprintf(stream, "commands:\n");
1915 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1916 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1917 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1918 }
1919}
1920
1921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1923{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001924 const struct hostapd_cli_cmd *cmd, *match = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 int count;
1926
1927 count = 0;
1928 cmd = hostapd_cli_commands;
1929 while (cmd->cmd) {
1930 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1931 match = cmd;
1932 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1933 /* we have an exact match */
1934 count = 1;
1935 break;
1936 }
1937 count++;
1938 }
1939 cmd++;
1940 }
1941
1942 if (count > 1) {
1943 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1944 cmd = hostapd_cli_commands;
1945 while (cmd->cmd) {
1946 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1947 0) {
1948 printf(" %s", cmd->cmd);
1949 }
1950 cmd++;
1951 }
1952 printf("\n");
1953 } else if (count == 0) {
1954 printf("Unknown command '%s'\n", argv[0]);
1955 } else {
1956 match->handler(ctrl, argc - 1, &argv[1]);
1957 }
1958}
1959
1960
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001961static void cli_event(const char *str)
1962{
1963 const char *start, *s;
1964
1965 start = os_strchr(str, '>');
1966 if (start == NULL)
1967 return;
1968
1969 start++;
1970
1971 if (str_starts(start, AP_STA_CONNECTED)) {
1972 s = os_strchr(start, ' ');
1973 if (s == NULL)
1974 return;
1975 cli_txt_list_add(&stations, s + 1);
1976 return;
1977 }
1978
1979 if (str_starts(start, AP_STA_DISCONNECTED)) {
1980 s = os_strchr(start, ' ');
1981 if (s == NULL)
1982 return;
1983 cli_txt_list_del_addr(&stations, s + 1);
1984 return;
1985 }
1986}
1987
1988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1990 int action_monitor)
1991{
1992 int first = 1;
1993 if (ctrl_conn == NULL)
1994 return;
1995 while (wpa_ctrl_pending(ctrl)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001996 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 size_t len = sizeof(buf) - 1;
1998 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1999 buf[len] = '\0';
2000 if (action_monitor)
2001 hostapd_cli_action_process(buf, len);
2002 else {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002003 cli_event(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 if (in_read && first)
2005 printf("\n");
2006 first = 0;
2007 printf("%s\n", buf);
2008 }
2009 } else {
2010 printf("Could not read pending message.\n");
2011 break;
2012 }
2013 }
2014}
2015
2016
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002017static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002019 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020}
2021
2022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024{
2025 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
2026 printf("Connection to hostapd lost - trying to reconnect\n");
2027 hostapd_cli_close_connection();
2028 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002029 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
2030 printf("Connection to hostapd re-established\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 if (ctrl_conn)
2032 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002033 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2034}
2035
2036
2037static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
2038{
2039 eloop_terminate();
2040}
2041
2042
2043static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
2044{
2045 char *argv[max_args];
2046 int argc;
2047 argc = tokenize_cmd(cmd, argv);
2048 if (argc)
2049 wpa_request(ctrl_conn, argc, argv);
2050}
2051
2052
2053static void hostapd_cli_edit_eof_cb(void *ctx)
2054{
2055 eloop_terminate();
2056}
2057
2058
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002059static char ** list_cmd_list(void)
2060{
2061 char **res;
2062 int i, count;
2063
2064 count = ARRAY_SIZE(hostapd_cli_commands);
2065 res = os_calloc(count + 1, sizeof(char *));
2066 if (res == NULL)
2067 return NULL;
2068
2069 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
2070 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
2071 if (res[i] == NULL)
2072 break;
2073 }
2074
2075 return res;
2076}
2077
2078
2079static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
2080 int pos)
2081{
2082 int i;
2083
2084 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
2085 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
2086 continue;
2087 if (hostapd_cli_commands[i].completion)
2088 return hostapd_cli_commands[i].completion(str, pos);
2089 if (!hostapd_cli_commands[i].usage)
2090 return NULL;
2091 edit_clear_line();
2092 printf("\r%s\n", hostapd_cli_commands[i].usage);
2093 edit_redraw();
2094 break;
2095 }
2096
2097 return NULL;
2098}
2099
2100
2101static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
2102 int pos)
2103{
2104 char **res;
2105 const char *end;
2106 char *cmd;
2107
2108 end = os_strchr(str, ' ');
2109 if (end == NULL || str + pos < end)
2110 return list_cmd_list();
2111
2112 cmd = os_malloc(pos + 1);
2113 if (cmd == NULL)
2114 return NULL;
2115 os_memcpy(cmd, str, pos);
2116 cmd[end - str] = '\0';
2117 res = hostapd_cli_cmd_completion(cmd, str, pos);
2118 os_free(cmd);
2119 return res;
2120}
2121
2122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123static void hostapd_cli_interactive(void)
2124{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002125 char *hfile = NULL;
2126 char *home;
2127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002128 printf("\nInteractive mode\n\n");
2129
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002130#ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
2131 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
2132#else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2133 home = getenv("HOME");
2134#endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2135 if (home) {
2136 const char *fname = ".hostapd_cli_history";
2137 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
2138 hfile = os_malloc(hfile_len);
2139 if (hfile)
2140 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
2141 }
2142
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002144 hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002145 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2146
2147 eloop_run();
2148
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002149 cli_txt_list_flush(&stations);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002150 edit_deinit(hfile, NULL);
2151 os_free(hfile);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002152 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
2153}
2154
2155
2156static void hostapd_cli_cleanup(void)
2157{
2158 hostapd_cli_close_connection();
2159 if (pid_file)
2160 os_daemonize_terminate(pid_file);
2161
2162 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163}
2164
2165
Sunil Ravi036cec52023-03-29 11:35:17 -07002166static void hostapd_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167{
Sunil Ravi036cec52023-03-29 11:35:17 -07002168 struct wpa_ctrl *ctrl = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 char buf[256];
2170 size_t len;
2171
Sunil Ravi036cec52023-03-29 11:35:17 -07002172 /* verify that connection is still working */
2173 len = sizeof(buf) - 1;
2174 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
2175 hostapd_cli_action_cb) < 0 ||
2176 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
2177 printf("hostapd did not reply to PING command - exiting\n");
2178 eloop_terminate();
2179 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002181 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2182 ctrl, NULL);
2183}
2184
2185
2186static void hostapd_cli_action_receive(int sock, void *eloop_ctx,
2187 void *sock_ctx)
2188{
2189 struct wpa_ctrl *ctrl = eloop_ctx;
2190
2191 hostapd_cli_recv_pending(ctrl, 0, 1);
2192}
2193
2194
2195static void hostapd_cli_action(struct wpa_ctrl *ctrl)
2196{
2197 int fd;
2198
2199 fd = wpa_ctrl_get_fd(ctrl);
2200 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2201 ctrl, NULL);
2202 eloop_register_read_sock(fd, hostapd_cli_action_receive, ctrl, NULL);
2203 eloop_run();
2204 eloop_cancel_timeout(hostapd_cli_action_ping, ctrl, NULL);
2205 eloop_unregister_read_sock(fd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206}
2207
2208
2209int main(int argc, char *argv[])
2210{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 int warning_displayed = 0;
2212 int c;
2213 int daemonize = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002214 int reconnect = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215
2216 if (os_program_init())
2217 return -1;
2218
2219 for (;;) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002220 c = getopt(argc, argv, "a:BhG:i:p:P:rs:v");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 if (c < 0)
2222 break;
2223 switch (c) {
2224 case 'a':
2225 action_file = optarg;
2226 break;
2227 case 'B':
2228 daemonize = 1;
2229 break;
2230 case 'G':
2231 ping_interval = atoi(optarg);
2232 break;
2233 case 'h':
2234 usage();
2235 return 0;
2236 case 'v':
2237 printf("%s\n", hostapd_cli_version);
2238 return 0;
2239 case 'i':
2240 os_free(ctrl_ifname);
2241 ctrl_ifname = os_strdup(optarg);
2242 break;
2243 case 'p':
2244 ctrl_iface_dir = optarg;
2245 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002246 case 'P':
2247 pid_file = optarg;
2248 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002249 case 'r':
2250 reconnect = 1;
2251 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002252 case 's':
2253 client_socket_dir = optarg;
2254 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255 default:
2256 usage();
2257 return -1;
2258 }
2259 }
2260
2261 interactive = (argc == optind) && (action_file == NULL);
2262
2263 if (interactive) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002264 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 }
2266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 if (eloop_init())
2268 return -1;
2269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 for (;;) {
2271 if (ctrl_ifname == NULL) {
2272 struct dirent *dent;
2273 DIR *dir = opendir(ctrl_iface_dir);
2274 if (dir) {
2275 while ((dent = readdir(dir))) {
2276 if (os_strcmp(dent->d_name, ".") == 0
2277 ||
2278 os_strcmp(dent->d_name, "..") == 0)
2279 continue;
2280 printf("Selected interface '%s'\n",
2281 dent->d_name);
2282 ctrl_ifname = os_strdup(dent->d_name);
2283 break;
2284 }
2285 closedir(dir);
2286 }
2287 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002288 hostapd_cli_reconnect(ctrl_ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 if (ctrl_conn) {
2290 if (warning_displayed)
2291 printf("Connection established.\n");
2292 break;
2293 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002294 if (!interactive && !reconnect) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 perror("Failed to connect to hostapd - "
2296 "wpa_ctrl_open");
2297 return -1;
2298 }
2299
2300 if (!warning_displayed) {
2301 printf("Could not connect to hostapd - re-trying\n");
2302 warning_displayed = 1;
2303 }
2304 os_sleep(1, 0);
2305 continue;
2306 }
2307
Sunil Ravi036cec52023-03-29 11:35:17 -07002308 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
2309
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002310 if (action_file && !hostapd_cli_attached)
2311 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002312 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002314 if (reconnect && action_file && ctrl_ifname) {
2315 while (!hostapd_cli_quit) {
2316 if (ctrl_conn)
2317 hostapd_cli_action(ctrl_conn);
2318 os_sleep(1, 0);
2319 hostapd_cli_reconnect(ctrl_ifname);
2320 }
2321 } else if (interactive)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 hostapd_cli_interactive();
2323 else if (action_file)
2324 hostapd_cli_action(ctrl_conn);
2325 else
2326 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
2327
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002328 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002330 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 hostapd_cli_cleanup();
2332 return 0;
2333}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002334
2335#else /* CONFIG_NO_CTRL_IFACE */
2336
2337int main(int argc, char *argv[])
2338{
2339 return -1;
2340}
2341
2342#endif /* CONFIG_NO_CTRL_IFACE */