-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmacgeiger.c
1436 lines (1268 loc) · 34.5 KB
/
macgeiger.c
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
/*
MacGeiger WIFI AP detector
Copyright (C) 2014 rofl0r
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pcap/pcap.h>
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <ctype.h>
#include <fcntl.h>
#define GUI_FPS 40
#include "audio-backend.c"
#include "pcapfile.h"
#define LIBRARY_CODE
#include "channel-switch.c"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#pragma RcB2 LINK "-lpcap" "-lpthread"
#include "../concol/console.h"
#include "../concol/console_keys.h"
#include "../concol/fonts/allfonts.h"
#ifdef NO_COLOR
#define console_setcolor(A, B, C) do {} while(0)
#endif
static int outfd;
static int usage(const char *argv0) {
dprintf(2, "%s [-c channel] network-interface\n"
"i.e.: %s wlan0\n", argv0, argv0
);
return 1;
}
/* originally 256, but that would make the struct too big for the stack */
#define WPS_MAX_STR_LEN 64
struct wps_data
{
uint8_t version;
uint8_t state;
uint8_t locked;
char manufacturer[WPS_MAX_STR_LEN];
char model_name[WPS_MAX_STR_LEN];
char model_number[WPS_MAX_STR_LEN];
char device_name[WPS_MAX_STR_LEN];
char ssid[WPS_MAX_STR_LEN];
char uuid[WPS_MAX_STR_LEN];
char serial[WPS_MAX_STR_LEN];
char selected_registrar[WPS_MAX_STR_LEN];
char response_type[WPS_MAX_STR_LEN];
char primary_device_type[WPS_MAX_STR_LEN];
char config_methods[WPS_MAX_STR_LEN];
char rf_bands[WPS_MAX_STR_LEN];
char os_version[WPS_MAX_STR_LEN];
};
static void init_wps_data(struct wps_data* wps) {
wps->version = 0;
wps->state = 0;
wps->locked = 0;
wps->manufacturer[0] = 0;
wps->model_name[0] = 0;
wps->model_number[0] = 0;
wps->device_name[0] = 0;
wps->ssid[0] = 0;
wps->uuid[0] = 0;
wps->serial[0] = 0;
wps->selected_registrar[0] = 0;
wps->response_type[0] = 0;
wps->primary_device_type[0] = 0;
wps->config_methods[0] = 0;
wps->rf_bands[0] = 0;
wps->os_version[0] = 0;
}
enum enctype {
ET_OPEN = 0,
ET_WEP,
ET_WPA,
ET_WPA2,
ET_MAX = ET_WPA2
};
struct ap_client {
long long total_rssi;
long long last_seen;
unsigned long count;
signed char last_rssi;
signed char min_rssi;
signed char max_rssi;
unsigned char mac[6];
struct ap_client *next;
};
struct wlaninfo {
struct wps_data *wps;
long long total_rssi;
long long last_seen;
uint64_t timestamp;
unsigned long count;
uint16_t beaconinterval;
char essid[32];
unsigned char mac[6];
unsigned char channel;
signed char last_rssi;
signed char min_rssi;
signed char max_rssi;
char enctype;
struct ap_client *clients;
};
#define STATIC_ALLOC 1
#if STATIC_ALLOC
static struct wlaninfo wlans_storage[256];
static unsigned wlan_count;
#define DYNA_NEW(X) wlans_storage
#define DYNA_GROW(X) (wlan_count++, wlans)
#define DYNA_COUNT(X) wlan_count
#else
#include "../lib/include/dynarray.h"
#endif
static struct wlaninfo *wlans;
static pthread_mutex_t wlan_lock = PTHREAD_MUTEX_INITIALIZER;
#define lock() pthread_mutex_lock(&wlan_lock)
#define unlock() pthread_mutex_unlock(&wlan_lock)
static inline void get_wlan(size_t index, struct wlaninfo* out) {
lock();
memcpy(out, &wlans[index], sizeof(*out));
unlock();
}
static inline void write_wlan(size_t index, struct wlaninfo* in) {
lock();
memcpy(&wlans[index], in, sizeof(*in));
unlock();
}
static signed char min, max;
static unsigned char selection, selected;
static Console co, *t = &co;
static int colorcount;
static int get_wlan_by_essid(char* essid) {
unsigned i, l;
int res = -1;
lock();
l = DYNA_COUNT(wlans);
for(i=0;i<l;i++) {
if(!strcmp(essid, wlans[i].essid)) {
res = i;
break;
}
}
unlock();
return res;
}
static int get_wlan_by_mac(unsigned char mac[6]) {
unsigned i, l;
int res = -1;
lock();
l = DYNA_COUNT(wlans);
for(i=0;i<l;i++) {
if(!memcmp(mac, wlans[i].mac, 6)) {
res = i;
break;
}
}
unlock();
return res;
}
static int get_new_wlan(void) {
lock();
void* p = DYNA_GROW(wlans);
if(!p) {
unlock();
return -1;
}
wlans = p;
memset(&wlans[DYNA_COUNT(wlans)-1], 0, sizeof(wlans[0]));
wlans[DYNA_COUNT(wlans)-1].min_rssi = 127;
wlans[DYNA_COUNT(wlans)-1].max_rssi = -127;
int res = DYNA_COUNT(wlans)-1;
unlock();
return res;
}
static struct ap_client *get_client(struct wlaninfo *w, unsigned char mac[6]) {
struct ap_client *c;
for(c = w->clients; c; c = c->next) {
if(!memcmp(mac, c->mac, 6)) return c;
}
return 0;
}
static struct ap_client *add_client(struct wlaninfo *w, unsigned char mac[6]) {
struct ap_client *c = calloc(1, sizeof(*c)), *it;
memcpy(c->mac, mac, 6);
c->min_rssi = 127;
c->max_rssi = -127;
if(!w->clients) w->clients = c;
else {
it = w->clients;
while(it->next) it=it->next;
it->next = c;
}
return c;
}
static long long getutime64(void);
static void set_client_rssi(struct wlaninfo* w, struct ap_client *c) {
c->count++;
c->total_rssi += w->last_rssi;
c->last_seen = getutime64();
c->last_rssi = w->last_rssi;
c->min_rssi = MIN(c->min_rssi, c->last_rssi);
c->max_rssi = MAX(c->max_rssi, c->last_rssi);
}
static int set_rssi(struct wlaninfo *w, struct wps_data* wps) {
int i = -1;
struct wlaninfo wtmp, *d = &wtmp;
if(i == -1) i = get_wlan_by_mac(w->mac);
if(i == -1) i = get_new_wlan();
if(i != -1) {
get_wlan(i, d);
if(w->essid[0]) strcpy(d->essid, w->essid);
memcpy(d->mac, w->mac, 6);
d->total_rssi += w->last_rssi;
d->count++;
d->last_rssi = w->last_rssi;
d->channel = w->channel;
d->timestamp = w->timestamp;
d->beaconinterval = w->beaconinterval;
d->min_rssi = MIN(d->min_rssi, d->last_rssi);
d->max_rssi = MAX(d->max_rssi, d->last_rssi);
d->enctype = w->enctype;
if(wps->version) {
if(!d->wps) {
d->wps = malloc(sizeof *wps);
if(d->wps) init_wps_data(d->wps);
}
if(d->wps) {
if(!wps->manufacturer[0]) {
d->wps->version = wps->version;
d->wps->state = wps->state;
d->wps->locked = wps->locked;
} else
memcpy(d->wps, wps, sizeof(*wps));
}
}
write_wlan(i, d);
}
return i;
}
volatile int stop;
void sigh(int x) {
stop = 1;
}
#include "radiotap_flags.h"
static unsigned get_flags_off(unsigned flags, unsigned start_off) {
return rt_get_flag_offset(flags, IEEE80211_RADIOTAP_FLAGS, start_off);
}
static unsigned get_dbm_off(unsigned flags, unsigned start_off) {
return rt_get_flag_offset(flags, IEEE80211_RADIOTAP_DBM_ANTSIGNAL, start_off);
}
static unsigned get_chan_off(unsigned flags, unsigned start_off) {
return rt_get_flag_offset(flags, IEEE80211_RADIOTAP_CHANNEL, start_off);
}
static unsigned channel_from_freq(unsigned freq) {
return freq==2484?14:(freq-2407)/5;
}
struct dot11frame {
uint16_t framecontrol;
uint16_t duration;
unsigned char receiver[6];
unsigned char source[6];
unsigned char bssid[6];
uint16_t sequence_no;
};
static unsigned char* find_tag(unsigned const char *tagdata, unsigned tag, unsigned bytes_left) {
while(bytes_left) {
if(*tagdata == tag) return (unsigned char*)tagdata;
unsigned tagsize = tagdata[1];
tagdata+=2+tagsize;
if(bytes_left < 2+tagsize) return 0;
bytes_left-=2+tagsize;
}
return 0;
}
static long long timeval2utime(struct timeval*t) {
return (t->tv_sec * 1000LL * 1000LL) + t->tv_usec;
}
static long long getutime64(void) {
struct timeval t;
gettimeofday(&t, NULL);
return timeval2utime(&t);
}
static int filebased;
static const unsigned char* pcap_next_wrapper(pcap_t *foo, struct pcap_pkthdr *h_out) {
if(!filebased) {
again:;
const unsigned char* ret = 0;
struct pcap_pkthdr *hdr_temp;
int err = pcap_next_ex(foo, &hdr_temp, &ret);
if(err == 1) {
/* skip malformed packets, like those emitted by busybox udhcpc */
struct ieee80211_radiotap_header *rh = (void*) ret;
if(rh->it_version != 0 || end_le16toh(rh->it_len) > hdr_temp->len)
goto again;
*h_out = *hdr_temp;
} else ret = 0;
if(ret && outfd != -1){
pcapfile_write_packet(outfd, h_out, ret);
}
return ret;
}
static long long pcap_file_start_time, start_time;
static unsigned char buf[2][2048];
static struct pcap_pkthdr h[2];
static int actbuf;
const unsigned char* ret;
if(start_time == 0 || getutime64() - start_time >= timeval2utime(&h[!actbuf].ts) - pcap_file_start_time) {
ret = pcap_next(foo, h_out);
if(ret) {
h[actbuf] = *h_out;
assert(h[actbuf].len <= sizeof buf[actbuf]);
memcpy(buf[actbuf], ret, h[actbuf].len);
actbuf = !actbuf;
}
if(!start_time) {
start_time = getutime64();
assert(ret);
pcap_file_start_time = timeval2utime(&h_out->ts);
return 0;
}
if(ret) {
*h_out = h[actbuf];
return buf[actbuf];
} else return 0;
} else
return 0;
}
static inline int myisascii(int x) {
return x >= ' ' && x < 127;
}
static void dump_packet(const unsigned char* data, size_t len) {
static const char atab[] = "0123456789abcdef";
char hex[24*2+1], ascii[24+1];
unsigned h = 0, a = 0;
int fill = ' ';
while(len) {
len--;
hex[h++] = atab[*data >> 4];
hex[h++] = atab[*data & 0xf];
ascii[a++] = myisascii(*data) ? *data : '.';
if(a == 24) {
dump:
hex[h] = 0;
ascii[a] = 0;
printf("%s\t%s\n", hex, ascii);
if(fill == '_') return; /* jump from filler */
a = 0;
h = 0;
}
data++;
}
if(a) {
filler:
while(a<24) {
hex[h++] = fill;
hex[h++] = fill;
ascii[a++] = fill;
}
goto dump;
}
a = 0;
fill = '_';
goto filler;
}
void setminmax(int val) {
min = MIN(min, val);
max = MAX(max, val);
char mmbuf[128];
snprintf(mmbuf, sizeof mmbuf, "min: %d, max: %d", min, max);
console_settitle(t, mmbuf);
}
static int get_next_ie(const unsigned char *data, size_t len, size_t *currpos) {
if(*currpos + 2 >= len) return 0;
*currpos = *currpos + 2 + data[*currpos + 1];
if(*currpos >= len) return 0;
return 1;
}
static int get_next_wps_el(const unsigned char *data, size_t len, size_t *currpos) {
if(*currpos + 4 >= len) return 0;
uint16_t el_len;
memcpy(&el_len, data + 2 + *currpos, 2);
el_len = end_be16toh(el_len);
*currpos = *currpos + 4 + el_len;
if(*currpos >= len) return 0;
return 1;
}
static void process_wps_tag(const unsigned char* tag, size_t len, struct wps_data *wps) {
unsigned const char *el;
char *str;
size_t el_iterator = 0, wfa_iterator, remain;
uint16_t el_id, el_len;
int hex;
do {
el = tag + el_iterator;
remain = len - el_iterator;
memcpy(&el_id, el, 2);
el_id = end_be16toh(el_id);
memcpy(&el_len, el+2, 2);
el_len = end_be16toh(el_len);
el += 4;
str = 0, hex = 0;
switch(el_id) {
case 0x104A: /* WPS_VERSION */
wps->version = *el;
break;
case 0x1044: /* WPS_STATE */
wps->state = *el;
break;
case 0x1057: /* WPS_LOCKED */
wps->locked = *el;
break;
case 0x1021: /* WPS_MANUFACTURER */
str = wps->manufacturer;
break;
case 0x1023: /*WPS_MODEL_NAME */
str = wps->model_name;
break;
case 0x1024:
str = wps->model_number;
break;
case 0x1011:
str = wps->device_name;
break;
case 0x1045:
str = wps->ssid;
break;
case 0x1047:
str = wps->uuid;
hex = 1;
break;
case 0x1042:
str = wps->serial;
break;
case 0x1041:
str = wps->selected_registrar;
hex = 1;
break;
case 0x103B:
str = wps->response_type;
hex = 1;
break;
case 0x1054:
str = wps->primary_device_type;
hex = 1;
break;
case 0x1008:
str = wps->config_methods;
hex = 1;
break;
case 0x103C:
str = wps->rf_bands;
hex = 1;
case 0x102D:
str = wps->os_version;
break;
case 0x1049: /* WPS_VENDOR_EXTENSION */
if(el_len >= 5 && !memcmp(el, "\x00\x37\x2A", 3)) { /* WFA_EXTENSION */
el_len -= 3;
el += 3;
wfa_iterator = 0;
do {
if(wfa_iterator+2 <= el_len && el[wfa_iterator] == 0 /* WPS_VERSION2_ID */) {
wps->version = el[2];
}
} while(get_next_ie(el, el_len, &wfa_iterator));
}
break;
}
if(str) {
size_t max;
if(hex) {
max = el_len >= WPS_MAX_STR_LEN/2 ? WPS_MAX_STR_LEN/2 - 1 : el_len;
while(max--) {
sprintf(str, "%02x", *el);
el++;
str += 2;
}
*str = 0;
} else {
max = el_len + 1 >= WPS_MAX_STR_LEN ? WPS_MAX_STR_LEN : el_len + 1;
snprintf(str, max, "%s", el);
}
}
} while(get_next_wps_el(tag, len, &el_iterator));
}
static void process_tags(const unsigned char* tagdata, size_t tagdata_len, struct wlaninfo *temp, struct wps_data *wps) {
unsigned const char *tag;
/* iterate through tags */
size_t ie_iterator = 0, remain;
do {
tag = tagdata + ie_iterator;
remain = tagdata_len - ie_iterator;
switch(tag[0]) {
case 0: /* essid tag */
if(tag[1] <= remain) {
memcpy(temp->essid, tag+2, tag[1]);
temp->essid[tag[1]] = 0;
}
break;
case 3: /* chan nr */
assert(tag[1] == 1);
temp->channel = tag[2];
break;
case 0x30: /* RSN_TAG_NUMBER */
temp->enctype = ET_WPA2;
break;
case 0xDD: /* VENDOR_SPECIFIC_TAG*/
if(tag[1] >= remain) break;
if(tag[1] >= 8 &&
!memcmp(tag+2, "\x00\x50\xF2\x01\x01\x00", 6))
temp->enctype = ET_WPA;
if(tag[1] > 4 && !memcmp(tag+2, "\x00\x50\xf2" /*micro$oft*/ "\x04" /*type WPS*/, 4))
process_wps_tag(tag+2+4, tag[1]-4, wps);
break;
}
} while(get_next_ie(tagdata, tagdata_len, &ie_iterator));
}
static int process_frame(pcap_t *foo) {
struct pcap_pkthdr h;
const unsigned char* data = pcap_next_wrapper(foo, &h);
if(data) {
if(console_getbackendtype(t) == cb_sdl && getenv("DEBUG")) dump_packet(data, h.len);
uint32_t flags, offset, fchksum;
if(!rt_get_presentflags(data, h.len, &flags, &offset))
return -1;
struct ieee80211_radiotap_header *rh = (void*) data;
unsigned rtap_data = offset;
if(flags & (1U << IEEE80211_RADIOTAP_FLAGS)) {
unsigned flags_off = get_flags_off(flags, rtap_data);
if(data[flags_off] & 0x10 /* IEEE80211_RADIOTAP_F_FCS */) {
/* TODO handle bad FCS IEEE80211_RADIOTAP_F_BADFCS 0x40 */
memcpy(&fchksum, data + h.len - 4, 4);
fchksum = end_le32toh(fchksum);
h.len -= 4;
}
}
struct wlaninfo temp = {0};
{
if(!(flags & (1U << IEEE80211_RADIOTAP_DBM_ANTSIGNAL))) return -1;
unsigned dbmoff = get_dbm_off(flags, rtap_data);
temp.last_rssi = ((signed char*)data)[dbmoff];
}
{
// if(!(flags & (1U << IEEE80211_RADIOTAP_CHANNEL))) return -1;
short freq;
unsigned chanoff = get_chan_off(flags, rtap_data);
memcpy(&freq, data+ chanoff, 2);
temp.channel = channel_from_freq(freq);
}
uint16_t framectl, fctype;
offset = end_le16toh(rh->it_len);
memcpy(&framectl, data+offset, 2);
framectl = end_le16toh(framectl);
struct dot11frame* beacon;
unsigned const char* tagdata;
unsigned pos;
uint16_t caps;
size_t tagdata_len;
struct wps_data wps;
switch(framectl) {
/* IEEE 802.11 packet type */
case 0x0080: /* beacon */
case 0x0050: /* probe response */
beacon = (void*)(data+offset);
memcpy(&temp.mac,beacon->source,6);
offset += sizeof(*beacon);
memcpy(&temp.timestamp,data+offset,8);
temp.timestamp = end_le64toh(temp.timestamp);
offset += 8;
memcpy(&temp.beaconinterval, data+offset,2);
temp.beaconinterval = end_le16toh(temp.beaconinterval);
offset += 2;
memcpy(&caps, data+offset, 2);
caps = end_le16toh(caps);
if(caps & 0x10 /* CAPABILITY_WEP */)
temp.enctype = ET_WEP;
offset += 2;
pos = offset;
tagdata = data+pos;
tagdata_len = h.len-pos;
init_wps_data(&wps);
process_tags(tagdata, tagdata_len, &temp, &wps);
setminmax(temp.last_rssi);
return set_rssi(&temp, &wps);
break;
case 0x00d4: /*ack*/
case 0x0040: /* probe request */
return -1;
default:
fctype = framectl & end_htole16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
if(fctype == end_htole16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA) ||
fctype == end_htole16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA) ||
fctype == end_htole16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC) ||
fctype == end_htole16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC)
) {
beacon = (void*)(data+offset);
if(!memcmp(beacon->receiver, "\x01\x80\xc2", 3) ||
!memcmp(beacon->receiver, "\x01\x00\x0c\xcc\xcc\xcc", 6) ||
!memcmp(beacon->receiver, "\xff\xff\xff", 3))
return -1;
int wifi_nr = get_wlan_by_mac(beacon->bssid);
/* ignore packets sent to unknown APs */
if(wifi_nr == -1) return -1;
unsigned char *client_mac;
if(memcmp(beacon->source, beacon->bssid, 6))
/* client to AP */
client_mac = beacon->source;
else
/* AP to client */
client_mac = beacon->receiver;
struct wlaninfo apbuf, *ap=&apbuf;
get_wlan(wifi_nr, ap);
struct ap_client* c = get_client(ap, client_mac);
if(!c) c = add_client(ap, client_mac);
write_wlan(wifi_nr, ap);
setminmax(temp.last_rssi);
if(client_mac == beacon->source) {
set_client_rssi(&temp, c);
return -1;
} else return wifi_nr;
}
else {
return -1;
}
}
//while(htonl(*(flags++)) & (1U << IEEE80211_RADIOTAP_EXT)) next_chunk+=4;
//dprintf(2, "got data\n");
//dump();
} else usleep(1);
return -1;
}
#if 0
static int next_chan(int chan) {
if(++chan > 11) chan = 1;
return chan;
}
#elif 1
static int next_chan(int chan) {
static char chanlist[]={1,5,9,13,2,6,10,14,3,7,11,4,8,12};
int i = 0;
for(i = 0; i < sizeof chanlist && chanlist[i] != chan; i++);
if(i >=13) return chanlist[0];
return chanlist[++i];
}
#else
static int next_chan(int chan) {
switch (chan) {
case 1: case 2: case 3: case 4: case 5:
return 6;
case 6: case 7: case 8: case 9: case 10:
return 11;
case 11: case 12: case 13:
/* uncomment next line if you leave in a country using chan 14 */
//return 14;
case 14:
return 1;
default:
assert(0);
return 0;
}
}
#endif
static struct {int w, h;} dim;
#define BGCOL RGB(33, 66, 133)
#define COL_BLACK RGB(0,0,0)
#define COL_WHITE RGB(255,255,255)
#define COL_YELLOW RGB(255,255,0)
static void draw_bg() {
unsigned x, y;
console_setcolor(t, 0, BGCOL);
for(y=0; y < dim.h; y++) {
console_goto(t, 0, y);
for(x = 0; x < dim.w; x++)
console_printchar(t, ' ', 0);
}
}
static unsigned reduce_color(unsigned val) {
unsigned a = val;
if (colorcount <= 8) {
a /= 85;
if(a > 2) a = 2;
static const unsigned tbl[] = {0, 127, 255};
a = tbl[a];
}
return a;
}
static int get_r(unsigned percent) {
return reduce_color((50 - percent/2) * 5);
}
static int get_g(unsigned percent) {
return reduce_color(percent/2 * 5);
}
static int get_a(unsigned age) {
return reduce_color(5+((50 - age)*5));
}
#define LINES_PER_NET 1
static void selection_move(int dir) {
if((int)selection+dir < 0) dir=0;
lock();
unsigned l = DYNA_COUNT(wlans);
unlock();
if((int)selection+dir >= l ||
((int)selection+dir)*LINES_PER_NET+1 >= dim.h) dir=0;
selection += dir;
}
static volatile unsigned bms;
static void set_bms(float percent) {
float max = 800, min=50;
float range=max-min;
float rpercent = range/100.f;
bms = min + (100 - percent) * rpercent;
}
char *mac2str(unsigned char mac[static 6], char buf[static 18]) {
unsigned m, x;
char hextab[16] = "0123456789abcdef";
for(m = 0, x=0 ; m<6; m++, x+=3) {
buf[x] = hextab[mac[m]>>4];
buf[x+1] = hextab[mac[m]&15];
buf[x+2] = ':';
}
buf[17]=0;
return buf;
}
static char* format_timestamp(uint64_t timestamp, char *ts) {
#define TSTP_SEC 1000000ULL /* 1 MHz clock -> 1 million ticks/sec */
#define TSTP_MIN (TSTP_SEC * 60ULL)
#define TSTP_HOUR (TSTP_MIN * 60ULL)
#define TSTP_DAY (TSTP_HOUR * 24ULL)
uint64_t rem;
unsigned days, hours, mins, secs;
days = timestamp / TSTP_DAY;
rem = timestamp % TSTP_DAY;
hours = rem / TSTP_HOUR;
rem %= TSTP_HOUR;
mins = rem / TSTP_MIN;
rem %= TSTP_MIN;
secs = rem / TSTP_SEC;
sprintf(ts, "%ud %02u:%02u:%02u", days, hours, mins, secs);
return ts;
}
static const char* enctype_str(enum enctype et) {
static const char enc_name[][5] = {
[ET_OPEN]= "OPEN",
[ET_WEP] = "WEP",
[ET_WPA] = "WPA",
[ET_WPA2]= "WPA2",
};
if(et > ET_MAX) abort();
return enc_name[et];
}
static char* sanitize_string(char *s, char *new) {
size_t i,j, l = strlen(s), ls=l;
for(i=0,j=0;i<ls;i++) {
if(s[i] < ' ' || s[i] > 127) {
sprintf(new + j, "\\x%02x", s[i] & 0xff);
j += 3;
} else new[j] = s[i];
j++;
}
new[j] = 0;
return new;
}
static unsigned gray_shade_from_timestamp(long long last_seen) {
long long now = getutime64();
long long age_ms = (now - last_seen)/1000;
age_ms=MIN(5000, age_ms)/100; /* seems we end up with a range 0-50 */
return get_a(age_ms);
}
#define ESSID_PRINT_START 1
#define ESSID_PRINT_END 32+ESSID_PRINT_START
#define ESSID_PRINT_LEN (ESSID_PRINT_END - ESSID_PRINT_START)
static void dump_wlan_info(unsigned wlanidx) {
struct wlaninfo wtmp, *w = &wtmp;
get_wlan(wlanidx, w);
unsigned line = 3, x, col1, col2, col3, col4;
console_setcolor(t, 0, BGCOL);
console_setcolor(t, 1, COL_WHITE);
col1 = x = 2;
console_goto(t, ++x, line);
char macbuf[18];
console_printf(t, "MAC %s", mac2str(w->mac, macbuf));
x += 25;
col2 = x;
console_goto(t, ++x, line);
console_printf(t, "CHAN %d", (int) w->channel);
x += 9 + 5;
col3 = x;
console_goto(t, ++x, line);
char ts[64];
format_timestamp(w->timestamp, ts);
console_printf(t, "UP: %s", ts);
x += strlen(ts) +5;
col4 = x;
console_goto(t, ++x, line);
console_printf(t, "BI %d ms", (int) w->beaconinterval);
line++;
x = col1;
console_goto(t, ++x, line);
console_printf(t, "AVG %.2f dBm", (double)w->total_rssi/(double)w->count);
//x += 14 + 5;
x = col2;
console_goto(t, ++x, line);
console_printf(t, "CURR %d dBm", w->last_rssi);
//x += 10 + 5;
x = col3;
console_goto(t, ++x, line);
console_printf(t, "MIN %d dBm", w->min_rssi);
//x += 9 + 5;
x = col4;
console_goto(t, ++x, line);
console_printf(t, "MAX %d dBm", w->max_rssi);
x += 9 + 5;
line++;
x = col1;
console_goto(t, ++x, line);
console_printf(t, "%4s", enctype_str(w->enctype));
x = col2;
console_goto(t, ++x, line);
if(w->wps) console_printf(t, "WPS %d.%d", w->wps->version >> 4, w->wps->version & 15);
x = col3;
console_goto(t, ++x, line);
if(w->wps) console_printf(t, w->wps->locked == 1 ? "LOCKED" : "-");
char sanbuf[WPS_MAX_STR_LEN*4+1];
x = col4;
console_goto(t, ++x, line);
if(w->wps && w->wps->manufacturer[0]) {
sanitize_string(w->wps->manufacturer, sanbuf);
console_printf(t, "%s", sanbuf);
}
line++;
x = col1;
console_goto(t, ++x, line);
if(w->wps && w->wps->model_name[0]) {
sanitize_string(w->wps->model_name, sanbuf);
console_printf(t, "%s", sanbuf);
}
x = col2;
console_goto(t, ++x, line);
if(w->wps && w->wps->model_number[0]) {
sanitize_string(w->wps->model_number, sanbuf);
console_printf(t, "%s", sanbuf);
}
x = col3;
console_goto(t, ++x, line);
if(w->wps && w->wps->device_name[0]) {
sanitize_string(w->wps->device_name, sanbuf);
console_printf(t, "%s", sanbuf);
}
x = col4;
console_goto(t, ++x, line);
if(w->wps && w->wps->serial[0]) {
sanitize_string(w->wps->serial, sanbuf);
console_printf(t, "%s", sanbuf);
}
line += 2;
if(w->clients) {
x = col1;
console_goto(t, ++x, line++);
console_printf(t, "CLIENT"
"%*s%s" "%*s%s" "%*s%s" "%*s%s",
21-6, "", "AVG",
15-3, "", "CURR",
12-4, "", "MIN",
12-3, "", "MAX"
);
}
struct ap_client *c;
for(c = w->clients; c; c = c->next) {
unsigned a = gray_shade_from_timestamp(c->last_seen);
console_setcolor(t, 1, RGB(a,a,a));