1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NF_CONNTRACK_TIMEOUT_H
3 #define _NF_CONNTRACK_TIMEOUT_H
4 
5 #include <net/net_namespace.h>
6 #include <linux/netfilter/nf_conntrack_common.h>
7 #include <linux/netfilter/nf_conntrack_tuple_common.h>
8 #include <linux/refcount.h>
9 #include <net/netfilter/nf_conntrack.h>
10 #include <net/netfilter/nf_conntrack_extend.h>
11 
12 #define CTNL_TIMEOUT_NAME_MAX	32
13 
14 struct nf_ct_timeout {
15 	__u16			l3num;
16 	const struct nf_conntrack_l4proto *l4proto;
17 	char			data[0];
18 };
19 
20 struct ctnl_timeout {
21 	struct list_head	head;
22 	struct rcu_head		rcu_head;
23 	refcount_t		refcnt;
24 	char			name[CTNL_TIMEOUT_NAME_MAX];
25 	struct nf_ct_timeout	timeout;
26 };
27 
28 struct nf_conn_timeout {
29 	struct nf_ct_timeout __rcu *timeout;
30 };
31 
32 static inline unsigned int *
33 nf_ct_timeout_data(struct nf_conn_timeout *t)
34 {
35 	struct nf_ct_timeout *timeout;
36 
37 	timeout = rcu_dereference(t->timeout);
38 	if (timeout == NULL)
39 		return NULL;
40 
41 	return (unsigned int *)timeout->data;
42 }
43 
44 static inline
45 struct nf_conn_timeout *nf_ct_timeout_find(const struct nf_conn *ct)
46 {
47 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
48 	return nf_ct_ext_find(ct, NF_CT_EXT_TIMEOUT);
49 #else
50 	return NULL;
51 #endif
52 }
53 
54 static inline
55 struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,
56 					      struct nf_ct_timeout *timeout,
57 					      gfp_t gfp)
58 {
59 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
60 	struct nf_conn_timeout *timeout_ext;
61 
62 	timeout_ext = nf_ct_ext_add(ct, NF_CT_EXT_TIMEOUT, gfp);
63 	if (timeout_ext == NULL)
64 		return NULL;
65 
66 	rcu_assign_pointer(timeout_ext->timeout, timeout);
67 
68 	return timeout_ext;
69 #else
70 	return NULL;
71 #endif
72 };
73 
74 static inline unsigned int *nf_ct_timeout_lookup(const struct nf_conn *ct)
75 {
76 	unsigned int *timeouts = NULL;
77 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
78 	struct nf_conn_timeout *timeout_ext;
79 
80 	timeout_ext = nf_ct_timeout_find(ct);
81 	if (timeout_ext)
82 		timeouts = nf_ct_timeout_data(timeout_ext);
83 #endif
84 	return timeouts;
85 }
86 
87 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
88 int nf_conntrack_timeout_init(void);
89 void nf_conntrack_timeout_fini(void);
90 void nf_ct_untimeout(struct net *net, struct nf_ct_timeout *timeout);
91 #else
92 static inline int nf_conntrack_timeout_init(void)
93 {
94         return 0;
95 }
96 
97 static inline void nf_conntrack_timeout_fini(void)
98 {
99         return;
100 }
101 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
102 
103 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
104 extern struct nf_ct_timeout *(*nf_ct_timeout_find_get_hook)(struct net *net, const char *name);
105 extern void (*nf_ct_timeout_put_hook)(struct nf_ct_timeout *timeout);
106 #endif
107 
108 #endif /* _NF_CONNTRACK_TIMEOUT_H */
109