cooking.c 14.9 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
38
39
char* to_free = NULL;
char* to_free2 = NULL;
int status;
40
unsigned char *last_dev_pkt;
's avatar
committed
41
42
static int cooking_id=0;

's avatar
committed
43
44
45
46
extern struct tcp_stream *find_stream(struct tcphdr * this_tcphdr, struct ip * this_iphdr,int *from_client,int id);
void create_mod_pkt(unsigned char *dev_pkt,struct mapidlibflow *flow,mapid_pkthdr_t *pkt_head); 
void process_flushed_data(struct mapidlibflow *flow); 

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

's avatar
committed
48
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
49
{
50
51
52
53

	flist_node_t *n;
	struct mapidlibflow *flow=NULL;
	mapidlib_instance_t *i;
54
	struct cooking_data* data; 
55

's avatar
committed
56

57
58
59
60
61
62
63
64
65
66
67
68
69
	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);
	}
	
70
71
	data= (struct cooking_data*)malloc(sizeof(struct cooking_data));
	data->flow=flow;
72
	instance->internal_data=data;
Arne Øslebø's avatar
Arne Øslebø committed
73
74
75
	return 0;
}

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

111
112
	flow->client_headers = flow->ret_client_headers;
	flow->server_headers = flow->ret_server_headers;
113

114
	pkt_head->caplen = pkt_head->wlen = flow->client_size + ether_len + ip_len + tcp_len;	
115
116
}

's avatar
committed
117
118
119
120
void process_flushed_data(struct mapidlibflow *flow) {
}


's avatar
committed
121
void tcp_callback(struct tcp_stream *ns, void **param)
Arne Øslebø's avatar
Arne Øslebø committed
122
{
's avatar
committed
123
124
125
	struct mapidlibflow *flow;

	flow=ns->flow;
126
127
128

	if(ns->nids_state == NIDS_JUST_EST)
	{
's avatar
fixes    
committed
129
		//fprintf(stderr,"JUST_ESTABLISHED\n");
130
131
132
133
134
135
		ns->client.collect++;
		if(threshold==-1) { 
			ns->client.reassembled_data = NULL; 
			ns->client_reassembly_limit=0;
		}
		else {
's avatar
fixes    
committed
136
			ns->client.reassembled_data = (char *)malloc(sizeof(char) * threshold+1500);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		}
		ns->client.read = 0;
		ns->client.total_read = 0;
		if(ns->client.headers==NULL) {
			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 {
's avatar
fixes    
committed
153
			ns->server.reassembled_data = (char *)malloc(sizeof(char)*threshold+1500);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
		}
		
		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) {
170
171
172
	    
		flow->ret_client_data = ns->client.reassembled_data;
		flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
173
174
		flow->client_size = ns->client.read;
		flow->server_size = ns->server.read;
175
176
		flow->ret_client_headers = ns->client.headers;
		flow->ret_server_headers = ns->server.headers;
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
		
		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)
		{
193
			if(flow->ret_client_data == NULL)
194
			{
195
196
	           	flow->ret_client_data = ns->client.reassembled_data;
				flow->ret_server_data = ns->server.reassembled_data;
197
				
198
199
				to_free = flow->ret_client_data;
				to_free2 = flow->ret_server_data;
200
				
's avatar
committed
201
202
				flow->client_size = ns->client.read;
				flow->server_size = ns->server.read;
203
204
				
				
205
206
				flow->ret_client_headers = ns->client.headers;
				flow->ret_server_headers = ns->server.headers;
207
				
's avatar
committed
208
				//fprintf(stderr,"final data: %d %d %d\n",flow->client_size,flist_size(ns->client.headers),ns->nids_state==NIDS_EXITING);
209
210
211
212
213
				return;
			}
		}
		else if(ns->server.total_read < ns->server.count || threshold ==-1)
		{
214
			if(flow->ret_server_data == NULL)
215
			{
216
217
218
219
				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;
220
				
's avatar
committed
221
222
				flow->server_size = ns->server.read;
				flow->client_size = ns->client.read;
223
224
				//fprintf(stderr,"++final data: %d %d\n",server_size,flist_size(ns->server.headers));
				
225
226
				flow->ret_server_headers = ns->server.headers;
				flow->ret_client_headers = ns->client.headers;
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

				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];
			
259
260
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
261
				flow->client_size = ns->client.read + ns->client.count_new;
262
263
264
				ns->client.total_read = ns->client.count;
				ns->client.read = 0;
				
265
266
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
267
				flow->server_size=ns->server.read;
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
				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];
				
309
310
				flow->ret_server_headers = ns->server.headers;
				flow->ret_server_data = ns->server.reassembled_data;
's avatar
committed
311
				flow->server_size = ns->server.read + ns->server.count_new;
312
				
313
314
				flow->ret_client_headers = ns->client.headers;
				flow->ret_client_data = ns->client.reassembled_data;
's avatar
committed
315
				flow->client_size = ns->client.read;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
				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
339
340
}

's avatar
committed
341
static int cook_init(mapidflib_function_instance_t *instance,flist_t *flist)
Arne Øslebø's avatar
Arne Øslebø committed
342
{
's avatar
committed
343
344
	mapiFunctArg* fargs;
	struct pcap desc;
345
	struct cooking_data *data;
's avatar
committed
346
	fargs=instance->args;
347
348

	data=(struct cooking_data *)(instance->internal_data);
349
	
's avatar
committed
350
351
	data->threshold=getargint(&fargs);
	data->timeout=getargint(&fargs);
's avatar
committed
352
353
354
	data->id=cooking_id;
	cooking_id++;
	
's avatar
committed
355
356
357
	desc.fd = 1;
	desc.linktype = instance->hwinfo->link_type;
	desc.bufsize = instance->hwinfo->cap_length;
Arne Øslebø's avatar
Arne Øslebø committed
358

's avatar
committed
359
	printf("INiting::: %d\n",data->id);
's avatar
committed
360
	nids_mapi_init(&desc, instance->hwinfo->link_type,data->id);
Arne Øslebø's avatar
Arne Øslebø committed
361

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

's avatar
committed
364
	if(data->threshold <= 0)
Arne Øslebø's avatar
Arne Øslebø committed
365
	{
's avatar
committed
366
		data->threshold = -1;
Arne Øslebø's avatar
Arne Øslebø committed
367
368
369
370
371
372
373
	}

	return 0;
}

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

's avatar
committed
374
375
// return 1 in order for processing to continue in other functions
// return 0 not to
Arne Øslebø's avatar
Arne Øslebø committed
376

's avatar
committed
377
static int cook_process(mapidflib_function_instance_t *instance,const unsigned char* dev_pkt,unsigned char* link_pkt, mapid_pkthdr_t* pkt_head)
Arne Øslebø's avatar
Arne Øslebø committed
378
{
379
380
381
382
383
384
385
	
	struct pcap_pkthdr h;
	ether_header* eth = NULL;
	struct ip *iph;
	struct tcphdr *tcph;
	int ether_len = 0, ip_len = 0;
	struct mapidlibflow *flow;
's avatar
committed
386
387
388
389
390
391
392
	struct cooking_data* data;
	struct headers_data *headers;
	
	data= (struct cooking_data*)(instance->internal_data);
	threshold = data->threshold;
	timeout = data->timeout;
	flow=data->flow;
393
394
395
	
	//fprintf(stderr,"I am COOKING\n");

396
	last_dev_pkt= link_pkt;
397
398
399
400
401
402
		
	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;

's avatar
committed
403
	
404
405
	status = NON_CHECKED;
	
406
407
	flow->ret_client_data = NULL;
	flow->ret_server_data = NULL;
's avatar
committed
408
	flow->client_size=flow->server_size=0;
409

410
	eth = (ether_header*)link_pkt;
411
	ether_len = sizeof(ether_header);
412
	iph = (struct ip*)(link_pkt + ether_len);
413
414
415
	ip_len = (iph->ip_hl & 0xf) * 4;
	if(iph->ip_p != IPPROTO_TCP)	// no TCP packet
		return 1;
's avatar
fixes    
committed
416
417

	//printf("i got a packet %d\n",data->id);
's avatar
committed
418
	nids_mapi_next(&h, link_pkt,data->id);	
419
420
421
422
423
424
425
	
	if(status == NON_CHECKED)
	{
		//fprintf(stderr,"\tnon check report\n");
		struct tcp_stream *stream;
		int from_client;

426
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
427

's avatar
committed
428
		stream= find_stream(tcph,iph,&from_client,data->id);
429
430
431
432
		if(stream) {
			//fprintf(stderr,"\tStream found\n");
			headers = malloc(sizeof(struct headers_data));
			headers->header = malloc(pkt_head->caplen); //added 100 more bytes
433
			memcpy(headers->header,link_pkt,pkt_head->caplen);
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
			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
465
		//fprintf(stderr,"\tcheck report %d\n",flow->client_size);
466
		tcph = (struct tcphdr *)(link_pkt + ether_len + ip_len);
467
468
469
		
		headers = malloc(sizeof(struct headers_data));
		headers->header = malloc(pkt_head->caplen); 
470
		memcpy(headers->header, link_pkt,pkt_head->caplen);
471
472
473
474
		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;
475
	
's avatar
committed
476
		stream= find_stream(tcph,iph,&from_client,data->id);
477
478
		if(stream==NULL) {
			//fprintf(stderr,"\tNULL stream %d %d\n",headers->ts.tv_sec,headers->ts.tv_usec);
479
			create_mod_pkt(link_pkt,flow,pkt_head);
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
			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);
		}
	}
	
502
	if(flow->ret_client_data != NULL || flow->ret_server_data!=NULL)
503
504
	{
		// Set pseudoheader and new cooked packet to return to the daemon
505
		create_mod_pkt(link_pkt,flow,pkt_head);
506
507
508
509
		return 1;
	}

	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
510
511
512
513
514
515
516

}

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

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
's avatar
committed
517
	return 0;
Arne Øslebø's avatar
Arne Øslebø committed
518
519
520
521
}

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

's avatar
committed
522
static mapidflib_function_def_t cooking_finfo =
Arne Øslebø's avatar
Arne Øslebø committed
523
524
{
	"", //libname
's avatar
committed
525
526
	"COOKING", //name
	"Cooking TCP/IP packets", //Description
Arne Øslebø's avatar
Arne Øslebø committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
	"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
548
	return &cooking_finfo;
Arne Øslebø's avatar
Arne Øslebø committed
549
};