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