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>
17 /* Responses from hook functions. */
24 #define NF_MAX_VERDICT NF_STOP
26 /* we overload the higher bits for encoding auxiliary data such as the queue
27 * number. Not nice, but better than additional function arguments. */
28 #define NF_VERDICT_MASK 0x0000ffff
29 #define NF_VERDICT_BITS 16
31 #define NF_VERDICT_QMASK 0xffff0000
32 #define NF_VERDICT_QBITS 16
34 #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
36 #define NF_DROP_ERR(x) (((-x) << NF_VERDICT_BITS) | NF_DROP)
38 /* only for userspace compatibility */
40 /* Generic cache responses from hook functions.
41 <= 0x2000 is used for protocol-flags. */
42 #define NFC_UNKNOWN 0x4000
43 #define NFC_ALTERED 0x8000
74 #ifdef CONFIG_NETFILTER
76 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
77 const union nf_inet_addr *a2)
79 return a1->all[0] == a2->all[0] &&
80 a1->all[1] == a2->all[1] &&
81 a1->all[2] == a2->all[2] &&
82 a1->all[3] == a2->all[3];
85 extern void netfilter_init(void);
87 /* Largest hook number + 1 */
88 #define NF_MAX_HOOKS 8
92 typedef unsigned int nf_hookfn(unsigned int hooknum,
94 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *));
99 struct list_head list;
101 /* User fills in from here down. */
103 struct module *owner;
105 unsigned int hooknum;
106 /* Hooks are ordered in ascending priority. */
110 struct nf_sockopt_ops {
111 struct list_head list;
115 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
118 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
120 int (*compat_set)(struct sock *sk, int optval,
121 void __user *user, unsigned int len);
125 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
127 int (*compat_get)(struct sock *sk, int optval,
128 void __user *user, int *len);
130 /* Use the module struct to lock set/get code in place */
131 struct module *owner;
134 /* Function to register/unregister hook points. */
135 int nf_register_hook(struct nf_hook_ops *reg);
136 void nf_unregister_hook(struct nf_hook_ops *reg);
137 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
138 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
140 /* Functions to register get/setsockopt ranges (non-inclusive). You
141 need to check permissions yourself! */
142 int nf_register_sockopt(struct nf_sockopt_ops *reg);
143 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
146 /* Sysctl registration */
147 extern struct ctl_path nf_net_netfilter_sysctl_path[];
148 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
149 #endif /* CONFIG_SYSCTL */
151 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
153 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
154 struct net_device *indev, struct net_device *outdev,
155 int (*okfn)(struct sk_buff *), int thresh);
158 * nf_hook_thresh - call a netfilter hook
160 * Returns 1 if the hook has allowed the packet to pass. The function
161 * okfn must be invoked by the caller in this case. Any other return
162 * value indicates the packet has been consumed by the hook.
164 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
166 struct net_device *indev,
167 struct net_device *outdev,
168 int (*okfn)(struct sk_buff *), int thresh)
170 #ifndef CONFIG_NETFILTER_DEBUG
171 if (list_empty(&nf_hooks[pf][hook]))
174 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
177 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
178 struct net_device *indev, struct net_device *outdev,
179 int (*okfn)(struct sk_buff *))
181 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
184 /* Activate hook; either okfn or kfree_skb called, unless a hook
185 returns NF_STOLEN (in which case, it's up to the hook to deal with
188 Returns -ERRNO if packet dropped. Zero means queued, stolen or
193 > I don't want nf_hook to return anything because people might forget
194 > about async and trust the return value to mean "packet was ok".
197 Just document it clearly, then you can expect some sense from kernel
202 NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
203 struct net_device *in, struct net_device *out,
204 int (*okfn)(struct sk_buff *), int thresh)
206 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
213 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
214 struct net_device *in, struct net_device *out,
215 int (*okfn)(struct sk_buff *), bool cond)
220 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
226 NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
227 struct net_device *in, struct net_device *out,
228 int (*okfn)(struct sk_buff *))
230 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
233 /* Call setsockopt() */
234 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
236 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
239 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
240 char __user *opt, unsigned int len);
241 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
242 char __user *opt, int *len);
245 /* Call this before modifying an existing packet: ensures it is
246 modifiable and linear to the point you care about (writable_len).
247 Returns true or false. */
248 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
251 struct nf_queue_entry;
254 unsigned short family;
255 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
256 unsigned int dataoff, u_int8_t protocol);
257 __sum16 (*checksum_partial)(struct sk_buff *skb,
259 unsigned int dataoff,
262 int (*route)(struct dst_entry **dst, struct flowi *fl);
263 void (*saveroute)(const struct sk_buff *skb,
264 struct nf_queue_entry *entry);
265 int (*reroute)(struct sk_buff *skb,
266 const struct nf_queue_entry *entry);
270 extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
271 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
273 return rcu_dereference(nf_afinfo[family]);
276 static inline __sum16
277 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
278 u_int8_t protocol, unsigned short family)
280 const struct nf_afinfo *afinfo;
284 afinfo = nf_get_afinfo(family);
286 csum = afinfo->checksum(skb, hook, dataoff, protocol);
291 static inline __sum16
292 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
293 unsigned int dataoff, unsigned int len,
294 u_int8_t protocol, unsigned short family)
296 const struct nf_afinfo *afinfo;
300 afinfo = nf_get_afinfo(family);
302 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
308 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
309 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
311 #include <net/flow.h>
312 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
315 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
317 #ifdef CONFIG_NF_NAT_NEEDED
318 void (*decodefn)(struct sk_buff *, struct flowi *);
320 if (family == AF_INET) {
322 decodefn = rcu_dereference(ip_nat_decode_session);
330 #ifdef CONFIG_PROC_FS
331 #include <linux/proc_fs.h>
332 extern struct proc_dir_entry *proc_net_netfilter;
335 #else /* !CONFIG_NETFILTER */
336 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
337 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
338 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
340 struct net_device *indev,
341 struct net_device *outdev,
342 int (*okfn)(struct sk_buff *), int thresh)
346 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
347 struct net_device *indev, struct net_device *outdev,
348 int (*okfn)(struct sk_buff *))
354 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
357 #endif /*CONFIG_NETFILTER*/
359 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
360 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
361 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
362 extern void (*nf_ct_destroy)(struct nf_conntrack *);
364 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
367 #endif /*__KERNEL__*/
368 #endif /*__LINUX_NETFILTER_H*/