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 <net/net_namespace.h>
18 static inline int NF_DROP_GETERR(int verdict
)
20 return -(verdict
>> NF_VERDICT_QBITS
);
23 static inline int nf_inet_addr_cmp(const union nf_inet_addr
*a1
,
24 const union nf_inet_addr
*a2
)
26 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
27 const unsigned long *ul1
= (const unsigned long *)a1
;
28 const unsigned long *ul2
= (const unsigned long *)a2
;
30 return ((ul1
[0] ^ ul2
[0]) | (ul1
[1] ^ ul2
[1])) == 0UL;
32 return a1
->all
[0] == a2
->all
[0] &&
33 a1
->all
[1] == a2
->all
[1] &&
34 a1
->all
[2] == a2
->all
[2] &&
35 a1
->all
[3] == a2
->all
[3];
39 static inline void nf_inet_addr_mask(const union nf_inet_addr
*a1
,
40 union nf_inet_addr
*result
,
41 const union nf_inet_addr
*mask
)
43 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
44 const unsigned long *ua
= (const unsigned long *)a1
;
45 unsigned long *ur
= (unsigned long *)result
;
46 const unsigned long *um
= (const unsigned long *)mask
;
48 ur
[0] = ua
[0] & um
[0];
49 ur
[1] = ua
[1] & um
[1];
51 result
->all
[0] = a1
->all
[0] & mask
->all
[0];
52 result
->all
[1] = a1
->all
[1] & mask
->all
[1];
53 result
->all
[2] = a1
->all
[2] & mask
->all
[2];
54 result
->all
[3] = a1
->all
[3] & mask
->all
[3];
58 int netfilter_init(void);
66 struct nf_hook_state
{
69 struct net_device
*in
;
70 struct net_device
*out
;
73 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*);
76 typedef unsigned int nf_hookfn(void *priv
,
78 const struct nf_hook_state
*state
);
80 /* User fills in from here down. */
82 struct net_device
*dev
;
86 /* Hooks are ordered in ascending priority. */
90 struct nf_hook_entry
{
95 struct nf_hook_entries_rcu_head
{
100 struct nf_hook_entries
{
101 u16 num_hook_entries
;
103 struct nf_hook_entry hooks
[];
105 /* trailer: pointers to original orig_ops of each hook,
106 * followed by rcu_head and scratch space used for freeing
107 * the structure via call_rcu.
109 * This is not part of struct nf_hook_entry since its only
110 * needed in slow path (hook register/unregister):
111 * const struct nf_hook_ops *orig_ops[]
113 * For the same reason, we store this at end -- its
114 * only needed when a hook is deleted, not during
115 * packet path processing:
116 * struct nf_hook_entries_rcu_head head
120 #ifdef CONFIG_NETFILTER
121 static inline struct nf_hook_ops
**nf_hook_entries_get_hook_ops(const struct nf_hook_entries
*e
)
123 unsigned int n
= e
->num_hook_entries
;
124 const void *hook_end
;
126 hook_end
= &e
->hooks
[n
]; /* this is *past* ->hooks[]! */
128 return (struct nf_hook_ops
**)hook_end
;
132 nf_hook_entry_hookfn(const struct nf_hook_entry
*entry
, struct sk_buff
*skb
,
133 struct nf_hook_state
*state
)
135 return entry
->hook(entry
->priv
, skb
, state
);
138 static inline void nf_hook_state_init(struct nf_hook_state
*p
,
141 struct net_device
*indev
,
142 struct net_device
*outdev
,
145 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
158 struct nf_sockopt_ops
{
159 struct list_head list
;
163 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
166 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
168 int (*compat_set
)(struct sock
*sk
, int optval
,
169 void __user
*user
, unsigned int len
);
173 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
175 int (*compat_get
)(struct sock
*sk
, int optval
,
176 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
]);
246 #if IS_ENABLED(CONFIG_DECNET)
248 hook_head
= rcu_dereference(net
->nf
.hooks_decnet
[hook
]);
257 struct nf_hook_state state
;
259 nf_hook_state_init(&state
, hook
, pf
, indev
, outdev
,
262 ret
= nf_hook_slow(skb
, &state
, hook_head
, 0);
269 /* Activate hook; either okfn or kfree_skb called, unless a hook
270 returns NF_STOLEN (in which case, it's up to the hook to deal with
273 Returns -ERRNO if packet dropped. Zero means queued, stolen or
278 > I don't want nf_hook to return anything because people might forget
279 > about async and trust the return value to mean "packet was ok".
282 Just document it clearly, then you can expect some sense from kernel
287 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
288 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
289 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*),
295 ((ret
= nf_hook(pf
, hook
, net
, sk
, skb
, in
, out
, okfn
)) == 1))
296 ret
= okfn(net
, sk
, skb
);
301 NF_HOOK(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
,
302 struct net_device
*in
, struct net_device
*out
,
303 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
305 int ret
= nf_hook(pf
, hook
, net
, sk
, skb
, in
, out
, okfn
);
307 ret
= okfn(net
, sk
, skb
);
312 NF_HOOK_LIST(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
313 struct list_head
*head
, struct net_device
*in
, struct net_device
*out
,
314 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
316 struct nf_hook_entries
*hook_head
= NULL
;
318 #ifdef CONFIG_JUMP_LABEL
319 if (__builtin_constant_p(pf
) &&
320 __builtin_constant_p(hook
) &&
321 !static_key_false(&nf_hooks_needed
[pf
][hook
]))
328 hook_head
= rcu_dereference(net
->nf
.hooks_ipv4
[hook
]);
331 hook_head
= rcu_dereference(net
->nf
.hooks_ipv6
[hook
]);
339 struct nf_hook_state state
;
341 nf_hook_state_init(&state
, hook
, pf
, in
, out
, sk
, net
, okfn
);
343 nf_hook_slow_list(head
, &state
, hook_head
);
348 /* Call setsockopt() */
349 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
351 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
354 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
355 char __user
*opt
, unsigned int len
);
356 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
357 char __user
*opt
, int *len
);
361 struct nf_queue_entry
;
363 __sum16
nf_checksum(struct sk_buff
*skb
, unsigned int hook
,
364 unsigned int dataoff
, u_int8_t protocol
,
365 unsigned short family
);
367 __sum16
nf_checksum_partial(struct sk_buff
*skb
, unsigned int hook
,
368 unsigned int dataoff
, unsigned int len
,
369 u_int8_t protocol
, unsigned short family
);
370 int nf_route(struct net
*net
, struct dst_entry
**dst
, struct flowi
*fl
,
371 bool strict
, unsigned short family
);
372 int nf_reroute(struct sk_buff
*skb
, struct nf_queue_entry
*entry
);
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
);
390 extern struct nf_nat_hook __rcu
*nf_nat_hook
;
393 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
395 #if IS_ENABLED(CONFIG_NF_NAT)
396 struct nf_nat_hook
*nat_hook
;
399 nat_hook
= rcu_dereference(nf_nat_hook
);
400 if (nat_hook
&& nat_hook
->decode_session
)
401 nat_hook
->decode_session(skb
, fl
);
406 #else /* !CONFIG_NETFILTER */
408 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
409 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
410 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*),
413 return okfn(net
, sk
, skb
);
417 NF_HOOK(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
418 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
419 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
421 return okfn(net
, sk
, skb
);
425 NF_HOOK_LIST(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
426 struct list_head
*head
, struct net_device
*in
, struct net_device
*out
,
427 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
432 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct net
*net
,
433 struct sock
*sk
, struct sk_buff
*skb
,
434 struct net_device
*indev
, struct net_device
*outdev
,
435 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
441 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
444 #endif /*CONFIG_NETFILTER*/
446 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
447 #include <linux/netfilter/nf_conntrack_zones_common.h>
449 extern void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*) __rcu
;
450 void nf_ct_attach(struct sk_buff
*, const struct sk_buff
*);
451 struct nf_conntrack_tuple
;
452 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple
*dst_tuple
,
453 const struct sk_buff
*skb
);
455 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
456 struct nf_conntrack_tuple
;
457 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple
*dst_tuple
,
458 const struct sk_buff
*skb
)
465 enum ip_conntrack_info
;
468 int (*update
)(struct net
*net
, struct sk_buff
*skb
);
469 void (*destroy
)(struct nf_conntrack
*);
470 bool (*get_tuple_skb
)(struct nf_conntrack_tuple
*,
471 const struct sk_buff
*);
473 extern struct nf_ct_hook __rcu
*nf_ct_hook
;
477 struct nfnl_ct_hook
{
478 struct nf_conn
*(*get_ct
)(const struct sk_buff
*skb
,
479 enum ip_conntrack_info
*ctinfo
);
480 size_t (*build_size
)(const struct nf_conn
*ct
);
481 int (*build
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
482 enum ip_conntrack_info ctinfo
,
483 u_int16_t ct_attr
, u_int16_t ct_info_attr
);
484 int (*parse
)(const struct nlattr
*attr
, struct nf_conn
*ct
);
485 int (*attach_expect
)(const struct nlattr
*attr
, struct nf_conn
*ct
,
486 u32 portid
, u32 report
);
487 void (*seq_adjust
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
488 enum ip_conntrack_info ctinfo
, s32 off
);
490 extern struct nfnl_ct_hook __rcu
*nfnl_ct_hook
;
493 * nf_skb_duplicated - TEE target has sent a packet
495 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
496 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
498 * This is used by xtables TEE target to prevent the duplicated skb from
499 * being duplicated again.
501 DECLARE_PER_CPU(bool, nf_skb_duplicated
);
503 #endif /*__LINUX_NETFILTER_H*/