]>
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 | ||
4698bfc2 JB |
14 | SECTION(station); |
15 | ||
3d1e8704 LCC |
16 | enum plink_state { |
17 | LISTEN, | |
18 | OPN_SNT, | |
19 | OPN_RCVD, | |
20 | CNF_RCVD, | |
21 | ESTAB, | |
22 | HOLDING, | |
23 | BLOCKED | |
24 | }; | |
25 | ||
26 | enum plink_actions { | |
27 | PLINK_ACTION_UNDEFINED, | |
28 | PLINK_ACTION_OPEN, | |
29 | PLINK_ACTION_BLOCK, | |
30 | }; | |
31 | ||
32 | ||
3d1e8704 LCC |
33 | static int print_sta_handler(struct nl_msg *msg, void *arg) |
34 | { | |
35 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
36 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
37 | struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; | |
9cd3f1c1 | 38 | struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; |
3d1e8704 | 39 | char mac_addr[20], state_name[10], dev[20]; |
b638215d | 40 | struct nl80211_sta_flag_update *sta_flags; |
3d1e8704 LCC |
41 | static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { |
42 | [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, | |
43 | [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, | |
44 | [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, | |
859677cb JB |
45 | [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, |
46 | [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, | |
9cd3f1c1 | 47 | [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, |
ea3ade85 | 48 | [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 }, |
9cd3f1c1 | 49 | [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, |
3d1e8704 LCC |
50 | [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, |
51 | [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, | |
52 | [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, | |
0f5868ee BR |
53 | [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 }, |
54 | [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, | |
b638215d HS |
55 | [NL80211_STA_INFO_STA_FLAGS] = |
56 | { .minlen = sizeof(struct nl80211_sta_flag_update) }, | |
3d1e8704 LCC |
57 | }; |
58 | ||
9cd3f1c1 HR |
59 | static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { |
60 | [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, | |
61 | [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, | |
62 | [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, | |
63 | [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, | |
64 | }; | |
65 | ||
3d1e8704 LCC |
66 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
67 | genlmsg_attrlen(gnlh, 0), NULL); | |
68 | ||
69 | /* | |
70 | * TODO: validate the interface and mac address! | |
71 | * Otherwise, there's a race condition as soon as | |
72 | * the kernel starts sending station notifications. | |
73 | */ | |
74 | ||
75 | if (!tb[NL80211_ATTR_STA_INFO]) { | |
5fe70c0e | 76 | fprintf(stderr, "sta stats missing!\n"); |
3d1e8704 LCC |
77 | return NL_SKIP; |
78 | } | |
79 | if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, | |
80 | tb[NL80211_ATTR_STA_INFO], | |
81 | stats_policy)) { | |
5fe70c0e | 82 | fprintf(stderr, "failed to parse nested attributes!\n"); |
3d1e8704 LCC |
83 | return NL_SKIP; |
84 | } | |
85 | ||
86 | mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC])); | |
87 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
fbb181fa | 88 | printf("Station %s (on %s)", mac_addr, dev); |
3d1e8704 LCC |
89 | |
90 | if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) | |
f5c9799c | 91 | printf("\n\tinactive time:\t%u ms", |
3d1e8704 LCC |
92 | nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME])); |
93 | if (sinfo[NL80211_STA_INFO_RX_BYTES]) | |
f5c9799c | 94 | printf("\n\trx bytes:\t%u", |
3d1e8704 | 95 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES])); |
859677cb | 96 | if (sinfo[NL80211_STA_INFO_RX_PACKETS]) |
f5c9799c | 97 | printf("\n\trx packets:\t%u", |
859677cb | 98 | nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS])); |
3d1e8704 | 99 | if (sinfo[NL80211_STA_INFO_TX_BYTES]) |
f5c9799c | 100 | printf("\n\ttx bytes:\t%u", |
3d1e8704 | 101 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES])); |
859677cb | 102 | if (sinfo[NL80211_STA_INFO_TX_PACKETS]) |
f5c9799c | 103 | printf("\n\ttx packets:\t%u", |
859677cb | 104 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS])); |
0f5868ee BR |
105 | if (sinfo[NL80211_STA_INFO_TX_RETRIES]) |
106 | printf("\n\ttx retries:\t%u", | |
107 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES])); | |
108 | if (sinfo[NL80211_STA_INFO_TX_FAILED]) | |
109 | printf("\n\ttx failed:\t%u", | |
110 | nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED])); | |
9cd3f1c1 HR |
111 | if (sinfo[NL80211_STA_INFO_SIGNAL]) |
112 | printf("\n\tsignal: \t%d dBm", | |
113 | (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL])); | |
ba292ae9 BR |
114 | if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) |
115 | printf("\n\tsignal avg:\t%d dBm", | |
116 | (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG])); | |
ea3ade85 AN |
117 | if (sinfo[NL80211_STA_INFO_T_OFFSET]) |
118 | printf("\n\tToffset:\t%lld us", | |
119 | (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET])); | |
9cd3f1c1 HR |
120 | |
121 | if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { | |
122 | if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, | |
123 | sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) { | |
5fe70c0e | 124 | fprintf(stderr, "failed to parse nested rate attributes!\n"); |
9cd3f1c1 HR |
125 | } else { |
126 | printf("\n\ttx bitrate:\t"); | |
127 | if (rinfo[NL80211_RATE_INFO_BITRATE]) { | |
128 | int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); | |
129 | printf("%d.%d MBit/s", rate / 10, rate % 10); | |
130 | } | |
131 | ||
132 | if (rinfo[NL80211_RATE_INFO_MCS]) | |
133 | printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS])); | |
134 | if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH]) | |
135 | printf(" 40Mhz"); | |
136 | if (rinfo[NL80211_RATE_INFO_SHORT_GI]) | |
137 | printf(" short GI"); | |
138 | } | |
139 | } | |
140 | ||
3d1e8704 | 141 | if (sinfo[NL80211_STA_INFO_LLID]) |
fbb181fa | 142 | printf("\n\tmesh llid:\t%d", |
3d1e8704 LCC |
143 | nla_get_u16(sinfo[NL80211_STA_INFO_LLID])); |
144 | if (sinfo[NL80211_STA_INFO_PLID]) | |
fbb181fa | 145 | printf("\n\tmesh plid:\t%d", |
3d1e8704 LCC |
146 | nla_get_u16(sinfo[NL80211_STA_INFO_PLID])); |
147 | if (sinfo[NL80211_STA_INFO_PLINK_STATE]) { | |
dbaabba1 | 148 | switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) { |
3d1e8704 LCC |
149 | case LISTEN: |
150 | strcpy(state_name, "LISTEN"); | |
151 | break; | |
152 | case OPN_SNT: | |
153 | strcpy(state_name, "OPN_SNT"); | |
154 | break; | |
155 | case OPN_RCVD: | |
156 | strcpy(state_name, "OPN_RCVD"); | |
157 | break; | |
158 | case CNF_RCVD: | |
159 | strcpy(state_name, "CNF_RCVD"); | |
160 | break; | |
161 | case ESTAB: | |
162 | strcpy(state_name, "ESTAB"); | |
163 | break; | |
164 | case HOLDING: | |
165 | strcpy(state_name, "HOLDING"); | |
166 | break; | |
167 | case BLOCKED: | |
168 | strcpy(state_name, "BLOCKED"); | |
169 | break; | |
170 | default: | |
171 | strcpy(state_name, "UNKNOWN"); | |
172 | break; | |
173 | } | |
fbb181fa | 174 | printf("\n\tmesh plink:\t%s", state_name); |
3d1e8704 LCC |
175 | } |
176 | ||
b638215d HS |
177 | if (sinfo[NL80211_STA_INFO_STA_FLAGS]) { |
178 | sta_flags = (struct nl80211_sta_flag_update *) | |
179 | nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]); | |
180 | ||
181 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) { | |
182 | printf("\n\tauthorized:\t"); | |
183 | if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED)) | |
184 | printf("yes"); | |
185 | else | |
186 | printf("no"); | |
187 | } | |
188 | ||
189 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) { | |
190 | printf("\n\tauthenticated:\t"); | |
191 | if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) | |
192 | printf("yes"); | |
193 | else | |
194 | printf("no"); | |
195 | } | |
196 | ||
197 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) { | |
198 | printf("\n\tpreamble:\t"); | |
199 | if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) | |
200 | printf("short"); | |
201 | else | |
202 | printf("long"); | |
203 | } | |
204 | ||
205 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) { | |
206 | printf("\n\tWMM/WME:\t"); | |
207 | if (sta_flags->set & BIT(NL80211_STA_FLAG_WME)) | |
208 | printf("yes"); | |
209 | else | |
210 | printf("no"); | |
211 | } | |
212 | ||
213 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) { | |
214 | printf("\n\tMFP:\t\t"); | |
215 | if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP)) | |
216 | printf("yes"); | |
217 | else | |
218 | printf("no"); | |
219 | } | |
220 | ||
221 | if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) { | |
222 | printf("\n\tTDLS peer:\t\t"); | |
223 | if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER)) | |
224 | printf("yes"); | |
225 | else | |
226 | printf("no"); | |
227 | } | |
228 | } | |
229 | ||
3d1e8704 LCC |
230 | printf("\n"); |
231 | return NL_SKIP; | |
232 | } | |
233 | ||
7c37a24d JB |
234 | static int handle_station_get(struct nl80211_state *state, |
235 | struct nl_cb *cb, | |
b1ca19a8 JB |
236 | struct nl_msg *msg, |
237 | int argc, char **argv) | |
3d1e8704 | 238 | { |
3d1e8704 LCC |
239 | unsigned char mac_addr[ETH_ALEN]; |
240 | ||
b1ca19a8 | 241 | if (argc < 1) |
5e75fd04 | 242 | return 1; |
3d1e8704 LCC |
243 | |
244 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
245 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 246 | return 2; |
3d1e8704 LCC |
247 | } |
248 | ||
249 | argc--; | |
250 | argv++; | |
251 | ||
b1ca19a8 | 252 | if (argc) |
5e75fd04 | 253 | return 1; |
3d1e8704 LCC |
254 | |
255 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 | 256 | |
3d1e8704 | 257 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
3d1e8704 | 258 | |
70391ccf | 259 | return 0; |
3d1e8704 | 260 | nla_put_failure: |
70391ccf | 261 | return -ENOBUFS; |
3d1e8704 | 262 | } |
b1ca19a8 | 263 | COMMAND(station, get, "<MAC address>", |
70cf4544 JB |
264 | NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get, |
265 | "Get information for a specific station."); | |
b1ca19a8 | 266 | COMMAND(station, del, "<MAC address>", |
70cf4544 JB |
267 | NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get, |
268 | "Remove the given station entry (use with caution!)"); | |
3d1e8704 | 269 | |
1633ddf7 JB |
270 | static const struct cmd *station_set_plink; |
271 | static const struct cmd *station_set_vlan; | |
272 | ||
273 | static const struct cmd *select_station_cmd(int argc, char **argv) | |
274 | { | |
275 | if (argc < 2) | |
276 | return NULL; | |
277 | if (strcmp(argv[1], "plink_action") == 0) | |
278 | return station_set_plink; | |
279 | if (strcmp(argv[1], "vlan") == 0) | |
280 | return station_set_vlan; | |
281 | return NULL; | |
282 | } | |
283 | ||
ce0fc33a | 284 | static int handle_station_set_plink(struct nl80211_state *state, |
7c37a24d | 285 | struct nl_cb *cb, |
b1ca19a8 JB |
286 | struct nl_msg *msg, |
287 | int argc, char **argv) | |
3d1e8704 | 288 | { |
3d1e8704 LCC |
289 | unsigned char plink_action; |
290 | unsigned char mac_addr[ETH_ALEN]; | |
291 | ||
b1ca19a8 | 292 | if (argc < 3) |
5e75fd04 | 293 | return 1; |
3d1e8704 LCC |
294 | |
295 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
296 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 297 | return 2; |
3d1e8704 LCC |
298 | } |
299 | argc--; | |
300 | argv++; | |
301 | ||
b1ca19a8 JB |
302 | if (strcmp("plink_action", argv[0]) != 0) |
303 | return 1; | |
3d1e8704 LCC |
304 | argc--; |
305 | argv++; | |
306 | ||
307 | if (strcmp("open", argv[0]) == 0) | |
308 | plink_action = PLINK_ACTION_OPEN; | |
309 | else if (strcmp("block", argv[0]) == 0) | |
310 | plink_action = PLINK_ACTION_BLOCK; | |
311 | else { | |
312 | fprintf(stderr, "plink action not supported\n"); | |
5e75fd04 | 313 | return 2; |
3d1e8704 LCC |
314 | } |
315 | argc--; | |
316 | argv++; | |
317 | ||
b1ca19a8 | 318 | if (argc) |
5e75fd04 | 319 | return 1; |
3d1e8704 LCC |
320 | |
321 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
3d1e8704 LCC |
322 | NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action); |
323 | ||
70391ccf | 324 | return 0; |
3d1e8704 | 325 | nla_put_failure: |
70391ccf | 326 | return -ENOBUFS; |
3d1e8704 | 327 | } |
1633ddf7 | 328 | COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>", |
ce0fc33a | 329 | NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink, |
1633ddf7 JB |
330 | "Set mesh peer link action for this station (peer).", |
331 | select_station_cmd, station_set_plink); | |
b1ca19a8 | 332 | |
ce0fc33a FF |
333 | static int handle_station_set_vlan(struct nl80211_state *state, |
334 | struct nl_cb *cb, | |
335 | struct nl_msg *msg, | |
336 | int argc, char **argv) | |
337 | { | |
338 | unsigned char mac_addr[ETH_ALEN]; | |
339 | unsigned long sta_vlan = 0; | |
340 | char *err = NULL; | |
341 | ||
342 | if (argc < 3) | |
343 | return 1; | |
344 | ||
345 | if (mac_addr_a2n(mac_addr, argv[0])) { | |
346 | fprintf(stderr, "invalid mac address\n"); | |
347 | return 2; | |
348 | } | |
349 | argc--; | |
350 | argv++; | |
351 | ||
352 | if (strcmp("vlan", argv[0]) != 0) | |
353 | return 1; | |
354 | argc--; | |
355 | argv++; | |
356 | ||
357 | sta_vlan = strtoul(argv[0], &err, 0); | |
358 | if (err && *err) { | |
359 | fprintf(stderr, "invalid vlan id\n"); | |
360 | return 2; | |
361 | } | |
362 | argc--; | |
363 | argv++; | |
364 | ||
365 | if (argc) | |
366 | return 1; | |
367 | ||
368 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
369 | NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan); | |
370 | ||
371 | return 0; | |
372 | nla_put_failure: | |
373 | return -ENOBUFS; | |
374 | } | |
1633ddf7 | 375 | COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>", |
ce0fc33a | 376 | NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan, |
1633ddf7 JB |
377 | "Set an AP VLAN for this station.", |
378 | select_station_cmd, station_set_vlan); | |
ce0fc33a FF |
379 | |
380 | ||
7c37a24d JB |
381 | static int handle_station_dump(struct nl80211_state *state, |
382 | struct nl_cb *cb, | |
b1ca19a8 JB |
383 | struct nl_msg *msg, |
384 | int argc, char **argv) | |
3d1e8704 | 385 | { |
3d1e8704 | 386 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL); |
70391ccf | 387 | return 0; |
3d1e8704 | 388 | } |
b1ca19a8 | 389 | COMMAND(station, dump, NULL, |
70cf4544 JB |
390 | NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump, |
391 | "List all stations known, e.g. the AP on managed interfaces"); |