]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
98c0852153c02635cf9b85cf65bc7cbb0c76e881
[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 enum id_input id)
194 {
195 exit(3);
196 }
197 TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
198 "Print usage for all or a specific command, e.g.\n"
199 "\"help wowlan\" or \"help wowlan enable\".");
200
201 static void usage_cmd(const struct cmd *cmd)
202 {
203 printf("Usage:\t%s [options] ", argv0);
204 __usage_cmd(cmd, "", true);
205 usage_options();
206 }
207
208 static void version(void)
209 {
210 printf("iw version %s\n", iw_version);
211 }
212
213 static int phy_lookup(char *name)
214 {
215 char buf[200];
216 int fd, pos;
217
218 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
219
220 fd = open(buf, O_RDONLY);
221 if (fd < 0)
222 return -1;
223 pos = read(fd, buf, sizeof(buf) - 1);
224 if (pos < 0) {
225 close(fd);
226 return -1;
227 }
228 buf[pos] = '\0';
229 close(fd);
230 return atoi(buf);
231 }
232
233 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
234 void *arg)
235 {
236 int *ret = arg;
237 *ret = err->error;
238 return NL_STOP;
239 }
240
241 static int finish_handler(struct nl_msg *msg, void *arg)
242 {
243 int *ret = arg;
244 *ret = 0;
245 return NL_SKIP;
246 }
247
248 static int ack_handler(struct nl_msg *msg, void *arg)
249 {
250 int *ret = arg;
251 *ret = 0;
252 return NL_STOP;
253 }
254
255 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
256 int argc, char **argv, const struct cmd **cmdout)
257 {
258 const struct cmd *cmd, *match = NULL, *sectcmd;
259 struct nl_cb *cb;
260 struct nl_cb *s_cb;
261 struct nl_msg *msg;
262 signed long long devidx = 0;
263 int err, o_argc;
264 const char *command, *section;
265 char *tmp, **o_argv;
266 enum command_identify_by command_idby = CIB_NONE;
267
268 if (argc <= 1 && idby != II_NONE)
269 return 1;
270
271 o_argc = argc;
272 o_argv = argv;
273
274 switch (idby) {
275 case II_PHY_IDX:
276 command_idby = CIB_PHY;
277 devidx = strtoul(*argv + 4, &tmp, 0);
278 if (*tmp != '\0')
279 return 1;
280 argc--;
281 argv++;
282 break;
283 case II_PHY_NAME:
284 command_idby = CIB_PHY;
285 devidx = phy_lookup(*argv);
286 argc--;
287 argv++;
288 break;
289 case II_NETDEV:
290 command_idby = CIB_NETDEV;
291 devidx = if_nametoindex(*argv);
292 if (devidx == 0)
293 devidx = -1;
294 argc--;
295 argv++;
296 break;
297 case II_WDEV:
298 command_idby = CIB_WDEV;
299 devidx = strtoll(*argv, &tmp, 0);
300 if (*tmp != '\0')
301 return 1;
302 argc--;
303 argv++;
304 default:
305 break;
306 }
307
308 if (devidx < 0)
309 return -errno;
310
311 section = *argv;
312 argc--;
313 argv++;
314
315 for_each_cmd(sectcmd) {
316 if (sectcmd->parent)
317 continue;
318 /* ok ... bit of a hack for the dupe 'info' section */
319 if (match && sectcmd->idby != command_idby)
320 continue;
321 if (strcmp(sectcmd->name, section) == 0)
322 match = sectcmd;
323 }
324
325 sectcmd = match;
326 match = NULL;
327 if (!sectcmd)
328 return 1;
329
330 if (argc > 0) {
331 command = *argv;
332
333 for_each_cmd(cmd) {
334 if (!cmd->handler)
335 continue;
336 if (cmd->parent != sectcmd)
337 continue;
338 if (cmd->idby != command_idby)
339 continue;
340 if (strcmp(cmd->name, command))
341 continue;
342 if (argc > 1 && !cmd->args)
343 continue;
344 match = cmd;
345 break;
346 }
347
348 if (match) {
349 argc--;
350 argv++;
351 }
352 }
353
354 if (match)
355 cmd = match;
356 else {
357 /* Use the section itself, if possible. */
358 cmd = sectcmd;
359 if (argc && !cmd->args)
360 return 1;
361 if (cmd->idby != command_idby)
362 return 1;
363 if (!cmd->handler)
364 return 1;
365 }
366
367 if (cmd->selector) {
368 cmd = cmd->selector(argc, argv);
369 if (!cmd)
370 return 1;
371 }
372
373 if (cmdout)
374 *cmdout = cmd;
375
376 if (!cmd->cmd) {
377 argc = o_argc;
378 argv = o_argv;
379 return cmd->handler(state, NULL, NULL, argc, argv, idby);
380 }
381
382 msg = nlmsg_alloc();
383 if (!msg) {
384 fprintf(stderr, "failed to allocate netlink message\n");
385 return 2;
386 }
387
388 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
389 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
390 if (!cb || !s_cb) {
391 fprintf(stderr, "failed to allocate netlink callbacks\n");
392 err = 2;
393 goto out_free_msg;
394 }
395
396 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
397 cmd->nl_msg_flags, cmd->cmd, 0);
398
399 switch (command_idby) {
400 case CIB_PHY:
401 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
402 break;
403 case CIB_NETDEV:
404 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
405 break;
406 case CIB_WDEV:
407 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
408 break;
409 default:
410 break;
411 }
412
413 err = cmd->handler(state, cb, msg, argc, argv, idby);
414 if (err)
415 goto out;
416
417 nl_socket_set_cb(state->nl_sock, s_cb);
418
419 err = nl_send_auto_complete(state->nl_sock, msg);
420 if (err < 0)
421 goto out;
422
423 err = 1;
424
425 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
426 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
427 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
428
429 while (err > 0)
430 nl_recvmsgs(state->nl_sock, cb);
431 out:
432 nl_cb_put(cb);
433 out_free_msg:
434 nlmsg_free(msg);
435 return err;
436 nla_put_failure:
437 fprintf(stderr, "building message failed\n");
438 return 2;
439 }
440
441 int handle_cmd(struct nl80211_state *state, enum id_input idby,
442 int argc, char **argv)
443 {
444 return __handle_cmd(state, idby, argc, argv, NULL);
445 }
446
447 int main(int argc, char **argv)
448 {
449 struct nl80211_state nlstate;
450 int err;
451 const struct cmd *cmd = NULL;
452
453 /* calculate command size including padding */
454 cmd_size = abs((long)&__section_set - (long)&__section_get);
455 /* strip off self */
456 argc--;
457 argv0 = *argv++;
458
459 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
460 iw_debug = 1;
461 argc--;
462 argv++;
463 }
464
465 if (argc > 0 && strcmp(*argv, "--version") == 0) {
466 version();
467 return 0;
468 }
469
470 /* need to treat "help" command specially so it works w/o nl80211 */
471 if (argc == 0 || strcmp(*argv, "help") == 0) {
472 usage(argc - 1, argv + 1);
473 return 0;
474 }
475
476 err = nl80211_init(&nlstate);
477 if (err)
478 return 1;
479
480 if (strcmp(*argv, "dev") == 0 && argc > 1) {
481 argc--;
482 argv++;
483 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
484 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
485 if (strlen(*argv) == 3) {
486 argc--;
487 argv++;
488 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
489 } else if (*(*argv + 3) == '#')
490 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
491 else
492 goto detect;
493 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
494 argc--;
495 argv++;
496 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
497 } else {
498 int idx;
499 enum id_input idby = II_NONE;
500 detect:
501 if ((idx = if_nametoindex(argv[0])) != 0)
502 idby = II_NETDEV;
503 else if ((idx = phy_lookup(argv[0])) >= 0)
504 idby = II_PHY_NAME;
505 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
506 }
507
508 if (err == 1) {
509 if (cmd)
510 usage_cmd(cmd);
511 else
512 usage(0, NULL);
513 } else if (err < 0)
514 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
515
516 nl80211_cleanup(&nlstate);
517
518 return err;
519 }