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 #ifdef CONFIG_NETFILTER
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 return a1
->all
[0] == a2
->all
[0] &&
28 a1
->all
[1] == a2
->all
[1] &&
29 a1
->all
[2] == a2
->all
[2] &&
30 a1
->all
[3] == a2
->all
[3];
33 static inline void nf_inet_addr_mask(const union nf_inet_addr
*a1
,
34 union nf_inet_addr
*result
,
35 const union nf_inet_addr
*mask
)
37 result
->all
[0] = a1
->all
[0] & mask
->all
[0];
38 result
->all
[1] = a1
->all
[1] & mask
->all
[1];
39 result
->all
[2] = a1
->all
[2] & mask
->all
[2];
40 result
->all
[3] = a1
->all
[3] & mask
->all
[3];
43 int netfilter_init(void);
51 struct nf_hook_state
{
54 struct net_device
*in
;
55 struct net_device
*out
;
58 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*);
61 typedef unsigned int nf_hookfn(void *priv
,
63 const struct nf_hook_state
*state
);
65 /* User fills in from here down. */
67 struct net_device
*dev
;
71 /* Hooks are ordered in ascending priority. */
75 struct nf_hook_entry
{
80 struct nf_hook_entries
{
83 struct nf_hook_entry hooks
[];
85 /* trailer: pointers to original orig_ops of each hook.
87 * This is not part of struct nf_hook_entry since its only
88 * needed in slow path (hook register/unregister).
90 * const struct nf_hook_ops *orig_ops[]
94 static inline struct nf_hook_ops
**nf_hook_entries_get_hook_ops(const struct nf_hook_entries
*e
)
96 unsigned int n
= e
->num_hook_entries
;
99 hook_end
= &e
->hooks
[n
]; /* this is *past* ->hooks[]! */
101 return (struct nf_hook_ops
**)hook_end
;
105 nf_hook_entry_hookfn(const struct nf_hook_entry
*entry
, struct sk_buff
*skb
,
106 struct nf_hook_state
*state
)
108 return entry
->hook(entry
->priv
, skb
, state
);
111 static inline void nf_hook_state_init(struct nf_hook_state
*p
,
114 struct net_device
*indev
,
115 struct net_device
*outdev
,
118 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
131 struct nf_sockopt_ops
{
132 struct list_head list
;
136 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
139 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
141 int (*compat_set
)(struct sock
*sk
, int optval
,
142 void __user
*user
, unsigned int len
);
146 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
148 int (*compat_get
)(struct sock
*sk
, int optval
,
149 void __user
*user
, int *len
);
151 /* Use the module struct to lock set/get code in place */
152 struct module
*owner
;
155 /* Function to register/unregister hook points. */
156 int nf_register_net_hook(struct net
*net
, const struct nf_hook_ops
*ops
);
157 void nf_unregister_net_hook(struct net
*net
, const struct nf_hook_ops
*ops
);
158 int nf_register_net_hooks(struct net
*net
, const struct nf_hook_ops
*reg
,
160 void nf_unregister_net_hooks(struct net
*net
, const struct nf_hook_ops
*reg
,
163 /* Functions to register get/setsockopt ranges (non-inclusive). You
164 need to check permissions yourself! */
165 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
166 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
168 #ifdef HAVE_JUMP_LABEL
169 extern struct static_key nf_hooks_needed
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
172 int nf_hook_slow(struct sk_buff
*skb
, struct nf_hook_state
*state
,
173 const struct nf_hook_entries
*e
, unsigned int i
);
176 * nf_hook - call a netfilter hook
178 * Returns 1 if the hook has allowed the packet to pass. The function
179 * okfn must be invoked by the caller in this case. Any other return
180 * value indicates the packet has been consumed by the hook.
182 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct net
*net
,
183 struct sock
*sk
, struct sk_buff
*skb
,
184 struct net_device
*indev
, struct net_device
*outdev
,
185 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
187 struct nf_hook_entries
*hook_head
;
190 #ifdef HAVE_JUMP_LABEL
191 if (__builtin_constant_p(pf
) &&
192 __builtin_constant_p(hook
) &&
193 !static_key_false(&nf_hooks_needed
[pf
][hook
]))
198 hook_head
= rcu_dereference(net
->nf
.hooks
[pf
][hook
]);
200 struct nf_hook_state state
;
202 nf_hook_state_init(&state
, hook
, pf
, indev
, outdev
,
205 ret
= nf_hook_slow(skb
, &state
, hook_head
, 0);
212 /* Activate hook; either okfn or kfree_skb called, unless a hook
213 returns NF_STOLEN (in which case, it's up to the hook to deal with
216 Returns -ERRNO if packet dropped. Zero means queued, stolen or
221 > I don't want nf_hook to return anything because people might forget
222 > about async and trust the return value to mean "packet was ok".
225 Just document it clearly, then you can expect some sense from kernel
230 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
231 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
232 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*),
238 ((ret
= nf_hook(pf
, hook
, net
, sk
, skb
, in
, out
, okfn
)) == 1))
239 ret
= okfn(net
, sk
, skb
);
244 NF_HOOK(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
,
245 struct net_device
*in
, struct net_device
*out
,
246 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
248 int ret
= nf_hook(pf
, hook
, net
, sk
, skb
, in
, out
, okfn
);
250 ret
= okfn(net
, sk
, skb
);
254 /* Call setsockopt() */
255 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
257 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
260 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
261 char __user
*opt
, unsigned int len
);
262 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
263 char __user
*opt
, int *len
);
266 /* Call this before modifying an existing packet: ensures it is
267 modifiable and linear to the point you care about (writable_len).
268 Returns true or false. */
269 int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
272 struct nf_queue_entry
;
275 unsigned short family
;
276 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
277 unsigned int dataoff
, u_int8_t protocol
);
278 __sum16 (*checksum_partial
)(struct sk_buff
*skb
,
280 unsigned int dataoff
,
283 int (*route
)(struct net
*net
, struct dst_entry
**dst
,
284 struct flowi
*fl
, bool strict
);
285 void (*saveroute
)(const struct sk_buff
*skb
,
286 struct nf_queue_entry
*entry
);
287 int (*reroute
)(struct net
*net
, struct sk_buff
*skb
,
288 const struct nf_queue_entry
*entry
);
292 extern const struct nf_afinfo __rcu
*nf_afinfo
[NFPROTO_NUMPROTO
];
293 static inline const struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
295 return rcu_dereference(nf_afinfo
[family
]);
298 static inline __sum16
299 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
300 u_int8_t protocol
, unsigned short family
)
302 const struct nf_afinfo
*afinfo
;
306 afinfo
= nf_get_afinfo(family
);
308 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
313 static inline __sum16
314 nf_checksum_partial(struct sk_buff
*skb
, unsigned int hook
,
315 unsigned int dataoff
, unsigned int len
,
316 u_int8_t protocol
, unsigned short family
)
318 const struct nf_afinfo
*afinfo
;
322 afinfo
= nf_get_afinfo(family
);
324 csum
= afinfo
->checksum_partial(skb
, hook
, dataoff
, len
,
330 int nf_register_afinfo(const struct nf_afinfo
*afinfo
);
331 void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
);
333 #include <net/flow.h>
334 extern void (*nf_nat_decode_session_hook
)(struct sk_buff
*, struct flowi
*);
337 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
339 #ifdef CONFIG_NF_NAT_NEEDED
340 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
343 decodefn
= rcu_dereference(nf_nat_decode_session_hook
);
350 #else /* !CONFIG_NETFILTER */
352 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
353 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
354 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*),
357 return okfn(net
, sk
, skb
);
361 NF_HOOK(uint8_t pf
, unsigned int hook
, struct net
*net
, struct sock
*sk
,
362 struct sk_buff
*skb
, struct net_device
*in
, struct net_device
*out
,
363 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
365 return okfn(net
, sk
, skb
);
368 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct net
*net
,
369 struct sock
*sk
, struct sk_buff
*skb
,
370 struct net_device
*indev
, struct net_device
*outdev
,
371 int (*okfn
)(struct net
*, struct sock
*, struct sk_buff
*))
377 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
380 #endif /*CONFIG_NETFILTER*/
382 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
383 #include <linux/netfilter/nf_conntrack_zones_common.h>
385 extern void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*) __rcu
;
386 void nf_ct_attach(struct sk_buff
*, const struct sk_buff
*);
387 extern void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu
;
389 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
393 enum ip_conntrack_info
;
396 struct nfnl_ct_hook
{
397 struct nf_conn
*(*get_ct
)(const struct sk_buff
*skb
,
398 enum ip_conntrack_info
*ctinfo
);
399 size_t (*build_size
)(const struct nf_conn
*ct
);
400 int (*build
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
401 enum ip_conntrack_info ctinfo
,
402 u_int16_t ct_attr
, u_int16_t ct_info_attr
);
403 int (*parse
)(const struct nlattr
*attr
, struct nf_conn
*ct
);
404 int (*attach_expect
)(const struct nlattr
*attr
, struct nf_conn
*ct
,
405 u32 portid
, u32 report
);
406 void (*seq_adjust
)(struct sk_buff
*skb
, struct nf_conn
*ct
,
407 enum ip_conntrack_info ctinfo
, s32 off
);
409 extern struct nfnl_ct_hook __rcu
*nfnl_ct_hook
;
412 * nf_skb_duplicated - TEE target has sent a packet
414 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
415 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
417 * This is used by xtables TEE target to prevent the duplicated skb from
418 * being duplicated again.
420 DECLARE_PER_CPU(bool, nf_skb_duplicated
);
422 #endif /*__LINUX_NETFILTER_H*/