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/module.h>
15 #include <linux/netfilter_defs.h>
16 #include <linux/netdevice.h>
17 #include <linux/sockptr.h>
18 #include <net/net_namespace.h>
20 static inline int NF_DROP_GETERR(int verdict)
22 return -(verdict >> NF_VERDICT_QBITS);
25 static __always_inline int
26 NF_DROP_REASON(struct sk_buff *skb, enum skb_drop_reason reason, u32 err)
28 BUILD_BUG_ON(err > 0xffff);
30 kfree_skb_reason(skb, reason);
32 return ((err << 16) | NF_STOLEN);
35 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
36 const union nf_inet_addr *a2)
38 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
39 const unsigned long *ul1 = (const unsigned long *)a1;
40 const unsigned long *ul2 = (const unsigned long *)a2;
42 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
44 return a1->all[0] == a2->all[0] &&
45 a1->all[1] == a2->all[1] &&
46 a1->all[2] == a2->all[2] &&
47 a1->all[3] == a2->all[3];
51 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
52 union nf_inet_addr *result,
53 const union nf_inet_addr *mask)
55 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
56 const unsigned long *ua = (const unsigned long *)a1;
57 unsigned long *ur = (unsigned long *)result;
58 const unsigned long *um = (const unsigned long *)mask;
60 ur[0] = ua[0] & um[0];
61 ur[1] = ua[1] & um[1];
63 result->all[0] = a1->all[0] & mask->all[0];
64 result->all[1] = a1->all[1] & mask->all[1];
65 result->all[2] = a1->all[2] & mask->all[2];
66 result->all[3] = a1->all[3] & mask->all[3];
70 int netfilter_init(void);
78 struct nf_hook_state {
81 struct net_device *in;
82 struct net_device *out;
85 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
88 typedef unsigned int nf_hookfn(void *priv,
90 const struct nf_hook_state *state);
91 enum nf_hook_ops_type {
98 /* User fills in from here down. */
100 struct net_device *dev;
103 enum nf_hook_ops_type hook_ops_type:8;
104 unsigned int hooknum;
105 /* Hooks are ordered in ascending priority. */
109 struct nf_hook_entry {
114 struct nf_hook_entries_rcu_head {
115 struct rcu_head head;
119 struct nf_hook_entries {
120 u16 num_hook_entries;
122 struct nf_hook_entry hooks[];
124 /* trailer: pointers to original orig_ops of each hook,
125 * followed by rcu_head and scratch space used for freeing
126 * the structure via call_rcu.
128 * This is not part of struct nf_hook_entry since its only
129 * needed in slow path (hook register/unregister):
130 * const struct nf_hook_ops *orig_ops[]
132 * For the same reason, we store this at end -- its
133 * only needed when a hook is deleted, not during
134 * packet path processing:
135 * struct nf_hook_entries_rcu_head head
139 #ifdef CONFIG_NETFILTER
140 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
142 unsigned int n = e->num_hook_entries;
143 const void *hook_end;
145 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
147 return (struct nf_hook_ops **)hook_end;
151 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
152 struct nf_hook_state *state)
154 return entry->hook(entry->priv, skb, state);
157 static inline void nf_hook_state_init(struct nf_hook_state *p,
160 struct net_device *indev,
161 struct net_device *outdev,
164 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
177 struct nf_sockopt_ops {
178 struct list_head list;
182 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
185 int (*set)(struct sock *sk, int optval, sockptr_t arg,
189 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
190 /* Use the module struct to lock set/get code in place */
191 struct module *owner;
194 /* Function to register/unregister hook points. */
195 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
196 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
197 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
199 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
202 /* Functions to register get/setsockopt ranges (non-inclusive). You
203 need to check permissions yourself! */
204 int nf_register_sockopt(struct nf_sockopt_ops *reg);
205 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
207 #ifdef CONFIG_JUMP_LABEL
208 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
211 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
212 const struct nf_hook_entries *e, unsigned int i);
214 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
215 const struct nf_hook_entries *e);
217 * nf_hook - call a netfilter hook
219 * Returns 1 if the hook has allowed the packet to pass. The function
220 * okfn must be invoked by the caller in this case. Any other return
221 * value indicates the packet has been consumed by the hook.
223 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
224 struct sock *sk, struct sk_buff *skb,
225 struct net_device *indev, struct net_device *outdev,
226 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
228 struct nf_hook_entries *hook_head = NULL;
231 #ifdef CONFIG_JUMP_LABEL
232 if (__builtin_constant_p(pf) &&
233 __builtin_constant_p(hook) &&
234 !static_key_false(&nf_hooks_needed[pf][hook]))
241 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
244 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
247 #ifdef CONFIG_NETFILTER_FAMILY_ARP
248 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
250 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
254 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
255 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
264 struct nf_hook_state state;
266 nf_hook_state_init(&state, hook, pf, indev, outdev,
269 ret = nf_hook_slow(skb, &state, hook_head, 0);
276 /* Activate hook; either okfn or kfree_skb called, unless a hook
277 returns NF_STOLEN (in which case, it's up to the hook to deal with
280 Returns -ERRNO if packet dropped. Zero means queued, stolen or
285 > I don't want nf_hook to return anything because people might forget
286 > about async and trust the return value to mean "packet was ok".
289 Just document it clearly, then you can expect some sense from kernel
294 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
295 struct sk_buff *skb, struct net_device *in, struct net_device *out,
296 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
302 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
303 ret = okfn(net, sk, skb);
308 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
309 struct net_device *in, struct net_device *out,
310 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
312 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
314 ret = okfn(net, sk, skb);
319 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
320 struct list_head *head, struct net_device *in, struct net_device *out,
321 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
323 struct nf_hook_entries *hook_head = NULL;
325 #ifdef CONFIG_JUMP_LABEL
326 if (__builtin_constant_p(pf) &&
327 __builtin_constant_p(hook) &&
328 !static_key_false(&nf_hooks_needed[pf][hook]))
335 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
338 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
346 struct nf_hook_state state;
348 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
350 nf_hook_slow_list(head, &state, hook_head);
355 /* Call setsockopt() */
356 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt,
358 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
362 struct nf_queue_entry;
364 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
365 unsigned int dataoff, u_int8_t protocol,
366 unsigned short family);
368 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
369 unsigned int dataoff, unsigned int len,
370 u_int8_t protocol, unsigned short family);
371 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
372 bool strict, unsigned short family);
374 #include <net/flow.h>
377 enum nf_nat_manip_type;
379 enum ip_conntrack_dir;
382 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
383 const struct nlattr *attr);
384 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
385 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
386 enum nf_nat_manip_type mtype,
387 enum ip_conntrack_dir dir);
388 void (*remove_nat_bysrc)(struct nf_conn *ct);
391 extern const struct nf_nat_hook __rcu *nf_nat_hook;
394 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
396 #if IS_ENABLED(CONFIG_NF_NAT)
397 const struct nf_nat_hook *nat_hook;
400 nat_hook = rcu_dereference(nf_nat_hook);
401 if (nat_hook && nat_hook->decode_session)
402 nat_hook->decode_session(skb, fl);
407 #else /* !CONFIG_NETFILTER */
409 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
410 struct sk_buff *skb, struct net_device *in, struct net_device *out,
411 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
414 return okfn(net, sk, skb);
418 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
419 struct sk_buff *skb, struct net_device *in, struct net_device *out,
420 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
422 return okfn(net, sk, skb);
426 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
427 struct list_head *head, struct net_device *in, struct net_device *out,
428 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
433 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
434 struct sock *sk, struct sk_buff *skb,
435 struct net_device *indev, struct net_device *outdev,
436 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
442 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
445 #endif /*CONFIG_NETFILTER*/
447 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
448 #include <linux/netfilter/nf_conntrack_zones_common.h>
450 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
451 void nf_ct_set_closing(struct nf_conntrack *nfct);
452 struct nf_conntrack_tuple;
453 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
454 const struct sk_buff *skb);
456 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
457 static inline void nf_ct_set_closing(struct nf_conntrack *nfct) {}
458 struct nf_conntrack_tuple;
459 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
460 const struct sk_buff *skb)
467 enum ip_conntrack_info;
470 int (*update)(struct net *net, struct sk_buff *skb);
471 void (*destroy)(struct nf_conntrack *);
472 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
473 const struct sk_buff *);
474 void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb);
475 void (*set_closing)(struct nf_conntrack *nfct);
476 int (*confirm)(struct sk_buff *skb);
478 extern const struct nf_ct_hook __rcu *nf_ct_hook;
482 struct nfnl_ct_hook {
483 size_t (*build_size)(const struct nf_conn *ct);
484 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
485 enum ip_conntrack_info ctinfo,
486 u_int16_t ct_attr, u_int16_t ct_info_attr);
487 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
488 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
489 u32 portid, u32 report);
490 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
491 enum ip_conntrack_info ctinfo, s32 off);
493 extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;
495 struct nf_defrag_hook {
496 struct module *owner;
497 int (*enable)(struct net *net);
498 void (*disable)(struct net *net);
501 extern const struct nf_defrag_hook __rcu *nf_defrag_v4_hook;
502 extern const struct nf_defrag_hook __rcu *nf_defrag_v6_hook;
505 * nf_skb_duplicated - TEE target has sent a packet
507 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
508 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
510 * This is used by xtables TEE target to prevent the duplicated skb from
511 * being duplicated again.
513 DECLARE_PER_CPU(bool, nf_skb_duplicated);
516 * Contains bitmask of ctnetlink event subscribers, if any.
517 * Can't be pernet due to NETLINK_LISTEN_ALL_NSID setsockopt flag.
519 extern u8 nf_ctnetlink_has_listener;
520 #endif /*__LINUX_NETFILTER_H*/