]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/netfilter.h
66bc52060fd63f3bf759505cd5cc923965a11332
[linux-2.6-omap-h63xx.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/types.h>
7 #include <linux/skbuff.h>
8 #include <linux/net.h>
9 #include <linux/netdevice.h>
10 #include <linux/if.h>
11 #include <linux/in.h>
12 #include <linux/in6.h>
13 #include <linux/wait.h>
14 #include <linux/list.h>
15 #include <net/net_namespace.h>
16 #endif
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         int             (*route)(struct dst_entry **dst, struct flowi *fl);
238         void            (*saveroute)(const struct sk_buff *skb,
239                                      struct nf_queue_entry *entry);
240         int             (*reroute)(struct sk_buff *skb,
241                                    const struct nf_queue_entry *entry);
242         int             route_key_size;
243 };
244
245 extern const struct nf_afinfo *nf_afinfo[NPROTO];
246 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
247 {
248         return rcu_dereference(nf_afinfo[family]);
249 }
250
251 static inline __sum16
252 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
253             u_int8_t protocol, unsigned short family)
254 {
255         const struct nf_afinfo *afinfo;
256         __sum16 csum = 0;
257
258         rcu_read_lock();
259         afinfo = nf_get_afinfo(family);
260         if (afinfo)
261                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
262         rcu_read_unlock();
263         return csum;
264 }
265
266 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
267 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
268
269 #include <net/flow.h>
270 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
271
272 static inline void
273 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
274 {
275 #ifdef CONFIG_NF_NAT_NEEDED
276         void (*decodefn)(struct sk_buff *, struct flowi *);
277
278         if (family == AF_INET) {
279                 rcu_read_lock();
280                 decodefn = rcu_dereference(ip_nat_decode_session);
281                 if (decodefn)
282                         decodefn(skb, fl);
283                 rcu_read_unlock();
284         }
285 #endif
286 }
287
288 #ifdef CONFIG_PROC_FS
289 #include <linux/proc_fs.h>
290 extern struct proc_dir_entry *proc_net_netfilter;
291 #endif
292
293 #else /* !CONFIG_NETFILTER */
294 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
295 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
296 static inline int nf_hook_thresh(int pf, unsigned int hook,
297                                  struct sk_buff *skb,
298                                  struct net_device *indev,
299                                  struct net_device *outdev,
300                                  int (*okfn)(struct sk_buff *), int thresh,
301                                  int cond)
302 {
303         return okfn(skb);
304 }
305 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
306                           struct net_device *indev, struct net_device *outdev,
307                           int (*okfn)(struct sk_buff *))
308 {
309         return 1;
310 }
311 struct flowi;
312 static inline void
313 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
314 #endif /*CONFIG_NETFILTER*/
315
316 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
317 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
318 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
319 extern void (*nf_ct_destroy)(struct nf_conntrack *);
320 #else
321 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
322 #endif
323
324 static inline struct net *nf_pre_routing_net(const struct net_device *in,
325                                              const struct net_device *out)
326 {
327 #ifdef CONFIG_NET_NS
328         return in->nd_net;
329 #else
330         return &init_net;
331 #endif
332 }
333
334 static inline struct net *nf_local_in_net(const struct net_device *in,
335                                           const struct net_device *out)
336 {
337 #ifdef CONFIG_NET_NS
338         return in->nd_net;
339 #else
340         return &init_net;
341 #endif
342 }
343
344 static inline struct net *nf_forward_net(const struct net_device *in,
345                                          const struct net_device *out)
346 {
347 #ifdef CONFIG_NET_NS
348         BUG_ON(in->nd_net != out->nd_net);
349         return in->nd_net;
350 #else
351         return &init_net;
352 #endif
353 }
354
355 static inline struct net *nf_local_out_net(const struct net_device *in,
356                                            const struct net_device *out)
357 {
358 #ifdef CONFIG_NET_NS
359         return out->nd_net;
360 #else
361         return &init_net;
362 #endif
363 }
364
365 static inline struct net *nf_post_routing_net(const struct net_device *in,
366                                               const struct net_device *out)
367 {
368 #ifdef CONFIG_NET_NS
369         return out->nd_net;
370 #else
371         return &init_net;
372 #endif
373 }
374
375 #endif /*__KERNEL__*/
376 #endif /*__LINUX_NETFILTER_H*/