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
|
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <bio.h>
#include <fcall.h>
#include <libsec.h>
#include "dat.h"
#include "protos.h"
#include "y.tab.h"
int Cflag;
int pflag;
int Nflag;
int sflag;
int tiflag;
int toflag;
char *prom = "promiscuous";
enum
{
Pktlen= 64*1024,
Blen= 16*1024
};
Filter *filter;
Proto *root;
Biobuf out;
vlong starttime, pkttime;
int pcap;
int filterpkt(Filter *f, uchar *ps, uchar *pe, Proto *pr, int);
void printpkt(char *p, char *e, uchar *ps, uchar *pe);
void mkprotograph(void);
Proto* findproto(char *name);
Filter* compile(Filter *f);
void printfilter(Filter *f, char *tag);
void printhelp(char*);
void tracepkt(uchar*, int);
void pcaphdr(int);
struct pcap_pkthdr {
u64int ts; /* time stamp */
u32int caplen; /* length of portion present */
u32int len; /* length this packet (off wire) */
};
void
printusage(void)
{
fprint(2, "usage: %s [-CDdpst] [-N n] [-f filter] [-h first-header] path\n", argv0);
fprint(2, " for protocol help: %s -? [proto]\n", argv0);
}
void
usage(void)
{
printusage();
exits("usage");
}
void
main(int argc, char **argv)
{
uchar *pkt;
char *buf, *file, *p, *e;
int fd;
int n;
Binit(&out, 1, OWRITE);
fmtinstall('E', eipfmt);
fmtinstall('V', eipfmt);
fmtinstall('I', eipfmt);
fmtinstall('H', encodefmt);
fmtinstall('F', fcallfmt);
pkt = malloc(Pktlen+16);
pkt += 16;
buf = malloc(Blen);
e = buf+Blen-1;
pflag = 1;
Nflag = 32;
sflag = 0;
mkprotograph();
ARGBEGIN{
default:
usage();
case '?':
printusage();
printhelp(ARGF());
exits(0);
break;
case 'N':
p = EARGF(usage());
Nflag = atoi(p);
break;
case 'f':
p = EARGF(usage());
yyinit(p);
yyparse();
break;
case 's':
sflag = 1;
break;
case 'h':
p = EARGF(usage());
root = findproto(p);
if(root == nil)
sysfatal("unknown protocol: %s", p);
break;
case 'd':
toflag = 1;
break;
case 'D':
toflag = 1;
pcap = 1;
break;
case 't':
tiflag = 1;
break;
case 'T':
tiflag = 1;
pcap = 1;
break;
case 'C':
Cflag = 1;
break;
case 'p':
pflag = 0;
break;
}ARGEND;
if(argc > 1)
usage();
if(argc == 0)
file = nil;
else
file = argv[0];
if(tiflag){
if(file == nil)
sysfatal("must specify file with -t");
fd = open(file, OREAD);
if(fd < 0)
sysfatal("opening %s: %r", file);
}else{
fd = opendevice(file, pflag);
if(fd < 0)
sysfatal("opening device %s: %r", file);
}
if(root == nil)
root = ðer;
if(pcap)
pcaphdr(fd);
filter = compile(filter);
if(tiflag){
/* read a trace file */
for(;;){
if(pcap){
struct pcap_pkthdr *goo;
n = read(fd, pkt, 16);
if(n != 16)
break;
goo = (struct pcap_pkthdr*)pkt;
pkttime = goo->ts;
n = goo->caplen;
}else{
n = read(fd, pkt, 10);
if(n != 10)
break;
pkttime = NetL(pkt+2);
pkttime = (pkttime<<32) | NetL(pkt+6);
if(starttime == 0LL)
starttime = pkttime;
n = NetS(pkt);
}
if(readn(fd, pkt, n) != n)
break;
if(filterpkt(filter, pkt, pkt+n, root, 1))
if(toflag)
tracepkt(pkt, n);
else
printpkt(buf, e, pkt, pkt+n);
}
} else {
/* read a real time stream */
starttime = nsec();
for(;;){
n = root->framer(fd, pkt, Pktlen);
if(n <= 0)
break;
pkttime = nsec();
if(filterpkt(filter, pkt, pkt+n, root, 1))
if(toflag)
tracepkt(pkt, n);
else
printpkt(buf, e, pkt, pkt+n);
}
}
}
/* create a new filter node */
Filter*
newfilter(void)
{
Filter *f;
f = mallocz(sizeof(*f), 1);
if(f == nil)
sysfatal("newfilter: %r");
return f;
}
/*
* apply filter to packet
*/
int
_filterpkt(Filter *f, Msg *m)
{
Msg ma;
if(f == nil)
return 1;
switch(f->op){
case '!':
return !_filterpkt(f->l, m);
case LAND:
ma = *m;
return _filterpkt(f->l, &ma) && _filterpkt(f->r, m);
case LOR:
ma = *m;
return _filterpkt(f->l, &ma) || _filterpkt(f->r, m);
case WORD:
if(m->needroot){
if(m->pr != f->pr)
return 0;
m->needroot = 0;
}else{
if(m->pr && (m->pr->filter==nil || !(m->pr->filter)(f, m)))
return 0;
}
if(f->l == nil)
return 1;
m->pr = f->pr;
return _filterpkt(f->l, m);
}
sysfatal("internal error: filterpkt op: %d", f->op);
return 0;
}
int
filterpkt(Filter *f, uchar *ps, uchar *pe, Proto *pr, int needroot)
{
Msg m;
if(f == nil)
return 1;
m.needroot = needroot;
m.ps = ps;
m.pe = pe;
m.pr = pr;
return _filterpkt(f, &m);
}
/*
* from the Unix world
*/
#define PCAP_VERSION_MAJOR 2
#define PCAP_VERSION_MINOR 4
#define TCPDUMP_MAGIC 0xa1b2c3d4
struct pcap_file_header {
u32int magic;
u16int version_major;
u16int version_minor;
s32int thiszone; /* gmt to local correction */
u32int sigfigs; /* accuracy of timestamps */
u32int snaplen; /* max length saved portion of each pkt */
u32int linktype; /* data link type (DLT_*) */
};
/*
* pcap trace header
*/
void
pcaphdr(int fd)
{
if(tiflag){
struct pcap_file_header hdr;
if(readn(fd, &hdr, sizeof hdr) != sizeof hdr)
sysfatal("short header");
if(hdr.magic != TCPDUMP_MAGIC)
sysfatal("packet header %ux != %ux", hdr.magic, TCPDUMP_MAGIC);
if(hdr.version_major != PCAP_VERSION_MAJOR || hdr.version_minor != PCAP_VERSION_MINOR)
sysfatal("version %d.%d != %d.%d", hdr.version_major, hdr.version_minor, PCAP_VERSION_MAJOR, PCAP_VERSION_MINOR);
if(hdr.linktype != 1)
sysfatal("unknown linktype %d != 1 (ethernet)", hdr.linktype);
}
if(toflag){
struct pcap_file_header hdr;
hdr.magic = TCPDUMP_MAGIC;
hdr.version_major = PCAP_VERSION_MAJOR;
hdr.version_minor = PCAP_VERSION_MINOR;
hdr.thiszone = 0;
hdr.snaplen = 1500;
hdr.sigfigs = 0;
hdr.linktype = 1;
write(1, &hdr, sizeof(hdr));
}
}
/*
* write out a packet trace
*/
void
tracepkt(uchar *ps, int len)
{
struct pcap_pkthdr *goo;
if(pcap){
goo = (struct pcap_pkthdr*)(ps-16);
goo->ts = pkttime;
goo->caplen = len;
goo->len = len;
write(1, goo, len+16);
} else {
hnputs(ps-10, len);
hnputl(ps-8, pkttime>>32);
hnputl(ps-4, pkttime);
write(1, ps-10, len+10);
}
}
/*
* format and print a packet
*/
void
printpkt(char *p, char *e, uchar *ps, uchar *pe)
{
Msg m;
ulong dt;
dt = (pkttime-starttime)/1000000LL;
m.p = seprint(p, e, "%6.6uld ms ", dt);
m.ps = ps;
m.pe = pe;
m.e = e;
m.pr = root;
while(m.p < m.e){
if(!sflag)
m.p = seprint(m.p, m.e, "\n\t");
m.p = seprint(m.p, m.e, "%s(", m.pr->name);
if((*m.pr->seprint)(&m) < 0){
m.p = seprint(m.p, m.e, "TOO SHORT");
m.ps = m.pe;
}
m.p = seprint(m.p, m.e, ")");
if(m.pr == nil || m.ps >= m.pe)
break;
}
*m.p++ = '\n';
if(write(1, p, m.p - p) < 0)
sysfatal("stdout: %r");
}
Proto **xprotos;
int nprotos;
/* look up a protocol by its name */
Proto*
findproto(char *name)
{
int i;
for(i = 0; i < nprotos; i++)
if(strcmp(xprotos[i]->name, name) == 0)
return xprotos[i];
return nil;
}
/*
* add an undefined protocol to protos[]
*/
Proto*
addproto(char *name)
{
Proto *pr;
xprotos = realloc(xprotos, (nprotos+1)*sizeof(Proto*));
pr = malloc(sizeof *pr);
*pr = dump;
pr->name = name;
xprotos[nprotos++] = pr;
return pr;
}
/*
* build a graph of protocols, this could easily be circular. This
* links together all the multiplexing in the protocol modules.
*/
void
mkprotograph(void)
{
Proto **l;
Proto *pr;
Mux *m;
/* copy protos into a reallocable area */
for(nprotos = 0; protos[nprotos] != nil; nprotos++)
;
xprotos = malloc(nprotos*sizeof(Proto*));
memmove(xprotos, protos, nprotos*sizeof(Proto*));
for(l = protos; *l != nil; l++){
pr = *l;
for(m = pr->mux; m != nil && m->name != nil; m++){
m->pr = findproto(m->name);
if(m->pr == nil)
m->pr = addproto(m->name);
}
}
}
/*
* add in a protocol node
*/
static Filter*
addnode(Filter *f, Proto *pr)
{
Filter *nf;
nf = newfilter();
nf->pr = pr;
nf->s = pr->name;
nf->l = f;
nf->op = WORD;
return nf;
}
/*
* recurse through the protocol graph adding missing nodes
* to the filter if we reach the filter's protocol
*/
static Filter*
_fillin(Filter *f, Proto *last, int depth)
{
Mux *m;
Filter *nf;
if(depth-- <= 0)
return nil;
for(m = last->mux; m != nil && m->name != nil; m++){
if(m->pr == nil)
continue;
if(f->pr == m->pr)
return f;
nf = _fillin(f, m->pr, depth);
if(nf != nil)
return addnode(nf, m->pr);
}
return nil;
}
static Filter*
fillin(Filter *f, Proto *last)
{
int i;
Filter *nf;
/* hack to make sure top level node is the root */
if(last == nil){
if(f->pr == root)
return f;
f = fillin(f, root);
if(f == nil)
return nil;
return addnode(f, root);
}
/* breadth first search though the protocol graph */
nf = f;
for(i = 1; i < 20; i++){
nf = _fillin(f, last, i);
if(nf != nil)
break;
}
return nf;
}
/*
* massage tree so that all paths from the root to a leaf
* contain a filter node for each header.
*
* also, set f->pr where possible
*/
Filter*
complete(Filter *f, Proto *last)
{
Proto *pr;
if(f == nil)
return f;
/* do a depth first traversal of the filter tree */
switch(f->op){
case '!':
f->l = complete(f->l, last);
break;
case LAND:
case LOR:
f->l = complete(f->l, last);
f->r = complete(f->r, last);
break;
case '=':
break;
case WORD:
pr = findproto(f->s);
f->pr = pr;
if(pr == nil){
if(f->l != nil){
fprint(2, "%s unknown proto, ignoring params\n",
f->s);
f->l = nil;
}
} else {
f->l = complete(f->l, pr);
f = fillin(f, last);
if(f == nil)
sysfatal("internal error: can't get to %s", pr->name);
}
break;
}
return f;
}
/*
* merge common nodes under | and & moving the merged node
* above the | or &.
*
* do some constant foldong, e.g. `true & x' becomes x and
* 'true | x' becomes true.
*/
static int changed;
static Filter*
_optimize(Filter *f)
{
Filter *l;
if(f == nil)
return f;
switch(f->op){
case '!':
/* is child also a not */
if(f->l->op == '!'){
changed = 1;
return f->l->l;
}
break;
case LOR:
/* are two children the same protocol? */
if(f->l->op != f->r->op || f->r->op != WORD
|| f->l->pr != f->r->pr || f->l->pr == nil)
break; /* no optimization */
changed = 1;
/* constant folding */
/* if either child is childless, just return that */
if(f->l->l == nil)
return f->l;
else if(f->r->l == nil)
return f->r;
/* move the common node up, thow away one node */
l = f->l;
f->l = l->l;
f->r = f->r->l;
l->l = f;
return l;
case LAND:
/* are two children the same protocol? */
if(f->l->op != f->r->op || f->r->op != WORD
|| f->l->pr != f->r->pr || f->l->pr == nil)
break; /* no optimization */
changed = 1;
/* constant folding */
/* if either child is childless, ignore it */
if(f->l->l == nil)
return f->r;
else if(f->r->l == nil)
return f->l;
/* move the common node up, thow away one node */
l = f->l;
f->l = _optimize(l->l);
f->r = _optimize(f->r->l);
l->l = f;
return l;
}
f->l = _optimize(f->l);
f->r = _optimize(f->r);
return f;
}
Filter*
optimize(Filter *f)
{
do{
changed = 0;
f = _optimize(f);
}while(changed);
return f;
}
/*
* find any top level nodes that aren't the root
*/
int
findbogus(Filter *f)
{
int rv;
if(f->op != WORD){
rv = findbogus(f->l);
if(f->r)
rv |= findbogus(f->r);
return rv;
} else if(f->pr != root){
fprint(2, "bad top-level protocol: %s\n", f->s);
return 1;
}
return 0;
}
/*
* compile the filter
*/
static void
_compile(Filter *f, Proto *last)
{
if(f == nil)
return;
switch(f->op){
case '!':
_compile(f->l, last);
break;
case LOR:
case LAND:
_compile(f->l, last);
_compile(f->r, last);
break;
case WORD:
if(last != nil){
if(last->compile == nil)
sysfatal("unknown %s subprotocol: %s", f->pr->name, f->s);
(*last->compile)(f);
}
if(f->l)
_compile(f->l, f->pr);
break;
case '=':
if(last == nil)
sysfatal("internal error: compilewalk: badly formed tree");
if(last->compile == nil)
sysfatal("unknown %s field: %s", f->pr->name, f->s);
(*last->compile)(f);
break;
default:
sysfatal("internal error: compilewalk op: %d", f->op);
}
}
Filter*
compile(Filter *f)
{
if(f == nil)
return f;
/* fill in the missing header filters */
f = complete(f, nil);
/* constant folding */
f = optimize(f);
if(!toflag)
printfilter(f, "after optimize");
/* protocol specific compilations */
_compile(f, nil);
/* at this point, the root had better be the root proto */
if(findbogus(f)){
fprint(2, "bogus filter\n");
exits("bad filter");
}
return f;
}
/*
* parse a byte array
*/
int
parseba(uchar *to, char *from)
{
char nip[4];
char *p;
int i;
p = from;
for(i = 0; i < 16; i++){
if(*p == 0)
return -1;
nip[0] = *p++;
if(*p == 0)
return -1;
nip[1] = *p++;
nip[2] = 0;
to[i] = strtoul(nip, 0, 16);
}
return i;
}
/*
* compile WORD = WORD, becomes a single node with a subop
*/
void
compile_cmp(char *proto, Filter *f, Field *fld)
{
uchar x[IPaddrlen];
if(f->op != '=')
sysfatal("internal error: compile_cmp %s: not a cmp", proto);
for(; fld->name != nil; fld++){
if(strcmp(f->l->s, fld->name) == 0){
f->op = WORD;
f->subop = fld->subop;
switch(fld->ftype){
case Fnum:
f->ulv = atoi(f->r->s);
break;
case Fether:
parseether(f->a, f->r->s);
break;
case Fv4ip:
f->ulv = parseip(x, f->r->s);
break;
case Fv6ip:
parseip(f->a, f->r->s);
break;
case Fba:
parseba(f->a, f->r->s);
break;
default:
sysfatal("internal error: compile_cmp %s: %d",
proto, fld->ftype);
}
f->l = f->r = nil;
return;
}
}
sysfatal("unknown %s field in: %s = %s", proto, f->l->s, f->r->s);
}
void
_pf(Filter *f)
{
char *s;
if(f == nil)
return;
s = nil;
switch(f->op){
case '!':
fprint(2, "!");
_pf(f->l);
break;
case WORD:
fprint(2, "%s", f->s);
if(f->l != nil){
fprint(2, "(");
_pf(f->l);
fprint(2, ")");
}
break;
case LAND:
s = "&&";
goto print;
case LOR:
s = "||";
goto print;
case '=':
print:
_pf(f->l);
if(s)
fprint(2, " %s ", s);
else
fprint(2, " %c ", f->op);
_pf(f->r);
break;
default:
fprint(2, "???");
break;
}
}
void
printfilter(Filter *f, char *tag)
{
fprint(2, "%s: ", tag);
_pf(f);
fprint(2, "\n");
}
void
cat(void)
{
char buf[1024];
int n;
while((n = read(0, buf, sizeof buf)) > 0)
write(1, buf, n);
}
static int fd1 = -1;
void
startmc(void)
{
int p[2];
if(fd1 == -1)
fd1 = dup(1, -1);
if(pipe(p) < 0)
return;
switch(fork()){
case -1:
return;
default:
close(p[0]);
dup(p[1], 1);
if(p[1] != 1)
close(p[1]);
return;
case 0:
close(p[1]);
dup(p[0], 0);
if(p[0] != 0)
close(p[0]);
execl("/bin/mc", "mc", nil);
cat();
_exits(0);
}
}
void
stopmc(void)
{
close(1);
dup(fd1, 1);
waitpid();
}
void
printhelp(char *name)
{
int len;
Proto *pr, **l;
Mux *m;
Field *f;
char fmt[40];
if(name == nil){
print("protocols:\n");
startmc();
for(l=protos; (pr=*l) != nil; l++)
print(" %s\n", pr->name);
stopmc();
return;
}
pr = findproto(name);
if(pr == nil){
print("unknown protocol %s\n", name);
return;
}
if(pr->field){
print("%s's filter attributes:\n", pr->name);
len = 0;
for(f=pr->field; f->name; f++)
if(len < strlen(f->name))
len = strlen(f->name);
startmc();
for(f=pr->field; f->name; f++)
print(" %-*s - %s\n", len, f->name, f->help);
stopmc();
}
if(pr->mux){
print("%s's subprotos:\n", pr->name);
startmc();
snprint(fmt, sizeof fmt, " %s %%s\n", pr->valfmt);
for(m=pr->mux; m->name != nil; m++)
print(fmt, m->val, m->name);
stopmc();
}
}
/*
* demultiplex to next prototol header
*/
void
demux(Mux *mx, ulong val1, ulong val2, Msg *m, Proto *def)
{
m->pr = def;
for(mx = mx; mx->name != nil; mx++){
if(val1 == mx->val || val2 == mx->val){
m->pr = mx->pr;
break;
}
}
}
/*
* default framer just assumes the input packet is
* a single read
*/
int
defaultframer(int fd, uchar *pkt, int pktlen)
{
return read(fd, pkt, pktlen);
}
|