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 inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
26 const union nf_inet_addr *a2)
28 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
29 const unsigned long *ul1 = (const unsigned long *)a1;
30 const unsigned long *ul2 = (const unsigned long *)a2;
32 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
34 return a1->all[0] == a2->all[0] &&
35 a1->all[1] == a2->all[1] &&
36 a1->all[2] == a2->all[2] &&
37 a1->all[3] == a2->all[3];
41 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
42 union nf_inet_addr *result,
43 const union nf_inet_addr *mask)
45 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
46 const unsigned long *ua = (const unsigned long *)a1;
47 unsigned long *ur = (unsigned long *)result;
48 const unsigned long *um = (const unsigned long *)mask;
50 ur[0] = ua[0] & um[0];
51 ur[1] = ua[1] & um[1];
53 result->all[0] = a1->all[0] & mask->all[0];
54 result->all[1] = a1->all[1] & mask->all[1];
55 result->all[2] = a1->all[2] & mask->all[2];
56 result->all[3] = a1->all[3] & mask->all[3];
60 int netfilter_init(void);
68 struct nf_hook_state {
71 struct net_device *in;
72 struct net_device *out;
75 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
78 typedef unsigned int nf_hookfn(void *priv,
80 const struct nf_hook_state *state);
81 enum nf_hook_ops_type {
88 /* User fills in from here down. */
90 struct net_device *dev;
93 enum nf_hook_ops_type hook_ops_type:8;
95 /* Hooks are ordered in ascending priority. */
99 struct nf_hook_entry {
104 struct nf_hook_entries_rcu_head {
105 struct rcu_head head;
109 struct nf_hook_entries {
110 u16 num_hook_entries;
112 struct nf_hook_entry hooks[];
114 /* trailer: pointers to original orig_ops of each hook,
115 * followed by rcu_head and scratch space used for freeing
116 * the structure via call_rcu.
118 * This is not part of struct nf_hook_entry since its only
119 * needed in slow path (hook register/unregister):
120 * const struct nf_hook_ops *orig_ops[]
122 * For the same reason, we store this at end -- its
123 * only needed when a hook is deleted, not during
124 * packet path processing:
125 * struct nf_hook_entries_rcu_head head
129 #ifdef CONFIG_NETFILTER
130 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
132 unsigned int n = e->num_hook_entries;
133 const void *hook_end;
135 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
137 return (struct nf_hook_ops **)hook_end;
141 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
142 struct nf_hook_state *state)
144 return entry->hook(entry->priv, skb, state);
147 static inline void nf_hook_state_init(struct nf_hook_state *p,
150 struct net_device *indev,
151 struct net_device *outdev,
154 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
167 struct nf_sockopt_ops {
168 struct list_head list;
172 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
175 int (*set)(struct sock *sk, int optval, sockptr_t arg,
179 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
180 /* Use the module struct to lock set/get code in place */
181 struct module *owner;
184 /* Function to register/unregister hook points. */
185 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
186 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
187 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
189 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
192 /* Functions to register get/setsockopt ranges (non-inclusive). You
193 need to check permissions yourself! */
194 int nf_register_sockopt(struct nf_sockopt_ops *reg);
195 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
197 #ifdef CONFIG_JUMP_LABEL
198 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
201 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
202 const struct nf_hook_entries *e, unsigned int i);
204 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
205 const struct nf_hook_entries *e);
207 * nf_hook - call a netfilter hook
209 * Returns 1 if the hook has allowed the packet to pass. The function
210 * okfn must be invoked by the caller in this case. Any other return
211 * value indicates the packet has been consumed by the hook.
213 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
214 struct sock *sk, struct sk_buff *skb,
215 struct net_device *indev, struct net_device *outdev,
216 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
218 struct nf_hook_entries *hook_head = NULL;
221 #ifdef CONFIG_JUMP_LABEL
222 if (__builtin_constant_p(pf) &&
223 __builtin_constant_p(hook) &&
224 !static_key_false(&nf_hooks_needed[pf][hook]))
231 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
234 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
237 #ifdef CONFIG_NETFILTER_FAMILY_ARP
238 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
240 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
244 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
245 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
254 struct nf_hook_state state;
256 nf_hook_state_init(&state, hook, pf, indev, outdev,
259 ret = nf_hook_slow(skb, &state, hook_head, 0);
266 /* Activate hook; either okfn or kfree_skb called, unless a hook
267 returns NF_STOLEN (in which case, it's up to the hook to deal with
270 Returns -ERRNO if packet dropped. Zero means queued, stolen or
275 > I don't want nf_hook to return anything because people might forget
276 > about async and trust the return value to mean "packet was ok".
279 Just document it clearly, then you can expect some sense from kernel
284 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
285 struct sk_buff *skb, struct net_device *in, struct net_device *out,
286 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
292 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
293 ret = okfn(net, sk, skb);
298 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
299 struct net_device *in, struct net_device *out,
300 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
302 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
304 ret = okfn(net, sk, skb);
309 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
310 struct list_head *head, struct net_device *in, struct net_device *out,
311 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
313 struct nf_hook_entries *hook_head = NULL;
315 #ifdef CONFIG_JUMP_LABEL
316 if (__builtin_constant_p(pf) &&
317 __builtin_constant_p(hook) &&
318 !static_key_false(&nf_hooks_needed[pf][hook]))
325 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
328 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
336 struct nf_hook_state state;
338 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
340 nf_hook_slow_list(head, &state, hook_head);
345 /* Call setsockopt() */
346 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt,
348 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
352 struct nf_queue_entry;
354 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
355 unsigned int dataoff, u_int8_t protocol,
356 unsigned short family);
358 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
359 unsigned int dataoff, unsigned int len,
360 u_int8_t protocol, unsigned short family);
361 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
362 bool strict, unsigned short family);
363 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
365 #include <net/flow.h>
368 enum nf_nat_manip_type;
370 enum ip_conntrack_dir;
373 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
374 const struct nlattr *attr);
375 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
376 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
377 enum nf_nat_manip_type mtype,
378 enum ip_conntrack_dir dir);
379 void (*remove_nat_bysrc)(struct nf_conn *ct);
382 extern const struct nf_nat_hook __rcu *nf_nat_hook;
385 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
387 #if IS_ENABLED(CONFIG_NF_NAT)
388 const struct nf_nat_hook *nat_hook;
391 nat_hook = rcu_dereference(nf_nat_hook);
392 if (nat_hook && nat_hook->decode_session)
393 nat_hook->decode_session(skb, fl);
398 #else /* !CONFIG_NETFILTER */
400 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
401 struct sk_buff *skb, struct net_device *in, struct net_device *out,
402 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
405 return okfn(net, sk, skb);
409 NF_HOOK(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 *))
413 return okfn(net, sk, skb);
417 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
418 struct list_head *head, struct net_device *in, struct net_device *out,
419 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
424 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
425 struct sock *sk, struct sk_buff *skb,
426 struct net_device *indev, struct net_device *outdev,
427 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
433 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
436 #endif /*CONFIG_NETFILTER*/
438 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
439 #include <linux/netfilter/nf_conntrack_zones_common.h>
441 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
442 void nf_ct_set_closing(struct nf_conntrack *nfct);
443 struct nf_conntrack_tuple;
444 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
445 const struct sk_buff *skb);
447 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
448 static inline void nf_ct_set_closing(struct nf_conntrack *nfct) {}
449 struct nf_conntrack_tuple;
450 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
451 const struct sk_buff *skb)
458 enum ip_conntrack_info;
461 int (*update)(struct net *net, struct sk_buff *skb);
462 void (*destroy)(struct nf_conntrack *);
463 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
464 const struct sk_buff *);
465 void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb);
466 void (*set_closing)(struct nf_conntrack *nfct);
468 extern const struct nf_ct_hook __rcu *nf_ct_hook;
472 struct nfnl_ct_hook {
473 size_t (*build_size)(const struct nf_conn *ct);
474 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
475 enum ip_conntrack_info ctinfo,
476 u_int16_t ct_attr, u_int16_t ct_info_attr);
477 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
478 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
479 u32 portid, u32 report);
480 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
481 enum ip_conntrack_info ctinfo, s32 off);
483 extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;
485 struct nf_defrag_hook {
486 struct module *owner;
487 int (*enable)(struct net *net);
488 void (*disable)(struct net *net);
491 extern const struct nf_defrag_hook __rcu *nf_defrag_v4_hook;
492 extern const struct nf_defrag_hook __rcu *nf_defrag_v6_hook;
495 * nf_skb_duplicated - TEE target has sent a packet
497 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
498 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
500 * This is used by xtables TEE target to prevent the duplicated skb from
501 * being duplicated again.
503 DECLARE_PER_CPU(bool, nf_skb_duplicated);
506 * Contains bitmask of ctnetlink event subscribers, if any.
507 * Can't be pernet due to NETLINK_LISTEN_ALL_NSID setsockopt flag.
509 extern u8 nf_ctnetlink_has_listener;
510 #endif /*__LINUX_NETFILTER_H*/