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