File: filter.c

package info (click to toggle)
ippl 1.4.14-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ansic: 1,683; yacc: 443; sh: 320; lex: 163; makefile: 93
file content (373 lines) | stat: -rw-r--r-- 9,005 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
/*
 *  filter.c - Filtering mechanism
 *
 *  Copyright (C) 1998-1999 Hugo Haas
 *
 *  Added a function to do the DNS queries again if necessary
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include <fnmatch.h>

#include <pthread.h>

#include <netdb.h>
#include <netinet/in.h>

#include "defines.h"
#include "filter.h"
#include "netutils.h"
#include "configuration.h"

struct filter_entry *filter = NULL;

#ifdef FILTER_DEBUG
#include "log.h"
extern struct loginfo logi;
#endif

/* Configuration variables */

extern unsigned short use_ident;
extern unsigned short resolve_protocols;
extern unsigned short portresolve_protocols;
extern unsigned short icmp_format;
extern unsigned short tcp_format;
extern unsigned short udp_format;
extern unsigned short log_closing;

/* Mutexes: if the filter has to be updated, no filtering */
pthread_mutex_t r_mux = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t w_mux = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t w_cond = PTHREAD_COND_INITIALIZER;
int readers = 0;

/*
 * display_info
 *
 * Show info structure (Debug)
 */

#ifdef FILTER_DEBUG
void display_info(struct log_info *info, int entries) {

  log.log(log.level_or_fd, "DBG: (e:%d) log:%d ident:%d resolve:%d portresolve: %d, closing:%d format:%d", entries, info->log, info->ident, info->resolve, info->portresolve, info->logclosing, info->logformat);
}
#endif

/*
 * check_range
 *
 * Check if a number is in a particular range
 */

int check_range(const __u16 number, const long min, const long max, const short protocol) {
  __u16 n;

  if ((min == NOT_DEFINED) && (max == NOT_DEFINED))
    return TRUE;

  if (protocol != IPPROTO_ICMP)
    n = ntohs(number);
  else
    n = number;

  if (min == NOT_DEFINED) {
    if (n <= max)
      return TRUE;
  }
  else {
    if (max == NOT_DEFINED) {
      if (n >= min)
	return TRUE;
    }
    else {
      if ((n <= max) && (n >= min))
	return TRUE;
    }
  }

  return FALSE;
}

/*
 * check_address
 *
 * Check if an address is described by address/mask
 */

int check_address(const __u32 host, const __u32 address, const __u32 mask, char *name) {

  if (address == 0) {
    if (name == NULL)
      return TRUE;
    else
      return FALSE;
  }

  if (((address ^ host) & mask) == 0)
    return TRUE;

  return FALSE;
}

/*
 * check_name
 *
 * Checks if a hostname matches a pattern
 */

int check_name(__u32 address, const char *pattern) {
  char remote_host[HOST_LENGTH];

  *remote_host = '\0';
  host_lookup(remote_host, address);

  if (!(pattern && *pattern))
    return TRUE;

  if (!*remote_host)
    return FALSE;

  if (!fnmatch(pattern, remote_host, FNM_NOESCAPE))
    return TRUE;

  return FALSE;
}

/*
 * do_log
 * plus mutex handlers
 *
 * Returns TRUE if the packet needs to be logged, FALSE otherwise
 */

void add_reader(void) {

  pthread_mutex_lock(&r_mux);
  readers++;
  pthread_mutex_unlock(&r_mux);
}

void remove_reader(void) {

  pthread_mutex_lock(&r_mux);
  readers--;
  if (readers == 0)
    pthread_cond_signal(&w_cond);
  pthread_mutex_unlock(&r_mux);
}

void set_defaults(int protocol, struct log_info *info) {
  if (info->logformat == -1) {
    switch (protocol) {
    case IPPROTO_ICMP:
      info->logformat = icmp_format;
      break;
    case IPPROTO_TCP:
      info->logformat = tcp_format;
      break;
    case IPPROTO_UDP:
      info->logformat = udp_format;
      break;
    }
  }
  if (info->resolve == -1) {
    switch (protocol) {
    case IPPROTO_ICMP:
      info->resolve = resolve_protocols & RUN_ICMP;
      break;
    case IPPROTO_TCP:
      info->resolve = resolve_protocols & RUN_TCP;
      break;
    case IPPROTO_UDP:
      info->resolve = resolve_protocols & RUN_UDP;
      break;
    }
  }
  if (info->portresolve == -1) {
    switch (protocol) {
    case IPPROTO_ICMP:
      info->portresolve = portresolve_protocols & RUN_ICMP;
      break;
    case IPPROTO_TCP:
      info->portresolve = portresolve_protocols & RUN_TCP;
      break;
    case IPPROTO_UDP:
      info->portresolve = portresolve_protocols & RUN_UDP;
      break;
    }
  }
}

struct log_info do_log(const __u32 from, const __u32 to, const __u16 type, const __u16 srctype, const short protocol) {
  struct filter_entry *p;
  struct log_info info;
#ifdef FILTER_DEBUG
  int entries = 0;
#endif

  pthread_mutex_lock(&w_mux);

  add_reader();

  pthread_mutex_unlock(&w_mux);

  for(p = filter; p != NULL; p = p-> next) {

#ifdef FILTER_DEBUG
    entries++;
#endif

    if ((protocol == IPPROTO_ICMP && !(p->protocol & RUN_ICMP)) ||
        (protocol == IPPROTO_TCP && !(p->protocol & RUN_TCP)) ||
        (protocol == IPPROTO_UDP && !(p->protocol & RUN_UDP))) continue;

#ifdef FILTER_DEBUG
    log.log(log.level_or_fd, "DBG: protocol:%d matches p->protocol:%d", protocol, p->protocol);
#endif

    if (protocol != IPPROTO_ICMP) {
      if (check_range(type, p->loginfo.portranges.dst.range_min,
                      p->loginfo.portranges.dst.range_max, protocol) &&
          check_range(srctype, p->loginfo.portranges.src.range_min,
                      p->loginfo.portranges.src.range_max, protocol) &&
          check_address(from, p->fromdesc.address, p->fromdesc.mask,
                        p->fromdesc.hostname) &&
          (p->fromdesc.hostmask == NULL ||
           check_name(from, p->fromdesc.hostmask)) &&
          check_address(to, p->todesc.address, p->todesc.mask,
                        p->todesc.hostname)) {
        remove_reader();
        info.log = p->log;
        info.ident = p->ident;
        info.resolve = p->resolve;
        info.portresolve = p->portresolve;
        info.logformat = p->logformat;
        info.logclosing = p->logclosing;
        set_defaults(protocol, &info);
#ifdef FILTER_DEBUG
        display_info(&info, entries);
#endif
        return info;
      }
    } else {
      if (((p->loginfo.icmptype == NO_ICMP_TYPE)
           || type == p->loginfo.icmptype) &&
          check_address(from, p->fromdesc.address, p->fromdesc.mask,
                        p->fromdesc.hostname) &&
          (p->fromdesc.hostmask == NULL ||
           check_name(from, p->fromdesc.hostmask)) &&
          check_address(to, p->todesc.address, p->todesc.mask,
                        p->todesc.hostname)) {
        remove_reader();
        info.log = p->log;
        info.ident = p->ident;
        info.resolve = p->resolve;
        info.portresolve = p->portresolve;
        info.logformat = p->logformat;
        set_defaults(protocol, &info);
#ifdef FILTER_DEBUG
        display_info(&info, entries);
#endif
        return info;
      }
    }
  }

  remove_reader();
  
  info.log = TRUE;
  info.ident = use_ident;
  info.logclosing = log_closing;
  info.logformat = info.resolve = info.portresolve = -1;
  set_defaults(protocol, &info);

#ifdef FILTER_DEBUG
  display_info(&info, entries);
#endif
  return info;
}

/*
 * refresh_filter
 *
 * Used to periodically do DNS queries for entries in the filter
 */

void refresh_filter() {
  struct filter_entry *p;
  struct hostent * host;

  pthread_mutex_lock(&w_mux);

  pthread_mutex_lock(&r_mux);
  while (readers > 0)
    pthread_cond_wait(&w_cond, &r_mux);

  for(p = filter; p != NULL; p = p-> next) {
    if (p->fromdesc.hostname != NULL) {
      if ((host = gethostbyname((const char *) p->fromdesc.hostname)) == 0) {
        p->fromdesc.address = 0;
        p->fromdesc.mask = 0;
      }
      else {
        memcpy(&(p->fromdesc.address), host->h_addr, host->h_length);
        p->fromdesc.mask = 0xffffffff;
      }
    }
  }

  pthread_mutex_unlock(&r_mux);

  pthread_mutex_unlock(&w_mux);
}

/*
 * destroy_filter
 *
 * Purge the filter
 */

void destroy_filter() {
  struct filter_entry *p;

  /* The mutexes should not be necessary, but let's lock them just in case */
  pthread_mutex_lock(&w_mux);
  pthread_mutex_lock(&r_mux);
  while (readers > 0)
    pthread_cond_wait(&w_cond, &r_mux);

  while ((p = filter) != NULL) {
    filter = filter->next;
    if (p->fromdesc.hostname != NULL)
      free(p->fromdesc.hostname);
    if (p->fromdesc.hostmask != NULL)
      free(p->fromdesc.hostmask);
    if (p->todesc.hostname != NULL)
      free(p->todesc.hostname);
    if (p->todesc.hostmask != NULL)
      free(p->todesc.hostmask);
    free(p);
  }

  pthread_mutex_unlock(&r_mux);
  pthread_mutex_unlock(&w_mux);
}