cooking.c 15.2 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;
Arne Øslebø's avatar
Arne Øslebø committed
57

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

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

's avatar
committed
65

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

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

126
127
	flow->client_headers = flow->ret_client_headers;
	flow->server_headers = flow->ret_server_headers;
128

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

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


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

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

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

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

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

's avatar
committed
367
368
369
	fargs=instance->args;
	data->threshold=getargint(&fargs);
	data->timeout=getargint(&fargs);
370
371
	data->flow=cook_tmp_flow;

's avatar
committed
372
373
374
	desc.fd = 1;
	desc.linktype = instance->hwinfo->link_type;
	desc.bufsize = instance->hwinfo->cap_length;
Arne Øslebø's avatar
Arne Øslebø committed
375

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

's avatar
committed
378
	nids_register_tcp(tcp_callback);
Arne Øslebø's avatar
Arne Øslebø committed
379

's avatar
committed
380
	if(data->threshold <= 0)
Arne Øslebø's avatar
Arne Øslebø committed
381
	{
's avatar
committed
382
		data->threshold = -1;
Arne Øslebø's avatar
Arne Øslebø committed
383
384
	}

's avatar
committed
385
386
	instance->internal_data=(void *)data;
	
Arne Øslebø's avatar
Arne Øslebø committed
387
388
389
390
391
	return 0;
}

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

's avatar
committed
392
393
// return 1 in order for processing to continue in other functions
// return 0 not to
Arne Øslebø's avatar
Arne Øslebø committed
394

's avatar
committed
395
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
396
{
397
398
399
400
401
402
403
404
405
	
	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");
406
	precooked_data = link_pkt;
407

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

	callbacks=0;
	
429
	eth = (ether_header*)link_pkt;
430
	ether_len = sizeof(ether_header);
431
	iph = (struct ip*)(link_pkt + ether_len);
432
433
434
435
	ip_len = (iph->ip_hl & 0xf) * 4;
	if(iph->ip_p != IPPROTO_TCP)	// no TCP packet
		return 1;
	
436
	nids_mapi_next(&h, link_pkt,0);	
437
438
439
440
441
442
443
444
445
	//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;

446
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
447

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

	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
533
534
535
536
537
538
539

}

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

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
's avatar
committed
540
	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
541
542
543
544
}

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

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