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