[NETFILTER]: nf_conntrack: optimize __nf_conntrack_find()
[linux-block.git] / include / net / netfilter / nf_conntrack_tuple.h
CommitLineData
9fb9cbb1
YK
1/*
2 * Definitions and Declarations for tuple.
3 *
4 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
5 * - generalize L3 protocol dependent part.
6 *
7 * Derived from include/linux/netfiter_ipv4/ip_conntrack_tuple.h
8 */
9
10#ifndef _NF_CONNTRACK_TUPLE_H
11#define _NF_CONNTRACK_TUPLE_H
12
643a2c15 13#include <linux/netfilter/x_tables.h>
9fb9cbb1
YK
14#include <linux/netfilter/nf_conntrack_tuple_common.h>
15
16/* A `tuple' is a structure containing the information to uniquely
17 identify a connection. ie. if two packets have the same tuple, they
18 are in the same connection; if not, they are not.
19
20 We divide the structure along "manipulatable" and
21 "non-manipulatable" lines, for the benefit of the NAT code.
22*/
23
643a2c15 24#define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
9fb9cbb1
YK
25
26/* The protocol-specific manipulable parts of the tuple: always in
27 network order! */
28union nf_conntrack_man_proto
29{
30 /* Add other protocols here. */
a34c4589 31 __be16 all;
9fb9cbb1
YK
32
33 struct {
bff9a89b 34 __be16 port;
9fb9cbb1
YK
35 } tcp;
36 struct {
bff9a89b 37 __be16 port;
9fb9cbb1
YK
38 } udp;
39 struct {
bff9a89b 40 __be16 id;
9fb9cbb1
YK
41 } icmp;
42 struct {
bff9a89b 43 __be16 port;
9fb9cbb1 44 } sctp;
f09943fe
PM
45 struct {
46 __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
47 } gre;
9fb9cbb1
YK
48};
49
50/* The manipulable part of the tuple. */
51struct nf_conntrack_man
52{
643a2c15 53 union nf_inet_addr u3;
9fb9cbb1
YK
54 union nf_conntrack_man_proto u;
55 /* Layer 3 protocol */
56 u_int16_t l3num;
57};
58
59/* This contains the information to distinguish a connection. */
60struct nf_conntrack_tuple
61{
62 struct nf_conntrack_man src;
63
64 /* These are the parts of the tuple which are fixed. */
65 struct {
643a2c15 66 union nf_inet_addr u3;
9fb9cbb1
YK
67 union {
68 /* Add other protocols here. */
a34c4589 69 __be16 all;
9fb9cbb1
YK
70
71 struct {
bff9a89b 72 __be16 port;
9fb9cbb1
YK
73 } tcp;
74 struct {
bff9a89b 75 __be16 port;
9fb9cbb1
YK
76 } udp;
77 struct {
78 u_int8_t type, code;
79 } icmp;
80 struct {
bff9a89b 81 __be16 port;
9fb9cbb1 82 } sctp;
f09943fe
PM
83 struct {
84 __be16 key;
85 } gre;
9fb9cbb1
YK
86 } u;
87
88 /* The protocol. */
89 u_int8_t protonum;
90
91 /* The direction (for tuplehash) */
92 u_int8_t dir;
93 } dst;
94};
95
d4156e8c
PM
96struct nf_conntrack_tuple_mask
97{
98 struct {
643a2c15 99 union nf_inet_addr u3;
d4156e8c
PM
100 union nf_conntrack_man_proto u;
101 } src;
102};
103
9fb9cbb1
YK
104/* This is optimized opposed to a memset of the whole structure. Everything we
105 * really care about is the source/destination unions */
106#define NF_CT_TUPLE_U_BLANK(tuple) \
107 do { \
108 (tuple)->src.u.all = 0; \
109 (tuple)->dst.u.all = 0; \
110 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3)); \
111 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3)); \
112 } while (0)
113
114#ifdef __KERNEL__
115
0d53778e
PM
116#define NF_CT_DUMP_TUPLE(tp) \
117pr_debug("tuple %p: %u %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n", \
118 (tp), (tp)->src.l3num, (tp)->dst.protonum, \
119 NIP6(*(struct in6_addr *)(tp)->src.u3.all), ntohs((tp)->src.u.all), \
120 NIP6(*(struct in6_addr *)(tp)->dst.u3.all), ntohs((tp)->dst.u.all))
9fb9cbb1
YK
121
122/* If we're the first tuple, it's the original dir. */
123#define NF_CT_DIRECTION(h) \
124 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
125
126/* Connections have two entries in the hash table: one for each way */
127struct nf_conntrack_tuple_hash
128{
f205c5e0 129 struct hlist_node hnode;
9fb9cbb1
YK
130 struct nf_conntrack_tuple tuple;
131};
132
133#endif /* __KERNEL__ */
134
135static inline int nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
136 const struct nf_conntrack_tuple *t2)
137{
138 return (t1->src.u3.all[0] == t2->src.u3.all[0] &&
139 t1->src.u3.all[1] == t2->src.u3.all[1] &&
140 t1->src.u3.all[2] == t2->src.u3.all[2] &&
141 t1->src.u3.all[3] == t2->src.u3.all[3] &&
142 t1->src.u.all == t2->src.u.all &&
143 t1->src.l3num == t2->src.l3num &&
144 t1->dst.protonum == t2->dst.protonum);
145}
146
147static inline int nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
148 const struct nf_conntrack_tuple *t2)
149{
150 return (t1->dst.u3.all[0] == t2->dst.u3.all[0] &&
151 t1->dst.u3.all[1] == t2->dst.u3.all[1] &&
152 t1->dst.u3.all[2] == t2->dst.u3.all[2] &&
153 t1->dst.u3.all[3] == t2->dst.u3.all[3] &&
154 t1->dst.u.all == t2->dst.u.all &&
155 t1->src.l3num == t2->src.l3num &&
156 t1->dst.protonum == t2->dst.protonum);
157}
158
159static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
160 const struct nf_conntrack_tuple *t2)
161{
162 return nf_ct_tuple_src_equal(t1, t2) && nf_ct_tuple_dst_equal(t1, t2);
163}
164
d4156e8c
PM
165static inline int nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
166 const struct nf_conntrack_tuple_mask *m2)
167{
168 return (m1->src.u3.all[0] == m2->src.u3.all[0] &&
169 m1->src.u3.all[1] == m2->src.u3.all[1] &&
170 m1->src.u3.all[2] == m2->src.u3.all[2] &&
171 m1->src.u3.all[3] == m2->src.u3.all[3] &&
172 m1->src.u.all == m2->src.u.all);
173}
174
175static inline int nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
176 const struct nf_conntrack_tuple *t2,
177 const struct nf_conntrack_tuple_mask *mask)
178{
179 int count;
180
181 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
182 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
183 mask->src.u3.all[count])
184 return 0;
185 }
186
187 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
188 return 0;
189
190 if (t1->src.l3num != t2->src.l3num ||
191 t1->dst.protonum != t2->dst.protonum)
192 return 0;
193
194 return 1;
195}
196
9fb9cbb1
YK
197static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
198 const struct nf_conntrack_tuple *tuple,
d4156e8c 199 const struct nf_conntrack_tuple_mask *mask)
9fb9cbb1 200{
d4156e8c
PM
201 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
202 nf_ct_tuple_dst_equal(t, tuple);
9fb9cbb1
YK
203}
204
205#endif /* _NF_CONNTRACK_TUPLE_H */