]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
don't emit usage to stderr
[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 __COMMAND(NULL, NULL, "", NULL, 0, 0, 0, CIB_NONE, NULL, NULL);
98 __COMMAND(NULL, NULL, "", NULL, 1, 0, 0, CIB_NONE, NULL, NULL);
99
100 static int cmd_size;
101
102 static void __usage_cmd(struct cmd *cmd, char *indent, bool full)
103 {
104 const char *start, *lend, *end;
105
106 printf("%s", indent);
107
108 switch (cmd->idby) {
109 case CIB_NONE:
110 break;
111 case CIB_PHY:
112 printf("phy <phyname> ");
113 break;
114 case CIB_NETDEV:
115 printf("dev <devname> ");
116 break;
117 }
118 if (cmd->section)
119 printf("%s ", cmd->section);
120 printf("%s", cmd->name);
121 if (cmd->args)
122 printf(" %s", cmd->args);
123 printf("\n");
124
125 if (!full || !cmd->help)
126 return;
127
128 /* hack */
129 if (strlen(indent))
130 indent = "\t\t";
131 else
132 printf("\n");
133
134 /* print line by line */
135 start = cmd->help;
136 end = strchr(start, '\0');
137 do {
138 lend = strchr(start, '\n');
139 if (!lend)
140 lend = end;
141 printf("%s", indent);
142 printf("%.*s\n", (int)(lend - start), start);
143 start = lend + 1;
144 } while (end != lend);
145
146 printf("\n");
147 }
148
149 static void usage_options(void)
150 {
151 printf("Options:\n");
152 printf("\t--debug\t\tenable netlink debugging\n");
153 }
154
155 static const char *argv0;
156
157 static void usage(bool full)
158 {
159 struct cmd *cmd;
160
161 printf("Usage:\t%s [options] command\n", argv0);
162 usage_options();
163 printf("\t--version\tshow version (%s)\n", iw_version);
164 printf("Commands:\n");
165 for (cmd = &__start___cmd; cmd < &__stop___cmd;
166 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
167 if (!cmd->handler || cmd->hidden)
168 continue;
169 __usage_cmd(cmd, "\t", full);
170 }
171 printf("\nYou can omit the 'phy' or 'dev' if "
172 "the identification is unique,\n"
173 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
174 "(Don't when scripting.)\n\n"
175 "Do NOT screenscrape this tool, we don't "
176 "consider its output stable.\n\n");
177 }
178
179 static int print_help(struct nl80211_state *state,
180 struct nl_cb *cb,
181 struct nl_msg *msg,
182 int argc, char **argv)
183 {
184 exit(3);
185 }
186 TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
187 "Print usage for each command.");
188
189 static void usage_cmd(struct cmd *cmd)
190 {
191 printf("Usage:\t%s [options] ", argv0);
192 __usage_cmd(cmd, "", true);
193 usage_options();
194 }
195
196 static void version(void)
197 {
198 printf("iw version %s\n", iw_version);
199 }
200
201 static int phy_lookup(char *name)
202 {
203 char buf[200];
204 int fd, pos;
205
206 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
207
208 fd = open(buf, O_RDONLY);
209 if (fd < 0)
210 return -1;
211 pos = read(fd, buf, sizeof(buf) - 1);
212 if (pos < 0)
213 return -1;
214 buf[pos] = '\0';
215 return atoi(buf);
216 }
217
218 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
219 void *arg)
220 {
221 int *ret = arg;
222 *ret = err->error;
223 return NL_STOP;
224 }
225
226 static int finish_handler(struct nl_msg *msg, void *arg)
227 {
228 int *ret = arg;
229 *ret = 0;
230 return NL_SKIP;
231 }
232
233 static int ack_handler(struct nl_msg *msg, void *arg)
234 {
235 int *ret = arg;
236 *ret = 0;
237 return NL_STOP;
238 }
239
240 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
241 int argc, char **argv, struct cmd **cmdout)
242 {
243 struct cmd *cmd, *match = NULL;
244 struct nl_cb *cb;
245 struct nl_msg *msg;
246 int devidx = 0;
247 int err, o_argc;
248 const char *command, *section;
249 char *tmp, **o_argv;
250 enum command_identify_by command_idby = CIB_NONE;
251
252 if (argc <= 1 && idby != II_NONE)
253 return 1;
254
255 o_argc = argc;
256 o_argv = argv;
257
258 switch (idby) {
259 case II_PHY_IDX:
260 command_idby = CIB_PHY;
261 devidx = strtoul(*argv + 4, &tmp, 0);
262 if (*tmp != '\0')
263 return 1;
264 argc--;
265 argv++;
266 break;
267 case II_PHY_NAME:
268 command_idby = CIB_PHY;
269 devidx = phy_lookup(*argv);
270 argc--;
271 argv++;
272 break;
273 case II_NETDEV:
274 command_idby = CIB_NETDEV;
275 devidx = if_nametoindex(*argv);
276 if (devidx == 0)
277 devidx = -1;
278 argc--;
279 argv++;
280 break;
281 default:
282 break;
283 }
284
285 if (devidx < 0)
286 return -errno;
287
288 section = command = *argv;
289 argc--;
290 argv++;
291
292 for (cmd = &__start___cmd; cmd < &__stop___cmd;
293 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
294 if (!cmd->handler)
295 continue;
296 if (cmd->idby != command_idby)
297 continue;
298 if (cmd->section) {
299 if (strcmp(cmd->section, section))
300 continue;
301 /* this is a bit icky ... */
302 if (command == section) {
303 if (argc <= 0) {
304 if (match)
305 break;
306 return 1;
307 }
308 command = *argv;
309 argc--;
310 argv++;
311 }
312 } else if (section != command)
313 continue;
314 if (strcmp(cmd->name, command))
315 continue;
316 if (argc && !cmd->args)
317 continue;
318
319 match = cmd;
320 }
321
322 cmd = match;
323
324 if (!cmd)
325 return 1;
326
327 if (cmdout)
328 *cmdout = cmd;
329
330 if (!cmd->cmd) {
331 argc = o_argc;
332 argv = o_argv;
333 return cmd->handler(state, NULL, NULL, argc, argv);
334 }
335
336 msg = nlmsg_alloc();
337 if (!msg) {
338 fprintf(stderr, "failed to allocate netlink message\n");
339 return 2;
340 }
341
342 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
343 if (!cb) {
344 fprintf(stderr, "failed to allocate netlink callbacks\n");
345 err = 2;
346 goto out_free_msg;
347 }
348
349 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
350 cmd->nl_msg_flags, cmd->cmd, 0);
351
352 switch (command_idby) {
353 case CIB_PHY:
354 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
355 break;
356 case CIB_NETDEV:
357 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
358 break;
359 default:
360 break;
361 }
362
363 err = cmd->handler(state, cb, msg, argc, argv);
364 if (err)
365 goto out;
366
367 err = nl_send_auto_complete(state->nl_sock, msg);
368 if (err < 0)
369 goto out;
370
371 err = 1;
372
373 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
374 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
375 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
376
377 while (err > 0)
378 nl_recvmsgs(state->nl_sock, cb);
379 out:
380 nl_cb_put(cb);
381 out_free_msg:
382 nlmsg_free(msg);
383 return err;
384 nla_put_failure:
385 fprintf(stderr, "building message failed\n");
386 return 2;
387 }
388
389 int handle_cmd(struct nl80211_state *state, enum id_input idby,
390 int argc, char **argv)
391 {
392 return __handle_cmd(state, idby, argc, argv, NULL);
393 }
394
395 int main(int argc, char **argv)
396 {
397 struct nl80211_state nlstate;
398 int err;
399 struct cmd *cmd = NULL;
400
401 /* calculate command size including padding */
402 cmd_size = abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
403 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0);
404 /* strip off self */
405 argc--;
406 argv0 = *argv++;
407
408 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
409 iw_debug = 1;
410 argc--;
411 argv++;
412 }
413
414 if (argc > 0 && strcmp(*argv, "--version") == 0) {
415 version();
416 return 0;
417 }
418
419 /* need to treat "help" command specially so it works w/o nl80211 */
420 if (argc == 0 || strcmp(*argv, "help") == 0) {
421 usage(argc != 0);
422 return 0;
423 }
424
425 err = nl80211_init(&nlstate);
426 if (err)
427 return 1;
428
429 if (strcmp(*argv, "dev") == 0 && argc > 1) {
430 argc--;
431 argv++;
432 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
433 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
434 if (strlen(*argv) == 3) {
435 argc--;
436 argv++;
437 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
438 } else if (*(*argv + 3) == '#')
439 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
440 else
441 goto detect;
442 } else {
443 int idx;
444 enum id_input idby = II_NONE;
445 detect:
446 if ((idx = if_nametoindex(argv[0])) != 0)
447 idby = II_NETDEV;
448 else if ((idx = phy_lookup(argv[0])) >= 0)
449 idby = II_PHY_NAME;
450 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
451 }
452
453 if (err == 1) {
454 if (cmd)
455 usage_cmd(cmd);
456 else
457 usage(false);
458 } else if (err < 0)
459 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
460
461 nl80211_cleanup(&nlstate);
462
463 return err;
464 }