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(int argc
, char **argv
)
143 const struct cmd
*section
, *cmd
;
144 bool full
= argc
>= 0;
145 const char *sect_filt
= NULL
;
146 const char *cmd_filt
= NULL
;
154 printf("Usage:\t%s [options] command\n", argv0
);
156 printf("\t--version\tshow version (%s)\n", iw_version
);
157 printf("Commands:\n");
158 for_each_cmd(section
) {
162 if (sect_filt
&& strcmp(section
->name
, sect_filt
))
165 if (section
->handler
&& !section
->hidden
)
166 __usage_cmd(section
, "\t", full
);
169 if (section
!= cmd
->parent
)
171 if (!cmd
->handler
|| cmd
->hidden
)
173 if (cmd_filt
&& strcmp(cmd
->name
, cmd_filt
))
175 __usage_cmd(cmd
, "\t", full
);
178 printf("\nYou can omit the 'phy' or 'dev' if "
179 "the identification is unique,\n"
180 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
181 "(Don't when scripting.)\n\n"
182 "Do NOT screenscrape this tool, we don't "
183 "consider its output stable.\n\n");
186 static int print_help(struct nl80211_state
*state
,
189 int argc
, char **argv
)
193 TOPLEVEL(help
, "[command]", 0, 0, CIB_NONE
, print_help
,
194 "Print usage for all or a specific command, e.g.\n"
195 "\"help wowlan\" or \"help wowlan enable\".");
197 static void usage_cmd(const struct cmd
*cmd
)
199 printf("Usage:\t%s [options] ", argv0
);
200 __usage_cmd(cmd
, "", true);
204 static void version(void)
206 printf("iw version %s\n", iw_version
);
209 static int phy_lookup(char *name
)
214 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
216 fd
= open(buf
, O_RDONLY
);
219 pos
= read(fd
, buf
, sizeof(buf
) - 1);
229 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
237 static int finish_handler(struct nl_msg
*msg
, void *arg
)
244 static int ack_handler(struct nl_msg
*msg
, void *arg
)
251 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
252 int argc
, char **argv
, const struct cmd
**cmdout
)
254 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
260 const char *command
, *section
;
262 enum command_identify_by command_idby
= CIB_NONE
;
264 if (argc
<= 1 && idby
!= II_NONE
)
272 command_idby
= CIB_PHY
;
273 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
280 command_idby
= CIB_PHY
;
281 devidx
= phy_lookup(*argv
);
286 command_idby
= CIB_NETDEV
;
287 devidx
= if_nametoindex(*argv
);
304 for_each_cmd(sectcmd
) {
307 /* ok ... bit of a hack for the dupe 'info' section */
308 if (match
&& sectcmd
->idby
!= command_idby
)
310 if (strcmp(sectcmd
->name
, section
) == 0)
325 if (cmd
->parent
!= sectcmd
)
327 if (cmd
->idby
!= command_idby
)
329 if (strcmp(cmd
->name
, command
))
331 if (argc
> 1 && !cmd
->args
)
346 /* Use the section itself, if possible. */
348 if (argc
&& !cmd
->args
)
350 if (cmd
->idby
!= command_idby
)
357 cmd
= cmd
->selector(argc
, argv
);
368 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
373 fprintf(stderr
, "failed to allocate netlink message\n");
377 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
378 s_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
380 fprintf(stderr
, "failed to allocate netlink callbacks\n");
385 genlmsg_put(msg
, 0, 0, state
->nl80211_id
, 0,
386 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
388 switch (command_idby
) {
390 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
393 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
399 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
403 nl_socket_set_cb(state
->nl_sock
, s_cb
);
405 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
411 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
412 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
413 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
416 nl_recvmsgs(state
->nl_sock
, cb
);
423 fprintf(stderr
, "building message failed\n");
427 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
428 int argc
, char **argv
)
430 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
433 int main(int argc
, char **argv
)
435 struct nl80211_state nlstate
;
437 const struct cmd
*cmd
= NULL
;
439 /* calculate command size including padding */
440 cmd_size
= abs((long)&__section_set
- (long)&__section_get
);
445 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
451 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
456 /* need to treat "help" command specially so it works w/o nl80211 */
457 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
458 usage(argc
- 1, argv
+ 1);
462 err
= nl80211_init(&nlstate
);
466 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
469 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
470 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
471 if (strlen(*argv
) == 3) {
474 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
475 } else if (*(*argv
+ 3) == '#')
476 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
481 enum id_input idby
= II_NONE
;
483 if ((idx
= if_nametoindex(argv
[0])) != 0)
485 else if ((idx
= phy_lookup(argv
[0])) >= 0)
487 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
496 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
498 nl80211_cleanup(&nlstate
);