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