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