]>
Commit | Line | Data |
---|---|---|
2ef1be68 | 1 | #include <net/if.h> |
3d1e8704 | 2 | #include <errno.h> |
d5ac8ad3 | 3 | #include <string.h> |
2ef1be68 | 4 | |
3d1e8704 LCC |
5 | #include <netlink/genl/genl.h> |
6 | #include <netlink/genl/family.h> | |
7 | #include <netlink/genl/ctrl.h> | |
8 | #include <netlink/msg.h> | |
9 | #include <netlink/attr.h> | |
3d1e8704 | 10 | |
f408e01b | 11 | #include "nl80211.h" |
3d1e8704 LCC |
12 | #include "iw.h" |
13 | ||
14 | enum plink_state { | |
15 | LISTEN, | |
16 | OPN_SNT, | |
17 | OPN_RCVD, | |
18 | CNF_RCVD, | |
19 | ESTAB, | |
20 | HOLDING, | |
21 | BLOCKED | |
22 | }; | |
23 | ||
24 | enum plink_actions { | |
25 | PLINK_ACTION_UNDEFINED, | |
26 | PLINK_ACTION_OPEN, | |
27 | PLINK_ACTION_BLOCK, | |
28 | }; | |
29 | ||
30 | ||
3d1e8704 LCC |
31 | static int print_sta_handler(struct nl_msg *msg, void *arg) |
32 | { | |
33 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
34 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
35 | struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; | |
9cd3f1c1 | 36 | struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; |
3d1e8704 LCC |
37 | char mac_addr[20], state_name[10], dev[20]; |
38 | static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { | |
39 | [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, | |
40 | [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, | |
41 | [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, | |
859677cb JB |
42 | [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, |
43 | [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, | |
9cd3f1c1 HR |
44 | [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, |
45 | [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, | |
3d1e8704 LCC |
46 | [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, |
47 | [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, | |
48 | [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, | |
49 | }; | |
50 | ||
9cd3f1c1 HR |
51 | static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { |
52 | [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, | |
53 | [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, | |
54 | [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, | |
55 | [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, | |
56 | }; | |
57 | ||
3d1e8704 LCC |
58 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
59 | genlmsg_attrlen(gnlh, 0), NULL); | |
60 | ||
61 | /* | |
62 | * TODO: validate the interface and mac address! | |
63 | * Otherwise, there's a race condition as soon as | |
64 | * the kernel starts sending station notifications. | |
65 | */ | |
66 | ||
67 | if (!tb[NL80211_ATTR_STA_INFO]) { | |
68 | fprintf(stderr, "sta stats missing!"); | |
69 | return NL_SKIP; | |
70 | } | |
71 | if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, | |
72 | tb[NL80211_ATTR_STA_INFO], | |
73 | stats_policy)) { | |
74 | fprintf(stderr, "failed to parse nested attributes!"); | |
75 | return NL_SKIP; | |
76 | } | |
77 | ||
78 | mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC])); | |
79 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
fbb181fa | 80 | printf("Station %s (on %s)", mac_addr, dev); |
3d1e8704 LCC |
81 | |
82 | if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) | |
fbb181fa | 83 | printf("\n\tinactive time:\t%d ms", |
3d1e8704 LCC |
84 | nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME])); |
85 | if (sinfo[NL80211_STA_INFO_RX_BYTES]) | |
fbb181fa | 86 | printf("\n\trx bytes:\t%d", |
3d1e8704 | 87 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES])); |
859677cb JB |
88 | if (sinfo[NL80211_STA_INFO_RX_PACKETS]) |
89 | printf("\n\trx packets:\t%d", | |
90 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS])); | |
3d1e8704 | 91 | if (sinfo[NL80211_STA_INFO_TX_BYTES]) |
fbb181fa | 92 | printf("\n\ttx bytes:\t%d", |
3d1e8704 | 93 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES])); |
859677cb JB |
94 | if (sinfo[NL80211_STA_INFO_TX_PACKETS]) |
95 | printf("\n\ttx packets:\t%d", | |
96 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS])); | |
9cd3f1c1 HR |
97 | if (sinfo[NL80211_STA_INFO_SIGNAL]) |
98 | printf("\n\tsignal: \t%d dBm", | |
99 | (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL])); | |
100 | ||
101 | if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { | |
102 | if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, | |
103 | sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) { | |
104 | fprintf(stderr, "failed to parse nested rate attributes!"); | |
105 | } else { | |
106 | printf("\n\ttx bitrate:\t"); | |
107 | if (rinfo[NL80211_RATE_INFO_BITRATE]) { | |
108 | int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); | |
109 | printf("%d.%d MBit/s", rate / 10, rate % 10); | |
110 | } | |
111 | ||
112 | if (rinfo[NL80211_RATE_INFO_MCS]) | |
113 | printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS])); | |
114 | if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH]) | |
115 | printf(" 40Mhz"); | |
116 | if (rinfo[NL80211_RATE_INFO_SHORT_GI]) | |
117 | printf(" short GI"); | |
118 | } | |
119 | } | |
120 | ||
3d1e8704 | 121 | if (sinfo[NL80211_STA_INFO_LLID]) |
fbb181fa | 122 | printf("\n\tmesh llid:\t%d", |
3d1e8704 LCC |
123 | nla_get_u16(sinfo[NL80211_STA_INFO_LLID])); |
124 | if (sinfo[NL80211_STA_INFO_PLID]) | |
fbb181fa | 125 | printf("\n\tmesh plid:\t%d", |
3d1e8704 LCC |
126 | nla_get_u16(sinfo[NL80211_STA_INFO_PLID])); |
127 | if (sinfo[NL80211_STA_INFO_PLINK_STATE]) { | |
128 | switch (nla_get_u16(sinfo[NL80211_STA_INFO_PLINK_STATE])) { | |
129 | case LISTEN: | |
130 | strcpy(state_name, "LISTEN"); | |
131 | break; | |
132 | case OPN_SNT: | |
133 | strcpy(state_name, "OPN_SNT"); | |
134 | break; | |
135 | case OPN_RCVD: | |
136 | strcpy(state_name, "OPN_RCVD"); | |
137 | break; | |
138 | case CNF_RCVD: | |
139 | strcpy(state_name, "CNF_RCVD"); | |
140 | break; | |
141 | case ESTAB: | |
142 | strcpy(state_name, "ESTAB"); | |
143 | break; | |
144 | case HOLDING: | |
145 | strcpy(state_name, "HOLDING"); | |
146 | break; | |
147 | case BLOCKED: | |
148 | strcpy(state_name, "BLOCKED"); | |
149 | break; | |
150 | default: | |
151 | strcpy(state_name, "UNKNOWN"); | |
152 | break; | |
153 | } | |
fbb181fa | 154 | printf("\n\tmesh plink:\t%s", state_name); |
3d1e8704 LCC |
155 | } |
156 | ||
157 | printf("\n"); | |
158 | return NL_SKIP; | |
159 | } | |
160 | ||
7c37a24d JB |
161 | static int handle_station_get(struct nl80211_state *state, |
162 | struct nl_cb *cb, | |
b1ca19a8 JB |
163 | struct nl_msg *msg, |
164 | int argc, char **argv) | |
3d1e8704 | 165 | { |
3d1e8704 LCC |
166 | unsigned char mac_addr[ETH_ALEN]; |
167 | ||
b1ca19a8 | 168 | if (argc < 1) |
5e75fd04 | 169 | return 1; |
3d1e8704 LCC |
170 | |
171 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
172 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 173 | return 2; |
3d1e8704 LCC |
174 | } |
175 | ||
176 | argc--; | |
177 | argv++; | |
178 | ||
b1ca19a8 | 179 | if (argc) |
5e75fd04 | 180 | return 1; |
3d1e8704 LCC |
181 | |
182 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 | 183 | |
3d1e8704 | 184 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
3d1e8704 | 185 | |
70391ccf | 186 | return 0; |
3d1e8704 | 187 | nla_put_failure: |
70391ccf | 188 | return -ENOBUFS; |
3d1e8704 | 189 | } |
b1ca19a8 JB |
190 | COMMAND(station, get, "<MAC address>", |
191 | NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get); | |
192 | COMMAND(station, del, "<MAC address>", | |
193 | NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get); | |
3d1e8704 | 194 | |
7c37a24d JB |
195 | static int handle_station_set(struct nl80211_state *state, |
196 | struct nl_cb *cb, | |
b1ca19a8 JB |
197 | struct nl_msg *msg, |
198 | int argc, char **argv) | |
3d1e8704 | 199 | { |
3d1e8704 LCC |
200 | unsigned char plink_action; |
201 | unsigned char mac_addr[ETH_ALEN]; | |
202 | ||
b1ca19a8 | 203 | if (argc < 3) |
5e75fd04 | 204 | return 1; |
3d1e8704 LCC |
205 | |
206 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
207 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 208 | return 2; |
3d1e8704 LCC |
209 | } |
210 | argc--; | |
211 | argv++; | |
212 | ||
b1ca19a8 JB |
213 | if (strcmp("plink_action", argv[0]) != 0) |
214 | return 1; | |
3d1e8704 LCC |
215 | argc--; |
216 | argv++; | |
217 | ||
218 | if (strcmp("open", argv[0]) == 0) | |
219 | plink_action = PLINK_ACTION_OPEN; | |
220 | else if (strcmp("block", argv[0]) == 0) | |
221 | plink_action = PLINK_ACTION_BLOCK; | |
222 | else { | |
223 | fprintf(stderr, "plink action not supported\n"); | |
5e75fd04 | 224 | return 2; |
3d1e8704 LCC |
225 | } |
226 | argc--; | |
227 | argv++; | |
228 | ||
b1ca19a8 | 229 | if (argc) |
5e75fd04 | 230 | return 1; |
3d1e8704 LCC |
231 | |
232 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 LCC |
233 | NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action); |
234 | ||
70391ccf | 235 | return 0; |
3d1e8704 | 236 | nla_put_failure: |
70391ccf | 237 | return -ENOBUFS; |
3d1e8704 | 238 | } |
b1ca19a8 JB |
239 | COMMAND(station, set, "<MAC address> plink_action <open|block>", |
240 | NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set); | |
241 | ||
7c37a24d JB |
242 | static int handle_station_dump(struct nl80211_state *state, |
243 | struct nl_cb *cb, | |
b1ca19a8 JB |
244 | struct nl_msg *msg, |
245 | int argc, char **argv) | |
3d1e8704 | 246 | { |
3d1e8704 | 247 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
70391ccf | 248 | return 0; |
3d1e8704 | 249 | } |
b1ca19a8 | 250 | COMMAND(station, dump, NULL, |
f903d035 | 251 | NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump); |