Merge tag 'for-5.18/io_uring-2022-04-01' of git://git.kernel.dk/linux-block
[linux-block.git] / include / net / netfilter / nf_conntrack_tuple.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
9fb9cbb1
YK
2/*
3 * Definitions and Declarations for tuple.
4 *
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalize L3 protocol dependent part.
7 *
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_tuple.h
9 */
10
11#ifndef _NF_CONNTRACK_TUPLE_H
12#define _NF_CONNTRACK_TUPLE_H
13
643a2c15 14#include <linux/netfilter/x_tables.h>
9fb9cbb1 15#include <linux/netfilter/nf_conntrack_tuple_common.h>
ea781f19 16#include <linux/list_nulls.h>
9fb9cbb1
YK
17
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.
21
22 We divide the structure along "manipulatable" and
23 "non-manipulatable" lines, for the benefit of the NAT code.
24*/
25
643a2c15 26#define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
9fb9cbb1 27
9fb9cbb1 28/* The manipulable part of the tuple. */
fd2c3ef7 29struct nf_conntrack_man {
643a2c15 30 union nf_inet_addr u3;
9fb9cbb1
YK
31 union nf_conntrack_man_proto u;
32 /* Layer 3 protocol */
33 u_int16_t l3num;
34};
35
36/* This contains the information to distinguish a connection. */
fd2c3ef7 37struct nf_conntrack_tuple {
9fb9cbb1
YK
38 struct nf_conntrack_man src;
39
40 /* These are the parts of the tuple which are fixed. */
41 struct {
643a2c15 42 union nf_inet_addr u3;
9fb9cbb1
YK
43 union {
44 /* Add other protocols here. */
a34c4589 45 __be16 all;
9fb9cbb1
YK
46
47 struct {
bff9a89b 48 __be16 port;
9fb9cbb1
YK
49 } tcp;
50 struct {
bff9a89b 51 __be16 port;
9fb9cbb1
YK
52 } udp;
53 struct {
54 u_int8_t type, code;
55 } icmp;
2bc78049
PM
56 struct {
57 __be16 port;
58 } dccp;
9fb9cbb1 59 struct {
bff9a89b 60 __be16 port;
9fb9cbb1 61 } sctp;
f09943fe
PM
62 struct {
63 __be16 key;
64 } gre;
9fb9cbb1
YK
65 } u;
66
67 /* The protocol. */
68 u_int8_t protonum;
69
70 /* The direction (for tuplehash) */
71 u_int8_t dir;
72 } dst;
73};
74
fd2c3ef7 75struct nf_conntrack_tuple_mask {
d4156e8c 76 struct {
643a2c15 77 union nf_inet_addr u3;
d4156e8c
PM
78 union nf_conntrack_man_proto u;
79 } src;
80};
81
ef27559b
PM
82static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
83{
84#ifdef DEBUG
3685f25d 85 printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
ef27559b 86 t, t->dst.protonum,
3685f25d
HH
87 &t->src.u3.ip, ntohs(t->src.u.all),
88 &t->dst.u3.ip, ntohs(t->dst.u.all));
ef27559b
PM
89#endif
90}
91
92static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
93{
94#ifdef DEBUG
5b095d98 95 printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
ef27559b 96 t, t->dst.protonum,
0c6ce78a
HH
97 t->src.u3.all, ntohs(t->src.u.all),
98 t->dst.u3.all, ntohs(t->dst.u.all));
ef27559b
PM
99#endif
100}
101
102static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
103{
104 switch (t->src.l3num) {
105 case AF_INET:
106 nf_ct_dump_tuple_ip(t);
107 break;
108 case AF_INET6:
109 nf_ct_dump_tuple_ipv6(t);
110 break;
111 }
112}
113
9fb9cbb1
YK
114/* If we're the first tuple, it's the original dir. */
115#define NF_CT_DIRECTION(h) \
116 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
117
118/* Connections have two entries in the hash table: one for each way */
ea781f19
ED
119struct nf_conntrack_tuple_hash {
120 struct hlist_nulls_node hnnode;
9fb9cbb1
YK
121 struct nf_conntrack_tuple tuple;
122};
123
5f2b4c90
JE
124static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
125 const struct nf_conntrack_tuple *t2)
b0edba2a 126{
b8beedd2 127 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
9fb9cbb1 128 t1->src.u.all == t2->src.u.all &&
380517de 129 t1->src.l3num == t2->src.l3num);
9fb9cbb1
YK
130}
131
5f2b4c90
JE
132static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
133 const struct nf_conntrack_tuple *t2)
9fb9cbb1 134{
b8beedd2 135 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
9fb9cbb1 136 t1->dst.u.all == t2->dst.u.all &&
9fb9cbb1
YK
137 t1->dst.protonum == t2->dst.protonum);
138}
139
5f2b4c90
JE
140static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
141 const struct nf_conntrack_tuple *t2)
9fb9cbb1 142{
380517de
PM
143 return __nf_ct_tuple_src_equal(t1, t2) &&
144 __nf_ct_tuple_dst_equal(t1, t2);
9fb9cbb1
YK
145}
146
5f2b4c90
JE
147static inline bool
148nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
149 const struct nf_conntrack_tuple_mask *m2)
d4156e8c 150{
b8beedd2 151 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
d4156e8c
PM
152 m1->src.u.all == m2->src.u.all);
153}
154
5f2b4c90
JE
155static inline bool
156nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
157 const struct nf_conntrack_tuple *t2,
158 const struct nf_conntrack_tuple_mask *mask)
d4156e8c
PM
159{
160 int count;
161
162 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
163 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
164 mask->src.u3.all[count])
5f2b4c90 165 return false;
d4156e8c
PM
166 }
167
168 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
5f2b4c90 169 return false;
d4156e8c
PM
170
171 if (t1->src.l3num != t2->src.l3num ||
172 t1->dst.protonum != t2->dst.protonum)
5f2b4c90 173 return false;
d4156e8c 174
5f2b4c90 175 return true;
d4156e8c
PM
176}
177
5f2b4c90
JE
178static inline bool
179nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
180 const struct nf_conntrack_tuple *tuple,
181 const struct nf_conntrack_tuple_mask *mask)
9fb9cbb1 182{
d4156e8c 183 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
380517de 184 __nf_ct_tuple_dst_equal(t, tuple);
9fb9cbb1
YK
185}
186
187#endif /* _NF_CONNTRACK_TUPLE_H */