]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
allow sub-command selection
[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_msg *msg;
263 int devidx = 0;
264 int err, o_argc;
265 const char *command, *section;
266 char *tmp, **o_argv;
267 enum command_identify_by command_idby = CIB_NONE;
268
269 if (argc <= 1 && idby != II_NONE)
270 return 1;
271
272 o_argc = argc;
273 o_argv = argv;
274
275 switch (idby) {
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;
286 devidx = phy_lookup(*argv);
287 argc--;
288 argv++;
289 break;
290 case II_NETDEV:
291 command_idby = CIB_NETDEV;
292 devidx = if_nametoindex(*argv);
293 if (devidx == 0)
294 devidx = -1;
295 argc--;
296 argv++;
297 break;
298 default:
299 break;
300 }
301
302 if (devidx < 0)
303 return -errno;
304
305 section = *argv;
306 argc--;
307 argv++;
308
309 for_each_cmd(sectcmd) {
310 if (sectcmd->parent)
311 continue;
312 /* ok ... bit of a hack for the dupe 'info' section */
313 if (match && sectcmd->idby != command_idby)
314 continue;
315 if (strcmp(sectcmd->name, section) == 0)
316 match = sectcmd;
317 }
318
319 sectcmd = match;
320 match = NULL;
321 if (!sectcmd)
322 return 1;
323
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
361 if (cmd->selector) {
362 cmd = cmd->selector(argc, argv);
363 if (!cmd)
364 return 1;
365 }
366
367 if (cmdout)
368 *cmdout = cmd;
369
370 if (!cmd->cmd) {
371 argc = o_argc;
372 argv = o_argv;
373 return cmd->handler(state, NULL, NULL, argc, argv);
374 }
375
376 msg = nlmsg_alloc();
377 if (!msg) {
378 fprintf(stderr, "failed to allocate netlink message\n");
379 return 2;
380 }
381
382 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
383 if (!cb) {
384 fprintf(stderr, "failed to allocate netlink callbacks\n");
385 err = 2;
386 goto out_free_msg;
387 }
388
389 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
390 cmd->nl_msg_flags, cmd->cmd, 0);
391
392 switch (command_idby) {
393 case CIB_PHY:
394 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
395 break;
396 case CIB_NETDEV:
397 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
398 break;
399 default:
400 break;
401 }
402
403 err = cmd->handler(state, cb, msg, argc, argv);
404 if (err)
405 goto out;
406
407 err = nl_send_auto_complete(state->nl_sock, msg);
408 if (err < 0)
409 goto out;
410
411 err = 1;
412
413 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
414 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
415 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
416
417 while (err > 0)
418 nl_recvmsgs(state->nl_sock, cb);
419 out:
420 nl_cb_put(cb);
421 out_free_msg:
422 nlmsg_free(msg);
423 return err;
424 nla_put_failure:
425 fprintf(stderr, "building message failed\n");
426 return 2;
427 }
428
429 int handle_cmd(struct nl80211_state *state, enum id_input idby,
430 int argc, char **argv)
431 {
432 return __handle_cmd(state, idby, argc, argv, NULL);
433 }
434
435 int main(int argc, char **argv)
436 {
437 struct nl80211_state nlstate;
438 int err;
439 const struct cmd *cmd = NULL;
440
441 /* calculate command size including padding */
442 cmd_size = abs((long)&__section_set - (long)&__section_get);
443 /* strip off self */
444 argc--;
445 argv0 = *argv++;
446
447 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
448 iw_debug = 1;
449 argc--;
450 argv++;
451 }
452
453 if (argc > 0 && strcmp(*argv, "--version") == 0) {
454 version();
455 return 0;
456 }
457
458 /* need to treat "help" command specially so it works w/o nl80211 */
459 if (argc == 0 || strcmp(*argv, "help") == 0) {
460 usage(argc != 0);
461 return 0;
462 }
463
464 err = nl80211_init(&nlstate);
465 if (err)
466 return 1;
467
468 if (strcmp(*argv, "dev") == 0 && argc > 1) {
469 argc--;
470 argv++;
471 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
472 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
473 if (strlen(*argv) == 3) {
474 argc--;
475 argv++;
476 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
477 } else if (*(*argv + 3) == '#')
478 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
479 else
480 goto detect;
481 } else {
482 int idx;
483 enum id_input idby = II_NONE;
484 detect:
485 if ((idx = if_nametoindex(argv[0])) != 0)
486 idby = II_NETDEV;
487 else if ((idx = phy_lookup(argv[0])) >= 0)
488 idby = II_PHY_NAME;
489 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
490 }
491
492 if (err == 1) {
493 if (cmd)
494 usage_cmd(cmd);
495 else
496 usage(false);
497 } else if (err < 0)
498 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
499
500 nl80211_cleanup(&nlstate);
501
502 return err;
503 }