]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
iw: add support for NL80211_CMD_DEL_STATION event
[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 /* libnl 1.x compatibility code */
27 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
28 static inline struct nl_handle *nl_socket_alloc(void)
29 {
30 return nl_handle_alloc();
31 }
32
33 static inline void nl_socket_free(struct nl_sock *h)
34 {
35 nl_handle_destroy(h);
36 }
37 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
38
39 int iw_debug = 0;
40
41 static int nl80211_init(struct nl80211_state *state)
42 {
43 int err;
44
45 state->nl_sock = nl_socket_alloc();
46 if (!state->nl_sock) {
47 fprintf(stderr, "Failed to allocate netlink socket.\n");
48 return -ENOMEM;
49 }
50
51 if (genl_connect(state->nl_sock)) {
52 fprintf(stderr, "Failed to connect to generic netlink.\n");
53 err = -ENOLINK;
54 goto out_handle_destroy;
55 }
56
57 state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
58 if (state->nl80211_id < 0) {
59 fprintf(stderr, "nl80211 not found.\n");
60 err = -ENOENT;
61 goto out_handle_destroy;
62 }
63
64 return 0;
65
66 out_handle_destroy:
67 nl_socket_free(state->nl_sock);
68 return err;
69 }
70
71 static void nl80211_cleanup(struct nl80211_state *state)
72 {
73 nl_socket_free(state->nl_sock);
74 }
75
76 static int cmd_size;
77
78 extern struct cmd __start___cmd;
79 extern struct cmd __stop___cmd;
80
81 #define for_each_cmd(_cmd) \
82 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
83 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
84
85
86 static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
87 {
88 const char *start, *lend, *end;
89
90 printf("%s", indent);
91
92 switch (cmd->idby) {
93 case CIB_NONE:
94 break;
95 case CIB_PHY:
96 printf("phy <phyname> ");
97 break;
98 case CIB_NETDEV:
99 printf("dev <devname> ");
100 break;
101 }
102 if (cmd->parent && cmd->parent->name)
103 printf("%s ", cmd->parent->name);
104 printf("%s", cmd->name);
105 if (cmd->args)
106 printf(" %s", cmd->args);
107 printf("\n");
108
109 if (!full || !cmd->help)
110 return;
111
112 /* hack */
113 if (strlen(indent))
114 indent = "\t\t";
115 else
116 printf("\n");
117
118 /* print line by line */
119 start = cmd->help;
120 end = strchr(start, '\0');
121 do {
122 lend = strchr(start, '\n');
123 if (!lend)
124 lend = end;
125 printf("%s", indent);
126 printf("%.*s\n", (int)(lend - start), start);
127 start = lend + 1;
128 } while (end != lend);
129
130 printf("\n");
131 }
132
133 static void usage_options(void)
134 {
135 printf("Options:\n");
136 printf("\t--debug\t\tenable netlink debugging\n");
137 }
138
139 static const char *argv0;
140
141 static void usage(bool full)
142 {
143 const struct cmd *section, *cmd;
144
145 printf("Usage:\t%s [options] command\n", argv0);
146 usage_options();
147 printf("\t--version\tshow version (%s)\n", iw_version);
148 printf("Commands:\n");
149 for_each_cmd(section) {
150 if (section->parent)
151 continue;
152
153 if (section->handler && !section->hidden)
154 __usage_cmd(section, "\t", full);
155
156 for_each_cmd(cmd) {
157 if (section != cmd->parent)
158 continue;
159 if (!cmd->handler || cmd->hidden)
160 continue;
161 __usage_cmd(cmd, "\t", full);
162 }
163 }
164 printf("\nYou can omit the 'phy' or 'dev' if "
165 "the identification is unique,\n"
166 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
167 "(Don't when scripting.)\n\n"
168 "Do NOT screenscrape this tool, we don't "
169 "consider its output stable.\n\n");
170 }
171
172 static int print_help(struct nl80211_state *state,
173 struct nl_cb *cb,
174 struct nl_msg *msg,
175 int argc, char **argv)
176 {
177 exit(3);
178 }
179 TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
180 "Print usage for each command.");
181
182 static void usage_cmd(const struct cmd *cmd)
183 {
184 printf("Usage:\t%s [options] ", argv0);
185 __usage_cmd(cmd, "", true);
186 usage_options();
187 }
188
189 static void version(void)
190 {
191 printf("iw version %s\n", iw_version);
192 }
193
194 static int phy_lookup(char *name)
195 {
196 char buf[200];
197 int fd, pos;
198
199 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
200
201 fd = open(buf, O_RDONLY);
202 if (fd < 0)
203 return -1;
204 pos = read(fd, buf, sizeof(buf) - 1);
205 if (pos < 0) {
206 close(fd);
207 return -1;
208 }
209 buf[pos] = '\0';
210 close(fd);
211 return atoi(buf);
212 }
213
214 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
215 void *arg)
216 {
217 int *ret = arg;
218 *ret = err->error;
219 return NL_STOP;
220 }
221
222 static int finish_handler(struct nl_msg *msg, void *arg)
223 {
224 int *ret = arg;
225 *ret = 0;
226 return NL_SKIP;
227 }
228
229 static int ack_handler(struct nl_msg *msg, void *arg)
230 {
231 int *ret = arg;
232 *ret = 0;
233 return NL_STOP;
234 }
235
236 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
237 int argc, char **argv, const struct cmd **cmdout)
238 {
239 const struct cmd *cmd, *match = NULL, *sectcmd;
240 struct nl_cb *cb;
241 struct nl_cb *s_cb;
242 struct nl_msg *msg;
243 int devidx = 0;
244 int err, o_argc;
245 const char *command, *section;
246 char *tmp, **o_argv;
247 enum command_identify_by command_idby = CIB_NONE;
248
249 if (argc <= 1 && idby != II_NONE)
250 return 1;
251
252 o_argc = argc;
253 o_argv = argv;
254
255 switch (idby) {
256 case II_PHY_IDX:
257 command_idby = CIB_PHY;
258 devidx = strtoul(*argv + 4, &tmp, 0);
259 if (*tmp != '\0')
260 return 1;
261 argc--;
262 argv++;
263 break;
264 case II_PHY_NAME:
265 command_idby = CIB_PHY;
266 devidx = phy_lookup(*argv);
267 argc--;
268 argv++;
269 break;
270 case II_NETDEV:
271 command_idby = CIB_NETDEV;
272 devidx = if_nametoindex(*argv);
273 if (devidx == 0)
274 devidx = -1;
275 argc--;
276 argv++;
277 break;
278 default:
279 break;
280 }
281
282 if (devidx < 0)
283 return -errno;
284
285 section = *argv;
286 argc--;
287 argv++;
288
289 for_each_cmd(sectcmd) {
290 if (sectcmd->parent)
291 continue;
292 /* ok ... bit of a hack for the dupe 'info' section */
293 if (match && sectcmd->idby != command_idby)
294 continue;
295 if (strcmp(sectcmd->name, section) == 0)
296 match = sectcmd;
297 }
298
299 sectcmd = match;
300 match = NULL;
301 if (!sectcmd)
302 return 1;
303
304 if (argc > 0) {
305 command = *argv;
306
307 for_each_cmd(cmd) {
308 if (!cmd->handler)
309 continue;
310 if (cmd->parent != sectcmd)
311 continue;
312 if (cmd->idby != command_idby)
313 continue;
314 if (strcmp(cmd->name, command))
315 continue;
316 if (argc > 1 && !cmd->args)
317 continue;
318 match = cmd;
319 break;
320 }
321
322 if (match) {
323 argc--;
324 argv++;
325 }
326 }
327
328 if (match)
329 cmd = match;
330 else {
331 /* Use the section itself, if possible. */
332 cmd = sectcmd;
333 if (argc && !cmd->args)
334 return 1;
335 if (cmd->idby != command_idby)
336 return 1;
337 if (!cmd->handler)
338 return 1;
339 }
340
341 if (cmd->selector) {
342 cmd = cmd->selector(argc, argv);
343 if (!cmd)
344 return 1;
345 }
346
347 if (cmdout)
348 *cmdout = cmd;
349
350 if (!cmd->cmd) {
351 argc = o_argc;
352 argv = o_argv;
353 return cmd->handler(state, NULL, NULL, argc, argv);
354 }
355
356 msg = nlmsg_alloc();
357 if (!msg) {
358 fprintf(stderr, "failed to allocate netlink message\n");
359 return 2;
360 }
361
362 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
363 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
364 if (!cb || !s_cb) {
365 fprintf(stderr, "failed to allocate netlink callbacks\n");
366 err = 2;
367 goto out_free_msg;
368 }
369
370 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
371 cmd->nl_msg_flags, cmd->cmd, 0);
372
373 switch (command_idby) {
374 case CIB_PHY:
375 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
376 break;
377 case CIB_NETDEV:
378 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
379 break;
380 default:
381 break;
382 }
383
384 err = cmd->handler(state, cb, msg, argc, argv);
385 if (err)
386 goto out;
387
388 nl_socket_set_cb(state->nl_sock, s_cb);
389
390 err = nl_send_auto_complete(state->nl_sock, msg);
391 if (err < 0)
392 goto out;
393
394 err = 1;
395
396 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
397 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
398 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
399
400 while (err > 0)
401 nl_recvmsgs(state->nl_sock, cb);
402 out:
403 nl_cb_put(cb);
404 out_free_msg:
405 nlmsg_free(msg);
406 return err;
407 nla_put_failure:
408 fprintf(stderr, "building message failed\n");
409 return 2;
410 }
411
412 int handle_cmd(struct nl80211_state *state, enum id_input idby,
413 int argc, char **argv)
414 {
415 return __handle_cmd(state, idby, argc, argv, NULL);
416 }
417
418 int main(int argc, char **argv)
419 {
420 struct nl80211_state nlstate;
421 int err;
422 const struct cmd *cmd = NULL;
423
424 /* calculate command size including padding */
425 cmd_size = abs((long)&__section_set - (long)&__section_get);
426 /* strip off self */
427 argc--;
428 argv0 = *argv++;
429
430 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
431 iw_debug = 1;
432 argc--;
433 argv++;
434 }
435
436 if (argc > 0 && strcmp(*argv, "--version") == 0) {
437 version();
438 return 0;
439 }
440
441 /* need to treat "help" command specially so it works w/o nl80211 */
442 if (argc == 0 || strcmp(*argv, "help") == 0) {
443 usage(argc != 0);
444 return 0;
445 }
446
447 err = nl80211_init(&nlstate);
448 if (err)
449 return 1;
450
451 if (strcmp(*argv, "dev") == 0 && argc > 1) {
452 argc--;
453 argv++;
454 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
455 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
456 if (strlen(*argv) == 3) {
457 argc--;
458 argv++;
459 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
460 } else if (*(*argv + 3) == '#')
461 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
462 else
463 goto detect;
464 } else {
465 int idx;
466 enum id_input idby = II_NONE;
467 detect:
468 if ((idx = if_nametoindex(argv[0])) != 0)
469 idby = II_NETDEV;
470 else if ((idx = phy_lookup(argv[0])) >= 0)
471 idby = II_PHY_NAME;
472 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
473 }
474
475 if (err == 1) {
476 if (cmd)
477 usage_cmd(cmd);
478 else
479 usage(false);
480 } else if (err < 0)
481 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
482
483 nl80211_cleanup(&nlstate);
484
485 return err;
486 }