CAPI: Call a controller 'controller', not 'card'
[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/if.h>
9 #include <linux/in.h>
10 #include <linux/in6.h>
11 #include <linux/wait.h>
12 #include <linux/list.h>
13 #endif
14 #include <linux/types.h>
15 #include <linux/compiler.h>
16
17 /* Responses from hook functions. */
18 #define NF_DROP 0
19 #define NF_ACCEPT 1
20 #define NF_STOLEN 2
21 #define NF_QUEUE 3
22 #define NF_REPEAT 4
23 #define NF_STOP 5
24 #define NF_MAX_VERDICT NF_STOP
25
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
30
31 #define NF_VERDICT_QMASK 0xffff0000
32 #define NF_VERDICT_QBITS 16
33
34 #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
35
36 /* only for userspace compatibility */
37 #ifndef __KERNEL__
38 /* Generic cache responses from hook functions.
39    <= 0x2000 is used for protocol-flags. */
40 #define NFC_UNKNOWN 0x4000
41 #define NFC_ALTERED 0x8000
42 #endif
43
44 enum nf_inet_hooks {
45         NF_INET_PRE_ROUTING,
46         NF_INET_LOCAL_IN,
47         NF_INET_FORWARD,
48         NF_INET_LOCAL_OUT,
49         NF_INET_POST_ROUTING,
50         NF_INET_NUMHOOKS
51 };
52
53 enum {
54         NFPROTO_UNSPEC =  0,
55         NFPROTO_IPV4   =  2,
56         NFPROTO_ARP    =  3,
57         NFPROTO_BRIDGE =  7,
58         NFPROTO_IPV6   = 10,
59         NFPROTO_DECNET = 12,
60         NFPROTO_NUMPROTO,
61 };
62
63 union nf_inet_addr {
64         __u32           all[4];
65         __be32          ip;
66         __be32          ip6[4];
67         struct in_addr  in;
68         struct in6_addr in6;
69 };
70
71 #ifdef __KERNEL__
72 #ifdef CONFIG_NETFILTER
73
74 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
75                                    const union nf_inet_addr *a2)
76 {
77         return a1->all[0] == a2->all[0] &&
78                a1->all[1] == a2->all[1] &&
79                a1->all[2] == a2->all[2] &&
80                a1->all[3] == a2->all[3];
81 }
82
83 extern void netfilter_init(void);
84
85 /* Largest hook number + 1 */
86 #define NF_MAX_HOOKS 8
87
88 struct sk_buff;
89
90 typedef unsigned int nf_hookfn(unsigned int hooknum,
91                                struct sk_buff *skb,
92                                const struct net_device *in,
93                                const struct net_device *out,
94                                int (*okfn)(struct sk_buff *));
95
96 struct nf_hook_ops {
97         struct list_head list;
98
99         /* User fills in from here down. */
100         nf_hookfn *hook;
101         struct module *owner;
102         u_int8_t pf;
103         unsigned int hooknum;
104         /* Hooks are ordered in ascending priority. */
105         int priority;
106 };
107
108 struct nf_sockopt_ops {
109         struct list_head list;
110
111         u_int8_t pf;
112
113         /* Non-inclusive ranges: use 0/0/NULL to never get called. */
114         int set_optmin;
115         int set_optmax;
116         int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
117 #ifdef CONFIG_COMPAT
118         int (*compat_set)(struct sock *sk, int optval,
119                         void __user *user, unsigned int len);
120 #endif
121         int get_optmin;
122         int get_optmax;
123         int (*get)(struct sock *sk, int optval, void __user *user, int *len);
124 #ifdef CONFIG_COMPAT
125         int (*compat_get)(struct sock *sk, int optval,
126                         void __user *user, int *len);
127 #endif
128         /* Use the module struct to lock set/get code in place */
129         struct module *owner;
130 };
131
132 /* Function to register/unregister hook points. */
133 int nf_register_hook(struct nf_hook_ops *reg);
134 void nf_unregister_hook(struct nf_hook_ops *reg);
135 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
136 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
137
138 /* Functions to register get/setsockopt ranges (non-inclusive).  You
139    need to check permissions yourself! */
140 int nf_register_sockopt(struct nf_sockopt_ops *reg);
141 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
142
143 #ifdef CONFIG_SYSCTL
144 /* Sysctl registration */
145 extern struct ctl_path nf_net_netfilter_sysctl_path[];
146 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
147 #endif /* CONFIG_SYSCTL */
148
149 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
150
151 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
152                  struct net_device *indev, struct net_device *outdev,
153                  int (*okfn)(struct sk_buff *), int thresh);
154
155 /**
156  *      nf_hook_thresh - call a netfilter hook
157  *      
158  *      Returns 1 if the hook has allowed the packet to pass.  The function
159  *      okfn must be invoked by the caller in this case.  Any other return
160  *      value indicates the packet has been consumed by the hook.
161  */
162 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
163                                  struct sk_buff *skb,
164                                  struct net_device *indev,
165                                  struct net_device *outdev,
166                                  int (*okfn)(struct sk_buff *), int thresh)
167 {
168 #ifndef CONFIG_NETFILTER_DEBUG
169         if (list_empty(&nf_hooks[pf][hook]))
170                 return 1;
171 #endif
172         return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
173 }
174
175 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
176                           struct net_device *indev, struct net_device *outdev,
177                           int (*okfn)(struct sk_buff *))
178 {
179         return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
180 }
181                    
182 /* Activate hook; either okfn or kfree_skb called, unless a hook
183    returns NF_STOLEN (in which case, it's up to the hook to deal with
184    the consequences).
185
186    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
187    accepted.
188 */
189
190 /* RR:
191    > I don't want nf_hook to return anything because people might forget
192    > about async and trust the return value to mean "packet was ok".
193
194    AK:
195    Just document it clearly, then you can expect some sense from kernel
196    coders :)
197 */
198
199 static inline int
200 NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
201                struct net_device *in, struct net_device *out,
202                int (*okfn)(struct sk_buff *), int thresh)
203 {
204         int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
205         if (ret == 1)
206                 ret = okfn(skb);
207         return ret;
208 }
209
210 static inline int
211 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
212              struct net_device *in, struct net_device *out,
213              int (*okfn)(struct sk_buff *), bool cond)
214 {
215         int ret = 1;
216         if (cond ||
217             (ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN) == 1))
218                 ret = okfn(skb);
219         return ret;
220 }
221
222 static inline int
223 NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
224         struct net_device *in, struct net_device *out,
225         int (*okfn)(struct sk_buff *))
226 {
227         return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
228 }
229
230 /* Call setsockopt() */
231 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
232                   unsigned int len);
233 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
234                   int *len);
235 #ifdef CONFIG_COMPAT
236 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
237                 char __user *opt, unsigned int len);
238 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
239                 char __user *opt, int *len);
240 #endif
241
242 /* Call this before modifying an existing packet: ensures it is
243    modifiable and linear to the point you care about (writable_len).
244    Returns true or false. */
245 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
246
247 struct flowi;
248 struct nf_queue_entry;
249
250 struct nf_afinfo {
251         unsigned short  family;
252         __sum16         (*checksum)(struct sk_buff *skb, unsigned int hook,
253                                     unsigned int dataoff, u_int8_t protocol);
254         __sum16         (*checksum_partial)(struct sk_buff *skb,
255                                             unsigned int hook,
256                                             unsigned int dataoff,
257                                             unsigned int len,
258                                             u_int8_t protocol);
259         int             (*route)(struct dst_entry **dst, struct flowi *fl);
260         void            (*saveroute)(const struct sk_buff *skb,
261                                      struct nf_queue_entry *entry);
262         int             (*reroute)(struct sk_buff *skb,
263                                    const struct nf_queue_entry *entry);
264         int             route_key_size;
265 };
266
267 extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
268 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
269 {
270         return rcu_dereference(nf_afinfo[family]);
271 }
272
273 static inline __sum16
274 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
275             u_int8_t protocol, unsigned short family)
276 {
277         const struct nf_afinfo *afinfo;
278         __sum16 csum = 0;
279
280         rcu_read_lock();
281         afinfo = nf_get_afinfo(family);
282         if (afinfo)
283                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
284         rcu_read_unlock();
285         return csum;
286 }
287
288 static inline __sum16
289 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
290                     unsigned int dataoff, unsigned int len,
291                     u_int8_t protocol, unsigned short family)
292 {
293         const struct nf_afinfo *afinfo;
294         __sum16 csum = 0;
295
296         rcu_read_lock();
297         afinfo = nf_get_afinfo(family);
298         if (afinfo)
299                 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
300                                                 protocol);
301         rcu_read_unlock();
302         return csum;
303 }
304
305 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
306 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
307
308 #include <net/flow.h>
309 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
310
311 static inline void
312 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
313 {
314 #ifdef CONFIG_NF_NAT_NEEDED
315         void (*decodefn)(struct sk_buff *, struct flowi *);
316
317         if (family == AF_INET) {
318                 rcu_read_lock();
319                 decodefn = rcu_dereference(ip_nat_decode_session);
320                 if (decodefn)
321                         decodefn(skb, fl);
322                 rcu_read_unlock();
323         }
324 #endif
325 }
326
327 #ifdef CONFIG_PROC_FS
328 #include <linux/proc_fs.h>
329 extern struct proc_dir_entry *proc_net_netfilter;
330 #endif
331
332 #else /* !CONFIG_NETFILTER */
333 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
334 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
335 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
336                                  struct sk_buff *skb,
337                                  struct net_device *indev,
338                                  struct net_device *outdev,
339                                  int (*okfn)(struct sk_buff *), int thresh)
340 {
341         return okfn(skb);
342 }
343 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
344                           struct net_device *indev, struct net_device *outdev,
345                           int (*okfn)(struct sk_buff *))
346 {
347         return 1;
348 }
349 struct flowi;
350 static inline void
351 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
352 {
353 }
354 #endif /*CONFIG_NETFILTER*/
355
356 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
357 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
358 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
359 extern void (*nf_ct_destroy)(struct nf_conntrack *);
360 #else
361 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
362 #endif
363
364 #endif /*__KERNEL__*/
365 #endif /*__LINUX_NETFILTER_H*/