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