forked from Delicode/libnl-ap-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1312 lines (1136 loc) · 33 KB
/
main.cpp
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Code applied from:
* - libnl sources LGPL2.1 https://www.infradead.org/~tgr/libnl/
* - example code from Python libnl port (LGPL2.1):
* https://github.com/Robpol86/libnl/blob/master/example_c/scan_access_points.c
* - as well as iw(8) source code (MIT).
* https://git.sipsolutions.net/iw.git/tree/scan.c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This program uses the scanning features of wifi hardware to discover nearby
* wifi access points and prints information about them. Due to libnl / nl80211
* being used, this program only works on somewhat newer wifi drivers.
* Only root can perform scanning due to NL80211_CMD_TRIGGER_SCAN being limited
* to root by default.
*
* This changed version of the example program scan_access_points.c addresses several
* errors that were not handled, scans for more information, rearranges the code
* in a cleaner format and improves on the documentation. In addition, several memory
* leaks with allocated libnl resources are handled.
*/
#include <errno.h>
#include <ctype.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/nl80211.h>
#include <net/if.h>
#include <memory>
#include <stdio.h>
#define ARRAY_SIZE(ar) (sizeof(ar)/sizeof(ar[0]))
// These are from iw source code, and they related to parsing BSS capabilities
#define WLAN_CAPABILITY_ESS (1<<0)
#define WLAN_CAPABILITY_IBSS (1<<1)
#define WLAN_CAPABILITY_CF_POLLABLE (1<<2)
#define WLAN_CAPABILITY_CF_POLL_REQUEST (1<<3)
#define WLAN_CAPABILITY_PRIVACY (1<<4)
#define WLAN_CAPABILITY_SHORT_PREAMBLE (1<<5)
#define WLAN_CAPABILITY_PBCC (1<<6)
#define WLAN_CAPABILITY_CHANNEL_AGILITY (1<<7)
#define WLAN_CAPABILITY_SPECTRUM_MGMT (1<<8)
#define WLAN_CAPABILITY_QOS (1<<9)
#define WLAN_CAPABILITY_SHORT_SLOT_TIME (1<<10)
#define WLAN_CAPABILITY_APSD (1<<11)
#define WLAN_CAPABILITY_RADIO_MEASURE (1<<12)
#define WLAN_CAPABILITY_DSSS_OFDM (1<<13)
#define WLAN_CAPABILITY_DEL_BACK (1<<14)
#define WLAN_CAPABILITY_IMM_BACK (1<<15)
/* DMG (60gHz) 802.11ad */
/* type - bits 0..1 */
#define WLAN_CAPABILITY_DMG_TYPE_MASK (3<<0)
#define WLAN_CAPABILITY_DMG_TYPE_IBSS (1<<0) /* Tx by: STA */
#define WLAN_CAPABILITY_DMG_TYPE_PBSS (2<<0) /* Tx by: PCP */
#define WLAN_CAPABILITY_DMG_TYPE_AP (3<<0) /* Tx by: AP */
#define WLAN_CAPABILITY_DMG_CBAP_ONLY (1<<2)
#define WLAN_CAPABILITY_DMG_CBAP_SOURCE (1<<3)
#define WLAN_CAPABILITY_DMG_PRIVACY (1<<4)
#define WLAN_CAPABILITY_DMG_ECPAC (1<<5)
#define WLAN_CAPABILITY_DMG_SPECTRUM_MGMT (1<<8)
#define WLAN_CAPABILITY_DMG_RADIO_MEASURE (1<<12)
// These are from iw source code, and they related to parsing authentication suites
const unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
const unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
const unsigned char wfa_oui[3] = { 0x50, 0x6f, 0x9a };
// global variable that contains the MAC address for the current scan result,
// used to make sure every print contains clarification for which MAC the data is.
char current_mac[20];
const char* DISCOVER_STR = "AP_DISCOVERED,";
const char* DATA_STR = "AP_DATA,";
const char* BSS_SECTION = "BSS";
inline void dataline(const char* section_name = NULL) {
printf("%s%s,%s,", DATA_STR, current_mac, section_name != NULL ? section_name : BSS_SECTION);
}
static void sep_if_not_first(bool *first, const char* separator = ",")
{
if (!*first)
printf("%s", separator);
else
*first = false;
}
struct init_scan_results {
int done;
int aborted;
};
struct print_ies_data {
unsigned char *ie;
int ielen;
};
// Error callback
int error_handler(struct sockaddr_nl* nla, struct nlmsgerr* err, void* arg) {
int* ret = (int*)arg;
*ret = err->error;
return NL_STOP;
}
// Callback for NL_CB_FINISH
int finish_handler(struct nl_msg* msg, void* arg) {
int* ret = (int*)arg;
*ret = 0;
return NL_SKIP;
}
// Callback for NL_CB_ACK
int ack_handler(struct nl_msg *msg, void* arg) {
int* ret = (int*)arg;
*ret = 0;
return NL_STOP;
}
// Callback for NL_CB_SEQ_CHECK
int no_seq_check(struct nl_msg* msg, void* arg) {
return NL_OK;
}
// From http://git.kernel.org/cgit/linux/kernel/git/jberg/iw.git/tree/util.c
void mac_addr_n2a(char* mac_addr, unsigned char* arg) {
int i, l;
l = 0;
for (i = 0; i < 6; i++) {
if (i == 0) {
sprintf(mac_addr+l, "%02x", arg[i]);
l += 2;
} else {
sprintf(mac_addr+l, ":%02x", arg[i]);
l += 3;
}
}
}
static void print_capa_dmg(__u16 capa, bool* first)
{
switch (capa & WLAN_CAPABILITY_DMG_TYPE_MASK) {
case WLAN_CAPABILITY_DMG_TYPE_AP: {
sep_if_not_first(first);
printf("DMG_ESS");
break;
}
case WLAN_CAPABILITY_DMG_TYPE_PBSS: {
sep_if_not_first(first);
printf("DMG_PCP");
break;
}
case WLAN_CAPABILITY_DMG_TYPE_IBSS: {
sep_if_not_first(first);
printf("DMG_IBSS");
break;
}
}
if (capa & WLAN_CAPABILITY_DMG_CBAP_ONLY){
sep_if_not_first(first);
printf("CBAP_Only");
}
if (capa & WLAN_CAPABILITY_DMG_CBAP_SOURCE){
sep_if_not_first(first);
printf("CBAP_Src");
}
if (capa & WLAN_CAPABILITY_DMG_PRIVACY){
sep_if_not_first(first);
printf("Privacy");
}
if (capa & WLAN_CAPABILITY_DMG_ECPAC){
sep_if_not_first(first);
printf("ECPAC");
}
if (capa & WLAN_CAPABILITY_DMG_SPECTRUM_MGMT){
sep_if_not_first(first);
printf("SpectrumMgmt");
}
if (capa & WLAN_CAPABILITY_DMG_RADIO_MEASURE){
sep_if_not_first(first);
printf("RadioMeasure");
}
}
static void print_capa_non_dmg(__u16 capa, bool* first)
{
if (capa & WLAN_CAPABILITY_ESS){
sep_if_not_first(first);
printf("ESS");
}
if (capa & WLAN_CAPABILITY_IBSS){
sep_if_not_first(first);
printf("IBSS");
}
if (capa & WLAN_CAPABILITY_CF_POLLABLE){
sep_if_not_first(first);
printf("CfPollable");
}
if (capa & WLAN_CAPABILITY_CF_POLL_REQUEST){
sep_if_not_first(first);
printf("CfPollReq");
}
if (capa & WLAN_CAPABILITY_PRIVACY){
sep_if_not_first(first);
printf("Privacy");
}
if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE){
sep_if_not_first(first);
printf("ShortPreamble");
}
if (capa & WLAN_CAPABILITY_PBCC){
sep_if_not_first(first);
printf("PBCC");
}
if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY){
sep_if_not_first(first);
printf("ChannelAgility");
}
if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT){
sep_if_not_first(first);
printf("SpectrumMgmt");
}
if (capa & WLAN_CAPABILITY_QOS){
sep_if_not_first(first);
printf("QoS");
}
if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME){
sep_if_not_first(first);
printf("ShortSlotTime");
}
if (capa & WLAN_CAPABILITY_APSD){
sep_if_not_first(first);
printf("APSD");
}
if (capa & WLAN_CAPABILITY_RADIO_MEASURE){
sep_if_not_first(first);
printf("RadioMeasure");
}
if (capa & WLAN_CAPABILITY_DSSS_OFDM){
sep_if_not_first(first);
printf("DSSS-OFDM");
}
if (capa & WLAN_CAPABILITY_DEL_BACK){
sep_if_not_first(first);
printf("DelayedBACK");
}
if (capa & WLAN_CAPABILITY_IMM_BACK){
sep_if_not_first(first);
printf("ImmediateBACK");
}
}
static const char * wifi_wps_dev_passwd_id(uint16_t id)
{
switch (id) {
case 0:
return "Default (PIN)";
case 1:
return "User-specified";
case 2:
return "Machine-specified";
case 3:
return "Rekey";
case 4:
return "PushButton";
case 5:
return "Registrar-specified";
default:
return "??";
}
}
static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data,
struct print_ies_data *ie_buffer, const char* section_name)
{
__u16 subtype, sublen;
while (len >= 4) {
subtype = (data[0] << 8) + data[1];
sublen = (data[2] << 8) + data[3];
if (sublen > len - 4)
break;
switch (subtype) {
case 0x104a:
if (sublen < 1) break;
dataline(section_name);
printf("version:%d.%d\n", data[4] >> 4, data[4] & 0xF);
break;
case 0x1011:
dataline(section_name);
printf("device name:%.*s\n", sublen, data + 4);
break;
case 0x1012: {
uint16_t id;
if (sublen != 2) break;
id = data[4] << 8 | data[5];
dataline(section_name);
printf("device password id:%u (%s)\n", id, wifi_wps_dev_passwd_id(id));
break;
}
case 0x1021:
dataline(section_name);
printf("manufacturer:%.*s\n", sublen, data + 4);
break;
case 0x1023:
dataline(section_name);
printf("model:%.*s\n", sublen, data + 4);
break;
case 0x1024:
dataline(section_name);
printf("model Number:%.*s\n", sublen, data + 4);
break;
case 0x103b: {
__u8 val;
if (sublen < 1) break;
val = data[4];
dataline(section_name);
printf("response type:%d%s\n", val, val == 3 ? " (AP)" : "");
break;
}
case 0x103c: {
__u8 val;
if (sublen < 1) break;
val = data[4];
dataline(section_name);
printf("rf bands:0x%x\n", val);
break;
}
case 0x1041: {
__u8 val;
if (sublen < 1) break;
val = data[4];
dataline(section_name);
printf("selected registrar:0x%x\n", val);
break;
}
case 0x1042:
dataline(section_name);
printf("serial number:%.*s\n", sublen, data + 4);
break;
case 0x1044: {
__u8 val;
if (sublen < 1) break;
val = data[4];
dataline(section_name);
printf("wi-fi protected setup state:%d%s%s\n",
val,
val == 1 ? " (Unconfigured)" : "",
val == 2 ? " (Configured)" : "");
break;
}
case 0x1047:
if (sublen != 16) break;
dataline(section_name);
printf("uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x-%02x%02x%02x%02x%02x%02x\n",
data[4], data[5], data[6], data[7],
data[8], data[9], data[10], data[11],
data[12], data[13], data[14], data[15],
data[16], data[17], data[18], data[19]);
break;
case 0x1049:
if (sublen == 6 &&
data[4] == 0x00 &&
data[5] == 0x37 &&
data[6] == 0x2a &&
data[7] == 0x00 &&
data[8] == 0x01) {
uint8_t v2 = data[9];
dataline(section_name);
printf("version2:%d.%d\n", v2 >> 4, v2 & 0xf);
}
break;
case 0x1054: {
if (sublen != 8) break;
dataline(section_name);
printf("primary device type:"
"%u-%02x%02x%02x%02x-%u\n",
data[4] << 8 | data[5],
data[6], data[7], data[8], data[9],
data[10] << 8 | data[11]);
break;
}
case 0x1057: {
__u8 val;
if (sublen < 1) break;
val = data[4];
dataline(section_name);
printf("ap setup locked:0x%.2x\n", val);
break;
}
case 0x1008:
case 0x1053: {
__u16 meth;
bool comma;
if (sublen < 2) break;
meth = (data[4] << 8) + data[5];
comma = false;
dataline(section_name);
printf("%sconfig methods:",
subtype == 0x1053 ? "selected registrar ": "");
#define T(bit, name) do { \
if (meth & (1<<bit)) { \
if (comma) \
printf(","); \
comma = true; \
printf("%s",name); \
} } while (0)
T(0, "USB");
T(1, "Ethernet");
T(2, "Label");
T(3, "Display");
T(4, "Ext. NFC");
T(5, "Int. NFC");
T(6, "NFC Intf.");
T(7, "PBC");
T(8, "Keypad");
printf("\n");
break;
#undef T
}
default: {
break;
}
}
data += sublen + 4;
len -= sublen + 4;
}
}
void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data,
struct print_ies_data *ie_buffer, const char* section_name) {
int i;
dataline();
printf("ssid:");
for (i = 0; i < len; i++) {
if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\') {
printf("%c", data[i]);
} else if (data[i] == ' ' && (i != 0 && i != len -1)) {
printf(" ");
} else {
printf("\\x%.2x", data[i]);
}
}
printf("\n");
}
void print_auth(const uint8_t *data) {
// This is copied from iw sources, and I have no idea how this works
// There's a lot of magic numbers going around.
if (memcmp(data, ms_oui, 3) == 0) {
switch (data[3]) {
case 1:
printf("IEEE 802.1X");
break;
case 2:
printf("PSK");
break;
default:
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
break;
}
} else if (memcmp(data, ieee80211_oui, 3) == 0) {
switch (data[3]) {
case 1:
printf("IEEE 802.1X");
break;
case 2:
printf("PSK");
break;
case 3:
printf("FT/IEEE 802.1X");
break;
case 4:
printf("FT/PSK");
break;
case 5:
printf("IEEE 802.1X/SHA-256");
break;
case 6:
printf("PSK/SHA-256");
break;
case 7:
printf("TDLS/TPK");
break;
case 8:
printf("SAE");
break;
case 9:
printf("FT/SAE");
break;
case 11:
printf("IEEE 802.1X/SUITE-B");
break;
case 12:
printf("IEEE 802.1X/SUITE-B-192");
break;
case 13:
printf("FT/IEEE 802.1X/SHA-384");
break;
case 14:
printf("FILS/SHA-256");
break;
case 15:
printf("FILS/SHA-384");
break;
case 16:
printf("FT/FILS/SHA-256");
break;
case 17:
printf("FT/FILS/SHA-384");
break;
case 18:
printf("OWE");
break;
default:
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
break;
}
} else if (memcmp(data, wfa_oui, 3) == 0) {
switch (data[3]) {
case 1:
printf("OSEN");
break;
case 2:
printf("DPP");
break;
default:
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
break;
}
} else {
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
}
}
// Copied from iw sources, no idea what the magic values are
static void print_cipher(const uint8_t *data) {
if (memcmp(data, ms_oui, 3) == 0) {
switch (data[3]) {
case 0:
printf("Use group cipher suite");
break;
case 1:
printf("WEP-40");
break;
case 2:
printf("TKIP");
break;
case 4:
printf("CCMP");
break;
case 5:
printf("WEP-104");
break;
default:
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
break;
}
} else if (memcmp(data, ieee80211_oui, 3) == 0) {
switch (data[3]) {
case 0:
printf("Use group cipher suite");
break;
case 1:
printf("WEP-40");
break;
case 2:
printf("TKIP");
break;
case 4:
printf("CCMP");
break;
case 5:
printf("WEP-104");
break;
case 6:
printf("AES-128-CMAC");
break;
case 7:
printf("NO-GROUP");
break;
case 8:
printf("GCMP");
break;
default:
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
break;
}
} else {
printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);
}
}
// from iw source code, no idea what's going on here
void print_rsn_ie(const char *defcipher, const char *defauth,
uint8_t len, const uint8_t *data, const char* section_name) {
__u16 count, capa;
int i;
int is_osen = 0;
bool first = true;
dataline(section_name);
if (!is_osen) {
__u16 version;
version = data[0] + (data[1] << 8);
printf("version:%d\n", version);
data += 2;
len -= 2;
}
if (len < 4) {
dataline(section_name);
printf("group cipher:%s\n", defcipher);
dataline(section_name);
printf("pairwise ciphers:%s\n", defcipher);
return;
}
dataline(section_name);
printf("group cipher:");
print_cipher(data);
printf("\n");
data += 4;
len -= 4;
if (len < 2) {
dataline(section_name);
printf("pairwise ciphers:%s\n", defcipher);
return;
}
count = data[0] | (data[1] << 8);
if (2 + (count * 4) > len) {
goto invalid;
}
dataline(section_name);
printf("pairwise ciphers:");
for (i = 0; i < count; i++) {
if (i > 0) printf(",");
print_cipher(data + 2 + (i * 4));
}
printf("\n");
data += 2 + (count * 4);
len -= 2 + (count * 4);
if (len < 2) {
dataline(section_name);
printf("authentication suites:%s\n", defauth);
return;
}
count = data[0] | (data[1] << 8);
if (2 + (count * 4) > len) {
goto invalid;
}
dataline(section_name);
printf("authentication suites:");
for (i = 0; i < count; i++) {
if (i > 0) printf(",");
print_auth(data + 2 + (i * 4));
}
printf("\n");
data += 2 + (count * 4);
len -= 2 + (count * 4);
if (len >= 2) {
capa = data[0] | (data[1] << 8);
dataline(section_name);
printf("capabilities:");
if (capa & 0x0001)
{sep_if_not_first(&first); printf("PreAuth");}
if (capa & 0x0002)
{sep_if_not_first(&first); printf("NoPairwise");}
switch ((capa & 0x000c) >> 2) {
case 0:
{sep_if_not_first(&first); printf("1-PTKSA-RC");
break;}
case 1:
{sep_if_not_first(&first); printf("2-PTKSA-RC");
break;}
case 2:
{sep_if_not_first(&first); printf("4-PTKSA-RC");
break;}
case 3:
{sep_if_not_first(&first); printf("16-PTKSA-RC");
break;}
}
switch ((capa & 0x0030) >> 4) {
case 0:
{sep_if_not_first(&first); printf("1-GTKSA-RC");
break;}
case 1:
{sep_if_not_first(&first); printf("2-GTKSA-RC");
break;}
case 2:
{sep_if_not_first(&first); printf("4-GTKSA-RC");
break;}
case 3:
{sep_if_not_first(&first); printf("16-GTKSA-RC");
break;}
}
if (capa & 0x0040)
{sep_if_not_first(&first); printf("MFP-required");}
if (capa & 0x0080)
{sep_if_not_first(&first); printf("MFP-capable");}
if (capa & 0x0200)
{sep_if_not_first(&first); printf("Peerkey-enabled");}
if (capa & 0x0400)
{sep_if_not_first(&first); printf("SPP-AMSDU-capable");}
if (capa & 0x0800)
{sep_if_not_first(&first); printf("SPP-AMSDU-required");}
if (capa & 0x2000)
{sep_if_not_first(&first); printf("Extended-Key-ID");}
{sep_if_not_first(&first); printf("(0x%.4x)", capa);}
data += 2;
len -= 2;
printf("\n");
}
if (len >= 2) {
int pmkid_count = data[0] | (data[1] << 8);
if (len >= 2 + 16 * pmkid_count) {
dataline(section_name);
printf("PMKID count:%d\n", pmkid_count);
/* not printing PMKID values */
data += 2 + 16 * pmkid_count;
len -= 2 + 16 * pmkid_count;
} else {
goto invalid;
}
}
if (len >= 4) {
dataline(section_name);
printf("group mgmt cipher suite:");
print_cipher(data);
data += 4;
len -= 4;
printf("\n");
}
invalid:
if (len != 0) {
dataline(section_name);
printf("bogus tail data:%d", len);
while (len) {
printf(" %.2x", *data);
data++;
len--;
}
printf("\n");
}
}
// from iw source code
void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data,
struct print_ies_data *ie_buffer, const char* section_name) {
print_rsn_ie("CCMP", "IEEE 802.1X", len, data, section_name);
}
static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data,
struct print_ies_data *ie_buffer, const char* section_name) {
print_rsn_ie("TKIP", "IEEE 802.1X", len, data, section_name);
}
// this struct is used to create a handler for specific magic values of
// IE (information element) in the wifi probe or beacon responses. From what
// I gather, each IE requires a bit different type of parsing, and what I do
// here is just directly copied from how iw does it. There's tons of magic
// values in the code, and I couldn't figure out where they are defined.
struct ie_print {
const char* name;
void (*print)(const uint8_t type, uint8_t len, const uint8_t *data,
struct print_ies_data *ie_buffer, const char* section_name);
uint8_t minlen;
uint8_t maxlen;
};
// This array size needs to be adjusted if magic values go beyond it. The array
// contains empty elements for each type of IE that is not handled and is only
// modified at those points where we have an IE handler. See how it is done in
// the beginning of main()
const int MAX_IE_MAGIC = 112;
static struct ie_print ieprinters[MAX_IE_MAGIC];
const int MAX_VENDOR_MAGIC = 112;
static struct ie_print wifiprinters[MAX_VENDOR_MAGIC];
// print a single IE parsed from a probe request or beacon response
static void print_ie(const struct ie_print *p, const uint8_t type, uint8_t len,
const uint8_t *data, struct print_ies_data *ie_buffer) {
// If no printer function is defined for type of IE
if (p->print == NULL) {
return;
}
if (len < p->minlen || len > p->maxlen) {
if (len > 1) {
printf(",invalid %d bytes:", len);
} else if (len) {
printf(",invalid:1 byte %.02x>\n", data[0]);
} else {
printf(",invalid:no data");
}
return;
}
p->print(type, len, data, ie_buffer, p->name);
}
static void print_vendor(unsigned char len, unsigned char *data)
{
if (len < 3) {
return;
}
if (len >= 4 && memcmp(data, ms_oui, 3) == 0) {
if (data[3] < ARRAY_SIZE(wifiprinters) &&
wifiprinters[data[3]].name) {
print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4, NULL);
return;
}
return;
}
}
// Go through all information elements and print them if a printer for them is defined
void print_ies(unsigned char *ie, int ielen) {
struct print_ies_data ie_buffer = {
.ie = ie,
.ielen = ielen };
if (ie == NULL || ielen < 0) {
return;
}
while (ielen >= 2 && ielen - 2 >= ie[1]) {
if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]].name) {
print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2, &ie_buffer);
} else if (ie[0] == 221) {
print_vendor(ie[1], ie + 2);
}
ielen -= ie[1] + 2;
ie += ie[1] + 2;
}
}
// Called by the kernel when the scan is done or has been aborted
int scan_finished_cb(struct nl_msg* msg, void* arg) {
struct genlmsghdr* gnlh = (genlmsghdr*)nlmsg_data(nlmsg_hdr(msg));
struct init_scan_results* results = (init_scan_results*)arg;
if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) {
results->done = 1;
results->aborted = 1;
} else if (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS) {
results->done = 1;
results->aborted = 0;
}
// else probably an uninteresting multicast message.
return NL_SKIP;
}
// Called by the kernel with a dump of the successful scan's data. Called for each SSID.
int receive_scan_result(struct nl_msg *msg, void *arg) {
struct genlmsghdr* gnlh = (genlmsghdr*)nlmsg_data(nlmsg_hdr(msg));
// Container for netlink attribute indices, each pointing to different parts of the
// netlink message stream. These can be used to then parse further attributes from
// the stream. Go read netlink documentation and see if you have more luck
// understanding how the messaging works.
struct nlattr* tb[NL80211_ATTR_MAX + 1];
// container for parsing the access point's basic service set information (BSS)
struct nlattr* bss[NL80211_BSS_MAX + 1];
// container specifying the types and lengths of data to be parsed from the netlink
// message, I think. The whole message parsing side of netlink is confusing.
struct nla_policy bss_policy[NL80211_BSS_MAX + 1];
memset(bss_policy, 0, sizeof(bss_policy));
memset(bss, 0, sizeof(bss));
memset(tb, 0, sizeof(tb));
bss_policy[NL80211_BSS_TSF] = { .type = NLA_U64 };
bss_policy[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
bss_policy[NL80211_BSS_BSSID] = { };
bss_policy[NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 };
bss_policy[NL80211_BSS_CAPABILITY] = { .type = NLA_U16 };
bss_policy[NL80211_BSS_INFORMATION_ELEMENTS] = { };
bss_policy[NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 };
bss_policy[NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 };
bss_policy[NL80211_BSS_STATUS] = { .type = NLA_U32 };
bss_policy[NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 };
bss_policy[NL80211_BSS_BEACON_IES] = { };
bool is_dmg = false;
int err = nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
if (err < 0) {
printf("error creating attribute indices from scan message: %d, %s\n", err, nl_geterror(err));
return NL_SKIP;
}
if (!tb[NL80211_ATTR_BSS]) {
printf("bss info missing\n");
return NL_SKIP;
}
// BSS information is a nested attribute, so a second parse call is needed
err = nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy);
if (err < 0) {
printf("failed to parse nested attributes: %d, %s\n", err, nl_geterror(err));
return NL_SKIP;
}
// If BSSID or IE is missing, we can't parse anything beyond this point
if (!bss[NL80211_BSS_BSSID] || !bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
return NL_SKIP;
}
memset(current_mac, '\0', sizeof(current_mac));
mac_addr_n2a(current_mac, (unsigned char*)nla_data(bss[NL80211_BSS_BSSID]));
printf("%s%s\n", DISCOVER_STR, current_mac);
if (bss[NL80211_BSS_SIGNAL_MBM]) {
dataline();
printf("signal strength:%d mBm\n", nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]));
} else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
dataline();
printf("signal strength:%d units\n", nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]));
}
if (bss[NL80211_BSS_FREQUENCY]) {
int freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
dataline();
int freq_offset = bss[NL80211_BSS_FREQUENCY_OFFSET] ? nla_get_u32(bss[NL80211_BSS_FREQUENCY_OFFSET]) : 0;
if (freq_offset > 0)
printf("frequency:%d.%d MHz\n", freq, freq_offset);
else
printf("frequency:%d MHz\n", freq);