]>
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 wait_handler(struct nl_msg
*msg
, void *arg
)
130 static int handle_cmd(struct nl80211_state
*state
,
131 enum command_identify_by idby
,
132 int argc
, char **argv
)
135 struct nl_cb
*cb
= NULL
;
139 const char *command
, *section
;
141 if (argc
<= 1 && idby
!= CIB_NONE
)
146 devidx
= phy_lookup(*argv
);
151 devidx
= if_nametoindex(*argv
);
159 section
= command
= *argv
;
163 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
; cmd
++) {
164 if (cmd
->idby
!= idby
)
167 if (strcmp(cmd
->section
, section
))
169 /* this is a bit icky ... */
170 if (command
== section
) {
177 } else if (section
!= command
)
179 if (strcmp(cmd
->name
, command
))
181 if (argc
&& !cmd
->args
)
186 if (cmd
== &__stop___cmd
)
191 fprintf(stderr
, "failed to allocate netlink message\n");
195 cb
= nl_cb_alloc(NL_CB_CUSTOM
);
197 fprintf(stderr
, "failed to allocate netlink callbacks\n");
202 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
203 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
207 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
210 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
216 err
= cmd
->handler(cb
, msg
, argc
, argv
);
220 err
= nl_send_auto_complete(state
->nl_handle
, msg
);
224 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
225 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, wait_handler
, &err
);
228 nl_recvmsgs(state
->nl_handle
, cb
);
231 err
= nl_wait_for_ack(state
->nl_handle
);
238 fprintf(stderr
, "building message failed\n");
242 int main(int argc
, char **argv
)
244 struct nl80211_state nlstate
;
248 err
= nl80211_init(&nlstate
);
256 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
261 if (strcmp(*argv
, "dev") == 0) {
264 err
= handle_cmd(&nlstate
, CIB_NETDEV
, argc
, argv
);
265 } else if (strcmp(*argv
, "phy") == 0) {
268 err
= handle_cmd(&nlstate
, CIB_PHY
, argc
, argv
);
270 err
= handle_cmd(&nlstate
, CIB_NONE
, argc
, argv
);
275 fprintf(stderr
, "command failed: %s\n", strerror(-err
));
278 nl80211_cleanup(&nlstate
);