]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
iw: fix cmd_size determination with LTO
[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 int parse_txq_stats(char *buf, int buflen, struct nlattr *tid_stats_attr, int header,
47 int tid, const char *indent)
48 {
49 struct nlattr *txqstats_info[NL80211_TXQ_STATS_MAX + 1], *txqinfo;
50 static struct nla_policy txqstats_policy[NL80211_TXQ_STATS_MAX + 1] = {
51 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
52 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
53 [NL80211_TXQ_STATS_FLOWS] = { .type = NLA_U32 },
54 [NL80211_TXQ_STATS_DROPS] = { .type = NLA_U32 },
55 [NL80211_TXQ_STATS_ECN_MARKS] = { .type = NLA_U32 },
56 [NL80211_TXQ_STATS_OVERLIMIT] = { .type = NLA_U32 },
57 [NL80211_TXQ_STATS_COLLISIONS] = { .type = NLA_U32 },
58 [NL80211_TXQ_STATS_TX_BYTES] = { .type = NLA_U32 },
59 [NL80211_TXQ_STATS_TX_PACKETS] = { .type = NLA_U32 },
60 };
61 char *pos = buf;
62 if (nla_parse_nested(txqstats_info, NL80211_TXQ_STATS_MAX, tid_stats_attr,
63 txqstats_policy)) {
64 printf("failed to parse nested TXQ stats attributes!");
65 return 0;
66 }
67
68 if (header)
69 pos += snprintf(buf, buflen, "\n%s\t%s\tqsz-byt\t"
70 "qsz-pkt\tflows\tdrops\tmarks\toverlmt\t"
71 "hashcol\ttx-bytes\ttx-packets", indent,
72 tid >= 0 ? "TID" : "");
73
74 pos += snprintf(pos, buflen - (pos - buf), "\n%s\t", indent);
75 if (tid >= 0)
76 pos += snprintf(pos, buflen - (pos - buf), "%d", tid);
77
78 #define PRINT_STAT(key, spacer) do { \
79 txqinfo = txqstats_info[NL80211_TXQ_STATS_ ## key]; \
80 pos += snprintf(pos, buflen - (pos - buf), spacer); \
81 if (txqinfo) \
82 pos += snprintf(pos, buflen - (pos - buf), "%u", \
83 nla_get_u32(txqinfo)); \
84 } while (0)
85
86
87 PRINT_STAT(BACKLOG_BYTES, "\t");
88 PRINT_STAT(BACKLOG_PACKETS, "\t");
89 PRINT_STAT(FLOWS, "\t");
90 PRINT_STAT(DROPS, "\t");
91 PRINT_STAT(ECN_MARKS, "\t");
92 PRINT_STAT(OVERLIMIT, "\t");
93 PRINT_STAT(COLLISIONS, "\t");
94 PRINT_STAT(TX_BYTES, "\t");
95 PRINT_STAT(TX_PACKETS, "\t\t");
96
97 #undef PRINT_STAT
98
99 return pos - buf;
100
101 }
102
103 static void parse_tid_stats(struct nlattr *tid_stats_attr)
104 {
105 struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1], *tidattr, *info;
106 static struct nla_policy stats_policy[NL80211_TID_STATS_MAX + 1] = {
107 [NL80211_TID_STATS_RX_MSDU] = { .type = NLA_U64 },
108 [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
109 [NL80211_TID_STATS_TX_MSDU_RETRIES] = { .type = NLA_U64 },
110 [NL80211_TID_STATS_TX_MSDU_FAILED] = { .type = NLA_U64 },
111 [NL80211_TID_STATS_TXQ_STATS] = { .type = NLA_NESTED },
112 };
113 int rem, i = 0;
114 char txqbuf[2000] = {}, *pos = txqbuf;
115 int buflen = sizeof(txqbuf), foundtxq = 0;
116
117 printf("\n\tMSDU:\n\t\tTID\trx\ttx\ttx retries\ttx failed");
118 nla_for_each_nested(tidattr, tid_stats_attr, rem) {
119 if (nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,
120 tidattr, stats_policy)) {
121 printf("failed to parse nested stats attributes!");
122 return;
123 }
124 printf("\n\t\t%d", i);
125 info = stats_info[NL80211_TID_STATS_RX_MSDU];
126 if (info)
127 printf("\t%llu", (unsigned long long)nla_get_u64(info));
128 info = stats_info[NL80211_TID_STATS_TX_MSDU];
129 if (info)
130 printf("\t%llu", (unsigned long long)nla_get_u64(info));
131 info = stats_info[NL80211_TID_STATS_TX_MSDU_RETRIES];
132 if (info)
133 printf("\t%llu", (unsigned long long)nla_get_u64(info));
134 info = stats_info[NL80211_TID_STATS_TX_MSDU_FAILED];
135 if (info)
136 printf("\t\t%llu", (unsigned long long)nla_get_u64(info));
137 info = stats_info[NL80211_TID_STATS_TXQ_STATS];
138 if (info) {
139 pos += parse_txq_stats(pos, buflen - (pos - txqbuf), info, !foundtxq, i, "\t");
140 foundtxq = 1;
141 }
142
143 i++;
144 }
145
146 if (foundtxq)
147 printf("\n\tTXQs:%s", txqbuf);
148 }
149
150 static void parse_bss_param(struct nlattr *bss_param_attr)
151 {
152 struct nlattr *bss_param_info[NL80211_STA_BSS_PARAM_MAX + 1], *info;
153 static struct nla_policy bss_poilcy[NL80211_STA_BSS_PARAM_MAX + 1] = {
154 [NL80211_STA_BSS_PARAM_CTS_PROT] = { .type = NLA_FLAG },
155 [NL80211_STA_BSS_PARAM_SHORT_PREAMBLE] = { .type = NLA_FLAG },
156 [NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME] = { .type = NLA_FLAG },
157 [NL80211_STA_BSS_PARAM_DTIM_PERIOD] = { .type = NLA_U8 },
158 [NL80211_STA_BSS_PARAM_BEACON_INTERVAL] = { .type = NLA_U16 },
159 };
160
161 if (nla_parse_nested(bss_param_info, NL80211_STA_BSS_PARAM_MAX,
162 bss_param_attr, bss_poilcy)) {
163 printf("failed to parse nested bss param attributes!");
164 }
165
166 info = bss_param_info[NL80211_STA_BSS_PARAM_DTIM_PERIOD];
167 if (info)
168 printf("\n\tDTIM period:\t%u", nla_get_u8(info));
169 info = bss_param_info[NL80211_STA_BSS_PARAM_BEACON_INTERVAL];
170 if (info)
171 printf("\n\tbeacon interval:%u", nla_get_u16(info));
172 info = bss_param_info[NL80211_STA_BSS_PARAM_CTS_PROT];
173 if (info) {
174 printf("\n\tCTS protection:");
175 if (nla_get_u16(info))
176 printf("\tyes");
177 else
178 printf("\tno");
179 }
180 info = bss_param_info[NL80211_STA_BSS_PARAM_SHORT_PREAMBLE];
181 if (info) {
182 printf("\n\tshort preamble:");
183 if (nla_get_u16(info))
184 printf("\tyes");
185 else
186 printf("\tno");
187 }
188 info = bss_param_info[NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME];
189 if (info) {
190 printf("\n\tshort slot time:");
191 if (nla_get_u16(info))
192 printf("yes");
193 else
194 printf("no");
195 }
196 }
197
198 void parse_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen)
199 {
200 int rate = 0;
201 char *pos = buf;
202 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
203 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
204 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
205 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
206 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
207 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
208 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
209 };
210
211 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
212 bitrate_attr, rate_policy)) {
213 snprintf(buf, buflen, "failed to parse nested rate attributes!");
214 return;
215 }
216
217 if (rinfo[NL80211_RATE_INFO_BITRATE32])
218 rate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]);
219 else if (rinfo[NL80211_RATE_INFO_BITRATE])
220 rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
221 if (rate > 0)
222 pos += snprintf(pos, buflen - (pos - buf),
223 "%d.%d MBit/s", rate / 10, rate % 10);
224 else
225 pos += snprintf(pos, buflen - (pos - buf), "(unknown)");
226
227 if (rinfo[NL80211_RATE_INFO_MCS])
228 pos += snprintf(pos, buflen - (pos - buf),
229 " MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
230 if (rinfo[NL80211_RATE_INFO_VHT_MCS])
231 pos += snprintf(pos, buflen - (pos - buf),
232 " VHT-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_MCS]));
233 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
234 pos += snprintf(pos, buflen - (pos - buf), " 40MHz");
235 if (rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
236 pos += snprintf(pos, buflen - (pos - buf), " 80MHz");
237 if (rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
238 pos += snprintf(pos, buflen - (pos - buf), " 80P80MHz");
239 if (rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
240 pos += snprintf(pos, buflen - (pos - buf), " 160MHz");
241 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
242 pos += snprintf(pos, buflen - (pos - buf), " short GI");
243 if (rinfo[NL80211_RATE_INFO_VHT_NSS])
244 pos += snprintf(pos, buflen - (pos - buf),
245 " VHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]));
246 }
247
248 static char *get_chain_signal(struct nlattr *attr_list)
249 {
250 struct nlattr *attr;
251 static char buf[64];
252 char *cur = buf;
253 int i = 0, rem;
254 const char *prefix;
255
256 if (!attr_list)
257 return "";
258
259 nla_for_each_nested(attr, attr_list, rem) {
260 if (i++ > 0)
261 prefix = ", ";
262 else
263 prefix = "[";
264
265 cur += snprintf(cur, sizeof(buf) - (cur - buf), "%s%d", prefix,
266 (int8_t) nla_get_u8(attr));
267 }
268
269 if (i)
270 snprintf(cur, sizeof(buf) - (cur - buf), "] ");
271
272 return buf;
273 }
274
275 static int print_sta_handler(struct nl_msg *msg, void *arg)
276 {
277 struct nlattr *tb[NL80211_ATTR_MAX + 1];
278 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
279 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
280 char mac_addr[20], state_name[10], dev[20];
281 struct nl80211_sta_flag_update *sta_flags;
282 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
283 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
284 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
285 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
286 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
287 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
288 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
289 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
290 [NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64},
291 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
292 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
293 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
294 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
295 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
296 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
297 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
298 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
299 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
300 [NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32},
301 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64},
302 [NL80211_STA_INFO_STA_FLAGS] =
303 { .minlen = sizeof(struct nl80211_sta_flag_update) },
304 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32},
305 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32},
306 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32},
307 [NL80211_STA_INFO_CHAIN_SIGNAL] = { .type = NLA_NESTED },
308 [NL80211_STA_INFO_CHAIN_SIGNAL_AVG] = { .type = NLA_NESTED },
309 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
310 [NL80211_STA_INFO_BSS_PARAM] = { .type = NLA_NESTED },
311 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
312 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
313 [NL80211_STA_INFO_ACK_SIGNAL] = {.type = NLA_U8 },
314 [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_U8 },
315 };
316 char *chain;
317
318 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
319 genlmsg_attrlen(gnlh, 0), NULL);
320
321 /*
322 * TODO: validate the interface and mac address!
323 * Otherwise, there's a race condition as soon as
324 * the kernel starts sending station notifications.
325 */
326
327 if (!tb[NL80211_ATTR_STA_INFO]) {
328 fprintf(stderr, "sta stats missing!\n");
329 return NL_SKIP;
330 }
331 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
332 tb[NL80211_ATTR_STA_INFO],
333 stats_policy)) {
334 fprintf(stderr, "failed to parse nested attributes!\n");
335 return NL_SKIP;
336 }
337
338 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
339 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
340 printf("Station %s (on %s)", mac_addr, dev);
341
342 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
343 printf("\n\tinactive time:\t%u ms",
344 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
345 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
346 printf("\n\trx bytes:\t%llu",
347 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]));
348 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
349 printf("\n\trx bytes:\t%u",
350 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
351 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
352 printf("\n\trx packets:\t%u",
353 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
354 if (sinfo[NL80211_STA_INFO_TX_BYTES64])
355 printf("\n\ttx bytes:\t%llu",
356 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]));
357 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
358 printf("\n\ttx bytes:\t%u",
359 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
360 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
361 printf("\n\ttx packets:\t%u",
362 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
363 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
364 printf("\n\ttx retries:\t%u",
365 nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
366 if (sinfo[NL80211_STA_INFO_TX_FAILED])
367 printf("\n\ttx failed:\t%u",
368 nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
369 if (sinfo[NL80211_STA_INFO_BEACON_LOSS])
370 printf("\n\tbeacon loss:\t%u",
371 nla_get_u32(sinfo[NL80211_STA_INFO_BEACON_LOSS]));
372 if (sinfo[NL80211_STA_INFO_BEACON_RX])
373 printf("\n\tbeacon rx:\t%llu",
374 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_BEACON_RX]));
375 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
376 printf("\n\trx drop misc:\t%llu",
377 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]));
378
379 chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL]);
380 if (sinfo[NL80211_STA_INFO_SIGNAL])
381 printf("\n\tsignal: \t%d %sdBm",
382 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]),
383 chain);
384
385 chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL_AVG]);
386 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
387 printf("\n\tsignal avg:\t%d %sdBm",
388 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]),
389 chain);
390
391 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
392 printf("\n\tbeacon signal avg:\t%d dBm",
393 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]));
394 if (sinfo[NL80211_STA_INFO_T_OFFSET])
395 printf("\n\tToffset:\t%llu us",
396 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
397
398 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
399 char buf[100];
400
401 parse_bitrate(sinfo[NL80211_STA_INFO_TX_BITRATE], buf, sizeof(buf));
402 printf("\n\ttx bitrate:\t%s", buf);
403 }
404
405 if (sinfo[NL80211_STA_INFO_TX_DURATION])
406 printf("\n\ttx duration:\t%lld us",
407 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_DURATION]));
408
409 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
410 char buf[100];
411
412 parse_bitrate(sinfo[NL80211_STA_INFO_RX_BITRATE], buf, sizeof(buf));
413 printf("\n\trx bitrate:\t%s", buf);
414 }
415
416 if (sinfo[NL80211_STA_INFO_RX_DURATION])
417 printf("\n\trx duration:\t%lld us",
418 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DURATION]));
419
420 if (sinfo[NL80211_STA_INFO_ACK_SIGNAL])
421 printf("\n\tlast ack signal:%d dBm",
422 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL]));
423
424 if (sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG])
425 printf("\n\tavg ack signal:\t%d dBm",
426 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG]));
427
428 if (sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]) {
429 printf("\n\tairtime weight: %d", nla_get_u16(sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]));
430 }
431
432 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) {
433 uint32_t thr;
434
435 thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
436 /* convert in Mbps but scale by 1000 to save kbps units */
437 thr = thr * 1000 / 1024;
438
439 printf("\n\texpected throughput:\t%u.%uMbps",
440 thr / 1000, thr % 1000);
441 }
442
443 if (sinfo[NL80211_STA_INFO_LLID])
444 printf("\n\tmesh llid:\t%d",
445 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
446 if (sinfo[NL80211_STA_INFO_PLID])
447 printf("\n\tmesh plid:\t%d",
448 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
449 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
450 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
451 case LISTEN:
452 strcpy(state_name, "LISTEN");
453 break;
454 case OPN_SNT:
455 strcpy(state_name, "OPN_SNT");
456 break;
457 case OPN_RCVD:
458 strcpy(state_name, "OPN_RCVD");
459 break;
460 case CNF_RCVD:
461 strcpy(state_name, "CNF_RCVD");
462 break;
463 case ESTAB:
464 strcpy(state_name, "ESTAB");
465 break;
466 case HOLDING:
467 strcpy(state_name, "HOLDING");
468 break;
469 case BLOCKED:
470 strcpy(state_name, "BLOCKED");
471 break;
472 default:
473 strcpy(state_name, "UNKNOWN");
474 break;
475 }
476 printf("\n\tmesh plink:\t%s", state_name);
477 }
478 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) {
479 printf("\n\tmesh local PS mode:\t");
480 print_power_mode(sinfo[NL80211_STA_INFO_LOCAL_PM]);
481 }
482 if (sinfo[NL80211_STA_INFO_PEER_PM]) {
483 printf("\n\tmesh peer PS mode:\t");
484 print_power_mode(sinfo[NL80211_STA_INFO_PEER_PM]);
485 }
486 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) {
487 printf("\n\tmesh non-peer PS mode:\t");
488 print_power_mode(sinfo[NL80211_STA_INFO_NONPEER_PM]);
489 }
490
491 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) {
492 sta_flags = (struct nl80211_sta_flag_update *)
493 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
494
495 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
496 printf("\n\tauthorized:\t");
497 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
498 printf("yes");
499 else
500 printf("no");
501 }
502
503 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) {
504 printf("\n\tauthenticated:\t");
505 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
506 printf("yes");
507 else
508 printf("no");
509 }
510
511 if (sta_flags->mask & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
512 printf("\n\tassociated:\t");
513 if (sta_flags->set & BIT(NL80211_STA_FLAG_ASSOCIATED))
514 printf("yes");
515 else
516 printf("no");
517 }
518
519 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
520 printf("\n\tpreamble:\t");
521 if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
522 printf("short");
523 else
524 printf("long");
525 }
526
527 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) {
528 printf("\n\tWMM/WME:\t");
529 if (sta_flags->set & BIT(NL80211_STA_FLAG_WME))
530 printf("yes");
531 else
532 printf("no");
533 }
534
535 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) {
536 printf("\n\tMFP:\t\t");
537 if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
538 printf("yes");
539 else
540 printf("no");
541 }
542
543 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
544 printf("\n\tTDLS peer:\t");
545 if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
546 printf("yes");
547 else
548 printf("no");
549 }
550 }
551
552 if (sinfo[NL80211_STA_INFO_TID_STATS] && arg != NULL &&
553 !strcmp((char *)arg, "-v"))
554 parse_tid_stats(sinfo[NL80211_STA_INFO_TID_STATS]);
555 if (sinfo[NL80211_STA_INFO_BSS_PARAM])
556 parse_bss_param(sinfo[NL80211_STA_INFO_BSS_PARAM]);
557 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
558 printf("\n\tconnected time:\t%u seconds",
559 nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]));
560
561 printf("\n");
562 return NL_SKIP;
563 }
564
565 static int handle_station_get(struct nl80211_state *state,
566 struct nl_msg *msg,
567 int argc, char **argv,
568 enum id_input id)
569 {
570 unsigned char mac_addr[ETH_ALEN];
571
572 if (argc < 1)
573 return 1;
574
575 if (mac_addr_a2n(mac_addr, argv[0])) {
576 fprintf(stderr, "invalid mac address\n");
577 return 2;
578 }
579
580 argc--;
581 argv++;
582
583 if (argc)
584 return 1;
585
586 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
587
588 register_handler(print_sta_handler, NULL);
589
590 return 0;
591 nla_put_failure:
592 return -ENOBUFS;
593 }
594 COMMAND(station, get, "<MAC address>",
595 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
596 "Get information for a specific station.");
597
598 static int handle_station_del(struct nl80211_state *state,
599 struct nl_msg *msg,
600 int argc, char **argv,
601 enum id_input id)
602 {
603 char *end;
604 unsigned char mac_addr[ETH_ALEN];
605 int subtype;
606 int reason_code;
607
608 if (argc < 1)
609 return 1;
610
611 if (mac_addr_a2n(mac_addr, argv[0])) {
612 fprintf(stderr, "invalid mac address\n");
613 return 2;
614 }
615
616 argc--;
617 argv++;
618 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
619
620 if (argc > 1 && strcmp(argv[0], "subtype") == 0) {
621 argv++;
622 argc--;
623
624 subtype = strtod(argv[0], &end);
625 if (*end != '\0')
626 return 1;
627
628 NLA_PUT_U8(msg, NL80211_ATTR_MGMT_SUBTYPE, subtype);
629 argv++;
630 argc--;
631 }
632
633 if (argc > 1 && strcmp(argv[0], "reason-code") == 0) {
634 argv++;
635 argc--;
636
637 reason_code = strtod(argv[0], &end);
638 if (*end != '\0')
639 return 1;
640
641 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
642 argv++;
643 argc--;
644 }
645
646 if (argc)
647 return 1;
648
649 register_handler(print_sta_handler, NULL);
650
651 return 0;
652 nla_put_failure:
653 return -ENOBUFS;
654 }
655 COMMAND(station, del, "<MAC address> [subtype <subtype>] [reason-code <code>]",
656 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_del,
657 "Remove the given station entry (use with caution!)\n"
658 "Example subtype values: 0xA (disassociation), 0xC (deauthentication)");
659
660 static const struct cmd *station_set_plink;
661 static const struct cmd *station_set_vlan;
662 static const struct cmd *station_set_mesh_power_mode;
663 static const struct cmd *station_set_airtime_weight;
664
665 static const struct cmd *select_station_cmd(int argc, char **argv)
666 {
667 if (argc < 2)
668 return NULL;
669 if (strcmp(argv[1], "plink_action") == 0)
670 return station_set_plink;
671 if (strcmp(argv[1], "vlan") == 0)
672 return station_set_vlan;
673 if (strcmp(argv[1], "mesh_power_mode") == 0)
674 return station_set_mesh_power_mode;
675 if (strcmp(argv[1], "airtime_weight") == 0)
676 return station_set_airtime_weight;
677 return NULL;
678 }
679
680 static int handle_station_set_plink(struct nl80211_state *state,
681 struct nl_msg *msg,
682 int argc, char **argv,
683 enum id_input id)
684 {
685 unsigned char plink_action;
686 unsigned char mac_addr[ETH_ALEN];
687
688 if (argc < 3)
689 return 1;
690
691 if (mac_addr_a2n(mac_addr, argv[0])) {
692 fprintf(stderr, "invalid mac address\n");
693 return 2;
694 }
695 argc--;
696 argv++;
697
698 if (strcmp("plink_action", argv[0]) != 0)
699 return 1;
700 argc--;
701 argv++;
702
703 if (strcmp("open", argv[0]) == 0)
704 plink_action = NL80211_PLINK_ACTION_OPEN;
705 else if (strcmp("block", argv[0]) == 0)
706 plink_action = NL80211_PLINK_ACTION_BLOCK;
707 else {
708 fprintf(stderr, "plink action not supported\n");
709 return 2;
710 }
711 argc--;
712 argv++;
713
714 if (argc)
715 return 1;
716
717 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
718 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
719
720 return 0;
721 nla_put_failure:
722 return -ENOBUFS;
723 }
724 COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
725 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
726 "Set mesh peer link action for this station (peer).",
727 select_station_cmd, station_set_plink);
728
729 static int handle_station_set_vlan(struct nl80211_state *state,
730 struct nl_msg *msg,
731 int argc, char **argv,
732 enum id_input id)
733 {
734 unsigned char mac_addr[ETH_ALEN];
735 unsigned long sta_vlan = 0;
736 char *err = NULL;
737
738 if (argc < 3)
739 return 1;
740
741 if (mac_addr_a2n(mac_addr, argv[0])) {
742 fprintf(stderr, "invalid mac address\n");
743 return 2;
744 }
745 argc--;
746 argv++;
747
748 if (strcmp("vlan", argv[0]) != 0)
749 return 1;
750 argc--;
751 argv++;
752
753 sta_vlan = strtoul(argv[0], &err, 0);
754 if (err && *err) {
755 fprintf(stderr, "invalid vlan id\n");
756 return 2;
757 }
758 argc--;
759 argv++;
760
761 if (argc)
762 return 1;
763
764 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
765 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan);
766
767 return 0;
768 nla_put_failure:
769 return -ENOBUFS;
770 }
771 COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
772 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
773 "Set an AP VLAN for this station.",
774 select_station_cmd, station_set_vlan);
775
776 static int handle_station_set_mesh_power_mode(struct nl80211_state *state,
777 struct nl_msg *msg,
778 int argc, char **argv,
779 enum id_input id)
780 {
781 unsigned char mesh_power_mode;
782 unsigned char mac_addr[ETH_ALEN];
783
784 if (argc < 3)
785 return 1;
786
787 if (mac_addr_a2n(mac_addr, argv[0])) {
788 fprintf(stderr, "invalid mac address\n");
789 return 2;
790 }
791 argc--;
792 argv++;
793
794 if (strcmp("mesh_power_mode", argv[0]) != 0)
795 return 1;
796 argc--;
797 argv++;
798
799 if (strcmp("active", argv[0]) == 0)
800 mesh_power_mode = NL80211_MESH_POWER_ACTIVE;
801 else if (strcmp("light", argv[0]) == 0)
802 mesh_power_mode = NL80211_MESH_POWER_LIGHT_SLEEP;
803 else if (strcmp("deep", argv[0]) == 0)
804 mesh_power_mode = NL80211_MESH_POWER_DEEP_SLEEP;
805 else {
806 fprintf(stderr, "unknown mesh power mode\n");
807 return 2;
808 }
809 argc--;
810 argv++;
811
812 if (argc)
813 return 1;
814
815 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
816 NLA_PUT_U32(msg, NL80211_ATTR_LOCAL_MESH_POWER_MODE, mesh_power_mode);
817
818 return 0;
819 nla_put_failure:
820 return -ENOBUFS;
821 }
822 COMMAND_ALIAS(station, set, "<MAC address> mesh_power_mode "
823 "<active|light|deep>", NL80211_CMD_SET_STATION, 0, CIB_NETDEV,
824 handle_station_set_mesh_power_mode,
825 "Set link-specific mesh power mode for this station",
826 select_station_cmd, station_set_mesh_power_mode);
827
828 static int handle_station_set_airtime_weight(struct nl80211_state *state,
829 struct nl_msg *msg,
830 int argc, char **argv,
831 enum id_input id)
832 {
833 unsigned char mac_addr[ETH_ALEN];
834 unsigned long airtime_weight = 0;
835 char *err = NULL;
836
837 if (argc < 3)
838 return 1;
839
840 if (mac_addr_a2n(mac_addr, argv[0])) {
841 fprintf(stderr, "invalid mac address\n");
842 return 2;
843 }
844 argc--;
845 argv++;
846
847 if (strcmp("airtime_weight", argv[0]) != 0)
848 return 1;
849 argc--;
850 argv++;
851
852 airtime_weight = strtoul(argv[0], &err, 0);
853 if (err && *err) {
854 fprintf(stderr, "invalid airtime weight\n");
855 return 2;
856 }
857 argc--;
858 argv++;
859
860 if (argc)
861 return 1;
862
863 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
864 NLA_PUT_U16(msg, NL80211_ATTR_AIRTIME_WEIGHT, airtime_weight);
865
866 return 0;
867 nla_put_failure:
868 return -ENOBUFS;
869
870 }
871 COMMAND_ALIAS(station, set, "<MAC address> airtime_weight <weight>",
872 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_airtime_weight,
873 "Set airtime weight for this station.",
874 select_station_cmd, station_set_airtime_weight);
875
876 static int handle_station_dump(struct nl80211_state *state,
877 struct nl_msg *msg,
878 int argc, char **argv,
879 enum id_input id)
880 {
881 register_handler(print_sta_handler, *argv);
882 return 0;
883 }
884 COMMAND(station, dump, "[-v]",
885 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
886 "List all stations known, e.g. the AP on managed interfaces");