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