1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Connection state tracking for netfilter. This is separated from,
4 * but required by, the (future) NAT layer; it can also be used by an iptables
5 * extension.
6 *
7 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 * - generalize L3 protocol dependent part.
9 *
10 * Derived from include/linux/netfiter_ipv4/ip_conntrack.h
11 */
12
13#ifndef _NF_CONNTRACK_H
14#define _NF_CONNTRACK_H
15
16#include <linux/netfilter/nf_conntrack_common.h>
17
18#include <linux/bitops.h>
19#include <linux/compiler.h>
20#include <linux/atomic.h>
21
22#include <linux/netfilter/nf_conntrack_tcp.h>
23#include <linux/netfilter/nf_conntrack_dccp.h>
24#include <linux/netfilter/nf_conntrack_sctp.h>
25#include <linux/netfilter/nf_conntrack_proto_gre.h>
26#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
27
28#include <net/netfilter/nf_conntrack_tuple.h>
29
30struct nf_ct_udp {
31 unsigned long stream_ts;
32};
33
34/* per conntrack: protocol private data */
35union nf_conntrack_proto {
36 /* insert conntrack proto private data here */
37 struct nf_ct_dccp dccp;
38 struct ip_ct_sctp sctp;
39 struct ip_ct_tcp tcp;
40 struct nf_ct_udp udp;
41 struct nf_ct_gre gre;
42 unsigned int tmpl_padto;
43};
44
45union nf_conntrack_expect_proto {
46 /* insert expect proto private data here */
47};
48
49struct nf_conntrack_net {
50 unsigned int users4;
51 unsigned int users6;
52};
53
54#include <linux/types.h>
55#include <linux/skbuff.h>
56
57#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
58#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
59
60struct nf_conn {
61 /* Usage count in here is 1 for hash table, 1 per skb,
62 * plus 1 for any connection(s) we are `master' for
63 *
64 * Hint, SKB address this struct and refcnt via skb->_nfct and
65 * helpers nf_conntrack_get() and nf_conntrack_put().
66 * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt,
67 * beware nf_ct_get() is different and don't inc refcnt.
68 */
69 struct nf_conntrack ct_general;
70
71 spinlock_t lock;
72 u16 cpu;
73
74#ifdef CONFIG_NF_CONNTRACK_ZONES
75 struct nf_conntrack_zone zone;
76#endif
77 /* XXX should I move this to the tail ? - Y.K */
78 /* These are my tuples; original and reply */
79 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
80
81 /* Have we seen traffic both ways yet? (bitset) */
82 unsigned long status;
83
84 /* jiffies32 when this ct is considered dead */
85 u32 timeout;
86
87 possible_net_t ct_net;
88
89#if IS_ENABLED(CONFIG_NF_NAT)
90 struct hlist_node nat_bysource;
91#endif
92 /* all members below initialized via memset */
93 u8 __nfct_init_offset[0];
94
95 /* If we were expected by an expectation, this will be it */
96 struct nf_conn *master;
97
98#if defined(CONFIG_NF_CONNTRACK_MARK)
99 u_int32_t mark;
100#endif
101
102#ifdef CONFIG_NF_CONNTRACK_SECMARK
103 u_int32_t secmark;
104#endif
105
106 /* Extensions */
107 struct nf_ct_ext *ext;
108
109 /* Storage reserved for other modules, must be the last member */
110 union nf_conntrack_proto proto;
111};
112
113static inline struct nf_conn *
114nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
115{
116 return container_of(hash, struct nf_conn,
117 tuplehash[hash->tuple.dst.dir]);
118}
119
120static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
121{
122 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
123}
124
125static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
126{
127 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
128}
129
130#define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
131
132/* get master conntrack via master expectation */
133#define master_ct(conntr) (conntr->master)
134
135extern struct net init_net;
136
137static inline struct net *nf_ct_net(const struct nf_conn *ct)
138{
139 return read_pnet(&ct->ct_net);
140}
141
142/* Alter reply tuple (maybe alter helper). */
143void nf_conntrack_alter_reply(struct nf_conn *ct,
144 const struct nf_conntrack_tuple *newreply);
145
146/* Is this tuple taken? (ignoring any belonging to the given
147 conntrack). */
148int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
149 const struct nf_conn *ignored_conntrack);
150
151#define NFCT_INFOMASK 7UL
152#define NFCT_PTRMASK ~(NFCT_INFOMASK)
153
154/* Return conntrack_info and tuple hash for given skb. */
155static inline struct nf_conn *
156nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
157{
158 *ctinfo = skb->_nfct & NFCT_INFOMASK;
159
160 return (struct nf_conn *)(skb->_nfct & NFCT_PTRMASK);
161}
162
163/* decrement reference count on a conntrack */
164static inline void nf_ct_put(struct nf_conn *ct)
165{
166 WARN_ON(!ct);
167 nf_conntrack_put(&ct->ct_general);
168}
169
170/* Protocol module loading */
171int nf_ct_l3proto_try_module_get(unsigned short l3proto);
172void nf_ct_l3proto_module_put(unsigned short l3proto);
173
174/* load module; enable/disable conntrack in this namespace */
175int nf_ct_netns_get(struct net *net, u8 nfproto);
176void nf_ct_netns_put(struct net *net, u8 nfproto);
177
178/*
179 * Allocate a hashtable of hlist_head (if nulls == 0),
180 * or hlist_nulls_head (if nulls == 1)
181 */
182void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
183
184int nf_conntrack_hash_check_insert(struct nf_conn *ct);
185bool nf_ct_delete(struct nf_conn *ct, u32 pid, int report);
186
187bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
188 u_int16_t l3num, struct net *net,
189 struct nf_conntrack_tuple *tuple);
190
191void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
192 const struct sk_buff *skb,
193 u32 extra_jiffies, bool do_acct);
194
195/* Refresh conntrack for this many jiffies and do accounting */
196static inline void nf_ct_refresh_acct(struct nf_conn *ct,
197 enum ip_conntrack_info ctinfo,
198 const struct sk_buff *skb,
199 u32 extra_jiffies)
200{
201 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, true);
202}
203
204/* Refresh conntrack for this many jiffies */
205static inline void nf_ct_refresh(struct nf_conn *ct,
206 const struct sk_buff *skb,
207 u32 extra_jiffies)
208{
209 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, false);
210}
211
212/* kill conntrack and do accounting */
213bool nf_ct_kill_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
214 const struct sk_buff *skb);
215
216/* kill conntrack without accounting */
217static inline bool nf_ct_kill(struct nf_conn *ct)
218{
219 return nf_ct_delete(ct, 0, 0);
220}
221
222/* Set all unconfirmed conntrack as dying */
223void nf_ct_unconfirmed_destroy(struct net *);
224
225/* Iterate over all conntracks: if iter returns true, it's deleted. */
226void nf_ct_iterate_cleanup_net(struct net *net,
227 int (*iter)(struct nf_conn *i, void *data),
228 void *data, u32 portid, int report);
229
230/* also set unconfirmed conntracks as dying. Only use in module exit path. */
231void nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data),
232 void *data);
233
234struct nf_conntrack_zone;
235
236void nf_conntrack_free(struct nf_conn *ct);
237struct nf_conn *nf_conntrack_alloc(struct net *net,
238 const struct nf_conntrack_zone *zone,
239 const struct nf_conntrack_tuple *orig,
240 const struct nf_conntrack_tuple *repl,
241 gfp_t gfp);
242
243static inline int nf_ct_is_template(const struct nf_conn *ct)
244{
245 return test_bit(IPS_TEMPLATE_BIT, &ct->status);
246}
247
248/* It's confirmed if it is, or has been in the hash table. */
249static inline int nf_ct_is_confirmed(const struct nf_conn *ct)
250{
251 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
252}
253
254static inline int nf_ct_is_dying(const struct nf_conn *ct)
255{
256 return test_bit(IPS_DYING_BIT, &ct->status);
257}
258
259/* Packet is received from loopback */
260static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
261{
262 return skb->dev && skb->skb_iif && skb->dev->flags & IFF_LOOPBACK;
263}
264
265#define nfct_time_stamp ((u32)(jiffies))
266
267/* jiffies until ct expires, 0 if already expired */
268static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
269{
270 s32 timeout = ct->timeout - nfct_time_stamp;
271
272 return timeout > 0 ? timeout : 0;
273}
274
275static inline bool nf_ct_is_expired(const struct nf_conn *ct)
276{
277 return (__s32)(ct->timeout - nfct_time_stamp) <= 0;
278}
279
280/* use after obtaining a reference count */
281static inline bool nf_ct_should_gc(const struct nf_conn *ct)
282{
283 return nf_ct_is_expired(ct) && nf_ct_is_confirmed(ct) &&
284 !nf_ct_is_dying(ct);
285}
286
287struct kernel_param;
288
289int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp);
290int nf_conntrack_hash_resize(unsigned int hashsize);
291
292extern struct hlist_nulls_head *nf_conntrack_hash;
293extern unsigned int nf_conntrack_htable_size;
294extern seqcount_t nf_conntrack_generation;
295extern unsigned int nf_conntrack_max;
296
297/* must be called with rcu read lock held */
298static inline void
299nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
300{
301 struct hlist_nulls_head *hptr;
302 unsigned int sequence, hsz;
303
304 do {
305 sequence = read_seqcount_begin(&nf_conntrack_generation);
306 hsz = nf_conntrack_htable_size;
307 hptr = nf_conntrack_hash;
308 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
309
310 *hash = hptr;
311 *hsize = hsz;
312}
313
314struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
315 const struct nf_conntrack_zone *zone,
316 gfp_t flags);
317void nf_ct_tmpl_free(struct nf_conn *tmpl);
318
319static inline void
320nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info)
321{
322 skb->_nfct = (unsigned long)ct | info;
323}
324
325#define NF_CT_STAT_INC(net, count) __this_cpu_inc((net)->ct.stat->count)
326#define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count)
327#define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v))
328
329#define MODULE_ALIAS_NFCT_HELPER(helper) \
330 MODULE_ALIAS("nfct-helper-" helper)
331
332#endif /* _NF_CONNTRACK_H */
333