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
)
37 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
41 static int nl80211_init(struct nl80211_state
*state
)
45 state
->nl_sock
= nl_socket_alloc();
46 if (!state
->nl_sock
) {
47 fprintf(stderr
, "Failed to allocate netlink socket.\n");
51 if (genl_connect(state
->nl_sock
)) {
52 fprintf(stderr
, "Failed to connect to generic netlink.\n");
54 goto out_handle_destroy
;
57 state
->nl80211_id
= genl_ctrl_resolve(state
->nl_sock
, "nl80211");
58 if (state
->nl80211_id
< 0) {
59 fprintf(stderr
, "nl80211 not found.\n");
61 goto out_handle_destroy
;
67 nl_socket_free(state
->nl_sock
);
71 static void nl80211_cleanup(struct nl80211_state
*state
)
73 nl_socket_free(state
->nl_sock
);
78 extern struct cmd __start___cmd
;
79 extern struct cmd __stop___cmd
;
81 #define for_each_cmd(_cmd) \
82 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
83 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
86 static void __usage_cmd(const struct cmd
*cmd
, char *indent
, bool full
)
88 const char *start
, *lend
, *end
;
96 printf("phy <phyname> ");
99 printf("dev <devname> ");
102 if (cmd
->parent
&& cmd
->parent
->name
)
103 printf("%s ", cmd
->parent
->name
);
104 printf("%s", cmd
->name
);
106 printf(" %s", cmd
->args
);
109 if (!full
|| !cmd
->help
)
118 /* print line by line */
120 end
= strchr(start
, '\0');
122 lend
= strchr(start
, '\n');
125 printf("%s", indent
);
126 printf("%.*s\n", (int)(lend
- start
), start
);
128 } while (end
!= lend
);
133 static void usage_options(void)
135 printf("Options:\n");
136 printf("\t--debug\t\tenable netlink debugging\n");
139 static const char *argv0
;
141 static void usage(bool full
)
143 const struct cmd
*section
, *cmd
;
145 printf("Usage:\t%s [options] command\n", argv0
);
147 printf("\t--version\tshow version (%s)\n", iw_version
);
148 printf("Commands:\n");
149 for_each_cmd(section
) {
153 if (section
->handler
&& !section
->hidden
)
154 __usage_cmd(section
, "\t", full
);
157 if (section
!= cmd
->parent
)
159 if (!cmd
->handler
|| cmd
->hidden
)
161 __usage_cmd(cmd
, "\t", full
);
164 printf("\nYou can omit the 'phy' or 'dev' if "
165 "the identification is unique,\n"
166 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
167 "(Don't when scripting.)\n\n"
168 "Do NOT screenscrape this tool, we don't "
169 "consider its output stable.\n\n");
172 static int print_help(struct nl80211_state
*state
,
175 int argc
, char **argv
)
179 TOPLEVEL(help
, NULL
, 0, 0, CIB_NONE
, print_help
,
180 "Print usage for each command.");
182 static void usage_cmd(const struct cmd
*cmd
)
184 printf("Usage:\t%s [options] ", argv0
);
185 __usage_cmd(cmd
, "", true);
189 static void version(void)
191 printf("iw version %s\n", iw_version
);
194 static int phy_lookup(char *name
)
199 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
201 fd
= open(buf
, O_RDONLY
);
204 pos
= read(fd
, buf
, sizeof(buf
) - 1);
214 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
222 static int finish_handler(struct nl_msg
*msg
, void *arg
)
229 static int ack_handler(struct nl_msg
*msg
, void *arg
)
236 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
237 int argc
, char **argv
, const struct cmd
**cmdout
)
239 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
245 const char *command
, *section
;
247 enum command_identify_by command_idby
= CIB_NONE
;
249 if (argc
<= 1 && idby
!= II_NONE
)
257 command_idby
= CIB_PHY
;
258 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
265 command_idby
= CIB_PHY
;
266 devidx
= phy_lookup(*argv
);
271 command_idby
= CIB_NETDEV
;
272 devidx
= if_nametoindex(*argv
);
289 for_each_cmd(sectcmd
) {
292 /* ok ... bit of a hack for the dupe 'info' section */
293 if (match
&& sectcmd
->idby
!= command_idby
)
295 if (strcmp(sectcmd
->name
, section
) == 0)
310 if (cmd
->parent
!= sectcmd
)
312 if (cmd
->idby
!= command_idby
)
314 if (strcmp(cmd
->name
, command
))
316 if (argc
> 1 && !cmd
->args
)
331 /* Use the section itself, if possible. */
333 if (argc
&& !cmd
->args
)
335 if (cmd
->idby
!= command_idby
)
342 cmd
= cmd
->selector(argc
, argv
);
353 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
358 fprintf(stderr
, "failed to allocate netlink message\n");
362 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
363 s_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
365 fprintf(stderr
, "failed to allocate netlink callbacks\n");
370 genlmsg_put(msg
, 0, 0, state
->nl80211_id
, 0,
371 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
373 switch (command_idby
) {
375 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
378 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
384 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
388 nl_socket_set_cb(state
->nl_sock
, s_cb
);
390 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
396 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
397 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
398 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
401 nl_recvmsgs(state
->nl_sock
, cb
);
408 fprintf(stderr
, "building message failed\n");
412 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
413 int argc
, char **argv
)
415 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
418 int main(int argc
, char **argv
)
420 struct nl80211_state nlstate
;
422 const struct cmd
*cmd
= NULL
;
424 /* calculate command size including padding */
425 cmd_size
= abs((long)&__section_set
- (long)&__section_get
);
430 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
436 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
441 /* need to treat "help" command specially so it works w/o nl80211 */
442 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
447 err
= nl80211_init(&nlstate
);
451 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
454 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
455 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
456 if (strlen(*argv
) == 3) {
459 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
460 } else if (*(*argv
+ 3) == '#')
461 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
466 enum id_input idby
= II_NONE
;
468 if ((idx
= if_nametoindex(argv
[0])) != 0)
470 else if ((idx
= phy_lookup(argv
[0])) >= 0)
472 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
481 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
483 nl80211_cleanup(&nlstate
);