blob: 8b8d7f92f00d8a8f367722d3c644e5dc9fb86f84 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd - command line interface for hostapd daemon
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <dirent.h>
11
12#include "common/wpa_ctrl.h"
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -070013#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014#include "utils/common.h"
15#include "utils/eloop.h"
16#include "utils/edit.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/version.h"
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070018#include "common/cli.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080020#ifndef CONFIG_NO_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070022static const char *const hostapd_cli_version =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023"hostapd_cli v" VERSION_STR "\n"
Dmitry Shmidtde47be72016-01-07 12:52:55 -080024"Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026static struct wpa_ctrl *ctrl_conn;
27static int hostapd_cli_quit = 0;
28static int hostapd_cli_attached = 0;
Jeff Johnson205f2142012-09-03 22:12:17 -070029
30#ifndef CONFIG_CTRL_IFACE_DIR
31#define CONFIG_CTRL_IFACE_DIR "/var/run/hostapd"
32#endif /* CONFIG_CTRL_IFACE_DIR */
33static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080034static const char *client_socket_dir = NULL;
Jeff Johnson205f2142012-09-03 22:12:17 -070035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036static char *ctrl_ifname = NULL;
37static const char *pid_file = NULL;
38static const char *action_file = NULL;
39static int ping_interval = 5;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080040static int interactive = 0;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070041static int event_handler_registered = 0;
42
43static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
44
45static void print_help(FILE *stream, const char *cmd);
46static char ** list_cmd_list(void);
47static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048static void update_stations(struct wpa_ctrl *ctrl);
49static void cli_event(const char *str);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
51
52static 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>] [-hvB] "
58 "[-a<path>] \\\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080059 " [-P<pid file>] [-G<ping interval>] [command..]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 "\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"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080066 " -s<dir_path> dir path to open client sockets (default: "
67 CONFIG_CTRL_IFACE_DIR ")\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 " -a<file> run in daemon mode executing the action file "
69 "based on events\n"
70 " from hostapd\n"
71 " -B run a daemon in the background\n"
72 " -i<ifname> Interface to listen on (default: first "
73 "interface found in the\n"
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070074 " socket path)\n\n");
75 print_help(stderr, NULL);
76}
77
78
79static void register_event_handler(struct wpa_ctrl *ctrl)
80{
81 if (!ctrl_conn)
82 return;
83 if (interactive) {
84 event_handler_registered =
85 !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
86 hostapd_cli_receive,
87 NULL, NULL);
88 }
89}
90
91
92static void unregister_event_handler(struct wpa_ctrl *ctrl)
93{
94 if (!ctrl_conn)
95 return;
96 if (interactive && event_handler_registered) {
97 eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
98 event_handler_registered = 0;
99 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100}
101
102
103static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
104{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800105#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 char *cfile;
107 int flen;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800108#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109
110 if (ifname == NULL)
111 return NULL;
112
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800113#ifdef CONFIG_CTRL_IFACE_UDP
114 ctrl_conn = wpa_ctrl_open(ifname);
115 return ctrl_conn;
116#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
118 cfile = malloc(flen);
119 if (cfile == NULL)
120 return NULL;
121 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
122
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800123 if (client_socket_dir && client_socket_dir[0] &&
124 access(client_socket_dir, F_OK) < 0) {
125 perror(client_socket_dir);
126 free(cfile);
127 return NULL;
128 }
129
130 ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 free(cfile);
132 return ctrl_conn;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800133#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134}
135
136
137static void hostapd_cli_close_connection(void)
138{
139 if (ctrl_conn == NULL)
140 return;
141
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700142 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143 if (hostapd_cli_attached) {
144 wpa_ctrl_detach(ctrl_conn);
145 hostapd_cli_attached = 0;
146 }
147 wpa_ctrl_close(ctrl_conn);
148 ctrl_conn = NULL;
149}
150
151
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800152static int hostapd_cli_reconnect(const char *ifname)
153{
154 char *next_ctrl_ifname;
155
156 hostapd_cli_close_connection();
157
158 if (!ifname)
159 return -1;
160
161 next_ctrl_ifname = os_strdup(ifname);
162 os_free(ctrl_ifname);
163 ctrl_ifname = next_ctrl_ifname;
164 if (!ctrl_ifname)
165 return -1;
166
167 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
168 if (!ctrl_conn)
169 return -1;
170 if (!interactive && !action_file)
171 return 0;
172 if (wpa_ctrl_attach(ctrl_conn) == 0) {
173 hostapd_cli_attached = 1;
174 register_event_handler(ctrl_conn);
175 update_stations(ctrl_conn);
176 } else {
177 printf("Warning: Failed to attach to hostapd.\n");
178 }
179 return 0;
180}
181
182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183static void hostapd_cli_msg_cb(char *msg, size_t len)
184{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800185 cli_event(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 printf("%s\n", msg);
187}
188
189
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800190static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191{
192 char buf[4096];
193 size_t len;
194 int ret;
195
196 if (ctrl_conn == NULL) {
197 printf("Not connected to hostapd - command dropped.\n");
198 return -1;
199 }
200 len = sizeof(buf) - 1;
201 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
202 hostapd_cli_msg_cb);
203 if (ret == -2) {
204 printf("'%s' command timed out.\n", cmd);
205 return -2;
206 } else if (ret < 0) {
207 printf("'%s' command failed.\n", cmd);
208 return -1;
209 }
210 if (print) {
211 buf[len] = '\0';
212 printf("%s", buf);
213 }
214 return 0;
215}
216
217
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800218static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219{
220 return _wpa_ctrl_command(ctrl, cmd, 1);
221}
222
223
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800224static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
225 int min_args, int argc, char *argv[])
226{
227 char buf[4096];
228
229 if (argc < min_args) {
230 printf("Invalid %s command - at least %d argument%s required.\n",
231 cmd, min_args, min_args > 1 ? "s are" : " is");
232 return -1;
233 }
234 if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
235 return -1;
236 return wpa_ctrl_command(ctrl, buf);
237}
238
239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
241{
242 return wpa_ctrl_command(ctrl, "PING");
243}
244
245
246static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
247{
248 return wpa_ctrl_command(ctrl, "RELOG");
249}
250
251
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800252static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
253{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800254 if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
255 return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800256 return wpa_ctrl_command(ctrl, "STATUS");
257}
258
259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
261{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800262 if (argc > 0) {
263 char buf[100];
264 os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
265 return wpa_ctrl_command(ctrl, buf);
266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 return wpa_ctrl_command(ctrl, "MIB");
268}
269
270
271static int hostapd_cli_exec(const char *program, const char *arg1,
272 const char *arg2)
273{
Jouni Malinen772e12c2014-10-07 10:29:35 -0700274 char *arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 size_t len;
276 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277
Jouni Malinen772e12c2014-10-07 10:29:35 -0700278 len = os_strlen(arg1) + os_strlen(arg2) + 2;
279 arg = os_malloc(len);
280 if (arg == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 return -1;
Jouni Malinen772e12c2014-10-07 10:29:35 -0700282 os_snprintf(arg, len, "%s %s", arg1, arg2);
283 res = os_exec(program, arg, 1);
284 os_free(arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285
Jouni Malinen772e12c2014-10-07 10:29:35 -0700286 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287}
288
289
290static void hostapd_cli_action_process(char *msg, size_t len)
291{
292 const char *pos;
293
294 pos = msg;
295 if (*pos == '<') {
296 pos = os_strchr(pos, '>');
297 if (pos)
298 pos++;
299 else
300 pos = msg;
301 }
302
303 hostapd_cli_exec(action_file, ctrl_ifname, pos);
304}
305
306
307static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
308{
309 char buf[64];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800310 if (argc < 1) {
311 printf("Invalid 'sta' command - at least one argument, STA "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 "address, is required.\n");
313 return -1;
314 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 if (argc > 1)
316 snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
317 else
318 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 return wpa_ctrl_command(ctrl, buf);
320}
321
322
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800323static char ** hostapd_complete_sta(const char *str, int pos)
324{
325 int arg = get_cmd_arg_num(str, pos);
326 char **res = NULL;
327
328 switch (arg) {
329 case 1:
330 res = cli_txt_list_array(&stations);
331 break;
332 }
333
334 return res;
335}
336
337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
339 char *argv[])
340{
341 char buf[64];
342 if (argc != 1) {
343 printf("Invalid 'new_sta' command - exactly one argument, STA "
344 "address, is required.\n");
345 return -1;
346 }
347 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
348 return wpa_ctrl_command(ctrl, buf);
349}
350
351
352static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
353 char *argv[])
354{
355 char buf[64];
356 if (argc < 1) {
357 printf("Invalid 'deauthenticate' command - exactly one "
358 "argument, STA address, is required.\n");
359 return -1;
360 }
361 if (argc > 1)
362 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
363 argv[0], argv[1]);
364 else
365 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
366 return wpa_ctrl_command(ctrl, buf);
367}
368
369
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700370static char ** hostapd_complete_deauthenticate(const char *str, int pos)
371{
372 int arg = get_cmd_arg_num(str, pos);
373 char **res = NULL;
374
375 switch (arg) {
376 case 1:
377 res = cli_txt_list_array(&stations);
378 break;
379 }
380
381 return res;
382}
383
384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
386 char *argv[])
387{
388 char buf[64];
389 if (argc < 1) {
390 printf("Invalid 'disassociate' command - exactly one "
391 "argument, STA address, is required.\n");
392 return -1;
393 }
394 if (argc > 1)
395 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
396 argv[0], argv[1]);
397 else
398 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
399 return wpa_ctrl_command(ctrl, buf);
400}
401
402
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700403static char ** hostapd_complete_disassociate(const char *str, int pos)
404{
405 int arg = get_cmd_arg_num(str, pos);
406 char **res = NULL;
407
408 switch (arg) {
409 case 1:
410 res = cli_txt_list_array(&stations);
411 break;
412 }
413
414 return res;
415}
416
417
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700418#ifdef CONFIG_TAXONOMY
419static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
420 char *argv[])
421{
422 char buf[64];
423
424 if (argc != 1) {
425 printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
426 return -1;
427 }
428 os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
429 return wpa_ctrl_command(ctrl, buf);
430}
431#endif /* CONFIG_TAXONOMY */
432
433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434#ifdef CONFIG_IEEE80211W
435static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
436 char *argv[])
437{
438 char buf[64];
439 if (argc != 1) {
440 printf("Invalid 'sa_query' command - exactly one argument, "
441 "STA address, is required.\n");
442 return -1;
443 }
444 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
445 return wpa_ctrl_command(ctrl, buf);
446}
447#endif /* CONFIG_IEEE80211W */
448
449
450#ifdef CONFIG_WPS
451static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
452 char *argv[])
453{
454 char buf[256];
455 if (argc < 2) {
456 printf("Invalid 'wps_pin' command - at least two arguments, "
457 "UUID and PIN, are required.\n");
458 return -1;
459 }
460 if (argc > 3)
461 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
462 argv[0], argv[1], argv[2], argv[3]);
463 else if (argc > 2)
464 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
465 argv[0], argv[1], argv[2]);
466 else
467 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
468 return wpa_ctrl_command(ctrl, buf);
469}
470
471
472static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
473 char *argv[])
474{
475 char cmd[256];
476 int res;
477
478 if (argc != 1 && argc != 2) {
479 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
480 "- PIN to be verified\n");
481 return -1;
482 }
483
484 if (argc == 2)
485 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
486 argv[0], argv[1]);
487 else
488 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
489 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800490 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 printf("Too long WPS_CHECK_PIN command.\n");
492 return -1;
493 }
494 return wpa_ctrl_command(ctrl, cmd);
495}
496
497
498static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
499 char *argv[])
500{
501 return wpa_ctrl_command(ctrl, "WPS_PBC");
502}
503
504
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
506 char *argv[])
507{
508 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
509}
510
511
Dmitry Shmidt04949592012-07-19 12:16:46 -0700512#ifdef CONFIG_WPS_NFC
513static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
514 char *argv[])
515{
516 int ret;
517 char *buf;
518 size_t buflen;
519
520 if (argc != 1) {
521 printf("Invalid 'wps_nfc_tag_read' command - one argument "
522 "is required.\n");
523 return -1;
524 }
525
526 buflen = 18 + os_strlen(argv[0]);
527 buf = os_malloc(buflen);
528 if (buf == NULL)
529 return -1;
530 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
531
532 ret = wpa_ctrl_command(ctrl, buf);
533 os_free(buf);
534
535 return ret;
536}
537
538
539static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
540 int argc, char *argv[])
541{
542 char cmd[64];
543 int res;
544
545 if (argc != 1) {
546 printf("Invalid 'wps_nfc_config_token' command - one argument "
547 "is required.\n");
548 return -1;
549 }
550
551 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
552 argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800553 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700554 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
555 return -1;
556 }
557 return wpa_ctrl_command(ctrl, cmd);
558}
559
560
561static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
562 int argc, char *argv[])
563{
564 char cmd[64];
565 int res;
566
567 if (argc != 1) {
568 printf("Invalid 'wps_nfc_token' command - one argument is "
569 "required.\n");
570 return -1;
571 }
572
573 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800574 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700575 printf("Too long WPS_NFC_TOKEN command.\n");
576 return -1;
577 }
578 return wpa_ctrl_command(ctrl, cmd);
579}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800580
581
582static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
583 int argc, char *argv[])
584{
585 char cmd[64];
586 int res;
587
588 if (argc != 2) {
589 printf("Invalid 'nfc_get_handover_sel' command - two arguments "
590 "are required.\n");
591 return -1;
592 }
593
594 res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
595 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800597 printf("Too long NFC_GET_HANDOVER_SEL command.\n");
598 return -1;
599 }
600 return wpa_ctrl_command(ctrl, cmd);
601}
602
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603#endif /* CONFIG_WPS_NFC */
604
605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
607 char *argv[])
608{
609 char buf[64];
610 if (argc < 1) {
611 printf("Invalid 'wps_ap_pin' command - at least one argument "
612 "is required.\n");
613 return -1;
614 }
615 if (argc > 2)
616 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
617 argv[0], argv[1], argv[2]);
618 else if (argc > 1)
619 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
620 argv[0], argv[1]);
621 else
622 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
623 return wpa_ctrl_command(ctrl, buf);
624}
625
626
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700627static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
628 char *argv[])
629{
630 return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
631}
632
633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
635 char *argv[])
636{
637 char buf[256];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700638 char ssid_hex[2 * SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 char key_hex[2 * 64 + 1];
640 int i;
641
642 if (argc < 1) {
643 printf("Invalid 'wps_config' command - at least two arguments "
644 "are required.\n");
645 return -1;
646 }
647
648 ssid_hex[0] = '\0';
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700649 for (i = 0; i < SSID_MAX_LEN; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 if (argv[0][i] == '\0')
651 break;
652 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
653 }
654
655 key_hex[0] = '\0';
656 if (argc > 3) {
657 for (i = 0; i < 64; i++) {
658 if (argv[3][i] == '\0')
659 break;
660 os_snprintf(&key_hex[i * 2], 3, "%02x",
661 argv[3][i]);
662 }
663 }
664
665 if (argc > 3)
666 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
667 ssid_hex, argv[1], argv[2], key_hex);
668 else if (argc > 2)
669 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
670 ssid_hex, argv[1], argv[2]);
671 else
672 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
673 ssid_hex, argv[1]);
674 return wpa_ctrl_command(ctrl, buf);
675}
676#endif /* CONFIG_WPS */
677
678
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800679static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
680 char *argv[])
681{
682 char buf[300];
683 int res;
684
685 if (argc < 2) {
686 printf("Invalid 'disassoc_imminent' command - two arguments "
687 "(STA addr and Disassociation Timer) are needed\n");
688 return -1;
689 }
690
691 res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
692 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800693 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800694 return -1;
695 return wpa_ctrl_command(ctrl, buf);
696}
697
698
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
700 char *argv[])
701{
702 char buf[300];
703 int res;
704
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700705 if (argc < 3) {
706 printf("Invalid 'ess_disassoc' command - three arguments (STA "
707 "addr, disassoc timer, and URL) are needed\n");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800708 return -1;
709 }
710
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700711 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
712 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800713 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714 return -1;
715 return wpa_ctrl_command(ctrl, buf);
716}
717
718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800719static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
720 char *argv[])
721{
722 char buf[2000], *tmp;
723 int res, i, total;
724
725 if (argc < 1) {
726 printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
727 return -1;
728 }
729
730 res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
731 if (os_snprintf_error(sizeof(buf), res))
732 return -1;
733
734 total = res;
735 for (i = 1; i < argc; i++) {
736 tmp = &buf[total];
737 res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
738 if (os_snprintf_error(sizeof(buf) - total, res))
739 return -1;
740 total += res;
741 }
742 return wpa_ctrl_command(ctrl, buf);
743}
744
745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
747 char *argv[])
748{
749 return wpa_ctrl_command(ctrl, "GET_CONFIG");
750}
751
752
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800753static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
754 char *addr, size_t addr_len, int print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755{
756 char buf[4096], *pos;
757 size_t len;
758 int ret;
759
760 if (ctrl_conn == NULL) {
761 printf("Not connected to hostapd - command dropped.\n");
762 return -1;
763 }
764 len = sizeof(buf) - 1;
765 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
766 hostapd_cli_msg_cb);
767 if (ret == -2) {
768 printf("'%s' command timed out.\n", cmd);
769 return -2;
770 } else if (ret < 0) {
771 printf("'%s' command failed.\n", cmd);
772 return -1;
773 }
774
775 buf[len] = '\0';
776 if (memcmp(buf, "FAIL", 4) == 0)
777 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800778 if (print)
779 printf("%s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
781 pos = buf;
782 while (*pos != '\0' && *pos != '\n')
783 pos++;
784 *pos = '\0';
785 os_strlcpy(addr, buf, addr_len);
786 return 0;
787}
788
789
790static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
791 char *argv[])
792{
793 char addr[32], cmd[64];
794
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800795 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 return 0;
797 do {
798 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800799 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800
801 return -1;
802}
803
804
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800805static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
806 char *argv[])
807{
808 char addr[32], cmd[64];
809
810 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
811 return 0;
812 do {
813 if (os_strcmp(addr, "") != 0)
814 printf("%s\n", addr);
815 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
816 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
817
818 return 0;
819}
820
821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
823{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700824 print_help(stdout, argc > 0 ? argv[0] : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 return 0;
826}
827
828
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700829static char ** hostapd_cli_complete_help(const char *str, int pos)
830{
831 int arg = get_cmd_arg_num(str, pos);
832 char **res = NULL;
833
834 switch (arg) {
835 case 1:
836 res = list_cmd_list();
837 break;
838 }
839
840 return res;
841}
842
843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
845 char *argv[])
846{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700847 printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 return 0;
849}
850
851
Dmitry Shmidt051af732013-10-22 13:52:46 -0700852static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
853 int argc, char *argv[])
854{
855 char buf[200];
856 int res;
857
858 if (argc != 1) {
859 printf("Invalid 'set_qos_map_set' command - "
860 "one argument (comma delimited QoS map set) "
861 "is needed\n");
862 return -1;
863 }
864
865 res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800866 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700867 return -1;
868 return wpa_ctrl_command(ctrl, buf);
869}
870
871
872static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
873 int argc, char *argv[])
874{
875 char buf[50];
876 int res;
877
878 if (argc != 1) {
879 printf("Invalid 'send_qos_map_conf' command - "
880 "one argument (STA addr) is needed\n");
881 return -1;
882 }
883
884 res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700886 return -1;
887 return wpa_ctrl_command(ctrl, buf);
888}
889
890
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800891static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
892 char *argv[])
893{
894 char buf[300];
895 int res;
896
897 if (argc < 2) {
898 printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
899 "addr and URL) are needed\n");
900 return -1;
901 }
902
903 res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
904 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800905 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800906 return -1;
907 return wpa_ctrl_command(ctrl, buf);
908}
909
910
911static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
912 char *argv[])
913{
914 char buf[300];
915 int res;
916
917 if (argc < 3) {
918 printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
919 return -1;
920 }
921
922 if (argc > 3)
923 res = os_snprintf(buf, sizeof(buf),
924 "HS20_DEAUTH_REQ %s %s %s %s",
925 argv[0], argv[1], argv[2], argv[3]);
926 else
927 res = os_snprintf(buf, sizeof(buf),
928 "HS20_DEAUTH_REQ %s %s %s",
929 argv[0], argv[1], argv[2]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800930 if (os_snprintf_error(sizeof(buf), res))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800931 return -1;
932 return wpa_ctrl_command(ctrl, buf);
933}
934
935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
937{
938 hostapd_cli_quit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800939 if (interactive)
940 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return 0;
942}
943
944
945static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
946{
947 char cmd[256];
948 if (argc != 1) {
949 printf("Invalid LEVEL command: needs one argument (debug "
950 "level)\n");
951 return 0;
952 }
953 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
954 return wpa_ctrl_command(ctrl, cmd);
955}
956
957
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800958static void update_stations(struct wpa_ctrl *ctrl)
959{
960 char addr[32], cmd[64];
961
962 if (!ctrl || !interactive)
963 return;
964
965 cli_txt_list_flush(&stations);
966
967 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
968 return;
969 do {
970 if (os_strcmp(addr, "") != 0)
971 cli_txt_list_add(&stations, addr);
972 os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
973 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
974}
975
976
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700977static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
978 struct dl_list *interfaces)
979{
980 struct dirent *dent;
981 DIR *dir;
982
983 if (!ctrl || !interfaces)
984 return;
985 dir = opendir(ctrl_iface_dir);
986 if (dir == NULL)
987 return;
988
989 while ((dent = readdir(dir))) {
990 if (strcmp(dent->d_name, ".") == 0 ||
991 strcmp(dent->d_name, "..") == 0)
992 continue;
993 cli_txt_list_add(interfaces, dent->d_name);
994 }
995 closedir(dir);
996}
997
998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
1000{
1001 struct dirent *dent;
1002 DIR *dir;
1003
1004 dir = opendir(ctrl_iface_dir);
1005 if (dir == NULL) {
1006 printf("Control interface directory '%s' could not be "
1007 "openned.\n", ctrl_iface_dir);
1008 return;
1009 }
1010
1011 printf("Available interfaces:\n");
1012 while ((dent = readdir(dir))) {
1013 if (strcmp(dent->d_name, ".") == 0 ||
1014 strcmp(dent->d_name, "..") == 0)
1015 continue;
1016 printf("%s\n", dent->d_name);
1017 }
1018 closedir(dir);
1019}
1020
1021
1022static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
1023 char *argv[])
1024{
1025 if (argc < 1) {
1026 hostapd_cli_list_interfaces(ctrl);
1027 return 0;
1028 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001029 if (hostapd_cli_reconnect(argv[0]) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 printf("Could not connect to interface '%s' - re-trying\n",
1031 ctrl_ifname);
1032 }
1033 return 0;
1034}
1035
1036
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001037static char ** hostapd_complete_interface(const char *str, int pos)
1038{
1039 int arg = get_cmd_arg_num(str, pos);
1040 char **res = NULL;
1041 DEFINE_DL_LIST(interfaces);
1042
1043 switch (arg) {
1044 case 1:
1045 hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
1046 res = cli_txt_list_array(&interfaces);
1047 cli_txt_list_flush(&interfaces);
1048 break;
1049 }
1050
1051 return res;
1052}
1053
1054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
1056{
1057 char cmd[256];
1058 int res;
1059
1060 if (argc != 2) {
1061 printf("Invalid SET command: needs two arguments (variable "
1062 "name and value)\n");
1063 return -1;
1064 }
1065
1066 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001067 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 printf("Too long SET command.\n");
1069 return -1;
1070 }
1071 return wpa_ctrl_command(ctrl, cmd);
1072}
1073
1074
1075static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
1076{
1077 char cmd[256];
1078 int res;
1079
1080 if (argc != 1) {
1081 printf("Invalid GET command: needs one argument (variable "
1082 "name)\n");
1083 return -1;
1084 }
1085
1086 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001087 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 printf("Too long GET command.\n");
1089 return -1;
1090 }
1091 return wpa_ctrl_command(ctrl, cmd);
1092}
1093
1094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001095#ifdef CONFIG_FST
1096static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
1097{
1098 char cmd[256];
1099 int res;
1100 int i;
1101 int total;
1102
1103 if (argc <= 0) {
1104 printf("FST command: parameters are required.\n");
1105 return -1;
1106 }
1107
1108 total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
1109
1110 for (i = 0; i < argc; i++) {
1111 res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
1112 argv[i]);
1113 if (os_snprintf_error(sizeof(cmd) - total, res)) {
1114 printf("Too long fst command.\n");
1115 return -1;
1116 }
1117 total += res;
1118 }
1119 return wpa_ctrl_command(ctrl, cmd);
1120}
1121#endif /* CONFIG_FST */
1122
1123
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001124static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
1125 int argc, char *argv[])
1126{
1127 char cmd[256];
1128 int res;
1129 int i;
1130 char *tmp;
1131 int total;
1132
1133 if (argc < 2) {
1134 printf("Invalid chan_switch command: needs at least two "
1135 "arguments (count and freq)\n"
1136 "usage: <cs_count> <freq> [sec_channel_offset=] "
1137 "[center_freq1=] [center_freq2=] [bandwidth=] "
1138 "[blocktx] [ht|vht]\n");
1139 return -1;
1140 }
1141
1142 res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
1143 argv[0], argv[1]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001144 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001145 printf("Too long CHAN_SWITCH command.\n");
1146 return -1;
1147 }
1148
1149 total = res;
1150 for (i = 2; i < argc; i++) {
1151 tmp = cmd + total;
1152 res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001153 if (os_snprintf_error(sizeof(cmd) - total, res)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001154 printf("Too long CHAN_SWITCH command.\n");
1155 return -1;
1156 }
1157 total += res;
1158 }
1159 return wpa_ctrl_command(ctrl, cmd);
1160}
1161
1162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
1164 char *argv[])
1165{
1166 return wpa_ctrl_command(ctrl, "ENABLE");
1167}
1168
1169
1170static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
1171 char *argv[])
1172{
1173 return wpa_ctrl_command(ctrl, "RELOAD");
1174}
1175
1176
1177static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
1178 char *argv[])
1179{
1180 return wpa_ctrl_command(ctrl, "DISABLE");
1181}
1182
1183
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001184static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
1185{
1186 char cmd[256];
1187 int res;
1188
1189 if (argc < 2 || argc > 3) {
1190 printf("Invalid vendor command\n"
1191 "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
1192 return -1;
1193 }
1194
1195 res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
1196 argc == 3 ? argv[2] : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001197 if (os_snprintf_error(sizeof(cmd), res)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001198 printf("Too long VENDOR command.\n");
1199 return -1;
1200 }
1201 return wpa_ctrl_command(ctrl, cmd);
1202}
1203
1204
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001205static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
1206 char *argv[])
1207{
1208 return wpa_ctrl_command(ctrl, "ERP_FLUSH");
1209}
1210
1211
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001212static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
1213 char *argv[])
1214{
1215 char cmd[256];
1216 int res;
1217
1218 res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
1219 argc >= 1 ? " " : "",
1220 argc >= 1 ? argv[0] : "",
1221 argc == 2 ? " " : "",
1222 argc == 2 ? argv[1] : "");
1223 if (os_snprintf_error(sizeof(cmd), res)) {
1224 printf("Too long option\n");
1225 return -1;
1226 }
1227 return wpa_ctrl_command(ctrl, cmd);
1228}
1229
1230
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001231static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
1232{
1233 if (argc == 0)
1234 return -1;
1235 return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
1236}
1237
1238
Dmitry Shmidte4663042016-04-04 10:07:49 -07001239static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
1240{
1241 return wpa_ctrl_command(ctrl, "PMKSA");
1242}
1243
1244
1245static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
1246 char *argv[])
1247{
1248 return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
1249}
1250
1251
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001252static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
1253 char *argv[])
1254{
1255 char cmd[2048];
1256 int res;
1257
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001258 if (argc < 3 || argc > 6) {
1259 printf("Invalid set_neighbor command: needs 3-6 arguments\n");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001260 return -1;
1261 }
1262
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001263 res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001264 argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001265 argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001266 if (os_snprintf_error(sizeof(cmd), res)) {
1267 printf("Too long SET_NEIGHBOR command.\n");
1268 return -1;
1269 }
1270 return wpa_ctrl_command(ctrl, cmd);
1271}
1272
1273
1274static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
1275 char *argv[])
1276{
1277 char cmd[400];
1278 int res;
1279
1280 if (argc != 2) {
1281 printf("Invalid remove_neighbor command: needs 2 arguments\n");
1282 return -1;
1283 }
1284
1285 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
1286 argv[0], argv[1]);
1287 if (os_snprintf_error(sizeof(cmd), res)) {
1288 printf("Too long REMOVE_NEIGHBOR command.\n");
1289 return -1;
1290 }
1291 return wpa_ctrl_command(ctrl, cmd);
1292}
1293
1294
1295static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
1296 char *argv[])
1297{
1298 char cmd[256];
1299 int res;
1300
1301 if (argc != 1) {
1302 printf("Invalid req_lci command - requires destination address\n");
1303 return -1;
1304 }
1305
1306 res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
1307 if (os_snprintf_error(sizeof(cmd), res)) {
1308 printf("Too long REQ_LCI command.\n");
1309 return -1;
1310 }
1311 return wpa_ctrl_command(ctrl, cmd);
1312}
1313
1314
1315static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
1316 char *argv[])
1317{
1318 if (argc < 4) {
1319 printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
1320 return -1;
1321 }
1322
1323 return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
1324}
1325
1326
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001327static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
1328 char *argv[])
1329{
1330 return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
1331}
1332
1333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334struct hostapd_cli_cmd {
1335 const char *cmd;
1336 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001337 char ** (*completion)(const char *str, int pos);
1338 const char *usage;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339};
1340
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001341static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001342 { "ping", hostapd_cli_cmd_ping, NULL,
1343 "= pings hostapd" },
1344 { "mib", hostapd_cli_cmd_mib, NULL,
1345 "= get MIB variables (dot1x, dot11, radius)" },
1346 { "relog", hostapd_cli_cmd_relog, NULL, NULL },
1347 { "status", hostapd_cli_cmd_status, NULL, NULL },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001348 { "sta", hostapd_cli_cmd_sta, hostapd_complete_sta,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001349 "<addr> = get MIB variables for one station" },
1350 { "all_sta", hostapd_cli_cmd_all_sta, NULL,
1351 "= get MIB variables for all stations" },
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001352 { "list_sta", hostapd_cli_cmd_list_sta, NULL,
1353 "= list all stations" },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001354 { "new_sta", hostapd_cli_cmd_new_sta, NULL,
1355 "<addr> = add a new station" },
1356 { "deauthenticate", hostapd_cli_cmd_deauthenticate,
1357 hostapd_complete_deauthenticate,
1358 "<addr> = deauthenticate a station" },
1359 { "disassociate", hostapd_cli_cmd_disassociate,
1360 hostapd_complete_disassociate,
1361 "<addr> = disassociate a station" },
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001362#ifdef CONFIG_TAXONOMY
1363 { "signature", hostapd_cli_cmd_signature, NULL,
1364 "<addr> = get taxonomy signature for a station" },
1365#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366#ifdef CONFIG_IEEE80211W
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001367 { "sa_query", hostapd_cli_cmd_sa_query, NULL,
1368 "<addr> = send SA Query to a station" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369#endif /* CONFIG_IEEE80211W */
1370#ifdef CONFIG_WPS
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001371 { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
1372 "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
1373 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
1374 "<PIN> = verify PIN checksum" },
1375 { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
1376 "= indicate button pushed to initiate PBC" },
1377 { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
1378 "= cancel the pending WPS operation" },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001379#ifdef CONFIG_WPS_NFC
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001380 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
1381 "<hexdump> = report read NFC tag with WPS data" },
1382 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
1383 "<WPS/NDEF> = build NFC configuration token" },
1384 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
1385 "<WPS/NDEF/enable/disable> = manager NFC password token" },
1386 { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
1387 NULL },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001389 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
1390 "<cmd> [params..] = enable/disable AP PIN" },
1391 { "wps_config", hostapd_cli_cmd_wps_config, NULL,
1392 "<SSID> <auth> <encr> <key> = configure AP" },
1393 { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
1394 "= show current WPS status" },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395#endif /* CONFIG_WPS */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001396 { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, NULL },
1397 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, NULL },
1398 { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, NULL },
1399 { "get_config", hostapd_cli_cmd_get_config, NULL,
1400 "= show current configuration" },
1401 { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
1402 "= show this usage help" },
1403 { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
1404 "[ifname] = show interfaces/select interface" },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405#ifdef CONFIG_FST
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001406 { "fst", hostapd_cli_cmd_fst, NULL, NULL },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001407#endif /* CONFIG_FST */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001408 { "raw", hostapd_cli_cmd_raw, NULL, NULL },
1409 { "level", hostapd_cli_cmd_level, NULL,
1410 "<debug level> = change debug level" },
1411 { "license", hostapd_cli_cmd_license, NULL,
1412 "= show full hostapd_cli license" },
1413 { "quit", hostapd_cli_cmd_quit, NULL,
1414 "= exit hostapd_cli" },
1415 { "set", hostapd_cli_cmd_set, NULL, NULL },
1416 { "get", hostapd_cli_cmd_get, NULL, NULL },
1417 { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, NULL },
1418 { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, NULL, NULL },
1419 { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, NULL },
1420 { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, NULL },
1421 { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, NULL },
1422 { "vendor", hostapd_cli_cmd_vendor, NULL, NULL },
1423 { "enable", hostapd_cli_cmd_enable, NULL, NULL },
1424 { "reload", hostapd_cli_cmd_reload, NULL, NULL },
1425 { "disable", hostapd_cli_cmd_disable, NULL, NULL },
1426 { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, NULL },
1427 { "log_level", hostapd_cli_cmd_log_level, NULL, NULL },
1428 { "pmksa", hostapd_cli_cmd_pmksa, NULL, NULL },
1429 { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, NULL },
1430 { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, NULL },
1431 { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, NULL },
1432 { "req_lci", hostapd_cli_cmd_req_lci, NULL, NULL },
1433 { "req_range", hostapd_cli_cmd_req_range, NULL, NULL },
1434 { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, NULL },
1435 { NULL, NULL, NULL, NULL }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436};
1437
1438
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001439/*
1440 * Prints command usage, lines are padded with the specified string.
1441 */
1442static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
1443 const char *pad)
1444{
1445 char c;
1446 size_t n;
1447
1448 if (cmd->usage == NULL)
1449 return;
1450 fprintf(stream, "%s%s ", pad, cmd->cmd);
1451 for (n = 0; (c = cmd->usage[n]); n++) {
1452 fprintf(stream, "%c", c);
1453 if (c == '\n')
1454 fprintf(stream, "%s", pad);
1455 }
1456 fprintf(stream, "\n");
1457}
1458
1459
1460static void print_help(FILE *stream, const char *cmd)
1461{
1462 int n;
1463
1464 fprintf(stream, "commands:\n");
1465 for (n = 0; hostapd_cli_commands[n].cmd; n++) {
1466 if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
1467 print_cmd_help(stream, &hostapd_cli_commands[n], " ");
1468 }
1469}
1470
1471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1473{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001474 const struct hostapd_cli_cmd *cmd, *match = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 int count;
1476
1477 count = 0;
1478 cmd = hostapd_cli_commands;
1479 while (cmd->cmd) {
1480 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
1481 match = cmd;
1482 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1483 /* we have an exact match */
1484 count = 1;
1485 break;
1486 }
1487 count++;
1488 }
1489 cmd++;
1490 }
1491
1492 if (count > 1) {
1493 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1494 cmd = hostapd_cli_commands;
1495 while (cmd->cmd) {
1496 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
1497 0) {
1498 printf(" %s", cmd->cmd);
1499 }
1500 cmd++;
1501 }
1502 printf("\n");
1503 } else if (count == 0) {
1504 printf("Unknown command '%s'\n", argv[0]);
1505 } else {
1506 match->handler(ctrl, argc - 1, &argv[1]);
1507 }
1508}
1509
1510
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001511static void cli_event(const char *str)
1512{
1513 const char *start, *s;
1514
1515 start = os_strchr(str, '>');
1516 if (start == NULL)
1517 return;
1518
1519 start++;
1520
1521 if (str_starts(start, AP_STA_CONNECTED)) {
1522 s = os_strchr(start, ' ');
1523 if (s == NULL)
1524 return;
1525 cli_txt_list_add(&stations, s + 1);
1526 return;
1527 }
1528
1529 if (str_starts(start, AP_STA_DISCONNECTED)) {
1530 s = os_strchr(start, ' ');
1531 if (s == NULL)
1532 return;
1533 cli_txt_list_del_addr(&stations, s + 1);
1534 return;
1535 }
1536}
1537
1538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1540 int action_monitor)
1541{
1542 int first = 1;
1543 if (ctrl_conn == NULL)
1544 return;
1545 while (wpa_ctrl_pending(ctrl)) {
1546 char buf[256];
1547 size_t len = sizeof(buf) - 1;
1548 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1549 buf[len] = '\0';
1550 if (action_monitor)
1551 hostapd_cli_action_process(buf, len);
1552 else {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001553 cli_event(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (in_read && first)
1555 printf("\n");
1556 first = 0;
1557 printf("%s\n", buf);
1558 }
1559 } else {
1560 printf("Could not read pending message.\n");
1561 break;
1562 }
1563 }
1564}
1565
1566
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001567static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001569 hostapd_cli_recv_pending(ctrl_conn, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570}
1571
1572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574{
1575 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1576 printf("Connection to hostapd lost - trying to reconnect\n");
1577 hostapd_cli_close_connection();
1578 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001579 if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
1580 printf("Connection to hostapd re-established\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 if (ctrl_conn)
1582 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1584}
1585
1586
1587static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
1588{
1589 eloop_terminate();
1590}
1591
1592
1593static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
1594{
1595 char *argv[max_args];
1596 int argc;
1597 argc = tokenize_cmd(cmd, argv);
1598 if (argc)
1599 wpa_request(ctrl_conn, argc, argv);
1600}
1601
1602
1603static void hostapd_cli_edit_eof_cb(void *ctx)
1604{
1605 eloop_terminate();
1606}
1607
1608
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001609static char ** list_cmd_list(void)
1610{
1611 char **res;
1612 int i, count;
1613
1614 count = ARRAY_SIZE(hostapd_cli_commands);
1615 res = os_calloc(count + 1, sizeof(char *));
1616 if (res == NULL)
1617 return NULL;
1618
1619 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1620 res[i] = os_strdup(hostapd_cli_commands[i].cmd);
1621 if (res[i] == NULL)
1622 break;
1623 }
1624
1625 return res;
1626}
1627
1628
1629static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
1630 int pos)
1631{
1632 int i;
1633
1634 for (i = 0; hostapd_cli_commands[i].cmd; i++) {
1635 if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
1636 continue;
1637 if (hostapd_cli_commands[i].completion)
1638 return hostapd_cli_commands[i].completion(str, pos);
1639 if (!hostapd_cli_commands[i].usage)
1640 return NULL;
1641 edit_clear_line();
1642 printf("\r%s\n", hostapd_cli_commands[i].usage);
1643 edit_redraw();
1644 break;
1645 }
1646
1647 return NULL;
1648}
1649
1650
1651static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
1652 int pos)
1653{
1654 char **res;
1655 const char *end;
1656 char *cmd;
1657
1658 end = os_strchr(str, ' ');
1659 if (end == NULL || str + pos < end)
1660 return list_cmd_list();
1661
1662 cmd = os_malloc(pos + 1);
1663 if (cmd == NULL)
1664 return NULL;
1665 os_memcpy(cmd, str, pos);
1666 cmd[end - str] = '\0';
1667 res = hostapd_cli_cmd_completion(cmd, str, pos);
1668 os_free(cmd);
1669 return res;
1670}
1671
1672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001673static void hostapd_cli_interactive(void)
1674{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001675 char *hfile = NULL;
1676 char *home;
1677
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678 printf("\nInteractive mode\n\n");
1679
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001680#ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
1681 home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
1682#else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1683 home = getenv("HOME");
1684#endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
1685 if (home) {
1686 const char *fname = ".hostapd_cli_history";
1687 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
1688 hfile = os_malloc(hfile_len);
1689 if (hfile)
1690 os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
1691 }
1692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
1694 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001695 hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001696 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
1697
1698 eloop_run();
1699
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001700 cli_txt_list_flush(&stations);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001701 edit_deinit(hfile, NULL);
1702 os_free(hfile);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
1704}
1705
1706
1707static void hostapd_cli_cleanup(void)
1708{
1709 hostapd_cli_close_connection();
1710 if (pid_file)
1711 os_daemonize_terminate(pid_file);
1712
1713 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714}
1715
1716
1717static void hostapd_cli_action(struct wpa_ctrl *ctrl)
1718{
1719 fd_set rfds;
1720 int fd, res;
1721 struct timeval tv;
1722 char buf[256];
1723 size_t len;
1724
1725 fd = wpa_ctrl_get_fd(ctrl);
1726
1727 while (!hostapd_cli_quit) {
1728 FD_ZERO(&rfds);
1729 FD_SET(fd, &rfds);
1730 tv.tv_sec = ping_interval;
1731 tv.tv_usec = 0;
1732 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1733 if (res < 0 && errno != EINTR) {
1734 perror("select");
1735 break;
1736 }
1737
1738 if (FD_ISSET(fd, &rfds))
1739 hostapd_cli_recv_pending(ctrl, 0, 1);
1740 else {
1741 len = sizeof(buf) - 1;
1742 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1743 hostapd_cli_action_process) < 0 ||
1744 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1745 printf("hostapd did not reply to PING "
1746 "command - exiting\n");
1747 break;
1748 }
1749 }
1750 }
1751}
1752
1753
1754int main(int argc, char *argv[])
1755{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 int warning_displayed = 0;
1757 int c;
1758 int daemonize = 0;
1759
1760 if (os_program_init())
1761 return -1;
1762
1763 for (;;) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001764 c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 if (c < 0)
1766 break;
1767 switch (c) {
1768 case 'a':
1769 action_file = optarg;
1770 break;
1771 case 'B':
1772 daemonize = 1;
1773 break;
1774 case 'G':
1775 ping_interval = atoi(optarg);
1776 break;
1777 case 'h':
1778 usage();
1779 return 0;
1780 case 'v':
1781 printf("%s\n", hostapd_cli_version);
1782 return 0;
1783 case 'i':
1784 os_free(ctrl_ifname);
1785 ctrl_ifname = os_strdup(optarg);
1786 break;
1787 case 'p':
1788 ctrl_iface_dir = optarg;
1789 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001790 case 'P':
1791 pid_file = optarg;
1792 break;
1793 case 's':
1794 client_socket_dir = optarg;
1795 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 default:
1797 usage();
1798 return -1;
1799 }
1800 }
1801
1802 interactive = (argc == optind) && (action_file == NULL);
1803
1804 if (interactive) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001805 printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 }
1807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 if (eloop_init())
1809 return -1;
1810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 for (;;) {
1812 if (ctrl_ifname == NULL) {
1813 struct dirent *dent;
1814 DIR *dir = opendir(ctrl_iface_dir);
1815 if (dir) {
1816 while ((dent = readdir(dir))) {
1817 if (os_strcmp(dent->d_name, ".") == 0
1818 ||
1819 os_strcmp(dent->d_name, "..") == 0)
1820 continue;
1821 printf("Selected interface '%s'\n",
1822 dent->d_name);
1823 ctrl_ifname = os_strdup(dent->d_name);
1824 break;
1825 }
1826 closedir(dir);
1827 }
1828 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001829 hostapd_cli_reconnect(ctrl_ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 if (ctrl_conn) {
1831 if (warning_displayed)
1832 printf("Connection established.\n");
1833 break;
1834 }
1835
1836 if (!interactive) {
1837 perror("Failed to connect to hostapd - "
1838 "wpa_ctrl_open");
1839 return -1;
1840 }
1841
1842 if (!warning_displayed) {
1843 printf("Could not connect to hostapd - re-trying\n");
1844 warning_displayed = 1;
1845 }
1846 os_sleep(1, 0);
1847 continue;
1848 }
1849
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001850 if (action_file && !hostapd_cli_attached)
1851 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001852 if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 return -1;
1854
1855 if (interactive)
1856 hostapd_cli_interactive();
1857 else if (action_file)
1858 hostapd_cli_action(ctrl_conn);
1859 else
1860 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1861
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001862 unregister_event_handler(ctrl_conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001864 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 hostapd_cli_cleanup();
1866 return 0;
1867}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001868
1869#else /* CONFIG_NO_CTRL_IFACE */
1870
1871int main(int argc, char *argv[])
1872{
1873 return -1;
1874}
1875
1876#endif /* CONFIG_NO_CTRL_IFACE */