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