1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETFILTER_H
3 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/skbuff.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 <linux/sockptr.h>
17 #include <net/net_namespace.h>
19 static inline int NF_DROP_GETERR(int verdict)
21 return -(verdict >> NF_VERDICT_QBITS);
24 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
25 const union nf_inet_addr *a2)
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;
31 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
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];
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)
44 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
45 const unsigned long *ua = (const unsigned long *)a1;
46 unsigned long *ur = (unsigned long *)result;
47 const unsigned long *um = (const unsigned long *)mask;
49 ur[0] = ua[0] & um[0];
50 ur[1] = ua[1] & um[1];
52 result->all[0] = a1->all[0] & mask->all[0];
53 result->all[1] = a1->all[1] & mask->all[1];
54 result->all[2] = a1->all[2] & mask->all[2];
55 result->all[3] = a1->all[3] & mask->all[3];
59 int netfilter_init(void);
67 struct nf_hook_state {
70 struct net_device *in;
71 struct net_device *out;
74 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
77 typedef unsigned int nf_hookfn(void *priv,
79 const struct nf_hook_state *state);
80 enum nf_hook_ops_type {
86 /* User fills in from here down. */
88 struct net_device *dev;
91 enum nf_hook_ops_type hook_ops_type:8;
93 /* Hooks are ordered in ascending priority. */
97 struct nf_hook_entry {
102 struct nf_hook_entries_rcu_head {
103 struct rcu_head head;
107 struct nf_hook_entries {
108 u16 num_hook_entries;
110 struct nf_hook_entry hooks[];
112 /* trailer: pointers to original orig_ops of each hook,
113 * followed by rcu_head and scratch space used for freeing
114 * the structure via call_rcu.
116 * This is not part of struct nf_hook_entry since its only
117 * needed in slow path (hook register/unregister):
118 * const struct nf_hook_ops *orig_ops[]
120 * For the same reason, we store this at end -- its
121 * only needed when a hook is deleted, not during
122 * packet path processing:
123 * struct nf_hook_entries_rcu_head head
127 #ifdef CONFIG_NETFILTER
128 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
130 unsigned int n = e->num_hook_entries;
131 const void *hook_end;
133 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
135 return (struct nf_hook_ops **)hook_end;
139 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
140 struct nf_hook_state *state)
142 return entry->hook(entry->priv, skb, state);
145 static inline void nf_hook_state_init(struct nf_hook_state *p,
148 struct net_device *indev,
149 struct net_device *outdev,
152 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
165 struct nf_sockopt_ops {
166 struct list_head list;
170 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
173 int (*set)(struct sock *sk, int optval, sockptr_t arg,
177 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
178 /* Use the module struct to lock set/get code in place */
179 struct module *owner;
182 /* Function to register/unregister hook points. */
183 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
184 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
185 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
187 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
190 /* Functions to register get/setsockopt ranges (non-inclusive). You
191 need to check permissions yourself! */
192 int nf_register_sockopt(struct nf_sockopt_ops *reg);
193 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
195 #ifdef CONFIG_JUMP_LABEL
196 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
199 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
200 const struct nf_hook_entries *e, unsigned int i);
202 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
203 const struct nf_hook_entries *e);
205 * nf_hook - call a netfilter hook
207 * Returns 1 if the hook has allowed the packet to pass. The function
208 * okfn must be invoked by the caller in this case. Any other return
209 * value indicates the packet has been consumed by the hook.
211 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
212 struct sock *sk, struct sk_buff *skb,
213 struct net_device *indev, struct net_device *outdev,
214 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
216 struct nf_hook_entries *hook_head = NULL;
219 #ifdef CONFIG_JUMP_LABEL
220 if (__builtin_constant_p(pf) &&
221 __builtin_constant_p(hook) &&
222 !static_key_false(&nf_hooks_needed[pf][hook]))
229 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
232 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
235 #ifdef CONFIG_NETFILTER_FAMILY_ARP
236 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
238 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
242 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
243 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
252 struct nf_hook_state state;
254 nf_hook_state_init(&state, hook, pf, indev, outdev,
257 ret = nf_hook_slow(skb, &state, hook_head, 0);
264 /* Activate hook; either okfn or kfree_skb called, unless a hook
265 returns NF_STOLEN (in which case, it's up to the hook to deal with
268 Returns -ERRNO if packet dropped. Zero means queued, stolen or
273 > I don't want nf_hook to return anything because people might forget
274 > about async and trust the return value to mean "packet was ok".
277 Just document it clearly, then you can expect some sense from kernel
282 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
283 struct sk_buff *skb, struct net_device *in, struct net_device *out,
284 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
290 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
291 ret = okfn(net, sk, skb);
296 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
297 struct net_device *in, struct net_device *out,
298 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
300 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
302 ret = okfn(net, sk, skb);
307 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
308 struct list_head *head, struct net_device *in, struct net_device *out,
309 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
311 struct nf_hook_entries *hook_head = NULL;
313 #ifdef CONFIG_JUMP_LABEL
314 if (__builtin_constant_p(pf) &&
315 __builtin_constant_p(hook) &&
316 !static_key_false(&nf_hooks_needed[pf][hook]))
323 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
326 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
334 struct nf_hook_state state;
336 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
338 nf_hook_slow_list(head, &state, hook_head);
343 /* Call setsockopt() */
344 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt,
346 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
350 struct nf_queue_entry;
352 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
353 unsigned int dataoff, u_int8_t protocol,
354 unsigned short family);
356 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
357 unsigned int dataoff, unsigned int len,
358 u_int8_t protocol, unsigned short family);
359 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
360 bool strict, unsigned short family);
361 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
363 #include <net/flow.h>
366 enum nf_nat_manip_type;
368 enum ip_conntrack_dir;
371 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
372 const struct nlattr *attr);
373 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
374 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
375 enum nf_nat_manip_type mtype,
376 enum ip_conntrack_dir dir);
377 void (*remove_nat_bysrc)(struct nf_conn *ct);
380 extern const struct nf_nat_hook __rcu *nf_nat_hook;
383 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
385 #if IS_ENABLED(CONFIG_NF_NAT)
386 const struct nf_nat_hook *nat_hook;
389 nat_hook = rcu_dereference(nf_nat_hook);
390 if (nat_hook && nat_hook->decode_session)
391 nat_hook->decode_session(skb, fl);
396 #else /* !CONFIG_NETFILTER */
398 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
399 struct sk_buff *skb, struct net_device *in, struct net_device *out,
400 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
403 return okfn(net, sk, skb);
407 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
408 struct sk_buff *skb, struct net_device *in, struct net_device *out,
409 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
411 return okfn(net, sk, skb);
415 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
416 struct list_head *head, struct net_device *in, struct net_device *out,
417 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
422 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
423 struct sock *sk, struct sk_buff *skb,
424 struct net_device *indev, struct net_device *outdev,
425 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
431 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
434 #endif /*CONFIG_NETFILTER*/
436 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
437 #include <linux/netfilter/nf_conntrack_zones_common.h>
439 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
440 void nf_ct_set_closing(struct nf_conntrack *nfct);
441 struct nf_conntrack_tuple;
442 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
443 const struct sk_buff *skb);
445 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
446 static inline void nf_ct_set_closing(struct nf_conntrack *nfct) {}
447 struct nf_conntrack_tuple;
448 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
449 const struct sk_buff *skb)
456 enum ip_conntrack_info;
459 int (*update)(struct net *net, struct sk_buff *skb);
460 void (*destroy)(struct nf_conntrack *);
461 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
462 const struct sk_buff *);
463 void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb);
464 void (*set_closing)(struct nf_conntrack *nfct);
466 extern const struct nf_ct_hook __rcu *nf_ct_hook;
470 struct nfnl_ct_hook {
471 size_t (*build_size)(const struct nf_conn *ct);
472 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
473 enum ip_conntrack_info ctinfo,
474 u_int16_t ct_attr, u_int16_t ct_info_attr);
475 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
476 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
477 u32 portid, u32 report);
478 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
479 enum ip_conntrack_info ctinfo, s32 off);
481 extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;
484 * nf_skb_duplicated - TEE target has sent a packet
486 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
487 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
489 * This is used by xtables TEE target to prevent the duplicated skb from
490 * being duplicated again.
492 DECLARE_PER_CPU(bool, nf_skb_duplicated);
494 #endif /*__LINUX_NETFILTER_H*/