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-2013, 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 = 0;
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 return wpa_ctrl_command(ctrl, "STATUS");
503 static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
505 return wpa_ctrl_command(ctrl, "PING");
509 static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
511 return wpa_ctrl_command(ctrl, "RELOG");
515 static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[])
517 return wpa_cli_cmd(ctrl, "NOTE", 1, argc, argv);
521 static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
523 return wpa_ctrl_command(ctrl, "MIB");
527 static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
529 return wpa_ctrl_command(ctrl, "PMKSA");
533 static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
535 print_help(argc > 0 ? argv[0] : NULL);
540 static char ** wpa_cli_complete_help(const char *str, int pos)
542 int arg = get_cmd_arg_num(str, pos);
547 res = wpa_list_cmd_list();
555 static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
557 printf("%s\n\n%s\n", wpa_cli_version, wpa_cli_full_license);
562 static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
571 static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
577 res = os_snprintf(cmd, sizeof(cmd), "SET %s ", argv[0]);
578 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
579 printf("Too long SET command.\n");
582 return wpa_ctrl_command(ctrl, cmd);
585 return wpa_cli_cmd(ctrl, "SET", 2, argc, argv);
589 static char ** wpa_cli_complete_set(const char *str, int pos)
591 int arg = get_cmd_arg_num(str, pos);
592 const char *fields[] = {
594 "EAPOL::heldPeriod", "EAPOL::authPeriod", "EAPOL::startPeriod",
595 "EAPOL::maxStart", "dot11RSNAConfigPMKLifetime",
596 "dot11RSNAConfigPMKReauthThreshold", "dot11RSNAConfigSATimeout",
597 "wps_fragment_size", "wps_version_number", "ampdu",
598 "tdls_testing", "tdls_disabled", "pno", "radio_disabled",
599 "uapsd", "ps", "wifi_display", "bssid_filter", "disallow_aps",
601 /* global configuration parameters */
602 "eapol_version", "ap_scan", "disable_scan_offload",
603 "fast_reauth", "opensc_engine_path", "pkcs11_engine_path",
604 "pkcs11_module_path", "pcsc_reader", "pcsc_pin",
605 "driver_param", "dot11RSNAConfigPMKLifetime",
606 "dot11RSNAConfigPMKReauthThreshold",
607 "dot11RSNAConfigSATimeout",
608 "update_config", "load_dynamic_eap", "uuid", "device_name",
609 "manufacturer", "model_name", "model_number", "serial_number",
610 "device_type", "os_version", "config_methods",
611 "wps_cred_processing", "wps_vendor_ext_m1", "sec_device_type",
612 "p2p_listen_reg_class", "p2p_listen_channel",
613 "p2p_oper_reg_class", "p2p_oper_channel",
614 "p2p_go_intent", "p2p_ssid_postfix", "persistent_reconnect",
615 "p2p_intra_bss", "p2p_group_idle", "p2p_pref_chan",
616 "p2p_go_ht40", "p2p_disabled", "p2p_no_group_iface",
617 "p2p_ignore_shared_freq", "country", "bss_max_count",
618 "bss_expiration_age", "bss_expiration_scan_count",
619 "filter_ssids", "filter_rssi", "max_num_sta",
620 "disassoc_low_ack", "hs20", "interworking", "hessid",
621 "access_network_type", "pbc_in_m1", "autoscan",
622 "wps_nfc_dev_pw_id", "wps_nfc_dh_pubkey", "wps_nfc_dh_privkey",
623 "wps_nfc_dev_pw", "ext_password_backend",
624 "p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
625 "sae_groups", "dtim_period", "beacon_int", "ap_vendor_elements",
626 "ignore_old_scan_res", "freq_list"
628 int i, num_fields = sizeof(fields) / sizeof(fields[0]);
631 char **res = os_calloc(num_fields + 1, sizeof(char *));
634 for (i = 0; i < num_fields; i++) {
635 res[i] = os_strdup(fields[i]);
642 if (arg > 1 && os_strncasecmp(str, "set bssid_filter ", 17) == 0)
643 return cli_txt_list_array(&bsses);
649 static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
651 return wpa_cli_cmd(ctrl, "GET", 1, argc, argv);
655 static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
657 return wpa_ctrl_command(ctrl, "LOGOFF");
661 static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
663 return wpa_ctrl_command(ctrl, "LOGON");
667 static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
670 return wpa_ctrl_command(ctrl, "REASSOCIATE");
674 static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
677 return wpa_cli_cmd(ctrl, "PREAUTH", 1, argc, argv);
681 static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
683 return wpa_cli_cmd(ctrl, "AP_SCAN", 1, argc, argv);
687 static int wpa_cli_cmd_scan_interval(struct wpa_ctrl *ctrl, int argc,
690 return wpa_cli_cmd(ctrl, "SCAN_INTERVAL", 1, argc, argv);
694 static int wpa_cli_cmd_bss_expire_age(struct wpa_ctrl *ctrl, int argc,
697 return wpa_cli_cmd(ctrl, "BSS_EXPIRE_AGE", 1, argc, argv);
701 static int wpa_cli_cmd_bss_expire_count(struct wpa_ctrl *ctrl, int argc,
704 return wpa_cli_cmd(ctrl, "BSS_EXPIRE_COUNT", 1, argc, argv);
708 static int wpa_cli_cmd_bss_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
714 res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH 0");
716 res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH %s", argv[0]);
717 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
718 printf("Too long BSS_FLUSH command.\n");
721 return wpa_ctrl_command(ctrl, cmd);
725 static int wpa_cli_cmd_stkstart(struct wpa_ctrl *ctrl, int argc,
728 return wpa_cli_cmd(ctrl, "STKSTART", 1, argc, argv);
732 static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
734 return wpa_cli_cmd(ctrl, "FT_DS", 1, argc, argv);
738 static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
740 return wpa_cli_cmd(ctrl, "WPS_PBC", 0, argc, argv);
744 static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
747 printf("Invalid WPS_PIN command: need one or two arguments:\n"
748 "- BSSID: use 'any' to select any\n"
749 "- PIN: optional, used only with devices that have no "
754 return wpa_cli_cmd(ctrl, "WPS_PIN", 1, argc, argv);
758 static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
761 return wpa_cli_cmd(ctrl, "WPS_CHECK_PIN", 1, argc, argv);
765 static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
768 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
772 #ifdef CONFIG_WPS_NFC
774 static int wpa_cli_cmd_wps_nfc(struct wpa_ctrl *ctrl, int argc, char *argv[])
776 return wpa_cli_cmd(ctrl, "WPS_NFC", 0, argc, argv);
780 static int wpa_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
783 return wpa_cli_cmd(ctrl, "WPS_NFC_CONFIG_TOKEN", 1, argc, argv);
787 static int wpa_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, int argc,
790 return wpa_cli_cmd(ctrl, "WPS_NFC_TOKEN", 1, argc, argv);
794 static int wpa_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
802 printf("Invalid 'wps_nfc_tag_read' command - one argument "
807 buflen = 18 + os_strlen(argv[0]);
808 buf = os_malloc(buflen);
811 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
813 ret = wpa_ctrl_command(ctrl, buf);
820 static int wpa_cli_cmd_nfc_get_handover_req(struct wpa_ctrl *ctrl, int argc,
823 return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_REQ", 2, argc, argv);
827 static int wpa_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, int argc,
830 return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_SEL", 2, argc, argv);
834 static int wpa_cli_cmd_nfc_rx_handover_req(struct wpa_ctrl *ctrl, int argc,
842 printf("Invalid 'nfc_rx_handover_req' command - one argument "
847 buflen = 21 + os_strlen(argv[0]);
848 buf = os_malloc(buflen);
851 os_snprintf(buf, buflen, "NFC_RX_HANDOVER_REQ %s", argv[0]);
853 ret = wpa_ctrl_command(ctrl, buf);
860 static int wpa_cli_cmd_nfc_rx_handover_sel(struct wpa_ctrl *ctrl, int argc,
868 printf("Invalid 'nfc_rx_handover_sel' command - one argument "
873 buflen = 21 + os_strlen(argv[0]);
874 buf = os_malloc(buflen);
877 os_snprintf(buf, buflen, "NFC_RX_HANDOVER_SEL %s", argv[0]);
879 ret = wpa_ctrl_command(ctrl, buf);
886 static int wpa_cli_cmd_nfc_report_handover(struct wpa_ctrl *ctrl, int argc,
889 return wpa_cli_cmd(ctrl, "NFC_REPORT_HANDOVER", 4, argc, argv);
892 #endif /* CONFIG_WPS_NFC */
895 static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
901 res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
903 else if (argc == 5 || argc == 6) {
904 char ssid_hex[2 * 32 + 1];
905 char key_hex[2 * 64 + 1];
909 for (i = 0; i < 32; i++) {
910 if (argv[2][i] == '\0')
912 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
917 for (i = 0; i < 64; i++) {
918 if (argv[5][i] == '\0')
920 os_snprintf(&key_hex[i * 2], 3, "%02x",
925 res = os_snprintf(cmd, sizeof(cmd),
926 "WPS_REG %s %s %s %s %s %s",
927 argv[0], argv[1], ssid_hex, argv[3], argv[4],
930 printf("Invalid WPS_REG command: need two arguments:\n"
931 "- BSSID of the target AP\n"
933 printf("Alternatively, six arguments can be used to "
934 "reconfigure the AP:\n"
935 "- BSSID of the target AP\n"
938 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
939 "- new encr (NONE, WEP, TKIP, CCMP)\n"
944 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
945 printf("Too long WPS_REG command.\n");
948 return wpa_ctrl_command(ctrl, cmd);
952 static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
955 return wpa_cli_cmd(ctrl, "WPS_AP_PIN", 1, argc, argv);
959 static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc,
962 return wpa_cli_cmd(ctrl, "WPS_ER_START", 0, argc, argv);
966 static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc,
969 return wpa_ctrl_command(ctrl, "WPS_ER_STOP");
974 static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc,
978 printf("Invalid WPS_ER_PIN command: need at least two "
980 "- UUID: use 'any' to select any\n"
981 "- PIN: Enrollee PIN\n"
982 "optional: - Enrollee MAC address\n");
986 return wpa_cli_cmd(ctrl, "WPS_ER_PIN", 2, argc, argv);
990 static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc,
993 return wpa_cli_cmd(ctrl, "WPS_ER_PBC", 1, argc, argv);
997 static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc,
1001 printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
1002 "- UUID: specify which AP to use\n"
1007 return wpa_cli_cmd(ctrl, "WPS_ER_LEARN", 2, argc, argv);
1011 static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc,
1015 printf("Invalid WPS_ER_SET_CONFIG command: need two "
1017 "- UUID: specify which AP to use\n"
1018 "- Network configuration id\n");
1022 return wpa_cli_cmd(ctrl, "WPS_ER_SET_CONFIG", 2, argc, argv);
1026 static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
1032 if (argc == 5 || argc == 6) {
1033 char ssid_hex[2 * 32 + 1];
1034 char key_hex[2 * 64 + 1];
1038 for (i = 0; i < 32; i++) {
1039 if (argv[2][i] == '\0')
1041 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
1046 for (i = 0; i < 64; i++) {
1047 if (argv[5][i] == '\0')
1049 os_snprintf(&key_hex[i * 2], 3, "%02x",
1054 res = os_snprintf(cmd, sizeof(cmd),
1055 "WPS_ER_CONFIG %s %s %s %s %s %s",
1056 argv[0], argv[1], ssid_hex, argv[3], argv[4],
1059 printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
1063 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
1064 "- new encr (NONE, WEP, TKIP, CCMP)\n"
1069 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
1070 printf("Too long WPS_ER_CONFIG command.\n");
1073 return wpa_ctrl_command(ctrl, cmd);
1077 #ifdef CONFIG_WPS_NFC
1078 static int wpa_cli_cmd_wps_er_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
1082 printf("Invalid WPS_ER_NFC_CONFIG_TOKEN command: need two "
1084 "- WPS/NDEF: token format\n"
1085 "- UUID: specify which AP to use\n");
1089 return wpa_cli_cmd(ctrl, "WPS_ER_NFC_CONFIG_TOKEN", 2, argc, argv);
1091 #endif /* CONFIG_WPS_NFC */
1094 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
1096 return wpa_cli_cmd(ctrl, "IBSS_RSN", 1, argc, argv);
1100 static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1102 return wpa_cli_cmd(ctrl, "LEVEL", 1, argc, argv);
1106 static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
1108 char cmd[256], *pos, *end;
1112 printf("Invalid IDENTITY command: needs two arguments "
1113 "(network id and identity)\n");
1117 end = cmd + sizeof(cmd);
1119 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
1121 if (ret < 0 || ret >= end - pos) {
1122 printf("Too long IDENTITY command.\n");
1126 for (i = 2; i < argc; i++) {
1127 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1128 if (ret < 0 || ret >= end - pos) {
1129 printf("Too long IDENTITY command.\n");
1135 return wpa_ctrl_command(ctrl, cmd);
1139 static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
1141 char cmd[256], *pos, *end;
1145 printf("Invalid PASSWORD command: needs two arguments "
1146 "(network id and password)\n");
1150 end = cmd + sizeof(cmd);
1152 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
1154 if (ret < 0 || ret >= end - pos) {
1155 printf("Too long PASSWORD command.\n");
1159 for (i = 2; i < argc; i++) {
1160 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1161 if (ret < 0 || ret >= end - pos) {
1162 printf("Too long PASSWORD command.\n");
1168 return wpa_ctrl_command(ctrl, cmd);
1172 static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
1175 char cmd[256], *pos, *end;
1179 printf("Invalid NEW_PASSWORD command: needs two arguments "
1180 "(network id and password)\n");
1184 end = cmd + sizeof(cmd);
1186 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
1188 if (ret < 0 || ret >= end - pos) {
1189 printf("Too long NEW_PASSWORD command.\n");
1193 for (i = 2; i < argc; i++) {
1194 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1195 if (ret < 0 || ret >= end - pos) {
1196 printf("Too long NEW_PASSWORD command.\n");
1202 return wpa_ctrl_command(ctrl, cmd);
1206 static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
1208 char cmd[256], *pos, *end;
1212 printf("Invalid PIN command: needs two arguments "
1213 "(network id and pin)\n");
1217 end = cmd + sizeof(cmd);
1219 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
1221 if (ret < 0 || ret >= end - pos) {
1222 printf("Too long PIN command.\n");
1226 for (i = 2; i < argc; i++) {
1227 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1228 if (ret < 0 || ret >= end - pos) {
1229 printf("Too long PIN command.\n");
1234 return wpa_ctrl_command(ctrl, cmd);
1238 static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
1240 char cmd[256], *pos, *end;
1244 printf("Invalid OTP command: needs two arguments (network "
1245 "id and password)\n");
1249 end = cmd + sizeof(cmd);
1251 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
1253 if (ret < 0 || ret >= end - pos) {
1254 printf("Too long OTP command.\n");
1258 for (i = 2; i < argc; i++) {
1259 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1260 if (ret < 0 || ret >= end - pos) {
1261 printf("Too long OTP command.\n");
1267 return wpa_ctrl_command(ctrl, cmd);
1271 static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
1274 char cmd[256], *pos, *end;
1278 printf("Invalid PASSPHRASE command: needs two arguments "
1279 "(network id and passphrase)\n");
1283 end = cmd + sizeof(cmd);
1285 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
1287 if (ret < 0 || ret >= end - pos) {
1288 printf("Too long PASSPHRASE command.\n");
1292 for (i = 2; i < argc; i++) {
1293 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1294 if (ret < 0 || ret >= end - pos) {
1295 printf("Too long PASSPHRASE command.\n");
1301 return wpa_ctrl_command(ctrl, cmd);
1305 static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
1308 printf("Invalid BSSID command: needs two arguments (network "
1313 return wpa_cli_cmd(ctrl, "BSSID", 2, argc, argv);
1317 static int wpa_cli_cmd_blacklist(struct wpa_ctrl *ctrl, int argc, char *argv[])
1319 return wpa_cli_cmd(ctrl, "BLACKLIST", 0, argc, argv);
1323 static int wpa_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1325 return wpa_cli_cmd(ctrl, "LOG_LEVEL", 0, argc, argv);
1329 static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
1332 return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
1336 static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
1339 return wpa_cli_cmd(ctrl, "SELECT_NETWORK", 1, argc, argv);
1343 static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
1346 return wpa_cli_cmd(ctrl, "ENABLE_NETWORK", 1, argc, argv);
1350 static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
1353 return wpa_cli_cmd(ctrl, "DISABLE_NETWORK", 1, argc, argv);
1357 static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
1360 return wpa_ctrl_command(ctrl, "ADD_NETWORK");
1364 static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
1367 return wpa_cli_cmd(ctrl, "REMOVE_NETWORK", 1, argc, argv);
1371 static void wpa_cli_show_network_variables(void)
1373 printf("set_network variables:\n"
1374 " ssid (network name, SSID)\n"
1375 " psk (WPA passphrase or pre-shared key)\n"
1376 " key_mgmt (key management protocol)\n"
1377 " identity (EAP identity)\n"
1378 " password (EAP password)\n"
1381 "Note: Values are entered in the same format as the "
1382 "configuration file is using,\n"
1383 "i.e., strings values need to be inside double quotation "
1385 "For example: set_network 1 ssid \"network name\"\n"
1387 "Please see wpa_supplicant.conf documentation for full list "
1388 "of\navailable variables.\n");
1392 static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
1396 wpa_cli_show_network_variables();
1401 printf("Invalid SET_NETWORK command: needs three arguments\n"
1402 "(network id, variable name, and value)\n");
1406 return wpa_cli_cmd(ctrl, "SET_NETWORK", 3, argc, argv);
1410 static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
1414 wpa_cli_show_network_variables();
1419 printf("Invalid GET_NETWORK command: needs two arguments\n"
1420 "(network id and variable name)\n");
1424 return wpa_cli_cmd(ctrl, "GET_NETWORK", 2, argc, argv);
1428 static int wpa_cli_cmd_list_creds(struct wpa_ctrl *ctrl, int argc,
1431 return wpa_ctrl_command(ctrl, "LIST_CREDS");
1435 static int wpa_cli_cmd_add_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1437 return wpa_ctrl_command(ctrl, "ADD_CRED");
1441 static int wpa_cli_cmd_remove_cred(struct wpa_ctrl *ctrl, int argc,
1444 return wpa_cli_cmd(ctrl, "REMOVE_CRED", 1, argc, argv);
1448 static int wpa_cli_cmd_set_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1451 printf("Invalid SET_CRED command: needs three arguments\n"
1452 "(cred id, variable name, and value)\n");
1456 return wpa_cli_cmd(ctrl, "SET_CRED", 3, argc, argv);
1460 static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
1463 return wpa_ctrl_command(ctrl, "DISCONNECT");
1467 static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
1470 return wpa_ctrl_command(ctrl, "RECONNECT");
1474 static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
1477 return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
1481 static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
1483 return wpa_cli_cmd(ctrl, "SCAN", 0, argc, argv);
1487 static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
1490 return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
1494 static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
1496 return wpa_cli_cmd(ctrl, "BSS", 1, argc, argv);
1500 static char ** wpa_cli_complete_bss(const char *str, int pos)
1502 int arg = get_cmd_arg_num(str, pos);
1507 res = cli_txt_list_array(&bsses);
1515 static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
1518 if (argc < 1 || argc > 2) {
1519 printf("Invalid GET_CAPABILITY command: need either one or "
1524 if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
1525 printf("Invalid GET_CAPABILITY command: second argument, "
1526 "if any, must be 'strict'\n");
1530 return wpa_cli_cmd(ctrl, "GET_CAPABILITY", 1, argc, argv);
1534 static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
1536 printf("Available interfaces:\n");
1537 return wpa_ctrl_command(ctrl, "INTERFACES");
1541 static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
1544 wpa_cli_list_interfaces(ctrl);
1548 wpa_cli_close_connection();
1549 os_free(ctrl_ifname);
1550 ctrl_ifname = os_strdup(argv[0]);
1552 if (wpa_cli_open_connection(ctrl_ifname, 1)) {
1553 printf("Connected to interface '%s.\n", ctrl_ifname);
1555 printf("Could not connect to interface '%s' - re-trying\n",
1562 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
1565 return wpa_ctrl_command(ctrl, "RECONFIGURE");
1569 static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
1572 return wpa_ctrl_command(ctrl, "TERMINATE");
1576 static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
1583 printf("Invalid INTERFACE_ADD command: needs at least one "
1584 "argument (interface name)\n"
1585 "All arguments: ifname confname driver ctrl_interface "
1586 "driver_param bridge_name\n");
1591 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1592 * <driver_param>TAB<bridge_name>
1594 res = os_snprintf(cmd, sizeof(cmd),
1595 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
1597 argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
1598 argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
1599 argc > 5 ? argv[5] : "");
1600 if (res < 0 || (size_t) res >= sizeof(cmd))
1602 cmd[sizeof(cmd) - 1] = '\0';
1603 return wpa_ctrl_command(ctrl, cmd);
1607 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
1610 return wpa_cli_cmd(ctrl, "INTERFACE_REMOVE", 1, argc, argv);
1614 static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
1617 return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
1622 static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1624 return wpa_cli_cmd(ctrl, "STA", 1, argc, argv);
1628 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
1629 char *addr, size_t addr_len)
1631 char buf[4096], *pos;
1635 if (ctrl_conn == NULL) {
1636 printf("Not connected to hostapd - command dropped.\n");
1639 len = sizeof(buf) - 1;
1640 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
1643 printf("'%s' command timed out.\n", cmd);
1645 } else if (ret < 0) {
1646 printf("'%s' command failed.\n", cmd);
1651 if (os_memcmp(buf, "FAIL", 4) == 0)
1656 while (*pos != '\0' && *pos != '\n')
1659 os_strlcpy(addr, buf, addr_len);
1664 static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1666 char addr[32], cmd[64];
1668 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
1671 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
1672 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
1678 static int wpa_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
1681 return wpa_cli_cmd(ctrl, "DEAUTHENTICATE", 1, argc, argv);
1685 static int wpa_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
1688 return wpa_cli_cmd(ctrl, "DISASSOCIATE", 1, argc, argv);
1690 #endif /* CONFIG_AP */
1693 static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[])
1695 return wpa_ctrl_command(ctrl, "SUSPEND");
1699 static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[])
1701 return wpa_ctrl_command(ctrl, "RESUME");
1705 static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1707 return wpa_ctrl_command(ctrl, "DROP_SA");
1711 static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[])
1713 return wpa_cli_cmd(ctrl, "ROAM", 1, argc, argv);
1719 static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
1721 return wpa_cli_cmd(ctrl, "P2P_FIND", 0, argc, argv);
1725 static char ** wpa_cli_complete_p2p_find(const char *str, int pos)
1728 int arg = get_cmd_arg_num(str, pos);
1730 res = os_calloc(6, sizeof(char *));
1733 res[0] = os_strdup("type=social");
1734 if (res[0] == NULL) {
1738 res[1] = os_strdup("type=progressive");
1741 res[2] = os_strdup("delay=");
1744 res[3] = os_strdup("dev_id=");
1748 res[4] = os_strdup("[timeout]");
1754 static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc,
1757 return wpa_ctrl_command(ctrl, "P2P_STOP_FIND");
1761 static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc,
1764 return wpa_cli_cmd(ctrl, "P2P_CONNECT", 2, argc, argv);
1768 static char ** wpa_cli_complete_p2p_connect(const char *str, int pos)
1770 int arg = get_cmd_arg_num(str, pos);
1775 res = cli_txt_list_array(&p2p_peers);
1783 static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc,
1786 return wpa_cli_cmd(ctrl, "P2P_LISTEN", 0, argc, argv);
1790 static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc,
1793 return wpa_cli_cmd(ctrl, "P2P_GROUP_REMOVE", 1, argc, argv);
1797 static char ** wpa_cli_complete_p2p_group_remove(const char *str, int pos)
1799 int arg = get_cmd_arg_num(str, pos);
1804 res = cli_txt_list_array(&p2p_groups);
1812 static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc,
1815 return wpa_cli_cmd(ctrl, "P2P_GROUP_ADD", 0, argc, argv);
1819 static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc,
1822 if (argc != 2 && argc != 3) {
1823 printf("Invalid P2P_PROV_DISC command: needs at least "
1824 "two arguments, address and config method\n"
1825 "(display, keypad, or pbc) and an optional join\n");
1829 return wpa_cli_cmd(ctrl, "P2P_PROV_DISC", 2, argc, argv);
1833 static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc,
1836 return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE");
1840 static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc,
1845 if (argc != 2 && argc != 4) {
1846 printf("Invalid P2P_SERV_DISC_REQ command: needs two "
1847 "arguments (address and TLVs) or four arguments "
1848 "(address, \"upnp\", version, search target "
1853 if (write_cmd(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ", argc, argv) < 0)
1855 return wpa_ctrl_command(ctrl, cmd);
1859 static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl,
1860 int argc, char *argv[])
1862 return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_CANCEL_REQ", 1, argc, argv);
1866 static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc,
1873 printf("Invalid P2P_SERV_DISC_RESP command: needs four "
1874 "arguments (freq, address, dialog token, and TLVs)\n");
1878 res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s",
1879 argv[0], argv[1], argv[2], argv[3]);
1880 if (res < 0 || (size_t) res >= sizeof(cmd))
1882 cmd[sizeof(cmd) - 1] = '\0';
1883 return wpa_ctrl_command(ctrl, cmd);
1887 static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc,
1890 return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE");
1894 static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl,
1895 int argc, char *argv[])
1897 return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_EXTERNAL", 1, argc, argv);
1901 static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc,
1904 return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH");
1908 static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc,
1914 if (argc != 3 && argc != 4) {
1915 printf("Invalid P2P_SERVICE_ADD command: needs three or four "
1921 res = os_snprintf(cmd, sizeof(cmd),
1922 "P2P_SERVICE_ADD %s %s %s %s",
1923 argv[0], argv[1], argv[2], argv[3]);
1925 res = os_snprintf(cmd, sizeof(cmd),
1926 "P2P_SERVICE_ADD %s %s %s",
1927 argv[0], argv[1], argv[2]);
1928 if (res < 0 || (size_t) res >= sizeof(cmd))
1930 cmd[sizeof(cmd) - 1] = '\0';
1931 return wpa_ctrl_command(ctrl, cmd);
1935 static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc,
1941 if (argc != 2 && argc != 3) {
1942 printf("Invalid P2P_SERVICE_DEL command: needs two or three "
1948 res = os_snprintf(cmd, sizeof(cmd),
1949 "P2P_SERVICE_DEL %s %s %s",
1950 argv[0], argv[1], argv[2]);
1952 res = os_snprintf(cmd, sizeof(cmd),
1953 "P2P_SERVICE_DEL %s %s",
1955 if (res < 0 || (size_t) res >= sizeof(cmd))
1957 cmd[sizeof(cmd) - 1] = '\0';
1958 return wpa_ctrl_command(ctrl, cmd);
1962 static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl,
1963 int argc, char *argv[])
1965 return wpa_cli_cmd(ctrl, "P2P_REJECT", 1, argc, argv);
1969 static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl,
1970 int argc, char *argv[])
1972 return wpa_cli_cmd(ctrl, "P2P_INVITE", 1, argc, argv);
1976 static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[])
1978 return wpa_cli_cmd(ctrl, "P2P_PEER", 1, argc, argv);
1982 static char ** wpa_cli_complete_p2p_peer(const char *str, int pos)
1984 int arg = get_cmd_arg_num(str, pos);
1989 res = cli_txt_list_array(&p2p_peers);
1997 static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, char *cmd,
1998 char *addr, size_t addr_len,
2001 char buf[4096], *pos;
2005 if (ctrl_conn == NULL)
2007 len = sizeof(buf) - 1;
2008 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
2011 printf("'%s' command timed out.\n", cmd);
2013 } else if (ret < 0) {
2014 printf("'%s' command failed.\n", cmd);
2019 if (os_memcmp(buf, "FAIL", 4) == 0)
2023 while (*pos != '\0' && *pos != '\n')
2026 os_strlcpy(addr, buf, addr_len);
2027 if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL)
2028 printf("%s\n", addr);
2033 static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[])
2035 char addr[32], cmd[64];
2038 discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0;
2040 if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST",
2041 addr, sizeof(addr), discovered))
2044 os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr);
2045 } while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr),
2052 static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
2054 return wpa_cli_cmd(ctrl, "P2P_SET", 2, argc, argv);
2058 static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2060 return wpa_ctrl_command(ctrl, "P2P_FLUSH");
2064 static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc,
2067 return wpa_ctrl_command(ctrl, "P2P_CANCEL");
2071 static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc,
2074 return wpa_cli_cmd(ctrl, "P2P_UNAUTHORIZE", 1, argc, argv);
2078 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");
2089 return wpa_cli_cmd(ctrl, "P2P_PRESENCE_REQ", 0, argc, argv);
2093 static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc,
2096 if (argc != 0 && argc != 2) {
2097 printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
2098 "(availability period, availability interval; in "
2100 "Extended Listen Timing can be cancelled with this "
2101 "command when used without parameters.\n");
2105 return wpa_cli_cmd(ctrl, "P2P_EXT_LISTEN", 0, argc, argv);
2108 #endif /* CONFIG_P2P */
2110 #ifdef CONFIG_WIFI_DISPLAY
2112 static int wpa_cli_cmd_wfd_subelem_set(struct wpa_ctrl *ctrl, int argc,
2118 if (argc != 1 && argc != 2) {
2119 printf("Invalid WFD_SUBELEM_SET command: needs one or two "
2120 "arguments (subelem, hexdump)\n");
2124 res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_SET %s %s",
2125 argv[0], argc > 1 ? argv[1] : "");
2126 if (res < 0 || (size_t) res >= sizeof(cmd))
2128 cmd[sizeof(cmd) - 1] = '\0';
2129 return wpa_ctrl_command(ctrl, cmd);
2133 static int wpa_cli_cmd_wfd_subelem_get(struct wpa_ctrl *ctrl, int argc,
2140 printf("Invalid WFD_SUBELEM_GET command: needs one "
2141 "argument (subelem)\n");
2145 res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_GET %s",
2147 if (res < 0 || (size_t) res >= sizeof(cmd))
2149 cmd[sizeof(cmd) - 1] = '\0';
2150 return wpa_ctrl_command(ctrl, cmd);
2152 #endif /* CONFIG_WIFI_DISPLAY */
2155 #ifdef CONFIG_INTERWORKING
2156 static int wpa_cli_cmd_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2159 return wpa_ctrl_command(ctrl, "FETCH_ANQP");
2163 static int wpa_cli_cmd_stop_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2166 return wpa_ctrl_command(ctrl, "STOP_FETCH_ANQP");
2170 static int wpa_cli_cmd_interworking_select(struct wpa_ctrl *ctrl, int argc,
2173 return wpa_cli_cmd(ctrl, "INTERWORKING_SELECT", 0, argc, argv);
2177 static int wpa_cli_cmd_interworking_connect(struct wpa_ctrl *ctrl, int argc,
2180 return wpa_cli_cmd(ctrl, "INTERWORKING_CONNECT", 1, argc, argv);
2184 static int wpa_cli_cmd_anqp_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
2186 return wpa_cli_cmd(ctrl, "ANQP_GET", 2, argc, argv);
2190 static int wpa_cli_cmd_gas_request(struct wpa_ctrl *ctrl, int argc,
2193 return wpa_cli_cmd(ctrl, "GAS_REQUEST", 2, argc, argv);
2197 static int wpa_cli_cmd_gas_response_get(struct wpa_ctrl *ctrl, int argc,
2200 return wpa_cli_cmd(ctrl, "GAS_RESPONSE_GET", 2, argc, argv);
2202 #endif /* CONFIG_INTERWORKING */
2207 static int wpa_cli_cmd_hs20_anqp_get(struct wpa_ctrl *ctrl, int argc,
2210 return wpa_cli_cmd(ctrl, "HS20_ANQP_GET", 2, argc, argv);
2214 static int wpa_cli_cmd_get_nai_home_realm_list(struct wpa_ctrl *ctrl, int argc,
2220 printf("Command needs one or two arguments (dst mac addr and "
2221 "optional home realm)\n");
2225 if (write_cmd(cmd, sizeof(cmd), "HS20_GET_NAI_HOME_REALM_LIST",
2229 return wpa_ctrl_command(ctrl, cmd);
2232 #endif /* CONFIG_HS20 */
2235 static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc,
2238 return wpa_cli_cmd(ctrl, "STA_AUTOCONNECT", 1, argc, argv);
2242 static int wpa_cli_cmd_tdls_discover(struct wpa_ctrl *ctrl, int argc,
2245 return wpa_cli_cmd(ctrl, "TDLS_DISCOVER", 1, argc, argv);
2249 static int wpa_cli_cmd_tdls_setup(struct wpa_ctrl *ctrl, int argc,
2252 return wpa_cli_cmd(ctrl, "TDLS_SETUP", 1, argc, argv);
2256 static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
2259 return wpa_cli_cmd(ctrl, "TDLS_TEARDOWN", 1, argc, argv);
2263 static int wpa_cli_cmd_signal_poll(struct wpa_ctrl *ctrl, int argc,
2266 return wpa_ctrl_command(ctrl, "SIGNAL_POLL");
2270 static int wpa_cli_cmd_pktcnt_poll(struct wpa_ctrl *ctrl, int argc,
2273 return wpa_ctrl_command(ctrl, "PKTCNT_POLL");
2277 static int wpa_cli_cmd_reauthenticate(struct wpa_ctrl *ctrl, int argc,
2280 return wpa_ctrl_command(ctrl, "REAUTHENTICATE");
2284 #ifdef CONFIG_AUTOSCAN
2286 static int wpa_cli_cmd_autoscan(struct wpa_ctrl *ctrl, int argc, char *argv[])
2289 return wpa_ctrl_command(ctrl, "AUTOSCAN ");
2291 return wpa_cli_cmd(ctrl, "AUTOSCAN", 0, argc, argv);
2294 #endif /* CONFIG_AUTOSCAN */
2299 static int wpa_cli_cmd_wnm_sleep(struct wpa_ctrl *ctrl, int argc, char *argv[])
2301 return wpa_cli_cmd(ctrl, "WNM_SLEEP", 0, argc, argv);
2305 static int wpa_cli_cmd_wnm_bss_query(struct wpa_ctrl *ctrl, int argc, char *argv[])
2307 return wpa_cli_cmd(ctrl, "WNM_BSS_QUERY", 1, argc, argv);
2310 #endif /* CONFIG_WNM */
2313 static int wpa_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
2317 return wpa_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
2321 static int wpa_cli_cmd_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2323 return wpa_ctrl_command(ctrl, "FLUSH");
2327 enum wpa_cli_cmd_flags {
2328 cli_cmd_flag_none = 0x00,
2329 cli_cmd_flag_sensitive = 0x01
2332 struct wpa_cli_cmd {
2334 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
2335 char ** (*completion)(const char *str, int pos);
2336 enum wpa_cli_cmd_flags flags;
2340 static struct wpa_cli_cmd wpa_cli_commands[] = {
2341 { "status", wpa_cli_cmd_status, NULL,
2343 "[verbose] = get current WPA/EAPOL/EAP status" },
2344 { "ifname", wpa_cli_cmd_ifname, NULL,
2346 "= get current interface name" },
2347 { "ping", wpa_cli_cmd_ping, NULL,
2349 "= pings wpa_supplicant" },
2350 { "relog", wpa_cli_cmd_relog, NULL,
2352 "= re-open log-file (allow rolling logs)" },
2353 { "note", wpa_cli_cmd_note, NULL,
2355 "<text> = add a note to wpa_supplicant debug log" },
2356 { "mib", wpa_cli_cmd_mib, NULL,
2358 "= get MIB variables (dot1x, dot11)" },
2359 { "help", wpa_cli_cmd_help, wpa_cli_complete_help,
2361 "[command] = show usage help" },
2362 { "interface", wpa_cli_cmd_interface, NULL,
2364 "[ifname] = show interfaces/select interface" },
2365 { "level", wpa_cli_cmd_level, NULL,
2367 "<debug level> = change debug level" },
2368 { "license", wpa_cli_cmd_license, NULL,
2370 "= show full wpa_cli license" },
2371 { "quit", wpa_cli_cmd_quit, NULL,
2374 { "set", wpa_cli_cmd_set, wpa_cli_complete_set,
2376 "= set variables (shows list of variables when run without "
2378 { "get", wpa_cli_cmd_get, NULL,
2380 "<name> = get information" },
2381 { "logon", wpa_cli_cmd_logon, NULL,
2383 "= IEEE 802.1X EAPOL state machine logon" },
2384 { "logoff", wpa_cli_cmd_logoff, NULL,
2386 "= IEEE 802.1X EAPOL state machine logoff" },
2387 { "pmksa", wpa_cli_cmd_pmksa, NULL,
2389 "= show PMKSA cache" },
2390 { "reassociate", wpa_cli_cmd_reassociate, NULL,
2392 "= force reassociation" },
2393 { "preauthenticate", wpa_cli_cmd_preauthenticate, wpa_cli_complete_bss,
2395 "<BSSID> = force preauthentication" },
2396 { "identity", wpa_cli_cmd_identity, NULL,
2398 "<network id> <identity> = configure identity for an SSID" },
2399 { "password", wpa_cli_cmd_password, NULL,
2400 cli_cmd_flag_sensitive,
2401 "<network id> <password> = configure password for an SSID" },
2402 { "new_password", wpa_cli_cmd_new_password, NULL,
2403 cli_cmd_flag_sensitive,
2404 "<network id> <password> = change password for an SSID" },
2405 { "pin", wpa_cli_cmd_pin, NULL,
2406 cli_cmd_flag_sensitive,
2407 "<network id> <pin> = configure pin for an SSID" },
2408 { "otp", wpa_cli_cmd_otp, NULL,
2409 cli_cmd_flag_sensitive,
2410 "<network id> <password> = configure one-time-password for an SSID"
2412 { "passphrase", wpa_cli_cmd_passphrase, NULL,
2413 cli_cmd_flag_sensitive,
2414 "<network id> <passphrase> = configure private key passphrase\n"
2416 { "bssid", wpa_cli_cmd_bssid, NULL,
2418 "<network id> <BSSID> = set preferred BSSID for an SSID" },
2419 { "blacklist", wpa_cli_cmd_blacklist, wpa_cli_complete_bss,
2421 "<BSSID> = add a BSSID to the blacklist\n"
2422 "blacklist clear = clear the blacklist\n"
2423 "blacklist = display the blacklist" },
2424 { "log_level", wpa_cli_cmd_log_level, NULL,
2426 "<level> [<timestamp>] = update the log level/timestamp\n"
2427 "log_level = display the current log level and log options" },
2428 { "list_networks", wpa_cli_cmd_list_networks, NULL,
2430 "= list configured networks" },
2431 { "select_network", wpa_cli_cmd_select_network, NULL,
2433 "<network id> = select a network (disable others)" },
2434 { "enable_network", wpa_cli_cmd_enable_network, NULL,
2436 "<network id> = enable a network" },
2437 { "disable_network", wpa_cli_cmd_disable_network, NULL,
2439 "<network id> = disable a network" },
2440 { "add_network", wpa_cli_cmd_add_network, NULL,
2442 "= add a network" },
2443 { "remove_network", wpa_cli_cmd_remove_network, NULL,
2445 "<network id> = remove a network" },
2446 { "set_network", wpa_cli_cmd_set_network, NULL,
2447 cli_cmd_flag_sensitive,
2448 "<network id> <variable> <value> = set network variables (shows\n"
2449 " list of variables when run without arguments)" },
2450 { "get_network", wpa_cli_cmd_get_network, NULL,
2452 "<network id> <variable> = get network variables" },
2453 { "list_creds", wpa_cli_cmd_list_creds, NULL,
2455 "= list configured credentials" },
2456 { "add_cred", wpa_cli_cmd_add_cred, NULL,
2458 "= add a credential" },
2459 { "remove_cred", wpa_cli_cmd_remove_cred, NULL,
2461 "<cred id> = remove a credential" },
2462 { "set_cred", wpa_cli_cmd_set_cred, NULL,
2463 cli_cmd_flag_sensitive,
2464 "<cred id> <variable> <value> = set credential variables" },
2465 { "save_config", wpa_cli_cmd_save_config, NULL,
2467 "= save the current configuration" },
2468 { "disconnect", wpa_cli_cmd_disconnect, NULL,
2470 "= disconnect and wait for reassociate/reconnect command before\n"
2472 { "reconnect", wpa_cli_cmd_reconnect, NULL,
2474 "= like reassociate, but only takes effect if already disconnected"
2476 { "scan", wpa_cli_cmd_scan, NULL,
2478 "= request new BSS scan" },
2479 { "scan_results", wpa_cli_cmd_scan_results, NULL,
2481 "= get latest scan results" },
2482 { "bss", wpa_cli_cmd_bss, wpa_cli_complete_bss,
2484 "<<idx> | <bssid>> = get detailed scan result info" },
2485 { "get_capability", wpa_cli_cmd_get_capability, NULL,
2487 "<eap/pairwise/group/key_mgmt/proto/auth_alg/channels/freq/modes> "
2488 "= get capabilies" },
2489 { "reconfigure", wpa_cli_cmd_reconfigure, NULL,
2491 "= force wpa_supplicant to re-read its configuration file" },
2492 { "terminate", wpa_cli_cmd_terminate, NULL,
2494 "= terminate wpa_supplicant" },
2495 { "interface_add", wpa_cli_cmd_interface_add, NULL,
2497 "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
2498 " <bridge_name> = adds new interface, all parameters but <ifname>\n"
2500 { "interface_remove", wpa_cli_cmd_interface_remove, NULL,
2502 "<ifname> = removes the interface" },
2503 { "interface_list", wpa_cli_cmd_interface_list, NULL,
2505 "= list available interfaces" },
2506 { "ap_scan", wpa_cli_cmd_ap_scan, NULL,
2508 "<value> = set ap_scan parameter" },
2509 { "scan_interval", wpa_cli_cmd_scan_interval, NULL,
2511 "<value> = set scan_interval parameter (in seconds)" },
2512 { "bss_expire_age", wpa_cli_cmd_bss_expire_age, NULL,
2514 "<value> = set BSS expiration age parameter" },
2515 { "bss_expire_count", wpa_cli_cmd_bss_expire_count, NULL,
2517 "<value> = set BSS expiration scan count parameter" },
2518 { "bss_flush", wpa_cli_cmd_bss_flush, NULL,
2520 "<value> = set BSS flush age (0 by default)" },
2521 { "stkstart", wpa_cli_cmd_stkstart, NULL,
2523 "<addr> = request STK negotiation with <addr>" },
2524 { "ft_ds", wpa_cli_cmd_ft_ds, wpa_cli_complete_bss,
2526 "<addr> = request over-the-DS FT with <addr>" },
2527 { "wps_pbc", wpa_cli_cmd_wps_pbc, wpa_cli_complete_bss,
2529 "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
2530 { "wps_pin", wpa_cli_cmd_wps_pin, wpa_cli_complete_bss,
2531 cli_cmd_flag_sensitive,
2532 "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
2534 { "wps_check_pin", wpa_cli_cmd_wps_check_pin, NULL,
2535 cli_cmd_flag_sensitive,
2536 "<PIN> = verify PIN checksum" },
2537 { "wps_cancel", wpa_cli_cmd_wps_cancel, NULL, cli_cmd_flag_none,
2538 "Cancels the pending WPS operation" },
2539 #ifdef CONFIG_WPS_NFC
2540 { "wps_nfc", wpa_cli_cmd_wps_nfc, wpa_cli_complete_bss,
2542 "[BSSID] = start Wi-Fi Protected Setup: NFC" },
2543 { "wps_nfc_config_token", wpa_cli_cmd_wps_nfc_config_token, NULL,
2545 "<WPS|NDEF> = build configuration token" },
2546 { "wps_nfc_token", wpa_cli_cmd_wps_nfc_token, NULL,
2548 "<WPS|NDEF> = create password token" },
2549 { "wps_nfc_tag_read", wpa_cli_cmd_wps_nfc_tag_read, NULL,
2550 cli_cmd_flag_sensitive,
2551 "<hexdump of payload> = report read NFC tag with WPS data" },
2552 { "nfc_get_handover_req", wpa_cli_cmd_nfc_get_handover_req, NULL,
2554 "<NDEF> <WPS> = create NFC handover request" },
2555 { "nfc_get_handover_sel", wpa_cli_cmd_nfc_get_handover_sel, NULL,
2557 "<NDEF> <WPS> = create NFC handover select" },
2558 { "nfc_rx_handover_req", wpa_cli_cmd_nfc_rx_handover_req, NULL,
2560 "<hexdump of payload> = report received NFC handover request" },
2561 { "nfc_rx_handover_sel", wpa_cli_cmd_nfc_rx_handover_sel, NULL,
2563 "<hexdump of payload> = report received NFC handover select" },
2564 { "nfc_report_handover", wpa_cli_cmd_nfc_report_handover, NULL,
2566 "<role> <type> <hexdump of req> <hexdump of sel> = report completed "
2568 #endif /* CONFIG_WPS_NFC */
2569 { "wps_reg", wpa_cli_cmd_wps_reg, wpa_cli_complete_bss,
2570 cli_cmd_flag_sensitive,
2571 "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
2572 { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin, NULL,
2573 cli_cmd_flag_sensitive,
2574 "[params..] = enable/disable AP PIN" },
2575 { "wps_er_start", wpa_cli_cmd_wps_er_start, NULL,
2577 "[IP address] = start Wi-Fi Protected Setup External Registrar" },
2578 { "wps_er_stop", wpa_cli_cmd_wps_er_stop, NULL,
2580 "= stop Wi-Fi Protected Setup External Registrar" },
2581 { "wps_er_pin", wpa_cli_cmd_wps_er_pin, NULL,
2582 cli_cmd_flag_sensitive,
2583 "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
2584 { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc, NULL,
2586 "<UUID> = accept an Enrollee PBC using External Registrar" },
2587 { "wps_er_learn", wpa_cli_cmd_wps_er_learn, NULL,
2588 cli_cmd_flag_sensitive,
2589 "<UUID> <PIN> = learn AP configuration" },
2590 { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config, NULL,
2592 "<UUID> <network id> = set AP configuration for enrolling" },
2593 { "wps_er_config", wpa_cli_cmd_wps_er_config, NULL,
2594 cli_cmd_flag_sensitive,
2595 "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
2596 #ifdef CONFIG_WPS_NFC
2597 { "wps_er_nfc_config_token", wpa_cli_cmd_wps_er_nfc_config_token, NULL,
2599 "<WPS/NDEF> <UUID> = build NFC configuration token" },
2600 #endif /* CONFIG_WPS_NFC */
2601 { "ibss_rsn", wpa_cli_cmd_ibss_rsn, NULL,
2603 "<addr> = request RSN authentication with <addr> in IBSS" },
2605 { "sta", wpa_cli_cmd_sta, NULL,
2607 "<addr> = get information about an associated station (AP)" },
2608 { "all_sta", wpa_cli_cmd_all_sta, NULL,
2610 "= get information about all associated stations (AP)" },
2611 { "deauthenticate", wpa_cli_cmd_deauthenticate, NULL,
2613 "<addr> = deauthenticate a station" },
2614 { "disassociate", wpa_cli_cmd_disassociate, NULL,
2616 "<addr> = disassociate a station" },
2617 #endif /* CONFIG_AP */
2618 { "suspend", wpa_cli_cmd_suspend, NULL, cli_cmd_flag_none,
2619 "= notification of suspend/hibernate" },
2620 { "resume", wpa_cli_cmd_resume, NULL, cli_cmd_flag_none,
2621 "= notification of resume/thaw" },
2622 { "drop_sa", wpa_cli_cmd_drop_sa, NULL, cli_cmd_flag_none,
2623 "= drop SA without deauth/disassoc (test command)" },
2624 { "roam", wpa_cli_cmd_roam, wpa_cli_complete_bss,
2626 "<addr> = roam to the specified BSS" },
2628 { "p2p_find", wpa_cli_cmd_p2p_find, wpa_cli_complete_p2p_find,
2630 "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
2631 { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, NULL, cli_cmd_flag_none,
2632 "= stop P2P Devices search" },
2633 { "p2p_connect", wpa_cli_cmd_p2p_connect, wpa_cli_complete_p2p_connect,
2635 "<addr> <\"pbc\"|PIN> [ht40] = connect to a P2P Device" },
2636 { "p2p_listen", wpa_cli_cmd_p2p_listen, NULL, cli_cmd_flag_none,
2637 "[timeout] = listen for P2P Devices for up-to timeout seconds" },
2638 { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove,
2639 wpa_cli_complete_p2p_group_remove, cli_cmd_flag_none,
2640 "<ifname> = remove P2P group interface (terminate group if GO)" },
2641 { "p2p_group_add", wpa_cli_cmd_p2p_group_add, NULL, cli_cmd_flag_none,
2642 "[ht40] = add a new P2P group (local end as GO)" },
2643 { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc,
2644 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2645 "<addr> <method> = request provisioning discovery" },
2646 { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase, NULL,
2648 "= get the passphrase for a group (GO only)" },
2649 { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req,
2650 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2651 "<addr> <TLVs> = schedule service discovery request" },
2652 { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req,
2653 NULL, cli_cmd_flag_none,
2654 "<id> = cancel pending service discovery request" },
2655 { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp, NULL,
2657 "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
2658 { "p2p_service_update", wpa_cli_cmd_p2p_service_update, NULL,
2660 "= indicate change in local services" },
2661 { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external, NULL,
2663 "<external> = set external processing of service discovery" },
2664 { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush, NULL,
2666 "= remove all stored service entries" },
2667 { "p2p_service_add", wpa_cli_cmd_p2p_service_add, NULL,
2669 "<bonjour|upnp> <query|version> <response|service> = add a local "
2671 { "p2p_service_del", wpa_cli_cmd_p2p_service_del, NULL,
2673 "<bonjour|upnp> <query|version> [|service] = remove a local "
2675 { "p2p_reject", wpa_cli_cmd_p2p_reject, wpa_cli_complete_p2p_peer,
2677 "<addr> = reject connection attempts from a specific peer" },
2678 { "p2p_invite", wpa_cli_cmd_p2p_invite, NULL,
2680 "<cmd> [peer=addr] = invite peer" },
2681 { "p2p_peers", wpa_cli_cmd_p2p_peers, NULL, cli_cmd_flag_none,
2682 "[discovered] = list known (optionally, only fully discovered) P2P "
2684 { "p2p_peer", wpa_cli_cmd_p2p_peer, wpa_cli_complete_p2p_peer,
2686 "<address> = show information about known P2P peer" },
2687 { "p2p_set", wpa_cli_cmd_p2p_set, NULL, cli_cmd_flag_none,
2688 "<field> <value> = set a P2P parameter" },
2689 { "p2p_flush", wpa_cli_cmd_p2p_flush, NULL, cli_cmd_flag_none,
2690 "= flush P2P state" },
2691 { "p2p_cancel", wpa_cli_cmd_p2p_cancel, NULL, cli_cmd_flag_none,
2692 "= cancel P2P group formation" },
2693 { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize,
2694 wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
2695 "<address> = unauthorize a peer" },
2696 { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, NULL,
2698 "[<duration> <interval>] [<duration> <interval>] = request GO "
2700 { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, NULL,
2702 "[<period> <interval>] = set extended listen timing" },
2703 #endif /* CONFIG_P2P */
2704 #ifdef CONFIG_WIFI_DISPLAY
2705 { "wfd_subelem_set", wpa_cli_cmd_wfd_subelem_set, NULL,
2707 "<subelem> [contents] = set Wi-Fi Display subelement" },
2708 { "wfd_subelem_get", wpa_cli_cmd_wfd_subelem_get, NULL,
2710 "<subelem> = get Wi-Fi Display subelement" },
2711 #endif /* CONFIG_WIFI_DISPLAY */
2712 #ifdef CONFIG_INTERWORKING
2713 { "fetch_anqp", wpa_cli_cmd_fetch_anqp, NULL, cli_cmd_flag_none,
2714 "= fetch ANQP information for all APs" },
2715 { "stop_fetch_anqp", wpa_cli_cmd_stop_fetch_anqp, NULL,
2717 "= stop fetch_anqp operation" },
2718 { "interworking_select", wpa_cli_cmd_interworking_select, NULL,
2720 "[auto] = perform Interworking network selection" },
2721 { "interworking_connect", wpa_cli_cmd_interworking_connect,
2722 wpa_cli_complete_bss, cli_cmd_flag_none,
2723 "<BSSID> = connect using Interworking credentials" },
2724 { "anqp_get", wpa_cli_cmd_anqp_get, wpa_cli_complete_bss,
2726 "<addr> <info id>[,<info id>]... = request ANQP information" },
2727 { "gas_request", wpa_cli_cmd_gas_request, wpa_cli_complete_bss,
2729 "<addr> <AdvProtoID> [QueryReq] = GAS request" },
2730 { "gas_response_get", wpa_cli_cmd_gas_response_get,
2731 wpa_cli_complete_bss, cli_cmd_flag_none,
2732 "<addr> <dialog token> [start,len] = Fetch last GAS response" },
2733 #endif /* CONFIG_INTERWORKING */
2735 { "hs20_anqp_get", wpa_cli_cmd_hs20_anqp_get, wpa_cli_complete_bss,
2737 "<addr> <subtype>[,<subtype>]... = request HS 2.0 ANQP information"
2739 { "nai_home_realm_list", wpa_cli_cmd_get_nai_home_realm_list,
2740 wpa_cli_complete_bss, cli_cmd_flag_none,
2741 "<addr> <home realm> = get HS20 nai home realm list" },
2742 #endif /* CONFIG_HS20 */
2743 { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, NULL,
2745 "<0/1> = disable/enable automatic reconnection" },
2746 { "tdls_discover", wpa_cli_cmd_tdls_discover, NULL,
2748 "<addr> = request TDLS discovery with <addr>" },
2749 { "tdls_setup", wpa_cli_cmd_tdls_setup, NULL,
2751 "<addr> = request TDLS setup with <addr>" },
2752 { "tdls_teardown", wpa_cli_cmd_tdls_teardown, NULL,
2754 "<addr> = tear down TDLS with <addr>" },
2755 { "signal_poll", wpa_cli_cmd_signal_poll, NULL,
2757 "= get signal parameters" },
2758 { "pktcnt_poll", wpa_cli_cmd_pktcnt_poll, NULL,
2760 "= get TX/RX packet counters" },
2761 { "reauthenticate", wpa_cli_cmd_reauthenticate, NULL,
2763 "= trigger IEEE 802.1X/EAPOL reauthentication" },
2764 #ifdef CONFIG_AUTOSCAN
2765 { "autoscan", wpa_cli_cmd_autoscan, NULL, cli_cmd_flag_none,
2766 "[params] = Set or unset (if none) autoscan parameters" },
2767 #endif /* CONFIG_AUTOSCAN */
2769 { "wnm_sleep", wpa_cli_cmd_wnm_sleep, NULL, cli_cmd_flag_none,
2770 "<enter/exit> [interval=#] = enter/exit WNM-Sleep mode" },
2771 { "wnm_bss_query", wpa_cli_cmd_wnm_bss_query, NULL, cli_cmd_flag_none,
2772 "<query reason> = Send BSS Transition Management Query" },
2773 #endif /* CONFIG_WNM */
2774 { "raw", wpa_cli_cmd_raw, NULL, cli_cmd_flag_sensitive,
2775 "<params..> = Sent unprocessed command" },
2776 { "flush", wpa_cli_cmd_flush, NULL, cli_cmd_flag_none,
2777 "= flush wpa_supplicant state" },
2778 { NULL, NULL, NULL, cli_cmd_flag_none, NULL }
2783 * Prints command usage, lines are padded with the specified string.
2785 static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
2790 printf("%s%s ", pad, cmd->cmd);
2791 for (n = 0; (c = cmd->usage[n]); n++) {
2800 static void print_help(const char *cmd)
2803 printf("commands:\n");
2804 for (n = 0; wpa_cli_commands[n].cmd; n++) {
2805 if (cmd == NULL || str_starts(wpa_cli_commands[n].cmd, cmd))
2806 print_cmd_help(&wpa_cli_commands[n], " ");
2811 static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd)
2813 const char *c, *delim;
2817 delim = os_strchr(cmd, ' ');
2821 len = os_strlen(cmd);
2823 for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
2824 if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
2825 return (wpa_cli_commands[n].flags &
2826 cli_cmd_flag_sensitive);
2832 static char ** wpa_list_cmd_list(void)
2836 struct cli_txt_entry *e;
2838 count = sizeof(wpa_cli_commands) / sizeof(wpa_cli_commands[0]);
2839 count += dl_list_len(&p2p_groups);
2840 count += dl_list_len(&ifnames);
2841 res = os_calloc(count + 1, sizeof(char *));
2845 for (i = 0; wpa_cli_commands[i].cmd; i++) {
2846 res[i] = os_strdup(wpa_cli_commands[i].cmd);
2851 dl_list_for_each(e, &p2p_groups, struct cli_txt_entry, list) {
2852 size_t len = 8 + os_strlen(e->txt);
2853 res[i] = os_malloc(len);
2856 os_snprintf(res[i], len, "ifname=%s", e->txt);
2860 dl_list_for_each(e, &ifnames, struct cli_txt_entry, list) {
2861 res[i] = os_strdup(e->txt);
2871 static char ** wpa_cli_cmd_completion(const char *cmd, const char *str,
2876 for (i = 0; wpa_cli_commands[i].cmd; i++) {
2877 if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) {
2878 if (wpa_cli_commands[i].completion)
2879 return wpa_cli_commands[i].completion(str,
2882 printf("\r%s\n", wpa_cli_commands[i].usage);
2892 static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
2898 if (pos > 7 && os_strncasecmp(str, "IFNAME=", 7) == 0) {
2899 end = os_strchr(str, ' ');
2900 if (end && pos > end - str) {
2901 pos -= end - str + 1;
2906 end = os_strchr(str, ' ');
2907 if (end == NULL || str + pos < end)
2908 return wpa_list_cmd_list();
2910 cmd = os_malloc(pos + 1);
2913 os_memcpy(cmd, str, pos);
2914 cmd[end - str] = '\0';
2915 res = wpa_cli_cmd_completion(cmd, str, pos);
2921 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
2923 struct wpa_cli_cmd *cmd, *match = NULL;
2927 if (argc > 1 && os_strncasecmp(argv[0], "IFNAME=", 7) == 0) {
2928 ifname_prefix = argv[0] + 7;
2932 ifname_prefix = NULL;
2938 cmd = wpa_cli_commands;
2940 if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
2943 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
2944 /* we have an exact match */
2954 printf("Ambiguous command '%s'; possible commands:", argv[0]);
2955 cmd = wpa_cli_commands;
2957 if (os_strncasecmp(cmd->cmd, argv[0],
2958 os_strlen(argv[0])) == 0) {
2959 printf(" %s", cmd->cmd);
2965 } else if (count == 0) {
2966 printf("Unknown command '%s'\n", argv[0]);
2969 ret = match->handler(ctrl, argc - 1, &argv[1]);
2976 static int str_match(const char *a, const char *b)
2978 return os_strncmp(a, b, os_strlen(b)) == 0;
2982 static int wpa_cli_exec(const char *program, const char *arg1,
2990 len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
2991 cmd = os_malloc(len);
2994 res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
2995 if (res < 0 || (size_t) res >= len) {
2999 cmd[len - 1] = '\0';
3001 if (system(cmd) < 0)
3003 #endif /* _WIN32_WCE */
3010 static void wpa_cli_action_process(const char *msg)
3013 char *copy = NULL, *id, *pos2;
3018 pos = os_strchr(pos, '>');
3025 if (str_match(pos, WPA_EVENT_CONNECTED)) {
3027 os_unsetenv("WPA_ID");
3028 os_unsetenv("WPA_ID_STR");
3029 os_unsetenv("WPA_CTRL_DIR");
3031 pos = os_strstr(pos, "[id=");
3033 copy = os_strdup(pos + 4);
3037 while (*pos2 && *pos2 != ' ')
3041 os_setenv("WPA_ID", id, 1);
3042 while (*pos2 && *pos2 != '=')
3047 while (*pos2 && *pos2 != ']')
3050 os_setenv("WPA_ID_STR", id, 1);
3054 os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
3056 if (!wpa_cli_connected || new_id != wpa_cli_last_id) {
3057 wpa_cli_connected = 1;
3058 wpa_cli_last_id = new_id;
3059 wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED");
3061 } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) {
3062 if (wpa_cli_connected) {
3063 wpa_cli_connected = 0;
3064 wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED");
3066 } else if (str_match(pos, P2P_EVENT_GROUP_STARTED)) {
3067 wpa_cli_exec(action_file, ctrl_ifname, pos);
3068 } else if (str_match(pos, P2P_EVENT_GROUP_REMOVED)) {
3069 wpa_cli_exec(action_file, ctrl_ifname, pos);
3070 } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) {
3071 wpa_cli_exec(action_file, ctrl_ifname, pos);
3072 } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) {
3073 wpa_cli_exec(action_file, ctrl_ifname, pos);
3074 } else if (str_match(pos, P2P_EVENT_GO_NEG_FAILURE)) {
3075 wpa_cli_exec(action_file, ctrl_ifname, pos);
3076 } else if (str_match(pos, WPS_EVENT_SUCCESS)) {
3077 wpa_cli_exec(action_file, ctrl_ifname, pos);
3078 } else if (str_match(pos, WPS_EVENT_FAIL)) {
3079 wpa_cli_exec(action_file, ctrl_ifname, pos);
3080 } else if (str_match(pos, AP_STA_CONNECTED)) {
3081 wpa_cli_exec(action_file, ctrl_ifname, pos);
3082 } else if (str_match(pos, AP_STA_DISCONNECTED)) {
3083 wpa_cli_exec(action_file, ctrl_ifname, pos);
3084 } else if (str_match(pos, ESS_DISASSOC_IMMINENT)) {
3085 wpa_cli_exec(action_file, ctrl_ifname, pos);
3086 } else if (str_match(pos, WPA_EVENT_TERMINATING)) {
3087 printf("wpa_supplicant is terminating - stop monitoring\n");
3093 #ifndef CONFIG_ANSI_C_EXTRA
3094 static void wpa_cli_action_cb(char *msg, size_t len)
3096 wpa_cli_action_process(msg);
3098 #endif /* CONFIG_ANSI_C_EXTRA */
3101 static void wpa_cli_reconnect(void)
3103 wpa_cli_close_connection();
3104 if (wpa_cli_open_connection(ctrl_ifname, 1) < 0)
3109 printf("\rConnection to wpa_supplicant re-established\n");
3115 static void cli_event(const char *str)
3117 const char *start, *s;
3119 start = os_strchr(str, '>');
3125 if (str_starts(start, WPA_EVENT_BSS_ADDED)) {
3126 s = os_strchr(start, ' ');
3129 s = os_strchr(s + 1, ' ');
3132 cli_txt_list_add(&bsses, s + 1);
3136 if (str_starts(start, WPA_EVENT_BSS_REMOVED)) {
3137 s = os_strchr(start, ' ');
3140 s = os_strchr(s + 1, ' ');
3143 cli_txt_list_del_addr(&bsses, s + 1);
3148 if (str_starts(start, P2P_EVENT_DEVICE_FOUND)) {
3149 s = os_strstr(start, " p2p_dev_addr=");
3152 cli_txt_list_add_addr(&p2p_peers, s + 14);
3156 if (str_starts(start, P2P_EVENT_DEVICE_LOST)) {
3157 s = os_strstr(start, " p2p_dev_addr=");
3160 cli_txt_list_del_addr(&p2p_peers, s + 14);
3164 if (str_starts(start, P2P_EVENT_GROUP_STARTED)) {
3165 s = os_strchr(start, ' ');
3168 cli_txt_list_add_word(&p2p_groups, s + 1);
3172 if (str_starts(start, P2P_EVENT_GROUP_REMOVED)) {
3173 s = os_strchr(start, ' ');
3176 cli_txt_list_del_word(&p2p_groups, s + 1);
3179 #endif /* CONFIG_P2P */
3183 static int check_terminating(const char *msg)
3185 const char *pos = msg;
3189 pos = os_strchr(pos, '>');
3196 if (str_match(pos, WPA_EVENT_TERMINATING) && ctrl_conn) {
3198 printf("\rConnection to wpa_supplicant lost - trying to "
3201 wpa_cli_attached = 0;
3202 wpa_cli_close_connection();
3210 static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
3212 if (ctrl_conn == NULL) {
3213 wpa_cli_reconnect();
3216 while (wpa_ctrl_pending(ctrl) > 0) {
3218 size_t len = sizeof(buf) - 1;
3219 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
3222 wpa_cli_action_process(buf);
3225 if (wpa_cli_show_event(buf)) {
3227 printf("\r%s\n", buf);
3231 if (interactive && check_terminating(buf) > 0)
3235 printf("Could not read pending message.\n");
3240 if (wpa_ctrl_pending(ctrl) < 0) {
3241 printf("Connection to wpa_supplicant lost - trying to "
3243 wpa_cli_reconnect();
3249 static int tokenize_cmd(char *cmd, char *argv[])
3262 if (argc == max_args)
3265 char *pos2 = os_strrchr(pos, '"');
3269 while (*pos != '\0' && *pos != ' ')
3279 static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
3281 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
3282 printf("Connection to wpa_supplicant lost - trying to "
3284 wpa_cli_close_connection();
3287 wpa_cli_reconnect();
3288 eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
3292 static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx)
3294 wpa_cli_recv_pending(mon_conn, 0);
3298 static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd)
3300 char *argv[max_args];
3302 argc = tokenize_cmd(cmd, argv);
3304 wpa_request(ctrl_conn, argc, argv);
3308 static void wpa_cli_edit_eof_cb(void *ctx)
3314 static int warning_displayed = 0;
3315 static char *hfile = NULL;
3316 static int edit_started = 0;
3318 static void start_edit(void)
3323 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3324 ps = wpa_ctrl_get_remote_ifname(ctrl_conn);
3325 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3327 home = getenv("HOME");
3329 const char *fname = ".wpa_cli_history";
3330 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
3331 hfile = os_malloc(hfile_len);
3333 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
3336 if (edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
3337 wpa_cli_edit_completion_cb, NULL, hfile, ps) < 0) {
3343 eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
3347 static void update_bssid_list(struct wpa_ctrl *ctrl)
3350 size_t len = sizeof(buf);
3352 char *cmd = "BSS RANGE=ALL MASK=0x2";
3357 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
3364 pos = os_strstr(pos, "bssid=");
3368 end = os_strchr(pos, '\n');
3372 cli_txt_list_add(&bsses, pos);
3378 static void update_ifnames(struct wpa_ctrl *ctrl)
3381 size_t len = sizeof(buf);
3383 char *cmd = "INTERFACES";
3387 cli_txt_list_flush(&ifnames);
3391 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
3398 end = os_strchr(pos, '\n');
3402 ret = os_snprintf(txt, sizeof(txt), "ifname=%s", pos);
3403 if (ret > 0 && ret < (int) sizeof(txt))
3404 cli_txt_list_add(&ifnames, txt);
3410 static void try_connection(void *eloop_ctx, void *timeout_ctx)
3415 if (ctrl_ifname == NULL)
3416 ctrl_ifname = wpa_cli_get_default_ifname();
3418 if (!wpa_cli_open_connection(ctrl_ifname, 1) == 0) {
3419 if (!warning_displayed) {
3420 printf("Could not connect to wpa_supplicant: "
3421 "%s - re-trying\n", ctrl_ifname);
3422 warning_displayed = 1;
3424 eloop_register_timeout(1, 0, try_connection, NULL, NULL);
3428 update_bssid_list(ctrl_conn);
3430 if (warning_displayed)
3431 printf("Connection established.\n");
3438 static void wpa_cli_interactive(void)
3440 printf("\nInteractive mode\n\n");
3442 eloop_register_timeout(0, 0, try_connection, NULL, NULL);
3444 eloop_cancel_timeout(try_connection, NULL, NULL);
3446 cli_txt_list_flush(&p2p_peers);
3447 cli_txt_list_flush(&p2p_groups);
3448 cli_txt_list_flush(&bsses);
3449 cli_txt_list_flush(&ifnames);
3451 edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
3453 eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
3454 wpa_cli_close_connection();
3458 static void wpa_cli_action(struct wpa_ctrl *ctrl)
3460 #ifdef CONFIG_ANSI_C_EXTRA
3461 /* TODO: ANSI C version(?) */
3462 printf("Action processing not supported in ANSI C build.\n");
3463 #else /* CONFIG_ANSI_C_EXTRA */
3467 char buf[256]; /* note: large enough to fit in unsolicited messages */
3470 fd = wpa_ctrl_get_fd(ctrl);
3472 while (!wpa_cli_quit) {
3475 tv.tv_sec = ping_interval;
3477 res = select(fd + 1, &rfds, NULL, NULL, &tv);
3478 if (res < 0 && errno != EINTR) {
3483 if (FD_ISSET(fd, &rfds))
3484 wpa_cli_recv_pending(ctrl, 1);
3486 /* verify that connection is still working */
3487 len = sizeof(buf) - 1;
3488 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
3489 wpa_cli_action_cb) < 0 ||
3490 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
3491 printf("wpa_supplicant did not reply to PING "
3492 "command - exiting\n");
3497 #endif /* CONFIG_ANSI_C_EXTRA */
3501 static void wpa_cli_cleanup(void)
3503 wpa_cli_close_connection();
3505 os_daemonize_terminate(pid_file);
3507 os_program_deinit();
3511 static void wpa_cli_terminate(int sig, void *ctx)
3517 static char * wpa_cli_get_default_ifname(void)
3519 char *ifname = NULL;
3521 #ifdef CONFIG_CTRL_IFACE_UNIX
3522 struct dirent *dent;
3523 DIR *dir = opendir(ctrl_iface_dir);
3526 char ifprop[PROPERTY_VALUE_MAX];
3527 if (property_get("wifi.interface", ifprop, NULL) != 0) {
3528 ifname = os_strdup(ifprop);
3529 printf("Using interface '%s'\n", ifname);
3532 #endif /* ANDROID */
3535 while ((dent = readdir(dir))) {
3536 #ifdef _DIRENT_HAVE_D_TYPE
3538 * Skip the file if it is not a socket. Also accept
3539 * DT_UNKNOWN (0) in case the C library or underlying
3540 * file system does not support d_type.
3542 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
3544 #endif /* _DIRENT_HAVE_D_TYPE */
3545 if (os_strcmp(dent->d_name, ".") == 0 ||
3546 os_strcmp(dent->d_name, "..") == 0)
3548 printf("Selected interface '%s'\n", dent->d_name);
3549 ifname = os_strdup(dent->d_name);
3553 #endif /* CONFIG_CTRL_IFACE_UNIX */
3555 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
3556 char buf[4096], *pos;
3558 struct wpa_ctrl *ctrl;
3561 ctrl = wpa_ctrl_open(NULL);
3565 len = sizeof(buf) - 1;
3566 ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
3569 pos = os_strchr(buf, '\n');
3572 ifname = os_strdup(buf);
3574 wpa_ctrl_close(ctrl);
3575 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3581 int main(int argc, char *argv[])
3586 const char *global = NULL;
3588 if (os_program_init())
3592 c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
3597 action_file = optarg;
3606 ping_interval = atoi(optarg);
3612 printf("%s\n", wpa_cli_version);
3615 os_free(ctrl_ifname);
3616 ctrl_ifname = os_strdup(optarg);
3619 ctrl_iface_dir = optarg;
3630 interactive = (argc == optind) && (action_file == NULL);
3633 printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
3639 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
3640 ctrl_conn = wpa_ctrl_open(NULL);
3641 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3642 ctrl_conn = wpa_ctrl_open(global);
3643 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
3644 if (ctrl_conn == NULL) {
3645 fprintf(stderr, "Failed to connect to wpa_supplicant "
3646 "global interface: %s error: %s\n",
3647 global, strerror(errno));
3652 update_ifnames(ctrl_conn);
3653 mon_conn = wpa_ctrl_open(global);
3655 if (wpa_ctrl_attach(mon_conn) == 0) {
3656 wpa_cli_attached = 1;
3657 eloop_register_read_sock(
3658 wpa_ctrl_get_fd(mon_conn),
3659 wpa_cli_mon_receive,
3662 printf("Failed to open monitor "
3663 "connection through global "
3664 "control interface\n");
3670 eloop_register_signal_terminate(wpa_cli_terminate, NULL);
3672 if (ctrl_ifname == NULL)
3673 ctrl_ifname = wpa_cli_get_default_ifname();
3676 wpa_cli_interactive();
3679 wpa_cli_open_connection(ctrl_ifname, 0) < 0) {
3680 fprintf(stderr, "Failed to connect to non-global "
3681 "ctrl_ifname: %s error: %s\n",
3682 ctrl_ifname, strerror(errno));
3687 if (wpa_ctrl_attach(ctrl_conn) == 0) {
3688 wpa_cli_attached = 1;
3690 printf("Warning: Failed to attach to "
3691 "wpa_supplicant.\n");
3696 if (daemonize && os_daemonize(pid_file))
3700 wpa_cli_action(ctrl_conn);
3702 ret = wpa_request(ctrl_conn, argc - optind,
3706 os_free(ctrl_ifname);
3713 #else /* CONFIG_CTRL_IFACE */
3714 int main(int argc, char *argv[])
3716 printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
3719 #endif /* CONFIG_CTRL_IFACE */