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