]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant - command line interface for wpa_supplicant daemon | |
dff0f701 | 3 | * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #ifdef CONFIG_CTRL_IFACE | |
18 | ||
19 | #ifdef CONFIG_CTRL_IFACE_UNIX | |
20 | #include <dirent.h> | |
21 | #endif /* CONFIG_CTRL_IFACE_UNIX */ | |
6fc6879b | 22 | |
90973fb2 | 23 | #include "common/wpa_ctrl.h" |
cd101567 JM |
24 | #include "utils/common.h" |
25 | #include "utils/eloop.h" | |
82a855bd | 26 | #include "utils/edit.h" |
90973fb2 | 27 | #include "common/version.h" |
6fc6879b JM |
28 | |
29 | ||
30 | static const char *wpa_cli_version = | |
31 | "wpa_cli v" VERSION_STR "\n" | |
dff0f701 | 32 | "Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi> and contributors"; |
6fc6879b JM |
33 | |
34 | ||
35 | static const char *wpa_cli_license = | |
36 | "This program is free software. You can distribute it and/or modify it\n" | |
37 | "under the terms of the GNU General Public License version 2.\n" | |
38 | "\n" | |
39 | "Alternatively, this software may be distributed under the terms of the\n" | |
40 | "BSD license. See README and COPYING for more details.\n"; | |
41 | ||
42 | static const char *wpa_cli_full_license = | |
43 | "This program is free software; you can redistribute it and/or modify\n" | |
44 | "it under the terms of the GNU General Public License version 2 as\n" | |
45 | "published by the Free Software Foundation.\n" | |
46 | "\n" | |
47 | "This program is distributed in the hope that it will be useful,\n" | |
48 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | |
49 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | |
50 | "GNU General Public License for more details.\n" | |
51 | "\n" | |
52 | "You should have received a copy of the GNU General Public License\n" | |
53 | "along with this program; if not, write to the Free Software\n" | |
54 | "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n" | |
55 | "\n" | |
56 | "Alternatively, this software may be distributed under the terms of the\n" | |
57 | "BSD license.\n" | |
58 | "\n" | |
59 | "Redistribution and use in source and binary forms, with or without\n" | |
60 | "modification, are permitted provided that the following conditions are\n" | |
61 | "met:\n" | |
62 | "\n" | |
63 | "1. Redistributions of source code must retain the above copyright\n" | |
64 | " notice, this list of conditions and the following disclaimer.\n" | |
65 | "\n" | |
66 | "2. Redistributions in binary form must reproduce the above copyright\n" | |
67 | " notice, this list of conditions and the following disclaimer in the\n" | |
68 | " documentation and/or other materials provided with the distribution.\n" | |
69 | "\n" | |
70 | "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n" | |
71 | " names of its contributors may be used to endorse or promote products\n" | |
72 | " derived from this software without specific prior written permission.\n" | |
73 | "\n" | |
74 | "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" | |
75 | "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n" | |
76 | "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" | |
77 | "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n" | |
78 | "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n" | |
79 | "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n" | |
80 | "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" | |
81 | "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n" | |
82 | "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" | |
83 | "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n" | |
84 | "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" | |
85 | "\n"; | |
86 | ||
6fc6879b | 87 | static struct wpa_ctrl *ctrl_conn; |
4a3ade4e | 88 | static struct wpa_ctrl *mon_conn; |
6fc6879b JM |
89 | static int wpa_cli_quit = 0; |
90 | static int wpa_cli_attached = 0; | |
91 | static int wpa_cli_connected = 0; | |
92 | static int wpa_cli_last_id = 0; | |
93 | static const char *ctrl_iface_dir = "/var/run/wpa_supplicant"; | |
94 | static char *ctrl_ifname = NULL; | |
95 | static const char *pid_file = NULL; | |
96 | static const char *action_file = NULL; | |
1cc84c1c | 97 | static int ping_interval = 5; |
4a3ade4e | 98 | static int interactive = 0; |
6fc6879b JM |
99 | |
100 | ||
cd101567 JM |
101 | static void print_help(void); |
102 | static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx); | |
dfa141b1 ER |
103 | |
104 | ||
6fc6879b JM |
105 | static void usage(void) |
106 | { | |
107 | printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] " | |
108 | "[-a<action file>] \\\n" | |
1cc84c1c JM |
109 | " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] " |
110 | "[command..]\n" | |
6fc6879b JM |
111 | " -h = help (show this usage text)\n" |
112 | " -v = shown version information\n" | |
113 | " -a = run in daemon mode executing the action file based on " | |
114 | "events from\n" | |
115 | " wpa_supplicant\n" | |
116 | " -B = run a daemon in the background\n" | |
117 | " default path: /var/run/wpa_supplicant\n" | |
dfa141b1 ER |
118 | " default interface: first interface found in socket path\n"); |
119 | print_help(); | |
6fc6879b JM |
120 | } |
121 | ||
122 | ||
f3f0f648 JM |
123 | static int str_starts(const char *src, const char *match) |
124 | { | |
125 | return os_strncmp(src, match, os_strlen(match)) == 0; | |
126 | } | |
127 | ||
128 | ||
129 | static int wpa_cli_show_event(const char *event) | |
130 | { | |
131 | const char *start; | |
132 | ||
133 | start = os_strchr(event, '>'); | |
134 | if (start == NULL) | |
135 | return 1; | |
136 | ||
137 | start++; | |
138 | /* | |
139 | * Skip BSS added/removed events since they can be relatively frequent | |
140 | * and are likely of not much use for an interactive user. | |
141 | */ | |
142 | if (str_starts(start, WPA_EVENT_BSS_ADDED) || | |
143 | str_starts(start, WPA_EVENT_BSS_REMOVED)) | |
144 | return 0; | |
145 | ||
146 | return 1; | |
147 | } | |
148 | ||
149 | ||
4a3ade4e | 150 | static int wpa_cli_open_connection(const char *ifname, int attach) |
6fc6879b JM |
151 | { |
152 | #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE) | |
153 | ctrl_conn = wpa_ctrl_open(ifname); | |
4a3ade4e JM |
154 | if (ctrl_conn == NULL) |
155 | return -1; | |
156 | ||
157 | if (attach && interactive) | |
158 | mon_conn = wpa_ctrl_open(ifname); | |
159 | else | |
160 | mon_conn = NULL; | |
6fc6879b JM |
161 | #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */ |
162 | char *cfile; | |
163 | int flen, res; | |
164 | ||
165 | if (ifname == NULL) | |
4a3ade4e | 166 | return -1; |
6fc6879b JM |
167 | |
168 | flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2; | |
169 | cfile = os_malloc(flen); | |
170 | if (cfile == NULL) | |
4a3ade4e | 171 | return -1L; |
6fc6879b JM |
172 | res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); |
173 | if (res < 0 || res >= flen) { | |
174 | os_free(cfile); | |
4a3ade4e | 175 | return -1; |
6fc6879b JM |
176 | } |
177 | ||
178 | ctrl_conn = wpa_ctrl_open(cfile); | |
4a3ade4e JM |
179 | if (ctrl_conn == NULL) { |
180 | os_free(cfile); | |
181 | return -1; | |
182 | } | |
183 | ||
184 | if (attach && interactive) | |
185 | mon_conn = wpa_ctrl_open(cfile); | |
186 | else | |
187 | mon_conn = NULL; | |
6fc6879b | 188 | os_free(cfile); |
6fc6879b | 189 | #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */ |
4a3ade4e JM |
190 | |
191 | if (mon_conn) { | |
192 | if (wpa_ctrl_attach(mon_conn) == 0) { | |
193 | wpa_cli_attached = 1; | |
cd101567 JM |
194 | if (interactive) |
195 | eloop_register_read_sock( | |
196 | wpa_ctrl_get_fd(mon_conn), | |
197 | wpa_cli_mon_receive, NULL, NULL); | |
4a3ade4e JM |
198 | } else { |
199 | printf("Warning: Failed to attach to " | |
200 | "wpa_supplicant.\n"); | |
201 | return -1; | |
202 | } | |
203 | } | |
204 | ||
205 | return 0; | |
6fc6879b JM |
206 | } |
207 | ||
208 | ||
209 | static void wpa_cli_close_connection(void) | |
210 | { | |
211 | if (ctrl_conn == NULL) | |
212 | return; | |
213 | ||
214 | if (wpa_cli_attached) { | |
4a3ade4e | 215 | wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn); |
6fc6879b JM |
216 | wpa_cli_attached = 0; |
217 | } | |
218 | wpa_ctrl_close(ctrl_conn); | |
219 | ctrl_conn = NULL; | |
4a3ade4e | 220 | if (mon_conn) { |
cd101567 | 221 | eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn)); |
4a3ade4e JM |
222 | wpa_ctrl_close(mon_conn); |
223 | mon_conn = NULL; | |
224 | } | |
6fc6879b JM |
225 | } |
226 | ||
227 | ||
228 | static void wpa_cli_msg_cb(char *msg, size_t len) | |
229 | { | |
230 | printf("%s\n", msg); | |
231 | } | |
232 | ||
233 | ||
234 | static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print) | |
235 | { | |
236 | char buf[2048]; | |
237 | size_t len; | |
238 | int ret; | |
239 | ||
240 | if (ctrl_conn == NULL) { | |
241 | printf("Not connected to wpa_supplicant - command dropped.\n"); | |
242 | return -1; | |
243 | } | |
244 | len = sizeof(buf) - 1; | |
245 | ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, | |
246 | wpa_cli_msg_cb); | |
247 | if (ret == -2) { | |
248 | printf("'%s' command timed out.\n", cmd); | |
249 | return -2; | |
250 | } else if (ret < 0) { | |
251 | printf("'%s' command failed.\n", cmd); | |
252 | return -1; | |
253 | } | |
254 | if (print) { | |
255 | buf[len] = '\0'; | |
256 | printf("%s", buf); | |
257 | } | |
258 | return 0; | |
259 | } | |
260 | ||
261 | ||
262 | static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd) | |
263 | { | |
264 | return _wpa_ctrl_command(ctrl, cmd, 1); | |
265 | } | |
266 | ||
267 | ||
268 | static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
269 | { | |
270 | int verbose = argc > 0 && os_strcmp(argv[0], "verbose") == 0; | |
271 | return wpa_ctrl_command(ctrl, verbose ? "STATUS-VERBOSE" : "STATUS"); | |
272 | } | |
273 | ||
274 | ||
275 | static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
276 | { | |
277 | return wpa_ctrl_command(ctrl, "PING"); | |
278 | } | |
279 | ||
280 | ||
ac6912b5 BG |
281 | static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
282 | { | |
283 | return wpa_ctrl_command(ctrl, "RELOG"); | |
284 | } | |
285 | ||
286 | ||
77895cd9 JM |
287 | static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
288 | { | |
289 | char cmd[256]; | |
290 | int ret; | |
291 | if (argc == 0) | |
292 | return -1; | |
293 | ret = os_snprintf(cmd, sizeof(cmd), "NOTE %s", argv[0]); | |
294 | if (ret < 0 || (size_t) ret >= sizeof(cmd)) | |
295 | return -1; | |
296 | return wpa_ctrl_command(ctrl, cmd); | |
297 | } | |
298 | ||
299 | ||
6fc6879b JM |
300 | static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
301 | { | |
302 | return wpa_ctrl_command(ctrl, "MIB"); | |
303 | } | |
304 | ||
305 | ||
306 | static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
307 | { | |
308 | return wpa_ctrl_command(ctrl, "PMKSA"); | |
309 | } | |
310 | ||
311 | ||
312 | static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
313 | { | |
dfa141b1 | 314 | print_help(); |
6fc6879b JM |
315 | return 0; |
316 | } | |
317 | ||
318 | ||
319 | static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
320 | { | |
321 | printf("%s\n\n%s\n", wpa_cli_version, wpa_cli_full_license); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | ||
326 | static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
327 | { | |
328 | wpa_cli_quit = 1; | |
cd101567 JM |
329 | if (interactive) |
330 | eloop_terminate(); | |
6fc6879b JM |
331 | return 0; |
332 | } | |
333 | ||
334 | ||
335 | static void wpa_cli_show_variables(void) | |
336 | { | |
337 | printf("set variables:\n" | |
338 | " EAPOL::heldPeriod (EAPOL state machine held period, " | |
339 | "in seconds)\n" | |
340 | " EAPOL::authPeriod (EAPOL state machine authentication " | |
341 | "period, in seconds)\n" | |
342 | " EAPOL::startPeriod (EAPOL state machine start period, in " | |
343 | "seconds)\n" | |
344 | " EAPOL::maxStart (EAPOL state machine maximum start " | |
345 | "attempts)\n"); | |
346 | printf(" dot11RSNAConfigPMKLifetime (WPA/WPA2 PMK lifetime in " | |
347 | "seconds)\n" | |
348 | " dot11RSNAConfigPMKReauthThreshold (WPA/WPA2 reauthentication" | |
349 | " threshold\n\tpercentage)\n" | |
350 | " dot11RSNAConfigSATimeout (WPA/WPA2 timeout for completing " | |
351 | "security\n\tassociation in seconds)\n"); | |
352 | } | |
353 | ||
354 | ||
355 | static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
356 | { | |
357 | char cmd[256]; | |
358 | int res; | |
359 | ||
360 | if (argc == 0) { | |
361 | wpa_cli_show_variables(); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | if (argc != 2) { | |
366 | printf("Invalid SET command: needs two arguments (variable " | |
367 | "name and value)\n"); | |
368 | return -1; | |
369 | } | |
370 | ||
371 | res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]); | |
372 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
373 | printf("Too long SET command.\n"); | |
374 | return -1; | |
375 | } | |
376 | return wpa_ctrl_command(ctrl, cmd); | |
377 | } | |
378 | ||
379 | ||
acec8d32 JM |
380 | static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
381 | { | |
382 | char cmd[256]; | |
383 | int res; | |
384 | ||
385 | if (argc != 1) { | |
386 | printf("Invalid GET command: need one argument (variable " | |
387 | "name)\n"); | |
388 | return -1; | |
389 | } | |
390 | ||
391 | res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]); | |
392 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
393 | printf("Too long GET command.\n"); | |
394 | return -1; | |
395 | } | |
396 | return wpa_ctrl_command(ctrl, cmd); | |
397 | } | |
398 | ||
399 | ||
6fc6879b JM |
400 | static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
401 | { | |
402 | return wpa_ctrl_command(ctrl, "LOGOFF"); | |
403 | } | |
404 | ||
405 | ||
406 | static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
407 | { | |
408 | return wpa_ctrl_command(ctrl, "LOGON"); | |
409 | } | |
410 | ||
411 | ||
412 | static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc, | |
413 | char *argv[]) | |
414 | { | |
415 | return wpa_ctrl_command(ctrl, "REASSOCIATE"); | |
416 | } | |
417 | ||
418 | ||
419 | static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc, | |
420 | char *argv[]) | |
421 | { | |
422 | char cmd[256]; | |
423 | int res; | |
424 | ||
425 | if (argc != 1) { | |
426 | printf("Invalid PREAUTH command: needs one argument " | |
427 | "(BSSID)\n"); | |
428 | return -1; | |
429 | } | |
430 | ||
431 | res = os_snprintf(cmd, sizeof(cmd), "PREAUTH %s", argv[0]); | |
432 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
433 | printf("Too long PREAUTH command.\n"); | |
434 | return -1; | |
435 | } | |
436 | return wpa_ctrl_command(ctrl, cmd); | |
437 | } | |
438 | ||
439 | ||
440 | static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
441 | { | |
442 | char cmd[256]; | |
443 | int res; | |
444 | ||
445 | if (argc != 1) { | |
446 | printf("Invalid AP_SCAN command: needs one argument (ap_scan " | |
447 | "value)\n"); | |
448 | return -1; | |
449 | } | |
450 | res = os_snprintf(cmd, sizeof(cmd), "AP_SCAN %s", argv[0]); | |
451 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
452 | printf("Too long AP_SCAN command.\n"); | |
453 | return -1; | |
454 | } | |
455 | return wpa_ctrl_command(ctrl, cmd); | |
456 | } | |
457 | ||
458 | ||
459 | static int wpa_cli_cmd_stkstart(struct wpa_ctrl *ctrl, int argc, | |
460 | char *argv[]) | |
461 | { | |
462 | char cmd[256]; | |
463 | int res; | |
464 | ||
465 | if (argc != 1) { | |
466 | printf("Invalid STKSTART command: needs one argument " | |
467 | "(Peer STA MAC address)\n"); | |
468 | return -1; | |
469 | } | |
470 | ||
471 | res = os_snprintf(cmd, sizeof(cmd), "STKSTART %s", argv[0]); | |
472 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
473 | printf("Too long STKSTART command.\n"); | |
474 | return -1; | |
475 | } | |
476 | return wpa_ctrl_command(ctrl, cmd); | |
477 | } | |
478 | ||
479 | ||
480 | static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
481 | { | |
482 | char cmd[256]; | |
483 | int res; | |
484 | ||
485 | if (argc != 1) { | |
486 | printf("Invalid FT_DS command: needs one argument " | |
487 | "(Target AP MAC address)\n"); | |
488 | return -1; | |
489 | } | |
490 | ||
491 | res = os_snprintf(cmd, sizeof(cmd), "FT_DS %s", argv[0]); | |
492 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
493 | printf("Too long FT_DS command.\n"); | |
494 | return -1; | |
495 | } | |
496 | return wpa_ctrl_command(ctrl, cmd); | |
497 | } | |
498 | ||
499 | ||
fcc60db4 JM |
500 | static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
501 | { | |
502 | char cmd[256]; | |
503 | int res; | |
504 | ||
505 | if (argc == 0) { | |
506 | /* Any BSSID */ | |
507 | return wpa_ctrl_command(ctrl, "WPS_PBC"); | |
508 | } | |
509 | ||
510 | /* Specific BSSID */ | |
511 | res = os_snprintf(cmd, sizeof(cmd), "WPS_PBC %s", argv[0]); | |
512 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
513 | printf("Too long WPS_PBC command.\n"); | |
514 | return -1; | |
515 | } | |
516 | return wpa_ctrl_command(ctrl, cmd); | |
517 | } | |
518 | ||
519 | ||
520 | static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
521 | { | |
522 | char cmd[256]; | |
523 | int res; | |
524 | ||
525 | if (argc == 0) { | |
526 | printf("Invalid WPS_PIN command: need one or two arguments:\n" | |
527 | "- BSSID: use 'any' to select any\n" | |
528 | "- PIN: optional, used only with devices that have no " | |
529 | "display\n"); | |
530 | return -1; | |
531 | } | |
532 | ||
533 | if (argc == 1) { | |
534 | /* Use dynamically generated PIN (returned as reply) */ | |
535 | res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s", argv[0]); | |
536 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
537 | printf("Too long WPS_PIN command.\n"); | |
538 | return -1; | |
539 | } | |
540 | return wpa_ctrl_command(ctrl, cmd); | |
541 | } | |
542 | ||
543 | /* Use hardcoded PIN from a label */ | |
544 | res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s", argv[0], argv[1]); | |
545 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
546 | printf("Too long WPS_PIN command.\n"); | |
547 | return -1; | |
548 | } | |
549 | return wpa_ctrl_command(ctrl, cmd); | |
550 | } | |
551 | ||
552 | ||
3981cb3c JM |
553 | static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc, |
554 | char *argv[]) | |
555 | { | |
556 | char cmd[256]; | |
557 | int res; | |
558 | ||
559 | if (argc != 1 && argc != 2) { | |
560 | printf("Invalid WPS_CHECK_PIN command: needs one argument:\n" | |
561 | "- PIN to be verified\n"); | |
562 | return -1; | |
563 | } | |
564 | ||
565 | if (argc == 2) | |
566 | res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s", | |
567 | argv[0], argv[1]); | |
568 | else | |
569 | res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s", | |
570 | argv[0]); | |
571 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
572 | printf("Too long WPS_CHECK_PIN command.\n"); | |
573 | return -1; | |
574 | } | |
575 | return wpa_ctrl_command(ctrl, cmd); | |
576 | } | |
577 | ||
578 | ||
2f9929ff AC |
579 | static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc, |
580 | char *argv[]) | |
581 | { | |
582 | return wpa_ctrl_command(ctrl, "WPS_CANCEL"); | |
583 | } | |
584 | ||
585 | ||
116f7bb0 | 586 | #ifdef CONFIG_WPS_OOB |
46bdb83a MH |
587 | static int wpa_cli_cmd_wps_oob(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
588 | { | |
589 | char cmd[256]; | |
590 | int res; | |
591 | ||
e1ee6b60 MH |
592 | if (argc != 3 && argc != 4) { |
593 | printf("Invalid WPS_OOB command: need three or four " | |
594 | "arguments:\n" | |
595 | "- DEV_TYPE: use 'ufd' or 'nfc'\n" | |
596 | "- PATH: path of OOB device like '/mnt'\n" | |
597 | "- METHOD: OOB method 'pin-e' or 'pin-r', " | |
598 | "'cred'\n" | |
599 | "- DEV_NAME: (only for NFC) device name like " | |
600 | "'pn531'\n"); | |
46bdb83a MH |
601 | return -1; |
602 | } | |
603 | ||
e1ee6b60 MH |
604 | if (argc == 3) |
605 | res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s", | |
606 | argv[0], argv[1], argv[2]); | |
607 | else | |
608 | res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s %s", | |
609 | argv[0], argv[1], argv[2], argv[3]); | |
46bdb83a MH |
610 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { |
611 | printf("Too long WPS_OOB command.\n"); | |
612 | return -1; | |
613 | } | |
614 | return wpa_ctrl_command(ctrl, cmd); | |
615 | } | |
116f7bb0 | 616 | #endif /* CONFIG_WPS_OOB */ |
46bdb83a MH |
617 | |
618 | ||
fcc60db4 JM |
619 | static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
620 | { | |
621 | char cmd[256]; | |
622 | int res; | |
623 | ||
52eb293d JM |
624 | if (argc == 2) |
625 | res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s", | |
626 | argv[0], argv[1]); | |
7d6640a6 | 627 | else if (argc == 5 || argc == 6) { |
52eb293d JM |
628 | char ssid_hex[2 * 32 + 1]; |
629 | char key_hex[2 * 64 + 1]; | |
630 | int i; | |
631 | ||
632 | ssid_hex[0] = '\0'; | |
633 | for (i = 0; i < 32; i++) { | |
634 | if (argv[2][i] == '\0') | |
635 | break; | |
636 | os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]); | |
637 | } | |
638 | ||
639 | key_hex[0] = '\0'; | |
7d6640a6 JM |
640 | if (argc == 6) { |
641 | for (i = 0; i < 64; i++) { | |
642 | if (argv[5][i] == '\0') | |
643 | break; | |
644 | os_snprintf(&key_hex[i * 2], 3, "%02x", | |
645 | argv[5][i]); | |
646 | } | |
52eb293d JM |
647 | } |
648 | ||
649 | res = os_snprintf(cmd, sizeof(cmd), | |
650 | "WPS_REG %s %s %s %s %s %s", | |
651 | argv[0], argv[1], ssid_hex, argv[3], argv[4], | |
652 | key_hex); | |
653 | } else { | |
fcc60db4 | 654 | printf("Invalid WPS_REG command: need two arguments:\n" |
129eb428 | 655 | "- BSSID of the target AP\n" |
fcc60db4 | 656 | "- AP PIN\n"); |
52eb293d JM |
657 | printf("Alternatively, six arguments can be used to " |
658 | "reconfigure the AP:\n" | |
129eb428 | 659 | "- BSSID of the target AP\n" |
52eb293d JM |
660 | "- AP PIN\n" |
661 | "- new SSID\n" | |
662 | "- new auth (OPEN, WPAPSK, WPA2PSK)\n" | |
663 | "- new encr (NONE, WEP, TKIP, CCMP)\n" | |
664 | "- new key\n"); | |
fcc60db4 JM |
665 | return -1; |
666 | } | |
667 | ||
fcc60db4 JM |
668 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { |
669 | printf("Too long WPS_REG command.\n"); | |
670 | return -1; | |
671 | } | |
672 | return wpa_ctrl_command(ctrl, cmd); | |
673 | } | |
674 | ||
675 | ||
70d84f11 JM |
676 | static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc, |
677 | char *argv[]) | |
678 | { | |
679 | char cmd[256]; | |
680 | int res; | |
681 | ||
682 | if (argc < 1) { | |
683 | printf("Invalid WPS_AP_PIN command: needs at least one " | |
684 | "argument\n"); | |
685 | return -1; | |
686 | } | |
687 | ||
688 | if (argc > 2) | |
689 | res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s %s %s", | |
690 | argv[0], argv[1], argv[2]); | |
691 | else if (argc > 1) | |
692 | res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s %s", | |
693 | argv[0], argv[1]); | |
694 | else | |
695 | res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s", | |
696 | argv[0]); | |
697 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
698 | printf("Too long WPS_AP_PIN command.\n"); | |
699 | return -1; | |
700 | } | |
701 | return wpa_ctrl_command(ctrl, cmd); | |
702 | } | |
703 | ||
704 | ||
e9bcfebf JM |
705 | static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc, |
706 | char *argv[]) | |
707 | { | |
08486685 JM |
708 | char cmd[100]; |
709 | if (argc > 0) { | |
710 | os_snprintf(cmd, sizeof(cmd), "WPS_ER_START %s", argv[0]); | |
711 | return wpa_ctrl_command(ctrl, cmd); | |
712 | } | |
e9bcfebf | 713 | return wpa_ctrl_command(ctrl, "WPS_ER_START"); |
e9bcfebf JM |
714 | } |
715 | ||
716 | ||
717 | static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc, | |
718 | char *argv[]) | |
719 | { | |
720 | return wpa_ctrl_command(ctrl, "WPS_ER_STOP"); | |
721 | ||
722 | } | |
723 | ||
724 | ||
72df2f5f JM |
725 | static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc, |
726 | char *argv[]) | |
727 | { | |
728 | char cmd[256]; | |
729 | int res; | |
730 | ||
31fcea93 JM |
731 | if (argc < 2) { |
732 | printf("Invalid WPS_ER_PIN command: need at least two " | |
733 | "arguments:\n" | |
72df2f5f | 734 | "- UUID: use 'any' to select any\n" |
31fcea93 JM |
735 | "- PIN: Enrollee PIN\n" |
736 | "optional: - Enrollee MAC address\n"); | |
72df2f5f JM |
737 | return -1; |
738 | } | |
739 | ||
31fcea93 JM |
740 | if (argc > 2) |
741 | res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s %s", | |
742 | argv[0], argv[1], argv[2]); | |
743 | else | |
744 | res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s", | |
745 | argv[0], argv[1]); | |
72df2f5f JM |
746 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { |
747 | printf("Too long WPS_ER_PIN command.\n"); | |
748 | return -1; | |
749 | } | |
750 | return wpa_ctrl_command(ctrl, cmd); | |
751 | } | |
752 | ||
753 | ||
564cd7fa JM |
754 | static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc, |
755 | char *argv[]) | |
756 | { | |
757 | char cmd[256]; | |
758 | int res; | |
759 | ||
760 | if (argc != 1) { | |
761 | printf("Invalid WPS_ER_PBC command: need one argument:\n" | |
762 | "- UUID: Specify the Enrollee\n"); | |
763 | return -1; | |
764 | } | |
765 | ||
766 | res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PBC %s", | |
767 | argv[0]); | |
768 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
769 | printf("Too long WPS_ER_PBC command.\n"); | |
770 | return -1; | |
771 | } | |
772 | return wpa_ctrl_command(ctrl, cmd); | |
773 | } | |
774 | ||
775 | ||
e64dcfd5 JM |
776 | static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc, |
777 | char *argv[]) | |
778 | { | |
779 | char cmd[256]; | |
780 | int res; | |
781 | ||
782 | if (argc != 2) { | |
783 | printf("Invalid WPS_ER_LEARN command: need two arguments:\n" | |
784 | "- UUID: specify which AP to use\n" | |
785 | "- PIN: AP PIN\n"); | |
786 | return -1; | |
787 | } | |
788 | ||
789 | res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_LEARN %s %s", | |
790 | argv[0], argv[1]); | |
791 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
792 | printf("Too long WPS_ER_LEARN command.\n"); | |
793 | return -1; | |
794 | } | |
795 | return wpa_ctrl_command(ctrl, cmd); | |
796 | } | |
797 | ||
798 | ||
ef10f473 JM |
799 | static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc, |
800 | char *argv[]) | |
801 | { | |
802 | char cmd[256]; | |
803 | int res; | |
804 | ||
805 | if (argc != 2) { | |
806 | printf("Invalid WPS_ER_SET_CONFIG command: need two " | |
807 | "arguments:\n" | |
808 | "- UUID: specify which AP to use\n" | |
809 | "- Network configuration id\n"); | |
810 | return -1; | |
811 | } | |
812 | ||
813 | res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_SET_CONFIG %s %s", | |
814 | argv[0], argv[1]); | |
815 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
816 | printf("Too long WPS_ER_SET_CONFIG command.\n"); | |
817 | return -1; | |
818 | } | |
819 | return wpa_ctrl_command(ctrl, cmd); | |
820 | } | |
821 | ||
822 | ||
7d6640a6 JM |
823 | static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc, |
824 | char *argv[]) | |
825 | { | |
826 | char cmd[256]; | |
827 | int res; | |
828 | ||
829 | if (argc == 5 || argc == 6) { | |
830 | char ssid_hex[2 * 32 + 1]; | |
831 | char key_hex[2 * 64 + 1]; | |
832 | int i; | |
833 | ||
834 | ssid_hex[0] = '\0'; | |
835 | for (i = 0; i < 32; i++) { | |
836 | if (argv[2][i] == '\0') | |
837 | break; | |
838 | os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]); | |
839 | } | |
840 | ||
841 | key_hex[0] = '\0'; | |
842 | if (argc == 6) { | |
843 | for (i = 0; i < 64; i++) { | |
844 | if (argv[5][i] == '\0') | |
845 | break; | |
846 | os_snprintf(&key_hex[i * 2], 3, "%02x", | |
847 | argv[5][i]); | |
848 | } | |
849 | } | |
850 | ||
851 | res = os_snprintf(cmd, sizeof(cmd), | |
852 | "WPS_ER_CONFIG %s %s %s %s %s %s", | |
853 | argv[0], argv[1], ssid_hex, argv[3], argv[4], | |
854 | key_hex); | |
855 | } else { | |
856 | printf("Invalid WPS_ER_CONFIG command: need six arguments:\n" | |
857 | "- AP UUID\n" | |
858 | "- AP PIN\n" | |
859 | "- new SSID\n" | |
860 | "- new auth (OPEN, WPAPSK, WPA2PSK)\n" | |
861 | "- new encr (NONE, WEP, TKIP, CCMP)\n" | |
862 | "- new key\n"); | |
863 | return -1; | |
864 | } | |
865 | ||
866 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
867 | printf("Too long WPS_ER_CONFIG command.\n"); | |
868 | return -1; | |
869 | } | |
870 | return wpa_ctrl_command(ctrl, cmd); | |
871 | } | |
872 | ||
873 | ||
11ef8d35 JM |
874 | static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
875 | { | |
876 | char cmd[256]; | |
877 | int res; | |
878 | ||
879 | if (argc != 1) { | |
880 | printf("Invalid IBSS_RSN command: needs one argument " | |
881 | "(Peer STA MAC address)\n"); | |
882 | return -1; | |
883 | } | |
884 | ||
885 | res = os_snprintf(cmd, sizeof(cmd), "IBSS_RSN %s", argv[0]); | |
886 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
887 | printf("Too long IBSS_RSN command.\n"); | |
888 | return -1; | |
889 | } | |
890 | return wpa_ctrl_command(ctrl, cmd); | |
891 | } | |
892 | ||
893 | ||
6fc6879b JM |
894 | static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
895 | { | |
896 | char cmd[256]; | |
897 | int res; | |
898 | ||
899 | if (argc != 1) { | |
900 | printf("Invalid LEVEL command: needs one argument (debug " | |
901 | "level)\n"); | |
902 | return -1; | |
903 | } | |
904 | res = os_snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]); | |
905 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
906 | printf("Too long LEVEL command.\n"); | |
907 | return -1; | |
908 | } | |
909 | return wpa_ctrl_command(ctrl, cmd); | |
910 | } | |
911 | ||
912 | ||
913 | static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
914 | { | |
915 | char cmd[256], *pos, *end; | |
916 | int i, ret; | |
917 | ||
918 | if (argc < 2) { | |
919 | printf("Invalid IDENTITY command: needs two arguments " | |
920 | "(network id and identity)\n"); | |
921 | return -1; | |
922 | } | |
923 | ||
924 | end = cmd + sizeof(cmd); | |
925 | pos = cmd; | |
926 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s", | |
927 | argv[0], argv[1]); | |
928 | if (ret < 0 || ret >= end - pos) { | |
929 | printf("Too long IDENTITY command.\n"); | |
930 | return -1; | |
931 | } | |
932 | pos += ret; | |
933 | for (i = 2; i < argc; i++) { | |
934 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
935 | if (ret < 0 || ret >= end - pos) { | |
936 | printf("Too long IDENTITY command.\n"); | |
937 | return -1; | |
938 | } | |
939 | pos += ret; | |
940 | } | |
941 | ||
942 | return wpa_ctrl_command(ctrl, cmd); | |
943 | } | |
944 | ||
945 | ||
946 | static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
947 | { | |
948 | char cmd[256], *pos, *end; | |
949 | int i, ret; | |
950 | ||
951 | if (argc < 2) { | |
952 | printf("Invalid PASSWORD command: needs two arguments " | |
953 | "(network id and password)\n"); | |
954 | return -1; | |
955 | } | |
956 | ||
957 | end = cmd + sizeof(cmd); | |
958 | pos = cmd; | |
959 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s", | |
960 | argv[0], argv[1]); | |
961 | if (ret < 0 || ret >= end - pos) { | |
962 | printf("Too long PASSWORD command.\n"); | |
963 | return -1; | |
964 | } | |
965 | pos += ret; | |
966 | for (i = 2; i < argc; i++) { | |
967 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
968 | if (ret < 0 || ret >= end - pos) { | |
969 | printf("Too long PASSWORD command.\n"); | |
970 | return -1; | |
971 | } | |
972 | pos += ret; | |
973 | } | |
974 | ||
975 | return wpa_ctrl_command(ctrl, cmd); | |
976 | } | |
977 | ||
978 | ||
979 | static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc, | |
980 | char *argv[]) | |
981 | { | |
982 | char cmd[256], *pos, *end; | |
983 | int i, ret; | |
984 | ||
985 | if (argc < 2) { | |
986 | printf("Invalid NEW_PASSWORD command: needs two arguments " | |
987 | "(network id and password)\n"); | |
988 | return -1; | |
989 | } | |
990 | ||
991 | end = cmd + sizeof(cmd); | |
992 | pos = cmd; | |
993 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s", | |
994 | argv[0], argv[1]); | |
995 | if (ret < 0 || ret >= end - pos) { | |
996 | printf("Too long NEW_PASSWORD command.\n"); | |
997 | return -1; | |
998 | } | |
999 | pos += ret; | |
1000 | for (i = 2; i < argc; i++) { | |
1001 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
1002 | if (ret < 0 || ret >= end - pos) { | |
1003 | printf("Too long NEW_PASSWORD command.\n"); | |
1004 | return -1; | |
1005 | } | |
1006 | pos += ret; | |
1007 | } | |
1008 | ||
1009 | return wpa_ctrl_command(ctrl, cmd); | |
1010 | } | |
1011 | ||
1012 | ||
1013 | static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1014 | { | |
1015 | char cmd[256], *pos, *end; | |
1016 | int i, ret; | |
1017 | ||
1018 | if (argc < 2) { | |
1019 | printf("Invalid PIN command: needs two arguments " | |
1020 | "(network id and pin)\n"); | |
1021 | return -1; | |
1022 | } | |
1023 | ||
1024 | end = cmd + sizeof(cmd); | |
1025 | pos = cmd; | |
1026 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s", | |
1027 | argv[0], argv[1]); | |
1028 | if (ret < 0 || ret >= end - pos) { | |
1029 | printf("Too long PIN command.\n"); | |
1030 | return -1; | |
1031 | } | |
1032 | pos += ret; | |
1033 | for (i = 2; i < argc; i++) { | |
1034 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
1035 | if (ret < 0 || ret >= end - pos) { | |
1036 | printf("Too long PIN command.\n"); | |
1037 | return -1; | |
1038 | } | |
1039 | pos += ret; | |
1040 | } | |
1041 | return wpa_ctrl_command(ctrl, cmd); | |
1042 | } | |
1043 | ||
1044 | ||
1045 | static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1046 | { | |
1047 | char cmd[256], *pos, *end; | |
1048 | int i, ret; | |
1049 | ||
1050 | if (argc < 2) { | |
1051 | printf("Invalid OTP command: needs two arguments (network " | |
1052 | "id and password)\n"); | |
1053 | return -1; | |
1054 | } | |
1055 | ||
1056 | end = cmd + sizeof(cmd); | |
1057 | pos = cmd; | |
1058 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s", | |
1059 | argv[0], argv[1]); | |
1060 | if (ret < 0 || ret >= end - pos) { | |
1061 | printf("Too long OTP command.\n"); | |
1062 | return -1; | |
1063 | } | |
1064 | pos += ret; | |
1065 | for (i = 2; i < argc; i++) { | |
1066 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
1067 | if (ret < 0 || ret >= end - pos) { | |
1068 | printf("Too long OTP command.\n"); | |
1069 | return -1; | |
1070 | } | |
1071 | pos += ret; | |
1072 | } | |
1073 | ||
1074 | return wpa_ctrl_command(ctrl, cmd); | |
1075 | } | |
1076 | ||
1077 | ||
1078 | static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc, | |
1079 | char *argv[]) | |
1080 | { | |
1081 | char cmd[256], *pos, *end; | |
1082 | int i, ret; | |
1083 | ||
1084 | if (argc < 2) { | |
1085 | printf("Invalid PASSPHRASE command: needs two arguments " | |
1086 | "(network id and passphrase)\n"); | |
1087 | return -1; | |
1088 | } | |
1089 | ||
1090 | end = cmd + sizeof(cmd); | |
1091 | pos = cmd; | |
1092 | ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s", | |
1093 | argv[0], argv[1]); | |
1094 | if (ret < 0 || ret >= end - pos) { | |
1095 | printf("Too long PASSPHRASE command.\n"); | |
1096 | return -1; | |
1097 | } | |
1098 | pos += ret; | |
1099 | for (i = 2; i < argc; i++) { | |
1100 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
1101 | if (ret < 0 || ret >= end - pos) { | |
1102 | printf("Too long PASSPHRASE command.\n"); | |
1103 | return -1; | |
1104 | } | |
1105 | pos += ret; | |
1106 | } | |
1107 | ||
1108 | return wpa_ctrl_command(ctrl, cmd); | |
1109 | } | |
1110 | ||
1111 | ||
1112 | static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1113 | { | |
1114 | char cmd[256], *pos, *end; | |
1115 | int i, ret; | |
1116 | ||
1117 | if (argc < 2) { | |
1118 | printf("Invalid BSSID command: needs two arguments (network " | |
1119 | "id and BSSID)\n"); | |
1120 | return -1; | |
1121 | } | |
1122 | ||
1123 | end = cmd + sizeof(cmd); | |
1124 | pos = cmd; | |
1125 | ret = os_snprintf(pos, end - pos, "BSSID"); | |
1126 | if (ret < 0 || ret >= end - pos) { | |
1127 | printf("Too long BSSID command.\n"); | |
1128 | return -1; | |
1129 | } | |
1130 | pos += ret; | |
1131 | for (i = 0; i < argc; i++) { | |
1132 | ret = os_snprintf(pos, end - pos, " %s", argv[i]); | |
1133 | if (ret < 0 || ret >= end - pos) { | |
1134 | printf("Too long BSSID command.\n"); | |
1135 | return -1; | |
1136 | } | |
1137 | pos += ret; | |
1138 | } | |
1139 | ||
1140 | return wpa_ctrl_command(ctrl, cmd); | |
1141 | } | |
1142 | ||
1143 | ||
1144 | static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc, | |
1145 | char *argv[]) | |
1146 | { | |
1147 | return wpa_ctrl_command(ctrl, "LIST_NETWORKS"); | |
1148 | } | |
1149 | ||
1150 | ||
1151 | static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc, | |
1152 | char *argv[]) | |
1153 | { | |
1154 | char cmd[32]; | |
1155 | int res; | |
1156 | ||
1157 | if (argc < 1) { | |
1158 | printf("Invalid SELECT_NETWORK command: needs one argument " | |
1159 | "(network id)\n"); | |
1160 | return -1; | |
1161 | } | |
1162 | ||
1163 | res = os_snprintf(cmd, sizeof(cmd), "SELECT_NETWORK %s", argv[0]); | |
1164 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1165 | return -1; | |
1166 | cmd[sizeof(cmd) - 1] = '\0'; | |
1167 | ||
1168 | return wpa_ctrl_command(ctrl, cmd); | |
1169 | } | |
1170 | ||
1171 | ||
1172 | static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc, | |
1173 | char *argv[]) | |
1174 | { | |
1175 | char cmd[32]; | |
1176 | int res; | |
1177 | ||
1178 | if (argc < 1) { | |
1179 | printf("Invalid ENABLE_NETWORK command: needs one argument " | |
1180 | "(network id)\n"); | |
1181 | return -1; | |
1182 | } | |
1183 | ||
1184 | res = os_snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %s", argv[0]); | |
1185 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1186 | return -1; | |
1187 | cmd[sizeof(cmd) - 1] = '\0'; | |
1188 | ||
1189 | return wpa_ctrl_command(ctrl, cmd); | |
1190 | } | |
1191 | ||
1192 | ||
1193 | static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc, | |
1194 | char *argv[]) | |
1195 | { | |
1196 | char cmd[32]; | |
1197 | int res; | |
1198 | ||
1199 | if (argc < 1) { | |
1200 | printf("Invalid DISABLE_NETWORK command: needs one argument " | |
1201 | "(network id)\n"); | |
1202 | return -1; | |
1203 | } | |
1204 | ||
1205 | res = os_snprintf(cmd, sizeof(cmd), "DISABLE_NETWORK %s", argv[0]); | |
1206 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1207 | return -1; | |
1208 | cmd[sizeof(cmd) - 1] = '\0'; | |
1209 | ||
1210 | return wpa_ctrl_command(ctrl, cmd); | |
1211 | } | |
1212 | ||
1213 | ||
1214 | static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc, | |
1215 | char *argv[]) | |
1216 | { | |
1217 | return wpa_ctrl_command(ctrl, "ADD_NETWORK"); | |
1218 | } | |
1219 | ||
1220 | ||
1221 | static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc, | |
1222 | char *argv[]) | |
1223 | { | |
1224 | char cmd[32]; | |
1225 | int res; | |
1226 | ||
1227 | if (argc < 1) { | |
1228 | printf("Invalid REMOVE_NETWORK command: needs one argument " | |
1229 | "(network id)\n"); | |
1230 | return -1; | |
1231 | } | |
1232 | ||
1233 | res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %s", argv[0]); | |
1234 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1235 | return -1; | |
1236 | cmd[sizeof(cmd) - 1] = '\0'; | |
1237 | ||
1238 | return wpa_ctrl_command(ctrl, cmd); | |
1239 | } | |
1240 | ||
1241 | ||
1242 | static void wpa_cli_show_network_variables(void) | |
1243 | { | |
1244 | printf("set_network variables:\n" | |
1245 | " ssid (network name, SSID)\n" | |
1246 | " psk (WPA passphrase or pre-shared key)\n" | |
1247 | " key_mgmt (key management protocol)\n" | |
1248 | " identity (EAP identity)\n" | |
1249 | " password (EAP password)\n" | |
1250 | " ...\n" | |
1251 | "\n" | |
1252 | "Note: Values are entered in the same format as the " | |
1253 | "configuration file is using,\n" | |
1254 | "i.e., strings values need to be inside double quotation " | |
1255 | "marks.\n" | |
1256 | "For example: set_network 1 ssid \"network name\"\n" | |
1257 | "\n" | |
1258 | "Please see wpa_supplicant.conf documentation for full list " | |
1259 | "of\navailable variables.\n"); | |
1260 | } | |
1261 | ||
1262 | ||
1263 | static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc, | |
1264 | char *argv[]) | |
1265 | { | |
1266 | char cmd[256]; | |
1267 | int res; | |
1268 | ||
1269 | if (argc == 0) { | |
1270 | wpa_cli_show_network_variables(); | |
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | if (argc != 3) { | |
1275 | printf("Invalid SET_NETWORK command: needs three arguments\n" | |
1276 | "(network id, variable name, and value)\n"); | |
1277 | return -1; | |
1278 | } | |
1279 | ||
1280 | res = os_snprintf(cmd, sizeof(cmd), "SET_NETWORK %s %s %s", | |
1281 | argv[0], argv[1], argv[2]); | |
1282 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
1283 | printf("Too long SET_NETWORK command.\n"); | |
1284 | return -1; | |
1285 | } | |
1286 | return wpa_ctrl_command(ctrl, cmd); | |
1287 | } | |
1288 | ||
1289 | ||
1290 | static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc, | |
1291 | char *argv[]) | |
1292 | { | |
1293 | char cmd[256]; | |
1294 | int res; | |
1295 | ||
1296 | if (argc == 0) { | |
1297 | wpa_cli_show_network_variables(); | |
1298 | return 0; | |
1299 | } | |
1300 | ||
1301 | if (argc != 2) { | |
1302 | printf("Invalid GET_NETWORK command: needs two arguments\n" | |
1303 | "(network id and variable name)\n"); | |
1304 | return -1; | |
1305 | } | |
1306 | ||
1307 | res = os_snprintf(cmd, sizeof(cmd), "GET_NETWORK %s %s", | |
1308 | argv[0], argv[1]); | |
1309 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
1310 | printf("Too long GET_NETWORK command.\n"); | |
1311 | return -1; | |
1312 | } | |
1313 | return wpa_ctrl_command(ctrl, cmd); | |
1314 | } | |
1315 | ||
1316 | ||
1317 | static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc, | |
1318 | char *argv[]) | |
1319 | { | |
1320 | return wpa_ctrl_command(ctrl, "DISCONNECT"); | |
1321 | } | |
1322 | ||
1323 | ||
1324 | static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc, | |
1325 | char *argv[]) | |
1326 | { | |
1327 | return wpa_ctrl_command(ctrl, "RECONNECT"); | |
1328 | } | |
1329 | ||
1330 | ||
1331 | static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc, | |
1332 | char *argv[]) | |
1333 | { | |
1334 | return wpa_ctrl_command(ctrl, "SAVE_CONFIG"); | |
1335 | } | |
1336 | ||
1337 | ||
1338 | static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1339 | { | |
1340 | return wpa_ctrl_command(ctrl, "SCAN"); | |
1341 | } | |
1342 | ||
1343 | ||
1344 | static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc, | |
1345 | char *argv[]) | |
1346 | { | |
1347 | return wpa_ctrl_command(ctrl, "SCAN_RESULTS"); | |
1348 | } | |
1349 | ||
1350 | ||
1351 | static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1352 | { | |
1353 | char cmd[64]; | |
1354 | int res; | |
1355 | ||
1356 | if (argc != 1) { | |
1357 | printf("Invalid BSS command: need one argument (index or " | |
1358 | "BSSID)\n"); | |
1359 | return -1; | |
1360 | } | |
1361 | ||
1362 | res = os_snprintf(cmd, sizeof(cmd), "BSS %s", argv[0]); | |
1363 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1364 | return -1; | |
1365 | cmd[sizeof(cmd) - 1] = '\0'; | |
1366 | ||
1367 | return wpa_ctrl_command(ctrl, cmd); | |
1368 | } | |
1369 | ||
1370 | ||
1371 | static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc, | |
1372 | char *argv[]) | |
1373 | { | |
1374 | char cmd[64]; | |
1375 | int res; | |
1376 | ||
1377 | if (argc < 1 || argc > 2) { | |
1378 | printf("Invalid GET_CAPABILITY command: need either one or " | |
1379 | "two arguments\n"); | |
1380 | return -1; | |
1381 | } | |
1382 | ||
1383 | if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) { | |
1384 | printf("Invalid GET_CAPABILITY command: second argument, " | |
1385 | "if any, must be 'strict'\n"); | |
1386 | return -1; | |
1387 | } | |
1388 | ||
1389 | res = os_snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s%s", argv[0], | |
1390 | (argc == 2) ? " strict" : ""); | |
1391 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1392 | return -1; | |
1393 | cmd[sizeof(cmd) - 1] = '\0'; | |
1394 | ||
1395 | return wpa_ctrl_command(ctrl, cmd); | |
1396 | } | |
1397 | ||
1398 | ||
1399 | static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl) | |
1400 | { | |
1401 | printf("Available interfaces:\n"); | |
1402 | return wpa_ctrl_command(ctrl, "INTERFACES"); | |
1403 | } | |
1404 | ||
1405 | ||
1406 | static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1407 | { | |
1408 | if (argc < 1) { | |
1409 | wpa_cli_list_interfaces(ctrl); | |
1410 | return 0; | |
1411 | } | |
1412 | ||
1413 | wpa_cli_close_connection(); | |
1414 | os_free(ctrl_ifname); | |
1415 | ctrl_ifname = os_strdup(argv[0]); | |
1416 | ||
4a3ade4e | 1417 | if (wpa_cli_open_connection(ctrl_ifname, 1)) { |
6fc6879b | 1418 | printf("Connected to interface '%s.\n", ctrl_ifname); |
6fc6879b JM |
1419 | } else { |
1420 | printf("Could not connect to interface '%s' - re-trying\n", | |
1421 | ctrl_ifname); | |
1422 | } | |
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | ||
1427 | static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc, | |
1428 | char *argv[]) | |
1429 | { | |
1430 | return wpa_ctrl_command(ctrl, "RECONFIGURE"); | |
1431 | } | |
1432 | ||
1433 | ||
1434 | static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc, | |
1435 | char *argv[]) | |
1436 | { | |
1437 | return wpa_ctrl_command(ctrl, "TERMINATE"); | |
1438 | } | |
1439 | ||
1440 | ||
1441 | static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc, | |
1442 | char *argv[]) | |
1443 | { | |
1444 | char cmd[256]; | |
1445 | int res; | |
1446 | ||
1447 | if (argc < 1) { | |
1448 | printf("Invalid INTERFACE_ADD command: needs at least one " | |
1449 | "argument (interface name)\n" | |
1450 | "All arguments: ifname confname driver ctrl_interface " | |
1451 | "driver_param bridge_name\n"); | |
1452 | return -1; | |
1453 | } | |
1454 | ||
1455 | /* | |
1456 | * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB | |
1457 | * <driver_param>TAB<bridge_name> | |
1458 | */ | |
1459 | res = os_snprintf(cmd, sizeof(cmd), | |
1460 | "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s", | |
1461 | argv[0], | |
1462 | argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "", | |
1463 | argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "", | |
1464 | argc > 5 ? argv[5] : ""); | |
1465 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1466 | return -1; | |
1467 | cmd[sizeof(cmd) - 1] = '\0'; | |
1468 | return wpa_ctrl_command(ctrl, cmd); | |
1469 | } | |
1470 | ||
1471 | ||
1472 | static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc, | |
1473 | char *argv[]) | |
1474 | { | |
1475 | char cmd[128]; | |
1476 | int res; | |
1477 | ||
1478 | if (argc != 1) { | |
1479 | printf("Invalid INTERFACE_REMOVE command: needs one argument " | |
1480 | "(interface name)\n"); | |
1481 | return -1; | |
1482 | } | |
1483 | ||
1484 | res = os_snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]); | |
1485 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1486 | return -1; | |
1487 | cmd[sizeof(cmd) - 1] = '\0'; | |
1488 | return wpa_ctrl_command(ctrl, cmd); | |
1489 | } | |
1490 | ||
1491 | ||
4b4a8ae5 JM |
1492 | static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc, |
1493 | char *argv[]) | |
1494 | { | |
1495 | return wpa_ctrl_command(ctrl, "INTERFACE_LIST"); | |
1496 | } | |
1497 | ||
1498 | ||
e653b622 JM |
1499 | #ifdef CONFIG_AP |
1500 | static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1501 | { | |
1502 | char buf[64]; | |
1503 | if (argc != 1) { | |
1504 | printf("Invalid 'sta' command - exactly one argument, STA " | |
1505 | "address, is required.\n"); | |
1506 | return -1; | |
1507 | } | |
e824cc46 | 1508 | os_snprintf(buf, sizeof(buf), "STA %s", argv[0]); |
e653b622 JM |
1509 | return wpa_ctrl_command(ctrl, buf); |
1510 | } | |
1511 | ||
1512 | ||
1513 | static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd, | |
1514 | char *addr, size_t addr_len) | |
1515 | { | |
1516 | char buf[4096], *pos; | |
1517 | size_t len; | |
1518 | int ret; | |
1519 | ||
1520 | if (ctrl_conn == NULL) { | |
1521 | printf("Not connected to hostapd - command dropped.\n"); | |
1522 | return -1; | |
1523 | } | |
1524 | len = sizeof(buf) - 1; | |
1525 | ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, | |
1526 | wpa_cli_msg_cb); | |
1527 | if (ret == -2) { | |
1528 | printf("'%s' command timed out.\n", cmd); | |
1529 | return -2; | |
1530 | } else if (ret < 0) { | |
1531 | printf("'%s' command failed.\n", cmd); | |
1532 | return -1; | |
1533 | } | |
1534 | ||
1535 | buf[len] = '\0'; | |
1536 | if (memcmp(buf, "FAIL", 4) == 0) | |
1537 | return -1; | |
1538 | printf("%s", buf); | |
1539 | ||
1540 | pos = buf; | |
1541 | while (*pos != '\0' && *pos != '\n') | |
1542 | pos++; | |
1543 | *pos = '\0'; | |
1544 | os_strlcpy(addr, buf, addr_len); | |
1545 | return 0; | |
1546 | } | |
1547 | ||
1548 | ||
1549 | static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1550 | { | |
1551 | char addr[32], cmd[64]; | |
1552 | ||
1553 | if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr))) | |
1554 | return 0; | |
1555 | do { | |
e824cc46 | 1556 | os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr); |
e653b622 JM |
1557 | } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0); |
1558 | ||
1559 | return -1; | |
1560 | } | |
1561 | #endif /* CONFIG_AP */ | |
1562 | ||
1563 | ||
207ef3fb JM |
1564 | static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
1565 | { | |
1566 | return wpa_ctrl_command(ctrl, "SUSPEND"); | |
1567 | } | |
1568 | ||
1569 | ||
1570 | static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1571 | { | |
1572 | return wpa_ctrl_command(ctrl, "RESUME"); | |
1573 | } | |
1574 | ||
1575 | ||
32d5295f JM |
1576 | static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
1577 | { | |
1578 | return wpa_ctrl_command(ctrl, "DROP_SA"); | |
1579 | } | |
1580 | ||
1581 | ||
86d4f806 JM |
1582 | static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
1583 | { | |
1584 | char cmd[128]; | |
1585 | int res; | |
1586 | ||
1587 | if (argc != 1) { | |
1588 | printf("Invalid ROAM command: needs one argument " | |
1589 | "(target AP's BSSID)\n"); | |
1590 | return -1; | |
1591 | } | |
1592 | ||
1593 | res = os_snprintf(cmd, sizeof(cmd), "ROAM %s", argv[0]); | |
1594 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
1595 | printf("Too long ROAM command.\n"); | |
1596 | return -1; | |
1597 | } | |
1598 | return wpa_ctrl_command(ctrl, cmd); | |
1599 | } | |
1600 | ||
1601 | ||
57faa1ce JM |
1602 | #ifdef CONFIG_P2P |
1603 | ||
1604 | static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1605 | { | |
1606 | char cmd[128]; | |
1607 | int res; | |
1608 | ||
1609 | if (argc == 0) | |
1610 | return wpa_ctrl_command(ctrl, "P2P_FIND"); | |
1611 | ||
1612 | if (argc > 1) | |
1613 | res = os_snprintf(cmd, sizeof(cmd), "P2P_FIND %s %s", | |
1614 | argv[0], argv[1]); | |
1615 | else | |
1616 | res = os_snprintf(cmd, sizeof(cmd), "P2P_FIND %s", argv[0]); | |
1617 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1618 | return -1; | |
1619 | cmd[sizeof(cmd) - 1] = '\0'; | |
1620 | return wpa_ctrl_command(ctrl, cmd); | |
1621 | } | |
1622 | ||
1623 | ||
1624 | static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc, | |
1625 | char *argv[]) | |
1626 | { | |
1627 | return wpa_ctrl_command(ctrl, "P2P_STOP_FIND"); | |
1628 | } | |
1629 | ||
1630 | ||
1631 | static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc, | |
1632 | char *argv[]) | |
1633 | { | |
1634 | char cmd[128]; | |
1635 | int res; | |
1636 | ||
1637 | if (argc < 2) { | |
1638 | printf("Invalid P2P_CONNECT command: needs at least two " | |
1639 | "arguments (address and pbc/PIN)\n"); | |
1640 | return -1; | |
1641 | } | |
1642 | ||
1643 | if (argc > 4) | |
1644 | res = os_snprintf(cmd, sizeof(cmd), | |
1645 | "P2P_CONNECT %s %s %s %s %s", | |
1646 | argv[0], argv[1], argv[2], argv[3], | |
1647 | argv[4]); | |
1648 | else if (argc > 3) | |
1649 | res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s %s %s", | |
1650 | argv[0], argv[1], argv[2], argv[3]); | |
1651 | else if (argc > 2) | |
1652 | res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s %s", | |
1653 | argv[0], argv[1], argv[2]); | |
1654 | else | |
1655 | res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s", | |
1656 | argv[0], argv[1]); | |
1657 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1658 | return -1; | |
1659 | cmd[sizeof(cmd) - 1] = '\0'; | |
1660 | return wpa_ctrl_command(ctrl, cmd); | |
1661 | } | |
1662 | ||
1663 | ||
1664 | static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc, | |
1665 | char *argv[]) | |
1666 | { | |
1667 | char cmd[128]; | |
1668 | int res; | |
1669 | ||
1670 | if (argc == 0) | |
1671 | return wpa_ctrl_command(ctrl, "P2P_LISTEN"); | |
1672 | ||
1673 | res = os_snprintf(cmd, sizeof(cmd), "P2P_LISTEN %s", argv[0]); | |
1674 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1675 | return -1; | |
1676 | cmd[sizeof(cmd) - 1] = '\0'; | |
1677 | return wpa_ctrl_command(ctrl, cmd); | |
1678 | } | |
1679 | ||
1680 | ||
1681 | static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc, | |
1682 | char *argv[]) | |
1683 | { | |
1684 | char cmd[128]; | |
1685 | int res; | |
1686 | ||
1687 | if (argc != 1) { | |
1688 | printf("Invalid P2P_GROUP_REMOVE command: needs one argument " | |
1689 | "(interface name)\n"); | |
1690 | return -1; | |
1691 | } | |
1692 | ||
1693 | res = os_snprintf(cmd, sizeof(cmd), "P2P_GROUP_REMOVE %s", argv[0]); | |
1694 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1695 | return -1; | |
1696 | cmd[sizeof(cmd) - 1] = '\0'; | |
1697 | return wpa_ctrl_command(ctrl, cmd); | |
1698 | } | |
1699 | ||
1700 | ||
1701 | static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc, | |
1702 | char *argv[]) | |
1703 | { | |
1704 | char cmd[128]; | |
1705 | int res; | |
1706 | ||
1707 | if (argc == 0) | |
1708 | return wpa_ctrl_command(ctrl, "P2P_GROUP_ADD"); | |
1709 | ||
1710 | res = os_snprintf(cmd, sizeof(cmd), "P2P_GROUP_ADD %s", argv[0]); | |
1711 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1712 | return -1; | |
1713 | cmd[sizeof(cmd) - 1] = '\0'; | |
1714 | return wpa_ctrl_command(ctrl, cmd); | |
1715 | } | |
1716 | ||
1717 | ||
1718 | static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc, | |
1719 | char *argv[]) | |
1720 | { | |
1721 | char cmd[128]; | |
1722 | int res; | |
1723 | ||
1724 | if (argc != 2) { | |
1725 | printf("Invalid P2P_PROV_DISC command: needs two arguments " | |
1726 | "(address and config method\n" | |
1727 | "(display, keypad, or pbc)\n"); | |
1728 | return -1; | |
1729 | } | |
1730 | ||
1731 | res = os_snprintf(cmd, sizeof(cmd), "P2P_PROV_DISC %s %s", | |
1732 | argv[0], argv[1]); | |
1733 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1734 | return -1; | |
1735 | cmd[sizeof(cmd) - 1] = '\0'; | |
1736 | return wpa_ctrl_command(ctrl, cmd); | |
1737 | } | |
1738 | ||
1739 | ||
1740 | static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc, | |
1741 | char *argv[]) | |
1742 | { | |
1743 | return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE"); | |
1744 | } | |
1745 | ||
1746 | ||
1747 | static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc, | |
1748 | char *argv[]) | |
1749 | { | |
1750 | char cmd[4096]; | |
1751 | int res; | |
1752 | ||
1753 | if (argc != 2 && argc != 4) { | |
1754 | printf("Invalid P2P_SERV_DISC_REQ command: needs two " | |
1755 | "arguments (address and TLVs) or four arguments " | |
1756 | "(address, \"upnp\", version, search target " | |
1757 | "(SSDP ST:)\n"); | |
1758 | return -1; | |
1759 | } | |
1760 | ||
1761 | if (argc == 4) | |
1762 | res = os_snprintf(cmd, sizeof(cmd), | |
1763 | "P2P_SERV_DISC_REQ %s %s %s %s", | |
1764 | argv[0], argv[1], argv[2], argv[3]); | |
1765 | else | |
1766 | res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ %s %s", | |
1767 | argv[0], argv[1]); | |
1768 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1769 | return -1; | |
1770 | cmd[sizeof(cmd) - 1] = '\0'; | |
1771 | return wpa_ctrl_command(ctrl, cmd); | |
1772 | } | |
1773 | ||
1774 | ||
1775 | static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl, | |
1776 | int argc, char *argv[]) | |
1777 | { | |
1778 | char cmd[128]; | |
1779 | int res; | |
1780 | ||
1781 | if (argc != 1) { | |
1782 | printf("Invalid P2P_SERV_DISC_CANCEL_REQ command: needs one " | |
1783 | "argument (pending request identifier)\n"); | |
1784 | return -1; | |
1785 | } | |
1786 | ||
1787 | res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_CANCEL_REQ %s", | |
1788 | argv[0]); | |
1789 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1790 | return -1; | |
1791 | cmd[sizeof(cmd) - 1] = '\0'; | |
1792 | return wpa_ctrl_command(ctrl, cmd); | |
1793 | } | |
1794 | ||
1795 | ||
1796 | static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc, | |
1797 | char *argv[]) | |
1798 | { | |
1799 | char cmd[4096]; | |
1800 | int res; | |
1801 | ||
1802 | if (argc != 4) { | |
1803 | printf("Invalid P2P_SERV_DISC_RESP command: needs four " | |
1804 | "arguments (freq, address, dialog token, and TLVs)\n"); | |
1805 | return -1; | |
1806 | } | |
1807 | ||
1808 | res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s", | |
1809 | argv[0], argv[1], argv[2], argv[3]); | |
1810 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1811 | return -1; | |
1812 | cmd[sizeof(cmd) - 1] = '\0'; | |
1813 | return wpa_ctrl_command(ctrl, cmd); | |
1814 | } | |
1815 | ||
1816 | ||
1817 | static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc, | |
1818 | char *argv[]) | |
1819 | { | |
1820 | return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE"); | |
1821 | } | |
1822 | ||
1823 | ||
1824 | static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl, | |
1825 | int argc, char *argv[]) | |
1826 | { | |
1827 | char cmd[128]; | |
1828 | int res; | |
1829 | ||
1830 | if (argc != 1) { | |
1831 | printf("Invalid P2P_SERV_DISC_EXTERNAL command: needs one " | |
1832 | "argument (external processing: 0/1)\n"); | |
1833 | return -1; | |
1834 | } | |
1835 | ||
1836 | res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_EXTERNAL %s", | |
1837 | argv[0]); | |
1838 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1839 | return -1; | |
1840 | cmd[sizeof(cmd) - 1] = '\0'; | |
1841 | return wpa_ctrl_command(ctrl, cmd); | |
1842 | } | |
1843 | ||
1844 | ||
1845 | static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc, | |
1846 | char *argv[]) | |
1847 | { | |
1848 | return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH"); | |
1849 | } | |
1850 | ||
1851 | ||
1852 | static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc, | |
1853 | char *argv[]) | |
1854 | { | |
1855 | char cmd[4096]; | |
1856 | int res; | |
1857 | ||
1858 | if (argc != 3 && argc != 4) { | |
1859 | printf("Invalid P2P_SERVICE_ADD command: needs three or four " | |
1860 | "arguments\n"); | |
1861 | return -1; | |
1862 | } | |
1863 | ||
1864 | if (argc == 4) | |
1865 | res = os_snprintf(cmd, sizeof(cmd), | |
1866 | "P2P_SERVICE_ADD %s %s %s %s", | |
1867 | argv[0], argv[1], argv[2], argv[3]); | |
1868 | else | |
1869 | res = os_snprintf(cmd, sizeof(cmd), | |
1870 | "P2P_SERVICE_ADD %s %s %s", | |
1871 | argv[0], argv[1], argv[2]); | |
1872 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1873 | return -1; | |
1874 | cmd[sizeof(cmd) - 1] = '\0'; | |
1875 | return wpa_ctrl_command(ctrl, cmd); | |
1876 | } | |
1877 | ||
1878 | ||
1879 | static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc, | |
1880 | char *argv[]) | |
1881 | { | |
1882 | char cmd[4096]; | |
1883 | int res; | |
1884 | ||
1885 | if (argc != 2 && argc != 3) { | |
1886 | printf("Invalid P2P_SERVICE_DEL command: needs two or three " | |
1887 | "arguments\n"); | |
1888 | return -1; | |
1889 | } | |
1890 | ||
1891 | if (argc == 3) | |
1892 | res = os_snprintf(cmd, sizeof(cmd), | |
1893 | "P2P_SERVICE_DEL %s %s %s", | |
1894 | argv[0], argv[1], argv[2]); | |
1895 | else | |
1896 | res = os_snprintf(cmd, sizeof(cmd), | |
1897 | "P2P_SERVICE_DEL %s %s", | |
1898 | argv[0], argv[1]); | |
1899 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1900 | return -1; | |
1901 | cmd[sizeof(cmd) - 1] = '\0'; | |
1902 | return wpa_ctrl_command(ctrl, cmd); | |
1903 | } | |
1904 | ||
1905 | ||
1906 | static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl, | |
1907 | int argc, char *argv[]) | |
1908 | { | |
1909 | char cmd[128]; | |
1910 | int res; | |
1911 | ||
1912 | if (argc != 1) { | |
1913 | printf("Invalid P2P_REJECT command: needs one argument " | |
1914 | "(peer address)\n"); | |
1915 | return -1; | |
1916 | } | |
1917 | ||
1918 | res = os_snprintf(cmd, sizeof(cmd), "P2P_REJECT %s", argv[0]); | |
1919 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1920 | return -1; | |
1921 | cmd[sizeof(cmd) - 1] = '\0'; | |
1922 | return wpa_ctrl_command(ctrl, cmd); | |
1923 | } | |
1924 | ||
1925 | ||
1926 | static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl, | |
1927 | int argc, char *argv[]) | |
1928 | { | |
1929 | char cmd[128]; | |
1930 | int res; | |
1931 | ||
1932 | if (argc < 1) { | |
1933 | printf("Invalid P2P_INVITE command: needs at least one " | |
1934 | "argument\n"); | |
1935 | return -1; | |
1936 | } | |
1937 | ||
1938 | if (argc > 2) | |
1939 | res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s %s %s", | |
1940 | argv[0], argv[1], argv[2]); | |
1941 | else if (argc > 1) | |
1942 | res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s %s", | |
1943 | argv[0], argv[1]); | |
1944 | else | |
1945 | res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s", argv[0]); | |
1946 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
1947 | return -1; | |
1948 | cmd[sizeof(cmd) - 1] = '\0'; | |
1949 | return wpa_ctrl_command(ctrl, cmd); | |
1950 | } | |
1951 | ||
1952 | ||
1953 | static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
1954 | { | |
1955 | char buf[64]; | |
1956 | if (argc != 1) { | |
1957 | printf("Invalid 'p2p_peer' command - exactly one argument, " | |
1958 | "P2P peer device address, is required.\n"); | |
1959 | return -1; | |
1960 | } | |
1961 | os_snprintf(buf, sizeof(buf), "P2P_PEER %s", argv[0]); | |
1962 | return wpa_ctrl_command(ctrl, buf); | |
1963 | } | |
1964 | ||
1965 | ||
1966 | static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, char *cmd, | |
1967 | char *addr, size_t addr_len, | |
1968 | int discovered) | |
1969 | { | |
1970 | char buf[4096], *pos; | |
1971 | size_t len; | |
1972 | int ret; | |
1973 | ||
1974 | if (ctrl_conn == NULL) | |
1975 | return -1; | |
1976 | len = sizeof(buf) - 1; | |
1977 | ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, | |
1978 | wpa_cli_msg_cb); | |
1979 | if (ret == -2) { | |
1980 | printf("'%s' command timed out.\n", cmd); | |
1981 | return -2; | |
1982 | } else if (ret < 0) { | |
1983 | printf("'%s' command failed.\n", cmd); | |
1984 | return -1; | |
1985 | } | |
1986 | ||
1987 | buf[len] = '\0'; | |
1988 | if (memcmp(buf, "FAIL", 4) == 0) | |
1989 | return -1; | |
1990 | ||
1991 | pos = buf; | |
1992 | while (*pos != '\0' && *pos != '\n') | |
1993 | pos++; | |
1994 | *pos++ = '\0'; | |
1995 | os_strlcpy(addr, buf, addr_len); | |
1996 | if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL) | |
1997 | printf("%s\n", addr); | |
1998 | return 0; | |
1999 | } | |
2000 | ||
2001 | ||
2002 | static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
2003 | { | |
2004 | char addr[32], cmd[64]; | |
2005 | int discovered; | |
2006 | ||
2007 | discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0; | |
2008 | ||
2009 | if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST", | |
2010 | addr, sizeof(addr), discovered)) | |
2011 | return 0; | |
2012 | do { | |
2013 | os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr); | |
2014 | } while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr), | |
2015 | discovered) == 0); | |
2016 | ||
2017 | return -1; | |
2018 | } | |
2019 | ||
2020 | ||
2021 | static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
2022 | { | |
2023 | char cmd[100]; | |
2024 | int res; | |
2025 | ||
2026 | if (argc != 2) { | |
2027 | printf("Invalid P2P_SET command: needs two arguments (field, " | |
2028 | "value)\n"); | |
2029 | return -1; | |
2030 | } | |
2031 | ||
2032 | res = os_snprintf(cmd, sizeof(cmd), "P2P_SET %s %s", argv[0], argv[1]); | |
2033 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
2034 | return -1; | |
2035 | cmd[sizeof(cmd) - 1] = '\0'; | |
2036 | return wpa_ctrl_command(ctrl, cmd); | |
2037 | } | |
2038 | ||
2039 | ||
2040 | static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[]) | |
2041 | { | |
2042 | return wpa_ctrl_command(ctrl, "P2P_FLUSH"); | |
2043 | } | |
2044 | ||
2045 | ||
59eba7a2 JM |
2046 | static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc, |
2047 | char *argv[]) | |
2048 | { | |
2049 | return wpa_ctrl_command(ctrl, "P2P_CANCEL"); | |
2050 | } | |
2051 | ||
2052 | ||
9d562b79 SS |
2053 | static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc, |
2054 | char *argv[]) | |
2055 | { | |
2056 | char cmd[100]; | |
2057 | int res; | |
2058 | ||
2059 | if (argc != 1) { | |
2060 | printf("Invalid P2P_UNAUTHORIZE command: needs one argument " | |
2061 | "(peer address)\n"); | |
2062 | return -1; | |
2063 | } | |
2064 | ||
2065 | res = os_snprintf(cmd, sizeof(cmd), "P2P_UNAUTHORIZE %s", argv[0]); | |
2066 | ||
2067 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
2068 | return -1; | |
2069 | ||
2070 | cmd[sizeof(cmd) - 1] = '\0'; | |
2071 | return wpa_ctrl_command(ctrl, cmd); | |
2072 | } | |
2073 | ||
2074 | ||
57faa1ce JM |
2075 | static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl *ctrl, int argc, |
2076 | char *argv[]) | |
2077 | { | |
2078 | char cmd[100]; | |
2079 | int res; | |
2080 | ||
2081 | if (argc != 0 && argc != 2 && argc != 4) { | |
2082 | printf("Invalid P2P_PRESENCE_REQ command: needs two arguments " | |
2083 | "(preferred duration, interval; in microsecods).\n" | |
2084 | "Optional second pair can be used to provide " | |
2085 | "acceptable values.\n"); | |
2086 | return -1; | |
2087 | } | |
2088 | ||
2089 | if (argc == 4) | |
2090 | res = os_snprintf(cmd, sizeof(cmd), | |
2091 | "P2P_PRESENCE_REQ %s %s %s %s", | |
2092 | argv[0], argv[1], argv[2], argv[3]); | |
2093 | else if (argc == 2) | |
2094 | res = os_snprintf(cmd, sizeof(cmd), "P2P_PRESENCE_REQ %s %s", | |
2095 | argv[0], argv[1]); | |
2096 | else | |
2097 | res = os_snprintf(cmd, sizeof(cmd), "P2P_PRESENCE_REQ"); | |
2098 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
2099 | return -1; | |
2100 | cmd[sizeof(cmd) - 1] = '\0'; | |
2101 | return wpa_ctrl_command(ctrl, cmd); | |
2102 | } | |
2103 | ||
2104 | ||
2105 | static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc, | |
2106 | char *argv[]) | |
2107 | { | |
2108 | char cmd[100]; | |
2109 | int res; | |
2110 | ||
2111 | if (argc != 0 && argc != 2) { | |
2112 | printf("Invalid P2P_EXT_LISTEN command: needs two arguments " | |
2113 | "(availability period, availability interval; in " | |
2114 | "millisecods).\n" | |
2115 | "Extended Listen Timing can be cancelled with this " | |
2116 | "command when used without parameters.\n"); | |
2117 | return -1; | |
2118 | } | |
2119 | ||
2120 | if (argc == 2) | |
2121 | res = os_snprintf(cmd, sizeof(cmd), "P2P_EXT_LISTEN %s %s", | |
2122 | argv[0], argv[1]); | |
2123 | else | |
2124 | res = os_snprintf(cmd, sizeof(cmd), "P2P_EXT_LISTEN"); | |
2125 | if (res < 0 || (size_t) res >= sizeof(cmd)) | |
2126 | return -1; | |
2127 | cmd[sizeof(cmd) - 1] = '\0'; | |
2128 | return wpa_ctrl_command(ctrl, cmd); | |
2129 | } | |
2130 | ||
2131 | #endif /* CONFIG_P2P */ | |
2132 | ||
2133 | ||
0d0a8ca1 AC |
2134 | static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc, |
2135 | char *argv[]) | |
2136 | { | |
2137 | char cmd[256]; | |
2138 | int res; | |
2139 | ||
2140 | if (argc != 1) { | |
2141 | printf("Invalid STA_AUTOCONNECT command: needs one argument " | |
2142 | "(0/1 = disable/enable automatic reconnection)\n"); | |
2143 | return -1; | |
2144 | } | |
2145 | res = os_snprintf(cmd, sizeof(cmd), "STA_AUTOCONNECT %s", argv[0]); | |
2146 | if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { | |
2147 | printf("Too long STA_AUTOCONNECT command.\n"); | |
2148 | return -1; | |
2149 | } | |
2150 | return wpa_ctrl_command(ctrl, cmd); | |
2151 | } | |
2152 | ||
2153 | ||
40fd868c ER |
2154 | enum wpa_cli_cmd_flags { |
2155 | cli_cmd_flag_none = 0x00, | |
2156 | cli_cmd_flag_sensitive = 0x01 | |
2157 | }; | |
2158 | ||
6fc6879b JM |
2159 | struct wpa_cli_cmd { |
2160 | const char *cmd; | |
2161 | int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]); | |
40fd868c | 2162 | enum wpa_cli_cmd_flags flags; |
dfa141b1 | 2163 | const char *usage; |
6fc6879b JM |
2164 | }; |
2165 | ||
2166 | static struct wpa_cli_cmd wpa_cli_commands[] = { | |
40fd868c | 2167 | { "status", wpa_cli_cmd_status, |
dfa141b1 ER |
2168 | cli_cmd_flag_none, |
2169 | "[verbose] = get current WPA/EAPOL/EAP status" }, | |
40fd868c | 2170 | { "ping", wpa_cli_cmd_ping, |
dfa141b1 ER |
2171 | cli_cmd_flag_none, |
2172 | "= pings wpa_supplicant" }, | |
ac6912b5 BG |
2173 | { "relog", wpa_cli_cmd_relog, |
2174 | cli_cmd_flag_none, | |
2175 | "= re-open log-file (allow rolling logs)" }, | |
77895cd9 JM |
2176 | { "note", wpa_cli_cmd_note, |
2177 | cli_cmd_flag_none, | |
2178 | "<text> = add a note to wpa_supplicant debug log" }, | |
40fd868c | 2179 | { "mib", wpa_cli_cmd_mib, |
dfa141b1 ER |
2180 | cli_cmd_flag_none, |
2181 | "= get MIB variables (dot1x, dot11)" }, | |
40fd868c | 2182 | { "help", wpa_cli_cmd_help, |
dfa141b1 ER |
2183 | cli_cmd_flag_none, |
2184 | "= show this usage help" }, | |
40fd868c | 2185 | { "interface", wpa_cli_cmd_interface, |
dfa141b1 ER |
2186 | cli_cmd_flag_none, |
2187 | "[ifname] = show interfaces/select interface" }, | |
40fd868c | 2188 | { "level", wpa_cli_cmd_level, |
dfa141b1 ER |
2189 | cli_cmd_flag_none, |
2190 | "<debug level> = change debug level" }, | |
40fd868c | 2191 | { "license", wpa_cli_cmd_license, |
dfa141b1 ER |
2192 | cli_cmd_flag_none, |
2193 | "= show full wpa_cli license" }, | |
40fd868c | 2194 | { "quit", wpa_cli_cmd_quit, |
dfa141b1 ER |
2195 | cli_cmd_flag_none, |
2196 | "= exit wpa_cli" }, | |
40fd868c | 2197 | { "set", wpa_cli_cmd_set, |
dfa141b1 ER |
2198 | cli_cmd_flag_none, |
2199 | "= set variables (shows list of variables when run without " | |
2200 | "arguments)" }, | |
acec8d32 JM |
2201 | { "get", wpa_cli_cmd_get, |
2202 | cli_cmd_flag_none, | |
2203 | "<name> = get information" }, | |
40fd868c | 2204 | { "logon", wpa_cli_cmd_logon, |
dfa141b1 ER |
2205 | cli_cmd_flag_none, |
2206 | "= IEEE 802.1X EAPOL state machine logon" }, | |
40fd868c | 2207 | { "logoff", wpa_cli_cmd_logoff, |
dfa141b1 ER |
2208 | cli_cmd_flag_none, |
2209 | "= IEEE 802.1X EAPOL state machine logoff" }, | |
40fd868c | 2210 | { "pmksa", wpa_cli_cmd_pmksa, |
dfa141b1 ER |
2211 | cli_cmd_flag_none, |
2212 | "= show PMKSA cache" }, | |
40fd868c | 2213 | { "reassociate", wpa_cli_cmd_reassociate, |
dfa141b1 ER |
2214 | cli_cmd_flag_none, |
2215 | "= force reassociation" }, | |
40fd868c | 2216 | { "preauthenticate", wpa_cli_cmd_preauthenticate, |
dfa141b1 ER |
2217 | cli_cmd_flag_none, |
2218 | "<BSSID> = force preauthentication" }, | |
40fd868c | 2219 | { "identity", wpa_cli_cmd_identity, |
dfa141b1 ER |
2220 | cli_cmd_flag_none, |
2221 | "<network id> <identity> = configure identity for an SSID" }, | |
40fd868c | 2222 | { "password", wpa_cli_cmd_password, |
dfa141b1 ER |
2223 | cli_cmd_flag_sensitive, |
2224 | "<network id> <password> = configure password for an SSID" }, | |
40fd868c | 2225 | { "new_password", wpa_cli_cmd_new_password, |
dfa141b1 ER |
2226 | cli_cmd_flag_sensitive, |
2227 | "<network id> <password> = change password for an SSID" }, | |
40fd868c | 2228 | { "pin", wpa_cli_cmd_pin, |
dfa141b1 ER |
2229 | cli_cmd_flag_sensitive, |
2230 | "<network id> <pin> = configure pin for an SSID" }, | |
40fd868c | 2231 | { "otp", wpa_cli_cmd_otp, |
dfa141b1 ER |
2232 | cli_cmd_flag_sensitive, |
2233 | "<network id> <password> = configure one-time-password for an SSID" | |
2234 | }, | |
40fd868c | 2235 | { "passphrase", wpa_cli_cmd_passphrase, |
dfa141b1 ER |
2236 | cli_cmd_flag_sensitive, |
2237 | "<network id> <passphrase> = configure private key passphrase\n" | |
2238 | " for an SSID" }, | |
40fd868c | 2239 | { "bssid", wpa_cli_cmd_bssid, |
dfa141b1 ER |
2240 | cli_cmd_flag_none, |
2241 | "<network id> <BSSID> = set preferred BSSID for an SSID" }, | |
40fd868c | 2242 | { "list_networks", wpa_cli_cmd_list_networks, |
dfa141b1 ER |
2243 | cli_cmd_flag_none, |
2244 | "= list configured networks" }, | |
40fd868c | 2245 | { "select_network", wpa_cli_cmd_select_network, |
dfa141b1 ER |
2246 | cli_cmd_flag_none, |
2247 | "<network id> = select a network (disable others)" }, | |
40fd868c | 2248 | { "enable_network", wpa_cli_cmd_enable_network, |
dfa141b1 ER |
2249 | cli_cmd_flag_none, |
2250 | "<network id> = enable a network" }, | |
40fd868c | 2251 | { "disable_network", wpa_cli_cmd_disable_network, |
dfa141b1 ER |
2252 | cli_cmd_flag_none, |
2253 | "<network id> = disable a network" }, | |
40fd868c | 2254 | { "add_network", wpa_cli_cmd_add_network, |
dfa141b1 ER |
2255 | cli_cmd_flag_none, |
2256 | "= add a network" }, | |
40fd868c | 2257 | { "remove_network", wpa_cli_cmd_remove_network, |
dfa141b1 ER |
2258 | cli_cmd_flag_none, |
2259 | "<network id> = remove a network" }, | |
40fd868c | 2260 | { "set_network", wpa_cli_cmd_set_network, |
dfa141b1 ER |
2261 | cli_cmd_flag_sensitive, |
2262 | "<network id> <variable> <value> = set network variables (shows\n" | |
2263 | " list of variables when run without arguments)" }, | |
40fd868c | 2264 | { "get_network", wpa_cli_cmd_get_network, |
dfa141b1 ER |
2265 | cli_cmd_flag_none, |
2266 | "<network id> <variable> = get network variables" }, | |
40fd868c | 2267 | { "save_config", wpa_cli_cmd_save_config, |
dfa141b1 ER |
2268 | cli_cmd_flag_none, |
2269 | "= save the current configuration" }, | |
40fd868c | 2270 | { "disconnect", wpa_cli_cmd_disconnect, |
dfa141b1 ER |
2271 | cli_cmd_flag_none, |
2272 | "= disconnect and wait for reassociate/reconnect command before\n" | |
2273 | " connecting" }, | |
40fd868c | 2274 | { "reconnect", wpa_cli_cmd_reconnect, |
dfa141b1 ER |
2275 | cli_cmd_flag_none, |
2276 | "= like reassociate, but only takes effect if already disconnected" | |
2277 | }, | |
40fd868c | 2278 | { "scan", wpa_cli_cmd_scan, |
dfa141b1 ER |
2279 | cli_cmd_flag_none, |
2280 | "= request new BSS scan" }, | |
40fd868c | 2281 | { "scan_results", wpa_cli_cmd_scan_results, |
dfa141b1 ER |
2282 | cli_cmd_flag_none, |
2283 | "= get latest scan results" }, | |
40fd868c | 2284 | { "bss", wpa_cli_cmd_bss, |
dfa141b1 ER |
2285 | cli_cmd_flag_none, |
2286 | "<<idx> | <bssid>> = get detailed scan result info" }, | |
40fd868c | 2287 | { "get_capability", wpa_cli_cmd_get_capability, |
dfa141b1 ER |
2288 | cli_cmd_flag_none, |
2289 | "<eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies" }, | |
40fd868c | 2290 | { "reconfigure", wpa_cli_cmd_reconfigure, |
dfa141b1 ER |
2291 | cli_cmd_flag_none, |
2292 | "= force wpa_supplicant to re-read its configuration file" }, | |
40fd868c | 2293 | { "terminate", wpa_cli_cmd_terminate, |
dfa141b1 ER |
2294 | cli_cmd_flag_none, |
2295 | "= terminate wpa_supplicant" }, | |
40fd868c | 2296 | { "interface_add", wpa_cli_cmd_interface_add, |
dfa141b1 ER |
2297 | cli_cmd_flag_none, |
2298 | "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n" | |
2299 | " <bridge_name> = adds new interface, all parameters but <ifname>\n" | |
2300 | " are optional" }, | |
40fd868c | 2301 | { "interface_remove", wpa_cli_cmd_interface_remove, |
dfa141b1 ER |
2302 | cli_cmd_flag_none, |
2303 | "<ifname> = removes the interface" }, | |
40fd868c | 2304 | { "interface_list", wpa_cli_cmd_interface_list, |
dfa141b1 ER |
2305 | cli_cmd_flag_none, |
2306 | "= list available interfaces" }, | |
40fd868c | 2307 | { "ap_scan", wpa_cli_cmd_ap_scan, |
dfa141b1 ER |
2308 | cli_cmd_flag_none, |
2309 | "<value> = set ap_scan parameter" }, | |
40fd868c | 2310 | { "stkstart", wpa_cli_cmd_stkstart, |
dfa141b1 ER |
2311 | cli_cmd_flag_none, |
2312 | "<addr> = request STK negotiation with <addr>" }, | |
40fd868c | 2313 | { "ft_ds", wpa_cli_cmd_ft_ds, |
dfa141b1 ER |
2314 | cli_cmd_flag_none, |
2315 | "<addr> = request over-the-DS FT with <addr>" }, | |
40fd868c | 2316 | { "wps_pbc", wpa_cli_cmd_wps_pbc, |
dfa141b1 ER |
2317 | cli_cmd_flag_none, |
2318 | "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" }, | |
40fd868c | 2319 | { "wps_pin", wpa_cli_cmd_wps_pin, |
dfa141b1 ER |
2320 | cli_cmd_flag_sensitive, |
2321 | "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not " | |
2322 | "hardcoded)" }, | |
3981cb3c JM |
2323 | { "wps_check_pin", wpa_cli_cmd_wps_check_pin, |
2324 | cli_cmd_flag_sensitive, | |
2325 | "<PIN> = verify PIN checksum" }, | |
2f9929ff AC |
2326 | { "wps_cancel", wpa_cli_cmd_wps_cancel, cli_cmd_flag_none, |
2327 | "Cancels the pending WPS operation" }, | |
116f7bb0 | 2328 | #ifdef CONFIG_WPS_OOB |
46bdb83a MH |
2329 | { "wps_oob", wpa_cli_cmd_wps_oob, |
2330 | cli_cmd_flag_sensitive, | |
e1ee6b60 | 2331 | "<DEV_TYPE> <PATH> <METHOD> [DEV_NAME] = start WPS OOB" }, |
116f7bb0 | 2332 | #endif /* CONFIG_WPS_OOB */ |
40fd868c | 2333 | { "wps_reg", wpa_cli_cmd_wps_reg, |
dfa141b1 ER |
2334 | cli_cmd_flag_sensitive, |
2335 | "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" }, | |
70d84f11 JM |
2336 | { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin, |
2337 | cli_cmd_flag_sensitive, | |
2338 | "[params..] = enable/disable AP PIN" }, | |
e9bcfebf JM |
2339 | { "wps_er_start", wpa_cli_cmd_wps_er_start, |
2340 | cli_cmd_flag_none, | |
08486685 | 2341 | "[IP address] = start Wi-Fi Protected Setup External Registrar" }, |
e9bcfebf JM |
2342 | { "wps_er_stop", wpa_cli_cmd_wps_er_stop, |
2343 | cli_cmd_flag_none, | |
2344 | "= stop Wi-Fi Protected Setup External Registrar" }, | |
72df2f5f JM |
2345 | { "wps_er_pin", wpa_cli_cmd_wps_er_pin, |
2346 | cli_cmd_flag_sensitive, | |
2347 | "<UUID> <PIN> = add an Enrollee PIN to External Registrar" }, | |
564cd7fa JM |
2348 | { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc, |
2349 | cli_cmd_flag_none, | |
2350 | "<UUID> = accept an Enrollee PBC using External Registrar" }, | |
e64dcfd5 JM |
2351 | { "wps_er_learn", wpa_cli_cmd_wps_er_learn, |
2352 | cli_cmd_flag_sensitive, | |
2353 | "<UUID> <PIN> = learn AP configuration" }, | |
ef10f473 JM |
2354 | { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config, |
2355 | cli_cmd_flag_none, | |
2356 | "<UUID> <network id> = set AP configuration for enrolling" }, | |
7d6640a6 JM |
2357 | { "wps_er_config", wpa_cli_cmd_wps_er_config, |
2358 | cli_cmd_flag_sensitive, | |
2359 | "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" }, | |
11ef8d35 JM |
2360 | { "ibss_rsn", wpa_cli_cmd_ibss_rsn, |
2361 | cli_cmd_flag_none, | |
2362 | "<addr> = request RSN authentication with <addr> in IBSS" }, | |
e653b622 JM |
2363 | #ifdef CONFIG_AP |
2364 | { "sta", wpa_cli_cmd_sta, | |
2365 | cli_cmd_flag_none, | |
2366 | "<addr> = get information about an associated station (AP)" }, | |
2367 | { "all_sta", wpa_cli_cmd_all_sta, | |
2368 | cli_cmd_flag_none, | |
2369 | "= get information about all associated stations (AP)" }, | |
2370 | #endif /* CONFIG_AP */ | |
207ef3fb JM |
2371 | { "suspend", wpa_cli_cmd_suspend, cli_cmd_flag_none, |
2372 | "= notification of suspend/hibernate" }, | |
2373 | { "resume", wpa_cli_cmd_resume, cli_cmd_flag_none, | |
2374 | "= notification of resume/thaw" }, | |
32d5295f JM |
2375 | { "drop_sa", wpa_cli_cmd_drop_sa, cli_cmd_flag_none, |
2376 | "= drop SA without deauth/disassoc (test command)" }, | |
86d4f806 JM |
2377 | { "roam", wpa_cli_cmd_roam, |
2378 | cli_cmd_flag_none, | |
2379 | "<addr> = roam to the specified BSS" }, | |
57faa1ce JM |
2380 | #ifdef CONFIG_P2P |
2381 | { "p2p_find", wpa_cli_cmd_p2p_find, cli_cmd_flag_none, | |
2382 | "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" }, | |
2383 | { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, cli_cmd_flag_none, | |
2384 | "= stop P2P Devices search" }, | |
2385 | { "p2p_connect", wpa_cli_cmd_p2p_connect, cli_cmd_flag_none, | |
2386 | "<addr> <\"pbc\"|PIN> = connect to a P2P Devices" }, | |
2387 | { "p2p_listen", wpa_cli_cmd_p2p_listen, cli_cmd_flag_none, | |
2388 | "[timeout] = listen for P2P Devices for up-to timeout seconds" }, | |
2389 | { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove, cli_cmd_flag_none, | |
4d2ea6a6 | 2390 | "<ifname> = remove P2P group interface (terminate group if GO)" }, |
57faa1ce JM |
2391 | { "p2p_group_add", wpa_cli_cmd_p2p_group_add, cli_cmd_flag_none, |
2392 | "= add a new P2P group (local end as GO)" }, | |
2393 | { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc, cli_cmd_flag_none, | |
2394 | "<addr> <method> = request provisioning discovery" }, | |
2395 | { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase, | |
2396 | cli_cmd_flag_none, | |
2397 | "= get the passphrase for a group (GO only)" }, | |
2398 | { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req, | |
2399 | cli_cmd_flag_none, | |
2400 | "<addr> <TLVs> = schedule service discovery request" }, | |
2401 | { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req, | |
2402 | cli_cmd_flag_none, | |
2403 | "<id> = cancel pending service discovery request" }, | |
2404 | { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp, | |
2405 | cli_cmd_flag_none, | |
2406 | "<freq> <addr> <dialog token> <TLVs> = service discovery response" }, | |
2407 | { "p2p_service_update", wpa_cli_cmd_p2p_service_update, | |
2408 | cli_cmd_flag_none, | |
2409 | "= indicate change in local services" }, | |
2410 | { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external, | |
2411 | cli_cmd_flag_none, | |
2412 | "<external> = set external processing of service discovery" }, | |
2413 | { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush, | |
2414 | cli_cmd_flag_none, | |
2415 | "= remove all stored service entries" }, | |
2416 | { "p2p_service_add", wpa_cli_cmd_p2p_service_add, | |
2417 | cli_cmd_flag_none, | |
2418 | "<bonjour|upnp> <query|version> <response|service> = add a local " | |
2419 | "service" }, | |
2420 | { "p2p_service_del", wpa_cli_cmd_p2p_service_del, | |
2421 | cli_cmd_flag_none, | |
2422 | "<bonjour|upnp> <query|version> [|service] = remove a local " | |
2423 | "service" }, | |
2424 | { "p2p_reject", wpa_cli_cmd_p2p_reject, | |
2425 | cli_cmd_flag_none, | |
2426 | "<addr> = reject connection attempts from a specific peer" }, | |
2427 | { "p2p_invite", wpa_cli_cmd_p2p_invite, | |
2428 | cli_cmd_flag_none, | |
2429 | "<cmd> [peer=addr] = invite peer" }, | |
2430 | { "p2p_peers", wpa_cli_cmd_p2p_peers, cli_cmd_flag_none, | |
2431 | "[discovered] = list known (optionally, only fully discovered) P2P " | |
2432 | "peers" }, | |
2433 | { "p2p_peer", wpa_cli_cmd_p2p_peer, cli_cmd_flag_none, | |
2434 | "<address> = show information about known P2P peer" }, | |
2435 | { "p2p_set", wpa_cli_cmd_p2p_set, cli_cmd_flag_none, | |
2436 | "<field> <value> = set a P2P parameter" }, | |
2437 | { "p2p_flush", wpa_cli_cmd_p2p_flush, cli_cmd_flag_none, | |
2438 | "= flush P2P state" }, | |
59eba7a2 JM |
2439 | { "p2p_cancel", wpa_cli_cmd_p2p_cancel, cli_cmd_flag_none, |
2440 | "= cancel P2P group formation" }, | |
9d562b79 SS |
2441 | { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize, cli_cmd_flag_none, |
2442 | "<address> = unauthorize a peer" }, | |
57faa1ce JM |
2443 | { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, cli_cmd_flag_none, |
2444 | "[<duration> <interval>] [<duration> <interval>] = request GO " | |
2445 | "presence" }, | |
2446 | { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, cli_cmd_flag_none, | |
2447 | "[<period> <interval>] = set extended listen timing" }, | |
2448 | #endif /* CONFIG_P2P */ | |
0d0a8ca1 AC |
2449 | { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, cli_cmd_flag_none, |
2450 | "<0/1> = disable/enable automatic reconnection" }, | |
dfa141b1 | 2451 | { NULL, NULL, cli_cmd_flag_none, NULL } |
6fc6879b JM |
2452 | }; |
2453 | ||
2454 | ||
dfa141b1 ER |
2455 | /* |
2456 | * Prints command usage, lines are padded with the specified string. | |
2457 | */ | |
2458 | static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad) | |
2459 | { | |
2460 | char c; | |
2461 | size_t n; | |
2462 | ||
2463 | printf("%s%s ", pad, cmd->cmd); | |
2464 | for (n = 0; (c = cmd->usage[n]); n++) { | |
2465 | printf("%c", c); | |
2466 | if (c == '\n') | |
2467 | printf("%s", pad); | |
2468 | } | |
2469 | printf("\n"); | |
2470 | } | |
2471 | ||
2472 | ||
2473 | static void print_help(void) | |
2474 | { | |
2475 | int n; | |
2476 | printf("commands:\n"); | |
2477 | for (n = 0; wpa_cli_commands[n].cmd; n++) | |
2478 | print_cmd_help(&wpa_cli_commands[n], " "); | |
2479 | } | |
2480 | ||
2481 | ||
e8ecb5fb | 2482 | static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd) |
40fd868c ER |
2483 | { |
2484 | const char *c, *delim; | |
2485 | int n; | |
2486 | size_t len; | |
2487 | ||
2488 | delim = os_strchr(cmd, ' '); | |
2489 | if (delim) | |
2490 | len = delim - cmd; | |
2491 | else | |
2492 | len = os_strlen(cmd); | |
2493 | ||
2494 | for (n = 0; (c = wpa_cli_commands[n].cmd); n++) { | |
2495 | if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c)) | |
2496 | return (wpa_cli_commands[n].flags & | |
2497 | cli_cmd_flag_sensitive); | |
2498 | } | |
2499 | return 0; | |
2500 | } | |
e8ecb5fb JM |
2501 | |
2502 | ||
2503 | static char ** wpa_list_cmd_list(void) | |
2504 | { | |
2505 | char **res; | |
2506 | int i, count; | |
2507 | ||
2508 | count = sizeof(wpa_cli_commands) / sizeof(wpa_cli_commands[0]); | |
2509 | res = os_zalloc(count * sizeof(char *)); | |
2510 | if (res == NULL) | |
2511 | return NULL; | |
2512 | ||
2513 | for (i = 0; wpa_cli_commands[i].cmd; i++) { | |
2514 | res[i] = os_strdup(wpa_cli_commands[i].cmd); | |
2515 | if (res[i] == NULL) | |
2516 | break; | |
2517 | } | |
2518 | ||
2519 | return res; | |
2520 | } | |
2521 | ||
2522 | ||
2523 | static char ** wpa_cli_cmd_completion(const char *cmd, const char *str, | |
2524 | int pos) | |
2525 | { | |
2526 | int i; | |
2527 | ||
2528 | for (i = 0; wpa_cli_commands[i].cmd; i++) { | |
2529 | if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) { | |
2530 | edit_clear_line(); | |
2531 | printf("\r%s\n", wpa_cli_commands[i].usage); | |
2532 | edit_redraw(); | |
2533 | break; | |
2534 | } | |
2535 | } | |
2536 | ||
2537 | return NULL; | |
2538 | } | |
2539 | ||
2540 | ||
2541 | static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos) | |
2542 | { | |
2543 | char **res; | |
2544 | const char *end; | |
2545 | char *cmd; | |
2546 | ||
2547 | end = os_strchr(str, ' '); | |
2548 | if (end == NULL || str + pos < end) | |
2549 | return wpa_list_cmd_list(); | |
2550 | ||
2551 | cmd = os_malloc(pos + 1); | |
2552 | if (cmd == NULL) | |
2553 | return NULL; | |
2554 | os_memcpy(cmd, str, pos); | |
2555 | cmd[end - str] = '\0'; | |
2556 | res = wpa_cli_cmd_completion(cmd, str, pos); | |
2557 | os_free(cmd); | |
2558 | return res; | |
2559 | } | |
40fd868c ER |
2560 | |
2561 | ||
6fc6879b JM |
2562 | static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[]) |
2563 | { | |
2564 | struct wpa_cli_cmd *cmd, *match = NULL; | |
2565 | int count; | |
2566 | int ret = 0; | |
2567 | ||
2568 | count = 0; | |
2569 | cmd = wpa_cli_commands; | |
2570 | while (cmd->cmd) { | |
2571 | if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0) | |
2572 | { | |
2573 | match = cmd; | |
2574 | if (os_strcasecmp(cmd->cmd, argv[0]) == 0) { | |
2575 | /* we have an exact match */ | |
2576 | count = 1; | |
2577 | break; | |
2578 | } | |
2579 | count++; | |
2580 | } | |
2581 | cmd++; | |
2582 | } | |
2583 | ||
2584 | if (count > 1) { | |
2585 | printf("Ambiguous command '%s'; possible commands:", argv[0]); | |
2586 | cmd = wpa_cli_commands; | |
2587 | while (cmd->cmd) { | |
2588 | if (os_strncasecmp(cmd->cmd, argv[0], | |
2589 | os_strlen(argv[0])) == 0) { | |
2590 | printf(" %s", cmd->cmd); | |
2591 | } | |
2592 | cmd++; | |
2593 | } | |
2594 | printf("\n"); | |
2595 | ret = 1; | |
2596 | } else if (count == 0) { | |
2597 | printf("Unknown command '%s'\n", argv[0]); | |
2598 | ret = 1; | |
2599 | } else { | |
2600 | ret = match->handler(ctrl, argc - 1, &argv[1]); | |
2601 | } | |
2602 | ||
2603 | return ret; | |
2604 | } | |
2605 | ||
2606 | ||
2607 | static int str_match(const char *a, const char *b) | |
2608 | { | |
2609 | return os_strncmp(a, b, os_strlen(b)) == 0; | |
2610 | } | |
2611 | ||
2612 | ||
2613 | static int wpa_cli_exec(const char *program, const char *arg1, | |
2614 | const char *arg2) | |
2615 | { | |
2616 | char *cmd; | |
2617 | size_t len; | |
2618 | int res; | |
308a4ec8 | 2619 | int ret = 0; |
6fc6879b JM |
2620 | |
2621 | len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3; | |
2622 | cmd = os_malloc(len); | |
2623 | if (cmd == NULL) | |
2624 | return -1; | |
2625 | res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2); | |
2626 | if (res < 0 || (size_t) res >= len) { | |
2627 | os_free(cmd); | |
2628 | return -1; | |
2629 | } | |
2630 | cmd[len - 1] = '\0'; | |
2631 | #ifndef _WIN32_WCE | |
308a4ec8 JM |
2632 | if (system(cmd) < 0) |
2633 | ret = -1; | |
6fc6879b JM |
2634 | #endif /* _WIN32_WCE */ |
2635 | os_free(cmd); | |
2636 | ||
308a4ec8 | 2637 | return ret; |
6fc6879b JM |
2638 | } |
2639 | ||
2640 | ||
2641 | static void wpa_cli_action_process(const char *msg) | |
2642 | { | |
2643 | const char *pos; | |
2644 | char *copy = NULL, *id, *pos2; | |
2645 | ||
2646 | pos = msg; | |
2647 | if (*pos == '<') { | |
2648 | /* skip priority */ | |
2649 | pos = os_strchr(pos, '>'); | |
2650 | if (pos) | |
2651 | pos++; | |
2652 | else | |
2653 | pos = msg; | |
2654 | } | |
2655 | ||
2656 | if (str_match(pos, WPA_EVENT_CONNECTED)) { | |
2657 | int new_id = -1; | |
2658 | os_unsetenv("WPA_ID"); | |
2659 | os_unsetenv("WPA_ID_STR"); | |
2660 | os_unsetenv("WPA_CTRL_DIR"); | |
2661 | ||
2662 | pos = os_strstr(pos, "[id="); | |
2663 | if (pos) | |
2664 | copy = os_strdup(pos + 4); | |
2665 | ||
2666 | if (copy) { | |
2667 | pos2 = id = copy; | |
2668 | while (*pos2 && *pos2 != ' ') | |
2669 | pos2++; | |
2670 | *pos2++ = '\0'; | |
2671 | new_id = atoi(id); | |
2672 | os_setenv("WPA_ID", id, 1); | |
2673 | while (*pos2 && *pos2 != '=') | |
2674 | pos2++; | |
2675 | if (*pos2 == '=') | |
2676 | pos2++; | |
2677 | id = pos2; | |
2678 | while (*pos2 && *pos2 != ']') | |
2679 | pos2++; | |
2680 | *pos2 = '\0'; | |
2681 | os_setenv("WPA_ID_STR", id, 1); | |
2682 | os_free(copy); | |
2683 | } | |
2684 | ||
2685 | os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1); | |
2686 | ||
2687 | if (!wpa_cli_connected || new_id != wpa_cli_last_id) { | |
2688 | wpa_cli_connected = 1; | |
2689 | wpa_cli_last_id = new_id; | |
2690 | wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED"); | |
2691 | } | |
2692 | } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) { | |
2693 | if (wpa_cli_connected) { | |
2694 | wpa_cli_connected = 0; | |
2695 | wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED"); | |
2696 | } | |
42f0101b JM |
2697 | } else if (str_match(pos, P2P_EVENT_GROUP_STARTED)) { |
2698 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
2699 | } else if (str_match(pos, P2P_EVENT_GROUP_REMOVED)) { | |
2700 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
72044390 JM |
2701 | } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) { |
2702 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
2703 | } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) { | |
2704 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
876103dc AC |
2705 | } else if (str_match(pos, WPS_EVENT_SUCCESS)) { |
2706 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
2707 | } else if (str_match(pos, WPS_EVENT_FAIL)) { | |
2708 | wpa_cli_exec(action_file, ctrl_ifname, pos); | |
6fc6879b JM |
2709 | } else if (str_match(pos, WPA_EVENT_TERMINATING)) { |
2710 | printf("wpa_supplicant is terminating - stop monitoring\n"); | |
2711 | wpa_cli_quit = 1; | |
2712 | } | |
2713 | } | |
2714 | ||
2715 | ||
2716 | #ifndef CONFIG_ANSI_C_EXTRA | |
2717 | static void wpa_cli_action_cb(char *msg, size_t len) | |
2718 | { | |
2719 | wpa_cli_action_process(msg); | |
2720 | } | |
2721 | #endif /* CONFIG_ANSI_C_EXTRA */ | |
2722 | ||
2723 | ||
2724 | static void wpa_cli_reconnect(void) | |
2725 | { | |
2726 | wpa_cli_close_connection(); | |
4a3ade4e | 2727 | wpa_cli_open_connection(ctrl_ifname, 1); |
6fc6879b JM |
2728 | } |
2729 | ||
2730 | ||
cd101567 | 2731 | static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor) |
6fc6879b | 2732 | { |
6fc6879b JM |
2733 | if (ctrl_conn == NULL) { |
2734 | wpa_cli_reconnect(); | |
2735 | return; | |
2736 | } | |
2737 | while (wpa_ctrl_pending(ctrl) > 0) { | |
2738 | char buf[256]; | |
2739 | size_t len = sizeof(buf) - 1; | |
2740 | if (wpa_ctrl_recv(ctrl, buf, &len) == 0) { | |
2741 | buf[len] = '\0'; | |
2742 | if (action_monitor) | |
2743 | wpa_cli_action_process(buf); | |
2744 | else { | |
f3f0f648 | 2745 | if (wpa_cli_show_event(buf)) { |
82a855bd | 2746 | edit_clear_line(); |
cd101567 | 2747 | printf("\r%s\n", buf); |
bdc45634 | 2748 | edit_redraw(); |
f3f0f648 | 2749 | } |
6fc6879b JM |
2750 | } |
2751 | } else { | |
2752 | printf("Could not read pending message.\n"); | |
2753 | break; | |
2754 | } | |
2755 | } | |
2756 | ||
2757 | if (wpa_ctrl_pending(ctrl) < 0) { | |
2758 | printf("Connection to wpa_supplicant lost - trying to " | |
2759 | "reconnect\n"); | |
2760 | wpa_cli_reconnect(); | |
2761 | } | |
2762 | } | |
2763 | ||
6f1c6549 JM |
2764 | #define max_args 10 |
2765 | ||
2766 | static int tokenize_cmd(char *cmd, char *argv[]) | |
2767 | { | |
2768 | char *pos; | |
2769 | int argc = 0; | |
2770 | ||
2771 | pos = cmd; | |
2772 | for (;;) { | |
2773 | while (*pos == ' ') | |
2774 | pos++; | |
2775 | if (*pos == '\0') | |
2776 | break; | |
2777 | argv[argc] = pos; | |
2778 | argc++; | |
2779 | if (argc == max_args) | |
2780 | break; | |
2781 | if (*pos == '"') { | |
2782 | char *pos2 = os_strrchr(pos, '"'); | |
2783 | if (pos2) | |
2784 | pos = pos2 + 1; | |
2785 | } | |
2786 | while (*pos != '\0' && *pos != ' ') | |
2787 | pos++; | |
2788 | if (*pos == ' ') | |
2789 | *pos++ = '\0'; | |
2790 | } | |
2791 | ||
2792 | return argc; | |
2793 | } | |
2794 | ||
2795 | ||
cd101567 JM |
2796 | static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx) |
2797 | { | |
2798 | if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) { | |
2799 | printf("Connection to wpa_supplicant lost - trying to " | |
2800 | "reconnect\n"); | |
2801 | wpa_cli_close_connection(); | |
2802 | } | |
2803 | if (!ctrl_conn) | |
2804 | wpa_cli_reconnect(); | |
2805 | eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL); | |
2806 | } | |
2807 | ||
2808 | ||
2809 | static void wpa_cli_eloop_terminate(int sig, void *signal_ctx) | |
2810 | { | |
2811 | eloop_terminate(); | |
2812 | } | |
2813 | ||
2814 | ||
2815 | static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx) | |
2816 | { | |
2817 | wpa_cli_recv_pending(mon_conn, 0); | |
2818 | } | |
2819 | ||
2820 | ||
82a855bd | 2821 | static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd) |
aee680e8 JM |
2822 | { |
2823 | char *argv[max_args]; | |
2824 | int argc; | |
82a855bd | 2825 | argc = tokenize_cmd(cmd, argv); |
aee680e8 JM |
2826 | if (argc) |
2827 | wpa_request(ctrl_conn, argc, argv); | |
aee680e8 JM |
2828 | } |
2829 | ||
2830 | ||
82a855bd | 2831 | static void wpa_cli_edit_eof_cb(void *ctx) |
6f1c6549 | 2832 | { |
82a855bd | 2833 | eloop_terminate(); |
cd101567 JM |
2834 | } |
2835 | ||
2836 | ||
2837 | static void wpa_cli_interactive(void) | |
2838 | { | |
8953e968 | 2839 | char *home, *hfile = NULL; |
aee680e8 | 2840 | |
cd101567 | 2841 | printf("\nInteractive mode\n\n"); |
cd101567 | 2842 | |
8953e968 JM |
2843 | home = getenv("HOME"); |
2844 | if (home) { | |
2845 | const char *fname = ".wpa_cli_history"; | |
2846 | int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1; | |
2847 | hfile = os_malloc(hfile_len); | |
2848 | if (hfile) | |
2849 | os_snprintf(hfile, hfile_len, "%s/%s", home, fname); | |
2850 | } | |
2851 | ||
cd101567 | 2852 | eloop_register_signal_terminate(wpa_cli_eloop_terminate, NULL); |
8953e968 JM |
2853 | edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb, |
2854 | wpa_cli_edit_completion_cb, NULL, hfile); | |
cd101567 JM |
2855 | eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL); |
2856 | ||
cd101567 JM |
2857 | eloop_run(); |
2858 | ||
8953e968 JM |
2859 | edit_deinit(hfile, wpa_cli_edit_filter_history_cb); |
2860 | os_free(hfile); | |
cd101567 JM |
2861 | eloop_cancel_timeout(wpa_cli_ping, NULL, NULL); |
2862 | wpa_cli_close_connection(); | |
6f1c6549 JM |
2863 | } |
2864 | ||
6fc6879b JM |
2865 | |
2866 | static void wpa_cli_action(struct wpa_ctrl *ctrl) | |
2867 | { | |
2868 | #ifdef CONFIG_ANSI_C_EXTRA | |
2869 | /* TODO: ANSI C version(?) */ | |
2870 | printf("Action processing not supported in ANSI C build.\n"); | |
2871 | #else /* CONFIG_ANSI_C_EXTRA */ | |
2872 | fd_set rfds; | |
2873 | int fd, res; | |
2874 | struct timeval tv; | |
2875 | char buf[256]; /* note: large enough to fit in unsolicited messages */ | |
2876 | size_t len; | |
2877 | ||
2878 | fd = wpa_ctrl_get_fd(ctrl); | |
2879 | ||
2880 | while (!wpa_cli_quit) { | |
2881 | FD_ZERO(&rfds); | |
2882 | FD_SET(fd, &rfds); | |
1cc84c1c | 2883 | tv.tv_sec = ping_interval; |
6fc6879b JM |
2884 | tv.tv_usec = 0; |
2885 | res = select(fd + 1, &rfds, NULL, NULL, &tv); | |
2886 | if (res < 0 && errno != EINTR) { | |
2887 | perror("select"); | |
2888 | break; | |
2889 | } | |
2890 | ||
2891 | if (FD_ISSET(fd, &rfds)) | |
cd101567 | 2892 | wpa_cli_recv_pending(ctrl, 1); |
6fc6879b JM |
2893 | else { |
2894 | /* verify that connection is still working */ | |
2895 | len = sizeof(buf) - 1; | |
2896 | if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len, | |
2897 | wpa_cli_action_cb) < 0 || | |
2898 | len < 4 || os_memcmp(buf, "PONG", 4) != 0) { | |
2899 | printf("wpa_supplicant did not reply to PING " | |
2900 | "command - exiting\n"); | |
2901 | break; | |
2902 | } | |
2903 | } | |
2904 | } | |
2905 | #endif /* CONFIG_ANSI_C_EXTRA */ | |
2906 | } | |
2907 | ||
2908 | ||
2909 | static void wpa_cli_cleanup(void) | |
2910 | { | |
2911 | wpa_cli_close_connection(); | |
2912 | if (pid_file) | |
2913 | os_daemonize_terminate(pid_file); | |
2914 | ||
2915 | os_program_deinit(); | |
2916 | } | |
2917 | ||
2918 | static void wpa_cli_terminate(int sig) | |
2919 | { | |
2920 | wpa_cli_cleanup(); | |
2921 | exit(0); | |
2922 | } | |
2923 | ||
2924 | ||
6fc6879b JM |
2925 | static char * wpa_cli_get_default_ifname(void) |
2926 | { | |
2927 | char *ifname = NULL; | |
2928 | ||
2929 | #ifdef CONFIG_CTRL_IFACE_UNIX | |
2930 | struct dirent *dent; | |
2931 | DIR *dir = opendir(ctrl_iface_dir); | |
2932 | if (!dir) | |
2933 | return NULL; | |
2934 | while ((dent = readdir(dir))) { | |
2935 | #ifdef _DIRENT_HAVE_D_TYPE | |
2936 | /* | |
2937 | * Skip the file if it is not a socket. Also accept | |
2938 | * DT_UNKNOWN (0) in case the C library or underlying | |
2939 | * file system does not support d_type. | |
2940 | */ | |
2941 | if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) | |
2942 | continue; | |
2943 | #endif /* _DIRENT_HAVE_D_TYPE */ | |
2944 | if (os_strcmp(dent->d_name, ".") == 0 || | |
2945 | os_strcmp(dent->d_name, "..") == 0) | |
2946 | continue; | |
2947 | printf("Selected interface '%s'\n", dent->d_name); | |
2948 | ifname = os_strdup(dent->d_name); | |
2949 | break; | |
2950 | } | |
2951 | closedir(dir); | |
2952 | #endif /* CONFIG_CTRL_IFACE_UNIX */ | |
2953 | ||
2954 | #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE | |
2955 | char buf[2048], *pos; | |
2956 | size_t len; | |
2957 | struct wpa_ctrl *ctrl; | |
2958 | int ret; | |
2959 | ||
2960 | ctrl = wpa_ctrl_open(NULL); | |
2961 | if (ctrl == NULL) | |
2962 | return NULL; | |
2963 | ||
2964 | len = sizeof(buf) - 1; | |
2965 | ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL); | |
2966 | if (ret >= 0) { | |
2967 | buf[len] = '\0'; | |
2968 | pos = os_strchr(buf, '\n'); | |
2969 | if (pos) | |
2970 | *pos = '\0'; | |
2971 | ifname = os_strdup(buf); | |
2972 | } | |
2973 | wpa_ctrl_close(ctrl); | |
2974 | #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ | |
2975 | ||
2976 | return ifname; | |
2977 | } | |
2978 | ||
2979 | ||
2980 | int main(int argc, char *argv[]) | |
2981 | { | |
6fc6879b JM |
2982 | int warning_displayed = 0; |
2983 | int c; | |
2984 | int daemonize = 0; | |
2985 | int ret = 0; | |
2986 | const char *global = NULL; | |
2987 | ||
2988 | if (os_program_init()) | |
2989 | return -1; | |
2990 | ||
2991 | for (;;) { | |
1cc84c1c | 2992 | c = getopt(argc, argv, "a:Bg:G:hi:p:P:v"); |
6fc6879b JM |
2993 | if (c < 0) |
2994 | break; | |
2995 | switch (c) { | |
2996 | case 'a': | |
2997 | action_file = optarg; | |
2998 | break; | |
2999 | case 'B': | |
3000 | daemonize = 1; | |
3001 | break; | |
3002 | case 'g': | |
3003 | global = optarg; | |
3004 | break; | |
1cc84c1c JM |
3005 | case 'G': |
3006 | ping_interval = atoi(optarg); | |
3007 | break; | |
6fc6879b JM |
3008 | case 'h': |
3009 | usage(); | |
3010 | return 0; | |
3011 | case 'v': | |
3012 | printf("%s\n", wpa_cli_version); | |
3013 | return 0; | |
3014 | case 'i': | |
3015 | os_free(ctrl_ifname); | |
3016 | ctrl_ifname = os_strdup(optarg); | |
3017 | break; | |
3018 | case 'p': | |
3019 | ctrl_iface_dir = optarg; | |
3020 | break; | |
3021 | case 'P': | |
3022 | pid_file = optarg; | |
3023 | break; | |
3024 | default: | |
3025 | usage(); | |
3026 | return -1; | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | interactive = (argc == optind) && (action_file == NULL); | |
3031 | ||
3032 | if (interactive) | |
3033 | printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license); | |
3034 | ||
cd101567 JM |
3035 | if (eloop_init()) |
3036 | return -1; | |
3037 | ||
6fc6879b JM |
3038 | if (global) { |
3039 | #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE | |
3040 | ctrl_conn = wpa_ctrl_open(NULL); | |
3041 | #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */ | |
3042 | ctrl_conn = wpa_ctrl_open(global); | |
3043 | #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ | |
3044 | if (ctrl_conn == NULL) { | |
3045 | perror("Failed to connect to wpa_supplicant - " | |
3046 | "wpa_ctrl_open"); | |
3047 | return -1; | |
3048 | } | |
3049 | } | |
3050 | ||
6fc6879b JM |
3051 | #ifndef _WIN32_WCE |
3052 | signal(SIGINT, wpa_cli_terminate); | |
3053 | signal(SIGTERM, wpa_cli_terminate); | |
3054 | #endif /* _WIN32_WCE */ | |
6fc6879b | 3055 | |
4a3ade4e JM |
3056 | if (ctrl_ifname == NULL) |
3057 | ctrl_ifname = wpa_cli_get_default_ifname(); | |
3058 | ||
3059 | if (interactive) { | |
3060 | for (; !global;) { | |
3061 | if (wpa_cli_open_connection(ctrl_ifname, 1) == 0) { | |
3062 | if (warning_displayed) | |
3063 | printf("Connection established.\n"); | |
3064 | break; | |
3065 | } | |
3066 | ||
3067 | if (!warning_displayed) { | |
3068 | printf("Could not connect to wpa_supplicant - " | |
3069 | "re-trying\n"); | |
3070 | warning_displayed = 1; | |
3071 | } | |
3072 | os_sleep(1, 0); | |
3073 | continue; | |
3074 | } | |
3075 | } else { | |
3076 | if (!global && | |
3077 | wpa_cli_open_connection(ctrl_ifname, 0) < 0) { | |
3078 | perror("Failed to connect to wpa_supplicant - " | |
3079 | "wpa_ctrl_open"); | |
3080 | return -1; | |
3081 | } | |
3082 | ||
3083 | if (action_file) { | |
3084 | if (wpa_ctrl_attach(ctrl_conn) == 0) { | |
3085 | wpa_cli_attached = 1; | |
3086 | } else { | |
3087 | printf("Warning: Failed to attach to " | |
3088 | "wpa_supplicant.\n"); | |
6fc6879b | 3089 | return -1; |
4a3ade4e | 3090 | } |
6fc6879b JM |
3091 | } |
3092 | } | |
3093 | ||
3094 | if (daemonize && os_daemonize(pid_file)) | |
3095 | return -1; | |
3096 | ||
3097 | if (interactive) | |
3098 | wpa_cli_interactive(); | |
3099 | else if (action_file) | |
3100 | wpa_cli_action(ctrl_conn); | |
3101 | else | |
3102 | ret = wpa_request(ctrl_conn, argc - optind, &argv[optind]); | |
3103 | ||
3104 | os_free(ctrl_ifname); | |
cd101567 | 3105 | eloop_destroy(); |
6fc6879b JM |
3106 | wpa_cli_cleanup(); |
3107 | ||
3108 | return ret; | |
3109 | } | |
3110 | ||
3111 | #else /* CONFIG_CTRL_IFACE */ | |
3112 | int main(int argc, char *argv[]) | |
3113 | { | |
3114 | printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n"); | |
3115 | return -1; | |
3116 | } | |
3117 | #endif /* CONFIG_CTRL_IFACE */ |