]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
iw: dump station rx bit rate information
[thirdparty/iw.git] / station.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4
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>
10
11 #include "nl80211.h"
12 #include "iw.h"
13
14 SECTION(station);
15
16 enum plink_state {
17 LISTEN,
18 OPN_SNT,
19 OPN_RCVD,
20 CNF_RCVD,
21 ESTAB,
22 HOLDING,
23 BLOCKED
24 };
25
26 static void print_power_mode(struct nlattr *a)
27 {
28 enum nl80211_mesh_power_mode pm = nla_get_u32(a);
29
30 switch (pm) {
31 case NL80211_MESH_POWER_ACTIVE:
32 printf("ACTIVE");
33 break;
34 case NL80211_MESH_POWER_LIGHT_SLEEP:
35 printf("LIGHT SLEEP");
36 break;
37 case NL80211_MESH_POWER_DEEP_SLEEP:
38 printf("DEEP SLEEP");
39 break;
40 default:
41 printf("UNKNOWN");
42 break;
43 }
44 }
45
46 void parse_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen)
47 {
48 int rate = 0;
49 char *pos = buf;
50 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
51 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
52 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
53 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
54 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
55 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
56 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
57 };
58
59 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
60 bitrate_attr, rate_policy)) {
61 snprintf(buf, buflen, "failed to parse nested rate attributes!");
62 return;
63 }
64
65 if (rinfo[NL80211_RATE_INFO_BITRATE32])
66 rate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]);
67 else if (rinfo[NL80211_RATE_INFO_BITRATE])
68 rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
69 if (rate > 0)
70 pos += snprintf(pos, buflen - (pos - buf),
71 "%d.%d MBit/s", rate / 10, rate % 10);
72
73 if (rinfo[NL80211_RATE_INFO_MCS])
74 pos += snprintf(pos, buflen - (pos - buf),
75 " MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
76 if (rinfo[NL80211_RATE_INFO_VHT_MCS])
77 pos += snprintf(pos, buflen - (pos - buf),
78 " VHT-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_MCS]));
79 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
80 pos += snprintf(pos, buflen - (pos - buf), " 40MHz");
81 if (rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
82 pos += snprintf(pos, buflen - (pos - buf), " 80MHz");
83 if (rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
84 pos += snprintf(pos, buflen - (pos - buf), " 80P80MHz");
85 if (rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
86 pos += snprintf(pos, buflen - (pos - buf), " 160MHz");
87 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
88 pos += snprintf(pos, buflen - (pos - buf), " short GI");
89 if (rinfo[NL80211_RATE_INFO_VHT_NSS])
90 pos += snprintf(pos, buflen - (pos - buf),
91 " VHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]));
92 }
93
94 static int print_sta_handler(struct nl_msg *msg, void *arg)
95 {
96 struct nlattr *tb[NL80211_ATTR_MAX + 1];
97 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
98 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
99 char mac_addr[20], state_name[10], dev[20];
100 struct nl80211_sta_flag_update *sta_flags;
101 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
102 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
103 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
104 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
105 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
106 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
107 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
108 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
109 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
110 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
111 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
112 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
113 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
114 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
115 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
116 [NL80211_STA_INFO_STA_FLAGS] =
117 { .minlen = sizeof(struct nl80211_sta_flag_update) },
118 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32},
119 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32},
120 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32},
121 };
122
123 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
124 genlmsg_attrlen(gnlh, 0), NULL);
125
126 /*
127 * TODO: validate the interface and mac address!
128 * Otherwise, there's a race condition as soon as
129 * the kernel starts sending station notifications.
130 */
131
132 if (!tb[NL80211_ATTR_STA_INFO]) {
133 fprintf(stderr, "sta stats missing!\n");
134 return NL_SKIP;
135 }
136 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
137 tb[NL80211_ATTR_STA_INFO],
138 stats_policy)) {
139 fprintf(stderr, "failed to parse nested attributes!\n");
140 return NL_SKIP;
141 }
142
143 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
144 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
145 printf("Station %s (on %s)", mac_addr, dev);
146
147 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
148 printf("\n\tinactive time:\t%u ms",
149 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
150 if (sinfo[NL80211_STA_INFO_RX_BYTES])
151 printf("\n\trx bytes:\t%u",
152 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
153 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
154 printf("\n\trx packets:\t%u",
155 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
156 if (sinfo[NL80211_STA_INFO_TX_BYTES])
157 printf("\n\ttx bytes:\t%u",
158 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
159 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
160 printf("\n\ttx packets:\t%u",
161 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
162 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
163 printf("\n\ttx retries:\t%u",
164 nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
165 if (sinfo[NL80211_STA_INFO_TX_FAILED])
166 printf("\n\ttx failed:\t%u",
167 nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
168 if (sinfo[NL80211_STA_INFO_SIGNAL])
169 printf("\n\tsignal: \t%d dBm",
170 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
171 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
172 printf("\n\tsignal avg:\t%d dBm",
173 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]));
174 if (sinfo[NL80211_STA_INFO_T_OFFSET])
175 printf("\n\tToffset:\t%lld us",
176 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
177
178 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
179 char buf[100];
180
181 parse_bitrate(sinfo[NL80211_STA_INFO_TX_BITRATE], buf, sizeof(buf));
182 printf("\n\ttx bitrate:\t%s", buf);
183 }
184
185 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
186 char buf[100];
187
188 parse_bitrate(sinfo[NL80211_STA_INFO_RX_BITRATE], buf, sizeof(buf));
189 printf("\n\trx bitrate:\t%s", buf);
190 }
191
192 if (sinfo[NL80211_STA_INFO_LLID])
193 printf("\n\tmesh llid:\t%d",
194 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
195 if (sinfo[NL80211_STA_INFO_PLID])
196 printf("\n\tmesh plid:\t%d",
197 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
198 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
199 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
200 case LISTEN:
201 strcpy(state_name, "LISTEN");
202 break;
203 case OPN_SNT:
204 strcpy(state_name, "OPN_SNT");
205 break;
206 case OPN_RCVD:
207 strcpy(state_name, "OPN_RCVD");
208 break;
209 case CNF_RCVD:
210 strcpy(state_name, "CNF_RCVD");
211 break;
212 case ESTAB:
213 strcpy(state_name, "ESTAB");
214 break;
215 case HOLDING:
216 strcpy(state_name, "HOLDING");
217 break;
218 case BLOCKED:
219 strcpy(state_name, "BLOCKED");
220 break;
221 default:
222 strcpy(state_name, "UNKNOWN");
223 break;
224 }
225 printf("\n\tmesh plink:\t%s", state_name);
226 }
227 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) {
228 printf("\n\tmesh local PS mode:\t");
229 print_power_mode(sinfo[NL80211_STA_INFO_LOCAL_PM]);
230 }
231 if (sinfo[NL80211_STA_INFO_PEER_PM]) {
232 printf("\n\tmesh peer PS mode:\t");
233 print_power_mode(sinfo[NL80211_STA_INFO_PEER_PM]);
234 }
235 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) {
236 printf("\n\tmesh non-peer PS mode:\t");
237 print_power_mode(sinfo[NL80211_STA_INFO_NONPEER_PM]);
238 }
239
240 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) {
241 sta_flags = (struct nl80211_sta_flag_update *)
242 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
243
244 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
245 printf("\n\tauthorized:\t");
246 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
247 printf("yes");
248 else
249 printf("no");
250 }
251
252 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) {
253 printf("\n\tauthenticated:\t");
254 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
255 printf("yes");
256 else
257 printf("no");
258 }
259
260 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
261 printf("\n\tpreamble:\t");
262 if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
263 printf("short");
264 else
265 printf("long");
266 }
267
268 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) {
269 printf("\n\tWMM/WME:\t");
270 if (sta_flags->set & BIT(NL80211_STA_FLAG_WME))
271 printf("yes");
272 else
273 printf("no");
274 }
275
276 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) {
277 printf("\n\tMFP:\t\t");
278 if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
279 printf("yes");
280 else
281 printf("no");
282 }
283
284 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
285 printf("\n\tTDLS peer:\t");
286 if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
287 printf("yes");
288 else
289 printf("no");
290 }
291 }
292
293 printf("\n");
294 return NL_SKIP;
295 }
296
297 static int handle_station_get(struct nl80211_state *state,
298 struct nl_cb *cb,
299 struct nl_msg *msg,
300 int argc, char **argv,
301 enum id_input id)
302 {
303 unsigned char mac_addr[ETH_ALEN];
304
305 if (argc < 1)
306 return 1;
307
308 if (mac_addr_a2n(mac_addr, argv[0])) {
309 fprintf(stderr, "invalid mac address\n");
310 return 2;
311 }
312
313 argc--;
314 argv++;
315
316 if (argc)
317 return 1;
318
319 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
320
321 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
322
323 return 0;
324 nla_put_failure:
325 return -ENOBUFS;
326 }
327 COMMAND(station, get, "<MAC address>",
328 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
329 "Get information for a specific station.");
330 COMMAND(station, del, "<MAC address>",
331 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
332 "Remove the given station entry (use with caution!)");
333
334 static const struct cmd *station_set_plink;
335 static const struct cmd *station_set_vlan;
336 static const struct cmd *station_set_mesh_power_mode;
337
338 static const struct cmd *select_station_cmd(int argc, char **argv)
339 {
340 if (argc < 2)
341 return NULL;
342 if (strcmp(argv[1], "plink_action") == 0)
343 return station_set_plink;
344 if (strcmp(argv[1], "vlan") == 0)
345 return station_set_vlan;
346 if (strcmp(argv[1], "mesh_power_mode") == 0)
347 return station_set_mesh_power_mode;
348 return NULL;
349 }
350
351 static int handle_station_set_plink(struct nl80211_state *state,
352 struct nl_cb *cb,
353 struct nl_msg *msg,
354 int argc, char **argv,
355 enum id_input id)
356 {
357 unsigned char plink_action;
358 unsigned char mac_addr[ETH_ALEN];
359
360 if (argc < 3)
361 return 1;
362
363 if (mac_addr_a2n(mac_addr, argv[0])) {
364 fprintf(stderr, "invalid mac address\n");
365 return 2;
366 }
367 argc--;
368 argv++;
369
370 if (strcmp("plink_action", argv[0]) != 0)
371 return 1;
372 argc--;
373 argv++;
374
375 if (strcmp("open", argv[0]) == 0)
376 plink_action = NL80211_PLINK_ACTION_OPEN;
377 else if (strcmp("block", argv[0]) == 0)
378 plink_action = NL80211_PLINK_ACTION_BLOCK;
379 else {
380 fprintf(stderr, "plink action not supported\n");
381 return 2;
382 }
383 argc--;
384 argv++;
385
386 if (argc)
387 return 1;
388
389 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
390 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
391
392 return 0;
393 nla_put_failure:
394 return -ENOBUFS;
395 }
396 COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
397 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
398 "Set mesh peer link action for this station (peer).",
399 select_station_cmd, station_set_plink);
400
401 static int handle_station_set_vlan(struct nl80211_state *state,
402 struct nl_cb *cb,
403 struct nl_msg *msg,
404 int argc, char **argv,
405 enum id_input id)
406 {
407 unsigned char mac_addr[ETH_ALEN];
408 unsigned long sta_vlan = 0;
409 char *err = NULL;
410
411 if (argc < 3)
412 return 1;
413
414 if (mac_addr_a2n(mac_addr, argv[0])) {
415 fprintf(stderr, "invalid mac address\n");
416 return 2;
417 }
418 argc--;
419 argv++;
420
421 if (strcmp("vlan", argv[0]) != 0)
422 return 1;
423 argc--;
424 argv++;
425
426 sta_vlan = strtoul(argv[0], &err, 0);
427 if (err && *err) {
428 fprintf(stderr, "invalid vlan id\n");
429 return 2;
430 }
431 argc--;
432 argv++;
433
434 if (argc)
435 return 1;
436
437 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
438 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan);
439
440 return 0;
441 nla_put_failure:
442 return -ENOBUFS;
443 }
444 COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
445 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
446 "Set an AP VLAN for this station.",
447 select_station_cmd, station_set_vlan);
448
449 static int handle_station_set_mesh_power_mode(struct nl80211_state *state,
450 struct nl_cb *cb,
451 struct nl_msg *msg,
452 int argc, char **argv,
453 enum id_input id)
454 {
455 unsigned char mesh_power_mode;
456 unsigned char mac_addr[ETH_ALEN];
457
458 if (argc < 3)
459 return 1;
460
461 if (mac_addr_a2n(mac_addr, argv[0])) {
462 fprintf(stderr, "invalid mac address\n");
463 return 2;
464 }
465 argc--;
466 argv++;
467
468 if (strcmp("mesh_power_mode", argv[0]) != 0)
469 return 1;
470 argc--;
471 argv++;
472
473 if (strcmp("active", argv[0]) == 0)
474 mesh_power_mode = NL80211_MESH_POWER_ACTIVE;
475 else if (strcmp("light", argv[0]) == 0)
476 mesh_power_mode = NL80211_MESH_POWER_LIGHT_SLEEP;
477 else if (strcmp("deep", argv[0]) == 0)
478 mesh_power_mode = NL80211_MESH_POWER_DEEP_SLEEP;
479 else {
480 fprintf(stderr, "unknown mesh power mode\n");
481 return 2;
482 }
483 argc--;
484 argv++;
485
486 if (argc)
487 return 1;
488
489 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
490 NLA_PUT_U32(msg, NL80211_ATTR_LOCAL_MESH_POWER_MODE, mesh_power_mode);
491
492 return 0;
493 nla_put_failure:
494 return -ENOBUFS;
495 }
496 COMMAND_ALIAS(station, set, "<MAC address> mesh_power_mode "
497 "<active|light|deep>", NL80211_CMD_SET_STATION, 0, CIB_NETDEV,
498 handle_station_set_mesh_power_mode,
499 "Set link-specific mesh power mode for this station",
500 select_station_cmd, station_set_mesh_power_mode);
501
502 static int handle_station_dump(struct nl80211_state *state,
503 struct nl_cb *cb,
504 struct nl_msg *msg,
505 int argc, char **argv,
506 enum id_input id)
507 {
508 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
509 return 0;
510 }
511 COMMAND(station, dump, NULL,
512 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
513 "List all stations known, e.g. the AP on managed interfaces");