aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/bpf.c
blob: 3f02859d13415e0588a4bac9c0af359237423d48 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * bpf.c
 * -----
 * Creates and handles the BPF code produced by wiretap.
 *
 * Gilbert Ramirez
 */

#ifndef __G_LIB_H__
#include <glib.h>
#endif

#include <sys/types.h>
#include <netinet/in.h>

#include "wtap.h"
#include "rt-compile.h"
#include "rt-global.h"
#include "bpf-engine.h"
#include "bpf.h"


static GList *bpf_code_just_parsed = NULL;
static struct bpf_instruction *bpf_record = NULL;

static int
bpf_clean_jump(GList *L, int i_this, int jmp, int num_bpf_instructions,
		int i_ret_success, int i_ret_failure);
static void
bpf_pass1(GList *L);

static GList*
bpf_mk_bytecmp(int ftype, int rel_opcode, guint8 *bytes);

static void
bpf_optimize(GList *L);

static int
bpf_attach(wtap *wth);

static void
bpf_attach_record(gpointer bpf_code, gpointer junk);

static int
offline_attach(wtap *wth);


/* sets function pointers in rt-grammar.y to point to the BPF-related
 * functions */
void
wtap_filter_bpf_init(void)
{
	mk_bytecmp = bpf_mk_bytecmp;
	mk_optimize = bpf_optimize;
	mk_attach = bpf_attach;
}

/* almost the same as bpf_init... */
void
wtap_filter_offline_init(wtap *wth)
{
	int fi; /* filter index */

	mk_bytecmp = bpf_mk_bytecmp;
	mk_optimize = bpf_optimize;
	mk_attach = offline_attach;

	wtap_filter_offline_clear(wth);

	/* make the offline filter array */
	wth->filter.offline = g_malloc(sizeof(int*) * WTAP_NUM_ENCAP_TYPES);
	wth->filter_type = WTAP_FILTER_OFFLINE;
	wth->offline_filter_lengths = g_malloc(sizeof(int) * WTAP_NUM_ENCAP_TYPES);

	for (fi = 0; fi < WTAP_NUM_ENCAP_TYPES; fi++) {
		wth->filter.offline[fi] = NULL;
	}
}

/* Removes an offline filter from a wtap struct, and frees memory used
 * by that filter */
void
wtap_filter_offline_clear(wtap *wth)
{
	int fi; /* filter index */

	if (wth->filter.offline) {
		for (fi = 0; fi < WTAP_NUM_ENCAP_TYPES; fi++) {
			if (wth->filter.offline[fi])
				g_free(wth->filter.offline[fi]);
		}
		g_free(wth->filter.offline);
		g_free(wth->offline_filter_lengths);
	}
	wth->filter_type = WTAP_FILTER_NONE;
}

/* Allocate a new bpf_code_unit structure and initialize the BPF instruction
 * codes to the values passed by the caller. */
static struct bpf_code_unit *
bpf_code_unit_alloc(guint8 label, guint16 code, guint8 jt, guint8 jf, guint32 k)
{
	struct bpf_code_unit *bpf;

	bpf = g_malloc(sizeof(struct bpf_code_unit));
	bpf->line_label = label;
	bpf->bpf.code = code;
	bpf->bpf.jt = jt;
	bpf->bpf.jf = jf;
	bpf->bpf.k = k;

	/*g_print("{ %d { 0x%02x, %d, %d, 0x%08x }},\n",
			label, code, jt, jf, k);*/
	return bpf;
}


/* Finds ftype in the bytecmp_table, the relation, and the n-string
byte array, and creates BPF that will check those bytes */
static GList*
bpf_mk_bytecmp(int ftype, int rel_opcode, guint8 *bytes)
{
	GList	*L;
	struct bpf_code_unit *bpf;
	int	len_to_cmp, offset, endpoint, label;
	bytecmp_info *b;

	L = g_list_alloc();

	/* find the field in the table */
	b = lookup_bytecmp(ftype);

	/* How many bytes do we have to compare, and where? */
	len_to_cmp = b->length;
	offset = b->offset;
	endpoint = len_to_cmp + offset;
	/*g_print("len_to_cmp=%d, offset=%d, endpoint=%d\n",
			len_to_cmp, offset, endpoint);
	g_print("bytes: (%d) %02X:%02X:%02X\n",
			bytes[0], bytes[1], bytes[2], bytes[3]);*/

	label = NEXT_BLOCK;
	/* loop until we have written instructions to compare
		all bytes */
	while (len_to_cmp) {

		if (len_to_cmp >= 4) {
			bpf = bpf_code_unit_alloc(label,
					BPF_LD|BPF_W|BPF_ABS,
					0, 0, endpoint - 4);
			g_list_append(L, bpf);
			label = NO_LABEL;

			endpoint -= 4;
			bpf = bpf_code_unit_alloc(NO_LABEL,
					BPF_JMP|BPF_JEQ,
					(len_to_cmp == 4 ? END_OF_PROGRAM_SUCCESS : 0),
					NEXT_BLOCK,
					phtonl(&bytes[len_to_cmp-3]));
			g_list_append(L, bpf);

			len_to_cmp -= 4;
		}
		else if (len_to_cmp == 3) {
			bpf = bpf_code_unit_alloc(label,
					BPF_LD|BPF_W|BPF_ABS,
					0, 0, endpoint - 3);
			g_list_append(L, bpf);
			label = NO_LABEL;
			endpoint -= 3;

			bpf = bpf_code_unit_alloc(NO_LABEL,
					BPF_ALU|BPF_AND,
					0, 0, 0xffffff);
					/*htonl(0xffffff));*/
			g_list_append(L, bpf);

			bpf = bpf_code_unit_alloc(NO_LABEL,
					BPF_JMP|BPF_JEQ,
					(len_to_cmp == 3 ? END_OF_PROGRAM_SUCCESS : 0),
					NEXT_BLOCK,
					phtonl(&bytes[len_to_cmp-2]) & 0xffffff00);
			g_list_append(L, bpf);

			len_to_cmp -= 3;
		}
		else if (len_to_cmp == 2) {
			bpf = bpf_code_unit_alloc(label,
					BPF_LD|BPF_H|BPF_ABS,
					0, 0, endpoint - 2);
			g_list_append(L, bpf);
			label = NO_LABEL;

			endpoint -= 2;
			bpf = bpf_code_unit_alloc(NO_LABEL,
					BPF_JMP|BPF_JEQ,
					(len_to_cmp == 2 ? END_OF_PROGRAM_SUCCESS : 0),
					NEXT_BLOCK,
					(guint32)phtons(&bytes[len_to_cmp-1]));
			g_list_append(L, bpf);

			len_to_cmp -= 2;
		}
		else if (len_to_cmp == 1) {
			bpf = bpf_code_unit_alloc(label,
					BPF_LD|BPF_B|BPF_ABS,
					0, 0, endpoint - 1);
			g_list_append(L, bpf);
			label = NO_LABEL;

			endpoint--;
			bpf = bpf_code_unit_alloc(NO_LABEL,
					BPF_JMP|BPF_JEQ,
					END_OF_PROGRAM_SUCCESS, NEXT_BLOCK,
					bytes[len_to_cmp]);
			g_list_append(L, bpf);
			len_to_cmp--;
		}
	}


	L = g_list_remove(L, 0);
	return L;
}


static void
bpf_optimize(GList *L)
{
	bpf_pass1(L);
	bpf_code_just_parsed = L;
}

/* after the BPF code is constructed from the parser, this step is run. During
 * pass1 we:
 *
 * 1. Clean up the jump variables
 */
static void
bpf_pass1(GList *L)
{
	struct bpf_code_unit *bpf;
	int	num_bpf_instructions;
	int	i_ret_success;
	int	i_ret_failure;
	int	i;

	/* Attach a SUCCESS return to the end of the BPF code */
	bpf = bpf_code_unit_alloc(END_OF_PROGRAM_SUCCESS, BPF_RET, 0, 0, 0xffff);
	g_list_append(L, bpf);

	/* Attach a FAILURE return to the end of the BPF code */
	bpf = bpf_code_unit_alloc(END_OF_PROGRAM_FAILURE, BPF_RET, 0, 0, 0);
	g_list_append(L, bpf);

	num_bpf_instructions = g_list_length(L);
	i_ret_success = num_bpf_instructions - 2;
	i_ret_failure = num_bpf_instructions - 1;

	for(i = 0; i < num_bpf_instructions; i++) {
		bpf = (struct bpf_code_unit*) g_list_nth_data(L, i);
		if (!bpf)
			continue;

		/* Check for Jump to end failure/success */
		if (bpf->bpf.code & BPF_JMP) {

			bpf->bpf.jt = bpf_clean_jump(L, i, bpf->bpf.jt, num_bpf_instructions,
					i_ret_success, i_ret_failure);

			bpf->bpf.jf = bpf_clean_jump(L, i, bpf->bpf.jf, num_bpf_instructions,
					i_ret_success, i_ret_failure);
		}
	}
}

static int
bpf_clean_jump(GList *L, int i_this, int jmp, int num_bpf_instructions,
		int i_ret_success, int i_ret_failure)
{
	int i;
	struct bpf_code_unit *bpf;

	switch(jmp) {
		case END_OF_PROGRAM_SUCCESS:
			return i_ret_success - i_this - 1;

		case END_OF_PROGRAM_FAILURE:
			return i_ret_failure - i_this - 1;

		case NEXT_BLOCK:
			for (i = i_this + 1; i < num_bpf_instructions; i++) {
				bpf = (struct bpf_code_unit*) g_list_nth_data(L, i);
				if (!bpf)
					continue;
				if (bpf->line_label == NEXT_BLOCK) {
					return i - i_this - 1;
				}
			}
			/* failed to find NEXT_BLOCK.... chose FAILURE */
			return i_ret_failure - i_this - 1;

		/* default: nothing */
	}
	return jmp;
}



/* Takes code from bpf_code_just_parsed and attaches it to wth
 * returns 1 if sucessfull, 0 if not */
static int bpf_attach(wtap *wth)
{
	if (wth->filter.bpf)
		g_free(wth->filter.bpf);

	/* filter_length will be number of bpf_block records */
	wth->filter_length = g_list_length(bpf_code_just_parsed) - 1;

	wth->filter.bpf = g_malloc(wth->filter_length *
				sizeof(struct bpf_instruction));
	wth->filter_type = WTAP_FILTER_BPF;

	bpf_record = wth->filter.bpf;

	g_list_foreach(bpf_code_just_parsed, bpf_attach_record, NULL);

	if (bpf_chk_filter(wth->filter.bpf, wth->filter_length) == 0)
		return 1;
	else
		return 0;

}

void bpf_attach_record(gpointer bpf_code, gpointer junk)
{
	struct bpf_code_unit *bpf_c = (struct bpf_code_unit*) bpf_code;

	struct bpf_instruction *bpf_i;

	if (!bpf_c)
		return;

	bpf_i = &(bpf_c->bpf);
	memcpy(bpf_record, bpf_i, sizeof(struct bpf_instruction));
	bpf_record++;
}


/* Takes code from bpf_code_just_parsed and attachs it to wth.
 * returns 1 if sucessfull, 0 if not */
static int offline_attach(wtap *wth)
{
	/* filter_length will be number of bpf_instruction records */
	wth->offline_filter_lengths[comp_encap_type] =
		g_list_length(bpf_code_just_parsed);

	/* Make space for this filter */
	wth->filter.offline[comp_encap_type] =
		g_malloc(wth->offline_filter_lengths[comp_encap_type]
				* sizeof(struct bpf_instruction));

	bpf_record = wth->filter.offline[comp_encap_type];

	g_list_foreach(bpf_code_just_parsed, bpf_attach_record, NULL);

	if (bpf_chk_filter(wth->filter.offline[comp_encap_type],
			wth->offline_filter_lengths[comp_encap_type]) == 0)
		return 1;
	else
		return 0;
}