blob: de1af3bb28be054c4bef686df59a0f461ff0c4bb [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd - command line interface for hostapd daemon
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2004-2012, 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 Shmidt1f69aa52012-01-24 16:10:04 -080013#include "utils/common.h"
14#include "utils/eloop.h"
15#include "utils/edit.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "common/version.h"
17
18
19static const char *hostapd_cli_version =
20"hostapd_cli v" VERSION_STR "\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080021"Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022
23
24static const char *hostapd_cli_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080025"This software may be distributed under the terms of the BSD license.\n"
26"See README for more details.\n";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027
28static const char *hostapd_cli_full_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080029"This software may be distributed under the terms of the BSD license.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030"\n"
31"Redistribution and use in source and binary forms, with or without\n"
32"modification, are permitted provided that the following conditions are\n"
33"met:\n"
34"\n"
35"1. Redistributions of source code must retain the above copyright\n"
36" notice, this list of conditions and the following disclaimer.\n"
37"\n"
38"2. Redistributions in binary form must reproduce the above copyright\n"
39" notice, this list of conditions and the following disclaimer in the\n"
40" documentation and/or other materials provided with the distribution.\n"
41"\n"
42"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
43" names of its contributors may be used to endorse or promote products\n"
44" derived from this software without specific prior written permission.\n"
45"\n"
46"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
47"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
48"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
49"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
50"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
51"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
52"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
53"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
54"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
55"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
56"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
57"\n";
58
59static const char *commands_help =
60"Commands:\n"
61" mib get MIB variables (dot1x, dot11, radius)\n"
62" sta <addr> get MIB variables for one station\n"
63" all_sta get MIB variables for all stations\n"
64" new_sta <addr> add a new station\n"
65" deauthenticate <addr> deauthenticate a station\n"
66" disassociate <addr> disassociate a station\n"
67#ifdef CONFIG_IEEE80211W
68" sa_query <addr> send SA Query to a station\n"
69#endif /* CONFIG_IEEE80211W */
70#ifdef CONFIG_WPS
71" wps_pin <uuid> <pin> [timeout] [addr] add WPS Enrollee PIN\n"
72" wps_check_pin <PIN> verify PIN checksum\n"
73" wps_pbc indicate button pushed to initiate PBC\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070074" wps_cancel cancel the pending WPS operation\n"
Dmitry Shmidt04949592012-07-19 12:16:46 -070075#ifdef CONFIG_WPS_NFC
76" wps_nfc_tag_read <hexdump> report read NFC tag with WPS data\n"
77" wps_nfc_config_token <WPS/NDEF> build NFC configuration token\n"
78" wps_nfc_token <WPS/NDEF/enable/disable> manager NFC password token\n"
79#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080" wps_ap_pin <cmd> [params..] enable/disable AP PIN\n"
81" wps_config <SSID> <auth> <encr> <key> configure AP\n"
82#endif /* CONFIG_WPS */
83" get_config show current configuration\n"
84" help show this usage help\n"
85" interface [ifname] show interfaces/select interface\n"
86" level <debug level> change debug level\n"
87" license show full hostapd_cli license\n"
88" quit exit hostapd_cli\n";
89
90static struct wpa_ctrl *ctrl_conn;
91static int hostapd_cli_quit = 0;
92static int hostapd_cli_attached = 0;
Jeff Johnson205f2142012-09-03 22:12:17 -070093
94#ifndef CONFIG_CTRL_IFACE_DIR
95#define CONFIG_CTRL_IFACE_DIR "/var/run/hostapd"
96#endif /* CONFIG_CTRL_IFACE_DIR */
97static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
98
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099static char *ctrl_ifname = NULL;
100static const char *pid_file = NULL;
101static const char *action_file = NULL;
102static int ping_interval = 5;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800103static int interactive = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104
105
106static void usage(void)
107{
108 fprintf(stderr, "%s\n", hostapd_cli_version);
109 fprintf(stderr,
110 "\n"
111 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvB] "
112 "[-a<path>] \\\n"
113 " [-G<ping interval>] [command..]\n"
114 "\n"
115 "Options:\n"
116 " -h help (show this usage text)\n"
117 " -v shown version information\n"
118 " -p<path> path to find control sockets (default: "
119 "/var/run/hostapd)\n"
120 " -a<file> run in daemon mode executing the action file "
121 "based on events\n"
122 " from hostapd\n"
123 " -B run a daemon in the background\n"
124 " -i<ifname> Interface to listen on (default: first "
125 "interface found in the\n"
126 " socket path)\n\n"
127 "%s",
128 commands_help);
129}
130
131
132static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
133{
134 char *cfile;
135 int flen;
136
137 if (ifname == NULL)
138 return NULL;
139
140 flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
141 cfile = malloc(flen);
142 if (cfile == NULL)
143 return NULL;
144 snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
145
146 ctrl_conn = wpa_ctrl_open(cfile);
147 free(cfile);
148 return ctrl_conn;
149}
150
151
152static void hostapd_cli_close_connection(void)
153{
154 if (ctrl_conn == NULL)
155 return;
156
157 if (hostapd_cli_attached) {
158 wpa_ctrl_detach(ctrl_conn);
159 hostapd_cli_attached = 0;
160 }
161 wpa_ctrl_close(ctrl_conn);
162 ctrl_conn = NULL;
163}
164
165
166static void hostapd_cli_msg_cb(char *msg, size_t len)
167{
168 printf("%s\n", msg);
169}
170
171
172static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
173{
174 char buf[4096];
175 size_t len;
176 int ret;
177
178 if (ctrl_conn == NULL) {
179 printf("Not connected to hostapd - command dropped.\n");
180 return -1;
181 }
182 len = sizeof(buf) - 1;
183 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
184 hostapd_cli_msg_cb);
185 if (ret == -2) {
186 printf("'%s' command timed out.\n", cmd);
187 return -2;
188 } else if (ret < 0) {
189 printf("'%s' command failed.\n", cmd);
190 return -1;
191 }
192 if (print) {
193 buf[len] = '\0';
194 printf("%s", buf);
195 }
196 return 0;
197}
198
199
200static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
201{
202 return _wpa_ctrl_command(ctrl, cmd, 1);
203}
204
205
206static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
207{
208 return wpa_ctrl_command(ctrl, "PING");
209}
210
211
212static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
213{
214 return wpa_ctrl_command(ctrl, "RELOG");
215}
216
217
218static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
219{
220 return wpa_ctrl_command(ctrl, "MIB");
221}
222
223
224static int hostapd_cli_exec(const char *program, const char *arg1,
225 const char *arg2)
226{
227 char *cmd;
228 size_t len;
229 int res;
230 int ret = 0;
231
232 len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
233 cmd = os_malloc(len);
234 if (cmd == NULL)
235 return -1;
236 res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
237 if (res < 0 || (size_t) res >= len) {
238 os_free(cmd);
239 return -1;
240 }
241 cmd[len - 1] = '\0';
242#ifndef _WIN32_WCE
243 if (system(cmd) < 0)
244 ret = -1;
245#endif /* _WIN32_WCE */
246 os_free(cmd);
247
248 return ret;
249}
250
251
252static void hostapd_cli_action_process(char *msg, size_t len)
253{
254 const char *pos;
255
256 pos = msg;
257 if (*pos == '<') {
258 pos = os_strchr(pos, '>');
259 if (pos)
260 pos++;
261 else
262 pos = msg;
263 }
264
265 hostapd_cli_exec(action_file, ctrl_ifname, pos);
266}
267
268
269static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
270{
271 char buf[64];
272 if (argc != 1) {
273 printf("Invalid 'sta' command - exactly one argument, STA "
274 "address, is required.\n");
275 return -1;
276 }
277 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
278 return wpa_ctrl_command(ctrl, buf);
279}
280
281
282static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
283 char *argv[])
284{
285 char buf[64];
286 if (argc != 1) {
287 printf("Invalid 'new_sta' command - exactly one argument, STA "
288 "address, is required.\n");
289 return -1;
290 }
291 snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
292 return wpa_ctrl_command(ctrl, buf);
293}
294
295
296static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
297 char *argv[])
298{
299 char buf[64];
300 if (argc < 1) {
301 printf("Invalid 'deauthenticate' command - exactly one "
302 "argument, STA address, is required.\n");
303 return -1;
304 }
305 if (argc > 1)
306 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
307 argv[0], argv[1]);
308 else
309 os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
310 return wpa_ctrl_command(ctrl, buf);
311}
312
313
314static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
315 char *argv[])
316{
317 char buf[64];
318 if (argc < 1) {
319 printf("Invalid 'disassociate' command - exactly one "
320 "argument, STA address, is required.\n");
321 return -1;
322 }
323 if (argc > 1)
324 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
325 argv[0], argv[1]);
326 else
327 os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
328 return wpa_ctrl_command(ctrl, buf);
329}
330
331
332#ifdef CONFIG_IEEE80211W
333static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
334 char *argv[])
335{
336 char buf[64];
337 if (argc != 1) {
338 printf("Invalid 'sa_query' command - exactly one argument, "
339 "STA address, is required.\n");
340 return -1;
341 }
342 snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
343 return wpa_ctrl_command(ctrl, buf);
344}
345#endif /* CONFIG_IEEE80211W */
346
347
348#ifdef CONFIG_WPS
349static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
350 char *argv[])
351{
352 char buf[256];
353 if (argc < 2) {
354 printf("Invalid 'wps_pin' command - at least two arguments, "
355 "UUID and PIN, are required.\n");
356 return -1;
357 }
358 if (argc > 3)
359 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
360 argv[0], argv[1], argv[2], argv[3]);
361 else if (argc > 2)
362 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
363 argv[0], argv[1], argv[2]);
364 else
365 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
366 return wpa_ctrl_command(ctrl, buf);
367}
368
369
370static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
371 char *argv[])
372{
373 char cmd[256];
374 int res;
375
376 if (argc != 1 && argc != 2) {
377 printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
378 "- PIN to be verified\n");
379 return -1;
380 }
381
382 if (argc == 2)
383 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
384 argv[0], argv[1]);
385 else
386 res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
387 argv[0]);
388 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
389 printf("Too long WPS_CHECK_PIN command.\n");
390 return -1;
391 }
392 return wpa_ctrl_command(ctrl, cmd);
393}
394
395
396static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
397 char *argv[])
398{
399 return wpa_ctrl_command(ctrl, "WPS_PBC");
400}
401
402
Dmitry Shmidt04949592012-07-19 12:16:46 -0700403static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
404 char *argv[])
405{
406 return wpa_ctrl_command(ctrl, "WPS_CANCEL");
407}
408
409
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410#ifdef CONFIG_WPS_NFC
411static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
412 char *argv[])
413{
414 int ret;
415 char *buf;
416 size_t buflen;
417
418 if (argc != 1) {
419 printf("Invalid 'wps_nfc_tag_read' command - one argument "
420 "is required.\n");
421 return -1;
422 }
423
424 buflen = 18 + os_strlen(argv[0]);
425 buf = os_malloc(buflen);
426 if (buf == NULL)
427 return -1;
428 os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
429
430 ret = wpa_ctrl_command(ctrl, buf);
431 os_free(buf);
432
433 return ret;
434}
435
436
437static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
438 int argc, char *argv[])
439{
440 char cmd[64];
441 int res;
442
443 if (argc != 1) {
444 printf("Invalid 'wps_nfc_config_token' command - one argument "
445 "is required.\n");
446 return -1;
447 }
448
449 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
450 argv[0]);
451 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
452 printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
453 return -1;
454 }
455 return wpa_ctrl_command(ctrl, cmd);
456}
457
458
459static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
460 int argc, char *argv[])
461{
462 char cmd[64];
463 int res;
464
465 if (argc != 1) {
466 printf("Invalid 'wps_nfc_token' command - one argument is "
467 "required.\n");
468 return -1;
469 }
470
471 res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
472 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
473 printf("Too long WPS_NFC_TOKEN command.\n");
474 return -1;
475 }
476 return wpa_ctrl_command(ctrl, cmd);
477}
478#endif /* CONFIG_WPS_NFC */
479
480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
482 char *argv[])
483{
484 char buf[64];
485 if (argc < 1) {
486 printf("Invalid 'wps_ap_pin' command - at least one argument "
487 "is required.\n");
488 return -1;
489 }
490 if (argc > 2)
491 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
492 argv[0], argv[1], argv[2]);
493 else if (argc > 1)
494 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
495 argv[0], argv[1]);
496 else
497 snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
498 return wpa_ctrl_command(ctrl, buf);
499}
500
501
502static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
503 char *argv[])
504{
505 char buf[256];
506 char ssid_hex[2 * 32 + 1];
507 char key_hex[2 * 64 + 1];
508 int i;
509
510 if (argc < 1) {
511 printf("Invalid 'wps_config' command - at least two arguments "
512 "are required.\n");
513 return -1;
514 }
515
516 ssid_hex[0] = '\0';
517 for (i = 0; i < 32; i++) {
518 if (argv[0][i] == '\0')
519 break;
520 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
521 }
522
523 key_hex[0] = '\0';
524 if (argc > 3) {
525 for (i = 0; i < 64; i++) {
526 if (argv[3][i] == '\0')
527 break;
528 os_snprintf(&key_hex[i * 2], 3, "%02x",
529 argv[3][i]);
530 }
531 }
532
533 if (argc > 3)
534 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
535 ssid_hex, argv[1], argv[2], key_hex);
536 else if (argc > 2)
537 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
538 ssid_hex, argv[1], argv[2]);
539 else
540 snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
541 ssid_hex, argv[1]);
542 return wpa_ctrl_command(ctrl, buf);
543}
544#endif /* CONFIG_WPS */
545
546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
548 char *argv[])
549{
550 char buf[300];
551 int res;
552
553 if (argc < 2) {
554 printf("Invalid 'ess_disassoc' command - two arguments (STA "
555 "addr and URL) are needed\n");
556 return -1;
557 }
558
559 res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s",
560 argv[0], argv[1]);
561 if (res < 0 || res >= (int) sizeof(buf))
562 return -1;
563 return wpa_ctrl_command(ctrl, buf);
564}
565
566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
568 char *argv[])
569{
570 return wpa_ctrl_command(ctrl, "GET_CONFIG");
571}
572
573
574static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
575 char *addr, size_t addr_len)
576{
577 char buf[4096], *pos;
578 size_t len;
579 int ret;
580
581 if (ctrl_conn == NULL) {
582 printf("Not connected to hostapd - command dropped.\n");
583 return -1;
584 }
585 len = sizeof(buf) - 1;
586 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
587 hostapd_cli_msg_cb);
588 if (ret == -2) {
589 printf("'%s' command timed out.\n", cmd);
590 return -2;
591 } else if (ret < 0) {
592 printf("'%s' command failed.\n", cmd);
593 return -1;
594 }
595
596 buf[len] = '\0';
597 if (memcmp(buf, "FAIL", 4) == 0)
598 return -1;
599 printf("%s", buf);
600
601 pos = buf;
602 while (*pos != '\0' && *pos != '\n')
603 pos++;
604 *pos = '\0';
605 os_strlcpy(addr, buf, addr_len);
606 return 0;
607}
608
609
610static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
611 char *argv[])
612{
613 char addr[32], cmd[64];
614
615 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
616 return 0;
617 do {
618 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
619 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
620
621 return -1;
622}
623
624
625static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
626{
627 printf("%s", commands_help);
628 return 0;
629}
630
631
632static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
633 char *argv[])
634{
635 printf("%s\n\n%s\n", hostapd_cli_version, hostapd_cli_full_license);
636 return 0;
637}
638
639
640static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
641{
642 hostapd_cli_quit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800643 if (interactive)
644 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 return 0;
646}
647
648
649static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
650{
651 char cmd[256];
652 if (argc != 1) {
653 printf("Invalid LEVEL command: needs one argument (debug "
654 "level)\n");
655 return 0;
656 }
657 snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
658 return wpa_ctrl_command(ctrl, cmd);
659}
660
661
662static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
663{
664 struct dirent *dent;
665 DIR *dir;
666
667 dir = opendir(ctrl_iface_dir);
668 if (dir == NULL) {
669 printf("Control interface directory '%s' could not be "
670 "openned.\n", ctrl_iface_dir);
671 return;
672 }
673
674 printf("Available interfaces:\n");
675 while ((dent = readdir(dir))) {
676 if (strcmp(dent->d_name, ".") == 0 ||
677 strcmp(dent->d_name, "..") == 0)
678 continue;
679 printf("%s\n", dent->d_name);
680 }
681 closedir(dir);
682}
683
684
685static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
686 char *argv[])
687{
688 if (argc < 1) {
689 hostapd_cli_list_interfaces(ctrl);
690 return 0;
691 }
692
693 hostapd_cli_close_connection();
694 free(ctrl_ifname);
695 ctrl_ifname = strdup(argv[0]);
696
697 if (hostapd_cli_open_connection(ctrl_ifname)) {
698 printf("Connected to interface '%s.\n", ctrl_ifname);
699 if (wpa_ctrl_attach(ctrl_conn) == 0) {
700 hostapd_cli_attached = 1;
701 } else {
702 printf("Warning: Failed to attach to "
703 "hostapd.\n");
704 }
705 } else {
706 printf("Could not connect to interface '%s' - re-trying\n",
707 ctrl_ifname);
708 }
709 return 0;
710}
711
712
713static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
714{
715 char cmd[256];
716 int res;
717
718 if (argc != 2) {
719 printf("Invalid SET command: needs two arguments (variable "
720 "name and value)\n");
721 return -1;
722 }
723
724 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
725 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
726 printf("Too long SET command.\n");
727 return -1;
728 }
729 return wpa_ctrl_command(ctrl, cmd);
730}
731
732
733static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
734{
735 char cmd[256];
736 int res;
737
738 if (argc != 1) {
739 printf("Invalid GET command: needs one argument (variable "
740 "name)\n");
741 return -1;
742 }
743
744 res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
745 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
746 printf("Too long GET command.\n");
747 return -1;
748 }
749 return wpa_ctrl_command(ctrl, cmd);
750}
751
752
753struct hostapd_cli_cmd {
754 const char *cmd;
755 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
756};
757
758static struct hostapd_cli_cmd hostapd_cli_commands[] = {
759 { "ping", hostapd_cli_cmd_ping },
760 { "mib", hostapd_cli_cmd_mib },
761 { "relog", hostapd_cli_cmd_relog },
762 { "sta", hostapd_cli_cmd_sta },
763 { "all_sta", hostapd_cli_cmd_all_sta },
764 { "new_sta", hostapd_cli_cmd_new_sta },
765 { "deauthenticate", hostapd_cli_cmd_deauthenticate },
766 { "disassociate", hostapd_cli_cmd_disassociate },
767#ifdef CONFIG_IEEE80211W
768 { "sa_query", hostapd_cli_cmd_sa_query },
769#endif /* CONFIG_IEEE80211W */
770#ifdef CONFIG_WPS
771 { "wps_pin", hostapd_cli_cmd_wps_pin },
772 { "wps_check_pin", hostapd_cli_cmd_wps_check_pin },
773 { "wps_pbc", hostapd_cli_cmd_wps_pbc },
Dmitry Shmidt04949592012-07-19 12:16:46 -0700774 { "wps_cancel", hostapd_cli_cmd_wps_cancel },
Dmitry Shmidt04949592012-07-19 12:16:46 -0700775#ifdef CONFIG_WPS_NFC
776 { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read },
777 { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token },
778 { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token },
779#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin },
781 { "wps_config", hostapd_cli_cmd_wps_config },
782#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 { "ess_disassoc", hostapd_cli_cmd_ess_disassoc },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 { "get_config", hostapd_cli_cmd_get_config },
785 { "help", hostapd_cli_cmd_help },
786 { "interface", hostapd_cli_cmd_interface },
787 { "level", hostapd_cli_cmd_level },
788 { "license", hostapd_cli_cmd_license },
789 { "quit", hostapd_cli_cmd_quit },
790 { "set", hostapd_cli_cmd_set },
791 { "get", hostapd_cli_cmd_get },
792 { NULL, NULL }
793};
794
795
796static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
797{
798 struct hostapd_cli_cmd *cmd, *match = NULL;
799 int count;
800
801 count = 0;
802 cmd = hostapd_cli_commands;
803 while (cmd->cmd) {
804 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
805 match = cmd;
806 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
807 /* we have an exact match */
808 count = 1;
809 break;
810 }
811 count++;
812 }
813 cmd++;
814 }
815
816 if (count > 1) {
817 printf("Ambiguous command '%s'; possible commands:", argv[0]);
818 cmd = hostapd_cli_commands;
819 while (cmd->cmd) {
820 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
821 0) {
822 printf(" %s", cmd->cmd);
823 }
824 cmd++;
825 }
826 printf("\n");
827 } else if (count == 0) {
828 printf("Unknown command '%s'\n", argv[0]);
829 } else {
830 match->handler(ctrl, argc - 1, &argv[1]);
831 }
832}
833
834
835static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
836 int action_monitor)
837{
838 int first = 1;
839 if (ctrl_conn == NULL)
840 return;
841 while (wpa_ctrl_pending(ctrl)) {
842 char buf[256];
843 size_t len = sizeof(buf) - 1;
844 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
845 buf[len] = '\0';
846 if (action_monitor)
847 hostapd_cli_action_process(buf, len);
848 else {
849 if (in_read && first)
850 printf("\n");
851 first = 0;
852 printf("%s\n", buf);
853 }
854 } else {
855 printf("Could not read pending message.\n");
856 break;
857 }
858 }
859}
860
861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862#define max_args 10
863
864static int tokenize_cmd(char *cmd, char *argv[])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800866 char *pos;
867 int argc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 pos = cmd;
870 for (;;) {
871 while (*pos == ' ')
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800873 if (*pos == '\0')
874 break;
875 argv[argc] = pos;
876 argc++;
877 if (argc == max_args)
878 break;
879 if (*pos == '"') {
880 char *pos2 = os_strrchr(pos, '"');
881 if (pos2)
882 pos = pos2 + 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884 while (*pos != '\0' && *pos != ' ')
885 pos++;
886 if (*pos == ' ')
887 *pos++ = '\0';
888 }
889
890 return argc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891}
892
893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895{
896 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
897 printf("Connection to hostapd lost - trying to reconnect\n");
898 hostapd_cli_close_connection();
899 }
900 if (!ctrl_conn) {
901 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
902 if (ctrl_conn) {
903 printf("Connection to hostapd re-established\n");
904 if (wpa_ctrl_attach(ctrl_conn) == 0) {
905 hostapd_cli_attached = 1;
906 } else {
907 printf("Warning: Failed to attach to "
908 "hostapd.\n");
909 }
910 }
911 }
912 if (ctrl_conn)
913 hostapd_cli_recv_pending(ctrl_conn, 1, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800914 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
915}
916
917
918static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
919{
920 eloop_terminate();
921}
922
923
924static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
925{
926 char *argv[max_args];
927 int argc;
928 argc = tokenize_cmd(cmd, argv);
929 if (argc)
930 wpa_request(ctrl_conn, argc, argv);
931}
932
933
934static void hostapd_cli_edit_eof_cb(void *ctx)
935{
936 eloop_terminate();
937}
938
939
940static void hostapd_cli_interactive(void)
941{
942 printf("\nInteractive mode\n\n");
943
944 eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
945 edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946 NULL, NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800947 eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
948
949 eloop_run();
950
951 edit_deinit(NULL, NULL);
952 eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
953}
954
955
956static void hostapd_cli_cleanup(void)
957{
958 hostapd_cli_close_connection();
959 if (pid_file)
960 os_daemonize_terminate(pid_file);
961
962 os_program_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963}
964
965
966static void hostapd_cli_action(struct wpa_ctrl *ctrl)
967{
968 fd_set rfds;
969 int fd, res;
970 struct timeval tv;
971 char buf[256];
972 size_t len;
973
974 fd = wpa_ctrl_get_fd(ctrl);
975
976 while (!hostapd_cli_quit) {
977 FD_ZERO(&rfds);
978 FD_SET(fd, &rfds);
979 tv.tv_sec = ping_interval;
980 tv.tv_usec = 0;
981 res = select(fd + 1, &rfds, NULL, NULL, &tv);
982 if (res < 0 && errno != EINTR) {
983 perror("select");
984 break;
985 }
986
987 if (FD_ISSET(fd, &rfds))
988 hostapd_cli_recv_pending(ctrl, 0, 1);
989 else {
990 len = sizeof(buf) - 1;
991 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
992 hostapd_cli_action_process) < 0 ||
993 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
994 printf("hostapd did not reply to PING "
995 "command - exiting\n");
996 break;
997 }
998 }
999 }
1000}
1001
1002
1003int main(int argc, char *argv[])
1004{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 int warning_displayed = 0;
1006 int c;
1007 int daemonize = 0;
1008
1009 if (os_program_init())
1010 return -1;
1011
1012 for (;;) {
1013 c = getopt(argc, argv, "a:BhG:i:p:v");
1014 if (c < 0)
1015 break;
1016 switch (c) {
1017 case 'a':
1018 action_file = optarg;
1019 break;
1020 case 'B':
1021 daemonize = 1;
1022 break;
1023 case 'G':
1024 ping_interval = atoi(optarg);
1025 break;
1026 case 'h':
1027 usage();
1028 return 0;
1029 case 'v':
1030 printf("%s\n", hostapd_cli_version);
1031 return 0;
1032 case 'i':
1033 os_free(ctrl_ifname);
1034 ctrl_ifname = os_strdup(optarg);
1035 break;
1036 case 'p':
1037 ctrl_iface_dir = optarg;
1038 break;
1039 default:
1040 usage();
1041 return -1;
1042 }
1043 }
1044
1045 interactive = (argc == optind) && (action_file == NULL);
1046
1047 if (interactive) {
1048 printf("%s\n\n%s\n\n", hostapd_cli_version,
1049 hostapd_cli_license);
1050 }
1051
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 if (eloop_init())
1053 return -1;
1054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 for (;;) {
1056 if (ctrl_ifname == NULL) {
1057 struct dirent *dent;
1058 DIR *dir = opendir(ctrl_iface_dir);
1059 if (dir) {
1060 while ((dent = readdir(dir))) {
1061 if (os_strcmp(dent->d_name, ".") == 0
1062 ||
1063 os_strcmp(dent->d_name, "..") == 0)
1064 continue;
1065 printf("Selected interface '%s'\n",
1066 dent->d_name);
1067 ctrl_ifname = os_strdup(dent->d_name);
1068 break;
1069 }
1070 closedir(dir);
1071 }
1072 }
1073 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
1074 if (ctrl_conn) {
1075 if (warning_displayed)
1076 printf("Connection established.\n");
1077 break;
1078 }
1079
1080 if (!interactive) {
1081 perror("Failed to connect to hostapd - "
1082 "wpa_ctrl_open");
1083 return -1;
1084 }
1085
1086 if (!warning_displayed) {
1087 printf("Could not connect to hostapd - re-trying\n");
1088 warning_displayed = 1;
1089 }
1090 os_sleep(1, 0);
1091 continue;
1092 }
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 if (interactive || action_file) {
1095 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1096 hostapd_cli_attached = 1;
1097 } else {
1098 printf("Warning: Failed to attach to hostapd.\n");
1099 if (action_file)
1100 return -1;
1101 }
1102 }
1103
1104 if (daemonize && os_daemonize(pid_file))
1105 return -1;
1106
1107 if (interactive)
1108 hostapd_cli_interactive();
1109 else if (action_file)
1110 hostapd_cli_action(ctrl_conn);
1111 else
1112 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1113
1114 os_free(ctrl_ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001115 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 hostapd_cli_cleanup();
1117 return 0;
1118}