cooking.c 15.1 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
1
2
3
4
5
6
7
8
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <pcap.h>
#include <sys/shm.h>
#include <netinet/in.h>
9
10
11
12
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <assert.h>
Arne Øslebø's avatar
Arne Øslebø committed
13
14
15
16
17
18
19
20

#include "mapid.h"
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "fhelp.h"
#include "cooking.h"
#include "list.h"
's avatar
committed
21
22
23
24
#include "pcapio.h"
#include "libnids/nids.h"

#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
Arne Øslebø's avatar
Arne Øslebø committed
25
26
27
28
29
30

#define COOKING "COOKING"
#define SERVER 0
#define CLIENT 1
#define FRAG_BUF_SIZE 16*1024

's avatar
committed
31
32
#define CHECKED 15
#define NON_CHECKED 16
Arne Øslebø's avatar
Arne Øslebø committed
33

's avatar
committed
34
35
static int threshold = 0;
static unsigned int timeout = 0;
's avatar
committed
36
//unsigned int client_size = 0,server_size=0;
Arne Øslebø's avatar
Arne Øslebø committed
37

's avatar
committed
38
struct mapidlibflow *mod_flow = NULL;
Arne Øslebø's avatar
Arne Øslebø committed
39

's avatar
committed
40
41
42
43
char* ret_client_data = NULL;
char* ret_server_data = NULL;
char* to_free = NULL;
char* to_free2 = NULL;
Arne Øslebø's avatar
Arne Øslebø committed
44

's avatar
committed
45
46
flist_t *ret_client_headers = NULL;
flist_t *ret_server_headers = NULL;
Arne Øslebø's avatar
Arne Øslebø committed
47

's avatar
committed
48
49
50
struct headers_data *headers = NULL;
char *precooked_data = NULL;
extern struct pcap_pkthdr *nids_last_pcap_header;
Arne Øslebø's avatar
Arne Øslebø committed
51

's avatar
committed
52
mapid_pkthdr_t *last_mapid_header;
Arne Øslebø's avatar
Arne Øslebø committed
53

's avatar
committed
54
int status;
Arne Øslebø's avatar
Arne Øslebø committed
55

's avatar
committed
56
57
int cooked_pkts = 0;
int un_cooked_pkts = 0;
58
59
60
61
unsigned char *last_dev_pkt;
int callbacks=0;

struct mapidlibflow *cook_tmp_flow;
Arne Øslebø's avatar
Arne Øslebø committed
62

's avatar
committed
63
static int cook_instance(mapidflib_function_instance_t* instance, flist_t *flist, mapidflib_flow_mod_t *flow_mod,function_manipulation_t* manip)
Arne Øslebø's avatar
Arne Øslebø committed
64
{
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

	flist_node_t *n;
	struct mapidlibflow *flow=NULL;
	mapidlib_instance_t *i;

	i = flow_mod->mi;
	n = flist_head(i->flowlist);

	while(n)
	{
		flow = flist_data(n);
		if(flow->fd == instance->fd)
		{
			break;
		}
		n = flist_next(n);
	}
	
	cook_tmp_flow=flow;


Arne Øslebø's avatar
Arne Øslebø committed
86
87
88
	return 0;
}

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
void create_mod_pkt(unsigned char *dev_pkt,struct mapidlibflow *flow,mapid_pkthdr_t *pkt_head) {
	
	ether_header* eth = NULL;
	ip_header* ip = NULL;
	tcp_header* tcp = NULL;
	int ether_len = 0, ip_len = 0, tcp_len = 0;
	
	eth = (ether_header*)dev_pkt;
	ether_len = sizeof(ether_header);
	ip = (ip_header*)(dev_pkt + ether_len);
	ip_len = (ip->ver_ihl & 0xf) * 4;
	
	tcp = (tcp_header*)(dev_pkt + ether_len + ip_len);
	tcp_len = tcp->off * 4;
	tcp->seq = 0;
	
's avatar
committed
105
	ip->tlen = ntohs(flow->client_size + ip_len + tcp_len);
106
	
's avatar
committed
107
	flow->mod_pkt = malloc(sizeof(char) * (flow->client_size + ether_len + ip_len + tcp_len));
108
	if(flow->mod_pkt==NULL) {
's avatar
committed
109
		//fprintf(stderr,"client size: %d\n",flow->client_size);
110
111
112
113
114
115
		assert(flow->mod_pkt!=NULL);
	}
	memcpy(flow->mod_pkt, eth, ether_len);
	memcpy(&flow->mod_pkt[ether_len], ip, ip_len);
	memcpy(&flow->mod_pkt[ether_len + ip_len], tcp, tcp_len);
	
's avatar
committed
116
	memcpy(&flow->mod_pkt[ether_len + ip_len + tcp_len], ret_client_data, flow->client_size);
117
118
	
	//server packet
's avatar
committed
119
	flow->server_mod_pkt = malloc(sizeof(char) * (flow->server_size + ether_len + ip_len + tcp_len));
120
	if(flow->server_mod_pkt==NULL) {
's avatar
committed
121
		fprintf(stderr,"client size: %d\n",flow->client_size);
122
123
124
125
126
		assert(flow->server_mod_pkt!=NULL);
	}
	memcpy(flow->server_mod_pkt, eth, ether_len);
	memcpy(&flow->server_mod_pkt[ether_len], ip, ip_len);
	memcpy(&flow->server_mod_pkt[ether_len + ip_len], tcp, tcp_len);
's avatar
committed
127
128
	memcpy(&flow->server_mod_pkt[ether_len + ip_len + tcp_len], ret_server_data, flow->server_size);
	flow->server_mod_pkt_head.caplen = flow->server_mod_pkt_head.wlen = flow->server_size + ether_len + ip_len + tcp_len;	
129
130
131
132

	flow->client_headers = ret_client_headers;
	flow->server_headers = ret_server_headers;

's avatar
committed
133
134
	pkt_head->caplen = flow->client_size + ether_len + ip_len + tcp_len;	
	pkt_head->wlen = flow->client_size + ether_len + ip_len + tcp_len;	
135
136
}

's avatar
committed
137
138
139
140
void process_flushed_data(struct mapidlibflow *flow) {
}


's avatar
committed
141
void tcp_callback(struct tcp_stream *ns, void **param)
Arne Øslebø's avatar
Arne Øslebø committed
142
{
143
144
	callbacks++;
	cooked_pkts++;
's avatar
committed
145
146
147
	struct mapidlibflow *flow;

	flow=ns->flow;
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

	if(ns->nids_state == NIDS_JUST_EST)
	{
		//fprintf(stderr,"JUST_EST\n");
		ns->client.collect++;
		if(threshold==-1) { 
			ns->client.reassembled_data = NULL; 
			ns->client_reassembly_limit=0;
		}
		else {
			ns->client.reassembled_data = (char *)malloc(sizeof(char) * threshold+2000);
		}
		ns->client.read = 0;
		ns->client.total_read = 0;
		if(ns->client.headers==NULL) {
			fprintf(stderr,"Initing 2..\n");
			ns->client.headers = (flist_t *)malloc(sizeof(flist_t));
			flist_init(ns->client.headers);
		}
		ns->client.pkt_count = 0;
		
		// and by a server
		ns->server.collect++;
		if(threshold==-1) { 
			ns->server.reassembled_data = NULL; 
			ns->server_reassembly_limit=0;
		}
		else {
			ns->server.reassembled_data = (char *)malloc(sizeof(char) * threshold+2000);
		}
		
		ns->server.read = 0;
		ns->server.total_read = 0;
		if(ns->server.headers==NULL) {
			ns->server.headers = (flist_t *)malloc(sizeof(flist_t));
			flist_init(ns->server.headers);
		}
		ns->server.pkt_count = 0;
		ns->flow=NULL;
		return;
	}

	status = CHECKED;

	if(ns->nids_state == NIDS_EXITING) {
		struct mapidlibflow *flow;
		
	    ret_client_data = ns->client.reassembled_data;
		ret_server_data = ns->server.reassembled_data;
's avatar
committed
197
198
		flow->client_size = ns->client.read;
		flow->server_size = ns->server.read;
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
		ret_client_headers = ns->client.headers;
		ret_server_headers = ns->server.headers;
		
		flow=(struct mapidlibflow *)(ns->flow);
		create_mod_pkt(last_dev_pkt,flow,&flow->mod_pkt_head);
		process_flushed_data(flow);
		return;
	}

	if(ns->nids_state == NIDS_CLOSE || ns->nids_state == NIDS_RESET || ns->nids_state == NIDS_TIMED_OUT || ns->nids_state == NIDS_EXITING)
	{
		//fprintf(stderr,"session closed %d   %d - %d\n",ns->nids_state==NIDS_CLOSE,ns->client.total_read,ns->client.count);

		if(to_free != NULL) {  free(to_free); to_free = NULL; }
		if(to_free2 != NULL) {  free(to_free2); to_free2 = NULL; }
		
		if(ns->client.total_read < ns->client.count || threshold == -1)
		{
			if(ret_client_data == NULL)
			{
	           	ret_client_data = ns->client.reassembled_data;
				ret_server_data = ns->server.reassembled_data;
				
				to_free = ret_client_data;
				to_free2 = ret_server_data;
				
's avatar
committed
225
226
				flow->client_size = ns->client.read;
				flow->server_size = ns->server.read;
227
228
229
230
231
				
				
				ret_client_headers = ns->client.headers;
				ret_server_headers = ns->server.headers;
				
's avatar
committed
232
				//fprintf(stderr,"final data: %d %d %d\n",flow->client_size,flist_size(ns->client.headers),ns->nids_state==NIDS_EXITING);
233
234
235
236
237
238
239
240
241
242
243
244
				return;
			}
		}
		else if(ns->server.total_read < ns->server.count || threshold ==-1)
		{
			if(ret_server_data == NULL)
			{
				ret_server_data = ns->server.reassembled_data;
	            ret_client_data = ns->client.reassembled_data;
				to_free = ret_server_data;
				to_free2 = ret_client_data;
				
's avatar
committed
245
246
				flow->server_size = ns->server.read;
				flow->client_size = ns->client.read;
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
				//fprintf(stderr,"++final data: %d %d\n",server_size,flist_size(ns->server.headers));
				
				ret_server_headers = ns->server.headers;
				ret_client_headers = ns->client.headers;

				return;
			}
		}
			
		return;
	}

	if(ns->nids_state == NIDS_DATA)
	{
		if(ns->client.count_new)
		{
			char *dest = NULL;
			ns->client.pkt_count++;
			
			if(threshold == -1 ) {
				if((ns->client.read + ns->client.count_new) > ns->client_reassembly_limit) {
					ns->client.reassembled_data=(unsigned char *)realloc(ns->client.reassembled_data,ns->client_reassembly_limit+INCR_SIZE);
					ns->client_reassembly_limit+=INCR_SIZE;
				}	
				
				dest = &ns->client.reassembled_data[ns->client.read];
				ns->client.total_read = ns->client.count;
				ns->client.read += (ns->client.count_new);
				memcpy(dest, ns->client.data, ns->client.count_new);
				return;
			}
			
			if((ns->client.read + ns->client.count_new) > threshold)	
			{
				dest = &ns->client.reassembled_data[ns->client.read];
			
				ret_client_headers = ns->client.headers;
				ret_client_data = ns->client.reassembled_data;
's avatar
committed
285
				flow->client_size = ns->client.read + ns->client.count_new;
286
287
288
289
290
				ns->client.total_read = ns->client.count;
				ns->client.read = 0;
				
				ret_server_headers = ns->server.headers;
				ret_server_data = ns->server.reassembled_data;
's avatar
committed
291
				flow->server_size=ns->server.read;
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
				ns->server.total_read = ns->server.count;
				ns->server.read = 0;
			
				memcpy(dest, ns->client.data, ns->client.count_new);
				return;	
			}
			else if((ns->client.count - ns->client.count_new) == 0)
			{
				dest = ns->client.reassembled_data;
			}
			else
			{
				dest = &ns->client.reassembled_data[ns->client.read];
			}
			
			memcpy(dest, ns->client.data, ns->client.count_new);
			ns->client.read += (ns->client.count_new);
		}
		
		if(ns->server.count_new)
		{
			char *dest = NULL;
			ns->server.pkt_count++;
			
			if(threshold == -1 ) {
				if((ns->server.read + ns->server.count_new) > ns->server_reassembly_limit) {
					ns->server.reassembled_data=(unsigned char *)realloc(ns->server.reassembled_data,ns->server_reassembly_limit+INCR_SIZE);
					ns->server_reassembly_limit+=INCR_SIZE;
				}	
				
				dest = &ns->server.reassembled_data[ns->server.read];
				ns->server.total_read = ns->server.count;
				ns->server.read += (ns->server.count_new);
				memcpy(dest, ns->server.data, ns->server.count_new);
				return;
			}
			
			if((ns->server.read + ns->server.count_new) > threshold)
			{
				dest = &ns->server.reassembled_data[ns->server.read];
				
				ret_server_headers = ns->server.headers;
				ret_server_data = ns->server.reassembled_data;
's avatar
committed
335
				flow->server_size = ns->server.read + ns->server.count_new;
336
337
338
				
				ret_client_headers = ns->client.headers;
				ret_client_data = ns->client.reassembled_data;
's avatar
committed
339
				flow->client_size = ns->client.read;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
				ns->client.total_read = ns->client.count;
				ns->client.read=0;
				
				ns->server.total_read = ns->server.count;
				ns->server.read = 0;

				memcpy(dest, ns->server.data, ns->server.count_new);
				return;
			}
			else if((ns->server.count - ns->server.count_new) == 0)
			{
				dest = ns->server.reassembled_data;
			}
			else
			{
				dest = &ns->server.reassembled_data[ns->server.read];
			} 
			memcpy(dest, ns->server.data, ns->server.count_new);
			ns->server.read += ns->server.count_new;
		}
	}

	return;
Arne Øslebø's avatar
Arne Øslebø committed
363
364
}

's avatar
committed
365
366
static int cook_init(mapidflib_function_instance_t *instance,
		     flist_t *flist)
Arne Øslebø's avatar
Arne Øslebø committed
367
{
's avatar
committed
368
369
370
	struct cooking_data* data = (struct cooking_data*)malloc(sizeof(struct cooking_data));
	mapiFunctArg* fargs;
	struct pcap desc;
Arne Øslebø's avatar
Arne Øslebø committed
371

's avatar
committed
372
373
374
	fargs=instance->args;
	data->threshold=getargint(&fargs);
	data->timeout=getargint(&fargs);
375
376
	data->flow=cook_tmp_flow;

's avatar
committed
377
378
379
	desc.fd = 1;
	desc.linktype = instance->hwinfo->link_type;
	desc.bufsize = instance->hwinfo->cap_length;
Arne Øslebø's avatar
Arne Øslebø committed
380

's avatar
committed
381
	nids_mapi_init(&desc, instance->hwinfo->link_type);
Arne Øslebø's avatar
Arne Øslebø committed
382

's avatar
committed
383
	nids_register_tcp(tcp_callback);
Arne Øslebø's avatar
Arne Øslebø committed
384

's avatar
committed
385
	if(data->threshold <= 0)
Arne Øslebø's avatar
Arne Øslebø committed
386
	{
's avatar
committed
387
		data->threshold = -1;
Arne Øslebø's avatar
Arne Øslebø committed
388
389
	}

's avatar
committed
390
391
	instance->internal_data=(void *)data;
	
Arne Øslebø's avatar
Arne Øslebø committed
392
393
394
395
396
	return 0;
}

//////////////////////////////////////////////

's avatar
committed
397
398
// return 1 in order for processing to continue in other functions
// return 0 not to
Arne Øslebø's avatar
Arne Øslebø committed
399

's avatar
committed
400
static int cook_process(mapidflib_function_instance_t *instance,unsigned char* dev_pkt, unsigned char* link_pkt, mapid_pkthdr_t* pkt_head)
Arne Øslebø's avatar
Arne Øslebø committed
401
{
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	
	struct pcap_pkthdr h;
	ether_header* eth = NULL;
	struct ip *iph;
	struct tcphdr *tcph;
	int ether_len = 0, ip_len = 0;
	struct mapidlibflow *flow;
	
	//fprintf(stderr,"I am COOKING\n");
	precooked_data = dev_pkt;

	last_dev_pkt= dev_pkt;
	last_mapid_header = pkt_head;
		
	h.caplen=pkt_head->caplen;
	h.len=pkt_head->wlen;
	h.ts.tv_sec=pkt_head->ts; //XXX 
	h.ts.tv_usec=pkt_head->ts;

	struct cooking_data* data = (struct cooking_data*)(instance->internal_data);
	threshold = data->threshold;
	timeout = data->timeout;
	flow=data->flow;
's avatar
committed
425
	
426
427
428
429
	status = NON_CHECKED;
	
	ret_client_data = NULL;
	ret_server_data = NULL;
's avatar
committed
430
	flow->client_size=flow->server_size=0;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

	callbacks=0;
	
	eth = (ether_header*)dev_pkt;
	ether_len = sizeof(ether_header);
	iph = (struct ip*)(dev_pkt + ether_len);
	ip_len = (iph->ip_hl & 0xf) * 4;
	if(iph->ip_p != IPPROTO_TCP)	// no TCP packet
		return 1;
	
	nids_mapi_next(&h, dev_pkt);	
	//if(callbacks>1)
	//fprintf(stderr,">> %d %d %d\n",callbacks,status,pkt_head->caplen);
	
	if(status == NON_CHECKED)
	{
		//fprintf(stderr,"\tnon check report\n");
		struct tcp_stream *stream;
		int from_client;

		tcph = (struct tcphdr *)(dev_pkt + ether_len + ip_len);

		stream= find_stream(tcph,iph,&from_client);
		if(stream) {
			//fprintf(stderr,"\tStream found\n");
			headers = malloc(sizeof(struct headers_data));
			headers->header = malloc(pkt_head->caplen); //added 100 more bytes
			memcpy(headers->header,dev_pkt,pkt_head->caplen);
			headers->caplen = pkt_head->caplen;
			headers->wlen = pkt_head->wlen;
			headers->ts.tv_sec = pkt_head->ts; //XXX
			headers->ts.tv_usec = pkt_head->ts;
			if(from_client==0) {
				if(stream->client.headers==NULL) {
					stream->client.headers = (flist_t *)malloc(sizeof(flist_t));
					flist_init(stream->client.headers);
				}
				flist_append(stream->client.headers, stream->client.pkt_count, (void *)headers);
			}
			else {
				if(stream->server.headers==NULL) {
					stream->server.headers = (flist_t *)malloc(sizeof(flist_t));
					flist_init(stream->server.headers);
				}
				flist_append(stream->server.headers, stream->server.pkt_count, (void *)headers);
			}

			stream->flow=(void *)flow;
		}
		else { //unsolicited data
			//fprintf(stderr,"Stream not found\n");
			return 1;
		}
		un_cooked_pkts++;
		return 0;
	}
	else {
		struct tcp_stream *stream;
		int from_client;
		
's avatar
committed
491
		//fprintf(stderr,"\tcheck report %d\n",flow->client_size);
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
		tcph = (struct tcphdr *)(dev_pkt + ether_len + ip_len);
		
		headers = malloc(sizeof(struct headers_data));
		headers->header = malloc(pkt_head->caplen); 
		memcpy(headers->header, dev_pkt,pkt_head->caplen);
		headers->caplen = pkt_head->caplen;
		headers->wlen = pkt_head->wlen;
		headers->ts.tv_sec = pkt_head->ts; //XXX
		headers->ts.tv_usec = pkt_head->ts;
		
		stream= find_stream(tcph,iph,&from_client);
		if(stream==NULL) {
			//fprintf(stderr,"\tNULL stream %d %d\n",headers->ts.tv_sec,headers->ts.tv_usec);
			create_mod_pkt(dev_pkt,flow,pkt_head);
			flist_append(flow->server_headers,0, (void *)headers);
			return 1;
		}
		
		stream->flow=(void *)flow;
		
		if(from_client==0) {
			if(stream->client.headers==NULL) {
				stream->client.headers = (flist_t *)malloc(sizeof(flist_t));
				flist_init(stream->client.headers);
			}
			flist_append(stream->client.headers, stream->client.pkt_count, (void *)headers);
		}
		else {
			if(stream->server.headers==NULL) {
				stream->server.headers = (flist_t *)malloc(sizeof(flist_t));
				flist_init(stream->server.headers);
			}
			flist_append(stream->server.headers, stream->server.pkt_count, (void *)headers);
		}
	}
	
	if(ret_client_data != NULL || ret_server_data!=NULL)
	{
		// Set pseudoheader and new cooked packet to return to the daemon
		create_mod_pkt(dev_pkt,flow,pkt_head);
		return 1;
	}

	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

}

//////////////////////////////////////////////

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
}

//////////////////////////////////////////////

static mapidflib_function_def_t finfo =
{
	"", //libname
	COOKING, //name
	"Cooking TCP/IP packets\nParameters:\n\tthreshold : int\n\ttimeout : int\n", //Description
	"ii", //argdescr
	MAPI_DEVICE_ALL, //Devoid
	MAPIRES_NONE,
	0, //shm size
	1, //modifies_pkts
	cook_instance, //instance
	cook_init, //init
	cook_process, //process
	NULL, //get_result,
	NULL, //change_args
	NULL, //reset
	cook_cleanup, //cleanup
	NULL, //client_init
	NULL, //client_read_result
	NULL  //client_cleanup
};

mapidflib_function_def_t* cooking_get_funct_info();

mapidflib_function_def_t* cooking_get_funct_info()
{
	return &finfo;
};