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
);
97 __COMMAND(NULL
, NULL
, "", NULL
, 0, 0, 0, CIB_NONE
, NULL
, NULL
);
98 __COMMAND(NULL
, NULL
, "", NULL
, 1, 0, 0, CIB_NONE
, NULL
, NULL
);
102 static void __usage_cmd(struct cmd
*cmd
, char *indent
, bool full
)
104 const char *start
, *lend
, *end
;
106 printf("%s", indent
);
112 printf("phy <phyname> ");
115 printf("dev <devname> ");
119 printf("%s ", cmd
->section
);
120 printf("%s", cmd
->name
);
122 printf(" %s", cmd
->args
);
125 if (!full
|| !cmd
->help
)
134 /* print line by line */
136 end
= strchr(start
, '\0');
138 lend
= strchr(start
, '\n');
141 printf("%s", indent
);
142 printf("%.*s\n", (int)(lend
- start
), start
);
144 } while (end
!= lend
);
149 static void usage_options(void)
151 printf("Options:\n");
152 printf("\t--debug\t\tenable netlink debugging\n");
155 static const char *argv0
;
157 static void usage(bool full
)
161 printf("Usage:\t%s [options] command\n", argv0
);
163 printf("\t--version\tshow version (%s)\n", iw_version
);
164 printf("Commands:\n");
165 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
166 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
167 if (!cmd
->handler
|| cmd
->hidden
)
169 __usage_cmd(cmd
, "\t", full
);
171 printf("\nYou can omit the 'phy' or 'dev' if "
172 "the identification is unique,\n"
173 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
174 "(Don't when scripting.)\n\n"
175 "Do NOT screenscrape this tool, we don't "
176 "consider its output stable.\n\n");
179 static int print_help(struct nl80211_state
*state
,
182 int argc
, char **argv
)
186 TOPLEVEL(help
, NULL
, 0, 0, CIB_NONE
, print_help
,
187 "Print usage for each command.");
189 static void usage_cmd(struct cmd
*cmd
)
191 printf("Usage:\t%s [options] ", argv0
);
192 __usage_cmd(cmd
, "", true);
196 static void version(void)
198 printf("iw version %s\n", iw_version
);
201 static int phy_lookup(char *name
)
206 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
208 fd
= open(buf
, O_RDONLY
);
211 pos
= read(fd
, buf
, sizeof(buf
) - 1);
218 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
226 static int finish_handler(struct nl_msg
*msg
, void *arg
)
233 static int ack_handler(struct nl_msg
*msg
, void *arg
)
240 static int __handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
241 int argc
, char **argv
, struct cmd
**cmdout
)
243 struct cmd
*cmd
, *match
= NULL
;
248 const char *command
, *section
;
250 enum command_identify_by command_idby
= CIB_NONE
;
252 if (argc
<= 1 && idby
!= II_NONE
)
260 command_idby
= CIB_PHY
;
261 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
268 command_idby
= CIB_PHY
;
269 devidx
= phy_lookup(*argv
);
274 command_idby
= CIB_NETDEV
;
275 devidx
= if_nametoindex(*argv
);
288 section
= command
= *argv
;
292 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
293 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
296 if (cmd
->idby
!= command_idby
)
299 if (strcmp(cmd
->section
, section
))
301 /* this is a bit icky ... */
302 if (command
== section
) {
312 } else if (section
!= command
)
314 if (strcmp(cmd
->name
, command
))
316 if (argc
&& !cmd
->args
)
333 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
338 fprintf(stderr
, "failed to allocate netlink message\n");
342 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
344 fprintf(stderr
, "failed to allocate netlink callbacks\n");
349 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
350 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
352 switch (command_idby
) {
354 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
357 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
363 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
367 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
373 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
374 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
375 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
378 nl_recvmsgs(state
->nl_sock
, cb
);
385 fprintf(stderr
, "building message failed\n");
389 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
390 int argc
, char **argv
)
392 return __handle_cmd(state
, idby
, argc
, argv
, NULL
);
395 int main(int argc
, char **argv
)
397 struct nl80211_state nlstate
;
399 struct cmd
*cmd
= NULL
;
401 /* calculate command size including padding */
402 cmd_size
= abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
403 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0
);
408 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
414 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
419 /* need to treat "help" command specially so it works w/o nl80211 */
420 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
425 err
= nl80211_init(&nlstate
);
429 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
432 err
= __handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
, &cmd
);
433 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
434 if (strlen(*argv
) == 3) {
437 err
= __handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
, &cmd
);
438 } else if (*(*argv
+ 3) == '#')
439 err
= __handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
, &cmd
);
444 enum id_input idby
= II_NONE
;
446 if ((idx
= if_nametoindex(argv
[0])) != 0)
448 else if ((idx
= phy_lookup(argv
[0])) >= 0)
450 err
= __handle_cmd(&nlstate
, idby
, argc
, argv
, &cmd
);
459 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
461 nl80211_cleanup(&nlstate
);