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