]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
update nl80211.h
[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("\nCommands that use the netdev ('dev') can also be given the\n"
182 "'wdev' instead to identify the device.\n");
183 printf("\nYou can omit the 'phy' or 'dev' if "
184 "the identification is unique,\n"
185 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
186 "(Don't when scripting.)\n\n"
187 "Do NOT screenscrape this tool, we don't "
188 "consider its output stable.\n\n");
189 }
190
191 static int print_help(struct nl80211_state *state,
192 struct nl_cb *cb,
193 struct nl_msg *msg,
194 int argc, char **argv,
195 enum id_input id)
196 {
197 exit(3);
198 }
199 TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
200 "Print usage for all or a specific command, e.g.\n"
201 "\"help wowlan\" or \"help wowlan enable\".");
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 signed long long 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 case II_WDEV:
300 command_idby = CIB_WDEV;
301 devidx = strtoll(*argv, &tmp, 0);
302 if (*tmp != '\0')
303 return 1;
304 argc--;
305 argv++;
306 default:
307 break;
308 }
309
310 if (devidx < 0)
311 return -errno;
312
313 section = *argv;
314 argc--;
315 argv++;
316
317 for_each_cmd(sectcmd) {
318 if (sectcmd->parent)
319 continue;
320 /* ok ... bit of a hack for the dupe 'info' section */
321 if (match && sectcmd->idby != command_idby)
322 continue;
323 if (strcmp(sectcmd->name, section) == 0)
324 match = sectcmd;
325 }
326
327 sectcmd = match;
328 match = NULL;
329 if (!sectcmd)
330 return 1;
331
332 if (argc > 0) {
333 command = *argv;
334
335 for_each_cmd(cmd) {
336 if (!cmd->handler)
337 continue;
338 if (cmd->parent != sectcmd)
339 continue;
340 /*
341 * ignore mismatch id by, but allow WDEV
342 * in place of NETDEV
343 */
344 if (cmd->idby != command_idby &&
345 !(cmd->idby == CIB_NETDEV &&
346 command_idby == CIB_WDEV))
347 continue;
348 if (strcmp(cmd->name, command))
349 continue;
350 if (argc > 1 && !cmd->args)
351 continue;
352 match = cmd;
353 break;
354 }
355
356 if (match) {
357 argc--;
358 argv++;
359 }
360 }
361
362 if (match)
363 cmd = match;
364 else {
365 /* Use the section itself, if possible. */
366 cmd = sectcmd;
367 if (argc && !cmd->args)
368 return 1;
369 if (cmd->idby != command_idby &&
370 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
371 return 1;
372 if (!cmd->handler)
373 return 1;
374 }
375
376 if (cmd->selector) {
377 cmd = cmd->selector(argc, argv);
378 if (!cmd)
379 return 1;
380 }
381
382 if (cmdout)
383 *cmdout = cmd;
384
385 if (!cmd->cmd) {
386 argc = o_argc;
387 argv = o_argv;
388 return cmd->handler(state, NULL, NULL, argc, argv, idby);
389 }
390
391 msg = nlmsg_alloc();
392 if (!msg) {
393 fprintf(stderr, "failed to allocate netlink message\n");
394 return 2;
395 }
396
397 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
398 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
399 if (!cb || !s_cb) {
400 fprintf(stderr, "failed to allocate netlink callbacks\n");
401 err = 2;
402 goto out_free_msg;
403 }
404
405 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
406 cmd->nl_msg_flags, cmd->cmd, 0);
407
408 switch (command_idby) {
409 case CIB_PHY:
410 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
411 break;
412 case CIB_NETDEV:
413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
414 break;
415 case CIB_WDEV:
416 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
417 break;
418 default:
419 break;
420 }
421
422 err = cmd->handler(state, cb, msg, argc, argv, idby);
423 if (err)
424 goto out;
425
426 nl_socket_set_cb(state->nl_sock, s_cb);
427
428 err = nl_send_auto_complete(state->nl_sock, msg);
429 if (err < 0)
430 goto out;
431
432 err = 1;
433
434 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
435 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
436 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
437
438 while (err > 0)
439 nl_recvmsgs(state->nl_sock, cb);
440 out:
441 nl_cb_put(cb);
442 out_free_msg:
443 nlmsg_free(msg);
444 return err;
445 nla_put_failure:
446 fprintf(stderr, "building message failed\n");
447 return 2;
448 }
449
450 int handle_cmd(struct nl80211_state *state, enum id_input idby,
451 int argc, char **argv)
452 {
453 return __handle_cmd(state, idby, argc, argv, NULL);
454 }
455
456 int main(int argc, char **argv)
457 {
458 struct nl80211_state nlstate;
459 int err;
460 const struct cmd *cmd = NULL;
461
462 /* calculate command size including padding */
463 cmd_size = abs((long)&__section_set - (long)&__section_get);
464 /* strip off self */
465 argc--;
466 argv0 = *argv++;
467
468 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
469 iw_debug = 1;
470 argc--;
471 argv++;
472 }
473
474 if (argc > 0 && strcmp(*argv, "--version") == 0) {
475 version();
476 return 0;
477 }
478
479 /* need to treat "help" command specially so it works w/o nl80211 */
480 if (argc == 0 || strcmp(*argv, "help") == 0) {
481 usage(argc - 1, argv + 1);
482 return 0;
483 }
484
485 err = nl80211_init(&nlstate);
486 if (err)
487 return 1;
488
489 if (strcmp(*argv, "dev") == 0 && argc > 1) {
490 argc--;
491 argv++;
492 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
493 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
494 if (strlen(*argv) == 3) {
495 argc--;
496 argv++;
497 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
498 } else if (*(*argv + 3) == '#')
499 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
500 else
501 goto detect;
502 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
503 argc--;
504 argv++;
505 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
506 } else {
507 int idx;
508 enum id_input idby = II_NONE;
509 detect:
510 if ((idx = if_nametoindex(argv[0])) != 0)
511 idby = II_NETDEV;
512 else if ((idx = phy_lookup(argv[0])) >= 0)
513 idby = II_PHY_NAME;
514 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
515 }
516
517 if (err == 1) {
518 if (cmd)
519 usage_cmd(cmd);
520 else
521 usage(0, NULL);
522 } else if (err < 0)
523 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
524
525 nl80211_cleanup(&nlstate);
526
527 return err;
528 }