xref: /openbmc/linux/include/linux/netfilter.h (revision 27f54b58)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETFILTER_H
3 #define __LINUX_NETFILTER_H
4 
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
7 #include <linux/net.h>
8 #include <linux/if.h>
9 #include <linux/in.h>
10 #include <linux/in6.h>
11 #include <linux/wait.h>
12 #include <linux/list.h>
13 #include <linux/static_key.h>
14 #include <linux/netfilter_defs.h>
15 #include <linux/netdevice.h>
16 #include <net/net_namespace.h>
17 
18 #ifdef CONFIG_NETFILTER
19 static inline int NF_DROP_GETERR(int verdict)
20 {
21 	return -(verdict >> NF_VERDICT_QBITS);
22 }
23 
24 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
25 				   const union nf_inet_addr *a2)
26 {
27 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
28 	const unsigned long *ul1 = (const unsigned long *)a1;
29 	const unsigned long *ul2 = (const unsigned long *)a2;
30 
31 	return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
32 #else
33 	return a1->all[0] == a2->all[0] &&
34 	       a1->all[1] == a2->all[1] &&
35 	       a1->all[2] == a2->all[2] &&
36 	       a1->all[3] == a2->all[3];
37 #endif
38 }
39 
40 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
41 				     union nf_inet_addr *result,
42 				     const union nf_inet_addr *mask)
43 {
44 	result->all[0] = a1->all[0] & mask->all[0];
45 	result->all[1] = a1->all[1] & mask->all[1];
46 	result->all[2] = a1->all[2] & mask->all[2];
47 	result->all[3] = a1->all[3] & mask->all[3];
48 }
49 
50 int netfilter_init(void);
51 
52 struct sk_buff;
53 
54 struct nf_hook_ops;
55 
56 struct sock;
57 
58 struct nf_hook_state {
59 	unsigned int hook;
60 	u_int8_t pf;
61 	struct net_device *in;
62 	struct net_device *out;
63 	struct sock *sk;
64 	struct net *net;
65 	int (*okfn)(struct net *, struct sock *, struct sk_buff *);
66 };
67 
68 typedef unsigned int nf_hookfn(void *priv,
69 			       struct sk_buff *skb,
70 			       const struct nf_hook_state *state);
71 struct nf_hook_ops {
72 	/* User fills in from here down. */
73 	nf_hookfn		*hook;
74 	struct net_device	*dev;
75 	void			*priv;
76 	u_int8_t		pf;
77 	unsigned int		hooknum;
78 	/* Hooks are ordered in ascending priority. */
79 	int			priority;
80 };
81 
82 struct nf_hook_entry {
83 	nf_hookfn			*hook;
84 	void				*priv;
85 };
86 
87 struct nf_hook_entries_rcu_head {
88 	struct rcu_head head;
89 	void	*allocation;
90 };
91 
92 struct nf_hook_entries {
93 	u16				num_hook_entries;
94 	/* padding */
95 	struct nf_hook_entry		hooks[];
96 
97 	/* trailer: pointers to original orig_ops of each hook,
98 	 * followed by rcu_head and scratch space used for freeing
99 	 * the structure via call_rcu.
100 	 *
101 	 *   This is not part of struct nf_hook_entry since its only
102 	 *   needed in slow path (hook register/unregister):
103 	 * const struct nf_hook_ops     *orig_ops[]
104 	 *
105 	 *   For the same reason, we store this at end -- its
106 	 *   only needed when a hook is deleted, not during
107 	 *   packet path processing:
108 	 * struct nf_hook_entries_rcu_head     head
109 	 */
110 };
111 
112 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
113 {
114 	unsigned int n = e->num_hook_entries;
115 	const void *hook_end;
116 
117 	hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
118 
119 	return (struct nf_hook_ops **)hook_end;
120 }
121 
122 static inline int
123 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
124 		     struct nf_hook_state *state)
125 {
126 	return entry->hook(entry->priv, skb, state);
127 }
128 
129 static inline void nf_hook_state_init(struct nf_hook_state *p,
130 				      unsigned int hook,
131 				      u_int8_t pf,
132 				      struct net_device *indev,
133 				      struct net_device *outdev,
134 				      struct sock *sk,
135 				      struct net *net,
136 				      int (*okfn)(struct net *, struct sock *, struct sk_buff *))
137 {
138 	p->hook = hook;
139 	p->pf = pf;
140 	p->in = indev;
141 	p->out = outdev;
142 	p->sk = sk;
143 	p->net = net;
144 	p->okfn = okfn;
145 }
146 
147 
148 
149 struct nf_sockopt_ops {
150 	struct list_head list;
151 
152 	u_int8_t pf;
153 
154 	/* Non-inclusive ranges: use 0/0/NULL to never get called. */
155 	int set_optmin;
156 	int set_optmax;
157 	int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
158 #ifdef CONFIG_COMPAT
159 	int (*compat_set)(struct sock *sk, int optval,
160 			void __user *user, unsigned int len);
161 #endif
162 	int get_optmin;
163 	int get_optmax;
164 	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
165 #ifdef CONFIG_COMPAT
166 	int (*compat_get)(struct sock *sk, int optval,
167 			void __user *user, int *len);
168 #endif
169 	/* Use the module struct to lock set/get code in place */
170 	struct module *owner;
171 };
172 
173 /* Function to register/unregister hook points. */
174 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
175 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
176 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
177 			  unsigned int n);
178 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
179 			     unsigned int n);
180 
181 /* Functions to register get/setsockopt ranges (non-inclusive).  You
182    need to check permissions yourself! */
183 int nf_register_sockopt(struct nf_sockopt_ops *reg);
184 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
185 
186 #ifdef CONFIG_JUMP_LABEL
187 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
188 #endif
189 
190 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
191 		 const struct nf_hook_entries *e, unsigned int i);
192 
193 /**
194  *	nf_hook - call a netfilter hook
195  *
196  *	Returns 1 if the hook has allowed the packet to pass.  The function
197  *	okfn must be invoked by the caller in this case.  Any other return
198  *	value indicates the packet has been consumed by the hook.
199  */
200 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
201 			  struct sock *sk, struct sk_buff *skb,
202 			  struct net_device *indev, struct net_device *outdev,
203 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
204 {
205 	struct nf_hook_entries *hook_head = NULL;
206 	int ret = 1;
207 
208 #ifdef CONFIG_JUMP_LABEL
209 	if (__builtin_constant_p(pf) &&
210 	    __builtin_constant_p(hook) &&
211 	    !static_key_false(&nf_hooks_needed[pf][hook]))
212 		return 1;
213 #endif
214 
215 	rcu_read_lock();
216 	switch (pf) {
217 	case NFPROTO_IPV4:
218 		hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
219 		break;
220 	case NFPROTO_IPV6:
221 		hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
222 		break;
223 	case NFPROTO_ARP:
224 #ifdef CONFIG_NETFILTER_FAMILY_ARP
225 		if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
226 			break;
227 		hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
228 #endif
229 		break;
230 	case NFPROTO_BRIDGE:
231 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
232 		hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
233 #endif
234 		break;
235 #if IS_ENABLED(CONFIG_DECNET)
236 	case NFPROTO_DECNET:
237 		hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
238 		break;
239 #endif
240 	default:
241 		WARN_ON_ONCE(1);
242 		break;
243 	}
244 
245 	if (hook_head) {
246 		struct nf_hook_state state;
247 
248 		nf_hook_state_init(&state, hook, pf, indev, outdev,
249 				   sk, net, okfn);
250 
251 		ret = nf_hook_slow(skb, &state, hook_head, 0);
252 	}
253 	rcu_read_unlock();
254 
255 	return ret;
256 }
257 
258 /* Activate hook; either okfn or kfree_skb called, unless a hook
259    returns NF_STOLEN (in which case, it's up to the hook to deal with
260    the consequences).
261 
262    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
263    accepted.
264 */
265 
266 /* RR:
267    > I don't want nf_hook to return anything because people might forget
268    > about async and trust the return value to mean "packet was ok".
269 
270    AK:
271    Just document it clearly, then you can expect some sense from kernel
272    coders :)
273 */
274 
275 static inline int
276 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
277 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
278 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
279 	     bool cond)
280 {
281 	int ret;
282 
283 	if (!cond ||
284 	    ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
285 		ret = okfn(net, sk, skb);
286 	return ret;
287 }
288 
289 static inline int
290 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
291 	struct net_device *in, struct net_device *out,
292 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
293 {
294 	int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
295 	if (ret == 1)
296 		ret = okfn(net, sk, skb);
297 	return ret;
298 }
299 
300 static inline void
301 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
302 	     struct list_head *head, struct net_device *in, struct net_device *out,
303 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *))
304 {
305 	struct sk_buff *skb, *next;
306 	struct list_head sublist;
307 
308 	INIT_LIST_HEAD(&sublist);
309 	list_for_each_entry_safe(skb, next, head, list) {
310 		list_del(&skb->list);
311 		if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
312 			list_add_tail(&skb->list, &sublist);
313 	}
314 	/* Put passed packets back on main list */
315 	list_splice(&sublist, head);
316 }
317 
318 /* Call setsockopt() */
319 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
320 		  unsigned int len);
321 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
322 		  int *len);
323 #ifdef CONFIG_COMPAT
324 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
325 		char __user *opt, unsigned int len);
326 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
327 		char __user *opt, int *len);
328 #endif
329 
330 /* Call this before modifying an existing packet: ensures it is
331    modifiable and linear to the point you care about (writable_len).
332    Returns true or false. */
333 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
334 
335 struct flowi;
336 struct nf_queue_entry;
337 
338 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
339 		    unsigned int dataoff, u_int8_t protocol,
340 		    unsigned short family);
341 
342 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
343 			    unsigned int dataoff, unsigned int len,
344 			    u_int8_t protocol, unsigned short family);
345 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
346 	     bool strict, unsigned short family);
347 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
348 
349 #include <net/flow.h>
350 
351 struct nf_conn;
352 enum nf_nat_manip_type;
353 struct nlattr;
354 enum ip_conntrack_dir;
355 
356 struct nf_nat_hook {
357 	int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
358 			       const struct nlattr *attr);
359 	void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
360 	unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
361 				  enum nf_nat_manip_type mtype,
362 				  enum ip_conntrack_dir dir);
363 };
364 
365 extern struct nf_nat_hook __rcu *nf_nat_hook;
366 
367 static inline void
368 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
369 {
370 #if IS_ENABLED(CONFIG_NF_NAT)
371 	struct nf_nat_hook *nat_hook;
372 
373 	rcu_read_lock();
374 	nat_hook = rcu_dereference(nf_nat_hook);
375 	if (nat_hook && nat_hook->decode_session)
376 		nat_hook->decode_session(skb, fl);
377 	rcu_read_unlock();
378 #endif
379 }
380 
381 #else /* !CONFIG_NETFILTER */
382 static inline int
383 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
384 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
385 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
386 	     bool cond)
387 {
388 	return okfn(net, sk, skb);
389 }
390 
391 static inline int
392 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
393 	struct sk_buff *skb, struct net_device *in, struct net_device *out,
394 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
395 {
396 	return okfn(net, sk, skb);
397 }
398 
399 static inline void
400 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
401 	     struct list_head *head, struct net_device *in, struct net_device *out,
402 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *))
403 {
404 	/* nothing to do */
405 }
406 
407 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
408 			  struct sock *sk, struct sk_buff *skb,
409 			  struct net_device *indev, struct net_device *outdev,
410 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
411 {
412 	return 1;
413 }
414 struct flowi;
415 static inline void
416 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
417 {
418 }
419 #endif /*CONFIG_NETFILTER*/
420 
421 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
422 #include <linux/netfilter/nf_conntrack_zones_common.h>
423 
424 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
425 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
426 struct nf_conntrack_tuple;
427 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
428 			 const struct sk_buff *skb);
429 #else
430 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
431 struct nf_conntrack_tuple;
432 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
433 				       const struct sk_buff *skb)
434 {
435 	return false;
436 }
437 #endif
438 
439 struct nf_conn;
440 enum ip_conntrack_info;
441 
442 struct nf_ct_hook {
443 	int (*update)(struct net *net, struct sk_buff *skb);
444 	void (*destroy)(struct nf_conntrack *);
445 	bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
446 			      const struct sk_buff *);
447 };
448 extern struct nf_ct_hook __rcu *nf_ct_hook;
449 
450 struct nlattr;
451 
452 struct nfnl_ct_hook {
453 	struct nf_conn *(*get_ct)(const struct sk_buff *skb,
454 				  enum ip_conntrack_info *ctinfo);
455 	size_t (*build_size)(const struct nf_conn *ct);
456 	int (*build)(struct sk_buff *skb, struct nf_conn *ct,
457 		     enum ip_conntrack_info ctinfo,
458 		     u_int16_t ct_attr, u_int16_t ct_info_attr);
459 	int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
460 	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
461 			     u32 portid, u32 report);
462 	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
463 			   enum ip_conntrack_info ctinfo, s32 off);
464 };
465 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
466 
467 /**
468  * nf_skb_duplicated - TEE target has sent a packet
469  *
470  * When a xtables target sends a packet, the OUTPUT and POSTROUTING
471  * hooks are traversed again, i.e. nft and xtables are invoked recursively.
472  *
473  * This is used by xtables TEE target to prevent the duplicated skb from
474  * being duplicated again.
475  */
476 DECLARE_PER_CPU(bool, nf_skb_duplicated);
477 
478 #endif /*__LINUX_NETFILTER_H*/
479