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