blob: 45497cd018d85093b0d1165d4d2f7575471acc26 [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"
Hai Shaloma20dcd72022-02-04 13:43:00 -080024"Copyright (c) 2004-2022, 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
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001171static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1172 int argc, char *argv[])
1173{
1174 char cmd[256];
1175 int res;
1176 int i;
1177 char *tmp;
1178 int total;
1179
1180 if (argc < 2) {
1181 printf("Invalid chan_switch command: needs at least two "
1182 "arguments (count and freq)\n"
1183 "usage: <cs_count> <freq> [sec_channel_offset=] "
1184 "[center_freq1=] [center_freq2=] [bandwidth=] "
Sunil Ravia04bd252022-05-02 22:54:18 -07001185 "[blocktx] [ht|vht|he|eht]\n");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001186 return -1;
1187 }
1188
1189 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1190 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001191 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001192 printf("Too long CHAN_SWITCH command.\n");
1193 return -1;
1194 }
1195
1196 total = res;
1197 for (i = 2; i < argc; i++) {
1198 tmp = cmd + total;
1199 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001200 if (os_snprintf_error(sizeof(cmd) - total, res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001201 printf("Too long CHAN_SWITCH command.\n");
1202 return -1;
1203 }
1204 total += res;
1205 }
1206 return wpa_ctrl_command(ctrl, cmd);
1207}
1208
1209
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001210static int hostapd_cli_cmd_notify_cw_change(struct wpa_ctrl *ctrl,
1211 int argc, char *argv[])
1212{
1213 return hostapd_cli_cmd(ctrl, "NOTIFY_CW_CHANGE", 1, argc, argv);
1214}
1215
1216
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001217static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1218 char *argv[])
1219{
1220 return wpa_ctrl_command(ctrl, "ENABLE");
1221}
1222
1223
1224static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1225 char *argv[])
1226{
1227 return wpa_ctrl_command(ctrl, "RELOAD");
1228}
1229
1230
Sunil Ravi77d572f2023-01-17 23:58:31 +00001231static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc,
1232 char *argv[])
1233{
1234 return wpa_ctrl_command(ctrl, "RELOAD_BSS");
1235}
1236
1237
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001238static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1239 char *argv[])
1240{
1241 return wpa_ctrl_command(ctrl, "DISABLE");
1242}
1243
1244
Hai Shalom81f62d82019-07-22 12:10:00 -07001245static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc,
1246 char *argv[])
1247{
1248 return wpa_ctrl_command(ctrl, "UPDATE_BEACON");
1249}
1250
1251
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001252static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1253{
1254 char cmd[256];
1255 int res;
1256
Hai Shalom60840252021-02-19 19:02:11 -08001257 if (argc < 2 || argc > 4) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001258 printf("Invalid vendor command\n"
Hai Shalom60840252021-02-19 19:02:11 -08001259 "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001260 return -1;
1261 }
1262
Hai Shalom60840252021-02-19 19:02:11 -08001263 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0],
1264 argv[1], argc >= 3 ? argv[2] : "",
1265 argc == 4 ? " " : "", argc == 4 ? argv[3] : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001266 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001267 printf("Too long VENDOR command.\n");
1268 return -1;
1269 }
1270 return wpa_ctrl_command(ctrl, cmd);
1271}
1272
1273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001274static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1275 char *argv[])
1276{
1277 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1278}
1279
1280
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001281static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1282 char *argv[])
1283{
1284 char cmd[256];
1285 int res;
1286
1287 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1288 argc >= 1 ? " " : "",
1289 argc >= 1 ? argv[0] : "",
1290 argc == 2 ? " " : "",
1291 argc == 2 ? argv[1] : "");
1292 if (os_snprintf_error(sizeof(cmd), res)) {
1293 printf("Too long option\n");
1294 return -1;
1295 }
1296 return wpa_ctrl_command(ctrl, cmd);
1297}
1298
1299
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001300static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1301{
1302 if (argc == 0)
1303 return -1;
1304 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1305}
1306
1307
Dmitry Shmidte4663042016-04-04 10:07:49 -07001308static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1309{
1310 return wpa_ctrl_command(ctrl, "PMKSA");
1311}
1312
1313
1314static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1315 char *argv[])
1316{
1317 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1318}
1319
1320
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001321static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1322 char *argv[])
1323{
1324 char cmd[2048];
1325 int res;
1326
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001327 if (argc < 3 || argc > 6) {
1328 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001329 return -1;
1330 }
1331
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001332 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001333 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001334 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001335 if (os_snprintf_error(sizeof(cmd), res)) {
1336 printf("Too long SET_NEIGHBOR command.\n");
1337 return -1;
1338 }
1339 return wpa_ctrl_command(ctrl, cmd);
1340}
1341
1342
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001343static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc,
1344 char *argv[])
1345{
1346 return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR");
1347}
1348
1349
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001350static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1351 char *argv[])
1352{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001353 return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001354}
1355
1356
1357static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1358 char *argv[])
1359{
1360 char cmd[256];
1361 int res;
1362
1363 if (argc != 1) {
1364 printf("Invalid req_lci command - requires destination address\n");
1365 return -1;
1366 }
1367
1368 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1369 if (os_snprintf_error(sizeof(cmd), res)) {
1370 printf("Too long REQ_LCI command.\n");
1371 return -1;
1372 }
1373 return wpa_ctrl_command(ctrl, cmd);
1374}
1375
1376
1377static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1378 char *argv[])
1379{
1380 if (argc < 4) {
1381 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1382 return -1;
1383 }
1384
1385 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1386}
1387
1388
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001389static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1390 char *argv[])
1391{
1392 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1393}
1394
1395
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001396static int hostapd_cli_cmd_driver_flags2(struct wpa_ctrl *ctrl, int argc,
1397 char *argv[])
1398{
1399 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS2");
1400}
1401
1402
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001403#ifdef CONFIG_DPP
1404
1405static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
1406 char *argv[])
1407{
1408 return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
1409}
1410
1411
1412static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
1413 char *argv[])
1414{
1415 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
1416}
1417
1418
1419static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
1420 char *argv[])
1421{
1422 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
1423}
1424
1425
1426static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
1427 int argc, char *argv[])
1428{
1429 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
1430}
1431
1432
1433static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
1434 char *argv[])
1435{
1436 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
1437}
1438
1439
Hai Shalom899fcc72020-10-19 14:38:18 -07001440static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc,
1441 char *argv[])
1442{
1443 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv);
1444}
1445
1446
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001447static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
1448 char *argv[])
1449{
1450 return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
1451}
1452
1453
Roshan Pius3a1667e2018-07-03 15:17:14 -07001454static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
1455 char *argv[])
1456{
1457 return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
1458}
1459
1460
1461static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
1462 char *argv[])
1463{
1464 return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
1465}
1466
1467
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001468static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
1469 char *argv[])
1470{
1471 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
1472}
1473
1474
1475static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
1476 int argc, char *argv[])
1477{
1478 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
1479}
1480
1481
Roshan Pius3a1667e2018-07-03 15:17:14 -07001482static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl,
1483 int argc, char *argv[])
1484{
1485 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
1486}
1487
1488
Hai Shalom74f70d42019-02-11 14:42:39 -08001489static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl,
1490 int argc, char *argv[])
1491{
1492 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv);
1493}
1494
1495
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001496static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
1497 char *argv[])
1498{
1499 return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
1500}
1501
1502
1503static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
1504 char *argv[])
1505{
1506 return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
1507}
1508
Hai Shalom899fcc72020-10-19 14:38:18 -07001509
1510#ifdef CONFIG_DPP2
1511
Hai Shalom60840252021-02-19 19:02:11 -08001512static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc,
1513 char *argv[])
1514{
Sunil Ravi89eba102022-09-13 21:04:37 -07001515 return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv);
Hai Shalom60840252021-02-19 19:02:11 -08001516}
1517
1518
1519static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc,
1520 char *argv[])
1521{
1522 return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP");
1523}
1524
1525
Hai Shalom899fcc72020-10-19 14:38:18 -07001526static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc,
1527 char *argv[])
1528{
1529 return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv);
1530}
1531
1532
1533static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc,
1534 char *argv[])
1535{
1536 return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP");
1537}
1538
1539#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001540
1541
1542#ifdef CONFIG_DPP3
1543static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc,
1544 char *argv[])
1545{
Sunil Ravi640215c2023-06-28 23:08:09 +00001546 return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 0, argc, argv);
Sunil Ravi89eba102022-09-13 21:04:37 -07001547}
1548#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001549#endif /* CONFIG_DPP */
1550
1551
Roshan Pius3a1667e2018-07-03 15:17:14 -07001552static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc,
1553 char *argv[])
1554{
1555 return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv);
1556}
1557
1558
1559static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc,
1560 char *argv[])
1561{
1562 return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv);
1563}
1564
1565
1566static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc,
1567 char *argv[])
1568{
1569 return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv);
1570}
1571
1572
Hai Shalom74f70d42019-02-11 14:42:39 -08001573static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc,
1574 char *argv[])
1575{
1576 return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv);
1577}
1578
1579
1580static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc,
1581 char *argv[])
1582{
1583 return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK");
1584}
1585
1586
Hai Shaloma20dcd72022-02-04 13:43:00 -08001587#ifdef ANDROID
1588static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
1589{
1590 return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
1591}
1592#endif /* ANDROID */
1593
1594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595struct hostapd_cli_cmd {
1596 const char *cmd;
1597 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001598 char ** (*completion)(const char *str, int pos);
1599 const char *usage;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600};
1601
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001602static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001603 { "ping", hostapd_cli_cmd_ping, NULL,
1604 "= pings hostapd" },
1605 { "mib", hostapd_cli_cmd_mib, NULL,
1606 "= get MIB variables (dot1x, dot11, radius)" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001607 { "relog", hostapd_cli_cmd_relog, NULL,
1608 "= reload/truncate debug log output file" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001609 { "close_log", hostapd_cli_cmd_close_log, NULL,
1610 "= disable debug log output file" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001611 { "status", hostapd_cli_cmd_status, NULL,
1612 "= show interface status info" },
1613 { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001614 "<addr> = get MIB variables for one station" },
1615 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1616 "= get MIB variables for all stations" },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001617 { "list_sta", hostapd_cli_cmd_list_sta, NULL,
1618 "= list all stations" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001619 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1620 "<addr> = add a new station" },
1621 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001622 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001623 "<addr> = deauthenticate a station" },
1624 { "disassociate", hostapd_cli_cmd_disassociate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001625 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001626 "<addr> = disassociate a station" },
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001627#ifdef CONFIG_TAXONOMY
Dmitry Shmidt29333592017-01-09 12:27:11 -08001628 { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001629 "<addr> = get taxonomy signature for a station" },
1630#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001631 { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001632 "<addr> = send SA Query to a station" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633#ifdef CONFIG_WPS
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001634 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1635 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1636 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1637 "<PIN> = verify PIN checksum" },
1638 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1639 "= indicate button pushed to initiate PBC" },
1640 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1641 "= cancel the pending WPS operation" },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001642#ifdef CONFIG_WPS_NFC
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001643 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1644 "<hexdump> = report read NFC tag with WPS data" },
1645 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1646 "<WPS/NDEF> = build NFC configuration token" },
1647 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1648 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1649 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1650 NULL },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001651#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001652 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1653 "<cmd> [params..] = enable/disable AP PIN" },
1654 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1655 "<SSID> <auth> <encr> <key> = configure AP" },
1656 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1657 "= show current WPS status" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658#endif /* CONFIG_WPS */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001659 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
1660 "= send Disassociation Imminent notification" },
1661 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
1662 "= send ESS Dissassociation Imminent notification" },
1663 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
1664 "= send BSS Transition Management Request" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001665 { "get_config", hostapd_cli_cmd_get_config, NULL,
1666 "= show current configuration" },
1667 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1668 "= show this usage help" },
1669 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1670 "[ifname] = show interfaces/select interface" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001671#ifdef CONFIG_FST
Dmitry Shmidt29333592017-01-09 12:27:11 -08001672 { "fst", hostapd_cli_cmd_fst, NULL,
1673 "<params...> = send FST-MANAGER control interface command" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001674#endif /* CONFIG_FST */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001675 { "raw", hostapd_cli_cmd_raw, NULL,
1676 "<params..> = send unprocessed command" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001677 { "level", hostapd_cli_cmd_level, NULL,
1678 "<debug level> = change debug level" },
1679 { "license", hostapd_cli_cmd_license, NULL,
1680 "= show full hostapd_cli license" },
1681 { "quit", hostapd_cli_cmd_quit, NULL,
1682 "= exit hostapd_cli" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001683 { "set", hostapd_cli_cmd_set, hostapd_complete_set,
1684 "<name> <value> = set runtime variables" },
1685 { "get", hostapd_cli_cmd_get, hostapd_complete_get,
1686 "<name> = get runtime info" },
1687 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
1688 "<arg,arg,...> = set QoS Map set element" },
1689 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
1690 hostapd_complete_stations,
1691 "<addr> = send QoS Map Configure frame" },
1692 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
1693 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1694 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1695 " = initiate channel switch announcement" },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001696 { "notify_cw_change", hostapd_cli_cmd_notify_cw_change, NULL,
1697 "<channel_width> = 0 - 20 MHz, 1 - 40 MHz, 2 - 80 MHz, 3 - 160 MHz" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001698 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
1699 "<addr> <url>\n"
1700 " = send WNM-Notification Subscription Remediation Request" },
1701 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
1702 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1703 " = send WNM-Notification imminent deauthentication indication" },
1704 { "vendor", hostapd_cli_cmd_vendor, NULL,
1705 "<vendor id> <sub command id> [<hex formatted data>]\n"
1706 " = send vendor driver command" },
1707 { "enable", hostapd_cli_cmd_enable, NULL,
1708 "= enable hostapd on current interface" },
1709 { "reload", hostapd_cli_cmd_reload, NULL,
1710 "= reload configuration for current interface" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001711 { "reload_bss", hostapd_cli_cmd_reload_bss, NULL,
1712 "= reload configuration for current BSS" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001713 { "disable", hostapd_cli_cmd_disable, NULL,
1714 "= disable hostapd on current interface" },
Hai Shalom81f62d82019-07-22 12:10:00 -07001715 { "update_beacon", hostapd_cli_cmd_update_beacon, NULL,
1716 "= update Beacon frame contents\n"},
Dmitry Shmidt29333592017-01-09 12:27:11 -08001717 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
1718 "= drop all ERP keys"},
1719 { "log_level", hostapd_cli_cmd_log_level, NULL,
1720 "[level] = show/change log verbosity level" },
1721 { "pmksa", hostapd_cli_cmd_pmksa, NULL,
1722 " = show PMKSA cache entries" },
1723 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
1724 " = flush PMKSA cache" },
1725 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
1726 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1727 " = add AP to neighbor database" },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001728 { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL,
1729 " = show neighbor database entries" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001730 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001731 "<addr> [ssid=<hex>] = remove AP from neighbor database" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001732 { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
1733 "<addr> = send LCI request to a station"},
1734 { "req_range", hostapd_cli_cmd_req_range, NULL,
1735 " = send FTM range request"},
1736 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
1737 " = show supported driver flags"},
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001738 { "driver_flags2", hostapd_cli_cmd_driver_flags2, NULL,
1739 " = show supported driver flags2"},
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001740#ifdef CONFIG_DPP
1741 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
1742 "report a scanned DPP URI from a QR Code" },
1743 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
1744 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1745 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
1746 "*|<id> = remove DPP bootstrap information" },
1747 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
1748 "<id> = get DPP bootstrap URI" },
1749 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
1750 "<id> = show DPP bootstrap information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001751 { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL,
1752 "<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 -07001753 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
1754 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
Roshan Pius3a1667e2018-07-03 15:17:14 -07001755 { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
1756 "<freq in MHz> = start DPP listen" },
1757 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
1758 "= stop DPP listen" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001759 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
1760 "[curve=..] [key=..] = add DPP configurator" },
1761 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
1762 NULL,
1763 "*|<id> = remove DPP configurator" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001764 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001765 NULL,
1766 "<id> = Get DPP configurator's private key" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001767 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL,
1768 "conf=<role> configurator=<id> = generate self DPP configuration" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001769 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
1770 "add PKEX code" },
1771 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
1772 "*|<id> = remove DPP pkex information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001773#ifdef CONFIG_DPP2
Hai Shalom60840252021-02-19 19:02:11 -08001774 { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL,
1775 "[tcp_port=<port>] [role=..] = start DPP controller" },
1776 { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL,
1777 "= stop DPP controller" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001778 { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL,
1779 "own=<BI ID> iter=<count> = start DPP chirp" },
1780 { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL,
1781 "= stop DPP chirp" },
1782#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001783#ifdef CONFIG_DPP3
1784 { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL,
1785 "= press DPP push button" },
1786#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001787#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001788 { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL,
1789 "=Add/Delete/Show/Clear accept MAC ACL" },
1790 { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL,
1791 "=Add/Delete/Show/Clear deny MAC ACL" },
1792 { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations,
1793 "<addr> = poll a STA to check connectivity with a QoS null frame" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001794 { "req_beacon", hostapd_cli_cmd_req_beacon, NULL,
1795 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
1796 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL,
1797 "= reload wpa_psk_file only" },
Hai Shaloma20dcd72022-02-04 13:43:00 -08001798#ifdef ANDROID
1799 { "driver", hostapd_cli_cmd_driver, NULL,
1800 "<driver sub command> [<hex formatted data>] = send driver command data" },
1801#endif /* ANDROID */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001802 { NULL, NULL, NULL, NULL }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803};
1804
1805
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001806/*
1807 * Prints command usage, lines are padded with the specified string.
1808 */
1809static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1810 const char *pad)
1811{
1812 char c;
1813 size_t n;
1814
1815 if (cmd->usage == NULL)
1816 return;
1817 fprintf(stream, "%s%s ", pad, cmd->cmd);
1818 for (n = 0; (c = cmd->usage[n]); n++) {
1819 fprintf(stream, "%c", c);
1820 if (c == '\n')
1821 fprintf(stream, "%s", pad);
1822 }
1823 fprintf(stream, "\n");
1824}
1825
1826
1827static void print_help(FILE *stream, const char *cmd)
1828{
1829 int n;
1830
1831 fprintf(stream, "commands:\n");
1832 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1833 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1834 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1835 }
1836}
1837
1838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1840{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001841 const struct hostapd_cli_cmd *cmd, *match = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 int count;
1843
1844 count = 0;
1845 cmd = hostapd_cli_commands;
1846 while (cmd->cmd) {
1847 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1848 match = cmd;
1849 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1850 /* we have an exact match */
1851 count = 1;
1852 break;
1853 }
1854 count++;
1855 }
1856 cmd++;
1857 }
1858
1859 if (count > 1) {
1860 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1861 cmd = hostapd_cli_commands;
1862 while (cmd->cmd) {
1863 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1864 0) {
1865 printf(" %s", cmd->cmd);
1866 }
1867 cmd++;
1868 }
1869 printf("\n");
1870 } else if (count == 0) {
1871 printf("Unknown command '%s'\n", argv[0]);
1872 } else {
1873 match->handler(ctrl, argc - 1, &argv[1]);
1874 }
1875}
1876
1877
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001878static void cli_event(const char *str)
1879{
1880 const char *start, *s;
1881
1882 start = os_strchr(str, '>');
1883 if (start == NULL)
1884 return;
1885
1886 start++;
1887
1888 if (str_starts(start, AP_STA_CONNECTED)) {
1889 s = os_strchr(start, ' ');
1890 if (s == NULL)
1891 return;
1892 cli_txt_list_add(&stations, s + 1);
1893 return;
1894 }
1895
1896 if (str_starts(start, AP_STA_DISCONNECTED)) {
1897 s = os_strchr(start, ' ');
1898 if (s == NULL)
1899 return;
1900 cli_txt_list_del_addr(&stations, s + 1);
1901 return;
1902 }
1903}
1904
1905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1907 int action_monitor)
1908{
1909 int first = 1;
1910 if (ctrl_conn == NULL)
1911 return;
1912 while (wpa_ctrl_pending(ctrl)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001913 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 size_t len = sizeof(buf) - 1;
1915 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1916 buf[len] = '\0';
1917 if (action_monitor)
1918 hostapd_cli_action_process(buf, len);
1919 else {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001920 cli_event(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 if (in_read && first)
1922 printf("\n");
1923 first = 0;
1924 printf("%s\n", buf);
1925 }
1926 } else {
1927 printf("Could not read pending message.\n");
1928 break;
1929 }
1930 }
1931}
1932
1933
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001934static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001936 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937}
1938
1939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001940static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941{
1942 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1943 printf("Connection to hostapd lost - trying to reconnect\n");
1944 hostapd_cli_close_connection();
1945 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001946 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
1947 printf("Connection to hostapd re-established\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948 if (ctrl_conn)
1949 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001950 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1951}
1952
1953
1954static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1955{
1956 eloop_terminate();
1957}
1958
1959
1960static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1961{
1962 char *argv[max_args];
1963 int argc;
1964 argc = tokenize_cmd(cmd, argv);
1965 if (argc)
1966 wpa_request(ctrl_conn, argc, argv);
1967}
1968
1969
1970static void hostapd_cli_edit_eof_cb(void *ctx)
1971{
1972 eloop_terminate();
1973}
1974
1975
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001976static char ** list_cmd_list(void)
1977{
1978 char **res;
1979 int i, count;
1980
1981 count = ARRAY_SIZE(hostapd_cli_commands);
1982 res = os_calloc(count + 1, sizeof(char *));
1983 if (res == NULL)
1984 return NULL;
1985
1986 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1987 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1988 if (res[i] == NULL)
1989 break;
1990 }
1991
1992 return res;
1993}
1994
1995
1996static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1997 int pos)
1998{
1999 int i;
2000
2001 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
2002 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
2003 continue;
2004 if (hostapd_cli_commands[i].completion)
2005 return hostapd_cli_commands[i].completion(str, pos);
2006 if (!hostapd_cli_commands[i].usage)
2007 return NULL;
2008 edit_clear_line();
2009 printf("\r%s\n", hostapd_cli_commands[i].usage);
2010 edit_redraw();
2011 break;
2012 }
2013
2014 return NULL;
2015}
2016
2017
2018static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
2019 int pos)
2020{
2021 char **res;
2022 const char *end;
2023 char *cmd;
2024
2025 end = os_strchr(str, ' ');
2026 if (end == NULL || str + pos < end)
2027 return list_cmd_list();
2028
2029 cmd = os_malloc(pos + 1);
2030 if (cmd == NULL)
2031 return NULL;
2032 os_memcpy(cmd, str, pos);
2033 cmd[end - str] = '\0';
2034 res = hostapd_cli_cmd_completion(cmd, str, pos);
2035 os_free(cmd);
2036 return res;
2037}
2038
2039
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002040static void hostapd_cli_interactive(void)
2041{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002042 char *hfile = NULL;
2043 char *home;
2044
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045 printf("\nInteractive mode\n\n");
2046
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002047#ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
2048 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
2049#else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2050 home = getenv("HOME");
2051#endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2052 if (home) {
2053 const char *fname = ".hostapd_cli_history";
2054 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
2055 hfile = os_malloc(hfile_len);
2056 if (hfile)
2057 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
2058 }
2059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002061 hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2063
2064 eloop_run();
2065
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002066 cli_txt_list_flush(&stations);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002067 edit_deinit(hfile, NULL);
2068 os_free(hfile);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002069 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
2070}
2071
2072
2073static void hostapd_cli_cleanup(void)
2074{
2075 hostapd_cli_close_connection();
2076 if (pid_file)
2077 os_daemonize_terminate(pid_file);
2078
2079 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080}
2081
2082
Sunil Ravi036cec52023-03-29 11:35:17 -07002083static void hostapd_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
Sunil Ravi036cec52023-03-29 11:35:17 -07002085 struct wpa_ctrl *ctrl = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 char buf[256];
2087 size_t len;
2088
Sunil Ravi036cec52023-03-29 11:35:17 -07002089 /* verify that connection is still working */
2090 len = sizeof(buf) - 1;
2091 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
2092 hostapd_cli_action_cb) < 0 ||
2093 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
2094 printf("hostapd did not reply to PING command - exiting\n");
2095 eloop_terminate();
2096 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002098 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2099 ctrl, NULL);
2100}
2101
2102
2103static void hostapd_cli_action_receive(int sock, void *eloop_ctx,
2104 void *sock_ctx)
2105{
2106 struct wpa_ctrl *ctrl = eloop_ctx;
2107
2108 hostapd_cli_recv_pending(ctrl, 0, 1);
2109}
2110
2111
2112static void hostapd_cli_action(struct wpa_ctrl *ctrl)
2113{
2114 int fd;
2115
2116 fd = wpa_ctrl_get_fd(ctrl);
2117 eloop_register_timeout(ping_interval, 0, hostapd_cli_action_ping,
2118 ctrl, NULL);
2119 eloop_register_read_sock(fd, hostapd_cli_action_receive, ctrl, NULL);
2120 eloop_run();
2121 eloop_cancel_timeout(hostapd_cli_action_ping, ctrl, NULL);
2122 eloop_unregister_read_sock(fd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123}
2124
2125
2126int main(int argc, char *argv[])
2127{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 int warning_displayed = 0;
2129 int c;
2130 int daemonize = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002131 int reconnect = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132
2133 if (os_program_init())
2134 return -1;
2135
2136 for (;;) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002137 c = getopt(argc, argv, "a:BhG:i:p:P:rs:v");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 if (c < 0)
2139 break;
2140 switch (c) {
2141 case 'a':
2142 action_file = optarg;
2143 break;
2144 case 'B':
2145 daemonize = 1;
2146 break;
2147 case 'G':
2148 ping_interval = atoi(optarg);
2149 break;
2150 case 'h':
2151 usage();
2152 return 0;
2153 case 'v':
2154 printf("%s\n", hostapd_cli_version);
2155 return 0;
2156 case 'i':
2157 os_free(ctrl_ifname);
2158 ctrl_ifname = os_strdup(optarg);
2159 break;
2160 case 'p':
2161 ctrl_iface_dir = optarg;
2162 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002163 case 'P':
2164 pid_file = optarg;
2165 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002166 case 'r':
2167 reconnect = 1;
2168 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002169 case 's':
2170 client_socket_dir = optarg;
2171 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 default:
2173 usage();
2174 return -1;
2175 }
2176 }
2177
2178 interactive = (argc == optind) && (action_file == NULL);
2179
2180 if (interactive) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002181 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 }
2183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002184 if (eloop_init())
2185 return -1;
2186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 for (;;) {
2188 if (ctrl_ifname == NULL) {
2189 struct dirent *dent;
2190 DIR *dir = opendir(ctrl_iface_dir);
2191 if (dir) {
2192 while ((dent = readdir(dir))) {
2193 if (os_strcmp(dent->d_name, ".") == 0
2194 ||
2195 os_strcmp(dent->d_name, "..") == 0)
2196 continue;
2197 printf("Selected interface '%s'\n",
2198 dent->d_name);
2199 ctrl_ifname = os_strdup(dent->d_name);
2200 break;
2201 }
2202 closedir(dir);
2203 }
2204 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002205 hostapd_cli_reconnect(ctrl_ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 if (ctrl_conn) {
2207 if (warning_displayed)
2208 printf("Connection established.\n");
2209 break;
2210 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002211 if (!interactive && !reconnect) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 perror("Failed to connect to hostapd - "
2213 "wpa_ctrl_open");
2214 return -1;
2215 }
2216
2217 if (!warning_displayed) {
2218 printf("Could not connect to hostapd - re-trying\n");
2219 warning_displayed = 1;
2220 }
2221 os_sleep(1, 0);
2222 continue;
2223 }
2224
Sunil Ravi036cec52023-03-29 11:35:17 -07002225 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
2226
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002227 if (action_file && !hostapd_cli_attached)
2228 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002229 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002231 if (reconnect && action_file && ctrl_ifname) {
2232 while (!hostapd_cli_quit) {
2233 if (ctrl_conn)
2234 hostapd_cli_action(ctrl_conn);
2235 os_sleep(1, 0);
2236 hostapd_cli_reconnect(ctrl_ifname);
2237 }
2238 } else if (interactive)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 hostapd_cli_interactive();
2240 else if (action_file)
2241 hostapd_cli_action(ctrl_conn);
2242 else
2243 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
2244
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002245 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002247 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 hostapd_cli_cleanup();
2249 return 0;
2250}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002251
2252#else /* CONFIG_NO_CTRL_IFACE */
2253
2254int main(int argc, char *argv[])
2255{
2256 return -1;
2257}
2258
2259#endif /* CONFIG_NO_CTRL_IFACE */