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>
15 #ifdef CONFIG_NETFILTER
16 static inline int NF_DROP_GETERR(int verdict)
18 return -(verdict >> NF_VERDICT_QBITS);
21 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
22 const union nf_inet_addr *a2)
24 return a1->all[0] == a2->all[0] &&
25 a1->all[1] == a2->all[1] &&
26 a1->all[2] == a2->all[2] &&
27 a1->all[3] == a2->all[3];
30 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
31 union nf_inet_addr *result,
32 const union nf_inet_addr *mask)
34 result->all[0] = a1->all[0] & mask->all[0];
35 result->all[1] = a1->all[1] & mask->all[1];
36 result->all[2] = a1->all[2] & mask->all[2];
37 result->all[3] = a1->all[3] & mask->all[3];
40 int netfilter_init(void);
48 struct nf_hook_state {
52 struct net_device *in;
53 struct net_device *out;
55 struct list_head *hook_list;
56 int (*okfn)(struct sock *, struct sk_buff *);
59 static inline void nf_hook_state_init(struct nf_hook_state *p,
60 struct list_head *hook_list,
62 int thresh, u_int8_t pf,
63 struct net_device *indev,
64 struct net_device *outdev,
66 int (*okfn)(struct sock *, struct sk_buff *))
74 p->hook_list = hook_list;
78 typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
80 const struct nf_hook_state *state);
83 struct list_head list;
85 /* User fills in from here down. */
87 struct net_device *dev;
92 /* Hooks are ordered in ascending priority. */
96 struct nf_sockopt_ops {
97 struct list_head list;
101 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
104 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
106 int (*compat_set)(struct sock *sk, int optval,
107 void __user *user, unsigned int len);
111 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
113 int (*compat_get)(struct sock *sk, int optval,
114 void __user *user, int *len);
116 /* Use the module struct to lock set/get code in place */
117 struct module *owner;
120 /* Function to register/unregister hook points. */
121 int nf_register_hook(struct nf_hook_ops *reg);
122 void nf_unregister_hook(struct nf_hook_ops *reg);
123 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
124 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
126 /* Functions to register get/setsockopt ranges (non-inclusive). You
127 need to check permissions yourself! */
128 int nf_register_sockopt(struct nf_sockopt_ops *reg);
129 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
131 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
133 #ifdef HAVE_JUMP_LABEL
134 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
136 static inline bool nf_hook_list_active(struct list_head *nf_hook_list,
137 u_int8_t pf, unsigned int hook)
139 if (__builtin_constant_p(pf) &&
140 __builtin_constant_p(hook))
141 return static_key_false(&nf_hooks_needed[pf][hook]);
143 return !list_empty(nf_hook_list);
146 static inline bool nf_hook_list_active(struct list_head *nf_hook_list,
147 u_int8_t pf, unsigned int hook)
149 return !list_empty(nf_hook_list);
153 static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
155 return nf_hook_list_active(&nf_hooks[pf][hook], pf, hook);
158 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
161 * nf_hook_thresh - call a netfilter hook
163 * Returns 1 if the hook has allowed the packet to pass. The function
164 * okfn must be invoked by the caller in this case. Any other return
165 * value indicates the packet has been consumed by the hook.
167 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
170 struct net_device *indev,
171 struct net_device *outdev,
172 int (*okfn)(struct sock *, struct sk_buff *),
175 if (nf_hooks_active(pf, hook)) {
176 struct nf_hook_state state;
178 nf_hook_state_init(&state, &nf_hooks[pf][hook], hook, thresh,
179 pf, indev, outdev, sk, okfn);
180 return nf_hook_slow(skb, &state);
185 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
186 struct sk_buff *skb, struct net_device *indev,
187 struct net_device *outdev,
188 int (*okfn)(struct sock *, struct sk_buff *))
190 return nf_hook_thresh(pf, hook, sk, skb, indev, outdev, okfn, INT_MIN);
193 /* Activate hook; either okfn or kfree_skb called, unless a hook
194 returns NF_STOLEN (in which case, it's up to the hook to deal with
197 Returns -ERRNO if packet dropped. Zero means queued, stolen or
202 > I don't want nf_hook to return anything because people might forget
203 > about async and trust the return value to mean "packet was ok".
206 Just document it clearly, then you can expect some sense from kernel
211 NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sock *sk,
212 struct sk_buff *skb, struct net_device *in,
213 struct net_device *out,
214 int (*okfn)(struct sock *, struct sk_buff *), int thresh)
216 int ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, thresh);
223 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sock *sk,
224 struct sk_buff *skb, struct net_device *in, struct net_device *out,
225 int (*okfn)(struct sock *, struct sk_buff *), bool cond)
230 ((ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, INT_MIN)) == 1))
236 NF_HOOK(uint8_t pf, unsigned int hook, struct sock *sk, struct sk_buff *skb,
237 struct net_device *in, struct net_device *out,
238 int (*okfn)(struct sock *, struct sk_buff *))
240 return NF_HOOK_THRESH(pf, hook, sk, skb, in, out, okfn, INT_MIN);
243 /* Call setsockopt() */
244 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
246 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
249 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
250 char __user *opt, unsigned int len);
251 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
252 char __user *opt, int *len);
255 /* Call this before modifying an existing packet: ensures it is
256 modifiable and linear to the point you care about (writable_len).
257 Returns true or false. */
258 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
261 struct nf_queue_entry;
264 unsigned short family;
265 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
266 unsigned int dataoff, u_int8_t protocol);
267 __sum16 (*checksum_partial)(struct sk_buff *skb,
269 unsigned int dataoff,
272 int (*route)(struct net *net, struct dst_entry **dst,
273 struct flowi *fl, bool strict);
274 void (*saveroute)(const struct sk_buff *skb,
275 struct nf_queue_entry *entry);
276 int (*reroute)(struct sk_buff *skb,
277 const struct nf_queue_entry *entry);
281 extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
282 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
284 return rcu_dereference(nf_afinfo[family]);
287 static inline __sum16
288 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
289 u_int8_t protocol, unsigned short family)
291 const struct nf_afinfo *afinfo;
295 afinfo = nf_get_afinfo(family);
297 csum = afinfo->checksum(skb, hook, dataoff, protocol);
302 static inline __sum16
303 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
304 unsigned int dataoff, unsigned int len,
305 u_int8_t protocol, unsigned short family)
307 const struct nf_afinfo *afinfo;
311 afinfo = nf_get_afinfo(family);
313 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
319 int nf_register_afinfo(const struct nf_afinfo *afinfo);
320 void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
322 #include <net/flow.h>
323 extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
326 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
328 #ifdef CONFIG_NF_NAT_NEEDED
329 void (*decodefn)(struct sk_buff *, struct flowi *);
332 decodefn = rcu_dereference(nf_nat_decode_session_hook);
339 #else /* !CONFIG_NETFILTER */
340 #define NF_HOOK(pf, hook, sk, skb, indev, outdev, okfn) (okfn)(sk, skb)
341 #define NF_HOOK_COND(pf, hook, sk, skb, indev, outdev, okfn, cond) (okfn)(sk, skb)
342 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
345 struct net_device *indev,
346 struct net_device *outdev,
347 int (*okfn)(struct sock *sk, struct sk_buff *), int thresh)
349 return okfn(sk, skb);
351 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
352 struct sk_buff *skb, struct net_device *indev,
353 struct net_device *outdev,
354 int (*okfn)(struct sock *, struct sk_buff *))
360 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
363 #endif /*CONFIG_NETFILTER*/
365 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
366 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
367 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
368 extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
371 enum ip_conntrack_info;
375 size_t (*build_size)(const struct nf_conn *ct);
376 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
377 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
378 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
379 u32 portid, u32 report);
380 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
381 enum ip_conntrack_info ctinfo, s32 off);
383 extern struct nfq_ct_hook __rcu *nfq_ct_hook;
385 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
388 #endif /*__LINUX_NETFILTER_H*/