]> Pileus Git - ~andy/linux/blob - include/linux/netfilter.h
netfilter.h needs rcuupdate.h for RCU locking functions
[~andy/linux] / 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/if.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
12 #endif
13 #include <linux/compiler.h>
14 #include <linux/rcupdate.h>
15
16 /* Responses from hook functions. */
17 #define NF_DROP 0
18 #define NF_ACCEPT 1
19 #define NF_STOLEN 2
20 #define NF_QUEUE 3
21 #define NF_REPEAT 4
22 #define NF_STOP 5
23 #define NF_MAX_VERDICT NF_STOP
24
25 /* we overload the higher bits for encoding auxiliary data such as the queue
26  * number. Not nice, but better than additional function arguments. */
27 #define NF_VERDICT_MASK 0x0000ffff
28 #define NF_VERDICT_BITS 16
29
30 #define NF_VERDICT_QMASK 0xffff0000
31 #define NF_VERDICT_QBITS 16
32
33 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
34
35 /* only for userspace compatibility */
36 #ifndef __KERNEL__
37 /* Generic cache responses from hook functions.
38    <= 0x2000 is used for protocol-flags. */
39 #define NFC_UNKNOWN 0x4000
40 #define NFC_ALTERED 0x8000
41 #endif
42
43 #ifdef __KERNEL__
44 #ifdef CONFIG_NETFILTER
45
46 extern void netfilter_init(void);
47
48 /* Largest hook number + 1 */
49 #define NF_MAX_HOOKS 8
50
51 struct sk_buff;
52 struct net_device;
53
54 typedef unsigned int nf_hookfn(unsigned int hooknum,
55                                struct sk_buff **skb,
56                                const struct net_device *in,
57                                const struct net_device *out,
58                                int (*okfn)(struct sk_buff *));
59
60 struct nf_hook_ops
61 {
62         struct list_head list;
63
64         /* User fills in from here down. */
65         nf_hookfn *hook;
66         struct module *owner;
67         int pf;
68         int hooknum;
69         /* Hooks are ordered in ascending priority. */
70         int priority;
71 };
72
73 struct nf_sockopt_ops
74 {
75         struct list_head list;
76
77         int pf;
78
79         /* Non-inclusive ranges: use 0/0/NULL to never get called. */
80         int set_optmin;
81         int set_optmax;
82         int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
83         int (*compat_set)(struct sock *sk, int optval,
84                         void __user *user, unsigned int len);
85
86         int get_optmin;
87         int get_optmax;
88         int (*get)(struct sock *sk, int optval, void __user *user, int *len);
89         int (*compat_get)(struct sock *sk, int optval,
90                         void __user *user, int *len);
91
92         /* Number of users inside set() or get(). */
93         unsigned int use;
94         struct task_struct *cleanup_task;
95 };
96
97 /* Each queued (to userspace) skbuff has one of these. */
98 struct nf_info
99 {
100         /* The ops struct which sent us to userspace. */
101         struct nf_hook_ops *elem;
102         
103         /* If we're sent to userspace, this keeps housekeeping info */
104         int pf;
105         unsigned int hook;
106         struct net_device *indev, *outdev;
107         int (*okfn)(struct sk_buff *);
108 };
109                                                                                 
110 /* Function to register/unregister hook points. */
111 int nf_register_hook(struct nf_hook_ops *reg);
112 void nf_unregister_hook(struct nf_hook_ops *reg);
113 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
114 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
115
116 /* Functions to register get/setsockopt ranges (non-inclusive).  You
117    need to check permissions yourself! */
118 int nf_register_sockopt(struct nf_sockopt_ops *reg);
119 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
120
121 #ifdef CONFIG_SYSCTL
122 /* Sysctl registration */
123 struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
124                                                   struct ctl_table *table);
125 void nf_unregister_sysctl_table(struct ctl_table_header *header,
126                                 struct ctl_table *table);
127 extern struct ctl_table nf_net_netfilter_sysctl_path[];
128 extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
129 #endif /* CONFIG_SYSCTL */
130
131 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
132
133 /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
134  * disappear once iptables is replaced with pkttables.  Please DO NOT use them
135  * for any new code! */
136 #define NF_LOG_TCPSEQ           0x01    /* Log TCP sequence numbers */
137 #define NF_LOG_TCPOPT           0x02    /* Log TCP options */
138 #define NF_LOG_IPOPT            0x04    /* Log IP options */
139 #define NF_LOG_UID              0x08    /* Log UID owning local socket */
140 #define NF_LOG_MASK             0x0f
141
142 #define NF_LOG_TYPE_LOG         0x01
143 #define NF_LOG_TYPE_ULOG        0x02
144
145 struct nf_loginfo {
146         u_int8_t type;
147         union {
148                 struct {
149                         u_int32_t copy_len;
150                         u_int16_t group;
151                         u_int16_t qthreshold;
152                 } ulog;
153                 struct {
154                         u_int8_t level;
155                         u_int8_t logflags;
156                 } log;
157         } u;
158 };
159
160 typedef void nf_logfn(unsigned int pf,
161                       unsigned int hooknum,
162                       const struct sk_buff *skb,
163                       const struct net_device *in,
164                       const struct net_device *out,
165                       const struct nf_loginfo *li,
166                       const char *prefix);
167
168 struct nf_logger {
169         struct module   *me;
170         nf_logfn        *logfn;
171         char            *name;
172 };
173
174 /* Function to register/unregister log function. */
175 int nf_log_register(int pf, struct nf_logger *logger);
176 int nf_log_unregister_pf(int pf);
177 void nf_log_unregister_logger(struct nf_logger *logger);
178
179 /* Calls the registered backend logging function */
180 void nf_log_packet(int pf,
181                    unsigned int hooknum,
182                    const struct sk_buff *skb,
183                    const struct net_device *in,
184                    const struct net_device *out,
185                    struct nf_loginfo *li,
186                    const char *fmt, ...);
187
188 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
189                  struct net_device *indev, struct net_device *outdev,
190                  int (*okfn)(struct sk_buff *), int thresh);
191
192 /**
193  *      nf_hook_thresh - call a netfilter hook
194  *      
195  *      Returns 1 if the hook has allowed the packet to pass.  The function
196  *      okfn must be invoked by the caller in this case.  Any other return
197  *      value indicates the packet has been consumed by the hook.
198  */
199 static inline int nf_hook_thresh(int pf, unsigned int hook,
200                                  struct sk_buff **pskb,
201                                  struct net_device *indev,
202                                  struct net_device *outdev,
203                                  int (*okfn)(struct sk_buff *), int thresh,
204                                  int cond)
205 {
206         if (!cond)
207                 return 1;
208 #ifndef CONFIG_NETFILTER_DEBUG
209         if (list_empty(&nf_hooks[pf][hook]))
210                 return 1;
211 #endif
212         return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
213 }
214
215 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
216                           struct net_device *indev, struct net_device *outdev,
217                           int (*okfn)(struct sk_buff *))
218 {
219         return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
220 }
221                    
222 /* Activate hook; either okfn or kfree_skb called, unless a hook
223    returns NF_STOLEN (in which case, it's up to the hook to deal with
224    the consequences).
225
226    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
227    accepted.
228 */
229
230 /* RR:
231    > I don't want nf_hook to return anything because people might forget
232    > about async and trust the return value to mean "packet was ok".
233
234    AK:
235    Just document it clearly, then you can expect some sense from kernel
236    coders :)
237 */
238
239 /* This is gross, but inline doesn't cut it for avoiding the function
240    call in fast path: gcc doesn't inline (needs value tracking?). --RR */
241
242 /* HX: It's slightly less gross now. */
243
244 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh)             \
245 ({int __ret;                                                                   \
246 if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
247         __ret = (okfn)(skb);                                                   \
248 __ret;})
249
250 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond)                 \
251 ({int __ret;                                                                   \
252 if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
253         __ret = (okfn)(skb);                                                   \
254 __ret;})
255
256 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
257         NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
258
259 /* Call setsockopt() */
260 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, 
261                   int len);
262 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
263                   int *len);
264
265 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
266                 char __user *opt, int len);
267 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
268                 char __user *opt, int *len);
269
270 /* Packet queuing */
271 struct nf_queue_handler {
272         int (*outfn)(struct sk_buff *skb, struct nf_info *info,
273                      unsigned int queuenum, void *data);
274         void *data;
275         char *name;
276 };
277 extern int nf_register_queue_handler(int pf, 
278                                      struct nf_queue_handler *qh);
279 extern int nf_unregister_queue_handler(int pf);
280 extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
281 extern void nf_reinject(struct sk_buff *skb,
282                         struct nf_info *info,
283                         unsigned int verdict);
284
285 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
286 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
287
288 /* FIXME: Before cache is ever used, this must be implemented for real. */
289 extern void nf_invalidate_cache(int pf);
290
291 /* Call this before modifying an existing packet: ensures it is
292    modifiable and linear to the point you care about (writable_len).
293    Returns true or false. */
294 extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
295
296 static inline void nf_csum_replace4(__sum16 *sum, __be32 from, __be32 to)
297 {
298         __be32 diff[] = { ~from, to };
299
300         *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), ~csum_unfold(*sum)));
301 }
302
303 static inline void nf_csum_replace2(__sum16 *sum, __be16 from, __be16 to)
304 {
305         nf_csum_replace4(sum, (__force __be32)from, (__force __be32)to);
306 }
307
308 extern void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
309                                       __be32 from, __be32 to, int pseudohdr);
310
311 static inline void nf_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
312                                       __be16 from, __be16 to, int pseudohdr)
313 {
314         nf_proto_csum_replace4(sum, skb, (__force __be32)from,
315                                 (__force __be32)to, pseudohdr);
316 }
317
318 struct nf_afinfo {
319         unsigned short  family;
320         __sum16         (*checksum)(struct sk_buff *skb, unsigned int hook,
321                                     unsigned int dataoff, u_int8_t protocol);
322         void            (*saveroute)(const struct sk_buff *skb,
323                                      struct nf_info *info);
324         int             (*reroute)(struct sk_buff **skb,
325                                    const struct nf_info *info);
326         int             route_key_size;
327 };
328
329 extern struct nf_afinfo *nf_afinfo[];
330 static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
331 {
332         return rcu_dereference(nf_afinfo[family]);
333 }
334
335 static inline __sum16
336 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
337             u_int8_t protocol, unsigned short family)
338 {
339         struct nf_afinfo *afinfo;
340         __sum16 csum = 0;
341
342         rcu_read_lock();
343         afinfo = nf_get_afinfo(family);
344         if (afinfo)
345                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
346         rcu_read_unlock();
347         return csum;
348 }
349
350 extern int nf_register_afinfo(struct nf_afinfo *afinfo);
351 extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
352
353 #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
354
355 #include <net/flow.h>
356 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
357
358 static inline void
359 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
360 {
361 #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
362         void (*decodefn)(struct sk_buff *, struct flowi *);
363
364         if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
365                 decodefn(skb, fl);
366 #endif
367 }
368
369 #ifdef CONFIG_PROC_FS
370 #include <linux/proc_fs.h>
371 extern struct proc_dir_entry *proc_net_netfilter;
372 #endif
373
374 #else /* !CONFIG_NETFILTER */
375 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
376 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
377 static inline int nf_hook_thresh(int pf, unsigned int hook,
378                                  struct sk_buff **pskb,
379                                  struct net_device *indev,
380                                  struct net_device *outdev,
381                                  int (*okfn)(struct sk_buff *), int thresh,
382                                  int cond)
383 {
384         return okfn(*pskb);
385 }
386 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
387                           struct net_device *indev, struct net_device *outdev,
388                           int (*okfn)(struct sk_buff *))
389 {
390         return 1;
391 }
392 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
393 struct flowi;
394 static inline void
395 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
396 #endif /*CONFIG_NETFILTER*/
397
398 #endif /*__KERNEL__*/
399 #endif /*__LINUX_NETFILTER_H*/