netfilter: xtables: do centralized checkentry call (1/2)
[safe/jmp/linux-2.6] / include / linux / netfilter / x_tables.h
1 #ifndef _X_TABLES_H
2 #define _X_TABLES_H
3
4 #define XT_FUNCTION_MAXNAMELEN 30
5 #define XT_TABLE_MAXNAMELEN 32
6
7 struct xt_entry_match
8 {
9         union {
10                 struct {
11                         u_int16_t match_size;
12
13                         /* Used by userspace */
14                         char name[XT_FUNCTION_MAXNAMELEN-1];
15
16                         u_int8_t revision;
17                 } user;
18                 struct {
19                         u_int16_t match_size;
20
21                         /* Used inside the kernel */
22                         struct xt_match *match;
23                 } kernel;
24
25                 /* Total length */
26                 u_int16_t match_size;
27         } u;
28
29         unsigned char data[0];
30 };
31
32 struct xt_entry_target
33 {
34         union {
35                 struct {
36                         u_int16_t target_size;
37
38                         /* Used by userspace */
39                         char name[XT_FUNCTION_MAXNAMELEN-1];
40
41                         u_int8_t revision;
42                 } user;
43                 struct {
44                         u_int16_t target_size;
45
46                         /* Used inside the kernel */
47                         struct xt_target *target;
48                 } kernel;
49
50                 /* Total length */
51                 u_int16_t target_size;
52         } u;
53
54         unsigned char data[0];
55 };
56
57 #define XT_TARGET_INIT(__name, __size)                                         \
58 {                                                                              \
59         .target.u.user = {                                                     \
60                 .target_size    = XT_ALIGN(__size),                            \
61                 .name           = __name,                                      \
62         },                                                                     \
63 }
64
65 struct xt_standard_target
66 {
67         struct xt_entry_target target;
68         int verdict;
69 };
70
71 /* The argument to IPT_SO_GET_REVISION_*.  Returns highest revision
72  * kernel supports, if >= revision. */
73 struct xt_get_revision
74 {
75         char name[XT_FUNCTION_MAXNAMELEN-1];
76
77         u_int8_t revision;
78 };
79
80 /* CONTINUE verdict for targets */
81 #define XT_CONTINUE 0xFFFFFFFF
82
83 /* For standard target */
84 #define XT_RETURN (-NF_REPEAT - 1)
85
86 /* this is a dummy structure to find out the alignment requirement for a struct
87  * containing all the fundamental data types that are used in ipt_entry,
88  * ip6t_entry and arpt_entry.  This sucks, and it is a hack.  It will be my
89  * personal pleasure to remove it -HW
90  */
91 struct _xt_align
92 {
93         u_int8_t u8;
94         u_int16_t u16;
95         u_int32_t u32;
96         u_int64_t u64;
97 };
98
99 #define XT_ALIGN(s) (((s) + (__alignof__(struct _xt_align)-1))  \
100                         & ~(__alignof__(struct _xt_align)-1))
101
102 /* Standard return verdict, or do jump. */
103 #define XT_STANDARD_TARGET ""
104 /* Error verdict. */
105 #define XT_ERROR_TARGET "ERROR"
106
107 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
108 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
109
110 struct xt_counters
111 {
112         u_int64_t pcnt, bcnt;                   /* Packet and byte counters */
113 };
114
115 /* The argument to IPT_SO_ADD_COUNTERS. */
116 struct xt_counters_info
117 {
118         /* Which table. */
119         char name[XT_TABLE_MAXNAMELEN];
120
121         unsigned int num_counters;
122
123         /* The counters (actually `number' of these). */
124         struct xt_counters counters[0];
125 };
126
127 #define XT_INV_PROTO            0x40    /* Invert the sense of PROTO. */
128
129 /* fn returns 0 to continue iteration */
130 #define XT_MATCH_ITERATE(type, e, fn, args...)                  \
131 ({                                                              \
132         unsigned int __i;                                       \
133         int __ret = 0;                                          \
134         struct xt_entry_match *__m;                             \
135                                                                 \
136         for (__i = sizeof(type);                                \
137              __i < (e)->target_offset;                          \
138              __i += __m->u.match_size) {                        \
139                 __m = (void *)e + __i;                          \
140                                                                 \
141                 __ret = fn(__m , ## args);                      \
142                 if (__ret != 0)                                 \
143                         break;                                  \
144         }                                                       \
145         __ret;                                                  \
146 })
147
148 /* fn returns 0 to continue iteration */
149 #define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
150 ({                                                              \
151         unsigned int __i, __n;                                  \
152         int __ret = 0;                                          \
153         type *__entry;                                          \
154                                                                 \
155         for (__i = 0, __n = 0; __i < (size);                    \
156              __i += __entry->next_offset, __n++) {              \
157                 __entry = (void *)(entries) + __i;              \
158                 if (__n < n)                                    \
159                         continue;                               \
160                                                                 \
161                 __ret = fn(__entry , ## args);                  \
162                 if (__ret != 0)                                 \
163                         break;                                  \
164         }                                                       \
165         __ret;                                                  \
166 })
167
168 /* fn returns 0 to continue iteration */
169 #define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
170         XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
171
172 #ifdef __KERNEL__
173
174 #include <linux/netdevice.h>
175
176 struct xt_match
177 {
178         struct list_head list;
179
180         const char name[XT_FUNCTION_MAXNAMELEN-1];
181
182         /* Return true or false: return FALSE and set *hotdrop = 1 to
183            force immediate packet drop. */
184         /* Arguments changed since 2.6.9, as this must now handle
185            non-linear skb, using skb_header_pointer and
186            skb_ip_make_writable. */
187         bool (*match)(const struct sk_buff *skb,
188                       const struct net_device *in,
189                       const struct net_device *out,
190                       const struct xt_match *match,
191                       const void *matchinfo,
192                       int offset,
193                       unsigned int protoff,
194                       bool *hotdrop);
195
196         /* Called when user tries to insert an entry of this type. */
197         /* Should return true or false. */
198         bool (*checkentry)(const char *tablename,
199                            const void *ip,
200                            const struct xt_match *match,
201                            void *matchinfo,
202                            unsigned int hook_mask);
203
204         /* Called when entry of this type deleted. */
205         void (*destroy)(const struct xt_match *match, void *matchinfo);
206
207         /* Called when userspace align differs from kernel space one */
208         void (*compat_from_user)(void *dst, void *src);
209         int (*compat_to_user)(void __user *dst, void *src);
210
211         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
212         struct module *me;
213
214         /* Free to use by each match */
215         unsigned long data;
216
217         const char *table;
218         unsigned int matchsize;
219         unsigned int compatsize;
220         unsigned int hooks;
221         unsigned short proto;
222
223         unsigned short family;
224         u_int8_t revision;
225 };
226
227 /* Registration hooks for targets. */
228 struct xt_target
229 {
230         struct list_head list;
231
232         const char name[XT_FUNCTION_MAXNAMELEN-1];
233
234         /* Returns verdict. Argument order changed since 2.6.9, as this
235            must now handle non-linear skbs, using skb_copy_bits and
236            skb_ip_make_writable. */
237         unsigned int (*target)(struct sk_buff *skb,
238                                const struct net_device *in,
239                                const struct net_device *out,
240                                unsigned int hooknum,
241                                const struct xt_target *target,
242                                const void *targinfo);
243
244         /* Called when user tries to insert an entry of this type:
245            hook_mask is a bitmask of hooks from which it can be
246            called. */
247         /* Should return true or false. */
248         bool (*checkentry)(const char *tablename,
249                            const void *entry,
250                            const struct xt_target *target,
251                            void *targinfo,
252                            unsigned int hook_mask);
253
254         /* Called when entry of this type deleted. */
255         void (*destroy)(const struct xt_target *target, void *targinfo);
256
257         /* Called when userspace align differs from kernel space one */
258         void (*compat_from_user)(void *dst, void *src);
259         int (*compat_to_user)(void __user *dst, void *src);
260
261         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
262         struct module *me;
263
264         const char *table;
265         unsigned int targetsize;
266         unsigned int compatsize;
267         unsigned int hooks;
268         unsigned short proto;
269
270         unsigned short family;
271         u_int8_t revision;
272 };
273
274 /* Furniture shopping... */
275 struct xt_table
276 {
277         struct list_head list;
278
279         /* A unique name... */
280         const char name[XT_TABLE_MAXNAMELEN];
281
282         /* What hooks you will enter on */
283         unsigned int valid_hooks;
284
285         /* Lock for the curtain */
286         rwlock_t lock;
287
288         /* Man behind the curtain... */
289         //struct ip6t_table_info *private;
290         void *private;
291
292         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
293         struct module *me;
294
295         u_int8_t af;            /* address/protocol family */
296 };
297
298 #include <linux/netfilter_ipv4.h>
299
300 /* The table itself */
301 struct xt_table_info
302 {
303         /* Size per table */
304         unsigned int size;
305         /* Number of entries: FIXME. --RR */
306         unsigned int number;
307         /* Initial number of entries. Needed for module usage count */
308         unsigned int initial_entries;
309
310         /* Entry points and underflows */
311         unsigned int hook_entry[NF_INET_NUMHOOKS];
312         unsigned int underflow[NF_INET_NUMHOOKS];
313
314         /* ipt_entry tables: one per CPU */
315         /* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
316         char *entries[1];
317 };
318
319 #define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
320                           + nr_cpu_ids * sizeof(char *))
321 extern int xt_register_target(struct xt_target *target);
322 extern void xt_unregister_target(struct xt_target *target);
323 extern int xt_register_targets(struct xt_target *target, unsigned int n);
324 extern void xt_unregister_targets(struct xt_target *target, unsigned int n);
325
326 extern int xt_register_match(struct xt_match *target);
327 extern void xt_unregister_match(struct xt_match *target);
328 extern int xt_register_matches(struct xt_match *match, unsigned int n);
329 extern void xt_unregister_matches(struct xt_match *match, unsigned int n);
330
331 extern int xt_check_match(const struct xt_match *match, unsigned short family,
332                           unsigned int size, const char *table, unsigned int hook,
333                           unsigned short proto, int inv_proto,
334                           const void *entry, void *matchinfo);
335 extern int xt_check_target(const struct xt_target *target, unsigned short family,
336                            unsigned int size, const char *table, unsigned int hook,
337                            unsigned short proto, int inv_proto,
338                            const void *entry, void *targinfo);
339
340 extern struct xt_table *xt_register_table(struct net *net,
341                                           struct xt_table *table,
342                                           struct xt_table_info *bootstrap,
343                                           struct xt_table_info *newinfo);
344 extern void *xt_unregister_table(struct xt_table *table);
345
346 extern struct xt_table_info *xt_replace_table(struct xt_table *table,
347                                               unsigned int num_counters,
348                                               struct xt_table_info *newinfo,
349                                               int *error);
350
351 extern struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
352 extern struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
353 extern struct xt_target *xt_request_find_target(u8 af, const char *name,
354                                                 u8 revision);
355 extern int xt_find_revision(u8 af, const char *name, u8 revision,
356                             int target, int *err);
357
358 extern struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
359                                            const char *name);
360 extern void xt_table_unlock(struct xt_table *t);
361
362 extern int xt_proto_init(struct net *net, u_int8_t af);
363 extern void xt_proto_fini(struct net *net, u_int8_t af);
364
365 extern struct xt_table_info *xt_alloc_table_info(unsigned int size);
366 extern void xt_free_table_info(struct xt_table_info *info);
367
368 #ifdef CONFIG_COMPAT
369 #include <net/compat.h>
370
371 struct compat_xt_entry_match
372 {
373         union {
374                 struct {
375                         u_int16_t match_size;
376                         char name[XT_FUNCTION_MAXNAMELEN - 1];
377                         u_int8_t revision;
378                 } user;
379                 struct {
380                         u_int16_t match_size;
381                         compat_uptr_t match;
382                 } kernel;
383                 u_int16_t match_size;
384         } u;
385         unsigned char data[0];
386 };
387
388 struct compat_xt_entry_target
389 {
390         union {
391                 struct {
392                         u_int16_t target_size;
393                         char name[XT_FUNCTION_MAXNAMELEN - 1];
394                         u_int8_t revision;
395                 } user;
396                 struct {
397                         u_int16_t target_size;
398                         compat_uptr_t target;
399                 } kernel;
400                 u_int16_t target_size;
401         } u;
402         unsigned char data[0];
403 };
404
405 /* FIXME: this works only on 32 bit tasks
406  * need to change whole approach in order to calculate align as function of
407  * current task alignment */
408
409 struct compat_xt_counters
410 {
411 #if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
412         u_int32_t cnt[4];
413 #else
414         u_int64_t cnt[2];
415 #endif
416 };
417
418 struct compat_xt_counters_info
419 {
420         char name[XT_TABLE_MAXNAMELEN];
421         compat_uint_t num_counters;
422         struct compat_xt_counters counters[0];
423 };
424
425 #define COMPAT_XT_ALIGN(s) (((s) + (__alignof__(struct compat_xt_counters)-1)) \
426                 & ~(__alignof__(struct compat_xt_counters)-1))
427
428 extern void xt_compat_lock(u_int8_t af);
429 extern void xt_compat_unlock(u_int8_t af);
430
431 extern int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta);
432 extern void xt_compat_flush_offsets(u_int8_t af);
433 extern short xt_compat_calc_jump(u_int8_t af, unsigned int offset);
434
435 extern int xt_compat_match_offset(const struct xt_match *match);
436 extern int xt_compat_match_from_user(struct xt_entry_match *m,
437                                      void **dstptr, unsigned int *size);
438 extern int xt_compat_match_to_user(struct xt_entry_match *m,
439                                    void __user **dstptr, unsigned int *size);
440
441 extern int xt_compat_target_offset(const struct xt_target *target);
442 extern void xt_compat_target_from_user(struct xt_entry_target *t,
443                                        void **dstptr, unsigned int *size);
444 extern int xt_compat_target_to_user(struct xt_entry_target *t,
445                                     void __user **dstptr, unsigned int *size);
446
447 #endif /* CONFIG_COMPAT */
448 #endif /* __KERNEL__ */
449
450 #endif /* _X_TABLES_H */