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 printf("wdev <idx> ");
105 if (cmd
->parent
&& cmd
->parent
->name
)
106 printf("%s ", cmd
->parent
->name
);
107 printf("%s", cmd
->name
);
109 printf(" %s", cmd
->args
);
112 if (!full
|| !cmd
->help
)
121 /* print line by line */
123 end
= strchr(start
, '\0');
125 lend
= strchr(start
, '\n');
128 printf("%s", indent
);
129 printf("%.*s\n", (int)(lend
- start
), start
);
131 } while (end
!= lend
);
136 static void usage_options(void)
138 printf("Options:\n");
139 printf("\t--debug\t\tenable netlink debugging\n");
142 static const char *argv0
;
144 static void usage(int argc
, char **argv
)
146 const struct cmd
*section
, *cmd
;
147 bool full
= argc
>= 0;
148 const char *sect_filt
= NULL
;
149 const char *cmd_filt
= NULL
;
157 printf("Usage:\t%s [options] command\n", argv0
);
159 printf("\t--version\tshow version (%s)\n", iw_version
);
160 printf("Commands:\n");
161 for_each_cmd(section
) {
165 if (sect_filt
&& strcmp(section
->name
, sect_filt
))
168 if (section
->handler
&& !section
->hidden
)
169 __usage_cmd(section
, "\t", full
);
172 if (section
!= cmd
->parent
)
174 if (!cmd
->handler
|| cmd
->hidden
)
176 if (cmd_filt
&& strcmp(cmd
->name
, cmd_filt
))
178 __usage_cmd(cmd
, "\t", full
);
181 printf("\nYou can omit the 'phy' or 'dev' if "
182 "the identification is unique,\n"
183 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
184 "(Don't when scripting.)\n\n"
185 "Do NOT screenscrape this tool, we don't "
186 "consider its output stable.\n\n");
189 static int print_help(struct nl80211_state
*state
,
192 int argc
, char **argv
,
197 TOPLEVEL(help
, "[command]", 0, 0, CIB_NONE
, print_help
,
198 "Print usage for all or a specific command, e.g.\n"
199 "\"help wowlan\" or \"help wowlan enable\".");
201 static void usage_cmd(const struct cmd
*cmd
)
203 printf("Usage:\t%s [options] ", argv0
);
204 __usage_cmd(cmd
, "", true);
208 static void version(void)
210 printf("iw version %s\n", iw_version
);
213 static int phy_lookup(char *name
)
218 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
220 fd
= open(buf
, O_RDONLY
);
223 pos
= read(fd
, buf
, sizeof(buf
) - 1);
233 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
241 static int finish_handler(struct nl_msg
*msg
, void *arg
)
248 static int ack_handler(struct nl_msg
*msg
, void *arg
)
255 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
256 int argc
, char **argv
, const struct cmd
**cmdout
)
258 const struct cmd
*cmd
, *match
= NULL
, *sectcmd
;
262 signed long long devidx
= 0;
264 const char *command
, *section
;
266 enum command_identify_by command_idby
= CIB_NONE
;
268 if (argc
<= 1 && idby
!= II_NONE
)
276 command_idby
= CIB_PHY
;
277 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
284 command_idby
= CIB_PHY
;
285 devidx
= phy_lookup(*argv
);
290 command_idby
= CIB_NETDEV
;
291 devidx
= if_nametoindex(*argv
);
298 command_idby
= CIB_WDEV
;
299 devidx
= strtoll(*argv
, &tmp
, 0);
315 for_each_cmd(sectcmd
) {
318 /* ok ... bit of a hack for the dupe 'info' section */
319 if (match
&& sectcmd
->idby
!= command_idby
)
321 if (strcmp(sectcmd
->name
, section
) == 0)
336 if (cmd
->parent
!= sectcmd
)
338 if (cmd
->idby
!= command_idby
)
340 if (strcmp(cmd
->name
, command
))
342 if (argc
> 1 && !cmd
->args
)
357 /* Use the section itself, if possible. */
359 if (argc
&& !cmd
->args
)
361 if (cmd
->idby
!= command_idby
)
368 cmd
= cmd
->selector(argc
, argv
);
379 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
, idby
);
384 fprintf(stderr
, "failed to allocate netlink message\n");
388 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
389 s_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
391 fprintf(stderr
, "failed to allocate netlink callbacks\n");
396 genlmsg_put(msg
, 0, 0, state
->nl80211_id
, 0,
397 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
399 switch (command_idby
) {
401 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
404 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
407 NLA_PUT_U64(msg
, NL80211_ATTR_WDEV
, devidx
);
413 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
, idby
);
417 nl_socket_set_cb(state
->nl_sock
, s_cb
);
419 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
425 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
426 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
427 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
430 nl_recvmsgs(state
->nl_sock
, cb
);
437 fprintf(stderr
, "building message failed\n");
441 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
442 int argc
, char **argv
)
444 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
447 int main(int argc
, char **argv
)
449 struct nl80211_state nlstate
;
451 const struct cmd
*cmd
= NULL
;
453 /* calculate command size including padding */
454 cmd_size
= abs((long)&__section_set
- (long)&__section_get
);
459 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
465 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
470 /* need to treat "help" command specially so it works w/o nl80211 */
471 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
472 usage(argc
- 1, argv
+ 1);
476 err
= nl80211_init(&nlstate
);
480 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
483 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
484 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
485 if (strlen(*argv
) == 3) {
488 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
489 } else if (*(*argv
+ 3) == '#')
490 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
493 } else if (strcmp(*argv
, "wdev") == 0 && argc
> 1) {
496 err
= __handle_cmd(&nlstate
, II_WDEV
, argc
, argv
, &cmd
);
499 enum id_input idby
= II_NONE
;
501 if ((idx
= if_nametoindex(argv
[0])) != 0)
503 else if ((idx
= phy_lookup(argv
[0])) >= 0)
505 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
514 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
516 nl80211_cleanup(&nlstate
);