]>
git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <sys/types.h>
16 #include <netlink/genl/genl.h>
17 #include <netlink/genl/family.h>
18 #include <netlink/genl/ctrl.h>
19 #include <netlink/msg.h>
20 #include <netlink/attr.h>
21 #include <linux/nl80211.h>
26 static int nl80211_init(struct nl80211_state
*state
)
30 state
->nl_handle
= nl_handle_alloc();
31 if (!state
->nl_handle
) {
32 fprintf(stderr
, "Failed to allocate netlink handle.\n");
36 if (genl_connect(state
->nl_handle
)) {
37 fprintf(stderr
, "Failed to connect to generic netlink.\n");
39 goto out_handle_destroy
;
42 state
->nl_cache
= genl_ctrl_alloc_cache(state
->nl_handle
);
43 if (!state
->nl_cache
) {
44 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
46 goto out_handle_destroy
;
49 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
50 if (!state
->nl80211
) {
51 fprintf(stderr
, "nl80211 not found.\n");
59 nl_cache_free(state
->nl_cache
);
61 nl_handle_destroy(state
->nl_handle
);
65 static void nl80211_cleanup(struct nl80211_state
*state
)
67 genl_family_put(state
->nl80211
);
68 nl_cache_free(state
->nl_cache
);
69 nl_handle_destroy(state
->nl_handle
);
72 static void usage(const char *argv0
)
76 fprintf(stderr
, "Usage:\t%s help\n", argv0
);
77 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
; cmd
++) {
80 fprintf(stderr
, "\t%s ", argv0
);
83 if (cmd
->idby
== CIB_PHY
)
84 fprintf(stderr
, "\t%s phy <phyname> ", argv0
);
87 if (cmd
->idby
== CIB_NETDEV
)
88 fprintf(stderr
, "\t%s dev <devname> ", argv0
);
90 fprintf(stderr
, "%s ", cmd
->section
);
91 fprintf(stderr
, "%s", cmd
->name
);
93 fprintf(stderr
, " %s", cmd
->args
);
94 fprintf(stderr
, "\n");
100 static int phy_lookup(char *name
)
105 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
107 fd
= open(buf
, O_RDONLY
);
108 pos
= read(fd
, buf
, sizeof(buf
) - 1);
115 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
123 static int finish_handler(struct nl_msg
*msg
, void *arg
)
128 static int ack_handler(struct nl_msg
*msg
, void *arg
)
135 static int handle_cmd(struct nl80211_state
*state
,
136 enum command_identify_by idby
,
137 int argc
, char **argv
)
140 struct nl_cb
*cb
= NULL
;
144 const char *command
, *section
;
146 if (argc
<= 1 && idby
!= CIB_NONE
)
151 devidx
= phy_lookup(*argv
);
156 devidx
= if_nametoindex(*argv
);
164 section
= command
= *argv
;
168 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
; cmd
++) {
169 if (cmd
->idby
!= idby
)
172 if (strcmp(cmd
->section
, section
))
174 /* this is a bit icky ... */
175 if (command
== section
) {
182 } else if (section
!= command
)
184 if (strcmp(cmd
->name
, command
))
186 if (argc
&& !cmd
->args
)
191 if (cmd
== &__stop___cmd
)
196 fprintf(stderr
, "failed to allocate netlink message\n");
200 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
202 fprintf(stderr
, "failed to allocate netlink callbacks\n");
207 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
208 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
212 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
215 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
221 err
= cmd
->handler(cb
, msg
, argc
, argv
);
225 err
= nl_send_auto_complete(state
->nl_handle
, msg
);
229 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
230 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, NULL
);
231 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
233 nl_recvmsgs(state
->nl_handle
, cb
);
240 fprintf(stderr
, "building message failed\n");
244 int main(int argc
, char **argv
)
246 struct nl80211_state nlstate
;
250 err
= nl80211_init(&nlstate
);
258 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
263 if (strcmp(*argv
, "dev") == 0) {
266 err
= handle_cmd(&nlstate
, CIB_NETDEV
, argc
, argv
);
267 } else if (strcmp(*argv
, "phy") == 0) {
270 err
= handle_cmd(&nlstate
, CIB_PHY
, argc
, argv
);
272 err
= handle_cmd(&nlstate
, CIB_NONE
, argc
, argv
);
277 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
280 nl80211_cleanup(&nlstate
);