2 * Definitions and Declarations for tuple.
4 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
5 * - generalize L3 protocol dependent part.
7 * Derived from include/linux/netfiter_ipv4/ip_conntrack_tuple.h
10 #ifndef _NF_CONNTRACK_TUPLE_H
11 #define _NF_CONNTRACK_TUPLE_H
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter/nf_conntrack_tuple_common.h>
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.
20 We divide the structure along "manipulatable" and
21 "non-manipulatable" lines, for the benefit of the NAT code.
24 #define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
26 /* The protocol-specific manipulable parts of the tuple: always in
28 union nf_conntrack_man_proto
30 /* Add other protocols here. */
49 __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
53 /* The manipulable part of the tuple. */
54 struct nf_conntrack_man
56 union nf_inet_addr u3;
57 union nf_conntrack_man_proto u;
58 /* Layer 3 protocol */
62 /* This contains the information to distinguish a connection. */
63 struct nf_conntrack_tuple
65 struct nf_conntrack_man src;
67 /* These are the parts of the tuple which are fixed. */
69 union nf_inet_addr u3;
71 /* Add other protocols here. */
97 /* The direction (for tuplehash) */
102 struct nf_conntrack_tuple_mask
105 union nf_inet_addr u3;
106 union nf_conntrack_man_proto u;
110 /* This is optimized opposed to a memset of the whole structure. Everything we
111 * really care about is the source/destination unions */
112 #define NF_CT_TUPLE_U_BLANK(tuple) \
114 (tuple)->src.u.all = 0; \
115 (tuple)->dst.u.all = 0; \
116 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3)); \
117 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3)); \
122 static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
125 printk("tuple %p: %u " NIPQUAD_FMT ":%hu -> " NIPQUAD_FMT ":%hu\n",
127 NIPQUAD(t->src.u3.ip), ntohs(t->src.u.all),
128 NIPQUAD(t->dst.u3.ip), ntohs(t->dst.u.all));
132 static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
135 printk("tuple %p: %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n",
137 NIP6(*(struct in6_addr *)t->src.u3.all), ntohs(t->src.u.all),
138 NIP6(*(struct in6_addr *)t->dst.u3.all), ntohs(t->dst.u.all));
142 static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
144 switch (t->src.l3num) {
146 nf_ct_dump_tuple_ip(t);
149 nf_ct_dump_tuple_ipv6(t);
154 #define NF_CT_DUMP_TUPLE(tp) nf_ct_dump_tuple(tp)
156 /* If we're the first tuple, it's the original dir. */
157 #define NF_CT_DIRECTION(h) \
158 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
160 /* Connections have two entries in the hash table: one for each way */
161 struct nf_conntrack_tuple_hash
163 struct hlist_node hnode;
164 struct nf_conntrack_tuple tuple;
167 #endif /* __KERNEL__ */
169 static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
170 const struct nf_conntrack_tuple *t2)
172 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
173 t1->src.u.all == t2->src.u.all &&
174 t1->src.l3num == t2->src.l3num);
177 static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
178 const struct nf_conntrack_tuple *t2)
180 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
181 t1->dst.u.all == t2->dst.u.all &&
182 t1->dst.protonum == t2->dst.protonum);
185 static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
186 const struct nf_conntrack_tuple *t2)
188 return __nf_ct_tuple_src_equal(t1, t2) &&
189 __nf_ct_tuple_dst_equal(t1, t2);
193 nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
194 const struct nf_conntrack_tuple_mask *m2)
196 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
197 m1->src.u.all == m2->src.u.all);
201 nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
202 const struct nf_conntrack_tuple *t2,
203 const struct nf_conntrack_tuple_mask *mask)
207 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
208 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
209 mask->src.u3.all[count])
213 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
216 if (t1->src.l3num != t2->src.l3num ||
217 t1->dst.protonum != t2->dst.protonum)
224 nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
225 const struct nf_conntrack_tuple *tuple,
226 const struct nf_conntrack_tuple_mask *mask)
228 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
229 __nf_ct_tuple_dst_equal(t, tuple);
232 #endif /* _NF_CONNTRACK_TUPLE_H */