blob: ec40dda778c38e364c4c7faba03129687c0a12f3 [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
317static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
318{
319 char buf[64];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800320 if (argc < 1) {
321 printf("Invalid 'sta' command - at least one argument, STA "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 "address, is required.\n");
323 return -1;
324 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800325 if (argc > 1)
326 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
327 else
328 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 return wpa_ctrl_command(ctrl, buf);
330}
331
332
Dmitry Shmidt29333592017-01-09 12:27:11 -0800333static char ** hostapd_complete_stations(const char *str, int pos)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800334{
335 int arg = get_cmd_arg_num(str, pos);
336 char **res = NULL;
337
338 switch (arg) {
339 case 1:
340 res = cli_txt_list_array(&stations);
341 break;
342 }
343
344 return res;
345}
346
347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
349 char *argv[])
350{
351 char buf[64];
352 if (argc != 1) {
353 printf("Invalid 'new_sta' command - exactly one argument, STA "
354 "address, is required.\n");
355 return -1;
356 }
357 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
358 return wpa_ctrl_command(ctrl, buf);
359}
360
361
362static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
363 char *argv[])
364{
365 char buf[64];
366 if (argc < 1) {
367 printf("Invalid 'deauthenticate' command - exactly one "
368 "argument, STA address, is required.\n");
369 return -1;
370 }
371 if (argc > 1)
372 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
373 argv[0], argv[1]);
374 else
375 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
376 return wpa_ctrl_command(ctrl, buf);
377}
378
379
380static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
381 char *argv[])
382{
383 char buf[64];
384 if (argc < 1) {
385 printf("Invalid 'disassociate' command - exactly one "
386 "argument, STA address, is required.\n");
387 return -1;
388 }
389 if (argc > 1)
390 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
391 argv[0], argv[1]);
392 else
393 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
394 return wpa_ctrl_command(ctrl, buf);
395}
396
397
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700398#ifdef CONFIG_TAXONOMY
399static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
400 char *argv[])
401{
402 char buf[64];
403
404 if (argc != 1) {
405 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
406 return -1;
407 }
408 os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
409 return wpa_ctrl_command(ctrl, buf);
410}
411#endif /* CONFIG_TAXONOMY */
412
413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
415 char *argv[])
416{
417 char buf[64];
418 if (argc != 1) {
419 printf("Invalid 'sa_query' command - exactly one argument, "
420 "STA address, is required.\n");
421 return -1;
422 }
423 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
424 return wpa_ctrl_command(ctrl, buf);
425}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426
427
428#ifdef CONFIG_WPS
429static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
430 char *argv[])
431{
432 char buf[256];
433 if (argc < 2) {
434 printf("Invalid 'wps_pin' command - at least two arguments, "
435 "UUID and PIN, are required.\n");
436 return -1;
437 }
438 if (argc > 3)
439 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
440 argv[0], argv[1], argv[2], argv[3]);
441 else if (argc > 2)
442 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
443 argv[0], argv[1], argv[2]);
444 else
445 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
446 return wpa_ctrl_command(ctrl, buf);
447}
448
449
450static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
451 char *argv[])
452{
453 char cmd[256];
454 int res;
455
456 if (argc != 1 && argc != 2) {
457 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
458 "- PIN to be verified\n");
459 return -1;
460 }
461
462 if (argc == 2)
463 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
464 argv[0], argv[1]);
465 else
466 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
467 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800468 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 printf("Too long WPS_CHECK_PIN command.\n");
470 return -1;
471 }
472 return wpa_ctrl_command(ctrl, cmd);
473}
474
475
476static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
477 char *argv[])
478{
479 return wpa_ctrl_command(ctrl, "WPS_PBC");
480}
481
482
Dmitry Shmidt04949592012-07-19 12:16:46 -0700483static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
484 char *argv[])
485{
486 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
487}
488
489
Dmitry Shmidt04949592012-07-19 12:16:46 -0700490#ifdef CONFIG_WPS_NFC
491static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
492 char *argv[])
493{
494 int ret;
495 char *buf;
496 size_t buflen;
497
498 if (argc != 1) {
499 printf("Invalid 'wps_nfc_tag_read' command - one argument "
500 "is required.\n");
501 return -1;
502 }
503
504 buflen = 18 + os_strlen(argv[0]);
505 buf = os_malloc(buflen);
506 if (buf == NULL)
507 return -1;
508 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
509
510 ret = wpa_ctrl_command(ctrl, buf);
511 os_free(buf);
512
513 return ret;
514}
515
516
517static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
518 int argc, char *argv[])
519{
520 char cmd[64];
521 int res;
522
523 if (argc != 1) {
524 printf("Invalid 'wps_nfc_config_token' command - one argument "
525 "is required.\n");
526 return -1;
527 }
528
529 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
530 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800531 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700532 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
533 return -1;
534 }
535 return wpa_ctrl_command(ctrl, cmd);
536}
537
538
539static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
540 int argc, char *argv[])
541{
542 char cmd[64];
543 int res;
544
545 if (argc != 1) {
546 printf("Invalid 'wps_nfc_token' command - one argument is "
547 "required.\n");
548 return -1;
549 }
550
551 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800552 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553 printf("Too long WPS_NFC_TOKEN command.\n");
554 return -1;
555 }
556 return wpa_ctrl_command(ctrl, cmd);
557}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800558
559
560static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
561 int argc, char *argv[])
562{
563 char cmd[64];
564 int res;
565
566 if (argc != 2) {
567 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
568 "are required.\n");
569 return -1;
570 }
571
572 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
573 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800574 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800575 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
576 return -1;
577 }
578 return wpa_ctrl_command(ctrl, cmd);
579}
580
Dmitry Shmidt04949592012-07-19 12:16:46 -0700581#endif /* CONFIG_WPS_NFC */
582
583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
585 char *argv[])
586{
587 char buf[64];
588 if (argc < 1) {
589 printf("Invalid 'wps_ap_pin' command - at least one argument "
590 "is required.\n");
591 return -1;
592 }
593 if (argc > 2)
594 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
595 argv[0], argv[1], argv[2]);
596 else if (argc > 1)
597 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
598 argv[0], argv[1]);
599 else
600 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
601 return wpa_ctrl_command(ctrl, buf);
602}
603
604
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700605static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
606 char *argv[])
607{
608 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
609}
610
611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
613 char *argv[])
614{
615 char buf[256];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700616 char ssid_hex[2 * SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 char key_hex[2 * 64 + 1];
618 int i;
619
620 if (argc < 1) {
621 printf("Invalid 'wps_config' command - at least two arguments "
622 "are required.\n");
623 return -1;
624 }
625
626 ssid_hex[0] = '\0';
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700627 for (i = 0; i < SSID_MAX_LEN; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 if (argv[0][i] == '\0')
629 break;
630 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
631 }
632
633 key_hex[0] = '\0';
634 if (argc > 3) {
635 for (i = 0; i < 64; i++) {
636 if (argv[3][i] == '\0')
637 break;
638 os_snprintf(&key_hex[i * 2], 3, "%02x",
639 argv[3][i]);
640 }
641 }
642
643 if (argc > 3)
644 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
645 ssid_hex, argv[1], argv[2], key_hex);
646 else if (argc > 2)
647 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
648 ssid_hex, argv[1], argv[2]);
649 else
650 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
651 ssid_hex, argv[1]);
652 return wpa_ctrl_command(ctrl, buf);
653}
654#endif /* CONFIG_WPS */
655
656
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800657static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
658 char *argv[])
659{
660 char buf[300];
661 int res;
662
663 if (argc < 2) {
664 printf("Invalid 'disassoc_imminent' command - two arguments "
665 "(STA addr and Disassociation Timer) are needed\n");
666 return -1;
667 }
668
669 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
670 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800671 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800672 return -1;
673 return wpa_ctrl_command(ctrl, buf);
674}
675
676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800677static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
678 char *argv[])
679{
680 char buf[300];
681 int res;
682
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700683 if (argc < 3) {
684 printf("Invalid 'ess_disassoc' command - three arguments (STA "
685 "addr, disassoc timer, and URL) are needed\n");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800686 return -1;
687 }
688
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700689 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
690 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800691 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800692 return -1;
693 return wpa_ctrl_command(ctrl, buf);
694}
695
696
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800697static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
698 char *argv[])
699{
700 char buf[2000], *tmp;
701 int res, i, total;
702
703 if (argc < 1) {
704 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
705 return -1;
706 }
707
708 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
709 if (os_snprintf_error(sizeof(buf), res))
710 return -1;
711
712 total = res;
713 for (i = 1; i < argc; i++) {
714 tmp = &buf[total];
715 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
716 if (os_snprintf_error(sizeof(buf) - total, res))
717 return -1;
718 total += res;
719 }
720 return wpa_ctrl_command(ctrl, buf);
721}
722
723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
725 char *argv[])
726{
727 return wpa_ctrl_command(ctrl, "GET_CONFIG");
728}
729
730
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800731static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
732 char *addr, size_t addr_len, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733{
734 char buf[4096], *pos;
735 size_t len;
736 int ret;
737
738 if (ctrl_conn == NULL) {
739 printf("Not connected to hostapd - command dropped.\n");
740 return -1;
741 }
742 len = sizeof(buf) - 1;
743 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
744 hostapd_cli_msg_cb);
745 if (ret == -2) {
746 printf("'%s' command timed out.\n", cmd);
747 return -2;
748 } else if (ret < 0) {
749 printf("'%s' command failed.\n", cmd);
750 return -1;
751 }
752
753 buf[len] = '\0';
754 if (memcmp(buf, "FAIL", 4) == 0)
755 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800756 if (print)
757 printf("%s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758
759 pos = buf;
760 while (*pos != '\0' && *pos != '\n')
761 pos++;
762 *pos = '\0';
763 os_strlcpy(addr, buf, addr_len);
764 return 0;
765}
766
767
768static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
769 char *argv[])
770{
771 char addr[32], cmd[64];
772
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800773 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 return 0;
775 do {
776 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800777 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778
779 return -1;
780}
781
782
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800783static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
784 char *argv[])
785{
786 char addr[32], cmd[64];
787
788 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
789 return 0;
790 do {
791 if (os_strcmp(addr, "") != 0)
792 printf("%s\n", addr);
793 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
794 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
795
796 return 0;
797}
798
799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
801{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700802 print_help(stdout, argc > 0 ? argv[0] : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 return 0;
804}
805
806
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700807static char ** hostapd_cli_complete_help(const char *str, int pos)
808{
809 int arg = get_cmd_arg_num(str, pos);
810 char **res = NULL;
811
812 switch (arg) {
813 case 1:
814 res = list_cmd_list();
815 break;
816 }
817
818 return res;
819}
820
821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
823 char *argv[])
824{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700825 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 return 0;
827}
828
829
Dmitry Shmidt051af732013-10-22 13:52:46 -0700830static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
831 int argc, char *argv[])
832{
833 char buf[200];
834 int res;
835
836 if (argc != 1) {
837 printf("Invalid 'set_qos_map_set' command - "
838 "one argument (comma delimited QoS map set) "
839 "is needed\n");
840 return -1;
841 }
842
843 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700845 return -1;
846 return wpa_ctrl_command(ctrl, buf);
847}
848
849
850static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
851 int argc, char *argv[])
852{
853 char buf[50];
854 int res;
855
856 if (argc != 1) {
857 printf("Invalid 'send_qos_map_conf' command - "
858 "one argument (STA addr) is needed\n");
859 return -1;
860 }
861
862 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800863 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700864 return -1;
865 return wpa_ctrl_command(ctrl, buf);
866}
867
868
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800869static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
870 char *argv[])
871{
872 char buf[300];
873 int res;
874
875 if (argc < 2) {
876 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
877 "addr and URL) are needed\n");
878 return -1;
879 }
880
881 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
882 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800883 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800884 return -1;
885 return wpa_ctrl_command(ctrl, buf);
886}
887
888
889static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
890 char *argv[])
891{
892 char buf[300];
893 int res;
894
895 if (argc < 3) {
896 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
897 return -1;
898 }
899
900 if (argc > 3)
901 res = os_snprintf(buf, sizeof(buf),
902 "HS20_DEAUTH_REQ %s %s %s %s",
903 argv[0], argv[1], argv[2], argv[3]);
904 else
905 res = os_snprintf(buf, sizeof(buf),
906 "HS20_DEAUTH_REQ %s %s %s",
907 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800908 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800909 return -1;
910 return wpa_ctrl_command(ctrl, buf);
911}
912
913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
915{
916 hostapd_cli_quit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 if (interactive)
918 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 return 0;
920}
921
922
923static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
924{
925 char cmd[256];
926 if (argc != 1) {
927 printf("Invalid LEVEL command: needs one argument (debug "
928 "level)\n");
929 return 0;
930 }
931 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
932 return wpa_ctrl_command(ctrl, cmd);
933}
934
935
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800936static void update_stations(struct wpa_ctrl *ctrl)
937{
938 char addr[32], cmd[64];
939
940 if (!ctrl || !interactive)
941 return;
942
943 cli_txt_list_flush(&stations);
944
945 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
946 return;
947 do {
948 if (os_strcmp(addr, "") != 0)
949 cli_txt_list_add(&stations, addr);
950 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
951 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
952}
953
954
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700955static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
956 struct dl_list *interfaces)
957{
958 struct dirent *dent;
959 DIR *dir;
960
961 if (!ctrl || !interfaces)
962 return;
963 dir = opendir(ctrl_iface_dir);
964 if (dir == NULL)
965 return;
966
967 while ((dent = readdir(dir))) {
968 if (strcmp(dent->d_name, ".") == 0 ||
969 strcmp(dent->d_name, "..") == 0)
970 continue;
971 cli_txt_list_add(interfaces, dent->d_name);
972 }
973 closedir(dir);
974}
975
976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
978{
979 struct dirent *dent;
980 DIR *dir;
981
982 dir = opendir(ctrl_iface_dir);
983 if (dir == NULL) {
984 printf("Control interface directory '%s' could not be "
Hai Shalom899fcc72020-10-19 14:38:18 -0700985 "opened.\n", ctrl_iface_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 return;
987 }
988
989 printf("Available interfaces:\n");
990 while ((dent = readdir(dir))) {
991 if (strcmp(dent->d_name, ".") == 0 ||
992 strcmp(dent->d_name, "..") == 0)
993 continue;
994 printf("%s\n", dent->d_name);
995 }
996 closedir(dir);
997}
998
999
1000static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
1001 char *argv[])
1002{
1003 if (argc < 1) {
1004 hostapd_cli_list_interfaces(ctrl);
1005 return 0;
1006 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001007 if (hostapd_cli_reconnect(argv[0]) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 printf("Could not connect to interface '%s' - re-trying\n",
1009 ctrl_ifname);
1010 }
1011 return 0;
1012}
1013
1014
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001015static char ** hostapd_complete_interface(const char *str, int pos)
1016{
1017 int arg = get_cmd_arg_num(str, pos);
1018 char **res = NULL;
1019 DEFINE_DL_LIST(interfaces);
1020
1021 switch (arg) {
1022 case 1:
1023 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1024 res = cli_txt_list_array(&interfaces);
1025 cli_txt_list_flush(&interfaces);
1026 break;
1027 }
1028
1029 return res;
1030}
1031
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1034{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001035 char cmd[2048];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 int res;
1037
1038 if (argc != 2) {
1039 printf("Invalid SET command: needs two arguments (variable "
1040 "name and value)\n");
1041 return -1;
1042 }
1043
1044 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001045 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 printf("Too long SET command.\n");
1047 return -1;
1048 }
1049 return wpa_ctrl_command(ctrl, cmd);
1050}
1051
1052
Dmitry Shmidt29333592017-01-09 12:27:11 -08001053static char ** hostapd_complete_set(const char *str, int pos)
1054{
1055 int arg = get_cmd_arg_num(str, pos);
1056 const char *fields[] = {
1057#ifdef CONFIG_WPS_TESTING
Hai Shaloma20dcd72022-02-04 13:43:00 -08001058 "wps_version_number", "wps_testing_stub_cred",
Dmitry Shmidt29333592017-01-09 12:27:11 -08001059 "wps_corrupt_pkhash",
1060#endif /* CONFIG_WPS_TESTING */
1061#ifdef CONFIG_INTERWORKING
1062 "gas_frag_limit",
1063#endif /* CONFIG_INTERWORKING */
1064#ifdef CONFIG_TESTING_OPTIONS
1065 "ext_mgmt_frame_handling", "ext_eapol_frame_io",
1066#endif /* CONFIG_TESTING_OPTIONS */
1067#ifdef CONFIG_MBO
1068 "mbo_assoc_disallow",
1069#endif /* CONFIG_MBO */
1070 "deny_mac_file", "accept_mac_file",
1071 };
1072 int i, num_fields = ARRAY_SIZE(fields);
1073
1074 if (arg == 1) {
1075 char **res;
1076
1077 res = os_calloc(num_fields + 1, sizeof(char *));
1078 if (!res)
1079 return NULL;
1080 for (i = 0; i < num_fields; i++) {
1081 res[i] = os_strdup(fields[i]);
1082 if (!res[i])
1083 return res;
1084 }
1085 return res;
1086 }
1087 return NULL;
1088}
1089
1090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1092{
1093 char cmd[256];
1094 int res;
1095
1096 if (argc != 1) {
1097 printf("Invalid GET command: needs one argument (variable "
1098 "name)\n");
1099 return -1;
1100 }
1101
1102 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001103 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 printf("Too long GET command.\n");
1105 return -1;
1106 }
1107 return wpa_ctrl_command(ctrl, cmd);
1108}
1109
1110
Dmitry Shmidt29333592017-01-09 12:27:11 -08001111static char ** hostapd_complete_get(const char *str, int pos)
1112{
1113 int arg = get_cmd_arg_num(str, pos);
1114 const char *fields[] = {
1115 "version", "tls_library",
1116 };
1117 int i, num_fields = ARRAY_SIZE(fields);
1118
1119 if (arg == 1) {
1120 char **res;
1121
1122 res = os_calloc(num_fields + 1, sizeof(char *));
1123 if (!res)
1124 return NULL;
1125 for (i = 0; i < num_fields; i++) {
1126 res[i] = os_strdup(fields[i]);
1127 if (!res[i])
1128 return res;
1129 }
1130 return res;
1131 }
1132 return NULL;
1133}
1134
1135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001136#ifdef CONFIG_FST
1137static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1138{
1139 char cmd[256];
1140 int res;
1141 int i;
1142 int total;
1143
1144 if (argc <= 0) {
1145 printf("FST command: parameters are required.\n");
1146 return -1;
1147 }
1148
1149 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1150
1151 for (i = 0; i < argc; i++) {
1152 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1153 argv[i]);
1154 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1155 printf("Too long fst command.\n");
1156 return -1;
1157 }
1158 total += res;
1159 }
1160 return wpa_ctrl_command(ctrl, cmd);
1161}
1162#endif /* CONFIG_FST */
1163
1164
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001165static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1166 int argc, char *argv[])
1167{
1168 char cmd[256];
1169 int res;
1170 int i;
1171 char *tmp;
1172 int total;
1173
1174 if (argc < 2) {
1175 printf("Invalid chan_switch command: needs at least two "
1176 "arguments (count and freq)\n"
1177 "usage: <cs_count> <freq> [sec_channel_offset=] "
1178 "[center_freq1=] [center_freq2=] [bandwidth=] "
Sunil Ravia04bd252022-05-02 22:54:18 -07001179 "[blocktx] [ht|vht|he|eht]\n");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001180 return -1;
1181 }
1182
1183 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1184 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001185 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001186 printf("Too long CHAN_SWITCH command.\n");
1187 return -1;
1188 }
1189
1190 total = res;
1191 for (i = 2; i < argc; i++) {
1192 tmp = cmd + total;
1193 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001194 if (os_snprintf_error(sizeof(cmd) - total, res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001195 printf("Too long CHAN_SWITCH command.\n");
1196 return -1;
1197 }
1198 total += res;
1199 }
1200 return wpa_ctrl_command(ctrl, cmd);
1201}
1202
1203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001204static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1205 char *argv[])
1206{
1207 return wpa_ctrl_command(ctrl, "ENABLE");
1208}
1209
1210
1211static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1212 char *argv[])
1213{
1214 return wpa_ctrl_command(ctrl, "RELOAD");
1215}
1216
1217
Sunil Ravi77d572f2023-01-17 23:58:31 +00001218static int hostapd_cli_cmd_reload_bss(struct wpa_ctrl *ctrl, int argc,
1219 char *argv[])
1220{
1221 return wpa_ctrl_command(ctrl, "RELOAD_BSS");
1222}
1223
1224
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001225static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1226 char *argv[])
1227{
1228 return wpa_ctrl_command(ctrl, "DISABLE");
1229}
1230
1231
Hai Shalom81f62d82019-07-22 12:10:00 -07001232static int hostapd_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc,
1233 char *argv[])
1234{
1235 return wpa_ctrl_command(ctrl, "UPDATE_BEACON");
1236}
1237
1238
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001239static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1240{
1241 char cmd[256];
1242 int res;
1243
Hai Shalom60840252021-02-19 19:02:11 -08001244 if (argc < 2 || argc > 4) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001245 printf("Invalid vendor command\n"
Hai Shalom60840252021-02-19 19:02:11 -08001246 "usage: <vendor id> <command id> [<hex formatted command argument>] [nested=<0|1>]\n");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001247 return -1;
1248 }
1249
Hai Shalom60840252021-02-19 19:02:11 -08001250 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s%s%s", argv[0],
1251 argv[1], argc >= 3 ? argv[2] : "",
1252 argc == 4 ? " " : "", argc == 4 ? argv[3] : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001253 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001254 printf("Too long VENDOR command.\n");
1255 return -1;
1256 }
1257 return wpa_ctrl_command(ctrl, cmd);
1258}
1259
1260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001261static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1262 char *argv[])
1263{
1264 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1265}
1266
1267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001268static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1269 char *argv[])
1270{
1271 char cmd[256];
1272 int res;
1273
1274 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1275 argc >= 1 ? " " : "",
1276 argc >= 1 ? argv[0] : "",
1277 argc == 2 ? " " : "",
1278 argc == 2 ? argv[1] : "");
1279 if (os_snprintf_error(sizeof(cmd), res)) {
1280 printf("Too long option\n");
1281 return -1;
1282 }
1283 return wpa_ctrl_command(ctrl, cmd);
1284}
1285
1286
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001287static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1288{
1289 if (argc == 0)
1290 return -1;
1291 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1292}
1293
1294
Dmitry Shmidte4663042016-04-04 10:07:49 -07001295static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1296{
1297 return wpa_ctrl_command(ctrl, "PMKSA");
1298}
1299
1300
1301static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1302 char *argv[])
1303{
1304 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1305}
1306
1307
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001308static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1309 char *argv[])
1310{
1311 char cmd[2048];
1312 int res;
1313
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001314 if (argc < 3 || argc > 6) {
1315 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001316 return -1;
1317 }
1318
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001319 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001320 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001321 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001322 if (os_snprintf_error(sizeof(cmd), res)) {
1323 printf("Too long SET_NEIGHBOR command.\n");
1324 return -1;
1325 }
1326 return wpa_ctrl_command(ctrl, cmd);
1327}
1328
1329
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001330static int hostapd_cli_cmd_show_neighbor(struct wpa_ctrl *ctrl, int argc,
1331 char *argv[])
1332{
1333 return wpa_ctrl_command(ctrl, "SHOW_NEIGHBOR");
1334}
1335
1336
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001337static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1338 char *argv[])
1339{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001340 return hostapd_cli_cmd(ctrl, "REMOVE_NEIGHBOR", 1, argc, argv);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001341}
1342
1343
1344static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1345 char *argv[])
1346{
1347 char cmd[256];
1348 int res;
1349
1350 if (argc != 1) {
1351 printf("Invalid req_lci command - requires destination address\n");
1352 return -1;
1353 }
1354
1355 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1356 if (os_snprintf_error(sizeof(cmd), res)) {
1357 printf("Too long REQ_LCI command.\n");
1358 return -1;
1359 }
1360 return wpa_ctrl_command(ctrl, cmd);
1361}
1362
1363
1364static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1365 char *argv[])
1366{
1367 if (argc < 4) {
1368 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1369 return -1;
1370 }
1371
1372 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1373}
1374
1375
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001376static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1377 char *argv[])
1378{
1379 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1380}
1381
1382
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001383#ifdef CONFIG_DPP
1384
1385static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
1386 char *argv[])
1387{
1388 return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
1389}
1390
1391
1392static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
1393 char *argv[])
1394{
1395 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
1396}
1397
1398
1399static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
1400 char *argv[])
1401{
1402 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
1403}
1404
1405
1406static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
1407 int argc, char *argv[])
1408{
1409 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
1410}
1411
1412
1413static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
1414 char *argv[])
1415{
1416 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
1417}
1418
1419
Hai Shalom899fcc72020-10-19 14:38:18 -07001420static int hostapd_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc,
1421 char *argv[])
1422{
1423 return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv);
1424}
1425
1426
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001427static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
1428 char *argv[])
1429{
1430 return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
1431}
1432
1433
Roshan Pius3a1667e2018-07-03 15:17:14 -07001434static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
1435 char *argv[])
1436{
1437 return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
1438}
1439
1440
1441static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
1442 char *argv[])
1443{
1444 return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
1445}
1446
1447
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001448static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
1449 char *argv[])
1450{
1451 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
1452}
1453
1454
1455static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
1456 int argc, char *argv[])
1457{
1458 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
1459}
1460
1461
Roshan Pius3a1667e2018-07-03 15:17:14 -07001462static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl,
1463 int argc, char *argv[])
1464{
1465 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
1466}
1467
1468
Hai Shalom74f70d42019-02-11 14:42:39 -08001469static int hostapd_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl,
1470 int argc, char *argv[])
1471{
1472 return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv);
1473}
1474
1475
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001476static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
1477 char *argv[])
1478{
1479 return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
1480}
1481
1482
1483static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
1484 char *argv[])
1485{
1486 return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
1487}
1488
Hai Shalom899fcc72020-10-19 14:38:18 -07001489
1490#ifdef CONFIG_DPP2
1491
Hai Shalom60840252021-02-19 19:02:11 -08001492static int hostapd_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc,
1493 char *argv[])
1494{
Sunil Ravi89eba102022-09-13 21:04:37 -07001495 return hostapd_cli_cmd(ctrl, "DPP_CONTROLLER_START", 0, argc, argv);
Hai Shalom60840252021-02-19 19:02:11 -08001496}
1497
1498
1499static int hostapd_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc,
1500 char *argv[])
1501{
1502 return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP");
1503}
1504
1505
Hai Shalom899fcc72020-10-19 14:38:18 -07001506static int hostapd_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc,
1507 char *argv[])
1508{
1509 return hostapd_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv);
1510}
1511
1512
1513static int hostapd_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc,
1514 char *argv[])
1515{
1516 return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP");
1517}
1518
1519#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001520
1521
1522#ifdef CONFIG_DPP3
1523static int hostapd_cli_cmd_dpp_push_button(struct wpa_ctrl *ctrl, int argc,
1524 char *argv[])
1525{
1526 return hostapd_cli_cmd(ctrl, "DPP_PUSH_BUTTON", 1, argc, argv);
1527}
1528#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001529#endif /* CONFIG_DPP */
1530
1531
Roshan Pius3a1667e2018-07-03 15:17:14 -07001532static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc,
1533 char *argv[])
1534{
1535 return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv);
1536}
1537
1538
1539static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc,
1540 char *argv[])
1541{
1542 return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv);
1543}
1544
1545
1546static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc,
1547 char *argv[])
1548{
1549 return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv);
1550}
1551
1552
Hai Shalom74f70d42019-02-11 14:42:39 -08001553static int hostapd_cli_cmd_req_beacon(struct wpa_ctrl *ctrl, int argc,
1554 char *argv[])
1555{
1556 return hostapd_cli_cmd(ctrl, "REQ_BEACON", 2, argc, argv);
1557}
1558
1559
1560static int hostapd_cli_cmd_reload_wpa_psk(struct wpa_ctrl *ctrl, int argc,
1561 char *argv[])
1562{
1563 return wpa_ctrl_command(ctrl, "RELOAD_WPA_PSK");
1564}
1565
1566
Hai Shaloma20dcd72022-02-04 13:43:00 -08001567#ifdef ANDROID
1568static int hostapd_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
1569{
1570 return hostapd_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
1571}
1572#endif /* ANDROID */
1573
1574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575struct hostapd_cli_cmd {
1576 const char *cmd;
1577 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001578 char ** (*completion)(const char *str, int pos);
1579 const char *usage;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580};
1581
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001582static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001583 { "ping", hostapd_cli_cmd_ping, NULL,
1584 "= pings hostapd" },
1585 { "mib", hostapd_cli_cmd_mib, NULL,
1586 "= get MIB variables (dot1x, dot11, radius)" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001587 { "relog", hostapd_cli_cmd_relog, NULL,
1588 "= reload/truncate debug log output file" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001589 { "close_log", hostapd_cli_cmd_close_log, NULL,
1590 "= disable debug log output file" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001591 { "status", hostapd_cli_cmd_status, NULL,
1592 "= show interface status info" },
1593 { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001594 "<addr> = get MIB variables for one station" },
1595 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1596 "= get MIB variables for all stations" },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001597 { "list_sta", hostapd_cli_cmd_list_sta, NULL,
1598 "= list all stations" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001599 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1600 "<addr> = add a new station" },
1601 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001602 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001603 "<addr> = deauthenticate a station" },
1604 { "disassociate", hostapd_cli_cmd_disassociate,
Dmitry Shmidt29333592017-01-09 12:27:11 -08001605 hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001606 "<addr> = disassociate a station" },
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001607#ifdef CONFIG_TAXONOMY
Dmitry Shmidt29333592017-01-09 12:27:11 -08001608 { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001609 "<addr> = get taxonomy signature for a station" },
1610#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001611 { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001612 "<addr> = send SA Query to a station" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613#ifdef CONFIG_WPS
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001614 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1615 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1616 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1617 "<PIN> = verify PIN checksum" },
1618 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1619 "= indicate button pushed to initiate PBC" },
1620 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1621 "= cancel the pending WPS operation" },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001622#ifdef CONFIG_WPS_NFC
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001623 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1624 "<hexdump> = report read NFC tag with WPS data" },
1625 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1626 "<WPS/NDEF> = build NFC configuration token" },
1627 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1628 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1629 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1630 NULL },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001631#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001632 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1633 "<cmd> [params..] = enable/disable AP PIN" },
1634 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1635 "<SSID> <auth> <encr> <key> = configure AP" },
1636 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1637 "= show current WPS status" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638#endif /* CONFIG_WPS */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001639 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
1640 "= send Disassociation Imminent notification" },
1641 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
1642 "= send ESS Dissassociation Imminent notification" },
1643 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
1644 "= send BSS Transition Management Request" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001645 { "get_config", hostapd_cli_cmd_get_config, NULL,
1646 "= show current configuration" },
1647 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1648 "= show this usage help" },
1649 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1650 "[ifname] = show interfaces/select interface" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001651#ifdef CONFIG_FST
Dmitry Shmidt29333592017-01-09 12:27:11 -08001652 { "fst", hostapd_cli_cmd_fst, NULL,
1653 "<params...> = send FST-MANAGER control interface command" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001654#endif /* CONFIG_FST */
Dmitry Shmidt29333592017-01-09 12:27:11 -08001655 { "raw", hostapd_cli_cmd_raw, NULL,
1656 "<params..> = send unprocessed command" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001657 { "level", hostapd_cli_cmd_level, NULL,
1658 "<debug level> = change debug level" },
1659 { "license", hostapd_cli_cmd_license, NULL,
1660 "= show full hostapd_cli license" },
1661 { "quit", hostapd_cli_cmd_quit, NULL,
1662 "= exit hostapd_cli" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001663 { "set", hostapd_cli_cmd_set, hostapd_complete_set,
1664 "<name> <value> = set runtime variables" },
1665 { "get", hostapd_cli_cmd_get, hostapd_complete_get,
1666 "<name> = get runtime info" },
1667 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
1668 "<arg,arg,...> = set QoS Map set element" },
1669 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
1670 hostapd_complete_stations,
1671 "<addr> = send QoS Map Configure frame" },
1672 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
1673 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
1674 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
1675 " = initiate channel switch announcement" },
1676 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
1677 "<addr> <url>\n"
1678 " = send WNM-Notification Subscription Remediation Request" },
1679 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
1680 "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
1681 " = send WNM-Notification imminent deauthentication indication" },
1682 { "vendor", hostapd_cli_cmd_vendor, NULL,
1683 "<vendor id> <sub command id> [<hex formatted data>]\n"
1684 " = send vendor driver command" },
1685 { "enable", hostapd_cli_cmd_enable, NULL,
1686 "= enable hostapd on current interface" },
1687 { "reload", hostapd_cli_cmd_reload, NULL,
1688 "= reload configuration for current interface" },
Sunil Ravi77d572f2023-01-17 23:58:31 +00001689 { "reload_bss", hostapd_cli_cmd_reload_bss, NULL,
1690 "= reload configuration for current BSS" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001691 { "disable", hostapd_cli_cmd_disable, NULL,
1692 "= disable hostapd on current interface" },
Hai Shalom81f62d82019-07-22 12:10:00 -07001693 { "update_beacon", hostapd_cli_cmd_update_beacon, NULL,
1694 "= update Beacon frame contents\n"},
Dmitry Shmidt29333592017-01-09 12:27:11 -08001695 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
1696 "= drop all ERP keys"},
1697 { "log_level", hostapd_cli_cmd_log_level, NULL,
1698 "[level] = show/change log verbosity level" },
1699 { "pmksa", hostapd_cli_cmd_pmksa, NULL,
1700 " = show PMKSA cache entries" },
1701 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
1702 " = flush PMKSA cache" },
1703 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
1704 "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
1705 " = add AP to neighbor database" },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001706 { "show_neighbor", hostapd_cli_cmd_show_neighbor, NULL,
1707 " = show neighbor database entries" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001708 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001709 "<addr> [ssid=<hex>] = remove AP from neighbor database" },
Dmitry Shmidt29333592017-01-09 12:27:11 -08001710 { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
1711 "<addr> = send LCI request to a station"},
1712 { "req_range", hostapd_cli_cmd_req_range, NULL,
1713 " = send FTM range request"},
1714 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
1715 " = show supported driver flags"},
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001716#ifdef CONFIG_DPP
1717 { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
1718 "report a scanned DPP URI from a QR Code" },
1719 { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
1720 "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
1721 { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
1722 "*|<id> = remove DPP bootstrap information" },
1723 { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
1724 "<id> = get DPP bootstrap URI" },
1725 { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
1726 "<id> = show DPP bootstrap information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001727 { "dpp_bootstrap_set", hostapd_cli_cmd_dpp_bootstrap_set, NULL,
1728 "<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 -07001729 { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
1730 "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
Roshan Pius3a1667e2018-07-03 15:17:14 -07001731 { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
1732 "<freq in MHz> = start DPP listen" },
1733 { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
1734 "= stop DPP listen" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001735 { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
1736 "[curve=..] [key=..] = add DPP configurator" },
1737 { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
1738 NULL,
1739 "*|<id> = remove DPP configurator" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001740 { "dpp_configurator_get_key", hostapd_cli_cmd_dpp_configurator_get_key,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001741 NULL,
1742 "<id> = Get DPP configurator's private key" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001743 { "dpp_configurator_sign", hostapd_cli_cmd_dpp_configurator_sign, NULL,
1744 "conf=<role> configurator=<id> = generate self DPP configuration" },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001745 { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
1746 "add PKEX code" },
1747 { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
1748 "*|<id> = remove DPP pkex information" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001749#ifdef CONFIG_DPP2
Hai Shalom60840252021-02-19 19:02:11 -08001750 { "dpp_controller_start", hostapd_cli_cmd_dpp_controller_start, NULL,
1751 "[tcp_port=<port>] [role=..] = start DPP controller" },
1752 { "dpp_controller_stop", hostapd_cli_cmd_dpp_controller_stop, NULL,
1753 "= stop DPP controller" },
Hai Shalom899fcc72020-10-19 14:38:18 -07001754 { "dpp_chirp", hostapd_cli_cmd_dpp_chirp, NULL,
1755 "own=<BI ID> iter=<count> = start DPP chirp" },
1756 { "dpp_stop_chirp", hostapd_cli_cmd_dpp_stop_chirp, NULL,
1757 "= stop DPP chirp" },
1758#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001759#ifdef CONFIG_DPP3
1760 { "dpp_push_button", hostapd_cli_cmd_dpp_push_button, NULL,
1761 "= press DPP push button" },
1762#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001763#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001764 { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL,
1765 "=Add/Delete/Show/Clear accept MAC ACL" },
1766 { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL,
1767 "=Add/Delete/Show/Clear deny MAC ACL" },
1768 { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations,
1769 "<addr> = poll a STA to check connectivity with a QoS null frame" },
Hai Shalom74f70d42019-02-11 14:42:39 -08001770 { "req_beacon", hostapd_cli_cmd_req_beacon, NULL,
1771 "<addr> [req_mode=] <measurement request hexdump> = send a Beacon report request to a station" },
1772 { "reload_wpa_psk", hostapd_cli_cmd_reload_wpa_psk, NULL,
1773 "= reload wpa_psk_file only" },
Hai Shaloma20dcd72022-02-04 13:43:00 -08001774#ifdef ANDROID
1775 { "driver", hostapd_cli_cmd_driver, NULL,
1776 "<driver sub command> [<hex formatted data>] = send driver command data" },
1777#endif /* ANDROID */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001778 { NULL, NULL, NULL, NULL }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779};
1780
1781
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001782/*
1783 * Prints command usage, lines are padded with the specified string.
1784 */
1785static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1786 const char *pad)
1787{
1788 char c;
1789 size_t n;
1790
1791 if (cmd->usage == NULL)
1792 return;
1793 fprintf(stream, "%s%s ", pad, cmd->cmd);
1794 for (n = 0; (c = cmd->usage[n]); n++) {
1795 fprintf(stream, "%c", c);
1796 if (c == '\n')
1797 fprintf(stream, "%s", pad);
1798 }
1799 fprintf(stream, "\n");
1800}
1801
1802
1803static void print_help(FILE *stream, const char *cmd)
1804{
1805 int n;
1806
1807 fprintf(stream, "commands:\n");
1808 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1809 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1810 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1811 }
1812}
1813
1814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1816{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001817 const struct hostapd_cli_cmd *cmd, *match = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 int count;
1819
1820 count = 0;
1821 cmd = hostapd_cli_commands;
1822 while (cmd->cmd) {
1823 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1824 match = cmd;
1825 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1826 /* we have an exact match */
1827 count = 1;
1828 break;
1829 }
1830 count++;
1831 }
1832 cmd++;
1833 }
1834
1835 if (count > 1) {
1836 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1837 cmd = hostapd_cli_commands;
1838 while (cmd->cmd) {
1839 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1840 0) {
1841 printf(" %s", cmd->cmd);
1842 }
1843 cmd++;
1844 }
1845 printf("\n");
1846 } else if (count == 0) {
1847 printf("Unknown command '%s'\n", argv[0]);
1848 } else {
1849 match->handler(ctrl, argc - 1, &argv[1]);
1850 }
1851}
1852
1853
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001854static void cli_event(const char *str)
1855{
1856 const char *start, *s;
1857
1858 start = os_strchr(str, '>');
1859 if (start == NULL)
1860 return;
1861
1862 start++;
1863
1864 if (str_starts(start, AP_STA_CONNECTED)) {
1865 s = os_strchr(start, ' ');
1866 if (s == NULL)
1867 return;
1868 cli_txt_list_add(&stations, s + 1);
1869 return;
1870 }
1871
1872 if (str_starts(start, AP_STA_DISCONNECTED)) {
1873 s = os_strchr(start, ' ');
1874 if (s == NULL)
1875 return;
1876 cli_txt_list_del_addr(&stations, s + 1);
1877 return;
1878 }
1879}
1880
1881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1883 int action_monitor)
1884{
1885 int first = 1;
1886 if (ctrl_conn == NULL)
1887 return;
1888 while (wpa_ctrl_pending(ctrl)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001889 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 size_t len = sizeof(buf) - 1;
1891 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1892 buf[len] = '\0';
1893 if (action_monitor)
1894 hostapd_cli_action_process(buf, len);
1895 else {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001896 cli_event(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 if (in_read && first)
1898 printf("\n");
1899 first = 0;
1900 printf("%s\n", buf);
1901 }
1902 } else {
1903 printf("Could not read pending message.\n");
1904 break;
1905 }
1906 }
1907}
1908
1909
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001910static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001912 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913}
1914
1915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001916static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917{
1918 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1919 printf("Connection to hostapd lost - trying to reconnect\n");
1920 hostapd_cli_close_connection();
1921 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001922 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
1923 printf("Connection to hostapd re-established\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 if (ctrl_conn)
1925 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001926 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1927}
1928
1929
1930static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1931{
1932 eloop_terminate();
1933}
1934
1935
1936static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1937{
1938 char *argv[max_args];
1939 int argc;
1940 argc = tokenize_cmd(cmd, argv);
1941 if (argc)
1942 wpa_request(ctrl_conn, argc, argv);
1943}
1944
1945
1946static void hostapd_cli_edit_eof_cb(void *ctx)
1947{
1948 eloop_terminate();
1949}
1950
1951
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001952static char ** list_cmd_list(void)
1953{
1954 char **res;
1955 int i, count;
1956
1957 count = ARRAY_SIZE(hostapd_cli_commands);
1958 res = os_calloc(count + 1, sizeof(char *));
1959 if (res == NULL)
1960 return NULL;
1961
1962 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1963 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1964 if (res[i] == NULL)
1965 break;
1966 }
1967
1968 return res;
1969}
1970
1971
1972static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1973 int pos)
1974{
1975 int i;
1976
1977 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1978 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
1979 continue;
1980 if (hostapd_cli_commands[i].completion)
1981 return hostapd_cli_commands[i].completion(str, pos);
1982 if (!hostapd_cli_commands[i].usage)
1983 return NULL;
1984 edit_clear_line();
1985 printf("\r%s\n", hostapd_cli_commands[i].usage);
1986 edit_redraw();
1987 break;
1988 }
1989
1990 return NULL;
1991}
1992
1993
1994static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
1995 int pos)
1996{
1997 char **res;
1998 const char *end;
1999 char *cmd;
2000
2001 end = os_strchr(str, ' ');
2002 if (end == NULL || str + pos < end)
2003 return list_cmd_list();
2004
2005 cmd = os_malloc(pos + 1);
2006 if (cmd == NULL)
2007 return NULL;
2008 os_memcpy(cmd, str, pos);
2009 cmd[end - str] = '\0';
2010 res = hostapd_cli_cmd_completion(cmd, str, pos);
2011 os_free(cmd);
2012 return res;
2013}
2014
2015
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002016static void hostapd_cli_interactive(void)
2017{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002018 char *hfile = NULL;
2019 char *home;
2020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021 printf("\nInteractive mode\n\n");
2022
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002023#ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
2024 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
2025#else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2026 home = getenv("HOME");
2027#endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
2028 if (home) {
2029 const char *fname = ".hostapd_cli_history";
2030 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
2031 hfile = os_malloc(hfile_len);
2032 if (hfile)
2033 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
2034 }
2035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002036 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
2037 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002038 hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002039 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
2040
2041 eloop_run();
2042
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002043 cli_txt_list_flush(&stations);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002044 edit_deinit(hfile, NULL);
2045 os_free(hfile);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002046 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
2047}
2048
2049
2050static void hostapd_cli_cleanup(void)
2051{
2052 hostapd_cli_close_connection();
2053 if (pid_file)
2054 os_daemonize_terminate(pid_file);
2055
2056 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057}
2058
2059
2060static void hostapd_cli_action(struct wpa_ctrl *ctrl)
2061{
2062 fd_set rfds;
2063 int fd, res;
2064 struct timeval tv;
2065 char buf[256];
2066 size_t len;
2067
2068 fd = wpa_ctrl_get_fd(ctrl);
2069
2070 while (!hostapd_cli_quit) {
2071 FD_ZERO(&rfds);
2072 FD_SET(fd, &rfds);
2073 tv.tv_sec = ping_interval;
2074 tv.tv_usec = 0;
2075 res = select(fd + 1, &rfds, NULL, NULL, &tv);
2076 if (res < 0 && errno != EINTR) {
2077 perror("select");
2078 break;
2079 }
2080
2081 if (FD_ISSET(fd, &rfds))
2082 hostapd_cli_recv_pending(ctrl, 0, 1);
2083 else {
2084 len = sizeof(buf) - 1;
2085 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
2086 hostapd_cli_action_process) < 0 ||
2087 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
2088 printf("hostapd did not reply to PING "
2089 "command - exiting\n");
2090 break;
2091 }
2092 }
2093 }
2094}
2095
2096
2097int main(int argc, char *argv[])
2098{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 int warning_displayed = 0;
2100 int c;
2101 int daemonize = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002102 int reconnect = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103
2104 if (os_program_init())
2105 return -1;
2106
2107 for (;;) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002108 c = getopt(argc, argv, "a:BhG:i:p:P:rs:v");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 if (c < 0)
2110 break;
2111 switch (c) {
2112 case 'a':
2113 action_file = optarg;
2114 break;
2115 case 'B':
2116 daemonize = 1;
2117 break;
2118 case 'G':
2119 ping_interval = atoi(optarg);
2120 break;
2121 case 'h':
2122 usage();
2123 return 0;
2124 case 'v':
2125 printf("%s\n", hostapd_cli_version);
2126 return 0;
2127 case 'i':
2128 os_free(ctrl_ifname);
2129 ctrl_ifname = os_strdup(optarg);
2130 break;
2131 case 'p':
2132 ctrl_iface_dir = optarg;
2133 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002134 case 'P':
2135 pid_file = optarg;
2136 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002137 case 'r':
2138 reconnect = 1;
2139 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002140 case 's':
2141 client_socket_dir = optarg;
2142 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 default:
2144 usage();
2145 return -1;
2146 }
2147 }
2148
2149 interactive = (argc == optind) && (action_file == NULL);
2150
2151 if (interactive) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002152 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 }
2154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002155 if (eloop_init())
2156 return -1;
2157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 for (;;) {
2159 if (ctrl_ifname == NULL) {
2160 struct dirent *dent;
2161 DIR *dir = opendir(ctrl_iface_dir);
2162 if (dir) {
2163 while ((dent = readdir(dir))) {
2164 if (os_strcmp(dent->d_name, ".") == 0
2165 ||
2166 os_strcmp(dent->d_name, "..") == 0)
2167 continue;
2168 printf("Selected interface '%s'\n",
2169 dent->d_name);
2170 ctrl_ifname = os_strdup(dent->d_name);
2171 break;
2172 }
2173 closedir(dir);
2174 }
2175 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002176 hostapd_cli_reconnect(ctrl_ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 if (ctrl_conn) {
2178 if (warning_displayed)
2179 printf("Connection established.\n");
2180 break;
2181 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002182 if (!interactive && !reconnect) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 perror("Failed to connect to hostapd - "
2184 "wpa_ctrl_open");
2185 return -1;
2186 }
2187
2188 if (!warning_displayed) {
2189 printf("Could not connect to hostapd - re-trying\n");
2190 warning_displayed = 1;
2191 }
2192 os_sleep(1, 0);
2193 continue;
2194 }
2195
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002196 if (action_file && !hostapd_cli_attached)
2197 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002198 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002200 if (reconnect && action_file && ctrl_ifname) {
2201 while (!hostapd_cli_quit) {
2202 if (ctrl_conn)
2203 hostapd_cli_action(ctrl_conn);
2204 os_sleep(1, 0);
2205 hostapd_cli_reconnect(ctrl_ifname);
2206 }
2207 } else if (interactive)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 hostapd_cli_interactive();
2209 else if (action_file)
2210 hostapd_cli_action(ctrl_conn);
2211 else
2212 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
2213
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002214 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002216 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 hostapd_cli_cleanup();
2218 return 0;
2219}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002220
2221#else /* CONFIG_NO_CTRL_IFACE */
2222
2223int main(int argc, char *argv[])
2224{
2225 return -1;
2226}
2227
2228#endif /* CONFIG_NO_CTRL_IFACE */