]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
Merge branch 'ibss-test'
[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
16 #include <netlink/genl/genl.h>
17 #include <netlink/genl/family.h>
18 #include <netlink/genl/ctrl.h>
19 #include <netlink/msg.h>
20 #include <netlink/attr.h>
21
22 #include "nl80211.h"
23 #include "iw.h"
24 #include "version.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(const char *argv0)
103 {
104 struct cmd *cmd;
105
106 fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
107 fprintf(stderr, "Options:\n");
108 fprintf(stderr, "\t--debug\t\tenable netlink debugging\n");
109 fprintf(stderr, "\t--version\tshow version\n");
110 fprintf(stderr, "Commands:\n");
111 fprintf(stderr, "\thelp\n");
112 fprintf(stderr, "\tevent\n");
113 for (cmd = &__start___cmd; cmd < &__stop___cmd;
114 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
115 if (!cmd->handler || cmd->hidden)
116 continue;
117 switch (cmd->idby) {
118 case CIB_NONE:
119 fprintf(stderr, "\t");
120 break;
121 case CIB_PHY:
122 fprintf(stderr, "\tphy <phyname> ");
123 break;
124 case CIB_NETDEV:
125 fprintf(stderr, "\tdev <devname> ");
126 break;
127 }
128 if (cmd->section)
129 fprintf(stderr, "%s ", cmd->section);
130 fprintf(stderr, "%s", cmd->name);
131 if (cmd->args)
132 fprintf(stderr, " %s", cmd->args);
133 fprintf(stderr, "\n");
134 }
135 }
136
137 static void version(void)
138 {
139 printf("iw version " IW_VERSION "\n");
140 }
141
142 static int phy_lookup(char *name)
143 {
144 char buf[200];
145 int fd, pos;
146
147 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
148
149 fd = open(buf, O_RDONLY);
150 if (fd < 0)
151 return -1;
152 pos = read(fd, buf, sizeof(buf) - 1);
153 if (pos < 0)
154 return -1;
155 buf[pos] = '\0';
156 return atoi(buf);
157 }
158
159 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
160 void *arg)
161 {
162 int *ret = arg;
163 *ret = err->error;
164 return NL_STOP;
165 }
166
167 static int finish_handler(struct nl_msg *msg, void *arg)
168 {
169 int *ret = arg;
170 *ret = 0;
171 return NL_SKIP;
172 }
173
174 static int ack_handler(struct nl_msg *msg, void *arg)
175 {
176 int *ret = arg;
177 *ret = 0;
178 return NL_STOP;
179 }
180
181 int handle_cmd(struct nl80211_state *state, enum id_input idby,
182 int argc, char **argv)
183 {
184 struct cmd *cmd, *match = NULL;
185 struct nl_cb *cb;
186 struct nl_msg *msg;
187 int devidx = 0;
188 int err, o_argc;
189 const char *command, *section;
190 char *tmp, **o_argv;
191 enum command_identify_by command_idby = CIB_NONE;
192
193 if (argc <= 1 && idby != II_NONE)
194 return 1;
195
196 o_argc = argc;
197 o_argv = argv;
198
199 switch (idby) {
200 case II_PHY_IDX:
201 command_idby = CIB_PHY;
202 devidx = strtoul(*argv + 4, &tmp, 0);
203 if (*tmp != '\0')
204 return 1;
205 argc--;
206 argv++;
207 break;
208 case II_PHY_NAME:
209 command_idby = CIB_PHY;
210 devidx = phy_lookup(*argv);
211 argc--;
212 argv++;
213 break;
214 case II_NETDEV:
215 command_idby = CIB_NETDEV;
216 devidx = if_nametoindex(*argv);
217 if (devidx == 0)
218 devidx = -1;
219 argc--;
220 argv++;
221 break;
222 default:
223 break;
224 }
225
226 if (devidx < 0)
227 return -errno;
228
229 section = command = *argv;
230 argc--;
231 argv++;
232
233 for (cmd = &__start___cmd; cmd < &__stop___cmd;
234 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
235 if (!cmd->handler)
236 continue;
237 if (cmd->idby != command_idby)
238 continue;
239 if (cmd->section) {
240 if (strcmp(cmd->section, section))
241 continue;
242 /* this is a bit icky ... */
243 if (command == section) {
244 if (argc <= 0) {
245 if (match)
246 break;
247 return 1;
248 }
249 command = *argv;
250 argc--;
251 argv++;
252 }
253 } else if (section != command)
254 continue;
255 if (strcmp(cmd->name, command))
256 continue;
257 if (argc && !cmd->args)
258 continue;
259
260 match = cmd;
261 }
262
263 cmd = match;
264
265 if (!cmd)
266 return 1;
267
268 if (!cmd->cmd) {
269 argc = o_argc;
270 argv = o_argv;
271 return cmd->handler(state, NULL, NULL, argc, argv);
272 }
273
274 msg = nlmsg_alloc();
275 if (!msg) {
276 fprintf(stderr, "failed to allocate netlink message\n");
277 return 2;
278 }
279
280 cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
281 if (!cb) {
282 fprintf(stderr, "failed to allocate netlink callbacks\n");
283 err = 2;
284 goto out_free_msg;
285 }
286
287 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
288 cmd->nl_msg_flags, cmd->cmd, 0);
289
290 switch (command_idby) {
291 case CIB_PHY:
292 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
293 break;
294 case CIB_NETDEV:
295 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
296 break;
297 default:
298 break;
299 }
300
301 err = cmd->handler(state, cb, msg, argc, argv);
302 if (err)
303 goto out;
304
305 err = nl_send_auto_complete(state->nl_sock, msg);
306 if (err < 0)
307 goto out;
308
309 err = 1;
310
311 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
312 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
313 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
314
315 while (err > 0)
316 nl_recvmsgs(state->nl_sock, cb);
317 out:
318 nl_cb_put(cb);
319 out_free_msg:
320 nlmsg_free(msg);
321 return err;
322 nla_put_failure:
323 fprintf(stderr, "building message failed\n");
324 return 2;
325 }
326
327 static int no_seq_check(struct nl_msg *msg, void *arg)
328 {
329 return NL_OK;
330 }
331
332 static int print_event(struct nl_msg *msg, void *arg)
333 {
334 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
335 struct nlattr *tb[NL80211_ATTR_MAX + 1];
336 char ifname[100];
337 char macbuf[6*3];
338 __u8 reg_type;
339
340 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
341 genlmsg_attrlen(gnlh, 0), NULL);
342
343 switch (gnlh->cmd) {
344 case NL80211_CMD_NEW_WIPHY:
345 printf("wiphy rename: phy #%d to %s\n",
346 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
347 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
348 break;
349 case NL80211_CMD_NEW_SCAN_RESULTS:
350 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
351 printf("scan finished on %s (phy #%d)\n",
352 ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
353 break;
354 case NL80211_CMD_SCAN_ABORTED:
355 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
356 printf("scan aborted on %s (phy #%d)\n",
357 ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
358 break;
359 case NL80211_CMD_REG_CHANGE:
360
361 printf("regulatory domain change: ");
362
363 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
364
365 switch (reg_type) {
366 case NL80211_REGDOM_TYPE_COUNTRY:
367 printf("set to %s by %s request",
368 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
369 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
370 if (tb[NL80211_ATTR_WIPHY])
371 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
372 break;
373 case NL80211_REGDOM_TYPE_WORLD:
374 printf("set to world roaming by %s request",
375 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
376 break;
377 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
378 printf("custom world roaming rules in place on phy%d by %s request",
379 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
380 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
381 break;
382 case NL80211_REGDOM_TYPE_INTERSECTION:
383 printf("intersection used due to a request made by %s",
384 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
385 if (tb[NL80211_ATTR_WIPHY])
386 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
387 break;
388 default:
389 printf("unknown source (upgrade this utility)");
390 break;
391 }
392
393 printf("\n");
394 break;
395 case NL80211_CMD_JOIN_IBSS:
396 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
397 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
398 printf("IBSS %s joined on %s (phy #%d)\n",
399 macbuf, ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
400 break;
401 default:
402 printf("unknown event: %d\n", gnlh->cmd);
403 break;
404 }
405
406 return NL_SKIP;
407 }
408
409 struct wait_event {
410 int n_cmds;
411 const __u32 *cmds;
412 __u32 cmd;
413 };
414
415 static int wait_event(struct nl_msg *msg, void *arg)
416 {
417 struct wait_event *wait = arg;
418 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
419 int i;
420
421 for (i = 0; i < wait->n_cmds; i++) {
422 if (gnlh->cmd == wait->cmds[i]) {
423 wait->cmd = gnlh->cmd;
424 }
425 }
426
427 return NL_SKIP;
428 }
429
430 __u32 listen_events(struct nl80211_state *state,
431 const int n_waits, const __u32 *waits)
432 {
433 int mcid, ret;
434 struct nl_cb *cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
435 struct wait_event wait_ev;
436
437 if (!cb) {
438 fprintf(stderr, "failed to allocate netlink callbacks\n");
439 return -ENOMEM;
440 }
441
442 /* Configuration multicast group */
443 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
444 if (mcid < 0)
445 return mcid;
446
447 ret = nl_socket_add_membership(state->nl_sock, mcid);
448 if (ret)
449 return ret;
450
451 /* Scan multicast group */
452 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
453 if (mcid >= 0) {
454 ret = nl_socket_add_membership(state->nl_sock, mcid);
455 if (ret)
456 return ret;
457 }
458
459 /* Regulatory multicast group */
460 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
461 if (mcid >= 0) {
462 ret = nl_socket_add_membership(state->nl_sock, mcid);
463 if (ret)
464 return ret;
465 }
466
467 /* MLME multicast group */
468 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
469 if (mcid >= 0) {
470 ret = nl_socket_add_membership(state->nl_sock, mcid);
471 if (ret)
472 return ret;
473 }
474
475 /* no sequence checking for multicast messages */
476 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
477
478 if (n_waits && waits) {
479 wait_ev.cmds = waits;
480 wait_ev.n_cmds = n_waits;
481 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
482 } else {
483 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, NULL);
484 }
485
486 wait_ev.cmd = 0;
487
488 while (!wait_ev.cmd)
489 nl_recvmsgs(state->nl_sock, cb);
490
491 nl_cb_put(cb);
492
493 return wait_ev.cmd;
494 }
495
496 int main(int argc, char **argv)
497 {
498 struct nl80211_state nlstate;
499 int err;
500 const char *argv0;
501
502 /* calculate command size including padding */
503 cmd_size = abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
504 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0);
505 /* strip off self */
506 argc--;
507 argv0 = *argv++;
508
509 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
510 debug = 1;
511 argc--;
512 argv++;
513 }
514
515 if (argc > 0 && strcmp(*argv, "--version") == 0) {
516 version();
517 return 0;
518 }
519
520 if (argc == 0 || strcmp(*argv, "help") == 0) {
521 usage(argv0);
522 return 0;
523 }
524
525 err = nl80211_init(&nlstate);
526 if (err)
527 return 1;
528
529 if (strcmp(*argv, "event") == 0) {
530 if (argc != 1)
531 err = 1;
532 else
533 err = listen_events(&nlstate, 0, NULL);
534 } else if (strcmp(*argv, "dev") == 0 && argc > 1) {
535 argc--;
536 argv++;
537 err = handle_cmd(&nlstate, II_NETDEV, argc, argv);
538 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
539 if (strlen(*argv) == 3) {
540 argc--;
541 argv++;
542 err = handle_cmd(&nlstate, II_PHY_NAME, argc, argv);
543 } else if (*(*argv + 3) == '#')
544 err = handle_cmd(&nlstate, II_PHY_IDX, argc, argv);
545 else
546 err = 1;
547 } else
548 err = handle_cmd(&nlstate, II_NONE, argc, argv);
549
550 if (err == 1)
551 usage(argv0);
552 if (err < 0)
553 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
554
555 nl80211_cleanup(&nlstate);
556
557 return err;
558 }