File: iptree.c

package info (click to toggle)
bld 0.3.4.1-4
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 576 kB
  • ctags: 268
  • sloc: ansic: 2,333; sh: 175; makefile: 157; perl: 135; python: 36
file content (583 lines) | stat: -rw-r--r-- 15,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
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
191
192
193
194
195
196
197
198
199
200
201
202
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
398
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*-
 * Copyright (c) 2004 Free (Olivier Beyssac)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ipinfo.h"
#include "iptree.h"
#include "options.h"


#define get_pos(x, y) (x + y * 256)


extern struct options opt;


#define IPTREE_M_IPLIST    (0)
#define IPTREE_M_BLACKLIST (1)


/* dlist is a common double linked list made of dl_nodes */
typedef struct dlist *dlist;


struct dl_node
{
  struct ipinfo *p;
  struct dl_node *prev;
  struct dl_node *next;
};


struct dlist
{
  struct dl_node *first;
  struct dl_node *last;
  size_t count;
};


/* iptree stores IP addresses
   IP x.y.z.t is in the tree at position iptree->[x + 256 * y]->c[z]->d[t] */
struct ipt_leaf
{
  size_t count;
  struct ipinfo *d[256];
};


struct ipt_node
{
  size_t count;
  struct ipt_leaf *c[256];
};


struct iptree
{
  struct ipt_node *ab[256 * 256];
  dlist ipl;                   /* Whole IPs list */
  dlist bl;                    /* Blacklisted IPs */
};


static void iptree_free(const iptree ipt, struct ipinfo *val);


static void *xcalloc(size_t nmemb, size_t size)
{
  void *tmp;

  if ((tmp = calloc(nmemb, size)) == NULL)
    syslog(LOG_ERR, "unable to calloc()");

  return tmp;
}


/* Dump dlist to file */
static void dlist_dump2file(const dlist dl, FILE *f)
{
  struct dl_node *p;

  for (p = dl->first; p; p = p->next)
    fwrite(p->p, sizeof(struct ipinfo), 1, f);
}


/* Remove the first element of dlist and return its address */
static struct dl_node *dlist_clean(const iptree ipt, const int mode)
{
  dlist l;
  struct dl_node *p;

  if (mode == IPTREE_M_IPLIST) {
    l = ipt->ipl;
    l->first->p->iplp = NULL;
  } else {
    l = ipt->bl;
    l->first->p->blp = NULL;
  }
  
  if (l->first == NULL)
    return NULL;
  
  p = l->first;
  
  /* Free only if
     - we are in the IP list and entry is not blacklisted,
     - we want to explicitly delete from blacklist and entry is
       not in IP list */
  if ((mode == IPTREE_M_IPLIST && !p->p->blp)
      || (mode == IPTREE_M_BLACKLIST && !p->p->iplp)) {
    if (opt.log_level >= 3)
      syslog(LOG_INFO, "Going to free %p (last=%lu - mode=%d)",
             (void *)p->p, (unsigned long)p->p->last, mode);
    iptree_free(ipt, p->p);
  }

  l->first = p->next;
  if (l->first)
    l->first->prev = NULL;
  l->count--;
  p->prev = NULL;
  p->next = NULL;

  return p;
}


/* Return a newly allocated dlist */
static dlist dlist_init(void)
{
  dlist tmp;

  tmp = xcalloc(sizeof(struct dlist), 1);
  
  return tmp;
}


/* Add an ipinfo record at the end of the wanted dlist (mode is IPTREE_M_IPLIST
   or M_BLACKLIST)
   Return 1 if success, 0 if allocation failed */
static int dlist_add(const iptree ipt, struct ipinfo *val,
		     const int mode, const time_t req_time)
{
  dlist l;
  unsigned int list_size;
  struct dl_node *p;
  time_t delta;

  if (val->blp) {
    if (opt.log_level >= 3)
      syslog(LOG_INFO, "%u.%u.%u.%u already in bl",
	     val->iptbl[3], val->iptbl[2], val->iptbl[1], val->iptbl[0]);
    return 1;
  }
  
  if (mode == IPTREE_M_IPLIST) {
    l = ipt->ipl;
    p = val->iplp;
    list_size = opt.list_size;
  } else {
    l = ipt->bl;
    p = val->blp;
    list_size = opt.blacklist_size;
  }

  if (p == NULL) {
    /* Add entry to list */
    l->count++;
    
    /* See if we can get an expirable, already allocated node, otherwise alloc
       a brand new one */
    if (l->count >= list_size + 1)
      p = dlist_clean(ipt, mode);
    else if ((p = xcalloc(sizeof(struct dl_node), 1)) == NULL) {
      syslog(LOG_ERR, "dlist_add failed to allocate %d bytes",
	     sizeof(struct dl_node));
      return 0;
    }
    
    if (mode == IPTREE_M_IPLIST)
      val->iplp = p;
    else
      val->blp = p;
    
    p->p = val;
    
    if (opt.log_level >= 3)
      syslog(LOG_INFO, "%s list size: %u/%u",
	     mode == IPTREE_M_IPLIST?"IP":"black", l->count, list_size);
    
    if (l->first == NULL) {
      l->first = p;
      l->last = p;
      return 1;
    }
  } else {
    /* Already existing entry, see if we have to blacklist */
    delta = val->last - val->start;
    if (mode == IPTREE_M_IPLIST && !val->blp
	&& delta >= opt.interval
	&& val->count * opt.interval / delta >= opt.max_req) {
      /* Blacklist
	 We will not remove the entry from the IP list and just wait
	 until the entry expires (no more updates once it's
	 blacklisted) */
      dlist_add(ipt, val, IPTREE_M_BLACKLIST, req_time);

      if (opt.log_level >= 1)
	syslog(LOG_INFO,
	       "%u.%u.%u.%u put in bl: %u reqs / %lu secs",
	       val->iptbl[3], val->iptbl[2], val->iptbl[1], val->iptbl[0],
	       val->count, (unsigned long)delta);
    }

    if (l->last == p)
      return 1;

    if (l->first == p)
      l->first = p->next;
  }

  /* Do the generic moving stuff */
  l->last->next = p;

  if (p->prev)
    p->prev->next = p->next;
  if (p->next)
    p->next->prev = p->prev;

  p->next = NULL;
  p->prev = l->last;
  l->last = p;
  l->first->prev = NULL;

  return 1;
}


#if 0
/* Call for debugging purposes only: brute force dump */
static void iptree_dump(const iptree ipt)
{
  size_t i, j, k;

  for (i = 0; i < 256 * 256; i++)
    if (ipt->ab[i])
      for (j = 0; j < 256; j++)
	if (ipt->ab[i]->c[j])
	  for (k = 0; k < 256; k++)
	    if (ipt->ab[i]->c[j]->d[k])
	      syslog(LOG_INFO, "iptree: %u.%u.%u.%u: %u %lu %lu",
		     i%256, i/256, j, k,
		     ipt->ab[i]->c[j]->d[k]->count,
		     ipt->ab[i]->c[j]->d[k]->start,
		     ipt->ab[i]->c[j]->d[k]->last);
}
#endif

 
/* Read dlist from file */
static void iptree_getfromfile(const iptree ipt, FILE *f)
{
  struct ipinfo ipi;
  int svlog_level;
  unsigned int val;
  
  svlog_level = opt.log_level;
  opt.log_level = 0;
  
  while (fread(&ipi, sizeof(struct ipinfo), 1, f)) {
    val = (ipi.iptbl[3]<<24) + (ipi.iptbl[2]<<16) + (ipi.iptbl[1]<<8)
      + ipi.iptbl[0];
    iptree_add(ipt, val, ipi.start, ipi.last, ipi.count, 0);
  }
  
  opt.log_level = svlog_level;
}
 

/* Return a newly allocated iptree */
extern iptree iptree_init(void)
{
  iptree tmp;
  struct stat sb;
  FILE *f;

  /* FIXME: should add a test */
  tmp = malloc(sizeof(struct iptree));
  memset(tmp->ab, 0, 256 * 256);
  tmp->ipl = dlist_init();
  tmp->bl = dlist_init();
  
  if (stat(opt.wl_filename, &sb) == 0 && S_ISREG(sb.st_mode)) {
    double records = (double)sb.st_size / (double)sizeof(struct ipinfo);
    int check = sb.st_size / sizeof(struct ipinfo);

    /* Sanity check */
    if ((double)check != records)
      return tmp;
    
    if ((f = fopen(opt.wl_filename, "r")) != NULL) {
      syslog(LOG_INFO, "loading information from previous dump");
      iptree_getfromfile(tmp, f);
      fclose(f);
    }
  }

  return tmp;
}


/* Add an IP address to a tree */
extern int iptree_add(const iptree ipt, const unsigned int val,
		      const time_t cr_time, const time_t req_time,
		      const int count, const int options)
{
  union {
    long l;
    unsigned char c[sizeof(val)];
  } u;
  unsigned char *iptbl;
  unsigned long pos;
  size_t i;

  u.l = val;
  iptbl = u.c;

  pos = get_pos(iptbl[3], iptbl[2]);

  if (!ipt->ab[pos]) {
    ipt->ab[pos] = xcalloc(sizeof(struct ipt_node), 1);
    if (ipt->ab[pos] == NULL) {
      syslog(LOG_ERR, "iptree_add failed to alloc %d bytes for %u.%u.%u.%u 1",
	     sizeof(struct ipt_node), iptbl[3], iptbl[2], iptbl[1], iptbl[0]);
      return 0;
    }
  }
  
  if (!ipt->ab[pos]->c[iptbl[1]]) {
    ipt->ab[pos]->c[iptbl[1]] = xcalloc(sizeof(struct ipt_leaf), 1);
    if (ipt->ab[pos]->c[iptbl[1]] == NULL) {
      syslog(LOG_ERR, "iptree_add failed to alloc %d bytes for %u.%u.%u.%u 2",
	     sizeof(struct ipt_node), iptbl[3], iptbl[2], iptbl[1], iptbl[0]);
      if (!ipt->ab[pos]->count) {
	syslog(LOG_ERR, "iptree_add freed block %u.%u.", iptbl[3], iptbl[2]);
	free(ipt->ab[pos]);
	ipt->ab[pos] = NULL;
      }
      return 0;
    }
  }
  
  if (!ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]) {
    ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]] = xcalloc(sizeof(struct ipinfo), 1);
    if (ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]] == NULL) {
      syslog(LOG_ERR, "iptree_add failed to alloc %d bytes for %u.%u.%u.%u 3",
	     sizeof(struct ipt_node), iptbl[3], iptbl[2], iptbl[1], iptbl[0]);
      if (!ipt->ab[pos]->c[iptbl[1]]->count) {
	syslog(LOG_ERR, "freed block %u.%u.%u", iptbl[3], iptbl[2], iptbl[1]);
	free(ipt->ab[pos]->c[iptbl[1]]);
	ipt->ab[pos]->c[iptbl[1]] = NULL;
      }
      if (!ipt->ab[pos]->count) {
	syslog(LOG_ERR, "freed block %u.", iptbl[3]);
	free(ipt->ab[pos]);
	ipt->ab[pos] = NULL;
      }
      return 0;
    }
    ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->start = cr_time;
    ipt->ab[pos]->c[iptbl[1]]->count++;
    ipt->ab[pos]->count++;
  }

  for (i = 0; i < sizeof(long); i++)
    ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->iptbl[i] = iptbl[i];

  ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->options |= options;
  
  /* Decrement only if IP count is greater than zero */
  if (count > 0 || ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->count > 0)
    ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->count += count;

  ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->last = req_time;

  if ((options & IPINFO_OPT_FORCED)) {
    if (dlist_add(ipt, ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]],
                  IPTREE_M_BLACKLIST, req_time) == 0) {
      syslog(LOG_INFO, "iptree_add cleaning last added bl entry %u.%u.%u.%u (%p)",
             iptbl[3], iptbl[2], iptbl[1], iptbl[0],
	     (void *)ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]);
      iptree_free(ipt, ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]);
      
      return 0;
    } else
      return 1;
  }
  
  if (dlist_add(ipt, ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]],
                IPTREE_M_IPLIST, req_time) == 0
      && ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->count == 1
      && ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->blp == NULL) {
    syslog(LOG_INFO, "iptree_add cleaning last added entry %u.%u.%u.%u (%p)",
	   iptbl[3], iptbl[2], iptbl[1], iptbl[0],
	   (void *)ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]);
    iptree_free(ipt, ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]);
    
    return 0;
  }
 
  return 1;
}


/* Remove an ipinfo record from the tree and any unused node above */
static void iptree_free(const iptree ipt, struct ipinfo *val)
{
  unsigned long pos;
  unsigned char *iptbl;

  /* Shortcut */
  iptbl = val->iptbl;

  pos = get_pos(iptbl[3], iptbl[2]);

  if (opt.log_level >= 3)
    syslog(LOG_INFO, "free IP %u.%u.%u.%u (%p)",
	   iptbl[3], iptbl[2], iptbl[1], iptbl[0], (void *)val);

  free(val);
  
  ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]] = NULL;
  ipt->ab[pos]->c[iptbl[1]]->count--;
  if (!ipt->ab[pos]->c[iptbl[1]]->count) {
    free(ipt->ab[pos]->c[iptbl[1]]);
    ipt->ab[pos]->c[iptbl[1]] = NULL;
    
    if (opt.log_level >= 3)
      syslog(LOG_INFO, "freed block %u.%u.%u.", iptbl[3], iptbl[2], iptbl[1]);
  }
  
  ipt->ab[pos]->count--;
  if (!ipt->ab[pos]->count) {
    free(ipt->ab[pos]);
    ipt->ab[pos] = NULL;
    
    if (opt.log_level >= 3)
      syslog(LOG_INFO, "freed block %u.%u.", iptbl[3], iptbl[2]);
  }
  
  return;
}


/* Dump all iptree lists to files */
extern void iptree_dump2files(const iptree ipt)
{
  FILE *f;

  if ((f = fopen(opt.wl_filename, "w+")) == NULL) {
    syslog(LOG_ERR, "fopen: %s", strerror(errno));
    return;
  }
  dlist_dump2file(ipt->ipl, f);
  if (opt.log_level >= 3)
    syslog(LOG_INFO, "IP list dumped to %s", opt.wl_filename);
  fclose(f);
  

  if ((f = fopen(opt.bl_filename, "w+")) == NULL) {
    syslog(LOG_ERR, "fopen: %s", strerror(errno));
    return;
  }
  dlist_dump2file(ipt->bl, f);
  if (opt.log_level >= 3)
    syslog(LOG_INFO, "black list dumped to %s", opt.bl_filename);
  fclose(f);
}


/* Return 1 if entry is blacklisted, 0 otherwise */
extern int iptree_get_bl(const iptree ipt, const unsigned long val,
			 const time_t req_time)
{
  unsigned char *iptbl;
  unsigned long pos;
  time_t t, delta;

  iptbl = (unsigned char *)&val;

  pos = get_pos(iptbl[3], iptbl[2]);

  if (ipt->ab[pos] && ipt->ab[pos]->c[iptbl[1]]
      && ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]
      && ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->blp) {
    t = req_time - ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->last;
    if (t <= opt.blacklist_expiration) {
      delta = ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->last
	- ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->start;
      if (ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->options & IPINFO_OPT_FORCED
	  || (delta >= opt.interval
	      && (ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->count * opt.interval
		  / delta >= opt.max_req))) {
	if (opt.log_level >= 3)
	  syslog(LOG_INFO, "request for a blacklisted IP: %u.%u.%u.%u",
		 iptbl[3], iptbl[2], iptbl[1], iptbl[0]);
	return 1;
      }
    } else {
      /* Redemption */
      ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->count = 0;
      ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->options = 0;
      ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->start = req_time;
      ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->last = req_time;

      /* if entry is still in IP list, update it */
      if (ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]->iplp) {
        if (opt.log_level >= 3)
          syslog(LOG_INFO, "re-adding %u.%u.%u.%u (%p)",
                 iptbl[3], iptbl[2], iptbl[1], iptbl[0],
                 (void *)ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]]);
	dlist_add(ipt, ipt->ab[pos]->c[iptbl[1]]->d[iptbl[0]],
		  IPTREE_M_IPLIST, req_time);
      }
    }
  }

  return 0;
}


/* Show some stats via syslog */
extern void iptree_show_stats(const iptree ipt, const time_t t)
{
  if (ipt->ipl->first == NULL)
    syslog(LOG_INFO, "IP list: %u/%u", ipt->ipl->count, opt.list_size);
  else
    syslog(LOG_INFO, "IP list: %u/%u (oldest: %lu secs)",
	   ipt->ipl->count, opt.list_size,
           (unsigned long)t - ipt->ipl->first->p->last);

  if (ipt->bl->first == NULL)
    syslog(LOG_INFO, "Blacklist: %u/%u", ipt->bl->count, opt.blacklist_size);
  else
    syslog(LOG_INFO, "Blacklist: %u/%u (oldest: %lu secs)",
	   ipt->bl->count, opt.blacklist_size,
           (unsigned long)t - ipt->bl->first->p->last);
}