1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/types.h>
7 #include <linux/skbuff.h>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.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 /* only for userspace compatibility */
38 /* Generic cache responses from hook functions.
39 <= 0x2000 is used for protocol-flags. */
40 #define NFC_UNKNOWN 0x4000
41 #define NFC_ALTERED 0x8000
62 #ifdef CONFIG_NETFILTER
64 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
65 const union nf_inet_addr *a2)
67 return a1->all[0] == a2->all[0] &&
68 a1->all[1] == a2->all[1] &&
69 a1->all[2] == a2->all[2] &&
70 a1->all[3] == a2->all[3];
73 extern void netfilter_init(void);
75 /* Largest hook number + 1 */
76 #define NF_MAX_HOOKS 8
81 typedef unsigned int nf_hookfn(unsigned int hooknum,
83 const struct net_device *in,
84 const struct net_device *out,
85 int (*okfn)(struct sk_buff *));
89 struct list_head list;
91 /* User fills in from here down. */
96 /* Hooks are ordered in ascending priority. */
100 struct nf_sockopt_ops
102 struct list_head list;
106 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
109 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
110 int (*compat_set)(struct sock *sk, int optval,
111 void __user *user, unsigned int len);
115 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
116 int (*compat_get)(struct sock *sk, int optval,
117 void __user *user, int *len);
119 /* Use the module struct to lock set/get code in place */
120 struct module *owner;
123 /* Function to register/unregister hook points. */
124 int nf_register_hook(struct nf_hook_ops *reg);
125 void nf_unregister_hook(struct nf_hook_ops *reg);
126 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
127 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
129 /* Functions to register get/setsockopt ranges (non-inclusive). You
130 need to check permissions yourself! */
131 int nf_register_sockopt(struct nf_sockopt_ops *reg);
132 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
135 /* Sysctl registration */
136 extern struct ctl_path nf_net_netfilter_sysctl_path[];
137 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
138 #endif /* CONFIG_SYSCTL */
140 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
142 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
143 struct net_device *indev, struct net_device *outdev,
144 int (*okfn)(struct sk_buff *), int thresh);
147 * nf_hook_thresh - call a netfilter hook
149 * Returns 1 if the hook has allowed the packet to pass. The function
150 * okfn must be invoked by the caller in this case. Any other return
151 * value indicates the packet has been consumed by the hook.
153 static inline int nf_hook_thresh(int pf, unsigned int hook,
155 struct net_device *indev,
156 struct net_device *outdev,
157 int (*okfn)(struct sk_buff *), int thresh,
162 #ifndef CONFIG_NETFILTER_DEBUG
163 if (list_empty(&nf_hooks[pf][hook]))
166 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
169 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
170 struct net_device *indev, struct net_device *outdev,
171 int (*okfn)(struct sk_buff *))
173 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
176 /* Activate hook; either okfn or kfree_skb called, unless a hook
177 returns NF_STOLEN (in which case, it's up to the hook to deal with
180 Returns -ERRNO if packet dropped. Zero means queued, stolen or
185 > I don't want nf_hook to return anything because people might forget
186 > about async and trust the return value to mean "packet was ok".
189 Just document it clearly, then you can expect some sense from kernel
193 /* This is gross, but inline doesn't cut it for avoiding the function
194 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
196 /* HX: It's slightly less gross now. */
198 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
200 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
201 __ret = (okfn)(skb); \
204 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
206 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
207 __ret = (okfn)(skb); \
210 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
211 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
213 /* Call setsockopt() */
214 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
216 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
219 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
220 char __user *opt, int len);
221 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
222 char __user *opt, int *len);
224 /* Call this before modifying an existing packet: ensures it is
225 modifiable and linear to the point you care about (writable_len).
226 Returns true or false. */
227 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
230 struct nf_queue_entry;
233 unsigned short family;
234 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
235 unsigned int dataoff, u_int8_t protocol);
236 int (*route)(struct dst_entry **dst, struct flowi *fl);
237 void (*saveroute)(const struct sk_buff *skb,
238 struct nf_queue_entry *entry);
239 int (*reroute)(struct sk_buff *skb,
240 const struct nf_queue_entry *entry);
244 extern const struct nf_afinfo *nf_afinfo[NPROTO];
245 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
247 return rcu_dereference(nf_afinfo[family]);
250 static inline __sum16
251 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
252 u_int8_t protocol, unsigned short family)
254 const struct nf_afinfo *afinfo;
258 afinfo = nf_get_afinfo(family);
260 csum = afinfo->checksum(skb, hook, dataoff, protocol);
265 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
266 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
268 #include <net/flow.h>
269 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
272 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
274 #ifdef CONFIG_NF_NAT_NEEDED
275 void (*decodefn)(struct sk_buff *, struct flowi *);
277 if (family == AF_INET) {
279 decodefn = rcu_dereference(ip_nat_decode_session);
287 #ifdef CONFIG_PROC_FS
288 #include <linux/proc_fs.h>
289 extern struct proc_dir_entry *proc_net_netfilter;
292 #else /* !CONFIG_NETFILTER */
293 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
294 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
295 static inline int nf_hook_thresh(int pf, unsigned int hook,
297 struct net_device *indev,
298 struct net_device *outdev,
299 int (*okfn)(struct sk_buff *), int thresh,
304 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
305 struct net_device *indev, struct net_device *outdev,
306 int (*okfn)(struct sk_buff *))
312 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
313 #endif /*CONFIG_NETFILTER*/
315 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
316 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
317 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
318 extern void (*nf_ct_destroy)(struct nf_conntrack *);
320 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
323 #endif /*__KERNEL__*/
324 #endif /*__LINUX_NETFILTER_H*/