]> git.ipfire.org Git - thirdparty/iw.git/blame - iw.c
iw: Add support for showing Beacon IEs and TIM IE
[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>
bd396f2a 16
cad53b3f
JB
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>
cad53b3f 22
f408e01b 23#include "nl80211.h"
cad53b3f
JB
24#include "iw.h"
25
dfd13ee5
PE
26#ifndef CONFIG_LIBNL20
27/* libnl 2.0 compatibility code */
28
29static inline struct nl_handle *nl_socket_alloc(void)
30{
31 return nl_handle_alloc();
32}
33
57077d64 34static inline void nl_socket_free(struct nl_sock *h)
dfd13ee5
PE
35{
36 nl_handle_destroy(h);
37}
38
57077d64 39static inline int __genl_ctrl_alloc_cache(struct nl_sock *h, struct nl_cache **cache)
dfd13ee5
PE
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
957a0f07 50int iw_debug = 0;
cad53b3f
JB
51
52static int nl80211_init(struct nl80211_state *state)
53{
54 int err;
55
57077d64
PE
56 state->nl_sock = nl_socket_alloc();
57 if (!state->nl_sock) {
58 fprintf(stderr, "Failed to allocate netlink socket.\n");
cad53b3f
JB
59 return -ENOMEM;
60 }
61
57077d64 62 if (genl_connect(state->nl_sock)) {
cad53b3f
JB
63 fprintf(stderr, "Failed to connect to generic netlink.\n");
64 err = -ENOLINK;
65 goto out_handle_destroy;
66 }
67
57077d64 68 if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
cad53b3f
JB
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:
57077d64 86 nl_socket_free(state->nl_sock);
cad53b3f
JB
87 return err;
88}
89
90static void nl80211_cleanup(struct nl80211_state *state)
91{
92 genl_family_put(state->nl80211);
93 nl_cache_free(state->nl_cache);
57077d64 94 nl_socket_free(state->nl_sock);
cad53b3f
JB
95}
96
403b9c83
JB
97static int cmd_size;
98
4698bfc2
JB
99extern struct cmd __start___cmd;
100extern struct cmd __stop___cmd;
101
102#define for_each_cmd(_cmd) \
103 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
104 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
105
106
107static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
3bb116da 108{
01ae06f9
JB
109 const char *start, *lend, *end;
110
bd663893 111 printf("%s", indent);
4f0cae73 112
3bb116da
JB
113 switch (cmd->idby) {
114 case CIB_NONE:
3bb116da
JB
115 break;
116 case CIB_PHY:
bd663893 117 printf("phy <phyname> ");
3bb116da
JB
118 break;
119 case CIB_NETDEV:
bd663893 120 printf("dev <devname> ");
3bb116da
JB
121 break;
122 }
4698bfc2
JB
123 if (cmd->parent && cmd->parent->name)
124 printf("%s ", cmd->parent->name);
bd663893 125 printf("%s", cmd->name);
3bb116da 126 if (cmd->args)
bd663893
HS
127 printf(" %s", cmd->args);
128 printf("\n");
01ae06f9
JB
129
130 if (!full || !cmd->help)
131 return;
132
133 /* hack */
134 if (strlen(indent))
135 indent = "\t\t";
136 else
bd663893 137 printf("\n");
01ae06f9
JB
138
139 /* print line by line */
140 start = cmd->help;
141 end = strchr(start, '\0');
142 do {
143 lend = strchr(start, '\n');
144 if (!lend)
145 lend = end;
bd663893
HS
146 printf("%s", indent);
147 printf("%.*s\n", (int)(lend - start), start);
01ae06f9
JB
148 start = lend + 1;
149 } while (end != lend);
150
bd663893 151 printf("\n");
3bb116da
JB
152}
153
4f0cae73
JB
154static void usage_options(void)
155{
bd663893
HS
156 printf("Options:\n");
157 printf("\t--debug\t\tenable netlink debugging\n");
4f0cae73
JB
158}
159
01ae06f9
JB
160static const char *argv0;
161
162static void usage(bool full)
bd396f2a 163{
4698bfc2 164 const struct cmd *section, *cmd;
bd396f2a 165
bd663893 166 printf("Usage:\t%s [options] command\n", argv0);
4f0cae73 167 usage_options();
bd663893
HS
168 printf("\t--version\tshow version (%s)\n", iw_version);
169 printf("Commands:\n");
4698bfc2
JB
170 for_each_cmd(section) {
171 if (section->parent)
403b9c83 172 continue;
4698bfc2
JB
173
174 if (section->handler && !section->hidden)
175 __usage_cmd(section, "\t", full);
176
177 for_each_cmd(cmd) {
178 if (section != cmd->parent)
179 continue;
180 if (!cmd->handler || cmd->hidden)
181 continue;
182 __usage_cmd(cmd, "\t", full);
183 }
bd396f2a 184 }
bd663893 185 printf("\nYou can omit the 'phy' or 'dev' if "
f4ec76d0 186 "the identification is unique,\n"
8aefee9a 187 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
fbdb8d05
JB
188 "(Don't when scripting.)\n\n"
189 "Do NOT screenscrape this tool, we don't "
190 "consider its output stable.\n\n");
bd396f2a
JB
191}
192
01ae06f9
JB
193static int print_help(struct nl80211_state *state,
194 struct nl_cb *cb,
195 struct nl_msg *msg,
196 int argc, char **argv)
197{
198 exit(3);
199}
200TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
201 "Print usage for each command.");
202
4698bfc2 203static void usage_cmd(const struct cmd *cmd)
4f0cae73 204{
bd663893 205 printf("Usage:\t%s [options] ", argv0);
01ae06f9 206 __usage_cmd(cmd, "", true);
4f0cae73
JB
207 usage_options();
208}
209
d711f013
JB
210static void version(void)
211{
133b069f 212 printf("iw version %s\n", iw_version);
d711f013
JB
213}
214
bd396f2a
JB
215static int phy_lookup(char *name)
216{
217 char buf[200];
218 int fd, pos;
219
220 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
221
222 fd = open(buf, O_RDONLY);
989e97c2
JB
223 if (fd < 0)
224 return -1;
bd396f2a 225 pos = read(fd, buf, sizeof(buf) - 1);
8f253ee2
ES
226 if (pos < 0) {
227 close(fd);
bd396f2a 228 return -1;
8f253ee2 229 }
bd396f2a 230 buf[pos] = '\0';
8f253ee2 231 close(fd);
bd396f2a
JB
232 return atoi(buf);
233}
234
70391ccf
JB
235static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
236 void *arg)
237{
238 int *ret = arg;
239 *ret = err->error;
240 return NL_STOP;
241}
242
561c5b7e
JB
243static int finish_handler(struct nl_msg *msg, void *arg)
244{
59c418c0
JB
245 int *ret = arg;
246 *ret = 0;
561c5b7e
JB
247 return NL_SKIP;
248}
249
250static int ack_handler(struct nl_msg *msg, void *arg)
70391ccf
JB
251{
252 int *ret = arg;
253 *ret = 0;
254 return NL_STOP;
255}
256
4f0cae73 257static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
4698bfc2 258 int argc, char **argv, const struct cmd **cmdout)
45c7212c 259{
4698bfc2 260 const struct cmd *cmd, *match = NULL, *sectcmd;
7c37a24d 261 struct nl_cb *cb;
bd396f2a
JB
262 struct nl_msg *msg;
263 int devidx = 0;
bb60b4ae 264 int err, o_argc;
bd396f2a 265 const char *command, *section;
bb60b4ae 266 char *tmp, **o_argv;
9927363c 267 enum command_identify_by command_idby = CIB_NONE;
45c7212c 268
9927363c 269 if (argc <= 1 && idby != II_NONE)
5e75fd04 270 return 1;
45c7212c 271
bb60b4ae
JB
272 o_argc = argc;
273 o_argv = argv;
274
bd396f2a 275 switch (idby) {
9927363c
JB
276 case II_PHY_IDX:
277 command_idby = CIB_PHY;
278 devidx = strtoul(*argv + 4, &tmp, 0);
279 if (*tmp != '\0')
280 return 1;
281 argc--;
282 argv++;
283 break;
284 case II_PHY_NAME:
285 command_idby = CIB_PHY;
bd396f2a
JB
286 devidx = phy_lookup(*argv);
287 argc--;
288 argv++;
289 break;
9927363c
JB
290 case II_NETDEV:
291 command_idby = CIB_NETDEV;
bd396f2a 292 devidx = if_nametoindex(*argv);
989e97c2
JB
293 if (devidx == 0)
294 devidx = -1;
bd396f2a
JB
295 argc--;
296 argv++;
297 break;
298 default:
299 break;
300 }
301
989e97c2
JB
302 if (devidx < 0)
303 return -errno;
304
4698bfc2 305 section = *argv;
bd396f2a
JB
306 argc--;
307 argv++;
308
4698bfc2
JB
309 for_each_cmd(sectcmd) {
310 if (sectcmd->parent)
403b9c83 311 continue;
4698bfc2
JB
312 /* ok ... bit of a hack for the dupe 'info' section */
313 if (match && sectcmd->idby != command_idby)
bd396f2a 314 continue;
4698bfc2
JB
315 if (strcmp(sectcmd->name, section) == 0)
316 match = sectcmd;
bd396f2a 317 }
45c7212c 318
4698bfc2
JB
319 sectcmd = match;
320 match = NULL;
321 if (!sectcmd)
5e75fd04 322 return 1;
45c7212c 323
4698bfc2
JB
324 if (argc > 0) {
325 command = *argv;
326
327 for_each_cmd(cmd) {
328 if (!cmd->handler)
329 continue;
330 if (cmd->parent != sectcmd)
331 continue;
332 if (cmd->idby != command_idby)
333 continue;
334 if (strcmp(cmd->name, command))
335 continue;
336 if (argc > 1 && !cmd->args)
337 continue;
338 match = cmd;
339 break;
340 }
341
342 if (match) {
343 argc--;
344 argv++;
345 }
346 }
347
348 if (match)
349 cmd = match;
350 else {
351 /* Use the section itself, if possible. */
352 cmd = sectcmd;
353 if (argc && !cmd->args)
354 return 1;
355 if (cmd->idby != command_idby)
356 return 1;
357 if (!cmd->handler)
358 return 1;
359 }
360
4f0cae73
JB
361 if (cmdout)
362 *cmdout = cmd;
363
bb60b4ae
JB
364 if (!cmd->cmd) {
365 argc = o_argc;
366 argv = o_argv;
367 return cmd->handler(state, NULL, NULL, argc, argv);
368 }
369
bd396f2a
JB
370 msg = nlmsg_alloc();
371 if (!msg) {
70391ccf
JB
372 fprintf(stderr, "failed to allocate netlink message\n");
373 return 2;
374 }
375
957a0f07 376 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
70391ccf
JB
377 if (!cb) {
378 fprintf(stderr, "failed to allocate netlink callbacks\n");
379 err = 2;
380 goto out_free_msg;
bd396f2a 381 }
45c7212c 382
bd396f2a
JB
383 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
384 cmd->nl_msg_flags, cmd->cmd, 0);
385
9927363c 386 switch (command_idby) {
bd396f2a
JB
387 case CIB_PHY:
388 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
389 break;
390 case CIB_NETDEV:
391 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
392 break;
393 default:
394 break;
395 }
396
7c37a24d 397 err = cmd->handler(state, cb, msg, argc, argv);
70391ccf
JB
398 if (err)
399 goto out;
400
57077d64 401 err = nl_send_auto_complete(state->nl_sock, msg);
70391ccf
JB
402 if (err < 0)
403 goto out;
404
c5c4471a
JB
405 err = 1;
406
70391ccf 407 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
59c418c0 408 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
561c5b7e 409 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
70391ccf 410
c5c4471a 411 while (err > 0)
57077d64 412 nl_recvmsgs(state->nl_sock, cb);
70391ccf
JB
413 out:
414 nl_cb_put(cb);
415 out_free_msg:
416 nlmsg_free(msg);
417 return err;
bd396f2a
JB
418 nla_put_failure:
419 fprintf(stderr, "building message failed\n");
70391ccf 420 return 2;
45c7212c
JB
421}
422
4f0cae73
JB
423int handle_cmd(struct nl80211_state *state, enum id_input idby,
424 int argc, char **argv)
425{
426 return __handle_cmd(state, idby, argc, argv, NULL);
427}
428
cad53b3f
JB
429int main(int argc, char **argv)
430{
431 struct nl80211_state nlstate;
bd396f2a 432 int err;
4698bfc2 433 const struct cmd *cmd = NULL;
cad53b3f 434
f408e01b 435 /* calculate command size including padding */
4698bfc2 436 cmd_size = abs((long)&__section_set - (long)&__section_get);
45c7212c
JB
437 /* strip off self */
438 argc--;
1cdd9016
MK
439 argv0 = *argv++;
440
59c49f09 441 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
957a0f07 442 iw_debug = 1;
59c49f09
JB
443 argc--;
444 argv++;
445 }
446
d711f013
JB
447 if (argc > 0 && strcmp(*argv, "--version") == 0) {
448 version();
449 return 0;
450 }
451
01ae06f9 452 /* need to treat "help" command specially so it works w/o nl80211 */
bd396f2a 453 if (argc == 0 || strcmp(*argv, "help") == 0) {
01ae06f9 454 usage(argc != 0);
4a972f80 455 return 0;
1cdd9016 456 }
45c7212c 457
2bdb6bd1
JB
458 err = nl80211_init(&nlstate);
459 if (err)
460 return 1;
461
957a0f07 462 if (strcmp(*argv, "dev") == 0 && argc > 1) {
14a0380d
LR
463 argc--;
464 argv++;
4f0cae73 465 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
811ec68f 466 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
9927363c
JB
467 if (strlen(*argv) == 3) {
468 argc--;
469 argv++;
4f0cae73 470 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
9927363c 471 } else if (*(*argv + 3) == '#')
4f0cae73 472 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
9927363c 473 else
f4ec76d0
JB
474 goto detect;
475 } else {
476 int idx;
477 enum id_input idby = II_NONE;
478 detect:
479 if ((idx = if_nametoindex(argv[0])) != 0)
480 idby = II_NETDEV;
66f8ca45 481 else if ((idx = phy_lookup(argv[0])) >= 0)
f4ec76d0
JB
482 idby = II_PHY_NAME;
483 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
484 }
45c7212c 485
4f0cae73
JB
486 if (err == 1) {
487 if (cmd)
01ae06f9 488 usage_cmd(cmd);
4f0cae73 489 else
01ae06f9 490 usage(false);
4f0cae73 491 } else if (err < 0)
b49be3e1 492 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
cad53b3f
JB
493
494 nl80211_cleanup(&nlstate);
495
45c7212c 496 return err;
cad53b3f 497}