4 #include <netlink/genl/genl.h>
5 #include <netlink/genl/family.h>
6 #include <netlink/genl/ctrl.h>
7 #include <netlink/msg.h>
8 #include <netlink/attr.h>
15 static int seq_handler(struct nl_msg
*msg
, void *arg
)
20 static int dump_mgmt_frame(struct nl_msg
*msg
, void *arg
)
22 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
23 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
25 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
26 genlmsg_attrlen(gnlh
, 0), NULL
);
28 if (tb_msg
[NL80211_ATTR_WIPHY_FREQ
]) {
29 uint32_t freq
= nla_get_u32(tb_msg
[NL80211_ATTR_WIPHY_FREQ
]);
30 printf("freq %u MHz\n", freq
);
33 if (tb_msg
[NL80211_ATTR_RX_SIGNAL_DBM
]) {
34 /* nl80211_send_mgmt sends signed dBm value as u32 */
35 int dbm
= nla_get_u32(tb_msg
[NL80211_ATTR_RX_SIGNAL_DBM
]);
36 printf("rssi %d dBm\n", dbm
);
39 if (tb_msg
[NL80211_ATTR_FRAME
]) {
40 int len
= nla_len(tb_msg
[NL80211_ATTR_FRAME
]);
41 uint8_t *data
= nla_data(tb_msg
[NL80211_ATTR_FRAME
]);
42 iw_hexdump("mgmt", data
, len
);
48 static int register_mgmt_frame(struct nl80211_state
*state
,
49 struct nl_msg
*msg
, int argc
, char **argv
,
57 ret
= sscanf(argv
[0], "%x", &type
);
59 printf("invalid frame type: %s\n", argv
[0]);
63 match
= parse_hex(argv
[1], &match_len
);
65 printf("invalid frame pattern: %s\n", argv
[1]);
69 NLA_PUT_U16(msg
, NL80211_ATTR_FRAME_TYPE
, type
);
70 NLA_PUT(msg
, NL80211_ATTR_FRAME_MATCH
, match_len
, match
);
80 static int handle_mgmt_reg(struct nl80211_state
*state
,
81 struct nl_msg
*msg
, int argc
,
82 char **argv
, enum id_input id
)
84 return register_mgmt_frame(state
, msg
, argc
, argv
, id
);
87 HIDDEN(mgmt
, reg
, "", NL80211_CMD_REGISTER_FRAME
, 0, CIB_NETDEV
, handle_mgmt_reg
);
89 static int handle_mgmt_dump(struct nl80211_state
*state
,
90 struct nl_msg
*msg
, int argc
,
91 char **argv
, enum id_input id
)
93 struct nl_cb
*mgmt_cb
;
97 unsigned int count
= 0;
101 mgmt_argv
= calloc(mgmt_argc
, sizeof(char*));
106 mgmt_argv
[1] = "mgmt";
107 mgmt_argv
[2] = "reg";
109 for (i
= 3; i
< argc
; i
+= 3) {
110 if (strcmp(argv
[i
], "count") == 0) {
111 count
= 1 + atoi(argv
[i
+ 1]);
117 if (strcmp(argv
[i
], "frame") != 0) {
122 mgmt_argv
[3] = argv
[i
+ 1];
123 mgmt_argv
[4] = argv
[i
+ 2];
125 err
= handle_cmd(state
, II_NETDEV
, mgmt_argc
, mgmt_argv
);
130 mgmt_cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
136 /* need to turn off sequence number checking */
137 nl_cb_set(mgmt_cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, seq_handler
, NULL
);
138 nl_cb_set(mgmt_cb
, NL_CB_VALID
, NL_CB_CUSTOM
, dump_mgmt_frame
, NULL
);
141 nl_recvmsgs(state
->nl_sock
, mgmt_cb
);
149 COMMAND(mgmt
, dump
, "frame <type as hex ab> <pattern as hex ab:cd:..> [frame <type> <pattern>]* [count <frames>]",
150 0, 0, CIB_NETDEV
, handle_mgmt_dump
,
151 "Register for receiving certain mgmt frames and print them.\n"
152 "Frames are selected by their type and pattern containing\n"
153 "the first several bytes of the frame that should match.\n\n"
154 "Example: iw dev wlan0 mgmt dump frame 40 00 frame 40 01:02 count 10\n");