]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
split out event handling code
[thirdparty/iw.git] / iw.c
1 /*
2 * nl80211 userspace tool
3 *
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <net/if.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <stdbool.h>
16
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>
22
23 #include "nl80211.h"
24 #include "iw.h"
25
26 #ifndef CONFIG_LIBNL20
27 /* libnl 2.0 compatibility code */
28
29 static inline struct nl_handle *nl_socket_alloc(void)
30 {
31 return nl_handle_alloc();
32 }
33
34 static inline void nl_socket_free(struct nl_sock *h)
35 {
36 nl_handle_destroy(h);
37 }
38
39 static inline int __genl_ctrl_alloc_cache(struct nl_sock *h, struct nl_cache **cache)
40 {
41 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
42 if (!tmp)
43 return -ENOMEM;
44 *cache = tmp;
45 return 0;
46 }
47 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
48 #endif /* CONFIG_LIBNL20 */
49
50 int iw_debug = 0;
51
52 static int nl80211_init(struct nl80211_state *state)
53 {
54 int err;
55
56 state->nl_sock = nl_socket_alloc();
57 if (!state->nl_sock) {
58 fprintf(stderr, "Failed to allocate netlink socket.\n");
59 return -ENOMEM;
60 }
61
62 if (genl_connect(state->nl_sock)) {
63 fprintf(stderr, "Failed to connect to generic netlink.\n");
64 err = -ENOLINK;
65 goto out_handle_destroy;
66 }
67
68 if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
69 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
70 err = -ENOMEM;
71 goto out_handle_destroy;
72 }
73
74 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
75 if (!state->nl80211) {
76 fprintf(stderr, "nl80211 not found.\n");
77 err = -ENOENT;
78 goto out_cache_free;
79 }
80
81 return 0;
82
83 out_cache_free:
84 nl_cache_free(state->nl_cache);
85 out_handle_destroy:
86 nl_socket_free(state->nl_sock);
87 return err;
88 }
89
90 static void nl80211_cleanup(struct nl80211_state *state)
91 {
92 genl_family_put(state->nl80211);
93 nl_cache_free(state->nl_cache);
94 nl_socket_free(state->nl_sock);
95 }
96
97 __COMMAND(NULL, NULL, "", NULL, 0, 0, 0, CIB_NONE, NULL);
98 __COMMAND(NULL, NULL, "", NULL, 1, 0, 0, CIB_NONE, NULL);
99
100 static int cmd_size;
101
102 static void usage_cmd(struct cmd *cmd)
103 {
104 switch (cmd->idby) {
105 case CIB_NONE:
106 fprintf(stderr, "\t");
107 break;
108 case CIB_PHY:
109 fprintf(stderr, "\tphy <phyname> ");
110 break;
111 case CIB_NETDEV:
112 fprintf(stderr, "\tdev <devname> ");
113 break;
114 }
115 if (cmd->section)
116 fprintf(stderr, "%s ", cmd->section);
117 fprintf(stderr, "%s", cmd->name);
118 if (cmd->args)
119 fprintf(stderr, " %s", cmd->args);
120 fprintf(stderr, "\n");
121 }
122
123 static void usage(const char *argv0)
124 {
125 struct cmd *cmd;
126
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)
136 continue;
137 usage_cmd(cmd);
138 }
139 }
140
141 static void version(void)
142 {
143 printf("iw version %s\n", iw_version);
144 }
145
146 static int phy_lookup(char *name)
147 {
148 char buf[200];
149 int fd, pos;
150
151 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
152
153 fd = open(buf, O_RDONLY);
154 if (fd < 0)
155 return -1;
156 pos = read(fd, buf, sizeof(buf) - 1);
157 if (pos < 0)
158 return -1;
159 buf[pos] = '\0';
160 return atoi(buf);
161 }
162
163 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
164 void *arg)
165 {
166 int *ret = arg;
167 *ret = err->error;
168 return NL_STOP;
169 }
170
171 static int finish_handler(struct nl_msg *msg, void *arg)
172 {
173 int *ret = arg;
174 *ret = 0;
175 return NL_SKIP;
176 }
177
178 static int ack_handler(struct nl_msg *msg, void *arg)
179 {
180 int *ret = arg;
181 *ret = 0;
182 return NL_STOP;
183 }
184
185 int handle_cmd(struct nl80211_state *state, enum id_input idby,
186 int argc, char **argv)
187 {
188 struct cmd *cmd, *match = NULL;
189 struct nl_cb *cb;
190 struct nl_msg *msg;
191 int devidx = 0;
192 int err, o_argc;
193 const char *command, *section;
194 char *tmp, **o_argv;
195 enum command_identify_by command_idby = CIB_NONE;
196
197 if (argc <= 1 && idby != II_NONE)
198 return 1;
199
200 o_argc = argc;
201 o_argv = argv;
202
203 switch (idby) {
204 case II_PHY_IDX:
205 command_idby = CIB_PHY;
206 devidx = strtoul(*argv + 4, &tmp, 0);
207 if (*tmp != '\0')
208 return 1;
209 argc--;
210 argv++;
211 break;
212 case II_PHY_NAME:
213 command_idby = CIB_PHY;
214 devidx = phy_lookup(*argv);
215 argc--;
216 argv++;
217 break;
218 case II_NETDEV:
219 command_idby = CIB_NETDEV;
220 devidx = if_nametoindex(*argv);
221 if (devidx == 0)
222 devidx = -1;
223 argc--;
224 argv++;
225 break;
226 default:
227 break;
228 }
229
230 if (devidx < 0)
231 return -errno;
232
233 section = command = *argv;
234 argc--;
235 argv++;
236
237 for (cmd = &__start___cmd; cmd < &__stop___cmd;
238 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
239 if (!cmd->handler)
240 continue;
241 if (cmd->idby != command_idby)
242 continue;
243 if (cmd->section) {
244 if (strcmp(cmd->section, section))
245 continue;
246 /* this is a bit icky ... */
247 if (command == section) {
248 if (argc <= 0) {
249 if (match)
250 break;
251 return 1;
252 }
253 command = *argv;
254 argc--;
255 argv++;
256 }
257 } else if (section != command)
258 continue;
259 if (strcmp(cmd->name, command))
260 continue;
261 if (argc && !cmd->args)
262 continue;
263
264 match = cmd;
265 }
266
267 cmd = match;
268
269 if (!cmd)
270 return 1;
271
272 if (!cmd->cmd) {
273 argc = o_argc;
274 argv = o_argv;
275 return cmd->handler(state, NULL, NULL, argc, argv);
276 }
277
278 msg = nlmsg_alloc();
279 if (!msg) {
280 fprintf(stderr, "failed to allocate netlink message\n");
281 return 2;
282 }
283
284 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
285 if (!cb) {
286 fprintf(stderr, "failed to allocate netlink callbacks\n");
287 err = 2;
288 goto out_free_msg;
289 }
290
291 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
292 cmd->nl_msg_flags, cmd->cmd, 0);
293
294 switch (command_idby) {
295 case CIB_PHY:
296 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
297 break;
298 case CIB_NETDEV:
299 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
300 break;
301 default:
302 break;
303 }
304
305 err = cmd->handler(state, cb, msg, argc, argv);
306 if (err)
307 goto out;
308
309 err = nl_send_auto_complete(state->nl_sock, msg);
310 if (err < 0)
311 goto out;
312
313 err = 1;
314
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);
318
319 while (err > 0)
320 nl_recvmsgs(state->nl_sock, cb);
321 out:
322 nl_cb_put(cb);
323 out_free_msg:
324 nlmsg_free(msg);
325 return err;
326 nla_put_failure:
327 fprintf(stderr, "building message failed\n");
328 return 2;
329 }
330
331 int main(int argc, char **argv)
332 {
333 struct nl80211_state nlstate;
334 int err;
335 const char *argv0;
336
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);
340 /* strip off self */
341 argc--;
342 argv0 = *argv++;
343
344 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
345 iw_debug = 1;
346 argc--;
347 argv++;
348 }
349
350 if (argc > 0 && strcmp(*argv, "--version") == 0) {
351 version();
352 return 0;
353 }
354
355 if (argc == 0 || strcmp(*argv, "help") == 0) {
356 usage(argv0);
357 return 0;
358 }
359
360 err = nl80211_init(&nlstate);
361 if (err)
362 return 1;
363
364 if (strcmp(*argv, "dev") == 0 && argc > 1) {
365 argc--;
366 argv++;
367 err = handle_cmd(&nlstate, II_NETDEV, argc, argv);
368 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
369 if (strlen(*argv) == 3) {
370 argc--;
371 argv++;
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);
375 else
376 err = 1;
377 } else
378 err = handle_cmd(&nlstate, II_NONE, argc, argv);
379
380 if (err == 1)
381 usage(argv0);
382 if (err < 0)
383 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
384
385 nl80211_cleanup(&nlstate);
386
387 return err;
388 }