]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
iw: add support for setting the wds/4-address flag when creating an interface
[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 (cmdout)
362 *cmdout = cmd;
363
364 if (!cmd->cmd) {
365 argc = o_argc;
366 argv = o_argv;
367 return cmd->handler(state, NULL, NULL, argc, argv);
368 }
369
370 msg = nlmsg_alloc();
371 if (!msg) {
372 fprintf(stderr, "failed to allocate netlink message\n");
373 return 2;
374 }
375
376 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
377 if (!cb) {
378 fprintf(stderr, "failed to allocate netlink callbacks\n");
379 err = 2;
380 goto out_free_msg;
381 }
382
383 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
384 cmd->nl_msg_flags, cmd->cmd, 0);
385
386 switch (command_idby) {
387 case CIB_PHY:
388 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
389 break;
390 case CIB_NETDEV:
391 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
392 break;
393 default:
394 break;
395 }
396
397 err = cmd->handler(state, cb, msg, argc, argv);
398 if (err)
399 goto out;
400
401 err = nl_send_auto_complete(state->nl_sock, msg);
402 if (err < 0)
403 goto out;
404
405 err = 1;
406
407 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
408 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
409 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
410
411 while (err > 0)
412 nl_recvmsgs(state->nl_sock, cb);
413 out:
414 nl_cb_put(cb);
415 out_free_msg:
416 nlmsg_free(msg);
417 return err;
418 nla_put_failure:
419 fprintf(stderr, "building message failed\n");
420 return 2;
421 }
422
423 int handle_cmd(struct nl80211_state *state, enum id_input idby,
424 int argc, char **argv)
425 {
426 return __handle_cmd(state, idby, argc, argv, NULL);
427 }
428
429 int main(int argc, char **argv)
430 {
431 struct nl80211_state nlstate;
432 int err;
433 const struct cmd *cmd = NULL;
434
435 /* calculate command size including padding */
436 cmd_size = abs((long)&__section_set - (long)&__section_get);
437 /* strip off self */
438 argc--;
439 argv0 = *argv++;
440
441 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
442 iw_debug = 1;
443 argc--;
444 argv++;
445 }
446
447 if (argc > 0 && strcmp(*argv, "--version") == 0) {
448 version();
449 return 0;
450 }
451
452 /* need to treat "help" command specially so it works w/o nl80211 */
453 if (argc == 0 || strcmp(*argv, "help") == 0) {
454 usage(argc != 0);
455 return 0;
456 }
457
458 err = nl80211_init(&nlstate);
459 if (err)
460 return 1;
461
462 if (strcmp(*argv, "dev") == 0 && argc > 1) {
463 argc--;
464 argv++;
465 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
466 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
467 if (strlen(*argv) == 3) {
468 argc--;
469 argv++;
470 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
471 } else if (*(*argv + 3) == '#')
472 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
473 else
474 goto detect;
475 } else {
476 int idx;
477 enum id_input idby = II_NONE;
478 detect:
479 if ((idx = if_nametoindex(argv[0])) != 0)
480 idby = II_NETDEV;
481 else if ((idx = phy_lookup(argv[0])) >= 0)
482 idby = II_PHY_NAME;
483 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
484 }
485
486 if (err == 1) {
487 if (cmd)
488 usage_cmd(cmd);
489 else
490 usage(false);
491 } else if (err < 0)
492 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
493
494 nl80211_cleanup(&nlstate);
495
496 return err;
497 }