summaryrefslogtreecommitdiffstats
path: root/src/conntrack/compare.c
blob: 02806383e05fe89ee41ea6dbe4e6abc86f051251 (plain)
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
/*
 * (C) 2007 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 */

#include "internal.h"

static int cmp_orig(const struct nf_conntrack *ct1,
		    const struct nf_conntrack *ct2)
{
	if (test_bit(ATTR_ORIG_IPV4_SRC, ct1->set) &&
	    test_bit(ATTR_ORIG_IPV4_SRC, ct2->set) &&
	    ct1->tuple[__DIR_ORIG].src.v4 !=
	    ct2->tuple[__DIR_ORIG].src.v4)
		return 0;

	if (test_bit(ATTR_ORIG_IPV4_DST, ct1->set) &&
	    test_bit(ATTR_ORIG_IPV4_DST, ct2->set) &&
	    ct1->tuple[__DIR_ORIG].dst.v4 !=
	    ct2->tuple[__DIR_ORIG].dst.v4)
		return 0;

	if (test_bit(ATTR_ORIG_IPV6_SRC, ct1->set) &&
	    test_bit(ATTR_ORIG_IPV6_SRC, ct2->set) &&
	    memcmp(&ct1->tuple[__DIR_ORIG].src.v6,
	    	   &ct2->tuple[__DIR_ORIG].src.v6,
		   sizeof(u_int32_t)*4) == 0)
		return 0;

	if (test_bit(ATTR_ORIG_IPV6_DST, ct1->set) &&
	    test_bit(ATTR_ORIG_IPV6_DST, ct2->set) &&
	    memcmp(&ct1->tuple[__DIR_ORIG].dst.v6,
	    	   &ct2->tuple[__DIR_ORIG].dst.v6,
		   sizeof(u_int32_t)*4) == 0)
		return 0;

	if (test_bit(ATTR_ORIG_L3PROTO, ct1->set) &&
	    test_bit(ATTR_ORIG_L3PROTO, ct2->set) &&
	    ct1->tuple[__DIR_ORIG].l3protonum != AF_UNSPEC && 
	    ct2->tuple[__DIR_ORIG].l3protonum != AF_UNSPEC &&
	    ct1->tuple[__DIR_ORIG].l3protonum !=
	    ct2->tuple[__DIR_ORIG].l3protonum)
 	   	return 0;

	if (test_bit(ATTR_ORIG_L4PROTO, ct1->set) &&
	    test_bit(ATTR_ORIG_L4PROTO, ct2->set) &&
	    ct1->tuple[__DIR_ORIG].protonum !=
	    ct2->tuple[__DIR_ORIG].protonum)
		return 0;

	return 1;
}

static int cmp_repl(const struct nf_conntrack *ct1,
		    const struct nf_conntrack *ct2)
{
	if (test_bit(ATTR_REPL_IPV4_SRC, ct1->set) &&
	    test_bit(ATTR_REPL_IPV4_SRC, ct2->set) &&
	    ct1->tuple[__DIR_REPL].src.v4 !=
	    ct2->tuple[__DIR_REPL].src.v4)
		return 0;

	if (test_bit(ATTR_REPL_IPV4_DST, ct1->set) && 
	    test_bit(ATTR_REPL_IPV4_DST, ct2->set) &&
	    ct1->tuple[__DIR_REPL].dst.v4 !=
	    ct2->tuple[__DIR_REPL].dst.v4)
		return 0;

	if (test_bit(ATTR_REPL_IPV6_SRC, ct1->set) &&
	    test_bit(ATTR_REPL_IPV6_SRC, ct2->set) &&
	    memcmp(&ct1->tuple[__DIR_REPL].src.v6,
	    	   &ct2->tuple[__DIR_REPL].src.v6,
		   sizeof(u_int32_t)*4) == 0)
		return 0;

	if (test_bit(ATTR_REPL_IPV6_DST, ct1->set) &&
	    test_bit(ATTR_REPL_IPV6_DST, ct2->set) &&
	    memcmp(&ct1->tuple[__DIR_REPL].dst.v6,
	    	   &ct2->tuple[__DIR_REPL].dst.v6,
		   sizeof(u_int32_t)*4) == 0)
		return 0;

	if (test_bit(ATTR_REPL_L3PROTO, ct1->set) &&
	    test_bit(ATTR_REPL_L3PROTO, ct2->set) &&
	    ct1->tuple[__DIR_REPL].l3protonum != AF_UNSPEC && 
	    ct2->tuple[__DIR_REPL].l3protonum != AF_UNSPEC &&
	    ct1->tuple[__DIR_REPL].l3protonum !=
	    ct2->tuple[__DIR_REPL].l3protonum)
		return 0;

	if (test_bit(ATTR_REPL_L4PROTO, ct1->set) &&
	    test_bit(ATTR_REPL_L4PROTO, ct2->set) &&
	    ct1->tuple[__DIR_REPL].protonum !=
	    ct2->tuple[__DIR_REPL].protonum)
		return 0;

	return 1;
}

static int cmp_meta(const struct nf_conntrack *ct1,
		    const struct nf_conntrack *ct2)
{
	if (test_bit(ATTR_MARK, ct1->set) && 
	    test_bit(ATTR_MARK, ct2->set) &&
	    ct1->mark != ct2->mark)
	    	return 0;

	if (test_bit(ATTR_TIMEOUT, ct1->set) &&
	    test_bit(ATTR_TIMEOUT, ct2->set) &&
	    ct1->timeout != ct2->timeout)
	    	return 0;

	if (test_bit(ATTR_STATUS, ct1->set) &&
	    test_bit(ATTR_STATUS, ct2->set) &&
	    ct1->status == ct2->status)
	    	return 0;

	if (test_bit(ATTR_TCP_STATE, ct1->set) &&
	    test_bit(ATTR_TCP_STATE, ct2->set) &&
	    ct1->protoinfo.tcp.state != ct2->protoinfo.tcp.state)
	    	return 0;

	return 1;
}

int __compare(const struct nf_conntrack *ct1,
	      const struct nf_conntrack *ct2,
	      unsigned int flags)
{
	if (flags == NFCT_CMP_ALL)
		return cmp_orig(ct1, ct2) &&
		       cmp_repl(ct1, ct2) &&
		       cmp_meta(ct1, ct2);

	if (flags & NFCT_CMP_ORIG && !cmp_orig(ct1, ct2))
		return 0;

	if (flags & NFCT_CMP_REPL && !cmp_repl(ct1, ct2))
		return 0;

	return 1;
}