]>
Commit | Line | Data |
---|---|---|
cad53b3f JB |
1 | /* |
2 | * nl80211 userspace tool | |
3 | * | |
2a1fced2 | 4 | * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net> |
cad53b3f JB |
5 | */ |
6 | ||
7 | #include <errno.h> | |
8 | #include <stdio.h> | |
d5ac8ad3 | 9 | #include <string.h> |
bd396f2a JB |
10 | #include <net/if.h> |
11 | #include <sys/types.h> | |
12 | #include <sys/stat.h> | |
13 | #include <fcntl.h> | |
14 | #include <unistd.h> | |
27c49ed6 | 15 | #include <stdbool.h> |
86da57ad | 16 | #include <linux/netlink.h> |
c551449a | 17 | |
cad53b3f JB |
18 | #include <netlink/genl/genl.h> |
19 | #include <netlink/genl/family.h> | |
c551449a | 20 | #include <netlink/genl/ctrl.h> |
cad53b3f JB |
21 | #include <netlink/msg.h> |
22 | #include <netlink/attr.h> | |
cad53b3f | 23 | |
f408e01b | 24 | #include "nl80211.h" |
cad53b3f JB |
25 | #include "iw.h" |
26 | ||
ded667b0 YY |
27 | /* libnl 1.x compatibility code */ |
28 | #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30) | |
dfd13ee5 PE |
29 | static inline struct nl_handle *nl_socket_alloc(void) |
30 | { | |
31 | return nl_handle_alloc(); | |
32 | } | |
33 | ||
57077d64 | 34 | static inline void nl_socket_free(struct nl_sock *h) |
dfd13ee5 PE |
35 | { |
36 | nl_handle_destroy(h); | |
37 | } | |
b9be8936 JB |
38 | |
39 | static inline int nl_socket_set_buffer_size(struct nl_sock *sk, | |
40 | int rxbuf, int txbuf) | |
41 | { | |
42 | return nl_set_buffer_size(sk, rxbuf, txbuf); | |
43 | } | |
ded667b0 | 44 | #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */ |
dfd13ee5 | 45 | |
957a0f07 | 46 | int iw_debug = 0; |
cad53b3f JB |
47 | |
48 | static int nl80211_init(struct nl80211_state *state) | |
49 | { | |
50 | int err; | |
51 | ||
57077d64 PE |
52 | state->nl_sock = nl_socket_alloc(); |
53 | if (!state->nl_sock) { | |
54 | fprintf(stderr, "Failed to allocate netlink socket.\n"); | |
cad53b3f JB |
55 | return -ENOMEM; |
56 | } | |
57 | ||
57077d64 | 58 | if (genl_connect(state->nl_sock)) { |
cad53b3f JB |
59 | fprintf(stderr, "Failed to connect to generic netlink.\n"); |
60 | err = -ENOLINK; | |
61 | goto out_handle_destroy; | |
62 | } | |
63 | ||
7687d596 TG |
64 | nl_socket_set_buffer_size(state->nl_sock, 8192, 8192); |
65 | ||
3a807325 JB |
66 | /* try to set NETLINK_EXT_ACK to 1, ignoring errors */ |
67 | err = 1; | |
68 | setsockopt(nl_socket_get_fd(state->nl_sock), SOL_NETLINK, | |
69 | NETLINK_EXT_ACK, &err, sizeof(err)); | |
70 | ||
f09cee6d JB |
71 | state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); |
72 | if (state->nl80211_id < 0) { | |
cad53b3f JB |
73 | fprintf(stderr, "nl80211 not found.\n"); |
74 | err = -ENOENT; | |
f09cee6d | 75 | goto out_handle_destroy; |
cad53b3f JB |
76 | } |
77 | ||
78 | return 0; | |
79 | ||
cad53b3f | 80 | out_handle_destroy: |
57077d64 | 81 | nl_socket_free(state->nl_sock); |
cad53b3f JB |
82 | return err; |
83 | } | |
84 | ||
85 | static void nl80211_cleanup(struct nl80211_state *state) | |
86 | { | |
57077d64 | 87 | nl_socket_free(state->nl_sock); |
cad53b3f JB |
88 | } |
89 | ||
403b9c83 JB |
90 | static int cmd_size; |
91 | ||
338059ac JB |
92 | extern struct cmd *__start___cmd[]; |
93 | extern struct cmd *__stop___cmd; | |
4698bfc2 | 94 | |
338059ac JB |
95 | #define for_each_cmd(_cmd, i) \ |
96 | for (i = 0; i < &__stop___cmd - __start___cmd; i++) \ | |
97 | if ((_cmd = __start___cmd[i])) | |
4698bfc2 JB |
98 | |
99 | ||
100 | static void __usage_cmd(const struct cmd *cmd, char *indent, bool full) | |
3bb116da | 101 | { |
01ae06f9 JB |
102 | const char *start, *lend, *end; |
103 | ||
bd663893 | 104 | printf("%s", indent); |
4f0cae73 | 105 | |
3bb116da JB |
106 | switch (cmd->idby) { |
107 | case CIB_NONE: | |
3bb116da JB |
108 | break; |
109 | case CIB_PHY: | |
bd663893 | 110 | printf("phy <phyname> "); |
3bb116da JB |
111 | break; |
112 | case CIB_NETDEV: | |
bd663893 | 113 | printf("dev <devname> "); |
3bb116da | 114 | break; |
290a3dd4 JB |
115 | case CIB_WDEV: |
116 | printf("wdev <idx> "); | |
117 | break; | |
3bb116da | 118 | } |
4698bfc2 JB |
119 | if (cmd->parent && cmd->parent->name) |
120 | printf("%s ", cmd->parent->name); | |
bd663893 | 121 | printf("%s", cmd->name); |
eb795501 JB |
122 | |
123 | if (cmd->args) { | |
124 | /* print line by line */ | |
125 | start = cmd->args; | |
126 | end = strchr(start, '\0'); | |
127 | printf(" "); | |
128 | do { | |
129 | lend = strchr(start, '\n'); | |
130 | if (!lend) | |
131 | lend = end; | |
132 | if (start != cmd->args) { | |
133 | printf("\t"); | |
134 | switch (cmd->idby) { | |
135 | case CIB_NONE: | |
136 | break; | |
137 | case CIB_PHY: | |
138 | printf("phy <phyname> "); | |
139 | break; | |
140 | case CIB_NETDEV: | |
141 | printf("dev <devname> "); | |
142 | break; | |
143 | case CIB_WDEV: | |
144 | printf("wdev <idx> "); | |
145 | break; | |
146 | } | |
147 | if (cmd->parent && cmd->parent->name) | |
148 | printf("%s ", cmd->parent->name); | |
149 | printf("%s ", cmd->name); | |
150 | } | |
151 | printf("%.*s\n", (int)(lend - start), start); | |
152 | start = lend + 1; | |
153 | } while (end != lend); | |
154 | } else | |
155 | printf("\n"); | |
01ae06f9 JB |
156 | |
157 | if (!full || !cmd->help) | |
158 | return; | |
159 | ||
160 | /* hack */ | |
161 | if (strlen(indent)) | |
162 | indent = "\t\t"; | |
163 | else | |
bd663893 | 164 | printf("\n"); |
01ae06f9 JB |
165 | |
166 | /* print line by line */ | |
167 | start = cmd->help; | |
168 | end = strchr(start, '\0'); | |
169 | do { | |
170 | lend = strchr(start, '\n'); | |
171 | if (!lend) | |
172 | lend = end; | |
bd663893 HS |
173 | printf("%s", indent); |
174 | printf("%.*s\n", (int)(lend - start), start); | |
01ae06f9 JB |
175 | start = lend + 1; |
176 | } while (end != lend); | |
177 | ||
bd663893 | 178 | printf("\n"); |
3bb116da JB |
179 | } |
180 | ||
4f0cae73 JB |
181 | static void usage_options(void) |
182 | { | |
bd663893 HS |
183 | printf("Options:\n"); |
184 | printf("\t--debug\t\tenable netlink debugging\n"); | |
4f0cae73 JB |
185 | } |
186 | ||
01ae06f9 JB |
187 | static const char *argv0; |
188 | ||
f3ac8bf1 | 189 | static void usage(int argc, char **argv) |
bd396f2a | 190 | { |
4698bfc2 | 191 | const struct cmd *section, *cmd; |
f3ac8bf1 JB |
192 | bool full = argc >= 0; |
193 | const char *sect_filt = NULL; | |
194 | const char *cmd_filt = NULL; | |
338059ac | 195 | unsigned int i, j; |
f3ac8bf1 JB |
196 | |
197 | if (argc > 0) | |
198 | sect_filt = argv[0]; | |
199 | ||
200 | if (argc > 1) | |
201 | cmd_filt = argv[1]; | |
bd396f2a | 202 | |
bd663893 | 203 | printf("Usage:\t%s [options] command\n", argv0); |
4f0cae73 | 204 | usage_options(); |
bd663893 HS |
205 | printf("\t--version\tshow version (%s)\n", iw_version); |
206 | printf("Commands:\n"); | |
338059ac | 207 | for_each_cmd(section, i) { |
4698bfc2 | 208 | if (section->parent) |
403b9c83 | 209 | continue; |
4698bfc2 | 210 | |
f3ac8bf1 JB |
211 | if (sect_filt && strcmp(section->name, sect_filt)) |
212 | continue; | |
213 | ||
4698bfc2 JB |
214 | if (section->handler && !section->hidden) |
215 | __usage_cmd(section, "\t", full); | |
216 | ||
338059ac | 217 | for_each_cmd(cmd, j) { |
4698bfc2 JB |
218 | if (section != cmd->parent) |
219 | continue; | |
220 | if (!cmd->handler || cmd->hidden) | |
221 | continue; | |
f3ac8bf1 JB |
222 | if (cmd_filt && strcmp(cmd->name, cmd_filt)) |
223 | continue; | |
4698bfc2 JB |
224 | __usage_cmd(cmd, "\t", full); |
225 | } | |
bd396f2a | 226 | } |
75f4204c JB |
227 | printf("\nCommands that use the netdev ('dev') can also be given the\n" |
228 | "'wdev' instead to identify the device.\n"); | |
bd663893 | 229 | printf("\nYou can omit the 'phy' or 'dev' if " |
f4ec76d0 | 230 | "the identification is unique,\n" |
8aefee9a | 231 | "e.g. \"iw wlan0 info\" or \"iw phy0 info\". " |
fbdb8d05 JB |
232 | "(Don't when scripting.)\n\n" |
233 | "Do NOT screenscrape this tool, we don't " | |
234 | "consider its output stable.\n\n"); | |
bd396f2a JB |
235 | } |
236 | ||
01ae06f9 | 237 | static int print_help(struct nl80211_state *state, |
01ae06f9 | 238 | struct nl_msg *msg, |
05514f95 JB |
239 | int argc, char **argv, |
240 | enum id_input id) | |
01ae06f9 JB |
241 | { |
242 | exit(3); | |
243 | } | |
f3ac8bf1 JB |
244 | TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help, |
245 | "Print usage for all or a specific command, e.g.\n" | |
246 | "\"help wowlan\" or \"help wowlan enable\"."); | |
01ae06f9 | 247 | |
4698bfc2 | 248 | static void usage_cmd(const struct cmd *cmd) |
4f0cae73 | 249 | { |
bd663893 | 250 | printf("Usage:\t%s [options] ", argv0); |
01ae06f9 | 251 | __usage_cmd(cmd, "", true); |
4f0cae73 JB |
252 | usage_options(); |
253 | } | |
254 | ||
d711f013 JB |
255 | static void version(void) |
256 | { | |
133b069f | 257 | printf("iw version %s\n", iw_version); |
d711f013 JB |
258 | } |
259 | ||
bd396f2a JB |
260 | static int phy_lookup(char *name) |
261 | { | |
262 | char buf[200]; | |
263 | int fd, pos; | |
264 | ||
265 | snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name); | |
266 | ||
267 | fd = open(buf, O_RDONLY); | |
989e97c2 JB |
268 | if (fd < 0) |
269 | return -1; | |
bd396f2a | 270 | pos = read(fd, buf, sizeof(buf) - 1); |
8f253ee2 ES |
271 | if (pos < 0) { |
272 | close(fd); | |
bd396f2a | 273 | return -1; |
8f253ee2 | 274 | } |
bd396f2a | 275 | buf[pos] = '\0'; |
8f253ee2 | 276 | close(fd); |
bd396f2a JB |
277 | return atoi(buf); |
278 | } | |
279 | ||
70391ccf JB |
280 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
281 | void *arg) | |
282 | { | |
3a807325 JB |
283 | struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1; |
284 | int len = nlh->nlmsg_len; | |
285 | struct nlattr *attrs; | |
286 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1]; | |
70391ccf | 287 | int *ret = arg; |
3a807325 JB |
288 | int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh); |
289 | ||
70391ccf | 290 | *ret = err->error; |
3a807325 JB |
291 | |
292 | if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS)) | |
293 | return NL_STOP; | |
294 | ||
295 | if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) | |
296 | ack_len += err->msg.nlmsg_len - sizeof(*nlh); | |
297 | ||
298 | if (len <= ack_len) | |
299 | return NL_STOP; | |
300 | ||
301 | attrs = (void *)((unsigned char *)nlh + ack_len); | |
302 | len -= ack_len; | |
303 | ||
304 | nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL); | |
305 | if (tb[NLMSGERR_ATTR_MSG]) { | |
306 | len = strnlen((char *)nla_data(tb[NLMSGERR_ATTR_MSG]), | |
307 | nla_len(tb[NLMSGERR_ATTR_MSG])); | |
308 | fprintf(stderr, "kernel reports: %*s\n", len, | |
309 | (char *)nla_data(tb[NLMSGERR_ATTR_MSG])); | |
310 | } | |
311 | ||
70391ccf JB |
312 | return NL_STOP; |
313 | } | |
314 | ||
561c5b7e JB |
315 | static int finish_handler(struct nl_msg *msg, void *arg) |
316 | { | |
59c418c0 JB |
317 | int *ret = arg; |
318 | *ret = 0; | |
561c5b7e JB |
319 | return NL_SKIP; |
320 | } | |
321 | ||
322 | static int ack_handler(struct nl_msg *msg, void *arg) | |
70391ccf JB |
323 | { |
324 | int *ret = arg; | |
325 | *ret = 0; | |
326 | return NL_STOP; | |
327 | } | |
328 | ||
34b23014 JB |
329 | static int (*registered_handler)(struct nl_msg *, void *); |
330 | static void *registered_handler_data; | |
331 | ||
332 | void register_handler(int (*handler)(struct nl_msg *, void *), void *data) | |
333 | { | |
334 | registered_handler = handler; | |
335 | registered_handler_data = data; | |
336 | } | |
337 | ||
338 | int valid_handler(struct nl_msg *msg, void *arg) | |
339 | { | |
340 | if (registered_handler) | |
341 | return registered_handler(msg, registered_handler_data); | |
342 | ||
343 | return NL_OK; | |
344 | } | |
345 | ||
4f0cae73 | 346 | static int __handle_cmd(struct nl80211_state *state, enum id_input idby, |
4698bfc2 | 347 | int argc, char **argv, const struct cmd **cmdout) |
45c7212c | 348 | { |
4698bfc2 | 349 | const struct cmd *cmd, *match = NULL, *sectcmd; |
7c37a24d | 350 | struct nl_cb *cb; |
5cb60f91 | 351 | struct nl_cb *s_cb; |
bd396f2a | 352 | struct nl_msg *msg; |
290a3dd4 | 353 | signed long long devidx = 0; |
338059ac | 354 | int err, o_argc, i; |
bd396f2a | 355 | const char *command, *section; |
bb60b4ae | 356 | char *tmp, **o_argv; |
9927363c | 357 | enum command_identify_by command_idby = CIB_NONE; |
45c7212c | 358 | |
9927363c | 359 | if (argc <= 1 && idby != II_NONE) |
5e75fd04 | 360 | return 1; |
45c7212c | 361 | |
bb60b4ae JB |
362 | o_argc = argc; |
363 | o_argv = argv; | |
364 | ||
bd396f2a | 365 | switch (idby) { |
9927363c JB |
366 | case II_PHY_IDX: |
367 | command_idby = CIB_PHY; | |
368 | devidx = strtoul(*argv + 4, &tmp, 0); | |
369 | if (*tmp != '\0') | |
370 | return 1; | |
371 | argc--; | |
372 | argv++; | |
373 | break; | |
374 | case II_PHY_NAME: | |
375 | command_idby = CIB_PHY; | |
bd396f2a JB |
376 | devidx = phy_lookup(*argv); |
377 | argc--; | |
378 | argv++; | |
379 | break; | |
9927363c JB |
380 | case II_NETDEV: |
381 | command_idby = CIB_NETDEV; | |
bd396f2a | 382 | devidx = if_nametoindex(*argv); |
989e97c2 JB |
383 | if (devidx == 0) |
384 | devidx = -1; | |
bd396f2a JB |
385 | argc--; |
386 | argv++; | |
387 | break; | |
290a3dd4 JB |
388 | case II_WDEV: |
389 | command_idby = CIB_WDEV; | |
390 | devidx = strtoll(*argv, &tmp, 0); | |
391 | if (*tmp != '\0') | |
392 | return 1; | |
393 | argc--; | |
394 | argv++; | |
bd396f2a JB |
395 | default: |
396 | break; | |
397 | } | |
398 | ||
989e97c2 JB |
399 | if (devidx < 0) |
400 | return -errno; | |
401 | ||
4698bfc2 | 402 | section = *argv; |
bd396f2a JB |
403 | argc--; |
404 | argv++; | |
405 | ||
338059ac | 406 | for_each_cmd(sectcmd, i) { |
4698bfc2 | 407 | if (sectcmd->parent) |
403b9c83 | 408 | continue; |
4698bfc2 JB |
409 | /* ok ... bit of a hack for the dupe 'info' section */ |
410 | if (match && sectcmd->idby != command_idby) | |
bd396f2a | 411 | continue; |
4698bfc2 JB |
412 | if (strcmp(sectcmd->name, section) == 0) |
413 | match = sectcmd; | |
bd396f2a | 414 | } |
45c7212c | 415 | |
4698bfc2 JB |
416 | sectcmd = match; |
417 | match = NULL; | |
418 | if (!sectcmd) | |
5e75fd04 | 419 | return 1; |
45c7212c | 420 | |
4698bfc2 JB |
421 | if (argc > 0) { |
422 | command = *argv; | |
423 | ||
338059ac | 424 | for_each_cmd(cmd, i) { |
4698bfc2 JB |
425 | if (!cmd->handler) |
426 | continue; | |
427 | if (cmd->parent != sectcmd) | |
428 | continue; | |
75f4204c JB |
429 | /* |
430 | * ignore mismatch id by, but allow WDEV | |
431 | * in place of NETDEV | |
432 | */ | |
433 | if (cmd->idby != command_idby && | |
434 | !(cmd->idby == CIB_NETDEV && | |
435 | command_idby == CIB_WDEV)) | |
4698bfc2 JB |
436 | continue; |
437 | if (strcmp(cmd->name, command)) | |
438 | continue; | |
439 | if (argc > 1 && !cmd->args) | |
440 | continue; | |
441 | match = cmd; | |
442 | break; | |
443 | } | |
444 | ||
445 | if (match) { | |
446 | argc--; | |
447 | argv++; | |
448 | } | |
449 | } | |
450 | ||
451 | if (match) | |
452 | cmd = match; | |
453 | else { | |
454 | /* Use the section itself, if possible. */ | |
455 | cmd = sectcmd; | |
456 | if (argc && !cmd->args) | |
457 | return 1; | |
75f4204c JB |
458 | if (cmd->idby != command_idby && |
459 | !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV)) | |
4698bfc2 JB |
460 | return 1; |
461 | if (!cmd->handler) | |
462 | return 1; | |
463 | } | |
464 | ||
1633ddf7 JB |
465 | if (cmd->selector) { |
466 | cmd = cmd->selector(argc, argv); | |
467 | if (!cmd) | |
468 | return 1; | |
469 | } | |
470 | ||
4f0cae73 JB |
471 | if (cmdout) |
472 | *cmdout = cmd; | |
473 | ||
bb60b4ae JB |
474 | if (!cmd->cmd) { |
475 | argc = o_argc; | |
476 | argv = o_argv; | |
34b23014 | 477 | return cmd->handler(state, NULL, argc, argv, idby); |
bb60b4ae JB |
478 | } |
479 | ||
bd396f2a JB |
480 | msg = nlmsg_alloc(); |
481 | if (!msg) { | |
70391ccf JB |
482 | fprintf(stderr, "failed to allocate netlink message\n"); |
483 | return 2; | |
484 | } | |
485 | ||
957a0f07 | 486 | cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
5cb60f91 SR |
487 | s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
488 | if (!cb || !s_cb) { | |
70391ccf JB |
489 | fprintf(stderr, "failed to allocate netlink callbacks\n"); |
490 | err = 2; | |
7faa1ba1 | 491 | goto out; |
bd396f2a | 492 | } |
45c7212c | 493 | |
f09cee6d | 494 | genlmsg_put(msg, 0, 0, state->nl80211_id, 0, |
bd396f2a JB |
495 | cmd->nl_msg_flags, cmd->cmd, 0); |
496 | ||
9927363c | 497 | switch (command_idby) { |
bd396f2a JB |
498 | case CIB_PHY: |
499 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx); | |
500 | break; | |
501 | case CIB_NETDEV: | |
502 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); | |
503 | break; | |
290a3dd4 JB |
504 | case CIB_WDEV: |
505 | NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx); | |
506 | break; | |
bd396f2a JB |
507 | default: |
508 | break; | |
509 | } | |
510 | ||
34b23014 | 511 | err = cmd->handler(state, msg, argc, argv, idby); |
70391ccf JB |
512 | if (err) |
513 | goto out; | |
514 | ||
5cb60f91 SR |
515 | nl_socket_set_cb(state->nl_sock, s_cb); |
516 | ||
57077d64 | 517 | err = nl_send_auto_complete(state->nl_sock, msg); |
70391ccf JB |
518 | if (err < 0) |
519 | goto out; | |
520 | ||
c5c4471a JB |
521 | err = 1; |
522 | ||
70391ccf | 523 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); |
59c418c0 | 524 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); |
561c5b7e | 525 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); |
34b23014 | 526 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL); |
70391ccf | 527 | |
c5c4471a | 528 | while (err > 0) |
57077d64 | 529 | nl_recvmsgs(state->nl_sock, cb); |
70391ccf JB |
530 | out: |
531 | nl_cb_put(cb); | |
7faa1ba1 | 532 | nl_cb_put(s_cb); |
70391ccf JB |
533 | nlmsg_free(msg); |
534 | return err; | |
bd396f2a JB |
535 | nla_put_failure: |
536 | fprintf(stderr, "building message failed\n"); | |
70391ccf | 537 | return 2; |
45c7212c JB |
538 | } |
539 | ||
4f0cae73 JB |
540 | int handle_cmd(struct nl80211_state *state, enum id_input idby, |
541 | int argc, char **argv) | |
542 | { | |
543 | return __handle_cmd(state, idby, argc, argv, NULL); | |
544 | } | |
545 | ||
ae03bc6e JB |
546 | /* |
547 | * Unfortunately, I don't know how densely the linker packs the struct cmd. | |
548 | * For example, if you have a 72-byte struct cmd, the linker will pad each | |
549 | * out to 96 bytes before putting them together in the section. There must | |
550 | * be some algorithm, but I haven't found it yet. | |
551 | * | |
552 | * We used to calculate this by taking the (abs value of) the difference | |
553 | * between __section_get and __section_set, but if LTO is enabled then this | |
554 | * stops working because the entries of the "__cmd" section get rearranged | |
555 | * freely by the compiler/linker. | |
556 | * | |
557 | * Fix this by using yet another "__sizer" section that only contains these | |
558 | * two entries - then the (abs value of) the difference between them will | |
559 | * be how they get packed and that can be used to iterate the __cmd section | |
560 | * as well. | |
561 | */ | |
4323f5a9 JB |
562 | static struct cmd sizer1 __attribute__((section("__sizer"))) = {}; |
563 | static struct cmd sizer2 __attribute__((section("__sizer"))) = {}; | |
ae03bc6e | 564 | |
cad53b3f JB |
565 | int main(int argc, char **argv) |
566 | { | |
567 | struct nl80211_state nlstate; | |
bd396f2a | 568 | int err; |
4698bfc2 | 569 | const struct cmd *cmd = NULL; |
cad53b3f | 570 | |
f408e01b | 571 | /* calculate command size including padding */ |
4323f5a9 | 572 | cmd_size = labs((long)&sizer2 - (long)&sizer1); |
45c7212c JB |
573 | /* strip off self */ |
574 | argc--; | |
1cdd9016 MK |
575 | argv0 = *argv++; |
576 | ||
59c49f09 | 577 | if (argc > 0 && strcmp(*argv, "--debug") == 0) { |
957a0f07 | 578 | iw_debug = 1; |
59c49f09 JB |
579 | argc--; |
580 | argv++; | |
581 | } | |
582 | ||
d711f013 JB |
583 | if (argc > 0 && strcmp(*argv, "--version") == 0) { |
584 | version(); | |
585 | return 0; | |
586 | } | |
587 | ||
01ae06f9 | 588 | /* need to treat "help" command specially so it works w/o nl80211 */ |
bd396f2a | 589 | if (argc == 0 || strcmp(*argv, "help") == 0) { |
f3ac8bf1 | 590 | usage(argc - 1, argv + 1); |
4a972f80 | 591 | return 0; |
1cdd9016 | 592 | } |
45c7212c | 593 | |
2bdb6bd1 JB |
594 | err = nl80211_init(&nlstate); |
595 | if (err) | |
596 | return 1; | |
597 | ||
957a0f07 | 598 | if (strcmp(*argv, "dev") == 0 && argc > 1) { |
14a0380d LR |
599 | argc--; |
600 | argv++; | |
4f0cae73 | 601 | err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd); |
811ec68f | 602 | } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) { |
9927363c JB |
603 | if (strlen(*argv) == 3) { |
604 | argc--; | |
605 | argv++; | |
4f0cae73 | 606 | err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd); |
9927363c | 607 | } else if (*(*argv + 3) == '#') |
4f0cae73 | 608 | err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd); |
9927363c | 609 | else |
f4ec76d0 | 610 | goto detect; |
290a3dd4 JB |
611 | } else if (strcmp(*argv, "wdev") == 0 && argc > 1) { |
612 | argc--; | |
613 | argv++; | |
614 | err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd); | |
f4ec76d0 JB |
615 | } else { |
616 | int idx; | |
8a2dfd51 | 617 | enum id_input idby; |
f4ec76d0 | 618 | detect: |
8a2dfd51 | 619 | idby = II_NONE; |
f4ec76d0 JB |
620 | if ((idx = if_nametoindex(argv[0])) != 0) |
621 | idby = II_NETDEV; | |
66f8ca45 | 622 | else if ((idx = phy_lookup(argv[0])) >= 0) |
f4ec76d0 JB |
623 | idby = II_PHY_NAME; |
624 | err = __handle_cmd(&nlstate, idby, argc, argv, &cmd); | |
625 | } | |
45c7212c | 626 | |
b2c4bf45 | 627 | if (err == HANDLER_RET_USAGE) { |
4f0cae73 | 628 | if (cmd) |
01ae06f9 | 629 | usage_cmd(cmd); |
4f0cae73 | 630 | else |
f3ac8bf1 | 631 | usage(0, NULL); |
94af668b JB |
632 | } else if (err == HANDLER_RET_DONE) { |
633 | err = 0; | |
4f0cae73 | 634 | } else if (err < 0) |
b49be3e1 | 635 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); |
cad53b3f JB |
636 | |
637 | nl80211_cleanup(&nlstate); | |
638 | ||
45c7212c | 639 | return err; |
cad53b3f | 640 | } |