aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/ip/snoopy/pppoe_disc.c
blob: c8a5aebac8eeb505ea611d45a91ea6e7b48fbd28 (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
174
#include <u.h>
#include <libc.h>
#include <ip.h>
#include "dat.h"
#include "protos.h"

typedef struct Hdr Hdr;
struct Hdr {
	uchar verstype;
	uchar code;
	uchar sessid[2];
	uchar length[2];	/* of payload */
};
enum
{
	HDRSIZE = 1+1+2+2
};

static Mux p_mux[] =
{
	{"ppp",		0,	} ,
	{0}
};

enum
{
	Overs,
	Otype,
	Ocode,
	Osess,
};

static Field p_fields[] = 
{
	{"v",	Fnum,	Overs,	"version",	} ,
	{"t",	Fnum,	Otype,	"type",	} ,
	{"c",	Fnum,	Ocode,	"code" } ,
	{"s",	Fnum,	Osess,	"sessid" } ,
	{0}
};

static void
p_compilesess(Filter *f)
{
//	Mux *m;

	if(f->op == '='){
		compile_cmp(pppoe_sess.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 pppoe field or protocol: %s", f->s);
}
static void
p_compiledisc(Filter *f)
{
//	Mux *m;

	if(f->op == '='){
		compile_cmp(pppoe_disc.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 pppoe field or protocol: %s", f->s);
}

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

	if(m->pe - m->ps < HDRSIZE)
		return 0;

	h = (Hdr*)m->ps;
	m->ps += HDRSIZE;

	switch(f->subop){
	case Overs:
		return (h->verstype>>4) == f->ulv;
	case Otype:
		return (h->verstype&0xF) == f->ulv;
	case Ocode:
		return h->code == f->ulv;
	case Osess:
		return NetS(h->sessid) == f->ulv;
	}
	return 0;
}

/* BUG: print all the discovery types */
static int
p_seprintdisc(Msg *m)
{
	Hdr *h;
	int len;

	len = m->pe - m->ps;
	if(len < HDRSIZE)
		return -1;

	h = (Hdr*)m->ps;
	m->ps += HDRSIZE;

	m->pr = nil;

	m->p = seprint(m->p, m->e, "v=%d t=%d c=0x%x s=0x%ux, len=%d",
		h->verstype>>4, h->verstype&0xF, h->code, NetS(h->sessid), NetS(h->length));

	return 0;
}

static int
p_seprintsess(Msg *m)
{
	Hdr *h;
	int len;

	len = m->pe - m->ps;
	if(len < HDRSIZE)
		return -1;

	h = (Hdr*)m->ps;
	m->ps += HDRSIZE;

	/* this will call ppp for me */
	demux(p_mux, 0, 0, m, &dump);

	m->p = seprint(m->p, m->e, "v=%d t=%d c=0x%x s=0x%ux, len=%d",
		h->verstype>>4, h->verstype&0xF, h->code, NetS(h->sessid), NetS(h->length));

	return 0;
}

Proto pppoe_disc =
{
	"pppoe_disc",
	p_compiledisc,
	p_filter,
	p_seprintdisc,
	p_mux,
	"%lud",
	p_fields,
	defaultframer
};

Proto pppoe_sess =
{
	"pppoe_sess",
	p_compilesess,
	p_filter,
	p_seprintsess,
	p_mux,
	"%lud",
	p_fields,
	defaultframer
};