Merge commit 'v2.6.26' into bkl-removal
[pandora-kernel.git] / include / linux / netfilter.h
1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
3
4 #ifdef __KERNEL__
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
7 #include <linux/net.h>
8 #include <linux/netdevice.h>
9 #include <linux/if.h>
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <net/net_namespace.h>
15 #endif
16 #include <linux/types.h>
17 #include <linux/compiler.h>
18
19 /* Responses from hook functions. */
20 #define NF_DROP 0
21 #define NF_ACCEPT 1
22 #define NF_STOLEN 2
23 #define NF_QUEUE 3
24 #define NF_REPEAT 4
25 #define NF_STOP 5
26 #define NF_MAX_VERDICT NF_STOP
27
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
32
33 #define NF_VERDICT_QMASK 0xffff0000
34 #define NF_VERDICT_QBITS 16
35
36 #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
37
38 /* only for userspace compatibility */
39 #ifndef __KERNEL__
40 /* Generic cache responses from hook functions.
41    <= 0x2000 is used for protocol-flags. */
42 #define NFC_UNKNOWN 0x4000
43 #define NFC_ALTERED 0x8000
44 #endif
45
46 enum nf_inet_hooks {
47         NF_INET_PRE_ROUTING,
48         NF_INET_LOCAL_IN,
49         NF_INET_FORWARD,
50         NF_INET_LOCAL_OUT,
51         NF_INET_POST_ROUTING,
52         NF_INET_NUMHOOKS
53 };
54
55 union nf_inet_addr {
56         __u32           all[4];
57         __be32          ip;
58         __be32          ip6[4];
59         struct in_addr  in;
60         struct in6_addr in6;
61 };
62
63 #ifdef __KERNEL__
64 #ifdef CONFIG_NETFILTER
65
66 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
67                                    const union nf_inet_addr *a2)
68 {
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];
73 }
74
75 extern void netfilter_init(void);
76
77 /* Largest hook number + 1 */
78 #define NF_MAX_HOOKS 8
79
80 struct sk_buff;
81
82 typedef unsigned int nf_hookfn(unsigned int hooknum,
83                                struct sk_buff *skb,
84                                const struct net_device *in,
85                                const struct net_device *out,
86                                int (*okfn)(struct sk_buff *));
87
88 struct nf_hook_ops
89 {
90         struct list_head list;
91
92         /* User fills in from here down. */
93         nf_hookfn *hook;
94         struct module *owner;
95         int pf;
96         int hooknum;
97         /* Hooks are ordered in ascending priority. */
98         int priority;
99 };
100
101 struct nf_sockopt_ops
102 {
103         struct list_head list;
104
105         int pf;
106
107         /* Non-inclusive ranges: use 0/0/NULL to never get called. */
108         int set_optmin;
109         int set_optmax;
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);
113
114         int get_optmin;
115         int get_optmax;
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);
119
120         /* Use the module struct to lock set/get code in place */
121         struct module *owner;
122 };
123
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);
129
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);
134
135 #ifdef CONFIG_SYSCTL
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 */
140
141 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
142
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);
146
147 /**
148  *      nf_hook_thresh - call a netfilter hook
149  *      
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.
153  */
154 static inline int nf_hook_thresh(int pf, unsigned int hook,
155                                  struct sk_buff *skb,
156                                  struct net_device *indev,
157                                  struct net_device *outdev,
158                                  int (*okfn)(struct sk_buff *), int thresh,
159                                  int cond)
160 {
161         if (!cond)
162                 return 1;
163 #ifndef CONFIG_NETFILTER_DEBUG
164         if (list_empty(&nf_hooks[pf][hook]))
165                 return 1;
166 #endif
167         return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
168 }
169
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 *))
173 {
174         return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
175 }
176                    
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
179    the consequences).
180
181    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
182    accepted.
183 */
184
185 /* RR:
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".
188
189    AK:
190    Just document it clearly, then you can expect some sense from kernel
191    coders :)
192 */
193
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 */
196
197 /* HX: It's slightly less gross now. */
198
199 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh)             \
200 ({int __ret;                                                                   \
201 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
202         __ret = (okfn)(skb);                                                   \
203 __ret;})
204
205 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond)                 \
206 ({int __ret;                                                                   \
207 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
208         __ret = (okfn)(skb);                                                   \
209 __ret;})
210
211 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
212         NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
213
214 /* Call setsockopt() */
215 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, 
216                   int len);
217 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
218                   int *len);
219
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);
224
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);
229
230 struct flowi;
231 struct nf_queue_entry;
232
233 struct nf_afinfo {
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,
238                                             unsigned int hook,
239                                             unsigned int dataoff,
240                                             unsigned int len,
241                                             u_int8_t protocol);
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);
247         int             route_key_size;
248 };
249
250 extern const struct nf_afinfo *nf_afinfo[NPROTO];
251 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
252 {
253         return rcu_dereference(nf_afinfo[family]);
254 }
255
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)
259 {
260         const struct nf_afinfo *afinfo;
261         __sum16 csum = 0;
262
263         rcu_read_lock();
264         afinfo = nf_get_afinfo(family);
265         if (afinfo)
266                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
267         rcu_read_unlock();
268         return csum;
269 }
270
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)
275 {
276         const struct nf_afinfo *afinfo;
277         __sum16 csum = 0;
278
279         rcu_read_lock();
280         afinfo = nf_get_afinfo(family);
281         if (afinfo)
282                 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
283                                                 protocol);
284         rcu_read_unlock();
285         return csum;
286 }
287
288 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
289 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
290
291 #include <net/flow.h>
292 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
293
294 static inline void
295 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
296 {
297 #ifdef CONFIG_NF_NAT_NEEDED
298         void (*decodefn)(struct sk_buff *, struct flowi *);
299
300         if (family == AF_INET) {
301                 rcu_read_lock();
302                 decodefn = rcu_dereference(ip_nat_decode_session);
303                 if (decodefn)
304                         decodefn(skb, fl);
305                 rcu_read_unlock();
306         }
307 #endif
308 }
309
310 #ifdef CONFIG_PROC_FS
311 #include <linux/proc_fs.h>
312 extern struct proc_dir_entry *proc_net_netfilter;
313 #endif
314
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,
319                                  struct sk_buff *skb,
320                                  struct net_device *indev,
321                                  struct net_device *outdev,
322                                  int (*okfn)(struct sk_buff *), int thresh,
323                                  int cond)
324 {
325         return okfn(skb);
326 }
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 *))
330 {
331         return 1;
332 }
333 struct flowi;
334 static inline void
335 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
336 #endif /*CONFIG_NETFILTER*/
337
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 *);
342 #else
343 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
344 #endif
345
346 static inline struct net *nf_pre_routing_net(const struct net_device *in,
347                                              const struct net_device *out)
348 {
349 #ifdef CONFIG_NET_NS
350         return in->nd_net;
351 #else
352         return &init_net;
353 #endif
354 }
355
356 static inline struct net *nf_local_in_net(const struct net_device *in,
357                                           const struct net_device *out)
358 {
359 #ifdef CONFIG_NET_NS
360         return in->nd_net;
361 #else
362         return &init_net;
363 #endif
364 }
365
366 static inline struct net *nf_forward_net(const struct net_device *in,
367                                          const struct net_device *out)
368 {
369 #ifdef CONFIG_NET_NS
370         BUG_ON(in->nd_net != out->nd_net);
371         return in->nd_net;
372 #else
373         return &init_net;
374 #endif
375 }
376
377 static inline struct net *nf_local_out_net(const struct net_device *in,
378                                            const struct net_device *out)
379 {
380 #ifdef CONFIG_NET_NS
381         return out->nd_net;
382 #else
383         return &init_net;
384 #endif
385 }
386
387 static inline struct net *nf_post_routing_net(const struct net_device *in,
388                                               const struct net_device *out)
389 {
390 #ifdef CONFIG_NET_NS
391         return out->nd_net;
392 #else
393         return &init_net;
394 #endif
395 }
396
397 #endif /*__KERNEL__*/
398 #endif /*__LINUX_NETFILTER_H*/