cooking.c 14.8 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
struct headers_data *headers = NULL;
extern struct pcap_pkthdr *nids_last_pcap_header;
Arne Øslebø's avatar
Arne Øslebø committed
45

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

's avatar
committed
48
int status;
49
50
unsigned char *last_dev_pkt;

's avatar
committed
51
struct mapidlibflow *cook_tmp_flow=NULL;
's avatar
committed
52
53
static int cooking_id=0;

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

's avatar
committed
55
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
56
{
57
58
59
60

	flist_node_t *n;
	struct mapidlibflow *flow=NULL;
	mapidlib_instance_t *i;
61
	struct cooking_data* data; 
62

's avatar
committed
63

64
65
66
67
68
69
70
71
72
73
74
75
76
	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);
	}
	
77
78
	data= (struct cooking_data*)malloc(sizeof(struct cooking_data));
	data->flow=flow;
79
	instance->internal_data=data;
Arne Øslebø's avatar
Arne Øslebø committed
80
81
82
	return 0;
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
99
	ip->tlen = ntohs(flow->client_size + ip_len + tcp_len);
100
	
's avatar
committed
101
	flow->mod_pkt = malloc(sizeof(char) * (flow->client_size + ether_len + ip_len + tcp_len));
102
	assert(flow->mod_pkt!=NULL);
103
104
105
106
	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);
	
107
	memcpy(&flow->mod_pkt[ether_len + ip_len + tcp_len], flow->ret_client_data, flow->client_size);
108
109
	
	//server packet
's avatar
committed
110
	flow->server_mod_pkt = malloc(sizeof(char) * (flow->server_size + ether_len + ip_len + tcp_len));
111
	assert(flow->server_mod_pkt!=NULL);
112
113
114
	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);
115
	memcpy(&flow->server_mod_pkt[ether_len + ip_len + tcp_len], flow->ret_server_data, flow->server_size);
's avatar
committed
116
	flow->server_mod_pkt_head.caplen = flow->server_mod_pkt_head.wlen = flow->server_size + ether_len + ip_len + tcp_len;	
117

118
119
	flow->client_headers = flow->ret_client_headers;
	flow->server_headers = flow->ret_server_headers;
120

121
	pkt_head->caplen = pkt_head->wlen = flow->client_size + ether_len + ip_len + tcp_len;	
122
123
}

's avatar
committed
124
125
126
127
void process_flushed_data(struct mapidlibflow *flow) {
}


's avatar
committed
128
void tcp_callback(struct tcp_stream *ns, void **param)
Arne Øslebø's avatar
Arne Øslebø committed
129
{
's avatar
committed
130
131
132
	struct mapidlibflow *flow;

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

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

				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];
			
267
268
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
269
				flow->client_size = ns->client.read + ns->client.count_new;
270
271
272
				ns->client.total_read = ns->client.count;
				ns->client.read = 0;
				
273
274
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
275
				flow->server_size=ns->server.read;
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
				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];
				
317
318
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
319
				flow->server_size = ns->server.read + ns->server.count_new;
320
				
321
322
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
323
				flow->client_size = ns->client.read;
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
				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
347
348
}

's avatar
committed
349
350
static int cook_init(mapidflib_function_instance_t *instance,
		     flist_t *flist)
Arne Øslebø's avatar
Arne Øslebø committed
351
{
's avatar
committed
352
353
	mapiFunctArg* fargs;
	struct pcap desc;
354
	struct cooking_data *data;
's avatar
committed
355
	fargs=instance->args;
356
357

	data=(struct cooking_data *)(instance->internal_data);
358
	
's avatar
committed
359
360
	data->threshold=getargint(&fargs);
	data->timeout=getargint(&fargs);
's avatar
committed
361
362
363
	data->id=cooking_id;
	cooking_id++;
	
's avatar
committed
364
365
366
	desc.fd = 1;
	desc.linktype = instance->hwinfo->link_type;
	desc.bufsize = instance->hwinfo->cap_length;
Arne Øslebø's avatar
Arne Øslebø committed
367

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

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

's avatar
committed
372
	if(data->threshold <= 0)
Arne Øslebø's avatar
Arne Øslebø committed
373
	{
's avatar
committed
374
		data->threshold = -1;
Arne Øslebø's avatar
Arne Øslebø committed
375
376
377
378
379
380
381
	}

	return 0;
}

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

's avatar
committed
382
383
// return 1 in order for processing to continue in other functions
// return 0 not to
Arne Øslebø's avatar
Arne Øslebø committed
384

's avatar
committed
385
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
386
{
387
388
389
390
391
392
393
394
395
396
	
	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");

397
	last_dev_pkt= link_pkt;
398
399
400
401
402
403
404
405
406
407
408
	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
409
	
410
411
	status = NON_CHECKED;
	
412
413
	flow->ret_client_data = NULL;
	flow->ret_server_data = NULL;
's avatar
committed
414
	flow->client_size=flow->server_size=0;
415
416

	
417
	eth = (ether_header*)link_pkt;
418
	ether_len = sizeof(ether_header);
419
	iph = (struct ip*)(link_pkt + ether_len);
420
421
422
423
	ip_len = (iph->ip_hl & 0xf) * 4;
	if(iph->ip_p != IPPROTO_TCP)	// no TCP packet
		return 1;
	
's avatar
committed
424
	nids_mapi_next(&h, link_pkt,data->id);	
425
426
427
428
429
430
431
	
	if(status == NON_CHECKED)
	{
		//fprintf(stderr,"\tnon check report\n");
		struct tcp_stream *stream;
		int from_client;

432
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
433

434
		//XXX replace 0 
's avatar
committed
435
		stream= find_stream(tcph,iph,&from_client,data->id);
436
437
438
439
		if(stream) {
			//fprintf(stderr,"\tStream found\n");
			headers = malloc(sizeof(struct headers_data));
			headers->header = malloc(pkt_head->caplen); //added 100 more bytes
440
			memcpy(headers->header,link_pkt,pkt_head->caplen);
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
			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;
		}
		return 0;
	}
	else {
		struct tcp_stream *stream;
		int from_client;
		
's avatar
committed
472
		//fprintf(stderr,"\tcheck report %d\n",flow->client_size);
473
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
474
475
476
		
		headers = malloc(sizeof(struct headers_data));
		headers->header = malloc(pkt_head->caplen); 
477
		memcpy(headers->header, link_pkt,pkt_head->caplen);
478
479
480
481
		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;
482
483
	
		//XXX replace 0
's avatar
committed
484
		stream= find_stream(tcph,iph,&from_client,data->id);
485
486
		if(stream==NULL) {
			//fprintf(stderr,"\tNULL stream %d %d\n",headers->ts.tv_sec,headers->ts.tv_usec);
487
			create_mod_pkt(link_pkt,flow,pkt_head);
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
			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);
		}
	}
	
510
	if(flow->ret_client_data != NULL || flow->ret_server_data!=NULL)
511
512
	{
		// Set pseudoheader and new cooked packet to return to the daemon
513
		create_mod_pkt(link_pkt,flow,pkt_head);
514
515
516
517
		return 1;
	}

	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
518
519
520
521
522
523
524

}

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

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
's avatar
committed
525
	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
526
527
528
529
}

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

's avatar
committed
530
static mapidflib_function_def_t cooking_finfo =
Arne Øslebø's avatar
Arne Øslebø committed
531
532
{
	"", //libname
's avatar
committed
533
534
	"COOKING", //name
	"Cooking TCP/IP packets", //Description
Arne Øslebø's avatar
Arne Øslebø committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
	"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
556
	return &cooking_finfo;
Arne Øslebø's avatar
Arne Øslebø committed
557
};