1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
4 #include <linux/init.h>
5 #include <linux/skbuff.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
12 #include <linux/static_key.h>
13 #include <linux/netfilter_defs.h>
14 #include <linux/netdevice.h>
15 #include <net/net_namespace.h>
17 #ifdef CONFIG_NETFILTER
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 return a1->all[0] == a2->all[0] &&
27 a1->all[1] == a2->all[1] &&
28 a1->all[2] == a2->all[2] &&
29 a1->all[3] == a2->all[3];
32 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
33 union nf_inet_addr *result,
34 const union nf_inet_addr *mask)
36 result->all[0] = a1->all[0] & mask->all[0];
37 result->all[1] = a1->all[1] & mask->all[1];
38 result->all[2] = a1->all[2] & mask->all[2];
39 result->all[3] = a1->all[3] & mask->all[3];
42 int netfilter_init(void);
50 struct nf_hook_state {
54 struct net_device *in;
55 struct net_device *out;
57 struct list_head *hook_list;
58 int (*okfn)(struct sock *, struct sk_buff *);
61 static inline void nf_hook_state_init(struct nf_hook_state *p,
62 struct list_head *hook_list,
64 int thresh, u_int8_t pf,
65 struct net_device *indev,
66 struct net_device *outdev,
68 int (*okfn)(struct sock *, struct sk_buff *))
76 p->hook_list = hook_list;
80 typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
82 const struct nf_hook_state *state);
85 struct list_head list;
87 /* User fills in from here down. */
89 struct net_device *dev;
94 /* Hooks are ordered in ascending priority. */
98 struct nf_sockopt_ops {
99 struct list_head list;
103 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
106 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
108 int (*compat_set)(struct sock *sk, int optval,
109 void __user *user, unsigned int len);
113 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
115 int (*compat_get)(struct sock *sk, int optval,
116 void __user *user, int *len);
118 /* Use the module struct to lock set/get code in place */
119 struct module *owner;
122 /* Function to register/unregister hook points. */
123 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
124 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
125 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
127 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
130 int nf_register_hook(struct nf_hook_ops *reg);
131 void nf_unregister_hook(struct nf_hook_ops *reg);
132 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
133 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
135 /* Functions to register get/setsockopt ranges (non-inclusive). You
136 need to check permissions yourself! */
137 int nf_register_sockopt(struct nf_sockopt_ops *reg);
138 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
140 #ifdef HAVE_JUMP_LABEL
141 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
143 static inline bool nf_hook_list_active(struct list_head *hook_list,
144 u_int8_t pf, unsigned int hook)
146 if (__builtin_constant_p(pf) &&
147 __builtin_constant_p(hook))
148 return static_key_false(&nf_hooks_needed[pf][hook]);
150 return !list_empty(hook_list);
153 static inline bool nf_hook_list_active(struct list_head *hook_list,
154 u_int8_t pf, unsigned int hook)
156 return !list_empty(hook_list);
160 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
163 * nf_hook_thresh - call a netfilter hook
165 * Returns 1 if the hook has allowed the packet to pass. The function
166 * okfn must be invoked by the caller in this case. Any other return
167 * value indicates the packet has been consumed by the hook.
169 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
172 struct net_device *indev,
173 struct net_device *outdev,
174 int (*okfn)(struct sock *, struct sk_buff *),
177 struct net *net = dev_net(indev ? indev : outdev);
178 struct list_head *hook_list = &net->nf.hooks[pf][hook];
180 if (nf_hook_list_active(hook_list, pf, hook)) {
181 struct nf_hook_state state;
183 nf_hook_state_init(&state, hook_list, hook, thresh,
184 pf, indev, outdev, sk, okfn);
185 return nf_hook_slow(skb, &state);
190 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
191 struct sk_buff *skb, struct net_device *indev,
192 struct net_device *outdev,
193 int (*okfn)(struct sock *, struct sk_buff *))
195 return nf_hook_thresh(pf, hook, sk, skb, indev, outdev, okfn, INT_MIN);
198 /* Activate hook; either okfn or kfree_skb called, unless a hook
199 returns NF_STOLEN (in which case, it's up to the hook to deal with
202 Returns -ERRNO if packet dropped. Zero means queued, stolen or
207 > I don't want nf_hook to return anything because people might forget
208 > about async and trust the return value to mean "packet was ok".
211 Just document it clearly, then you can expect some sense from kernel
216 NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sock *sk,
217 struct sk_buff *skb, struct net_device *in,
218 struct net_device *out,
219 int (*okfn)(struct sock *, struct sk_buff *), int thresh)
221 int ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, thresh);
228 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sock *sk,
229 struct sk_buff *skb, struct net_device *in, struct net_device *out,
230 int (*okfn)(struct sock *, struct sk_buff *), bool cond)
235 ((ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, INT_MIN)) == 1))
241 NF_HOOK(uint8_t pf, unsigned int hook, struct sock *sk, struct sk_buff *skb,
242 struct net_device *in, struct net_device *out,
243 int (*okfn)(struct sock *, struct sk_buff *))
245 return NF_HOOK_THRESH(pf, hook, sk, skb, in, out, okfn, INT_MIN);
248 /* Call setsockopt() */
249 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
251 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
254 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
255 char __user *opt, unsigned int len);
256 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
257 char __user *opt, int *len);
260 /* Call this before modifying an existing packet: ensures it is
261 modifiable and linear to the point you care about (writable_len).
262 Returns true or false. */
263 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
266 struct nf_queue_entry;
269 unsigned short family;
270 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
271 unsigned int dataoff, u_int8_t protocol);
272 __sum16 (*checksum_partial)(struct sk_buff *skb,
274 unsigned int dataoff,
277 int (*route)(struct net *net, struct dst_entry **dst,
278 struct flowi *fl, bool strict);
279 void (*saveroute)(const struct sk_buff *skb,
280 struct nf_queue_entry *entry);
281 int (*reroute)(struct sk_buff *skb,
282 const struct nf_queue_entry *entry);
286 extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
287 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
289 return rcu_dereference(nf_afinfo[family]);
292 static inline __sum16
293 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
294 u_int8_t protocol, unsigned short family)
296 const struct nf_afinfo *afinfo;
300 afinfo = nf_get_afinfo(family);
302 csum = afinfo->checksum(skb, hook, dataoff, protocol);
307 static inline __sum16
308 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
309 unsigned int dataoff, unsigned int len,
310 u_int8_t protocol, unsigned short family)
312 const struct nf_afinfo *afinfo;
316 afinfo = nf_get_afinfo(family);
318 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
324 int nf_register_afinfo(const struct nf_afinfo *afinfo);
325 void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
327 #include <net/flow.h>
328 extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
331 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
333 #ifdef CONFIG_NF_NAT_NEEDED
334 void (*decodefn)(struct sk_buff *, struct flowi *);
337 decodefn = rcu_dereference(nf_nat_decode_session_hook);
344 #else /* !CONFIG_NETFILTER */
345 #define NF_HOOK(pf, hook, sk, skb, indev, outdev, okfn) (okfn)(sk, skb)
346 #define NF_HOOK_COND(pf, hook, sk, skb, indev, outdev, okfn, cond) (okfn)(sk, skb)
347 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
350 struct net_device *indev,
351 struct net_device *outdev,
352 int (*okfn)(struct sock *sk, struct sk_buff *), int thresh)
354 return okfn(sk, skb);
356 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
357 struct sk_buff *skb, struct net_device *indev,
358 struct net_device *outdev,
359 int (*okfn)(struct sock *, struct sk_buff *))
365 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
368 #endif /*CONFIG_NETFILTER*/
370 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
371 #include <linux/netfilter/nf_conntrack_zones_common.h>
373 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
374 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
375 extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
378 enum ip_conntrack_info;
382 size_t (*build_size)(const struct nf_conn *ct);
383 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
384 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
385 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
386 u32 portid, u32 report);
387 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
388 enum ip_conntrack_info ctinfo, s32 off);
390 extern struct nfq_ct_hook __rcu *nfq_ct_hook;
392 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
396 * nf_skb_duplicated - TEE target has sent a packet
398 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
399 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
401 * This is used by xtables TEE target to prevent the duplicated skb from
402 * being duplicated again.
404 DECLARE_PER_CPU(bool, nf_skb_duplicated);
406 #endif /*__LINUX_NETFILTER_H*/