-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiw-scan.c
More file actions
843 lines (734 loc) · 23.1 KB
/
iw-scan.c
File metadata and controls
843 lines (734 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/* davep 20181011 ; learning nl80211 */
//https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/nl80211.h
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <errno.h>
#include <assert.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <linux/nl80211.h>
#include "core.h"
#include "hdump.h"
#include "nlnames.h"
#include "bytebuf.h"
#include "iw-scan.h"
/* iw-4.9 iw.h */
#define BIT(x) (1ULL<<(x))
/* iw-4.9 station.c */
enum plink_state {
LISTEN,
OPN_SNT,
OPN_RCVD,
CNF_RCVD,
ESTAB,
HOLDING,
BLOCKED
};
#if 0
static void peek(struct nl_msg *msg)
{
struct nlmsghdr * nlh;
struct nlattr * attr;
int i, rem;
// curious about how to use nlmsg_for_each_xxx()
// tests/check-attr.c from libnl-3.2.25
printf( "%s\n", __func__);
nlh = nlmsg_hdr(msg);
i = 1;
// int len = NLMSG_LENGTH(nlh);
int len = 1024;
printf("peek len=%d\n", len);
hex_dump("peek", (const unsigned char *)msg, 64);
int hdrlen = 0;
nlmsg_attrdata(nlh, hdrlen);
nlmsg_attrlen(nlh, hdrlen);
nlmsg_for_each_attr(attr, nlh, 0, rem) {
printf("peek type=%d len=%d\n", nla_type(attr), nla_len(attr));
i++;
}
}
#endif
/*
* iw-4.9 station.c
*/
void decode_bss_param(struct nlattr *bss_param_attr)
{
struct nlattr *bss_param_info[NL80211_STA_BSS_PARAM_MAX + 1], *info;
static struct nla_policy bss_poilcy[NL80211_STA_BSS_PARAM_MAX + 1] = {
[NL80211_STA_BSS_PARAM_CTS_PROT] = { .type = NLA_FLAG },
[NL80211_STA_BSS_PARAM_SHORT_PREAMBLE] = { .type = NLA_FLAG },
[NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME] = { .type = NLA_FLAG },
[NL80211_STA_BSS_PARAM_DTIM_PERIOD] = { .type = NLA_U8 },
[NL80211_STA_BSS_PARAM_BEACON_INTERVAL] = { .type = NLA_U16 },
};
if (nla_parse_nested(bss_param_info, NL80211_STA_BSS_PARAM_MAX,
bss_param_attr, bss_poilcy)) {
printf("failed to parse nested bss param attributes!");
}
info = bss_param_info[NL80211_STA_BSS_PARAM_DTIM_PERIOD];
if (info)
printf("\n\tDTIM period:\t%u", nla_get_u8(info));
info = bss_param_info[NL80211_STA_BSS_PARAM_BEACON_INTERVAL];
if (info)
printf("\n\tbeacon interval:%u", nla_get_u16(info));
info = bss_param_info[NL80211_STA_BSS_PARAM_CTS_PROT];
if (info) {
printf("\n\tCTS protection:");
if (nla_get_u16(info))
printf("\tyes");
else
printf("\tno");
}
info = bss_param_info[NL80211_STA_BSS_PARAM_SHORT_PREAMBLE];
if (info) {
printf("\n\tshort preamble:");
if (nla_get_u16(info))
printf("\tyes");
else
printf("\tno");
}
info = bss_param_info[NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME];
if (info) {
printf("\n\tshort slot time:");
if (nla_get_u16(info))
printf("yes");
else
printf("no");
}
}
void decode_tid_stats(struct nlattr *tid_stats_attr)
{
struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1], *tidattr, *info;
static struct nla_policy stats_policy[NL80211_TID_STATS_MAX + 1] = {
[NL80211_TID_STATS_RX_MSDU] = { .type = NLA_U64 },
[NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
[NL80211_TID_STATS_TX_MSDU_RETRIES] = { .type = NLA_U64 },
[NL80211_TID_STATS_TX_MSDU_FAILED] = { .type = NLA_U64 },
};
int rem, i = 0;
printf("\n\tMSDU:\n\t\tTID\trx\ttx\ttx retries\ttx failed");
nla_for_each_nested(tidattr, tid_stats_attr, rem) {
if (nla_parse_nested(stats_info, NL80211_TID_STATS_MAX,
tidattr, stats_policy)) {
printf("failed to parse nested stats attributes!");
return;
}
printf("\n\t\t%d", i++);
info = stats_info[NL80211_TID_STATS_RX_MSDU];
if (info)
printf("\t%llu", (unsigned long long)nla_get_u64(info));
info = stats_info[NL80211_TID_STATS_TX_MSDU];
if (info)
printf("\t%llu", (unsigned long long)nla_get_u64(info));
info = stats_info[NL80211_TID_STATS_TX_MSDU_RETRIES];
if (info)
printf("\t%llu", (unsigned long long)nla_get_u64(info));
info = stats_info[NL80211_TID_STATS_TX_MSDU_FAILED];
if (info)
printf("\t\t%llu", (unsigned long long)nla_get_u64(info));
}
}
static void print_power_mode(struct nlattr *a)
{
enum nl80211_mesh_power_mode pm = nla_get_u32(a);
switch (pm) {
case NL80211_MESH_POWER_ACTIVE:
printf("ACTIVE");
break;
case NL80211_MESH_POWER_LIGHT_SLEEP:
printf("LIGHT SLEEP");
break;
case NL80211_MESH_POWER_DEEP_SLEEP:
printf("DEEP SLEEP");
break;
case NL80211_MESH_POWER_UNKNOWN:
case __NL80211_MESH_POWER_AFTER_LAST:
default:
printf("UNKNOWN");
break;
}
}
void decode_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen)
{
int rate = 0;
char *pos = buf;
struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
[NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
[NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
[NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
[NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
[NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
};
if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
bitrate_attr, rate_policy)) {
snprintf(buf, buflen, "failed to parse nested rate attributes!");
return;
}
if (rinfo[NL80211_RATE_INFO_BITRATE32])
rate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]);
else if (rinfo[NL80211_RATE_INFO_BITRATE])
rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
if (rate > 0)
pos += snprintf(pos, buflen - (pos - buf),
"%d.%d MBit/s", rate / 10, rate % 10);
if (rinfo[NL80211_RATE_INFO_MCS])
pos += snprintf(pos, buflen - (pos - buf),
" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
if (rinfo[NL80211_RATE_INFO_VHT_MCS])
pos += snprintf(pos, buflen - (pos - buf),
" VHT-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_MCS]));
if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
pos += snprintf(pos, buflen - (pos - buf), " 40MHz");
if (rinfo[NL80211_RATE_INFO_80_MHZ_WIDTH])
pos += snprintf(pos, buflen - (pos - buf), " 80MHz");
if (rinfo[NL80211_RATE_INFO_80P80_MHZ_WIDTH])
pos += snprintf(pos, buflen - (pos - buf), " 80P80MHz");
if (rinfo[NL80211_RATE_INFO_160_MHZ_WIDTH])
pos += snprintf(pos, buflen - (pos - buf), " 160MHz");
if (rinfo[NL80211_RATE_INFO_SHORT_GI])
pos += snprintf(pos, buflen - (pos - buf), " short GI");
if (rinfo[NL80211_RATE_INFO_VHT_NSS])
pos += snprintf(pos, buflen - (pos - buf),
" VHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]));
}
static char *get_chain_signal(struct nlattr *attr_list)
{
struct nlattr *attr;
static char buf[64];
char *cur = buf;
int i = 0, rem;
const char *prefix;
if (!attr_list)
return "";
nla_for_each_nested(attr, attr_list, rem) {
if (i++ > 0)
prefix = ", ";
else
prefix = "[";
cur += snprintf(cur, sizeof(buf) - (cur - buf), "%s%d", prefix,
(int8_t) nla_get_u8(attr));
}
if (i)
snprintf(cur, sizeof(buf) - (cur - buf), "] ");
return buf;
}
int print_sta_handler(struct nl_msg *msg, void *arg)
{
struct nlattr *tb[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
char mac_addr[20], state_name[10], dev[20];
struct nl80211_sta_flag_update *sta_flags;
static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
[NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
[NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
[NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
[NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
[NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64},
[NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
[NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
[NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
[NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
[NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
[NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
[NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
[NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
[NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
[NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32},
[NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64},
[NL80211_STA_INFO_STA_FLAGS] =
{ .minlen = sizeof(struct nl80211_sta_flag_update) },
[NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32},
[NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32},
[NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32},
[NL80211_STA_INFO_CHAIN_SIGNAL] = { .type = NLA_NESTED },
[NL80211_STA_INFO_CHAIN_SIGNAL_AVG] = { .type = NLA_NESTED },
[NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
[NL80211_STA_INFO_BSS_PARAM] = { .type = NLA_NESTED },
[NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
};
char *chain;
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
/*
* TODO: validate the interface and mac address!
* Otherwise, there's a race condition as soon as
* the kernel starts sending station notifications.
*/
if (!tb[NL80211_ATTR_STA_INFO]) {
fprintf(stderr, "sta stats missing!\n");
return NL_SKIP;
}
if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
tb[NL80211_ATTR_STA_INFO],
stats_policy)) {
fprintf(stderr, "failed to parse nested attributes!\n");
return NL_SKIP;
}
mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
printf("Station %s (on %s)", mac_addr, dev);
if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
printf("\n\tinactive time:\t%u ms",
nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
if (sinfo[NL80211_STA_INFO_RX_BYTES64])
printf("\n\trx bytes:\t%llu",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]));
else if (sinfo[NL80211_STA_INFO_RX_BYTES])
printf("\n\trx bytes:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
if (sinfo[NL80211_STA_INFO_RX_PACKETS])
printf("\n\trx packets:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
if (sinfo[NL80211_STA_INFO_TX_BYTES64])
printf("\n\ttx bytes:\t%llu",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]));
else if (sinfo[NL80211_STA_INFO_TX_BYTES])
printf("\n\ttx bytes:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
if (sinfo[NL80211_STA_INFO_TX_PACKETS])
printf("\n\ttx packets:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
if (sinfo[NL80211_STA_INFO_TX_RETRIES])
printf("\n\ttx retries:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
if (sinfo[NL80211_STA_INFO_TX_FAILED])
printf("\n\ttx failed:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
if (sinfo[NL80211_STA_INFO_BEACON_LOSS])
printf("\n\tbeacon loss:\t%u",
nla_get_u32(sinfo[NL80211_STA_INFO_BEACON_LOSS]));
if (sinfo[NL80211_STA_INFO_BEACON_RX])
printf("\n\tbeacon rx:\t%llu",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_BEACON_RX]));
if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
printf("\n\trx drop misc:\t%llu",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]));
chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL]);
if (sinfo[NL80211_STA_INFO_SIGNAL])
printf("\n\tsignal: \t%d %sdBm",
(int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]),
chain);
chain = get_chain_signal(sinfo[NL80211_STA_INFO_CHAIN_SIGNAL_AVG]);
if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
printf("\n\tsignal avg:\t%d %sdBm",
(int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]),
chain);
if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
printf("\n\tbeacon signal avg:\t%d dBm",
nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]));
if (sinfo[NL80211_STA_INFO_T_OFFSET])
printf("\n\tToffset:\t%llu us",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
char buf[100];
decode_bitrate(sinfo[NL80211_STA_INFO_TX_BITRATE], buf, sizeof(buf));
printf("\n\ttx bitrate:\t%s", buf);
}
if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
char buf[100];
decode_bitrate(sinfo[NL80211_STA_INFO_RX_BITRATE], buf, sizeof(buf));
printf("\n\trx bitrate:\t%s", buf);
}
if (sinfo[NL80211_STA_INFO_RX_DURATION])
printf("\n\trx duration:\t%lld us",
(unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DURATION]));
if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) {
uint32_t thr;
thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
/* convert in Mbps but scale by 1000 to save kbps units */
thr = thr * 1000 / 1024;
printf("\n\texpected throughput:\t%u.%uMbps",
thr / 1000, thr % 1000);
}
if (sinfo[NL80211_STA_INFO_LLID])
printf("\n\tmesh llid:\t%d",
nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
if (sinfo[NL80211_STA_INFO_PLID])
printf("\n\tmesh plid:\t%d",
nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
case LISTEN:
strcpy(state_name, "LISTEN");
break;
case OPN_SNT:
strcpy(state_name, "OPN_SNT");
break;
case OPN_RCVD:
strcpy(state_name, "OPN_RCVD");
break;
case CNF_RCVD:
strcpy(state_name, "CNF_RCVD");
break;
case ESTAB:
strcpy(state_name, "ESTAB");
break;
case HOLDING:
strcpy(state_name, "HOLDING");
break;
case BLOCKED:
strcpy(state_name, "BLOCKED");
break;
default:
strcpy(state_name, "UNKNOWN");
break;
}
printf("\n\tmesh plink:\t%s", state_name);
}
if (sinfo[NL80211_STA_INFO_LOCAL_PM]) {
printf("\n\tmesh local PS mode:\t");
print_power_mode(sinfo[NL80211_STA_INFO_LOCAL_PM]);
}
if (sinfo[NL80211_STA_INFO_PEER_PM]) {
printf("\n\tmesh peer PS mode:\t");
print_power_mode(sinfo[NL80211_STA_INFO_PEER_PM]);
}
if (sinfo[NL80211_STA_INFO_NONPEER_PM]) {
printf("\n\tmesh non-peer PS mode:\t");
print_power_mode(sinfo[NL80211_STA_INFO_NONPEER_PM]);
}
if (sinfo[NL80211_STA_INFO_STA_FLAGS]) {
sta_flags = (struct nl80211_sta_flag_update *)
nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
printf("\n\tauthorized:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
printf("yes");
else
printf("no");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) {
printf("\n\tauthenticated:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
printf("yes");
else
printf("no");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
printf("\n\tassociated:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_ASSOCIATED))
printf("yes");
else
printf("no");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
printf("\n\tpreamble:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
printf("short");
else
printf("long");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) {
printf("\n\tWMM/WME:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_WME))
printf("yes");
else
printf("no");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) {
printf("\n\tMFP:\t\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
printf("yes");
else
printf("no");
}
if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
printf("\n\tTDLS peer:\t");
if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
printf("yes");
else
printf("no");
}
}
if (sinfo[NL80211_STA_INFO_TID_STATS] && arg != NULL &&
!strcmp((char *)arg, "-v"))
decode_tid_stats(sinfo[NL80211_STA_INFO_TID_STATS]);
if (sinfo[NL80211_STA_INFO_BSS_PARAM])
decode_bss_param(sinfo[NL80211_STA_INFO_BSS_PARAM]);
if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
printf("\n\tconnected time:\t%u seconds",
nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]));
printf("\n");
return NL_SKIP;
}
/*
* end nl-4.9
*/
void decode_attr_bss( struct nlattr *attr )
{
/* from iw-4.9 */
struct nlattr *bss[NL80211_BSS_MAX + 1];
static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
[NL80211_BSS_TSF] = { .type = NLA_U64 },
[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
[NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
[NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
[NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
[NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
[NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
[NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
[NL80211_BSS_STATUS] = { .type = NLA_U32 },
};
/* decode ATTR_BSS */
printf("%s len=%" PRIu16 " type=%" PRIu16 "\n", __func__, attr->nla_len, attr->nla_type);
/* from iw-4.9 */
if (nla_parse_nested(bss, NL80211_BSS_MAX,
attr, bss_policy)) {
fprintf(stderr, "failed to parse nested attributes!\n");
return;
}
// report attrs not handled in my crappy code below
ssize_t counter=0;
enum nl80211_bss bss_attr;
for (int i=0 ; i<NL80211_BSS_MAX ; i++ ) {
if (bss[i]) {
bss_attr = nla_type(bss[i]);
printf("%s %d=%p type=%d len=%d\n", __func__, i, (void *)bss[i], bss_attr, nla_len(bss[i]));
counter++;
}
}
if (bss[NL80211_BSS_BSSID]) {
counter--;
char mac_addr[64];
mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
printf("bssid=%s\n", mac_addr);
}
if (bss[NL80211_BSS_FREQUENCY]) {
counter--;
uint32_t freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
printf("freq=%" PRIu32 "\n", freq);
}
if (bss[NL80211_BSS_TSF]) {
counter--;
/* Timing synchronization function
* https://en.wikipedia.org/wiki/Timing_synchronization_function
*/
uint64_t tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
printf("tsf=%#" PRIx64 "\n", tsf);
}
if (bss[NL80211_BSS_BEACON_INTERVAL]) {
counter--;
uint16_t bi = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
printf("beacon interval=%" PRIu16 "\n", bi);
}
if (bss[NL80211_BSS_CAPABILITY]) {
counter--;
uint16_t capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
printf("capabilities=%#" PRIx16 "\n", capa);
}
if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
counter--;
uint8_t *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
size_t ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
printf("found len=%zu of information elements\n", ie_len);
hex_dump("ie", ie, ie_len);
}
if (bss[NL80211_BSS_SIGNAL_MBM]) {
counter--;
int32_t mbm = (int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
printf("mbm=%" PRIi32 "\n", mbm);
}
if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
counter--;
uint8_t signal = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
printf("signal=%" PRIu8 "\n", signal);
}
if (bss[NL80211_BSS_STATUS]) {
counter--;
uint32_t status = nla_get_u32(bss[NL80211_BSS_STATUS]);
printf("status=%" PRIu32 "\n", status);
}
if (bss[NL80211_BSS_SEEN_MS_AGO]) {
counter--;
uint32_t seen_ms = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
printf("seen %" PRIu32 " ms ago\n", seen_ms);
}
if (bss[NL80211_BSS_BEACON_IES]) {
counter--;
uint8_t *beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
hex_dump("beacon_ie", beacon_ie, nla_len(bss[NL80211_BSS_BEACON_IES]));
}
if (bss[NL80211_BSS_CHAN_WIDTH]) {
counter--;
enum nl80211_bss_scan_width chan_width = nla_get_u32(bss[NL80211_BSS_CHAN_WIDTH]);
printf("chan width=%" PRIu32 "\n", chan_width);
}
if (bss[NL80211_BSS_BEACON_TSF]) {
counter --;
uint64_t beacon_tsf = nla_get_u64(bss[NL80211_BSS_BEACON_TSF]);
printf("beacon tsf=%" PRIu64 "\n", beacon_tsf);
}
if (bss[NL80211_BSS_PRESP_DATA]) {
counter--;
bool presp_data = nla_get_flag(bss[NL80211_BSS_PRESP_DATA]);
printf("presp_data is %s\n", presp_data ? "true": "false");
}
if (bss[NL80211_BSS_LAST_SEEN_BOOTTIME]) {
counter--;
/* nanoseconds */
uint64_t last_seen_boottime = nla_get_u64(bss[NL80211_BSS_LAST_SEEN_BOOTTIME]);
printf("last seen boottime=%" PRIu64 "ns\n", last_seen_boottime);
}
printf("%s counter=%zd unhandled attributes\n", __func__, counter);
}
int decode_attr_scan_frequencies( struct nlattr *attr )
{
struct nlattr *nst;
int rem_nst;
printf("scan_frequencies\n");
nla_for_each_nested(nst, attr, rem_nst)
printf(" %d", nla_get_u32(nst));
printf("\n");
return 0;
}
int decode_attr_scan_ssids( struct nlattr *attr, struct bytebuf_array* ssid_list )
{
struct nlattr *nst;
int rem_nst;
int err;
bytebuf_array_verify(ssid_list);
nla_for_each_nested(nst, attr, rem_nst) {
err = bytebuf_array_emplace_back( ssid_list, nla_data(nst), nla_len(nst));
if (err) {
goto fail;
}
}
return 0;
fail:
bytebuf_array_free(ssid_list);
return err;
}
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
printf("%s\n", __func__);
(void)nla;
(void)err;
(void)arg;
assert(0);
return NL_STOP;
}
//static int finish_handler(struct nl_msg *msg, void *arg)
//{
// printf("%s\n", __func__);
// (void)msg;
//
// int *ret = arg;
// *ret = 0;
// return NL_SKIP;
//}
static int ack_handler(struct nl_msg *msg, void *arg)
{
printf("%s\n", __func__);
(void)msg;
int *ret = arg;
*ret = 0;
return NL_STOP;
}
#if 0
static int msg_in_handler(struct nl_msg *msg, void *arg )
{
printf("%s\n", __func__);
/* "Called for every message received." */
(void)msg;
(void)arg;
return NL_OK;
}
#endif
/* from iw genl.c */
struct handler_args {
const char *group;
int id;
};
/* from iw genl.c */
static int family_handler(struct nl_msg *msg, void *arg)
{
struct handler_args *grp = arg;
struct nlattr *tb[CTRL_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *mcgrp;
int rem_mcgrp;
printf("%s\n", __func__);
nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (!tb[CTRL_ATTR_MCAST_GROUPS])
return NL_SKIP;
nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) {
struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX,
nla_data(mcgrp), nla_len(mcgrp), NULL);
if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] ||
!tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
continue;
if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
grp->group, nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])))
continue;
grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
break;
}
return NL_SKIP;
}
/* from iw genl.c but I renamed from nl_get_multicast_id() because it was
* confusing me thinking it was part of the libnl.
*/
int get_multicast_id(struct nl_sock *sock, const char *family, const char *group)
{
struct nl_msg *msg;
struct nl_cb *cb;
int ret, ctrlid;
struct handler_args grp = {
.group = group,
.id = -ENOENT,
};
msg = nlmsg_alloc();
if (!msg)
return -ENOMEM;
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb) {
ret = -ENOMEM;
goto out_fail_cb;
}
ctrlid = genl_ctrl_resolve(sock, "nlctrl");
genlmsg_put(msg, 0, 0, ctrlid, 0,
0, CTRL_CMD_GETFAMILY, 0);
ret = -ENOBUFS;
NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
ret = nl_send_auto_complete(sock, msg);
if (ret < 0)
goto out;
ret = 1;
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &ret);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &ret);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, family_handler, &grp);
while (ret > 0)
nl_recvmsgs(sock, cb);
if (ret == 0)
ret = grp.id;
nla_put_failure:
out:
nl_cb_put(cb);
out_fail_cb:
nlmsg_free(msg);
return ret;
}
///* from iw event.c */
//static int no_seq_check(struct nl_msg *msg, void *arg)
//{
// (void)msg;
// (void)arg;
//
// return NL_OK;
//}