aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/ip/snoopy/llc.c
blob: bbc21986ff98e74cfc5ff55a813d5ea53199b9cb (plain)
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
/* 
 * LLC.  Only enough to dispatch to SNAP and IP.
 */

#include <u.h>
#include <libc.h>
#include <ip.h>
#include "dat.h"
#include "protos.h"

enum
{
	UFmt = 3,
	Gsap = 1,
	IG = 1,
	SFmt = 1,
	UPoll = 0x10,
	IsPoll = 0x100,
	XidFi = 0x81,
	
	SapNull = 0,
	SapGlobal = 0xff,
	Sap8021BI = 0x02,
	Sap8021BG = 0x03,
	SapSNA = 0x04,
	SapIP = 0x06,
	SapProwayNM = 0x0e,
	Sap8021D = 0x42,
	SapRS511 = 0x4e,
	SapISO8208 = 0x7e,
	SapProway = 0x8e,
	SapSnap = 0xaa,
	SapIpx = 0xe0,
	SapNetbeui = 0xf0,
	SapIsons = 0xfe,
};

static Mux p_mux[] =
{
// Linux gives llc -> snap not llc -> ip.
// If we don't tell snoopy about llc -> ip, then the default patterns
// like snoopy -h radiotap -f dns work better.
//	{ "ip", SapIP },	
	{ "snap", SapSnap },
	{ 0 }
};

typedef struct Hdr Hdr;
struct Hdr
{
	uchar dsap;
	uchar ssap;
	uchar dsapf;
	uchar ssapf;
	ushort ctl;
	uchar isu;
	int hdrlen;
};

static int
unpackhdr(uchar *p, uchar *ep, Hdr *h)
{
	if(p+3 > ep)
		return -1;
	h->dsapf = p[0];
	h->dsap = h->dsapf & ~IG;
	h->ssapf = p[1];
	h->ssap = h->ssapf & ~Gsap;
	h->ctl = p[2];
	h->hdrlen = 3;
	if((h->ctl&UFmt) == UFmt)
		h->isu = 1;
	else{
		if(p+4 > ep)
			return -1;
		h->hdrlen = 4;
		h->ctl = LittleS(p+2);
	}
	return 0;
}

enum
{
	Ossap,
	Odsap,
	Ot,
};

static Field p_fields[] =
{
	{ "ssap",	Fnum,	Ossap,	"ssap" },
	{ "dsap",	Fnum,	Odsap,	"dsap" },
	{ 0 }
};

static void
p_compile(Filter *f)
{
	Mux *m;

	if(f->op == '='){
		compile_cmp(llc.name, f, p_fields);
		return;
	}
	for(m = p_mux; m->name != nil; m++){
		if(strcmp(f->s, m->name) == 0){
			f->pr = m->pr;
			f->ulv = m->val;
			f->subop = Ot;
			return;
		}
	}
	sysfatal("unknown llc field or protocol: %s", f->s);
}

static int
p_filter(Filter *f, Msg *m)
{
	Hdr h;

	memset(&h, 0, sizeof h);
	if(unpackhdr(m->ps, m->pe, &h) < 0)
		return 0;
	m->ps += h.hdrlen;

	switch(f->subop){
	case Ossap:
		return f->ulv == h.ssap;
	case Odsap:
		return f->ulv == h.dsap;
	case Ot:
		return f->ulv == h.ssap && f->ulv == h.dsap;
	}
	return 0;
}

static int
p_seprint(Msg *m)
{
	Hdr h;
	
	memset(&h, 0, sizeof h);
	if(unpackhdr(m->ps, m->pe, &h) < 0)
		return -1;

	m->pr = &dump;
	m->p = seprint(m->p, m->e, "ssap=%02x dsap=%02x ctl=%04x", h.ssap, h.dsap, h.ctl);
	m->ps += h.hdrlen;
	m->pr = &dump;
	if(h.ssap == h.dsap){
		switch(h.ssap){
		case SapIP:
			m->pr = &ip;
			break;
		case SapSnap:
			m->pr = &snap;
			break;
		}
	}	
	return 0;
}

Proto llc =
{
	"llc",
	p_compile,
	p_filter,
	p_seprint,
	p_mux,
	nil,
	nil,
	defaultframer
};