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. */
46 __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
50 /* The manipulable part of the tuple. */
51 struct nf_conntrack_man
53 union nf_inet_addr u3;
54 union nf_conntrack_man_proto u;
55 /* Layer 3 protocol */
59 /* This contains the information to distinguish a connection. */
60 struct nf_conntrack_tuple
62 struct nf_conntrack_man src;
64 /* These are the parts of the tuple which are fixed. */
66 union nf_inet_addr u3;
68 /* Add other protocols here. */
91 /* The direction (for tuplehash) */
96 struct nf_conntrack_tuple_mask
99 union nf_inet_addr u3;
100 union nf_conntrack_man_proto u;
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) \
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)); \
116 static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
119 printk("tuple %p: %u " NIPQUAD_FMT ":%hu -> " NIPQUAD_FMT ":%hu\n",
121 NIPQUAD(t->src.u3.ip), ntohs(t->src.u.all),
122 NIPQUAD(t->dst.u3.ip), ntohs(t->dst.u.all));
126 static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
129 printk("tuple %p: %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n",
131 NIP6(*(struct in6_addr *)t->src.u3.all), ntohs(t->src.u.all),
132 NIP6(*(struct in6_addr *)t->dst.u3.all), ntohs(t->dst.u.all));
136 static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
138 switch (t->src.l3num) {
140 nf_ct_dump_tuple_ip(t);
143 nf_ct_dump_tuple_ipv6(t);
148 #define NF_CT_DUMP_TUPLE(tp) nf_ct_dump_tuple(tp)
150 /* If we're the first tuple, it's the original dir. */
151 #define NF_CT_DIRECTION(h) \
152 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
154 /* Connections have two entries in the hash table: one for each way */
155 struct nf_conntrack_tuple_hash
157 struct hlist_node hnode;
158 struct nf_conntrack_tuple tuple;
161 #endif /* __KERNEL__ */
163 static inline int __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
164 const struct nf_conntrack_tuple *t2)
166 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
167 t1->src.u.all == t2->src.u.all &&
168 t1->src.l3num == t2->src.l3num);
171 static inline int __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
172 const struct nf_conntrack_tuple *t2)
174 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
175 t1->dst.u.all == t2->dst.u.all &&
176 t1->dst.protonum == t2->dst.protonum);
179 static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
180 const struct nf_conntrack_tuple *t2)
182 return __nf_ct_tuple_src_equal(t1, t2) &&
183 __nf_ct_tuple_dst_equal(t1, t2);
186 static inline int nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
187 const struct nf_conntrack_tuple_mask *m2)
189 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
190 m1->src.u.all == m2->src.u.all);
193 static inline int nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
194 const struct nf_conntrack_tuple *t2,
195 const struct nf_conntrack_tuple_mask *mask)
199 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
200 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
201 mask->src.u3.all[count])
205 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
208 if (t1->src.l3num != t2->src.l3num ||
209 t1->dst.protonum != t2->dst.protonum)
215 static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
216 const struct nf_conntrack_tuple *tuple,
217 const struct nf_conntrack_tuple_mask *mask)
219 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
220 __nf_ct_tuple_dst_equal(t, tuple);
223 #endif /* _NF_CONNTRACK_TUPLE_H */