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>
10 #include <linux/wait.h>
11 #include <linux/list.h>
13 #include <linux/compiler.h>
15 /* Responses from hook functions. */
22 #define NF_MAX_VERDICT NF_STOP
24 /* we overload the higher bits for encoding auxiliary data such as the queue
25 * number. Not nice, but better than additional function arguments. */
26 #define NF_VERDICT_MASK 0x0000ffff
27 #define NF_VERDICT_BITS 16
29 #define NF_VERDICT_QMASK 0xffff0000
30 #define NF_VERDICT_QBITS 16
32 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
34 /* only for userspace compatibility */
36 /* Generic cache responses from hook functions.
37 <= 0x2000 is used for protocol-flags. */
38 #define NFC_UNKNOWN 0x4000
39 #define NFC_ALTERED 0x8000
52 #ifdef CONFIG_NETFILTER
54 extern void netfilter_init(void);
56 /* Largest hook number + 1 */
57 #define NF_MAX_HOOKS 8
62 typedef unsigned int nf_hookfn(unsigned int hooknum,
64 const struct net_device *in,
65 const struct net_device *out,
66 int (*okfn)(struct sk_buff *));
70 struct list_head list;
72 /* User fills in from here down. */
77 /* Hooks are ordered in ascending priority. */
83 struct list_head list;
87 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
90 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
91 int (*compat_set)(struct sock *sk, int optval,
92 void __user *user, unsigned int len);
96 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
97 int (*compat_get)(struct sock *sk, int optval,
98 void __user *user, int *len);
100 /* Use the module struct to lock set/get code in place */
101 struct module *owner;
104 /* Function to register/unregister hook points. */
105 int nf_register_hook(struct nf_hook_ops *reg);
106 void nf_unregister_hook(struct nf_hook_ops *reg);
107 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
108 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
110 /* Functions to register get/setsockopt ranges (non-inclusive). You
111 need to check permissions yourself! */
112 int nf_register_sockopt(struct nf_sockopt_ops *reg);
113 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
116 /* Sysctl registration */
117 struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
118 struct ctl_table *table);
119 void nf_unregister_sysctl_table(struct ctl_table_header *header,
120 struct ctl_table *table);
121 extern struct ctl_table nf_net_netfilter_sysctl_path[];
122 extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
123 #endif /* CONFIG_SYSCTL */
125 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
127 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
128 struct net_device *indev, struct net_device *outdev,
129 int (*okfn)(struct sk_buff *), int thresh);
132 * nf_hook_thresh - call a netfilter hook
134 * Returns 1 if the hook has allowed the packet to pass. The function
135 * okfn must be invoked by the caller in this case. Any other return
136 * value indicates the packet has been consumed by the hook.
138 static inline int nf_hook_thresh(int pf, unsigned int hook,
140 struct net_device *indev,
141 struct net_device *outdev,
142 int (*okfn)(struct sk_buff *), int thresh,
147 #ifndef CONFIG_NETFILTER_DEBUG
148 if (list_empty(&nf_hooks[pf][hook]))
151 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
154 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
155 struct net_device *indev, struct net_device *outdev,
156 int (*okfn)(struct sk_buff *))
158 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
161 /* Activate hook; either okfn or kfree_skb called, unless a hook
162 returns NF_STOLEN (in which case, it's up to the hook to deal with
165 Returns -ERRNO if packet dropped. Zero means queued, stolen or
170 > I don't want nf_hook to return anything because people might forget
171 > about async and trust the return value to mean "packet was ok".
174 Just document it clearly, then you can expect some sense from kernel
178 /* This is gross, but inline doesn't cut it for avoiding the function
179 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
181 /* HX: It's slightly less gross now. */
183 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
185 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
186 __ret = (okfn)(skb); \
189 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
191 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
192 __ret = (okfn)(skb); \
195 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
196 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
198 /* Call setsockopt() */
199 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
201 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
204 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
205 char __user *opt, int len);
206 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
207 char __user *opt, int *len);
209 /* FIXME: Before cache is ever used, this must be implemented for real. */
210 extern void nf_invalidate_cache(int pf);
212 /* Call this before modifying an existing packet: ensures it is
213 modifiable and linear to the point you care about (writable_len).
214 Returns true or false. */
215 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
218 struct nf_queue_entry;
221 unsigned short family;
222 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
223 unsigned int dataoff, u_int8_t protocol);
224 int (*route)(struct dst_entry **dst, struct flowi *fl);
225 void (*saveroute)(const struct sk_buff *skb,
226 struct nf_queue_entry *entry);
227 int (*reroute)(struct sk_buff *skb,
228 const struct nf_queue_entry *entry);
232 extern struct nf_afinfo *nf_afinfo[];
233 static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
235 return rcu_dereference(nf_afinfo[family]);
238 static inline __sum16
239 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
240 u_int8_t protocol, unsigned short family)
242 struct nf_afinfo *afinfo;
246 afinfo = nf_get_afinfo(family);
248 csum = afinfo->checksum(skb, hook, dataoff, protocol);
253 extern int nf_register_afinfo(struct nf_afinfo *afinfo);
254 extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
256 #include <net/flow.h>
257 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
260 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
262 #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
263 void (*decodefn)(struct sk_buff *, struct flowi *);
265 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
270 #ifdef CONFIG_PROC_FS
271 #include <linux/proc_fs.h>
272 extern struct proc_dir_entry *proc_net_netfilter;
275 #else /* !CONFIG_NETFILTER */
276 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
277 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
278 static inline int nf_hook_thresh(int pf, unsigned int hook,
280 struct net_device *indev,
281 struct net_device *outdev,
282 int (*okfn)(struct sk_buff *), int thresh,
287 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
288 struct net_device *indev, struct net_device *outdev,
289 int (*okfn)(struct sk_buff *))
295 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
296 #endif /*CONFIG_NETFILTER*/
298 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
299 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
300 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
301 extern void (*nf_ct_destroy)(struct nf_conntrack *);
303 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
306 #endif /*__KERNEL__*/
307 #endif /*__LINUX_NETFILTER_H*/