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