]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
iw: fix cmd_size determination with LTO
[thirdparty/iw.git] / iw.c
1 /*
2 * nl80211 userspace tool
3 *
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <net/if.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <stdbool.h>
16 #include <linux/netlink.h>
17
18 #include <netlink/genl/genl.h>
19 #include <netlink/genl/family.h>
20 #include <netlink/genl/ctrl.h>
21 #include <netlink/msg.h>
22 #include <netlink/attr.h>
23
24 #include "nl80211.h"
25 #include "iw.h"
26
27 /* libnl 1.x compatibility code */
28 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
29 static inline struct nl_handle *nl_socket_alloc(void)
30 {
31 return nl_handle_alloc();
32 }
33
34 static inline void nl_socket_free(struct nl_sock *h)
35 {
36 nl_handle_destroy(h);
37 }
38
39 static inline int nl_socket_set_buffer_size(struct nl_sock *sk,
40 int rxbuf, int txbuf)
41 {
42 return nl_set_buffer_size(sk, rxbuf, txbuf);
43 }
44 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
45
46 int iw_debug = 0;
47
48 static int nl80211_init(struct nl80211_state *state)
49 {
50 int err;
51
52 state->nl_sock = nl_socket_alloc();
53 if (!state->nl_sock) {
54 fprintf(stderr, "Failed to allocate netlink socket.\n");
55 return -ENOMEM;
56 }
57
58 if (genl_connect(state->nl_sock)) {
59 fprintf(stderr, "Failed to connect to generic netlink.\n");
60 err = -ENOLINK;
61 goto out_handle_destroy;
62 }
63
64 nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
65
66 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
67 err = 1;
68 setsockopt(nl_socket_get_fd(state->nl_sock), SOL_NETLINK,
69 NETLINK_EXT_ACK, &err, sizeof(err));
70
71 state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
72 if (state->nl80211_id < 0) {
73 fprintf(stderr, "nl80211 not found.\n");
74 err = -ENOENT;
75 goto out_handle_destroy;
76 }
77
78 return 0;
79
80 out_handle_destroy:
81 nl_socket_free(state->nl_sock);
82 return err;
83 }
84
85 static void nl80211_cleanup(struct nl80211_state *state)
86 {
87 nl_socket_free(state->nl_sock);
88 }
89
90 static int cmd_size;
91
92 extern struct cmd __start___cmd;
93 extern struct cmd __stop___cmd;
94
95 #define for_each_cmd(_cmd) \
96 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
97 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
98
99
100 static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
101 {
102 const char *start, *lend, *end;
103
104 printf("%s", indent);
105
106 switch (cmd->idby) {
107 case CIB_NONE:
108 break;
109 case CIB_PHY:
110 printf("phy <phyname> ");
111 break;
112 case CIB_NETDEV:
113 printf("dev <devname> ");
114 break;
115 case CIB_WDEV:
116 printf("wdev <idx> ");
117 break;
118 }
119 if (cmd->parent && cmd->parent->name)
120 printf("%s ", cmd->parent->name);
121 printf("%s", cmd->name);
122
123 if (cmd->args) {
124 /* print line by line */
125 start = cmd->args;
126 end = strchr(start, '\0');
127 printf(" ");
128 do {
129 lend = strchr(start, '\n');
130 if (!lend)
131 lend = end;
132 if (start != cmd->args) {
133 printf("\t");
134 switch (cmd->idby) {
135 case CIB_NONE:
136 break;
137 case CIB_PHY:
138 printf("phy <phyname> ");
139 break;
140 case CIB_NETDEV:
141 printf("dev <devname> ");
142 break;
143 case CIB_WDEV:
144 printf("wdev <idx> ");
145 break;
146 }
147 if (cmd->parent && cmd->parent->name)
148 printf("%s ", cmd->parent->name);
149 printf("%s ", cmd->name);
150 }
151 printf("%.*s\n", (int)(lend - start), start);
152 start = lend + 1;
153 } while (end != lend);
154 } else
155 printf("\n");
156
157 if (!full || !cmd->help)
158 return;
159
160 /* hack */
161 if (strlen(indent))
162 indent = "\t\t";
163 else
164 printf("\n");
165
166 /* print line by line */
167 start = cmd->help;
168 end = strchr(start, '\0');
169 do {
170 lend = strchr(start, '\n');
171 if (!lend)
172 lend = end;
173 printf("%s", indent);
174 printf("%.*s\n", (int)(lend - start), start);
175 start = lend + 1;
176 } while (end != lend);
177
178 printf("\n");
179 }
180
181 static void usage_options(void)
182 {
183 printf("Options:\n");
184 printf("\t--debug\t\tenable netlink debugging\n");
185 }
186
187 static const char *argv0;
188
189 static void usage(int argc, char **argv)
190 {
191 const struct cmd *section, *cmd;
192 bool full = argc >= 0;
193 const char *sect_filt = NULL;
194 const char *cmd_filt = NULL;
195
196 if (argc > 0)
197 sect_filt = argv[0];
198
199 if (argc > 1)
200 cmd_filt = argv[1];
201
202 printf("Usage:\t%s [options] command\n", argv0);
203 usage_options();
204 printf("\t--version\tshow version (%s)\n", iw_version);
205 printf("Commands:\n");
206 for_each_cmd(section) {
207 if (section->parent)
208 continue;
209
210 if (sect_filt && strcmp(section->name, sect_filt))
211 continue;
212
213 if (section->handler && !section->hidden)
214 __usage_cmd(section, "\t", full);
215
216 for_each_cmd(cmd) {
217 if (section != cmd->parent)
218 continue;
219 if (!cmd->handler || cmd->hidden)
220 continue;
221 if (cmd_filt && strcmp(cmd->name, cmd_filt))
222 continue;
223 __usage_cmd(cmd, "\t", full);
224 }
225 }
226 printf("\nCommands that use the netdev ('dev') can also be given the\n"
227 "'wdev' instead to identify the device.\n");
228 printf("\nYou can omit the 'phy' or 'dev' if "
229 "the identification is unique,\n"
230 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
231 "(Don't when scripting.)\n\n"
232 "Do NOT screenscrape this tool, we don't "
233 "consider its output stable.\n\n");
234 }
235
236 static int print_help(struct nl80211_state *state,
237 struct nl_msg *msg,
238 int argc, char **argv,
239 enum id_input id)
240 {
241 exit(3);
242 }
243 TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
244 "Print usage for all or a specific command, e.g.\n"
245 "\"help wowlan\" or \"help wowlan enable\".");
246
247 static void usage_cmd(const struct cmd *cmd)
248 {
249 printf("Usage:\t%s [options] ", argv0);
250 __usage_cmd(cmd, "", true);
251 usage_options();
252 }
253
254 static void version(void)
255 {
256 printf("iw version %s\n", iw_version);
257 }
258
259 static int phy_lookup(char *name)
260 {
261 char buf[200];
262 int fd, pos;
263
264 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
265
266 fd = open(buf, O_RDONLY);
267 if (fd < 0)
268 return -1;
269 pos = read(fd, buf, sizeof(buf) - 1);
270 if (pos < 0) {
271 close(fd);
272 return -1;
273 }
274 buf[pos] = '\0';
275 close(fd);
276 return atoi(buf);
277 }
278
279 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
280 void *arg)
281 {
282 struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1;
283 int len = nlh->nlmsg_len;
284 struct nlattr *attrs;
285 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
286 int *ret = arg;
287 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
288
289 *ret = err->error;
290
291 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
292 return NL_STOP;
293
294 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
295 ack_len += err->msg.nlmsg_len - sizeof(*nlh);
296
297 if (len <= ack_len)
298 return NL_STOP;
299
300 attrs = (void *)((unsigned char *)nlh + ack_len);
301 len -= ack_len;
302
303 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
304 if (tb[NLMSGERR_ATTR_MSG]) {
305 len = strnlen((char *)nla_data(tb[NLMSGERR_ATTR_MSG]),
306 nla_len(tb[NLMSGERR_ATTR_MSG]));
307 fprintf(stderr, "kernel reports: %*s\n", len,
308 (char *)nla_data(tb[NLMSGERR_ATTR_MSG]));
309 }
310
311 return NL_STOP;
312 }
313
314 static int finish_handler(struct nl_msg *msg, void *arg)
315 {
316 int *ret = arg;
317 *ret = 0;
318 return NL_SKIP;
319 }
320
321 static int ack_handler(struct nl_msg *msg, void *arg)
322 {
323 int *ret = arg;
324 *ret = 0;
325 return NL_STOP;
326 }
327
328 static int (*registered_handler)(struct nl_msg *, void *);
329 static void *registered_handler_data;
330
331 void register_handler(int (*handler)(struct nl_msg *, void *), void *data)
332 {
333 registered_handler = handler;
334 registered_handler_data = data;
335 }
336
337 int valid_handler(struct nl_msg *msg, void *arg)
338 {
339 if (registered_handler)
340 return registered_handler(msg, registered_handler_data);
341
342 return NL_OK;
343 }
344
345 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
346 int argc, char **argv, const struct cmd **cmdout)
347 {
348 const struct cmd *cmd, *match = NULL, *sectcmd;
349 struct nl_cb *cb;
350 struct nl_cb *s_cb;
351 struct nl_msg *msg;
352 signed long long devidx = 0;
353 int err, o_argc;
354 const char *command, *section;
355 char *tmp, **o_argv;
356 enum command_identify_by command_idby = CIB_NONE;
357
358 if (argc <= 1 && idby != II_NONE)
359 return 1;
360
361 o_argc = argc;
362 o_argv = argv;
363
364 switch (idby) {
365 case II_PHY_IDX:
366 command_idby = CIB_PHY;
367 devidx = strtoul(*argv + 4, &tmp, 0);
368 if (*tmp != '\0')
369 return 1;
370 argc--;
371 argv++;
372 break;
373 case II_PHY_NAME:
374 command_idby = CIB_PHY;
375 devidx = phy_lookup(*argv);
376 argc--;
377 argv++;
378 break;
379 case II_NETDEV:
380 command_idby = CIB_NETDEV;
381 devidx = if_nametoindex(*argv);
382 if (devidx == 0)
383 devidx = -1;
384 argc--;
385 argv++;
386 break;
387 case II_WDEV:
388 command_idby = CIB_WDEV;
389 devidx = strtoll(*argv, &tmp, 0);
390 if (*tmp != '\0')
391 return 1;
392 argc--;
393 argv++;
394 default:
395 break;
396 }
397
398 if (devidx < 0)
399 return -errno;
400
401 section = *argv;
402 argc--;
403 argv++;
404
405 for_each_cmd(sectcmd) {
406 if (sectcmd->parent)
407 continue;
408 /* ok ... bit of a hack for the dupe 'info' section */
409 if (match && sectcmd->idby != command_idby)
410 continue;
411 if (strcmp(sectcmd->name, section) == 0)
412 match = sectcmd;
413 }
414
415 sectcmd = match;
416 match = NULL;
417 if (!sectcmd)
418 return 1;
419
420 if (argc > 0) {
421 command = *argv;
422
423 for_each_cmd(cmd) {
424 if (!cmd->handler)
425 continue;
426 if (cmd->parent != sectcmd)
427 continue;
428 /*
429 * ignore mismatch id by, but allow WDEV
430 * in place of NETDEV
431 */
432 if (cmd->idby != command_idby &&
433 !(cmd->idby == CIB_NETDEV &&
434 command_idby == CIB_WDEV))
435 continue;
436 if (strcmp(cmd->name, command))
437 continue;
438 if (argc > 1 && !cmd->args)
439 continue;
440 match = cmd;
441 break;
442 }
443
444 if (match) {
445 argc--;
446 argv++;
447 }
448 }
449
450 if (match)
451 cmd = match;
452 else {
453 /* Use the section itself, if possible. */
454 cmd = sectcmd;
455 if (argc && !cmd->args)
456 return 1;
457 if (cmd->idby != command_idby &&
458 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
459 return 1;
460 if (!cmd->handler)
461 return 1;
462 }
463
464 if (cmd->selector) {
465 cmd = cmd->selector(argc, argv);
466 if (!cmd)
467 return 1;
468 }
469
470 if (cmdout)
471 *cmdout = cmd;
472
473 if (!cmd->cmd) {
474 argc = o_argc;
475 argv = o_argv;
476 return cmd->handler(state, NULL, argc, argv, idby);
477 }
478
479 msg = nlmsg_alloc();
480 if (!msg) {
481 fprintf(stderr, "failed to allocate netlink message\n");
482 return 2;
483 }
484
485 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
486 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
487 if (!cb || !s_cb) {
488 fprintf(stderr, "failed to allocate netlink callbacks\n");
489 err = 2;
490 goto out;
491 }
492
493 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
494 cmd->nl_msg_flags, cmd->cmd, 0);
495
496 switch (command_idby) {
497 case CIB_PHY:
498 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
499 break;
500 case CIB_NETDEV:
501 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
502 break;
503 case CIB_WDEV:
504 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
505 break;
506 default:
507 break;
508 }
509
510 err = cmd->handler(state, msg, argc, argv, idby);
511 if (err)
512 goto out;
513
514 nl_socket_set_cb(state->nl_sock, s_cb);
515
516 err = nl_send_auto_complete(state->nl_sock, msg);
517 if (err < 0)
518 goto out;
519
520 err = 1;
521
522 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
523 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
524 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
525 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
526
527 while (err > 0)
528 nl_recvmsgs(state->nl_sock, cb);
529 out:
530 nl_cb_put(cb);
531 nl_cb_put(s_cb);
532 nlmsg_free(msg);
533 return err;
534 nla_put_failure:
535 fprintf(stderr, "building message failed\n");
536 return 2;
537 }
538
539 int handle_cmd(struct nl80211_state *state, enum id_input idby,
540 int argc, char **argv)
541 {
542 return __handle_cmd(state, idby, argc, argv, NULL);
543 }
544
545 /*
546 * Unfortunately, I don't know how densely the linker packs the struct cmd.
547 * For example, if you have a 72-byte struct cmd, the linker will pad each
548 * out to 96 bytes before putting them together in the section. There must
549 * be some algorithm, but I haven't found it yet.
550 *
551 * We used to calculate this by taking the (abs value of) the difference
552 * between __section_get and __section_set, but if LTO is enabled then this
553 * stops working because the entries of the "__cmd" section get rearranged
554 * freely by the compiler/linker.
555 *
556 * Fix this by using yet another "__sizer" section that only contains these
557 * two entries - then the (abs value of) the difference between them will
558 * be how they get packed and that can be used to iterate the __cmd section
559 * as well.
560 */
561 static struct cmd sizer1 __attribute__((used,section("__sizer"))) = {};
562 static struct cmd sizer2 __attribute__((used,section("__sizer"))) = {};
563
564 int main(int argc, char **argv)
565 {
566 struct nl80211_state nlstate;
567 int err;
568 const struct cmd *cmd = NULL;
569
570 /* calculate command size including padding */
571 cmd_size = labs((uintptr_t)&sizer2 - (uintptr_t)&sizer1);
572 /* strip off self */
573 argc--;
574 argv0 = *argv++;
575
576 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
577 iw_debug = 1;
578 argc--;
579 argv++;
580 }
581
582 if (argc > 0 && strcmp(*argv, "--version") == 0) {
583 version();
584 return 0;
585 }
586
587 /* need to treat "help" command specially so it works w/o nl80211 */
588 if (argc == 0 || strcmp(*argv, "help") == 0) {
589 usage(argc - 1, argv + 1);
590 return 0;
591 }
592
593 err = nl80211_init(&nlstate);
594 if (err)
595 return 1;
596
597 if (strcmp(*argv, "dev") == 0 && argc > 1) {
598 argc--;
599 argv++;
600 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
601 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
602 if (strlen(*argv) == 3) {
603 argc--;
604 argv++;
605 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
606 } else if (*(*argv + 3) == '#')
607 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
608 else
609 goto detect;
610 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
611 argc--;
612 argv++;
613 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
614 } else {
615 int idx;
616 enum id_input idby;
617 detect:
618 idby = II_NONE;
619 if ((idx = if_nametoindex(argv[0])) != 0)
620 idby = II_NETDEV;
621 else if ((idx = phy_lookup(argv[0])) >= 0)
622 idby = II_PHY_NAME;
623 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
624 }
625
626 if (err == HANDLER_RET_USAGE) {
627 if (cmd)
628 usage_cmd(cmd);
629 else
630 usage(0, NULL);
631 } else if (err == HANDLER_RET_DONE) {
632 err = 0;
633 } else if (err < 0)
634 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
635
636 nl80211_cleanup(&nlstate);
637
638 return err;
639 }