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