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
);
98 __COMMAND(NULL
, NULL
, "", NULL
, 1, 0, 0, CIB_NONE
, NULL
);
102 static void usage_cmd(struct cmd
*cmd
)
106 fprintf(stderr
, "\t");
109 fprintf(stderr
, "\tphy <phyname> ");
112 fprintf(stderr
, "\tdev <devname> ");
116 fprintf(stderr
, "%s ", cmd
->section
);
117 fprintf(stderr
, "%s", cmd
->name
);
119 fprintf(stderr
, " %s", cmd
->args
);
120 fprintf(stderr
, "\n");
123 static void usage(const char *argv0
)
127 fprintf(stderr
, "Usage:\t%s [options] command\n", argv0
);
128 fprintf(stderr
, "Options:\n");
129 fprintf(stderr
, "\t--debug\t\tenable netlink debugging\n");
130 fprintf(stderr
, "\t--version\tshow version\n");
131 fprintf(stderr
, "Commands:\n");
132 fprintf(stderr
, "\thelp\n");
133 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
134 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
135 if (!cmd
->handler
|| cmd
->hidden
)
141 static void version(void)
143 printf("iw version %s\n", iw_version
);
146 static int phy_lookup(char *name
)
151 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
153 fd
= open(buf
, O_RDONLY
);
156 pos
= read(fd
, buf
, sizeof(buf
) - 1);
163 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
171 static int finish_handler(struct nl_msg
*msg
, void *arg
)
178 static int ack_handler(struct nl_msg
*msg
, void *arg
)
185 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
186 int argc
, char **argv
)
188 struct cmd
*cmd
, *match
= NULL
;
193 const char *command
, *section
;
195 enum command_identify_by command_idby
= CIB_NONE
;
197 if (argc
<= 1 && idby
!= II_NONE
)
205 command_idby
= CIB_PHY
;
206 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
213 command_idby
= CIB_PHY
;
214 devidx
= phy_lookup(*argv
);
219 command_idby
= CIB_NETDEV
;
220 devidx
= if_nametoindex(*argv
);
233 section
= command
= *argv
;
237 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
238 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
241 if (cmd
->idby
!= command_idby
)
244 if (strcmp(cmd
->section
, section
))
246 /* this is a bit icky ... */
247 if (command
== section
) {
257 } else if (section
!= command
)
259 if (strcmp(cmd
->name
, command
))
261 if (argc
&& !cmd
->args
)
275 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
280 fprintf(stderr
, "failed to allocate netlink message\n");
284 cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
286 fprintf(stderr
, "failed to allocate netlink callbacks\n");
291 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
292 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
294 switch (command_idby
) {
296 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
299 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
305 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
309 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
315 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
316 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
317 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
320 nl_recvmsgs(state
->nl_sock
, cb
);
327 fprintf(stderr
, "building message failed\n");
331 int main(int argc
, char **argv
)
333 struct nl80211_state nlstate
;
337 /* calculate command size including padding */
338 cmd_size
= abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
339 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0
);
344 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
350 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
355 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
360 err
= nl80211_init(&nlstate
);
364 if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
367 err
= handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
);
368 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
369 if (strlen(*argv
) == 3) {
372 err
= handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
);
373 } else if (*(*argv
+ 3) == '#')
374 err
= handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
);
378 err
= handle_cmd(&nlstate
, II_NONE
, argc
, argv
);
383 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
385 nl80211_cleanup(&nlstate
);