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