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 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
28 const unsigned long *ul1 = (const unsigned long *)a1;
29 const unsigned long *ul2 = (const unsigned long *)a2;
31 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
33 return a1->all[0] == a2->all[0] &&
34 a1->all[1] == a2->all[1] &&
35 a1->all[2] == a2->all[2] &&
36 a1->all[3] == a2->all[3];
40 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
41 union nf_inet_addr *result,
42 const union nf_inet_addr *mask)
44 result->all[0] = a1->all[0] & mask->all[0];
45 result->all[1] = a1->all[1] & mask->all[1];
46 result->all[2] = a1->all[2] & mask->all[2];
47 result->all[3] = a1->all[3] & mask->all[3];
50 int netfilter_init(void);
58 struct nf_hook_state {
61 struct net_device *in;
62 struct net_device *out;
65 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
68 typedef unsigned int nf_hookfn(void *priv,
70 const struct nf_hook_state *state);
72 /* User fills in from here down. */
74 struct net_device *dev;
78 /* Hooks are ordered in ascending priority. */
82 struct nf_hook_entry {
87 struct nf_hook_entries_rcu_head {
92 struct nf_hook_entries {
95 struct nf_hook_entry hooks[];
97 /* trailer: pointers to original orig_ops of each hook,
98 * followed by rcu_head and scratch space used for freeing
99 * the structure via call_rcu.
101 * This is not part of struct nf_hook_entry since its only
102 * needed in slow path (hook register/unregister):
103 * const struct nf_hook_ops *orig_ops[]
105 * For the same reason, we store this at end -- its
106 * only needed when a hook is deleted, not during
107 * packet path processing:
108 * struct nf_hook_entries_rcu_head head
112 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
114 unsigned int n = e->num_hook_entries;
115 const void *hook_end;
117 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
119 return (struct nf_hook_ops **)hook_end;
123 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
124 struct nf_hook_state *state)
126 return entry->hook(entry->priv, skb, state);
129 static inline void nf_hook_state_init(struct nf_hook_state *p,
132 struct net_device *indev,
133 struct net_device *outdev,
136 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
149 struct nf_sockopt_ops {
150 struct list_head list;
154 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
157 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
159 int (*compat_set)(struct sock *sk, int optval,
160 void __user *user, unsigned int len);
164 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
166 int (*compat_get)(struct sock *sk, int optval,
167 void __user *user, int *len);
169 /* Use the module struct to lock set/get code in place */
170 struct module *owner;
173 /* Function to register/unregister hook points. */
174 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
175 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
176 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
178 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
181 /* Functions to register get/setsockopt ranges (non-inclusive). You
182 need to check permissions yourself! */
183 int nf_register_sockopt(struct nf_sockopt_ops *reg);
184 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
186 #ifdef CONFIG_JUMP_LABEL
187 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
190 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
191 const struct nf_hook_entries *e, unsigned int i);
194 * nf_hook - call a netfilter hook
196 * Returns 1 if the hook has allowed the packet to pass. The function
197 * okfn must be invoked by the caller in this case. Any other return
198 * value indicates the packet has been consumed by the hook.
200 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
201 struct sock *sk, struct sk_buff *skb,
202 struct net_device *indev, struct net_device *outdev,
203 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
205 struct nf_hook_entries *hook_head = NULL;
208 #ifdef CONFIG_JUMP_LABEL
209 if (__builtin_constant_p(pf) &&
210 __builtin_constant_p(hook) &&
211 !static_key_false(&nf_hooks_needed[pf][hook]))
218 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
221 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
224 #ifdef CONFIG_NETFILTER_FAMILY_ARP
225 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
227 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
231 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
232 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
235 #if IS_ENABLED(CONFIG_DECNET)
237 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
246 struct nf_hook_state state;
248 nf_hook_state_init(&state, hook, pf, indev, outdev,
251 ret = nf_hook_slow(skb, &state, hook_head, 0);
258 /* Activate hook; either okfn or kfree_skb called, unless a hook
259 returns NF_STOLEN (in which case, it's up to the hook to deal with
262 Returns -ERRNO if packet dropped. Zero means queued, stolen or
267 > I don't want nf_hook to return anything because people might forget
268 > about async and trust the return value to mean "packet was ok".
271 Just document it clearly, then you can expect some sense from kernel
276 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
277 struct sk_buff *skb, struct net_device *in, struct net_device *out,
278 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
284 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
285 ret = okfn(net, sk, skb);
290 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
291 struct net_device *in, struct net_device *out,
292 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
294 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
296 ret = okfn(net, sk, skb);
301 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
302 struct list_head *head, struct net_device *in, struct net_device *out,
303 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
305 struct sk_buff *skb, *next;
306 struct list_head sublist;
308 INIT_LIST_HEAD(&sublist);
309 list_for_each_entry_safe(skb, next, head, list) {
310 list_del(&skb->list);
311 if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
312 list_add_tail(&skb->list, &sublist);
314 /* Put passed packets back on main list */
315 list_splice(&sublist, head);
318 /* Call setsockopt() */
319 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
321 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
324 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
325 char __user *opt, unsigned int len);
326 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
327 char __user *opt, int *len);
330 /* Call this before modifying an existing packet: ensures it is
331 modifiable and linear to the point you care about (writable_len).
332 Returns true or false. */
333 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
336 struct nf_queue_entry;
338 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
339 unsigned int dataoff, u_int8_t protocol,
340 unsigned short family);
342 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
343 unsigned int dataoff, unsigned int len,
344 u_int8_t protocol, unsigned short family);
345 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
346 bool strict, unsigned short family);
347 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
349 #include <net/flow.h>
352 enum nf_nat_manip_type;
354 enum ip_conntrack_dir;
357 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
358 const struct nlattr *attr);
359 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
360 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
361 enum nf_nat_manip_type mtype,
362 enum ip_conntrack_dir dir);
365 extern struct nf_nat_hook __rcu *nf_nat_hook;
368 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
370 #if IS_ENABLED(CONFIG_NF_NAT)
371 struct nf_nat_hook *nat_hook;
374 nat_hook = rcu_dereference(nf_nat_hook);
375 if (nat_hook && nat_hook->decode_session)
376 nat_hook->decode_session(skb, fl);
381 #else /* !CONFIG_NETFILTER */
383 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
384 struct sk_buff *skb, struct net_device *in, struct net_device *out,
385 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
388 return okfn(net, sk, skb);
392 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
393 struct sk_buff *skb, struct net_device *in, struct net_device *out,
394 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
396 return okfn(net, sk, skb);
400 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
401 struct list_head *head, struct net_device *in, struct net_device *out,
402 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
407 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
408 struct sock *sk, struct sk_buff *skb,
409 struct net_device *indev, struct net_device *outdev,
410 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
416 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
419 #endif /*CONFIG_NETFILTER*/
421 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
422 #include <linux/netfilter/nf_conntrack_zones_common.h>
424 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
425 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
426 struct nf_conntrack_tuple;
427 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
428 const struct sk_buff *skb);
430 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
431 struct nf_conntrack_tuple;
432 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
433 const struct sk_buff *skb)
440 enum ip_conntrack_info;
443 int (*update)(struct net *net, struct sk_buff *skb);
444 void (*destroy)(struct nf_conntrack *);
445 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
446 const struct sk_buff *);
448 extern struct nf_ct_hook __rcu *nf_ct_hook;
452 struct nfnl_ct_hook {
453 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
454 enum ip_conntrack_info *ctinfo);
455 size_t (*build_size)(const struct nf_conn *ct);
456 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
457 enum ip_conntrack_info ctinfo,
458 u_int16_t ct_attr, u_int16_t ct_info_attr);
459 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
460 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
461 u32 portid, u32 report);
462 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
463 enum ip_conntrack_info ctinfo, s32 off);
465 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
468 * nf_skb_duplicated - TEE target has sent a packet
470 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
471 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
473 * This is used by xtables TEE target to prevent the duplicated skb from
474 * being duplicated again.
476 DECLARE_PER_CPU(bool, nf_skb_duplicated);
478 #endif /*__LINUX_NETFILTER_H*/