-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.c
236 lines (195 loc) · 3.89 KB
/
filter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <net/ethernet.h>
#include "ethdump.h"
/* The type of a function for looking up fields in a packet. */
typedef struct value (*lookup_fn)(struct packet *p);
struct field_ent {
char *name;
lookup_fn fn;
};
struct value
ipsrc(struct packet *p)
{
struct value value;
if (ntohs(p->eh->ether_type) != ETHERTYPE_IP) {
value.type = None;
return value;
}
value.type = IP4Addr;
value.v.ipaddr = ntohl(p->iph->saddr);
return value;
}
struct value
ipdst(struct packet *p)
{
struct value value;
if (ntohs(p->eh->ether_type) != ETHERTYPE_IP) {
value.type = None;
return value;
}
value.type = IP4Addr;
value.v.ipaddr = ntohl(p->iph->daddr);
return value;
}
struct value
ipproto(struct packet *p)
{
struct value value;
if (htons(p->eh->ether_type) != ETHERTYPE_IP) {
value.type = None;
return value;
}
value.type = Number;
value.v.number = p->iph->protocol;
return value;
}
struct value
ethsrc(struct packet *p) {
int i;
struct value value;
value.type = EthAddr;
for (i = 0; i < ETH_ALEN; i++)
value.v.ethaddr[i] = p->eh->ether_shost[i];
return value;
}
struct value
ethdst(struct packet *p)
{
int i;
struct value value;
value.type = EthAddr;
for (i = 0; i < ETH_ALEN; i++)
value.v.ethaddr[i] = p->eh->ether_dhost[i];
return value;
}
struct value
ethtype(struct packet *p)
{
struct value value;
value.type = Number;
value.v.number = ntohs(p->eh->ether_type);
return value;
}
struct field_ent fieldtable[] = {
{ .name = "ethsrc", .fn = ethsrc },
{ .name = "ethdst", .fn = ethdst },
{ .name = "ethtype", .fn = ethtype },
{ .name = "ipsrc", .fn = ipsrc },
{ .name = "ipdst", .fn = ipdst },
{ .name = "iptype", .fn = ipproto },
{ NULL },
};
typedef int (*filter_fn)(struct value *, struct value *);
struct op_ent {
char *name;
filter_fn fn;
};
int
notequals(struct value *lhs, struct value *rhs)
{
int i;
if (!lhs)
return !!rhs;
if (lhs->type != rhs->type)
return 1;
switch (lhs->type) {
case None:
return 0;
case Number:
return lhs->v.number != rhs->v.number;
case EthAddr:
for (i = 0; i < ETH_ALEN; i++)
if (lhs->v.ethaddr[i] == rhs->v.ethaddr[i])
return 0;
return 1;
case IP4Addr:
return lhs->v.ipaddr != rhs->v.ipaddr;
}
return 0;
}
int
equals(struct value *lhs, struct value *rhs)
{
int i;
if (!lhs)
return lhs == rhs;
if (lhs->type != rhs->type)
return 0;
switch (lhs->type) {
case None:
return 1;
case Number:
return lhs->v.number == rhs->v.number;
case EthAddr:
for (i = 0; i < ETH_ALEN; i++)
if (lhs->v.ethaddr[i] != rhs->v.ethaddr[i])
return 0;
return 1;
case IP4Addr:
return lhs->v.ipaddr == rhs->v.ipaddr;
}
return 0;
}
struct op_ent optable[] = {
{ .name = "==", .fn = equals },
{ .name = "!=", .fn = notequals },
{ NULL },
};
void
printvalue(struct value value)
{
int i;
switch (value.type) {
case Number:
printf("%d", value.v.number);
break;
case EthAddr:
for (i = 0; i < ETH_ALEN; i++) {
printf("%x", value.v.ethaddr[i]);
if (i < ETH_ALEN - 1)
printf(":");
}
break;
}
}
/* Determine whether the packet should be filtered or not. */
int
filterpacket(struct packet *p, struct filter *f)
{
struct value value;
lookup_fn lookup = NULL;
filter_fn filter = NULL;
struct field_ent *fp;
struct op_ent *op;
if (!f)
return Show;
for (fp = fieldtable; fp->name != NULL; fp++) {
if (strcmp(fp->name, f->field) == 0) {
lookup = fp->fn;
break;
}
}
if (!lookup) {
fprintf(stderr, "Unrecognized field: %s\n", f->field);
return Error;
}
for (op = optable; op->name != NULL; op++) {
if (strcmp(op->name, f->op) == 0) {
filter = op->fn;
break;
}
}
if (!filter) {
fprintf(stderr, "Unrecognized operator: %s\n", f->op);
return Error;
}
value = lookup(p);
if (filter(&value, &f->value))
return Show;
return Ignore;
}