~ubuntu-branches/ubuntu/trusty/conntrack/trusty-proposed

« back to all changes in this revision

Viewing changes to src/ignore_pool.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt, Max Kellermann
  • Date: 2008-04-14 23:09:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080414230922-9xoi1gl38tc8lyng
Tags: 1:0.9.6-4
[ Max Kellermann ]
fix compilation on SPARC (printf argument mismatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * (C) 2006-2007 by Pablo Neira Ayuso <pablo@netfilter.org>
 
3
 * 
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 */
 
18
 
 
19
#include "ignore.h"
 
20
#include "jhash.h"
 
21
#include "hash.h"
 
22
#include "conntrackd.h"
 
23
#include "log.h"
 
24
 
 
25
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
/* XXX: These should be configurable, better use a rb-tree */
 
30
#define IGNORE_POOL_SIZE 128
 
31
#define IGNORE_POOL_LIMIT INT_MAX
 
32
 
 
33
static uint32_t hash(const void *data, struct hashtable *table)
 
34
{
 
35
        const uint32_t *ip = data;
 
36
 
 
37
        return jhash_1word(*ip, 0) % table->hashsize;
 
38
}
 
39
 
 
40
static uint32_t hash6(const void *data, struct hashtable *table)
 
41
{
 
42
        return jhash(data, sizeof(uint32_t)*4, 0) % table->hashsize;
 
43
}
 
44
 
 
45
static int compare(const void *data1, const void *data2)
 
46
{
 
47
        const uint32_t *ip1 = data1;
 
48
        const uint32_t *ip2 = data2;
 
49
 
 
50
        return *ip1 == *ip2;
 
51
}
 
52
 
 
53
static int compare6(const void *data1, const void *data2)
 
54
{
 
55
        return memcmp(data1, data2, sizeof(uint32_t)*4) == 0;
 
56
}
 
57
 
 
58
struct ignore_pool *ignore_pool_create(void)
 
59
{
 
60
        struct ignore_pool *ip;
 
61
 
 
62
        ip = malloc(sizeof(struct ignore_pool));
 
63
        if (!ip)
 
64
                return NULL;
 
65
        memset(ip, 0, sizeof(struct ignore_pool));
 
66
 
 
67
        ip->h = hashtable_create(IGNORE_POOL_SIZE,
 
68
                                 IGNORE_POOL_LIMIT,
 
69
                                 sizeof(uint32_t),
 
70
                                 hash,
 
71
                                 compare);
 
72
        if (!ip->h) {
 
73
                free(ip);
 
74
                return NULL;
 
75
        }
 
76
 
 
77
        ip->h6 = hashtable_create(IGNORE_POOL_SIZE,
 
78
                                  IGNORE_POOL_LIMIT,
 
79
                                  sizeof(uint32_t)*4,
 
80
                                  hash6,
 
81
                                  compare6);
 
82
        if (!ip->h6) {
 
83
                free(ip->h);
 
84
                free(ip);
 
85
                return NULL;
 
86
        }
 
87
 
 
88
        return ip;
 
89
}
 
90
 
 
91
void ignore_pool_destroy(struct ignore_pool *ip)
 
92
{
 
93
        hashtable_destroy(ip->h);
 
94
        hashtable_destroy(ip->h6);
 
95
        free(ip);
 
96
}
 
97
 
 
98
int ignore_pool_add(struct ignore_pool *ip, void *data, uint8_t family)
 
99
{
 
100
        switch(family) {
 
101
                case AF_INET:
 
102
                        if (!hashtable_add(ip->h, data))
 
103
                                return 0;
 
104
                        break;
 
105
                case AF_INET6:
 
106
                        if (!hashtable_add(ip->h6, data))
 
107
                                return 0;
 
108
                        break;
 
109
        }
 
110
        return 1;
 
111
}
 
112
 
 
113
static int
 
114
__ignore_pool_test_ipv4(struct ignore_pool *ip, struct nf_conntrack *ct)
 
115
{
 
116
        if (!ip->h)
 
117
                return 0;
 
118
 
 
119
        return (hashtable_test(ip->h, nfct_get_attr(ct, ATTR_ORIG_IPV4_SRC)) ||
 
120
                hashtable_test(ip->h, nfct_get_attr(ct, ATTR_ORIG_IPV4_DST)) ||
 
121
                hashtable_test(ip->h, nfct_get_attr(ct, ATTR_REPL_IPV4_SRC)) ||
 
122
                hashtable_test(ip->h, nfct_get_attr(ct, ATTR_REPL_IPV4_DST)));
 
123
}
 
124
 
 
125
static int
 
126
__ignore_pool_test_ipv6(struct ignore_pool *ip, struct nf_conntrack *ct)
 
127
{
 
128
        if (!ip->h6)
 
129
                return 0;
 
130
 
 
131
        return (hashtable_test(ip->h6, nfct_get_attr(ct, ATTR_ORIG_IPV6_SRC)) ||
 
132
                hashtable_test(ip->h6, nfct_get_attr(ct, ATTR_ORIG_IPV6_DST)) ||
 
133
                hashtable_test(ip->h6, nfct_get_attr(ct, ATTR_REPL_IPV6_SRC)) ||
 
134
                hashtable_test(ip->h6, nfct_get_attr(ct, ATTR_REPL_IPV6_DST)));
 
135
}
 
136
 
 
137
int ignore_pool_test(struct ignore_pool *ip, struct nf_conntrack *ct)
 
138
{
 
139
        int ret = 0;
 
140
 
 
141
        switch(nfct_get_attr_u8(ct, ATTR_ORIG_L3PROTO)) {
 
142
        case AF_INET:
 
143
                ret = __ignore_pool_test_ipv4(ip, ct);
 
144
                break;
 
145
        case AF_INET6:
 
146
                ret = __ignore_pool_test_ipv6(ip, ct);
 
147
                break;
 
148
        default:
 
149
                dlog(LOG_WARNING, "unknown layer 3 protocol?");
 
150
                break;
 
151
        }
 
152
 
 
153
        return ret;
 
154
}