netfilter: conntrack: udp: only extend timeout to stream mode after 2s
[linux-2.6-block.git] / include / net / netfilter / nf_conntrack.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
9fb9cbb1
YK
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
9fb9cbb1
YK
18#include <linux/bitops.h>
19#include <linux/compiler.h>
60063497 20#include <linux/atomic.h>
9fb9cbb1
YK
21
22#include <linux/netfilter/nf_conntrack_tcp.h>
2bc78049 23#include <linux/netfilter/nf_conntrack_dccp.h>
9fb9cbb1 24#include <linux/netfilter/nf_conntrack_sctp.h>
f09943fe 25#include <linux/netfilter/nf_conntrack_proto_gre.h>
9fb9cbb1
YK
26#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
27
28#include <net/netfilter/nf_conntrack_tuple.h>
29
d535c8a6
FW
30struct nf_ct_udp {
31 unsigned long stream_ts;
32};
33
9fb9cbb1
YK
34/* per conntrack: protocol private data */
35union nf_conntrack_proto {
36 /* insert conntrack proto private data here */
2bc78049 37 struct nf_ct_dccp dccp;
9fb9cbb1
YK
38 struct ip_ct_sctp sctp;
39 struct ip_ct_tcp tcp;
d535c8a6 40 struct nf_ct_udp udp;
f09943fe 41 struct nf_ct_gre gre;
c74454fa 42 unsigned int tmpl_padto;
9fb9cbb1
YK
43};
44
45union nf_conntrack_expect_proto {
46 /* insert expect proto private data here */
47};
48
a0ae2562
FW
49struct nf_conntrack_net {
50 unsigned int users4;
51 unsigned int users6;
52};
53
9fb9cbb1
YK
54#include <linux/types.h>
55#include <linux/skbuff.h>
56
9fb9cbb1 57#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
f8eb24a8
PM
58#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
59
ea781f19 60struct nf_conn {
f330a7fd 61 /* Usage count in here is 1 for hash table, 1 per skb,
b476b72a
JDB
62 * plus 1 for any connection(s) we are `master' for
63 *
a9e419dc 64 * Hint, SKB address this struct and refcnt via skb->_nfct and
b476b72a
JDB
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 */
9fb9cbb1
YK
69 struct nf_conntrack ct_general;
70
b7779d06
JDB
71 spinlock_t lock;
72 u16 cpu;
440f0d58 73
6c8dee98
FW
74#ifdef CONFIG_NF_CONNTRACK_ZONES
75 struct nf_conntrack_zone zone;
76#endif
9fb9cbb1
YK
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
f330a7fd
FW
84 /* jiffies32 when this ct is considered dead */
85 u32 timeout;
9fb9cbb1 86
0c5c9fb5
EB
87 possible_net_t ct_net;
88
5173bc67 89#if IS_ENABLED(CONFIG_NF_NAT)
e1bf1687 90 struct hlist_node nat_bysource;
5173bc67 91#endif
c41884ce
FW
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
9fb9cbb1
YK
98#if defined(CONFIG_NF_CONNTRACK_MARK)
99 u_int32_t mark;
100#endif
101
7c9728c3
JM
102#ifdef CONFIG_NF_CONNTRACK_SECMARK
103 u_int32_t secmark;
104#endif
105
ecfab2c9
YK
106 /* Extensions */
107 struct nf_ct_ext *ext;
e5fc9e7a
CG
108
109 /* Storage reserved for other modules, must be the last member */
110 union nf_conntrack_proto proto;
9fb9cbb1
YK
111};
112
9fb9cbb1
YK
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
5e8fbe2a
PM
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
f2f3e38c
PNA
130#define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
131
9fb9cbb1
YK
132/* get master conntrack via master expectation */
133#define master_ct(conntr) (conntr->master)
134
5a1fb391
AD
135extern struct net init_net;
136
137static inline struct net *nf_ct_net(const struct nf_conn *ct)
138{
c2d9ba9b 139 return read_pnet(&ct->ct_net);
5a1fb391
AD
140}
141
9fb9cbb1 142/* Alter reply tuple (maybe alter helper). */
4e77be46
JP
143void nf_conntrack_alter_reply(struct nf_conn *ct,
144 const struct nf_conntrack_tuple *newreply);
9fb9cbb1
YK
145
146/* Is this tuple taken? (ignoring any belonging to the given
147 conntrack). */
4e77be46
JP
148int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
149 const struct nf_conn *ignored_conntrack);
9fb9cbb1 150
30322309 151#define NFCT_INFOMASK 7UL
a9e419dc 152#define NFCT_PTRMASK ~(NFCT_INFOMASK)
30322309 153
9fb9cbb1
YK
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{
a9e419dc
FW
158 *ctinfo = skb->_nfct & NFCT_INFOMASK;
159
160 return (struct nf_conn *)(skb->_nfct & NFCT_PTRMASK);
9fb9cbb1
YK
161}
162
163/* decrement reference count on a conntrack */
164static inline void nf_ct_put(struct nf_conn *ct)
165{
44d6e2f2 166 WARN_ON(!ct);
9fb9cbb1
YK
167 nf_conntrack_put(&ct->ct_general);
168}
169
b9f78f9f 170/* Protocol module loading */
4e77be46
JP
171int nf_ct_l3proto_try_module_get(unsigned short l3proto);
172void nf_ct_l3proto_module_put(unsigned short l3proto);
b9f78f9f 173
ecb2421b
FW
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
ea781f19
ED
178/*
179 * Allocate a hashtable of hlist_head (if nulls == 0),
180 * or hlist_nulls_head (if nulls == 1)
181 */
4e77be46 182void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
ea781f19 183
4e77be46 184int nf_conntrack_hash_check_insert(struct nf_conn *ct);
02982c27 185bool nf_ct_delete(struct nf_conn *ct, u32 pid, int report);
c1d10adb 186
4e77be46 187bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
a31f1adc
EB
188 u_int16_t l3num, struct net *net,
189 struct nf_conntrack_tuple *tuple);
4e77be46
JP
190bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
191 const struct nf_conntrack_tuple *orig);
9fb9cbb1 192
4e77be46
JP
193void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
194 const struct sk_buff *skb,
195 unsigned long extra_jiffies, int do_acct);
9fb9cbb1
YK
196
197/* Refresh conntrack for this many jiffies and do accounting */
198static inline void nf_ct_refresh_acct(struct nf_conn *ct,
199 enum ip_conntrack_info ctinfo,
200 const struct sk_buff *skb,
201 unsigned long extra_jiffies)
202{
203 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
204}
205
206/* Refresh conntrack for this many jiffies */
207static inline void nf_ct_refresh(struct nf_conn *ct,
208 const struct sk_buff *skb,
209 unsigned long extra_jiffies)
210{
211 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
212}
213
718d4ad9 214/* kill conntrack and do accounting */
ad66713f
FW
215bool nf_ct_kill_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
216 const struct sk_buff *skb);
718d4ad9
FH
217
218/* kill conntrack without accounting */
4c889498 219static inline bool nf_ct_kill(struct nf_conn *ct)
718d4ad9 220{
ad66713f 221 return nf_ct_delete(ct, 0, 0);
718d4ad9 222}
51091764 223
84657984
FW
224/* Set all unconfirmed conntrack as dying */
225void nf_ct_unconfirmed_destroy(struct net *);
226
9fb9cbb1 227/* Iterate over all conntracks: if iter returns true, it's deleted. */
9fd6452d
FW
228void nf_ct_iterate_cleanup_net(struct net *net,
229 int (*iter)(struct nf_conn *i, void *data),
230 void *data, u32 portid, int report);
308ac914 231
2843fb69
FW
232/* also set unconfirmed conntracks as dying. Only use in module exit path. */
233void nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data),
234 void *data);
235
308ac914
DB
236struct nf_conntrack_zone;
237
4e77be46 238void nf_conntrack_free(struct nf_conn *ct);
308ac914
DB
239struct nf_conn *nf_conntrack_alloc(struct net *net,
240 const struct nf_conntrack_zone *zone,
4e77be46
JP
241 const struct nf_conntrack_tuple *orig,
242 const struct nf_conntrack_tuple *repl,
243 gfp_t gfp);
9fb9cbb1 244
b2a15a60
PM
245static inline int nf_ct_is_template(const struct nf_conn *ct)
246{
247 return test_bit(IPS_TEMPLATE_BIT, &ct->status);
248}
249
9fb9cbb1 250/* It's confirmed if it is, or has been in the hash table. */
d51ed836 251static inline int nf_ct_is_confirmed(const struct nf_conn *ct)
9fb9cbb1
YK
252{
253 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
254}
255
d51ed836 256static inline int nf_ct_is_dying(const struct nf_conn *ct)
9fb9cbb1
YK
257{
258 return test_bit(IPS_DYING_BIT, &ct->status);
259}
260
42c1edd3
JA
261/* Packet is received from loopback */
262static 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
f330a7fd
FW
267#define nfct_time_stamp ((u32)(jiffies))
268
c8607e02
FW
269/* jiffies until ct expires, 0 if already expired */
270static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
271{
f330a7fd 272 s32 timeout = ct->timeout - nfct_time_stamp;
c8607e02
FW
273
274 return timeout > 0 ? timeout : 0;
275}
276
f330a7fd
FW
277static 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 */
283static 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
34641c6d
PG
289struct kernel_param;
290
e4dca7b7 291int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp);
3183ab89 292int nf_conntrack_hash_resize(unsigned int hashsize);
92e47ba8
LZ
293
294extern struct hlist_nulls_head *nf_conntrack_hash;
9fb9cbb1 295extern unsigned int nf_conntrack_htable_size;
92e47ba8 296extern seqcount_t nf_conntrack_generation;
e478075c 297extern unsigned int nf_conntrack_max;
9fb9cbb1 298
92e47ba8
LZ
299/* must be called with rcu read lock held */
300static inline void
301nf_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
308ac914
DB
316struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
317 const struct nf_conntrack_zone *zone,
318 gfp_t flags);
9cf94eab 319void nf_ct_tmpl_free(struct nf_conn *tmpl);
e53376be 320
c74454fa
FW
321static inline void
322nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info)
323{
a9e419dc 324 skb->_nfct = (unsigned long)ct | info;
c74454fa
FW
325}
326
ac3a546a
ED
327#define NF_CT_STAT_INC(net, count) __this_cpu_inc((net)->ct.stat->count)
328#define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count)
242922a0 329#define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v))
9fb9cbb1 330
4dc06f96
PNA
331#define MODULE_ALIAS_NFCT_HELPER(helper) \
332 MODULE_ALIAS("nfct-helper-" helper)
333
9fb9cbb1 334#endif /* _NF_CONNTRACK_H */