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
;
381 int (*parse_nat_setup
)(struct nf_conn
*ct
, enum nf_nat_manip_type manip
,
382 const struct nlattr
*attr
);
383 void (*decode_session
)(struct sk_buff
*skb
, struct flowi
*fl
);
384 void (*remove_nat_bysrc
)(struct nf_conn
*ct
);
387 extern const struct nf_nat_hook __rcu
*nf_nat_hook
;
390 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
392 #if IS_ENABLED(CONFIG_NF_NAT)
393 const struct nf_nat_hook
*nat_hook
;
396 nat_hook
= rcu_dereference(nf_nat_hook
);
397 if (nat_hook
&& nat_hook
->decode_session
)
398 nat_hook
->decode_session(skb
, fl
);
403 #else /* !CONFIG_NETFILTER */
405 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
406 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
407 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*),
410 return okfn(net
, sk
, skb
);
414 NF_HOOK(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
415 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
416 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
418 return okfn(net
, sk
, skb
);
422 NF_HOOK_LIST(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
423 struct list_head
*head
, struct net_device
*in
, struct net_device
*out
,
424 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
429 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct net
*net
,
430 struct sock
*sk
, struct sk_buff
*skb
,
431 struct net_device
*indev
, struct net_device
*outdev
,
432 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
438 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
441 #endif /*CONFIG_NETFILTER*/
443 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
444 #include <linux/netfilter/nf_conntrack_zones_common.h>
446 void nf_ct_attach(struct sk_buff
*, const struct sk_buff
*);
447 void nf_ct_set_closing(struct nf_conntrack
*nfct
);
448 struct nf_conntrack_tuple
;
449 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple
*dst_tuple
,
450 const struct sk_buff
*skb
);
452 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
453 static inline void nf_ct_set_closing(struct nf_conntrack
*nfct
) {}
454 struct nf_conntrack_tuple
;
455 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple
*dst_tuple
,
456 const struct sk_buff
*skb
)
463 enum ip_conntrack_info
;
466 int (*update
)(struct net
*net
, struct sk_buff
*skb
);
467 void (*destroy
)(struct nf_conntrack
*);
468 bool (*get_tuple_skb
)(struct nf_conntrack_tuple
*,
469 const struct sk_buff
*);
470 void (*attach
)(struct sk_buff
*nskb
, const struct sk_buff
*skb
);
471 void (*set_closing
)(struct nf_conntrack
*nfct
);
472 int (*confirm
)(struct sk_buff
*skb
);
474 extern const struct nf_ct_hook __rcu
*nf_ct_hook
;
478 struct nfnl_ct_hook
{
479 size_t (*build_size
)(const struct nf_conn
*ct
);
480 int (*build
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
481 enum ip_conntrack_info ctinfo
,
482 u_int16_t ct_attr
, u_int16_t ct_info_attr
);
483 int (*parse
)(const struct nlattr
*attr
, struct nf_conn
*ct
);
484 int (*attach_expect
)(const struct nlattr
*attr
, struct nf_conn
*ct
,
485 u32 portid
, u32 report
);
486 void (*seq_adjust
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
487 enum ip_conntrack_info ctinfo
, s32 off
);
489 extern const struct nfnl_ct_hook __rcu
*nfnl_ct_hook
;
491 struct nf_defrag_hook
{
492 struct module
*owner
;
493 int (*enable
)(struct net
*net
);
494 void (*disable
)(struct net
*net
);
497 extern const struct nf_defrag_hook __rcu
*nf_defrag_v4_hook
;
498 extern const struct nf_defrag_hook __rcu
*nf_defrag_v6_hook
;
501 * nf_skb_duplicated - TEE target has sent a packet
503 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
504 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
506 * This is used by xtables TEE target to prevent the duplicated skb from
507 * being duplicated again.
509 DECLARE_PER_CPU(bool, nf_skb_duplicated
);
512 * Contains bitmask of ctnetlink event subscribers, if any.
513 * Can't be pernet due to NETLINK_LISTEN_ALL_NSID setsockopt flag.
515 extern u8 nf_ctnetlink_has_listener
;
516 #endif /*__LINUX_NETFILTER_H*/