2 * WPA Supplicant - command line interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifdef CONFIG_CTRL_IFACE
19 #ifdef CONFIG_CTRL_IFACE_UNIX
21 #endif /* CONFIG_CTRL_IFACE_UNIX */
23 #include "common/wpa_ctrl.h"
24 #include "utils/common.h"
25 #include "utils/eloop.h"
26 #include "utils/edit.h"
27 #include "common/version.h"
30 static const char *wpa_cli_version
=
31 "wpa_cli v" VERSION_STR
"\n"
32 "Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi> and contributors";
35 static const char *wpa_cli_license
=
36 "This program is free software. You can distribute it and/or modify it\n"
37 "under the terms of the GNU General Public License version 2.\n"
39 "Alternatively, this software may be distributed under the terms of the\n"
40 "BSD license. See README and COPYING for more details.\n";
42 static const char *wpa_cli_full_license
=
43 "This program is free software; you can redistribute it and/or modify\n"
44 "it under the terms of the GNU General Public License version 2 as\n"
45 "published by the Free Software Foundation.\n"
47 "This program is distributed in the hope that it will be useful,\n"
48 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
49 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
50 "GNU General Public License for more details.\n"
52 "You should have received a copy of the GNU General Public License\n"
53 "along with this program; if not, write to the Free Software\n"
54 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
56 "Alternatively, this software may be distributed under the terms of the\n"
59 "Redistribution and use in source and binary forms, with or without\n"
60 "modification, are permitted provided that the following conditions are\n"
63 "1. Redistributions of source code must retain the above copyright\n"
64 " notice, this list of conditions and the following disclaimer.\n"
66 "2. Redistributions in binary form must reproduce the above copyright\n"
67 " notice, this list of conditions and the following disclaimer in the\n"
68 " documentation and/or other materials provided with the distribution.\n"
70 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
71 " names of its contributors may be used to endorse or promote products\n"
72 " derived from this software without specific prior written permission.\n"
74 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
75 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
76 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
77 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
78 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
79 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
80 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
81 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
82 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
83 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
84 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
87 static struct wpa_ctrl
*ctrl_conn
;
88 static struct wpa_ctrl
*mon_conn
;
89 static int wpa_cli_quit
= 0;
90 static int wpa_cli_attached
= 0;
91 static int wpa_cli_connected
= 0;
92 static int wpa_cli_last_id
= 0;
93 static const char *ctrl_iface_dir
= "/var/run/wpa_supplicant";
94 static char *ctrl_ifname
= NULL
;
95 static const char *pid_file
= NULL
;
96 static const char *action_file
= NULL
;
97 static int ping_interval
= 5;
98 static int interactive
= 0;
101 static void print_help(void);
102 static void wpa_cli_mon_receive(int sock
, void *eloop_ctx
, void *sock_ctx
);
105 static void usage(void)
107 printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
108 "[-a<action file>] \\\n"
109 " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
111 " -h = help (show this usage text)\n"
112 " -v = shown version information\n"
113 " -a = run in daemon mode executing the action file based on "
116 " -B = run a daemon in the background\n"
117 " default path: /var/run/wpa_supplicant\n"
118 " default interface: first interface found in socket path\n");
123 static int str_starts(const char *src
, const char *match
)
125 return os_strncmp(src
, match
, os_strlen(match
)) == 0;
129 static int wpa_cli_show_event(const char *event
)
133 start
= os_strchr(event
, '>');
139 * Skip BSS added/removed events since they can be relatively frequent
140 * and are likely of not much use for an interactive user.
142 if (str_starts(start
, WPA_EVENT_BSS_ADDED
) ||
143 str_starts(start
, WPA_EVENT_BSS_REMOVED
))
150 static int wpa_cli_open_connection(const char *ifname
, int attach
)
152 #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
153 ctrl_conn
= wpa_ctrl_open(ifname
);
154 if (ctrl_conn
== NULL
)
157 if (attach
&& interactive
)
158 mon_conn
= wpa_ctrl_open(ifname
);
161 #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
168 flen
= os_strlen(ctrl_iface_dir
) + os_strlen(ifname
) + 2;
169 cfile
= os_malloc(flen
);
172 res
= os_snprintf(cfile
, flen
, "%s/%s", ctrl_iface_dir
, ifname
);
173 if (res
< 0 || res
>= flen
) {
178 ctrl_conn
= wpa_ctrl_open(cfile
);
179 if (ctrl_conn
== NULL
) {
184 if (attach
&& interactive
)
185 mon_conn
= wpa_ctrl_open(cfile
);
189 #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
192 if (wpa_ctrl_attach(mon_conn
) == 0) {
193 wpa_cli_attached
= 1;
195 eloop_register_read_sock(
196 wpa_ctrl_get_fd(mon_conn
),
197 wpa_cli_mon_receive
, NULL
, NULL
);
199 printf("Warning: Failed to attach to "
200 "wpa_supplicant.\n");
209 static void wpa_cli_close_connection(void)
211 if (ctrl_conn
== NULL
)
214 if (wpa_cli_attached
) {
215 wpa_ctrl_detach(interactive
? mon_conn
: ctrl_conn
);
216 wpa_cli_attached
= 0;
218 wpa_ctrl_close(ctrl_conn
);
221 eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn
));
222 wpa_ctrl_close(mon_conn
);
228 static void wpa_cli_msg_cb(char *msg
, size_t len
)
234 static int _wpa_ctrl_command(struct wpa_ctrl
*ctrl
, char *cmd
, int print
)
240 if (ctrl_conn
== NULL
) {
241 printf("Not connected to wpa_supplicant - command dropped.\n");
244 len
= sizeof(buf
) - 1;
245 ret
= wpa_ctrl_request(ctrl
, cmd
, os_strlen(cmd
), buf
, &len
,
248 printf("'%s' command timed out.\n", cmd
);
250 } else if (ret
< 0) {
251 printf("'%s' command failed.\n", cmd
);
262 static int wpa_ctrl_command(struct wpa_ctrl
*ctrl
, char *cmd
)
264 return _wpa_ctrl_command(ctrl
, cmd
, 1);
268 static int wpa_cli_cmd_status(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
270 int verbose
= argc
> 0 && os_strcmp(argv
[0], "verbose") == 0;
271 return wpa_ctrl_command(ctrl
, verbose
? "STATUS-VERBOSE" : "STATUS");
275 static int wpa_cli_cmd_ping(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
277 return wpa_ctrl_command(ctrl
, "PING");
281 static int wpa_cli_cmd_relog(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
283 return wpa_ctrl_command(ctrl
, "RELOG");
287 static int wpa_cli_cmd_note(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
293 ret
= os_snprintf(cmd
, sizeof(cmd
), "NOTE %s", argv
[0]);
294 if (ret
< 0 || (size_t) ret
>= sizeof(cmd
))
296 return wpa_ctrl_command(ctrl
, cmd
);
300 static int wpa_cli_cmd_mib(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
302 return wpa_ctrl_command(ctrl
, "MIB");
306 static int wpa_cli_cmd_pmksa(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
308 return wpa_ctrl_command(ctrl
, "PMKSA");
312 static int wpa_cli_cmd_help(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
319 static int wpa_cli_cmd_license(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
321 printf("%s\n\n%s\n", wpa_cli_version
, wpa_cli_full_license
);
326 static int wpa_cli_cmd_quit(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
335 static void wpa_cli_show_variables(void)
337 printf("set variables:\n"
338 " EAPOL::heldPeriod (EAPOL state machine held period, "
340 " EAPOL::authPeriod (EAPOL state machine authentication "
341 "period, in seconds)\n"
342 " EAPOL::startPeriod (EAPOL state machine start period, in "
344 " EAPOL::maxStart (EAPOL state machine maximum start "
346 printf(" dot11RSNAConfigPMKLifetime (WPA/WPA2 PMK lifetime in "
348 " dot11RSNAConfigPMKReauthThreshold (WPA/WPA2 reauthentication"
349 " threshold\n\tpercentage)\n"
350 " dot11RSNAConfigSATimeout (WPA/WPA2 timeout for completing "
351 "security\n\tassociation in seconds)\n");
355 static int wpa_cli_cmd_set(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
361 wpa_cli_show_variables();
366 printf("Invalid SET command: needs two arguments (variable "
367 "name and value)\n");
371 res
= os_snprintf(cmd
, sizeof(cmd
), "SET %s %s", argv
[0], argv
[1]);
372 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
373 printf("Too long SET command.\n");
376 return wpa_ctrl_command(ctrl
, cmd
);
380 static int wpa_cli_cmd_get(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
386 printf("Invalid GET command: need one argument (variable "
391 res
= os_snprintf(cmd
, sizeof(cmd
), "GET %s", argv
[0]);
392 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
393 printf("Too long GET command.\n");
396 return wpa_ctrl_command(ctrl
, cmd
);
400 static int wpa_cli_cmd_logoff(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
402 return wpa_ctrl_command(ctrl
, "LOGOFF");
406 static int wpa_cli_cmd_logon(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
408 return wpa_ctrl_command(ctrl
, "LOGON");
412 static int wpa_cli_cmd_reassociate(struct wpa_ctrl
*ctrl
, int argc
,
415 return wpa_ctrl_command(ctrl
, "REASSOCIATE");
419 static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl
*ctrl
, int argc
,
426 printf("Invalid PREAUTH command: needs one argument "
431 res
= os_snprintf(cmd
, sizeof(cmd
), "PREAUTH %s", argv
[0]);
432 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
433 printf("Too long PREAUTH command.\n");
436 return wpa_ctrl_command(ctrl
, cmd
);
440 static int wpa_cli_cmd_ap_scan(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
446 printf("Invalid AP_SCAN command: needs one argument (ap_scan "
450 res
= os_snprintf(cmd
, sizeof(cmd
), "AP_SCAN %s", argv
[0]);
451 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
452 printf("Too long AP_SCAN command.\n");
455 return wpa_ctrl_command(ctrl
, cmd
);
459 static int wpa_cli_cmd_stkstart(struct wpa_ctrl
*ctrl
, int argc
,
466 printf("Invalid STKSTART command: needs one argument "
467 "(Peer STA MAC address)\n");
471 res
= os_snprintf(cmd
, sizeof(cmd
), "STKSTART %s", argv
[0]);
472 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
473 printf("Too long STKSTART command.\n");
476 return wpa_ctrl_command(ctrl
, cmd
);
480 static int wpa_cli_cmd_ft_ds(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
486 printf("Invalid FT_DS command: needs one argument "
487 "(Target AP MAC address)\n");
491 res
= os_snprintf(cmd
, sizeof(cmd
), "FT_DS %s", argv
[0]);
492 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
493 printf("Too long FT_DS command.\n");
496 return wpa_ctrl_command(ctrl
, cmd
);
500 static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
507 return wpa_ctrl_command(ctrl
, "WPS_PBC");
511 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_PBC %s", argv
[0]);
512 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
513 printf("Too long WPS_PBC command.\n");
516 return wpa_ctrl_command(ctrl
, cmd
);
520 static int wpa_cli_cmd_wps_pin(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
526 printf("Invalid WPS_PIN command: need one or two arguments:\n"
527 "- BSSID: use 'any' to select any\n"
528 "- PIN: optional, used only with devices that have no "
534 /* Use dynamically generated PIN (returned as reply) */
535 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_PIN %s", argv
[0]);
536 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
537 printf("Too long WPS_PIN command.\n");
540 return wpa_ctrl_command(ctrl
, cmd
);
543 /* Use hardcoded PIN from a label */
544 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_PIN %s %s", argv
[0], argv
[1]);
545 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
546 printf("Too long WPS_PIN command.\n");
549 return wpa_ctrl_command(ctrl
, cmd
);
553 static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl
*ctrl
, int argc
,
559 if (argc
!= 1 && argc
!= 2) {
560 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
561 "- PIN to be verified\n");
566 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s %s",
569 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_CHECK_PIN %s",
571 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
572 printf("Too long WPS_CHECK_PIN command.\n");
575 return wpa_ctrl_command(ctrl
, cmd
);
579 static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl
*ctrl
, int argc
,
582 return wpa_ctrl_command(ctrl
, "WPS_CANCEL");
586 #ifdef CONFIG_WPS_OOB
587 static int wpa_cli_cmd_wps_oob(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
592 if (argc
!= 3 && argc
!= 4) {
593 printf("Invalid WPS_OOB command: need three or four "
595 "- DEV_TYPE: use 'ufd' or 'nfc'\n"
596 "- PATH: path of OOB device like '/mnt'\n"
597 "- METHOD: OOB method 'pin-e' or 'pin-r', "
599 "- DEV_NAME: (only for NFC) device name like "
605 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_OOB %s %s %s",
606 argv
[0], argv
[1], argv
[2]);
608 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_OOB %s %s %s %s",
609 argv
[0], argv
[1], argv
[2], argv
[3]);
610 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
611 printf("Too long WPS_OOB command.\n");
614 return wpa_ctrl_command(ctrl
, cmd
);
616 #endif /* CONFIG_WPS_OOB */
619 static int wpa_cli_cmd_wps_reg(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
625 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_REG %s %s",
627 else if (argc
== 5 || argc
== 6) {
628 char ssid_hex
[2 * 32 + 1];
629 char key_hex
[2 * 64 + 1];
633 for (i
= 0; i
< 32; i
++) {
634 if (argv
[2][i
] == '\0')
636 os_snprintf(&ssid_hex
[i
* 2], 3, "%02x", argv
[2][i
]);
641 for (i
= 0; i
< 64; i
++) {
642 if (argv
[5][i
] == '\0')
644 os_snprintf(&key_hex
[i
* 2], 3, "%02x",
649 res
= os_snprintf(cmd
, sizeof(cmd
),
650 "WPS_REG %s %s %s %s %s %s",
651 argv
[0], argv
[1], ssid_hex
, argv
[3], argv
[4],
654 printf("Invalid WPS_REG command: need two arguments:\n"
655 "- BSSID of the target AP\n"
657 printf("Alternatively, six arguments can be used to "
658 "reconfigure the AP:\n"
659 "- BSSID of the target AP\n"
662 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
663 "- new encr (NONE, WEP, TKIP, CCMP)\n"
668 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
669 printf("Too long WPS_REG command.\n");
672 return wpa_ctrl_command(ctrl
, cmd
);
676 static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl
*ctrl
, int argc
,
683 printf("Invalid WPS_AP_PIN command: needs at least one "
689 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_AP_PIN %s %s %s",
690 argv
[0], argv
[1], argv
[2]);
692 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_AP_PIN %s %s",
695 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_AP_PIN %s",
697 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
698 printf("Too long WPS_AP_PIN command.\n");
701 return wpa_ctrl_command(ctrl
, cmd
);
705 static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl
*ctrl
, int argc
,
710 os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_START %s", argv
[0]);
711 return wpa_ctrl_command(ctrl
, cmd
);
713 return wpa_ctrl_command(ctrl
, "WPS_ER_START");
717 static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl
*ctrl
, int argc
,
720 return wpa_ctrl_command(ctrl
, "WPS_ER_STOP");
725 static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl
*ctrl
, int argc
,
732 printf("Invalid WPS_ER_PIN command: need at least two "
734 "- UUID: use 'any' to select any\n"
735 "- PIN: Enrollee PIN\n"
736 "optional: - Enrollee MAC address\n");
741 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_PIN %s %s %s",
742 argv
[0], argv
[1], argv
[2]);
744 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_PIN %s %s",
746 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
747 printf("Too long WPS_ER_PIN command.\n");
750 return wpa_ctrl_command(ctrl
, cmd
);
754 static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl
*ctrl
, int argc
,
761 printf("Invalid WPS_ER_PBC command: need one argument:\n"
762 "- UUID: Specify the Enrollee\n");
766 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_PBC %s",
768 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
769 printf("Too long WPS_ER_PBC command.\n");
772 return wpa_ctrl_command(ctrl
, cmd
);
776 static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl
*ctrl
, int argc
,
783 printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
784 "- UUID: specify which AP to use\n"
789 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_LEARN %s %s",
791 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
792 printf("Too long WPS_ER_LEARN command.\n");
795 return wpa_ctrl_command(ctrl
, cmd
);
799 static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl
*ctrl
, int argc
,
806 printf("Invalid WPS_ER_SET_CONFIG command: need two "
808 "- UUID: specify which AP to use\n"
809 "- Network configuration id\n");
813 res
= os_snprintf(cmd
, sizeof(cmd
), "WPS_ER_SET_CONFIG %s %s",
815 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
816 printf("Too long WPS_ER_SET_CONFIG command.\n");
819 return wpa_ctrl_command(ctrl
, cmd
);
823 static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl
*ctrl
, int argc
,
829 if (argc
== 5 || argc
== 6) {
830 char ssid_hex
[2 * 32 + 1];
831 char key_hex
[2 * 64 + 1];
835 for (i
= 0; i
< 32; i
++) {
836 if (argv
[2][i
] == '\0')
838 os_snprintf(&ssid_hex
[i
* 2], 3, "%02x", argv
[2][i
]);
843 for (i
= 0; i
< 64; i
++) {
844 if (argv
[5][i
] == '\0')
846 os_snprintf(&key_hex
[i
* 2], 3, "%02x",
851 res
= os_snprintf(cmd
, sizeof(cmd
),
852 "WPS_ER_CONFIG %s %s %s %s %s %s",
853 argv
[0], argv
[1], ssid_hex
, argv
[3], argv
[4],
856 printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
860 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
861 "- new encr (NONE, WEP, TKIP, CCMP)\n"
866 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
867 printf("Too long WPS_ER_CONFIG command.\n");
870 return wpa_ctrl_command(ctrl
, cmd
);
874 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
880 printf("Invalid IBSS_RSN command: needs one argument "
881 "(Peer STA MAC address)\n");
885 res
= os_snprintf(cmd
, sizeof(cmd
), "IBSS_RSN %s", argv
[0]);
886 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
887 printf("Too long IBSS_RSN command.\n");
890 return wpa_ctrl_command(ctrl
, cmd
);
894 static int wpa_cli_cmd_level(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
900 printf("Invalid LEVEL command: needs one argument (debug "
904 res
= os_snprintf(cmd
, sizeof(cmd
), "LEVEL %s", argv
[0]);
905 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
906 printf("Too long LEVEL command.\n");
909 return wpa_ctrl_command(ctrl
, cmd
);
913 static int wpa_cli_cmd_identity(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
915 char cmd
[256], *pos
, *end
;
919 printf("Invalid IDENTITY command: needs two arguments "
920 "(network id and identity)\n");
924 end
= cmd
+ sizeof(cmd
);
926 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"IDENTITY-%s:%s",
928 if (ret
< 0 || ret
>= end
- pos
) {
929 printf("Too long IDENTITY command.\n");
933 for (i
= 2; i
< argc
; i
++) {
934 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
935 if (ret
< 0 || ret
>= end
- pos
) {
936 printf("Too long IDENTITY command.\n");
942 return wpa_ctrl_command(ctrl
, cmd
);
946 static int wpa_cli_cmd_password(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
948 char cmd
[256], *pos
, *end
;
952 printf("Invalid PASSWORD command: needs two arguments "
953 "(network id and password)\n");
957 end
= cmd
+ sizeof(cmd
);
959 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"PASSWORD-%s:%s",
961 if (ret
< 0 || ret
>= end
- pos
) {
962 printf("Too long PASSWORD command.\n");
966 for (i
= 2; i
< argc
; i
++) {
967 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
968 if (ret
< 0 || ret
>= end
- pos
) {
969 printf("Too long PASSWORD command.\n");
975 return wpa_ctrl_command(ctrl
, cmd
);
979 static int wpa_cli_cmd_new_password(struct wpa_ctrl
*ctrl
, int argc
,
982 char cmd
[256], *pos
, *end
;
986 printf("Invalid NEW_PASSWORD command: needs two arguments "
987 "(network id and password)\n");
991 end
= cmd
+ sizeof(cmd
);
993 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"NEW_PASSWORD-%s:%s",
995 if (ret
< 0 || ret
>= end
- pos
) {
996 printf("Too long NEW_PASSWORD command.\n");
1000 for (i
= 2; i
< argc
; i
++) {
1001 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
1002 if (ret
< 0 || ret
>= end
- pos
) {
1003 printf("Too long NEW_PASSWORD command.\n");
1009 return wpa_ctrl_command(ctrl
, cmd
);
1013 static int wpa_cli_cmd_pin(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1015 char cmd
[256], *pos
, *end
;
1019 printf("Invalid PIN command: needs two arguments "
1020 "(network id and pin)\n");
1024 end
= cmd
+ sizeof(cmd
);
1026 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"PIN-%s:%s",
1028 if (ret
< 0 || ret
>= end
- pos
) {
1029 printf("Too long PIN command.\n");
1033 for (i
= 2; i
< argc
; i
++) {
1034 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
1035 if (ret
< 0 || ret
>= end
- pos
) {
1036 printf("Too long PIN command.\n");
1041 return wpa_ctrl_command(ctrl
, cmd
);
1045 static int wpa_cli_cmd_otp(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1047 char cmd
[256], *pos
, *end
;
1051 printf("Invalid OTP command: needs two arguments (network "
1052 "id and password)\n");
1056 end
= cmd
+ sizeof(cmd
);
1058 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"OTP-%s:%s",
1060 if (ret
< 0 || ret
>= end
- pos
) {
1061 printf("Too long OTP command.\n");
1065 for (i
= 2; i
< argc
; i
++) {
1066 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
1067 if (ret
< 0 || ret
>= end
- pos
) {
1068 printf("Too long OTP command.\n");
1074 return wpa_ctrl_command(ctrl
, cmd
);
1078 static int wpa_cli_cmd_passphrase(struct wpa_ctrl
*ctrl
, int argc
,
1081 char cmd
[256], *pos
, *end
;
1085 printf("Invalid PASSPHRASE command: needs two arguments "
1086 "(network id and passphrase)\n");
1090 end
= cmd
+ sizeof(cmd
);
1092 ret
= os_snprintf(pos
, end
- pos
, WPA_CTRL_RSP
"PASSPHRASE-%s:%s",
1094 if (ret
< 0 || ret
>= end
- pos
) {
1095 printf("Too long PASSPHRASE command.\n");
1099 for (i
= 2; i
< argc
; i
++) {
1100 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
1101 if (ret
< 0 || ret
>= end
- pos
) {
1102 printf("Too long PASSPHRASE command.\n");
1108 return wpa_ctrl_command(ctrl
, cmd
);
1112 static int wpa_cli_cmd_bssid(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1114 char cmd
[256], *pos
, *end
;
1118 printf("Invalid BSSID command: needs two arguments (network "
1123 end
= cmd
+ sizeof(cmd
);
1125 ret
= os_snprintf(pos
, end
- pos
, "BSSID");
1126 if (ret
< 0 || ret
>= end
- pos
) {
1127 printf("Too long BSSID command.\n");
1131 for (i
= 0; i
< argc
; i
++) {
1132 ret
= os_snprintf(pos
, end
- pos
, " %s", argv
[i
]);
1133 if (ret
< 0 || ret
>= end
- pos
) {
1134 printf("Too long BSSID command.\n");
1140 return wpa_ctrl_command(ctrl
, cmd
);
1144 static int wpa_cli_cmd_list_networks(struct wpa_ctrl
*ctrl
, int argc
,
1147 return wpa_ctrl_command(ctrl
, "LIST_NETWORKS");
1151 static int wpa_cli_cmd_select_network(struct wpa_ctrl
*ctrl
, int argc
,
1158 printf("Invalid SELECT_NETWORK command: needs one argument "
1163 res
= os_snprintf(cmd
, sizeof(cmd
), "SELECT_NETWORK %s", argv
[0]);
1164 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1166 cmd
[sizeof(cmd
) - 1] = '\0';
1168 return wpa_ctrl_command(ctrl
, cmd
);
1172 static int wpa_cli_cmd_enable_network(struct wpa_ctrl
*ctrl
, int argc
,
1179 printf("Invalid ENABLE_NETWORK command: needs one argument "
1184 res
= os_snprintf(cmd
, sizeof(cmd
), "ENABLE_NETWORK %s", argv
[0]);
1185 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1187 cmd
[sizeof(cmd
) - 1] = '\0';
1189 return wpa_ctrl_command(ctrl
, cmd
);
1193 static int wpa_cli_cmd_disable_network(struct wpa_ctrl
*ctrl
, int argc
,
1200 printf("Invalid DISABLE_NETWORK command: needs one argument "
1205 res
= os_snprintf(cmd
, sizeof(cmd
), "DISABLE_NETWORK %s", argv
[0]);
1206 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1208 cmd
[sizeof(cmd
) - 1] = '\0';
1210 return wpa_ctrl_command(ctrl
, cmd
);
1214 static int wpa_cli_cmd_add_network(struct wpa_ctrl
*ctrl
, int argc
,
1217 return wpa_ctrl_command(ctrl
, "ADD_NETWORK");
1221 static int wpa_cli_cmd_remove_network(struct wpa_ctrl
*ctrl
, int argc
,
1228 printf("Invalid REMOVE_NETWORK command: needs one argument "
1233 res
= os_snprintf(cmd
, sizeof(cmd
), "REMOVE_NETWORK %s", argv
[0]);
1234 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1236 cmd
[sizeof(cmd
) - 1] = '\0';
1238 return wpa_ctrl_command(ctrl
, cmd
);
1242 static void wpa_cli_show_network_variables(void)
1244 printf("set_network variables:\n"
1245 " ssid (network name, SSID)\n"
1246 " psk (WPA passphrase or pre-shared key)\n"
1247 " key_mgmt (key management protocol)\n"
1248 " identity (EAP identity)\n"
1249 " password (EAP password)\n"
1252 "Note: Values are entered in the same format as the "
1253 "configuration file is using,\n"
1254 "i.e., strings values need to be inside double quotation "
1256 "For example: set_network 1 ssid \"network name\"\n"
1258 "Please see wpa_supplicant.conf documentation for full list "
1259 "of\navailable variables.\n");
1263 static int wpa_cli_cmd_set_network(struct wpa_ctrl
*ctrl
, int argc
,
1270 wpa_cli_show_network_variables();
1275 printf("Invalid SET_NETWORK command: needs three arguments\n"
1276 "(network id, variable name, and value)\n");
1280 res
= os_snprintf(cmd
, sizeof(cmd
), "SET_NETWORK %s %s %s",
1281 argv
[0], argv
[1], argv
[2]);
1282 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
1283 printf("Too long SET_NETWORK command.\n");
1286 return wpa_ctrl_command(ctrl
, cmd
);
1290 static int wpa_cli_cmd_get_network(struct wpa_ctrl
*ctrl
, int argc
,
1297 wpa_cli_show_network_variables();
1302 printf("Invalid GET_NETWORK command: needs two arguments\n"
1303 "(network id and variable name)\n");
1307 res
= os_snprintf(cmd
, sizeof(cmd
), "GET_NETWORK %s %s",
1309 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
1310 printf("Too long GET_NETWORK command.\n");
1313 return wpa_ctrl_command(ctrl
, cmd
);
1317 static int wpa_cli_cmd_disconnect(struct wpa_ctrl
*ctrl
, int argc
,
1320 return wpa_ctrl_command(ctrl
, "DISCONNECT");
1324 static int wpa_cli_cmd_reconnect(struct wpa_ctrl
*ctrl
, int argc
,
1327 return wpa_ctrl_command(ctrl
, "RECONNECT");
1331 static int wpa_cli_cmd_save_config(struct wpa_ctrl
*ctrl
, int argc
,
1334 return wpa_ctrl_command(ctrl
, "SAVE_CONFIG");
1338 static int wpa_cli_cmd_scan(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1340 return wpa_ctrl_command(ctrl
, "SCAN");
1344 static int wpa_cli_cmd_scan_results(struct wpa_ctrl
*ctrl
, int argc
,
1347 return wpa_ctrl_command(ctrl
, "SCAN_RESULTS");
1351 static int wpa_cli_cmd_bss(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1357 printf("Invalid BSS command: need one argument (index or "
1362 res
= os_snprintf(cmd
, sizeof(cmd
), "BSS %s", argv
[0]);
1363 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1365 cmd
[sizeof(cmd
) - 1] = '\0';
1367 return wpa_ctrl_command(ctrl
, cmd
);
1371 static int wpa_cli_cmd_get_capability(struct wpa_ctrl
*ctrl
, int argc
,
1377 if (argc
< 1 || argc
> 2) {
1378 printf("Invalid GET_CAPABILITY command: need either one or "
1383 if ((argc
== 2) && os_strcmp(argv
[1], "strict") != 0) {
1384 printf("Invalid GET_CAPABILITY command: second argument, "
1385 "if any, must be 'strict'\n");
1389 res
= os_snprintf(cmd
, sizeof(cmd
), "GET_CAPABILITY %s%s", argv
[0],
1390 (argc
== 2) ? " strict" : "");
1391 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1393 cmd
[sizeof(cmd
) - 1] = '\0';
1395 return wpa_ctrl_command(ctrl
, cmd
);
1399 static int wpa_cli_list_interfaces(struct wpa_ctrl
*ctrl
)
1401 printf("Available interfaces:\n");
1402 return wpa_ctrl_command(ctrl
, "INTERFACES");
1406 static int wpa_cli_cmd_interface(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1409 wpa_cli_list_interfaces(ctrl
);
1413 wpa_cli_close_connection();
1414 os_free(ctrl_ifname
);
1415 ctrl_ifname
= os_strdup(argv
[0]);
1417 if (wpa_cli_open_connection(ctrl_ifname
, 1)) {
1418 printf("Connected to interface '%s.\n", ctrl_ifname
);
1420 printf("Could not connect to interface '%s' - re-trying\n",
1427 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl
*ctrl
, int argc
,
1430 return wpa_ctrl_command(ctrl
, "RECONFIGURE");
1434 static int wpa_cli_cmd_terminate(struct wpa_ctrl
*ctrl
, int argc
,
1437 return wpa_ctrl_command(ctrl
, "TERMINATE");
1441 static int wpa_cli_cmd_interface_add(struct wpa_ctrl
*ctrl
, int argc
,
1448 printf("Invalid INTERFACE_ADD command: needs at least one "
1449 "argument (interface name)\n"
1450 "All arguments: ifname confname driver ctrl_interface "
1451 "driver_param bridge_name\n");
1456 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1457 * <driver_param>TAB<bridge_name>
1459 res
= os_snprintf(cmd
, sizeof(cmd
),
1460 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
1462 argc
> 1 ? argv
[1] : "", argc
> 2 ? argv
[2] : "",
1463 argc
> 3 ? argv
[3] : "", argc
> 4 ? argv
[4] : "",
1464 argc
> 5 ? argv
[5] : "");
1465 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1467 cmd
[sizeof(cmd
) - 1] = '\0';
1468 return wpa_ctrl_command(ctrl
, cmd
);
1472 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl
*ctrl
, int argc
,
1479 printf("Invalid INTERFACE_REMOVE command: needs one argument "
1480 "(interface name)\n");
1484 res
= os_snprintf(cmd
, sizeof(cmd
), "INTERFACE_REMOVE %s", argv
[0]);
1485 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1487 cmd
[sizeof(cmd
) - 1] = '\0';
1488 return wpa_ctrl_command(ctrl
, cmd
);
1492 static int wpa_cli_cmd_interface_list(struct wpa_ctrl
*ctrl
, int argc
,
1495 return wpa_ctrl_command(ctrl
, "INTERFACE_LIST");
1500 static int wpa_cli_cmd_sta(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1504 printf("Invalid 'sta' command - exactly one argument, STA "
1505 "address, is required.\n");
1508 os_snprintf(buf
, sizeof(buf
), "STA %s", argv
[0]);
1509 return wpa_ctrl_command(ctrl
, buf
);
1513 static int wpa_ctrl_command_sta(struct wpa_ctrl
*ctrl
, char *cmd
,
1514 char *addr
, size_t addr_len
)
1516 char buf
[4096], *pos
;
1520 if (ctrl_conn
== NULL
) {
1521 printf("Not connected to hostapd - command dropped.\n");
1524 len
= sizeof(buf
) - 1;
1525 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
1528 printf("'%s' command timed out.\n", cmd
);
1530 } else if (ret
< 0) {
1531 printf("'%s' command failed.\n", cmd
);
1536 if (memcmp(buf
, "FAIL", 4) == 0)
1541 while (*pos
!= '\0' && *pos
!= '\n')
1544 os_strlcpy(addr
, buf
, addr_len
);
1549 static int wpa_cli_cmd_all_sta(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1551 char addr
[32], cmd
[64];
1553 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
)))
1556 os_snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
1557 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
)) == 0);
1561 #endif /* CONFIG_AP */
1564 static int wpa_cli_cmd_suspend(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1566 return wpa_ctrl_command(ctrl
, "SUSPEND");
1570 static int wpa_cli_cmd_resume(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1572 return wpa_ctrl_command(ctrl
, "RESUME");
1576 static int wpa_cli_cmd_drop_sa(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1578 return wpa_ctrl_command(ctrl
, "DROP_SA");
1582 static int wpa_cli_cmd_roam(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1588 printf("Invalid ROAM command: needs one argument "
1589 "(target AP's BSSID)\n");
1593 res
= os_snprintf(cmd
, sizeof(cmd
), "ROAM %s", argv
[0]);
1594 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
1595 printf("Too long ROAM command.\n");
1598 return wpa_ctrl_command(ctrl
, cmd
);
1604 static int wpa_cli_cmd_p2p_find(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1610 return wpa_ctrl_command(ctrl
, "P2P_FIND");
1613 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_FIND %s %s",
1616 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_FIND %s", argv
[0]);
1617 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1619 cmd
[sizeof(cmd
) - 1] = '\0';
1620 return wpa_ctrl_command(ctrl
, cmd
);
1624 static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl
*ctrl
, int argc
,
1627 return wpa_ctrl_command(ctrl
, "P2P_STOP_FIND");
1631 static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl
*ctrl
, int argc
,
1638 printf("Invalid P2P_CONNECT command: needs at least two "
1639 "arguments (address and pbc/PIN)\n");
1644 res
= os_snprintf(cmd
, sizeof(cmd
),
1645 "P2P_CONNECT %s %s %s %s %s",
1646 argv
[0], argv
[1], argv
[2], argv
[3],
1649 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_CONNECT %s %s %s %s",
1650 argv
[0], argv
[1], argv
[2], argv
[3]);
1652 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_CONNECT %s %s %s",
1653 argv
[0], argv
[1], argv
[2]);
1655 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_CONNECT %s %s",
1657 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1659 cmd
[sizeof(cmd
) - 1] = '\0';
1660 return wpa_ctrl_command(ctrl
, cmd
);
1664 static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl
*ctrl
, int argc
,
1671 return wpa_ctrl_command(ctrl
, "P2P_LISTEN");
1673 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_LISTEN %s", argv
[0]);
1674 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1676 cmd
[sizeof(cmd
) - 1] = '\0';
1677 return wpa_ctrl_command(ctrl
, cmd
);
1681 static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl
*ctrl
, int argc
,
1688 printf("Invalid P2P_GROUP_REMOVE command: needs one argument "
1689 "(interface name)\n");
1693 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_GROUP_REMOVE %s", argv
[0]);
1694 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1696 cmd
[sizeof(cmd
) - 1] = '\0';
1697 return wpa_ctrl_command(ctrl
, cmd
);
1701 static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl
*ctrl
, int argc
,
1708 return wpa_ctrl_command(ctrl
, "P2P_GROUP_ADD");
1710 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_GROUP_ADD %s", argv
[0]);
1711 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1713 cmd
[sizeof(cmd
) - 1] = '\0';
1714 return wpa_ctrl_command(ctrl
, cmd
);
1718 static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl
*ctrl
, int argc
,
1725 printf("Invalid P2P_PROV_DISC command: needs two arguments "
1726 "(address and config method\n"
1727 "(display, keypad, or pbc)\n");
1731 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_PROV_DISC %s %s",
1733 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1735 cmd
[sizeof(cmd
) - 1] = '\0';
1736 return wpa_ctrl_command(ctrl
, cmd
);
1740 static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl
*ctrl
, int argc
,
1743 return wpa_ctrl_command(ctrl
, "P2P_GET_PASSPHRASE");
1747 static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl
*ctrl
, int argc
,
1753 if (argc
!= 2 && argc
!= 4) {
1754 printf("Invalid P2P_SERV_DISC_REQ command: needs two "
1755 "arguments (address and TLVs) or four arguments "
1756 "(address, \"upnp\", version, search target "
1762 res
= os_snprintf(cmd
, sizeof(cmd
),
1763 "P2P_SERV_DISC_REQ %s %s %s %s",
1764 argv
[0], argv
[1], argv
[2], argv
[3]);
1766 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_SERV_DISC_REQ %s %s",
1768 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1770 cmd
[sizeof(cmd
) - 1] = '\0';
1771 return wpa_ctrl_command(ctrl
, cmd
);
1775 static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl
*ctrl
,
1776 int argc
, char *argv
[])
1782 printf("Invalid P2P_SERV_DISC_CANCEL_REQ command: needs one "
1783 "argument (pending request identifier)\n");
1787 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_SERV_DISC_CANCEL_REQ %s",
1789 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1791 cmd
[sizeof(cmd
) - 1] = '\0';
1792 return wpa_ctrl_command(ctrl
, cmd
);
1796 static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl
*ctrl
, int argc
,
1803 printf("Invalid P2P_SERV_DISC_RESP command: needs four "
1804 "arguments (freq, address, dialog token, and TLVs)\n");
1808 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_SERV_DISC_RESP %s %s %s %s",
1809 argv
[0], argv
[1], argv
[2], argv
[3]);
1810 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1812 cmd
[sizeof(cmd
) - 1] = '\0';
1813 return wpa_ctrl_command(ctrl
, cmd
);
1817 static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl
*ctrl
, int argc
,
1820 return wpa_ctrl_command(ctrl
, "P2P_SERVICE_UPDATE");
1824 static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl
*ctrl
,
1825 int argc
, char *argv
[])
1831 printf("Invalid P2P_SERV_DISC_EXTERNAL command: needs one "
1832 "argument (external processing: 0/1)\n");
1836 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_SERV_DISC_EXTERNAL %s",
1838 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1840 cmd
[sizeof(cmd
) - 1] = '\0';
1841 return wpa_ctrl_command(ctrl
, cmd
);
1845 static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl
*ctrl
, int argc
,
1848 return wpa_ctrl_command(ctrl
, "P2P_SERVICE_FLUSH");
1852 static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl
*ctrl
, int argc
,
1858 if (argc
!= 3 && argc
!= 4) {
1859 printf("Invalid P2P_SERVICE_ADD command: needs three or four "
1865 res
= os_snprintf(cmd
, sizeof(cmd
),
1866 "P2P_SERVICE_ADD %s %s %s %s",
1867 argv
[0], argv
[1], argv
[2], argv
[3]);
1869 res
= os_snprintf(cmd
, sizeof(cmd
),
1870 "P2P_SERVICE_ADD %s %s %s",
1871 argv
[0], argv
[1], argv
[2]);
1872 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1874 cmd
[sizeof(cmd
) - 1] = '\0';
1875 return wpa_ctrl_command(ctrl
, cmd
);
1879 static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl
*ctrl
, int argc
,
1885 if (argc
!= 2 && argc
!= 3) {
1886 printf("Invalid P2P_SERVICE_DEL command: needs two or three "
1892 res
= os_snprintf(cmd
, sizeof(cmd
),
1893 "P2P_SERVICE_DEL %s %s %s",
1894 argv
[0], argv
[1], argv
[2]);
1896 res
= os_snprintf(cmd
, sizeof(cmd
),
1897 "P2P_SERVICE_DEL %s %s",
1899 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1901 cmd
[sizeof(cmd
) - 1] = '\0';
1902 return wpa_ctrl_command(ctrl
, cmd
);
1906 static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl
*ctrl
,
1907 int argc
, char *argv
[])
1913 printf("Invalid P2P_REJECT command: needs one argument "
1914 "(peer address)\n");
1918 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_REJECT %s", argv
[0]);
1919 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1921 cmd
[sizeof(cmd
) - 1] = '\0';
1922 return wpa_ctrl_command(ctrl
, cmd
);
1926 static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl
*ctrl
,
1927 int argc
, char *argv
[])
1933 printf("Invalid P2P_INVITE command: needs at least one "
1939 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_INVITE %s %s %s",
1940 argv
[0], argv
[1], argv
[2]);
1942 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_INVITE %s %s",
1945 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_INVITE %s", argv
[0]);
1946 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
1948 cmd
[sizeof(cmd
) - 1] = '\0';
1949 return wpa_ctrl_command(ctrl
, cmd
);
1953 static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
1957 printf("Invalid 'p2p_peer' command - exactly one argument, "
1958 "P2P peer device address, is required.\n");
1961 os_snprintf(buf
, sizeof(buf
), "P2P_PEER %s", argv
[0]);
1962 return wpa_ctrl_command(ctrl
, buf
);
1966 static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl
*ctrl
, char *cmd
,
1967 char *addr
, size_t addr_len
,
1970 char buf
[4096], *pos
;
1974 if (ctrl_conn
== NULL
)
1976 len
= sizeof(buf
) - 1;
1977 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
1980 printf("'%s' command timed out.\n", cmd
);
1982 } else if (ret
< 0) {
1983 printf("'%s' command failed.\n", cmd
);
1988 if (memcmp(buf
, "FAIL", 4) == 0)
1992 while (*pos
!= '\0' && *pos
!= '\n')
1995 os_strlcpy(addr
, buf
, addr_len
);
1996 if (!discovered
|| os_strstr(pos
, "[PROBE_REQ_ONLY]") == NULL
)
1997 printf("%s\n", addr
);
2002 static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
2004 char addr
[32], cmd
[64];
2007 discovered
= argc
> 0 && os_strcmp(argv
[0], "discovered") == 0;
2009 if (wpa_ctrl_command_p2p_peer(ctrl
, "P2P_PEER FIRST",
2010 addr
, sizeof(addr
), discovered
))
2013 os_snprintf(cmd
, sizeof(cmd
), "P2P_PEER NEXT-%s", addr
);
2014 } while (wpa_ctrl_command_p2p_peer(ctrl
, cmd
, addr
, sizeof(addr
),
2021 static int wpa_cli_cmd_p2p_set(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
2027 printf("Invalid P2P_SET command: needs two arguments (field, "
2032 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_SET %s %s", argv
[0], argv
[1]);
2033 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
2035 cmd
[sizeof(cmd
) - 1] = '\0';
2036 return wpa_ctrl_command(ctrl
, cmd
);
2040 static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
2042 return wpa_ctrl_command(ctrl
, "P2P_FLUSH");
2046 static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl
*ctrl
, int argc
,
2049 return wpa_ctrl_command(ctrl
, "P2P_CANCEL");
2053 static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl
*ctrl
, int argc
,
2060 printf("Invalid P2P_UNAUTHORIZE command: needs one argument "
2061 "(peer address)\n");
2065 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_UNAUTHORIZE %s", argv
[0]);
2067 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
2070 cmd
[sizeof(cmd
) - 1] = '\0';
2071 return wpa_ctrl_command(ctrl
, cmd
);
2075 static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl
*ctrl
, int argc
,
2081 if (argc
!= 0 && argc
!= 2 && argc
!= 4) {
2082 printf("Invalid P2P_PRESENCE_REQ command: needs two arguments "
2083 "(preferred duration, interval; in microsecods).\n"
2084 "Optional second pair can be used to provide "
2085 "acceptable values.\n");
2090 res
= os_snprintf(cmd
, sizeof(cmd
),
2091 "P2P_PRESENCE_REQ %s %s %s %s",
2092 argv
[0], argv
[1], argv
[2], argv
[3]);
2094 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_PRESENCE_REQ %s %s",
2097 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_PRESENCE_REQ");
2098 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
2100 cmd
[sizeof(cmd
) - 1] = '\0';
2101 return wpa_ctrl_command(ctrl
, cmd
);
2105 static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl
*ctrl
, int argc
,
2111 if (argc
!= 0 && argc
!= 2) {
2112 printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
2113 "(availability period, availability interval; in "
2115 "Extended Listen Timing can be cancelled with this "
2116 "command when used without parameters.\n");
2121 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_EXT_LISTEN %s %s",
2124 res
= os_snprintf(cmd
, sizeof(cmd
), "P2P_EXT_LISTEN");
2125 if (res
< 0 || (size_t) res
>= sizeof(cmd
))
2127 cmd
[sizeof(cmd
) - 1] = '\0';
2128 return wpa_ctrl_command(ctrl
, cmd
);
2131 #endif /* CONFIG_P2P */
2134 static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl
*ctrl
, int argc
,
2141 printf("Invalid STA_AUTOCONNECT command: needs one argument "
2142 "(0/1 = disable/enable automatic reconnection)\n");
2145 res
= os_snprintf(cmd
, sizeof(cmd
), "STA_AUTOCONNECT %s", argv
[0]);
2146 if (res
< 0 || (size_t) res
>= sizeof(cmd
) - 1) {
2147 printf("Too long STA_AUTOCONNECT command.\n");
2150 return wpa_ctrl_command(ctrl
, cmd
);
2154 enum wpa_cli_cmd_flags
{
2155 cli_cmd_flag_none
= 0x00,
2156 cli_cmd_flag_sensitive
= 0x01
2159 struct wpa_cli_cmd
{
2161 int (*handler
)(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[]);
2162 enum wpa_cli_cmd_flags flags
;
2166 static struct wpa_cli_cmd wpa_cli_commands
[] = {
2167 { "status", wpa_cli_cmd_status
,
2169 "[verbose] = get current WPA/EAPOL/EAP status" },
2170 { "ping", wpa_cli_cmd_ping
,
2172 "= pings wpa_supplicant" },
2173 { "relog", wpa_cli_cmd_relog
,
2175 "= re-open log-file (allow rolling logs)" },
2176 { "note", wpa_cli_cmd_note
,
2178 "<text> = add a note to wpa_supplicant debug log" },
2179 { "mib", wpa_cli_cmd_mib
,
2181 "= get MIB variables (dot1x, dot11)" },
2182 { "help", wpa_cli_cmd_help
,
2184 "= show this usage help" },
2185 { "interface", wpa_cli_cmd_interface
,
2187 "[ifname] = show interfaces/select interface" },
2188 { "level", wpa_cli_cmd_level
,
2190 "<debug level> = change debug level" },
2191 { "license", wpa_cli_cmd_license
,
2193 "= show full wpa_cli license" },
2194 { "quit", wpa_cli_cmd_quit
,
2197 { "set", wpa_cli_cmd_set
,
2199 "= set variables (shows list of variables when run without "
2201 { "get", wpa_cli_cmd_get
,
2203 "<name> = get information" },
2204 { "logon", wpa_cli_cmd_logon
,
2206 "= IEEE 802.1X EAPOL state machine logon" },
2207 { "logoff", wpa_cli_cmd_logoff
,
2209 "= IEEE 802.1X EAPOL state machine logoff" },
2210 { "pmksa", wpa_cli_cmd_pmksa
,
2212 "= show PMKSA cache" },
2213 { "reassociate", wpa_cli_cmd_reassociate
,
2215 "= force reassociation" },
2216 { "preauthenticate", wpa_cli_cmd_preauthenticate
,
2218 "<BSSID> = force preauthentication" },
2219 { "identity", wpa_cli_cmd_identity
,
2221 "<network id> <identity> = configure identity for an SSID" },
2222 { "password", wpa_cli_cmd_password
,
2223 cli_cmd_flag_sensitive
,
2224 "<network id> <password> = configure password for an SSID" },
2225 { "new_password", wpa_cli_cmd_new_password
,
2226 cli_cmd_flag_sensitive
,
2227 "<network id> <password> = change password for an SSID" },
2228 { "pin", wpa_cli_cmd_pin
,
2229 cli_cmd_flag_sensitive
,
2230 "<network id> <pin> = configure pin for an SSID" },
2231 { "otp", wpa_cli_cmd_otp
,
2232 cli_cmd_flag_sensitive
,
2233 "<network id> <password> = configure one-time-password for an SSID"
2235 { "passphrase", wpa_cli_cmd_passphrase
,
2236 cli_cmd_flag_sensitive
,
2237 "<network id> <passphrase> = configure private key passphrase\n"
2239 { "bssid", wpa_cli_cmd_bssid
,
2241 "<network id> <BSSID> = set preferred BSSID for an SSID" },
2242 { "list_networks", wpa_cli_cmd_list_networks
,
2244 "= list configured networks" },
2245 { "select_network", wpa_cli_cmd_select_network
,
2247 "<network id> = select a network (disable others)" },
2248 { "enable_network", wpa_cli_cmd_enable_network
,
2250 "<network id> = enable a network" },
2251 { "disable_network", wpa_cli_cmd_disable_network
,
2253 "<network id> = disable a network" },
2254 { "add_network", wpa_cli_cmd_add_network
,
2256 "= add a network" },
2257 { "remove_network", wpa_cli_cmd_remove_network
,
2259 "<network id> = remove a network" },
2260 { "set_network", wpa_cli_cmd_set_network
,
2261 cli_cmd_flag_sensitive
,
2262 "<network id> <variable> <value> = set network variables (shows\n"
2263 " list of variables when run without arguments)" },
2264 { "get_network", wpa_cli_cmd_get_network
,
2266 "<network id> <variable> = get network variables" },
2267 { "save_config", wpa_cli_cmd_save_config
,
2269 "= save the current configuration" },
2270 { "disconnect", wpa_cli_cmd_disconnect
,
2272 "= disconnect and wait for reassociate/reconnect command before\n"
2274 { "reconnect", wpa_cli_cmd_reconnect
,
2276 "= like reassociate, but only takes effect if already disconnected"
2278 { "scan", wpa_cli_cmd_scan
,
2280 "= request new BSS scan" },
2281 { "scan_results", wpa_cli_cmd_scan_results
,
2283 "= get latest scan results" },
2284 { "bss", wpa_cli_cmd_bss
,
2286 "<<idx> | <bssid>> = get detailed scan result info" },
2287 { "get_capability", wpa_cli_cmd_get_capability
,
2289 "<eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies" },
2290 { "reconfigure", wpa_cli_cmd_reconfigure
,
2292 "= force wpa_supplicant to re-read its configuration file" },
2293 { "terminate", wpa_cli_cmd_terminate
,
2295 "= terminate wpa_supplicant" },
2296 { "interface_add", wpa_cli_cmd_interface_add
,
2298 "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
2299 " <bridge_name> = adds new interface, all parameters but <ifname>\n"
2301 { "interface_remove", wpa_cli_cmd_interface_remove
,
2303 "<ifname> = removes the interface" },
2304 { "interface_list", wpa_cli_cmd_interface_list
,
2306 "= list available interfaces" },
2307 { "ap_scan", wpa_cli_cmd_ap_scan
,
2309 "<value> = set ap_scan parameter" },
2310 { "stkstart", wpa_cli_cmd_stkstart
,
2312 "<addr> = request STK negotiation with <addr>" },
2313 { "ft_ds", wpa_cli_cmd_ft_ds
,
2315 "<addr> = request over-the-DS FT with <addr>" },
2316 { "wps_pbc", wpa_cli_cmd_wps_pbc
,
2318 "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
2319 { "wps_pin", wpa_cli_cmd_wps_pin
,
2320 cli_cmd_flag_sensitive
,
2321 "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
2323 { "wps_check_pin", wpa_cli_cmd_wps_check_pin
,
2324 cli_cmd_flag_sensitive
,
2325 "<PIN> = verify PIN checksum" },
2326 { "wps_cancel", wpa_cli_cmd_wps_cancel
, cli_cmd_flag_none
,
2327 "Cancels the pending WPS operation" },
2328 #ifdef CONFIG_WPS_OOB
2329 { "wps_oob", wpa_cli_cmd_wps_oob
,
2330 cli_cmd_flag_sensitive
,
2331 "<DEV_TYPE> <PATH> <METHOD> [DEV_NAME] = start WPS OOB" },
2332 #endif /* CONFIG_WPS_OOB */
2333 { "wps_reg", wpa_cli_cmd_wps_reg
,
2334 cli_cmd_flag_sensitive
,
2335 "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
2336 { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin
,
2337 cli_cmd_flag_sensitive
,
2338 "[params..] = enable/disable AP PIN" },
2339 { "wps_er_start", wpa_cli_cmd_wps_er_start
,
2341 "[IP address] = start Wi-Fi Protected Setup External Registrar" },
2342 { "wps_er_stop", wpa_cli_cmd_wps_er_stop
,
2344 "= stop Wi-Fi Protected Setup External Registrar" },
2345 { "wps_er_pin", wpa_cli_cmd_wps_er_pin
,
2346 cli_cmd_flag_sensitive
,
2347 "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
2348 { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc
,
2350 "<UUID> = accept an Enrollee PBC using External Registrar" },
2351 { "wps_er_learn", wpa_cli_cmd_wps_er_learn
,
2352 cli_cmd_flag_sensitive
,
2353 "<UUID> <PIN> = learn AP configuration" },
2354 { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config
,
2356 "<UUID> <network id> = set AP configuration for enrolling" },
2357 { "wps_er_config", wpa_cli_cmd_wps_er_config
,
2358 cli_cmd_flag_sensitive
,
2359 "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
2360 { "ibss_rsn", wpa_cli_cmd_ibss_rsn
,
2362 "<addr> = request RSN authentication with <addr> in IBSS" },
2364 { "sta", wpa_cli_cmd_sta
,
2366 "<addr> = get information about an associated station (AP)" },
2367 { "all_sta", wpa_cli_cmd_all_sta
,
2369 "= get information about all associated stations (AP)" },
2370 #endif /* CONFIG_AP */
2371 { "suspend", wpa_cli_cmd_suspend
, cli_cmd_flag_none
,
2372 "= notification of suspend/hibernate" },
2373 { "resume", wpa_cli_cmd_resume
, cli_cmd_flag_none
,
2374 "= notification of resume/thaw" },
2375 { "drop_sa", wpa_cli_cmd_drop_sa
, cli_cmd_flag_none
,
2376 "= drop SA without deauth/disassoc (test command)" },
2377 { "roam", wpa_cli_cmd_roam
,
2379 "<addr> = roam to the specified BSS" },
2381 { "p2p_find", wpa_cli_cmd_p2p_find
, cli_cmd_flag_none
,
2382 "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
2383 { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find
, cli_cmd_flag_none
,
2384 "= stop P2P Devices search" },
2385 { "p2p_connect", wpa_cli_cmd_p2p_connect
, cli_cmd_flag_none
,
2386 "<addr> <\"pbc\"|PIN> = connect to a P2P Devices" },
2387 { "p2p_listen", wpa_cli_cmd_p2p_listen
, cli_cmd_flag_none
,
2388 "[timeout] = listen for P2P Devices for up-to timeout seconds" },
2389 { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove
, cli_cmd_flag_none
,
2390 "<ifname> = remove P2P group interface (terminate group if GO)" },
2391 { "p2p_group_add", wpa_cli_cmd_p2p_group_add
, cli_cmd_flag_none
,
2392 "= add a new P2P group (local end as GO)" },
2393 { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc
, cli_cmd_flag_none
,
2394 "<addr> <method> = request provisioning discovery" },
2395 { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase
,
2397 "= get the passphrase for a group (GO only)" },
2398 { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req
,
2400 "<addr> <TLVs> = schedule service discovery request" },
2401 { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req
,
2403 "<id> = cancel pending service discovery request" },
2404 { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp
,
2406 "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
2407 { "p2p_service_update", wpa_cli_cmd_p2p_service_update
,
2409 "= indicate change in local services" },
2410 { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external
,
2412 "<external> = set external processing of service discovery" },
2413 { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush
,
2415 "= remove all stored service entries" },
2416 { "p2p_service_add", wpa_cli_cmd_p2p_service_add
,
2418 "<bonjour|upnp> <query|version> <response|service> = add a local "
2420 { "p2p_service_del", wpa_cli_cmd_p2p_service_del
,
2422 "<bonjour|upnp> <query|version> [|service] = remove a local "
2424 { "p2p_reject", wpa_cli_cmd_p2p_reject
,
2426 "<addr> = reject connection attempts from a specific peer" },
2427 { "p2p_invite", wpa_cli_cmd_p2p_invite
,
2429 "<cmd> [peer=addr] = invite peer" },
2430 { "p2p_peers", wpa_cli_cmd_p2p_peers
, cli_cmd_flag_none
,
2431 "[discovered] = list known (optionally, only fully discovered) P2P "
2433 { "p2p_peer", wpa_cli_cmd_p2p_peer
, cli_cmd_flag_none
,
2434 "<address> = show information about known P2P peer" },
2435 { "p2p_set", wpa_cli_cmd_p2p_set
, cli_cmd_flag_none
,
2436 "<field> <value> = set a P2P parameter" },
2437 { "p2p_flush", wpa_cli_cmd_p2p_flush
, cli_cmd_flag_none
,
2438 "= flush P2P state" },
2439 { "p2p_cancel", wpa_cli_cmd_p2p_cancel
, cli_cmd_flag_none
,
2440 "= cancel P2P group formation" },
2441 { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize
, cli_cmd_flag_none
,
2442 "<address> = unauthorize a peer" },
2443 { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req
, cli_cmd_flag_none
,
2444 "[<duration> <interval>] [<duration> <interval>] = request GO "
2446 { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen
, cli_cmd_flag_none
,
2447 "[<period> <interval>] = set extended listen timing" },
2448 #endif /* CONFIG_P2P */
2449 { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect
, cli_cmd_flag_none
,
2450 "<0/1> = disable/enable automatic reconnection" },
2451 { NULL
, NULL
, cli_cmd_flag_none
, NULL
}
2456 * Prints command usage, lines are padded with the specified string.
2458 static void print_cmd_help(struct wpa_cli_cmd
*cmd
, const char *pad
)
2463 printf("%s%s ", pad
, cmd
->cmd
);
2464 for (n
= 0; (c
= cmd
->usage
[n
]); n
++) {
2473 static void print_help(void)
2476 printf("commands:\n");
2477 for (n
= 0; wpa_cli_commands
[n
].cmd
; n
++)
2478 print_cmd_help(&wpa_cli_commands
[n
], " ");
2482 static int wpa_cli_edit_filter_history_cb(void *ctx
, const char *cmd
)
2484 const char *c
, *delim
;
2488 delim
= os_strchr(cmd
, ' ');
2492 len
= os_strlen(cmd
);
2494 for (n
= 0; (c
= wpa_cli_commands
[n
].cmd
); n
++) {
2495 if (os_strncasecmp(cmd
, c
, len
) == 0 && len
== os_strlen(c
))
2496 return (wpa_cli_commands
[n
].flags
&
2497 cli_cmd_flag_sensitive
);
2503 static char ** wpa_list_cmd_list(void)
2508 count
= sizeof(wpa_cli_commands
) / sizeof(wpa_cli_commands
[0]);
2509 res
= os_zalloc(count
* sizeof(char *));
2513 for (i
= 0; wpa_cli_commands
[i
].cmd
; i
++) {
2514 res
[i
] = os_strdup(wpa_cli_commands
[i
].cmd
);
2523 static char ** wpa_cli_cmd_completion(const char *cmd
, const char *str
,
2528 for (i
= 0; wpa_cli_commands
[i
].cmd
; i
++) {
2529 if (os_strcasecmp(wpa_cli_commands
[i
].cmd
, cmd
) == 0) {
2531 printf("\r%s\n", wpa_cli_commands
[i
].usage
);
2541 static char ** wpa_cli_edit_completion_cb(void *ctx
, const char *str
, int pos
)
2547 end
= os_strchr(str
, ' ');
2548 if (end
== NULL
|| str
+ pos
< end
)
2549 return wpa_list_cmd_list();
2551 cmd
= os_malloc(pos
+ 1);
2554 os_memcpy(cmd
, str
, pos
);
2555 cmd
[end
- str
] = '\0';
2556 res
= wpa_cli_cmd_completion(cmd
, str
, pos
);
2562 static int wpa_request(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
2564 struct wpa_cli_cmd
*cmd
, *match
= NULL
;
2569 cmd
= wpa_cli_commands
;
2571 if (os_strncasecmp(cmd
->cmd
, argv
[0], os_strlen(argv
[0])) == 0)
2574 if (os_strcasecmp(cmd
->cmd
, argv
[0]) == 0) {
2575 /* we have an exact match */
2585 printf("Ambiguous command '%s'; possible commands:", argv
[0]);
2586 cmd
= wpa_cli_commands
;
2588 if (os_strncasecmp(cmd
->cmd
, argv
[0],
2589 os_strlen(argv
[0])) == 0) {
2590 printf(" %s", cmd
->cmd
);
2596 } else if (count
== 0) {
2597 printf("Unknown command '%s'\n", argv
[0]);
2600 ret
= match
->handler(ctrl
, argc
- 1, &argv
[1]);
2607 static int str_match(const char *a
, const char *b
)
2609 return os_strncmp(a
, b
, os_strlen(b
)) == 0;
2613 static int wpa_cli_exec(const char *program
, const char *arg1
,
2621 len
= os_strlen(program
) + os_strlen(arg1
) + os_strlen(arg2
) + 3;
2622 cmd
= os_malloc(len
);
2625 res
= os_snprintf(cmd
, len
, "%s %s %s", program
, arg1
, arg2
);
2626 if (res
< 0 || (size_t) res
>= len
) {
2630 cmd
[len
- 1] = '\0';
2632 if (system(cmd
) < 0)
2634 #endif /* _WIN32_WCE */
2641 static void wpa_cli_action_process(const char *msg
)
2644 char *copy
= NULL
, *id
, *pos2
;
2649 pos
= os_strchr(pos
, '>');
2656 if (str_match(pos
, WPA_EVENT_CONNECTED
)) {
2658 os_unsetenv("WPA_ID");
2659 os_unsetenv("WPA_ID_STR");
2660 os_unsetenv("WPA_CTRL_DIR");
2662 pos
= os_strstr(pos
, "[id=");
2664 copy
= os_strdup(pos
+ 4);
2668 while (*pos2
&& *pos2
!= ' ')
2672 os_setenv("WPA_ID", id
, 1);
2673 while (*pos2
&& *pos2
!= '=')
2678 while (*pos2
&& *pos2
!= ']')
2681 os_setenv("WPA_ID_STR", id
, 1);
2685 os_setenv("WPA_CTRL_DIR", ctrl_iface_dir
, 1);
2687 if (!wpa_cli_connected
|| new_id
!= wpa_cli_last_id
) {
2688 wpa_cli_connected
= 1;
2689 wpa_cli_last_id
= new_id
;
2690 wpa_cli_exec(action_file
, ctrl_ifname
, "CONNECTED");
2692 } else if (str_match(pos
, WPA_EVENT_DISCONNECTED
)) {
2693 if (wpa_cli_connected
) {
2694 wpa_cli_connected
= 0;
2695 wpa_cli_exec(action_file
, ctrl_ifname
, "DISCONNECTED");
2697 } else if (str_match(pos
, P2P_EVENT_GROUP_STARTED
)) {
2698 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2699 } else if (str_match(pos
, P2P_EVENT_GROUP_REMOVED
)) {
2700 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2701 } else if (str_match(pos
, P2P_EVENT_CROSS_CONNECT_ENABLE
)) {
2702 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2703 } else if (str_match(pos
, P2P_EVENT_CROSS_CONNECT_DISABLE
)) {
2704 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2705 } else if (str_match(pos
, WPS_EVENT_SUCCESS
)) {
2706 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2707 } else if (str_match(pos
, WPS_EVENT_FAIL
)) {
2708 wpa_cli_exec(action_file
, ctrl_ifname
, pos
);
2709 } else if (str_match(pos
, WPA_EVENT_TERMINATING
)) {
2710 printf("wpa_supplicant is terminating - stop monitoring\n");
2716 #ifndef CONFIG_ANSI_C_EXTRA
2717 static void wpa_cli_action_cb(char *msg
, size_t len
)
2719 wpa_cli_action_process(msg
);
2721 #endif /* CONFIG_ANSI_C_EXTRA */
2724 static void wpa_cli_reconnect(void)
2726 wpa_cli_close_connection();
2727 wpa_cli_open_connection(ctrl_ifname
, 1);
2731 static void wpa_cli_recv_pending(struct wpa_ctrl
*ctrl
, int action_monitor
)
2733 if (ctrl_conn
== NULL
) {
2734 wpa_cli_reconnect();
2737 while (wpa_ctrl_pending(ctrl
) > 0) {
2739 size_t len
= sizeof(buf
) - 1;
2740 if (wpa_ctrl_recv(ctrl
, buf
, &len
) == 0) {
2743 wpa_cli_action_process(buf
);
2745 if (wpa_cli_show_event(buf
)) {
2747 printf("\r%s\n", buf
);
2752 printf("Could not read pending message.\n");
2757 if (wpa_ctrl_pending(ctrl
) < 0) {
2758 printf("Connection to wpa_supplicant lost - trying to "
2760 wpa_cli_reconnect();
2766 static int tokenize_cmd(char *cmd
, char *argv
[])
2779 if (argc
== max_args
)
2782 char *pos2
= os_strrchr(pos
, '"');
2786 while (*pos
!= '\0' && *pos
!= ' ')
2796 static void wpa_cli_ping(void *eloop_ctx
, void *timeout_ctx
)
2798 if (ctrl_conn
&& _wpa_ctrl_command(ctrl_conn
, "PING", 0)) {
2799 printf("Connection to wpa_supplicant lost - trying to "
2801 wpa_cli_close_connection();
2804 wpa_cli_reconnect();
2805 eloop_register_timeout(ping_interval
, 0, wpa_cli_ping
, NULL
, NULL
);
2809 static void wpa_cli_eloop_terminate(int sig
, void *signal_ctx
)
2815 static void wpa_cli_mon_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
2817 wpa_cli_recv_pending(mon_conn
, 0);
2821 static void wpa_cli_edit_cmd_cb(void *ctx
, char *cmd
)
2823 char *argv
[max_args
];
2825 argc
= tokenize_cmd(cmd
, argv
);
2827 wpa_request(ctrl_conn
, argc
, argv
);
2831 static void wpa_cli_edit_eof_cb(void *ctx
)
2837 static void wpa_cli_interactive(void)
2839 char *home
, *hfile
= NULL
;
2841 printf("\nInteractive mode\n\n");
2843 home
= getenv("HOME");
2845 const char *fname
= ".wpa_cli_history";
2846 int hfile_len
= os_strlen(home
) + 1 + os_strlen(fname
) + 1;
2847 hfile
= os_malloc(hfile_len
);
2849 os_snprintf(hfile
, hfile_len
, "%s/%s", home
, fname
);
2852 eloop_register_signal_terminate(wpa_cli_eloop_terminate
, NULL
);
2853 edit_init(wpa_cli_edit_cmd_cb
, wpa_cli_edit_eof_cb
,
2854 wpa_cli_edit_completion_cb
, NULL
, hfile
);
2855 eloop_register_timeout(ping_interval
, 0, wpa_cli_ping
, NULL
, NULL
);
2859 edit_deinit(hfile
, wpa_cli_edit_filter_history_cb
);
2861 eloop_cancel_timeout(wpa_cli_ping
, NULL
, NULL
);
2862 wpa_cli_close_connection();
2866 static void wpa_cli_action(struct wpa_ctrl
*ctrl
)
2868 #ifdef CONFIG_ANSI_C_EXTRA
2869 /* TODO: ANSI C version(?) */
2870 printf("Action processing not supported in ANSI C build.\n");
2871 #else /* CONFIG_ANSI_C_EXTRA */
2875 char buf
[256]; /* note: large enough to fit in unsolicited messages */
2878 fd
= wpa_ctrl_get_fd(ctrl
);
2880 while (!wpa_cli_quit
) {
2883 tv
.tv_sec
= ping_interval
;
2885 res
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
2886 if (res
< 0 && errno
!= EINTR
) {
2891 if (FD_ISSET(fd
, &rfds
))
2892 wpa_cli_recv_pending(ctrl
, 1);
2894 /* verify that connection is still working */
2895 len
= sizeof(buf
) - 1;
2896 if (wpa_ctrl_request(ctrl
, "PING", 4, buf
, &len
,
2897 wpa_cli_action_cb
) < 0 ||
2898 len
< 4 || os_memcmp(buf
, "PONG", 4) != 0) {
2899 printf("wpa_supplicant did not reply to PING "
2900 "command - exiting\n");
2905 #endif /* CONFIG_ANSI_C_EXTRA */
2909 static void wpa_cli_cleanup(void)
2911 wpa_cli_close_connection();
2913 os_daemonize_terminate(pid_file
);
2915 os_program_deinit();
2918 static void wpa_cli_terminate(int sig
)
2925 static char * wpa_cli_get_default_ifname(void)
2927 char *ifname
= NULL
;
2929 #ifdef CONFIG_CTRL_IFACE_UNIX
2930 struct dirent
*dent
;
2931 DIR *dir
= opendir(ctrl_iface_dir
);
2934 while ((dent
= readdir(dir
))) {
2935 #ifdef _DIRENT_HAVE_D_TYPE
2937 * Skip the file if it is not a socket. Also accept
2938 * DT_UNKNOWN (0) in case the C library or underlying
2939 * file system does not support d_type.
2941 if (dent
->d_type
!= DT_SOCK
&& dent
->d_type
!= DT_UNKNOWN
)
2943 #endif /* _DIRENT_HAVE_D_TYPE */
2944 if (os_strcmp(dent
->d_name
, ".") == 0 ||
2945 os_strcmp(dent
->d_name
, "..") == 0)
2947 printf("Selected interface '%s'\n", dent
->d_name
);
2948 ifname
= os_strdup(dent
->d_name
);
2952 #endif /* CONFIG_CTRL_IFACE_UNIX */
2954 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
2955 char buf
[2048], *pos
;
2957 struct wpa_ctrl
*ctrl
;
2960 ctrl
= wpa_ctrl_open(NULL
);
2964 len
= sizeof(buf
) - 1;
2965 ret
= wpa_ctrl_request(ctrl
, "INTERFACES", 10, buf
, &len
, NULL
);
2968 pos
= os_strchr(buf
, '\n');
2971 ifname
= os_strdup(buf
);
2973 wpa_ctrl_close(ctrl
);
2974 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
2980 int main(int argc
, char *argv
[])
2982 int warning_displayed
= 0;
2986 const char *global
= NULL
;
2988 if (os_program_init())
2992 c
= getopt(argc
, argv
, "a:Bg:G:hi:p:P:v");
2997 action_file
= optarg
;
3006 ping_interval
= atoi(optarg
);
3012 printf("%s\n", wpa_cli_version
);
3015 os_free(ctrl_ifname
);
3016 ctrl_ifname
= os_strdup(optarg
);
3019 ctrl_iface_dir
= optarg
;
3030 interactive
= (argc
== optind
) && (action_file
== NULL
);
3033 printf("%s\n\n%s\n\n", wpa_cli_version
, wpa_cli_license
);
3039 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
3040 ctrl_conn
= wpa_ctrl_open(NULL
);
3041 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3042 ctrl_conn
= wpa_ctrl_open(global
);
3043 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3044 if (ctrl_conn
== NULL
) {
3045 perror("Failed to connect to wpa_supplicant - "
3052 signal(SIGINT
, wpa_cli_terminate
);
3053 signal(SIGTERM
, wpa_cli_terminate
);
3054 #endif /* _WIN32_WCE */
3056 if (ctrl_ifname
== NULL
)
3057 ctrl_ifname
= wpa_cli_get_default_ifname();
3061 if (wpa_cli_open_connection(ctrl_ifname
, 1) == 0) {
3062 if (warning_displayed
)
3063 printf("Connection established.\n");
3067 if (!warning_displayed
) {
3068 printf("Could not connect to wpa_supplicant - "
3070 warning_displayed
= 1;
3077 wpa_cli_open_connection(ctrl_ifname
, 0) < 0) {
3078 perror("Failed to connect to wpa_supplicant - "
3084 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
3085 wpa_cli_attached
= 1;
3087 printf("Warning: Failed to attach to "
3088 "wpa_supplicant.\n");
3094 if (daemonize
&& os_daemonize(pid_file
))
3098 wpa_cli_interactive();
3099 else if (action_file
)
3100 wpa_cli_action(ctrl_conn
);
3102 ret
= wpa_request(ctrl_conn
, argc
- optind
, &argv
[optind
]);
3104 os_free(ctrl_ifname
);
3111 #else /* CONFIG_CTRL_IFACE */
3112 int main(int argc
, char *argv
[])
3114 printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
3117 #endif /* CONFIG_CTRL_IFACE */