1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <net/net_namespace.h>
16 #include <linux/types.h>
17 #include <linux/compiler.h>
19 /* Responses from hook functions. */
26 #define NF_MAX_VERDICT NF_STOP
28 /* we overload the higher bits for encoding auxiliary data such as the queue
29 * number. Not nice, but better than additional function arguments. */
30 #define NF_VERDICT_MASK 0x0000ffff
31 #define NF_VERDICT_BITS 16
33 #define NF_VERDICT_QMASK 0xffff0000
34 #define NF_VERDICT_QBITS 16
36 #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
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
64 #ifdef CONFIG_NETFILTER
66 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
67 const union nf_inet_addr *a2)
69 return a1->all[0] == a2->all[0] &&
70 a1->all[1] == a2->all[1] &&
71 a1->all[2] == a2->all[2] &&
72 a1->all[3] == a2->all[3];
75 extern void netfilter_init(void);
77 /* Largest hook number + 1 */
78 #define NF_MAX_HOOKS 8
82 typedef unsigned int nf_hookfn(unsigned int hooknum,
84 const struct net_device *in,
85 const struct net_device *out,
86 int (*okfn)(struct sk_buff *));
90 struct list_head list;
92 /* User fills in from here down. */
97 /* Hooks are ordered in ascending priority. */
101 struct nf_sockopt_ops
103 struct list_head list;
107 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
110 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
111 int (*compat_set)(struct sock *sk, int optval,
112 void __user *user, unsigned int len);
116 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
117 int (*compat_get)(struct sock *sk, int optval,
118 void __user *user, int *len);
120 /* Use the module struct to lock set/get code in place */
121 struct module *owner;
124 /* Function to register/unregister hook points. */
125 int nf_register_hook(struct nf_hook_ops *reg);
126 void nf_unregister_hook(struct nf_hook_ops *reg);
127 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
128 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
130 /* Functions to register get/setsockopt ranges (non-inclusive). You
131 need to check permissions yourself! */
132 int nf_register_sockopt(struct nf_sockopt_ops *reg);
133 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
136 /* Sysctl registration */
137 extern struct ctl_path nf_net_netfilter_sysctl_path[];
138 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
139 #endif /* CONFIG_SYSCTL */
141 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
143 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
144 struct net_device *indev, struct net_device *outdev,
145 int (*okfn)(struct sk_buff *), int thresh);
148 * nf_hook_thresh - call a netfilter hook
150 * Returns 1 if the hook has allowed the packet to pass. The function
151 * okfn must be invoked by the caller in this case. Any other return
152 * value indicates the packet has been consumed by the hook.
154 static inline int nf_hook_thresh(int pf, unsigned int hook,
156 struct net_device *indev,
157 struct net_device *outdev,
158 int (*okfn)(struct sk_buff *), int thresh,
163 #ifndef CONFIG_NETFILTER_DEBUG
164 if (list_empty(&nf_hooks[pf][hook]))
167 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
170 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
171 struct net_device *indev, struct net_device *outdev,
172 int (*okfn)(struct sk_buff *))
174 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
177 /* Activate hook; either okfn or kfree_skb called, unless a hook
178 returns NF_STOLEN (in which case, it's up to the hook to deal with
181 Returns -ERRNO if packet dropped. Zero means queued, stolen or
186 > I don't want nf_hook to return anything because people might forget
187 > about async and trust the return value to mean "packet was ok".
190 Just document it clearly, then you can expect some sense from kernel
194 /* This is gross, but inline doesn't cut it for avoiding the function
195 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
197 /* HX: It's slightly less gross now. */
199 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
201 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
202 __ret = (okfn)(skb); \
205 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
207 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
208 __ret = (okfn)(skb); \
211 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
212 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
214 /* Call setsockopt() */
215 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
217 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
220 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
221 char __user *opt, int len);
222 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
223 char __user *opt, int *len);
225 /* Call this before modifying an existing packet: ensures it is
226 modifiable and linear to the point you care about (writable_len).
227 Returns true or false. */
228 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
231 struct nf_queue_entry;
234 unsigned short family;
235 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
236 unsigned int dataoff, u_int8_t protocol);
237 __sum16 (*checksum_partial)(struct sk_buff *skb,
239 unsigned int dataoff,
242 int (*route)(struct dst_entry **dst, struct flowi *fl);
243 void (*saveroute)(const struct sk_buff *skb,
244 struct nf_queue_entry *entry);
245 int (*reroute)(struct sk_buff *skb,
246 const struct nf_queue_entry *entry);
250 extern const struct nf_afinfo *nf_afinfo[NPROTO];
251 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
253 return rcu_dereference(nf_afinfo[family]);
256 static inline __sum16
257 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
258 u_int8_t protocol, unsigned short family)
260 const struct nf_afinfo *afinfo;
264 afinfo = nf_get_afinfo(family);
266 csum = afinfo->checksum(skb, hook, dataoff, protocol);
271 static inline __sum16
272 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
273 unsigned int dataoff, unsigned int len,
274 u_int8_t protocol, unsigned short family)
276 const struct nf_afinfo *afinfo;
280 afinfo = nf_get_afinfo(family);
282 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
288 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
289 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
291 #include <net/flow.h>
292 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
295 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
297 #ifdef CONFIG_NF_NAT_NEEDED
298 void (*decodefn)(struct sk_buff *, struct flowi *);
300 if (family == AF_INET) {
302 decodefn = rcu_dereference(ip_nat_decode_session);
310 #ifdef CONFIG_PROC_FS
311 #include <linux/proc_fs.h>
312 extern struct proc_dir_entry *proc_net_netfilter;
315 #else /* !CONFIG_NETFILTER */
316 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
317 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
318 static inline int nf_hook_thresh(int pf, unsigned int hook,
320 struct net_device *indev,
321 struct net_device *outdev,
322 int (*okfn)(struct sk_buff *), int thresh,
327 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
328 struct net_device *indev, struct net_device *outdev,
329 int (*okfn)(struct sk_buff *))
335 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
336 #endif /*CONFIG_NETFILTER*/
338 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
339 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
340 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
341 extern void (*nf_ct_destroy)(struct nf_conntrack *);
343 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
346 static inline struct net *nf_pre_routing_net(const struct net_device *in,
347 const struct net_device *out)
356 static inline struct net *nf_local_in_net(const struct net_device *in,
357 const struct net_device *out)
366 static inline struct net *nf_forward_net(const struct net_device *in,
367 const struct net_device *out)
370 BUG_ON(in->nd_net != out->nd_net);
377 static inline struct net *nf_local_out_net(const struct net_device *in,
378 const struct net_device *out)
387 static inline struct net *nf_post_routing_net(const struct net_device *in,
388 const struct net_device *out)
397 #endif /*__KERNEL__*/
398 #endif /*__LINUX_NETFILTER_H*/