2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <sys/types.h>
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>
26 /* libnl 1.x compatibility code */
27 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
28 static inline struct nl_handle
*nl_socket_alloc(void)
30 return nl_handle_alloc();
33 static inline void nl_socket_free(struct nl_sock
*h
)
38 static inline int nl_socket_set_buffer_size(struct nl_sock
*sk
,
41 return nl_set_buffer_size(sk
, rxbuf
, txbuf
);
43 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
47 static int nl80211_init(struct nl80211_state
*state
)
51 state
->nl_sock
= nl_socket_alloc();
52 if (!state
->nl_sock
) {
53 fprintf(stderr
, "Failed to allocate netlink socket.\n");
57 if (genl_connect(state
->nl_sock
)) {
58 fprintf(stderr
, "Failed to connect to generic netlink.\n");
60 goto out_handle_destroy
;
63 nl_socket_set_buffer_size(state
->nl_sock
, 8192, 8192);
65 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
67 setsockopt(nl_socket_get_fd(state
->nl_sock
), SOL_NETLINK
,
68 NETLINK_EXT_ACK
, &err
, sizeof(err
));
70 state
->nl80211_id
= genl_ctrl_resolve(state
->nl_sock
, "nl80211");
71 if (state
->nl80211_id
< 0) {
72 fprintf(stderr
, "nl80211 not found.\n");
74 goto out_handle_destroy
;
80 nl_socket_free(state
->nl_sock
);
84 static void nl80211_cleanup(struct nl80211_state
*state
)
86 nl_socket_free(state
->nl_sock
);
91 extern struct cmd __start___cmd
;
92 extern struct cmd __stop___cmd
;
94 #define for_each_cmd(_cmd) \
95 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
96 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
99 static void __usage_cmd(const struct cmd
*cmd
, char *indent
, bool full
)
101 const char *start
, *lend
, *end
;
103 printf("%s", indent
);
109 printf("phy <phyname> ");
112 printf("dev <devname> ");
115 printf("wdev <idx> ");
118 if (cmd
->parent
&& cmd
->parent
->name
)
119 printf("%s ", cmd
->parent
->name
);
120 printf("%s", cmd
->name
);
123 /* print line by line */
125 end
= strchr(start
, '\0');
128 lend
= strchr(start
, '\n');
131 if (start
!= cmd
->args
) {
137 printf("phy <phyname> ");
140 printf("dev <devname> ");
143 printf("wdev <idx> ");
146 if (cmd
->parent
&& cmd
->parent
->name
)
147 printf("%s ", cmd
->parent
->name
);
148 printf("%s ", cmd
->name
);
150 printf("%.*s\n", (int)(lend
- start
), start
);
152 } while (end
!= lend
);
156 if (!full
|| !cmd
->help
)
165 /* print line by line */
167 end
= strchr(start
, '\0');
169 lend
= strchr(start
, '\n');
172 printf("%s", indent
);
173 printf("%.*s\n", (int)(lend
- start
), start
);
175 } while (end
!= lend
);
180 static void usage_options(void)
182 printf("Options:\n");
183 printf("\t--debug\t\tenable netlink debugging\n");
186 static const char *argv0
;
188 static void usage(int argc
, char **argv
)
190 const struct cmd
*section
, *cmd
;
191 bool full
= argc
>= 0;
192 const char *sect_filt
= NULL
;
193 const char *cmd_filt
= NULL
;
201 printf("Usage:\t%s [options] command\n", argv0
);
203 printf("\t--version\tshow version (%s)\n", iw_version
);
204 printf("Commands:\n");
205 for_each_cmd(section
) {
209 if (sect_filt
&& strcmp(section
->name
, sect_filt
))
212 if (section
->handler
&& !section
->hidden
)
213 __usage_cmd(section
, "\t", full
);
216 if (section
!= cmd
->parent
)
218 if (!cmd
->handler
|| cmd
->hidden
)
220 if (cmd_filt
&& strcmp(cmd
->name
, cmd_filt
))
222 __usage_cmd(cmd
, "\t", full
);
225 printf("\nCommands that use the netdev ('dev') can also be given the\n"
226 "'wdev' instead to identify the device.\n");
227 printf("\nYou can omit the 'phy' or 'dev' if "
228 "the identification is unique,\n"
229 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
230 "(Don't when scripting.)\n\n"
231 "Do NOT screenscrape this tool, we don't "
232 "consider its output stable.\n\n");
235 static int print_help(struct nl80211_state
*state
,
237 int argc
, char **argv
,
242 TOPLEVEL(help
, "[command]", 0, 0, CIB_NONE
, print_help
,
243 "Print usage for all or a specific command, e.g.\n"
244 "\"help wowlan\" or \"help wowlan enable\".");
246 static void usage_cmd(const struct cmd
*cmd
)
248 printf("Usage:\t%s [options] ", argv0
);
249 __usage_cmd(cmd
, "", true);
253 static void version(void)
255 printf("iw version %s\n", iw_version
);
258 static int phy_lookup(char *name
)
263 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
265 fd
= open(buf
, O_RDONLY
);
268 pos
= read(fd
, buf
, sizeof(buf
) - 1);
278 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
281 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)err
- 1;
282 int len
= nlh
->nlmsg_len
;
283 struct nlattr
*attrs
;
284 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
286 int ack_len
= sizeof(*nlh
) + sizeof(int) + sizeof(*nlh
);
290 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
293 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
294 ack_len
+= err
->msg
.nlmsg_len
- sizeof(*nlh
);
299 attrs
= (void *)((unsigned char *)nlh
+ ack_len
);
302 nla_parse(tb
, NLMSGERR_ATTR_MAX
, attrs
, len
, NULL
);
303 if (tb
[NLMSGERR_ATTR_MSG
]) {
304 len
= strnlen((char *)nla_data(tb
[NLMSGERR_ATTR_MSG
]),
305 nla_len(tb
[NLMSGERR_ATTR_MSG
]));
306 fprintf(stderr
, "kernel reports: %*s\n", len
,
307 (char *)nla_data(tb
[NLMSGERR_ATTR_MSG
]));
313 static int finish_handler(struct nl_msg
*msg
, void *arg
)
320 static int ack_handler(struct nl_msg
*msg
, void *arg
)
327 static int (*registered_handler
)(struct nl_msg
*, void *);
328 static void *registered_handler_data
;
330 void register_handler(int (*handler
)(struct nl_msg
*, void *), void *data
)
332 registered_handler
= handler
;
333 registered_handler_data
= data
;
336 int valid_handler(struct nl_msg
*msg
, void *arg
)
338 if (registered_handler
)
339 return registered_handler(msg
, registered_handler_data
);
344 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
345 int argc
, char **argv
, const struct cmd
**cmdout
)
347 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
351 signed long long devidx
= 0;
353 const char *command
, *section
;
355 enum command_identify_by command_idby
= CIB_NONE
;
357 if (argc
<= 1 && idby
!= II_NONE
)
365 command_idby
= CIB_PHY
;
366 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
373 command_idby
= CIB_PHY
;
374 devidx
= phy_lookup(*argv
);
379 command_idby
= CIB_NETDEV
;
380 devidx
= if_nametoindex(*argv
);
387 command_idby
= CIB_WDEV
;
388 devidx
= strtoll(*argv
, &tmp
, 0);
404 for_each_cmd(sectcmd
) {
407 /* ok ... bit of a hack for the dupe 'info' section */
408 if (match
&& sectcmd
->idby
!= command_idby
)
410 if (strcmp(sectcmd
->name
, section
) == 0)
425 if (cmd
->parent
!= sectcmd
)
428 * ignore mismatch id by, but allow WDEV
431 if (cmd
->idby
!= command_idby
&&
432 !(cmd
->idby
== CIB_NETDEV
&&
433 command_idby
== CIB_WDEV
))
435 if (strcmp(cmd
->name
, command
))
437 if (argc
> 1 && !cmd
->args
)
452 /* Use the section itself, if possible. */
454 if (argc
&& !cmd
->args
)
456 if (cmd
->idby
!= command_idby
&&
457 !(cmd
->idby
== CIB_NETDEV
&& command_idby
== CIB_WDEV
))
464 cmd
= cmd
->selector(argc
, argv
);
475 return cmd
->handler(state
, NULL
, argc
, argv
, idby
);
480 fprintf(stderr
, "failed to allocate netlink message\n");
484 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
485 s_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
487 fprintf(stderr
, "failed to allocate netlink callbacks\n");
492 genlmsg_put(msg
, 0, 0, state
->nl80211_id
, 0,
493 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
495 switch (command_idby
) {
497 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
500 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
503 NLA_PUT_U64(msg
, NL80211_ATTR_WDEV
, devidx
);
509 err
= cmd
->handler(state
, msg
, argc
, argv
, idby
);
513 nl_socket_set_cb(state
->nl_sock
, s_cb
);
515 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
521 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
522 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
523 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
524 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, valid_handler
, NULL
);
527 nl_recvmsgs(state
->nl_sock
, cb
);
534 fprintf(stderr
, "building message failed\n");
538 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
539 int argc
, char **argv
)
541 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
544 int main(int argc
, char **argv
)
546 struct nl80211_state nlstate
;
548 const struct cmd
*cmd
= NULL
;
550 /* calculate command size including padding */
551 cmd_size
= labs((long)&__section_set
- (long)&__section_get
);
556 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
562 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
567 /* need to treat "help" command specially so it works w/o nl80211 */
568 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
569 usage(argc
- 1, argv
+ 1);
573 err
= nl80211_init(&nlstate
);
577 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
580 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
581 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
582 if (strlen(*argv
) == 3) {
585 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
586 } else if (*(*argv
+ 3) == '#')
587 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
590 } else if (strcmp(*argv
, "wdev") == 0 && argc
> 1) {
593 err
= __handle_cmd(&nlstate
, II_WDEV
, argc
, argv
, &cmd
);
596 enum id_input idby
= II_NONE
;
598 if ((idx
= if_nametoindex(argv
[0])) != 0)
600 else if ((idx
= phy_lookup(argv
[0])) >= 0)
602 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
605 if (err
== HANDLER_RET_USAGE
) {
610 } else if (err
== HANDLER_RET_DONE
) {
613 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
615 nl80211_cleanup(&nlstate
);