tcp.c 20.3 KB
Newer Older
's avatar
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
  Copyright (c) 1999 Rafal Wojtczuk <nergal@avet.com.pl>. All rights reserved.
  See the file COPYING for license details.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>

#include "tcp.h"
#include "util.h"
#include "nids.h"
#include "hash.h"
21
#include "ip_fragment.h"
's avatar
committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#if ! HAVE_TCP_STATES
enum {
  TCP_ESTABLISHED = 1,
  TCP_SYN_SENT,
  TCP_SYN_RECV,
  TCP_FIN_WAIT1,
  TCP_FIN_WAIT2,
  TCP_TIME_WAIT,
  TCP_CLOSE,
  TCP_CLOSE_WAIT,
  TCP_LAST_ACK,
  TCP_LISTEN,
  TCP_CLOSING			/* now a valid state */
};

#endif

#define FIN_SENT 120
#define FIN_CONFIRMED 121
#define COLLECT_cc 1
#define COLLECT_sc 2
#define COLLECT_ccu 4
#define COLLECT_scu 8

#define EXP_SEQ (snd->first_data_seq + rcv->count + rcv->urg_count)

49
extern void mapi_tcp_callback(struct tcp_stream *ns, void **param);
's avatar
committed
50
51
52
53

static int tcp_stream_table_size;
static int max_stream;
static struct ip *ugly_iphdr;
54

's avatar
committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

static void purge_queue(struct half_stream * h)
{
  struct skbuff *tmp, *p = h->list;

  while (p) {
    free(p->data);
    tmp = p->next;
    free(p);
    p = tmp;
  }
  h->list = h->listtail = 0;
  h->rmem_alloc = 0;
}

static void
71
free_tcp(struct tcp_stream * a_tcp, nids_instance_t *ni)
's avatar
committed
72
73
74
75
76
77
78
79
80
81
82
83
{
  int hash_index = a_tcp->hash_index;
  struct lurker_node *i, *j;
 
  purge_queue(&a_tcp->server);
  purge_queue(&a_tcp->client);
   
  if (a_tcp->next_node)
    a_tcp->next_node->prev_node = a_tcp->prev_node;
  if (a_tcp->prev_node)
    a_tcp->prev_node->next_node = a_tcp->next_node;
  else
84
    ni->tcp_stream_table[hash_index] = a_tcp->next_node;
's avatar
committed
85
  if (a_tcp->client.data) {
's avatar
committed
86
    free(a_tcp->client.data);
's avatar
committed
87
88
89
    a_tcp->client.data = NULL;
  }
  if (a_tcp->server.data) {
's avatar
committed
90
    free(a_tcp->server.data);
's avatar
committed
91
92
    a_tcp->server.data = NULL;
  }
's avatar
committed
93
94
95
96
  if (a_tcp->next_time)
    a_tcp->next_time->prev_time = a_tcp->prev_time;
  if (a_tcp->prev_time)
    a_tcp->prev_time->next_time = a_tcp->next_time;
97
98
99
100
  if (a_tcp == ni->tcp_oldest)
    ni->tcp_oldest = a_tcp->prev_time;
  if (a_tcp == ni->tcp_latest)
    ni->tcp_latest = a_tcp->next_time;
's avatar
committed
101
102
103
104
105
106
107
108
  
  i = a_tcp->listeners;
  
  while (i) {
    j = i->next;
    free(i);
    i = j;
  }
109
110
111
  a_tcp->next_free = ni->free_streams;
  ni->free_streams = a_tcp;
  ni->tcp_num--;
's avatar
committed
112
113
114
115
116
117
118
119
120
121
122
123
124
}

static int
mk_hash_index(struct tuple4 addr)
{
  int hash=mkhash(addr.saddr, addr.source, addr.daddr, addr.dest);
  return hash % tcp_stream_table_size;
}

static int get_ts(struct tcphdr * this_tcphdr, unsigned int * ts)
{
  int len = 4 * this_tcphdr->th_off;
  unsigned int tmp_ts;
's avatar
committed
125
  unsigned char * options = (unsigned char*)(this_tcphdr + 1);
's avatar
committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  int ind = 0, ret = 0;
  while (ind <=  len - (int)sizeof (struct tcphdr) - 10 )
  	switch (options[ind]) {
		case 0: /* TCPOPT_EOL */
			return ret;
		case 1: /* TCPOPT_NOP */
			ind++;
			continue;	
  		case 8: /* TCPOPT_TIMESTAMP */
	  		memcpy((char*)&tmp_ts, options + ind + 2, 4);
  			*ts=ntohl(tmp_ts);
			ret = 1;
			/* no break, intentionally */
		default:	
			if (options[ind+1] < 2 ) /* "silly option" */
				return ret;
			ind += options[ind+1];
	}			
			
  return ret;
}  		
    


static void
151
add_new_tcp(struct tcphdr * this_tcphdr, struct ip * this_iphdr, nids_instance_t *ni)
's avatar
committed
152
153
154
155
156
157
158
159
160
161
162
163
{
  struct tcp_stream *tolink;
  struct tcp_stream *a_tcp;
  int hash_index;
  struct tuple4 addr;
  
  addr.source = ntohs(this_tcphdr->th_sport);
  addr.dest = ntohs(this_tcphdr->th_dport);
  addr.saddr = this_iphdr->ip_src.s_addr;
  addr.daddr = this_iphdr->ip_dst.s_addr;
  hash_index = mk_hash_index(addr);
  
164
  if (ni->tcp_num > max_stream) {
's avatar
committed
165
    struct lurker_node *i;
166
167
168
169
    ni->tcp_oldest->nids_state = NIDS_TIMED_OUT;
    for (i = ni->tcp_oldest->listeners; i; i = i->next)
      (i->item) (ni->tcp_oldest, &i->data);
    free_tcp(ni->tcp_oldest, ni);
's avatar
committed
170
  }
171
  a_tcp = ni->free_streams;
's avatar
committed
172
173
174
175
  if (!a_tcp) {
    fprintf(stderr, "gdb me ...\n");
    pause();
  }
176
  ni->free_streams = a_tcp->next_free;
's avatar
committed
177
  
178
179
  ni->tcp_num++;
  tolink = ni->tcp_stream_table[hash_index];
's avatar
committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  memset(a_tcp, 0, sizeof(struct tcp_stream));
  a_tcp->hash_index = hash_index;
  a_tcp->addr = addr;
  a_tcp->client.state = TCP_SYN_SENT;
  //antonat
  a_tcp->client.headers = a_tcp->server.headers = NULL;
  a_tcp->client.seq = ntohl(this_tcphdr->th_seq) + 1;
  a_tcp->client.first_data_seq = a_tcp->client.seq;
  a_tcp->client.window = ntohs(this_tcphdr->th_win);
  a_tcp->client.ts_on = get_ts(this_tcphdr, &a_tcp->client.curr_ts);
  a_tcp->server.state = TCP_CLOSE;
  a_tcp->next_node = tolink;
  a_tcp->prev_node = 0;
  if (tolink)
    tolink->prev_node = a_tcp;
195
196
  ni->tcp_stream_table[hash_index] = a_tcp;
  a_tcp->next_time = ni->tcp_latest;
's avatar
committed
197
  a_tcp->prev_time = 0;
198
199
200
201
202
  if (!ni->tcp_oldest)
    ni->tcp_oldest = a_tcp;
  if (ni->tcp_latest)
    ni->tcp_latest->prev_time = a_tcp;
  ni->tcp_latest = a_tcp;
's avatar
committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
}

static void
add2buf(struct half_stream * rcv, char *data, int datalen)
{
  int toalloc;
  
  if (datalen + rcv->count - rcv->offset > rcv->bufsize) {
    if (!rcv->data) {
      if (datalen < 2048)
	toalloc = 4096;
      else
	toalloc = datalen * 2;
      rcv->data = malloc(toalloc);
      rcv->bufsize = toalloc;
    }
    else {
      if (datalen < rcv->bufsize)
      	toalloc = 2 * rcv->bufsize;
      else	
      	toalloc = rcv->bufsize + 2*datalen;
      rcv->data = realloc(rcv->data, toalloc);
      rcv->bufsize = toalloc;
    }
    if (!rcv->data)
      nids_params.no_mem("add2buf");
  }
  memcpy(rcv->data + rcv->count - rcv->offset, data, datalen);
  rcv->count_new = datalen;
  rcv->count += datalen;
}

static void
ride_lurkers(struct tcp_stream * a_tcp, char mask)
{
  struct lurker_node *i;
  char cc, sc, ccu, scu;
  
  for (i = a_tcp->listeners; i; i = i->next)
    if (i->whatto & mask) {
      cc = a_tcp->client.collect;
      sc = a_tcp->server.collect;
      ccu = a_tcp->client.collect_urg;
      scu = a_tcp->server.collect_urg;
      
      (i->item) (a_tcp, &i->data);
      if (cc < a_tcp->client.collect)
	i->whatto |= COLLECT_cc;
      if (ccu < a_tcp->client.collect_urg)
	i->whatto |= COLLECT_ccu;
      if (sc < a_tcp->server.collect)
	i->whatto |= COLLECT_sc;
      if (scu < a_tcp->server.collect_urg)
	i->whatto |= COLLECT_scu;
      if (cc > a_tcp->client.collect)
	i->whatto &= ~COLLECT_cc;
      if (ccu > a_tcp->client.collect_urg)
	i->whatto &= ~COLLECT_ccu;
      if (sc > a_tcp->server.collect)
	i->whatto &= ~COLLECT_sc;
      if (scu > a_tcp->server.collect_urg)
	i->whatto &= ~COLLECT_scu;
    }
}

static void
notify(struct tcp_stream * a_tcp, struct half_stream * rcv)
{
  struct lurker_node *i, **prev_addr;
  char mask;

  if (rcv->count_new_urg) {
    if (!rcv->collect_urg)
      return;
    if (rcv == &a_tcp->client)
      mask = COLLECT_ccu;
    else
      mask = COLLECT_scu;
    ride_lurkers(a_tcp, mask);
    goto prune_listeners;
  }
  if (rcv->collect) {
    if (rcv == &a_tcp->client)
      mask = COLLECT_cc;
    else
      mask = COLLECT_sc;
   do {
	int total;
		a_tcp->read = rcv->count - rcv->offset;
		  total=a_tcp->read;
  
	    ride_lurkers(a_tcp, mask);
	    if (a_tcp->read>total-rcv->count_new)
	    	rcv->count_new=total-a_tcp->read;
	    
	    if (a_tcp->read > 0) {
	      memmove(rcv->data, rcv->data + a_tcp->read, rcv->count - rcv->offset - a_tcp->read);
	      rcv->offset += a_tcp->read;
	    }
	}while (nids_params.one_loop_less && a_tcp->read>0 && rcv->count_new); 
// we know that if one_loop_less!=0, we have only one callback to notify
   rcv->count_new=0;	    
  }
 prune_listeners:
  prev_addr = &a_tcp->listeners;
  i = a_tcp->listeners;
  while (i)
    if (!i->whatto) {
      *prev_addr = i->next;
      free(i);
      i = *prev_addr;
    }
    else {
      prev_addr = &i->next;
      i = i->next;
    }
}

static void
add_from_skb(struct tcp_stream * a_tcp, struct half_stream * rcv,
	     struct half_stream * snd,
	     u_char *data, int datalen,
	     u_int this_seq, char fin, char urg, u_int urg_ptr)
{
  u_int lost = EXP_SEQ - this_seq;
  int to_copy, to_copy2;
  
  if (urg && after(urg_ptr, EXP_SEQ - 1) &&
      (!rcv->urg_seen || after(urg_ptr, rcv->urg_ptr))) {
    rcv->urg_ptr = urg_ptr;
    rcv->urg_seen = 1;
  }
  if (after(rcv->urg_ptr + 1, this_seq + lost) &&
      before(rcv->urg_ptr, this_seq + datalen)) {
    to_copy = rcv->urg_ptr - (this_seq + lost);
    if (to_copy > 0) {
      if (rcv->collect) {
's avatar
committed
340
	add2buf(rcv, (char *)data + lost, to_copy);
's avatar
committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
	notify(a_tcp, rcv);
      }
      else {
	rcv->count += to_copy;
	rcv->offset = rcv->count; /* clear the buffer */
      }
    }
    rcv->urgdata = data[rcv->urg_ptr - this_seq];
    rcv->count_new_urg = 1;
    notify(a_tcp, rcv);
    rcv->count_new_urg = 0;
    rcv->urg_count++;
    to_copy2 = this_seq + datalen - rcv->urg_ptr - 1;
    if (to_copy2 > 0) {
      if (rcv->collect) {
's avatar
committed
356
	add2buf(rcv, (char *)data + lost + to_copy + 1, to_copy2);
's avatar
committed
357
358
359
360
361
362
363
364
365
366
367
	notify(a_tcp, rcv);
      }
      else {
	rcv->count += to_copy2;
	rcv->offset = rcv->count; /* clear the buffer */
      }
    }
  }
  else {
    if (datalen - lost > 0) {
      if (rcv->collect) {
's avatar
committed
368
	add2buf(rcv, (char *)data + lost, datalen - lost);
's avatar
committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
	notify(a_tcp, rcv);
      }
      else {
	rcv->count += datalen - lost;
	rcv->offset = rcv->count; /* clear the buffer */
      }
    }
  }
  if (fin)
    snd->state = FIN_SENT;
}

static void
tcp_queue(struct tcp_stream * a_tcp, struct tcphdr * this_tcphdr,
	  struct half_stream * snd, struct half_stream * rcv,
	  char *data, int datalen, int skblen
	  )
{
  u_int this_seq = ntohl(this_tcphdr->th_seq);
  struct skbuff *pakiet;
  
  /*
   * Did we get anything new to ack?
   */
  
  if (!after(this_seq, EXP_SEQ)) {
    if (after(this_seq + datalen + (this_tcphdr->th_flags & TH_FIN), EXP_SEQ)) {
      /* the packet straddles our window end */
      get_ts(this_tcphdr, &snd->curr_ts);
's avatar
committed
398
      add_from_skb(a_tcp, rcv, snd, (unsigned char *)data, datalen, this_seq,
's avatar
committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
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
		   (this_tcphdr->th_flags & TH_FIN),
		   (this_tcphdr->th_flags & TH_URG),
		   ntohs(this_tcphdr->th_urp) + this_seq - 1);
      /*
       * Do we have any old packets to ack that the above
       * made visible? (Go forward from skb)
       */
      pakiet = rcv->list;
      while (pakiet) {
	if (after(pakiet->seq, EXP_SEQ))
	  break;
	if (after(pakiet->seq + pakiet->len + pakiet->fin, EXP_SEQ)) {
	  struct skbuff *tmp;
	  
	  add_from_skb(a_tcp, rcv, snd, pakiet->data,
		       pakiet->len, pakiet->seq, pakiet->fin, pakiet->urg,
		       pakiet->urg_ptr + pakiet->seq - 1);
	  rcv->rmem_alloc -= pakiet->truesize;
	  if (pakiet->prev)
	    pakiet->prev->next = pakiet->next;
	  else
	    rcv->list = pakiet->next;
	  if (pakiet->next)
	    pakiet->next->prev = pakiet->prev;
	  else
	    rcv->listtail = pakiet->prev;
	  tmp = pakiet->next;
	  free(pakiet->data);
	  free(pakiet);
	  pakiet = tmp;
	}
	else
	  pakiet = pakiet->next;
      }
    }
    else
      return;
  }
  else {
    struct skbuff *p = rcv->listtail;

    pakiet = mknew(struct skbuff);
    pakiet->truesize = skblen;
    rcv->rmem_alloc += pakiet->truesize;
    pakiet->len = datalen;
    pakiet->data = malloc(datalen);
    if (!pakiet->data)
      nids_params.no_mem("tcp_queue");
    memcpy(pakiet->data, data, datalen);
    pakiet->fin = (this_tcphdr->th_flags & TH_FIN);
    pakiet->seq = this_seq;
    pakiet->urg = (this_tcphdr->th_flags & TH_URG);
    pakiet->urg_ptr = ntohs(this_tcphdr->th_urp);
    for (;;) {
      if (!p || !after(p->seq, this_seq))
	break;
      p = p->prev;
    }
    if (!p) {
      pakiet->prev = 0;
      pakiet->next = rcv->list;
      if (rcv->list)
         rcv->list->prev = pakiet;
      rcv->list = pakiet;
      if (!rcv->listtail)
	rcv->listtail = pakiet;
    }
    else {
      pakiet->next = p->next;
      p->next = pakiet;
      pakiet->prev = p;
      if (pakiet->next)
	pakiet->next->prev = pakiet;
      else
	rcv->listtail = pakiet;
    }
  }
}

static void
prune_queue(struct half_stream * rcv, struct tcphdr * this_tcphdr)
{
  struct skbuff *tmp, *p = rcv->list;
482
483
  
  (void)this_tcphdr;
's avatar
committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  while (p) {
    free(p->data);
    tmp = p->next;
    free(p);
    p = tmp;
  }
  rcv->list = rcv->listtail = 0;
  rcv->rmem_alloc = 0;
}

static void
handle_ack(struct half_stream * snd, u_int acknum)
{
  int ackdiff;

  ackdiff = acknum - snd->ack_seq;
  if (ackdiff > 0) {
    snd->ack_seq = acknum;
  }
}

505
struct tcp_stream *find_stream(struct tcphdr * this_tcphdr, struct ip * this_iphdr,
506
	    int *from_client, nids_instance_t *ni)
's avatar
committed
507
508
509
510
511
512
513
514
515
516
{
  struct tuple4 this_addr, reversed;
  int hash_index;
  struct tcp_stream *a_tcp;

  this_addr.source = ntohs(this_tcphdr->th_sport);
  this_addr.dest = ntohs(this_tcphdr->th_dport);
  this_addr.saddr = this_iphdr->ip_src.s_addr;
  this_addr.daddr = this_iphdr->ip_dst.s_addr;
  hash_index = mk_hash_index(this_addr);
517
  for (a_tcp = ni->tcp_stream_table[hash_index];
's avatar
committed
518
519
520
521
522
523
524
525
526
527
528
       a_tcp && !b_comp(a_tcp->addr, this_addr);
       a_tcp = a_tcp->next_node);
  if (a_tcp) {
    *from_client = 1;
    return a_tcp;
  }
  reversed.source = ntohs(this_tcphdr->th_dport);
  reversed.dest = ntohs(this_tcphdr->th_sport);
  reversed.saddr = this_iphdr->ip_dst.s_addr;
  reversed.daddr = this_iphdr->ip_src.s_addr;
  hash_index = mk_hash_index(reversed);
529
  for (a_tcp = ni->tcp_stream_table[hash_index];
's avatar
committed
530
531
532
533
534
535
536
537
538
539
       a_tcp && !b_comp(a_tcp->addr, reversed);
       a_tcp = a_tcp->next_node);
  if (a_tcp) {
    *from_client = 0;
    return a_tcp;
  }
  else
    return 0;
}

540
void clear_stream_buffers(nids_instance_t *ni)
's avatar
committed
541
542
543
544
545
546
{
  int i;
  struct lurker_node *j;
  struct tcp_stream *a_tcp;
  
  for (i = 0; i < tcp_stream_table_size; i++) {
547
    for (a_tcp = ni->tcp_stream_table[i];
's avatar
committed
548
549
550
551
552
553
554
555
556
557
         a_tcp;
         a_tcp = a_tcp->next_node) {
      for (j = a_tcp->listeners; j; j = j->next) {
          a_tcp->nids_state = NIDS_EXITING;
          (j->item)(a_tcp, &j->data);
      }
    }
  }
}
        
558
void process_tcp(u_char * data, int skblen, nids_instance_t *ni)
's avatar
committed
559
560
561
562
563
564
565
566
567
{
  struct ip *this_iphdr = (struct ip *)data;
  struct tcphdr *this_tcphdr = (struct tcphdr *)(data + 4 * this_iphdr->ip_hl);
  int datalen, iplen;
  int from_client = 1;
  unsigned int tmp_ts;
  struct tcp_stream *a_tcp;
  struct half_stream *snd, *rcv;

568
  //vpappas
569
570
571
  if (ni->tcp2free) {
      free_tcp(ni->tcp2free, ni);
      ni->tcp2free = NULL;
572
573
  }

's avatar
committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  ugly_iphdr = this_iphdr;
  iplen = ntohs(this_iphdr->ip_len);
  if ((unsigned)iplen < 4 * this_iphdr->ip_hl + sizeof(struct tcphdr)) {
    return;
  } // ktos sie bawi
  
  datalen = iplen - 4 * this_iphdr->ip_hl - 4 * this_tcphdr->th_off;
  
  if (datalen < 0) {
    return;
  } // ktos sie bawi

  if ((this_iphdr->ip_src.s_addr | this_iphdr->ip_dst.s_addr) == 0) {
    return;
  }
589
  
590
591
  if (!nids_params.n_tcp_streams) 
      return;
592
  
593
  if (!(a_tcp = find_stream(this_tcphdr, this_iphdr, &from_client, ni))) {
's avatar
committed
594
595
596
    if ((this_tcphdr->th_flags & TH_SYN) &&
	!(this_tcphdr->th_flags & TH_ACK) &&
	!(this_tcphdr->th_flags & TH_RST))
597
      add_new_tcp(this_tcphdr, this_iphdr, ni);
598
    //fprintf(stderr, "libnids: added stream: %p\n", find_stream(this_tcphdr, this_iphdr, &from_client,id));
's avatar
committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
    return;
  }
  if (from_client) {
    snd = &a_tcp->client;
    rcv = &a_tcp->server;
  }
  else {
    rcv = &a_tcp->client;
    snd = &a_tcp->server;
  }
  if ((this_tcphdr->th_flags & TH_SYN)) {
    if (from_client || a_tcp->client.state != TCP_SYN_SENT ||
      a_tcp->server.state != TCP_CLOSE || !(this_tcphdr->th_flags & TH_ACK))
      return;
    if (a_tcp->client.seq != ntohl(this_tcphdr->th_ack))
      return;
    a_tcp->server.state = TCP_SYN_RECV;
    a_tcp->server.seq = ntohl(this_tcphdr->th_seq) + 1;
    a_tcp->server.first_data_seq = a_tcp->server.seq;
    a_tcp->server.ack_seq = ntohl(this_tcphdr->th_ack);
    a_tcp->server.window = ntohs(this_tcphdr->th_win);
    if (a_tcp->client.ts_on) {
    	a_tcp->server.ts_on = get_ts(this_tcphdr, &a_tcp->server.curr_ts);
	if (!a_tcp->server.ts_on)
		a_tcp->client.ts_on = 0;
    } else a_tcp->server.ts_on = 0;	
    return;
  }
  if (
  	! (  !datalen && ntohl(this_tcphdr->th_seq) == rcv->ack_seq  )
  	&&
  	( !before(ntohl(this_tcphdr->th_seq), rcv->ack_seq + rcv->window) ||
          before(ntohl(this_tcphdr->th_seq) + datalen, rcv->ack_seq)  
        )
     )     
     return;

  if ((this_tcphdr->th_flags & TH_RST)) {
    if (a_tcp->nids_state == NIDS_DATA) {
      struct lurker_node *i;
      
      a_tcp->nids_state = NIDS_RESET;
      for (i = a_tcp->listeners; i; i = i->next)
	(i->item) (a_tcp, &i->data);
    }
644
    ni->tcp2free = a_tcp; //free_tcp(a_tcp,id);
's avatar
committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    return;
  }

  /* PAWS check */
  if (rcv->ts_on && get_ts(this_tcphdr, &tmp_ts) && 
  	before(tmp_ts, snd->curr_ts))
  return; 	
  
  if ((this_tcphdr->th_flags & TH_ACK)) {
    if (from_client && a_tcp->client.state == TCP_SYN_SENT &&
	a_tcp->server.state == TCP_SYN_RECV) {
      if (ntohl(this_tcphdr->th_ack) == a_tcp->server.seq) {
	a_tcp->client.state = TCP_ESTABLISHED;
	a_tcp->client.ack_seq = ntohl(this_tcphdr->th_ack);
	{
	  struct lurker_node *j;
	  void *data;
	  
	  a_tcp->server.state = TCP_ESTABLISHED;
	  a_tcp->nids_state = NIDS_JUST_EST;
665

's avatar
committed
666
667
668
669
670
671
	    char whatto = 0;
	    char cc = a_tcp->client.collect;
	    char sc = a_tcp->server.collect;
	    char ccu = a_tcp->client.collect_urg;
	    char scu = a_tcp->server.collect_urg;
	    
672
673
	    mapi_tcp_callback(a_tcp, &data);
	    
's avatar
committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
	    if (cc < a_tcp->client.collect)
	      whatto |= COLLECT_cc;
	    if (ccu < a_tcp->client.collect_urg)
	      whatto |= COLLECT_ccu;
	    if (sc < a_tcp->server.collect)
	      whatto |= COLLECT_sc;
	    if (scu < a_tcp->server.collect_urg)
	      whatto |= COLLECT_scu;
	    if (nids_params.one_loop_less) {
	    		if (a_tcp->client.collect >=2) {
	    			a_tcp->client.collect=cc;
	    			whatto&=~COLLECT_cc;
	    		}
	    		if (a_tcp->server.collect >=2 ) {
	    			a_tcp->server.collect=sc;
	    			whatto&=~COLLECT_sc;
	    		}
	    }  
	    if (whatto) {
	      j = mknew(struct lurker_node);
694
	      j->item = &mapi_tcp_callback;
's avatar
committed
695
696
697
698
699
700
	      j->data = data;
	      j->whatto = whatto;
	      j->next = a_tcp->listeners;
	      a_tcp->listeners = j;
	    }
	  if (!a_tcp->listeners) {
701
	    ni->tcp2free = a_tcp; //free_tcp(a_tcp,id);
's avatar
committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
	    return;
	  }
	  a_tcp->nids_state = NIDS_DATA;
	}
      }
      // return;
    }
  }
  if ((this_tcphdr->th_flags & TH_ACK)) {
    handle_ack(snd, ntohl(this_tcphdr->th_ack));
    if (rcv->state == FIN_SENT)
      rcv->state = FIN_CONFIRMED;
    if (rcv->state == FIN_CONFIRMED && snd->state == FIN_CONFIRMED) {
      struct lurker_node *i;
      
      a_tcp->nids_state = NIDS_CLOSE;
      for (i = a_tcp->listeners; i; i = i->next)
	(i->item) (a_tcp, &i->data);
720
      ni->tcp2free = a_tcp; //free_tcp(a_tcp,id);
's avatar
committed
721
722
723
724
725
726
727
728
729
730
731
      return;
    }
  }
  if (datalen + (this_tcphdr->th_flags & TH_FIN) > 0)
    tcp_queue(a_tcp, this_tcphdr, snd, rcv,
	      (char *) (this_tcphdr) + 4 * this_tcphdr->th_off,
	      datalen, skblen);
  snd->window = ntohs(this_tcphdr->th_win);
  if (rcv->rmem_alloc > 65535)
    prune_queue(rcv, this_tcphdr);
  if (!a_tcp->listeners)
732
    ni->tcp2free = a_tcp; //free_tcp(a_tcp,id);
's avatar
committed
733
734
735
736
737
738
739
740
741
}

void
nids_discard(struct tcp_stream * a_tcp, int num)
{
  if (num < a_tcp->read)
    a_tcp->read = num;
}

742
int tcp_init(int size, nids_instance_t *ni)
's avatar
committed
743
744
745
746
{
  int i;
  if (!size) return 0;
  tcp_stream_table_size = size;
747
748
  ni->tcp_stream_table = malloc(tcp_stream_table_size * sizeof(char *));
  if (!ni->tcp_stream_table)
's avatar
committed
749
    nids_params.no_mem("tcp_init");
750
  memset(ni->tcp_stream_table, 0, tcp_stream_table_size * sizeof(char *));
751
  
's avatar
committed
752
  max_stream = 3 * tcp_stream_table_size / 4;
753
754
755
  ni->streams_pool = (struct tcp_stream *) malloc((max_stream + 1) * sizeof(struct tcp_stream));
  memset(ni->streams_pool, 0, (max_stream + 1) * sizeof(struct tcp_stream));
  if (!ni->streams_pool)
's avatar
committed
756
757
758
    nids_params.no_mem("tcp_init");
  for (i = 0; i < max_stream; i++) {
    	  //antonat
759
760
761
762
	  ni->streams_pool[i].client.headers = NULL;
    	  ni->streams_pool[i].server.headers = NULL;
	  ni->streams_pool[i].next_free = &(ni->streams_pool[i + 1]);
	  ni->streams_pool[i].flow = NULL;
's avatar
committed
763
  }
764
765
766
767
768
769
770
  ni->tcp2free = NULL; //vpappas
  ni->streams_pool[max_stream].next_free = 0;
  ni->free_streams = ni->streams_pool;
  ni->tcp_num = 0;

  memset(&ni->tcp_latest,0,sizeof(struct tcp_stream *));
  memset(&ni->tcp_oldest,0,sizeof(struct tcp_stream *));
771

's avatar
committed
772
773
774
775
  init_hash(); 
  return 0;
}

776
777
778
779
780
781
782
783
784
785
786
787
788
void tcp_free(nids_instance_t *ni) {
    int i;
    for (i=0; i < max_stream; i++) {
	if (ni->streams_pool[i].server.data)
	    free(ni->streams_pool[i].server.data);
	if (ni->streams_pool[i].client.data)
	    free(ni->streams_pool[i].client.data);
	if (ni->streams_pool[i].listeners)
	    free(ni->streams_pool[i].listeners);
    }
    free(ni->tcp_stream_table);
    free(ni->streams_pool);
    ip_frag_free(ni);
's avatar
committed
789
}