xref: /plan9/sys/src/cmd/ip/snoopy/ether.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1 #include <u.h>
2 #include <libc.h>
3 #include <ip.h>
4 #include "dat.h"
5 #include "protos.h"
6 
7 typedef struct Hdr	Hdr;
8 struct Hdr {
9 	uchar d[6];
10 	uchar s[6];
11 	uchar type[2];
12 	char data[1500];
13 };
14 #define	ETHERMINTU	60	/* minimum transmit size */
15 #define	ETHERMAXTU	1514	/* maximum transmit size */
16 #define ETHERHDRSIZE	14	/* size of an ethernet header */
17 
18 static Mux p_mux[] =
19 {
20 	{"ip",		0x0800,	} ,
21 	{"arp",		0x0806,	} ,
22 	{"rarp",	0x0806,	} ,
23 	{"ip6", 	0x86dd, } ,
24 	{"pppoe_disc",	0x8863, },
25 	{"pppoe_sess",	0x8864, },
26 	{0}
27 };
28 
29 enum
30 {
31 	Os,	// source
32 	Od,	// destination
33 	Oa,	// source or destination
34 	Ot,	// type
35 };
36 
37 static Field p_fields[] =
38 {
39 	{"s",	Fether,	Os,	"source address",	} ,
40 	{"d",	Fether,	Od,	"destination address",	} ,
41 	{"a",	Fether,	Oa,	"source|destination address" } ,
42 	{"sd",	Fether,	Oa,	"source|destination address" } ,
43 	{"t",	Fnum,	Ot,	"type" } ,
44 	{0}
45 };
46 
47 static void
48 p_compile(Filter *f)
49 {
50 	Mux *m;
51 
52 	if(f->op == '='){
53 		compile_cmp(ether.name, f, p_fields);
54 		return;
55 	}
56 	for(m = p_mux; m->name != nil; m++)
57 		if(strcmp(f->s, m->name) == 0){
58 			f->pr = m->pr;
59 			f->ulv = m->val;
60 			f->subop = Ot;
61 			return;
62 		}
63 	sysfatal("unknown ethernet field or protocol: %s", f->s);
64 }
65 
66 static int
67 p_filter(Filter *f, Msg *m)
68 {
69 	Hdr *h;
70 
71 	if(m->pe - m->ps < ETHERHDRSIZE)
72 		return 0;
73 
74 	h = (Hdr*)m->ps;
75 	m->ps += ETHERHDRSIZE;
76 
77 	switch(f->subop){
78 	case Os:
79 		return !memcmp(h->s, f->a, 6);
80 	case Od:
81 		return !memcmp(h->d, f->a, 6);
82 	case Oa:
83 		return memcmp(h->s, f->a, 6) == 0 || memcmp(h->d, f->a, 6) == 0;
84 	case Ot:
85 		return NetS(h->type) == f->ulv;
86 	}
87 	return 0;
88 }
89 
90 static int
91 p_seprint(Msg *m)
92 {
93 	Hdr *h;
94 	uint t;
95 	int len;
96 
97 	len = m->pe - m->ps;
98 	if(len < ETHERHDRSIZE)
99 		return -1;
100 
101 	h = (Hdr*)m->ps;
102 	m->ps += ETHERHDRSIZE;
103 
104 	t = NetS(h->type);
105 	demux(p_mux, t, t, m, &dump);
106 
107 	m->p = seprint(m->p, m->e, "s=%E d=%E pr=%4.4ux ln=%d", h->s, h->d,
108 		t, len);
109 	return 0;
110 }
111 
112 Proto ether =
113 {
114 	"ether",
115 	p_compile,
116 	p_filter,
117 	p_seprint,
118 	p_mux,
119 	p_fields,
120 	defaultframer
121 };
122