1 /*
2  * WPA Supplicant - command line interface for wpa_supplicant daemon
3  * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #ifdef CONFIG_CTRL_IFACE
12 
13 #ifdef CONFIG_CTRL_IFACE_UNIX
14 #include <dirent.h>
15 #endif /* CONFIG_CTRL_IFACE_UNIX */
16 
17 #include "common/cli.h"
18 #include "common/wpa_ctrl.h"
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "utils/edit.h"
22 #include "utils/list.h"
23 #include "common/version.h"
24 #include "common/ieee802_11_defs.h"
25 #ifdef ANDROID
26 #include <cutils/properties.h>
27 #endif /* ANDROID */
28 
29 
30 static const char *const wpa_cli_version =
31 "wpa_cli v" VERSION_STR "\n"
32 "Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi> and contributors";
33 
34 #define VENDOR_ELEM_FRAME_ID \
35 	"  0: Probe Req (P2P), 1: Probe Resp (P2P) , 2: Probe Resp (GO), " \
36 	"3: Beacon (GO), 4: PD Req, 5: PD Resp, 6: GO Neg Req, " \
37 	"7: GO Neg Resp, 8: GO Neg Conf, 9: Inv Req, 10: Inv Resp, " \
38 	"11: Assoc Req (P2P), 12: Assoc Resp (P2P)"
39 #if defined(__ZEPHYR__)
40 struct wpa_ctrl *ctrl_conn;
41 char *ifname_prefix = NULL;
42 #else
43 static struct wpa_ctrl *ctrl_conn;
44 static char *ifname_prefix = NULL;
45 #endif /* __ZEPHYR__ */
46 static struct wpa_ctrl *mon_conn;
47 static int wpa_cli_quit = 0;
48 static int wpa_cli_attached = 0;
49 static int wpa_cli_connected = -1;
50 static int wpa_cli_last_id = 0;
51 #ifndef CONFIG_CTRL_IFACE_DIR
52 #define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
53 #endif /* CONFIG_CTRL_IFACE_DIR */
54 static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
55 static const char *client_socket_dir = NULL;
56 static char *ctrl_ifname = NULL;
57 static const char *global = NULL;
58 static const char *pid_file = NULL;
59 static const char *action_file = NULL;
60 static int reconnect = 0;
61 static int ping_interval = 5;
62 static int interactive = 0;
63 
64 static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
65 static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */
66 static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */
67 static DEFINE_DL_LIST(ifnames); /* struct cli_txt_entry */
68 static DEFINE_DL_LIST(networks); /* struct cli_txt_entry */
69 static DEFINE_DL_LIST(creds); /* struct cli_txt_entry */
70 #ifdef CONFIG_AP
71 static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
72 #endif /* CONFIG_AP */
73 
74 
75 static void print_help(const char *cmd);
76 static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx);
77 static void wpa_cli_close_connection(void);
78 static char * wpa_cli_get_default_ifname(void);
79 static char ** wpa_list_cmd_list(void);
80 static void update_creds(struct wpa_ctrl *ctrl);
81 static void update_networks(struct wpa_ctrl *ctrl);
82 static void update_stations(struct wpa_ctrl *ctrl);
83 static void update_ifnames(struct wpa_ctrl *ctrl);
84 
85 
usage(void)86 static void usage(void)
87 {
88 	printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvBr] "
89 	       "[-a<action file>] \\\n"
90 	       "        [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
91 	       "\\\n"
92 	       "        [-s<wpa_client_socket_file_path>] "
93 	       "[command..]\n"
94 	       "  -h = help (show this usage text)\n"
95 	       "  -v = shown version information\n"
96 	       "  -a = run in daemon mode executing the action file based on "
97 	       "events from\n"
98 	       "       wpa_supplicant\n"
99 	       "  -r = try to reconnect when client socket is disconnected.\n"
100 	       "       This is useful only when used with -a.\n"
101 	       "  -B = run a daemon in the background\n"
102 	       "  default path: " CONFIG_CTRL_IFACE_DIR "\n"
103 	       "  default interface: first interface found in socket path\n");
104 	print_help(NULL);
105 }
106 
107 
wpa_cli_show_event(const char * event)108 static int wpa_cli_show_event(const char *event)
109 {
110 	const char *start;
111 
112 	start = os_strchr(event, '>');
113 	if (start == NULL)
114 		return 1;
115 
116 	start++;
117 	/*
118 	 * Skip BSS added/removed events since they can be relatively frequent
119 	 * and are likely of not much use for an interactive user.
120 	 */
121 	if (str_starts(start, WPA_EVENT_BSS_ADDED) ||
122 	    str_starts(start, WPA_EVENT_BSS_REMOVED))
123 		return 0;
124 
125 	return 1;
126 }
127 
128 
wpa_cli_open_connection(const char * ifname,int attach)129 static int wpa_cli_open_connection(const char *ifname, int attach)
130 {
131 #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
132 	ctrl_conn = wpa_ctrl_open(ifname);
133 	if (ctrl_conn == NULL)
134 		return -1;
135 
136 	if (attach && interactive)
137 		mon_conn = wpa_ctrl_open(ifname);
138 	else
139 		mon_conn = NULL;
140 #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
141 	char *cfile = NULL;
142 	int flen, res;
143 
144 	if (ifname == NULL)
145 		return -1;
146 
147 #ifdef ANDROID
148 	if (access(ctrl_iface_dir, F_OK) < 0) {
149 		cfile = os_strdup(ifname);
150 		if (cfile == NULL)
151 			return -1;
152 	}
153 #endif /* ANDROID */
154 
155 	if (client_socket_dir && client_socket_dir[0] &&
156 	    access(client_socket_dir, F_OK) < 0) {
157 		perror(client_socket_dir);
158 		os_free(cfile);
159 		return -1;
160 	}
161 
162 	if (cfile == NULL) {
163 		flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
164 		cfile = os_malloc(flen);
165 		if (cfile == NULL)
166 			return -1;
167 		res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir,
168 				  ifname);
169 		if (os_snprintf_error(flen, res)) {
170 			os_free(cfile);
171 			return -1;
172 		}
173 	}
174 
175 	ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
176 	if (ctrl_conn == NULL) {
177 		os_free(cfile);
178 		return -1;
179 	}
180 
181 	if (attach && interactive)
182 		mon_conn = wpa_ctrl_open2(cfile, client_socket_dir);
183 	else
184 		mon_conn = NULL;
185 	os_free(cfile);
186 #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
187 
188 	if (mon_conn) {
189 		if (wpa_ctrl_attach(mon_conn) == 0) {
190 			wpa_cli_attached = 1;
191 			if (interactive)
192 				eloop_register_read_sock(
193 					wpa_ctrl_get_fd(mon_conn),
194 					wpa_cli_mon_receive, NULL, NULL);
195 		} else {
196 			printf("Warning: Failed to attach to "
197 			       "wpa_supplicant.\n");
198 			wpa_cli_close_connection();
199 			return -1;
200 		}
201 	}
202 
203 	return 0;
204 }
205 
206 
wpa_cli_close_connection(void)207 static void wpa_cli_close_connection(void)
208 {
209 	if (ctrl_conn == NULL)
210 		return;
211 
212 	if (wpa_cli_attached) {
213 		wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn);
214 		wpa_cli_attached = 0;
215 	}
216 	wpa_ctrl_close(ctrl_conn);
217 	ctrl_conn = NULL;
218 	if (mon_conn) {
219 		eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn));
220 		wpa_ctrl_close(mon_conn);
221 		mon_conn = NULL;
222 	}
223 }
224 
225 
wpa_cli_msg_cb(char * msg,size_t len)226 static void wpa_cli_msg_cb(char *msg, size_t len)
227 {
228 	printf("%s\n", msg);
229 }
230 
231 
_wpa_ctrl_command(struct wpa_ctrl * ctrl,const char * cmd,int print)232 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
233 {
234 	char buf[4096];
235 	size_t len;
236 	int ret;
237 
238 	if (ctrl_conn == NULL) {
239 		printf("Not connected to wpa_supplicant - command dropped.\n");
240 		return -1;
241 	}
242 	if (ifname_prefix) {
243 		os_snprintf(buf, sizeof(buf), "IFNAME=%s %s",
244 			    ifname_prefix, cmd);
245 		buf[sizeof(buf) - 1] = '\0';
246 		cmd = buf;
247 	}
248 	len = sizeof(buf) - 1;
249 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
250 			       wpa_cli_msg_cb);
251 	if (ret == -2) {
252 		printf("'%s' command timed out.\n", cmd);
253 		return -2;
254 	} else if (ret < 0) {
255 		printf("'%s' command failed.\n", cmd);
256 		return -1;
257 	}
258 	if (print) {
259 		buf[len] = '\0';
260 		printf("%s", buf);
261 		if (interactive && len > 0 && buf[len - 1] != '\n')
262 			printf("\n");
263 	}
264 	return 0;
265 }
266 
267 #if defined(__ZEPHYR__)
wpa_ctrl_command(struct wpa_ctrl * ctrl,const char * cmd)268 int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
269 #else
270 static int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
271 #endif /* __ZEPHYR__ */
272 {
273 	return _wpa_ctrl_command(ctrl, cmd, 1);
274 }
275 
276 
wpa_cli_cmd(struct wpa_ctrl * ctrl,const char * cmd,int min_args,int argc,char * argv[])277 static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
278 		       int argc, char *argv[])
279 {
280 	char buf[4096];
281 	if (argc < min_args) {
282 		printf("Invalid %s command - at least %d argument%s "
283 		       "required.\n", cmd, min_args,
284 		       min_args > 1 ? "s are" : " is");
285 		return -1;
286 	}
287 	if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
288 		return -1;
289 	return wpa_ctrl_command(ctrl, buf);
290 }
291 
292 #if !defined(__ZEPHYR__)
wpa_cli_cmd_ifname(struct wpa_ctrl * ctrl,int argc,char * argv[])293 static int wpa_cli_cmd_ifname(struct wpa_ctrl *ctrl, int argc, char *argv[])
294 {
295 	return wpa_ctrl_command(ctrl, "IFNAME");
296 }
297 
298 
wpa_cli_cmd_status(struct wpa_ctrl * ctrl,int argc,char * argv[])299 static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
300 {
301 	if (argc > 0 && os_strcmp(argv[0], "verbose") == 0)
302 		return wpa_ctrl_command(ctrl, "STATUS-VERBOSE");
303 	if (argc > 0 && os_strcmp(argv[0], "wps") == 0)
304 		return wpa_ctrl_command(ctrl, "STATUS-WPS");
305 	if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
306 		return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
307 #ifdef ANDROID
308 	if (argc > 0 && os_strcmp(argv[0], "no_events") == 0)
309 		return wpa_ctrl_command(ctrl, "STATUS-NO_EVENTS");
310 #endif /* ANDROID */
311 	return wpa_ctrl_command(ctrl, "STATUS");
312 }
313 
314 
wpa_cli_cmd_ping(struct wpa_ctrl * ctrl,int argc,char * argv[])315 static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
316 {
317 	return wpa_ctrl_command(ctrl, "PING");
318 }
319 
320 
wpa_cli_cmd_relog(struct wpa_ctrl * ctrl,int argc,char * argv[])321 static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
322 {
323 	return wpa_ctrl_command(ctrl, "RELOG");
324 }
325 
326 
wpa_cli_cmd_note(struct wpa_ctrl * ctrl,int argc,char * argv[])327 static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[])
328 {
329 	return wpa_cli_cmd(ctrl, "NOTE", 1, argc, argv);
330 }
331 
332 
wpa_cli_cmd_mib(struct wpa_ctrl * ctrl,int argc,char * argv[])333 static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
334 {
335 	return wpa_ctrl_command(ctrl, "MIB");
336 }
337 
338 
wpa_cli_cmd_pmksa(struct wpa_ctrl * ctrl,int argc,char * argv[])339 static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
340 {
341 	return wpa_ctrl_command(ctrl, "PMKSA");
342 }
343 
344 
wpa_cli_cmd_pmksa_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])345 static int wpa_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
346 				   char *argv[])
347 {
348 	return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
349 }
350 
351 
352 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
353 
wpa_cli_cmd_pmksa_get(struct wpa_ctrl * ctrl,int argc,char * argv[])354 static int wpa_cli_cmd_pmksa_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
355 {
356 	return wpa_cli_cmd(ctrl, "PMKSA_GET", 1, argc, argv);
357 }
358 
359 
wpa_cli_cmd_pmksa_add(struct wpa_ctrl * ctrl,int argc,char * argv[])360 static int wpa_cli_cmd_pmksa_add(struct wpa_ctrl *ctrl, int argc, char *argv[])
361 {
362 	return wpa_cli_cmd(ctrl, "PMKSA_ADD", 8, argc, argv);
363 }
364 
365 
366 #ifdef CONFIG_MESH
367 
wpa_cli_mesh_cmd_pmksa_get(struct wpa_ctrl * ctrl,int argc,char * argv[])368 static int wpa_cli_mesh_cmd_pmksa_get(struct wpa_ctrl *ctrl, int argc,
369 				      char *argv[])
370 {
371 	return wpa_cli_cmd(ctrl, "MESH_PMKSA_GET", 1, argc, argv);
372 }
373 
374 
wpa_cli_mesh_cmd_pmksa_add(struct wpa_ctrl * ctrl,int argc,char * argv[])375 static int wpa_cli_mesh_cmd_pmksa_add(struct wpa_ctrl *ctrl, int argc,
376 				      char *argv[])
377 {
378 	return wpa_cli_cmd(ctrl, "MESH_PMKSA_ADD", 4, argc, argv);
379 }
380 
381 #endif /* CONFIG_MESH */
382 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
383 
384 
wpa_cli_cmd_help(struct wpa_ctrl * ctrl,int argc,char * argv[])385 static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
386 {
387 	print_help(argc > 0 ? argv[0] : NULL);
388 	return 0;
389 }
390 
391 
wpa_cli_complete_help(const char * str,int pos)392 static char ** wpa_cli_complete_help(const char *str, int pos)
393 {
394 	int arg = get_cmd_arg_num(str, pos);
395 	char **res = NULL;
396 
397 	switch (arg) {
398 	case 1:
399 		res = wpa_list_cmd_list();
400 		break;
401 	}
402 
403 	return res;
404 }
405 
406 
wpa_cli_cmd_license(struct wpa_ctrl * ctrl,int argc,char * argv[])407 static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
408 {
409 	printf("%s\n\n%s\n", wpa_cli_version, cli_full_license);
410 	return 0;
411 }
412 
413 
wpa_cli_cmd_quit(struct wpa_ctrl * ctrl,int argc,char * argv[])414 static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
415 {
416 	wpa_cli_quit = 1;
417 	if (interactive)
418 		eloop_terminate();
419 	return 0;
420 }
421 
422 
wpa_cli_cmd_set(struct wpa_ctrl * ctrl,int argc,char * argv[])423 static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
424 {
425 	char cmd[256];
426 	int res;
427 
428 	if (argc == 1) {
429 		res = os_snprintf(cmd, sizeof(cmd), "SET %s ", argv[0]);
430 		if (os_snprintf_error(sizeof(cmd), res)) {
431 			printf("Too long SET command.\n");
432 			return -1;
433 		}
434 		return wpa_ctrl_command(ctrl, cmd);
435 	}
436 
437 	return wpa_cli_cmd(ctrl, "SET", 2, argc, argv);
438 }
439 
440 
wpa_cli_complete_set(const char * str,int pos)441 static char ** wpa_cli_complete_set(const char *str, int pos)
442 {
443 	int arg = get_cmd_arg_num(str, pos);
444 	const char *fields[] = {
445 		/* runtime values */
446 		"EAPOL::heldPeriod", "EAPOL::authPeriod", "EAPOL::startPeriod",
447 		"EAPOL::maxStart", "dot11RSNAConfigPMKLifetime",
448 		"dot11RSNAConfigPMKReauthThreshold", "dot11RSNAConfigSATimeout",
449 		"wps_fragment_size", "wps_version_number", "ampdu",
450 		"tdls_testing", "tdls_disabled", "pno", "radio_disabled",
451 		"uapsd", "ps", "wifi_display", "bssid_filter", "disallow_aps",
452 		"no_keep_alive",
453 		/* global configuration parameters */
454 #ifdef CONFIG_CTRL_IFACE
455 		"ctrl_interface", "no_ctrl_interface", "ctrl_interface_group",
456 #endif /* CONFIG_CTRL_IFACE */
457 		"eapol_version", "ap_scan", "bgscan",
458 #ifdef CONFIG_MESH
459 		"user_mpm", "max_peer_links", "mesh_max_inactivity",
460 		"dot11RSNASAERetransPeriod",
461 #endif /* CONFIG_MESH */
462 		"disable_scan_offload", "fast_reauth", "opensc_engine_path",
463 		"pkcs11_engine_path", "pkcs11_module_path", "openssl_ciphers",
464 		"pcsc_reader", "pcsc_pin", "external_sim", "driver_param",
465 		"dot11RSNAConfigPMKLifetime",
466 		"dot11RSNAConfigPMKReauthThreshold",
467 		"dot11RSNAConfigSATimeout",
468 #ifndef CONFIG_NO_CONFIG_WRITE
469 		"update_config",
470 #endif /* CONFIG_NO_CONFIG_WRITE */
471 		"load_dynamic_eap",
472 #ifdef CONFIG_WPS
473 		"uuid", "device_name", "manufacturer", "model_name",
474 		"model_number", "serial_number", "device_type", "os_version",
475 		"config_methods", "wps_cred_processing", "wps_vendor_ext_m1",
476 #endif /* CONFIG_WPS */
477 #ifdef CONFIG_P2P
478 		"sec_device_type",
479 		"p2p_listen_reg_class", "p2p_listen_channel",
480 		"p2p_oper_reg_class", "p2p_oper_channel", "p2p_go_intent",
481 		"p2p_ssid_postfix", "persistent_reconnect", "p2p_intra_bss",
482 		"p2p_group_idle", "p2p_passphrase_len", "p2p_pref_chan",
483 		"p2p_no_go_freq", "p2p_add_cli_chan",
484 		"p2p_optimize_listen_chan", "p2p_go_ht40", "p2p_go_vht",
485 		"p2p_disabled", "p2p_go_ctwindow", "p2p_no_group_iface",
486 		"p2p_ignore_shared_freq", "ip_addr_go", "ip_addr_mask",
487 		"ip_addr_start", "ip_addr_end", "p2p_go_edmg",
488 #endif /* CONFIG_P2P */
489 		"country", "bss_max_count", "bss_expiration_age",
490 		"bss_expiration_scan_count", "filter_ssids", "filter_rssi",
491 		"max_num_sta", "disassoc_low_ack", "ap_isolate",
492 #ifdef CONFIG_HS20
493 		"hs20",
494 #endif /* CONFIG_HS20 */
495 		"interworking", "hessid", "access_network_type", "pbc_in_m1",
496 		"go_interworking", "go_access_network_type", "go_internet",
497 		"go_venue_group", "go_venue_type",
498 		"autoscan", "wps_nfc_dev_pw_id", "wps_nfc_dh_pubkey",
499 		"wps_nfc_dh_privkey", "wps_nfc_dev_pw", "ext_password_backend",
500 		"p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
501 		"sae_groups", "dtim_period", "beacon_int",
502 		"ap_vendor_elements", "ignore_old_scan_res", "freq_list",
503 		"scan_cur_freq", "scan_res_valid_for_connect",
504 		"sched_scan_interval",
505 		"tdls_external_control", "osu_dir", "wowlan_triggers",
506 		"p2p_search_delay", "mac_addr", "rand_addr_lifetime",
507 		"preassoc_mac_addr", "key_mgmt_offload", "passive_scan",
508 		"reassoc_same_bss_optim", "wps_priority",
509 		"ap_assocresp_elements",
510 #ifdef CONFIG_TESTING_OPTIONS
511 		"ignore_auth_resp",
512 #endif /* CONFIG_TESTING_OPTIONS */
513 		"relative_rssi", "relative_band_adjust",
514 		"extended_key_id",
515 	};
516 	int i, num_fields = ARRAY_SIZE(fields);
517 
518 	if (arg == 1) {
519 		char **res = os_calloc(num_fields + 1, sizeof(char *));
520 		if (res == NULL)
521 			return NULL;
522 		for (i = 0; i < num_fields; i++) {
523 			res[i] = os_strdup(fields[i]);
524 			if (res[i] == NULL)
525 				return res;
526 		}
527 		return res;
528 	}
529 
530 	if (arg > 1 && os_strncasecmp(str, "set bssid_filter ", 17) == 0)
531 		return cli_txt_list_array(&bsses);
532 
533 	return NULL;
534 }
535 
wpa_cli_cmd_dump(struct wpa_ctrl * ctrl,int argc,char * argv[])536 static int wpa_cli_cmd_dump(struct wpa_ctrl *ctrl, int argc, char *argv[])
537 {
538 	return wpa_ctrl_command(ctrl, "DUMP");
539 }
540 
541 
wpa_cli_cmd_driver_flags(struct wpa_ctrl * ctrl,int argc,char * argv[])542 static int wpa_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
543 				    char *argv[])
544 {
545 	return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
546 }
547 
548 
wpa_cli_cmd_get(struct wpa_ctrl * ctrl,int argc,char * argv[])549 static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
550 {
551 	return wpa_cli_cmd(ctrl, "GET", 1, argc, argv);
552 }
553 
554 
wpa_cli_complete_get(const char * str,int pos)555 static char ** wpa_cli_complete_get(const char *str, int pos)
556 {
557 	int arg = get_cmd_arg_num(str, pos);
558 	const char *fields[] = {
559 #ifdef CONFIG_CTRL_IFACE
560 		"ctrl_interface", "ctrl_interface_group",
561 #endif /* CONFIG_CTRL_IFACE */
562 		"eapol_version", "ap_scan",
563 #ifdef CONFIG_MESH
564 		"user_mpm", "max_peer_links", "mesh_max_inactivity",
565 #endif /* CONFIG_MESH */
566 		"disable_scan_offload", "fast_reauth", "opensc_engine_path",
567 		"pkcs11_engine_path", "pkcs11_module_path", "openssl_ciphers",
568 		"pcsc_reader", "pcsc_pin", "external_sim", "driver_param",
569 		"dot11RSNAConfigPMKLifetime",
570 		"dot11RSNAConfigPMKReauthThreshold",
571 		"dot11RSNAConfigSATimeout",
572 #ifndef CONFIG_NO_CONFIG_WRITE
573 		"update_config",
574 #endif /* CONFIG_NO_CONFIG_WRITE */
575 #ifdef CONFIG_WPS
576 		"device_name", "manufacturer", "model_name", "model_number",
577 		"serial_number", "config_methods", "wps_cred_processing",
578 #endif /* CONFIG_WPS */
579 #ifdef CONFIG_P2P
580 		"p2p_listen_reg_class", "p2p_listen_channel",
581 		"p2p_oper_reg_class", "p2p_oper_channel", "p2p_go_intent",
582 		"p2p_ssid_postfix", "persistent_reconnect", "p2p_intra_bss",
583 		"p2p_group_idle", "p2p_passphrase_len", "p2p_add_cli_chan",
584 		"p2p_optimize_listen_chan", "p2p_go_ht40", "p2p_go_vht",
585 		"p2p_disabled", "p2p_go_ctwindow", "p2p_no_group_iface",
586 		"p2p_ignore_shared_freq", "ip_addr_go", "ip_addr_mask",
587 		"ip_addr_start", "ip_addr_end",
588 #endif /* CONFIG_P2P */
589 		"bss_max_count", "bss_expiration_age",
590 		"bss_expiration_scan_count", "filter_ssids", "filter_rssi",
591 		"max_num_sta", "disassoc_low_ack", "ap_isolate",
592 #ifdef CONFIG_HS20
593 		"hs20",
594 #endif /* CONFIG_HS20 */
595 		"interworking", "access_network_type", "pbc_in_m1", "autoscan",
596 		"go_interworking", "go_access_network_type", "go_internet",
597 		"go_venue_group", "go_venue_type",
598 		"wps_nfc_dev_pw_id", "ext_password_backend",
599 		"p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
600 		"dtim_period", "beacon_int", "ignore_old_scan_res",
601 		"scan_cur_freq", "scan_res_valid_for_connect",
602 		"sched_scan_interval",
603 		"sched_scan_start_delay",
604 		"tdls_external_control", "osu_dir", "wowlan_triggers",
605 		"p2p_search_delay", "mac_addr", "rand_addr_lifetime",
606 		"preassoc_mac_addr", "key_mgmt_offload", "passive_scan",
607 		"reassoc_same_bss_optim", "extended_key_id"
608 	};
609 	int i, num_fields = ARRAY_SIZE(fields);
610 
611 	if (arg == 1) {
612 		char **res = os_calloc(num_fields + 1, sizeof(char *));
613 		if (res == NULL)
614 			return NULL;
615 		for (i = 0; i < num_fields; i++) {
616 			res[i] = os_strdup(fields[i]);
617 			if (res[i] == NULL)
618 				return res;
619 		}
620 		return res;
621 	}
622 
623 	return NULL;
624 }
625 
626 
wpa_cli_cmd_logoff(struct wpa_ctrl * ctrl,int argc,char * argv[])627 static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
628 {
629 	return wpa_ctrl_command(ctrl, "LOGOFF");
630 }
631 
632 
wpa_cli_cmd_logon(struct wpa_ctrl * ctrl,int argc,char * argv[])633 static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
634 {
635 	return wpa_ctrl_command(ctrl, "LOGON");
636 }
637 
638 
wpa_cli_cmd_reassociate(struct wpa_ctrl * ctrl,int argc,char * argv[])639 static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
640 				   char *argv[])
641 {
642 	return wpa_ctrl_command(ctrl, "REASSOCIATE");
643 }
644 
645 
wpa_cli_cmd_reattach(struct wpa_ctrl * ctrl,int argc,char * argv[])646 static int wpa_cli_cmd_reattach(struct wpa_ctrl *ctrl, int argc, char *argv[])
647 {
648 	return wpa_ctrl_command(ctrl, "REATTACH");
649 }
650 
651 
wpa_cli_cmd_preauthenticate(struct wpa_ctrl * ctrl,int argc,char * argv[])652 static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
653 				       char *argv[])
654 {
655 	return wpa_cli_cmd(ctrl, "PREAUTH", 1, argc, argv);
656 }
657 
658 
wpa_cli_cmd_ap_scan(struct wpa_ctrl * ctrl,int argc,char * argv[])659 static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
660 {
661 	return wpa_cli_cmd(ctrl, "AP_SCAN", 1, argc, argv);
662 }
663 
664 
wpa_cli_cmd_scan_interval(struct wpa_ctrl * ctrl,int argc,char * argv[])665 static int wpa_cli_cmd_scan_interval(struct wpa_ctrl *ctrl, int argc,
666 				     char *argv[])
667 {
668 	return wpa_cli_cmd(ctrl, "SCAN_INTERVAL", 1, argc, argv);
669 }
670 
671 
wpa_cli_cmd_bss_expire_age(struct wpa_ctrl * ctrl,int argc,char * argv[])672 static int wpa_cli_cmd_bss_expire_age(struct wpa_ctrl *ctrl, int argc,
673 				      char *argv[])
674 {
675 	return wpa_cli_cmd(ctrl, "BSS_EXPIRE_AGE", 1, argc, argv);
676 }
677 
678 
wpa_cli_cmd_bss_expire_count(struct wpa_ctrl * ctrl,int argc,char * argv[])679 static int wpa_cli_cmd_bss_expire_count(struct wpa_ctrl *ctrl, int argc,
680 				        char *argv[])
681 {
682 	return wpa_cli_cmd(ctrl, "BSS_EXPIRE_COUNT", 1, argc, argv);
683 }
684 
685 
wpa_cli_cmd_bss_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])686 static int wpa_cli_cmd_bss_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
687 {
688 	char cmd[256];
689 	int res;
690 
691 	if (argc < 1)
692 		res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH 0");
693 	else
694 		res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH %s", argv[0]);
695 	if (os_snprintf_error(sizeof(cmd), res)) {
696 		printf("Too long BSS_FLUSH command.\n");
697 		return -1;
698 	}
699 	return wpa_ctrl_command(ctrl, cmd);
700 }
701 
702 
wpa_cli_cmd_ft_ds(struct wpa_ctrl * ctrl,int argc,char * argv[])703 static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
704 {
705 	return wpa_cli_cmd(ctrl, "FT_DS", 1, argc, argv);
706 }
707 
708 
wpa_cli_cmd_wps_pbc(struct wpa_ctrl * ctrl,int argc,char * argv[])709 static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
710 {
711 	return wpa_cli_cmd(ctrl, "WPS_PBC", 0, argc, argv);
712 }
713 
714 
wpa_cli_cmd_wps_pin(struct wpa_ctrl * ctrl,int argc,char * argv[])715 static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
716 {
717 	if (argc == 0) {
718 		printf("Invalid WPS_PIN command: need one or two arguments:\n"
719 		       "- BSSID: use 'any' to select any\n"
720 		       "- PIN: optional, used only with devices that have no "
721 		       "display\n");
722 		return -1;
723 	}
724 
725 	return wpa_cli_cmd(ctrl, "WPS_PIN", 1, argc, argv);
726 }
727 
728 
wpa_cli_cmd_wps_check_pin(struct wpa_ctrl * ctrl,int argc,char * argv[])729 static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
730 				     char *argv[])
731 {
732 	return wpa_cli_cmd(ctrl, "WPS_CHECK_PIN", 1, argc, argv);
733 }
734 
735 
wpa_cli_cmd_wps_cancel(struct wpa_ctrl * ctrl,int argc,char * argv[])736 static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
737 				  char *argv[])
738 {
739 	return wpa_ctrl_command(ctrl, "WPS_CANCEL");
740 }
741 
742 
743 #ifdef CONFIG_WPS_NFC
744 
wpa_cli_cmd_wps_nfc(struct wpa_ctrl * ctrl,int argc,char * argv[])745 static int wpa_cli_cmd_wps_nfc(struct wpa_ctrl *ctrl, int argc, char *argv[])
746 {
747 	return wpa_cli_cmd(ctrl, "WPS_NFC", 0, argc, argv);
748 }
749 
750 
wpa_cli_cmd_wps_nfc_config_token(struct wpa_ctrl * ctrl,int argc,char * argv[])751 static int wpa_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
752 					    char *argv[])
753 {
754 	return wpa_cli_cmd(ctrl, "WPS_NFC_CONFIG_TOKEN", 1, argc, argv);
755 }
756 
757 
wpa_cli_cmd_wps_nfc_token(struct wpa_ctrl * ctrl,int argc,char * argv[])758 static int wpa_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, int argc,
759 				     char *argv[])
760 {
761 	return wpa_cli_cmd(ctrl, "WPS_NFC_TOKEN", 1, argc, argv);
762 }
763 
764 
wpa_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl * ctrl,int argc,char * argv[])765 static int wpa_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
766 					char *argv[])
767 {
768 	int ret;
769 	char *buf;
770 	size_t buflen;
771 
772 	if (argc != 1) {
773 		printf("Invalid 'wps_nfc_tag_read' command - one argument "
774 		       "is required.\n");
775 		return -1;
776 	}
777 
778 	buflen = 18 + os_strlen(argv[0]);
779 	buf = os_malloc(buflen);
780 	if (buf == NULL)
781 		return -1;
782 	os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
783 
784 	ret = wpa_ctrl_command(ctrl, buf);
785 	os_free(buf);
786 
787 	return ret;
788 }
789 
790 
wpa_cli_cmd_nfc_get_handover_req(struct wpa_ctrl * ctrl,int argc,char * argv[])791 static int wpa_cli_cmd_nfc_get_handover_req(struct wpa_ctrl *ctrl, int argc,
792 					    char *argv[])
793 {
794 	return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_REQ", 2, argc, argv);
795 }
796 
797 
wpa_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl * ctrl,int argc,char * argv[])798 static int wpa_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, int argc,
799 					    char *argv[])
800 {
801 	return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_SEL", 2, argc, argv);
802 }
803 
804 
wpa_cli_cmd_nfc_report_handover(struct wpa_ctrl * ctrl,int argc,char * argv[])805 static int wpa_cli_cmd_nfc_report_handover(struct wpa_ctrl *ctrl, int argc,
806 					   char *argv[])
807 {
808 	return wpa_cli_cmd(ctrl, "NFC_REPORT_HANDOVER", 4, argc, argv);
809 }
810 
811 #endif /* CONFIG_WPS_NFC */
812 
813 
wpa_cli_cmd_wps_reg(struct wpa_ctrl * ctrl,int argc,char * argv[])814 static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
815 {
816 	char cmd[256];
817 	int res;
818 
819 	if (argc == 2)
820 		res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
821 				  argv[0], argv[1]);
822 	else if (argc == 5 || argc == 6) {
823 		char ssid_hex[2 * SSID_MAX_LEN + 1];
824 		char key_hex[2 * 64 + 1];
825 		int i;
826 
827 		ssid_hex[0] = '\0';
828 		for (i = 0; i < SSID_MAX_LEN; i++) {
829 			if (argv[2][i] == '\0')
830 				break;
831 			os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
832 		}
833 
834 		key_hex[0] = '\0';
835 		if (argc == 6) {
836 			for (i = 0; i < 64; i++) {
837 				if (argv[5][i] == '\0')
838 					break;
839 				os_snprintf(&key_hex[i * 2], 3, "%02x",
840 					    argv[5][i]);
841 			}
842 		}
843 
844 		res = os_snprintf(cmd, sizeof(cmd),
845 				  "WPS_REG %s %s %s %s %s %s",
846 				  argv[0], argv[1], ssid_hex, argv[3], argv[4],
847 				  key_hex);
848 	} else {
849 		printf("Invalid WPS_REG command: need two arguments:\n"
850 		       "- BSSID of the target AP\n"
851 		       "- AP PIN\n");
852 		printf("Alternatively, six arguments can be used to "
853 		       "reconfigure the AP:\n"
854 		       "- BSSID of the target AP\n"
855 		       "- AP PIN\n"
856 		       "- new SSID\n"
857 		       "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
858 		       "- new encr (NONE, WEP, TKIP, CCMP)\n"
859 		       "- new key\n");
860 		return -1;
861 	}
862 
863 	if (os_snprintf_error(sizeof(cmd), res)) {
864 		printf("Too long WPS_REG command.\n");
865 		return -1;
866 	}
867 	return wpa_ctrl_command(ctrl, cmd);
868 }
869 
870 
wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl * ctrl,int argc,char * argv[])871 static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
872 				  char *argv[])
873 {
874 	return wpa_cli_cmd(ctrl, "WPS_AP_PIN", 1, argc, argv);
875 }
876 
877 
wpa_cli_cmd_wps_er_start(struct wpa_ctrl * ctrl,int argc,char * argv[])878 static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc,
879 				    char *argv[])
880 {
881 	return wpa_cli_cmd(ctrl, "WPS_ER_START", 0, argc, argv);
882 }
883 
884 
wpa_cli_cmd_wps_er_stop(struct wpa_ctrl * ctrl,int argc,char * argv[])885 static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc,
886 				   char *argv[])
887 {
888 	return wpa_ctrl_command(ctrl, "WPS_ER_STOP");
889 
890 }
891 
892 
wpa_cli_cmd_wps_er_pin(struct wpa_ctrl * ctrl,int argc,char * argv[])893 static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc,
894 				  char *argv[])
895 {
896 	if (argc < 2) {
897 		printf("Invalid WPS_ER_PIN command: need at least two "
898 		       "arguments:\n"
899 		       "- UUID: use 'any' to select any\n"
900 		       "- PIN: Enrollee PIN\n"
901 		       "optional: - Enrollee MAC address\n");
902 		return -1;
903 	}
904 
905 	return wpa_cli_cmd(ctrl, "WPS_ER_PIN", 2, argc, argv);
906 }
907 
908 
wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl * ctrl,int argc,char * argv[])909 static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc,
910 				  char *argv[])
911 {
912 	return wpa_cli_cmd(ctrl, "WPS_ER_PBC", 1, argc, argv);
913 }
914 
915 
wpa_cli_cmd_wps_er_learn(struct wpa_ctrl * ctrl,int argc,char * argv[])916 static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc,
917 				    char *argv[])
918 {
919 	if (argc != 2) {
920 		printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
921 		       "- UUID: specify which AP to use\n"
922 		       "- PIN: AP PIN\n");
923 		return -1;
924 	}
925 
926 	return wpa_cli_cmd(ctrl, "WPS_ER_LEARN", 2, argc, argv);
927 }
928 
929 
wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl * ctrl,int argc,char * argv[])930 static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc,
931 					 char *argv[])
932 {
933 	if (argc != 2) {
934 		printf("Invalid WPS_ER_SET_CONFIG command: need two "
935 		       "arguments:\n"
936 		       "- UUID: specify which AP to use\n"
937 		       "- Network configuration id\n");
938 		return -1;
939 	}
940 
941 	return wpa_cli_cmd(ctrl, "WPS_ER_SET_CONFIG", 2, argc, argv);
942 }
943 
944 
wpa_cli_cmd_wps_er_config(struct wpa_ctrl * ctrl,int argc,char * argv[])945 static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
946 				     char *argv[])
947 {
948 	char cmd[256];
949 	int res;
950 
951 	if (argc == 5 || argc == 6) {
952 		char ssid_hex[2 * SSID_MAX_LEN + 1];
953 		char key_hex[2 * 64 + 1];
954 		int i;
955 
956 		ssid_hex[0] = '\0';
957 		for (i = 0; i < SSID_MAX_LEN; i++) {
958 			if (argv[2][i] == '\0')
959 				break;
960 			os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
961 		}
962 
963 		key_hex[0] = '\0';
964 		if (argc == 6) {
965 			for (i = 0; i < 64; i++) {
966 				if (argv[5][i] == '\0')
967 					break;
968 				os_snprintf(&key_hex[i * 2], 3, "%02x",
969 					    argv[5][i]);
970 			}
971 		}
972 
973 		res = os_snprintf(cmd, sizeof(cmd),
974 				  "WPS_ER_CONFIG %s %s %s %s %s %s",
975 				  argv[0], argv[1], ssid_hex, argv[3], argv[4],
976 				  key_hex);
977 	} else {
978 		printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
979 		       "- AP UUID\n"
980 		       "- AP PIN\n"
981 		       "- new SSID\n"
982 		       "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
983 		       "- new encr (NONE, WEP, TKIP, CCMP)\n"
984 		       "- new key\n");
985 		return -1;
986 	}
987 
988 	if (os_snprintf_error(sizeof(cmd), res)) {
989 		printf("Too long WPS_ER_CONFIG command.\n");
990 		return -1;
991 	}
992 	return wpa_ctrl_command(ctrl, cmd);
993 }
994 
995 
996 #ifdef CONFIG_WPS_NFC
wpa_cli_cmd_wps_er_nfc_config_token(struct wpa_ctrl * ctrl,int argc,char * argv[])997 static int wpa_cli_cmd_wps_er_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
998 					       char *argv[])
999 {
1000 	if (argc != 2) {
1001 		printf("Invalid WPS_ER_NFC_CONFIG_TOKEN command: need two "
1002 		       "arguments:\n"
1003 		       "- WPS/NDEF: token format\n"
1004 		       "- UUID: specify which AP to use\n");
1005 		return -1;
1006 	}
1007 
1008 	return wpa_cli_cmd(ctrl, "WPS_ER_NFC_CONFIG_TOKEN", 2, argc, argv);
1009 }
1010 #endif /* CONFIG_WPS_NFC */
1011 
1012 
wpa_cli_cmd_ibss_rsn(struct wpa_ctrl * ctrl,int argc,char * argv[])1013 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
1014 {
1015 	return wpa_cli_cmd(ctrl, "IBSS_RSN", 1, argc, argv);
1016 }
1017 
1018 
wpa_cli_cmd_level(struct wpa_ctrl * ctrl,int argc,char * argv[])1019 static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1020 {
1021 	return wpa_cli_cmd(ctrl, "LEVEL", 1, argc, argv);
1022 }
1023 
1024 
wpa_cli_cmd_identity(struct wpa_ctrl * ctrl,int argc,char * argv[])1025 static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
1026 {
1027 	char cmd[256], *pos, *end;
1028 	int i, ret;
1029 
1030 	if (argc < 2) {
1031 		printf("Invalid IDENTITY command: needs two arguments "
1032 		       "(network id and identity)\n");
1033 		return -1;
1034 	}
1035 
1036 	end = cmd + sizeof(cmd);
1037 	pos = cmd;
1038 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
1039 			  argv[0], argv[1]);
1040 	if (os_snprintf_error(end - pos, ret)) {
1041 		printf("Too long IDENTITY command.\n");
1042 		return -1;
1043 	}
1044 	pos += ret;
1045 	for (i = 2; i < argc; i++) {
1046 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1047 		if (os_snprintf_error(end - pos, ret)) {
1048 			printf("Too long IDENTITY command.\n");
1049 			return -1;
1050 		}
1051 		pos += ret;
1052 	}
1053 
1054 	return wpa_ctrl_command(ctrl, cmd);
1055 }
1056 
1057 
wpa_cli_cmd_password(struct wpa_ctrl * ctrl,int argc,char * argv[])1058 static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
1059 {
1060 	char cmd[256], *pos, *end;
1061 	int i, ret;
1062 
1063 	if (argc < 2) {
1064 		printf("Invalid PASSWORD command: needs two arguments "
1065 		       "(network id and password)\n");
1066 		return -1;
1067 	}
1068 
1069 	end = cmd + sizeof(cmd);
1070 	pos = cmd;
1071 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
1072 			  argv[0], argv[1]);
1073 	if (os_snprintf_error(end - pos, ret)) {
1074 		printf("Too long PASSWORD command.\n");
1075 		return -1;
1076 	}
1077 	pos += ret;
1078 	for (i = 2; i < argc; i++) {
1079 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1080 		if (os_snprintf_error(end - pos, ret)) {
1081 			printf("Too long PASSWORD command.\n");
1082 			return -1;
1083 		}
1084 		pos += ret;
1085 	}
1086 
1087 	return wpa_ctrl_command(ctrl, cmd);
1088 }
1089 
1090 
wpa_cli_cmd_new_password(struct wpa_ctrl * ctrl,int argc,char * argv[])1091 static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
1092 				    char *argv[])
1093 {
1094 	char cmd[256], *pos, *end;
1095 	int i, ret;
1096 
1097 	if (argc < 2) {
1098 		printf("Invalid NEW_PASSWORD command: needs two arguments "
1099 		       "(network id and password)\n");
1100 		return -1;
1101 	}
1102 
1103 	end = cmd + sizeof(cmd);
1104 	pos = cmd;
1105 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
1106 			  argv[0], argv[1]);
1107 	if (os_snprintf_error(end - pos, ret)) {
1108 		printf("Too long NEW_PASSWORD command.\n");
1109 		return -1;
1110 	}
1111 	pos += ret;
1112 	for (i = 2; i < argc; i++) {
1113 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1114 		if (os_snprintf_error(end - pos, ret)) {
1115 			printf("Too long NEW_PASSWORD command.\n");
1116 			return -1;
1117 		}
1118 		pos += ret;
1119 	}
1120 
1121 	return wpa_ctrl_command(ctrl, cmd);
1122 }
1123 
1124 
wpa_cli_cmd_pin(struct wpa_ctrl * ctrl,int argc,char * argv[])1125 static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
1126 {
1127 	char cmd[256], *pos, *end;
1128 	int i, ret;
1129 
1130 	if (argc < 2) {
1131 		printf("Invalid PIN command: needs two arguments "
1132 		       "(network id and pin)\n");
1133 		return -1;
1134 	}
1135 
1136 	end = cmd + sizeof(cmd);
1137 	pos = cmd;
1138 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
1139 			  argv[0], argv[1]);
1140 	if (os_snprintf_error(end - pos, ret)) {
1141 		printf("Too long PIN command.\n");
1142 		return -1;
1143 	}
1144 	pos += ret;
1145 	for (i = 2; i < argc; i++) {
1146 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1147 		if (os_snprintf_error(end - pos, ret)) {
1148 			printf("Too long PIN command.\n");
1149 			return -1;
1150 		}
1151 		pos += ret;
1152 	}
1153 	return wpa_ctrl_command(ctrl, cmd);
1154 }
1155 
1156 
wpa_cli_cmd_otp(struct wpa_ctrl * ctrl,int argc,char * argv[])1157 static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
1158 {
1159 	char cmd[256], *pos, *end;
1160 	int i, ret;
1161 
1162 	if (argc < 2) {
1163 		printf("Invalid OTP command: needs two arguments (network "
1164 		       "id and password)\n");
1165 		return -1;
1166 	}
1167 
1168 	end = cmd + sizeof(cmd);
1169 	pos = cmd;
1170 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
1171 			  argv[0], argv[1]);
1172 	if (os_snprintf_error(end - pos, ret)) {
1173 		printf("Too long OTP command.\n");
1174 		return -1;
1175 	}
1176 	pos += ret;
1177 	for (i = 2; i < argc; i++) {
1178 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1179 		if (os_snprintf_error(end - pos, ret)) {
1180 			printf("Too long OTP command.\n");
1181 			return -1;
1182 		}
1183 		pos += ret;
1184 	}
1185 
1186 	return wpa_ctrl_command(ctrl, cmd);
1187 }
1188 
1189 
wpa_cli_cmd_sim(struct wpa_ctrl * ctrl,int argc,char * argv[])1190 static int wpa_cli_cmd_sim(struct wpa_ctrl *ctrl, int argc, char *argv[])
1191 {
1192 	char cmd[256], *pos, *end;
1193 	int i, ret;
1194 
1195 	if (argc < 2) {
1196 		printf("Invalid SIM command: needs two arguments "
1197 		       "(network id and SIM operation response)\n");
1198 		return -1;
1199 	}
1200 
1201 	end = cmd + sizeof(cmd);
1202 	pos = cmd;
1203 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "SIM-%s:%s",
1204 			  argv[0], argv[1]);
1205 	if (os_snprintf_error(end - pos, ret)) {
1206 		printf("Too long SIM command.\n");
1207 		return -1;
1208 	}
1209 	pos += ret;
1210 	for (i = 2; i < argc; i++) {
1211 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1212 		if (os_snprintf_error(end - pos, ret)) {
1213 			printf("Too long SIM command.\n");
1214 			return -1;
1215 		}
1216 		pos += ret;
1217 	}
1218 	return wpa_ctrl_command(ctrl, cmd);
1219 }
1220 
1221 
wpa_cli_cmd_psk_passphrase(struct wpa_ctrl * ctrl,int argc,char * argv[])1222 static int wpa_cli_cmd_psk_passphrase(struct wpa_ctrl *ctrl, int argc,
1223 				      char *argv[])
1224 {
1225 	char cmd[256], *pos, *end;
1226 	int i, ret;
1227 
1228 	if (argc < 2) {
1229 		printf("Invalid PSK_PASSPHRASE command: needs two arguments (network id and PSK/passphrase)\n");
1230 		return -1;
1231 	}
1232 
1233 	end = cmd + sizeof(cmd);
1234 	pos = cmd;
1235 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PSK_PASSPHRASE-%s:%s",
1236 			  argv[0], argv[1]);
1237 	if (os_snprintf_error(end - pos, ret)) {
1238 		printf("Too long PSK_PASSPHRASE command.\n");
1239 		return -1;
1240 	}
1241 	pos += ret;
1242 	for (i = 2; i < argc; i++) {
1243 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1244 		if (os_snprintf_error(end - pos, ret)) {
1245 			printf("Too long PSK_PASSPHRASE command.\n");
1246 			return -1;
1247 		}
1248 		pos += ret;
1249 	}
1250 
1251 	return wpa_ctrl_command(ctrl, cmd);
1252 }
1253 
1254 
wpa_cli_cmd_passphrase(struct wpa_ctrl * ctrl,int argc,char * argv[])1255 static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
1256 				  char *argv[])
1257 {
1258 	char cmd[256], *pos, *end;
1259 	int i, ret;
1260 
1261 	if (argc < 2) {
1262 		printf("Invalid PASSPHRASE command: needs two arguments "
1263 		       "(network id and passphrase)\n");
1264 		return -1;
1265 	}
1266 
1267 	end = cmd + sizeof(cmd);
1268 	pos = cmd;
1269 	ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
1270 			  argv[0], argv[1]);
1271 	if (os_snprintf_error(end - pos, ret)) {
1272 		printf("Too long PASSPHRASE command.\n");
1273 		return -1;
1274 	}
1275 	pos += ret;
1276 	for (i = 2; i < argc; i++) {
1277 		ret = os_snprintf(pos, end - pos, " %s", argv[i]);
1278 		if (os_snprintf_error(end - pos, ret)) {
1279 			printf("Too long PASSPHRASE command.\n");
1280 			return -1;
1281 		}
1282 		pos += ret;
1283 	}
1284 
1285 	return wpa_ctrl_command(ctrl, cmd);
1286 }
1287 
1288 
wpa_cli_cmd_bssid(struct wpa_ctrl * ctrl,int argc,char * argv[])1289 static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
1290 {
1291 	if (argc < 2) {
1292 		printf("Invalid BSSID command: needs two arguments (network "
1293 		       "id and BSSID)\n");
1294 		return -1;
1295 	}
1296 
1297 	return wpa_cli_cmd(ctrl, "BSSID", 2, argc, argv);
1298 }
1299 
1300 
wpa_cli_cmd_bssid_ignore(struct wpa_ctrl * ctrl,int argc,char * argv[])1301 static int wpa_cli_cmd_bssid_ignore(struct wpa_ctrl *ctrl, int argc,
1302 				    char *argv[])
1303 {
1304 	return wpa_cli_cmd(ctrl, "BSSID_IGNORE", 0, argc, argv);
1305 }
1306 
1307 
wpa_cli_cmd_log_level(struct wpa_ctrl * ctrl,int argc,char * argv[])1308 static int wpa_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
1309 {
1310 	return wpa_cli_cmd(ctrl, "LOG_LEVEL", 0, argc, argv);
1311 }
1312 
1313 
wpa_cli_cmd_list_networks(struct wpa_ctrl * ctrl,int argc,char * argv[])1314 static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
1315 				     char *argv[])
1316 {
1317 	return wpa_cli_cmd(ctrl, "LIST_NETWORKS", 0, argc, argv);
1318 }
1319 
1320 
wpa_cli_cmd_select_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1321 static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
1322 				      char *argv[])
1323 {
1324 	return wpa_cli_cmd(ctrl, "SELECT_NETWORK", 1, argc, argv);
1325 }
1326 
1327 
wpa_cli_cmd_enable_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1328 static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
1329 				      char *argv[])
1330 {
1331 	return wpa_cli_cmd(ctrl, "ENABLE_NETWORK", 1, argc, argv);
1332 }
1333 
1334 
wpa_cli_cmd_disable_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1335 static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
1336 				       char *argv[])
1337 {
1338 	return wpa_cli_cmd(ctrl, "DISABLE_NETWORK", 1, argc, argv);
1339 }
1340 
1341 
wpa_cli_cmd_add_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1342 static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
1343 				   char *argv[])
1344 {
1345 	int res = wpa_ctrl_command(ctrl, "ADD_NETWORK");
1346 	if (interactive)
1347 		update_networks(ctrl);
1348 	return res;
1349 }
1350 
1351 
wpa_cli_cmd_remove_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1352 static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
1353 				      char *argv[])
1354 {
1355 	int res = wpa_cli_cmd(ctrl, "REMOVE_NETWORK", 1, argc, argv);
1356 	if (interactive)
1357 		update_networks(ctrl);
1358 	return res;
1359 }
1360 
1361 
wpa_cli_show_network_variables(void)1362 static void wpa_cli_show_network_variables(void)
1363 {
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"
1370 	       "  ...\n"
1371 	       "\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 "
1375 	       "marks.\n"
1376 	       "For example: set_network 1 ssid \"network name\"\n"
1377 	       "\n"
1378 	       "Please see wpa_supplicant.conf documentation for full list "
1379 	       "of\navailable variables.\n");
1380 }
1381 
1382 
wpa_cli_cmd_set_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1383 static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
1384 				   char *argv[])
1385 {
1386 	if (argc == 0) {
1387 		wpa_cli_show_network_variables();
1388 		return 0;
1389 	}
1390 
1391 	if (argc < 3) {
1392 		printf("Invalid SET_NETWORK command: needs three arguments\n"
1393 		       "(network id, variable name, and value)\n");
1394 		return -1;
1395 	}
1396 
1397 	return wpa_cli_cmd(ctrl, "SET_NETWORK", 3, argc, argv);
1398 }
1399 
1400 
wpa_cli_cmd_get_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1401 static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
1402 				   char *argv[])
1403 {
1404 	if (argc == 0) {
1405 		wpa_cli_show_network_variables();
1406 		return 0;
1407 	}
1408 
1409 	if (argc != 2) {
1410 		printf("Invalid GET_NETWORK command: needs two arguments\n"
1411 		       "(network id and variable name)\n");
1412 		return -1;
1413 	}
1414 
1415 	return wpa_cli_cmd(ctrl, "GET_NETWORK", 2, argc, argv);
1416 }
1417 
1418 
1419 static const char *network_fields[] = {
1420 	"ssid", "scan_ssid", "bssid", "bssid_ignore",
1421 	"bssid_accept", "psk", "proto", "key_mgmt",
1422 	"bg_scan_period", "pairwise", "group", "auth_alg", "scan_freq",
1423 	"freq_list", "max_oper_chwidth", "ht40", "vht", "vht_center_freq1",
1424 	"vht_center_freq2", "ht", "edmg",
1425 #ifdef IEEE8021X_EAPOL
1426 	"eap", "identity", "anonymous_identity", "password", "ca_cert",
1427 	"ca_path", "client_cert", "private_key", "private_key_passwd",
1428 	"dh_file", "subject_match", "altsubject_match",
1429 	"check_cert_subject",
1430 	"domain_suffix_match", "domain_match", "ca_cert2", "ca_path2",
1431 	"client_cert2", "private_key2", "private_key2_passwd",
1432 	"dh_file2", "subject_match2", "altsubject_match2",
1433 	"check_cert_subject2",
1434 	"domain_suffix_match2", "domain_match2", "phase1", "phase2",
1435 	"pcsc", "pin", "engine_id", "key_id", "cert_id", "ca_cert_id",
1436 	"pin2", "engine2_id", "key2_id", "cert2_id", "ca_cert2_id",
1437 	"engine", "engine2", "eapol_flags", "sim_num",
1438 	"openssl_ciphers", "erp",
1439 #endif /* IEEE8021X_EAPOL */
1440 	"wep_key0", "wep_key1", "wep_key2", "wep_key3",
1441 	"wep_tx_keyidx", "priority",
1442 #ifdef IEEE8021X_EAPOL
1443 	"eap_workaround", "pac_file", "fragment_size", "ocsp",
1444 #endif /* IEEE8021X_EAPOL */
1445 	"mode",
1446 	"proactive_key_caching", "disabled", "id_str",
1447 	"ieee80211w",
1448 	"mixed_cell", "frequency", "fixed_freq",
1449 #ifdef CONFIG_MESH
1450 	"no_auto_peer", "mesh_rssi_threshold",
1451 	"mesh_basic_rates", "dot11MeshMaxRetries",
1452 	"dot11MeshRetryTimeout", "dot11MeshConfirmTimeout",
1453 	"dot11MeshHoldingTimeout",
1454 #endif /* CONFIG_MESH */
1455 	"wpa_ptk_rekey", "bgscan", "ignore_broadcast_ssid",
1456 	"wpa_deny_ptk0_rekey",
1457 	"enable_edmg", "edmg_channel",
1458 #ifdef CONFIG_P2P
1459 	"go_p2p_dev_addr", "p2p_client_list", "psk_list",
1460 #endif /* CONFIG_P2P */
1461 #ifdef CONFIG_HT_OVERRIDES
1462 	"disable_ht", "disable_ht40", "disable_sgi", "disable_ldpc",
1463 	"ht40_intolerant", "disable_max_amsdu", "ampdu_factor",
1464 	"ampdu_density", "ht_mcs", "rx_stbc", "tx_stbc",
1465 #endif /* CONFIG_HT_OVERRIDES */
1466 #ifdef CONFIG_VHT_OVERRIDES
1467 	"disable_vht", "vht_capa", "vht_capa_mask", "vht_rx_mcs_nss_1",
1468 	"vht_rx_mcs_nss_2", "vht_rx_mcs_nss_3", "vht_rx_mcs_nss_4",
1469 	"vht_rx_mcs_nss_5", "vht_rx_mcs_nss_6", "vht_rx_mcs_nss_7",
1470 	"vht_rx_mcs_nss_8", "vht_tx_mcs_nss_1", "vht_tx_mcs_nss_2",
1471 	"vht_tx_mcs_nss_3", "vht_tx_mcs_nss_4", "vht_tx_mcs_nss_5",
1472 	"vht_tx_mcs_nss_6", "vht_tx_mcs_nss_7", "vht_tx_mcs_nss_8",
1473 #endif /* CONFIG_VHT_OVERRIDES */
1474 #ifdef CONFIG_HE_OVERRIDES
1475 	"disable_he",
1476 #endif /* CONFIG_HE_OVERRIDES */
1477 	"ap_max_inactivity", "dtim_period", "beacon_int",
1478 #ifdef CONFIG_MACSEC
1479 	"macsec_policy",
1480 	"macsec_integ_only",
1481 	"macsec_replay_protect",
1482 	"macsec_replay_window",
1483 	"macsec_port",
1484 	"mka_priority",
1485 #endif /* CONFIG_MACSEC */
1486 #ifdef CONFIG_HS20
1487 	"update_identifier",
1488 #endif /* CONFIG_HS20 */
1489 	"mac_addr", "pbss", "wps_disabled"
1490 };
1491 
1492 
wpa_cli_complete_network(const char * str,int pos)1493 static char ** wpa_cli_complete_network(const char *str, int pos)
1494 {
1495 	int arg = get_cmd_arg_num(str, pos);
1496 	int i, num_fields = ARRAY_SIZE(network_fields);
1497 	char **res = NULL;
1498 
1499 	switch (arg) {
1500 	case 1:
1501 		res = cli_txt_list_array(&networks);
1502 		break;
1503 	case 2:
1504 		res = os_calloc(num_fields + 1, sizeof(char *));
1505 		if (res == NULL)
1506 			return NULL;
1507 		for (i = 0; i < num_fields; i++) {
1508 			res[i] = os_strdup(network_fields[i]);
1509 			if (res[i] == NULL)
1510 				break;
1511 		}
1512 	}
1513 	return res;
1514 }
1515 
1516 
wpa_cli_complete_network_id(const char * str,int pos)1517 static char ** wpa_cli_complete_network_id(const char *str, int pos)
1518 {
1519 	int arg = get_cmd_arg_num(str, pos);
1520 	if (arg == 1)
1521 		return cli_txt_list_array(&networks);
1522 	return NULL;
1523 }
1524 
1525 
wpa_cli_cmd_dup_network(struct wpa_ctrl * ctrl,int argc,char * argv[])1526 static int wpa_cli_cmd_dup_network(struct wpa_ctrl *ctrl, int argc,
1527 				   char *argv[])
1528 {
1529 	if (argc == 0) {
1530 		wpa_cli_show_network_variables();
1531 		return 0;
1532 	}
1533 
1534 	if (argc < 3) {
1535 		printf("Invalid DUP_NETWORK command: needs three arguments\n"
1536 		       "(src netid, dest netid, and variable name)\n");
1537 		return -1;
1538 	}
1539 
1540 	return wpa_cli_cmd(ctrl, "DUP_NETWORK", 3, argc, argv);
1541 }
1542 
1543 
wpa_cli_complete_dup_network(const char * str,int pos)1544 static char ** wpa_cli_complete_dup_network(const char *str, int pos)
1545 {
1546 	int arg = get_cmd_arg_num(str, pos);
1547 	int i, num_fields = ARRAY_SIZE(network_fields);
1548 	char **res = NULL;
1549 
1550 	switch (arg) {
1551 	case 1:
1552 	case 2:
1553 		res = cli_txt_list_array(&networks);
1554 		break;
1555 	case 3:
1556 		res = os_calloc(num_fields + 1, sizeof(char *));
1557 		if (res == NULL)
1558 			return NULL;
1559 		for (i = 0; i < num_fields; i++) {
1560 			res[i] = os_strdup(network_fields[i]);
1561 			if (res[i] == NULL)
1562 				break;
1563 		}
1564 	}
1565 	return res;
1566 }
1567 
1568 
wpa_cli_cmd_list_creds(struct wpa_ctrl * ctrl,int argc,char * argv[])1569 static int wpa_cli_cmd_list_creds(struct wpa_ctrl *ctrl, int argc,
1570 				  char *argv[])
1571 {
1572 	return wpa_ctrl_command(ctrl, "LIST_CREDS");
1573 }
1574 
1575 
wpa_cli_cmd_add_cred(struct wpa_ctrl * ctrl,int argc,char * argv[])1576 static int wpa_cli_cmd_add_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1577 {
1578 	int res = wpa_ctrl_command(ctrl, "ADD_CRED");
1579 	if (interactive)
1580 		update_creds(ctrl);
1581 	return res;
1582 }
1583 
1584 
wpa_cli_cmd_remove_cred(struct wpa_ctrl * ctrl,int argc,char * argv[])1585 static int wpa_cli_cmd_remove_cred(struct wpa_ctrl *ctrl, int argc,
1586 				   char *argv[])
1587 {
1588 	int res = wpa_cli_cmd(ctrl, "REMOVE_CRED", 1, argc, argv);
1589 	if (interactive)
1590 		update_creds(ctrl);
1591 	return res;
1592 }
1593 
1594 
1595 static const char * const cred_fields[] = {
1596 	"temporary", "priority", "sp_priority", "pcsc", "eap",
1597 	"update_identifier", "min_dl_bandwidth_home", "min_ul_bandwidth_home",
1598 	"min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming", "max_bss_load",
1599 	"req_conn_capab", "ocsp", "sim_num", "realm", "username", "password",
1600 	"ca_cert", "client_cert", "private_key", "private_key_passwd", "imsi",
1601 	"ca_cert_id", "cert_id", "key_id", "engine_id", "engine",
1602 	"milenage", "domain_suffix_match", "domain", "phase1", "phase2",
1603 	"roaming_consortium", "required_roaming_consortium", "excluded_ssid",
1604 	"roaming_partner", "provisioning_sp"
1605 };
1606 
1607 
wpa_cli_complete_cred(const char * str,int pos)1608 static char ** wpa_cli_complete_cred(const char *str, int pos)
1609 {
1610 	int arg = get_cmd_arg_num(str, pos);
1611 	int i, num_fields = ARRAY_SIZE(cred_fields);
1612 	char **res = NULL;
1613 
1614 	switch (arg) {
1615 	case 1:
1616 		res = cli_txt_list_array(&creds);
1617 		break;
1618 	case 2:
1619 		res = os_calloc(num_fields + 1, sizeof(char *));
1620 		if (res == NULL)
1621 			return NULL;
1622 		for (i = 0; i < num_fields; i++) {
1623 			res[i] = os_strdup(cred_fields[i]);
1624 			if (res[i] == NULL)
1625 				break;
1626 		}
1627 	}
1628 	return res;
1629 }
1630 
1631 
wpa_cli_cmd_set_cred(struct wpa_ctrl * ctrl,int argc,char * argv[])1632 static int wpa_cli_cmd_set_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1633 {
1634 	if (argc != 3) {
1635 		printf("Invalid SET_CRED command: needs three arguments\n"
1636 		       "(cred id, variable name, and value)\n");
1637 		return -1;
1638 	}
1639 
1640 	return wpa_cli_cmd(ctrl, "SET_CRED", 3, argc, argv);
1641 }
1642 
1643 
wpa_cli_cmd_get_cred(struct wpa_ctrl * ctrl,int argc,char * argv[])1644 static int wpa_cli_cmd_get_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
1645 {
1646 	if (argc != 2) {
1647 		printf("Invalid GET_CRED command: needs two arguments\n"
1648 		       "(cred id, variable name)\n");
1649 		return -1;
1650 	}
1651 
1652 	return wpa_cli_cmd(ctrl, "GET_CRED", 2, argc, argv);
1653 }
1654 
1655 
wpa_cli_cmd_disconnect(struct wpa_ctrl * ctrl,int argc,char * argv[])1656 static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
1657 				  char *argv[])
1658 {
1659 	return wpa_ctrl_command(ctrl, "DISCONNECT");
1660 }
1661 
1662 
wpa_cli_cmd_reconnect(struct wpa_ctrl * ctrl,int argc,char * argv[])1663 static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
1664 				  char *argv[])
1665 {
1666 	return wpa_ctrl_command(ctrl, "RECONNECT");
1667 }
1668 
1669 
wpa_cli_cmd_save_config(struct wpa_ctrl * ctrl,int argc,char * argv[])1670 static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
1671 				   char *argv[])
1672 {
1673 	return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
1674 }
1675 
1676 
wpa_cli_cmd_scan(struct wpa_ctrl * ctrl,int argc,char * argv[])1677 static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
1678 {
1679 	return wpa_cli_cmd(ctrl, "SCAN", 0, argc, argv);
1680 }
1681 
1682 
wpa_cli_cmd_scan_results(struct wpa_ctrl * ctrl,int argc,char * argv[])1683 static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
1684 				    char *argv[])
1685 {
1686 	return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
1687 }
1688 
1689 
wpa_cli_cmd_abort_scan(struct wpa_ctrl * ctrl,int argc,char * argv[])1690 static int wpa_cli_cmd_abort_scan(struct wpa_ctrl *ctrl, int argc,
1691 				  char *argv[])
1692 {
1693 	return wpa_ctrl_command(ctrl, "ABORT_SCAN");
1694 }
1695 
1696 
wpa_cli_cmd_bss(struct wpa_ctrl * ctrl,int argc,char * argv[])1697 static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
1698 {
1699 	return wpa_cli_cmd(ctrl, "BSS", 1, argc, argv);
1700 }
1701 
1702 
wpa_cli_complete_bss(const char * str,int pos)1703 static char ** wpa_cli_complete_bss(const char *str, int pos)
1704 {
1705 	int arg = get_cmd_arg_num(str, pos);
1706 	char **res = NULL;
1707 
1708 	switch (arg) {
1709 	case 1:
1710 		res = cli_txt_list_array(&bsses);
1711 		break;
1712 	}
1713 
1714 	return res;
1715 }
1716 
1717 
wpa_cli_cmd_get_capability(struct wpa_ctrl * ctrl,int argc,char * argv[])1718 static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
1719 				      char *argv[])
1720 {
1721 	if (argc < 1 || argc > 3) {
1722 		printf("Invalid GET_CAPABILITY command: need at least one argument and max three arguments\n");
1723 		return -1;
1724 	}
1725 
1726 	if (argc > 1 && os_strcmp(argv[0], "key_mgmt") != 0 &&
1727 	    os_strncmp(argv[1], "iftype=", 7) == 0) {
1728 		printf("Invalid GET_CAPABILITY command: 'iftype=' param is allowed only for 'key_mgmt'\n");
1729 		return -1;
1730 	}
1731 
1732 	if (argc == 2 && os_strcmp(argv[1], "strict") != 0 &&
1733 	    os_strncmp(argv[1], "iftype=", 7) != 0) {
1734 		printf("Invalid GET_CAPABILITY command: the second argument, if any, must be 'strict' OR 'iftype=<iftype_name>'\n");
1735 		return -1;
1736 	}
1737 
1738 	if (argc == 3 && os_strcmp(argv[2], "strict") != 0) {
1739 		printf("Invalid GET_CAPABILITY command: the third argument, if any, must be 'strict'\n");
1740 		return -1;
1741 	}
1742 
1743 	return wpa_cli_cmd(ctrl, "GET_CAPABILITY", 1, argc, argv);
1744 }
1745 
1746 
wpa_cli_complete_get_capability(const char * str,int pos)1747 static char ** wpa_cli_complete_get_capability(const char *str, int pos)
1748 {
1749 	int arg = get_cmd_arg_num(str, pos);
1750 	const char *fields[] = {
1751 		"eap", "pairwise", "group", "group_mgmt", "key_mgmt",
1752 		"proto", "auth_alg", "modes", "channels", "freq",
1753 #ifdef CONFIG_TDLS
1754 		"tdls",
1755 #endif /* CONFIG_TDLS */
1756 #ifdef CONFIG_ERP
1757 		"erp",
1758 #endif /* CONFIG_ERP */
1759 #ifdef CONFIG_FIPS
1760 		"fips",
1761 #endif /* CONFIG_FIPS */
1762 #ifdef CONFIG_ACS
1763 		"acs",
1764 #endif /* CONFIG_ACS */
1765 	};
1766 	const char *iftypes[] = {
1767 		"iftype=STATION", "iftype=AP", "iftype=P2P_CLIENT",
1768 		"iftype=P2P_GO", "iftype=AP_VLAN", "iftype=IBSS", "iftype=NAN",
1769 		"iftype=P2P_DEVICE", "iftype=MESH",
1770 	};
1771 	int i, num_fields = ARRAY_SIZE(fields);
1772 	int num_iftypes = ARRAY_SIZE(iftypes);
1773 	char **res = NULL;
1774 
1775 	if (arg == 1) {
1776 		res = os_calloc(num_fields + 1, sizeof(char *));
1777 		if (res == NULL)
1778 			return NULL;
1779 		for (i = 0; i < num_fields; i++) {
1780 			res[i] = os_strdup(fields[i]);
1781 			if (res[i] == NULL)
1782 				return res;
1783 		}
1784 	}
1785 	if (arg == 2) {
1786 		/* the second argument can be "iftype=<iftype_name>" OR
1787 		 * "strict" */
1788 		res = os_calloc(num_iftypes + 2, sizeof(char *));
1789 		if (!res)
1790 			return NULL;
1791 		res[0] = os_strdup("strict");
1792 		if (!res[0])
1793 			return res;
1794 		for (i = 0; i < num_iftypes; i++) {
1795 			res[i + 1] = os_strdup(iftypes[i]);
1796 			if (!res[i + 1])
1797 				return res;
1798 		}
1799 	}
1800 	if (arg == 3) {
1801 		res = os_calloc(1 + 1, sizeof(char *));
1802 		if (res == NULL)
1803 			return NULL;
1804 		res[0] = os_strdup("strict");
1805 	}
1806 	return res;
1807 }
1808 
1809 
wpa_cli_list_interfaces(struct wpa_ctrl * ctrl)1810 static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
1811 {
1812 	printf("Available interfaces:\n");
1813 	return wpa_ctrl_command(ctrl, "INTERFACES");
1814 }
1815 
1816 
wpa_cli_cmd_interface(struct wpa_ctrl * ctrl,int argc,char * argv[])1817 static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
1818 {
1819 	if (argc < 1) {
1820 		wpa_cli_list_interfaces(ctrl);
1821 		return 0;
1822 	}
1823 
1824 	wpa_cli_close_connection();
1825 	os_free(ctrl_ifname);
1826 	ctrl_ifname = os_strdup(argv[0]);
1827 	if (!ctrl_ifname) {
1828 		printf("Failed to allocate memory\n");
1829 		return 0;
1830 	}
1831 
1832 	if (wpa_cli_open_connection(ctrl_ifname, 1) == 0) {
1833 		printf("Connected to interface '%s'.\n", ctrl_ifname);
1834 	} else {
1835 		printf("Could not connect to interface '%s' - re-trying\n",
1836 		       ctrl_ifname);
1837 	}
1838 	return 0;
1839 }
1840 
1841 
wpa_cli_cmd_reconfigure(struct wpa_ctrl * ctrl,int argc,char * argv[])1842 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
1843 				   char *argv[])
1844 {
1845 	return wpa_ctrl_command(ctrl, "RECONFIGURE");
1846 }
1847 
1848 
wpa_cli_cmd_terminate(struct wpa_ctrl * ctrl,int argc,char * argv[])1849 static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
1850 				 char *argv[])
1851 {
1852 	return wpa_ctrl_command(ctrl, "TERMINATE");
1853 }
1854 
1855 
wpa_cli_cmd_interface_add(struct wpa_ctrl * ctrl,int argc,char * argv[])1856 static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
1857 				     char *argv[])
1858 {
1859 	char cmd[256];
1860 	int res;
1861 
1862 	if (argc < 1) {
1863 		printf("Invalid INTERFACE_ADD command: needs at least one "
1864 		       "argument (interface name)\n"
1865 		       "All arguments: ifname confname driver ctrl_interface "
1866 		       "driver_param bridge_name [create]\n");
1867 		return -1;
1868 	}
1869 
1870 	/*
1871 	 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1872 	 * <driver_param>TAB<bridge_name>[TAB<create>[TAB<type>]]
1873 	 */
1874 	res = os_snprintf(cmd, sizeof(cmd),
1875 			  "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
1876 			  argv[0],
1877 			  argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
1878 			  argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
1879 			  argc > 5 ? argv[5] : "", argc > 6 ? argv[6] : "",
1880 			  argc > 7 ? argv[7] : "");
1881 	if (os_snprintf_error(sizeof(cmd), res))
1882 		return -1;
1883 	cmd[sizeof(cmd) - 1] = '\0';
1884 	return wpa_ctrl_command(ctrl, cmd);
1885 }
1886 
1887 
wpa_cli_cmd_interface_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])1888 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
1889 					char *argv[])
1890 {
1891 	return wpa_cli_cmd(ctrl, "INTERFACE_REMOVE", 1, argc, argv);
1892 }
1893 
1894 
wpa_cli_cmd_interface_list(struct wpa_ctrl * ctrl,int argc,char * argv[])1895 static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
1896 				      char *argv[])
1897 {
1898 	return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
1899 }
1900 
1901 
1902 #ifdef CONFIG_AP
wpa_cli_cmd_sta(struct wpa_ctrl * ctrl,int argc,char * argv[])1903 static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1904 {
1905 	return wpa_cli_cmd(ctrl, "STA", 1, argc, argv);
1906 }
1907 
1908 
wpa_cli_complete_sta(const char * str,int pos)1909 static char ** wpa_cli_complete_sta(const char *str, int pos)
1910 {
1911 	int arg = get_cmd_arg_num(str, pos);
1912 	char **res = NULL;
1913 
1914 	switch (arg) {
1915 	case 1:
1916 		res = cli_txt_list_array(&stations);
1917 		break;
1918 	}
1919 
1920 	return res;
1921 }
1922 
1923 
wpa_ctrl_command_sta(struct wpa_ctrl * ctrl,const char * cmd,char * addr,size_t addr_len,int print)1924 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
1925 				char *addr, size_t addr_len, int print)
1926 {
1927 	char buf[4096], *pos;
1928 	size_t len;
1929 	int ret;
1930 
1931 	if (ctrl_conn == NULL) {
1932 		printf("Not connected to hostapd - command dropped.\n");
1933 		return -1;
1934 	}
1935 	if (ifname_prefix) {
1936 		os_snprintf(buf, sizeof(buf), "IFNAME=%s %s",
1937 			    ifname_prefix, cmd);
1938 		buf[sizeof(buf) - 1] = '\0';
1939 		cmd = buf;
1940 	}
1941 	len = sizeof(buf) - 1;
1942 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
1943 			       wpa_cli_msg_cb);
1944 	if (ret == -2) {
1945 		printf("'%s' command timed out.\n", cmd);
1946 		return -2;
1947 	} else if (ret < 0) {
1948 		printf("'%s' command failed.\n", cmd);
1949 		return -1;
1950 	}
1951 
1952 	buf[len] = '\0';
1953 	if (os_memcmp(buf, "FAIL", 4) == 0 ||
1954 	    os_memcmp(buf, "UNKNOWN COMMAND", 15) == 0)
1955 		return -1;
1956 	if (print)
1957 		printf("%s", buf);
1958 
1959 	pos = buf;
1960 	while (*pos != '\0' && *pos != '\n')
1961 		pos++;
1962 	*pos = '\0';
1963 	os_strlcpy(addr, buf, addr_len);
1964 	return 0;
1965 }
1966 
1967 
wpa_cli_cmd_all_sta(struct wpa_ctrl * ctrl,int argc,char * argv[])1968 static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1969 {
1970 	char addr[32], cmd[64];
1971 
1972 	if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
1973 		return 0;
1974 	do {
1975 		os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
1976 	} while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
1977 
1978 	return -1;
1979 }
1980 
1981 
wpa_cli_cmd_list_sta(struct wpa_ctrl * ctrl,int argc,char * argv[])1982 static int wpa_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
1983 				char *argv[])
1984 {
1985 	char addr[32], cmd[64];
1986 
1987 	if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
1988 		return 0;
1989 	do {
1990 		if (os_strcmp(addr, "") != 0)
1991 			printf("%s\n", addr);
1992 		os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
1993 	} while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
1994 
1995 	return 0;
1996 }
1997 
1998 
wpa_cli_cmd_deauthenticate(struct wpa_ctrl * ctrl,int argc,char * argv[])1999 static int wpa_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
2000 				      char *argv[])
2001 {
2002 	return wpa_cli_cmd(ctrl, "DEAUTHENTICATE", 1, argc, argv);
2003 }
2004 
2005 
wpa_cli_complete_deauthenticate(const char * str,int pos)2006 static char ** wpa_cli_complete_deauthenticate(const char *str, int pos)
2007 {
2008 	int arg = get_cmd_arg_num(str, pos);
2009 	char **res = NULL;
2010 
2011 	switch (arg) {
2012 	case 1:
2013 		res = cli_txt_list_array(&stations);
2014 		break;
2015 	}
2016 
2017 	return res;
2018 }
2019 
2020 
wpa_cli_cmd_disassociate(struct wpa_ctrl * ctrl,int argc,char * argv[])2021 static int wpa_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
2022 				    char *argv[])
2023 {
2024 	return wpa_cli_cmd(ctrl, "DISASSOCIATE", 1, argc, argv);
2025 }
2026 
2027 
wpa_cli_complete_disassociate(const char * str,int pos)2028 static char ** wpa_cli_complete_disassociate(const char *str, int pos)
2029 {
2030 	int arg = get_cmd_arg_num(str, pos);
2031 	char **res = NULL;
2032 
2033 	switch (arg) {
2034 	case 1:
2035 		res = cli_txt_list_array(&stations);
2036 		break;
2037 	}
2038 
2039 	return res;
2040 }
2041 
2042 
wpa_cli_cmd_chanswitch(struct wpa_ctrl * ctrl,int argc,char * argv[])2043 static int wpa_cli_cmd_chanswitch(struct wpa_ctrl *ctrl, int argc,
2044 				    char *argv[])
2045 {
2046 	return wpa_cli_cmd(ctrl, "CHAN_SWITCH", 2, argc, argv);
2047 }
2048 
2049 
wpa_cli_cmd_update_beacon(struct wpa_ctrl * ctrl,int argc,char * argv[])2050 static int wpa_cli_cmd_update_beacon(struct wpa_ctrl *ctrl, int argc,
2051 				     char *argv[])
2052 {
2053 	return wpa_ctrl_command(ctrl, "UPDATE_BEACON");
2054 }
2055 
2056 #endif /* CONFIG_AP */
2057 
2058 
wpa_cli_cmd_suspend(struct wpa_ctrl * ctrl,int argc,char * argv[])2059 static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[])
2060 {
2061 	return wpa_ctrl_command(ctrl, "SUSPEND");
2062 }
2063 
2064 
wpa_cli_cmd_resume(struct wpa_ctrl * ctrl,int argc,char * argv[])2065 static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[])
2066 {
2067 	return wpa_ctrl_command(ctrl, "RESUME");
2068 }
2069 
2070 
2071 #ifdef CONFIG_TESTING_OPTIONS
wpa_cli_cmd_drop_sa(struct wpa_ctrl * ctrl,int argc,char * argv[])2072 static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[])
2073 {
2074 	return wpa_ctrl_command(ctrl, "DROP_SA");
2075 }
2076 #endif /* CONFIG_TESTING_OPTIONS */
2077 
2078 
wpa_cli_cmd_roam(struct wpa_ctrl * ctrl,int argc,char * argv[])2079 static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[])
2080 {
2081 	return wpa_cli_cmd(ctrl, "ROAM", 1, argc, argv);
2082 }
2083 
2084 
2085 #ifdef CONFIG_MESH
2086 
wpa_cli_cmd_mesh_interface_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2087 static int wpa_cli_cmd_mesh_interface_add(struct wpa_ctrl *ctrl, int argc,
2088 					  char *argv[])
2089 {
2090 	return wpa_cli_cmd(ctrl, "MESH_INTERFACE_ADD", 0, argc, argv);
2091 }
2092 
2093 
wpa_cli_cmd_mesh_group_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2094 static int wpa_cli_cmd_mesh_group_add(struct wpa_ctrl *ctrl, int argc,
2095 				      char *argv[])
2096 {
2097 	return wpa_cli_cmd(ctrl, "MESH_GROUP_ADD", 1, argc, argv);
2098 }
2099 
2100 
wpa_cli_cmd_mesh_group_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])2101 static int wpa_cli_cmd_mesh_group_remove(struct wpa_ctrl *ctrl, int argc,
2102 					 char *argv[])
2103 {
2104 	return wpa_cli_cmd(ctrl, "MESH_GROUP_REMOVE", 1, argc, argv);
2105 }
2106 
2107 
wpa_cli_cmd_mesh_peer_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])2108 static int wpa_cli_cmd_mesh_peer_remove(struct wpa_ctrl *ctrl, int argc,
2109 					char *argv[])
2110 {
2111 	return wpa_cli_cmd(ctrl, "MESH_PEER_REMOVE", 1, argc, argv);
2112 }
2113 
2114 
wpa_cli_cmd_mesh_peer_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2115 static int wpa_cli_cmd_mesh_peer_add(struct wpa_ctrl *ctrl, int argc,
2116 				     char *argv[])
2117 {
2118 	return wpa_cli_cmd(ctrl, "MESH_PEER_ADD", 1, argc, argv);
2119 }
2120 
2121 
wpa_cli_cmd_mesh_link_probe(struct wpa_ctrl * ctrl,int argc,char * argv[])2122 static int wpa_cli_cmd_mesh_link_probe(struct wpa_ctrl *ctrl, int argc,
2123 				       char *argv[])
2124 {
2125 	return wpa_cli_cmd(ctrl, "MESH_LINK_PROBE", 1, argc, argv);
2126 }
2127 
2128 #endif /* CONFIG_MESH */
2129 
2130 
2131 #ifdef CONFIG_P2P
2132 
wpa_cli_cmd_p2p_find(struct wpa_ctrl * ctrl,int argc,char * argv[])2133 static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
2134 {
2135 	return wpa_cli_cmd(ctrl, "P2P_FIND", 0, argc, argv);
2136 }
2137 
2138 
wpa_cli_complete_p2p_find(const char * str,int pos)2139 static char ** wpa_cli_complete_p2p_find(const char *str, int pos)
2140 {
2141 	char **res = NULL;
2142 	int arg = get_cmd_arg_num(str, pos);
2143 
2144 	res = os_calloc(6, sizeof(char *));
2145 	if (res == NULL)
2146 		return NULL;
2147 	res[0] = os_strdup("type=social");
2148 	if (res[0] == NULL) {
2149 		os_free(res);
2150 		return NULL;
2151 	}
2152 	res[1] = os_strdup("type=progressive");
2153 	if (res[1] == NULL)
2154 		return res;
2155 	res[2] = os_strdup("delay=");
2156 	if (res[2] == NULL)
2157 		return res;
2158 	res[3] = os_strdup("dev_id=");
2159 	if (res[3] == NULL)
2160 		return res;
2161 	if (arg == 1)
2162 		res[4] = os_strdup("[timeout]");
2163 
2164 	return res;
2165 }
2166 
2167 
wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl * ctrl,int argc,char * argv[])2168 static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc,
2169 				     char *argv[])
2170 {
2171 	return wpa_ctrl_command(ctrl, "P2P_STOP_FIND");
2172 }
2173 
2174 
wpa_cli_cmd_p2p_asp_provision(struct wpa_ctrl * ctrl,int argc,char * argv[])2175 static int wpa_cli_cmd_p2p_asp_provision(struct wpa_ctrl *ctrl, int argc,
2176 					 char *argv[])
2177 {
2178 	return wpa_cli_cmd(ctrl, "P2P_ASP_PROVISION", 3, argc, argv);
2179 }
2180 
2181 
wpa_cli_cmd_p2p_asp_provision_resp(struct wpa_ctrl * ctrl,int argc,char * argv[])2182 static int wpa_cli_cmd_p2p_asp_provision_resp(struct wpa_ctrl *ctrl, int argc,
2183 					      char *argv[])
2184 {
2185 	return wpa_cli_cmd(ctrl, "P2P_ASP_PROVISION_RESP", 2, argc, argv);
2186 }
2187 
2188 
wpa_cli_cmd_p2p_connect(struct wpa_ctrl * ctrl,int argc,char * argv[])2189 static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc,
2190 				   char *argv[])
2191 {
2192 	return wpa_cli_cmd(ctrl, "P2P_CONNECT", 2, argc, argv);
2193 }
2194 
2195 
wpa_cli_complete_p2p_connect(const char * str,int pos)2196 static char ** wpa_cli_complete_p2p_connect(const char *str, int pos)
2197 {
2198 	int arg = get_cmd_arg_num(str, pos);
2199 	char **res = NULL;
2200 
2201 	switch (arg) {
2202 	case 1:
2203 		res = cli_txt_list_array(&p2p_peers);
2204 		break;
2205 	}
2206 
2207 	return res;
2208 }
2209 
2210 
wpa_cli_cmd_p2p_listen(struct wpa_ctrl * ctrl,int argc,char * argv[])2211 static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc,
2212 				  char *argv[])
2213 {
2214 	return wpa_cli_cmd(ctrl, "P2P_LISTEN", 0, argc, argv);
2215 }
2216 
2217 
wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])2218 static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc,
2219 					char *argv[])
2220 {
2221 	return wpa_cli_cmd(ctrl, "P2P_GROUP_REMOVE", 1, argc, argv);
2222 }
2223 
2224 
wpa_cli_complete_p2p_group_remove(const char * str,int pos)2225 static char ** wpa_cli_complete_p2p_group_remove(const char *str, int pos)
2226 {
2227 	int arg = get_cmd_arg_num(str, pos);
2228 	char **res = NULL;
2229 
2230 	switch (arg) {
2231 	case 1:
2232 		res = cli_txt_list_array(&p2p_groups);
2233 		break;
2234 	}
2235 
2236 	return res;
2237 }
2238 
2239 
wpa_cli_cmd_p2p_group_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2240 static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc,
2241 					char *argv[])
2242 {
2243 	return wpa_cli_cmd(ctrl, "P2P_GROUP_ADD", 0, argc, argv);
2244 }
2245 
2246 
wpa_cli_cmd_p2p_group_member(struct wpa_ctrl * ctrl,int argc,char * argv[])2247 static int wpa_cli_cmd_p2p_group_member(struct wpa_ctrl *ctrl, int argc,
2248 					char *argv[])
2249 {
2250 	return wpa_cli_cmd(ctrl, "P2P_GROUP_MEMBER", 1, argc, argv);
2251 }
2252 
2253 
wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl * ctrl,int argc,char * argv[])2254 static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc,
2255 				     char *argv[])
2256 {
2257 	if (argc != 2 && argc != 3) {
2258 		printf("Invalid P2P_PROV_DISC command: needs at least "
2259 		       "two arguments, address and config method\n"
2260 		       "(display, keypad, or pbc) and an optional join\n");
2261 		return -1;
2262 	}
2263 
2264 	return wpa_cli_cmd(ctrl, "P2P_PROV_DISC", 2, argc, argv);
2265 }
2266 
2267 
wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl * ctrl,int argc,char * argv[])2268 static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc,
2269 					  char *argv[])
2270 {
2271 	return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE");
2272 }
2273 
2274 
wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl * ctrl,int argc,char * argv[])2275 static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc,
2276 					 char *argv[])
2277 {
2278 	char cmd[4096];
2279 
2280 	if (argc < 2) {
2281 		printf("Invalid P2P_SERV_DISC_REQ command: needs two "
2282 		       "or more arguments (address and TLVs)\n");
2283 		return -1;
2284 	}
2285 
2286 	if (write_cmd(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ", argc, argv) < 0)
2287 		return -1;
2288 	return wpa_ctrl_command(ctrl, cmd);
2289 }
2290 
2291 
wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl * ctrl,int argc,char * argv[])2292 static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl,
2293 						int argc, char *argv[])
2294 {
2295 	return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_CANCEL_REQ", 1, argc, argv);
2296 }
2297 
2298 
wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl * ctrl,int argc,char * argv[])2299 static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc,
2300 					  char *argv[])
2301 {
2302 	char cmd[4096];
2303 	int res;
2304 
2305 	if (argc != 4) {
2306 		printf("Invalid P2P_SERV_DISC_RESP command: needs four "
2307 		       "arguments (freq, address, dialog token, and TLVs)\n");
2308 		return -1;
2309 	}
2310 
2311 	res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s",
2312 			  argv[0], argv[1], argv[2], argv[3]);
2313 	if (os_snprintf_error(sizeof(cmd), res))
2314 		return -1;
2315 	cmd[sizeof(cmd) - 1] = '\0';
2316 	return wpa_ctrl_command(ctrl, cmd);
2317 }
2318 
2319 
wpa_cli_cmd_p2p_service_update(struct wpa_ctrl * ctrl,int argc,char * argv[])2320 static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc,
2321 					  char *argv[])
2322 {
2323 	return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE");
2324 }
2325 
2326 
wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl * ctrl,int argc,char * argv[])2327 static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl,
2328 					      int argc, char *argv[])
2329 {
2330 	return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_EXTERNAL", 1, argc, argv);
2331 }
2332 
2333 
wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])2334 static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc,
2335 					 char *argv[])
2336 {
2337 	return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH");
2338 }
2339 
2340 
wpa_cli_cmd_p2p_service_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2341 static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc,
2342 				       char *argv[])
2343 {
2344 	if (argc < 3) {
2345 		printf("Invalid P2P_SERVICE_ADD command: needs 3-6 arguments\n");
2346 		return -1;
2347 	}
2348 
2349 	return wpa_cli_cmd(ctrl, "P2P_SERVICE_ADD", 3, argc, argv);
2350 }
2351 
2352 
wpa_cli_cmd_p2p_service_rep(struct wpa_ctrl * ctrl,int argc,char * argv[])2353 static int wpa_cli_cmd_p2p_service_rep(struct wpa_ctrl *ctrl, int argc,
2354 				       char *argv[])
2355 {
2356 	if (argc < 5 || argc > 6) {
2357 		printf("Invalid P2P_SERVICE_REP command: needs 5-6 "
2358 		       "arguments\n");
2359 		return -1;
2360 	}
2361 
2362 	return wpa_cli_cmd(ctrl, "P2P_SERVICE_REP", 5, argc, argv);
2363 }
2364 
2365 
wpa_cli_cmd_p2p_service_del(struct wpa_ctrl * ctrl,int argc,char * argv[])2366 static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc,
2367 				       char *argv[])
2368 {
2369 	char cmd[4096];
2370 	int res;
2371 
2372 	if (argc != 2 && argc != 3) {
2373 		printf("Invalid P2P_SERVICE_DEL command: needs two or three "
2374 		       "arguments\n");
2375 		return -1;
2376 	}
2377 
2378 	if (argc == 3)
2379 		res = os_snprintf(cmd, sizeof(cmd),
2380 				  "P2P_SERVICE_DEL %s %s %s",
2381 				  argv[0], argv[1], argv[2]);
2382 	else
2383 		res = os_snprintf(cmd, sizeof(cmd),
2384 				  "P2P_SERVICE_DEL %s %s",
2385 				  argv[0], argv[1]);
2386 	if (os_snprintf_error(sizeof(cmd), res))
2387 		return -1;
2388 	cmd[sizeof(cmd) - 1] = '\0';
2389 	return wpa_ctrl_command(ctrl, cmd);
2390 }
2391 
2392 
wpa_cli_cmd_p2p_reject(struct wpa_ctrl * ctrl,int argc,char * argv[])2393 static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl,
2394 				  int argc, char *argv[])
2395 {
2396 	return wpa_cli_cmd(ctrl, "P2P_REJECT", 1, argc, argv);
2397 }
2398 
2399 
wpa_cli_cmd_p2p_invite(struct wpa_ctrl * ctrl,int argc,char * argv[])2400 static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl,
2401 				  int argc, char *argv[])
2402 {
2403 	return wpa_cli_cmd(ctrl, "P2P_INVITE", 1, argc, argv);
2404 }
2405 
2406 
wpa_cli_cmd_p2p_peer(struct wpa_ctrl * ctrl,int argc,char * argv[])2407 static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[])
2408 {
2409 	return wpa_cli_cmd(ctrl, "P2P_PEER", 1, argc, argv);
2410 }
2411 
2412 
wpa_cli_complete_p2p_peer(const char * str,int pos)2413 static char ** wpa_cli_complete_p2p_peer(const char *str, int pos)
2414 {
2415 	int arg = get_cmd_arg_num(str, pos);
2416 	char **res = NULL;
2417 
2418 	switch (arg) {
2419 	case 1:
2420 		res = cli_txt_list_array(&p2p_peers);
2421 		break;
2422 	}
2423 
2424 	return res;
2425 }
2426 
2427 
wpa_ctrl_command_p2p_peer(struct wpa_ctrl * ctrl,const char * cmd,char * addr,size_t addr_len,int discovered)2428 static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, const char *cmd,
2429 				     char *addr, size_t addr_len,
2430 				     int discovered)
2431 {
2432 	char buf[4096], *pos;
2433 	size_t len;
2434 	int ret;
2435 
2436 	if (ctrl_conn == NULL)
2437 		return -1;
2438 	len = sizeof(buf) - 1;
2439 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
2440 			       wpa_cli_msg_cb);
2441 	if (ret == -2) {
2442 		printf("'%s' command timed out.\n", cmd);
2443 		return -2;
2444 	} else if (ret < 0) {
2445 		printf("'%s' command failed.\n", cmd);
2446 		return -1;
2447 	}
2448 
2449 	buf[len] = '\0';
2450 	if (os_memcmp(buf, "FAIL", 4) == 0)
2451 		return -1;
2452 
2453 	pos = buf;
2454 	while (*pos != '\0' && *pos != '\n')
2455 		pos++;
2456 	*pos++ = '\0';
2457 	os_strlcpy(addr, buf, addr_len);
2458 	if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL)
2459 		printf("%s\n", addr);
2460 	return 0;
2461 }
2462 
2463 
wpa_cli_cmd_p2p_peers(struct wpa_ctrl * ctrl,int argc,char * argv[])2464 static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[])
2465 {
2466 	char addr[32], cmd[64];
2467 	int discovered;
2468 
2469 	discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0;
2470 
2471 	if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST",
2472 				      addr, sizeof(addr), discovered))
2473 		return -1;
2474 	do {
2475 		os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr);
2476 	} while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr),
2477 			 discovered) == 0);
2478 
2479 	return 0;
2480 }
2481 
2482 
wpa_cli_cmd_p2p_set(struct wpa_ctrl * ctrl,int argc,char * argv[])2483 static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
2484 {
2485 	return wpa_cli_cmd(ctrl, "P2P_SET", 2, argc, argv);
2486 }
2487 
2488 
wpa_cli_complete_p2p_set(const char * str,int pos)2489 static char ** wpa_cli_complete_p2p_set(const char *str, int pos)
2490 {
2491 	int arg = get_cmd_arg_num(str, pos);
2492 	const char *fields[] = {
2493 		"discoverability",
2494 		"managed",
2495 		"listen_channel",
2496 		"ssid_postfix",
2497 		"noa",
2498 		"ps",
2499 		"oppps",
2500 		"ctwindow",
2501 		"disabled",
2502 		"conc_pref",
2503 		"force_long_sd",
2504 		"peer_filter",
2505 		"cross_connect",
2506 		"go_apsd",
2507 		"client_apsd",
2508 		"disallow_freq",
2509 		"disc_int",
2510 		"per_sta_psk",
2511 	};
2512 	int i, num_fields = ARRAY_SIZE(fields);
2513 
2514 	if (arg == 1) {
2515 		char **res = os_calloc(num_fields + 1, sizeof(char *));
2516 		if (res == NULL)
2517 			return NULL;
2518 		for (i = 0; i < num_fields; i++) {
2519 			res[i] = os_strdup(fields[i]);
2520 			if (res[i] == NULL)
2521 				return res;
2522 		}
2523 		return res;
2524 	}
2525 
2526 	if (arg == 2 && os_strncasecmp(str, "p2p_set peer_filter ", 20) == 0)
2527 		return cli_txt_list_array(&p2p_peers);
2528 
2529 	return NULL;
2530 }
2531 
2532 
wpa_cli_cmd_p2p_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])2533 static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2534 {
2535 	return wpa_ctrl_command(ctrl, "P2P_FLUSH");
2536 }
2537 
2538 
wpa_cli_cmd_p2p_cancel(struct wpa_ctrl * ctrl,int argc,char * argv[])2539 static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc,
2540 				  char *argv[])
2541 {
2542 	return wpa_ctrl_command(ctrl, "P2P_CANCEL");
2543 }
2544 
2545 
wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl * ctrl,int argc,char * argv[])2546 static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc,
2547 				       char *argv[])
2548 {
2549 	return wpa_cli_cmd(ctrl, "P2P_UNAUTHORIZE", 1, argc, argv);
2550 }
2551 
2552 
wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl * ctrl,int argc,char * argv[])2553 static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl *ctrl, int argc,
2554 					char *argv[])
2555 {
2556 	if (argc != 0 && argc != 2 && argc != 4) {
2557 		printf("Invalid P2P_PRESENCE_REQ command: needs two arguments "
2558 		       "(preferred duration, interval; in microsecods).\n"
2559 		       "Optional second pair can be used to provide "
2560 		       "acceptable values.\n");
2561 		return -1;
2562 	}
2563 
2564 	return wpa_cli_cmd(ctrl, "P2P_PRESENCE_REQ", 0, argc, argv);
2565 }
2566 
2567 
wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl * ctrl,int argc,char * argv[])2568 static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc,
2569 				      char *argv[])
2570 {
2571 	if (argc != 0 && argc != 2) {
2572 		printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
2573 		       "(availability period, availability interval; in "
2574 		       "millisecods).\n"
2575 		       "Extended Listen Timing can be cancelled with this "
2576 		       "command when used without parameters.\n");
2577 		return -1;
2578 	}
2579 
2580 	return wpa_cli_cmd(ctrl, "P2P_EXT_LISTEN", 0, argc, argv);
2581 }
2582 
2583 
wpa_cli_cmd_p2p_remove_client(struct wpa_ctrl * ctrl,int argc,char * argv[])2584 static int wpa_cli_cmd_p2p_remove_client(struct wpa_ctrl *ctrl, int argc,
2585 					 char *argv[])
2586 {
2587 	return wpa_cli_cmd(ctrl, "P2P_REMOVE_CLIENT", 1, argc, argv);
2588 }
2589 
2590 #endif /* CONFIG_P2P */
2591 
2592 
wpa_cli_cmd_vendor_elem_add(struct wpa_ctrl * ctrl,int argc,char * argv[])2593 static int wpa_cli_cmd_vendor_elem_add(struct wpa_ctrl *ctrl, int argc,
2594 				       char *argv[])
2595 {
2596 	return wpa_cli_cmd(ctrl, "VENDOR_ELEM_ADD", 2, argc, argv);
2597 }
2598 
2599 
wpa_cli_cmd_vendor_elem_get(struct wpa_ctrl * ctrl,int argc,char * argv[])2600 static int wpa_cli_cmd_vendor_elem_get(struct wpa_ctrl *ctrl, int argc,
2601 				       char *argv[])
2602 {
2603 	return wpa_cli_cmd(ctrl, "VENDOR_ELEM_GET", 1, argc, argv);
2604 }
2605 
2606 
wpa_cli_cmd_vendor_elem_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])2607 static int wpa_cli_cmd_vendor_elem_remove(struct wpa_ctrl *ctrl, int argc,
2608 					  char *argv[])
2609 {
2610 	return wpa_cli_cmd(ctrl, "VENDOR_ELEM_REMOVE", 2, argc, argv);
2611 }
2612 
2613 
2614 #ifdef CONFIG_WIFI_DISPLAY
2615 
wpa_cli_cmd_wfd_subelem_set(struct wpa_ctrl * ctrl,int argc,char * argv[])2616 static int wpa_cli_cmd_wfd_subelem_set(struct wpa_ctrl *ctrl, int argc,
2617 				       char *argv[])
2618 {
2619 	char cmd[100];
2620 	int res;
2621 
2622 	if (argc != 1 && argc != 2) {
2623 		printf("Invalid WFD_SUBELEM_SET command: needs one or two "
2624 		       "arguments (subelem, hexdump)\n");
2625 		return -1;
2626 	}
2627 
2628 	res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_SET %s %s",
2629 			  argv[0], argc > 1 ? argv[1] : "");
2630 	if (os_snprintf_error(sizeof(cmd), res))
2631 		return -1;
2632 	cmd[sizeof(cmd) - 1] = '\0';
2633 	return wpa_ctrl_command(ctrl, cmd);
2634 }
2635 
2636 
wpa_cli_cmd_wfd_subelem_get(struct wpa_ctrl * ctrl,int argc,char * argv[])2637 static int wpa_cli_cmd_wfd_subelem_get(struct wpa_ctrl *ctrl, int argc,
2638 				       char *argv[])
2639 {
2640 	char cmd[100];
2641 	int res;
2642 
2643 	if (argc != 1) {
2644 		printf("Invalid WFD_SUBELEM_GET command: needs one "
2645 		       "argument (subelem)\n");
2646 		return -1;
2647 	}
2648 
2649 	res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_GET %s",
2650 			  argv[0]);
2651 	if (os_snprintf_error(sizeof(cmd), res))
2652 		return -1;
2653 	cmd[sizeof(cmd) - 1] = '\0';
2654 	return wpa_ctrl_command(ctrl, cmd);
2655 }
2656 #endif /* CONFIG_WIFI_DISPLAY */
2657 
2658 
2659 #ifdef CONFIG_INTERWORKING
wpa_cli_cmd_fetch_anqp(struct wpa_ctrl * ctrl,int argc,char * argv[])2660 static int wpa_cli_cmd_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2661 				  char *argv[])
2662 {
2663 	return wpa_ctrl_command(ctrl, "FETCH_ANQP");
2664 }
2665 
2666 
wpa_cli_cmd_stop_fetch_anqp(struct wpa_ctrl * ctrl,int argc,char * argv[])2667 static int wpa_cli_cmd_stop_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
2668 				       char *argv[])
2669 {
2670 	return wpa_ctrl_command(ctrl, "STOP_FETCH_ANQP");
2671 }
2672 
2673 
wpa_cli_cmd_interworking_select(struct wpa_ctrl * ctrl,int argc,char * argv[])2674 static int wpa_cli_cmd_interworking_select(struct wpa_ctrl *ctrl, int argc,
2675 					   char *argv[])
2676 {
2677 	return wpa_cli_cmd(ctrl, "INTERWORKING_SELECT", 0, argc, argv);
2678 }
2679 
2680 
wpa_cli_cmd_interworking_connect(struct wpa_ctrl * ctrl,int argc,char * argv[])2681 static int wpa_cli_cmd_interworking_connect(struct wpa_ctrl *ctrl, int argc,
2682 					    char *argv[])
2683 {
2684 	return wpa_cli_cmd(ctrl, "INTERWORKING_CONNECT", 1, argc, argv);
2685 }
2686 
2687 
wpa_cli_cmd_interworking_add_network(struct wpa_ctrl * ctrl,int argc,char * argv[])2688 static int wpa_cli_cmd_interworking_add_network(struct wpa_ctrl *ctrl, int argc,
2689 						char *argv[])
2690 {
2691 	return wpa_cli_cmd(ctrl, "INTERWORKING_ADD_NETWORK", 1, argc, argv);
2692 }
2693 
2694 
wpa_cli_cmd_anqp_get(struct wpa_ctrl * ctrl,int argc,char * argv[])2695 static int wpa_cli_cmd_anqp_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
2696 {
2697 	return wpa_cli_cmd(ctrl, "ANQP_GET", 2, argc, argv);
2698 }
2699 
2700 
wpa_cli_cmd_gas_request(struct wpa_ctrl * ctrl,int argc,char * argv[])2701 static int wpa_cli_cmd_gas_request(struct wpa_ctrl *ctrl, int argc,
2702 				   char *argv[])
2703 {
2704 	return wpa_cli_cmd(ctrl, "GAS_REQUEST", 2, argc, argv);
2705 }
2706 
2707 
wpa_cli_cmd_gas_response_get(struct wpa_ctrl * ctrl,int argc,char * argv[])2708 static int wpa_cli_cmd_gas_response_get(struct wpa_ctrl *ctrl, int argc,
2709 					char *argv[])
2710 {
2711 	return wpa_cli_cmd(ctrl, "GAS_RESPONSE_GET", 2, argc, argv);
2712 }
2713 #endif /* CONFIG_INTERWORKING */
2714 
2715 
2716 #ifdef CONFIG_HS20
2717 
wpa_cli_cmd_hs20_anqp_get(struct wpa_ctrl * ctrl,int argc,char * argv[])2718 static int wpa_cli_cmd_hs20_anqp_get(struct wpa_ctrl *ctrl, int argc,
2719 				     char *argv[])
2720 {
2721 	return wpa_cli_cmd(ctrl, "HS20_ANQP_GET", 2, argc, argv);
2722 }
2723 
2724 
wpa_cli_cmd_get_nai_home_realm_list(struct wpa_ctrl * ctrl,int argc,char * argv[])2725 static int wpa_cli_cmd_get_nai_home_realm_list(struct wpa_ctrl *ctrl, int argc,
2726 					       char *argv[])
2727 {
2728 	char cmd[512];
2729 
2730 	if (argc == 0) {
2731 		printf("Command needs one or two arguments (dst mac addr and "
2732 		       "optional home realm)\n");
2733 		return -1;
2734 	}
2735 
2736 	if (write_cmd(cmd, sizeof(cmd), "HS20_GET_NAI_HOME_REALM_LIST",
2737 		      argc, argv) < 0)
2738 		return -1;
2739 
2740 	return wpa_ctrl_command(ctrl, cmd);
2741 }
2742 
2743 
wpa_cli_cmd_hs20_icon_request(struct wpa_ctrl * ctrl,int argc,char * argv[])2744 static int wpa_cli_cmd_hs20_icon_request(struct wpa_ctrl *ctrl, int argc,
2745 					 char *argv[])
2746 {
2747 	char cmd[512];
2748 
2749 	if (argc < 2) {
2750 		printf("Command needs two arguments (dst mac addr and "
2751 		       "icon name)\n");
2752 		return -1;
2753 	}
2754 
2755 	if (write_cmd(cmd, sizeof(cmd), "HS20_ICON_REQUEST", argc, argv) < 0)
2756 		return -1;
2757 
2758 	return wpa_ctrl_command(ctrl, cmd);
2759 }
2760 
2761 
wpa_cli_cmd_fetch_osu(struct wpa_ctrl * ctrl,int argc,char * argv[])2762 static int wpa_cli_cmd_fetch_osu(struct wpa_ctrl *ctrl, int argc, char *argv[])
2763 {
2764 	return wpa_ctrl_command(ctrl, "FETCH_OSU");
2765 }
2766 
2767 
wpa_cli_cmd_cancel_fetch_osu(struct wpa_ctrl * ctrl,int argc,char * argv[])2768 static int wpa_cli_cmd_cancel_fetch_osu(struct wpa_ctrl *ctrl, int argc,
2769 					char *argv[])
2770 {
2771 	return wpa_ctrl_command(ctrl, "CANCEL_FETCH_OSU");
2772 }
2773 
2774 #endif /* CONFIG_HS20 */
2775 
2776 
wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl * ctrl,int argc,char * argv[])2777 static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc,
2778 				       char *argv[])
2779 {
2780 	return wpa_cli_cmd(ctrl, "STA_AUTOCONNECT", 1, argc, argv);
2781 }
2782 
2783 
wpa_cli_cmd_tdls_discover(struct wpa_ctrl * ctrl,int argc,char * argv[])2784 static int wpa_cli_cmd_tdls_discover(struct wpa_ctrl *ctrl, int argc,
2785 				     char *argv[])
2786 {
2787 	return wpa_cli_cmd(ctrl, "TDLS_DISCOVER", 1, argc, argv);
2788 }
2789 
2790 
wpa_cli_cmd_tdls_setup(struct wpa_ctrl * ctrl,int argc,char * argv[])2791 static int wpa_cli_cmd_tdls_setup(struct wpa_ctrl *ctrl, int argc,
2792 				  char *argv[])
2793 {
2794 	return wpa_cli_cmd(ctrl, "TDLS_SETUP", 1, argc, argv);
2795 }
2796 
2797 
wpa_cli_cmd_tdls_teardown(struct wpa_ctrl * ctrl,int argc,char * argv[])2798 static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
2799 				     char *argv[])
2800 {
2801 	return wpa_cli_cmd(ctrl, "TDLS_TEARDOWN", 1, argc, argv);
2802 }
2803 
2804 
wpa_cli_cmd_tdls_link_status(struct wpa_ctrl * ctrl,int argc,char * argv[])2805 static int wpa_cli_cmd_tdls_link_status(struct wpa_ctrl *ctrl, int argc,
2806 					char *argv[])
2807 {
2808 	return wpa_cli_cmd(ctrl, "TDLS_LINK_STATUS", 1, argc, argv);
2809 }
2810 
2811 
wpa_cli_cmd_wmm_ac_addts(struct wpa_ctrl * ctrl,int argc,char * argv[])2812 static int wpa_cli_cmd_wmm_ac_addts(struct wpa_ctrl *ctrl, int argc,
2813 				    char *argv[])
2814 {
2815 	return wpa_cli_cmd(ctrl, "WMM_AC_ADDTS", 3, argc, argv);
2816 }
2817 
2818 
wpa_cli_cmd_wmm_ac_delts(struct wpa_ctrl * ctrl,int argc,char * argv[])2819 static int wpa_cli_cmd_wmm_ac_delts(struct wpa_ctrl *ctrl, int argc,
2820 				    char *argv[])
2821 {
2822 	return wpa_cli_cmd(ctrl, "WMM_AC_DELTS", 1, argc, argv);
2823 }
2824 
2825 
wpa_cli_cmd_wmm_ac_status(struct wpa_ctrl * ctrl,int argc,char * argv[])2826 static int wpa_cli_cmd_wmm_ac_status(struct wpa_ctrl *ctrl, int argc,
2827 				    char *argv[])
2828 {
2829 	return wpa_ctrl_command(ctrl, "WMM_AC_STATUS");
2830 }
2831 
2832 
wpa_cli_cmd_tdls_chan_switch(struct wpa_ctrl * ctrl,int argc,char * argv[])2833 static int wpa_cli_cmd_tdls_chan_switch(struct wpa_ctrl *ctrl, int argc,
2834 					char *argv[])
2835 {
2836 	return wpa_cli_cmd(ctrl, "TDLS_CHAN_SWITCH", 2, argc, argv);
2837 }
2838 
2839 
wpa_cli_cmd_tdls_cancel_chan_switch(struct wpa_ctrl * ctrl,int argc,char * argv[])2840 static int wpa_cli_cmd_tdls_cancel_chan_switch(struct wpa_ctrl *ctrl, int argc,
2841 					       char *argv[])
2842 {
2843 	return wpa_cli_cmd(ctrl, "TDLS_CANCEL_CHAN_SWITCH", 1, argc, argv);
2844 }
2845 
2846 
wpa_cli_cmd_signal_poll(struct wpa_ctrl * ctrl,int argc,char * argv[])2847 static int wpa_cli_cmd_signal_poll(struct wpa_ctrl *ctrl, int argc,
2848 				   char *argv[])
2849 {
2850 	return wpa_ctrl_command(ctrl, "SIGNAL_POLL");
2851 }
2852 
2853 
wpa_cli_cmd_signal_monitor(struct wpa_ctrl * ctrl,int argc,char * argv[])2854 static int wpa_cli_cmd_signal_monitor(struct wpa_ctrl *ctrl, int argc,
2855 				   char *argv[])
2856 {
2857 	return wpa_cli_cmd(ctrl, "SIGNAL_MONITOR", 0, argc, argv);
2858 }
2859 
2860 
wpa_cli_cmd_pktcnt_poll(struct wpa_ctrl * ctrl,int argc,char * argv[])2861 static int wpa_cli_cmd_pktcnt_poll(struct wpa_ctrl *ctrl, int argc,
2862 				   char *argv[])
2863 {
2864 	return wpa_ctrl_command(ctrl, "PKTCNT_POLL");
2865 }
2866 
2867 
wpa_cli_cmd_reauthenticate(struct wpa_ctrl * ctrl,int argc,char * argv[])2868 static int wpa_cli_cmd_reauthenticate(struct wpa_ctrl *ctrl, int argc,
2869 				      char *argv[])
2870 {
2871 	return wpa_ctrl_command(ctrl, "REAUTHENTICATE");
2872 }
2873 
2874 
2875 #ifdef CONFIG_AUTOSCAN
2876 
wpa_cli_cmd_autoscan(struct wpa_ctrl * ctrl,int argc,char * argv[])2877 static int wpa_cli_cmd_autoscan(struct wpa_ctrl *ctrl, int argc, char *argv[])
2878 {
2879 	if (argc == 0)
2880 		return wpa_ctrl_command(ctrl, "AUTOSCAN ");
2881 
2882 	return wpa_cli_cmd(ctrl, "AUTOSCAN", 0, argc, argv);
2883 }
2884 
2885 #endif /* CONFIG_AUTOSCAN */
2886 
2887 
2888 #ifdef CONFIG_WNM
2889 
wpa_cli_cmd_wnm_sleep(struct wpa_ctrl * ctrl,int argc,char * argv[])2890 static int wpa_cli_cmd_wnm_sleep(struct wpa_ctrl *ctrl, int argc, char *argv[])
2891 {
2892 	return wpa_cli_cmd(ctrl, "WNM_SLEEP", 0, argc, argv);
2893 }
2894 
2895 
wpa_cli_cmd_wnm_bss_query(struct wpa_ctrl * ctrl,int argc,char * argv[])2896 static int wpa_cli_cmd_wnm_bss_query(struct wpa_ctrl *ctrl, int argc, char *argv[])
2897 {
2898 	return wpa_cli_cmd(ctrl, "WNM_BSS_QUERY", 1, argc, argv);
2899 }
2900 
2901 #endif /* CONFIG_WNM */
2902 
2903 
wpa_cli_cmd_raw(struct wpa_ctrl * ctrl,int argc,char * argv[])2904 static int wpa_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
2905 {
2906 	if (argc == 0)
2907 		return -1;
2908 	return wpa_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
2909 }
2910 
2911 
2912 #ifdef ANDROID
wpa_cli_cmd_driver(struct wpa_ctrl * ctrl,int argc,char * argv[])2913 static int wpa_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
2914 {
2915 	return wpa_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
2916 }
2917 #endif /* ANDROID */
2918 
2919 
wpa_cli_cmd_vendor(struct wpa_ctrl * ctrl,int argc,char * argv[])2920 static int wpa_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
2921 {
2922 	return wpa_cli_cmd(ctrl, "VENDOR", 1, argc, argv);
2923 }
2924 
2925 
wpa_cli_cmd_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])2926 static int wpa_cli_cmd_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2927 {
2928 	return wpa_ctrl_command(ctrl, "FLUSH");
2929 }
2930 
2931 
wpa_cli_cmd_radio_work(struct wpa_ctrl * ctrl,int argc,char * argv[])2932 static int wpa_cli_cmd_radio_work(struct wpa_ctrl *ctrl, int argc, char *argv[])
2933 {
2934 	return wpa_cli_cmd(ctrl, "RADIO_WORK", 1, argc, argv);
2935 }
2936 
2937 
wpa_cli_cmd_neighbor_rep_request(struct wpa_ctrl * ctrl,int argc,char * argv[])2938 static int wpa_cli_cmd_neighbor_rep_request(struct wpa_ctrl *ctrl, int argc,
2939 					    char *argv[])
2940 {
2941 	return wpa_cli_cmd(ctrl, "NEIGHBOR_REP_REQUEST", 0, argc, argv);
2942 }
2943 
2944 
wpa_cli_cmd_twt_setup(struct wpa_ctrl * ctrl,int argc,char * argv[])2945 static int wpa_cli_cmd_twt_setup(struct wpa_ctrl *ctrl, int argc,
2946 				 char *argv[])
2947 {
2948 	return wpa_cli_cmd(ctrl, "TWT_SETUP", 0, argc, argv);
2949 }
2950 
2951 
wpa_cli_cmd_twt_teardown(struct wpa_ctrl * ctrl,int argc,char * argv[])2952 static int wpa_cli_cmd_twt_teardown(struct wpa_ctrl *ctrl, int argc,
2953 				    char *argv[])
2954 {
2955 	return wpa_cli_cmd(ctrl, "TWT_TEARDOWN", 0, argc, argv);
2956 }
2957 
2958 
wpa_cli_cmd_erp_flush(struct wpa_ctrl * ctrl,int argc,char * argv[])2959 static int wpa_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
2960 {
2961 	return wpa_ctrl_command(ctrl, "ERP_FLUSH");
2962 }
2963 
2964 
wpa_cli_cmd_mac_rand_scan(struct wpa_ctrl * ctrl,int argc,char * argv[])2965 static int wpa_cli_cmd_mac_rand_scan(struct wpa_ctrl *ctrl, int argc,
2966 				     char *argv[])
2967 {
2968 	return wpa_cli_cmd(ctrl, "MAC_RAND_SCAN", 1, argc, argv);
2969 }
2970 
2971 
wpa_cli_cmd_get_pref_freq_list(struct wpa_ctrl * ctrl,int argc,char * argv[])2972 static int wpa_cli_cmd_get_pref_freq_list(struct wpa_ctrl *ctrl, int argc,
2973 					  char *argv[])
2974 {
2975 	return wpa_cli_cmd(ctrl, "GET_PREF_FREQ_LIST", 1, argc, argv);
2976 }
2977 
2978 
wpa_cli_cmd_p2p_lo_start(struct wpa_ctrl * ctrl,int argc,char * argv[])2979 static int wpa_cli_cmd_p2p_lo_start(struct wpa_ctrl *ctrl, int argc,
2980 				    char *argv[])
2981 {
2982 	return wpa_cli_cmd(ctrl, "P2P_LO_START", 4, argc, argv);
2983 }
2984 
2985 
wpa_cli_cmd_p2p_lo_stop(struct wpa_ctrl * ctrl,int argc,char * argv[])2986 static int wpa_cli_cmd_p2p_lo_stop(struct wpa_ctrl *ctrl, int argc,
2987 				   char *argv[])
2988 {
2989 	return wpa_cli_cmd(ctrl, "P2P_LO_STOP", 0, argc, argv);
2990 }
2991 
2992 
2993 #ifdef CONFIG_DPP
2994 
wpa_cli_cmd_dpp_qr_code(struct wpa_ctrl * ctrl,int argc,char * argv[])2995 static int wpa_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
2996 				   char *argv[])
2997 {
2998 	return wpa_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
2999 }
3000 
3001 
wpa_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl * ctrl,int argc,char * argv[])3002 static int wpa_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
3003 					 char *argv[])
3004 {
3005 	return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
3006 }
3007 
3008 
wpa_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])3009 static int wpa_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
3010 					    char *argv[])
3011 {
3012 	return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
3013 }
3014 
3015 
wpa_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl * ctrl,int argc,char * argv[])3016 static int wpa_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl, int argc,
3017 					     char *argv[])
3018 {
3019 	return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
3020 }
3021 
3022 
wpa_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl * ctrl,int argc,char * argv[])3023 static int wpa_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
3024 					  char *argv[])
3025 {
3026 	return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
3027 }
3028 
3029 
wpa_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl * ctrl,int argc,char * argv[])3030 static int wpa_cli_cmd_dpp_bootstrap_set(struct wpa_ctrl *ctrl, int argc,
3031 					 char *argv[])
3032 {
3033 	return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_SET", 1, argc, argv);
3034 }
3035 
3036 
wpa_cli_cmd_dpp_auth_init(struct wpa_ctrl * ctrl,int argc,char * argv[])3037 static int wpa_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
3038 				     char *argv[])
3039 {
3040 	return wpa_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
3041 }
3042 
3043 
wpa_cli_cmd_dpp_listen(struct wpa_ctrl * ctrl,int argc,char * argv[])3044 static int wpa_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
3045 				  char *argv[])
3046 {
3047 	return wpa_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
3048 }
3049 
3050 
wpa_cli_cmd_dpp_stop_listen(struct wpa_ctrl * ctrl,int argc,char * argv[])3051 static int wpa_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
3052 				       char *argv[])
3053 {
3054 	return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
3055 }
3056 
3057 
wpa_cli_cmd_dpp_configurator_add(struct wpa_ctrl * ctrl,int argc,char * argv[])3058 static int wpa_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
3059 					    char *argv[])
3060 {
3061 	return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
3062 }
3063 
3064 
wpa_cli_cmd_dpp_configurator_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])3065 static int wpa_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl, int argc,
3066 					       char *argv[])
3067 {
3068 	return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
3069 }
3070 
3071 
wpa_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl * ctrl,int argc,char * argv[])3072 static int wpa_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl, int argc,
3073 						char *argv[])
3074 {
3075 	return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
3076 }
3077 
3078 
wpa_cli_cmd_dpp_configurator_sign(struct wpa_ctrl * ctrl,int argc,char * argv[])3079 static int wpa_cli_cmd_dpp_configurator_sign(struct wpa_ctrl *ctrl, int argc,
3080 					     char *argv[])
3081 {
3082 	return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_SIGN", 1, argc, argv);
3083 }
3084 
3085 
wpa_cli_cmd_dpp_pkex_add(struct wpa_ctrl * ctrl,int argc,char * argv[])3086 static int wpa_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
3087 				    char *argv[])
3088 {
3089 	return wpa_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
3090 }
3091 
3092 
wpa_cli_cmd_dpp_pkex_remove(struct wpa_ctrl * ctrl,int argc,char * argv[])3093 static int wpa_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
3094 				       char *argv[])
3095 {
3096 	return wpa_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
3097 }
3098 
3099 
3100 #ifdef CONFIG_DPP2
3101 
wpa_cli_cmd_dpp_controller_start(struct wpa_ctrl * ctrl,int argc,char * argv[])3102 static int wpa_cli_cmd_dpp_controller_start(struct wpa_ctrl *ctrl, int argc,
3103 					    char *argv[])
3104 {
3105 	return wpa_cli_cmd(ctrl, "DPP_CONTROLLER_START", 1, argc, argv);
3106 }
3107 
3108 
wpa_cli_cmd_dpp_controller_stop(struct wpa_ctrl * ctrl,int argc,char * argv[])3109 static int wpa_cli_cmd_dpp_controller_stop(struct wpa_ctrl *ctrl, int argc,
3110 					    char *argv[])
3111 {
3112 	return wpa_ctrl_command(ctrl, "DPP_CONTROLLER_STOP");
3113 }
3114 
3115 
wpa_cli_cmd_dpp_chirp(struct wpa_ctrl * ctrl,int argc,char * argv[])3116 static int wpa_cli_cmd_dpp_chirp(struct wpa_ctrl *ctrl, int argc,
3117 				 char *argv[])
3118 {
3119 	return wpa_cli_cmd(ctrl, "DPP_CHIRP", 1, argc, argv);
3120 }
3121 
3122 
wpa_cli_cmd_dpp_stop_chirp(struct wpa_ctrl * ctrl,int argc,char * argv[])3123 static int wpa_cli_cmd_dpp_stop_chirp(struct wpa_ctrl *ctrl, int argc,
3124 				      char *argv[])
3125 {
3126 	return wpa_ctrl_command(ctrl, "DPP_STOP_CHIRP");
3127 }
3128 
3129 #endif /* CONFIG_DPP2 */
3130 #endif /* CONFIG_DPP */
3131 
3132 
wpa_ctrl_command_bss(struct wpa_ctrl * ctrl,const char * cmd)3133 static int wpa_ctrl_command_bss(struct wpa_ctrl *ctrl, const char *cmd)
3134 {
3135 	char buf[512], *pos, *bssid = NULL, *freq = NULL, *level = NULL,
3136 		*flags = NULL, *ssid = NULL;
3137 	size_t len;
3138 	int ret, id = -1;
3139 
3140 	if (!ctrl_conn)
3141 		return -1;
3142 	len = sizeof(buf) - 1;
3143 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
3144 			       wpa_cli_msg_cb);
3145 	if (ret == -2) {
3146 		printf("'%s' command timed out.\n", cmd);
3147 		return -2;
3148 	} else if (ret < 0) {
3149 		printf("'%s' command failed.\n", cmd);
3150 		return -1;
3151 	}
3152 
3153 	buf[len] = '\0';
3154 	if (os_memcmp(buf, "FAIL", 4) == 0)
3155 		return -1;
3156 
3157 	pos = buf;
3158 	while (*pos != '\0') {
3159 		if (str_starts(pos, "id="))
3160 			id = atoi(pos + 3);
3161 		if (str_starts(pos, "bssid="))
3162 			bssid = pos + 6;
3163 		if (str_starts(pos, "freq="))
3164 			freq = pos + 5;
3165 		if (str_starts(pos, "level="))
3166 			level = pos + 6;
3167 		if (str_starts(pos, "flags="))
3168 			flags = pos + 6;
3169 		if (str_starts(pos, "ssid="))
3170 			ssid = pos + 5;
3171 
3172 		while (*pos != '\0' && *pos != '\n')
3173 			pos++;
3174 		*pos++ = '\0';
3175 	}
3176 	if (id != -1)
3177 		printf("%s\t%s\t%s\t%s\t%s\n", bssid ? bssid : "N/A",
3178 		       freq ? freq : "N/A", level ? level : "N/A",
3179 		       flags ? flags : "N/A", ssid ? ssid : "N/A");
3180 	return id;
3181 }
3182 
3183 
wpa_cli_cmd_all_bss(struct wpa_ctrl * ctrl,int argc,char * argv[])3184 static int wpa_cli_cmd_all_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
3185 {
3186 	char cmd[64];
3187 	int id = -1;
3188 	unsigned int mask;
3189 
3190 	printf("bssid / frequency / signal level / flags / ssid\n");
3191 
3192 	mask = WPA_BSS_MASK_ID | WPA_BSS_MASK_BSSID | WPA_BSS_MASK_FREQ |
3193 		WPA_BSS_MASK_LEVEL | WPA_BSS_MASK_FLAGS | WPA_BSS_MASK_SSID;
3194 	do {
3195 		if (id < 0)
3196 			os_snprintf(cmd, sizeof(cmd), "BSS FIRST MASK=0x%x",
3197 				    mask);
3198 		else
3199 			os_snprintf(cmd, sizeof(cmd), "BSS NEXT-%d MASK=0x%x",
3200 				    id, mask);
3201 		id = wpa_ctrl_command_bss(ctrl, cmd);
3202 	} while (id >= 0);
3203 
3204 	return 0;
3205 }
3206 
3207 
3208 #ifdef CONFIG_PASN
3209 
wpa_cli_cmd_pasn_auth_start(struct wpa_ctrl * ctrl,int argc,char * argv[])3210 static int wpa_cli_cmd_pasn_auth_start(struct wpa_ctrl *ctrl, int argc,
3211 				       char *argv[])
3212 {
3213 	return wpa_cli_cmd(ctrl, "PASN_AUTH_START", 4, argc, argv);
3214 }
3215 
3216 
wpa_cli_cmd_pasn_auth_stop(struct wpa_ctrl * ctrl,int argc,char * argv[])3217 static int wpa_cli_cmd_pasn_auth_stop(struct wpa_ctrl *ctrl, int argc,
3218 				      char *argv[])
3219 {
3220 	return wpa_cli_cmd(ctrl, "PASN_AUTH_STOP", 0, argc, argv);
3221 }
3222 
wpa_cli_cmd_ptksa_cache_list(struct wpa_ctrl * ctrl,int argc,char * argv[])3223 static int wpa_cli_cmd_ptksa_cache_list(struct wpa_ctrl *ctrl, int argc,
3224 					char *argv[])
3225 {
3226 	return wpa_cli_cmd(ctrl, "PTKSA_CACHE_LIST", 0, argc, argv);
3227 }
3228 
3229 
wpa_cli_cmd_pasn_deauth(struct wpa_ctrl * ctrl,int argc,char * argv[])3230 static int wpa_cli_cmd_pasn_deauth(struct wpa_ctrl *ctrl, int argc,
3231 				   char *argv[])
3232 {
3233 	return wpa_cli_cmd(ctrl, "PASN_DEAUTH", 1, argc, argv);
3234 }
3235 
3236 #endif /* CONFIG_PASN */
3237 
3238 
wpa_cli_cmd_mscs(struct wpa_ctrl * ctrl,int argc,char * argv[])3239 static int wpa_cli_cmd_mscs(struct wpa_ctrl *ctrl, int argc, char *argv[])
3240 {
3241 	return wpa_cli_cmd(ctrl, "MSCS", 1, argc, argv);
3242 }
3243 
3244 
wpa_cli_cmd_scs(struct wpa_ctrl * ctrl,int argc,char * argv[])3245 static int wpa_cli_cmd_scs(struct wpa_ctrl *ctrl, int argc, char *argv[])
3246 {
3247 	return wpa_cli_cmd(ctrl, "SCS", 2, argc, argv);
3248 }
3249 
3250 
wpa_cli_cmd_dscp_resp(struct wpa_ctrl * ctrl,int argc,char * argv[])3251 static int wpa_cli_cmd_dscp_resp(struct wpa_ctrl *ctrl, int argc, char *argv[])
3252 {
3253 	return wpa_cli_cmd(ctrl, "DSCP_RESP", 1, argc, argv);
3254 }
3255 
3256 
wpa_cli_cmd_dscp_query(struct wpa_ctrl * ctrl,int argc,char * argv[])3257 static int wpa_cli_cmd_dscp_query(struct wpa_ctrl *ctrl, int argc, char *argv[])
3258 {
3259 	return wpa_cli_cmd(ctrl, "DSCP_QUERY", 1, argc, argv);
3260 }
3261 
3262 
3263 enum wpa_cli_cmd_flags {
3264 	cli_cmd_flag_none		= 0x00,
3265 	cli_cmd_flag_sensitive		= 0x01
3266 };
3267 
3268 struct wpa_cli_cmd {
3269 	const char *cmd;
3270 	int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
3271 	char ** (*completion)(const char *str, int pos);
3272 	enum wpa_cli_cmd_flags flags;
3273 	const char *usage;
3274 };
3275 
3276 static const struct wpa_cli_cmd wpa_cli_commands[] = {
3277 	{ "status", wpa_cli_cmd_status, NULL,
3278 	  cli_cmd_flag_none,
3279 	  "[verbose] = get current WPA/EAPOL/EAP status" },
3280 	{ "ifname", wpa_cli_cmd_ifname, NULL,
3281 	  cli_cmd_flag_none,
3282 	  "= get current interface name" },
3283 	{ "ping", wpa_cli_cmd_ping, NULL,
3284 	  cli_cmd_flag_none,
3285 	  "= pings wpa_supplicant" },
3286 	{ "relog", wpa_cli_cmd_relog, NULL,
3287 	  cli_cmd_flag_none,
3288 	  "= re-open log-file (allow rolling logs)" },
3289 	{ "note", wpa_cli_cmd_note, NULL,
3290 	  cli_cmd_flag_none,
3291 	  "<text> = add a note to wpa_supplicant debug log" },
3292 	{ "mib", wpa_cli_cmd_mib, NULL,
3293 	  cli_cmd_flag_none,
3294 	  "= get MIB variables (dot1x, dot11)" },
3295 	{ "help", wpa_cli_cmd_help, wpa_cli_complete_help,
3296 	  cli_cmd_flag_none,
3297 	  "[command] = show usage help" },
3298 	{ "interface", wpa_cli_cmd_interface, NULL,
3299 	  cli_cmd_flag_none,
3300 	  "[ifname] = show interfaces/select interface" },
3301 	{ "level", wpa_cli_cmd_level, NULL,
3302 	  cli_cmd_flag_none,
3303 	  "<debug level> = change debug level" },
3304 	{ "license", wpa_cli_cmd_license, NULL,
3305 	  cli_cmd_flag_none,
3306 	  "= show full wpa_cli license" },
3307 	{ "quit", wpa_cli_cmd_quit, NULL,
3308 	  cli_cmd_flag_none,
3309 	  "= exit wpa_cli" },
3310 	{ "set", wpa_cli_cmd_set, wpa_cli_complete_set,
3311 	  cli_cmd_flag_none,
3312 	  "= set variables (shows list of variables when run without "
3313 	  "arguments)" },
3314 	{ "dump", wpa_cli_cmd_dump, NULL,
3315 	  cli_cmd_flag_none,
3316 	  "= dump config variables" },
3317 	{ "get", wpa_cli_cmd_get, wpa_cli_complete_get,
3318 	  cli_cmd_flag_none,
3319 	  "<name> = get information" },
3320 	{ "driver_flags", wpa_cli_cmd_driver_flags, NULL,
3321 	  cli_cmd_flag_none,
3322 	  "= list driver flags" },
3323 	{ "logon", wpa_cli_cmd_logon, NULL,
3324 	  cli_cmd_flag_none,
3325 	  "= IEEE 802.1X EAPOL state machine logon" },
3326 	{ "logoff", wpa_cli_cmd_logoff, NULL,
3327 	  cli_cmd_flag_none,
3328 	  "= IEEE 802.1X EAPOL state machine logoff" },
3329 	{ "pmksa", wpa_cli_cmd_pmksa, NULL,
3330 	  cli_cmd_flag_none,
3331 	  "= show PMKSA cache" },
3332 	{ "pmksa_flush", wpa_cli_cmd_pmksa_flush, NULL,
3333 	  cli_cmd_flag_none,
3334 	  "= flush PMKSA cache entries" },
3335 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
3336 	{ "pmksa_get", wpa_cli_cmd_pmksa_get, NULL,
3337 	  cli_cmd_flag_none,
3338 	  "<network_id> = fetch all stored PMKSA cache entries" },
3339 	{ "pmksa_add", wpa_cli_cmd_pmksa_add, NULL,
3340 	  cli_cmd_flag_sensitive,
3341 	  "<network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds> <expiration in seconds> <akmp> <opportunistic> = store PMKSA cache entry from external storage" },
3342 #ifdef CONFIG_MESH
3343 	{ "mesh_pmksa_get", wpa_cli_mesh_cmd_pmksa_get, NULL,
3344 	  cli_cmd_flag_none,
3345 	  "<peer MAC address | any> = fetch all stored mesh PMKSA cache entries" },
3346 	{ "mesh_pmksa_add", wpa_cli_mesh_cmd_pmksa_add, NULL,
3347 	  cli_cmd_flag_sensitive,
3348 	  "<BSSID> <PMKID> <PMK> <expiration in seconds> = store mesh PMKSA cache entry from external storage" },
3349 #endif /* CONFIG_MESH */
3350 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
3351 	{ "reassociate", wpa_cli_cmd_reassociate, NULL,
3352 	  cli_cmd_flag_none,
3353 	  "= force reassociation" },
3354 	{ "reattach", wpa_cli_cmd_reattach, NULL,
3355 	  cli_cmd_flag_none,
3356 	  "= force reassociation back to the same BSS" },
3357 	{ "preauthenticate", wpa_cli_cmd_preauthenticate, wpa_cli_complete_bss,
3358 	  cli_cmd_flag_none,
3359 	  "<BSSID> = force preauthentication" },
3360 	{ "identity", wpa_cli_cmd_identity, wpa_cli_complete_network_id,
3361 	  cli_cmd_flag_none,
3362 	  "<network id> <identity> = configure identity for an SSID" },
3363 	{ "password", wpa_cli_cmd_password, wpa_cli_complete_network_id,
3364 	  cli_cmd_flag_sensitive,
3365 	  "<network id> <password> = configure password for an SSID" },
3366 	{ "new_password", wpa_cli_cmd_new_password,
3367 	  wpa_cli_complete_network_id, cli_cmd_flag_sensitive,
3368 	  "<network id> <password> = change password for an SSID" },
3369 	{ "pin", wpa_cli_cmd_pin, wpa_cli_complete_network_id,
3370 	  cli_cmd_flag_sensitive,
3371 	  "<network id> <pin> = configure pin for an SSID" },
3372 	{ "otp", wpa_cli_cmd_otp, wpa_cli_complete_network_id,
3373 	  cli_cmd_flag_sensitive,
3374 	  "<network id> <password> = configure one-time-password for an SSID"
3375 	},
3376 	{ "psk_passphrase", wpa_cli_cmd_psk_passphrase,
3377 	  wpa_cli_complete_network_id, cli_cmd_flag_sensitive,
3378 	  "<network id> <PSK/passphrase> = configure PSK/passphrase for an SSID" },
3379 	{ "passphrase", wpa_cli_cmd_passphrase, wpa_cli_complete_network_id,
3380 	  cli_cmd_flag_sensitive,
3381 	  "<network id> <passphrase> = configure private key passphrase\n"
3382 	  "  for an SSID" },
3383 	{ "sim", wpa_cli_cmd_sim, wpa_cli_complete_network_id,
3384 	  cli_cmd_flag_sensitive,
3385 	  "<network id> <pin> = report SIM operation result" },
3386 	{ "bssid", wpa_cli_cmd_bssid, wpa_cli_complete_network_id,
3387 	  cli_cmd_flag_none,
3388 	  "<network id> <BSSID> = set preferred BSSID for an SSID" },
3389 	{ "bssid_ignore", wpa_cli_cmd_bssid_ignore, wpa_cli_complete_bss,
3390 	  cli_cmd_flag_none,
3391 	  "<BSSID> = add a BSSID to the list of temporarily ignored BSSs\n"
3392 	  "bssid_ignore clear = clear the list of temporarily ignored BSSIDs\n"
3393 	  "bssid_ignore = display the list of temporarily ignored BSSIDs" },
3394 	{ "blacklist", /* deprecated alias for bssid_ignore */
3395 	  wpa_cli_cmd_bssid_ignore, wpa_cli_complete_bss,
3396 	  cli_cmd_flag_none,
3397 	  "= deprecated alias for bssid_ignore" },
3398 	{ "log_level", wpa_cli_cmd_log_level, NULL,
3399 	  cli_cmd_flag_none,
3400 	  "<level> [<timestamp>] = update the log level/timestamp\n"
3401 	  "log_level = display the current log level and log options" },
3402 	{ "list_networks", wpa_cli_cmd_list_networks, NULL,
3403 	  cli_cmd_flag_none,
3404 	  "= list configured networks" },
3405 	{ "select_network", wpa_cli_cmd_select_network,
3406 	  wpa_cli_complete_network_id,
3407 	  cli_cmd_flag_none,
3408 	  "<network id> = select a network (disable others)" },
3409 	{ "enable_network", wpa_cli_cmd_enable_network,
3410 	  wpa_cli_complete_network_id,
3411 	  cli_cmd_flag_none,
3412 	  "<network id> = enable a network" },
3413 	{ "disable_network", wpa_cli_cmd_disable_network,
3414 	  wpa_cli_complete_network_id,
3415 	  cli_cmd_flag_none,
3416 	  "<network id> = disable a network" },
3417 	{ "add_network", wpa_cli_cmd_add_network, NULL,
3418 	  cli_cmd_flag_none,
3419 	  "= add a network" },
3420 	{ "remove_network", wpa_cli_cmd_remove_network,
3421 	  wpa_cli_complete_network_id,
3422 	  cli_cmd_flag_none,
3423 	  "<network id> = remove a network" },
3424 	{ "set_network", wpa_cli_cmd_set_network, wpa_cli_complete_network,
3425 	  cli_cmd_flag_sensitive,
3426 	  "<network id> <variable> <value> = set network variables (shows\n"
3427 	  "  list of variables when run without arguments)" },
3428 	{ "get_network", wpa_cli_cmd_get_network, wpa_cli_complete_network,
3429 	  cli_cmd_flag_none,
3430 	  "<network id> <variable> = get network variables" },
3431 	{ "dup_network", wpa_cli_cmd_dup_network, wpa_cli_complete_dup_network,
3432 	  cli_cmd_flag_none,
3433 	  "<src network id> <dst network id> <variable> = duplicate network variables"
3434 	},
3435 	{ "list_creds", wpa_cli_cmd_list_creds, NULL,
3436 	  cli_cmd_flag_none,
3437 	  "= list configured credentials" },
3438 	{ "add_cred", wpa_cli_cmd_add_cred, NULL,
3439 	  cli_cmd_flag_none,
3440 	  "= add a credential" },
3441 	{ "remove_cred", wpa_cli_cmd_remove_cred, NULL,
3442 	  cli_cmd_flag_none,
3443 	  "<cred id> = remove a credential" },
3444 	{ "set_cred", wpa_cli_cmd_set_cred, wpa_cli_complete_cred,
3445 	  cli_cmd_flag_sensitive,
3446 	  "<cred id> <variable> <value> = set credential variables" },
3447 	{ "get_cred", wpa_cli_cmd_get_cred, wpa_cli_complete_cred,
3448 	  cli_cmd_flag_none,
3449 	  "<cred id> <variable> = get credential variables" },
3450 	{ "save_config", wpa_cli_cmd_save_config, NULL,
3451 	  cli_cmd_flag_none,
3452 	  "= save the current configuration" },
3453 	{ "disconnect", wpa_cli_cmd_disconnect, NULL,
3454 	  cli_cmd_flag_none,
3455 	  "= disconnect and wait for reassociate/reconnect command before\n"
3456 	  "  connecting" },
3457 	{ "reconnect", wpa_cli_cmd_reconnect, NULL,
3458 	  cli_cmd_flag_none,
3459 	  "= like reassociate, but only takes effect if already disconnected"
3460 	},
3461 	{ "scan", wpa_cli_cmd_scan, NULL,
3462 	  cli_cmd_flag_none,
3463 	  "= request new BSS scan" },
3464 	{ "scan_results", wpa_cli_cmd_scan_results, NULL,
3465 	  cli_cmd_flag_none,
3466 	  "= get latest scan results" },
3467 	{ "abort_scan", wpa_cli_cmd_abort_scan, NULL,
3468 	  cli_cmd_flag_none,
3469 	  "= request ongoing scan to be aborted" },
3470 	{ "bss", wpa_cli_cmd_bss, wpa_cli_complete_bss,
3471 	  cli_cmd_flag_none,
3472 	  "<<idx> | <bssid>> = get detailed scan result info" },
3473 	{ "get_capability", wpa_cli_cmd_get_capability,
3474 	  wpa_cli_complete_get_capability, cli_cmd_flag_none,
3475 	  "<eap/pairwise/group/key_mgmt/proto/auth_alg/channels/freq/modes> "
3476 	  "= get capabilities" },
3477 	{ "reconfigure", wpa_cli_cmd_reconfigure, NULL,
3478 	  cli_cmd_flag_none,
3479 	  "= force wpa_supplicant to re-read its configuration file" },
3480 	{ "terminate", wpa_cli_cmd_terminate, NULL,
3481 	  cli_cmd_flag_none,
3482 	  "= terminate wpa_supplicant" },
3483 	{ "interface_add", wpa_cli_cmd_interface_add, NULL,
3484 	  cli_cmd_flag_none,
3485 	  "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
3486 	  "  <bridge_name> <create> <type> = adds new interface, all "
3487 	  "parameters but\n"
3488 	  "  <ifname> are optional. Supported types are station ('sta') and "
3489 	  "AP ('ap')" },
3490 	{ "interface_remove", wpa_cli_cmd_interface_remove, NULL,
3491 	  cli_cmd_flag_none,
3492 	  "<ifname> = removes the interface" },
3493 	{ "interface_list", wpa_cli_cmd_interface_list, NULL,
3494 	  cli_cmd_flag_none,
3495 	  "= list available interfaces" },
3496 	{ "ap_scan", wpa_cli_cmd_ap_scan, NULL,
3497 	  cli_cmd_flag_none,
3498 	  "<value> = set ap_scan parameter" },
3499 	{ "scan_interval", wpa_cli_cmd_scan_interval, NULL,
3500 	  cli_cmd_flag_none,
3501 	  "<value> = set scan_interval parameter (in seconds)" },
3502 	{ "bss_expire_age", wpa_cli_cmd_bss_expire_age, NULL,
3503 	  cli_cmd_flag_none,
3504 	  "<value> = set BSS expiration age parameter" },
3505 	{ "bss_expire_count", wpa_cli_cmd_bss_expire_count, NULL,
3506 	  cli_cmd_flag_none,
3507 	  "<value> = set BSS expiration scan count parameter" },
3508 	{ "bss_flush", wpa_cli_cmd_bss_flush, NULL,
3509 	  cli_cmd_flag_none,
3510 	  "<value> = set BSS flush age (0 by default)" },
3511 	{ "ft_ds", wpa_cli_cmd_ft_ds, wpa_cli_complete_bss,
3512 	  cli_cmd_flag_none,
3513 	  "<addr> = request over-the-DS FT with <addr>" },
3514 	{ "wps_pbc", wpa_cli_cmd_wps_pbc, wpa_cli_complete_bss,
3515 	  cli_cmd_flag_none,
3516 	  "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
3517 	{ "wps_pin", wpa_cli_cmd_wps_pin, wpa_cli_complete_bss,
3518 	  cli_cmd_flag_sensitive,
3519 	  "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
3520 	  "hardcoded)" },
3521 	{ "wps_check_pin", wpa_cli_cmd_wps_check_pin, NULL,
3522 	  cli_cmd_flag_sensitive,
3523 	  "<PIN> = verify PIN checksum" },
3524 	{ "wps_cancel", wpa_cli_cmd_wps_cancel, NULL, cli_cmd_flag_none,
3525 	  "Cancels the pending WPS operation" },
3526 #ifdef CONFIG_WPS_NFC
3527 	{ "wps_nfc", wpa_cli_cmd_wps_nfc, wpa_cli_complete_bss,
3528 	  cli_cmd_flag_none,
3529 	  "[BSSID] = start Wi-Fi Protected Setup: NFC" },
3530 	{ "wps_nfc_config_token", wpa_cli_cmd_wps_nfc_config_token, NULL,
3531 	  cli_cmd_flag_none,
3532 	  "<WPS|NDEF> = build configuration token" },
3533 	{ "wps_nfc_token", wpa_cli_cmd_wps_nfc_token, NULL,
3534 	  cli_cmd_flag_none,
3535 	  "<WPS|NDEF> = create password token" },
3536 	{ "wps_nfc_tag_read", wpa_cli_cmd_wps_nfc_tag_read, NULL,
3537 	  cli_cmd_flag_sensitive,
3538 	  "<hexdump of payload> = report read NFC tag with WPS data" },
3539 	{ "nfc_get_handover_req", wpa_cli_cmd_nfc_get_handover_req, NULL,
3540 	  cli_cmd_flag_none,
3541 	  "<NDEF> <WPS> = create NFC handover request" },
3542 	{ "nfc_get_handover_sel", wpa_cli_cmd_nfc_get_handover_sel, NULL,
3543 	  cli_cmd_flag_none,
3544 	  "<NDEF> <WPS> = create NFC handover select" },
3545 	{ "nfc_report_handover", wpa_cli_cmd_nfc_report_handover, NULL,
3546 	  cli_cmd_flag_none,
3547 	  "<role> <type> <hexdump of req> <hexdump of sel> = report completed "
3548 	  "NFC handover" },
3549 #endif /* CONFIG_WPS_NFC */
3550 	{ "wps_reg", wpa_cli_cmd_wps_reg, wpa_cli_complete_bss,
3551 	  cli_cmd_flag_sensitive,
3552 	  "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
3553 	{ "wps_ap_pin", wpa_cli_cmd_wps_ap_pin, NULL,
3554 	  cli_cmd_flag_sensitive,
3555 	  "[params..] = enable/disable AP PIN" },
3556 	{ "wps_er_start", wpa_cli_cmd_wps_er_start, NULL,
3557 	  cli_cmd_flag_none,
3558 	  "[IP address] = start Wi-Fi Protected Setup External Registrar" },
3559 	{ "wps_er_stop", wpa_cli_cmd_wps_er_stop, NULL,
3560 	  cli_cmd_flag_none,
3561 	  "= stop Wi-Fi Protected Setup External Registrar" },
3562 	{ "wps_er_pin", wpa_cli_cmd_wps_er_pin, NULL,
3563 	  cli_cmd_flag_sensitive,
3564 	  "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
3565 	{ "wps_er_pbc", wpa_cli_cmd_wps_er_pbc, NULL,
3566 	  cli_cmd_flag_none,
3567 	  "<UUID> = accept an Enrollee PBC using External Registrar" },
3568 	{ "wps_er_learn", wpa_cli_cmd_wps_er_learn, NULL,
3569 	  cli_cmd_flag_sensitive,
3570 	  "<UUID> <PIN> = learn AP configuration" },
3571 	{ "wps_er_set_config", wpa_cli_cmd_wps_er_set_config, NULL,
3572 	  cli_cmd_flag_none,
3573 	  "<UUID> <network id> = set AP configuration for enrolling" },
3574 	{ "wps_er_config", wpa_cli_cmd_wps_er_config, NULL,
3575 	  cli_cmd_flag_sensitive,
3576 	  "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
3577 #ifdef CONFIG_WPS_NFC
3578 	{ "wps_er_nfc_config_token", wpa_cli_cmd_wps_er_nfc_config_token, NULL,
3579 	  cli_cmd_flag_none,
3580 	  "<WPS/NDEF> <UUID> = build NFC configuration token" },
3581 #endif /* CONFIG_WPS_NFC */
3582 	{ "ibss_rsn", wpa_cli_cmd_ibss_rsn, NULL,
3583 	  cli_cmd_flag_none,
3584 	  "<addr> = request RSN authentication with <addr> in IBSS" },
3585 #ifdef CONFIG_AP
3586 	{ "sta", wpa_cli_cmd_sta, wpa_cli_complete_sta,
3587 	  cli_cmd_flag_none,
3588 	  "<addr> = get information about an associated station (AP)" },
3589 	{ "all_sta", wpa_cli_cmd_all_sta, NULL,
3590 	  cli_cmd_flag_none,
3591 	  "= get information about all associated stations (AP)" },
3592 	{ "list_sta", wpa_cli_cmd_list_sta, NULL,
3593 	  cli_cmd_flag_none,
3594 	  "= list all stations (AP)" },
3595 	{ "deauthenticate", wpa_cli_cmd_deauthenticate,
3596 	  wpa_cli_complete_deauthenticate, cli_cmd_flag_none,
3597 	  "<addr> = deauthenticate a station" },
3598 	{ "disassociate", wpa_cli_cmd_disassociate,
3599 	  wpa_cli_complete_disassociate, cli_cmd_flag_none,
3600 	  "<addr> = disassociate a station" },
3601 	{ "chan_switch", wpa_cli_cmd_chanswitch, NULL,
3602 	  cli_cmd_flag_none,
3603 	  "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]"
3604 	  " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]"
3605 	  " = CSA parameters" },
3606 	{ "update_beacon", wpa_cli_cmd_update_beacon, NULL,
3607 	  cli_cmd_flag_none,
3608 	  "= update Beacon frame contents"},
3609 #endif /* CONFIG_AP */
3610 	{ "suspend", wpa_cli_cmd_suspend, NULL, cli_cmd_flag_none,
3611 	  "= notification of suspend/hibernate" },
3612 	{ "resume", wpa_cli_cmd_resume, NULL, cli_cmd_flag_none,
3613 	  "= notification of resume/thaw" },
3614 #ifdef CONFIG_TESTING_OPTIONS
3615 	{ "drop_sa", wpa_cli_cmd_drop_sa, NULL, cli_cmd_flag_none,
3616 	  "= drop SA without deauth/disassoc (test command)" },
3617 #endif /* CONFIG_TESTING_OPTIONS */
3618 	{ "roam", wpa_cli_cmd_roam, wpa_cli_complete_bss,
3619 	  cli_cmd_flag_none,
3620 	  "<addr> = roam to the specified BSS" },
3621 #ifdef CONFIG_MESH
3622 	{ "mesh_interface_add", wpa_cli_cmd_mesh_interface_add, NULL,
3623 	  cli_cmd_flag_none,
3624 	  "[ifname] = Create a new mesh interface" },
3625 	{ "mesh_group_add", wpa_cli_cmd_mesh_group_add, NULL,
3626 	  cli_cmd_flag_none,
3627 	  "<network id> = join a mesh network (disable others)" },
3628 	{ "mesh_group_remove", wpa_cli_cmd_mesh_group_remove, NULL,
3629 	  cli_cmd_flag_none,
3630 	  "<ifname> = Remove mesh group interface" },
3631 	{ "mesh_peer_remove", wpa_cli_cmd_mesh_peer_remove, NULL,
3632 	  cli_cmd_flag_none,
3633 	  "<addr> = Remove a mesh peer" },
3634 	{ "mesh_peer_add", wpa_cli_cmd_mesh_peer_add, NULL,
3635 	  cli_cmd_flag_none,
3636 	  "<addr> [duration=<seconds>] = Add a mesh peer" },
3637 	{ "mesh_link_probe", wpa_cli_cmd_mesh_link_probe, NULL,
3638 	  cli_cmd_flag_none,
3639 	  "<addr> [payload=<hex dump of payload>] = Probe a mesh link for a given peer by injecting a frame." },
3640 #endif /* CONFIG_MESH */
3641 #ifdef CONFIG_P2P
3642 	{ "p2p_find", wpa_cli_cmd_p2p_find, wpa_cli_complete_p2p_find,
3643 	  cli_cmd_flag_none,
3644 	  "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
3645 	{ "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, NULL, cli_cmd_flag_none,
3646 	  "= stop P2P Devices search" },
3647 	{ "p2p_asp_provision", wpa_cli_cmd_p2p_asp_provision, NULL,
3648 	  cli_cmd_flag_none,
3649 	  "<addr> adv_id=<adv_id> conncap=<conncap> [info=<infodata>] = provision with a P2P ASP Device" },
3650 	{ "p2p_asp_provision_resp", wpa_cli_cmd_p2p_asp_provision_resp, NULL,
3651 	  cli_cmd_flag_none,
3652 	  "<addr> adv_id=<adv_id> [role<conncap>] [info=<infodata>] = provision with a P2P ASP Device" },
3653 	{ "p2p_connect", wpa_cli_cmd_p2p_connect, wpa_cli_complete_p2p_connect,
3654 	  cli_cmd_flag_none,
3655 	  "<addr> <\"pbc\"|PIN> [ht40] = connect to a P2P Device" },
3656 	{ "p2p_listen", wpa_cli_cmd_p2p_listen, NULL, cli_cmd_flag_none,
3657 	  "[timeout] = listen for P2P Devices for up-to timeout seconds" },
3658 	{ "p2p_group_remove", wpa_cli_cmd_p2p_group_remove,
3659 	  wpa_cli_complete_p2p_group_remove, cli_cmd_flag_none,
3660 	  "<ifname> = remove P2P group interface (terminate group if GO)" },
3661 	{ "p2p_group_add", wpa_cli_cmd_p2p_group_add, NULL, cli_cmd_flag_none,
3662 	  "[ht40] = add a new P2P group (local end as GO)" },
3663 	{ "p2p_group_member", wpa_cli_cmd_p2p_group_member, NULL,
3664 	  cli_cmd_flag_none,
3665 	  "<dev_addr> = Get peer interface address on local GO using peer Device Address" },
3666 	{ "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc,
3667 	  wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
3668 	  "<addr> <method> = request provisioning discovery" },
3669 	{ "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase, NULL,
3670 	  cli_cmd_flag_none,
3671 	  "= get the passphrase for a group (GO only)" },
3672 	{ "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req,
3673 	  wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
3674 	  "<addr> <TLVs> = schedule service discovery request" },
3675 	{ "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req,
3676 	  NULL, cli_cmd_flag_none,
3677 	  "<id> = cancel pending service discovery request" },
3678 	{ "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp, NULL,
3679 	  cli_cmd_flag_none,
3680 	  "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
3681 	{ "p2p_service_update", wpa_cli_cmd_p2p_service_update, NULL,
3682 	  cli_cmd_flag_none,
3683 	  "= indicate change in local services" },
3684 	{ "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external, NULL,
3685 	  cli_cmd_flag_none,
3686 	  "<external> = set external processing of service discovery" },
3687 	{ "p2p_service_flush", wpa_cli_cmd_p2p_service_flush, NULL,
3688 	  cli_cmd_flag_none,
3689 	  "= remove all stored service entries" },
3690 	{ "p2p_service_add", wpa_cli_cmd_p2p_service_add, NULL,
3691 	  cli_cmd_flag_none,
3692 	  "<bonjour|upnp|asp> <query|version> <response|service> = add a local "
3693 	  "service" },
3694 	{ "p2p_service_rep", wpa_cli_cmd_p2p_service_rep, NULL,
3695 	  cli_cmd_flag_none,
3696 	  "asp <auto> <adv_id> <svc_state> <svc_string> [<svc_info>] = replace "
3697 	  "local ASP service" },
3698 	{ "p2p_service_del", wpa_cli_cmd_p2p_service_del, NULL,
3699 	  cli_cmd_flag_none,
3700 	  "<bonjour|upnp> <query|version> [|service] = remove a local "
3701 	  "service" },
3702 	{ "p2p_reject", wpa_cli_cmd_p2p_reject, wpa_cli_complete_p2p_peer,
3703 	  cli_cmd_flag_none,
3704 	  "<addr> = reject connection attempts from a specific peer" },
3705 	{ "p2p_invite", wpa_cli_cmd_p2p_invite, NULL,
3706 	  cli_cmd_flag_none,
3707 	  "<cmd> [peer=addr] = invite peer" },
3708 	{ "p2p_peers", wpa_cli_cmd_p2p_peers, NULL, cli_cmd_flag_none,
3709 	  "[discovered] = list known (optionally, only fully discovered) P2P "
3710 	  "peers" },
3711 	{ "p2p_peer", wpa_cli_cmd_p2p_peer, wpa_cli_complete_p2p_peer,
3712 	  cli_cmd_flag_none,
3713 	  "<address> = show information about known P2P peer" },
3714 	{ "p2p_set", wpa_cli_cmd_p2p_set, wpa_cli_complete_p2p_set,
3715 	  cli_cmd_flag_none,
3716 	  "<field> <value> = set a P2P parameter" },
3717 	{ "p2p_flush", wpa_cli_cmd_p2p_flush, NULL, cli_cmd_flag_none,
3718 	  "= flush P2P state" },
3719 	{ "p2p_cancel", wpa_cli_cmd_p2p_cancel, NULL, cli_cmd_flag_none,
3720 	  "= cancel P2P group formation" },
3721 	{ "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize,
3722 	  wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
3723 	  "<address> = unauthorize a peer" },
3724 	{ "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, NULL,
3725 	  cli_cmd_flag_none,
3726 	  "[<duration> <interval>] [<duration> <interval>] = request GO "
3727 	  "presence" },
3728 	{ "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, NULL,
3729 	  cli_cmd_flag_none,
3730 	  "[<period> <interval>] = set extended listen timing" },
3731 	{ "p2p_remove_client", wpa_cli_cmd_p2p_remove_client,
3732 	  wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
3733 	  "<address|iface=address> = remove a peer from all groups" },
3734 #endif /* CONFIG_P2P */
3735 	{ "vendor_elem_add", wpa_cli_cmd_vendor_elem_add, NULL,
3736 	  cli_cmd_flag_none,
3737 	  "<frame id> <hexdump of elem(s)> = add vendor specific IEs to frame(s)\n"
3738 	  VENDOR_ELEM_FRAME_ID },
3739 	{ "vendor_elem_get", wpa_cli_cmd_vendor_elem_get, NULL,
3740 	  cli_cmd_flag_none,
3741 	  "<frame id> = get vendor specific IE(s) to frame(s)\n"
3742 	  VENDOR_ELEM_FRAME_ID },
3743 	{ "vendor_elem_remove", wpa_cli_cmd_vendor_elem_remove, NULL,
3744 	  cli_cmd_flag_none,
3745 	  "<frame id> <hexdump of elem(s)> = remove vendor specific IE(s) in frame(s)\n"
3746 	  VENDOR_ELEM_FRAME_ID },
3747 #ifdef CONFIG_WIFI_DISPLAY
3748 	{ "wfd_subelem_set", wpa_cli_cmd_wfd_subelem_set, NULL,
3749 	  cli_cmd_flag_none,
3750 	  "<subelem> [contents] = set Wi-Fi Display subelement" },
3751 	{ "wfd_subelem_get", wpa_cli_cmd_wfd_subelem_get, NULL,
3752 	  cli_cmd_flag_none,
3753 	  "<subelem> = get Wi-Fi Display subelement" },
3754 #endif /* CONFIG_WIFI_DISPLAY */
3755 #ifdef CONFIG_INTERWORKING
3756 	{ "fetch_anqp", wpa_cli_cmd_fetch_anqp, NULL, cli_cmd_flag_none,
3757 	  "= fetch ANQP information for all APs" },
3758 	{ "stop_fetch_anqp", wpa_cli_cmd_stop_fetch_anqp, NULL,
3759 	  cli_cmd_flag_none,
3760 	  "= stop fetch_anqp operation" },
3761 	{ "interworking_select", wpa_cli_cmd_interworking_select, NULL,
3762 	  cli_cmd_flag_none,
3763 	  "[auto] = perform Interworking network selection" },
3764 	{ "interworking_connect", wpa_cli_cmd_interworking_connect,
3765 	  wpa_cli_complete_bss, cli_cmd_flag_none,
3766 	  "<BSSID> = connect using Interworking credentials" },
3767 	{ "interworking_add_network", wpa_cli_cmd_interworking_add_network,
3768 	  wpa_cli_complete_bss, cli_cmd_flag_none,
3769 	  "<BSSID> = connect using Interworking credentials" },
3770 	{ "anqp_get", wpa_cli_cmd_anqp_get, wpa_cli_complete_bss,
3771 	  cli_cmd_flag_none,
3772 	  "<addr> <info id>[,<info id>]... = request ANQP information" },
3773 	{ "gas_request", wpa_cli_cmd_gas_request, wpa_cli_complete_bss,
3774 	  cli_cmd_flag_none,
3775 	  "<addr> <AdvProtoID> [QueryReq] = GAS request" },
3776 	{ "gas_response_get", wpa_cli_cmd_gas_response_get,
3777 	  wpa_cli_complete_bss, cli_cmd_flag_none,
3778 	  "<addr> <dialog token> [start,len] = Fetch last GAS response" },
3779 #endif /* CONFIG_INTERWORKING */
3780 #ifdef CONFIG_HS20
3781 	{ "hs20_anqp_get", wpa_cli_cmd_hs20_anqp_get, wpa_cli_complete_bss,
3782 	  cli_cmd_flag_none,
3783 	  "<addr> <subtype>[,<subtype>]... = request HS 2.0 ANQP information"
3784 	},
3785 	{ "nai_home_realm_list", wpa_cli_cmd_get_nai_home_realm_list,
3786 	  wpa_cli_complete_bss, cli_cmd_flag_none,
3787 	  "<addr> <home realm> = get HS20 nai home realm list" },
3788 	{ "hs20_icon_request", wpa_cli_cmd_hs20_icon_request,
3789 	  wpa_cli_complete_bss, cli_cmd_flag_none,
3790 	  "<addr> <icon name> = get Hotspot 2.0 OSU icon" },
3791 	{ "fetch_osu", wpa_cli_cmd_fetch_osu, NULL, cli_cmd_flag_none,
3792 	  "= fetch OSU provider information from all APs" },
3793 	{ "cancel_fetch_osu", wpa_cli_cmd_cancel_fetch_osu, NULL,
3794 	  cli_cmd_flag_none,
3795 	  "= cancel fetch_osu command" },
3796 #endif /* CONFIG_HS20 */
3797 	{ "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, NULL,
3798 	  cli_cmd_flag_none,
3799 	  "<0/1> = disable/enable automatic reconnection" },
3800 	{ "tdls_discover", wpa_cli_cmd_tdls_discover, NULL,
3801 	  cli_cmd_flag_none,
3802 	  "<addr> = request TDLS discovery with <addr>" },
3803 	{ "tdls_setup", wpa_cli_cmd_tdls_setup, NULL,
3804 	  cli_cmd_flag_none,
3805 	  "<addr> = request TDLS setup with <addr>" },
3806 	{ "tdls_teardown", wpa_cli_cmd_tdls_teardown, NULL,
3807 	  cli_cmd_flag_none,
3808 	  "<addr> = tear down TDLS with <addr>" },
3809 	{ "tdls_link_status", wpa_cli_cmd_tdls_link_status, NULL,
3810 	  cli_cmd_flag_none,
3811 	  "<addr> = TDLS link status with <addr>" },
3812 	{ "wmm_ac_addts", wpa_cli_cmd_wmm_ac_addts, NULL,
3813 	  cli_cmd_flag_none,
3814 	  "<uplink/downlink/bidi> <tsid=0..7> <up=0..7> [nominal_msdu_size=#] "
3815 	  "[mean_data_rate=#] [min_phy_rate=#] [sba=#] [fixed_nominal_msdu] "
3816 	  "= add WMM-AC traffic stream" },
3817 	{ "wmm_ac_delts", wpa_cli_cmd_wmm_ac_delts, NULL,
3818 	  cli_cmd_flag_none,
3819 	  "<tsid> = delete WMM-AC traffic stream" },
3820 	{ "wmm_ac_status", wpa_cli_cmd_wmm_ac_status, NULL,
3821 	  cli_cmd_flag_none,
3822 	  "= show status for Wireless Multi-Media Admission-Control" },
3823 	{ "tdls_chan_switch", wpa_cli_cmd_tdls_chan_switch, NULL,
3824 	  cli_cmd_flag_none,
3825 	  "<addr> <oper class> <freq> [sec_channel_offset=] [center_freq1=] "
3826 	  "[center_freq2=] [bandwidth=] [ht|vht] = enable channel switching "
3827 	  "with TDLS peer" },
3828 	{ "tdls_cancel_chan_switch", wpa_cli_cmd_tdls_cancel_chan_switch, NULL,
3829 	  cli_cmd_flag_none,
3830 	  "<addr> = disable channel switching with TDLS peer <addr>" },
3831 	{ "signal_poll", wpa_cli_cmd_signal_poll, NULL,
3832 	  cli_cmd_flag_none,
3833 	  "= get signal parameters" },
3834 	{ "signal_monitor", wpa_cli_cmd_signal_monitor, NULL,
3835 	  cli_cmd_flag_none,
3836 	  "= set signal monitor parameters" },
3837 	{ "pktcnt_poll", wpa_cli_cmd_pktcnt_poll, NULL,
3838 	  cli_cmd_flag_none,
3839 	  "= get TX/RX packet counters" },
3840 	{ "reauthenticate", wpa_cli_cmd_reauthenticate, NULL,
3841 	  cli_cmd_flag_none,
3842 	  "= trigger IEEE 802.1X/EAPOL reauthentication" },
3843 #ifdef CONFIG_AUTOSCAN
3844 	{ "autoscan", wpa_cli_cmd_autoscan, NULL, cli_cmd_flag_none,
3845 	  "[params] = Set or unset (if none) autoscan parameters" },
3846 #endif /* CONFIG_AUTOSCAN */
3847 #ifdef CONFIG_WNM
3848 	{ "wnm_sleep", wpa_cli_cmd_wnm_sleep, NULL, cli_cmd_flag_none,
3849 	  "<enter/exit> [interval=#] = enter/exit WNM-Sleep mode" },
3850 	{ "wnm_bss_query", wpa_cli_cmd_wnm_bss_query, NULL, cli_cmd_flag_none,
3851 	  "<query reason> [list]"
3852 	  " [neighbor=<BSSID>,<BSSID information>,<operating class>,<channel number>,<PHY type>[,<hexdump of optional subelements>]"
3853 	  " = Send BSS Transition Management Query" },
3854 #endif /* CONFIG_WNM */
3855 	{ "raw", wpa_cli_cmd_raw, NULL, cli_cmd_flag_sensitive,
3856 	  "<params..> = Sent unprocessed command" },
3857 	{ "flush", wpa_cli_cmd_flush, NULL, cli_cmd_flag_none,
3858 	  "= flush wpa_supplicant state" },
3859 #ifdef ANDROID
3860 	{ "driver", wpa_cli_cmd_driver, NULL, cli_cmd_flag_none,
3861 	  "<command> = driver private commands" },
3862 #endif /* ANDROID */
3863 	{ "radio_work", wpa_cli_cmd_radio_work, NULL, cli_cmd_flag_none,
3864 	  "= radio_work <show/add/done>" },
3865 	{ "vendor", wpa_cli_cmd_vendor, NULL, cli_cmd_flag_none,
3866 	  "<vendor id> <command id> [<hex formatted command argument>] = Send vendor command"
3867 	},
3868 	{ "neighbor_rep_request",
3869 	  wpa_cli_cmd_neighbor_rep_request, NULL, cli_cmd_flag_none,
3870 	  "[ssid=<SSID>] [lci] [civic] = Trigger request to AP for neighboring AP report (with optional given SSID in hex or enclosed in double quotes, default: current SSID; with optional LCI and location civic request)"
3871 	},
3872 	{ "twt_setup",
3873 	  wpa_cli_cmd_twt_setup, NULL, cli_cmd_flag_none,
3874 	  "[dialog=<token>] [exponent=<exponent>] [mantissa=<mantissa>] [min_twt=<Min TWT>] [setup_cmd=<setup-cmd>] [twt=<u64>] [requestor=0|1] [trigger=0|1] [implicit=0|1] [flow_type=0|1] [flow_id=<3-bit-id>] [protection=0|1] [twt_channel=<twt chanel id>] [control=<control-u8>] = Send TWT Setup frame"
3875 	},
3876 	{ "twt_teardown",
3877 	  wpa_cli_cmd_twt_teardown, NULL, cli_cmd_flag_none,
3878 	  "[flags=<value>] = Send TWT Teardown frame"
3879 	},
3880 	{ "erp_flush", wpa_cli_cmd_erp_flush, NULL, cli_cmd_flag_none,
3881 	  "= flush ERP keys" },
3882 	{ "mac_rand_scan",
3883 	  wpa_cli_cmd_mac_rand_scan, NULL, cli_cmd_flag_none,
3884 	  "<scan|sched|pno|all> enable=<0/1> [addr=mac-address "
3885 	  "mask=mac-address-mask] = scan MAC randomization"
3886 	},
3887 	{ "get_pref_freq_list", wpa_cli_cmd_get_pref_freq_list, NULL,
3888 	  cli_cmd_flag_none,
3889 	  "<interface type> = retrieve preferred freq list for the specified interface type" },
3890 	{ "p2p_lo_start", wpa_cli_cmd_p2p_lo_start, NULL,
3891 	  cli_cmd_flag_none,
3892 	  "<freq> <period> <interval> <count> = start P2P listen offload" },
3893 	{ "p2p_lo_stop", wpa_cli_cmd_p2p_lo_stop, NULL,
3894 	  cli_cmd_flag_none,
3895 	  "= stop P2P listen offload" },
3896 #ifdef CONFIG_DPP
3897 	{ "dpp_qr_code", wpa_cli_cmd_dpp_qr_code, NULL, cli_cmd_flag_none,
3898 	  "report a scanned DPP URI from a QR Code" },
3899 	{ "dpp_bootstrap_gen", wpa_cli_cmd_dpp_bootstrap_gen, NULL,
3900 	  cli_cmd_flag_sensitive,
3901 	  "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
3902 	{ "dpp_bootstrap_remove", wpa_cli_cmd_dpp_bootstrap_remove, NULL,
3903 	  cli_cmd_flag_none,
3904 	  "*|<id> = remove DPP bootstrap information" },
3905 	{ "dpp_bootstrap_get_uri", wpa_cli_cmd_dpp_bootstrap_get_uri, NULL,
3906 	  cli_cmd_flag_none,
3907 	  "<id> = get DPP bootstrap URI" },
3908 	{ "dpp_bootstrap_info", wpa_cli_cmd_dpp_bootstrap_info, NULL,
3909 	  cli_cmd_flag_none,
3910 	  "<id> = show DPP bootstrap information" },
3911 	{ "dpp_bootstrap_set", wpa_cli_cmd_dpp_bootstrap_set, NULL,
3912 	  cli_cmd_flag_none,
3913 	  "<id> [conf=..] [ssid=<SSID>] [ssid_charset=#] [psk=<PSK>] [pass=<passphrase>] [configurator=<id>] [conn_status=#] [akm_use_selector=<0|1>] [group_id=..] [expiry=#] [csrattrs=..] = set DPP configurator parameters" },
3914 	{ "dpp_auth_init", wpa_cli_cmd_dpp_auth_init, NULL, cli_cmd_flag_none,
3915 	  "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
3916 	{ "dpp_listen", wpa_cli_cmd_dpp_listen, NULL, cli_cmd_flag_none,
3917 	  "<freq in MHz> = start DPP listen" },
3918 	{ "dpp_stop_listen", wpa_cli_cmd_dpp_stop_listen, NULL,
3919 	  cli_cmd_flag_none,
3920 	  "= stop DPP listen" },
3921 	{ "dpp_configurator_add", wpa_cli_cmd_dpp_configurator_add, NULL,
3922 	  cli_cmd_flag_sensitive,
3923 	  "[curve=..] [key=..] = add DPP configurator" },
3924 	{ "dpp_configurator_remove", wpa_cli_cmd_dpp_configurator_remove, NULL,
3925 	  cli_cmd_flag_none,
3926 	  "*|<id> = remove DPP configurator" },
3927 	{ "dpp_configurator_get_key", wpa_cli_cmd_dpp_configurator_get_key,
3928 	  NULL, cli_cmd_flag_none,
3929 	  "<id> = Get DPP configurator's private key" },
3930 	{ "dpp_configurator_sign", wpa_cli_cmd_dpp_configurator_sign, NULL,
3931 	  cli_cmd_flag_none,
3932 	  "conf=<role> configurator=<id> = generate self DPP configuration" },
3933 	{ "dpp_pkex_add", wpa_cli_cmd_dpp_pkex_add, NULL,
3934 	  cli_cmd_flag_sensitive,
3935 	  "add PKEX code" },
3936 	{ "dpp_pkex_remove", wpa_cli_cmd_dpp_pkex_remove, NULL,
3937 	  cli_cmd_flag_none,
3938 	  "*|<id> = remove DPP pkex information" },
3939 #ifdef CONFIG_DPP2
3940 	{ "dpp_controller_start", wpa_cli_cmd_dpp_controller_start, NULL,
3941 	  cli_cmd_flag_none,
3942 	  "[tcp_port=<port>] [role=..] = start DPP controller" },
3943 	{ "dpp_controller_stop", wpa_cli_cmd_dpp_controller_stop, NULL,
3944 	  cli_cmd_flag_none,
3945 	  "= stop DPP controller" },
3946 	{ "dpp_chirp", wpa_cli_cmd_dpp_chirp, NULL,
3947 	  cli_cmd_flag_none,
3948 	  "own=<BI ID> iter=<count> = start DPP chirp" },
3949 	{ "dpp_stop_chirp", wpa_cli_cmd_dpp_stop_chirp, NULL,
3950 	  cli_cmd_flag_none,
3951 	  "= stop DPP chirp" },
3952 #endif /* CONFIG_DPP2 */
3953 #endif /* CONFIG_DPP */
3954 	{ "all_bss", wpa_cli_cmd_all_bss, NULL, cli_cmd_flag_none,
3955 	  "= list all BSS entries (scan results)" },
3956 #ifdef CONFIG_PASN
3957 	{ "pasn_auth_start", wpa_cli_cmd_pasn_auth_start, NULL,
3958 	  cli_cmd_flag_none,
3959 	  "bssid=<BSSID> akmp=<WPA key mgmt> cipher=<WPA cipher> group=<group> nid=<network id> = Start PASN authentication" },
3960 	{ "pasn_auth_stop", wpa_cli_cmd_pasn_auth_stop, NULL,
3961 	  cli_cmd_flag_none,
3962 	  "= Stop PASN authentication" },
3963 	{ "ptksa_cache_list", wpa_cli_cmd_ptksa_cache_list, NULL,
3964 	  cli_cmd_flag_none,
3965 	  "= Get the PTKSA Cache" },
3966 	{ "pasn_deauth", wpa_cli_cmd_pasn_deauth, NULL,
3967 	  cli_cmd_flag_none,
3968 	  "bssid=<BSSID> = Remove PASN PTKSA state" },
3969 #endif /* CONFIG_PASN */
3970 	{ "mscs", wpa_cli_cmd_mscs, NULL,
3971 	  cli_cmd_flag_none,
3972 	  "<add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>] [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>] = Configure MSCS request" },
3973 	{ "scs", wpa_cli_cmd_scs, NULL,
3974 	  cli_cmd_flag_none,
3975 	  "[scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>] [classifier_type=<4|10>] [classifier params based on classifier type] [tclas_processing=<0|1>] [scs_id=<decimal number>] ... = Send SCS request" },
3976 	{ "dscp_resp", wpa_cli_cmd_dscp_resp, NULL,
3977 	  cli_cmd_flag_none,
3978 	  "<[reset]>/<[solicited] [policy_id=1 status=0...]> [more] = Send DSCP response" },
3979 	{ "dscp_query", wpa_cli_cmd_dscp_query, NULL,
3980 	  cli_cmd_flag_none,
3981 	  "wildcard/domain_name=<string> = Send DSCP Query" },
3982 	{ NULL, NULL, NULL, cli_cmd_flag_none, NULL }
3983 };
3984 
3985 #endif /* !__ZEPHYR__ */
3986 
3987 /*
3988  * Prints command usage, lines are padded with the specified string.
3989  */
print_cmd_help(const struct wpa_cli_cmd * cmd,const char * pad)3990 static void print_cmd_help(const struct wpa_cli_cmd *cmd, const char *pad)
3991 {
3992 	char c;
3993 	size_t n;
3994 
3995 	printf("%s%s ", pad, cmd->cmd);
3996 	for (n = 0; (c = cmd->usage[n]); n++) {
3997 		printf("%c", c);
3998 		if (c == '\n')
3999 			printf("%s", pad);
4000 	}
4001 	printf("\n");
4002 }
4003 
4004 
print_help(const char * cmd)4005 static void print_help(const char *cmd)
4006 {
4007 	int n;
4008 	printf("commands:\n");
4009 	for (n = 0; wpa_cli_commands[n].cmd; n++) {
4010 		if (cmd == NULL || str_starts(wpa_cli_commands[n].cmd, cmd))
4011 			print_cmd_help(&wpa_cli_commands[n], "  ");
4012 	}
4013 }
4014 
4015 
wpa_cli_edit_filter_history_cb(void * ctx,const char * cmd)4016 static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd)
4017 {
4018 	const char *c, *delim;
4019 	int n;
4020 	size_t len;
4021 
4022 	delim = os_strchr(cmd, ' ');
4023 	if (delim)
4024 		len = delim - cmd;
4025 	else
4026 		len = os_strlen(cmd);
4027 
4028 	for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
4029 		if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
4030 			return (wpa_cli_commands[n].flags &
4031 				cli_cmd_flag_sensitive);
4032 	}
4033 	return 0;
4034 }
4035 
4036 
wpa_list_cmd_list(void)4037 static char ** wpa_list_cmd_list(void)
4038 {
4039 	char **res;
4040 	int i, count;
4041 	struct cli_txt_entry *e;
4042 
4043 	count = ARRAY_SIZE(wpa_cli_commands);
4044 	count += dl_list_len(&p2p_groups);
4045 	count += dl_list_len(&ifnames);
4046 	res = os_calloc(count + 1, sizeof(char *));
4047 	if (res == NULL)
4048 		return NULL;
4049 
4050 	for (i = 0; wpa_cli_commands[i].cmd; i++) {
4051 		res[i] = os_strdup(wpa_cli_commands[i].cmd);
4052 		if (res[i] == NULL)
4053 			break;
4054 	}
4055 
4056 	dl_list_for_each(e, &p2p_groups, struct cli_txt_entry, list) {
4057 		size_t len = 8 + os_strlen(e->txt);
4058 		res[i] = os_malloc(len);
4059 		if (res[i] == NULL)
4060 			break;
4061 		os_snprintf(res[i], len, "ifname=%s", e->txt);
4062 		i++;
4063 	}
4064 
4065 	dl_list_for_each(e, &ifnames, struct cli_txt_entry, list) {
4066 		res[i] = os_strdup(e->txt);
4067 		if (res[i] == NULL)
4068 			break;
4069 		i++;
4070 	}
4071 
4072 	return res;
4073 }
4074 
4075 
wpa_cli_cmd_completion(const char * cmd,const char * str,int pos)4076 static char ** wpa_cli_cmd_completion(const char *cmd, const char *str,
4077 				      int pos)
4078 {
4079 	int i;
4080 
4081 	for (i = 0; wpa_cli_commands[i].cmd; i++) {
4082 		if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) {
4083 			if (wpa_cli_commands[i].completion)
4084 				return wpa_cli_commands[i].completion(str,
4085 								      pos);
4086 			edit_clear_line();
4087 			printf("\r%s\n", wpa_cli_commands[i].usage);
4088 			edit_redraw();
4089 			break;
4090 		}
4091 	}
4092 
4093 	return NULL;
4094 }
4095 
4096 
wpa_cli_edit_completion_cb(void * ctx,const char * str,int pos)4097 static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
4098 {
4099 	char **res;
4100 	const char *end;
4101 	char *cmd;
4102 
4103 	if (pos > 7 && os_strncasecmp(str, "IFNAME=", 7) == 0) {
4104 		end = os_strchr(str, ' ');
4105 		if (end && pos > end - str) {
4106 			pos -= end - str + 1;
4107 			str = end + 1;
4108 		}
4109 	}
4110 
4111 	end = os_strchr(str, ' ');
4112 	if (end == NULL || str + pos < end)
4113 		return wpa_list_cmd_list();
4114 
4115 	cmd = os_malloc(pos + 1);
4116 	if (cmd == NULL)
4117 		return NULL;
4118 	os_memcpy(cmd, str, pos);
4119 	cmd[end - str] = '\0';
4120 	res = wpa_cli_cmd_completion(cmd, str, pos);
4121 	os_free(cmd);
4122 	return res;
4123 }
4124 
4125 
wpa_request(struct wpa_ctrl * ctrl,int argc,char * argv[])4126 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
4127 {
4128 	const struct wpa_cli_cmd *cmd, *match = NULL;
4129 	int count;
4130 	int ret = 0;
4131 
4132 	if (argc > 1 && os_strncasecmp(argv[0], "IFNAME=", 7) == 0) {
4133 		ifname_prefix = argv[0] + 7;
4134 		argv = &argv[1];
4135 		argc--;
4136 	} else
4137 		ifname_prefix = NULL;
4138 
4139 	if (argc == 0)
4140 		return -1;
4141 
4142 	count = 0;
4143 	cmd = wpa_cli_commands;
4144 	while (cmd->cmd) {
4145 		if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
4146 		{
4147 			match = cmd;
4148 			if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
4149 				/* we have an exact match */
4150 				count = 1;
4151 				break;
4152 			}
4153 			count++;
4154 		}
4155 		cmd++;
4156 	}
4157 
4158 	if (count > 1) {
4159 		printf("Ambiguous command '%s'; possible commands:", argv[0]);
4160 		cmd = wpa_cli_commands;
4161 		while (cmd->cmd) {
4162 			if (os_strncasecmp(cmd->cmd, argv[0],
4163 					   os_strlen(argv[0])) == 0) {
4164 				printf(" %s", cmd->cmd);
4165 			}
4166 			cmd++;
4167 		}
4168 		printf("\n");
4169 		ret = 1;
4170 	} else if (count == 0) {
4171 		printf("Unknown command '%s'\n", argv[0]);
4172 		ret = 1;
4173 	} else {
4174 		ret = match->handler(ctrl, argc - 1, &argv[1]);
4175 	}
4176 
4177 	return ret;
4178 }
4179 
4180 
wpa_cli_exec(const char * program,const char * arg1,const char * arg2)4181 static int wpa_cli_exec(const char *program, const char *arg1,
4182 			const char *arg2)
4183 {
4184 	char *arg;
4185 	size_t len;
4186 	int res;
4187 
4188 	/* If no interface is specified, set the global */
4189 	if (!arg1)
4190 		arg1 = "global";
4191 
4192 	len = os_strlen(arg1) + os_strlen(arg2) + 2;
4193 	arg = os_malloc(len);
4194 	if (arg == NULL)
4195 		return -1;
4196 	os_snprintf(arg, len, "%s %s", arg1, arg2);
4197 	res = os_exec(program, arg, 1);
4198 	os_free(arg);
4199 
4200 	return res;
4201 }
4202 
4203 
wpa_cli_action_process(const char * msg)4204 static void wpa_cli_action_process(const char *msg)
4205 {
4206 	const char *pos;
4207 	char *copy = NULL, *id, *pos2;
4208 	const char *ifname = ctrl_ifname;
4209 	char ifname_buf[100];
4210 
4211 	if (eloop_terminated())
4212 		return;
4213 
4214 	pos = msg;
4215 	if (os_strncmp(pos, "IFNAME=", 7) == 0) {
4216 		const char *end;
4217 		end = os_strchr(pos + 7, ' ');
4218 		if (end && (unsigned int) (end - pos) < sizeof(ifname_buf)) {
4219 			pos += 7;
4220 			os_memcpy(ifname_buf, pos, end - pos);
4221 			ifname_buf[end - pos] = '\0';
4222 			ifname = ifname_buf;
4223 			pos = end + 1;
4224 		}
4225 	}
4226 	if (*pos == '<') {
4227 		const char *prev = pos;
4228 		/* skip priority */
4229 		pos = os_strchr(pos, '>');
4230 		if (pos)
4231 			pos++;
4232 		else
4233 			pos = prev;
4234 	}
4235 
4236 	if (str_starts(pos, WPA_EVENT_CONNECTED)) {
4237 		int new_id = -1;
4238 		os_unsetenv("WPA_ID");
4239 		os_unsetenv("WPA_ID_STR");
4240 		os_unsetenv("WPA_CTRL_DIR");
4241 
4242 		pos = os_strstr(pos, "[id=");
4243 		if (pos)
4244 			copy = os_strdup(pos + 4);
4245 
4246 		if (copy) {
4247 			pos2 = id = copy;
4248 			while (*pos2 && *pos2 != ' ')
4249 				pos2++;
4250 			*pos2++ = '\0';
4251 			new_id = atoi(id);
4252 			os_setenv("WPA_ID", id, 1);
4253 			while (*pos2 && *pos2 != '=')
4254 				pos2++;
4255 			if (*pos2 == '=')
4256 				pos2++;
4257 			id = pos2;
4258 			while (*pos2 && *pos2 != ']')
4259 				pos2++;
4260 			*pos2 = '\0';
4261 			os_setenv("WPA_ID_STR", id, 1);
4262 			os_free(copy);
4263 		}
4264 
4265 		os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
4266 
4267 		if (wpa_cli_connected <= 0 || new_id != wpa_cli_last_id) {
4268 			wpa_cli_connected = 1;
4269 			wpa_cli_last_id = new_id;
4270 			wpa_cli_exec(action_file, ifname, "CONNECTED");
4271 		}
4272 	} else if (str_starts(pos, WPA_EVENT_DISCONNECTED)) {
4273 		if (wpa_cli_connected) {
4274 			wpa_cli_connected = 0;
4275 			wpa_cli_exec(action_file, ifname, "DISCONNECTED");
4276 		}
4277 	} else if (str_starts(pos, WPA_EVENT_CHANNEL_SWITCH_STARTED)) {
4278 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4279 	} else if (str_starts(pos, AP_EVENT_ENABLED)) {
4280 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4281 	} else if (str_starts(pos, AP_EVENT_DISABLED)) {
4282 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4283 	} else if (str_starts(pos, MESH_GROUP_STARTED)) {
4284 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4285 	} else if (str_starts(pos, MESH_GROUP_REMOVED)) {
4286 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4287 	} else if (str_starts(pos, MESH_PEER_CONNECTED)) {
4288 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4289 	} else if (str_starts(pos, MESH_PEER_DISCONNECTED)) {
4290 		wpa_cli_exec(action_file, ctrl_ifname, pos);
4291 	} else if (str_starts(pos, P2P_EVENT_GROUP_STARTED)) {
4292 		wpa_cli_exec(action_file, ifname, pos);
4293 	} else if (str_starts(pos, P2P_EVENT_GROUP_REMOVED)) {
4294 		wpa_cli_exec(action_file, ifname, pos);
4295 	} else if (str_starts(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) {
4296 		wpa_cli_exec(action_file, ifname, pos);
4297 	} else if (str_starts(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) {
4298 		wpa_cli_exec(action_file, ifname, pos);
4299 	} else if (str_starts(pos, P2P_EVENT_GO_NEG_FAILURE)) {
4300 		wpa_cli_exec(action_file, ifname, pos);
4301 	} else if (str_starts(pos, WPS_EVENT_SUCCESS)) {
4302 		wpa_cli_exec(action_file, ifname, pos);
4303 	} else if (str_starts(pos, WPS_EVENT_ACTIVE)) {
4304 		wpa_cli_exec(action_file, ifname, pos);
4305 	} else if (str_starts(pos, WPS_EVENT_OVERLAP)) {
4306 		wpa_cli_exec(action_file, ifname, pos);
4307 	} else if (str_starts(pos, WPS_EVENT_PIN_ACTIVE)) {
4308 		wpa_cli_exec(action_file, ifname, pos);
4309 	} else if (str_starts(pos, WPS_EVENT_CANCEL)) {
4310 		wpa_cli_exec(action_file, ifname, pos);
4311 	} else if (str_starts(pos, WPS_EVENT_TIMEOUT)) {
4312 		wpa_cli_exec(action_file, ifname, pos);
4313 	} else if (str_starts(pos, WPS_EVENT_FAIL)) {
4314 		wpa_cli_exec(action_file, ifname, pos);
4315 	} else if (str_starts(pos, AP_STA_CONNECTED)) {
4316 		wpa_cli_exec(action_file, ifname, pos);
4317 	} else if (str_starts(pos, AP_STA_DISCONNECTED)) {
4318 		wpa_cli_exec(action_file, ifname, pos);
4319 	} else if (str_starts(pos, ESS_DISASSOC_IMMINENT)) {
4320 		wpa_cli_exec(action_file, ifname, pos);
4321 	} else if (str_starts(pos, HS20_SUBSCRIPTION_REMEDIATION)) {
4322 		wpa_cli_exec(action_file, ifname, pos);
4323 	} else if (str_starts(pos, HS20_DEAUTH_IMMINENT_NOTICE)) {
4324 		wpa_cli_exec(action_file, ifname, pos);
4325 	} else if (str_starts(pos, HS20_T_C_ACCEPTANCE)) {
4326 		wpa_cli_exec(action_file, ifname, pos);
4327 	} else if (str_starts(pos, DPP_EVENT_CONF_RECEIVED)) {
4328 		wpa_cli_exec(action_file, ifname, pos);
4329 	} else if (str_starts(pos, DPP_EVENT_CONFOBJ_AKM)) {
4330 		wpa_cli_exec(action_file, ifname, pos);
4331 	} else if (str_starts(pos, DPP_EVENT_CONFOBJ_SSID)) {
4332 		wpa_cli_exec(action_file, ifname, pos);
4333 	} else if (str_starts(pos, DPP_EVENT_CONNECTOR)) {
4334 		wpa_cli_exec(action_file, ifname, pos);
4335 	} else if (str_starts(pos, DPP_EVENT_CONFOBJ_PASS)) {
4336 		wpa_cli_exec(action_file, ifname, pos);
4337 	} else if (str_starts(pos, DPP_EVENT_CONFOBJ_PSK)) {
4338 		wpa_cli_exec(action_file, ifname, pos);
4339 	} else if (str_starts(pos, DPP_EVENT_C_SIGN_KEY)) {
4340 		wpa_cli_exec(action_file, ifname, pos);
4341 	} else if (str_starts(pos, DPP_EVENT_NET_ACCESS_KEY)) {
4342 		wpa_cli_exec(action_file, ifname, pos);
4343 	} else if (str_starts(pos, WPA_EVENT_TERMINATING)) {
4344 		printf("wpa_supplicant is terminating - stop monitoring\n");
4345 		if (!reconnect)
4346 			wpa_cli_quit = 1;
4347 	}
4348 }
4349 
4350 
4351 #ifndef CONFIG_ANSI_C_EXTRA
wpa_cli_action_cb(char * msg,size_t len)4352 static void wpa_cli_action_cb(char *msg, size_t len)
4353 {
4354 	wpa_cli_action_process(msg);
4355 }
4356 #endif /* CONFIG_ANSI_C_EXTRA */
4357 
4358 
wpa_cli_open_global_ctrl(void)4359 static int wpa_cli_open_global_ctrl(void)
4360 {
4361 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
4362 	ctrl_conn = wpa_ctrl_open(NULL);
4363 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
4364 	ctrl_conn = wpa_ctrl_open(global);
4365 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
4366 	if (!ctrl_conn) {
4367 		fprintf(stderr,
4368 			"Failed to connect to wpa_supplicant global interface: %s  error: %s\n",
4369 			global, strerror(errno));
4370 		return -1;
4371 	}
4372 
4373 	if (interactive) {
4374 		update_ifnames(ctrl_conn);
4375 		mon_conn = wpa_ctrl_open(global);
4376 		if (mon_conn) {
4377 			if (wpa_ctrl_attach(mon_conn) == 0) {
4378 				wpa_cli_attached = 1;
4379 				eloop_register_read_sock(
4380 					wpa_ctrl_get_fd(mon_conn),
4381 					wpa_cli_mon_receive,
4382 					NULL, NULL);
4383 			} else {
4384 				printf("Failed to open monitor connection through global control interface\n");
4385 			}
4386 		}
4387 		update_stations(ctrl_conn);
4388 	}
4389 
4390 	return 0;
4391 }
4392 
4393 
wpa_cli_reconnect(void)4394 static void wpa_cli_reconnect(void)
4395 {
4396 	wpa_cli_close_connection();
4397 	if ((global && wpa_cli_open_global_ctrl() < 0) ||
4398 	    (!global && wpa_cli_open_connection(ctrl_ifname, 1) < 0))
4399 		return;
4400 
4401 	if (interactive) {
4402 		edit_clear_line();
4403 		printf("\rConnection to wpa_supplicant re-established\n");
4404 		edit_redraw();
4405 		update_stations(ctrl_conn);
4406 	}
4407 }
4408 
4409 
cli_event(const char * str)4410 static void cli_event(const char *str)
4411 {
4412 	const char *start, *s;
4413 
4414 	start = os_strchr(str, '>');
4415 	if (start == NULL)
4416 		return;
4417 
4418 	start++;
4419 
4420 	if (str_starts(start, WPA_EVENT_BSS_ADDED)) {
4421 		s = os_strchr(start, ' ');
4422 		if (s == NULL)
4423 			return;
4424 		s = os_strchr(s + 1, ' ');
4425 		if (s == NULL)
4426 			return;
4427 		cli_txt_list_add(&bsses, s + 1);
4428 		return;
4429 	}
4430 
4431 	if (str_starts(start, WPA_EVENT_BSS_REMOVED)) {
4432 		s = os_strchr(start, ' ');
4433 		if (s == NULL)
4434 			return;
4435 		s = os_strchr(s + 1, ' ');
4436 		if (s == NULL)
4437 			return;
4438 		cli_txt_list_del_addr(&bsses, s + 1);
4439 		return;
4440 	}
4441 
4442 #ifdef CONFIG_P2P
4443 	if (str_starts(start, P2P_EVENT_DEVICE_FOUND)) {
4444 		s = os_strstr(start, " p2p_dev_addr=");
4445 		if (s == NULL)
4446 			return;
4447 		cli_txt_list_add_addr(&p2p_peers, s + 14);
4448 		return;
4449 	}
4450 
4451 	if (str_starts(start, P2P_EVENT_DEVICE_LOST)) {
4452 		s = os_strstr(start, " p2p_dev_addr=");
4453 		if (s == NULL)
4454 			return;
4455 		cli_txt_list_del_addr(&p2p_peers, s + 14);
4456 		return;
4457 	}
4458 
4459 	if (str_starts(start, P2P_EVENT_GROUP_STARTED)) {
4460 		s = os_strchr(start, ' ');
4461 		if (s == NULL)
4462 			return;
4463 		cli_txt_list_add_word(&p2p_groups, s + 1, ' ');
4464 		return;
4465 	}
4466 
4467 	if (str_starts(start, P2P_EVENT_GROUP_REMOVED)) {
4468 		s = os_strchr(start, ' ');
4469 		if (s == NULL)
4470 			return;
4471 		cli_txt_list_del_word(&p2p_groups, s + 1, ' ');
4472 		return;
4473 	}
4474 #endif /* CONFIG_P2P */
4475 }
4476 
4477 
check_terminating(const char * msg)4478 static int check_terminating(const char *msg)
4479 {
4480 	const char *pos = msg;
4481 
4482 	if (*pos == '<') {
4483 		/* skip priority */
4484 		pos = os_strchr(pos, '>');
4485 		if (pos)
4486 			pos++;
4487 		else
4488 			pos = msg;
4489 	}
4490 
4491 	if (str_starts(pos, WPA_EVENT_TERMINATING) && ctrl_conn) {
4492 		edit_clear_line();
4493 		printf("\rConnection to wpa_supplicant lost - trying to "
4494 		       "reconnect\n");
4495 		edit_redraw();
4496 		wpa_cli_attached = 0;
4497 		wpa_cli_close_connection();
4498 		return 1;
4499 	}
4500 
4501 	return 0;
4502 }
4503 
4504 
wpa_cli_recv_pending(struct wpa_ctrl * ctrl,int action_monitor)4505 static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
4506 {
4507 	if (ctrl_conn == NULL) {
4508 		wpa_cli_reconnect();
4509 		return;
4510 	}
4511 	while (wpa_ctrl_pending(ctrl) > 0) {
4512 		char buf[4096];
4513 		size_t len = sizeof(buf) - 1;
4514 		if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
4515 			buf[len] = '\0';
4516 			if (action_monitor)
4517 				wpa_cli_action_process(buf);
4518 			else {
4519 				cli_event(buf);
4520 				if (wpa_cli_show_event(buf)) {
4521 					edit_clear_line();
4522 					printf("\r%s\n", buf);
4523 					edit_redraw();
4524 				}
4525 
4526 				if (interactive && check_terminating(buf) > 0)
4527 					return;
4528 			}
4529 		} else {
4530 			printf("Could not read pending message.\n");
4531 			break;
4532 		}
4533 	}
4534 
4535 	if (wpa_ctrl_pending(ctrl) < 0) {
4536 		printf("Connection to wpa_supplicant lost - trying to "
4537 		       "reconnect\n");
4538 		if (reconnect) {
4539 			eloop_terminate();
4540 			return;
4541 		}
4542 		wpa_cli_reconnect();
4543 	}
4544 }
4545 
4546 
wpa_cli_ping(void * eloop_ctx,void * timeout_ctx)4547 static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
4548 {
4549 	if (ctrl_conn) {
4550 		int res;
4551 		char *prefix = ifname_prefix;
4552 
4553 		ifname_prefix = NULL;
4554 		res = _wpa_ctrl_command(ctrl_conn, "PING", 0);
4555 		ifname_prefix = prefix;
4556 		if (res) {
4557 			printf("Connection to wpa_supplicant lost - trying to "
4558 			       "reconnect\n");
4559 			wpa_cli_close_connection();
4560 		}
4561 	}
4562 	if (!ctrl_conn)
4563 		wpa_cli_reconnect();
4564 	eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
4565 }
4566 
4567 
wpa_cli_mon_receive(int sock,void * eloop_ctx,void * sock_ctx)4568 static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx)
4569 {
4570 	wpa_cli_recv_pending(mon_conn, 0);
4571 }
4572 
4573 
wpa_cli_edit_cmd_cb(void * ctx,char * cmd)4574 static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd)
4575 {
4576 	char *argv[max_args];
4577 	int argc;
4578 	argc = tokenize_cmd(cmd, argv);
4579 	if (argc)
4580 		wpa_request(ctrl_conn, argc, argv);
4581 }
4582 
4583 
wpa_cli_edit_eof_cb(void * ctx)4584 static void wpa_cli_edit_eof_cb(void *ctx)
4585 {
4586 	eloop_terminate();
4587 }
4588 
4589 
4590 static int warning_displayed = 0;
4591 static char *hfile = NULL;
4592 static int edit_started = 0;
4593 
start_edit(void)4594 static void start_edit(void)
4595 {
4596 	char *home;
4597 	char *ps = NULL;
4598 
4599 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4600 	ps = wpa_ctrl_get_remote_ifname(ctrl_conn);
4601 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4602 
4603 #ifdef CONFIG_WPA_CLI_HISTORY_DIR
4604 	home = CONFIG_WPA_CLI_HISTORY_DIR;
4605 #else /* CONFIG_WPA_CLI_HISTORY_DIR */
4606 	home = getenv("HOME");
4607 #endif /* CONFIG_WPA_CLI_HISTORY_DIR */
4608 	if (home) {
4609 		const char *fname = ".wpa_cli_history";
4610 		int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
4611 		hfile = os_malloc(hfile_len);
4612 		if (hfile)
4613 			os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
4614 	}
4615 
4616 	if (edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
4617 		      wpa_cli_edit_completion_cb, NULL, hfile, ps) < 0) {
4618 		eloop_terminate();
4619 		return;
4620 	}
4621 
4622 	edit_started = 1;
4623 	eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
4624 }
4625 
4626 
update_bssid_list(struct wpa_ctrl * ctrl)4627 static void update_bssid_list(struct wpa_ctrl *ctrl)
4628 {
4629 	char buf[4096];
4630 	size_t len = sizeof(buf);
4631 	int ret;
4632 	const char *cmd = "BSS RANGE=ALL MASK=0x2";
4633 	char *pos, *end;
4634 
4635 	if (ctrl == NULL)
4636 		return;
4637 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
4638 	if (ret < 0)
4639 		return;
4640 	buf[len] = '\0';
4641 
4642 	pos = buf;
4643 	while (pos) {
4644 		pos = os_strstr(pos, "bssid=");
4645 		if (pos == NULL)
4646 			break;
4647 		pos += 6;
4648 		end = os_strchr(pos, '\n');
4649 		if (end == NULL)
4650 			break;
4651 		*end = '\0';
4652 		cli_txt_list_add(&bsses, pos);
4653 		pos = end + 1;
4654 	}
4655 }
4656 
4657 
update_ifnames(struct wpa_ctrl * ctrl)4658 static void update_ifnames(struct wpa_ctrl *ctrl)
4659 {
4660 	char buf[4096];
4661 	size_t len = sizeof(buf);
4662 	int ret;
4663 	const char *cmd = "INTERFACES";
4664 	char *pos, *end;
4665 	char txt[200];
4666 
4667 	cli_txt_list_flush(&ifnames);
4668 
4669 	if (ctrl == NULL)
4670 		return;
4671 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
4672 	if (ret < 0)
4673 		return;
4674 	buf[len] = '\0';
4675 
4676 	pos = buf;
4677 	while (pos) {
4678 		end = os_strchr(pos, '\n');
4679 		if (end == NULL)
4680 			break;
4681 		*end = '\0';
4682 		ret = os_snprintf(txt, sizeof(txt), "ifname=%s", pos);
4683 		if (!os_snprintf_error(sizeof(txt), ret))
4684 			cli_txt_list_add(&ifnames, txt);
4685 		pos = end + 1;
4686 	}
4687 }
4688 
4689 
update_creds(struct wpa_ctrl * ctrl)4690 static void update_creds(struct wpa_ctrl *ctrl)
4691 {
4692 	char buf[4096];
4693 	size_t len = sizeof(buf);
4694 	int ret;
4695 	const char *cmd = "LIST_CREDS";
4696 	char *pos, *end;
4697 	int header = 1;
4698 
4699 	cli_txt_list_flush(&creds);
4700 
4701 	if (ctrl == NULL)
4702 		return;
4703 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
4704 	if (ret < 0)
4705 		return;
4706 	buf[len] = '\0';
4707 
4708 	pos = buf;
4709 	while (pos) {
4710 		end = os_strchr(pos, '\n');
4711 		if (end == NULL)
4712 			break;
4713 		*end = '\0';
4714 		if (!header)
4715 			cli_txt_list_add_word(&creds, pos, '\t');
4716 		header = 0;
4717 		pos = end + 1;
4718 	}
4719 }
4720 
4721 
update_networks(struct wpa_ctrl * ctrl)4722 static void update_networks(struct wpa_ctrl *ctrl)
4723 {
4724 	char buf[4096];
4725 	size_t len = sizeof(buf);
4726 	int ret;
4727 	const char *cmd = "LIST_NETWORKS";
4728 	char *pos, *end;
4729 	int header = 1;
4730 
4731 	cli_txt_list_flush(&networks);
4732 
4733 	if (ctrl == NULL)
4734 		return;
4735 	ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
4736 	if (ret < 0)
4737 		return;
4738 	buf[len] = '\0';
4739 
4740 	pos = buf;
4741 	while (pos) {
4742 		end = os_strchr(pos, '\n');
4743 		if (end == NULL)
4744 			break;
4745 		*end = '\0';
4746 		if (!header)
4747 			cli_txt_list_add_word(&networks, pos, '\t');
4748 		header = 0;
4749 		pos = end + 1;
4750 	}
4751 }
4752 
4753 
update_stations(struct wpa_ctrl * ctrl)4754 static void update_stations(struct wpa_ctrl *ctrl)
4755 {
4756 #ifdef CONFIG_AP
4757 	char addr[32], cmd[64];
4758 
4759 	if (!ctrl || !interactive)
4760 		return;
4761 
4762 	cli_txt_list_flush(&stations);
4763 
4764 	if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
4765 		return;
4766 	do {
4767 		if (os_strcmp(addr, "") != 0)
4768 			cli_txt_list_add(&stations, addr);
4769 		os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
4770 	} while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
4771 #endif /* CONFIG_AP */
4772 }
4773 
4774 
try_connection(void * eloop_ctx,void * timeout_ctx)4775 static void try_connection(void *eloop_ctx, void *timeout_ctx)
4776 {
4777 	if (ctrl_conn)
4778 		goto done;
4779 
4780 	if (ctrl_ifname == NULL)
4781 		ctrl_ifname = wpa_cli_get_default_ifname();
4782 
4783 	if (wpa_cli_open_connection(ctrl_ifname, 1)) {
4784 		if (!warning_displayed) {
4785 			printf("Could not connect to wpa_supplicant: "
4786 			       "%s - re-trying\n",
4787 			       ctrl_ifname ? ctrl_ifname : "(nil)");
4788 			warning_displayed = 1;
4789 		}
4790 		eloop_register_timeout(1, 0, try_connection, NULL, NULL);
4791 		return;
4792 	}
4793 
4794 	update_bssid_list(ctrl_conn);
4795 	update_creds(ctrl_conn);
4796 	update_networks(ctrl_conn);
4797 	update_stations(ctrl_conn);
4798 
4799 	if (warning_displayed)
4800 		printf("Connection established.\n");
4801 
4802 done:
4803 	start_edit();
4804 }
4805 
4806 
wpa_cli_interactive(void)4807 static void wpa_cli_interactive(void)
4808 {
4809 	printf("\nInteractive mode\n\n");
4810 
4811 	eloop_register_timeout(0, 0, try_connection, NULL, NULL);
4812 	eloop_run();
4813 	eloop_cancel_timeout(try_connection, NULL, NULL);
4814 
4815 	cli_txt_list_flush(&p2p_peers);
4816 	cli_txt_list_flush(&p2p_groups);
4817 	cli_txt_list_flush(&bsses);
4818 	cli_txt_list_flush(&ifnames);
4819 	cli_txt_list_flush(&creds);
4820 	cli_txt_list_flush(&networks);
4821 	if (edit_started)
4822 		edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
4823 	os_free(hfile);
4824 	eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
4825 	wpa_cli_close_connection();
4826 }
4827 
4828 
wpa_cli_action_ping(void * eloop_ctx,void * timeout_ctx)4829 static void wpa_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
4830 {
4831 	struct wpa_ctrl *ctrl = eloop_ctx;
4832 	char buf[256];
4833 	size_t len;
4834 
4835 	/* verify that connection is still working */
4836 	len = sizeof(buf) - 1;
4837 	if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
4838 			     wpa_cli_action_cb) < 0 ||
4839 	    len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
4840 		printf("wpa_supplicant did not reply to PING command - exiting\n");
4841 		eloop_terminate();
4842 		return;
4843 	}
4844 	eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
4845 			       ctrl, NULL);
4846 }
4847 
4848 
wpa_cli_action_receive(int sock,void * eloop_ctx,void * sock_ctx)4849 static void wpa_cli_action_receive(int sock, void *eloop_ctx, void *sock_ctx)
4850 {
4851 	struct wpa_ctrl *ctrl = eloop_ctx;
4852 
4853 	wpa_cli_recv_pending(ctrl, 1);
4854 }
4855 
4856 
wpa_cli_action(struct wpa_ctrl * ctrl)4857 static void wpa_cli_action(struct wpa_ctrl *ctrl)
4858 {
4859 #ifdef CONFIG_ANSI_C_EXTRA
4860 	/* TODO: ANSI C version(?) */
4861 	printf("Action processing not supported in ANSI C build.\n");
4862 #else /* CONFIG_ANSI_C_EXTRA */
4863 	int fd;
4864 
4865 	fd = wpa_ctrl_get_fd(ctrl);
4866 	eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
4867 			       ctrl, NULL);
4868 	eloop_register_read_sock(fd, wpa_cli_action_receive, ctrl, NULL);
4869 	eloop_run();
4870 	eloop_cancel_timeout(wpa_cli_action_ping, ctrl, NULL);
4871 	eloop_unregister_read_sock(fd);
4872 #endif /* CONFIG_ANSI_C_EXTRA */
4873 }
4874 
4875 
wpa_cli_cleanup(void)4876 static void wpa_cli_cleanup(void)
4877 {
4878 	wpa_cli_close_connection();
4879 	if (pid_file)
4880 		os_daemonize_terminate(pid_file);
4881 
4882 	os_program_deinit();
4883 }
4884 
4885 
wpa_cli_terminate(int sig,void * ctx)4886 static void wpa_cli_terminate(int sig, void *ctx)
4887 {
4888 	eloop_terminate();
4889 	if (reconnect)
4890 		wpa_cli_quit = 1;
4891 }
4892 
4893 
wpa_cli_get_default_ifname(void)4894 static char * wpa_cli_get_default_ifname(void)
4895 {
4896 	char *ifname = NULL;
4897 
4898 #ifdef ANDROID
4899 	char ifprop[PROPERTY_VALUE_MAX];
4900 	if (property_get("wifi.interface", ifprop, NULL) != 0) {
4901 		ifname = os_strdup(ifprop);
4902 		printf("Using interface '%s'\n", ifname ? ifname : "N/A");
4903 	}
4904 #else /* ANDROID */
4905 #ifdef CONFIG_CTRL_IFACE_UNIX
4906 	struct dirent *dent;
4907 	DIR *dir = opendir(ctrl_iface_dir);
4908 	if (!dir) {
4909 		return NULL;
4910 	}
4911 	while ((dent = readdir(dir))) {
4912 #ifdef _DIRENT_HAVE_D_TYPE
4913 		/*
4914 		 * Skip the file if it is not a socket. Also accept
4915 		 * DT_UNKNOWN (0) in case the C library or underlying
4916 		 * file system does not support d_type.
4917 		 */
4918 		if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
4919 			continue;
4920 #endif /* _DIRENT_HAVE_D_TYPE */
4921 		/* Skip current/previous directory and special P2P Device
4922 		 * interfaces. */
4923 		if (os_strcmp(dent->d_name, ".") == 0 ||
4924 		    os_strcmp(dent->d_name, "..") == 0 ||
4925 		    os_strncmp(dent->d_name, "p2p-dev-", 8) == 0)
4926 			continue;
4927 		printf("Selected interface '%s'\n", dent->d_name);
4928 		ifname = os_strdup(dent->d_name);
4929 		break;
4930 	}
4931 	closedir(dir);
4932 #endif /* CONFIG_CTRL_IFACE_UNIX */
4933 
4934 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
4935 	char buf[4096], *pos;
4936 	size_t len;
4937 	struct wpa_ctrl *ctrl;
4938 	int ret;
4939 
4940 	ctrl = wpa_ctrl_open(NULL);
4941 	if (ctrl == NULL)
4942 		return NULL;
4943 
4944 	len = sizeof(buf) - 1;
4945 	ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
4946 	if (ret >= 0) {
4947 		buf[len] = '\0';
4948 		pos = os_strchr(buf, '\n');
4949 		if (pos)
4950 			*pos = '\0';
4951 		ifname = os_strdup(buf);
4952 	}
4953 	wpa_ctrl_close(ctrl);
4954 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
4955 #endif /* ANDROID */
4956 
4957 	return ifname;
4958 }
4959 
4960 
main(int argc,char * argv[])4961 int main(int argc, char *argv[])
4962 {
4963 	int c;
4964 	int daemonize = 0;
4965 	int ret = 0;
4966 
4967 	if (os_program_init())
4968 		return -1;
4969 
4970 	for (;;) {
4971 		c = getopt(argc, argv, "a:Bg:G:hi:p:P:rs:v");
4972 		if (c < 0)
4973 			break;
4974 		switch (c) {
4975 		case 'a':
4976 			action_file = optarg;
4977 			break;
4978 		case 'B':
4979 			daemonize = 1;
4980 			break;
4981 		case 'g':
4982 			global = optarg;
4983 			break;
4984 		case 'G':
4985 			ping_interval = atoi(optarg);
4986 			break;
4987 		case 'h':
4988 			usage();
4989 			return 0;
4990 		case 'v':
4991 			printf("%s\n", wpa_cli_version);
4992 			return 0;
4993 		case 'i':
4994 			os_free(ctrl_ifname);
4995 			ctrl_ifname = os_strdup(optarg);
4996 			break;
4997 		case 'p':
4998 			ctrl_iface_dir = optarg;
4999 			break;
5000 		case 'P':
5001 			pid_file = optarg;
5002 			break;
5003 		case 'r':
5004 			reconnect = 1;
5005 			break;
5006 		case 's':
5007 			client_socket_dir = optarg;
5008 			break;
5009 		default:
5010 			usage();
5011 			return -1;
5012 		}
5013 	}
5014 
5015 	interactive = (argc == optind) && (action_file == NULL);
5016 
5017 	if (interactive)
5018 		printf("%s\n\n%s\n\n", wpa_cli_version, cli_license);
5019 
5020 	if (eloop_init())
5021 		return -1;
5022 
5023 	if (global && wpa_cli_open_global_ctrl() < 0)
5024 		return -1;
5025 
5026 	eloop_register_signal_terminate(wpa_cli_terminate, NULL);
5027 
5028 	if (ctrl_ifname == NULL)
5029 		ctrl_ifname = wpa_cli_get_default_ifname();
5030 
5031 	if (reconnect && action_file && ctrl_ifname) {
5032 		while (!wpa_cli_quit) {
5033 			if (ctrl_conn)
5034 				wpa_cli_action(ctrl_conn);
5035 			else
5036 				os_sleep(1, 0);
5037 			wpa_cli_close_connection();
5038 			wpa_cli_open_connection(ctrl_ifname, 0);
5039 			if (ctrl_conn) {
5040 				if (wpa_ctrl_attach(ctrl_conn) != 0)
5041 					wpa_cli_close_connection();
5042 				else
5043 					wpa_cli_attached = 1;
5044 			}
5045 		}
5046 	} else if (interactive) {
5047 		wpa_cli_interactive();
5048 	} else {
5049 		if (!global &&
5050 		    wpa_cli_open_connection(ctrl_ifname, 0) < 0) {
5051 			fprintf(stderr, "Failed to connect to non-global "
5052 				"ctrl_ifname: %s  error: %s\n",
5053 				ctrl_ifname ? ctrl_ifname : "(nil)",
5054 				strerror(errno));
5055 			return -1;
5056 		}
5057 
5058 		if (action_file) {
5059 			if (wpa_ctrl_attach(ctrl_conn) == 0) {
5060 				wpa_cli_attached = 1;
5061 			} else {
5062 				printf("Warning: Failed to attach to "
5063 				       "wpa_supplicant.\n");
5064 				return -1;
5065 			}
5066 		}
5067 
5068 		if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
5069 			return -1;
5070 
5071 		if (action_file)
5072 			wpa_cli_action(ctrl_conn);
5073 		else
5074 			ret = wpa_request(ctrl_conn, argc - optind,
5075 					  &argv[optind]);
5076 	}
5077 
5078 	os_free(ctrl_ifname);
5079 	eloop_destroy();
5080 	wpa_cli_cleanup();
5081 
5082 	return ret;
5083 }
5084 
5085 #else /* CONFIG_CTRL_IFACE */
main(int argc,char * argv[])5086 int main(int argc, char *argv[])
5087 {
5088 	printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
5089 	return -1;
5090 }
5091 #endif /* CONFIG_CTRL_IFACE */
5092