]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
split out per-command help
[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 #ifndef CONFIG_LIBNL20
27 /* libnl 2.0 compatibility code */
28
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 __genl_ctrl_alloc_cache(struct nl_sock *h, struct nl_cache **cache)
40 {
41 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
42 if (!tmp)
43 return -ENOMEM;
44 *cache = tmp;
45 return 0;
46 }
47 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
48 #endif /* CONFIG_LIBNL20 */
49
50 static int debug = 0;
51
52 static int nl80211_init(struct nl80211_state *state)
53 {
54 int err;
55
56 state->nl_sock = nl_socket_alloc();
57 if (!state->nl_sock) {
58 fprintf(stderr, "Failed to allocate netlink socket.\n");
59 return -ENOMEM;
60 }
61
62 if (genl_connect(state->nl_sock)) {
63 fprintf(stderr, "Failed to connect to generic netlink.\n");
64 err = -ENOLINK;
65 goto out_handle_destroy;
66 }
67
68 if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
69 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
70 err = -ENOMEM;
71 goto out_handle_destroy;
72 }
73
74 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
75 if (!state->nl80211) {
76 fprintf(stderr, "nl80211 not found.\n");
77 err = -ENOENT;
78 goto out_cache_free;
79 }
80
81 return 0;
82
83 out_cache_free:
84 nl_cache_free(state->nl_cache);
85 out_handle_destroy:
86 nl_socket_free(state->nl_sock);
87 return err;
88 }
89
90 static void nl80211_cleanup(struct nl80211_state *state)
91 {
92 genl_family_put(state->nl80211);
93 nl_cache_free(state->nl_cache);
94 nl_socket_free(state->nl_sock);
95 }
96
97 __COMMAND(NULL, NULL, "", NULL, 0, 0, 0, CIB_NONE, NULL);
98 __COMMAND(NULL, NULL, "", NULL, 1, 0, 0, CIB_NONE, NULL);
99
100 static int cmd_size;
101
102 static void usage_cmd(struct cmd *cmd)
103 {
104 switch (cmd->idby) {
105 case CIB_NONE:
106 fprintf(stderr, "\t");
107 break;
108 case CIB_PHY:
109 fprintf(stderr, "\tphy <phyname> ");
110 break;
111 case CIB_NETDEV:
112 fprintf(stderr, "\tdev <devname> ");
113 break;
114 }
115 if (cmd->section)
116 fprintf(stderr, "%s ", cmd->section);
117 fprintf(stderr, "%s", cmd->name);
118 if (cmd->args)
119 fprintf(stderr, " %s", cmd->args);
120 fprintf(stderr, "\n");
121 }
122
123 static void usage(const char *argv0)
124 {
125 struct cmd *cmd;
126
127 fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
128 fprintf(stderr, "Options:\n");
129 fprintf(stderr, "\t--debug\t\tenable netlink debugging\n");
130 fprintf(stderr, "\t--version\tshow version\n");
131 fprintf(stderr, "Commands:\n");
132 fprintf(stderr, "\thelp\n");
133 fprintf(stderr, "\tevent [-t] [-f]\n");
134 for (cmd = &__start___cmd; cmd < &__stop___cmd;
135 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
136 if (!cmd->handler || cmd->hidden)
137 continue;
138 usage_cmd(cmd);
139 }
140 }
141
142 static void version(void)
143 {
144 printf("iw version %s\n", iw_version);
145 }
146
147 static int phy_lookup(char *name)
148 {
149 char buf[200];
150 int fd, pos;
151
152 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
153
154 fd = open(buf, O_RDONLY);
155 if (fd < 0)
156 return -1;
157 pos = read(fd, buf, sizeof(buf) - 1);
158 if (pos < 0)
159 return -1;
160 buf[pos] = '\0';
161 return atoi(buf);
162 }
163
164 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
165 void *arg)
166 {
167 int *ret = arg;
168 *ret = err->error;
169 return NL_STOP;
170 }
171
172 static int finish_handler(struct nl_msg *msg, void *arg)
173 {
174 int *ret = arg;
175 *ret = 0;
176 return NL_SKIP;
177 }
178
179 static int ack_handler(struct nl_msg *msg, void *arg)
180 {
181 int *ret = arg;
182 *ret = 0;
183 return NL_STOP;
184 }
185
186 int handle_cmd(struct nl80211_state *state, enum id_input idby,
187 int argc, char **argv)
188 {
189 struct cmd *cmd, *match = NULL;
190 struct nl_cb *cb;
191 struct nl_msg *msg;
192 int devidx = 0;
193 int err, o_argc;
194 const char *command, *section;
195 char *tmp, **o_argv;
196 enum command_identify_by command_idby = CIB_NONE;
197
198 if (argc <= 1 && idby != II_NONE)
199 return 1;
200
201 o_argc = argc;
202 o_argv = argv;
203
204 switch (idby) {
205 case II_PHY_IDX:
206 command_idby = CIB_PHY;
207 devidx = strtoul(*argv + 4, &tmp, 0);
208 if (*tmp != '\0')
209 return 1;
210 argc--;
211 argv++;
212 break;
213 case II_PHY_NAME:
214 command_idby = CIB_PHY;
215 devidx = phy_lookup(*argv);
216 argc--;
217 argv++;
218 break;
219 case II_NETDEV:
220 command_idby = CIB_NETDEV;
221 devidx = if_nametoindex(*argv);
222 if (devidx == 0)
223 devidx = -1;
224 argc--;
225 argv++;
226 break;
227 default:
228 break;
229 }
230
231 if (devidx < 0)
232 return -errno;
233
234 section = command = *argv;
235 argc--;
236 argv++;
237
238 for (cmd = &__start___cmd; cmd < &__stop___cmd;
239 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
240 if (!cmd->handler)
241 continue;
242 if (cmd->idby != command_idby)
243 continue;
244 if (cmd->section) {
245 if (strcmp(cmd->section, section))
246 continue;
247 /* this is a bit icky ... */
248 if (command == section) {
249 if (argc <= 0) {
250 if (match)
251 break;
252 return 1;
253 }
254 command = *argv;
255 argc--;
256 argv++;
257 }
258 } else if (section != command)
259 continue;
260 if (strcmp(cmd->name, command))
261 continue;
262 if (argc && !cmd->args)
263 continue;
264
265 match = cmd;
266 }
267
268 cmd = match;
269
270 if (!cmd)
271 return 1;
272
273 if (!cmd->cmd) {
274 argc = o_argc;
275 argv = o_argv;
276 return cmd->handler(state, NULL, NULL, argc, argv);
277 }
278
279 msg = nlmsg_alloc();
280 if (!msg) {
281 fprintf(stderr, "failed to allocate netlink message\n");
282 return 2;
283 }
284
285 cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
286 if (!cb) {
287 fprintf(stderr, "failed to allocate netlink callbacks\n");
288 err = 2;
289 goto out_free_msg;
290 }
291
292 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
293 cmd->nl_msg_flags, cmd->cmd, 0);
294
295 switch (command_idby) {
296 case CIB_PHY:
297 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
298 break;
299 case CIB_NETDEV:
300 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
301 break;
302 default:
303 break;
304 }
305
306 err = cmd->handler(state, cb, msg, argc, argv);
307 if (err)
308 goto out;
309
310 err = nl_send_auto_complete(state->nl_sock, msg);
311 if (err < 0)
312 goto out;
313
314 err = 1;
315
316 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
317 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
318 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
319
320 while (err > 0)
321 nl_recvmsgs(state->nl_sock, cb);
322 out:
323 nl_cb_put(cb);
324 out_free_msg:
325 nlmsg_free(msg);
326 return err;
327 nla_put_failure:
328 fprintf(stderr, "building message failed\n");
329 return 2;
330 }
331
332 static int no_seq_check(struct nl_msg *msg, void *arg)
333 {
334 return NL_OK;
335 }
336
337 struct print_event_args {
338 bool frame, time;
339 };
340
341 static void print_frame(struct print_event_args *args, struct nlattr *attr)
342 {
343 uint8_t *frame;
344 size_t len;
345 int i;
346 char macbuf[6*3];
347 uint16_t tmp;
348
349 if (!attr)
350 printf(" [no frame]");
351
352 frame = nla_data(attr);
353 len = nla_len(attr);
354
355 if (len < 26) {
356 printf(" [invalid frame: ");
357 goto print_frame;
358 }
359
360 mac_addr_n2a(macbuf, frame + 10);
361 printf(" %s -> ", macbuf);
362 mac_addr_n2a(macbuf, frame + 4);
363 printf("%s", macbuf);
364
365 switch (frame[0] & 0xfc) {
366 case 0x10: /* assoc resp */
367 case 0x30: /* reassoc resp */
368 /* status */
369 tmp = (frame[27] << 8) + frame[26];
370 printf(" status: %d: %s", tmp, get_status_str(tmp));
371 break;
372 case 0x00: /* assoc req */
373 case 0x20: /* reassoc req */
374 break;
375 case 0xb0: /* auth */
376 /* status */
377 tmp = (frame[29] << 8) + frame[28];
378 printf(" status: %d: %s", tmp, get_status_str(tmp));
379 break;
380 break;
381 case 0xa0: /* disassoc */
382 case 0xc0: /* deauth */
383 /* reason */
384 tmp = (frame[25] << 8) + frame[24];
385 printf(" reason %d: %s", tmp, get_reason_str(tmp));
386 break;
387 }
388
389 if (!args->frame)
390 return;
391
392 printf(" [frame:");
393
394 print_frame:
395 for (i = 0; i < len; i++)
396 printf(" %.02x", frame[i]);
397 printf("]");
398 }
399
400 static int print_event(struct nl_msg *msg, void *arg)
401 {
402 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
403 struct nlattr *tb[NL80211_ATTR_MAX + 1];
404 struct print_event_args *args = arg;
405 char ifname[100];
406 char macbuf[6*3];
407 __u8 reg_type;
408
409 if (args->time) {
410 struct timeval tv;
411 gettimeofday(&tv, NULL);
412 printf("%ld.%06u: ", (long) tv.tv_sec, (unsigned int) tv.tv_usec);
413 }
414
415 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
416 genlmsg_attrlen(gnlh, 0), NULL);
417
418 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
419 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
420 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
421 } else if (tb[NL80211_ATTR_IFINDEX]) {
422 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
423 printf("%s: ", ifname);
424 } else if (tb[NL80211_ATTR_WIPHY]) {
425 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
426 }
427
428 switch (gnlh->cmd) {
429 case NL80211_CMD_NEW_WIPHY:
430 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
431 break;
432 case NL80211_CMD_NEW_SCAN_RESULTS:
433 printf("scan finished\n");
434 break;
435 case NL80211_CMD_SCAN_ABORTED:
436 printf("scan aborted\n");
437 break;
438 case NL80211_CMD_REG_CHANGE:
439 printf("regulatory domain change: ");
440
441 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
442
443 switch (reg_type) {
444 case NL80211_REGDOM_TYPE_COUNTRY:
445 printf("set to %s by %s request",
446 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
447 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
448 if (tb[NL80211_ATTR_WIPHY])
449 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
450 break;
451 case NL80211_REGDOM_TYPE_WORLD:
452 printf("set to world roaming by %s request",
453 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
454 break;
455 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
456 printf("custom world roaming rules in place on phy%d by %s request",
457 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
458 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
459 break;
460 case NL80211_REGDOM_TYPE_INTERSECTION:
461 printf("intersection used due to a request made by %s",
462 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
463 if (tb[NL80211_ATTR_WIPHY])
464 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
465 break;
466 default:
467 printf("unknown source (upgrade this utility)");
468 break;
469 }
470
471 printf("\n");
472 break;
473 case NL80211_CMD_JOIN_IBSS:
474 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
475 printf("IBSS %s joined\n", macbuf);
476 break;
477 case NL80211_CMD_AUTHENTICATE:
478 printf("auth");
479 print_frame(args, tb[NL80211_ATTR_FRAME]);
480 printf("\n");
481 break;
482 case NL80211_CMD_ASSOCIATE:
483 printf("assoc");
484 print_frame(args, tb[NL80211_ATTR_FRAME]);
485 printf("\n");
486 break;
487 case NL80211_CMD_DEAUTHENTICATE:
488 printf("deauth");
489 print_frame(args, tb[NL80211_ATTR_FRAME]);
490 printf("\n");
491 break;
492 case NL80211_CMD_DISASSOCIATE:
493 printf("disassoc");
494 print_frame(args, tb[NL80211_ATTR_FRAME]);
495 printf("\n");
496 break;
497 default:
498 printf("unknown event %d\n", gnlh->cmd);
499 break;
500 }
501
502 return NL_SKIP;
503 }
504
505 struct wait_event {
506 int n_cmds;
507 const __u32 *cmds;
508 __u32 cmd;
509 };
510
511 static int wait_event(struct nl_msg *msg, void *arg)
512 {
513 struct wait_event *wait = arg;
514 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
515 int i;
516
517 for (i = 0; i < wait->n_cmds; i++) {
518 if (gnlh->cmd == wait->cmds[i]) {
519 wait->cmd = gnlh->cmd;
520 }
521 }
522
523 return NL_SKIP;
524 }
525
526 static __u32 __listen_events(struct nl80211_state *state,
527 const int n_waits, const __u32 *waits,
528 struct print_event_args *args)
529 {
530 int mcid, ret;
531 struct nl_cb *cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
532 struct wait_event wait_ev;
533
534 if (!cb) {
535 fprintf(stderr, "failed to allocate netlink callbacks\n");
536 return -ENOMEM;
537 }
538
539 /* Configuration multicast group */
540 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
541 if (mcid < 0)
542 return mcid;
543
544 ret = nl_socket_add_membership(state->nl_sock, mcid);
545 if (ret)
546 return ret;
547
548 /* Scan multicast group */
549 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
550 if (mcid >= 0) {
551 ret = nl_socket_add_membership(state->nl_sock, mcid);
552 if (ret)
553 return ret;
554 }
555
556 /* Regulatory multicast group */
557 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
558 if (mcid >= 0) {
559 ret = nl_socket_add_membership(state->nl_sock, mcid);
560 if (ret)
561 return ret;
562 }
563
564 /* MLME multicast group */
565 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
566 if (mcid >= 0) {
567 ret = nl_socket_add_membership(state->nl_sock, mcid);
568 if (ret)
569 return ret;
570 }
571
572 /* no sequence checking for multicast messages */
573 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
574
575 if (n_waits && waits) {
576 wait_ev.cmds = waits;
577 wait_ev.n_cmds = n_waits;
578 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
579 } else {
580 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args);
581 }
582
583 wait_ev.cmd = 0;
584
585 while (!wait_ev.cmd)
586 nl_recvmsgs(state->nl_sock, cb);
587
588 nl_cb_put(cb);
589
590 return wait_ev.cmd;
591 }
592
593 __u32 listen_events(struct nl80211_state *state,
594 const int n_waits, const __u32 *waits)
595 {
596 return __listen_events(state, n_waits, waits, NULL);
597 }
598
599 static int print_events(struct nl80211_state *state, int argc, char **argv)
600 {
601 struct print_event_args args;
602
603 memset(&args, 0, sizeof(args));
604
605 while (argc > 0) {
606 if (strcmp(argv[0], "-f") == 0)
607 args.frame = true;
608 else if (strcmp(argv[0], "-t") == 0)
609 args.time = true;
610 else
611 return 1;
612 argc--;
613 argv++;
614 }
615
616 if (argc)
617 return 1;
618
619 return __listen_events(state, 0, NULL, &args);
620 }
621
622 int main(int argc, char **argv)
623 {
624 struct nl80211_state nlstate;
625 int err;
626 const char *argv0;
627
628 /* calculate command size including padding */
629 cmd_size = abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
630 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0);
631 /* strip off self */
632 argc--;
633 argv0 = *argv++;
634
635 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
636 debug = 1;
637 argc--;
638 argv++;
639 }
640
641 if (argc > 0 && strcmp(*argv, "--version") == 0) {
642 version();
643 return 0;
644 }
645
646 if (argc == 0 || strcmp(*argv, "help") == 0) {
647 usage(argv0);
648 return 0;
649 }
650
651 err = nl80211_init(&nlstate);
652 if (err)
653 return 1;
654
655 if (strcmp(*argv, "event") == 0) {
656 argc--;
657 argv++;
658 err = print_events(&nlstate, argc, argv);
659 } else if (strcmp(*argv, "dev") == 0 && argc > 1) {
660 argc--;
661 argv++;
662 err = handle_cmd(&nlstate, II_NETDEV, argc, argv);
663 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
664 if (strlen(*argv) == 3) {
665 argc--;
666 argv++;
667 err = handle_cmd(&nlstate, II_PHY_NAME, argc, argv);
668 } else if (*(*argv + 3) == '#')
669 err = handle_cmd(&nlstate, II_PHY_IDX, argc, argv);
670 else
671 err = 1;
672 } else
673 err = handle_cmd(&nlstate, II_NONE, argc, argv);
674
675 if (err == 1)
676 usage(argv0);
677 if (err < 0)
678 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
679
680 nl80211_cleanup(&nlstate);
681
682 return err;
683 }