]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
2102278034700828e9b230dca30de7808991d014
[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, NULL);
98 __COMMAND(NULL, NULL, "", NULL, 1, 0, 0, CIB_NONE, NULL, NULL);
99
100 static int cmd_size;
101
102 static void __usage_cmd(struct cmd *cmd, char *indent, bool full)
103 {
104 const char *start, *lend, *end;
105
106 fprintf(stderr, "%s", indent);
107
108 switch (cmd->idby) {
109 case CIB_NONE:
110 break;
111 case CIB_PHY:
112 fprintf(stderr, "phy <phyname> ");
113 break;
114 case CIB_NETDEV:
115 fprintf(stderr, "dev <devname> ");
116 break;
117 }
118 if (cmd->section)
119 fprintf(stderr, "%s ", cmd->section);
120 fprintf(stderr, "%s", cmd->name);
121 if (cmd->args)
122 fprintf(stderr, " %s", cmd->args);
123 fprintf(stderr, "\n");
124
125 if (!full || !cmd->help)
126 return;
127
128 /* hack */
129 if (strlen(indent))
130 indent = "\t\t";
131 else
132 fprintf(stderr, "\n");
133
134 /* print line by line */
135 start = cmd->help;
136 end = strchr(start, '\0');
137 do {
138 lend = strchr(start, '\n');
139 if (!lend)
140 lend = end;
141 fprintf(stderr, "%s", indent);
142 fprintf(stderr, "%.*s\n", (int)(lend - start), start);
143 start = lend + 1;
144 } while (end != lend);
145
146 fprintf(stderr, "\n");
147 }
148
149 static void usage_options(void)
150 {
151 fprintf(stderr, "Options:\n");
152 fprintf(stderr, "\t--debug\t\tenable netlink debugging\n");
153 }
154
155 static const char *argv0;
156
157 static void usage(bool full)
158 {
159 struct cmd *cmd;
160
161 fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
162 usage_options();
163 fprintf(stderr, "\t--version\tshow version\n");
164 fprintf(stderr, "Commands:\n");
165 for (cmd = &__start___cmd; cmd < &__stop___cmd;
166 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
167 if (!cmd->handler || cmd->hidden)
168 continue;
169 __usage_cmd(cmd, "\t", full);
170 }
171 }
172
173 static int print_help(struct nl80211_state *state,
174 struct nl_cb *cb,
175 struct nl_msg *msg,
176 int argc, char **argv)
177 {
178 exit(3);
179 }
180 TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
181 "Print usage for each command.");
182
183 static void usage_cmd(struct cmd *cmd)
184 {
185 fprintf(stderr, "Usage:\t%s [options] ", argv0);
186 __usage_cmd(cmd, "", true);
187 usage_options();
188 }
189
190 static void version(void)
191 {
192 printf("iw version %s\n", iw_version);
193 }
194
195 static int phy_lookup(char *name)
196 {
197 char buf[200];
198 int fd, pos;
199
200 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
201
202 fd = open(buf, O_RDONLY);
203 if (fd < 0)
204 return -1;
205 pos = read(fd, buf, sizeof(buf) - 1);
206 if (pos < 0)
207 return -1;
208 buf[pos] = '\0';
209 return atoi(buf);
210 }
211
212 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
213 void *arg)
214 {
215 int *ret = arg;
216 *ret = err->error;
217 return NL_STOP;
218 }
219
220 static int finish_handler(struct nl_msg *msg, void *arg)
221 {
222 int *ret = arg;
223 *ret = 0;
224 return NL_SKIP;
225 }
226
227 static int ack_handler(struct nl_msg *msg, void *arg)
228 {
229 int *ret = arg;
230 *ret = 0;
231 return NL_STOP;
232 }
233
234 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
235 int argc, char **argv, struct cmd **cmdout)
236 {
237 struct cmd *cmd, *match = NULL;
238 struct nl_cb *cb;
239 struct nl_msg *msg;
240 int devidx = 0;
241 int err, o_argc;
242 const char *command, *section;
243 char *tmp, **o_argv;
244 enum command_identify_by command_idby = CIB_NONE;
245
246 if (argc <= 1 && idby != II_NONE)
247 return 1;
248
249 o_argc = argc;
250 o_argv = argv;
251
252 switch (idby) {
253 case II_PHY_IDX:
254 command_idby = CIB_PHY;
255 devidx = strtoul(*argv + 4, &tmp, 0);
256 if (*tmp != '\0')
257 return 1;
258 argc--;
259 argv++;
260 break;
261 case II_PHY_NAME:
262 command_idby = CIB_PHY;
263 devidx = phy_lookup(*argv);
264 argc--;
265 argv++;
266 break;
267 case II_NETDEV:
268 command_idby = CIB_NETDEV;
269 devidx = if_nametoindex(*argv);
270 if (devidx == 0)
271 devidx = -1;
272 argc--;
273 argv++;
274 break;
275 default:
276 break;
277 }
278
279 if (devidx < 0)
280 return -errno;
281
282 section = command = *argv;
283 argc--;
284 argv++;
285
286 for (cmd = &__start___cmd; cmd < &__stop___cmd;
287 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
288 if (!cmd->handler)
289 continue;
290 if (cmd->idby != command_idby)
291 continue;
292 if (cmd->section) {
293 if (strcmp(cmd->section, section))
294 continue;
295 /* this is a bit icky ... */
296 if (command == section) {
297 if (argc <= 0) {
298 if (match)
299 break;
300 return 1;
301 }
302 command = *argv;
303 argc--;
304 argv++;
305 }
306 } else if (section != command)
307 continue;
308 if (strcmp(cmd->name, command))
309 continue;
310 if (argc && !cmd->args)
311 continue;
312
313 match = cmd;
314 }
315
316 cmd = match;
317
318 if (!cmd)
319 return 1;
320
321 if (cmdout)
322 *cmdout = cmd;
323
324 if (!cmd->cmd) {
325 argc = o_argc;
326 argv = o_argv;
327 return cmd->handler(state, NULL, NULL, argc, argv);
328 }
329
330 msg = nlmsg_alloc();
331 if (!msg) {
332 fprintf(stderr, "failed to allocate netlink message\n");
333 return 2;
334 }
335
336 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
337 if (!cb) {
338 fprintf(stderr, "failed to allocate netlink callbacks\n");
339 err = 2;
340 goto out_free_msg;
341 }
342
343 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
344 cmd->nl_msg_flags, cmd->cmd, 0);
345
346 switch (command_idby) {
347 case CIB_PHY:
348 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
349 break;
350 case CIB_NETDEV:
351 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
352 break;
353 default:
354 break;
355 }
356
357 err = cmd->handler(state, cb, msg, argc, argv);
358 if (err)
359 goto out;
360
361 err = nl_send_auto_complete(state->nl_sock, msg);
362 if (err < 0)
363 goto out;
364
365 err = 1;
366
367 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
368 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
369 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
370
371 while (err > 0)
372 nl_recvmsgs(state->nl_sock, cb);
373 out:
374 nl_cb_put(cb);
375 out_free_msg:
376 nlmsg_free(msg);
377 return err;
378 nla_put_failure:
379 fprintf(stderr, "building message failed\n");
380 return 2;
381 }
382
383 int handle_cmd(struct nl80211_state *state, enum id_input idby,
384 int argc, char **argv)
385 {
386 return __handle_cmd(state, idby, argc, argv, NULL);
387 }
388
389 int main(int argc, char **argv)
390 {
391 struct nl80211_state nlstate;
392 int err;
393 struct cmd *cmd = NULL;
394
395 /* calculate command size including padding */
396 cmd_size = abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
397 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0);
398 /* strip off self */
399 argc--;
400 argv0 = *argv++;
401
402 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
403 iw_debug = 1;
404 argc--;
405 argv++;
406 }
407
408 if (argc > 0 && strcmp(*argv, "--version") == 0) {
409 version();
410 return 0;
411 }
412
413 /* need to treat "help" command specially so it works w/o nl80211 */
414 if (argc == 0 || strcmp(*argv, "help") == 0) {
415 usage(argc != 0);
416 return 0;
417 }
418
419 err = nl80211_init(&nlstate);
420 if (err)
421 return 1;
422
423 if (strcmp(*argv, "dev") == 0 && argc > 1) {
424 argc--;
425 argv++;
426 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
427 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
428 if (strlen(*argv) == 3) {
429 argc--;
430 argv++;
431 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
432 } else if (*(*argv + 3) == '#')
433 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
434 else
435 err = 1;
436 } else
437 err = __handle_cmd(&nlstate, II_NONE, argc, argv, &cmd);
438
439 if (err == 1) {
440 if (cmd)
441 usage_cmd(cmd);
442 else
443 usage(false);
444 } else if (err < 0)
445 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
446
447 nl80211_cleanup(&nlstate);
448
449 return err;
450 }