]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
update nl80211.h
[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 #include <time.h>
11
12 #include "nl80211.h"
13 #include "iw.h"
14
15 SECTION(station);
16
17 enum plink_state {
18 LISTEN,
19 OPN_SNT,
20 OPN_RCVD,
21 CNF_RCVD,
22 ESTAB,
23 HOLDING,
24 BLOCKED
25 };
26
27 static 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 }
46
47 int 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 }
103
104 static void parse_tid_stats(struct nlattr *tid_stats_attr)
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 },
112 [NL80211_TID_STATS_TXQ_STATS] = { .type = NLA_NESTED },
113 };
114 int rem, i = 0;
115 char txqbuf[2000] = {}, *pos = txqbuf;
116 int buflen = sizeof(txqbuf), foundtxq = 0;
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 }
125 printf("\n\t\t%d", i);
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));
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++;
145 }
146
147 if (foundtxq)
148 printf("\n\tTXQs:%s", txqbuf);
149 }
150
151 static void parse_bss_param(struct nlattr *bss_param_attr)
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
199 void parse_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen)
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);
225 else
226 pos += snprintf(pos, buflen - (pos - buf), "(unknown)");
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_320_MHZ_WIDTH])
243 pos += snprintf(pos, buflen - (pos - buf), " 320MHz");
244 if (rinfo[NL80211_RATE_INFO_1_MHZ_WIDTH])
245 pos += snprintf(pos, buflen - (pos - buf), " 1MHz");
246 if (rinfo[NL80211_RATE_INFO_2_MHZ_WIDTH])
247 pos += snprintf(pos, buflen - (pos - buf), " 2MHz");
248 if (rinfo[NL80211_RATE_INFO_4_MHZ_WIDTH])
249 pos += snprintf(pos, buflen - (pos - buf), " 4MHz");
250 if (rinfo[NL80211_RATE_INFO_8_MHZ_WIDTH])
251 pos += snprintf(pos, buflen - (pos - buf), " 8MHz");
252 if (rinfo[NL80211_RATE_INFO_16_MHZ_WIDTH])
253 pos += snprintf(pos, buflen - (pos - buf), " 16MHz");
254 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
255 pos += snprintf(pos, buflen - (pos - buf), " short GI");
256 if (rinfo[NL80211_RATE_INFO_VHT_NSS])
257 pos += snprintf(pos, buflen - (pos - buf),
258 " VHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]));
259 if (rinfo[NL80211_RATE_INFO_HE_MCS])
260 pos += snprintf(pos, buflen - (pos - buf),
261 " HE-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_MCS]));
262 if (rinfo[NL80211_RATE_INFO_HE_NSS])
263 pos += snprintf(pos, buflen - (pos - buf),
264 " HE-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_NSS]));
265 if (rinfo[NL80211_RATE_INFO_HE_GI])
266 pos += snprintf(pos, buflen - (pos - buf),
267 " HE-GI %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_GI]));
268 if (rinfo[NL80211_RATE_INFO_HE_DCM])
269 pos += snprintf(pos, buflen - (pos - buf),
270 " HE-DCM %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_DCM]));
271 if (rinfo[NL80211_RATE_INFO_HE_RU_ALLOC])
272 pos += snprintf(pos, buflen - (pos - buf),
273 " HE-RU-ALLOC %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_RU_ALLOC]));
274 if (rinfo[NL80211_RATE_INFO_EHT_MCS])
275 pos += snprintf(pos, buflen - (pos - buf),
276 " EHT-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_EHT_MCS]));
277 if (rinfo[NL80211_RATE_INFO_EHT_NSS])
278 pos += snprintf(pos, buflen - (pos - buf),
279 " EHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_EHT_NSS]));
280 if (rinfo[NL80211_RATE_INFO_EHT_GI])
281 pos += snprintf(pos, buflen - (pos - buf),
282 " EHT-GI %d", nla_get_u8(rinfo[NL80211_RATE_INFO_EHT_GI]));
283 if (rinfo[NL80211_RATE_INFO_EHT_RU_ALLOC])
284 pos += snprintf(pos, buflen - (pos - buf),
285 " EHT-RU-ALLOC %d", nla_get_u8(rinfo[NL80211_RATE_INFO_EHT_RU_ALLOC]));
286 }
287
288 static char *get_chain_signal(struct nlattr *attr_list)
289 {
290 struct nlattr *attr;
291 static char buf[64];
292 char *cur = buf;
293 int i = 0, rem;
294 const char *prefix;
295
296 if (!attr_list)
297 return "";
298
299 nla_for_each_nested(attr, attr_list, rem) {
300 if (i++ > 0)
301 prefix = ", ";
302 else
303 prefix = "[";
304
305 cur += snprintf(cur, sizeof(buf) - (cur - buf), "%s%d", prefix,
306 (int8_t) nla_get_u8(attr));
307 }
308
309 if (i)
310 snprintf(cur, sizeof(buf) - (cur - buf), "] ");
311
312 return buf;
313 }
314
315 static int print_sta_handler(struct nl_msg *msg, void *arg)
316 {
317 struct nlattr *tb[NL80211_ATTR_MAX + 1];
318 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
319 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
320 char mac_addr[20], state_name[10], dev[20];
321 struct nl80211_sta_flag_update *sta_flags;
322 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
323 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
324 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
325 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
326 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
327 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
328 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
329 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
330 [NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64},
331 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
332 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
333 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
334 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
335 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
336 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
337 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
338 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
339 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
340 [NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32},
341 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64},
342 [NL80211_STA_INFO_STA_FLAGS] =
343 { .minlen = sizeof(struct nl80211_sta_flag_update) },
344 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32},
345 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32},
346 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32},
347 [NL80211_STA_INFO_CHAIN_SIGNAL] = { .type = NLA_NESTED },
348 [NL80211_STA_INFO_CHAIN_SIGNAL_AVG] = { .type = NLA_NESTED },
349 [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
350 [NL80211_STA_INFO_BSS_PARAM] = { .type = NLA_NESTED },
351 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
352 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
353 [NL80211_STA_INFO_ACK_SIGNAL] = {.type = NLA_U8 },
354 [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_U8 },
355 [NL80211_STA_INFO_AIRTIME_LINK_METRIC] = { .type = NLA_U32 },
356 [NL80211_STA_INFO_CONNECTED_TO_AS] = { .type = NLA_U8 },
357 [NL80211_STA_INFO_CONNECTED_TO_GATE] = { .type = NLA_U8 },
358 };
359 char *chain;
360 struct timeval now;
361 unsigned long long now_ms;
362
363 gettimeofday(&now, NULL);
364 now_ms = now.tv_sec * 1000ULL;
365 now_ms += (now.tv_usec / 1000);
366
367 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
368 genlmsg_attrlen(gnlh, 0), NULL);
369
370 /*
371 * TODO: validate the interface and mac address!
372 * Otherwise, there's a race condition as soon as
373 * the kernel starts sending station notifications.
374 */
375
376 if (!tb[NL80211_ATTR_STA_INFO]) {
377 fprintf(stderr, "sta stats missing!\n");
378 return NL_SKIP;
379 }
380 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
381 tb[NL80211_ATTR_STA_INFO],
382 stats_policy)) {
383 fprintf(stderr, "failed to parse nested attributes!\n");
384 return NL_SKIP;
385 }
386
387 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
388 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
389 printf("Station %s (on %s)", mac_addr, dev);
390
391 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
392 printf("\n\tinactive time:\t%u ms",
393 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
394 if (sinfo[NL80211_STA_INFO_RX_BYTES64])
395 printf("\n\trx bytes:\t%llu",
396 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]));
397 else if (sinfo[NL80211_STA_INFO_RX_BYTES])
398 printf("\n\trx bytes:\t%u",
399 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
400 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
401 printf("\n\trx packets:\t%u",
402 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
403 if (sinfo[NL80211_STA_INFO_TX_BYTES64])
404 printf("\n\ttx bytes:\t%llu",
405 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]));
406 else if (sinfo[NL80211_STA_INFO_TX_BYTES])
407 printf("\n\ttx bytes:\t%u",
408 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
409 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
410 printf("\n\ttx packets:\t%u",
411 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
412 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
413 printf("\n\ttx retries:\t%u",
414 nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
415 if (sinfo[NL80211_STA_INFO_TX_FAILED])
416 printf("\n\ttx failed:\t%u",
417 nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
418 if (sinfo[NL80211_STA_INFO_BEACON_LOSS])
419 printf("\n\tbeacon loss:\t%u",
420 nla_get_u32(sinfo[NL80211_STA_INFO_BEACON_LOSS]));
421 if (sinfo[NL80211_STA_INFO_BEACON_RX])
422 printf("\n\tbeacon rx:\t%llu",
423 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_BEACON_RX]));
424 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
425 printf("\n\trx drop misc:\t%llu",
426 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]));
427
428 chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL]);
429 if (sinfo[NL80211_STA_INFO_SIGNAL])
430 printf("\n\tsignal: \t%d %sdBm",
431 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]),
432 chain);
433
434 chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL_AVG]);
435 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
436 printf("\n\tsignal avg:\t%d %sdBm",
437 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]),
438 chain);
439
440 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
441 printf("\n\tbeacon signal avg:\t%d dBm",
442 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]));
443 if (sinfo[NL80211_STA_INFO_T_OFFSET])
444 printf("\n\tToffset:\t%llu us",
445 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
446
447 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
448 char buf[100];
449
450 parse_bitrate(sinfo[NL80211_STA_INFO_TX_BITRATE], buf, sizeof(buf));
451 printf("\n\ttx bitrate:\t%s", buf);
452 }
453
454 if (sinfo[NL80211_STA_INFO_TX_DURATION])
455 printf("\n\ttx duration:\t%lld us",
456 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_DURATION]));
457
458 if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
459 char buf[100];
460
461 parse_bitrate(sinfo[NL80211_STA_INFO_RX_BITRATE], buf, sizeof(buf));
462 printf("\n\trx bitrate:\t%s", buf);
463 }
464
465 if (sinfo[NL80211_STA_INFO_RX_DURATION])
466 printf("\n\trx duration:\t%lld us",
467 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DURATION]));
468
469 if (sinfo[NL80211_STA_INFO_ACK_SIGNAL])
470 printf("\n\tlast ack signal:%d dBm",
471 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL]));
472
473 if (sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG])
474 printf("\n\tavg ack signal:\t%d dBm",
475 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG]));
476
477 if (sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]) {
478 printf("\n\tairtime weight: %d", nla_get_u16(sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]));
479 }
480
481 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) {
482 uint32_t thr;
483
484 thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
485 /* convert in Mbps but scale by 1000 to save kbps units */
486 thr = thr * 1000 / 1024;
487
488 printf("\n\texpected throughput:\t%u.%uMbps",
489 thr / 1000, thr % 1000);
490 }
491
492 if (sinfo[NL80211_STA_INFO_LLID])
493 printf("\n\tmesh llid:\t%d",
494 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
495 if (sinfo[NL80211_STA_INFO_PLID])
496 printf("\n\tmesh plid:\t%d",
497 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
498 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
499 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
500 case LISTEN:
501 strcpy(state_name, "LISTEN");
502 break;
503 case OPN_SNT:
504 strcpy(state_name, "OPN_SNT");
505 break;
506 case OPN_RCVD:
507 strcpy(state_name, "OPN_RCVD");
508 break;
509 case CNF_RCVD:
510 strcpy(state_name, "CNF_RCVD");
511 break;
512 case ESTAB:
513 strcpy(state_name, "ESTAB");
514 break;
515 case HOLDING:
516 strcpy(state_name, "HOLDING");
517 break;
518 case BLOCKED:
519 strcpy(state_name, "BLOCKED");
520 break;
521 default:
522 strcpy(state_name, "UNKNOWN");
523 break;
524 }
525 printf("\n\tmesh plink:\t%s", state_name);
526 }
527 if (sinfo[NL80211_STA_INFO_AIRTIME_LINK_METRIC])
528 printf("\n\tmesh airtime link metric: %d",
529 nla_get_u32(sinfo[NL80211_STA_INFO_AIRTIME_LINK_METRIC]));
530 if (sinfo[NL80211_STA_INFO_CONNECTED_TO_GATE])
531 printf("\n\tmesh connected to gate:\t%s",
532 nla_get_u8(sinfo[NL80211_STA_INFO_CONNECTED_TO_GATE]) ?
533 "yes" : "no");
534 if (sinfo[NL80211_STA_INFO_CONNECTED_TO_AS])
535 printf("\n\tmesh connected to auth server:\t%s",
536 nla_get_u8(sinfo[NL80211_STA_INFO_CONNECTED_TO_AS]) ?
537 "yes" : "no");
538
539 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) {
540 printf("\n\tmesh local PS mode:\t");
541 print_power_mode(sinfo[NL80211_STA_INFO_LOCAL_PM]);
542 }
543 if (sinfo[NL80211_STA_INFO_PEER_PM]) {
544 printf("\n\tmesh peer PS mode:\t");
545 print_power_mode(sinfo[NL80211_STA_INFO_PEER_PM]);
546 }
547 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) {
548 printf("\n\tmesh non-peer PS mode:\t");
549 print_power_mode(sinfo[NL80211_STA_INFO_NONPEER_PM]);
550 }
551
552 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) {
553 sta_flags = (struct nl80211_sta_flag_update *)
554 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
555
556 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
557 printf("\n\tauthorized:\t");
558 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
559 printf("yes");
560 else
561 printf("no");
562 }
563
564 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) {
565 printf("\n\tauthenticated:\t");
566 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
567 printf("yes");
568 else
569 printf("no");
570 }
571
572 if (sta_flags->mask & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
573 printf("\n\tassociated:\t");
574 if (sta_flags->set & BIT(NL80211_STA_FLAG_ASSOCIATED))
575 printf("yes");
576 else
577 printf("no");
578 }
579
580 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
581 printf("\n\tpreamble:\t");
582 if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
583 printf("short");
584 else
585 printf("long");
586 }
587
588 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) {
589 printf("\n\tWMM/WME:\t");
590 if (sta_flags->set & BIT(NL80211_STA_FLAG_WME))
591 printf("yes");
592 else
593 printf("no");
594 }
595
596 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) {
597 printf("\n\tMFP:\t\t");
598 if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
599 printf("yes");
600 else
601 printf("no");
602 }
603
604 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
605 printf("\n\tTDLS peer:\t");
606 if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
607 printf("yes");
608 else
609 printf("no");
610 }
611 }
612
613 if (sinfo[NL80211_STA_INFO_TID_STATS] && arg != NULL &&
614 !strcmp((char *)arg, "-v"))
615 parse_tid_stats(sinfo[NL80211_STA_INFO_TID_STATS]);
616 if (sinfo[NL80211_STA_INFO_BSS_PARAM])
617 parse_bss_param(sinfo[NL80211_STA_INFO_BSS_PARAM]);
618 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
619 printf("\n\tconnected time:\t%u seconds",
620 nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]));
621 if (sinfo[NL80211_STA_INFO_ASSOC_AT_BOOTTIME]) {
622 unsigned long long bt;
623 struct timespec now_ts;
624 unsigned long long boot_ns;
625 unsigned long long assoc_at_ms;
626
627 clock_gettime(CLOCK_BOOTTIME, &now_ts);
628 boot_ns = now_ts.tv_sec * 1000000000ULL;
629 boot_ns += now_ts.tv_nsec;
630
631 bt = (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_ASSOC_AT_BOOTTIME]);
632 printf("\n\tassociated at [boottime]:\t%llu.%.3llus",
633 bt/1000000000, (bt%1000000000)/1000000);
634 assoc_at_ms = now_ms - ((boot_ns - bt) / 1000000);
635 printf("\n\tassociated at:\t%llu ms", assoc_at_ms);
636 }
637
638 printf("\n\tcurrent time:\t%llu ms\n", now_ms);
639 return NL_SKIP;
640 }
641
642 static int handle_station_get(struct nl80211_state *state,
643 struct nl_msg *msg,
644 int argc, char **argv,
645 enum id_input id)
646 {
647 unsigned char mac_addr[ETH_ALEN];
648
649 if (argc < 1)
650 return 1;
651
652 if (mac_addr_a2n(mac_addr, argv[0])) {
653 fprintf(stderr, "invalid mac address\n");
654 return 2;
655 }
656
657 argc--;
658 argv++;
659
660 if (argc)
661 return 1;
662
663 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
664
665 register_handler(print_sta_handler, NULL);
666
667 return 0;
668 nla_put_failure:
669 return -ENOBUFS;
670 }
671 COMMAND(station, get, "<MAC address>",
672 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
673 "Get information for a specific station.");
674
675 static int handle_station_del(struct nl80211_state *state,
676 struct nl_msg *msg,
677 int argc, char **argv,
678 enum id_input id)
679 {
680 char *end;
681 unsigned char mac_addr[ETH_ALEN];
682 int subtype;
683 int reason_code;
684
685 if (argc < 1)
686 return 1;
687
688 if (mac_addr_a2n(mac_addr, argv[0])) {
689 fprintf(stderr, "invalid mac address\n");
690 return 2;
691 }
692
693 argc--;
694 argv++;
695 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
696
697 if (argc > 1 && strcmp(argv[0], "subtype") == 0) {
698 argv++;
699 argc--;
700
701 subtype = strtod(argv[0], &end);
702 if (*end != '\0')
703 return 1;
704
705 NLA_PUT_U8(msg, NL80211_ATTR_MGMT_SUBTYPE, subtype);
706 argv++;
707 argc--;
708 }
709
710 if (argc > 1 && strcmp(argv[0], "reason-code") == 0) {
711 argv++;
712 argc--;
713
714 reason_code = strtod(argv[0], &end);
715 if (*end != '\0')
716 return 1;
717
718 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
719 argv++;
720 argc--;
721 }
722
723 if (argc)
724 return 1;
725
726 register_handler(print_sta_handler, NULL);
727
728 return 0;
729 nla_put_failure:
730 return -ENOBUFS;
731 }
732 COMMAND(station, del, "<MAC address> [subtype <subtype>] [reason-code <code>]",
733 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_del,
734 "Remove the given station entry (use with caution!)\n"
735 "Example subtype values: 0xA (disassociation), 0xC (deauthentication)");
736
737 static const struct cmd *station_set_plink;
738 static const struct cmd *station_set_vlan;
739 static const struct cmd *station_set_mesh_power_mode;
740 static const struct cmd *station_set_airtime_weight;
741 static const struct cmd *station_set_txpwr;
742
743 static const struct cmd *select_station_cmd(int argc, char **argv)
744 {
745 if (argc < 2)
746 return NULL;
747 if (strcmp(argv[1], "plink_action") == 0)
748 return station_set_plink;
749 if (strcmp(argv[1], "vlan") == 0)
750 return station_set_vlan;
751 if (strcmp(argv[1], "mesh_power_mode") == 0)
752 return station_set_mesh_power_mode;
753 if (strcmp(argv[1], "airtime_weight") == 0)
754 return station_set_airtime_weight;
755 if (strcmp(argv[1], "txpwr") == 0)
756 return station_set_txpwr;
757 return NULL;
758 }
759
760 static int handle_station_set_plink(struct nl80211_state *state,
761 struct nl_msg *msg,
762 int argc, char **argv,
763 enum id_input id)
764 {
765 unsigned char plink_action;
766 unsigned char mac_addr[ETH_ALEN];
767
768 if (argc < 3)
769 return 1;
770
771 if (mac_addr_a2n(mac_addr, argv[0])) {
772 fprintf(stderr, "invalid mac address\n");
773 return 2;
774 }
775 argc--;
776 argv++;
777
778 if (strcmp("plink_action", argv[0]) != 0)
779 return 1;
780 argc--;
781 argv++;
782
783 if (strcmp("open", argv[0]) == 0)
784 plink_action = NL80211_PLINK_ACTION_OPEN;
785 else if (strcmp("block", argv[0]) == 0)
786 plink_action = NL80211_PLINK_ACTION_BLOCK;
787 else {
788 fprintf(stderr, "plink action not supported\n");
789 return 2;
790 }
791 argc--;
792 argv++;
793
794 if (argc)
795 return 1;
796
797 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
798 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
799
800 return 0;
801 nla_put_failure:
802 return -ENOBUFS;
803 }
804 COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
805 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
806 "Set mesh peer link action for this station (peer).",
807 select_station_cmd, station_set_plink);
808
809 static int handle_station_set_vlan(struct nl80211_state *state,
810 struct nl_msg *msg,
811 int argc, char **argv,
812 enum id_input id)
813 {
814 unsigned char mac_addr[ETH_ALEN];
815 unsigned long sta_vlan = 0;
816 char *err = NULL;
817
818 if (argc < 3)
819 return 1;
820
821 if (mac_addr_a2n(mac_addr, argv[0])) {
822 fprintf(stderr, "invalid mac address\n");
823 return 2;
824 }
825 argc--;
826 argv++;
827
828 if (strcmp("vlan", argv[0]) != 0)
829 return 1;
830 argc--;
831 argv++;
832
833 sta_vlan = strtoul(argv[0], &err, 0);
834 if (err && *err) {
835 fprintf(stderr, "invalid vlan id\n");
836 return 2;
837 }
838 argc--;
839 argv++;
840
841 if (argc)
842 return 1;
843
844 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
845 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan);
846
847 return 0;
848 nla_put_failure:
849 return -ENOBUFS;
850 }
851 COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
852 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
853 "Set an AP VLAN for this station.",
854 select_station_cmd, station_set_vlan);
855
856 static int handle_station_set_mesh_power_mode(struct nl80211_state *state,
857 struct nl_msg *msg,
858 int argc, char **argv,
859 enum id_input id)
860 {
861 unsigned char mesh_power_mode;
862 unsigned char mac_addr[ETH_ALEN];
863
864 if (argc < 3)
865 return 1;
866
867 if (mac_addr_a2n(mac_addr, argv[0])) {
868 fprintf(stderr, "invalid mac address\n");
869 return 2;
870 }
871 argc--;
872 argv++;
873
874 if (strcmp("mesh_power_mode", argv[0]) != 0)
875 return 1;
876 argc--;
877 argv++;
878
879 if (strcmp("active", argv[0]) == 0)
880 mesh_power_mode = NL80211_MESH_POWER_ACTIVE;
881 else if (strcmp("light", argv[0]) == 0)
882 mesh_power_mode = NL80211_MESH_POWER_LIGHT_SLEEP;
883 else if (strcmp("deep", argv[0]) == 0)
884 mesh_power_mode = NL80211_MESH_POWER_DEEP_SLEEP;
885 else {
886 fprintf(stderr, "unknown mesh power mode\n");
887 return 2;
888 }
889 argc--;
890 argv++;
891
892 if (argc)
893 return 1;
894
895 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
896 NLA_PUT_U32(msg, NL80211_ATTR_LOCAL_MESH_POWER_MODE, mesh_power_mode);
897
898 return 0;
899 nla_put_failure:
900 return -ENOBUFS;
901 }
902 COMMAND_ALIAS(station, set, "<MAC address> mesh_power_mode "
903 "<active|light|deep>", NL80211_CMD_SET_STATION, 0, CIB_NETDEV,
904 handle_station_set_mesh_power_mode,
905 "Set link-specific mesh power mode for this station",
906 select_station_cmd, station_set_mesh_power_mode);
907
908 static int handle_station_set_airtime_weight(struct nl80211_state *state,
909 struct nl_msg *msg,
910 int argc, char **argv,
911 enum id_input id)
912 {
913 unsigned char mac_addr[ETH_ALEN];
914 unsigned long airtime_weight = 0;
915 char *err = NULL;
916
917 if (argc < 3)
918 return 1;
919
920 if (mac_addr_a2n(mac_addr, argv[0])) {
921 fprintf(stderr, "invalid mac address\n");
922 return 2;
923 }
924 argc--;
925 argv++;
926
927 if (strcmp("airtime_weight", argv[0]) != 0)
928 return 1;
929 argc--;
930 argv++;
931
932 airtime_weight = strtoul(argv[0], &err, 0);
933 if (err && *err) {
934 fprintf(stderr, "invalid airtime weight\n");
935 return 2;
936 }
937 argc--;
938 argv++;
939
940 if (argc)
941 return 1;
942
943 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
944 NLA_PUT_U16(msg, NL80211_ATTR_AIRTIME_WEIGHT, airtime_weight);
945
946 return 0;
947 nla_put_failure:
948 return -ENOBUFS;
949
950 }
951 COMMAND_ALIAS(station, set, "<MAC address> airtime_weight <weight>",
952 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_airtime_weight,
953 "Set airtime weight for this station.",
954 select_station_cmd, station_set_airtime_weight);
955
956 static int handle_station_set_txpwr(struct nl80211_state *state,
957 struct nl_msg *msg,
958 int argc, char **argv,
959 enum id_input id)
960 {
961 enum nl80211_tx_power_setting type;
962 unsigned char mac_addr[ETH_ALEN];
963 int sta_txpwr = 0;
964 char *err = NULL;
965
966 if (argc != 3 && argc != 4)
967 return 1;
968
969 if (mac_addr_a2n(mac_addr, argv[0])) {
970 fprintf(stderr, "invalid mac address\n");
971 return 2;
972 }
973
974 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
975 argc--;
976 argv++;
977
978 if (strcmp("txpwr", argv[0]) != 0)
979 return 1;
980 argc--;
981 argv++;
982
983 if (!strcmp(argv[0], "auto"))
984 type = NL80211_TX_POWER_AUTOMATIC;
985 else if (!strcmp(argv[0], "limit"))
986 type = NL80211_TX_POWER_LIMITED;
987 else {
988 printf("Invalid parameter: %s\n", argv[0]);
989 return 2;
990 }
991
992 NLA_PUT_U8(msg, NL80211_ATTR_STA_TX_POWER_SETTING, type);
993
994 if (type != NL80211_TX_POWER_AUTOMATIC) {
995 if (argc != 2) {
996 printf("Missing TX power level argument.\n");
997 return 2;
998 }
999
1000 argc--;
1001 argv++;
1002
1003 sta_txpwr = strtoul(argv[0], &err, 0);
1004 NLA_PUT_U16(msg, NL80211_ATTR_STA_TX_POWER, sta_txpwr);
1005 }
1006
1007 argc--;
1008 argv++;
1009
1010 if (argc)
1011 return 1;
1012
1013 return 0;
1014 nla_put_failure:
1015 return -ENOBUFS;
1016 }
1017 COMMAND_ALIAS(station, set, "<MAC address> txpwr <auto|limit> [<tx power dBm>]",
1018 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_txpwr,
1019 "Set Tx power for this station.",
1020 select_station_cmd, station_set_txpwr);
1021
1022 static int handle_station_dump(struct nl80211_state *state,
1023 struct nl_msg *msg,
1024 int argc, char **argv,
1025 enum id_input id)
1026 {
1027 register_handler(print_sta_handler, *argv);
1028 return 0;
1029 }
1030 COMMAND(station, dump, "[-v]",
1031 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
1032 "List all stations known, e.g. the AP on managed interfaces");