netfilter: nf_conntrack: use SLAB_DESTROY_BY_RCU and get rid of call_rcu()
[pandora-kernel.git] / include / net / netfilter / nf_conntrack_tuple.h
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
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter/nf_conntrack_tuple_common.h>
15 #include <linux/list_nulls.h>
16
17 /* A `tuple' is a structure containing the information to uniquely
18   identify a connection.  ie. if two packets have the same tuple, they
19   are in the same connection; if not, they are not.
20
21   We divide the structure along "manipulatable" and
22   "non-manipulatable" lines, for the benefit of the NAT code.
23 */
24
25 #define NF_CT_TUPLE_L3SIZE      ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
26
27 /* The protocol-specific manipulable parts of the tuple: always in
28    network order! */
29 union nf_conntrack_man_proto
30 {
31         /* Add other protocols here. */
32         __be16 all;
33
34         struct {
35                 __be16 port;
36         } tcp;
37         struct {
38                 __be16 port;
39         } udp;
40         struct {
41                 __be16 id;
42         } icmp;
43         struct {
44                 __be16 port;
45         } dccp;
46         struct {
47                 __be16 port;
48         } sctp;
49         struct {
50                 __be16 key;     /* GRE key is 32bit, PPtP only uses 16bit */
51         } gre;
52 };
53
54 /* The manipulable part of the tuple. */
55 struct nf_conntrack_man
56 {
57         union nf_inet_addr u3;
58         union nf_conntrack_man_proto u;
59         /* Layer 3 protocol */
60         u_int16_t l3num;
61 };
62
63 /* This contains the information to distinguish a connection. */
64 struct nf_conntrack_tuple
65 {
66         struct nf_conntrack_man src;
67
68         /* These are the parts of the tuple which are fixed. */
69         struct {
70                 union nf_inet_addr u3;
71                 union {
72                         /* Add other protocols here. */
73                         __be16 all;
74
75                         struct {
76                                 __be16 port;
77                         } tcp;
78                         struct {
79                                 __be16 port;
80                         } udp;
81                         struct {
82                                 u_int8_t type, code;
83                         } icmp;
84                         struct {
85                                 __be16 port;
86                         } dccp;
87                         struct {
88                                 __be16 port;
89                         } sctp;
90                         struct {
91                                 __be16 key;
92                         } gre;
93                 } u;
94
95                 /* The protocol. */
96                 u_int8_t protonum;
97
98                 /* The direction (for tuplehash) */
99                 u_int8_t dir;
100         } dst;
101 };
102
103 struct nf_conntrack_tuple_mask
104 {
105         struct {
106                 union nf_inet_addr u3;
107                 union nf_conntrack_man_proto u;
108         } src;
109 };
110
111 #ifdef __KERNEL__
112
113 static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
114 {
115 #ifdef DEBUG
116         printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
117                t, t->dst.protonum,
118                &t->src.u3.ip, ntohs(t->src.u.all),
119                &t->dst.u3.ip, ntohs(t->dst.u.all));
120 #endif
121 }
122
123 static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
124 {
125 #ifdef DEBUG
126         printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
127                t, t->dst.protonum,
128                t->src.u3.all, ntohs(t->src.u.all),
129                t->dst.u3.all, ntohs(t->dst.u.all));
130 #endif
131 }
132
133 static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
134 {
135         switch (t->src.l3num) {
136         case AF_INET:
137                 nf_ct_dump_tuple_ip(t);
138                 break;
139         case AF_INET6:
140                 nf_ct_dump_tuple_ipv6(t);
141                 break;
142         }
143 }
144
145 /* If we're the first tuple, it's the original dir. */
146 #define NF_CT_DIRECTION(h)                                              \
147         ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
148
149 /* Connections have two entries in the hash table: one for each way */
150 struct nf_conntrack_tuple_hash {
151         struct hlist_nulls_node hnnode;
152         struct nf_conntrack_tuple tuple;
153 };
154
155 #endif /* __KERNEL__ */
156
157 static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
158                                            const struct nf_conntrack_tuple *t2)
159
160         return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
161                 t1->src.u.all == t2->src.u.all &&
162                 t1->src.l3num == t2->src.l3num);
163 }
164
165 static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
166                                            const struct nf_conntrack_tuple *t2)
167 {
168         return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
169                 t1->dst.u.all == t2->dst.u.all &&
170                 t1->dst.protonum == t2->dst.protonum);
171 }
172
173 static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
174                                      const struct nf_conntrack_tuple *t2)
175 {
176         return __nf_ct_tuple_src_equal(t1, t2) &&
177                __nf_ct_tuple_dst_equal(t1, t2);
178 }
179
180 static inline bool
181 nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
182                        const struct nf_conntrack_tuple_mask *m2)
183 {
184         return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
185                 m1->src.u.all == m2->src.u.all);
186 }
187
188 static inline bool
189 nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
190                          const struct nf_conntrack_tuple *t2,
191                          const struct nf_conntrack_tuple_mask *mask)
192 {
193         int count;
194
195         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
196                 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
197                     mask->src.u3.all[count])
198                         return false;
199         }
200
201         if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
202                 return false;
203
204         if (t1->src.l3num != t2->src.l3num ||
205             t1->dst.protonum != t2->dst.protonum)
206                 return false;
207
208         return true;
209 }
210
211 static inline bool
212 nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
213                      const struct nf_conntrack_tuple *tuple,
214                      const struct nf_conntrack_tuple_mask *mask)
215 {
216         return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
217                __nf_ct_tuple_dst_equal(t, tuple);
218 }
219
220 #endif /* _NF_CONNTRACK_TUPLE_H */