1 #ifndef __LINUX_NETFILTER_H
2 #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>
14 #include <linux/types.h>
15 #include <linux/compiler.h>
16 #include <linux/sysctl.h>
18 /* Responses from hook functions. */
25 #define NF_MAX_VERDICT NF_STOP
27 /* we overload the higher bits for encoding auxiliary data such as the queue
28 * number or errno values. Not nice, but better than additional function
30 #define NF_VERDICT_MASK 0x000000ff
32 /* extra verdict flags have mask 0x0000ff00 */
33 #define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
35 /* queue number (NF_QUEUE) or errno (NF_DROP) */
36 #define NF_VERDICT_QMASK 0xffff0000
37 #define NF_VERDICT_QBITS 16
39 #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
41 #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
43 /* only for userspace compatibility */
45 /* Generic cache responses from hook functions.
46 <= 0x2000 is used for protocol-flags. */
47 #define NFC_UNKNOWN 0x4000
48 #define NFC_ALTERED 0x8000
50 /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
51 #define NF_VERDICT_BITS 16
82 #ifdef CONFIG_NETFILTER
83 static inline int NF_DROP_GETERR(int verdict)
85 return -(verdict >> NF_VERDICT_QBITS);
88 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
89 const union nf_inet_addr *a2)
91 return a1->all[0] == a2->all[0] &&
92 a1->all[1] == a2->all[1] &&
93 a1->all[2] == a2->all[2] &&
94 a1->all[3] == a2->all[3];
97 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
98 union nf_inet_addr *result,
99 const union nf_inet_addr *mask)
101 result->all[0] = a1->all[0] & mask->all[0];
102 result->all[1] = a1->all[1] & mask->all[1];
103 result->all[2] = a1->all[2] & mask->all[2];
104 result->all[3] = a1->all[3] & mask->all[3];
107 extern void netfilter_init(void);
109 /* Largest hook number + 1 */
110 #define NF_MAX_HOOKS 8
114 typedef unsigned int nf_hookfn(unsigned int hooknum,
116 const struct net_device *in,
117 const struct net_device *out,
118 int (*okfn)(struct sk_buff *));
121 struct list_head list;
123 /* User fills in from here down. */
125 struct module *owner;
127 unsigned int hooknum;
128 /* Hooks are ordered in ascending priority. */
132 struct nf_sockopt_ops {
133 struct list_head list;
137 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
140 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
142 int (*compat_set)(struct sock *sk, int optval,
143 void __user *user, unsigned int len);
147 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
149 int (*compat_get)(struct sock *sk, int optval,
150 void __user *user, int *len);
152 /* Use the module struct to lock set/get code in place */
153 struct module *owner;
156 /* Function to register/unregister hook points. */
157 int nf_register_hook(struct nf_hook_ops *reg);
158 void nf_unregister_hook(struct nf_hook_ops *reg);
159 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
160 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
162 /* Functions to register get/setsockopt ranges (non-inclusive). You
163 need to check permissions yourself! */
164 int nf_register_sockopt(struct nf_sockopt_ops *reg);
165 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
167 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
169 #if defined(CONFIG_JUMP_LABEL)
170 #include <linux/static_key.h>
171 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
172 static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
174 if (__builtin_constant_p(pf) &&
175 __builtin_constant_p(hook))
176 return static_key_false(&nf_hooks_needed[pf][hook]);
178 return !list_empty(&nf_hooks[pf][hook]);
181 static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
183 return !list_empty(&nf_hooks[pf][hook]);
187 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
188 struct net_device *indev, struct net_device *outdev,
189 int (*okfn)(struct sk_buff *), int thresh);
192 * nf_hook_thresh - call a netfilter hook
194 * Returns 1 if the hook has allowed the packet to pass. The function
195 * okfn must be invoked by the caller in this case. Any other return
196 * value indicates the packet has been consumed by the hook.
198 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
200 struct net_device *indev,
201 struct net_device *outdev,
202 int (*okfn)(struct sk_buff *), int thresh)
204 if (nf_hooks_active(pf, hook))
205 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
209 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
210 struct net_device *indev, struct net_device *outdev,
211 int (*okfn)(struct sk_buff *))
213 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
216 /* Activate hook; either okfn or kfree_skb called, unless a hook
217 returns NF_STOLEN (in which case, it's up to the hook to deal with
220 Returns -ERRNO if packet dropped. Zero means queued, stolen or
225 > I don't want nf_hook to return anything because people might forget
226 > about async and trust the return value to mean "packet was ok".
229 Just document it clearly, then you can expect some sense from kernel
234 NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
235 struct net_device *in, struct net_device *out,
236 int (*okfn)(struct sk_buff *), int thresh)
238 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
245 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
246 struct net_device *in, struct net_device *out,
247 int (*okfn)(struct sk_buff *), bool cond)
252 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
258 NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
259 struct net_device *in, struct net_device *out,
260 int (*okfn)(struct sk_buff *))
262 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
265 /* Call setsockopt() */
266 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
268 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
271 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
272 char __user *opt, unsigned int len);
273 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
274 char __user *opt, int *len);
277 /* Call this before modifying an existing packet: ensures it is
278 modifiable and linear to the point you care about (writable_len).
279 Returns true or false. */
280 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
283 struct nf_queue_entry;
286 unsigned short family;
287 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
288 unsigned int dataoff, u_int8_t protocol);
289 __sum16 (*checksum_partial)(struct sk_buff *skb,
291 unsigned int dataoff,
294 int (*route)(struct net *net, struct dst_entry **dst,
295 struct flowi *fl, bool strict);
296 void (*saveroute)(const struct sk_buff *skb,
297 struct nf_queue_entry *entry);
298 int (*reroute)(struct sk_buff *skb,
299 const struct nf_queue_entry *entry);
303 extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
304 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
306 return rcu_dereference(nf_afinfo[family]);
309 static inline __sum16
310 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
311 u_int8_t protocol, unsigned short family)
313 const struct nf_afinfo *afinfo;
317 afinfo = nf_get_afinfo(family);
319 csum = afinfo->checksum(skb, hook, dataoff, protocol);
324 static inline __sum16
325 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
326 unsigned int dataoff, unsigned int len,
327 u_int8_t protocol, unsigned short family)
329 const struct nf_afinfo *afinfo;
333 afinfo = nf_get_afinfo(family);
335 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
341 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
342 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
344 #include <net/flow.h>
345 extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
348 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
350 #ifdef CONFIG_NF_NAT_NEEDED
351 void (*decodefn)(struct sk_buff *, struct flowi *);
354 decodefn = rcu_dereference(nf_nat_decode_session_hook);
361 #ifdef CONFIG_PROC_FS
362 #include <linux/proc_fs.h>
363 extern struct proc_dir_entry *proc_net_netfilter;
366 #else /* !CONFIG_NETFILTER */
367 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
368 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
369 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
371 struct net_device *indev,
372 struct net_device *outdev,
373 int (*okfn)(struct sk_buff *), int thresh)
377 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
378 struct net_device *indev, struct net_device *outdev,
379 int (*okfn)(struct sk_buff *))
385 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
388 #endif /*CONFIG_NETFILTER*/
390 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
391 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
392 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
393 extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
399 size_t (*build_size)(const struct nf_conn *ct);
400 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
401 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
403 extern struct nfq_ct_hook __rcu *nfq_ct_hook;
405 struct nfq_ct_nat_hook {
406 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
407 u32 ctinfo, int off);
409 extern struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook;
411 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
414 #endif /*__KERNEL__*/
415 #endif /*__LINUX_NETFILTER_H*/