2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <sys/types.h>
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>
25 #ifndef CONFIG_LIBNL20
26 /* libnl 2.0 compatibility code */
28 static inline struct nl_handle
*nl_socket_alloc(void)
30 return nl_handle_alloc();
33 static inline void nl_socket_free(struct nl_sock
*h
)
38 static inline int __genl_ctrl_alloc_cache(struct nl_sock
*h
, struct nl_cache
**cache
)
40 struct nl_cache
*tmp
= genl_ctrl_alloc_cache(h
);
46 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
47 #endif /* CONFIG_LIBNL20 */
51 static int nl80211_init(struct nl80211_state
*state
)
55 state
->nl_sock
= nl_socket_alloc();
56 if (!state
->nl_sock
) {
57 fprintf(stderr
, "Failed to allocate netlink socket.\n");
61 if (genl_connect(state
->nl_sock
)) {
62 fprintf(stderr
, "Failed to connect to generic netlink.\n");
64 goto out_handle_destroy
;
67 if (genl_ctrl_alloc_cache(state
->nl_sock
, &state
->nl_cache
)) {
68 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
70 goto out_handle_destroy
;
73 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
74 if (!state
->nl80211
) {
75 fprintf(stderr
, "nl80211 not found.\n");
83 nl_cache_free(state
->nl_cache
);
85 nl_socket_free(state
->nl_sock
);
89 static void nl80211_cleanup(struct nl80211_state
*state
)
91 genl_family_put(state
->nl80211
);
92 nl_cache_free(state
->nl_cache
);
93 nl_socket_free(state
->nl_sock
);
96 __COMMAND(NULL
, NULL
, "", NULL
, 0, 0, 0, CIB_NONE
, NULL
);
97 __COMMAND(NULL
, NULL
, "", NULL
, 1, 0, 0, CIB_NONE
, NULL
);
101 static void usage(const char *argv0
)
105 fprintf(stderr
, "Usage:\t%s [options] command\n", argv0
);
106 fprintf(stderr
, "Options:\n");
107 fprintf(stderr
, "\t--debug\t\tenable netlink debugging\n");
108 fprintf(stderr
, "\t--version\tshow version\n");
109 fprintf(stderr
, "Commands:\n");
110 fprintf(stderr
, "\thelp\n");
111 fprintf(stderr
, "\tevent\n");
112 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
113 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
114 if (!cmd
->handler
|| cmd
->hidden
)
118 fprintf(stderr
, "\t");
121 fprintf(stderr
, "\tphy <phyname> ");
124 fprintf(stderr
, "\tdev <devname> ");
128 fprintf(stderr
, "%s ", cmd
->section
);
129 fprintf(stderr
, "%s", cmd
->name
);
131 fprintf(stderr
, " %s", cmd
->args
);
132 fprintf(stderr
, "\n");
136 static void version(void)
138 printf("iw version %s\n", iw_version
);
141 static int phy_lookup(char *name
)
146 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
148 fd
= open(buf
, O_RDONLY
);
151 pos
= read(fd
, buf
, sizeof(buf
) - 1);
158 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
166 static int finish_handler(struct nl_msg
*msg
, void *arg
)
173 static int ack_handler(struct nl_msg
*msg
, void *arg
)
180 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
181 int argc
, char **argv
)
183 struct cmd
*cmd
, *match
= NULL
;
188 const char *command
, *section
;
190 enum command_identify_by command_idby
= CIB_NONE
;
192 if (argc
<= 1 && idby
!= II_NONE
)
200 command_idby
= CIB_PHY
;
201 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
208 command_idby
= CIB_PHY
;
209 devidx
= phy_lookup(*argv
);
214 command_idby
= CIB_NETDEV
;
215 devidx
= if_nametoindex(*argv
);
228 section
= command
= *argv
;
232 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
233 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
236 if (cmd
->idby
!= command_idby
)
239 if (strcmp(cmd
->section
, section
))
241 /* this is a bit icky ... */
242 if (command
== section
) {
252 } else if (section
!= command
)
254 if (strcmp(cmd
->name
, command
))
256 if (argc
&& !cmd
->args
)
270 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
275 fprintf(stderr
, "failed to allocate netlink message\n");
279 cb
= nl_cb_alloc(debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
281 fprintf(stderr
, "failed to allocate netlink callbacks\n");
286 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
287 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
289 switch (command_idby
) {
291 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
294 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
300 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
304 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
310 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
311 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
312 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
315 nl_recvmsgs(state
->nl_sock
, cb
);
322 fprintf(stderr
, "building message failed\n");
326 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
331 static int print_event(struct nl_msg
*msg
, void *arg
)
333 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
334 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
339 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
340 genlmsg_attrlen(gnlh
, 0), NULL
);
342 if (tb
[NL80211_ATTR_IFINDEX
] && tb
[NL80211_ATTR_WIPHY
]) {
343 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
344 printf("%s (phy #%d): ", ifname
, nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
345 } else if (tb
[NL80211_ATTR_IFINDEX
]) {
346 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
347 printf("%s: ", ifname
);
348 } else if (tb
[NL80211_ATTR_WIPHY
]) {
349 printf("phy #%d: ", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
353 case NL80211_CMD_NEW_WIPHY
:
354 printf("renamed to %s\n", nla_get_string(tb
[NL80211_ATTR_WIPHY_NAME
]));
356 case NL80211_CMD_NEW_SCAN_RESULTS
:
357 printf("scan finished\n");
359 case NL80211_CMD_SCAN_ABORTED
:
360 printf("scan aborted\n");
362 case NL80211_CMD_REG_CHANGE
:
363 printf("regulatory domain change: ");
365 reg_type
= nla_get_u8(tb
[NL80211_ATTR_REG_TYPE
]);
368 case NL80211_REGDOM_TYPE_COUNTRY
:
369 printf("set to %s by %s request",
370 nla_get_string(tb
[NL80211_ATTR_REG_ALPHA2
]),
371 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
372 if (tb
[NL80211_ATTR_WIPHY
])
373 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
375 case NL80211_REGDOM_TYPE_WORLD
:
376 printf("set to world roaming by %s request",
377 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
379 case NL80211_REGDOM_TYPE_CUSTOM_WORLD
:
380 printf("custom world roaming rules in place on phy%d by %s request",
381 nla_get_u32(tb
[NL80211_ATTR_WIPHY
]),
382 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
384 case NL80211_REGDOM_TYPE_INTERSECTION
:
385 printf("intersection used due to a request made by %s",
386 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
387 if (tb
[NL80211_ATTR_WIPHY
])
388 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
391 printf("unknown source (upgrade this utility)");
397 case NL80211_CMD_JOIN_IBSS
:
398 mac_addr_n2a(macbuf
, nla_data(tb
[NL80211_ATTR_MAC
]));
399 printf("IBSS %s joined\n", macbuf
);
401 case NL80211_CMD_AUTHENTICATE
:
404 case NL80211_CMD_ASSOCIATE
:
407 case NL80211_CMD_DEAUTHENTICATE
:
410 case NL80211_CMD_DISASSOCIATE
:
411 printf("disassoc\n");
414 printf("unknown event %d\n", gnlh
->cmd
);
427 static int wait_event(struct nl_msg
*msg
, void *arg
)
429 struct wait_event
*wait
= arg
;
430 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
433 for (i
= 0; i
< wait
->n_cmds
; i
++) {
434 if (gnlh
->cmd
== wait
->cmds
[i
]) {
435 wait
->cmd
= gnlh
->cmd
;
442 __u32
listen_events(struct nl80211_state
*state
,
443 const int n_waits
, const __u32
*waits
)
446 struct nl_cb
*cb
= nl_cb_alloc(debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
447 struct wait_event wait_ev
;
450 fprintf(stderr
, "failed to allocate netlink callbacks\n");
454 /* Configuration multicast group */
455 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "config");
459 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
463 /* Scan multicast group */
464 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "scan");
466 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
471 /* Regulatory multicast group */
472 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "regulatory");
474 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
479 /* MLME multicast group */
480 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "mlme");
482 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
487 /* no sequence checking for multicast messages */
488 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
490 if (n_waits
&& waits
) {
491 wait_ev
.cmds
= waits
;
492 wait_ev
.n_cmds
= n_waits
;
493 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, wait_event
, &wait_ev
);
495 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_event
, NULL
);
501 nl_recvmsgs(state
->nl_sock
, cb
);
508 int main(int argc
, char **argv
)
510 struct nl80211_state nlstate
;
514 /* calculate command size including padding */
515 cmd_size
= abs((long)&__cmd_NULL_NULL_1_CIB_NONE_0
516 - (long)&__cmd_NULL_NULL_0_CIB_NONE_0
);
521 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
527 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
532 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
537 err
= nl80211_init(&nlstate
);
541 if (strcmp(*argv
, "event") == 0) {
545 err
= listen_events(&nlstate
, 0, NULL
);
546 } else if (strcmp(*argv
, "dev") == 0 && argc
> 1) {
549 err
= handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
);
550 } else if (strncmp(*argv
, "phy", 3) == 0 && argc
> 1) {
551 if (strlen(*argv
) == 3) {
554 err
= handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
);
555 } else if (*(*argv
+ 3) == '#')
556 err
= handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
);
560 err
= handle_cmd(&nlstate
, II_NONE
, argc
, argv
);
565 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
567 nl80211_cleanup(&nlstate
);