xref: /openbmc/linux/include/linux/netfilter.h (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
3 
4 #include <linux/init.h>
5 #include <linux/skbuff.h>
6 #include <linux/net.h>
7 #include <linux/if.h>
8 #include <linux/in.h>
9 #include <linux/in6.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
12 #include <linux/static_key.h>
13 #include <linux/netfilter_defs.h>
14 #include <linux/netdevice.h>
15 #include <net/net_namespace.h>
16 
17 #ifdef CONFIG_NETFILTER
18 static inline int NF_DROP_GETERR(int verdict)
19 {
20 	return -(verdict >> NF_VERDICT_QBITS);
21 }
22 
23 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
24 				   const union nf_inet_addr *a2)
25 {
26 	return a1->all[0] == a2->all[0] &&
27 	       a1->all[1] == a2->all[1] &&
28 	       a1->all[2] == a2->all[2] &&
29 	       a1->all[3] == a2->all[3];
30 }
31 
32 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
33 				     union nf_inet_addr *result,
34 				     const union nf_inet_addr *mask)
35 {
36 	result->all[0] = a1->all[0] & mask->all[0];
37 	result->all[1] = a1->all[1] & mask->all[1];
38 	result->all[2] = a1->all[2] & mask->all[2];
39 	result->all[3] = a1->all[3] & mask->all[3];
40 }
41 
42 int netfilter_init(void);
43 
44 struct sk_buff;
45 
46 struct nf_hook_ops;
47 
48 struct sock;
49 
50 struct nf_hook_state {
51 	unsigned int hook;
52 	u_int8_t pf;
53 	struct net_device *in;
54 	struct net_device *out;
55 	struct sock *sk;
56 	struct net *net;
57 	int (*okfn)(struct net *, struct sock *, struct sk_buff *);
58 };
59 
60 typedef unsigned int nf_hookfn(void *priv,
61 			       struct sk_buff *skb,
62 			       const struct nf_hook_state *state);
63 struct nf_hook_ops {
64 	struct list_head	list;
65 
66 	/* User fills in from here down. */
67 	nf_hookfn		*hook;
68 	struct net_device	*dev;
69 	void			*priv;
70 	u_int8_t		pf;
71 	unsigned int		hooknum;
72 	/* Hooks are ordered in ascending priority. */
73 	int			priority;
74 };
75 
76 struct nf_hook_entry {
77 	struct nf_hook_entry __rcu	*next;
78 	struct nf_hook_ops		ops;
79 	const struct nf_hook_ops	*orig_ops;
80 };
81 
82 static inline void nf_hook_state_init(struct nf_hook_state *p,
83 				      unsigned int hook,
84 				      u_int8_t pf,
85 				      struct net_device *indev,
86 				      struct net_device *outdev,
87 				      struct sock *sk,
88 				      struct net *net,
89 				      int (*okfn)(struct net *, struct sock *, struct sk_buff *))
90 {
91 	p->hook = hook;
92 	p->pf = pf;
93 	p->in = indev;
94 	p->out = outdev;
95 	p->sk = sk;
96 	p->net = net;
97 	p->okfn = okfn;
98 }
99 
100 
101 
102 struct nf_sockopt_ops {
103 	struct list_head list;
104 
105 	u_int8_t pf;
106 
107 	/* Non-inclusive ranges: use 0/0/NULL to never get called. */
108 	int set_optmin;
109 	int set_optmax;
110 	int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
111 #ifdef CONFIG_COMPAT
112 	int (*compat_set)(struct sock *sk, int optval,
113 			void __user *user, unsigned int len);
114 #endif
115 	int get_optmin;
116 	int get_optmax;
117 	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
118 #ifdef CONFIG_COMPAT
119 	int (*compat_get)(struct sock *sk, int optval,
120 			void __user *user, int *len);
121 #endif
122 	/* Use the module struct to lock set/get code in place */
123 	struct module *owner;
124 };
125 
126 /* Function to register/unregister hook points. */
127 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
128 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
129 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
130 			  unsigned int n);
131 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
132 			     unsigned int n);
133 
134 int nf_register_hook(struct nf_hook_ops *reg);
135 void nf_unregister_hook(struct nf_hook_ops *reg);
136 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
137 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
138 int _nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
139 void _nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
140 
141 /* Functions to register get/setsockopt ranges (non-inclusive).  You
142    need to check permissions yourself! */
143 int nf_register_sockopt(struct nf_sockopt_ops *reg);
144 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
145 
146 #ifdef HAVE_JUMP_LABEL
147 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
148 #endif
149 
150 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
151 		 struct nf_hook_entry *entry);
152 
153 /**
154  *	nf_hook - call a netfilter hook
155  *
156  *	Returns 1 if the hook has allowed the packet to pass.  The function
157  *	okfn must be invoked by the caller in this case.  Any other return
158  *	value indicates the packet has been consumed by the hook.
159  */
160 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
161 			  struct sock *sk, struct sk_buff *skb,
162 			  struct net_device *indev, struct net_device *outdev,
163 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
164 {
165 	struct nf_hook_entry *hook_head;
166 	int ret = 1;
167 
168 #ifdef HAVE_JUMP_LABEL
169 	if (__builtin_constant_p(pf) &&
170 	    __builtin_constant_p(hook) &&
171 	    !static_key_false(&nf_hooks_needed[pf][hook]))
172 		return 1;
173 #endif
174 
175 	rcu_read_lock();
176 	hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
177 	if (hook_head) {
178 		struct nf_hook_state state;
179 
180 		nf_hook_state_init(&state, hook, pf, indev, outdev,
181 				   sk, net, okfn);
182 
183 		ret = nf_hook_slow(skb, &state, hook_head);
184 	}
185 	rcu_read_unlock();
186 
187 	return ret;
188 }
189 
190 /* Activate hook; either okfn or kfree_skb called, unless a hook
191    returns NF_STOLEN (in which case, it's up to the hook to deal with
192    the consequences).
193 
194    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
195    accepted.
196 */
197 
198 /* RR:
199    > I don't want nf_hook to return anything because people might forget
200    > about async and trust the return value to mean "packet was ok".
201 
202    AK:
203    Just document it clearly, then you can expect some sense from kernel
204    coders :)
205 */
206 
207 static inline int
208 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
209 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
210 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
211 	     bool cond)
212 {
213 	int ret;
214 
215 	if (!cond ||
216 	    ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
217 		ret = okfn(net, sk, skb);
218 	return ret;
219 }
220 
221 static inline int
222 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
223 	struct net_device *in, struct net_device *out,
224 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
225 {
226 	int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
227 	if (ret == 1)
228 		ret = okfn(net, sk, skb);
229 	return ret;
230 }
231 
232 /* Call setsockopt() */
233 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
234 		  unsigned int len);
235 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
236 		  int *len);
237 #ifdef CONFIG_COMPAT
238 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
239 		char __user *opt, unsigned int len);
240 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
241 		char __user *opt, int *len);
242 #endif
243 
244 /* Call this before modifying an existing packet: ensures it is
245    modifiable and linear to the point you care about (writable_len).
246    Returns true or false. */
247 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
248 
249 struct flowi;
250 struct nf_queue_entry;
251 
252 struct nf_afinfo {
253 	unsigned short	family;
254 	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
255 				    unsigned int dataoff, u_int8_t protocol);
256 	__sum16		(*checksum_partial)(struct sk_buff *skb,
257 					    unsigned int hook,
258 					    unsigned int dataoff,
259 					    unsigned int len,
260 					    u_int8_t protocol);
261 	int		(*route)(struct net *net, struct dst_entry **dst,
262 				 struct flowi *fl, bool strict);
263 	void		(*saveroute)(const struct sk_buff *skb,
264 				     struct nf_queue_entry *entry);
265 	int		(*reroute)(struct net *net, struct sk_buff *skb,
266 				   const struct nf_queue_entry *entry);
267 	int		route_key_size;
268 };
269 
270 extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
271 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
272 {
273 	return rcu_dereference(nf_afinfo[family]);
274 }
275 
276 static inline __sum16
277 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
278 	    u_int8_t protocol, unsigned short family)
279 {
280 	const struct nf_afinfo *afinfo;
281 	__sum16 csum = 0;
282 
283 	rcu_read_lock();
284 	afinfo = nf_get_afinfo(family);
285 	if (afinfo)
286 		csum = afinfo->checksum(skb, hook, dataoff, protocol);
287 	rcu_read_unlock();
288 	return csum;
289 }
290 
291 static inline __sum16
292 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
293 		    unsigned int dataoff, unsigned int len,
294 		    u_int8_t protocol, unsigned short family)
295 {
296 	const struct nf_afinfo *afinfo;
297 	__sum16 csum = 0;
298 
299 	rcu_read_lock();
300 	afinfo = nf_get_afinfo(family);
301 	if (afinfo)
302 		csum = afinfo->checksum_partial(skb, hook, dataoff, len,
303 						protocol);
304 	rcu_read_unlock();
305 	return csum;
306 }
307 
308 int nf_register_afinfo(const struct nf_afinfo *afinfo);
309 void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
310 
311 #include <net/flow.h>
312 extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
313 
314 static inline void
315 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
316 {
317 #ifdef CONFIG_NF_NAT_NEEDED
318 	void (*decodefn)(struct sk_buff *, struct flowi *);
319 
320 	rcu_read_lock();
321 	decodefn = rcu_dereference(nf_nat_decode_session_hook);
322 	if (decodefn)
323 		decodefn(skb, fl);
324 	rcu_read_unlock();
325 #endif
326 }
327 
328 #else /* !CONFIG_NETFILTER */
329 static inline int
330 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
331 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
332 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
333 	     bool cond)
334 {
335 	return okfn(net, sk, skb);
336 }
337 
338 static inline int
339 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
340 	struct sk_buff *skb, struct net_device *in, struct net_device *out,
341 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
342 {
343 	return okfn(net, sk, skb);
344 }
345 
346 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
347 			  struct sock *sk, struct sk_buff *skb,
348 			  struct net_device *indev, struct net_device *outdev,
349 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
350 {
351 	return 1;
352 }
353 struct flowi;
354 static inline void
355 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
356 {
357 }
358 #endif /*CONFIG_NETFILTER*/
359 
360 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
361 #include <linux/netfilter/nf_conntrack_zones_common.h>
362 
363 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
364 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
365 extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
366 #else
367 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
368 #endif
369 
370 struct nf_conn;
371 enum ip_conntrack_info;
372 struct nlattr;
373 
374 struct nfnl_ct_hook {
375 	struct nf_conn *(*get_ct)(const struct sk_buff *skb,
376 				  enum ip_conntrack_info *ctinfo);
377 	size_t (*build_size)(const struct nf_conn *ct);
378 	int (*build)(struct sk_buff *skb, struct nf_conn *ct,
379 		     enum ip_conntrack_info ctinfo,
380 		     u_int16_t ct_attr, u_int16_t ct_info_attr);
381 	int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
382 	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
383 			     u32 portid, u32 report);
384 	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
385 			   enum ip_conntrack_info ctinfo, s32 off);
386 };
387 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
388 
389 /**
390  * nf_skb_duplicated - TEE target has sent a packet
391  *
392  * When a xtables target sends a packet, the OUTPUT and POSTROUTING
393  * hooks are traversed again, i.e. nft and xtables are invoked recursively.
394  *
395  * This is used by xtables TEE target to prevent the duplicated skb from
396  * being duplicated again.
397  */
398 DECLARE_PER_CPU(bool, nf_skb_duplicated);
399 
400 #endif /*__LINUX_NETFILTER_H*/
401