blob: 57702d93b249e04defb3068dbc915db1d819876f [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"
Sunil Ravic0f5d412024-09-11 22:12:49 +000057 "usage: hostapd_cli [-p<path>] [-i<ifname>] "
58#ifdef CONFIG_IEEE80211BE
59 "[-l<link_id>] "
60#endif /* CONFIG_IEEE80211BE */
61 "[-hvBr] "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 "[-a<path>] \\\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080063 " [-P<pid file>] [-G<ping interval>] [command..]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 "\n"
65 "Options:\n"
66 " -h help (show this usage text)\n"
67 " -v shown version information\n"
68 " -p<path> path to find control sockets (default: "
69 "/var/run/hostapd)\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080070 " -s<dir_path> dir path to open client sockets (default: "
71 CONFIG_CTRL_IFACE_DIR ")\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 " -a<file> run in daemon mode executing the action file "
73 "based on events\n"
74 " from hostapd\n"
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080075 " -r try to reconnect when client socket is "
76 "disconnected.\n"
77 " This is useful only when used with -a.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 " -B run a daemon in the background\n"
79 " -i<ifname> Interface to listen on (default: first "
80 "interface found in the\n"
Sunil Ravic0f5d412024-09-11 22:12:49 +000081 " socket path)\n"
82#ifdef CONFIG_IEEE80211BE
83 " -l<link_id> Link ID of the interface in case of Multi-Link Operation\n"
84#endif /* CONFIG_IEEE80211BE */
85 "\n");
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070086 print_help(stderr, NULL);
87}
88
89
90static void register_event_handler(struct wpa_ctrl *ctrl)
91{
92 if (!ctrl_conn)
93 return;
94 if (interactive) {
95 event_handler_registered =
96 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
97 hostapd_cli_receive,
98 NULL, NULL);
99 }
100}
101
102
103static void unregister_event_handler(struct wpa_ctrl *ctrl)
104{
105 if (!ctrl_conn)
106 return;
107 if (interactive && event_handler_registered) {
108 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
109 event_handler_registered = 0;
110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
115{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800116#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 char *cfile;
118 int flen;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800119#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120
121 if (ifname == NULL)
122 return NULL;
123
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800124#ifdef CONFIG_CTRL_IFACE_UDP
125 ctrl_conn = wpa_ctrl_open(ifname);
126 return ctrl_conn;
127#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
129 cfile = malloc(flen);
130 if (cfile == NULL)
131 return NULL;
132 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
133
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800134 if (client_socket_dir && client_socket_dir[0] &&
135 access(client_socket_dir, F_OK) < 0) {
136 perror(client_socket_dir);
137 free(cfile);
138 return NULL;
139 }
140
141 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 free(cfile);
143 return ctrl_conn;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800144#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145}
146
147
148static void hostapd_cli_close_connection(void)
149{
150 if (ctrl_conn == NULL)
151 return;
152
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700153 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 if (hostapd_cli_attached) {
155 wpa_ctrl_detach(ctrl_conn);
156 hostapd_cli_attached = 0;
157 }
158 wpa_ctrl_close(ctrl_conn);
159 ctrl_conn = NULL;
160}
161
162
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800163static int hostapd_cli_reconnect(const char *ifname)
164{
165 char *next_ctrl_ifname;
166
167 hostapd_cli_close_connection();
168
169 if (!ifname)
170 return -1;
171
172 next_ctrl_ifname = os_strdup(ifname);
173 os_free(ctrl_ifname);
174 ctrl_ifname = next_ctrl_ifname;
175 if (!ctrl_ifname)
176 return -1;
177
178 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
179 if (!ctrl_conn)
180 return -1;
181 if (!interactive && !action_file)
182 return 0;
183 if (wpa_ctrl_attach(ctrl_conn) == 0) {
184 hostapd_cli_attached = 1;
185 register_event_handler(ctrl_conn);
186 update_stations(ctrl_conn);
187 } else {
188 printf("Warning: Failed to attach to hostapd.\n");
189 }
190 return 0;
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void hostapd_cli_msg_cb(char *msg, size_t len)
195{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800196 cli_event(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197 printf("%s\n", msg);
198}
199
200
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800201static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202{
203 char buf[4096];
204 size_t len;
205 int ret;
206
207 if (ctrl_conn == NULL) {
208 printf("Not connected to hostapd - command dropped.\n");
209 return -1;
210 }
211 len = sizeof(buf) - 1;
212 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
213 hostapd_cli_msg_cb);
214 if (ret == -2) {
215 printf("'%s' command timed out.\n", cmd);
216 return -2;
217 } else if (ret < 0) {
218 printf("'%s' command failed.\n", cmd);
219 return -1;
220 }
221 if (print) {
222 buf[len] = '\0';
223 printf("%s", buf);
224 }
225 return 0;
226}
227
228
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800229static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230{
231 return _wpa_ctrl_command(ctrl, cmd, 1);
232}
233
234
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800235static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
236 int min_args, int argc, char *argv[])
237{
238 char buf[4096];
239
240 if (argc < min_args) {
241 printf("Invalid %s command - at least %d argument%s required.\n",
242 cmd, min_args, min_args > 1 ? "s are" : " is");
243 return -1;
244 }
245 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
246 return -1;
247 return wpa_ctrl_command(ctrl, buf);
248}
249
250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
252{
253 return wpa_ctrl_command(ctrl, "PING");
254}
255
256
257static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
258{
259 return wpa_ctrl_command(ctrl, "RELOG");
260}
261
262
Sunil Ravi77d572f2023-01-17 23:58:31 +0000263static int hostapd_cli_cmd_close_log(struct wpa_ctrl *ctrl, int argc,
264 char *argv[])
265{
266 return wpa_ctrl_command(ctrl, "CLOSE_LOG");
267}
268
269
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800270static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
271{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800272 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
273 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800274 return wpa_ctrl_command(ctrl, "STATUS");
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
279{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 if (argc > 0) {
281 char buf[100];
282 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
283 return wpa_ctrl_command(ctrl, buf);
284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 return wpa_ctrl_command(ctrl, "MIB");
286}
287
288
289static int hostapd_cli_exec(const char *program, const char *arg1,
290 const char *arg2)
291{
Jouni Malinen772e12c2014-10-07 10:29:35 -0700292 char *arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 size_t len;
294 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
Jouni Malinen772e12c2014-10-07 10:29:35 -0700296 len = os_strlen(arg1) + os_strlen(arg2) + 2;
297 arg = os_malloc(len);
298 if (arg == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 return -1;
Jouni Malinen772e12c2014-10-07 10:29:35 -0700300 os_snprintf(arg, len, "%s %s", arg1, arg2);
301 res = os_exec(program, arg, 1);
302 os_free(arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
Jouni Malinen772e12c2014-10-07 10:29:35 -0700304 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305}
306
307
308static void hostapd_cli_action_process(char *msg, size_t len)
309{
310 const char *pos;
311
312 pos = msg;
313 if (*pos == '<') {
314 pos = os_strchr(pos, '>');
315 if (pos)
316 pos++;
317 else
318 pos = msg;
319 }
320
321 hostapd_cli_exec(action_file, ctrl_ifname, pos);
322}
323
324
Sunil Ravi036cec52023-03-29 11:35:17 -0700325static void hostapd_cli_action_cb(char *msg, size_t len)
326{
327 hostapd_cli_action_process(msg, len);
328}
329
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
332{
333 char buf[64];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800334 if (argc < 1) {
335 printf("Invalid 'sta' command - at least one argument, STA "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 "address, is required.\n");
337 return -1;
338 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800339 if (argc > 1)
340 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
341 else
342 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 return wpa_ctrl_command(ctrl, buf);
344}
345
346
Dmitry Shmidt29333592017-01-09 12:27:11 -0800347static char ** hostapd_complete_stations(const char *str, int pos)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800348{
349 int arg = get_cmd_arg_num(str, pos);
350 char **res = NULL;
351
352 switch (arg) {
353 case 1:
354 res = cli_txt_list_array(&stations);
355 break;
356 }
357
358 return res;
359}
360
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
363 char *argv[])
364{
365 char buf[64];
366 if (argc != 1) {
367 printf("Invalid 'new_sta' command - exactly one argument, STA "
368 "address, is required.\n");
369 return -1;
370 }
371 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
372 return wpa_ctrl_command(ctrl, buf);
373}
374
375
376static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
377 char *argv[])
378{
379 char buf[64];
380 if (argc < 1) {
381 printf("Invalid 'deauthenticate' command - exactly one "
382 "argument, STA address, is required.\n");
383 return -1;
384 }
385 if (argc > 1)
386 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
387 argv[0], argv[1]);
388 else
389 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
390 return wpa_ctrl_command(ctrl, buf);
391}
392
393
394static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
395 char *argv[])
396{
397 char buf[64];
398 if (argc < 1) {
399 printf("Invalid 'disassociate' command - exactly one "
400 "argument, STA address, is required.\n");
401 return -1;
402 }
403 if (argc > 1)
404 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
405 argv[0], argv[1]);
406 else
407 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
408 return wpa_ctrl_command(ctrl, buf);
409}
410
411
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700412#ifdef CONFIG_TAXONOMY
413static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
414 char *argv[])
415{
416 char buf[64];
417
418 if (argc != 1) {
419 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
420 return -1;
421 }
422 os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
423 return wpa_ctrl_command(ctrl, buf);
424}
425#endif /* CONFIG_TAXONOMY */
426
427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
429 char *argv[])
430{
431 char buf[64];
432 if (argc != 1) {
433 printf("Invalid 'sa_query' command - exactly one argument, "
434 "STA address, is required.\n");
435 return -1;
436 }
437 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
438 return wpa_ctrl_command(ctrl, buf);
439}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440
441
442#ifdef CONFIG_WPS
443static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
444 char *argv[])
445{
446 char buf[256];
447 if (argc < 2) {
448 printf("Invalid 'wps_pin' command - at least two arguments, "
449 "UUID and PIN, are required.\n");
450 return -1;
451 }
452 if (argc > 3)
453 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
454 argv[0], argv[1], argv[2], argv[3]);
455 else if (argc > 2)
456 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
457 argv[0], argv[1], argv[2]);
458 else
459 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
460 return wpa_ctrl_command(ctrl, buf);
461}
462
463
464static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
465 char *argv[])
466{
467 char cmd[256];
468 int res;
469
470 if (argc != 1 && argc != 2) {
471 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
472 "- PIN to be verified\n");
473 return -1;
474 }
475
476 if (argc == 2)
477 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
478 argv[0], argv[1]);
479 else
480 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
481 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800482 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 printf("Too long WPS_CHECK_PIN command.\n");
484 return -1;
485 }
486 return wpa_ctrl_command(ctrl, cmd);
487}
488
489
490static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
491 char *argv[])
492{
493 return wpa_ctrl_command(ctrl, "WPS_PBC");
494}
495
496
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
498 char *argv[])
499{
500 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
501}
502
503
Dmitry Shmidt04949592012-07-19 12:16:46 -0700504#ifdef CONFIG_WPS_NFC
505static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
506 char *argv[])
507{
508 int ret;
509 char *buf;
510 size_t buflen;
511
512 if (argc != 1) {
513 printf("Invalid 'wps_nfc_tag_read' command - one argument "
514 "is required.\n");
515 return -1;
516 }
517
518 buflen = 18 + os_strlen(argv[0]);
519 buf = os_malloc(buflen);
520 if (buf == NULL)
521 return -1;
522 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
523
524 ret = wpa_ctrl_command(ctrl, buf);
525 os_free(buf);
526
527 return ret;
528}
529
530
531static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
532 int argc, char *argv[])
533{
534 char cmd[64];
535 int res;
536
537 if (argc != 1) {
538 printf("Invalid 'wps_nfc_config_token' command - one argument "
539 "is required.\n");
540 return -1;
541 }
542
543 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
544 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800545 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700546 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
547 return -1;
548 }
549 return wpa_ctrl_command(ctrl, cmd);
550}
551
552
553static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
554 int argc, char *argv[])
555{
556 char cmd[64];
557 int res;
558
559 if (argc != 1) {
560 printf("Invalid 'wps_nfc_token' command - one argument is "
561 "required.\n");
562 return -1;
563 }
564
565 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800566 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700567 printf("Too long WPS_NFC_TOKEN command.\n");
568 return -1;
569 }
570 return wpa_ctrl_command(ctrl, cmd);
571}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800572
573
574static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
575 int argc, char *argv[])
576{
577 char cmd[64];
578 int res;
579
580 if (argc != 2) {
581 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
582 "are required.\n");
583 return -1;
584 }
585
586 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
587 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800588 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800589 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
590 return -1;
591 }
592 return wpa_ctrl_command(ctrl, cmd);
593}
594
Dmitry Shmidt04949592012-07-19 12:16:46 -0700595#endif /* CONFIG_WPS_NFC */
596
597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
599 char *argv[])
600{
601 char buf[64];
602 if (argc < 1) {
603 printf("Invalid 'wps_ap_pin' command - at least one argument "
604 "is required.\n");
605 return -1;
606 }
607 if (argc > 2)
608 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
609 argv[0], argv[1], argv[2]);
610 else if (argc > 1)
611 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
612 argv[0], argv[1]);
613 else
614 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
615 return wpa_ctrl_command(ctrl, buf);
616}
617
618
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700619static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
620 char *argv[])
621{
622 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
623}
624
625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
627 char *argv[])
628{
629 char buf[256];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700630 char ssid_hex[2 * SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 char key_hex[2 * 64 + 1];
632 int i;
633
634 if (argc < 1) {
635 printf("Invalid 'wps_config' command - at least two arguments "
636 "are required.\n");
637 return -1;
638 }
639
640 ssid_hex[0] = '\0';
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700641 for (i = 0; i < SSID_MAX_LEN; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 if (argv[0][i] == '\0')
643 break;
644 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
645 }
646
647 key_hex[0] = '\0';
648 if (argc > 3) {
649 for (i = 0; i < 64; i++) {
650 if (argv[3][i] == '\0')
651 break;
652 os_snprintf(&key_hex[i * 2], 3, "%02x",
653 argv[3][i]);
654 }
655 }
656
657 if (argc > 3)
658 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
659 ssid_hex, argv[1], argv[2], key_hex);
660 else if (argc > 2)
661 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
662 ssid_hex, argv[1], argv[2]);
663 else
664 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
665 ssid_hex, argv[1]);
666 return wpa_ctrl_command(ctrl, buf);
667}
668#endif /* CONFIG_WPS */
669
670
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800671static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
672 char *argv[])
673{
674 char buf[300];
675 int res;
676
677 if (argc < 2) {
678 printf("Invalid 'disassoc_imminent' command - two arguments "
679 "(STA addr and Disassociation Timer) are needed\n");
680 return -1;
681 }
682
683 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
684 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800685 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800686 return -1;
687 return wpa_ctrl_command(ctrl, buf);
688}
689
690
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800691static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
692 char *argv[])
693{
694 char buf[300];
695 int res;
696
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700697 if (argc < 3) {
698 printf("Invalid 'ess_disassoc' command - three arguments (STA "
699 "addr, disassoc timer, and URL) are needed\n");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800700 return -1;
701 }
702
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700703 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
704 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800705 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800706 return -1;
707 return wpa_ctrl_command(ctrl, buf);
708}
709
710
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800711static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
712 char *argv[])
713{
714 char buf[2000], *tmp;
715 int res, i, total;
716
717 if (argc < 1) {
718 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
719 return -1;
720 }
721
722 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
723 if (os_snprintf_error(sizeof(buf), res))
724 return -1;
725
726 total = res;
727 for (i = 1; i < argc; i++) {
728 tmp = &buf[total];
729 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
730 if (os_snprintf_error(sizeof(buf) - total, res))
731 return -1;
732 total += res;
733 }
734 return wpa_ctrl_command(ctrl, buf);
735}
736
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
739 char *argv[])
740{
741 return wpa_ctrl_command(ctrl, "GET_CONFIG");
742}
743
744
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800745static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
746 char *addr, size_t addr_len, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747{
748 char buf[4096], *pos;
749 size_t len;
750 int ret;
751
752 if (ctrl_conn == NULL) {
753 printf("Not connected to hostapd - command dropped.\n");
754 return -1;
755 }
756 len = sizeof(buf) - 1;
757 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
758 hostapd_cli_msg_cb);
759 if (ret == -2) {
760 printf("'%s' command timed out.\n", cmd);
761 return -2;
762 } else if (ret < 0) {
763 printf("'%s' command failed.\n", cmd);
764 return -1;
765 }
766
767 buf[len] = '\0';
768 if (memcmp(buf, "FAIL", 4) == 0)
769 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800770 if (print)
771 printf("%s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772
773 pos = buf;
774 while (*pos != '\0' && *pos != '\n')
775 pos++;
776 *pos = '\0';
777 os_strlcpy(addr, buf, addr_len);
778 return 0;
779}
780
781
782static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
783 char *argv[])
784{
785 char addr[32], cmd[64];
786
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800787 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 return 0;
789 do {
790 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800791 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792
793 return -1;
794}
795
796
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800797static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
798 char *argv[])
799{
800 char addr[32], cmd[64];
801
802 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
803 return 0;
804 do {
805 if (os_strcmp(addr, "") != 0)
806 printf("%s\n", addr);
807 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
808 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
809
810 return 0;
811}
812
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
815{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700816 print_help(stdout, argc > 0 ? argv[0] : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 return 0;
818}
819
820
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700821static char ** hostapd_cli_complete_help(const char *str, int pos)
822{
823 int arg = get_cmd_arg_num(str, pos);
824 char **res = NULL;
825
826 switch (arg) {
827 case 1:
828 res = list_cmd_list();
829 break;
830 }
831
832 return res;
833}
834
835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
837 char *argv[])
838{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700839 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 return 0;
841}
842
843
Dmitry Shmidt051af732013-10-22 13:52:46 -0700844static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
845 int argc, char *argv[])
846{
847 char buf[200];
848 int res;
849
850 if (argc != 1) {
851 printf("Invalid 'set_qos_map_set' command - "
852 "one argument (comma delimited QoS map set) "
853 "is needed\n");
854 return -1;
855 }
856
857 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700859 return -1;
860 return wpa_ctrl_command(ctrl, buf);
861}
862
863
864static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
865 int argc, char *argv[])
866{
867 char buf[50];
868 int res;
869
870 if (argc != 1) {
871 printf("Invalid 'send_qos_map_conf' command - "
872 "one argument (STA addr) is needed\n");
873 return -1;
874 }
875
876 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700878 return -1;
879 return wpa_ctrl_command(ctrl, buf);
880}
881
882
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800883static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
884 char *argv[])
885{
886 char buf[300];
887 int res;
888
889 if (argc < 2) {
890 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
891 "addr and URL) are needed\n");
892 return -1;
893 }
894
895 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
896 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800897 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800898 return -1;
899 return wpa_ctrl_command(ctrl, buf);
900}
901
902
903static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
904 char *argv[])
905{
906 char buf[300];
907 int res;
908
909 if (argc < 3) {
910 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
911 return -1;
912 }
913
914 if (argc > 3)
915 res = os_snprintf(buf, sizeof(buf),
916 "HS20_DEAUTH_REQ %s %s %s %s",
917 argv[0], argv[1], argv[2], argv[3]);
918 else
919 res = os_snprintf(buf, sizeof(buf),
920 "HS20_DEAUTH_REQ %s %s %s",
921 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800922 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800923 return -1;
924 return wpa_ctrl_command(ctrl, buf);
925}
926
927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
929{
930 hostapd_cli_quit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800931 if (interactive)
932 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return 0;
934}
935
936
937static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
938{
939 char cmd[256];
940 if (argc != 1) {
941 printf("Invalid LEVEL command: needs one argument (debug "
942 "level)\n");
943 return 0;
944 }
945 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
946 return wpa_ctrl_command(ctrl, cmd);
947}
948
949
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800950static void update_stations(struct wpa_ctrl *ctrl)
951{
952 char addr[32], cmd[64];
953
954 if (!ctrl || !interactive)
955 return;
956
957 cli_txt_list_flush(&stations);
958
959 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
960 return;
961 do {
962 if (os_strcmp(addr, "") != 0)
963 cli_txt_list_add(&stations, addr);
964 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
965 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
966}
967
968
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700969static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
970 struct dl_list *interfaces)
971{
972 struct dirent *dent;
973 DIR *dir;
974
975 if (!ctrl || !interfaces)
976 return;
977 dir = opendir(ctrl_iface_dir);
978 if (dir == NULL)
979 return;
980
981 while ((dent = readdir(dir))) {
982 if (strcmp(dent->d_name, ".") == 0 ||
983 strcmp(dent->d_name, "..") == 0)
984 continue;
985 cli_txt_list_add(interfaces, dent->d_name);
986 }
987 closedir(dir);
988}
989
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
992{
993 struct dirent *dent;
994 DIR *dir;
995
996 dir = opendir(ctrl_iface_dir);
997 if (dir == NULL) {
998 printf("Control interface directory '%s' could not be "
Hai Shalom899fcc72020-10-19 14:38:18 -0700999 "opened.\n", ctrl_iface_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return;
1001 }
1002
1003 printf("Available interfaces:\n");
1004 while ((dent = readdir(dir))) {
1005 if (strcmp(dent->d_name, ".") == 0 ||
1006 strcmp(dent->d_name, "..") == 0)
1007 continue;
1008 printf("%s\n", dent->d_name);
1009 }
1010 closedir(dir);
1011}
1012
1013
1014static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
1015 char *argv[])
1016{
1017 if (argc < 1) {
1018 hostapd_cli_list_interfaces(ctrl);
1019 return 0;
1020 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001021 if (hostapd_cli_reconnect(argv[0]) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 printf("Could not connect to interface '%s' - re-trying\n",
1023 ctrl_ifname);
1024 }
1025 return 0;
1026}
1027
1028
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001029static char ** hostapd_complete_interface(const char *str, int pos)
1030{
1031 int arg = get_cmd_arg_num(str, pos);
1032 char **res = NULL;
1033 DEFINE_DL_LIST(interfaces);
1034
1035 switch (arg) {
1036 case 1:
1037 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1038 res = cli_txt_list_array(&interfaces);
1039 cli_txt_list_flush(&interfaces);
1040 break;
1041 }
1042
1043 return res;
1044}
1045
1046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1048{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001049 char cmd[2048];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 int res;
1051
1052 if (argc != 2) {
1053 printf("Invalid SET command: needs two arguments (variable "
1054 "name and value)\n");
1055 return -1;
1056 }
1057
1058 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001059 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 printf("Too long SET command.\n");
1061 return -1;
1062 }
1063 return wpa_ctrl_command(ctrl, cmd);
1064}
1065
1066
Dmitry Shmidt29333592017-01-09 12:27:11 -08001067static char ** hostapd_complete_set(const char *str, int pos)
1068{
1069 int arg = get_cmd_arg_num(str, pos);
1070 const char *fields[] = {
1071#ifdef CONFIG_WPS_TESTING
Hai Shaloma20dcd72022-02-04 13:43:00 -08001072 "wps_version_number", "wps_testing_stub_cred",
Dmitry Shmidt29333592017-01-09 12:27:11 -08001073 "wps_corrupt_pkhash",
1074#endif /* CONFIG_WPS_TESTING */
1075#ifdef CONFIG_INTERWORKING
1076 "gas_frag_limit",
1077#endif /* CONFIG_INTERWORKING */
1078#ifdef CONFIG_TESTING_OPTIONS
1079 "ext_mgmt_frame_handling", "ext_eapol_frame_io",
1080#endif /* CONFIG_TESTING_OPTIONS */
1081#ifdef CONFIG_MBO
1082 "mbo_assoc_disallow",
1083#endif /* CONFIG_MBO */
1084 "deny_mac_file", "accept_mac_file",
1085 };
1086 int i, num_fields = ARRAY_SIZE(fields);
1087
1088 if (arg == 1) {
1089 char **res;
1090
1091 res = os_calloc(num_fields + 1, sizeof(char *));
1092 if (!res)
1093 return NULL;
1094 for (i = 0; i < num_fields; i++) {
1095 res[i] = os_strdup(fields[i]);
1096 if (!res[i])
1097 return res;
1098 }
1099 return res;
1100 }
1101 return NULL;
1102}
1103
1104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1106{
1107 char cmd[256];
1108 int res;
1109
1110 if (argc != 1) {
1111 printf("Invalid GET command: needs one argument (variable "
1112 "name)\n");
1113 return -1;
1114 }
1115
1116 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001117 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 printf("Too long GET command.\n");
1119 return -1;
1120 }
1121 return wpa_ctrl_command(ctrl, cmd);
1122}
1123
1124
Dmitry Shmidt29333592017-01-09 12:27:11 -08001125static char ** hostapd_complete_get(const char *str, int pos)
1126{
1127 int arg = get_cmd_arg_num(str, pos);
1128 const char *fields[] = {
1129 "version", "tls_library",
1130 };
1131 int i, num_fields = ARRAY_SIZE(fields);
1132
1133 if (arg == 1) {
1134 char **res;
1135
1136 res = os_calloc(num_fields + 1, sizeof(char *));
1137 if (!res)
1138 return NULL;
1139 for (i = 0; i < num_fields; i++) {
1140 res[i] = os_strdup(fields[i]);
1141 if (!res[i])
1142 return res;
1143 }
1144 return res;
1145 }
1146 return NULL;
1147}
1148
1149
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001150#ifdef CONFIG_FST
1151static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1152{
1153 char cmd[256];
1154 int res;
1155 int i;
1156 int total;
1157
1158 if (argc <= 0) {
1159 printf("FST command: parameters are required.\n");
1160 return -1;
1161 }
1162
1163 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1164
1165 for (i = 0; i < argc; i++) {
1166 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1167 argv[i]);
1168 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1169 printf("Too long fst command.\n");
1170 return -1;
1171 }
1172 total += res;
1173 }
1174 return wpa_ctrl_command(ctrl, cmd);
1175}
1176#endif /* CONFIG_FST */
1177
1178
Sunil Ravi7f769292024-07-23 22:21:32 +00001179#ifdef CONFIG_IEEE80211AX
1180static int hostapd_cli_cmd_color_change(struct wpa_ctrl *ctrl,
1181 int argc, char *argv[])
1182{
1183 return hostapd_cli_cmd(ctrl, "COLOR_CHANGE", 1, argc, argv);
1184}
1185#endif /* CONFIG_IEEE80211AX */
1186
1187
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001188static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1189 int argc, char *argv[])
1190{
1191 char cmd[256];
1192 int res;
1193 int i;
1194 char *tmp;
1195 int total;
1196
1197 if (argc < 2) {
1198 printf("Invalid chan_switch command: needs at least two "
1199 "arguments (count and freq)\n"
1200 "usage: <cs_count> <freq> [sec_channel_offset=] "
1201 "[center_freq1=] [center_freq2=] [bandwidth=] "
Sunil Ravia04bd252022-05-02 22:54:18 -07001202 "[blocktx] [ht|vht|he|eht]\n");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001203 return -1;
1204 }
1205
1206 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1207 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001208 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001209 printf("Too long CHAN_SWITCH command.\n");
1210 return -1;
1211 }
1212
1213 total = res;
1214 for (i = 2; i < argc; i++) {
1215 tmp = cmd + total;
1216 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001217 if (os_snprintf_error(sizeof(cmd) - total, res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001218 printf("Too long CHAN_SWITCH command.\n");
1219 return -1;
1220 }
1221 total += res;
1222 }
1223 return wpa_ctrl_command(ctrl, cmd);
1224}
1225
1226
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001227static int hostapd_cli_cmd_notify_cw_change(struct wpa_ctrl *ctrl,
1228 int argc, char *argv[])
1229{
1230 return hostapd_cli_cmd(ctrl, "NOTIFY_CW_CHANGE", 1, argc, argv);
1231}
1232
1233
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001234static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001235 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001236{
1237 return wpa_ctrl_command(ctrl, "ENABLE");
1238}
1239
1240
1241static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001242 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001243{
1244 return wpa_ctrl_command(ctrl, "RELOAD");
1245}
1246
1247
Sunil Ravi77d572f2023-01-17 23:58:31 +00001248static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc,
1249 char *argv[])
1250{
1251 return wpa_ctrl_command(ctrl, "RELOAD_BSS");
1252}
1253
1254
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001255static int hostapd_cli_cmd_reload_config(struct wpa_ctrl *ctrl, int argc,
1256 char *argv[])
1257{
1258 return wpa_ctrl_command(ctrl, "RELOAD_CONFIG");
1259}
1260
1261
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001262static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001263 char *argv[])
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001264{
1265 return wpa_ctrl_command(ctrl, "DISABLE");
1266}
1267
1268
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001269static int hostapd_cli_cmd_enable_mld(struct wpa_ctrl *ctrl, int argc,
1270 char *argv[])
1271{
1272 return wpa_ctrl_command(ctrl, "ENABLE_MLD");
1273}
1274
1275
1276static int hostapd_cli_cmd_disable_mld(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001277 char *argv[])
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001278{
1279 return wpa_ctrl_command(ctrl, "DISABLE_MLD");
1280}
1281
1282
Hai Shalom81f62d82019-07-22 12:10:00 -07001283static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001284 char *argv[])
Hai Shalom81f62d82019-07-22 12:10:00 -07001285{
1286 return wpa_ctrl_command(ctrl, "UPDATE_BEACON");
1287}
1288
1289
Sunil Ravi99c035e2024-07-12 01:42:03 +00001290static int hostapd_cli_cmd_stop_ap(struct wpa_ctrl *ctrl, int argc,
1291 char *argv[])
1292{
1293 return wpa_ctrl_command(ctrl, "STOP_AP");
1294}
1295
1296
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001297static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1298{
1299 char cmd[256];
1300 int res;
1301
Hai Shalom60840252021-02-19 19:02:11 -08001302 if (argc < 2 || argc > 4) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001303 printf("Invalid vendor command\n"
Hai Shalom60840252021-02-19 19:02:11 -08001304 "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001305 return -1;
1306 }
1307
Hai Shalom60840252021-02-19 19:02:11 -08001308 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0],
1309 argv[1], argc >= 3 ? argv[2] : "",
1310 argc == 4 ? " " : "", argc == 4 ? argv[3] : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001312 printf("Too long VENDOR command.\n");
1313 return -1;
1314 }
1315 return wpa_ctrl_command(ctrl, cmd);
1316}
1317
1318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1320 char *argv[])
1321{
1322 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1323}
1324
1325
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001326static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1327 char *argv[])
1328{
1329 char cmd[256];
1330 int res;
1331
1332 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1333 argc >= 1 ? " " : "",
1334 argc >= 1 ? argv[0] : "",
1335 argc == 2 ? " " : "",
1336 argc == 2 ? argv[1] : "");
1337 if (os_snprintf_error(sizeof(cmd), res)) {
1338 printf("Too long option\n");
1339 return -1;
1340 }
1341 return wpa_ctrl_command(ctrl, cmd);
1342}
1343
1344
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001345static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1346{
1347 if (argc == 0)
1348 return -1;
1349 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1350}
1351
1352
Dmitry Shmidte4663042016-04-04 10:07:49 -07001353static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1354{
1355 return wpa_ctrl_command(ctrl, "PMKSA");
1356}
1357
1358
1359static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1360 char *argv[])
1361{
1362 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1363}
1364
1365
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001366static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1367 char *argv[])
1368{
1369 char cmd[2048];
1370 int res;
1371
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001372 if (argc < 3 || argc > 6) {
1373 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001374 return -1;
1375 }
1376
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001377 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001378 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001379 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001380 if (os_snprintf_error(sizeof(cmd), res)) {
1381 printf("Too long SET_NEIGHBOR command.\n");
1382 return -1;
1383 }
1384 return wpa_ctrl_command(ctrl, cmd);
1385}
1386
1387
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001388static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc,
1389 char *argv[])
1390{
1391 return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR");
1392}
1393
1394
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001395static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1396 char *argv[])
1397{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001398 return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001399}
1400
1401
1402static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1403 char *argv[])
1404{
1405 char cmd[256];
1406 int res;
1407
1408 if (argc != 1) {
1409 printf("Invalid req_lci command - requires destination address\n");
1410 return -1;
1411 }
1412
1413 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1414 if (os_snprintf_error(sizeof(cmd), res)) {
1415 printf("Too long REQ_LCI command.\n");
1416 return -1;
1417 }
1418 return wpa_ctrl_command(ctrl, cmd);
1419}
1420
1421
1422static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1423 char *argv[])
1424{
1425 if (argc < 4) {
1426 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1427 return -1;
1428 }
1429
1430 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1431}
1432
1433
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001434static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1435 char *argv[])
1436{
1437 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1438}
1439
1440
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001441static int hostapd_cli_cmd_driver_flags2(struct wpa_ctrl *ctrl, int argc,
1442 char *argv[])
1443{
1444 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS2");
1445}
1446
1447
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001448#ifdef CONFIG_DPP
1449
1450static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
1451 char *argv[])
1452{
1453 return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
1454}
1455
1456
1457static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
1458 char *argv[])
1459{
1460 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
1461}
1462
1463
1464static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
1465 char *argv[])
1466{
1467 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
1468}
1469
1470
1471static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
1472 int argc, char *argv[])
1473{
1474 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
1475}
1476
1477
1478static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
1479 char *argv[])
1480{
1481 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
1482}
1483
1484
Hai Shalom899fcc72020-10-19 14:38:18 -07001485static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc,
1486 char *argv[])
1487{
1488 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv);
1489}
1490
1491
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001492static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
1493 char *argv[])
1494{
1495 return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
1496}
1497
1498
Roshan Pius3a1667e2018-07-03 15:17:14 -07001499static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
1500 char *argv[])
1501{
1502 return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
1503}
1504
1505
1506static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
1507 char *argv[])
1508{
1509 return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
1510}
1511
1512
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001513static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
1514 char *argv[])
1515{
1516 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
1517}
1518
1519
1520static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
1521 int argc, char *argv[])
1522{
1523 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
1524}
1525
1526
Roshan Pius3a1667e2018-07-03 15:17:14 -07001527static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl,
1528 int argc, char *argv[])
1529{
1530 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
1531}
1532
1533
Hai Shalom74f70d42019-02-11 14:42:39 -08001534static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl,
1535 int argc, char *argv[])
1536{
1537 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv);
1538}
1539
1540
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001541static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
1542 char *argv[])
1543{
1544 return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
1545}
1546
1547
1548static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
1549 char *argv[])
1550{
1551 return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
1552}
1553
Hai Shalom899fcc72020-10-19 14:38:18 -07001554
1555#ifdef CONFIG_DPP2
1556
Hai Shalom60840252021-02-19 19:02:11 -08001557static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc,
1558 char *argv[])
1559{
Sunil Ravi89eba102022-09-13 21:04:37 -07001560 return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv);
Hai Shalom60840252021-02-19 19:02:11 -08001561}
1562
1563
1564static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc,
1565 char *argv[])
1566{
1567 return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP");
1568}
1569
1570
Hai Shalom899fcc72020-10-19 14:38:18 -07001571static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc,
1572 char *argv[])
1573{
1574 return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv);
1575}
1576
1577
1578static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc,
1579 char *argv[])
1580{
1581 return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP");
1582}
1583
1584#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001585
1586
1587#ifdef CONFIG_DPP3
1588static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc,
1589 char *argv[])
1590{
Sunil Ravi640215c2023-06-28 23:08:09 +00001591 return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 0, argc, argv);
Sunil Ravi89eba102022-09-13 21:04:37 -07001592}
1593#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001594#endif /* CONFIG_DPP */
1595
1596
Roshan Pius3a1667e2018-07-03 15:17:14 -07001597static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc,
1598 char *argv[])
1599{
1600 return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv);
1601}
1602
1603
1604static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc,
1605 char *argv[])
1606{
1607 return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv);
1608}
1609
1610
1611static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc,
1612 char *argv[])
1613{
1614 return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv);
1615}
1616
1617
Hai Shalom74f70d42019-02-11 14:42:39 -08001618static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc,
1619 char *argv[])
1620{
1621 return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv);
1622}
1623
1624
Sunil Ravi99c035e2024-07-12 01:42:03 +00001625static int hostapd_cli_cmd_req_link_measurement(struct wpa_ctrl *ctrl, int argc,
1626 char *argv[])
1627{
1628 return hostapd_cli_cmd(ctrl, "REQ_LINK_MEASUREMENT", 1, argc, argv);
1629}
1630
1631
Hai Shalom74f70d42019-02-11 14:42:39 -08001632static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc,
1633 char *argv[])
1634{
1635 return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK");
1636}
1637
1638
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001639#ifdef CONFIG_IEEE80211R_AP
1640
1641static int hostapd_cli_cmd_get_rxkhs(struct wpa_ctrl *ctrl, int argc,
1642 char *argv[])
1643{
1644 return wpa_ctrl_command(ctrl, "GET_RXKHS");
1645}
1646
1647
1648static int hostapd_cli_cmd_reload_rxkhs(struct wpa_ctrl *ctrl, int argc,
1649 char *argv[])
1650{
1651 return wpa_ctrl_command(ctrl, "RELOAD_RXKHS");
1652}
1653
1654#endif /* CONFIG_IEEE80211R_AP */
1655
1656
Hai Shaloma20dcd72022-02-04 13:43:00 -08001657#ifdef ANDROID
1658static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
1659{
1660 return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
1661}
1662#endif /* ANDROID */
1663
1664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665struct hostapd_cli_cmd {
1666 const char *cmd;
1667 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001668 char ** (*completion)(const char *str, int pos);
1669 const char *usage;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670};
1671
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001672static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001673 { "ping", hostapd_cli_cmd_ping, NULL,
1674 "= pings hostapd" },
1675 { "mib", hostapd_cli_cmd_mib, NULL,
1676 "= get MIB variables (dot1x, dot11, radius)" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001677 { "relog", hostapd_cli_cmd_relog, NULL,
1678 "= reload/truncate debug log output file" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001679 { "close_log", hostapd_cli_cmd_close_log, NULL,
1680 "= disable debug log output file" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001681 { "status", hostapd_cli_cmd_status, NULL,
1682 "= show interface status info" },
1683 { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001684 "<addr> = get MIB variables for one station" },
1685 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1686 "= get MIB variables for all stations" },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001687 { "list_sta", hostapd_cli_cmd_list_sta, NULL,
1688 "= list all stations" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001689 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1690 "<addr> = add a new station" },
1691 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001692 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001693 "<addr> = deauthenticate a station" },
1694 { "disassociate", hostapd_cli_cmd_disassociate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001695 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001696 "<addr> = disassociate a station" },
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001697#ifdef CONFIG_TAXONOMY
Dmitry Shmidt29333592017-01-09 12:27:11 -08001698 { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001699 "<addr> = get taxonomy signature for a station" },
1700#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001701 { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001702 "<addr> = send SA Query to a station" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703#ifdef CONFIG_WPS
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001704 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1705 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1706 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1707 "<PIN> = verify PIN checksum" },
1708 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1709 "= indicate button pushed to initiate PBC" },
1710 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1711 "= cancel the pending WPS operation" },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001712#ifdef CONFIG_WPS_NFC
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001713 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1714 "<hexdump> = report read NFC tag with WPS data" },
1715 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1716 "<WPS/NDEF> = build NFC configuration token" },
1717 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1718 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1719 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1720 NULL },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001721#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001722 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1723 "<cmd> [params..] = enable/disable AP PIN" },
1724 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1725 "<SSID> <auth> <encr> <key> = configure AP" },
1726 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1727 "= show current WPS status" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728#endif /* CONFIG_WPS */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001729 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
1730 "= send Disassociation Imminent notification" },
1731 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
1732 "= send ESS Dissassociation Imminent notification" },
1733 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
1734 "= send BSS Transition Management Request" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001735 { "get_config", hostapd_cli_cmd_get_config, NULL,
1736 "= show current configuration" },
1737 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1738 "= show this usage help" },
1739 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1740 "[ifname] = show interfaces/select interface" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001741#ifdef CONFIG_FST
Dmitry Shmidt29333592017-01-09 12:27:11 -08001742 { "fst", hostapd_cli_cmd_fst, NULL,
1743 "<params...> = send FST-MANAGER control interface command" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001744#endif /* CONFIG_FST */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001745 { "raw", hostapd_cli_cmd_raw, NULL,
1746 "<params..> = send unprocessed command" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001747 { "level", hostapd_cli_cmd_level, NULL,
1748 "<debug level> = change debug level" },
1749 { "license", hostapd_cli_cmd_license, NULL,
1750 "= show full hostapd_cli license" },
1751 { "quit", hostapd_cli_cmd_quit, NULL,
1752 "= exit hostapd_cli" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001753 { "set", hostapd_cli_cmd_set, hostapd_complete_set,
1754 "<name> <value> = set runtime variables" },
1755 { "get", hostapd_cli_cmd_get, hostapd_complete_get,
1756 "<name> = get runtime info" },
1757 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
1758 "<arg,arg,...> = set QoS Map set element" },
1759 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
1760 hostapd_complete_stations,
1761 "<addr> = send QoS Map Configure frame" },
1762 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
1763 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1764 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1765 " = initiate channel switch announcement" },
Sunil Ravi7f769292024-07-23 22:21:32 +00001766#ifdef CONFIG_IEEE80211AX
1767 { "color_change", hostapd_cli_cmd_color_change, NULL,
1768 "<color> = initiate BSS color change to set the specified color\n"
1769 "Value 0 will disable the color.\n"},
1770#endif /* CONFIG_IEEE80211AX */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001771 { "notify_cw_change", hostapd_cli_cmd_notify_cw_change, NULL,
1772 "<channel_width> = 0 - 20 MHz, 1 - 40 MHz, 2 - 80 MHz, 3 - 160 MHz" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001773 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
1774 "<addr> <url>\n"
1775 " = send WNM-Notification Subscription Remediation Request" },
1776 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
1777 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1778 " = send WNM-Notification imminent deauthentication indication" },
1779 { "vendor", hostapd_cli_cmd_vendor, NULL,
1780 "<vendor id> <sub command id> [<hex formatted data>]\n"
1781 " = send vendor driver command" },
1782 { "enable", hostapd_cli_cmd_enable, NULL,
1783 "= enable hostapd on current interface" },
1784 { "reload", hostapd_cli_cmd_reload, NULL,
1785 "= reload configuration for current interface" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001786 { "reload_bss", hostapd_cli_cmd_reload_bss, NULL,
1787 "= reload configuration for current BSS" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001788 { "reload_config", hostapd_cli_cmd_reload_config, NULL,
1789 "= reload configuration for current interface" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001790 { "disable", hostapd_cli_cmd_disable, NULL,
1791 "= disable hostapd on current interface" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001792 { "enable_mld", hostapd_cli_cmd_enable_mld, NULL,
1793 "= enable AP MLD to which the interface is affiliated" },
1794 { "disable_mld", hostapd_cli_cmd_disable_mld, NULL,
1795 "= disable AP MLD to which the interface is affiliated" },
Hai Shalom81f62d82019-07-22 12:10:00 -07001796 { "update_beacon", hostapd_cli_cmd_update_beacon, NULL,
1797 "= update Beacon frame contents\n"},
Sunil Ravi99c035e2024-07-12 01:42:03 +00001798 { "stop_ap", hostapd_cli_cmd_stop_ap, NULL,
1799 "= stop AP\n"},
Dmitry Shmidt29333592017-01-09 12:27:11 -08001800 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
1801 "= drop all ERP keys"},
1802 { "log_level", hostapd_cli_cmd_log_level, NULL,
1803 "[level] = show/change log verbosity level" },
1804 { "pmksa", hostapd_cli_cmd_pmksa, NULL,
1805 " = show PMKSA cache entries" },
1806 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
1807 " = flush PMKSA cache" },
1808 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
1809 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1810 " = add AP to neighbor database" },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001811 { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL,
1812 " = show neighbor database entries" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001813 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001814 "<addr> [ssid=<hex>] = remove AP from neighbor database" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001815 { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
1816 "<addr> = send LCI request to a station"},
1817 { "req_range", hostapd_cli_cmd_req_range, NULL,
1818 " = send FTM range request"},
1819 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
1820 " = show supported driver flags"},
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001821 { "driver_flags2", hostapd_cli_cmd_driver_flags2, NULL,
1822 " = show supported driver flags2"},
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001823#ifdef CONFIG_DPP
1824 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
1825 "report a scanned DPP URI from a QR Code" },
1826 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
1827 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1828 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
1829 "*|<id> = remove DPP bootstrap information" },
1830 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
1831 "<id> = get DPP bootstrap URI" },
1832 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
1833 "<id> = show DPP bootstrap information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001834 { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL,
1835 "<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 -07001836 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
1837 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
Roshan Pius3a1667e2018-07-03 15:17:14 -07001838 { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
1839 "<freq in MHz> = start DPP listen" },
1840 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
1841 "= stop DPP listen" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001842 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
1843 "[curve=..] [key=..] = add DPP configurator" },
1844 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
1845 NULL,
1846 "*|<id> = remove DPP configurator" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001847 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001848 NULL,
1849 "<id> = Get DPP configurator's private key" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001850 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL,
1851 "conf=<role> configurator=<id> = generate self DPP configuration" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001852 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
1853 "add PKEX code" },
1854 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
1855 "*|<id> = remove DPP pkex information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001856#ifdef CONFIG_DPP2
Hai Shalom60840252021-02-19 19:02:11 -08001857 { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL,
1858 "[tcp_port=<port>] [role=..] = start DPP controller" },
1859 { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL,
1860 "= stop DPP controller" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001861 { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL,
1862 "own=<BI ID> iter=<count> = start DPP chirp" },
1863 { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL,
1864 "= stop DPP chirp" },
1865#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001866#ifdef CONFIG_DPP3
1867 { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL,
1868 "= press DPP push button" },
1869#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001870#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001871 { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL,
1872 "=Add/Delete/Show/Clear accept MAC ACL" },
1873 { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL,
1874 "=Add/Delete/Show/Clear deny MAC ACL" },
1875 { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations,
1876 "<addr> = poll a STA to check connectivity with a QoS null frame" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001877 { "req_beacon", hostapd_cli_cmd_req_beacon, NULL,
1878 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
Sunil Ravi99c035e2024-07-12 01:42:03 +00001879 { "req_link_measurement", hostapd_cli_cmd_req_link_measurement, NULL,
1880 "<addr> = send a link measurement report request to a station"},
Hai Shalom74f70d42019-02-11 14:42:39 -08001881 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL,
1882 "= reload wpa_psk_file only" },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001883#ifdef CONFIG_IEEE80211R_AP
1884 { "reload_rxkhs", hostapd_cli_cmd_reload_rxkhs, NULL,
1885 "= reload R0KHs and R1KHs" },
1886 { "get_rxkhs", hostapd_cli_cmd_get_rxkhs, NULL,
1887 "= get R0KHs and R1KHs" },
1888#endif /* CONFIG_IEEE80211R_AP */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001889#ifdef ANDROID
1890 { "driver", hostapd_cli_cmd_driver, NULL,
1891 "<driver sub command> [<hex formatted data>] = send driver command data" },
1892#endif /* ANDROID */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001893 { NULL, NULL, NULL, NULL }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894};
1895
1896
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001897/*
1898 * Prints command usage, lines are padded with the specified string.
1899 */
1900static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1901 const char *pad)
1902{
1903 char c;
1904 size_t n;
1905
1906 if (cmd->usage == NULL)
1907 return;
1908 fprintf(stream, "%s%s ", pad, cmd->cmd);
1909 for (n = 0; (c = cmd->usage[n]); n++) {
1910 fprintf(stream, "%c", c);
1911 if (c == '\n')
1912 fprintf(stream, "%s", pad);
1913 }
1914 fprintf(stream, "\n");
1915}
1916
1917
1918static void print_help(FILE *stream, const char *cmd)
1919{
1920 int n;
1921
1922 fprintf(stream, "commands:\n");
1923 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1924 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1925 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1926 }
1927}
1928
1929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1931{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001932 const struct hostapd_cli_cmd *cmd, *match = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 int count;
1934
1935 count = 0;
1936 cmd = hostapd_cli_commands;
1937 while (cmd->cmd) {
1938 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1939 match = cmd;
1940 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1941 /* we have an exact match */
1942 count = 1;
1943 break;
1944 }
1945 count++;
1946 }
1947 cmd++;
1948 }
1949
1950 if (count > 1) {
1951 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1952 cmd = hostapd_cli_commands;
1953 while (cmd->cmd) {
1954 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1955 0) {
1956 printf(" %s", cmd->cmd);
1957 }
1958 cmd++;
1959 }
1960 printf("\n");
1961 } else if (count == 0) {
1962 printf("Unknown command '%s'\n", argv[0]);
1963 } else {
1964 match->handler(ctrl, argc - 1, &argv[1]);
1965 }
1966}
1967
1968
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001969static void cli_event(const char *str)
1970{
1971 const char *start, *s;
1972
1973 start = os_strchr(str, '>');
1974 if (start == NULL)
1975 return;
1976
1977 start++;
1978
1979 if (str_starts(start, AP_STA_CONNECTED)) {
1980 s = os_strchr(start, ' ');
1981 if (s == NULL)
1982 return;
1983 cli_txt_list_add(&stations, s + 1);
1984 return;
1985 }
1986
1987 if (str_starts(start, AP_STA_DISCONNECTED)) {
1988 s = os_strchr(start, ' ');
1989 if (s == NULL)
1990 return;
1991 cli_txt_list_del_addr(&stations, s + 1);
1992 return;
1993 }
1994}
1995
1996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1998 int action_monitor)
1999{
2000 int first = 1;
2001 if (ctrl_conn == NULL)
2002 return;
2003 while (wpa_ctrl_pending(ctrl)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002004 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 size_t len = sizeof(buf) - 1;
2006 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
2007 buf[len] = '\0';
2008 if (action_monitor)
2009 hostapd_cli_action_process(buf, len);
2010 else {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002011 cli_event(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 if (in_read && first)
2013 printf("\n");
2014 first = 0;
2015 printf("%s\n", buf);
2016 }
2017 } else {
2018 printf("Could not read pending message.\n");
2019 break;
2020 }
2021 }
2022}
2023
2024
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002025static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002027 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028}
2029
2030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002031static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032{
2033 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
2034 printf("Connection to hostapd lost - trying to reconnect\n");
2035 hostapd_cli_close_connection();
2036 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002037 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
2038 printf("Connection to hostapd re-established\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 if (ctrl_conn)
2040 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2042}
2043
2044
2045static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
2046{
2047 eloop_terminate();
2048}
2049
2050
2051static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
2052{
2053 char *argv[max_args];
2054 int argc;
2055 argc = tokenize_cmd(cmd, argv);
2056 if (argc)
2057 wpa_request(ctrl_conn, argc, argv);
2058}
2059
2060
2061static void hostapd_cli_edit_eof_cb(void *ctx)
2062{
2063 eloop_terminate();
2064}
2065
2066
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002067static char ** list_cmd_list(void)
2068{
2069 char **res;
2070 int i, count;
2071
2072 count = ARRAY_SIZE(hostapd_cli_commands);
2073 res = os_calloc(count + 1, sizeof(char *));
2074 if (res == NULL)
2075 return NULL;
2076
2077 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
2078 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
2079 if (res[i] == NULL)
2080 break;
2081 }
2082
2083 return res;
2084}
2085
2086
2087static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
2088 int pos)
2089{
2090 int i;
2091
2092 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
2093 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
2094 continue;
2095 if (hostapd_cli_commands[i].completion)
2096 return hostapd_cli_commands[i].completion(str, pos);
2097 if (!hostapd_cli_commands[i].usage)
2098 return NULL;
2099 edit_clear_line();
2100 printf("\r%s\n", hostapd_cli_commands[i].usage);
2101 edit_redraw();
2102 break;
2103 }
2104
2105 return NULL;
2106}
2107
2108
2109static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
2110 int pos)
2111{
2112 char **res;
2113 const char *end;
2114 char *cmd;
2115
2116 end = os_strchr(str, ' ');
2117 if (end == NULL || str + pos < end)
2118 return list_cmd_list();
2119
2120 cmd = os_malloc(pos + 1);
2121 if (cmd == NULL)
2122 return NULL;
2123 os_memcpy(cmd, str, pos);
2124 cmd[end - str] = '\0';
2125 res = hostapd_cli_cmd_completion(cmd, str, pos);
2126 os_free(cmd);
2127 return res;
2128}
2129
2130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131static void hostapd_cli_interactive(void)
2132{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002133 char *hfile = NULL;
2134 char *home;
2135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136 printf("\nInteractive mode\n\n");
2137
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002138#ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
2139 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
2140#else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2141 home = getenv("HOME");
2142#endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2143 if (home) {
2144 const char *fname = ".hostapd_cli_history";
2145 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
2146 hfile = os_malloc(hfile_len);
2147 if (hfile)
2148 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
2149 }
2150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002151 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002152 hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002153 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2154
2155 eloop_run();
2156
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002157 cli_txt_list_flush(&stations);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002158 edit_deinit(hfile, NULL);
2159 os_free(hfile);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002160 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
2161}
2162
2163
2164static void hostapd_cli_cleanup(void)
2165{
2166 hostapd_cli_close_connection();
2167 if (pid_file)
2168 os_daemonize_terminate(pid_file);
2169
2170 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171}
2172
2173
Sunil Ravi036cec52023-03-29 11:35:17 -07002174static void hostapd_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175{
Sunil Ravi036cec52023-03-29 11:35:17 -07002176 struct wpa_ctrl *ctrl = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 char buf[256];
2178 size_t len;
2179
Sunil Ravi036cec52023-03-29 11:35:17 -07002180 /* verify that connection is still working */
2181 len = sizeof(buf) - 1;
2182 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
2183 hostapd_cli_action_cb) < 0 ||
2184 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
2185 printf("hostapd did not reply to PING command - exiting\n");
2186 eloop_terminate();
2187 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002189 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2190 ctrl, NULL);
2191}
2192
2193
2194static void hostapd_cli_action_receive(int sock, void *eloop_ctx,
2195 void *sock_ctx)
2196{
2197 struct wpa_ctrl *ctrl = eloop_ctx;
2198
2199 hostapd_cli_recv_pending(ctrl, 0, 1);
2200}
2201
2202
2203static void hostapd_cli_action(struct wpa_ctrl *ctrl)
2204{
2205 int fd;
2206
2207 fd = wpa_ctrl_get_fd(ctrl);
2208 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2209 ctrl, NULL);
2210 eloop_register_read_sock(fd, hostapd_cli_action_receive, ctrl, NULL);
2211 eloop_run();
2212 eloop_cancel_timeout(hostapd_cli_action_ping, ctrl, NULL);
2213 eloop_unregister_read_sock(fd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214}
2215
2216
2217int main(int argc, char *argv[])
2218{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 int warning_displayed = 0;
2220 int c;
2221 int daemonize = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002222 int reconnect = 0;
Sunil Ravic0f5d412024-09-11 22:12:49 +00002223#ifdef CONFIG_IEEE80211BE
2224 int link_id = -1;
2225#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226
2227 if (os_program_init())
2228 return -1;
2229
2230 for (;;) {
Sunil Ravic0f5d412024-09-11 22:12:49 +00002231 c = getopt(argc, argv, "a:BhG:i:l:p:P:rs:v");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 if (c < 0)
2233 break;
2234 switch (c) {
2235 case 'a':
2236 action_file = optarg;
2237 break;
2238 case 'B':
2239 daemonize = 1;
2240 break;
2241 case 'G':
2242 ping_interval = atoi(optarg);
2243 break;
2244 case 'h':
2245 usage();
2246 return 0;
2247 case 'v':
2248 printf("%s\n", hostapd_cli_version);
2249 return 0;
2250 case 'i':
2251 os_free(ctrl_ifname);
2252 ctrl_ifname = os_strdup(optarg);
2253 break;
2254 case 'p':
2255 ctrl_iface_dir = optarg;
2256 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002257 case 'P':
2258 pid_file = optarg;
2259 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002260 case 'r':
2261 reconnect = 1;
2262 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002263 case 's':
2264 client_socket_dir = optarg;
2265 break;
Sunil Ravic0f5d412024-09-11 22:12:49 +00002266#ifdef CONFIG_IEEE80211BE
2267 case 'l':
2268 link_id = atoi(optarg);
2269 break;
2270#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 default:
2272 usage();
2273 return -1;
2274 }
2275 }
2276
2277 interactive = (argc == optind) && (action_file == NULL);
2278
2279 if (interactive) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002280 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 }
2282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 if (eloop_init())
2284 return -1;
2285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 for (;;) {
2287 if (ctrl_ifname == NULL) {
2288 struct dirent *dent;
2289 DIR *dir = opendir(ctrl_iface_dir);
2290 if (dir) {
2291 while ((dent = readdir(dir))) {
2292 if (os_strcmp(dent->d_name, ".") == 0
2293 ||
2294 os_strcmp(dent->d_name, "..") == 0)
2295 continue;
2296 printf("Selected interface '%s'\n",
2297 dent->d_name);
2298 ctrl_ifname = os_strdup(dent->d_name);
2299 break;
2300 }
2301 closedir(dir);
2302 }
2303 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00002304
2305#ifdef CONFIG_IEEE80211BE
2306 if (link_id >= 0 && ctrl_ifname) {
2307 int ret;
2308 char buf[300];
2309
2310 ret = os_snprintf(buf, sizeof(buf), "%s_%s%d",
2311 ctrl_ifname, WPA_CTRL_IFACE_LINK_NAME,
2312 link_id);
2313 if (os_snprintf_error(sizeof(buf), ret))
2314 return -1;
2315
2316 os_free(ctrl_ifname);
2317 ctrl_ifname = os_strdup(buf);
2318 link_id = -1;
2319 }
2320#endif /* CONFIG_IEEE80211BE */
2321
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002322 hostapd_cli_reconnect(ctrl_ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 if (ctrl_conn) {
2324 if (warning_displayed)
2325 printf("Connection established.\n");
2326 break;
2327 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002328 if (!interactive && !reconnect) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 perror("Failed to connect to hostapd - "
2330 "wpa_ctrl_open");
2331 return -1;
2332 }
2333
2334 if (!warning_displayed) {
2335 printf("Could not connect to hostapd - re-trying\n");
2336 warning_displayed = 1;
2337 }
2338 os_sleep(1, 0);
2339 continue;
2340 }
2341
Sunil Ravi036cec52023-03-29 11:35:17 -07002342 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
2343
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002344 if (action_file && !hostapd_cli_attached)
2345 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002346 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002348 if (reconnect && action_file && ctrl_ifname) {
2349 while (!hostapd_cli_quit) {
2350 if (ctrl_conn)
2351 hostapd_cli_action(ctrl_conn);
2352 os_sleep(1, 0);
2353 hostapd_cli_reconnect(ctrl_ifname);
2354 }
2355 } else if (interactive)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 hostapd_cli_interactive();
2357 else if (action_file)
2358 hostapd_cli_action(ctrl_conn);
2359 else
2360 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
2361
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002362 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 hostapd_cli_cleanup();
2366 return 0;
2367}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002368
2369#else /* CONFIG_NO_CTRL_IFACE */
2370
2371int main(int argc, char *argv[])
2372{
2373 return -1;
2374}
2375
2376#endif /* CONFIG_NO_CTRL_IFACE */