Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[sfrench/cifs-2.6.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/nf_conntrack_tuple_common.h>
14
15 /* A `tuple' is a structure containing the information to uniquely
16   identify a connection.  ie. if two packets have the same tuple, they
17   are in the same connection; if not, they are not.
18
19   We divide the structure along "manipulatable" and
20   "non-manipulatable" lines, for the benefit of the NAT code.
21 */
22
23 #define NF_CT_TUPLE_L3SIZE      4
24
25 /* The l3 protocol-specific manipulable parts of the tuple: always in
26    network order! */
27 union nf_conntrack_man_l3proto {
28         u_int32_t all[NF_CT_TUPLE_L3SIZE];
29         u_int32_t ip;
30         u_int32_t ip6[4];
31 };
32
33 /* The protocol-specific manipulable parts of the tuple: always in
34    network order! */
35 union nf_conntrack_man_proto
36 {
37         /* Add other protocols here. */
38         u_int16_t all;
39
40         struct {
41                 u_int16_t port;
42         } tcp;
43         struct {
44                 u_int16_t port;
45         } udp;
46         struct {
47                 u_int16_t id;
48         } icmp;
49         struct {
50                 u_int16_t port;
51         } sctp;
52 };
53
54 /* The manipulable part of the tuple. */
55 struct nf_conntrack_man
56 {
57         union nf_conntrack_man_l3proto 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 {
71                         u_int32_t all[NF_CT_TUPLE_L3SIZE];
72                         u_int32_t ip;
73                         u_int32_t ip6[4];
74                 } u3;
75                 union {
76                         /* Add other protocols here. */
77                         u_int16_t all;
78
79                         struct {
80                                 u_int16_t port;
81                         } tcp;
82                         struct {
83                                 u_int16_t port;
84                         } udp;
85                         struct {
86                                 u_int8_t type, code;
87                         } icmp;
88                         struct {
89                                 u_int16_t port;
90                         } sctp;
91                 } u;
92
93                 /* The protocol. */
94                 u_int8_t protonum;
95
96                 /* The direction (for tuplehash) */
97                 u_int8_t dir;
98         } dst;
99 };
100
101 /* This is optimized opposed to a memset of the whole structure.  Everything we
102  * really care about is the  source/destination unions */
103 #define NF_CT_TUPLE_U_BLANK(tuple)                                      \
104         do {                                                            \
105                 (tuple)->src.u.all = 0;                                 \
106                 (tuple)->dst.u.all = 0;                                 \
107                 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3));   \
108                 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3));   \
109         } while (0)
110
111 #ifdef __KERNEL__
112
113 #define NF_CT_DUMP_TUPLE(tp)                                                \
114 DEBUGP("tuple %p: %u %u %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x %hu -> %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x %hu\n",                                      \
115         (tp), (tp)->src.l3num, (tp)->dst.protonum,                          \
116         NIP6(*(struct in6_addr *)(tp)->src.u3.all), ntohs((tp)->src.u.all), \
117         NIP6(*(struct in6_addr *)(tp)->dst.u3.all), ntohs((tp)->dst.u.all))
118
119 /* If we're the first tuple, it's the original dir. */
120 #define NF_CT_DIRECTION(h)                                              \
121         ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
122
123 /* Connections have two entries in the hash table: one for each way */
124 struct nf_conntrack_tuple_hash
125 {
126         struct list_head list;
127
128         struct nf_conntrack_tuple tuple;
129 };
130
131 #endif /* __KERNEL__ */
132
133 static inline int nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
134                                         const struct nf_conntrack_tuple *t2)
135
136         return (t1->src.u3.all[0] == t2->src.u3.all[0] &&
137                 t1->src.u3.all[1] == t2->src.u3.all[1] &&
138                 t1->src.u3.all[2] == t2->src.u3.all[2] &&
139                 t1->src.u3.all[3] == t2->src.u3.all[3] &&
140                 t1->src.u.all == t2->src.u.all &&
141                 t1->src.l3num == t2->src.l3num &&
142                 t1->dst.protonum == t2->dst.protonum);
143 }
144
145 static inline int nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
146                                         const struct nf_conntrack_tuple *t2)
147 {
148         return (t1->dst.u3.all[0] == t2->dst.u3.all[0] &&
149                 t1->dst.u3.all[1] == t2->dst.u3.all[1] &&
150                 t1->dst.u3.all[2] == t2->dst.u3.all[2] &&
151                 t1->dst.u3.all[3] == t2->dst.u3.all[3] &&
152                 t1->dst.u.all == t2->dst.u.all &&
153                 t1->src.l3num == t2->src.l3num &&
154                 t1->dst.protonum == t2->dst.protonum);
155 }
156
157 static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
158                                     const struct nf_conntrack_tuple *t2)
159 {
160         return nf_ct_tuple_src_equal(t1, t2) && nf_ct_tuple_dst_equal(t1, t2);
161 }
162
163 static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
164                                        const struct nf_conntrack_tuple *tuple,
165                                        const struct nf_conntrack_tuple *mask)
166 {
167         int count = 0;
168
169         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
170                 if ((t->src.u3.all[count] ^ tuple->src.u3.all[count]) &
171                     mask->src.u3.all[count])
172                         return 0;
173         }
174
175         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
176                 if ((t->dst.u3.all[count] ^ tuple->dst.u3.all[count]) &
177                     mask->dst.u3.all[count])
178                         return 0;
179         }
180
181         if ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all ||
182             (t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all ||
183             (t->src.l3num ^ tuple->src.l3num) & mask->src.l3num ||
184             (t->dst.protonum ^ tuple->dst.protonum) & mask->dst.protonum)
185                 return 0;
186
187         return 1;
188 }
189
190 #endif /* _NF_CONNTRACK_TUPLE_H */