]> git.ipfire.org Git - thirdparty/iw.git/blame - iw.c
allow multiple command variants
[thirdparty/iw.git] / iw.c
CommitLineData
cad53b3f
JB
1/*
2 * nl80211 userspace tool
3 *
2a1fced2 4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
cad53b3f
JB
5 */
6
7#include <errno.h>
8#include <stdio.h>
d5ac8ad3 9#include <string.h>
bd396f2a
JB
10#include <net/if.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <unistd.h>
27c49ed6 15#include <stdbool.h>
c551449a 16
cad53b3f
JB
17#include <netlink/genl/genl.h>
18#include <netlink/genl/family.h>
c551449a 19#include <netlink/genl/ctrl.h>
cad53b3f
JB
20#include <netlink/msg.h>
21#include <netlink/attr.h>
cad53b3f 22
f408e01b 23#include "nl80211.h"
cad53b3f
JB
24#include "iw.h"
25
ded667b0
YY
26/* libnl 1.x compatibility code */
27#if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
dfd13ee5
PE
28static inline struct nl_handle *nl_socket_alloc(void)
29{
30 return nl_handle_alloc();
31}
32
57077d64 33static inline void nl_socket_free(struct nl_sock *h)
dfd13ee5
PE
34{
35 nl_handle_destroy(h);
36}
ded667b0 37#endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
dfd13ee5 38
957a0f07 39int iw_debug = 0;
cad53b3f
JB
40
41static int nl80211_init(struct nl80211_state *state)
42{
43 int err;
44
57077d64
PE
45 state->nl_sock = nl_socket_alloc();
46 if (!state->nl_sock) {
47 fprintf(stderr, "Failed to allocate netlink socket.\n");
cad53b3f
JB
48 return -ENOMEM;
49 }
50
57077d64 51 if (genl_connect(state->nl_sock)) {
cad53b3f
JB
52 fprintf(stderr, "Failed to connect to generic netlink.\n");
53 err = -ENOLINK;
54 goto out_handle_destroy;
55 }
56
f09cee6d
JB
57 state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
58 if (state->nl80211_id < 0) {
cad53b3f
JB
59 fprintf(stderr, "nl80211 not found.\n");
60 err = -ENOENT;
f09cee6d 61 goto out_handle_destroy;
cad53b3f
JB
62 }
63
64 return 0;
65
cad53b3f 66 out_handle_destroy:
57077d64 67 nl_socket_free(state->nl_sock);
cad53b3f
JB
68 return err;
69}
70
71static void nl80211_cleanup(struct nl80211_state *state)
72{
57077d64 73 nl_socket_free(state->nl_sock);
cad53b3f
JB
74}
75
403b9c83
JB
76static int cmd_size;
77
4698bfc2
JB
78extern struct cmd __start___cmd;
79extern struct cmd __stop___cmd;
80
81#define for_each_cmd(_cmd) \
82 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
83 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
84
85
86static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
3bb116da 87{
01ae06f9
JB
88 const char *start, *lend, *end;
89
bd663893 90 printf("%s", indent);
4f0cae73 91
3bb116da
JB
92 switch (cmd->idby) {
93 case CIB_NONE:
3bb116da
JB
94 break;
95 case CIB_PHY:
bd663893 96 printf("phy <phyname> ");
3bb116da
JB
97 break;
98 case CIB_NETDEV:
bd663893 99 printf("dev <devname> ");
3bb116da 100 break;
290a3dd4
JB
101 case CIB_WDEV:
102 printf("wdev <idx> ");
103 break;
3bb116da 104 }
4698bfc2
JB
105 if (cmd->parent && cmd->parent->name)
106 printf("%s ", cmd->parent->name);
bd663893 107 printf("%s", cmd->name);
eb795501
JB
108
109 if (cmd->args) {
110 /* print line by line */
111 start = cmd->args;
112 end = strchr(start, '\0');
113 printf(" ");
114 do {
115 lend = strchr(start, '\n');
116 if (!lend)
117 lend = end;
118 if (start != cmd->args) {
119 printf("\t");
120 switch (cmd->idby) {
121 case CIB_NONE:
122 break;
123 case CIB_PHY:
124 printf("phy <phyname> ");
125 break;
126 case CIB_NETDEV:
127 printf("dev <devname> ");
128 break;
129 case CIB_WDEV:
130 printf("wdev <idx> ");
131 break;
132 }
133 if (cmd->parent && cmd->parent->name)
134 printf("%s ", cmd->parent->name);
135 printf("%s ", cmd->name);
136 }
137 printf("%.*s\n", (int)(lend - start), start);
138 start = lend + 1;
139 } while (end != lend);
140 } else
141 printf("\n");
01ae06f9
JB
142
143 if (!full || !cmd->help)
144 return;
145
146 /* hack */
147 if (strlen(indent))
148 indent = "\t\t";
149 else
bd663893 150 printf("\n");
01ae06f9
JB
151
152 /* print line by line */
153 start = cmd->help;
154 end = strchr(start, '\0');
155 do {
156 lend = strchr(start, '\n');
157 if (!lend)
158 lend = end;
bd663893
HS
159 printf("%s", indent);
160 printf("%.*s\n", (int)(lend - start), start);
01ae06f9
JB
161 start = lend + 1;
162 } while (end != lend);
163
bd663893 164 printf("\n");
3bb116da
JB
165}
166
4f0cae73
JB
167static void usage_options(void)
168{
bd663893
HS
169 printf("Options:\n");
170 printf("\t--debug\t\tenable netlink debugging\n");
4f0cae73
JB
171}
172
01ae06f9
JB
173static const char *argv0;
174
f3ac8bf1 175static void usage(int argc, char **argv)
bd396f2a 176{
4698bfc2 177 const struct cmd *section, *cmd;
f3ac8bf1
JB
178 bool full = argc >= 0;
179 const char *sect_filt = NULL;
180 const char *cmd_filt = NULL;
181
182 if (argc > 0)
183 sect_filt = argv[0];
184
185 if (argc > 1)
186 cmd_filt = argv[1];
bd396f2a 187
bd663893 188 printf("Usage:\t%s [options] command\n", argv0);
4f0cae73 189 usage_options();
bd663893
HS
190 printf("\t--version\tshow version (%s)\n", iw_version);
191 printf("Commands:\n");
4698bfc2
JB
192 for_each_cmd(section) {
193 if (section->parent)
403b9c83 194 continue;
4698bfc2 195
f3ac8bf1
JB
196 if (sect_filt && strcmp(section->name, sect_filt))
197 continue;
198
4698bfc2
JB
199 if (section->handler && !section->hidden)
200 __usage_cmd(section, "\t", full);
201
202 for_each_cmd(cmd) {
203 if (section != cmd->parent)
204 continue;
205 if (!cmd->handler || cmd->hidden)
206 continue;
f3ac8bf1
JB
207 if (cmd_filt && strcmp(cmd->name, cmd_filt))
208 continue;
4698bfc2
JB
209 __usage_cmd(cmd, "\t", full);
210 }
bd396f2a 211 }
75f4204c
JB
212 printf("\nCommands that use the netdev ('dev') can also be given the\n"
213 "'wdev' instead to identify the device.\n");
bd663893 214 printf("\nYou can omit the 'phy' or 'dev' if "
f4ec76d0 215 "the identification is unique,\n"
8aefee9a 216 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
fbdb8d05
JB
217 "(Don't when scripting.)\n\n"
218 "Do NOT screenscrape this tool, we don't "
219 "consider its output stable.\n\n");
bd396f2a
JB
220}
221
01ae06f9
JB
222static int print_help(struct nl80211_state *state,
223 struct nl_cb *cb,
224 struct nl_msg *msg,
05514f95
JB
225 int argc, char **argv,
226 enum id_input id)
01ae06f9
JB
227{
228 exit(3);
229}
f3ac8bf1
JB
230TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
231 "Print usage for all or a specific command, e.g.\n"
232 "\"help wowlan\" or \"help wowlan enable\".");
01ae06f9 233
4698bfc2 234static void usage_cmd(const struct cmd *cmd)
4f0cae73 235{
bd663893 236 printf("Usage:\t%s [options] ", argv0);
01ae06f9 237 __usage_cmd(cmd, "", true);
4f0cae73
JB
238 usage_options();
239}
240
d711f013
JB
241static void version(void)
242{
133b069f 243 printf("iw version %s\n", iw_version);
d711f013
JB
244}
245
bd396f2a
JB
246static int phy_lookup(char *name)
247{
248 char buf[200];
249 int fd, pos;
250
251 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
252
253 fd = open(buf, O_RDONLY);
989e97c2
JB
254 if (fd < 0)
255 return -1;
bd396f2a 256 pos = read(fd, buf, sizeof(buf) - 1);
8f253ee2
ES
257 if (pos < 0) {
258 close(fd);
bd396f2a 259 return -1;
8f253ee2 260 }
bd396f2a 261 buf[pos] = '\0';
8f253ee2 262 close(fd);
bd396f2a
JB
263 return atoi(buf);
264}
265
70391ccf
JB
266static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
267 void *arg)
268{
269 int *ret = arg;
270 *ret = err->error;
271 return NL_STOP;
272}
273
561c5b7e
JB
274static int finish_handler(struct nl_msg *msg, void *arg)
275{
59c418c0
JB
276 int *ret = arg;
277 *ret = 0;
561c5b7e
JB
278 return NL_SKIP;
279}
280
281static int ack_handler(struct nl_msg *msg, void *arg)
70391ccf
JB
282{
283 int *ret = arg;
284 *ret = 0;
285 return NL_STOP;
286}
287
4f0cae73 288static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
4698bfc2 289 int argc, char **argv, const struct cmd **cmdout)
45c7212c 290{
4698bfc2 291 const struct cmd *cmd, *match = NULL, *sectcmd;
7c37a24d 292 struct nl_cb *cb;
5cb60f91 293 struct nl_cb *s_cb;
bd396f2a 294 struct nl_msg *msg;
290a3dd4 295 signed long long devidx = 0;
bb60b4ae 296 int err, o_argc;
bd396f2a 297 const char *command, *section;
bb60b4ae 298 char *tmp, **o_argv;
9927363c 299 enum command_identify_by command_idby = CIB_NONE;
45c7212c 300
9927363c 301 if (argc <= 1 && idby != II_NONE)
5e75fd04 302 return 1;
45c7212c 303
bb60b4ae
JB
304 o_argc = argc;
305 o_argv = argv;
306
bd396f2a 307 switch (idby) {
9927363c
JB
308 case II_PHY_IDX:
309 command_idby = CIB_PHY;
310 devidx = strtoul(*argv + 4, &tmp, 0);
311 if (*tmp != '\0')
312 return 1;
313 argc--;
314 argv++;
315 break;
316 case II_PHY_NAME:
317 command_idby = CIB_PHY;
bd396f2a
JB
318 devidx = phy_lookup(*argv);
319 argc--;
320 argv++;
321 break;
9927363c
JB
322 case II_NETDEV:
323 command_idby = CIB_NETDEV;
bd396f2a 324 devidx = if_nametoindex(*argv);
989e97c2
JB
325 if (devidx == 0)
326 devidx = -1;
bd396f2a
JB
327 argc--;
328 argv++;
329 break;
290a3dd4
JB
330 case II_WDEV:
331 command_idby = CIB_WDEV;
332 devidx = strtoll(*argv, &tmp, 0);
333 if (*tmp != '\0')
334 return 1;
335 argc--;
336 argv++;
bd396f2a
JB
337 default:
338 break;
339 }
340
989e97c2
JB
341 if (devidx < 0)
342 return -errno;
343
4698bfc2 344 section = *argv;
bd396f2a
JB
345 argc--;
346 argv++;
347
4698bfc2
JB
348 for_each_cmd(sectcmd) {
349 if (sectcmd->parent)
403b9c83 350 continue;
4698bfc2
JB
351 /* ok ... bit of a hack for the dupe 'info' section */
352 if (match && sectcmd->idby != command_idby)
bd396f2a 353 continue;
4698bfc2
JB
354 if (strcmp(sectcmd->name, section) == 0)
355 match = sectcmd;
bd396f2a 356 }
45c7212c 357
4698bfc2
JB
358 sectcmd = match;
359 match = NULL;
360 if (!sectcmd)
5e75fd04 361 return 1;
45c7212c 362
4698bfc2
JB
363 if (argc > 0) {
364 command = *argv;
365
366 for_each_cmd(cmd) {
367 if (!cmd->handler)
368 continue;
369 if (cmd->parent != sectcmd)
370 continue;
75f4204c
JB
371 /*
372 * ignore mismatch id by, but allow WDEV
373 * in place of NETDEV
374 */
375 if (cmd->idby != command_idby &&
376 !(cmd->idby == CIB_NETDEV &&
377 command_idby == CIB_WDEV))
4698bfc2
JB
378 continue;
379 if (strcmp(cmd->name, command))
380 continue;
381 if (argc > 1 && !cmd->args)
382 continue;
383 match = cmd;
384 break;
385 }
386
387 if (match) {
388 argc--;
389 argv++;
390 }
391 }
392
393 if (match)
394 cmd = match;
395 else {
396 /* Use the section itself, if possible. */
397 cmd = sectcmd;
398 if (argc && !cmd->args)
399 return 1;
75f4204c
JB
400 if (cmd->idby != command_idby &&
401 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
4698bfc2
JB
402 return 1;
403 if (!cmd->handler)
404 return 1;
405 }
406
1633ddf7
JB
407 if (cmd->selector) {
408 cmd = cmd->selector(argc, argv);
409 if (!cmd)
410 return 1;
411 }
412
4f0cae73
JB
413 if (cmdout)
414 *cmdout = cmd;
415
bb60b4ae
JB
416 if (!cmd->cmd) {
417 argc = o_argc;
418 argv = o_argv;
05514f95 419 return cmd->handler(state, NULL, NULL, argc, argv, idby);
bb60b4ae
JB
420 }
421
bd396f2a
JB
422 msg = nlmsg_alloc();
423 if (!msg) {
70391ccf
JB
424 fprintf(stderr, "failed to allocate netlink message\n");
425 return 2;
426 }
427
957a0f07 428 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
5cb60f91
SR
429 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
430 if (!cb || !s_cb) {
70391ccf
JB
431 fprintf(stderr, "failed to allocate netlink callbacks\n");
432 err = 2;
433 goto out_free_msg;
bd396f2a 434 }
45c7212c 435
f09cee6d 436 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
bd396f2a
JB
437 cmd->nl_msg_flags, cmd->cmd, 0);
438
9927363c 439 switch (command_idby) {
bd396f2a
JB
440 case CIB_PHY:
441 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
442 break;
443 case CIB_NETDEV:
444 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
445 break;
290a3dd4
JB
446 case CIB_WDEV:
447 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
448 break;
bd396f2a
JB
449 default:
450 break;
451 }
452
05514f95 453 err = cmd->handler(state, cb, msg, argc, argv, idby);
70391ccf
JB
454 if (err)
455 goto out;
456
5cb60f91
SR
457 nl_socket_set_cb(state->nl_sock, s_cb);
458
57077d64 459 err = nl_send_auto_complete(state->nl_sock, msg);
70391ccf
JB
460 if (err < 0)
461 goto out;
462
c5c4471a
JB
463 err = 1;
464
70391ccf 465 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
59c418c0 466 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
561c5b7e 467 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
70391ccf 468
c5c4471a 469 while (err > 0)
57077d64 470 nl_recvmsgs(state->nl_sock, cb);
70391ccf
JB
471 out:
472 nl_cb_put(cb);
473 out_free_msg:
474 nlmsg_free(msg);
475 return err;
bd396f2a
JB
476 nla_put_failure:
477 fprintf(stderr, "building message failed\n");
70391ccf 478 return 2;
45c7212c
JB
479}
480
4f0cae73
JB
481int handle_cmd(struct nl80211_state *state, enum id_input idby,
482 int argc, char **argv)
483{
484 return __handle_cmd(state, idby, argc, argv, NULL);
485}
486
cad53b3f
JB
487int main(int argc, char **argv)
488{
489 struct nl80211_state nlstate;
bd396f2a 490 int err;
4698bfc2 491 const struct cmd *cmd = NULL;
cad53b3f 492
f408e01b 493 /* calculate command size including padding */
4698bfc2 494 cmd_size = abs((long)&__section_set - (long)&__section_get);
45c7212c
JB
495 /* strip off self */
496 argc--;
1cdd9016
MK
497 argv0 = *argv++;
498
59c49f09 499 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
957a0f07 500 iw_debug = 1;
59c49f09
JB
501 argc--;
502 argv++;
503 }
504
d711f013
JB
505 if (argc > 0 && strcmp(*argv, "--version") == 0) {
506 version();
507 return 0;
508 }
509
01ae06f9 510 /* need to treat "help" command specially so it works w/o nl80211 */
bd396f2a 511 if (argc == 0 || strcmp(*argv, "help") == 0) {
f3ac8bf1 512 usage(argc - 1, argv + 1);
4a972f80 513 return 0;
1cdd9016 514 }
45c7212c 515
2bdb6bd1
JB
516 err = nl80211_init(&nlstate);
517 if (err)
518 return 1;
519
957a0f07 520 if (strcmp(*argv, "dev") == 0 && argc > 1) {
14a0380d
LR
521 argc--;
522 argv++;
4f0cae73 523 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
811ec68f 524 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
9927363c
JB
525 if (strlen(*argv) == 3) {
526 argc--;
527 argv++;
4f0cae73 528 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
9927363c 529 } else if (*(*argv + 3) == '#')
4f0cae73 530 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
9927363c 531 else
f4ec76d0 532 goto detect;
290a3dd4
JB
533 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
534 argc--;
535 argv++;
536 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
f4ec76d0
JB
537 } else {
538 int idx;
539 enum id_input idby = II_NONE;
540 detect:
541 if ((idx = if_nametoindex(argv[0])) != 0)
542 idby = II_NETDEV;
66f8ca45 543 else if ((idx = phy_lookup(argv[0])) >= 0)
f4ec76d0
JB
544 idby = II_PHY_NAME;
545 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
546 }
45c7212c 547
4f0cae73
JB
548 if (err == 1) {
549 if (cmd)
01ae06f9 550 usage_cmd(cmd);
4f0cae73 551 else
f3ac8bf1 552 usage(0, NULL);
4f0cae73 553 } else if (err < 0)
b49be3e1 554 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
cad53b3f
JB
555
556 nl80211_cleanup(&nlstate);
557
45c7212c 558 return err;
cad53b3f 559}