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