aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/bpf-engine.c
blob: d1bff53587350b429553fdf0221e3012c8631d7e (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
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
/* bpf-engine.c
 * ------------
 * The BPF engine used for offline ("display") filters in wiretap.
 * The code is taken from the Linux Socket Filter, and only slightly
 * modified for use in wiretap.
 *
 * Gilbert Ramirez <gram@verdict.uthscsa.edu>
 */

/*
 * Linux Socket Filter - Kernel level socket filtering
 *
 * Author:
 *     Jay Schulist <Jay.Schulist@spacs.k12.wi.us>
 *
 * Based on the design of:
 *     - The Berkeley Packet Filter
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <glib.h>
#include "wtap.h"
#include "bpf-engine.h"


/*
 * Decode and apply filter instructions to the skb->data.
 * Return length to keep, 0 for none. skb is the data we are
 * filtering, filter is the array of filter instructions, and
 * len is the number of filter blocks in the array.
 */
 
int bpf_run_filter(unsigned char *data, int len, struct bpf_instruction *filter, int flen)
{
	struct bpf_instruction *fentry;	/* We walk down these */
	guint32 A = 0;	   		/* Accumulator */
	guint32 X = 0;   		/* Index Register */
	guint32 mem[BPF_MEMWORDS];	/* Scratch Memory Store */
	int k;
	int pc;
	int *t;

	/*
	 * Process array of filter instructions.
	 */

	for(pc = 0; pc < flen; pc++)
	{
		fentry = &filter[pc];
		if(fentry->code & BPF_X)
			t=&X;
		else
			t=&fentry->k;
			
		switch(fentry->code)
		{
			case BPF_ALU|BPF_ADD|BPF_X:
			case BPF_ALU|BPF_ADD|BPF_K:
				A += *t;
				continue;

			case BPF_ALU|BPF_SUB|BPF_X:
			case BPF_ALU|BPF_SUB|BPF_K:
				A -= *t;
				continue;

			case BPF_ALU|BPF_MUL|BPF_X:
			case BPF_ALU|BPF_MUL|BPF_K:
				A *= *t;
				continue;

			case BPF_ALU|BPF_DIV|BPF_X:
			case BPF_ALU|BPF_DIV|BPF_K:
				if(*t == 0)
					return (0);
				A /= *t;
				continue;

			case BPF_ALU|BPF_AND|BPF_X:
			case BPF_ALU|BPF_AND|BPF_K:
				A &= *t;
				continue;

			case BPF_ALU|BPF_OR|BPF_X:
			case BPF_ALU|BPF_OR|BPF_K:
				A |= *t;
				continue;

			case BPF_ALU|BPF_LSH|BPF_X:
			case BPF_ALU|BPF_LSH|BPF_K:
				A <<= *t;
				continue;

			case BPF_ALU|BPF_RSH|BPF_X:
			case BPF_ALU|BPF_RSH|BPF_K:
				A >>= *t;
				continue;

			case BPF_ALU|BPF_NEG:
				A = -A;
				continue;

			case BPF_JMP|BPF_JA:
				pc += fentry->k;
				continue;

			case BPF_JMP|BPF_JGT|BPF_K:
				pc += (A > fentry->k) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JGE|BPF_K:
				pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JEQ|BPF_K:
				pc += (A == fentry->k) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JSET|BPF_K:
				pc += (A & fentry->k) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JGT|BPF_X:
				pc += (A > X) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JGE|BPF_X:
				pc += (A >= X) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JEQ|BPF_X:
				pc += (A == X) ? fentry->jt : fentry->jf;
				continue;

			case BPF_JMP|BPF_JSET|BPF_X:
				pc += (A & X) ? fentry->jt : fentry->jf;
				continue;
			case BPF_LD|BPF_W|BPF_ABS:
				k = fentry->k;
				if(k + sizeof(long) > len)
					return (0);
				A = pntohl(&data[k]);
				continue;

			case BPF_LD|BPF_H|BPF_ABS:
				k = fentry->k;
				if(k + sizeof(short) > len)
					return (0);
				A = pntohs(&data[k]);
				continue;

			case BPF_LD|BPF_B|BPF_ABS:
				k = fentry->k;
				if(k >= len)
					return (0);
				A = data[k];
				continue;

			case BPF_LD|BPF_W|BPF_LEN:
				A = len;
				continue;

			case BPF_LDX|BPF_W|BPF_LEN:
				X = len;
				continue;

                      case BPF_LD|BPF_W|BPF_IND:
				k = X + fentry->k;
				if(k + sizeof(guint32) > len)
					return (0);
                                A = pntohl(&data[k]);
				continue;

                       case BPF_LD|BPF_H|BPF_IND:
				k = X + fentry->k;
				if(k + sizeof(guint16) > len)
					return (0);
				A = pntohs(&data[k]);
				continue;

                       case BPF_LD|BPF_B|BPF_IND:
				k = X + fentry->k;
				if(k >= len)
					return (0);
				A = data[k];
				continue;

			case BPF_LDX|BPF_B|BPF_MSH:
				/*
				 *	Hack for BPF to handle TOS etc
				 */
				k = fentry->k;
				if(k >= len)
					return (0);
				X = (data[fentry->k] & 0xf) << 2;
				continue;

			case BPF_LD|BPF_IMM:
				A = fentry->k;
				continue;

			case BPF_LDX|BPF_IMM:
				X = fentry->k;
				continue;

                       case BPF_LD|BPF_MEM:
				A = mem[fentry->k];
				continue;

			case BPF_LDX|BPF_MEM:
				X = mem[fentry->k];
				continue;

			case BPF_MISC|BPF_TAX:
				X = A;
				continue;

			case BPF_MISC|BPF_TXA:
				A = X;
				continue;

			case BPF_RET|BPF_K:
				return ((unsigned int)fentry->k);

			case BPF_RET|BPF_A:
				return ((unsigned int)A);

			case BPF_ST:
				mem[fentry->k] = A;
				continue;

			case BPF_STX:
				mem[fentry->k] = X;
				continue;



			default:
				/* Invalid instruction counts as RET */
				return (0);
		}
	}

	g_error("Filter ruleset ran off the end.\n");
	return (0);
}

/*
 * Check the user's filter code. If we let some ugly
 * filter code slip through kaboom!
 */

int bpf_chk_filter(struct bpf_instruction *filter, int flen)
{
	struct bpf_instruction *ftest;
        int pc;

       /*
        * Check the filter code now.
        */
	for(pc = 0; pc < flen; pc++)
	{
		/*
                 *	All jumps are forward as they are not signed
                 */
                 
                ftest = &filter[pc];
		if(BPF_CLASS(ftest->code) == BPF_JMP)
		{	
			/*
			 *	But they mustn't jump off the end.
			 */
			if(BPF_OP(ftest->code) == BPF_JA)
			{
				if(pc + ftest->k + 1>= (unsigned)flen)
					return (-1);
			}
                        else
			{
				/*
				 *	For conditionals both must be safe
				 */
 				if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
					return (-1);
			}
                }

                /*
                 *	Check that memory operations use valid addresses.
                 */
                 
                if(ftest->k <0 || ftest->k >= BPF_MEMWORDS)
                {
                	/*
                	 *	But it might not be a memory operation...
                	 */
                	 
                	if (BPF_CLASS(ftest->code) == BPF_ST)
                		return -1;
			if((BPF_CLASS(ftest->code) == BPF_LD) && 
				(BPF_MODE(ftest->code) == BPF_MEM))
	                        	return (-1);
		}
        }

	/*
	 *	The program must end with a return. We don't care where they
	 *	jumped within the script (its always forwards) but in the
	 *	end they _will_ hit this.
	 */
	 
        return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-1;
}