netfilter: Introduce NFPROTO_* constants
[safe/jmp/linux-2.6] / 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 enum {
56         NFPROTO_UNSPEC =  0,
57         NFPROTO_IPV4   =  2,
58         NFPROTO_ARP    =  3,
59         NFPROTO_BRIDGE =  7,
60         NFPROTO_IPV6   = 10,
61         NFPROTO_DECNET = 12,
62         NFPROTO_NUMPROTO,
63 };
64
65 union nf_inet_addr {
66         __u32           all[4];
67         __be32          ip;
68         __be32          ip6[4];
69         struct in_addr  in;
70         struct in6_addr in6;
71 };
72
73 #ifdef __KERNEL__
74 #ifdef CONFIG_NETFILTER
75
76 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
77                                    const union nf_inet_addr *a2)
78 {
79         return a1->all[0] == a2->all[0] &&
80                a1->all[1] == a2->all[1] &&
81                a1->all[2] == a2->all[2] &&
82                a1->all[3] == a2->all[3];
83 }
84
85 extern void netfilter_init(void);
86
87 /* Largest hook number + 1 */
88 #define NF_MAX_HOOKS 8
89
90 struct sk_buff;
91
92 typedef unsigned int nf_hookfn(unsigned int hooknum,
93                                struct sk_buff *skb,
94                                const struct net_device *in,
95                                const struct net_device *out,
96                                int (*okfn)(struct sk_buff *));
97
98 struct nf_hook_ops
99 {
100         struct list_head list;
101
102         /* User fills in from here down. */
103         nf_hookfn *hook;
104         struct module *owner;
105         u_int8_t pf;
106         unsigned int hooknum;
107         /* Hooks are ordered in ascending priority. */
108         int priority;
109 };
110
111 struct nf_sockopt_ops
112 {
113         struct list_head list;
114
115         u_int8_t pf;
116
117         /* Non-inclusive ranges: use 0/0/NULL to never get called. */
118         int set_optmin;
119         int set_optmax;
120         int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
121         int (*compat_set)(struct sock *sk, int optval,
122                         void __user *user, unsigned int len);
123
124         int get_optmin;
125         int get_optmax;
126         int (*get)(struct sock *sk, int optval, void __user *user, int *len);
127         int (*compat_get)(struct sock *sk, int optval,
128                         void __user *user, int *len);
129
130         /* Use the module struct to lock set/get code in place */
131         struct module *owner;
132 };
133
134 /* Function to register/unregister hook points. */
135 int nf_register_hook(struct nf_hook_ops *reg);
136 void nf_unregister_hook(struct nf_hook_ops *reg);
137 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
138 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
139
140 /* Functions to register get/setsockopt ranges (non-inclusive).  You
141    need to check permissions yourself! */
142 int nf_register_sockopt(struct nf_sockopt_ops *reg);
143 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
144
145 #ifdef CONFIG_SYSCTL
146 /* Sysctl registration */
147 extern struct ctl_path nf_net_netfilter_sysctl_path[];
148 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
149 #endif /* CONFIG_SYSCTL */
150
151 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
152
153 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
154                  struct net_device *indev, struct net_device *outdev,
155                  int (*okfn)(struct sk_buff *), int thresh);
156
157 /**
158  *      nf_hook_thresh - call a netfilter hook
159  *      
160  *      Returns 1 if the hook has allowed the packet to pass.  The function
161  *      okfn must be invoked by the caller in this case.  Any other return
162  *      value indicates the packet has been consumed by the hook.
163  */
164 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
165                                  struct sk_buff *skb,
166                                  struct net_device *indev,
167                                  struct net_device *outdev,
168                                  int (*okfn)(struct sk_buff *), int thresh,
169                                  int cond)
170 {
171         if (!cond)
172                 return 1;
173 #ifndef CONFIG_NETFILTER_DEBUG
174         if (list_empty(&nf_hooks[pf][hook]))
175                 return 1;
176 #endif
177         return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
178 }
179
180 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
181                           struct net_device *indev, struct net_device *outdev,
182                           int (*okfn)(struct sk_buff *))
183 {
184         return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
185 }
186                    
187 /* Activate hook; either okfn or kfree_skb called, unless a hook
188    returns NF_STOLEN (in which case, it's up to the hook to deal with
189    the consequences).
190
191    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
192    accepted.
193 */
194
195 /* RR:
196    > I don't want nf_hook to return anything because people might forget
197    > about async and trust the return value to mean "packet was ok".
198
199    AK:
200    Just document it clearly, then you can expect some sense from kernel
201    coders :)
202 */
203
204 /* This is gross, but inline doesn't cut it for avoiding the function
205    call in fast path: gcc doesn't inline (needs value tracking?). --RR */
206
207 /* HX: It's slightly less gross now. */
208
209 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh)             \
210 ({int __ret;                                                                   \
211 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
212         __ret = (okfn)(skb);                                                   \
213 __ret;})
214
215 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond)                 \
216 ({int __ret;                                                                   \
217 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
218         __ret = (okfn)(skb);                                                   \
219 __ret;})
220
221 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
222         NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
223
224 /* Call setsockopt() */
225 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
226                   int len);
227 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
228                   int *len);
229
230 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
231                 char __user *opt, int len);
232 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
233                 char __user *opt, int *len);
234
235 /* Call this before modifying an existing packet: ensures it is
236    modifiable and linear to the point you care about (writable_len).
237    Returns true or false. */
238 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
239
240 struct flowi;
241 struct nf_queue_entry;
242
243 struct nf_afinfo {
244         unsigned short  family;
245         __sum16         (*checksum)(struct sk_buff *skb, unsigned int hook,
246                                     unsigned int dataoff, u_int8_t protocol);
247         __sum16         (*checksum_partial)(struct sk_buff *skb,
248                                             unsigned int hook,
249                                             unsigned int dataoff,
250                                             unsigned int len,
251                                             u_int8_t protocol);
252         int             (*route)(struct dst_entry **dst, struct flowi *fl);
253         void            (*saveroute)(const struct sk_buff *skb,
254                                      struct nf_queue_entry *entry);
255         int             (*reroute)(struct sk_buff *skb,
256                                    const struct nf_queue_entry *entry);
257         int             route_key_size;
258 };
259
260 extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
261 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
262 {
263         return rcu_dereference(nf_afinfo[family]);
264 }
265
266 static inline __sum16
267 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
268             u_int8_t protocol, unsigned short family)
269 {
270         const struct nf_afinfo *afinfo;
271         __sum16 csum = 0;
272
273         rcu_read_lock();
274         afinfo = nf_get_afinfo(family);
275         if (afinfo)
276                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
277         rcu_read_unlock();
278         return csum;
279 }
280
281 static inline __sum16
282 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
283                     unsigned int dataoff, unsigned int len,
284                     u_int8_t protocol, unsigned short family)
285 {
286         const struct nf_afinfo *afinfo;
287         __sum16 csum = 0;
288
289         rcu_read_lock();
290         afinfo = nf_get_afinfo(family);
291         if (afinfo)
292                 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
293                                                 protocol);
294         rcu_read_unlock();
295         return csum;
296 }
297
298 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
299 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
300
301 #include <net/flow.h>
302 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
303
304 static inline void
305 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
306 {
307 #ifdef CONFIG_NF_NAT_NEEDED
308         void (*decodefn)(struct sk_buff *, struct flowi *);
309
310         if (family == AF_INET) {
311                 rcu_read_lock();
312                 decodefn = rcu_dereference(ip_nat_decode_session);
313                 if (decodefn)
314                         decodefn(skb, fl);
315                 rcu_read_unlock();
316         }
317 #endif
318 }
319
320 #ifdef CONFIG_PROC_FS
321 #include <linux/proc_fs.h>
322 extern struct proc_dir_entry *proc_net_netfilter;
323 #endif
324
325 #else /* !CONFIG_NETFILTER */
326 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
327 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
328 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
329                                  struct sk_buff *skb,
330                                  struct net_device *indev,
331                                  struct net_device *outdev,
332                                  int (*okfn)(struct sk_buff *), int thresh,
333                                  int cond)
334 {
335         return okfn(skb);
336 }
337 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
338                           struct net_device *indev, struct net_device *outdev,
339                           int (*okfn)(struct sk_buff *))
340 {
341         return 1;
342 }
343 struct flowi;
344 static inline void
345 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
346 {
347 }
348 #endif /*CONFIG_NETFILTER*/
349
350 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
351 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
352 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
353 extern void (*nf_ct_destroy)(struct nf_conntrack *);
354 #else
355 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
356 #endif
357
358 static inline struct net *nf_pre_routing_net(const struct net_device *in,
359                                              const struct net_device *out)
360 {
361 #ifdef CONFIG_NET_NS
362         return in->nd_net;
363 #else
364         return &init_net;
365 #endif
366 }
367
368 static inline struct net *nf_local_in_net(const struct net_device *in,
369                                           const struct net_device *out)
370 {
371 #ifdef CONFIG_NET_NS
372         return in->nd_net;
373 #else
374         return &init_net;
375 #endif
376 }
377
378 static inline struct net *nf_forward_net(const struct net_device *in,
379                                          const struct net_device *out)
380 {
381 #ifdef CONFIG_NET_NS
382         BUG_ON(in->nd_net != out->nd_net);
383         return in->nd_net;
384 #else
385         return &init_net;
386 #endif
387 }
388
389 static inline struct net *nf_local_out_net(const struct net_device *in,
390                                            const struct net_device *out)
391 {
392 #ifdef CONFIG_NET_NS
393         return out->nd_net;
394 #else
395         return &init_net;
396 #endif
397 }
398
399 static inline struct net *nf_post_routing_net(const struct net_device *in,
400                                               const struct net_device *out)
401 {
402 #ifdef CONFIG_NET_NS
403         return out->nd_net;
404 #else
405         return &init_net;
406 #endif
407 }
408
409 #endif /*__KERNEL__*/
410 #endif /*__LINUX_NETFILTER_H*/