xref: /linux/include/net/netfilter/nf_conntrack_timeout.h (revision a55f7f5f29b32c2c53cc291899cf9b0c25a07f7c)
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 	struct rcu_head		rcu;
18 	char			data[];
19 };
20 
21 struct nf_conn_timeout {
22 	struct nf_ct_timeout __rcu *timeout;
23 };
24 
25 static inline unsigned int *
nf_ct_timeout_data(const struct nf_conn_timeout * t)26 nf_ct_timeout_data(const struct nf_conn_timeout *t)
27 {
28 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
29 	struct nf_ct_timeout *timeout;
30 
31 	timeout = rcu_dereference(t->timeout);
32 	if (timeout == NULL)
33 		return NULL;
34 
35 	return (unsigned int *)timeout->data;
36 #else
37 	return NULL;
38 #endif
39 }
40 
41 static inline
nf_ct_timeout_find(const struct nf_conn * ct)42 struct nf_conn_timeout *nf_ct_timeout_find(const struct nf_conn *ct)
43 {
44 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
45 	return nf_ct_ext_find(ct, NF_CT_EXT_TIMEOUT);
46 #else
47 	return NULL;
48 #endif
49 }
50 
51 static inline
nf_ct_timeout_ext_add(struct nf_conn * ct,struct nf_ct_timeout * timeout,gfp_t gfp)52 struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,
53 					      struct nf_ct_timeout *timeout,
54 					      gfp_t gfp)
55 {
56 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
57 	struct nf_conn_timeout *timeout_ext;
58 
59 	timeout_ext = nf_ct_ext_add(ct, NF_CT_EXT_TIMEOUT, gfp);
60 	if (timeout_ext == NULL)
61 		return NULL;
62 
63 	rcu_assign_pointer(timeout_ext->timeout, timeout);
64 
65 	return timeout_ext;
66 #else
67 	return NULL;
68 #endif
69 };
70 
nf_ct_timeout_lookup(const struct nf_conn * ct)71 static inline unsigned int *nf_ct_timeout_lookup(const struct nf_conn *ct)
72 {
73 	unsigned int *timeouts = NULL;
74 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
75 	struct nf_conn_timeout *timeout_ext;
76 
77 	timeout_ext = nf_ct_timeout_find(ct);
78 	if (timeout_ext)
79 		timeouts = nf_ct_timeout_data(timeout_ext);
80 #endif
81 	return timeouts;
82 }
83 
84 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
85 void nf_ct_untimeout(struct net *net, struct nf_ct_timeout *timeout);
86 int nf_ct_set_timeout(struct net *net, struct nf_conn *ct, u8 l3num, u8 l4num,
87 		      const char *timeout_name);
88 void nf_ct_destroy_timeout(struct nf_conn *ct);
89 #else
nf_ct_set_timeout(struct net * net,struct nf_conn * ct,u8 l3num,u8 l4num,const char * timeout_name)90 static inline int nf_ct_set_timeout(struct net *net, struct nf_conn *ct,
91 				    u8 l3num, u8 l4num,
92 				    const char *timeout_name)
93 {
94 	return -EOPNOTSUPP;
95 }
96 
nf_ct_destroy_timeout(struct nf_conn * ct)97 static inline void nf_ct_destroy_timeout(struct nf_conn *ct)
98 {
99 	return;
100 }
101 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
102 
103 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
104 struct nf_ct_timeout_hooks {
105 	struct nf_ct_timeout *(*timeout_find_get)(struct net *net, const char *name);
106 	void (*timeout_put)(struct nf_ct_timeout *timeout);
107 };
108 
109 extern const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook;
110 #endif
111 
112 #endif /* _NF_CONNTRACK_TIMEOUT_H */
113