]> git.ipfire.org Git - thirdparty/iw.git/blame - iw.c
default install to $(PREFIX)/sbin
[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
JB
225 pos = read(fd, buf, sizeof(buf) - 1);
226 if (pos < 0)
227 return -1;
228 buf[pos] = '\0';
229 return atoi(buf);
230}
231
70391ccf
JB
232static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
233 void *arg)
234{
235 int *ret = arg;
236 *ret = err->error;
237 return NL_STOP;
238}
239
561c5b7e
JB
240static int finish_handler(struct nl_msg *msg, void *arg)
241{
59c418c0
JB
242 int *ret = arg;
243 *ret = 0;
561c5b7e
JB
244 return NL_SKIP;
245}
246
247static int ack_handler(struct nl_msg *msg, void *arg)
70391ccf
JB
248{
249 int *ret = arg;
250 *ret = 0;
251 return NL_STOP;
252}
253
4f0cae73 254static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
4698bfc2 255 int argc, char **argv, const struct cmd **cmdout)
45c7212c 256{
4698bfc2 257 const struct cmd *cmd, *match = NULL, *sectcmd;
7c37a24d 258 struct nl_cb *cb;
bd396f2a
JB
259 struct nl_msg *msg;
260 int devidx = 0;
bb60b4ae 261 int err, o_argc;
bd396f2a 262 const char *command, *section;
bb60b4ae 263 char *tmp, **o_argv;
9927363c 264 enum command_identify_by command_idby = CIB_NONE;
45c7212c 265
9927363c 266 if (argc <= 1 && idby != II_NONE)
5e75fd04 267 return 1;
45c7212c 268
bb60b4ae
JB
269 o_argc = argc;
270 o_argv = argv;
271
bd396f2a 272 switch (idby) {
9927363c
JB
273 case II_PHY_IDX:
274 command_idby = CIB_PHY;
275 devidx = strtoul(*argv + 4, &tmp, 0);
276 if (*tmp != '\0')
277 return 1;
278 argc--;
279 argv++;
280 break;
281 case II_PHY_NAME:
282 command_idby = CIB_PHY;
bd396f2a
JB
283 devidx = phy_lookup(*argv);
284 argc--;
285 argv++;
286 break;
9927363c
JB
287 case II_NETDEV:
288 command_idby = CIB_NETDEV;
bd396f2a 289 devidx = if_nametoindex(*argv);
989e97c2
JB
290 if (devidx == 0)
291 devidx = -1;
bd396f2a
JB
292 argc--;
293 argv++;
294 break;
295 default:
296 break;
297 }
298
989e97c2
JB
299 if (devidx < 0)
300 return -errno;
301
4698bfc2 302 section = *argv;
bd396f2a
JB
303 argc--;
304 argv++;
305
4698bfc2
JB
306 for_each_cmd(sectcmd) {
307 if (sectcmd->parent)
403b9c83 308 continue;
4698bfc2
JB
309 /* ok ... bit of a hack for the dupe 'info' section */
310 if (match && sectcmd->idby != command_idby)
bd396f2a 311 continue;
4698bfc2
JB
312 if (strcmp(sectcmd->name, section) == 0)
313 match = sectcmd;
bd396f2a 314 }
45c7212c 315
4698bfc2
JB
316 sectcmd = match;
317 match = NULL;
318 if (!sectcmd)
5e75fd04 319 return 1;
45c7212c 320
4698bfc2
JB
321 if (argc > 0) {
322 command = *argv;
323
324 for_each_cmd(cmd) {
325 if (!cmd->handler)
326 continue;
327 if (cmd->parent != sectcmd)
328 continue;
329 if (cmd->idby != command_idby)
330 continue;
331 if (strcmp(cmd->name, command))
332 continue;
333 if (argc > 1 && !cmd->args)
334 continue;
335 match = cmd;
336 break;
337 }
338
339 if (match) {
340 argc--;
341 argv++;
342 }
343 }
344
345 if (match)
346 cmd = match;
347 else {
348 /* Use the section itself, if possible. */
349 cmd = sectcmd;
350 if (argc && !cmd->args)
351 return 1;
352 if (cmd->idby != command_idby)
353 return 1;
354 if (!cmd->handler)
355 return 1;
356 }
357
4f0cae73
JB
358 if (cmdout)
359 *cmdout = cmd;
360
bb60b4ae
JB
361 if (!cmd->cmd) {
362 argc = o_argc;
363 argv = o_argv;
364 return cmd->handler(state, NULL, NULL, argc, argv);
365 }
366
bd396f2a
JB
367 msg = nlmsg_alloc();
368 if (!msg) {
70391ccf
JB
369 fprintf(stderr, "failed to allocate netlink message\n");
370 return 2;
371 }
372
957a0f07 373 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
70391ccf
JB
374 if (!cb) {
375 fprintf(stderr, "failed to allocate netlink callbacks\n");
376 err = 2;
377 goto out_free_msg;
bd396f2a 378 }
45c7212c 379
bd396f2a
JB
380 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
381 cmd->nl_msg_flags, cmd->cmd, 0);
382
9927363c 383 switch (command_idby) {
bd396f2a
JB
384 case CIB_PHY:
385 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
386 break;
387 case CIB_NETDEV:
388 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
389 break;
390 default:
391 break;
392 }
393
7c37a24d 394 err = cmd->handler(state, cb, msg, argc, argv);
70391ccf
JB
395 if (err)
396 goto out;
397
57077d64 398 err = nl_send_auto_complete(state->nl_sock, msg);
70391ccf
JB
399 if (err < 0)
400 goto out;
401
c5c4471a
JB
402 err = 1;
403
70391ccf 404 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
59c418c0 405 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
561c5b7e 406 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
70391ccf 407
c5c4471a 408 while (err > 0)
57077d64 409 nl_recvmsgs(state->nl_sock, cb);
70391ccf
JB
410 out:
411 nl_cb_put(cb);
412 out_free_msg:
413 nlmsg_free(msg);
414 return err;
bd396f2a
JB
415 nla_put_failure:
416 fprintf(stderr, "building message failed\n");
70391ccf 417 return 2;
45c7212c
JB
418}
419
4f0cae73
JB
420int handle_cmd(struct nl80211_state *state, enum id_input idby,
421 int argc, char **argv)
422{
423 return __handle_cmd(state, idby, argc, argv, NULL);
424}
425
cad53b3f
JB
426int main(int argc, char **argv)
427{
428 struct nl80211_state nlstate;
bd396f2a 429 int err;
4698bfc2 430 const struct cmd *cmd = NULL;
cad53b3f 431
f408e01b 432 /* calculate command size including padding */
4698bfc2 433 cmd_size = abs((long)&__section_set - (long)&__section_get);
45c7212c
JB
434 /* strip off self */
435 argc--;
1cdd9016
MK
436 argv0 = *argv++;
437
59c49f09 438 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
957a0f07 439 iw_debug = 1;
59c49f09
JB
440 argc--;
441 argv++;
442 }
443
d711f013
JB
444 if (argc > 0 && strcmp(*argv, "--version") == 0) {
445 version();
446 return 0;
447 }
448
01ae06f9 449 /* need to treat "help" command specially so it works w/o nl80211 */
bd396f2a 450 if (argc == 0 || strcmp(*argv, "help") == 0) {
01ae06f9 451 usage(argc != 0);
4a972f80 452 return 0;
1cdd9016 453 }
45c7212c 454
2bdb6bd1
JB
455 err = nl80211_init(&nlstate);
456 if (err)
457 return 1;
458
957a0f07 459 if (strcmp(*argv, "dev") == 0 && argc > 1) {
14a0380d
LR
460 argc--;
461 argv++;
4f0cae73 462 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
811ec68f 463 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
9927363c
JB
464 if (strlen(*argv) == 3) {
465 argc--;
466 argv++;
4f0cae73 467 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
9927363c 468 } else if (*(*argv + 3) == '#')
4f0cae73 469 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
9927363c 470 else
f4ec76d0
JB
471 goto detect;
472 } else {
473 int idx;
474 enum id_input idby = II_NONE;
475 detect:
476 if ((idx = if_nametoindex(argv[0])) != 0)
477 idby = II_NETDEV;
66f8ca45 478 else if ((idx = phy_lookup(argv[0])) >= 0)
f4ec76d0
JB
479 idby = II_PHY_NAME;
480 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
481 }
45c7212c 482
4f0cae73
JB
483 if (err == 1) {
484 if (cmd)
01ae06f9 485 usage_cmd(cmd);
4f0cae73 486 else
01ae06f9 487 usage(false);
4f0cae73 488 } else if (err < 0)
b49be3e1 489 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
cad53b3f
JB
490
491 nl80211_cleanup(&nlstate);
492
45c7212c 493 return err;
cad53b3f 494}