]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
more verbose output for interface combinations
[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 int iw_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 static int cmd_size;
98
99 extern struct cmd __start___cmd;
100 extern 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
107 static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
108 {
109 const char *start, *lend, *end;
110
111 printf("%s", indent);
112
113 switch (cmd->idby) {
114 case CIB_NONE:
115 break;
116 case CIB_PHY:
117 printf("phy <phyname> ");
118 break;
119 case CIB_NETDEV:
120 printf("dev <devname> ");
121 break;
122 }
123 if (cmd->parent && cmd->parent->name)
124 printf("%s ", cmd->parent->name);
125 printf("%s", cmd->name);
126 if (cmd->args)
127 printf(" %s", cmd->args);
128 printf("\n");
129
130 if (!full || !cmd->help)
131 return;
132
133 /* hack */
134 if (strlen(indent))
135 indent = "\t\t";
136 else
137 printf("\n");
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;
146 printf("%s", indent);
147 printf("%.*s\n", (int)(lend - start), start);
148 start = lend + 1;
149 } while (end != lend);
150
151 printf("\n");
152 }
153
154 static void usage_options(void)
155 {
156 printf("Options:\n");
157 printf("\t--debug\t\tenable netlink debugging\n");
158 }
159
160 static const char *argv0;
161
162 static void usage(bool full)
163 {
164 const struct cmd *section, *cmd;
165
166 printf("Usage:\t%s [options] command\n", argv0);
167 usage_options();
168 printf("\t--version\tshow version (%s)\n", iw_version);
169 printf("Commands:\n");
170 for_each_cmd(section) {
171 if (section->parent)
172 continue;
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 }
184 }
185 printf("\nYou can omit the 'phy' or 'dev' if "
186 "the identification is unique,\n"
187 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
188 "(Don't when scripting.)\n\n"
189 "Do NOT screenscrape this tool, we don't "
190 "consider its output stable.\n\n");
191 }
192
193 static 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 }
200 TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
201 "Print usage for each command.");
202
203 static void usage_cmd(const struct cmd *cmd)
204 {
205 printf("Usage:\t%s [options] ", argv0);
206 __usage_cmd(cmd, "", true);
207 usage_options();
208 }
209
210 static void version(void)
211 {
212 printf("iw version %s\n", iw_version);
213 }
214
215 static 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);
223 if (fd < 0)
224 return -1;
225 pos = read(fd, buf, sizeof(buf) - 1);
226 if (pos < 0) {
227 close(fd);
228 return -1;
229 }
230 buf[pos] = '\0';
231 close(fd);
232 return atoi(buf);
233 }
234
235 static 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
243 static int finish_handler(struct nl_msg *msg, void *arg)
244 {
245 int *ret = arg;
246 *ret = 0;
247 return NL_SKIP;
248 }
249
250 static int ack_handler(struct nl_msg *msg, void *arg)
251 {
252 int *ret = arg;
253 *ret = 0;
254 return NL_STOP;
255 }
256
257 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
258 int argc, char **argv, const struct cmd **cmdout)
259 {
260 const struct cmd *cmd, *match = NULL, *sectcmd;
261 struct nl_cb *cb;
262 struct nl_cb *s_cb;
263 struct nl_msg *msg;
264 int devidx = 0;
265 int err, o_argc;
266 const char *command, *section;
267 char *tmp, **o_argv;
268 enum command_identify_by command_idby = CIB_NONE;
269
270 if (argc <= 1 && idby != II_NONE)
271 return 1;
272
273 o_argc = argc;
274 o_argv = argv;
275
276 switch (idby) {
277 case II_PHY_IDX:
278 command_idby = CIB_PHY;
279 devidx = strtoul(*argv + 4, &tmp, 0);
280 if (*tmp != '\0')
281 return 1;
282 argc--;
283 argv++;
284 break;
285 case II_PHY_NAME:
286 command_idby = CIB_PHY;
287 devidx = phy_lookup(*argv);
288 argc--;
289 argv++;
290 break;
291 case II_NETDEV:
292 command_idby = CIB_NETDEV;
293 devidx = if_nametoindex(*argv);
294 if (devidx == 0)
295 devidx = -1;
296 argc--;
297 argv++;
298 break;
299 default:
300 break;
301 }
302
303 if (devidx < 0)
304 return -errno;
305
306 section = *argv;
307 argc--;
308 argv++;
309
310 for_each_cmd(sectcmd) {
311 if (sectcmd->parent)
312 continue;
313 /* ok ... bit of a hack for the dupe 'info' section */
314 if (match && sectcmd->idby != command_idby)
315 continue;
316 if (strcmp(sectcmd->name, section) == 0)
317 match = sectcmd;
318 }
319
320 sectcmd = match;
321 match = NULL;
322 if (!sectcmd)
323 return 1;
324
325 if (argc > 0) {
326 command = *argv;
327
328 for_each_cmd(cmd) {
329 if (!cmd->handler)
330 continue;
331 if (cmd->parent != sectcmd)
332 continue;
333 if (cmd->idby != command_idby)
334 continue;
335 if (strcmp(cmd->name, command))
336 continue;
337 if (argc > 1 && !cmd->args)
338 continue;
339 match = cmd;
340 break;
341 }
342
343 if (match) {
344 argc--;
345 argv++;
346 }
347 }
348
349 if (match)
350 cmd = match;
351 else {
352 /* Use the section itself, if possible. */
353 cmd = sectcmd;
354 if (argc && !cmd->args)
355 return 1;
356 if (cmd->idby != command_idby)
357 return 1;
358 if (!cmd->handler)
359 return 1;
360 }
361
362 if (cmd->selector) {
363 cmd = cmd->selector(argc, argv);
364 if (!cmd)
365 return 1;
366 }
367
368 if (cmdout)
369 *cmdout = cmd;
370
371 if (!cmd->cmd) {
372 argc = o_argc;
373 argv = o_argv;
374 return cmd->handler(state, NULL, NULL, argc, argv);
375 }
376
377 msg = nlmsg_alloc();
378 if (!msg) {
379 fprintf(stderr, "failed to allocate netlink message\n");
380 return 2;
381 }
382
383 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
384 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
385 if (!cb || !s_cb) {
386 fprintf(stderr, "failed to allocate netlink callbacks\n");
387 err = 2;
388 goto out_free_msg;
389 }
390
391 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
392 cmd->nl_msg_flags, cmd->cmd, 0);
393
394 switch (command_idby) {
395 case CIB_PHY:
396 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
397 break;
398 case CIB_NETDEV:
399 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
400 break;
401 default:
402 break;
403 }
404
405 err = cmd->handler(state, cb, msg, argc, argv);
406 if (err)
407 goto out;
408
409 nl_socket_set_cb(state->nl_sock, s_cb);
410
411 err = nl_send_auto_complete(state->nl_sock, msg);
412 if (err < 0)
413 goto out;
414
415 err = 1;
416
417 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
418 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
419 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
420
421 while (err > 0)
422 nl_recvmsgs(state->nl_sock, cb);
423 out:
424 nl_cb_put(cb);
425 out_free_msg:
426 nlmsg_free(msg);
427 return err;
428 nla_put_failure:
429 fprintf(stderr, "building message failed\n");
430 return 2;
431 }
432
433 int handle_cmd(struct nl80211_state *state, enum id_input idby,
434 int argc, char **argv)
435 {
436 return __handle_cmd(state, idby, argc, argv, NULL);
437 }
438
439 int main(int argc, char **argv)
440 {
441 struct nl80211_state nlstate;
442 int err;
443 const struct cmd *cmd = NULL;
444
445 /* calculate command size including padding */
446 cmd_size = abs((long)&__section_set - (long)&__section_get);
447 /* strip off self */
448 argc--;
449 argv0 = *argv++;
450
451 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
452 iw_debug = 1;
453 argc--;
454 argv++;
455 }
456
457 if (argc > 0 && strcmp(*argv, "--version") == 0) {
458 version();
459 return 0;
460 }
461
462 /* need to treat "help" command specially so it works w/o nl80211 */
463 if (argc == 0 || strcmp(*argv, "help") == 0) {
464 usage(argc != 0);
465 return 0;
466 }
467
468 err = nl80211_init(&nlstate);
469 if (err)
470 return 1;
471
472 if (strcmp(*argv, "dev") == 0 && argc > 1) {
473 argc--;
474 argv++;
475 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
476 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
477 if (strlen(*argv) == 3) {
478 argc--;
479 argv++;
480 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
481 } else if (*(*argv + 3) == '#')
482 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
483 else
484 goto detect;
485 } else {
486 int idx;
487 enum id_input idby = II_NONE;
488 detect:
489 if ((idx = if_nametoindex(argv[0])) != 0)
490 idby = II_NETDEV;
491 else if ((idx = phy_lookup(argv[0])) >= 0)
492 idby = II_PHY_NAME;
493 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
494 }
495
496 if (err == 1) {
497 if (cmd)
498 usage_cmd(cmd);
499 else
500 usage(false);
501 } else if (err < 0)
502 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
503
504 nl80211_cleanup(&nlstate);
505
506 return err;
507 }