]> git.ipfire.org Git - thirdparty/iw.git/blame - iw.c
allow wdev in place of dev
[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);
3bb116da 108 if (cmd->args)
bd663893
HS
109 printf(" %s", cmd->args);
110 printf("\n");
01ae06f9
JB
111
112 if (!full || !cmd->help)
113 return;
114
115 /* hack */
116 if (strlen(indent))
117 indent = "\t\t";
118 else
bd663893 119 printf("\n");
01ae06f9
JB
120
121 /* print line by line */
122 start = cmd->help;
123 end = strchr(start, '\0');
124 do {
125 lend = strchr(start, '\n');
126 if (!lend)
127 lend = end;
bd663893
HS
128 printf("%s", indent);
129 printf("%.*s\n", (int)(lend - start), start);
01ae06f9
JB
130 start = lend + 1;
131 } while (end != lend);
132
bd663893 133 printf("\n");
3bb116da
JB
134}
135
4f0cae73
JB
136static void usage_options(void)
137{
bd663893
HS
138 printf("Options:\n");
139 printf("\t--debug\t\tenable netlink debugging\n");
4f0cae73
JB
140}
141
01ae06f9
JB
142static const char *argv0;
143
f3ac8bf1 144static void usage(int argc, char **argv)
bd396f2a 145{
4698bfc2 146 const struct cmd *section, *cmd;
f3ac8bf1
JB
147 bool full = argc >= 0;
148 const char *sect_filt = NULL;
149 const char *cmd_filt = NULL;
150
151 if (argc > 0)
152 sect_filt = argv[0];
153
154 if (argc > 1)
155 cmd_filt = argv[1];
bd396f2a 156
bd663893 157 printf("Usage:\t%s [options] command\n", argv0);
4f0cae73 158 usage_options();
bd663893
HS
159 printf("\t--version\tshow version (%s)\n", iw_version);
160 printf("Commands:\n");
4698bfc2
JB
161 for_each_cmd(section) {
162 if (section->parent)
403b9c83 163 continue;
4698bfc2 164
f3ac8bf1
JB
165 if (sect_filt && strcmp(section->name, sect_filt))
166 continue;
167
4698bfc2
JB
168 if (section->handler && !section->hidden)
169 __usage_cmd(section, "\t", full);
170
171 for_each_cmd(cmd) {
172 if (section != cmd->parent)
173 continue;
174 if (!cmd->handler || cmd->hidden)
175 continue;
f3ac8bf1
JB
176 if (cmd_filt && strcmp(cmd->name, cmd_filt))
177 continue;
4698bfc2
JB
178 __usage_cmd(cmd, "\t", full);
179 }
bd396f2a 180 }
75f4204c
JB
181 printf("\nCommands that use the netdev ('dev') can also be given the\n"
182 "'wdev' instead to identify the device.\n");
bd663893 183 printf("\nYou can omit the 'phy' or 'dev' if "
f4ec76d0 184 "the identification is unique,\n"
8aefee9a 185 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
fbdb8d05
JB
186 "(Don't when scripting.)\n\n"
187 "Do NOT screenscrape this tool, we don't "
188 "consider its output stable.\n\n");
bd396f2a
JB
189}
190
01ae06f9
JB
191static int print_help(struct nl80211_state *state,
192 struct nl_cb *cb,
193 struct nl_msg *msg,
05514f95
JB
194 int argc, char **argv,
195 enum id_input id)
01ae06f9
JB
196{
197 exit(3);
198}
f3ac8bf1
JB
199TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
200 "Print usage for all or a specific command, e.g.\n"
201 "\"help wowlan\" or \"help wowlan enable\".");
01ae06f9 202
4698bfc2 203static void usage_cmd(const struct cmd *cmd)
4f0cae73 204{
bd663893 205 printf("Usage:\t%s [options] ", argv0);
01ae06f9 206 __usage_cmd(cmd, "", true);
4f0cae73
JB
207 usage_options();
208}
209
d711f013
JB
210static void version(void)
211{
133b069f 212 printf("iw version %s\n", iw_version);
d711f013
JB
213}
214
bd396f2a
JB
215static int phy_lookup(char *name)
216{
217 char buf[200];
218 int fd, pos;
219
220 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
221
222 fd = open(buf, O_RDONLY);
989e97c2
JB
223 if (fd < 0)
224 return -1;
bd396f2a 225 pos = read(fd, buf, sizeof(buf) - 1);
8f253ee2
ES
226 if (pos < 0) {
227 close(fd);
bd396f2a 228 return -1;
8f253ee2 229 }
bd396f2a 230 buf[pos] = '\0';
8f253ee2 231 close(fd);
bd396f2a
JB
232 return atoi(buf);
233}
234
70391ccf
JB
235static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
236 void *arg)
237{
238 int *ret = arg;
239 *ret = err->error;
240 return NL_STOP;
241}
242
561c5b7e
JB
243static int finish_handler(struct nl_msg *msg, void *arg)
244{
59c418c0
JB
245 int *ret = arg;
246 *ret = 0;
561c5b7e
JB
247 return NL_SKIP;
248}
249
250static int ack_handler(struct nl_msg *msg, void *arg)
70391ccf
JB
251{
252 int *ret = arg;
253 *ret = 0;
254 return NL_STOP;
255}
256
4f0cae73 257static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
4698bfc2 258 int argc, char **argv, const struct cmd **cmdout)
45c7212c 259{
4698bfc2 260 const struct cmd *cmd, *match = NULL, *sectcmd;
7c37a24d 261 struct nl_cb *cb;
5cb60f91 262 struct nl_cb *s_cb;
bd396f2a 263 struct nl_msg *msg;
290a3dd4 264 signed long long devidx = 0;
bb60b4ae 265 int err, o_argc;
bd396f2a 266 const char *command, *section;
bb60b4ae 267 char *tmp, **o_argv;
9927363c 268 enum command_identify_by command_idby = CIB_NONE;
45c7212c 269
9927363c 270 if (argc <= 1 && idby != II_NONE)
5e75fd04 271 return 1;
45c7212c 272
bb60b4ae
JB
273 o_argc = argc;
274 o_argv = argv;
275
bd396f2a 276 switch (idby) {
9927363c
JB
277 case II_PHY_IDX:
278 command_idby = CIB_PHY;
279 devidx = strtoul(*argv + 4, &tmp, 0);
280 if (*tmp != '\0')
281 return 1;
282 argc--;
283 argv++;
284 break;
285 case II_PHY_NAME:
286 command_idby = CIB_PHY;
bd396f2a
JB
287 devidx = phy_lookup(*argv);
288 argc--;
289 argv++;
290 break;
9927363c
JB
291 case II_NETDEV:
292 command_idby = CIB_NETDEV;
bd396f2a 293 devidx = if_nametoindex(*argv);
989e97c2
JB
294 if (devidx == 0)
295 devidx = -1;
bd396f2a
JB
296 argc--;
297 argv++;
298 break;
290a3dd4
JB
299 case II_WDEV:
300 command_idby = CIB_WDEV;
301 devidx = strtoll(*argv, &tmp, 0);
302 if (*tmp != '\0')
303 return 1;
304 argc--;
305 argv++;
bd396f2a
JB
306 default:
307 break;
308 }
309
989e97c2
JB
310 if (devidx < 0)
311 return -errno;
312
4698bfc2 313 section = *argv;
bd396f2a
JB
314 argc--;
315 argv++;
316
4698bfc2
JB
317 for_each_cmd(sectcmd) {
318 if (sectcmd->parent)
403b9c83 319 continue;
4698bfc2
JB
320 /* ok ... bit of a hack for the dupe 'info' section */
321 if (match && sectcmd->idby != command_idby)
bd396f2a 322 continue;
4698bfc2
JB
323 if (strcmp(sectcmd->name, section) == 0)
324 match = sectcmd;
bd396f2a 325 }
45c7212c 326
4698bfc2
JB
327 sectcmd = match;
328 match = NULL;
329 if (!sectcmd)
5e75fd04 330 return 1;
45c7212c 331
4698bfc2
JB
332 if (argc > 0) {
333 command = *argv;
334
335 for_each_cmd(cmd) {
336 if (!cmd->handler)
337 continue;
338 if (cmd->parent != sectcmd)
339 continue;
75f4204c
JB
340 /*
341 * ignore mismatch id by, but allow WDEV
342 * in place of NETDEV
343 */
344 if (cmd->idby != command_idby &&
345 !(cmd->idby == CIB_NETDEV &&
346 command_idby == CIB_WDEV))
4698bfc2
JB
347 continue;
348 if (strcmp(cmd->name, command))
349 continue;
350 if (argc > 1 && !cmd->args)
351 continue;
352 match = cmd;
353 break;
354 }
355
356 if (match) {
357 argc--;
358 argv++;
359 }
360 }
361
362 if (match)
363 cmd = match;
364 else {
365 /* Use the section itself, if possible. */
366 cmd = sectcmd;
367 if (argc && !cmd->args)
368 return 1;
75f4204c
JB
369 if (cmd->idby != command_idby &&
370 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
4698bfc2
JB
371 return 1;
372 if (!cmd->handler)
373 return 1;
374 }
375
1633ddf7
JB
376 if (cmd->selector) {
377 cmd = cmd->selector(argc, argv);
378 if (!cmd)
379 return 1;
380 }
381
4f0cae73
JB
382 if (cmdout)
383 *cmdout = cmd;
384
bb60b4ae
JB
385 if (!cmd->cmd) {
386 argc = o_argc;
387 argv = o_argv;
05514f95 388 return cmd->handler(state, NULL, NULL, argc, argv, idby);
bb60b4ae
JB
389 }
390
bd396f2a
JB
391 msg = nlmsg_alloc();
392 if (!msg) {
70391ccf
JB
393 fprintf(stderr, "failed to allocate netlink message\n");
394 return 2;
395 }
396
957a0f07 397 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
5cb60f91
SR
398 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
399 if (!cb || !s_cb) {
70391ccf
JB
400 fprintf(stderr, "failed to allocate netlink callbacks\n");
401 err = 2;
402 goto out_free_msg;
bd396f2a 403 }
45c7212c 404
f09cee6d 405 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
bd396f2a
JB
406 cmd->nl_msg_flags, cmd->cmd, 0);
407
9927363c 408 switch (command_idby) {
bd396f2a
JB
409 case CIB_PHY:
410 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
411 break;
412 case CIB_NETDEV:
413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
414 break;
290a3dd4
JB
415 case CIB_WDEV:
416 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
417 break;
bd396f2a
JB
418 default:
419 break;
420 }
421
05514f95 422 err = cmd->handler(state, cb, msg, argc, argv, idby);
70391ccf
JB
423 if (err)
424 goto out;
425
5cb60f91
SR
426 nl_socket_set_cb(state->nl_sock, s_cb);
427
57077d64 428 err = nl_send_auto_complete(state->nl_sock, msg);
70391ccf
JB
429 if (err < 0)
430 goto out;
431
c5c4471a
JB
432 err = 1;
433
70391ccf 434 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
59c418c0 435 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
561c5b7e 436 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
70391ccf 437
c5c4471a 438 while (err > 0)
57077d64 439 nl_recvmsgs(state->nl_sock, cb);
70391ccf
JB
440 out:
441 nl_cb_put(cb);
442 out_free_msg:
443 nlmsg_free(msg);
444 return err;
bd396f2a
JB
445 nla_put_failure:
446 fprintf(stderr, "building message failed\n");
70391ccf 447 return 2;
45c7212c
JB
448}
449
4f0cae73
JB
450int handle_cmd(struct nl80211_state *state, enum id_input idby,
451 int argc, char **argv)
452{
453 return __handle_cmd(state, idby, argc, argv, NULL);
454}
455
cad53b3f
JB
456int main(int argc, char **argv)
457{
458 struct nl80211_state nlstate;
bd396f2a 459 int err;
4698bfc2 460 const struct cmd *cmd = NULL;
cad53b3f 461
f408e01b 462 /* calculate command size including padding */
4698bfc2 463 cmd_size = abs((long)&__section_set - (long)&__section_get);
45c7212c
JB
464 /* strip off self */
465 argc--;
1cdd9016
MK
466 argv0 = *argv++;
467
59c49f09 468 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
957a0f07 469 iw_debug = 1;
59c49f09
JB
470 argc--;
471 argv++;
472 }
473
d711f013
JB
474 if (argc > 0 && strcmp(*argv, "--version") == 0) {
475 version();
476 return 0;
477 }
478
01ae06f9 479 /* need to treat "help" command specially so it works w/o nl80211 */
bd396f2a 480 if (argc == 0 || strcmp(*argv, "help") == 0) {
f3ac8bf1 481 usage(argc - 1, argv + 1);
4a972f80 482 return 0;
1cdd9016 483 }
45c7212c 484
2bdb6bd1
JB
485 err = nl80211_init(&nlstate);
486 if (err)
487 return 1;
488
957a0f07 489 if (strcmp(*argv, "dev") == 0 && argc > 1) {
14a0380d
LR
490 argc--;
491 argv++;
4f0cae73 492 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
811ec68f 493 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
9927363c
JB
494 if (strlen(*argv) == 3) {
495 argc--;
496 argv++;
4f0cae73 497 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
9927363c 498 } else if (*(*argv + 3) == '#')
4f0cae73 499 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
9927363c 500 else
f4ec76d0 501 goto detect;
290a3dd4
JB
502 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
503 argc--;
504 argv++;
505 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
f4ec76d0
JB
506 } else {
507 int idx;
508 enum id_input idby = II_NONE;
509 detect:
510 if ((idx = if_nametoindex(argv[0])) != 0)
511 idby = II_NETDEV;
66f8ca45 512 else if ((idx = phy_lookup(argv[0])) >= 0)
f4ec76d0
JB
513 idby = II_PHY_NAME;
514 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
515 }
45c7212c 516
4f0cae73
JB
517 if (err == 1) {
518 if (cmd)
01ae06f9 519 usage_cmd(cmd);
4f0cae73 520 else
f3ac8bf1 521 usage(0, NULL);
4f0cae73 522 } else if (err < 0)
b49be3e1 523 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
cad53b3f
JB
524
525 nl80211_cleanup(&nlstate);
526
45c7212c 527 return err;
cad53b3f 528}