]> git.ipfire.org Git - thirdparty/iw.git/blame - iw.c
better error reporting if dev/phy does not exist
[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>
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
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
656aa246 50static int 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
ce5af55c
JB
97__COMMAND(NULL, NULL, NULL, 0, 0, 0, CIB_NONE, NULL);
98__COMMAND(NULL, NULL, NULL, 1, 0, 0, CIB_NONE, NULL);
403b9c83
JB
99
100static int cmd_size;
101
bd396f2a
JB
102static void usage(const char *argv0)
103{
104 struct cmd *cmd;
105
59c49f09
JB
106 fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
107 fprintf(stderr, "Options:\n");
d711f013
JB
108 fprintf(stderr, "\t--debug\t\tenable netlink debugging\n");
109 fprintf(stderr, "\t--version\tshow version\n");
59c49f09 110 fprintf(stderr, "Commands:\n");
2c61ba61
JB
111 fprintf(stderr, "\thelp\n");
112 fprintf(stderr, "\tevent\n");
403b9c83
JB
113 for (cmd = &__start___cmd; cmd < &__stop___cmd;
114 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
ce5af55c 115 if (!cmd->handler || cmd->hidden)
403b9c83 116 continue;
bd396f2a
JB
117 switch (cmd->idby) {
118 case CIB_NONE:
59c49f09 119 fprintf(stderr, "\t");
d631650b 120 /* fall through */
bd396f2a 121 case CIB_PHY:
d631650b 122 if (cmd->idby == CIB_PHY)
59c49f09 123 fprintf(stderr, "\tphy <phyname> ");
bd396f2a
JB
124 /* fall through */
125 case CIB_NETDEV:
126 if (cmd->idby == CIB_NETDEV)
59c49f09 127 fprintf(stderr, "\tdev <devname> ");
bd396f2a
JB
128 if (cmd->section)
129 fprintf(stderr, "%s ", cmd->section);
130 fprintf(stderr, "%s", cmd->name);
131 if (cmd->args)
132 fprintf(stderr, " %s", cmd->args);
133 fprintf(stderr, "\n");
134 break;
135 }
136 }
137}
138
d711f013
JB
139static void version(void)
140{
2dc285b7 141 printf("iw version " IW_VERSION "\n");
d711f013
JB
142}
143
bd396f2a
JB
144static int phy_lookup(char *name)
145{
146 char buf[200];
147 int fd, pos;
148
149 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
150
151 fd = open(buf, O_RDONLY);
989e97c2
JB
152 if (fd < 0)
153 return -1;
bd396f2a
JB
154 pos = read(fd, buf, sizeof(buf) - 1);
155 if (pos < 0)
156 return -1;
157 buf[pos] = '\0';
158 return atoi(buf);
159}
160
70391ccf
JB
161static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
162 void *arg)
163{
164 int *ret = arg;
165 *ret = err->error;
166 return NL_STOP;
167}
168
561c5b7e
JB
169static int finish_handler(struct nl_msg *msg, void *arg)
170{
59c418c0
JB
171 int *ret = arg;
172 *ret = 0;
561c5b7e
JB
173 return NL_SKIP;
174}
175
176static int ack_handler(struct nl_msg *msg, void *arg)
70391ccf
JB
177{
178 int *ret = arg;
179 *ret = 0;
180 return NL_STOP;
181}
182
d631650b
JB
183static int handle_cmd(struct nl80211_state *state,
184 enum command_identify_by idby,
185 int argc, char **argv)
45c7212c 186{
bd396f2a 187 struct cmd *cmd;
70391ccf 188 struct nl_cb *cb = NULL;
bd396f2a
JB
189 struct nl_msg *msg;
190 int devidx = 0;
70391ccf 191 int err;
bd396f2a 192 const char *command, *section;
45c7212c 193
d631650b 194 if (argc <= 1 && idby != CIB_NONE)
5e75fd04 195 return 1;
45c7212c 196
bd396f2a
JB
197 switch (idby) {
198 case CIB_PHY:
199 devidx = phy_lookup(*argv);
200 argc--;
201 argv++;
202 break;
203 case CIB_NETDEV:
204 devidx = if_nametoindex(*argv);
989e97c2
JB
205 if (devidx == 0)
206 devidx = -1;
bd396f2a
JB
207 argc--;
208 argv++;
209 break;
210 default:
211 break;
212 }
213
989e97c2
JB
214 if (devidx < 0)
215 return -errno;
216
bd396f2a
JB
217 section = command = *argv;
218 argc--;
219 argv++;
220
403b9c83
JB
221 for (cmd = &__start___cmd; cmd < &__stop___cmd;
222 cmd = (struct cmd *)((char *)cmd + cmd_size)) {
223 if (!cmd->handler)
224 continue;
bd396f2a
JB
225 if (cmd->idby != idby)
226 continue;
227 if (cmd->section) {
228 if (strcmp(cmd->section, section))
229 continue;
230 /* this is a bit icky ... */
231 if (command == section) {
232 if (argc <= 0)
5e75fd04 233 return 1;
bd396f2a
JB
234 command = *argv;
235 argc--;
236 argv++;
237 }
238 } else if (section != command)
239 continue;
240 if (strcmp(cmd->name, command))
241 continue;
242 if (argc && !cmd->args)
243 continue;
244 break;
245 }
45c7212c 246
74701031 247 if (cmd >= &__stop___cmd)
5e75fd04 248 return 1;
45c7212c 249
bd396f2a
JB
250 msg = nlmsg_alloc();
251 if (!msg) {
70391ccf
JB
252 fprintf(stderr, "failed to allocate netlink message\n");
253 return 2;
254 }
255
59c49f09 256 cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
70391ccf
JB
257 if (!cb) {
258 fprintf(stderr, "failed to allocate netlink callbacks\n");
259 err = 2;
260 goto out_free_msg;
bd396f2a 261 }
45c7212c 262
bd396f2a
JB
263 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
264 cmd->nl_msg_flags, cmd->cmd, 0);
265
266 switch (idby) {
267 case CIB_PHY:
268 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
269 break;
270 case CIB_NETDEV:
271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
272 break;
273 default:
274 break;
275 }
276
70391ccf
JB
277 err = cmd->handler(cb, msg, argc, argv);
278 if (err)
279 goto out;
280
57077d64 281 err = nl_send_auto_complete(state->nl_sock, msg);
70391ccf
JB
282 if (err < 0)
283 goto out;
284
c5c4471a
JB
285 err = 1;
286
70391ccf 287 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
59c418c0 288 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
561c5b7e 289 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
70391ccf 290
c5c4471a 291 while (err > 0)
57077d64 292 nl_recvmsgs(state->nl_sock, cb);
70391ccf
JB
293 out:
294 nl_cb_put(cb);
295 out_free_msg:
296 nlmsg_free(msg);
297 return err;
bd396f2a
JB
298 nla_put_failure:
299 fprintf(stderr, "building message failed\n");
70391ccf 300 return 2;
45c7212c
JB
301}
302
2c61ba61
JB
303static int no_seq_check(struct nl_msg *msg, void *arg)
304{
305 return NL_OK;
306}
307
308static int print_event(struct nl_msg *msg, void *arg)
309{
310 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
311 struct nlattr *tb[NL80211_ATTR_MAX + 1];
99f46802 312 char ifname[100];
601c6ab2 313 __u8 reg_type;
2c61ba61
JB
314
315 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
316 genlmsg_attrlen(gnlh, 0), NULL);
317
318 switch (gnlh->cmd) {
03109dcb 319 case NL80211_CMD_NEW_WIPHY:
2c61ba61
JB
320 printf("wiphy rename: phy #%d to %s\n",
321 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
322 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
323 break;
99f46802
JB
324 case NL80211_CMD_NEW_SCAN_RESULTS:
325 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
326 printf("scan finished on %s (phy #%d)\n",
327 ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
328 break;
329 case NL80211_CMD_SCAN_ABORTED:
330 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
331 printf("scan aborted on %s (phy #%d)\n",
332 ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
333 break;
601c6ab2
LR
334 case NL80211_CMD_REG_CHANGE:
335
336 printf("regulatory domain change: ");
337
338 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
339
340 switch (reg_type) {
341 case NL80211_REGDOM_TYPE_COUNTRY:
342 printf("set to %s by %s request",
343 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
344 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
345 if (tb[NL80211_ATTR_WIPHY])
346 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
347 break;
348 case NL80211_REGDOM_TYPE_WORLD:
349 printf("set to world roaming by %s request",
350 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
351 break;
352 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
353 printf("custom world roaming rules in place on phy%d by %s request",
354 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
355 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
356 break;
357 case NL80211_REGDOM_TYPE_INTERSECTION:
358 printf("intersection used due to a request made by %s",
359 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
360 if (tb[NL80211_ATTR_WIPHY])
361 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
362 break;
363 default:
364 printf("unknown source (upgrade this utility)");
365 break;
366 }
367
368 printf("\n");
369 break;
03109dcb
JB
370 default:
371 printf("unknown event: %d\n", gnlh->cmd);
372 break;
2c61ba61
JB
373 }
374
375 return NL_SKIP;
376}
377
378static int listen_events(struct nl80211_state *state,
379 int argc, char **argv)
380{
381 int mcid, ret;
382 struct nl_cb *cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
383
384 if (!cb) {
385 fprintf(stderr, "failed to allocate netlink callbacks\n");
386 return -ENOMEM;
387 }
388
601c6ab2 389 /* Configuration multicast group */
57077d64 390 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
2c61ba61
JB
391 if (mcid < 0)
392 return mcid;
393
57077d64 394 ret = nl_socket_add_membership(state->nl_sock, mcid);
2c61ba61
JB
395 if (ret)
396 return ret;
99f46802 397
601c6ab2 398 /* Scan multicast group */
99f46802 399 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
5406d9e4
JB
400 if (mcid >= 0) {
401 ret = nl_socket_add_membership(state->nl_sock, mcid);
402 if (ret)
403 return ret;
404 }
99f46802 405
601c6ab2
LR
406 /* Regulatory multicast group */
407 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
408 if (mcid >= 0) {
409 ret = nl_socket_add_membership(state->nl_sock, mcid);
410 if (ret)
411 return ret;
412 }
413
2c61ba61
JB
414 /* no sequence checking for multicast messages */
415 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
416 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, NULL);
417
418 while (1)
57077d64 419 nl_recvmsgs(state->nl_sock, cb);
2c61ba61
JB
420
421 nl_cb_put(cb);
422
423 return 0;
424}
425
cad53b3f
JB
426int main(int argc, char **argv)
427{
428 struct nl80211_state nlstate;
bd396f2a
JB
429 int err;
430 const char *argv0;
cad53b3f 431
f408e01b 432 /* calculate command size including padding */
ce5af55c
JB
433 cmd_size = abs((long)&__cmd_NULL_1_CIB_NONE_0
434 - (long)&__cmd_NULL_0_CIB_NONE_0);
45c7212c
JB
435 /* strip off self */
436 argc--;
1cdd9016
MK
437 argv0 = *argv++;
438
59c49f09
JB
439 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
440 debug = 1;
441 argc--;
442 argv++;
443 }
444
d711f013
JB
445 if (argc > 0 && strcmp(*argv, "--version") == 0) {
446 version();
447 return 0;
448 }
449
bd396f2a 450 if (argc == 0 || strcmp(*argv, "help") == 0) {
1cdd9016 451 usage(argv0);
4a972f80 452 return 0;
1cdd9016 453 }
45c7212c 454
2bdb6bd1
JB
455 err = nl80211_init(&nlstate);
456 if (err)
457 return 1;
458
2c61ba61
JB
459 if (strcmp(*argv, "event") == 0) {
460 err = listen_events(&nlstate, argc, argv);
461 } else if (strcmp(*argv, "dev") == 0) {
14a0380d
LR
462 argc--;
463 argv++;
d631650b 464 err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv);
bd396f2a
JB
465 } else if (strcmp(*argv, "phy") == 0) {
466 argc--;
467 argv++;
d631650b 468 err = handle_cmd(&nlstate, CIB_PHY, argc, argv);
bd396f2a 469 } else
d631650b 470 err = handle_cmd(&nlstate, CIB_NONE, argc, argv);
45c7212c 471
5e75fd04 472 if (err == 1)
bd396f2a 473 usage(argv0);
5e75fd04 474 if (err < 0)
b49be3e1 475 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
cad53b3f
JB
476
477 nl80211_cleanup(&nlstate);
478
45c7212c 479 return err;
cad53b3f 480}