2 * WPA Supplicant - command line interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifdef CONFIG_CTRL_IFACE
13 #ifdef CONFIG_CTRL_IFACE_UNIX
15 #endif /* CONFIG_CTRL_IFACE_UNIX */
17 #include "common/wpa_ctrl.h"
18 #include "utils/common.h"
19 #include "utils/eloop.h"
20 #include "utils/edit.h"
21 #include "utils/list.h"
22 #include "common/version.h"
23 #include "common/ieee802_11_defs.h"
25 #include <cutils/properties.h>
29 static const char *wpa_cli_version =
30 "wpa_cli v" VERSION_STR "\n"
31 "Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi> and contributors";
34 static const char *wpa_cli_license =
35 "This software may be distributed under the terms of the BSD license.\n"
36 "See README for more details.\n";
38 static const char *wpa_cli_full_license =
39 "This software may be distributed under the terms of the BSD license.\n"
41 "Redistribution and use in source and binary forms, with or without\n"
42 "modification, are permitted provided that the following conditions are\n"
45 "1. Redistributions of source code must retain the above copyright\n"
46 " notice, this list of conditions and the following disclaimer.\n"
48 "2. Redistributions in binary form must reproduce the above copyright\n"
49 " notice, this list of conditions and the following disclaimer in the\n"
50 " documentation and/or other materials provided with the distribution.\n"
52 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
53 " names of its contributors may be used to endorse or promote products\n"
54 " derived from this software without specific prior written permission.\n"
56 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
57 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
58 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
59 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
60 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
61 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
62 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
63 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
64 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
65 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
66 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
69 static struct wpa_ctrl *ctrl_conn;
70 static struct wpa_ctrl *mon_conn;
71 static int wpa_cli_quit = 0;
72 static int wpa_cli_attached = 0;
73 static int wpa_cli_connected = -1;
74 static int wpa_cli_last_id = 0;
75 #ifndef CONFIG_CTRL_IFACE_DIR
76 #define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
77 #endif /* CONFIG_CTRL_IFACE_DIR */
78 static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
79 static char *ctrl_ifname = NULL;
80 static const char *pid_file = NULL;
81 static const char *action_file = NULL;
82 static int ping_interval = 5;
83 static int interactive = 0;
84 static char *ifname_prefix = NULL;
86 struct cli_txt_entry {
91 static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
92 static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */
93 static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */
94 static DEFINE_DL_LIST(ifnames); /* struct cli_txt_entry */
97 static void print_help(const char *cmd);
98 static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx);
99 static void wpa_cli_close_connection(void);
100 static char * wpa_cli_get_default_ifname(void);
101 static char ** wpa_list_cmd_list(void);
104 static void usage(void)
106 printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
107 "[-a<action file>] \\\n"
108 " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
110 " -h = help (show this usage text)\n"
111 " -v = shown version information\n"
112 " -a = run in daemon mode executing the action file based on "
115 " -B = run a daemon in the background\n"
116 " default path: " CONFIG_CTRL_IFACE_DIR "\n"
117 " default interface: first interface found in socket path\n");
122 static void cli_txt_list_free(struct cli_txt_entry *e)
124 dl_list_del(&e->list);
130 static void cli_txt_list_flush(struct dl_list *list)
132 struct cli_txt_entry *e;
133 while ((e = dl_list_first(list, struct cli_txt_entry, list)))
134 cli_txt_list_free(e);
138 static struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
141 struct cli_txt_entry *e;
142 dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
143 if (os_strcmp(e->txt, txt) == 0)
150 static void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
152 struct cli_txt_entry *e;
153 e = cli_txt_list_get(txt_list, txt);
155 cli_txt_list_free(e);
159 static void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
163 if (hwaddr_aton(txt, addr) < 0)
165 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
166 cli_txt_list_del(txt_list, buf);
171 static void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt)
175 end = os_strchr(txt, ' ');
177 end = txt + os_strlen(txt);
178 buf = dup_binstr(txt, end - txt);
181 cli_txt_list_del(txt_list, buf);
184 #endif /* CONFIG_P2P */
187 static int cli_txt_list_add(struct dl_list *txt_list, const char *txt)
189 struct cli_txt_entry *e;
190 e = cli_txt_list_get(txt_list, txt);
193 e = os_zalloc(sizeof(*e));
196 e->txt = os_strdup(txt);
197 if (e->txt == NULL) {
201 dl_list_add(txt_list, &e->list);
207 static int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt)
211 if (hwaddr_aton(txt, addr) < 0)
213 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
214 return cli_txt_list_add(txt_list, buf);
218 static int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt)
223 end = os_strchr(txt, ' ');
225 end = txt + os_strlen(txt);
226 buf = dup_binstr(txt, end - txt);
229 ret = cli_txt_list_add(txt_list, buf);
233 #endif /* CONFIG_P2P */
236 static char ** cli_txt_list_array(struct dl_list *txt_list)
238 unsigned int i, count = dl_list_len(txt_list);
240 struct cli_txt_entry *e;
242 res = os_calloc(count + 1, sizeof(char *));
247 dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
248 res[i] = os_strdup(e->txt);
258 static int get_cmd_arg_num(const char *str, int pos)
262 for (i = 0; i <= pos; i++) {
265 while (i <= pos && str[i] != ' ')
276 static int str_starts(const char *src, const char *match)
278 return os_strncmp(src, match, os_strlen(match)) == 0;
282 static int wpa_cli_show_event(const char *event)
286 start = os_strchr(event, '>');
292 * Skip BSS added/removed events since they can be relatively frequent
293 * and are likely of not much use for an interactive user.
295 if (str_starts(start, WPA_EVENT_BSS_ADDED) ||
296 str_starts(start, WPA_EVENT_BSS_REMOVED))
303 static int wpa_cli_open_connection(const char *ifname, int attach)
305 #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
306 ctrl_conn = wpa_ctrl_open(ifname);
307 if (ctrl_conn == NULL)
310 if (attach && interactive)
311 mon_conn = wpa_ctrl_open(ifname);
314 #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
322 if (access(ctrl_iface_dir, F_OK) < 0) {
323 cfile = os_strdup(ifname);
330 flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
331 cfile = os_malloc(flen);
334 res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir,
336 if (res < 0 || res >= flen) {
342 ctrl_conn = wpa_ctrl_open(cfile);
343 if (ctrl_conn == NULL) {
348 if (attach && interactive)
349 mon_conn = wpa_ctrl_open(cfile);
353 #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
356 if (wpa_ctrl_attach(mon_conn) == 0) {
357 wpa_cli_attached = 1;
359 eloop_register_read_sock(
360 wpa_ctrl_get_fd(mon_conn),
361 wpa_cli_mon_receive, NULL, NULL);
363 printf("Warning: Failed to attach to "
364 "wpa_supplicant.\n");
365 wpa_cli_close_connection();
374 static void wpa_cli_close_connection(void)
376 if (ctrl_conn == NULL)
379 if (wpa_cli_attached) {
380 wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn);
381 wpa_cli_attached = 0;
383 wpa_ctrl_close(ctrl_conn);
386 eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn));
387 wpa_ctrl_close(mon_conn);
393 static void wpa_cli_msg_cb(char *msg, size_t len)
399 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
405 if (ctrl_conn == NULL) {
406 printf("Not connected to wpa_supplicant - command dropped.\n");
410 os_snprintf(buf, sizeof(buf), "IFNAME=%s %s",
412 buf[sizeof(buf) - 1] = '\0';
415 len = sizeof(buf) - 1;
416 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
419 printf("'%s' command timed out.\n", cmd);
421 } else if (ret < 0) {
422 printf("'%s' command failed.\n", cmd);
428 if (interactive && len > 0 && buf[len - 1] != '\n')
435 static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
437 return _wpa_ctrl_command(ctrl, cmd, 1);
441 static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc,
450 res = os_snprintf(pos, end - pos, "%s", cmd);
451 if (res < 0 || res >= end - pos)
455 for (i = 0; i < argc; i++) {
456 res = os_snprintf(pos, end - pos, " %s", argv[i]);
457 if (res < 0 || res >= end - pos)
462 buf[buflen - 1] = '\0';
466 printf("Too long command\n");
471 static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
472 int argc, char *argv[])
475 if (argc < min_args) {
476 printf("Invalid %s command - at least %d argument%s "
477 "required.\n", cmd, min_args,
478 min_args > 1 ? "s are" : " is");
481 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
483 return wpa_ctrl_command(ctrl, buf);
487 static int wpa_cli_cmd_ifname(struct wpa_ctrl *ctrl, int argc, char *argv[])
489 return wpa_ctrl_command(ctrl, "IFNAME");
493 static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
495 if (argc > 0 && os_strcmp(argv[0], "verbose") == 0)
496 return wpa_ctrl_command(ctrl, "STATUS-VERBOSE");
497 if (argc > 0 && os_strcmp(argv[0], "wps") == 0)
498 return wpa_ctrl_command(ctrl, "STATUS-WPS");
499 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
500 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
501 return wpa_ctrl_command(ctrl, "STATUS");
505 static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
507 return wpa_ctrl_command(ctrl, "PING");
511 static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
513 return wpa_ctrl_command(ctrl, "RELOG");
517 static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[])
519 return wpa_cli_cmd(ctrl, "NOTE", 1, argc, argv);
523 static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
525 return wpa_ctrl_command(ctrl, "MIB");
529 static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
531 return wpa_ctrl_command(ctrl, "PMKSA");
535 static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
537 print_help(argc > 0 ? argv[0] : NULL);
542 static char ** wpa_cli_complete_help(const char *str, int pos)
544 int arg = get_cmd_arg_num(str, pos);
549 res = wpa_list_cmd_list();
557 static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
559 printf("%s\n\n%s\n", wpa_cli_version, wpa_cli_full_license);
564 static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
573 static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
579 res = os_snprintf(cmd, sizeof(cmd), "SET %s ", argv[0]);
580 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
581 printf("Too long SET command.\n");
584 return wpa_ctrl_command(ctrl, cmd);
587 return wpa_cli_cmd(ctrl, "SET", 2, argc, argv);
591 static char ** wpa_cli_complete_set(const char *str, int pos)
593 int arg = get_cmd_arg_num(str, pos);
594 const char *fields[] = {
596 "EAPOL::heldPeriod", "EAPOL::authPeriod", "EAPOL::startPeriod",
597 "EAPOL::maxStart", "dot11RSNAConfigPMKLifetime",
598 "dot11RSNAConfigPMKReauthThreshold", "dot11RSNAConfigSATimeout",
599 "wps_fragment_size", "wps_version_number", "ampdu",
600 "tdls_testing", "tdls_disabled", "pno", "radio_disabled",
601 "uapsd", "ps", "wifi_display", "bssid_filter", "disallow_aps",
603 /* global configuration parameters */
604 "eapol_version", "ap_scan", "disable_scan_offload",
605 "fast_reauth", "opensc_engine_path", "pkcs11_engine_path",
606 "pkcs11_module_path", "pcsc_reader", "pcsc_pin",
607 "driver_param", "dot11RSNAConfigPMKLifetime",
608 "dot11RSNAConfigPMKReauthThreshold",
609 "dot11RSNAConfigSATimeout",
610 "update_config", "load_dynamic_eap", "uuid", "device_name",
611 "manufacturer", "model_name", "model_number", "serial_number",
612 "device_type", "os_version", "config_methods",
613 "wps_cred_processing", "wps_vendor_ext_m1", "sec_device_type",
614 "p2p_listen_reg_class", "p2p_listen_channel",
615 "p2p_oper_reg_class", "p2p_oper_channel",
616 "p2p_go_intent", "p2p_ssid_postfix", "persistent_reconnect",
617 "p2p_intra_bss", "p2p_group_idle", "p2p_pref_chan",
619 "p2p_go_ht40", "p2p_disabled", "p2p_no_group_iface",
621 "p2p_ignore_shared_freq", "country", "bss_max_count",
622 "bss_expiration_age", "bss_expiration_scan_count",
623 "filter_ssids", "filter_rssi", "max_num_sta",
624 "disassoc_low_ack", "hs20", "interworking", "hessid",
625 "access_network_type", "pbc_in_m1", "autoscan",
626 "wps_nfc_dev_pw_id", "wps_nfc_dh_pubkey", "wps_nfc_dh_privkey",
627 "wps_nfc_dev_pw", "ext_password_backend",
628 "p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
629 "sae_groups", "dtim_period", "beacon_int", "ap_vendor_elements",
630 "ignore_old_scan_res", "freq_list", "external_sim",
631 "tdls_external_control"
633 int i, num_fields = ARRAY_SIZE(fields);
636 char **res = os_calloc(num_fields + 1, sizeof(char *));
639 for (i = 0; i < num_fields; i++) {
640 res[i] = os_strdup(fields[i]);
647 if (arg > 1 && os_strncasecmp(str, "set bssid_filter ", 17) == 0)
648 return cli_txt_list_array(&bsses);
654 static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
656 return wpa_cli_cmd(ctrl, "GET", 1, argc, argv);
660 static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
662 return wpa_ctrl_command(ctrl, "LOGOFF");
666 static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
668 return wpa_ctrl_command(ctrl, "LOGON");
672 static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
675 return wpa_ctrl_command(ctrl, "REASSOCIATE");
679 static int wpa_cli_cmd_reattach(struct wpa_ctrl *ctrl, int argc, char *argv[])
681 return wpa_ctrl_command(ctrl, "REATTACH");
685 static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
688 return wpa_cli_cmd(ctrl, "PREAUTH", 1, argc, argv);
692 static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
694 return wpa_cli_cmd(ctrl, "AP_SCAN", 1, argc, argv);
698 static int wpa_cli_cmd_scan_interval(struct wpa_ctrl *ctrl, int argc,
701 return wpa_cli_cmd(ctrl, "SCAN_INTERVAL", 1, argc, argv);
705 static int wpa_cli_cmd_bss_expire_age(struct wpa_ctrl *ctrl, int argc,
708 return wpa_cli_cmd(ctrl, "BSS_EXPIRE_AGE", 1, argc, argv);
712 static int wpa_cli_cmd_bss_expire_count(struct wpa_ctrl *ctrl, int argc,
715 return wpa_cli_cmd(ctrl, "BSS_EXPIRE_COUNT", 1, argc, argv);
719 static int wpa_cli_cmd_bss_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
725 res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH 0");
727 res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH %s", argv[0]);
728 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
729 printf("Too long BSS_FLUSH command.\n");
732 return wpa_ctrl_command(ctrl, cmd);
736 static int wpa_cli_cmd_stkstart(struct wpa_ctrl *ctrl, int argc,
739 return wpa_cli_cmd(ctrl, "STKSTART", 1, argc, argv);
743 static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
745 return wpa_cli_cmd(ctrl, "FT_DS", 1, argc, argv);
749 static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
751 return wpa_cli_cmd(ctrl, "WPS_PBC", 0, argc, argv);
755 static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
758 printf("Invalid WPS_PIN command: need one or two arguments:\n"
759 "- BSSID: use 'any' to select any\n"
760 "- PIN: optional, used only with devices that have no "
765 return wpa_cli_cmd(ctrl, "WPS_PIN", 1, argc, argv);
769 static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
772 return wpa_cli_cmd(ctrl, "WPS_CHECK_PIN", 1, argc, argv);
776 static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
779 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
783 #ifdef CONFIG_WPS_NFC
785 static int wpa_cli_cmd_wps_nfc(struct wpa_ctrl *ctrl, int argc, char *argv[])
787 return wpa_cli_cmd(ctrl, "WPS_NFC", 0, argc, argv);
791 static int wpa_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
794 return wpa_cli_cmd(ctrl, "WPS_NFC_CONFIG_TOKEN", 1, argc, argv);
798 static int wpa_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, int argc,
801 return wpa_cli_cmd(ctrl, "WPS_NFC_TOKEN", 1, argc, argv);
805 static int wpa_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
813 printf("Invalid 'wps_nfc_tag_read' command - one argument "
818 buflen = 18 + os_strlen(argv[0]);
819 buf = os_malloc(buflen);
822 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
824 ret = wpa_ctrl_command(ctrl, buf);
831 static int wpa_cli_cmd_nfc_get_handover_req(struct wpa_ctrl *ctrl, int argc,
834 return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_REQ", 2, argc, argv);
838 static int wpa_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, int argc,
841 return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_SEL", 2, argc, argv);
845 static int wpa_cli_cmd_nfc_report_handover(struct wpa_ctrl *ctrl, int argc,
848 return wpa_cli_cmd(ctrl, "NFC_REPORT_HANDOVER", 4, argc, argv);
851 #endif /* CONFIG_WPS_NFC */
854 static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
860 res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
862 else if (argc == 5 || argc == 6) {
863 char ssid_hex[2 * 32 + 1];
864 char key_hex[2 * 64 + 1];
868 for (i = 0; i < 32; i++) {
869 if (argv[2][i] == '\0')
871 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
876 for (i = 0; i < 64; i++) {
877 if (argv[5][i] == '\0')
879 os_snprintf(&key_hex[i * 2], 3, "%02x",
884 res = os_snprintf(cmd, sizeof(cmd),
885 "WPS_REG %s %s %s %s %s %s",
886 argv[0], argv[1], ssid_hex, argv[3], argv[4],
889 printf("Invalid WPS_REG command: need two arguments:\n"
890 "- BSSID of the target AP\n"
892 printf("Alternatively, six arguments can be used to "
893 "reconfigure the AP:\n"
894 "- BSSID of the target AP\n"
897 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
898 "- new encr (NONE, WEP, TKIP, CCMP)\n"
903 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
904 printf("Too long WPS_REG command.\n");
907 return wpa_ctrl_command(ctrl, cmd);
911 static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
914 return wpa_cli_cmd(ctrl, "WPS_AP_PIN", 1, argc, argv);
918 static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc,
921 return wpa_cli_cmd(ctrl, "WPS_ER_START", 0, argc, argv);
925 static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc,
928 return wpa_ctrl_command(ctrl, "WPS_ER_STOP");
933 static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc,
937 printf("Invalid WPS_ER_PIN command: need at least two "
939 "- UUID: use 'any' to select any\n"
940 "- PIN: Enrollee PIN\n"
941 "optional: - Enrollee MAC address\n");
945 return wpa_cli_cmd(ctrl, "WPS_ER_PIN", 2, argc, argv);
949 static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc,
952 return wpa_cli_cmd(ctrl, "WPS_ER_PBC", 1, argc, argv);
956 static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc,
960 printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
961 "- UUID: specify which AP to use\n"
966 return wpa_cli_cmd(ctrl, "WPS_ER_LEARN", 2, argc, argv);
970 static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc,
974 printf("Invalid WPS_ER_SET_CONFIG command: need two "
976 "- UUID: specify which AP to use\n"
977 "- Network configuration id\n");
981 return wpa_cli_cmd(ctrl, "WPS_ER_SET_CONFIG", 2, argc, argv);
985 static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
991 if (argc == 5 || argc == 6) {
992 char ssid_hex[2 * 32 + 1];
993 char key_hex[2 * 64 + 1];
997 for (i = 0; i < 32; i++) {
998 if (argv[2][i] == '\0')
1000 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
1005 for (i = 0; i < 64; i++) {
1006 if (argv[5][i] == '\0')
1008 os_snprintf(&key_hex[i * 2], 3, "%02x",
1013 res = os_snprintf(cmd, sizeof(cmd),
1014 "WPS_ER_CONFIG %s %s %s %s %s %s",
1015 argv[0], argv[1], ssid_hex, argv[3], argv[4],
1018 printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
1022 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
1023 "- new encr (NONE, WEP, TKIP, CCMP)\n"
1028 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
1029 printf("Too long WPS_ER_CONFIG command.\n");
1032 return wpa_ctrl_command(ctrl, cmd);
1036 #ifdef CONFIG_WPS_NFC
1037 static int wpa_cli_cmd_wps_er_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
1041 printf("Invalid WPS_ER_NFC_CONFIG_TOKEN command: need two "
1043 "- WPS/NDEF: token format\n"
1044 "- UUID: specify which AP to use\n");
1048 return wpa_cli_cmd(ctrl, "WPS_ER_NFC_CONFIG_TOKEN", 2, argc, argv);
1050 #endif /* CONFIG_WPS_NFC */
1053 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
1055 return wpa_cli_cmd(ctrl, "IBSS_RSN", 1, argc, argv);
1059 static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1061 return wpa_cli_cmd(ctrl, "LEVEL", 1, argc, argv);
1065 static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
1067 char cmd[256], *pos, *end;
1071 printf("Invalid IDENTITY command: needs two arguments "
1072 "(network id and identity)\n");
1076 end = cmd + sizeof(cmd);
1078 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
1080 if (ret < 0 || ret >= end - pos) {
1081 printf("Too long IDENTITY command.\n");
1085 for (i = 2; i < argc; i++) {
1086 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1087 if (ret < 0 || ret >= end - pos) {
1088 printf("Too long IDENTITY command.\n");
1094 return wpa_ctrl_command(ctrl, cmd);
1098 static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
1100 char cmd[256], *pos, *end;
1104 printf("Invalid PASSWORD command: needs two arguments "
1105 "(network id and password)\n");
1109 end = cmd + sizeof(cmd);
1111 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
1113 if (ret < 0 || ret >= end - pos) {
1114 printf("Too long PASSWORD command.\n");
1118 for (i = 2; i < argc; i++) {
1119 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1120 if (ret < 0 || ret >= end - pos) {
1121 printf("Too long PASSWORD command.\n");
1127 return wpa_ctrl_command(ctrl, cmd);
1131 static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
1134 char cmd[256], *pos, *end;
1138 printf("Invalid NEW_PASSWORD command: needs two arguments "
1139 "(network id and password)\n");
1143 end = cmd + sizeof(cmd);
1145 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
1147 if (ret < 0 || ret >= end - pos) {
1148 printf("Too long NEW_PASSWORD command.\n");
1152 for (i = 2; i < argc; i++) {
1153 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1154 if (ret < 0 || ret >= end - pos) {
1155 printf("Too long NEW_PASSWORD command.\n");
1161 return wpa_ctrl_command(ctrl, cmd);
1165 static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
1167 char cmd[256], *pos, *end;
1171 printf("Invalid PIN command: needs two arguments "
1172 "(network id and pin)\n");
1176 end = cmd + sizeof(cmd);
1178 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
1180 if (ret < 0 || ret >= end - pos) {
1181 printf("Too long PIN command.\n");
1185 for (i = 2; i < argc; i++) {
1186 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1187 if (ret < 0 || ret >= end - pos) {
1188 printf("Too long PIN command.\n");
1193 return wpa_ctrl_command(ctrl, cmd);
1197 static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
1199 char cmd[256], *pos, *end;
1203 printf("Invalid OTP command: needs two arguments (network "
1204 "id and password)\n");
1208 end = cmd + sizeof(cmd);
1210 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
1212 if (ret < 0 || ret >= end - pos) {
1213 printf("Too long OTP command.\n");
1217 for (i = 2; i < argc; i++) {
1218 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1219 if (ret < 0 || ret >= end - pos) {
1220 printf("Too long OTP command.\n");
1226 return wpa_ctrl_command(ctrl, cmd);
1230 static int wpa_cli_cmd_sim(struct wpa_ctrl *ctrl, int argc, char *argv[])
1232 char cmd[256], *pos, *end;
1236 printf("Invalid SIM command: needs two arguments "
1237 "(network id and SIM operation response)\n");
1241 end = cmd + sizeof(cmd);
1243 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "SIM-%s:%s",
1245 if (ret < 0 || ret >= end - pos) {
1246 printf("Too long SIM command.\n");
1250 for (i = 2; i < argc; i++) {
1251 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1252 if (ret < 0 || ret >= end - pos) {
1253 printf("Too long SIM command.\n");
1258 return wpa_ctrl_command(ctrl, cmd);
1262 static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
1265 char cmd[256], *pos, *end;
1269 printf("Invalid PASSPHRASE command: needs two arguments "
1270 "(network id and passphrase)\n");
1274 end = cmd + sizeof(cmd);
1276 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
1278 if (ret < 0 || ret >= end - pos) {
1279 printf("Too long PASSPHRASE command.\n");
1283 for (i = 2; i < argc; i++) {
1284 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1285 if (ret < 0 || ret >= end - pos) {
1286 printf("Too long PASSPHRASE command.\n");
1292 return wpa_ctrl_command(ctrl, cmd);
1296 static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
1299 printf("Invalid BSSID command: needs two arguments (network "
1304 return wpa_cli_cmd(ctrl, "BSSID", 2, argc, argv);
1308 static int wpa_cli_cmd_blacklist(struct wpa_ctrl *ctrl, int argc, char *argv[])
1310 return wpa_cli_cmd(ctrl, "BLACKLIST", 0, argc, argv);
1314 static int wpa_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1316 return wpa_cli_cmd(ctrl, "LOG_LEVEL", 0, argc, argv);
1320 static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
1323 return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
1327 static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
1330 return wpa_cli_cmd(ctrl, "SELECT_NETWORK", 1, argc, argv);
1334 static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
1337 return wpa_cli_cmd(ctrl, "ENABLE_NETWORK", 1, argc, argv);
1341 static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
1344 return wpa_cli_cmd(ctrl, "DISABLE_NETWORK", 1, argc, argv);
1348 static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
1351 return wpa_ctrl_command(ctrl, "ADD_NETWORK");
1355 static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
1358 return wpa_cli_cmd(ctrl, "REMOVE_NETWORK", 1, argc, argv);
1362 static void wpa_cli_show_network_variables(void)
1364 printf("set_network variables:\n"
1365 " ssid (network name, SSID)\n"
1366 " psk (WPA passphrase or pre-shared key)\n"
1367 " key_mgmt (key management protocol)\n"
1368 " identity (EAP identity)\n"
1369 " password (EAP password)\n"
1372 "Note: Values are entered in the same format as the "
1373 "configuration file is using,\n"
1374 "i.e., strings values need to be inside double quotation "
1376 "For example: set_network 1 ssid \"network name\"\n"
1378 "Please see wpa_supplicant.conf documentation for full list "
1379 "of\navailable variables.\n");
1383 static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
1387 wpa_cli_show_network_variables();
1392 printf("Invalid SET_NETWORK command: needs three arguments\n"
1393 "(network id, variable name, and value)\n");
1397 return wpa_cli_cmd(ctrl, "SET_NETWORK", 3, argc, argv);
1401 static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
1405 wpa_cli_show_network_variables();
1410 printf("Invalid GET_NETWORK command: needs two arguments\n"
1411 "(network id and variable name)\n");
1415 return wpa_cli_cmd(ctrl, "GET_NETWORK", 2, argc, argv);
1419 static int wpa_cli_cmd_list_creds(struct wpa_ctrl *ctrl, int argc,
1422 return wpa_ctrl_command(ctrl, "LIST_CREDS");
1426 static int wpa_cli_cmd_add_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1428 return wpa_ctrl_command(ctrl, "ADD_CRED");
1432 static int wpa_cli_cmd_remove_cred(struct wpa_ctrl *ctrl, int argc,
1435 return wpa_cli_cmd(ctrl, "REMOVE_CRED", 1, argc, argv);
1439 static int wpa_cli_cmd_set_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1442 printf("Invalid SET_CRED command: needs three arguments\n"
1443 "(cred id, variable name, and value)\n");
1447 return wpa_cli_cmd(ctrl, "SET_CRED", 3, argc, argv);
1451 static int wpa_cli_cmd_get_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1454 printf("Invalid GET_CRED command: needs two arguments\n"
1455 "(cred id, variable name)\n");
1459 return wpa_cli_cmd(ctrl, "GET_CRED", 2, argc, argv);
1463 static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
1466 return wpa_ctrl_command(ctrl, "DISCONNECT");
1470 static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
1473 return wpa_ctrl_command(ctrl, "RECONNECT");
1477 static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
1480 return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
1484 static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
1486 return wpa_cli_cmd(ctrl, "SCAN", 0, argc, argv);
1490 static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
1493 return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
1497 static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
1499 return wpa_cli_cmd(ctrl, "BSS", 1, argc, argv);
1503 static char ** wpa_cli_complete_bss(const char *str, int pos)
1505 int arg = get_cmd_arg_num(str, pos);
1510 res = cli_txt_list_array(&bsses);
1518 static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
1521 if (argc < 1 || argc > 2) {
1522 printf("Invalid GET_CAPABILITY command: need either one or "
1527 if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
1528 printf("Invalid GET_CAPABILITY command: second argument, "
1529 "if any, must be 'strict'\n");
1533 return wpa_cli_cmd(ctrl, "GET_CAPABILITY", 1, argc, argv);
1537 static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
1539 printf("Available interfaces:\n");
1540 return wpa_ctrl_command(ctrl, "INTERFACES");
1544 static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
1547 wpa_cli_list_interfaces(ctrl);
1551 wpa_cli_close_connection();
1552 os_free(ctrl_ifname);
1553 ctrl_ifname = os_strdup(argv[0]);
1555 if (wpa_cli_open_connection(ctrl_ifname, 1)) {
1556 printf("Connected to interface '%s.\n", ctrl_ifname);
1558 printf("Could not connect to interface '%s' - re-trying\n",
1565 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
1568 return wpa_ctrl_command(ctrl, "RECONFIGURE");
1572 static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
1575 return wpa_ctrl_command(ctrl, "TERMINATE");
1579 static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
1586 printf("Invalid INTERFACE_ADD command: needs at least one "
1587 "argument (interface name)\n"
1588 "All arguments: ifname confname driver ctrl_interface "
1589 "driver_param bridge_name\n");
1594 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1595 * <driver_param>TAB<bridge_name>
1597 res = os_snprintf(cmd, sizeof(cmd),
1598 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
1600 argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
1601 argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
1602 argc > 5 ? argv[5] : "");
1603 if (res < 0 || (size_t) res >= sizeof(cmd))
1605 cmd[sizeof(cmd) - 1] = '\0';
1606 return wpa_ctrl_command(ctrl, cmd);
1610 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
1613 return wpa_cli_cmd(ctrl, "INTERFACE_REMOVE", 1, argc, argv);
1617 static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
1620 return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
1625 static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1627 return wpa_cli_cmd(ctrl, "STA", 1, argc, argv);
1631 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
1632 char *addr, size_t addr_len)
1634 char buf[4096], *pos;
1638 if (ctrl_conn == NULL) {
1639 printf("Not connected to hostapd - command dropped.\n");
1642 len = sizeof(buf) - 1;
1643 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
1646 printf("'%s' command timed out.\n", cmd);
1648 } else if (ret < 0) {
1649 printf("'%s' command failed.\n", cmd);
1654 if (os_memcmp(buf, "FAIL", 4) == 0)
1659 while (*pos != '\0' && *pos != '\n')
1662 os_strlcpy(addr, buf, addr_len);
1667 static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1669 char addr[32], cmd[64];
1671 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
1674 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
1675 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
1681 static int wpa_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
1684 return wpa_cli_cmd(ctrl, "DEAUTHENTICATE", 1, argc, argv);
1688 static int wpa_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
1691 return wpa_cli_cmd(ctrl, "DISASSOCIATE", 1, argc, argv);
1694 static int wpa_cli_cmd_chanswitch(struct wpa_ctrl *ctrl, int argc,
1697 return wpa_cli_cmd(ctrl, "CHAN_SWITCH", 2, argc, argv);
1700 #endif /* CONFIG_AP */
1703 static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[])
1705 return wpa_ctrl_command(ctrl, "SUSPEND");
1709 static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[])
1711 return wpa_ctrl_command(ctrl, "RESUME");
1715 #ifdef CONFIG_TESTING_OPTIONS
1716 static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1718 return wpa_ctrl_command(ctrl, "DROP_SA");
1720 #endif /* CONFIG_TESTING_OPTIONS */
1723 static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[])
1725 return wpa_cli_cmd(ctrl, "ROAM", 1, argc, argv);
1731 static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
1733 return wpa_cli_cmd(ctrl, "P2P_FIND", 0, argc, argv);
1737 static char ** wpa_cli_complete_p2p_find(const char *str, int pos)
1740 int arg = get_cmd_arg_num(str, pos);
1742 res = os_calloc(6, sizeof(char *));
1745 res[0] = os_strdup("type=social");
1746 if (res[0] == NULL) {
1750 res[1] = os_strdup("type=progressive");
1753 res[2] = os_strdup("delay=");
1756 res[3] = os_strdup("dev_id=");
1760 res[4] = os_strdup("[timeout]");
1766 static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc,
1769 return wpa_ctrl_command(ctrl, "P2P_STOP_FIND");
1773 static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc,
1776 return wpa_cli_cmd(ctrl, "P2P_CONNECT", 2, argc, argv);
1780 static char ** wpa_cli_complete_p2p_connect(const char *str, int pos)
1782 int arg = get_cmd_arg_num(str, pos);
1787 res = cli_txt_list_array(&p2p_peers);
1795 static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc,
1798 return wpa_cli_cmd(ctrl, "P2P_LISTEN", 0, argc, argv);
1802 static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc,
1805 return wpa_cli_cmd(ctrl, "P2P_GROUP_REMOVE", 1, argc, argv);
1809 static char ** wpa_cli_complete_p2p_group_remove(const char *str, int pos)
1811 int arg = get_cmd_arg_num(str, pos);
1816 res = cli_txt_list_array(&p2p_groups);
1824 static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc,
1827 return wpa_cli_cmd(ctrl, "P2P_GROUP_ADD", 0, argc, argv);
1831 static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc,
1834 if (argc != 2 && argc != 3) {
1835 printf("Invalid P2P_PROV_DISC command: needs at least "
1836 "two arguments, address and config method\n"
1837 "(display, keypad, or pbc) and an optional join\n");
1841 return wpa_cli_cmd(ctrl, "P2P_PROV_DISC", 2, argc, argv);
1845 static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc,
1848 return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE");
1852 static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc,
1857 if (argc != 2 && argc != 4) {
1858 printf("Invalid P2P_SERV_DISC_REQ command: needs two "
1859 "arguments (address and TLVs) or four arguments "
1860 "(address, \"upnp\", version, search target "
1865 if (write_cmd(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ", argc, argv) < 0)
1867 return wpa_ctrl_command(ctrl, cmd);
1871 static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl,
1872 int argc, char *argv[])
1874 return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_CANCEL_REQ", 1, argc, argv);
1878 static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc,
1885 printf("Invalid P2P_SERV_DISC_RESP command: needs four "
1886 "arguments (freq, address, dialog token, and TLVs)\n");
1890 res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s",
1891 argv[0], argv[1], argv[2], argv[3]);
1892 if (res < 0 || (size_t) res >= sizeof(cmd))
1894 cmd[sizeof(cmd) - 1] = '\0';
1895 return wpa_ctrl_command(ctrl, cmd);
1899 static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc,
1902 return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE");
1906 static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl,
1907 int argc, char *argv[])
1909 return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_EXTERNAL", 1, argc, argv);
1913 static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc,
1916 return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH");
1920 static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc,
1926 if (argc != 3 && argc != 4) {
1927 printf("Invalid P2P_SERVICE_ADD command: needs three or four "
1933 res = os_snprintf(cmd, sizeof(cmd),
1934 "P2P_SERVICE_ADD %s %s %s %s",
1935 argv[0], argv[1], argv[2], argv[3]);
1937 res = os_snprintf(cmd, sizeof(cmd),
1938 "P2P_SERVICE_ADD %s %s %s",
1939 argv[0], argv[1], argv[2]);
1940 if (res < 0 || (size_t) res >= sizeof(cmd))
1942 cmd[sizeof(cmd) - 1] = '\0';
1943 return wpa_ctrl_command(ctrl, cmd);
1947 static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc,
1953 if (argc != 2 && argc != 3) {
1954 printf("Invalid P2P_SERVICE_DEL command: needs two or three "
1960 res = os_snprintf(cmd, sizeof(cmd),
1961 "P2P_SERVICE_DEL %s %s %s",
1962 argv[0], argv[1], argv[2]);
1964 res = os_snprintf(cmd, sizeof(cmd),
1965 "P2P_SERVICE_DEL %s %s",
1967 if (res < 0 || (size_t) res >= sizeof(cmd))
1969 cmd[sizeof(cmd) - 1] = '\0';
1970 return wpa_ctrl_command(ctrl, cmd);
1974 static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl,
1975 int argc, char *argv[])
1977 return wpa_cli_cmd(ctrl, "P2P_REJECT", 1, argc, argv);
1981 static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl,
1982 int argc, char *argv[])
1984 return wpa_cli_cmd(ctrl, "P2P_INVITE", 1, argc, argv);
1988 static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[])
1990 return wpa_cli_cmd(ctrl, "P2P_PEER", 1, argc, argv);
1994 static char ** wpa_cli_complete_p2p_peer(const char *str, int pos)
1996 int arg = get_cmd_arg_num(str, pos);
2001 res = cli_txt_list_array(&p2p_peers);
2009 static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, char *cmd,
2010 char *addr, size_t addr_len,
2013 char buf[4096], *pos;
2017 if (ctrl_conn == NULL)
2019 len = sizeof(buf) - 1;
2020 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
2023 printf("'%s' command timed out.\n", cmd);
2025 } else if (ret < 0) {
2026 printf("'%s' command failed.\n", cmd);
2031 if (os_memcmp(buf, "FAIL", 4) == 0)
2035 while (*pos != '\0' && *pos != '\n')
2038 os_strlcpy(addr, buf, addr_len);
2039 if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL)
2040 printf("%s\n", addr);
2045 static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[])
2047 char addr[32], cmd[64];
2050 discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0;
2052 if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST",
2053 addr, sizeof(addr), discovered))
2056 os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr);
2057 } while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr),
2064 static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
2066 return wpa_cli_cmd(ctrl, "P2P_SET", 2, argc, argv);
2070 static char ** wpa_cli_complete_p2p_set(const char *str, int pos)
2072 int arg = get_cmd_arg_num(str, pos);
2073 const char *fields[] = {
2093 int i, num_fields = ARRAY_SIZE(fields);
2096 char **res = os_calloc(num_fields + 1, sizeof(char *));
2099 for (i = 0; i < num_fields; i++) {
2100 res[i] = os_strdup(fields[i]);
2107 if (arg == 2 && os_strncasecmp(str, "p2p_set peer_filter ", 20) == 0)
2108 return cli_txt_list_array(&p2p_peers);
2114 static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2116 return wpa_ctrl_command(ctrl, "P2P_FLUSH");
2120 static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc,
2123 return wpa_ctrl_command(ctrl, "P2P_CANCEL");
2127 static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc,
2130 return wpa_cli_cmd(ctrl, "P2P_UNAUTHORIZE", 1, argc, argv);
2134 static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl *ctrl, int argc,
2137 if (argc != 0 && argc != 2 && argc != 4) {
2138 printf("Invalid P2P_PRESENCE_REQ command: needs two arguments "
2139 "(preferred duration, interval; in microsecods).\n"
2140 "Optional second pair can be used to provide "
2141 "acceptable values.\n");
2145 return wpa_cli_cmd(ctrl, "P2P_PRESENCE_REQ", 0, argc, argv);
2149 static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc,
2152 if (argc != 0 && argc != 2) {
2153 printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
2154 "(availability period, availability interval; in "
2156 "Extended Listen Timing can be cancelled with this "
2157 "command when used without parameters.\n");
2161 return wpa_cli_cmd(ctrl, "P2P_EXT_LISTEN", 0, argc, argv);
2165 static int wpa_cli_cmd_p2p_remove_client(struct wpa_ctrl *ctrl, int argc,
2168 return wpa_cli_cmd(ctrl, "P2P_REMOVE_CLIENT", 1, argc, argv);
2171 #endif /* CONFIG_P2P */
2173 #ifdef CONFIG_WIFI_DISPLAY
2175 static int wpa_cli_cmd_wfd_subelem_set(struct wpa_ctrl *ctrl, int argc,
2181 if (argc != 1 && argc != 2) {
2182 printf("Invalid WFD_SUBELEM_SET command: needs one or two "
2183 "arguments (subelem, hexdump)\n");
2187 res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_SET %s %s",
2188 argv[0], argc > 1 ? argv[1] : "");
2189 if (res < 0 || (size_t) res >= sizeof(cmd))
2191 cmd[sizeof(cmd) - 1] = '\0';
2192 return wpa_ctrl_command(ctrl, cmd);
2196 static int wpa_cli_cmd_wfd_subelem_get(struct wpa_ctrl *ctrl, int argc,
2203 printf("Invalid WFD_SUBELEM_GET command: needs one "
2204 "argument (subelem)\n");
2208 res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_GET %s",
2210 if (res < 0 || (size_t) res >= sizeof(cmd))
2212 cmd[sizeof(cmd) - 1] = '\0';
2213 return wpa_ctrl_command(ctrl, cmd);
2215 #endif /* CONFIG_WIFI_DISPLAY */
2218 #ifdef CONFIG_INTERWORKING
2219 static int wpa_cli_cmd_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2222 return wpa_ctrl_command(ctrl, "FETCH_ANQP");
2226 static int wpa_cli_cmd_stop_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2229 return wpa_ctrl_command(ctrl, "STOP_FETCH_ANQP");
2233 static int wpa_cli_cmd_interworking_select(struct wpa_ctrl *ctrl, int argc,
2236 return wpa_cli_cmd(ctrl, "INTERWORKING_SELECT", 0, argc, argv);
2240 static int wpa_cli_cmd_interworking_connect(struct wpa_ctrl *ctrl, int argc,
2243 return wpa_cli_cmd(ctrl, "INTERWORKING_CONNECT", 1, argc, argv);
2247 static int wpa_cli_cmd_anqp_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
2249 return wpa_cli_cmd(ctrl, "ANQP_GET", 2, argc, argv);
2253 static int wpa_cli_cmd_gas_request(struct wpa_ctrl *ctrl, int argc,
2256 return wpa_cli_cmd(ctrl, "GAS_REQUEST", 2, argc, argv);
2260 static int wpa_cli_cmd_gas_response_get(struct wpa_ctrl *ctrl, int argc,
2263 return wpa_cli_cmd(ctrl, "GAS_RESPONSE_GET", 2, argc, argv);
2265 #endif /* CONFIG_INTERWORKING */
2270 static int wpa_cli_cmd_hs20_anqp_get(struct wpa_ctrl *ctrl, int argc,
2273 return wpa_cli_cmd(ctrl, "HS20_ANQP_GET", 2, argc, argv);
2277 static int wpa_cli_cmd_get_nai_home_realm_list(struct wpa_ctrl *ctrl, int argc,
2283 printf("Command needs one or two arguments (dst mac addr and "
2284 "optional home realm)\n");
2288 if (write_cmd(cmd, sizeof(cmd), "HS20_GET_NAI_HOME_REALM_LIST",
2292 return wpa_ctrl_command(ctrl, cmd);
2296 static int wpa_cli_cmd_hs20_icon_request(struct wpa_ctrl *ctrl, int argc,
2302 printf("Command needs two arguments (dst mac addr and "
2307 if (write_cmd(cmd, sizeof(cmd), "HS20_ICON_REQUEST", argc, argv) < 0)
2310 return wpa_ctrl_command(ctrl, cmd);
2314 static int wpa_cli_cmd_fetch_osu(struct wpa_ctrl *ctrl, int argc, char *argv[])
2316 return wpa_ctrl_command(ctrl, "FETCH_OSU");
2320 static int wpa_cli_cmd_cancel_fetch_osu(struct wpa_ctrl *ctrl, int argc,
2323 return wpa_ctrl_command(ctrl, "CANCEL_FETCH_OSU");
2326 #endif /* CONFIG_HS20 */
2329 static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc,
2332 return wpa_cli_cmd(ctrl, "STA_AUTOCONNECT", 1, argc, argv);
2336 static int wpa_cli_cmd_tdls_discover(struct wpa_ctrl *ctrl, int argc,
2339 return wpa_cli_cmd(ctrl, "TDLS_DISCOVER", 1, argc, argv);
2343 static int wpa_cli_cmd_tdls_setup(struct wpa_ctrl *ctrl, int argc,
2346 return wpa_cli_cmd(ctrl, "TDLS_SETUP", 1, argc, argv);
2350 static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
2353 return wpa_cli_cmd(ctrl, "TDLS_TEARDOWN", 1, argc, argv);
2357 static int wpa_cli_cmd_signal_poll(struct wpa_ctrl *ctrl, int argc,
2360 return wpa_ctrl_command(ctrl, "SIGNAL_POLL");
2364 static int wpa_cli_cmd_pktcnt_poll(struct wpa_ctrl *ctrl, int argc,
2367 return wpa_ctrl_command(ctrl, "PKTCNT_POLL");
2371 static int wpa_cli_cmd_reauthenticate(struct wpa_ctrl *ctrl, int argc,
2374 return wpa_ctrl_command(ctrl, "REAUTHENTICATE");
2378 #ifdef CONFIG_AUTOSCAN
2380 static int wpa_cli_cmd_autoscan(struct wpa_ctrl *ctrl, int argc, char *argv[])
2383 return wpa_ctrl_command(ctrl, "AUTOSCAN ");
2385 return wpa_cli_cmd(ctrl, "AUTOSCAN", 0, argc, argv);
2388 #endif /* CONFIG_AUTOSCAN */
2393 static int wpa_cli_cmd_wnm_sleep(struct wpa_ctrl *ctrl, int argc, char *argv[])
2395 return wpa_cli_cmd(ctrl, "WNM_SLEEP", 0, argc, argv);
2399 static int wpa_cli_cmd_wnm_bss_query(struct wpa_ctrl *ctrl, int argc, char *argv[])
2401 return wpa_cli_cmd(ctrl, "WNM_BSS_QUERY", 1, argc, argv);
2404 #endif /* CONFIG_WNM */
2407 static int wpa_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
2411 return wpa_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
2416 static int wpa_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
2418 return wpa_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
2420 #endif /* ANDROID */
2423 static int wpa_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
2425 return wpa_cli_cmd(ctrl, "VENDOR", 1, argc, argv);
2429 static int wpa_cli_cmd_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2431 return wpa_ctrl_command(ctrl, "FLUSH");
2435 static int wpa_cli_cmd_radio_work(struct wpa_ctrl *ctrl, int argc, char *argv[])
2437 return wpa_cli_cmd(ctrl, "RADIO_WORK", 1, argc, argv);
2441 enum wpa_cli_cmd_flags {
2442 cli_cmd_flag_none = 0x00,
2443 cli_cmd_flag_sensitive = 0x01
2446 struct wpa_cli_cmd {
2448 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
2449 char ** (*completion)(const char *str, int pos);
2450 enum wpa_cli_cmd_flags flags;
2454 static struct wpa_cli_cmd wpa_cli_commands[] = {
2455 { "status", wpa_cli_cmd_status, NULL,
2457 "[verbose] = get current WPA/EAPOL/EAP status" },
2458 { "ifname", wpa_cli_cmd_ifname, NULL,
2460 "= get current interface name" },
2461 { "ping", wpa_cli_cmd_ping, NULL,
2463 "= pings wpa_supplicant" },
2464 { "relog", wpa_cli_cmd_relog, NULL,
2466 "= re-open log-file (allow rolling logs)" },
2467 { "note", wpa_cli_cmd_note, NULL,
2469 "<text> = add a note to wpa_supplicant debug log" },
2470 { "mib", wpa_cli_cmd_mib, NULL,
2472 "= get MIB variables (dot1x, dot11)" },
2473 { "help", wpa_cli_cmd_help, wpa_cli_complete_help,
2475 "[command] = show usage help" },
2476 { "interface", wpa_cli_cmd_interface, NULL,
2478 "[ifname] = show interfaces/select interface" },
2479 { "level", wpa_cli_cmd_level, NULL,
2481 "<debug level> = change debug level" },
2482 { "license", wpa_cli_cmd_license, NULL,
2484 "= show full wpa_cli license" },
2485 { "quit", wpa_cli_cmd_quit, NULL,
2488 { "set", wpa_cli_cmd_set, wpa_cli_complete_set,
2490 "= set variables (shows list of variables when run without "
2492 { "get", wpa_cli_cmd_get, NULL,
2494 "<name> = get information" },
2495 { "logon", wpa_cli_cmd_logon, NULL,
2497 "= IEEE 802.1X EAPOL state machine logon" },
2498 { "logoff", wpa_cli_cmd_logoff, NULL,
2500 "= IEEE 802.1X EAPOL state machine logoff" },
2501 { "pmksa", wpa_cli_cmd_pmksa, NULL,
2503 "= show PMKSA cache" },
2504 { "reassociate", wpa_cli_cmd_reassociate, NULL,
2506 "= force reassociation" },
2507 { "reattach", wpa_cli_cmd_reattach, NULL,
2509 "= force reassociation back to the same BSS" },
2510 { "preauthenticate", wpa_cli_cmd_preauthenticate, wpa_cli_complete_bss,
2512 "<BSSID> = force preauthentication" },
2513 { "identity", wpa_cli_cmd_identity, NULL,
2515 "<network id> <identity> = configure identity for an SSID" },
2516 { "password", wpa_cli_cmd_password, NULL,
2517 cli_cmd_flag_sensitive,
2518 "<network id> <password> = configure password for an SSID" },
2519 { "new_password", wpa_cli_cmd_new_password, NULL,
2520 cli_cmd_flag_sensitive,
2521 "<network id> <password> = change password for an SSID" },
2522 { "pin", wpa_cli_cmd_pin, NULL,
2523 cli_cmd_flag_sensitive,
2524 "<network id> <pin> = configure pin for an SSID" },
2525 { "otp", wpa_cli_cmd_otp, NULL,
2526 cli_cmd_flag_sensitive,
2527 "<network id> <password> = configure one-time-password for an SSID"
2529 { "passphrase", wpa_cli_cmd_passphrase, NULL,
2530 cli_cmd_flag_sensitive,
2531 "<network id> <passphrase> = configure private key passphrase\n"
2533 { "sim", wpa_cli_cmd_sim, NULL,
2534 cli_cmd_flag_sensitive,
2535 "<network id> <pin> = report SIM operation result" },
2536 { "bssid", wpa_cli_cmd_bssid, NULL,
2538 "<network id> <BSSID> = set preferred BSSID for an SSID" },
2539 { "blacklist", wpa_cli_cmd_blacklist, wpa_cli_complete_bss,
2541 "<BSSID> = add a BSSID to the blacklist\n"
2542 "blacklist clear = clear the blacklist\n"
2543 "blacklist = display the blacklist" },
2544 { "log_level", wpa_cli_cmd_log_level, NULL,
2546 "<level> [<timestamp>] = update the log level/timestamp\n"
2547 "log_level = display the current log level and log options" },
2548 { "list_networks", wpa_cli_cmd_list_networks, NULL,
2550 "= list configured networks" },
2551 { "select_network", wpa_cli_cmd_select_network, NULL,
2553 "<network id> = select a network (disable others)" },
2554 { "enable_network", wpa_cli_cmd_enable_network, NULL,
2556 "<network id> = enable a network" },
2557 { "disable_network", wpa_cli_cmd_disable_network, NULL,
2559 "<network id> = disable a network" },
2560 { "add_network", wpa_cli_cmd_add_network, NULL,
2562 "= add a network" },
2563 { "remove_network", wpa_cli_cmd_remove_network, NULL,
2565 "<network id> = remove a network" },
2566 { "set_network", wpa_cli_cmd_set_network, NULL,
2567 cli_cmd_flag_sensitive,
2568 "<network id> <variable> <value> = set network variables (shows\n"
2569 " list of variables when run without arguments)" },
2570 { "get_network", wpa_cli_cmd_get_network, NULL,
2572 "<network id> <variable> = get network variables" },
2573 { "list_creds", wpa_cli_cmd_list_creds, NULL,
2575 "= list configured credentials" },
2576 { "add_cred", wpa_cli_cmd_add_cred, NULL,
2578 "= add a credential" },
2579 { "remove_cred", wpa_cli_cmd_remove_cred, NULL,
2581 "<cred id> = remove a credential" },
2582 { "set_cred", wpa_cli_cmd_set_cred, NULL,
2583 cli_cmd_flag_sensitive,
2584 "<cred id> <variable> <value> = set credential variables" },
2585 { "get_cred", wpa_cli_cmd_get_cred, NULL,
2587 "<cred id> <variable> = get credential variables" },
2588 { "save_config", wpa_cli_cmd_save_config, NULL,
2590 "= save the current configuration" },
2591 { "disconnect", wpa_cli_cmd_disconnect, NULL,
2593 "= disconnect and wait for reassociate/reconnect command before\n"
2595 { "reconnect", wpa_cli_cmd_reconnect, NULL,
2597 "= like reassociate, but only takes effect if already disconnected"
2599 { "scan", wpa_cli_cmd_scan, NULL,
2601 "= request new BSS scan" },
2602 { "scan_results", wpa_cli_cmd_scan_results, NULL,
2604 "= get latest scan results" },
2605 { "bss", wpa_cli_cmd_bss, wpa_cli_complete_bss,
2607 "<<idx> | <bssid>> = get detailed scan result info" },
2608 { "get_capability", wpa_cli_cmd_get_capability, NULL,
2610 "<eap/pairwise/group/key_mgmt/proto/auth_alg/channels/freq/modes> "
2611 "= get capabilies" },
2612 { "reconfigure", wpa_cli_cmd_reconfigure, NULL,
2614 "= force wpa_supplicant to re-read its configuration file" },
2615 { "terminate", wpa_cli_cmd_terminate, NULL,
2617 "= terminate wpa_supplicant" },
2618 { "interface_add", wpa_cli_cmd_interface_add, NULL,
2620 "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
2621 " <bridge_name> = adds new interface, all parameters but <ifname>\n"
2623 { "interface_remove", wpa_cli_cmd_interface_remove, NULL,
2625 "<ifname> = removes the interface" },
2626 { "interface_list", wpa_cli_cmd_interface_list, NULL,
2628 "= list available interfaces" },
2629 { "ap_scan", wpa_cli_cmd_ap_scan, NULL,
2631 "<value> = set ap_scan parameter" },
2632 { "scan_interval", wpa_cli_cmd_scan_interval, NULL,
2634 "<value> = set scan_interval parameter (in seconds)" },
2635 { "bss_expire_age", wpa_cli_cmd_bss_expire_age, NULL,
2637 "<value> = set BSS expiration age parameter" },
2638 { "bss_expire_count", wpa_cli_cmd_bss_expire_count, NULL,
2640 "<value> = set BSS expiration scan count parameter" },
2641 { "bss_flush", wpa_cli_cmd_bss_flush, NULL,
2643 "<value> = set BSS flush age (0 by default)" },
2644 { "stkstart", wpa_cli_cmd_stkstart, NULL,
2646 "<addr> = request STK negotiation with <addr>" },
2647 { "ft_ds", wpa_cli_cmd_ft_ds, wpa_cli_complete_bss,
2649 "<addr> = request over-the-DS FT with <addr>" },
2650 { "wps_pbc", wpa_cli_cmd_wps_pbc, wpa_cli_complete_bss,
2652 "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
2653 { "wps_pin", wpa_cli_cmd_wps_pin, wpa_cli_complete_bss,
2654 cli_cmd_flag_sensitive,
2655 "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
2657 { "wps_check_pin", wpa_cli_cmd_wps_check_pin, NULL,
2658 cli_cmd_flag_sensitive,
2659 "<PIN> = verify PIN checksum" },
2660 { "wps_cancel", wpa_cli_cmd_wps_cancel, NULL, cli_cmd_flag_none,
2661 "Cancels the pending WPS operation" },
2662 #ifdef CONFIG_WPS_NFC
2663 { "wps_nfc", wpa_cli_cmd_wps_nfc, wpa_cli_complete_bss,
2665 "[BSSID] = start Wi-Fi Protected Setup: NFC" },
2666 { "wps_nfc_config_token", wpa_cli_cmd_wps_nfc_config_token, NULL,
2668 "<WPS|NDEF> = build configuration token" },
2669 { "wps_nfc_token", wpa_cli_cmd_wps_nfc_token, NULL,
2671 "<WPS|NDEF> = create password token" },
2672 { "wps_nfc_tag_read", wpa_cli_cmd_wps_nfc_tag_read, NULL,
2673 cli_cmd_flag_sensitive,
2674 "<hexdump of payload> = report read NFC tag with WPS data" },
2675 { "nfc_get_handover_req", wpa_cli_cmd_nfc_get_handover_req, NULL,
2677 "<NDEF> <WPS> = create NFC handover request" },
2678 { "nfc_get_handover_sel", wpa_cli_cmd_nfc_get_handover_sel, NULL,
2680 "<NDEF> <WPS> = create NFC handover select" },
2681 { "nfc_report_handover", wpa_cli_cmd_nfc_report_handover, NULL,
2683 "<role> <type> <hexdump of req> <hexdump of sel> = report completed "
2685 #endif /* CONFIG_WPS_NFC */
2686 { "wps_reg", wpa_cli_cmd_wps_reg, wpa_cli_complete_bss,
2687 cli_cmd_flag_sensitive,
2688 "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
2689 { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin, NULL,
2690 cli_cmd_flag_sensitive,
2691 "[params..] = enable/disable AP PIN" },
2692 { "wps_er_start", wpa_cli_cmd_wps_er_start, NULL,
2694 "[IP address] = start Wi-Fi Protected Setup External Registrar" },
2695 { "wps_er_stop", wpa_cli_cmd_wps_er_stop, NULL,
2697 "= stop Wi-Fi Protected Setup External Registrar" },
2698 { "wps_er_pin", wpa_cli_cmd_wps_er_pin, NULL,
2699 cli_cmd_flag_sensitive,
2700 "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
2701 { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc, NULL,
2703 "<UUID> = accept an Enrollee PBC using External Registrar" },
2704 { "wps_er_learn", wpa_cli_cmd_wps_er_learn, NULL,
2705 cli_cmd_flag_sensitive,
2706 "<UUID> <PIN> = learn AP configuration" },
2707 { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config, NULL,
2709 "<UUID> <network id> = set AP configuration for enrolling" },
2710 { "wps_er_config", wpa_cli_cmd_wps_er_config, NULL,
2711 cli_cmd_flag_sensitive,
2712 "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
2713 #ifdef CONFIG_WPS_NFC
2714 { "wps_er_nfc_config_token", wpa_cli_cmd_wps_er_nfc_config_token, NULL,
2716 "<WPS/NDEF> <UUID> = build NFC configuration token" },
2717 #endif /* CONFIG_WPS_NFC */
2718 { "ibss_rsn", wpa_cli_cmd_ibss_rsn, NULL,
2720 "<addr> = request RSN authentication with <addr> in IBSS" },
2722 { "sta", wpa_cli_cmd_sta, NULL,
2724 "<addr> = get information about an associated station (AP)" },
2725 { "all_sta", wpa_cli_cmd_all_sta, NULL,
2727 "= get information about all associated stations (AP)" },
2728 { "deauthenticate", wpa_cli_cmd_deauthenticate, NULL,
2730 "<addr> = deauthenticate a station" },
2731 { "disassociate", wpa_cli_cmd_disassociate, NULL,
2733 "<addr> = disassociate a station" },
2734 { "chan_switch", wpa_cli_cmd_chanswitch, NULL,
2736 "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]"
2737 " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]"
2738 " = CSA parameters" },
2739 #endif /* CONFIG_AP */
2740 { "suspend", wpa_cli_cmd_suspend, NULL, cli_cmd_flag_none,
2741 "= notification of suspend/hibernate" },
2742 { "resume", wpa_cli_cmd_resume, NULL, cli_cmd_flag_none,
2743 "= notification of resume/thaw" },
2744 #ifdef CONFIG_TESTING_OPTIONS
2745 { "drop_sa", wpa_cli_cmd_drop_sa, NULL, cli_cmd_flag_none,
2746 "= drop SA without deauth/disassoc (test command)" },
2747 #endif /* CONFIG_TESTING_OPTIONS */
2748 { "roam", wpa_cli_cmd_roam, wpa_cli_complete_bss,
2750 "<addr> = roam to the specified BSS" },
2752 { "p2p_find", wpa_cli_cmd_p2p_find, wpa_cli_complete_p2p_find,
2754 "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
2755 { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, NULL, cli_cmd_flag_none,
2756 "= stop P2P Devices search" },
2757 { "p2p_connect", wpa_cli_cmd_p2p_connect, wpa_cli_complete_p2p_connect,
2759 "<addr> <\"pbc\"|PIN> [ht40] = connect to a P2P Device" },
2760 { "p2p_listen", wpa_cli_cmd_p2p_listen, NULL, cli_cmd_flag_none,
2761 "[timeout] = listen for P2P Devices for up-to timeout seconds" },
2762 { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove,
2763 wpa_cli_complete_p2p_group_remove, cli_cmd_flag_none,
2764 "<ifname> = remove P2P group interface (terminate group if GO)" },
2765 { "p2p_group_add", wpa_cli_cmd_p2p_group_add, NULL, cli_cmd_flag_none,
2766 "[ht40] = add a new P2P group (local end as GO)" },
2767 { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc,
2768 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2769 "<addr> <method> = request provisioning discovery" },
2770 { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase, NULL,
2772 "= get the passphrase for a group (GO only)" },
2773 { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req,
2774 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2775 "<addr> <TLVs> = schedule service discovery request" },
2776 { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req,
2777 NULL, cli_cmd_flag_none,
2778 "<id> = cancel pending service discovery request" },
2779 { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp, NULL,
2781 "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
2782 { "p2p_service_update", wpa_cli_cmd_p2p_service_update, NULL,
2784 "= indicate change in local services" },
2785 { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external, NULL,
2787 "<external> = set external processing of service discovery" },
2788 { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush, NULL,
2790 "= remove all stored service entries" },
2791 { "p2p_service_add", wpa_cli_cmd_p2p_service_add, NULL,
2793 "<bonjour|upnp> <query|version> <response|service> = add a local "
2795 { "p2p_service_del", wpa_cli_cmd_p2p_service_del, NULL,
2797 "<bonjour|upnp> <query|version> [|service] = remove a local "
2799 { "p2p_reject", wpa_cli_cmd_p2p_reject, wpa_cli_complete_p2p_peer,
2801 "<addr> = reject connection attempts from a specific peer" },
2802 { "p2p_invite", wpa_cli_cmd_p2p_invite, NULL,
2804 "<cmd> [peer=addr] = invite peer" },
2805 { "p2p_peers", wpa_cli_cmd_p2p_peers, NULL, cli_cmd_flag_none,
2806 "[discovered] = list known (optionally, only fully discovered) P2P "
2808 { "p2p_peer", wpa_cli_cmd_p2p_peer, wpa_cli_complete_p2p_peer,
2810 "<address> = show information about known P2P peer" },
2811 { "p2p_set", wpa_cli_cmd_p2p_set, wpa_cli_complete_p2p_set,
2813 "<field> <value> = set a P2P parameter" },
2814 { "p2p_flush", wpa_cli_cmd_p2p_flush, NULL, cli_cmd_flag_none,
2815 "= flush P2P state" },
2816 { "p2p_cancel", wpa_cli_cmd_p2p_cancel, NULL, cli_cmd_flag_none,
2817 "= cancel P2P group formation" },
2818 { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize,
2819 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2820 "<address> = unauthorize a peer" },
2821 { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, NULL,
2823 "[<duration> <interval>] [<duration> <interval>] = request GO "
2825 { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, NULL,
2827 "[<period> <interval>] = set extended listen timing" },
2828 { "p2p_remove_client", wpa_cli_cmd_p2p_remove_client,
2829 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2830 "<address|iface=address> = remove a peer from all groups" },
2831 #endif /* CONFIG_P2P */
2832 #ifdef CONFIG_WIFI_DISPLAY
2833 { "wfd_subelem_set", wpa_cli_cmd_wfd_subelem_set, NULL,
2835 "<subelem> [contents] = set Wi-Fi Display subelement" },
2836 { "wfd_subelem_get", wpa_cli_cmd_wfd_subelem_get, NULL,
2838 "<subelem> = get Wi-Fi Display subelement" },
2839 #endif /* CONFIG_WIFI_DISPLAY */
2840 #ifdef CONFIG_INTERWORKING
2841 { "fetch_anqp", wpa_cli_cmd_fetch_anqp, NULL, cli_cmd_flag_none,
2842 "= fetch ANQP information for all APs" },
2843 { "stop_fetch_anqp", wpa_cli_cmd_stop_fetch_anqp, NULL,
2845 "= stop fetch_anqp operation" },
2846 { "interworking_select", wpa_cli_cmd_interworking_select, NULL,
2848 "[auto] = perform Interworking network selection" },
2849 { "interworking_connect", wpa_cli_cmd_interworking_connect,
2850 wpa_cli_complete_bss, cli_cmd_flag_none,
2851 "<BSSID> = connect using Interworking credentials" },
2852 { "anqp_get", wpa_cli_cmd_anqp_get, wpa_cli_complete_bss,
2854 "<addr> <info id>[,<info id>]... = request ANQP information" },
2855 { "gas_request", wpa_cli_cmd_gas_request, wpa_cli_complete_bss,
2857 "<addr> <AdvProtoID> [QueryReq] = GAS request" },
2858 { "gas_response_get", wpa_cli_cmd_gas_response_get,
2859 wpa_cli_complete_bss, cli_cmd_flag_none,
2860 "<addr> <dialog token> [start,len] = Fetch last GAS response" },
2861 #endif /* CONFIG_INTERWORKING */
2863 { "hs20_anqp_get", wpa_cli_cmd_hs20_anqp_get, wpa_cli_complete_bss,
2865 "<addr> <subtype>[,<subtype>]... = request HS 2.0 ANQP information"
2867 { "nai_home_realm_list", wpa_cli_cmd_get_nai_home_realm_list,
2868 wpa_cli_complete_bss, cli_cmd_flag_none,
2869 "<addr> <home realm> = get HS20 nai home realm list" },
2870 { "hs20_icon_request", wpa_cli_cmd_hs20_icon_request,
2871 wpa_cli_complete_bss, cli_cmd_flag_none,
2872 "<addr> <icon name> = get Hotspot 2.0 OSU icon" },
2873 { "fetch_osu", wpa_cli_cmd_fetch_osu, NULL, cli_cmd_flag_none,
2874 "= fetch OSU provider information from all APs" },
2875 { "cancel_fetch_osu", wpa_cli_cmd_cancel_fetch_osu, NULL,
2877 "= cancel fetch_osu command" },
2878 #endif /* CONFIG_HS20 */
2879 { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, NULL,
2881 "<0/1> = disable/enable automatic reconnection" },
2882 { "tdls_discover", wpa_cli_cmd_tdls_discover, NULL,
2884 "<addr> = request TDLS discovery with <addr>" },
2885 { "tdls_setup", wpa_cli_cmd_tdls_setup, NULL,
2887 "<addr> = request TDLS setup with <addr>" },
2888 { "tdls_teardown", wpa_cli_cmd_tdls_teardown, NULL,
2890 "<addr> = tear down TDLS with <addr>" },
2891 { "signal_poll", wpa_cli_cmd_signal_poll, NULL,
2893 "= get signal parameters" },
2894 { "pktcnt_poll", wpa_cli_cmd_pktcnt_poll, NULL,
2896 "= get TX/RX packet counters" },
2897 { "reauthenticate", wpa_cli_cmd_reauthenticate, NULL,
2899 "= trigger IEEE 802.1X/EAPOL reauthentication" },
2900 #ifdef CONFIG_AUTOSCAN
2901 { "autoscan", wpa_cli_cmd_autoscan, NULL, cli_cmd_flag_none,
2902 "[params] = Set or unset (if none) autoscan parameters" },
2903 #endif /* CONFIG_AUTOSCAN */
2905 { "wnm_sleep", wpa_cli_cmd_wnm_sleep, NULL, cli_cmd_flag_none,
2906 "<enter/exit> [interval=#] = enter/exit WNM-Sleep mode" },
2907 { "wnm_bss_query", wpa_cli_cmd_wnm_bss_query, NULL, cli_cmd_flag_none,
2908 "<query reason> = Send BSS Transition Management Query" },
2909 #endif /* CONFIG_WNM */
2910 { "raw", wpa_cli_cmd_raw, NULL, cli_cmd_flag_sensitive,
2911 "<params..> = Sent unprocessed command" },
2912 { "flush", wpa_cli_cmd_flush, NULL, cli_cmd_flag_none,
2913 "= flush wpa_supplicant state" },
2915 { "driver", wpa_cli_cmd_driver, NULL, cli_cmd_flag_none,
2916 "<command> = driver private commands" },
2917 #endif /* ANDROID */
2918 { "radio_work", wpa_cli_cmd_radio_work, NULL, cli_cmd_flag_none,
2919 "= radio_work <show/add/done>" },
2920 { "vendor", wpa_cli_cmd_vendor, NULL, cli_cmd_flag_none,
2921 "<vendor id> <command id> [<hex formatted command argument>] = Send vendor command"
2923 { NULL, NULL, NULL, cli_cmd_flag_none, NULL }
2928 * Prints command usage, lines are padded with the specified string.
2930 static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
2935 printf("%s%s ", pad, cmd->cmd);
2936 for (n = 0; (c = cmd->usage[n]); n++) {
2945 static void print_help(const char *cmd)
2948 printf("commands:\n");
2949 for (n = 0; wpa_cli_commands[n].cmd; n++) {
2950 if (cmd == NULL || str_starts(wpa_cli_commands[n].cmd, cmd))
2951 print_cmd_help(&wpa_cli_commands[n], " ");
2956 static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd)
2958 const char *c, *delim;
2962 delim = os_strchr(cmd, ' ');
2966 len = os_strlen(cmd);
2968 for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
2969 if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
2970 return (wpa_cli_commands[n].flags &
2971 cli_cmd_flag_sensitive);
2977 static char ** wpa_list_cmd_list(void)
2981 struct cli_txt_entry *e;
2983 count = ARRAY_SIZE(wpa_cli_commands);
2984 count += dl_list_len(&p2p_groups);
2985 count += dl_list_len(&ifnames);
2986 res = os_calloc(count + 1, sizeof(char *));
2990 for (i = 0; wpa_cli_commands[i].cmd; i++) {
2991 res[i] = os_strdup(wpa_cli_commands[i].cmd);
2996 dl_list_for_each(e, &p2p_groups, struct cli_txt_entry, list) {
2997 size_t len = 8 + os_strlen(e->txt);
2998 res[i] = os_malloc(len);
3001 os_snprintf(res[i], len, "ifname=%s", e->txt);
3005 dl_list_for_each(e, &ifnames, struct cli_txt_entry, list) {
3006 res[i] = os_strdup(e->txt);
3016 static char ** wpa_cli_cmd_completion(const char *cmd, const char *str,
3021 for (i = 0; wpa_cli_commands[i].cmd; i++) {
3022 if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) {
3023 if (wpa_cli_commands[i].completion)
3024 return wpa_cli_commands[i].completion(str,
3027 printf("\r%s\n", wpa_cli_commands[i].usage);
3037 static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
3043 if (pos > 7 && os_strncasecmp(str, "IFNAME=", 7) == 0) {
3044 end = os_strchr(str, ' ');
3045 if (end && pos > end - str) {
3046 pos -= end - str + 1;
3051 end = os_strchr(str, ' ');
3052 if (end == NULL || str + pos < end)
3053 return wpa_list_cmd_list();
3055 cmd = os_malloc(pos + 1);
3058 os_memcpy(cmd, str, pos);
3059 cmd[end - str] = '\0';
3060 res = wpa_cli_cmd_completion(cmd, str, pos);
3066 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
3068 struct wpa_cli_cmd *cmd, *match = NULL;
3072 if (argc > 1 && os_strncasecmp(argv[0], "IFNAME=", 7) == 0) {
3073 ifname_prefix = argv[0] + 7;
3077 ifname_prefix = NULL;
3083 cmd = wpa_cli_commands;
3085 if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
3088 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
3089 /* we have an exact match */
3099 printf("Ambiguous command '%s'; possible commands:", argv[0]);
3100 cmd = wpa_cli_commands;
3102 if (os_strncasecmp(cmd->cmd, argv[0],
3103 os_strlen(argv[0])) == 0) {
3104 printf(" %s", cmd->cmd);
3110 } else if (count == 0) {
3111 printf("Unknown command '%s'\n", argv[0]);
3114 ret = match->handler(ctrl, argc - 1, &argv[1]);
3121 static int str_match(const char *a, const char *b)
3123 return os_strncmp(a, b, os_strlen(b)) == 0;
3127 static int wpa_cli_exec(const char *program, const char *arg1,
3135 len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
3136 cmd = os_malloc(len);
3139 res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
3140 if (res < 0 || (size_t) res >= len) {
3144 cmd[len - 1] = '\0';
3146 if (system(cmd) < 0)
3148 #endif /* _WIN32_WCE */
3155 static void wpa_cli_action_process(const char *msg)
3158 char *copy = NULL, *id, *pos2;
3163 pos = os_strchr(pos, '>');
3170 if (str_match(pos, WPA_EVENT_CONNECTED)) {
3172 os_unsetenv("WPA_ID");
3173 os_unsetenv("WPA_ID_STR");
3174 os_unsetenv("WPA_CTRL_DIR");
3176 pos = os_strstr(pos, "[id=");
3178 copy = os_strdup(pos + 4);
3182 while (*pos2 && *pos2 != ' ')
3186 os_setenv("WPA_ID", id, 1);
3187 while (*pos2 && *pos2 != '=')
3192 while (*pos2 && *pos2 != ']')
3195 os_setenv("WPA_ID_STR", id, 1);
3199 os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
3201 if (wpa_cli_connected <= 0 || new_id != wpa_cli_last_id) {
3202 wpa_cli_connected = 1;
3203 wpa_cli_last_id = new_id;
3204 wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED");
3206 } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) {
3207 if (wpa_cli_connected) {
3208 wpa_cli_connected = 0;
3209 wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED");
3211 } else if (str_match(pos, P2P_EVENT_GROUP_STARTED)) {
3212 wpa_cli_exec(action_file, ctrl_ifname, pos);
3213 } else if (str_match(pos, P2P_EVENT_GROUP_REMOVED)) {
3214 wpa_cli_exec(action_file, ctrl_ifname, pos);
3215 } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) {
3216 wpa_cli_exec(action_file, ctrl_ifname, pos);
3217 } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) {
3218 wpa_cli_exec(action_file, ctrl_ifname, pos);
3219 } else if (str_match(pos, P2P_EVENT_GO_NEG_FAILURE)) {
3220 wpa_cli_exec(action_file, ctrl_ifname, pos);
3221 } else if (str_match(pos, WPS_EVENT_SUCCESS)) {
3222 wpa_cli_exec(action_file, ctrl_ifname, pos);
3223 } else if (str_match(pos, WPS_EVENT_FAIL)) {
3224 wpa_cli_exec(action_file, ctrl_ifname, pos);
3225 } else if (str_match(pos, AP_STA_CONNECTED)) {
3226 wpa_cli_exec(action_file, ctrl_ifname, pos);
3227 } else if (str_match(pos, AP_STA_DISCONNECTED)) {
3228 wpa_cli_exec(action_file, ctrl_ifname, pos);
3229 } else if (str_match(pos, ESS_DISASSOC_IMMINENT)) {
3230 wpa_cli_exec(action_file, ctrl_ifname, pos);
3231 } else if (str_match(pos, HS20_SUBSCRIPTION_REMEDIATION)) {
3232 wpa_cli_exec(action_file, ctrl_ifname, pos);
3233 } else if (str_match(pos, HS20_DEAUTH_IMMINENT_NOTICE)) {
3234 wpa_cli_exec(action_file, ctrl_ifname, pos);
3235 } else if (str_match(pos, WPA_EVENT_TERMINATING)) {
3236 printf("wpa_supplicant is terminating - stop monitoring\n");
3242 #ifndef CONFIG_ANSI_C_EXTRA
3243 static void wpa_cli_action_cb(char *msg, size_t len)
3245 wpa_cli_action_process(msg);
3247 #endif /* CONFIG_ANSI_C_EXTRA */
3250 static void wpa_cli_reconnect(void)
3252 wpa_cli_close_connection();
3253 if (wpa_cli_open_connection(ctrl_ifname, 1) < 0)
3258 printf("\rConnection to wpa_supplicant re-established\n");
3264 static void cli_event(const char *str)
3266 const char *start, *s;
3268 start = os_strchr(str, '>');
3274 if (str_starts(start, WPA_EVENT_BSS_ADDED)) {
3275 s = os_strchr(start, ' ');
3278 s = os_strchr(s + 1, ' ');
3281 cli_txt_list_add(&bsses, s + 1);
3285 if (str_starts(start, WPA_EVENT_BSS_REMOVED)) {
3286 s = os_strchr(start, ' ');
3289 s = os_strchr(s + 1, ' ');
3292 cli_txt_list_del_addr(&bsses, s + 1);
3297 if (str_starts(start, P2P_EVENT_DEVICE_FOUND)) {
3298 s = os_strstr(start, " p2p_dev_addr=");
3301 cli_txt_list_add_addr(&p2p_peers, s + 14);
3305 if (str_starts(start, P2P_EVENT_DEVICE_LOST)) {
3306 s = os_strstr(start, " p2p_dev_addr=");
3309 cli_txt_list_del_addr(&p2p_peers, s + 14);
3313 if (str_starts(start, P2P_EVENT_GROUP_STARTED)) {
3314 s = os_strchr(start, ' ');
3317 cli_txt_list_add_word(&p2p_groups, s + 1);
3321 if (str_starts(start, P2P_EVENT_GROUP_REMOVED)) {
3322 s = os_strchr(start, ' ');
3325 cli_txt_list_del_word(&p2p_groups, s + 1);
3328 #endif /* CONFIG_P2P */
3332 static int check_terminating(const char *msg)
3334 const char *pos = msg;
3338 pos = os_strchr(pos, '>');
3345 if (str_match(pos, WPA_EVENT_TERMINATING) && ctrl_conn) {
3347 printf("\rConnection to wpa_supplicant lost - trying to "
3350 wpa_cli_attached = 0;
3351 wpa_cli_close_connection();
3359 static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
3361 if (ctrl_conn == NULL) {
3362 wpa_cli_reconnect();
3365 while (wpa_ctrl_pending(ctrl) > 0) {
3367 size_t len = sizeof(buf) - 1;
3368 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
3371 wpa_cli_action_process(buf);
3374 if (wpa_cli_show_event(buf)) {
3376 printf("\r%s\n", buf);
3380 if (interactive && check_terminating(buf) > 0)
3384 printf("Could not read pending message.\n");
3389 if (wpa_ctrl_pending(ctrl) < 0) {
3390 printf("Connection to wpa_supplicant lost - trying to "
3392 wpa_cli_reconnect();
3398 static int tokenize_cmd(char *cmd, char *argv[])
3411 if (argc == max_args)
3414 char *pos2 = os_strrchr(pos, '"');
3418 while (*pos != '\0' && *pos != ' ')
3428 static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
3430 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
3431 printf("Connection to wpa_supplicant lost - trying to "
3433 wpa_cli_close_connection();
3436 wpa_cli_reconnect();
3437 eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
3441 static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx)
3443 wpa_cli_recv_pending(mon_conn, 0);
3447 static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd)
3449 char *argv[max_args];
3451 argc = tokenize_cmd(cmd, argv);
3453 wpa_request(ctrl_conn, argc, argv);
3457 static void wpa_cli_edit_eof_cb(void *ctx)
3463 static int warning_displayed = 0;
3464 static char *hfile = NULL;
3465 static int edit_started = 0;
3467 static void start_edit(void)
3472 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3473 ps = wpa_ctrl_get_remote_ifname(ctrl_conn);
3474 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3476 home = getenv("HOME");
3478 const char *fname = ".wpa_cli_history";
3479 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
3480 hfile = os_malloc(hfile_len);
3482 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
3485 if (edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
3486 wpa_cli_edit_completion_cb, NULL, hfile, ps) < 0) {
3492 eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
3496 static void update_bssid_list(struct wpa_ctrl *ctrl)
3499 size_t len = sizeof(buf);
3501 char *cmd = "BSS RANGE=ALL MASK=0x2";
3506 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
3513 pos = os_strstr(pos, "bssid=");
3517 end = os_strchr(pos, '\n');
3521 cli_txt_list_add(&bsses, pos);
3527 static void update_ifnames(struct wpa_ctrl *ctrl)
3530 size_t len = sizeof(buf);
3532 char *cmd = "INTERFACES";
3536 cli_txt_list_flush(&ifnames);
3540 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
3547 end = os_strchr(pos, '\n');
3551 ret = os_snprintf(txt, sizeof(txt), "ifname=%s", pos);
3552 if (ret > 0 && ret < (int) sizeof(txt))
3553 cli_txt_list_add(&ifnames, txt);
3559 static void try_connection(void *eloop_ctx, void *timeout_ctx)
3564 if (ctrl_ifname == NULL)
3565 ctrl_ifname = wpa_cli_get_default_ifname();
3567 if (!wpa_cli_open_connection(ctrl_ifname, 1) == 0) {
3568 if (!warning_displayed) {
3569 printf("Could not connect to wpa_supplicant: "
3570 "%s - re-trying\n", ctrl_ifname);
3571 warning_displayed = 1;
3573 eloop_register_timeout(1, 0, try_connection, NULL, NULL);
3577 update_bssid_list(ctrl_conn);
3579 if (warning_displayed)
3580 printf("Connection established.\n");
3587 static void wpa_cli_interactive(void)
3589 printf("\nInteractive mode\n\n");
3591 eloop_register_timeout(0, 0, try_connection, NULL, NULL);
3593 eloop_cancel_timeout(try_connection, NULL, NULL);
3595 cli_txt_list_flush(&p2p_peers);
3596 cli_txt_list_flush(&p2p_groups);
3597 cli_txt_list_flush(&bsses);
3598 cli_txt_list_flush(&ifnames);
3600 edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
3602 eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
3603 wpa_cli_close_connection();
3607 static void wpa_cli_action(struct wpa_ctrl *ctrl)
3609 #ifdef CONFIG_ANSI_C_EXTRA
3610 /* TODO: ANSI C version(?) */
3611 printf("Action processing not supported in ANSI C build.\n");
3612 #else /* CONFIG_ANSI_C_EXTRA */
3616 char buf[256]; /* note: large enough to fit in unsolicited messages */
3619 fd = wpa_ctrl_get_fd(ctrl);
3621 while (!wpa_cli_quit) {
3624 tv.tv_sec = ping_interval;
3626 res = select(fd + 1, &rfds, NULL, NULL, &tv);
3627 if (res < 0 && errno != EINTR) {
3632 if (FD_ISSET(fd, &rfds))
3633 wpa_cli_recv_pending(ctrl, 1);
3635 /* verify that connection is still working */
3636 len = sizeof(buf) - 1;
3637 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
3638 wpa_cli_action_cb) < 0 ||
3639 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
3640 printf("wpa_supplicant did not reply to PING "
3641 "command - exiting\n");
3646 #endif /* CONFIG_ANSI_C_EXTRA */
3650 static void wpa_cli_cleanup(void)
3652 wpa_cli_close_connection();
3654 os_daemonize_terminate(pid_file);
3656 os_program_deinit();
3660 static void wpa_cli_terminate(int sig, void *ctx)
3666 static char * wpa_cli_get_default_ifname(void)
3668 char *ifname = NULL;
3670 #ifdef CONFIG_CTRL_IFACE_UNIX
3671 struct dirent *dent;
3672 DIR *dir = opendir(ctrl_iface_dir);
3675 char ifprop[PROPERTY_VALUE_MAX];
3676 if (property_get("wifi.interface", ifprop, NULL) != 0) {
3677 ifname = os_strdup(ifprop);
3678 printf("Using interface '%s'\n", ifname);
3681 #endif /* ANDROID */
3684 while ((dent = readdir(dir))) {
3685 #ifdef _DIRENT_HAVE_D_TYPE
3687 * Skip the file if it is not a socket. Also accept
3688 * DT_UNKNOWN (0) in case the C library or underlying
3689 * file system does not support d_type.
3691 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
3693 #endif /* _DIRENT_HAVE_D_TYPE */
3694 if (os_strcmp(dent->d_name, ".") == 0 ||
3695 os_strcmp(dent->d_name, "..") == 0)
3697 printf("Selected interface '%s'\n", dent->d_name);
3698 ifname = os_strdup(dent->d_name);
3702 #endif /* CONFIG_CTRL_IFACE_UNIX */
3704 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
3705 char buf[4096], *pos;
3707 struct wpa_ctrl *ctrl;
3710 ctrl = wpa_ctrl_open(NULL);
3714 len = sizeof(buf) - 1;
3715 ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
3718 pos = os_strchr(buf, '\n');
3721 ifname = os_strdup(buf);
3723 wpa_ctrl_close(ctrl);
3724 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3730 int main(int argc, char *argv[])
3735 const char *global = NULL;
3737 if (os_program_init())
3741 c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
3746 action_file = optarg;
3755 ping_interval = atoi(optarg);
3761 printf("%s\n", wpa_cli_version);
3764 os_free(ctrl_ifname);
3765 ctrl_ifname = os_strdup(optarg);
3768 ctrl_iface_dir = optarg;
3779 interactive = (argc == optind) && (action_file == NULL);
3782 printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
3788 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
3789 ctrl_conn = wpa_ctrl_open(NULL);
3790 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3791 ctrl_conn = wpa_ctrl_open(global);
3792 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3793 if (ctrl_conn == NULL) {
3794 fprintf(stderr, "Failed to connect to wpa_supplicant "
3795 "global interface: %s error: %s\n",
3796 global, strerror(errno));
3801 update_ifnames(ctrl_conn);
3802 mon_conn = wpa_ctrl_open(global);
3804 if (wpa_ctrl_attach(mon_conn) == 0) {
3805 wpa_cli_attached = 1;
3806 eloop_register_read_sock(
3807 wpa_ctrl_get_fd(mon_conn),
3808 wpa_cli_mon_receive,
3811 printf("Failed to open monitor "
3812 "connection through global "
3813 "control interface\n");
3819 eloop_register_signal_terminate(wpa_cli_terminate, NULL);
3821 if (ctrl_ifname == NULL)
3822 ctrl_ifname = wpa_cli_get_default_ifname();
3825 wpa_cli_interactive();
3828 wpa_cli_open_connection(ctrl_ifname, 0) < 0) {
3829 fprintf(stderr, "Failed to connect to non-global "
3830 "ctrl_ifname: %s error: %s\n",
3831 ctrl_ifname, strerror(errno));
3836 if (wpa_ctrl_attach(ctrl_conn) == 0) {
3837 wpa_cli_attached = 1;
3839 printf("Warning: Failed to attach to "
3840 "wpa_supplicant.\n");
3845 if (daemonize && os_daemonize(pid_file))
3849 wpa_cli_action(ctrl_conn);
3851 ret = wpa_request(ctrl_conn, argc - optind,
3855 os_free(ctrl_ifname);
3862 #else /* CONFIG_CTRL_IFACE */
3863 int main(int argc, char *argv[])
3865 printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
3868 #endif /* CONFIG_CTRL_IFACE */