]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
iw: bump version to 4.7
[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
17 #include <netlink/genl/genl.h>
18 #include <netlink/genl/family.h>
19 #include <netlink/genl/ctrl.h>
20 #include <netlink/msg.h>
21 #include <netlink/attr.h>
22
23 #include "nl80211.h"
24 #include "iw.h"
25
26 /* libnl 1.x compatibility code */
27 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
28 static inline struct nl_handle *nl_socket_alloc(void)
29 {
30 return nl_handle_alloc();
31 }
32
33 static inline void nl_socket_free(struct nl_sock *h)
34 {
35 nl_handle_destroy(h);
36 }
37
38 static 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 }
43 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
44
45 int iw_debug = 0;
46
47 static int nl80211_init(struct nl80211_state *state)
48 {
49 int err;
50
51 state->nl_sock = nl_socket_alloc();
52 if (!state->nl_sock) {
53 fprintf(stderr, "Failed to allocate netlink socket.\n");
54 return -ENOMEM;
55 }
56
57 if (genl_connect(state->nl_sock)) {
58 fprintf(stderr, "Failed to connect to generic netlink.\n");
59 err = -ENOLINK;
60 goto out_handle_destroy;
61 }
62
63 nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
64
65 state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
66 if (state->nl80211_id < 0) {
67 fprintf(stderr, "nl80211 not found.\n");
68 err = -ENOENT;
69 goto out_handle_destroy;
70 }
71
72 return 0;
73
74 out_handle_destroy:
75 nl_socket_free(state->nl_sock);
76 return err;
77 }
78
79 static void nl80211_cleanup(struct nl80211_state *state)
80 {
81 nl_socket_free(state->nl_sock);
82 }
83
84 static int cmd_size;
85
86 extern struct cmd __start___cmd;
87 extern 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
94 static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
95 {
96 const char *start, *lend, *end;
97
98 printf("%s", indent);
99
100 switch (cmd->idby) {
101 case CIB_NONE:
102 break;
103 case CIB_PHY:
104 printf("phy <phyname> ");
105 break;
106 case CIB_NETDEV:
107 printf("dev <devname> ");
108 break;
109 case CIB_WDEV:
110 printf("wdev <idx> ");
111 break;
112 }
113 if (cmd->parent && cmd->parent->name)
114 printf("%s ", cmd->parent->name);
115 printf("%s", cmd->name);
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");
150
151 if (!full || !cmd->help)
152 return;
153
154 /* hack */
155 if (strlen(indent))
156 indent = "\t\t";
157 else
158 printf("\n");
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;
167 printf("%s", indent);
168 printf("%.*s\n", (int)(lend - start), start);
169 start = lend + 1;
170 } while (end != lend);
171
172 printf("\n");
173 }
174
175 static void usage_options(void)
176 {
177 printf("Options:\n");
178 printf("\t--debug\t\tenable netlink debugging\n");
179 }
180
181 static const char *argv0;
182
183 static void usage(int argc, char **argv)
184 {
185 const struct cmd *section, *cmd;
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];
195
196 printf("Usage:\t%s [options] command\n", argv0);
197 usage_options();
198 printf("\t--version\tshow version (%s)\n", iw_version);
199 printf("Commands:\n");
200 for_each_cmd(section) {
201 if (section->parent)
202 continue;
203
204 if (sect_filt && strcmp(section->name, sect_filt))
205 continue;
206
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;
215 if (cmd_filt && strcmp(cmd->name, cmd_filt))
216 continue;
217 __usage_cmd(cmd, "\t", full);
218 }
219 }
220 printf("\nCommands that use the netdev ('dev') can also be given the\n"
221 "'wdev' instead to identify the device.\n");
222 printf("\nYou can omit the 'phy' or 'dev' if "
223 "the identification is unique,\n"
224 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
225 "(Don't when scripting.)\n\n"
226 "Do NOT screenscrape this tool, we don't "
227 "consider its output stable.\n\n");
228 }
229
230 static int print_help(struct nl80211_state *state,
231 struct nl_msg *msg,
232 int argc, char **argv,
233 enum id_input id)
234 {
235 exit(3);
236 }
237 TOPLEVEL(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\".");
240
241 static void usage_cmd(const struct cmd *cmd)
242 {
243 printf("Usage:\t%s [options] ", argv0);
244 __usage_cmd(cmd, "", true);
245 usage_options();
246 }
247
248 static void version(void)
249 {
250 printf("iw version %s\n", iw_version);
251 }
252
253 static 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);
261 if (fd < 0)
262 return -1;
263 pos = read(fd, buf, sizeof(buf) - 1);
264 if (pos < 0) {
265 close(fd);
266 return -1;
267 }
268 buf[pos] = '\0';
269 close(fd);
270 return atoi(buf);
271 }
272
273 static 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
281 static int finish_handler(struct nl_msg *msg, void *arg)
282 {
283 int *ret = arg;
284 *ret = 0;
285 return NL_SKIP;
286 }
287
288 static int ack_handler(struct nl_msg *msg, void *arg)
289 {
290 int *ret = arg;
291 *ret = 0;
292 return NL_STOP;
293 }
294
295 static int (*registered_handler)(struct nl_msg *, void *);
296 static void *registered_handler_data;
297
298 void register_handler(int (*handler)(struct nl_msg *, void *), void *data)
299 {
300 registered_handler = handler;
301 registered_handler_data = data;
302 }
303
304 int 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
312 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
313 int argc, char **argv, const struct cmd **cmdout)
314 {
315 const struct cmd *cmd, *match = NULL, *sectcmd;
316 struct nl_cb *cb;
317 struct nl_cb *s_cb;
318 struct nl_msg *msg;
319 signed long long devidx = 0;
320 int err, o_argc;
321 const char *command, *section;
322 char *tmp, **o_argv;
323 enum command_identify_by command_idby = CIB_NONE;
324
325 if (argc <= 1 && idby != II_NONE)
326 return 1;
327
328 o_argc = argc;
329 o_argv = argv;
330
331 switch (idby) {
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;
342 devidx = phy_lookup(*argv);
343 argc--;
344 argv++;
345 break;
346 case II_NETDEV:
347 command_idby = CIB_NETDEV;
348 devidx = if_nametoindex(*argv);
349 if (devidx == 0)
350 devidx = -1;
351 argc--;
352 argv++;
353 break;
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++;
361 default:
362 break;
363 }
364
365 if (devidx < 0)
366 return -errno;
367
368 section = *argv;
369 argc--;
370 argv++;
371
372 for_each_cmd(sectcmd) {
373 if (sectcmd->parent)
374 continue;
375 /* ok ... bit of a hack for the dupe 'info' section */
376 if (match && sectcmd->idby != command_idby)
377 continue;
378 if (strcmp(sectcmd->name, section) == 0)
379 match = sectcmd;
380 }
381
382 sectcmd = match;
383 match = NULL;
384 if (!sectcmd)
385 return 1;
386
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;
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))
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;
424 if (cmd->idby != command_idby &&
425 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
426 return 1;
427 if (!cmd->handler)
428 return 1;
429 }
430
431 if (cmd->selector) {
432 cmd = cmd->selector(argc, argv);
433 if (!cmd)
434 return 1;
435 }
436
437 if (cmdout)
438 *cmdout = cmd;
439
440 if (!cmd->cmd) {
441 argc = o_argc;
442 argv = o_argv;
443 return cmd->handler(state, NULL, argc, argv, idby);
444 }
445
446 msg = nlmsg_alloc();
447 if (!msg) {
448 fprintf(stderr, "failed to allocate netlink message\n");
449 return 2;
450 }
451
452 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
453 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
454 if (!cb || !s_cb) {
455 fprintf(stderr, "failed to allocate netlink callbacks\n");
456 err = 2;
457 goto out;
458 }
459
460 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
461 cmd->nl_msg_flags, cmd->cmd, 0);
462
463 switch (command_idby) {
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;
470 case CIB_WDEV:
471 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
472 break;
473 default:
474 break;
475 }
476
477 err = cmd->handler(state, msg, argc, argv, idby);
478 if (err)
479 goto out;
480
481 nl_socket_set_cb(state->nl_sock, s_cb);
482
483 err = nl_send_auto_complete(state->nl_sock, msg);
484 if (err < 0)
485 goto out;
486
487 err = 1;
488
489 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
490 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
491 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
492 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
493
494 while (err > 0)
495 nl_recvmsgs(state->nl_sock, cb);
496 out:
497 nl_cb_put(cb);
498 nl_cb_put(s_cb);
499 nlmsg_free(msg);
500 return err;
501 nla_put_failure:
502 fprintf(stderr, "building message failed\n");
503 return 2;
504 }
505
506 int 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
512 int main(int argc, char **argv)
513 {
514 struct nl80211_state nlstate;
515 int err;
516 const struct cmd *cmd = NULL;
517
518 /* calculate command size including padding */
519 cmd_size = labs((long)&__section_set - (long)&__section_get);
520 /* strip off self */
521 argc--;
522 argv0 = *argv++;
523
524 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
525 iw_debug = 1;
526 argc--;
527 argv++;
528 }
529
530 if (argc > 0 && strcmp(*argv, "--version") == 0) {
531 version();
532 return 0;
533 }
534
535 /* need to treat "help" command specially so it works w/o nl80211 */
536 if (argc == 0 || strcmp(*argv, "help") == 0) {
537 usage(argc - 1, argv + 1);
538 return 0;
539 }
540
541 err = nl80211_init(&nlstate);
542 if (err)
543 return 1;
544
545 if (strcmp(*argv, "dev") == 0 && argc > 1) {
546 argc--;
547 argv++;
548 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
549 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
550 if (strlen(*argv) == 3) {
551 argc--;
552 argv++;
553 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
554 } else if (*(*argv + 3) == '#')
555 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
556 else
557 goto detect;
558 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
559 argc--;
560 argv++;
561 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
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;
568 else if ((idx = phy_lookup(argv[0])) >= 0)
569 idby = II_PHY_NAME;
570 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
571 }
572
573 if (err == HANDLER_RET_USAGE) {
574 if (cmd)
575 usage_cmd(cmd);
576 else
577 usage(0, NULL);
578 } else if (err == HANDLER_RET_DONE) {
579 err = 0;
580 } else if (err < 0)
581 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
582
583 nl80211_cleanup(&nlstate);
584
585 return err;
586 }