]>
Commit | Line | Data |
---|---|---|
cad53b3f JB |
1 | /* |
2 | * nl80211 userspace tool | |
3 | * | |
2a1fced2 | 4 | * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net> |
cad53b3f JB |
5 | */ |
6 | ||
7 | #include <errno.h> | |
8 | #include <stdio.h> | |
d5ac8ad3 | 9 | #include <string.h> |
bd396f2a JB |
10 | #include <net/if.h> |
11 | #include <sys/types.h> | |
12 | #include <sys/stat.h> | |
13 | #include <fcntl.h> | |
14 | #include <unistd.h> | |
15 | ||
cad53b3f JB |
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> | |
22 | ||
23 | #include "iw.h" | |
24 | ||
25 | ||
26 | static int nl80211_init(struct nl80211_state *state) | |
27 | { | |
28 | int err; | |
29 | ||
30 | state->nl_handle = nl_handle_alloc(); | |
31 | if (!state->nl_handle) { | |
32 | fprintf(stderr, "Failed to allocate netlink handle.\n"); | |
33 | return -ENOMEM; | |
34 | } | |
35 | ||
36 | if (genl_connect(state->nl_handle)) { | |
37 | fprintf(stderr, "Failed to connect to generic netlink.\n"); | |
38 | err = -ENOLINK; | |
39 | goto out_handle_destroy; | |
40 | } | |
41 | ||
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"); | |
45 | err = -ENOMEM; | |
46 | goto out_handle_destroy; | |
47 | } | |
48 | ||
49 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); | |
50 | if (!state->nl80211) { | |
51 | fprintf(stderr, "nl80211 not found.\n"); | |
52 | err = -ENOENT; | |
53 | goto out_cache_free; | |
54 | } | |
55 | ||
56 | return 0; | |
57 | ||
58 | out_cache_free: | |
59 | nl_cache_free(state->nl_cache); | |
60 | out_handle_destroy: | |
61 | nl_handle_destroy(state->nl_handle); | |
62 | return err; | |
63 | } | |
64 | ||
65 | static void nl80211_cleanup(struct nl80211_state *state) | |
66 | { | |
67 | genl_family_put(state->nl80211); | |
68 | nl_cache_free(state->nl_cache); | |
69 | nl_handle_destroy(state->nl_handle); | |
70 | } | |
71 | ||
bd396f2a JB |
72 | static void usage(const char *argv0) |
73 | { | |
74 | struct cmd *cmd; | |
75 | ||
76 | fprintf(stderr, "Usage:\t%s help\n", argv0); | |
77 | for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) { | |
78 | switch (cmd->idby) { | |
79 | case CIB_NONE: | |
d631650b JB |
80 | fprintf(stderr, "\t%s ", argv0); |
81 | /* fall through */ | |
bd396f2a | 82 | case CIB_PHY: |
d631650b JB |
83 | if (cmd->idby == CIB_PHY) |
84 | fprintf(stderr, "\t%s phy <phyname> ", argv0); | |
bd396f2a JB |
85 | /* fall through */ |
86 | case CIB_NETDEV: | |
87 | if (cmd->idby == CIB_NETDEV) | |
88 | fprintf(stderr, "\t%s dev <devname> ", argv0); | |
89 | if (cmd->section) | |
90 | fprintf(stderr, "%s ", cmd->section); | |
91 | fprintf(stderr, "%s", cmd->name); | |
92 | if (cmd->args) | |
93 | fprintf(stderr, " %s", cmd->args); | |
94 | fprintf(stderr, "\n"); | |
95 | break; | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | static int phy_lookup(char *name) | |
101 | { | |
102 | char buf[200]; | |
103 | int fd, pos; | |
104 | ||
105 | snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name); | |
106 | ||
107 | fd = open(buf, O_RDONLY); | |
108 | pos = read(fd, buf, sizeof(buf) - 1); | |
109 | if (pos < 0) | |
110 | return -1; | |
111 | buf[pos] = '\0'; | |
112 | return atoi(buf); | |
113 | } | |
114 | ||
70391ccf JB |
115 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
116 | void *arg) | |
117 | { | |
118 | int *ret = arg; | |
119 | *ret = err->error; | |
120 | return NL_STOP; | |
121 | } | |
122 | ||
561c5b7e JB |
123 | static int finish_handler(struct nl_msg *msg, void *arg) |
124 | { | |
125 | return NL_SKIP; | |
126 | } | |
127 | ||
128 | static int ack_handler(struct nl_msg *msg, void *arg) | |
70391ccf JB |
129 | { |
130 | int *ret = arg; | |
131 | *ret = 0; | |
132 | return NL_STOP; | |
133 | } | |
134 | ||
d631650b JB |
135 | static int handle_cmd(struct nl80211_state *state, |
136 | enum command_identify_by idby, | |
137 | int argc, char **argv) | |
45c7212c | 138 | { |
bd396f2a | 139 | struct cmd *cmd; |
70391ccf | 140 | struct nl_cb *cb = NULL; |
bd396f2a JB |
141 | struct nl_msg *msg; |
142 | int devidx = 0; | |
70391ccf | 143 | int err; |
bd396f2a | 144 | const char *command, *section; |
45c7212c | 145 | |
d631650b | 146 | if (argc <= 1 && idby != CIB_NONE) |
5e75fd04 | 147 | return 1; |
45c7212c | 148 | |
bd396f2a JB |
149 | switch (idby) { |
150 | case CIB_PHY: | |
151 | devidx = phy_lookup(*argv); | |
152 | argc--; | |
153 | argv++; | |
154 | break; | |
155 | case CIB_NETDEV: | |
156 | devidx = if_nametoindex(*argv); | |
157 | argc--; | |
158 | argv++; | |
159 | break; | |
160 | default: | |
161 | break; | |
162 | } | |
163 | ||
164 | section = command = *argv; | |
165 | argc--; | |
166 | argv++; | |
167 | ||
168 | for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) { | |
169 | if (cmd->idby != idby) | |
170 | continue; | |
171 | if (cmd->section) { | |
172 | if (strcmp(cmd->section, section)) | |
173 | continue; | |
174 | /* this is a bit icky ... */ | |
175 | if (command == section) { | |
176 | if (argc <= 0) | |
5e75fd04 | 177 | return 1; |
bd396f2a JB |
178 | command = *argv; |
179 | argc--; | |
180 | argv++; | |
181 | } | |
182 | } else if (section != command) | |
183 | continue; | |
184 | if (strcmp(cmd->name, command)) | |
185 | continue; | |
186 | if (argc && !cmd->args) | |
187 | continue; | |
188 | break; | |
189 | } | |
45c7212c | 190 | |
bd396f2a | 191 | if (cmd == &__stop___cmd) |
5e75fd04 | 192 | return 1; |
45c7212c | 193 | |
bd396f2a JB |
194 | msg = nlmsg_alloc(); |
195 | if (!msg) { | |
70391ccf JB |
196 | fprintf(stderr, "failed to allocate netlink message\n"); |
197 | return 2; | |
198 | } | |
199 | ||
561c5b7e | 200 | cb = nl_cb_alloc(NL_CB_DEFAULT); |
70391ccf JB |
201 | if (!cb) { |
202 | fprintf(stderr, "failed to allocate netlink callbacks\n"); | |
203 | err = 2; | |
204 | goto out_free_msg; | |
bd396f2a | 205 | } |
45c7212c | 206 | |
bd396f2a JB |
207 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
208 | cmd->nl_msg_flags, cmd->cmd, 0); | |
209 | ||
210 | switch (idby) { | |
211 | case CIB_PHY: | |
212 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx); | |
213 | break; | |
214 | case CIB_NETDEV: | |
215 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); | |
216 | break; | |
217 | default: | |
218 | break; | |
219 | } | |
220 | ||
70391ccf JB |
221 | err = cmd->handler(cb, msg, argc, argv); |
222 | if (err) | |
223 | goto out; | |
224 | ||
225 | err = nl_send_auto_complete(state->nl_handle, msg); | |
226 | if (err < 0) | |
227 | goto out; | |
228 | ||
229 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); | |
561c5b7e JB |
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); | |
70391ccf | 232 | |
70391ccf | 233 | nl_recvmsgs(state->nl_handle, cb); |
70391ccf JB |
234 | out: |
235 | nl_cb_put(cb); | |
236 | out_free_msg: | |
237 | nlmsg_free(msg); | |
238 | return err; | |
bd396f2a JB |
239 | nla_put_failure: |
240 | fprintf(stderr, "building message failed\n"); | |
70391ccf | 241 | return 2; |
45c7212c JB |
242 | } |
243 | ||
cad53b3f JB |
244 | int main(int argc, char **argv) |
245 | { | |
246 | struct nl80211_state nlstate; | |
bd396f2a JB |
247 | int err; |
248 | const char *argv0; | |
cad53b3f JB |
249 | |
250 | err = nl80211_init(&nlstate); | |
251 | if (err) | |
252 | return 1; | |
253 | ||
45c7212c JB |
254 | /* strip off self */ |
255 | argc--; | |
1cdd9016 MK |
256 | argv0 = *argv++; |
257 | ||
bd396f2a | 258 | if (argc == 0 || strcmp(*argv, "help") == 0) { |
1cdd9016 MK |
259 | usage(argv0); |
260 | goto out; | |
261 | } | |
45c7212c | 262 | |
bd396f2a | 263 | if (strcmp(*argv, "dev") == 0) { |
14a0380d LR |
264 | argc--; |
265 | argv++; | |
d631650b | 266 | err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv); |
bd396f2a JB |
267 | } else if (strcmp(*argv, "phy") == 0) { |
268 | argc--; | |
269 | argv++; | |
d631650b | 270 | err = handle_cmd(&nlstate, CIB_PHY, argc, argv); |
bd396f2a | 271 | } else |
d631650b | 272 | err = handle_cmd(&nlstate, CIB_NONE, argc, argv); |
45c7212c | 273 | |
5e75fd04 | 274 | if (err == 1) |
bd396f2a | 275 | usage(argv0); |
5e75fd04 | 276 | if (err < 0) |
b49be3e1 | 277 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); |
cad53b3f | 278 | |
45c7212c | 279 | out: |
cad53b3f JB |
280 | nl80211_cleanup(&nlstate); |
281 | ||
45c7212c | 282 | return err; |
cad53b3f | 283 | } |