]>
Commit | Line | Data |
---|---|---|
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 | return -1; | |
228 | buf[pos] = '\0'; | |
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_msg *msg; | |
260 | int devidx = 0; | |
261 | int err, o_argc; | |
262 | const char *command, *section; | |
263 | char *tmp, **o_argv; | |
264 | enum command_identify_by command_idby = CIB_NONE; | |
265 | ||
266 | if (argc <= 1 && idby != II_NONE) | |
267 | return 1; | |
268 | ||
269 | o_argc = argc; | |
270 | o_argv = argv; | |
271 | ||
272 | switch (idby) { | |
273 | case II_PHY_IDX: | |
274 | command_idby = CIB_PHY; | |
275 | devidx = strtoul(*argv + 4, &tmp, 0); | |
276 | if (*tmp != '\0') | |
277 | return 1; | |
278 | argc--; | |
279 | argv++; | |
280 | break; | |
281 | case II_PHY_NAME: | |
282 | command_idby = CIB_PHY; | |
283 | devidx = phy_lookup(*argv); | |
284 | argc--; | |
285 | argv++; | |
286 | break; | |
287 | case II_NETDEV: | |
288 | command_idby = CIB_NETDEV; | |
289 | devidx = if_nametoindex(*argv); | |
290 | if (devidx == 0) | |
291 | devidx = -1; | |
292 | argc--; | |
293 | argv++; | |
294 | break; | |
295 | default: | |
296 | break; | |
297 | } | |
298 | ||
299 | if (devidx < 0) | |
300 | return -errno; | |
301 | ||
302 | section = *argv; | |
303 | argc--; | |
304 | argv++; | |
305 | ||
306 | for_each_cmd(sectcmd) { | |
307 | if (sectcmd->parent) | |
308 | continue; | |
309 | /* ok ... bit of a hack for the dupe 'info' section */ | |
310 | if (match && sectcmd->idby != command_idby) | |
311 | continue; | |
312 | if (strcmp(sectcmd->name, section) == 0) | |
313 | match = sectcmd; | |
314 | } | |
315 | ||
316 | sectcmd = match; | |
317 | match = NULL; | |
318 | if (!sectcmd) | |
319 | return 1; | |
320 | ||
321 | if (argc > 0) { | |
322 | command = *argv; | |
323 | ||
324 | for_each_cmd(cmd) { | |
325 | if (!cmd->handler) | |
326 | continue; | |
327 | if (cmd->parent != sectcmd) | |
328 | continue; | |
329 | if (cmd->idby != command_idby) | |
330 | continue; | |
331 | if (strcmp(cmd->name, command)) | |
332 | continue; | |
333 | if (argc > 1 && !cmd->args) | |
334 | continue; | |
335 | match = cmd; | |
336 | break; | |
337 | } | |
338 | ||
339 | if (match) { | |
340 | argc--; | |
341 | argv++; | |
342 | } | |
343 | } | |
344 | ||
345 | if (match) | |
346 | cmd = match; | |
347 | else { | |
348 | /* Use the section itself, if possible. */ | |
349 | cmd = sectcmd; | |
350 | if (argc && !cmd->args) | |
351 | return 1; | |
352 | if (cmd->idby != command_idby) | |
353 | return 1; | |
354 | if (!cmd->handler) | |
355 | return 1; | |
356 | } | |
357 | ||
358 | if (cmdout) | |
359 | *cmdout = cmd; | |
360 | ||
361 | if (!cmd->cmd) { | |
362 | argc = o_argc; | |
363 | argv = o_argv; | |
364 | return cmd->handler(state, NULL, NULL, argc, argv); | |
365 | } | |
366 | ||
367 | msg = nlmsg_alloc(); | |
368 | if (!msg) { | |
369 | fprintf(stderr, "failed to allocate netlink message\n"); | |
370 | return 2; | |
371 | } | |
372 | ||
373 | cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); | |
374 | if (!cb) { | |
375 | fprintf(stderr, "failed to allocate netlink callbacks\n"); | |
376 | err = 2; | |
377 | goto out_free_msg; | |
378 | } | |
379 | ||
380 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, | |
381 | cmd->nl_msg_flags, cmd->cmd, 0); | |
382 | ||
383 | switch (command_idby) { | |
384 | case CIB_PHY: | |
385 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx); | |
386 | break; | |
387 | case CIB_NETDEV: | |
388 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); | |
389 | break; | |
390 | default: | |
391 | break; | |
392 | } | |
393 | ||
394 | err = cmd->handler(state, cb, msg, argc, argv); | |
395 | if (err) | |
396 | goto out; | |
397 | ||
398 | err = nl_send_auto_complete(state->nl_sock, msg); | |
399 | if (err < 0) | |
400 | goto out; | |
401 | ||
402 | err = 1; | |
403 | ||
404 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); | |
405 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); | |
406 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); | |
407 | ||
408 | while (err > 0) | |
409 | nl_recvmsgs(state->nl_sock, cb); | |
410 | out: | |
411 | nl_cb_put(cb); | |
412 | out_free_msg: | |
413 | nlmsg_free(msg); | |
414 | return err; | |
415 | nla_put_failure: | |
416 | fprintf(stderr, "building message failed\n"); | |
417 | return 2; | |
418 | } | |
419 | ||
420 | int handle_cmd(struct nl80211_state *state, enum id_input idby, | |
421 | int argc, char **argv) | |
422 | { | |
423 | return __handle_cmd(state, idby, argc, argv, NULL); | |
424 | } | |
425 | ||
426 | int main(int argc, char **argv) | |
427 | { | |
428 | struct nl80211_state nlstate; | |
429 | int err; | |
430 | const struct cmd *cmd = NULL; | |
431 | ||
432 | /* calculate command size including padding */ | |
433 | cmd_size = abs((long)&__section_set - (long)&__section_get); | |
434 | /* strip off self */ | |
435 | argc--; | |
436 | argv0 = *argv++; | |
437 | ||
438 | if (argc > 0 && strcmp(*argv, "--debug") == 0) { | |
439 | iw_debug = 1; | |
440 | argc--; | |
441 | argv++; | |
442 | } | |
443 | ||
444 | if (argc > 0 && strcmp(*argv, "--version") == 0) { | |
445 | version(); | |
446 | return 0; | |
447 | } | |
448 | ||
449 | /* need to treat "help" command specially so it works w/o nl80211 */ | |
450 | if (argc == 0 || strcmp(*argv, "help") == 0) { | |
451 | usage(argc != 0); | |
452 | return 0; | |
453 | } | |
454 | ||
455 | err = nl80211_init(&nlstate); | |
456 | if (err) | |
457 | return 1; | |
458 | ||
459 | if (strcmp(*argv, "dev") == 0 && argc > 1) { | |
460 | argc--; | |
461 | argv++; | |
462 | err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd); | |
463 | } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) { | |
464 | if (strlen(*argv) == 3) { | |
465 | argc--; | |
466 | argv++; | |
467 | err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd); | |
468 | } else if (*(*argv + 3) == '#') | |
469 | err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd); | |
470 | else | |
471 | goto detect; | |
472 | } else { | |
473 | int idx; | |
474 | enum id_input idby = II_NONE; | |
475 | detect: | |
476 | if ((idx = if_nametoindex(argv[0])) != 0) | |
477 | idby = II_NETDEV; | |
478 | else if ((idx = phy_lookup(argv[0])) >= 0) | |
479 | idby = II_PHY_NAME; | |
480 | err = __handle_cmd(&nlstate, idby, argc, argv, &cmd); | |
481 | } | |
482 | ||
483 | if (err == 1) { | |
484 | if (cmd) | |
485 | usage_cmd(cmd); | |
486 | else | |
487 | usage(false); | |
488 | } else if (err < 0) | |
489 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); | |
490 | ||
491 | nl80211_cleanup(&nlstate); | |
492 | ||
493 | return err; | |
494 | } |