1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Definitions and Declarations for tuple.
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalize L3 protocol dependent part.
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_tuple.h
11 #ifndef _NF_CONNTRACK_TUPLE_H
12 #define _NF_CONNTRACK_TUPLE_H
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/nf_conntrack_tuple_common.h>
16 #include <linux/list_nulls.h>
18 /* A `tuple' is a structure containing the information to uniquely
19 identify a connection. ie. if two packets have the same tuple, they
20 are in the same connection; if not, they are not.
22 We divide the structure along "manipulatable" and
23 "non-manipulatable" lines, for the benefit of the NAT code.
26 #define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
28 /* The manipulable part of the tuple. */
29 struct nf_conntrack_man {
30 union nf_inet_addr u3;
31 union nf_conntrack_man_proto u;
32 /* Layer 3 protocol */
36 /* This contains the information to distinguish a connection. */
37 struct nf_conntrack_tuple {
38 struct nf_conntrack_man src;
40 /* These are the parts of the tuple which are fixed. */
42 union nf_inet_addr u3;
44 /* Add other protocols here. */
70 /* The direction must be ignored for the tuplehash */
71 struct { } __nfct_hash_offsetend;
73 /* The direction (for tuplehash) */
78 struct nf_conntrack_tuple_mask {
80 union nf_inet_addr u3;
81 union nf_conntrack_man_proto u;
85 static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
88 printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
90 &t->src.u3.ip, ntohs(t->src.u.all),
91 &t->dst.u3.ip, ntohs(t->dst.u.all));
95 static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
98 printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
100 t->src.u3.all, ntohs(t->src.u.all),
101 t->dst.u3.all, ntohs(t->dst.u.all));
105 static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
107 switch (t->src.l3num) {
109 nf_ct_dump_tuple_ip(t);
112 nf_ct_dump_tuple_ipv6(t);
117 /* If we're the first tuple, it's the original dir. */
118 #define NF_CT_DIRECTION(h) \
119 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
121 /* Connections have two entries in the hash table: one for each way */
122 struct nf_conntrack_tuple_hash {
123 struct hlist_nulls_node hnnode;
124 struct nf_conntrack_tuple tuple;
127 static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
128 const struct nf_conntrack_tuple *t2)
130 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
131 t1->src.u.all == t2->src.u.all &&
132 t1->src.l3num == t2->src.l3num);
135 static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
136 const struct nf_conntrack_tuple *t2)
138 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
139 t1->dst.u.all == t2->dst.u.all &&
140 t1->dst.protonum == t2->dst.protonum);
143 static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
144 const struct nf_conntrack_tuple *t2)
146 return __nf_ct_tuple_src_equal(t1, t2) &&
147 __nf_ct_tuple_dst_equal(t1, t2);
151 nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
152 const struct nf_conntrack_tuple_mask *m2)
154 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
155 m1->src.u.all == m2->src.u.all);
159 nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
160 const struct nf_conntrack_tuple *t2,
161 const struct nf_conntrack_tuple_mask *mask)
165 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
166 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
167 mask->src.u3.all[count])
171 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
174 if (t1->src.l3num != t2->src.l3num ||
175 t1->dst.protonum != t2->dst.protonum)
182 nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
183 const struct nf_conntrack_tuple *tuple,
184 const struct nf_conntrack_tuple_mask *mask)
186 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
187 __nf_ct_tuple_dst_equal(t, tuple);
190 #endif /* _NF_CONNTRACK_TUPLE_H */