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