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