xref: /linux/include/net/netfilter/nf_conntrack_helper.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * connection tracking helpers.
4  *
5  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6  *	- generalize L3 protocol dependent part.
7  *
8  * Derived from include/linux/netfiter_ipv4/ip_conntrack_helper.h
9  */
10 
11 #ifndef _NF_CONNTRACK_HELPER_H
12 #define _NF_CONNTRACK_HELPER_H
13 #include <linux/refcount.h>
14 #include <net/netfilter/nf_conntrack.h>
15 #include <net/netfilter/nf_conntrack_extend.h>
16 #include <net/netfilter/nf_conntrack_expect.h>
17 
18 #define NF_NAT_HELPER_PREFIX		"ip_nat_"
19 #define NF_NAT_HELPER_NAME(name)	NF_NAT_HELPER_PREFIX name
20 #define MODULE_ALIAS_NF_NAT_HELPER(name) \
21 	MODULE_ALIAS(NF_NAT_HELPER_NAME(name))
22 
23 struct module;
24 
25 enum nf_ct_helper_flags {
26 	NF_CT_HELPER_F_USERSPACE	= (1 << 0),
27 	NF_CT_HELPER_F_CONFIGURED	= (1 << 1),
28 };
29 
30 #define NF_CT_HELPER_NAME_LEN	16
31 
32 /* Must be kept in sync with the classes defined by helpers */
33 #define NF_CT_MAX_EXPECT_CLASSES	4
34 
35 struct nf_conntrack_helper {
36 	struct hlist_node hnode;	/* Internal use. */
37 
38 	struct rcu_head rcu;
39 
40 	char name[NF_CT_HELPER_NAME_LEN]; /* name of the module */
41 	struct module *me;		/* pointer to self */
42 	struct nf_conntrack_expect_policy expect_policy[NF_CT_MAX_EXPECT_CLASSES];
43 
44 	refcount_t ct_refcnt;
45 
46 	u8 nfproto;	/* NFPROTO_*, can be NFPROTO_UNSPEC */
47 	u8 l4proto;	/* IPPROTO_UDP/TCP */
48 
49 	/* Function to call when data passes; return verdict */
50 	int __rcu (*help)(struct sk_buff *skb, unsigned int protoff,
51 			  struct nf_conn *ct,
52 			  enum ip_conntrack_info conntrackinfo);
53 
54 	void (*destroy)(struct nf_conn *ct);
55 
56 	int (*from_nlattr)(struct nlattr *attr, struct nf_conn *ct);
57 	int (*to_nlattr)(struct sk_buff *skb, const struct nf_conn *ct);
58 	unsigned int expect_class_max;
59 
60 	unsigned int flags;
61 
62 	/* For user-space helpers: */
63 	unsigned int queue_num;
64 	/* length of userspace private data stored in nf_conn_help->data */
65 	u16 data_len;
66 	/* name of NAT helper module */
67 	char nat_mod_name[NF_CT_HELPER_NAME_LEN];
68 };
69 
70 /* nf_conn feature for connections that have a helper */
71 struct nf_conn_help {
72 	/* Helper. if any */
73 	struct nf_conntrack_helper __rcu *helper;
74 
75 	struct hlist_head expectations;
76 
77 	/* Current number of expected connections */
78 	u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
79 
80 	/* private helper information. */
81 	char data[32] __aligned(8);
82 };
83 
84 #define NF_CT_HELPER_BUILD_BUG_ON(structsize) \
85 	BUILD_BUG_ON((structsize) > sizeof_field(struct nf_conn_help, data))
86 
87 struct nf_conntrack_helper *__nf_conntrack_helper_find(const char *name,
88 						       u16 l3num, u8 protonum);
89 
90 struct nf_conntrack_helper *nf_conntrack_helper_try_module_get(const char *name,
91 							       u16 l3num,
92 							       u8 protonum);
93 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper);
94 
95 void nf_ct_helper_init(struct nf_conntrack_helper *helper,
96 		       u8 l3num, u16 protonum, const char *name,
97 		       const struct nf_conntrack_expect_policy *exp_pol,
98 		       u32 expect_class_max,
99 		       int (*help)(struct sk_buff *skb, unsigned int protoff,
100 				   struct nf_conn *ct,
101 				   enum ip_conntrack_info ctinfo),
102 		       int (*from_nlattr)(struct nlattr *attr,
103 					  struct nf_conn *ct),
104 		       struct module *module);
105 
106 int nf_conntrack_helper_register(struct nf_conntrack_helper *, struct nf_conntrack_helper **);
107 int __nf_conntrack_helper_register(struct nf_conntrack_helper *);
108 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *);
109 void nf_conntrack_helper_release(struct nf_conntrack_helper *);
110 
111 int nf_conntrack_helpers_register(struct nf_conntrack_helper *, unsigned int,
112 				  struct nf_conntrack_helper **);
113 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper **,
114 				     unsigned int);
115 
116 #define nf_conntrack_helper_deprecated(name) \
117 	pr_warn("The %s conntrack helper is scheduled for removal.\n"	\
118 		"Please contact the netfilter-devel mailing list if you still need this.\n", name)
119 
120 struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp);
121 
122 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
123 			      gfp_t flags);
124 
125 int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct,
126 		 enum ip_conntrack_info ctinfo, u16 proto);
127 int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family,
128 		     u8 proto, bool nat, struct nf_conntrack_helper **hp);
129 
130 void nf_ct_helper_destroy(struct nf_conn *ct);
131 
nfct_help(const struct nf_conn * ct)132 static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct)
133 {
134 	return nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
135 }
136 
nfct_help_data(const struct nf_conn * ct)137 static inline void *nfct_help_data(const struct nf_conn *ct)
138 {
139 	struct nf_conn_help *help;
140 
141 	help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
142 	if (!help)
143 		return NULL;
144 
145 	return (void *)help->data;
146 }
147 
nf_ct_help_put(const struct nf_conn * ct)148 static inline void nf_ct_help_put(const struct nf_conn *ct)
149 {
150 	struct nf_conntrack_helper *helper;
151 	struct nf_conn_help *help;
152 
153 	help = nfct_help(ct);
154 	if (!help)
155 		return;
156 
157 	helper = rcu_dereference(help->helper);
158 	if (helper && refcount_dec_and_test(&helper->ct_refcnt))
159 		kfree_rcu(helper, rcu);
160 }
161 
162 int nf_conntrack_helper_init(void);
163 void nf_conntrack_helper_fini(void);
164 
165 int nf_conntrack_broadcast_help(struct sk_buff *skb, struct nf_conn *ct,
166 				enum ip_conntrack_info ctinfo,
167 				unsigned int timeout);
168 
169 struct nf_ct_helper_expectfn {
170 	struct list_head head;
171 	const char *name;
172 	void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
173 };
174 
175 __printf(3,4)
176 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
177 		      const char *fmt, ...);
178 
179 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
180 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
181 void nf_ct_helper_expectfn_destroy(const struct nf_ct_helper_expectfn *n);
182 struct nf_ct_helper_expectfn *
183 nf_ct_helper_expectfn_find_by_name(const char *name);
184 struct nf_ct_helper_expectfn *
185 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
186 
187 extern struct hlist_head *nf_ct_helper_hash;
188 extern unsigned int nf_ct_helper_hsize;
189 
190 struct nf_conntrack_nat_helper {
191 	struct list_head list;
192 	char mod_name[NF_CT_HELPER_NAME_LEN];	/* module name */
193 	struct module *module;			/* pointer to self */
194 };
195 
196 #define NF_CT_NAT_HELPER_INIT(name) \
197 	{ \
198 	.mod_name = NF_NAT_HELPER_NAME(name), \
199 	.module = THIS_MODULE \
200 	}
201 
202 void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat);
203 void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat);
204 int nf_nat_helper_try_module_get(const char *name, u16 l3num,
205 				 u8 protonum);
206 void nf_nat_helper_put(struct nf_conntrack_helper *helper);
207 #endif /*_NF_CONNTRACK_HELPER_H*/
208