cooking.c 15.3 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

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

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

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

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

's avatar
committed
39
40
char* to_free = NULL;
char* to_free2 = NULL;
Arne Øslebø's avatar
Arne Øslebø committed
41
42


's avatar
committed
43
44
45
struct headers_data *headers = NULL;
char *precooked_data = NULL;
extern struct pcap_pkthdr *nids_last_pcap_header;
Arne Øslebø's avatar
Arne Øslebø committed
46

's avatar
committed
47
mapid_pkthdr_t *last_mapid_header;
Arne Øslebø's avatar
Arne Øslebø committed
48

's avatar
committed
49
int status;
Arne Øslebø's avatar
Arne Øslebø committed
50

's avatar
committed
51
52
int cooked_pkts = 0;
int un_cooked_pkts = 0;
53
54
55
unsigned char *last_dev_pkt;
int callbacks=0;

's avatar
committed
56
struct mapidlibflow *cook_tmp_flow=NULL;
's avatar
committed
57
58
static int cooking_id=0;

Arne Øslebø's avatar
Arne Øslebø committed
59

's avatar
committed
60
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
61
{
62
63
64
65
66

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

's avatar
committed
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	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
84
85
86
	return 0;
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
103
	ip->tlen = ntohs(flow->client_size + ip_len + tcp_len);
104
	
's avatar
committed
105
	flow->mod_pkt = malloc(sizeof(char) * (flow->client_size + ether_len + ip_len + tcp_len));
106
	if(flow->mod_pkt==NULL) {
's avatar
committed
107
		//fprintf(stderr,"client size: %d\n",flow->client_size);
108
109
110
111
112
113
		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);
	
114
	memcpy(&flow->mod_pkt[ether_len + ip_len + tcp_len], flow->ret_client_data, flow->client_size);
115
116
	
	//server packet
's avatar
committed
117
	flow->server_mod_pkt = malloc(sizeof(char) * (flow->server_size + ether_len + ip_len + tcp_len));
118
	if(flow->server_mod_pkt==NULL) {
's avatar
committed
119
		fprintf(stderr,"client size: %d\n",flow->client_size);
120
121
122
123
124
		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);
125
	memcpy(&flow->server_mod_pkt[ether_len + ip_len + tcp_len], flow->ret_server_data, flow->server_size);
's avatar
committed
126
	flow->server_mod_pkt_head.caplen = flow->server_mod_pkt_head.wlen = flow->server_size + ether_len + ip_len + tcp_len;	
127

128
129
	flow->client_headers = flow->ret_client_headers;
	flow->server_headers = flow->ret_server_headers;
130

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

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


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

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

	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) {
191
192
193
	    
		flow->ret_client_data = ns->client.reassembled_data;
		flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
194
195
		flow->client_size = ns->client.read;
		flow->server_size = ns->server.read;
196
197
		flow->ret_client_headers = ns->client.headers;
		flow->ret_server_headers = ns->server.headers;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
		
		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)
		{
214
			if(flow->ret_client_data == NULL)
215
			{
216
217
	           	flow->ret_client_data = ns->client.reassembled_data;
				flow->ret_server_data = ns->server.reassembled_data;
218
				
219
220
				to_free = flow->ret_client_data;
				to_free2 = flow->ret_server_data;
221
				
's avatar
committed
222
223
				flow->client_size = ns->client.read;
				flow->server_size = ns->server.read;
224
225
				
				
226
227
				flow->ret_client_headers = ns->client.headers;
				flow->ret_server_headers = ns->server.headers;
228
				
's avatar
committed
229
				//fprintf(stderr,"final data: %d %d %d\n",flow->client_size,flist_size(ns->client.headers),ns->nids_state==NIDS_EXITING);
230
231
232
233
234
				return;
			}
		}
		else if(ns->server.total_read < ns->server.count || threshold ==-1)
		{
235
			if(flow->ret_server_data == NULL)
236
			{
237
238
239
240
				flow->ret_server_data = ns->server.reassembled_data;
	            flow->ret_client_data = ns->client.reassembled_data;
				to_free = flow->ret_server_data;
				to_free2 = flow->ret_client_data;
241
				
's avatar
committed
242
243
				flow->server_size = ns->server.read;
				flow->client_size = ns->client.read;
244
245
				//fprintf(stderr,"++final data: %d %d\n",server_size,flist_size(ns->server.headers));
				
246
247
				flow->ret_server_headers = ns->server.headers;
				flow->ret_client_headers = ns->client.headers;
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

				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];
			
280
281
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
282
				flow->client_size = ns->client.read + ns->client.count_new;
283
284
285
				ns->client.total_read = ns->client.count;
				ns->client.read = 0;
				
286
287
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
288
				flow->server_size=ns->server.read;
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
				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];
				
330
331
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
332
				flow->server_size = ns->server.read + ns->server.count_new;
333
				
334
335
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
336
				flow->client_size = ns->client.read;
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
				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
360
361
}

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

's avatar
committed
369
370
371
	fargs=instance->args;
	data->threshold=getargint(&fargs);
	data->timeout=getargint(&fargs);
372
	data->flow=cook_tmp_flow;
's avatar
committed
373
374
375
	data->id=cooking_id;
	cooking_id++;
	
's avatar
committed
376
377
378
	desc.fd = 1;
	desc.linktype = instance->hwinfo->link_type;
	desc.bufsize = instance->hwinfo->cap_length;
Arne Øslebø's avatar
Arne Øslebø committed
379

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

's avatar
committed
382
	nids_mapi_register_tcp(tcp_callback,data->id);
Arne Øslebø's avatar
Arne Øslebø committed
383

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

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

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

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

's avatar
committed
399
static int cook_process(mapidflib_function_instance_t *instance,const unsigned char* dev_pkt,const unsigned char* link_pkt, mapid_pkthdr_t* pkt_head)
Arne Øslebø's avatar
Arne Øslebø committed
400
{
401
402
403
404
405
406
407
408
409
	
	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");
410
	precooked_data = link_pkt;
411

412
	last_dev_pkt= link_pkt;
413
414
415
416
417
418
419
420
421
422
423
	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
424
	
425
426
	status = NON_CHECKED;
	
427
428
	flow->ret_client_data = NULL;
	flow->ret_server_data = NULL;
's avatar
committed
429
	flow->client_size=flow->server_size=0;
430
431
432

	callbacks=0;
	
433
	eth = (ether_header*)link_pkt;
434
	ether_len = sizeof(ether_header);
435
	iph = (struct ip*)(link_pkt + ether_len);
436
437
438
439
	ip_len = (iph->ip_hl & 0xf) * 4;
	if(iph->ip_p != IPPROTO_TCP)	// no TCP packet
		return 1;
	
's avatar
committed
440
	nids_mapi_next(&h, link_pkt,data->id);	
441
442
443
444
445
446
447
448
449
	//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;

450
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
451

452
		//XXX replace 0 
's avatar
committed
453
		stream= find_stream(tcph,iph,&from_client,data->id);
454
455
456
457
		if(stream) {
			//fprintf(stderr,"\tStream found\n");
			headers = malloc(sizeof(struct headers_data));
			headers->header = malloc(pkt_head->caplen); //added 100 more bytes
458
			memcpy(headers->header,link_pkt,pkt_head->caplen);
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
			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
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
493
494
495
		
		headers = malloc(sizeof(struct headers_data));
		headers->header = malloc(pkt_head->caplen); 
496
		memcpy(headers->header, link_pkt,pkt_head->caplen);
497
498
499
500
		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;
501
502
	
		//XXX replace 0
's avatar
committed
503
		stream= find_stream(tcph,iph,&from_client,data->id);
504
505
		if(stream==NULL) {
			//fprintf(stderr,"\tNULL stream %d %d\n",headers->ts.tv_sec,headers->ts.tv_usec);
506
			create_mod_pkt(link_pkt,flow,pkt_head);
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
			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);
		}
	}
	
529
	if(flow->ret_client_data != NULL || flow->ret_server_data!=NULL)
530
531
	{
		// Set pseudoheader and new cooked packet to return to the daemon
532
		create_mod_pkt(link_pkt,flow,pkt_head);
533
534
535
536
		return 1;
	}

	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
537
538
539
540
541
542
543

}

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

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
's avatar
committed
544
	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
545
546
547
548
}

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

's avatar
committed
549
static mapidflib_function_def_t cooking_finfo =
Arne Øslebø's avatar
Arne Øslebø committed
550
551
{
	"", //libname
's avatar
committed
552
553
	"COOKING", //name
	"Cooking TCP/IP packets", //Description
Arne Øslebø's avatar
Arne Øslebø committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
	"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()
{
's avatar
committed
575
	return &cooking_finfo;
Arne Øslebø's avatar
Arne Øslebø committed
576
};