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>
26 #ifndef CONFIG_LIBNL20
27 /* libnl 2.0 compatibility code */
29 static inline struct nl_handle
*nl_socket_alloc(void)
31 return nl_handle_alloc();
34 static inline void nl_socket_free(struct nl_sock
*h
)
39 static inline int __genl_ctrl_alloc_cache(struct nl_sock
*h
, struct nl_cache
**cache
)
41 struct nl_cache
*tmp
= genl_ctrl_alloc_cache(h
);
47 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
48 #endif /* CONFIG_LIBNL20 */
52 static int nl80211_init(struct nl80211_state
*state
)
56 state
->nl_sock
= nl_socket_alloc();
57 if (!state
->nl_sock
) {
58 fprintf(stderr
, "Failed to allocate netlink socket.\n");
62 if (genl_connect(state
->nl_sock
)) {
63 fprintf(stderr
, "Failed to connect to generic netlink.\n");
65 goto out_handle_destroy
;
68 if (genl_ctrl_alloc_cache(state
->nl_sock
, &state
->nl_cache
)) {
69 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
71 goto out_handle_destroy
;
74 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
75 if (!state
->nl80211
) {
76 fprintf(stderr
, "nl80211 not found.\n");
84 nl_cache_free(state
->nl_cache
);
86 nl_socket_free(state
->nl_sock
);
90 static void nl80211_cleanup(struct nl80211_state
*state
)
92 genl_family_put(state
->nl80211
);
93 nl_cache_free(state
->nl_cache
);
94 nl_socket_free(state
->nl_sock
);
97 __COMMAND(NULL
, NULL
, NULL
, 0, 0, 0, CIB_NONE
, NULL
);
98 __COMMAND(NULL
, NULL
, NULL
, 1, 0, 0, CIB_NONE
, NULL
);
102 static void usage(const char *argv0
)
106 fprintf(stderr
, "Usage:\t%s [options] command\n", argv0
);
107 fprintf(stderr
, "Options:\n");
108 fprintf(stderr
, "\t--debug\t\tenable netlink debugging\n");
109 fprintf(stderr
, "\t--version\tshow version\n");
110 fprintf(stderr
, "Commands:\n");
111 fprintf(stderr
, "\thelp\n");
112 fprintf(stderr
, "\tevent\n");
113 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
114 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
115 if (!cmd
->handler
|| cmd
->hidden
)
119 fprintf(stderr
, "\t");
122 fprintf(stderr
, "\tphy <phyname> ");
125 fprintf(stderr
, "\tdev <devname> ");
129 fprintf(stderr
, "%s ", cmd
->section
);
130 fprintf(stderr
, "%s", cmd
->name
);
132 fprintf(stderr
, " %s", cmd
->args
);
133 fprintf(stderr
, "\n");
137 static void version(void)
139 printf("iw version " IW_VERSION
"\n");
142 static int phy_lookup(char *name
)
147 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
149 fd
= open(buf
, O_RDONLY
);
152 pos
= read(fd
, buf
, sizeof(buf
) - 1);
159 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
167 static int finish_handler(struct nl_msg
*msg
, void *arg
)
174 static int ack_handler(struct nl_msg
*msg
, void *arg
)
181 int handle_cmd(struct nl80211_state
*state
, enum id_input idby
,
182 int argc
, char **argv
)
184 struct cmd
*cmd
, *match
= NULL
;
189 const char *command
, *section
;
191 enum command_identify_by command_idby
= CIB_NONE
;
193 if (argc
<= 1 && idby
!= II_NONE
)
201 command_idby
= CIB_PHY
;
202 devidx
= strtoul(*argv
+ 4, &tmp
, 0);
209 command_idby
= CIB_PHY
;
210 devidx
= phy_lookup(*argv
);
215 command_idby
= CIB_NETDEV
;
216 devidx
= if_nametoindex(*argv
);
229 section
= command
= *argv
;
233 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
;
234 cmd
= (struct cmd
*)((char *)cmd
+ cmd_size
)) {
237 if (cmd
->idby
!= command_idby
)
240 if (strcmp(cmd
->section
, section
))
242 /* this is a bit icky ... */
243 if (command
== section
) {
253 } else if (section
!= command
)
255 if (strcmp(cmd
->name
, command
))
257 if (argc
&& !cmd
->args
)
271 return cmd
->handler(state
, NULL
, NULL
, argc
, argv
);
276 fprintf(stderr
, "failed to allocate netlink message\n");
280 cb
= nl_cb_alloc(debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
282 fprintf(stderr
, "failed to allocate netlink callbacks\n");
287 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
288 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
290 switch (command_idby
) {
292 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
295 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
301 err
= cmd
->handler(state
, cb
, msg
, argc
, argv
);
305 err
= nl_send_auto_complete(state
->nl_sock
, msg
);
311 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
312 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
313 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
316 nl_recvmsgs(state
->nl_sock
, cb
);
323 fprintf(stderr
, "building message failed\n");
327 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
332 static int print_event(struct nl_msg
*msg
, void *arg
)
334 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
335 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
339 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
340 genlmsg_attrlen(gnlh
, 0), NULL
);
343 case NL80211_CMD_NEW_WIPHY
:
344 printf("wiphy rename: phy #%d to %s\n",
345 nla_get_u32(tb
[NL80211_ATTR_WIPHY
]),
346 nla_get_string(tb
[NL80211_ATTR_WIPHY_NAME
]));
348 case NL80211_CMD_NEW_SCAN_RESULTS
:
349 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
350 printf("scan finished on %s (phy #%d)\n",
351 ifname
, nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
353 case NL80211_CMD_SCAN_ABORTED
:
354 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
355 printf("scan aborted on %s (phy #%d)\n",
356 ifname
, nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
358 case NL80211_CMD_REG_CHANGE
:
360 printf("regulatory domain change: ");
362 reg_type
= nla_get_u8(tb
[NL80211_ATTR_REG_TYPE
]);
365 case NL80211_REGDOM_TYPE_COUNTRY
:
366 printf("set to %s by %s request",
367 nla_get_string(tb
[NL80211_ATTR_REG_ALPHA2
]),
368 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
369 if (tb
[NL80211_ATTR_WIPHY
])
370 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
372 case NL80211_REGDOM_TYPE_WORLD
:
373 printf("set to world roaming by %s request",
374 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
376 case NL80211_REGDOM_TYPE_CUSTOM_WORLD
:
377 printf("custom world roaming rules in place on phy%d by %s request",
378 nla_get_u32(tb
[NL80211_ATTR_WIPHY
]),
379 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
381 case NL80211_REGDOM_TYPE_INTERSECTION
:
382 printf("intersection used due to a request made by %s",
383 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
384 if (tb
[NL80211_ATTR_WIPHY
])
385 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
388 printf("unknown source (upgrade this utility)");
395 printf("unknown event: %d\n", gnlh
->cmd
);
408 static int wait_event(struct nl_msg
*msg
, void *arg
)
410 struct wait_event
*wait
= arg
;
411 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
414 for (i
= 0; i
< wait
->n_cmds
; i
++) {
415 if (gnlh
->cmd
== wait
->cmds
[i
]) {
416 wait
->cmd
= gnlh
->cmd
;
423 __u32
listen_events(struct nl80211_state
*state
,
424 const int n_waits
, const __u32
*waits
)
427 struct nl_cb
*cb
= nl_cb_alloc(debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
428 struct wait_event wait_ev
;
431 fprintf(stderr
, "failed to allocate netlink callbacks\n");
435 /* Configuration multicast group */
436 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "config");
440 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
444 /* Scan multicast group */
445 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "scan");
447 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
452 /* Regulatory multicast group */
453 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "regulatory");
455 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
460 /* no sequence checking for multicast messages */
461 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
463 if (n_waits
&& waits
) {
464 wait_ev
.cmds
= waits
;
465 wait_ev
.n_cmds
= n_waits
;
466 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, wait_event
, &wait_ev
);
468 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_event
, NULL
);
474 nl_recvmsgs(state
->nl_sock
, cb
);
481 int main(int argc
, char **argv
)
483 struct nl80211_state nlstate
;
487 /* calculate command size including padding */
488 cmd_size
= abs((long)&__cmd_NULL_1_CIB_NONE_0
489 - (long)&__cmd_NULL_0_CIB_NONE_0
);
494 if (argc
> 0 && strcmp(*argv
, "--debug") == 0) {
500 if (argc
> 0 && strcmp(*argv
, "--version") == 0) {
505 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
510 err
= nl80211_init(&nlstate
);
514 if (strcmp(*argv
, "event") == 0) {
518 err
= listen_events(&nlstate
, 0, NULL
);
519 } else if (strcmp(*argv
, "dev") == 0) {
522 err
= handle_cmd(&nlstate
, II_NETDEV
, argc
, argv
);
523 } else if (strncmp(*argv
, "phy", 3) == 0) {
524 if (strlen(*argv
) == 3) {
527 err
= handle_cmd(&nlstate
, II_PHY_NAME
, argc
, argv
);
528 } else if (*(*argv
+ 3) == '#')
529 err
= handle_cmd(&nlstate
, II_PHY_IDX
, argc
, argv
);
533 err
= handle_cmd(&nlstate
, II_NONE
, argc
, argv
);
538 fprintf(stderr
, "command failed: %s (%d)\n", strerror(-err
), err
);
540 nl80211_cleanup(&nlstate
);