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 #ifndef CONFIG_LIBNL20
27 /* libnl 2.0 compatibility code */
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 __genl_ctrl_alloc_cache(struct nl_sock
*h
, struct nl_cache
**cache
)
41 struct nl_cache
*tmp
= genl_ctrl_alloc_cache(h
);
47 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
48 #endif /* CONFIG_LIBNL20 */
52 static int nl80211_init(struct nl80211_state
*state
)
56 state
->nl_sock
= nl_socket_alloc();
57 if (!state
->nl_sock
) {
58 fprintf(stderr
, "Failed to allocate netlink socket.\n");
62 if (genl_connect(state
->nl_sock
)) {
63 fprintf(stderr
, "Failed to connect to generic netlink.\n");
65 goto out_handle_destroy
;
68 if (genl_ctrl_alloc_cache(state
->nl_sock
, &state
->nl_cache
)) {
69 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
71 goto out_handle_destroy
;
74 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
75 if (!state
->nl80211
) {
76 fprintf(stderr
, "nl80211 not found.\n");
84 nl_cache_free(state
->nl_cache
);
86 nl_socket_free(state
->nl_sock
);
90 static void nl80211_cleanup(struct nl80211_state
*state
)
92 genl_family_put(state
->nl80211
);
93 nl_cache_free(state
->nl_cache
);
94 nl_socket_free(state
->nl_sock
);
99 extern struct cmd __start___cmd
;
100 extern struct cmd __stop___cmd
;
102 #define for_each_cmd(_cmd) \
103 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
104 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
107 static void __usage_cmd(const struct cmd
*cmd
, char *indent
, bool full
)
109 const char *start
, *lend
, *end
;
111 printf("%s", indent
);
117 printf("phy <phyname> ");
120 printf("dev <devname> ");
123 if (cmd
->parent
&& cmd
->parent
->name
)
124 printf("%s ", cmd
->parent
->name
);
125 printf("%s", cmd
->name
);
127 printf(" %s", cmd
->args
);
130 if (!full
|| !cmd
->help
)
139 /* print line by line */
141 end
= strchr(start
, '\0');
143 lend
= strchr(start
, '\n');
146 printf("%s", indent
);
147 printf("%.*s\n", (int)(lend
- start
), start
);
149 } while (end
!= lend
);
154 static void usage_options(void)
156 printf("Options:\n");
157 printf("\t--debug\t\tenable netlink debugging\n");
160 static const char *argv0
;
162 static void usage(bool full
)
164 const struct cmd
*section
, *cmd
;
166 printf("Usage:\t%s [options] command\n", argv0
);
168 printf("\t--version\tshow version (%s)\n", iw_version
);
169 printf("Commands:\n");
170 for_each_cmd(section
) {
174 if (section
->handler
&& !section
->hidden
)
175 __usage_cmd(section
, "\t", full
);
178 if (section
!= cmd
->parent
)
180 if (!cmd
->handler
|| cmd
->hidden
)
182 __usage_cmd(cmd
, "\t", full
);
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");
193 static int print_help(struct nl80211_state
*state
,
196 int argc
, char **argv
)
200 TOPLEVEL(help
, NULL
, 0, 0, CIB_NONE
, print_help
,
201 "Print usage for each command.");
203 static void usage_cmd(const struct cmd
*cmd
)
205 printf("Usage:\t%s [options] ", argv0
);
206 __usage_cmd(cmd
, "", true);
210 static void version(void)
212 printf("iw version %s\n", iw_version
);
215 static int phy_lookup(char *name
)
220 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
222 fd
= open(buf
, O_RDONLY
);
225 pos
= read(fd
, buf
, sizeof(buf
) - 1);
235 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
243 static int finish_handler(struct nl_msg
*msg
, void *arg
)
250 static int ack_handler(struct nl_msg
*msg
, void *arg
)
257 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
258 int argc
, char **argv
, const struct cmd
**cmdout
)
260 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
265 const char *command
, *section
;
267 enum command_identify_by command_idby
= CIB_NONE
;
269 if (argc
<= 1 && idby
!= II_NONE
)
277 command_idby
= CIB_PHY
;
278 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
285 command_idby
= CIB_PHY
;
286 devidx
= phy_lookup(*argv
);
291 command_idby
= CIB_NETDEV
;
292 devidx
= if_nametoindex(*argv
);
309 for_each_cmd(sectcmd
) {
312 /* ok ... bit of a hack for the dupe 'info' section */
313 if (match
&& sectcmd
->idby
!= command_idby
)
315 if (strcmp(sectcmd
->name
, section
) == 0)
330 if (cmd
->parent
!= sectcmd
)
332 if (cmd
->idby
!= command_idby
)
334 if (strcmp(cmd
->name
, command
))
336 if (argc
> 1 && !cmd
->args
)
351 /* Use the section itself, if possible. */
353 if (argc
&& !cmd
->args
)
355 if (cmd
->idby
!= command_idby
)
362 cmd
= cmd
->selector(argc
, argv
);
373 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
378 fprintf(stderr
, "failed to allocate netlink message\n");
382 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
384 fprintf(stderr
, "failed to allocate netlink callbacks\n");
389 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
390 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
392 switch (command_idby
) {
394 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
397 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
403 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
407 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
413 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
414 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
415 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
418 nl_recvmsgs(state
->nl_sock
, cb
);
425 fprintf(stderr
, "building message failed\n");
429 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
430 int argc
, char **argv
)
432 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
435 int main(int argc
, char **argv
)
437 struct nl80211_state nlstate
;
439 const struct cmd
*cmd
= NULL
;
441 /* calculate command size including padding */
442 cmd_size
= abs((long)&__section_set
- (long)&__section_get
);
447 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
453 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
458 /* need to treat "help" command specially so it works w/o nl80211 */
459 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
464 err
= nl80211_init(&nlstate
);
468 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
471 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
472 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
473 if (strlen(*argv
) == 3) {
476 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
477 } else if (*(*argv
+ 3) == '#')
478 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
483 enum id_input idby
= II_NONE
;
485 if ((idx
= if_nametoindex(argv
[0])) != 0)
487 else if ((idx
= phy_lookup(argv
[0])) >= 0)
489 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
498 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
500 nl80211_cleanup(&nlstate
);