2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <sys/types.h>
16 #include <linux/netlink.h>
18 #include <netlink/genl/genl.h>
19 #include <netlink/genl/family.h>
20 #include <netlink/genl/ctrl.h>
21 #include <netlink/msg.h>
22 #include <netlink/attr.h>
27 /* libnl 1.x compatibility code */
28 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
29 static inline struct nl_handle
*nl_socket_alloc(void)
31 return nl_handle_alloc();
34 static inline void nl_socket_free(struct nl_sock
*h
)
39 static inline int nl_socket_set_buffer_size(struct nl_sock
*sk
,
42 return nl_set_buffer_size(sk
, rxbuf
, txbuf
);
44 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
48 static int nl80211_init(struct nl80211_state
*state
)
52 state
->nl_sock
= nl_socket_alloc();
53 if (!state
->nl_sock
) {
54 fprintf(stderr
, "Failed to allocate netlink socket.\n");
58 if (genl_connect(state
->nl_sock
)) {
59 fprintf(stderr
, "Failed to connect to generic netlink.\n");
61 goto out_handle_destroy
;
64 nl_socket_set_buffer_size(state
->nl_sock
, 8192, 8192);
66 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
68 setsockopt(nl_socket_get_fd(state
->nl_sock
), SOL_NETLINK
,
69 NETLINK_EXT_ACK
, &err
, sizeof(err
));
71 state
->nl80211_id
= genl_ctrl_resolve(state
->nl_sock
, "nl80211");
72 if (state
->nl80211_id
< 0) {
73 fprintf(stderr
, "nl80211 not found.\n");
75 goto out_handle_destroy
;
81 nl_socket_free(state
->nl_sock
);
85 static void nl80211_cleanup(struct nl80211_state
*state
)
87 nl_socket_free(state
->nl_sock
);
92 extern struct cmd __start___cmd
;
93 extern struct cmd __stop___cmd
;
95 #define for_each_cmd(_cmd) \
96 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
97 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
100 static void __usage_cmd(const struct cmd
*cmd
, char *indent
, bool full
)
102 const char *start
, *lend
, *end
;
104 printf("%s", indent
);
110 printf("phy <phyname> ");
113 printf("dev <devname> ");
116 printf("wdev <idx> ");
119 if (cmd
->parent
&& cmd
->parent
->name
)
120 printf("%s ", cmd
->parent
->name
);
121 printf("%s", cmd
->name
);
124 /* print line by line */
126 end
= strchr(start
, '\0');
129 lend
= strchr(start
, '\n');
132 if (start
!= cmd
->args
) {
138 printf("phy <phyname> ");
141 printf("dev <devname> ");
144 printf("wdev <idx> ");
147 if (cmd
->parent
&& cmd
->parent
->name
)
148 printf("%s ", cmd
->parent
->name
);
149 printf("%s ", cmd
->name
);
151 printf("%.*s\n", (int)(lend
- start
), start
);
153 } while (end
!= lend
);
157 if (!full
|| !cmd
->help
)
166 /* print line by line */
168 end
= strchr(start
, '\0');
170 lend
= strchr(start
, '\n');
173 printf("%s", indent
);
174 printf("%.*s\n", (int)(lend
- start
), start
);
176 } while (end
!= lend
);
181 static void usage_options(void)
183 printf("Options:\n");
184 printf("\t--debug\t\tenable netlink debugging\n");
187 static const char *argv0
;
189 static void usage(int argc
, char **argv
)
191 const struct cmd
*section
, *cmd
;
192 bool full
= argc
>= 0;
193 const char *sect_filt
= NULL
;
194 const char *cmd_filt
= NULL
;
202 printf("Usage:\t%s [options] command\n", argv0
);
204 printf("\t--version\tshow version (%s)\n", iw_version
);
205 printf("Commands:\n");
206 for_each_cmd(section
) {
210 if (sect_filt
&& strcmp(section
->name
, sect_filt
))
213 if (section
->handler
&& !section
->hidden
)
214 __usage_cmd(section
, "\t", full
);
217 if (section
!= cmd
->parent
)
219 if (!cmd
->handler
|| cmd
->hidden
)
221 if (cmd_filt
&& strcmp(cmd
->name
, cmd_filt
))
223 __usage_cmd(cmd
, "\t", full
);
226 printf("\nCommands that use the netdev ('dev') can also be given the\n"
227 "'wdev' instead to identify the device.\n");
228 printf("\nYou can omit the 'phy' or 'dev' if "
229 "the identification is unique,\n"
230 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
231 "(Don't when scripting.)\n\n"
232 "Do NOT screenscrape this tool, we don't "
233 "consider its output stable.\n\n");
236 static int print_help(struct nl80211_state
*state
,
238 int argc
, char **argv
,
243 TOPLEVEL(help
, "[command]", 0, 0, CIB_NONE
, print_help
,
244 "Print usage for all or a specific command, e.g.\n"
245 "\"help wowlan\" or \"help wowlan enable\".");
247 static void usage_cmd(const struct cmd
*cmd
)
249 printf("Usage:\t%s [options] ", argv0
);
250 __usage_cmd(cmd
, "", true);
254 static void version(void)
256 printf("iw version %s\n", iw_version
);
259 static int phy_lookup(char *name
)
264 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
266 fd
= open(buf
, O_RDONLY
);
269 pos
= read(fd
, buf
, sizeof(buf
) - 1);
279 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
282 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)err
- 1;
283 int len
= nlh
->nlmsg_len
;
284 struct nlattr
*attrs
;
285 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
287 int ack_len
= sizeof(*nlh
) + sizeof(int) + sizeof(*nlh
);
291 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
294 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
295 ack_len
+= err
->msg
.nlmsg_len
- sizeof(*nlh
);
300 attrs
= (void *)((unsigned char *)nlh
+ ack_len
);
303 nla_parse(tb
, NLMSGERR_ATTR_MAX
, attrs
, len
, NULL
);
304 if (tb
[NLMSGERR_ATTR_MSG
]) {
305 len
= strnlen((char *)nla_data(tb
[NLMSGERR_ATTR_MSG
]),
306 nla_len(tb
[NLMSGERR_ATTR_MSG
]));
307 fprintf(stderr
, "kernel reports: %*s\n", len
,
308 (char *)nla_data(tb
[NLMSGERR_ATTR_MSG
]));
314 static int finish_handler(struct nl_msg
*msg
, void *arg
)
321 static int ack_handler(struct nl_msg
*msg
, void *arg
)
328 static int (*registered_handler
)(struct nl_msg
*, void *);
329 static void *registered_handler_data
;
331 void register_handler(int (*handler
)(struct nl_msg
*, void *), void *data
)
333 registered_handler
= handler
;
334 registered_handler_data
= data
;
337 int valid_handler(struct nl_msg
*msg
, void *arg
)
339 if (registered_handler
)
340 return registered_handler(msg
, registered_handler_data
);
345 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
346 int argc
, char **argv
, const struct cmd
**cmdout
)
348 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
352 signed long long devidx
= 0;
354 const char *command
, *section
;
356 enum command_identify_by command_idby
= CIB_NONE
;
358 if (argc
<= 1 && idby
!= II_NONE
)
366 command_idby
= CIB_PHY
;
367 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
374 command_idby
= CIB_PHY
;
375 devidx
= phy_lookup(*argv
);
380 command_idby
= CIB_NETDEV
;
381 devidx
= if_nametoindex(*argv
);
388 command_idby
= CIB_WDEV
;
389 devidx
= strtoll(*argv
, &tmp
, 0);
405 for_each_cmd(sectcmd
) {
408 /* ok ... bit of a hack for the dupe 'info' section */
409 if (match
&& sectcmd
->idby
!= command_idby
)
411 if (strcmp(sectcmd
->name
, section
) == 0)
426 if (cmd
->parent
!= sectcmd
)
429 * ignore mismatch id by, but allow WDEV
432 if (cmd
->idby
!= command_idby
&&
433 !(cmd
->idby
== CIB_NETDEV
&&
434 command_idby
== CIB_WDEV
))
436 if (strcmp(cmd
->name
, command
))
438 if (argc
> 1 && !cmd
->args
)
453 /* Use the section itself, if possible. */
455 if (argc
&& !cmd
->args
)
457 if (cmd
->idby
!= command_idby
&&
458 !(cmd
->idby
== CIB_NETDEV
&& command_idby
== CIB_WDEV
))
465 cmd
= cmd
->selector(argc
, argv
);
476 return cmd
->handler(state
, NULL
, argc
, argv
, idby
);
481 fprintf(stderr
, "failed to allocate netlink message\n");
485 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
486 s_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
488 fprintf(stderr
, "failed to allocate netlink callbacks\n");
493 genlmsg_put(msg
, 0, 0, state
->nl80211_id
, 0,
494 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
496 switch (command_idby
) {
498 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
501 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
504 NLA_PUT_U64(msg
, NL80211_ATTR_WDEV
, devidx
);
510 err
= cmd
->handler(state
, msg
, argc
, argv
, idby
);
514 nl_socket_set_cb(state
->nl_sock
, s_cb
);
516 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
522 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
523 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
524 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
525 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, valid_handler
, NULL
);
528 nl_recvmsgs(state
->nl_sock
, cb
);
535 fprintf(stderr
, "building message failed\n");
539 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
540 int argc
, char **argv
)
542 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
545 int main(int argc
, char **argv
)
547 struct nl80211_state nlstate
;
549 const struct cmd
*cmd
= NULL
;
551 /* calculate command size including padding */
552 cmd_size
= labs((long)&__section_set
- (long)&__section_get
);
557 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
563 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
568 /* need to treat "help" command specially so it works w/o nl80211 */
569 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
570 usage(argc
- 1, argv
+ 1);
574 err
= nl80211_init(&nlstate
);
578 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
581 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
582 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
583 if (strlen(*argv
) == 3) {
586 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
587 } else if (*(*argv
+ 3) == '#')
588 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
591 } else if (strcmp(*argv
, "wdev") == 0 && argc
> 1) {
594 err
= __handle_cmd(&nlstate
, II_WDEV
, argc
, argv
, &cmd
);
597 enum id_input idby
= II_NONE
;
599 if ((idx
= if_nametoindex(argv
[0])) != 0)
601 else if ((idx
= phy_lookup(argv
[0])) >= 0)
603 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
606 if (err
== HANDLER_RET_USAGE
) {
611 } else if (err
== HANDLER_RET_DONE
) {
614 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
616 nl80211_cleanup(&nlstate
);