]> bbs.cooldavid.org Git - net-next-2.6.git/blob - include/linux/netfilter/x_tables.h
Merge branch 'master' of git://dev.medozas.de/linux
[net-next-2.6.git] / include / linux / netfilter / x_tables.h
1 #ifndef _X_TABLES_H
2 #define _X_TABLES_H
3
4 #include <linux/types.h>
5
6 #define XT_FUNCTION_MAXNAMELEN 30
7 #define XT_TABLE_MAXNAMELEN 32
8
9 struct xt_entry_match {
10         union {
11                 struct {
12                         __u16 match_size;
13
14                         /* Used by userspace */
15                         char name[XT_FUNCTION_MAXNAMELEN-1];
16
17                         __u8 revision;
18                 } user;
19                 struct {
20                         __u16 match_size;
21
22                         /* Used inside the kernel */
23                         struct xt_match *match;
24                 } kernel;
25
26                 /* Total length */
27                 __u16 match_size;
28         } u;
29
30         unsigned char data[0];
31 };
32
33 struct xt_entry_target {
34         union {
35                 struct {
36                         __u16 target_size;
37
38                         /* Used by userspace */
39                         char name[XT_FUNCTION_MAXNAMELEN-1];
40
41                         __u8 revision;
42                 } user;
43                 struct {
44                         __u16 target_size;
45
46                         /* Used inside the kernel */
47                         struct xt_target *target;
48                 } kernel;
49
50                 /* Total length */
51                 __u16 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         struct xt_entry_target target;
67         int verdict;
68 };
69
70 /* The argument to IPT_SO_GET_REVISION_*.  Returns highest revision
71  * kernel supports, if >= revision. */
72 struct xt_get_revision {
73         char name[XT_FUNCTION_MAXNAMELEN-1];
74
75         __u8 revision;
76 };
77
78 /* CONTINUE verdict for targets */
79 #define XT_CONTINUE 0xFFFFFFFF
80
81 /* For standard target */
82 #define XT_RETURN (-NF_REPEAT - 1)
83
84 /* this is a dummy structure to find out the alignment requirement for a struct
85  * containing all the fundamental data types that are used in ipt_entry,
86  * ip6t_entry and arpt_entry.  This sucks, and it is a hack.  It will be my
87  * personal pleasure to remove it -HW
88  */
89 struct _xt_align {
90         __u8 u8;
91         __u16 u16;
92         __u32 u32;
93         __u64 u64;
94 };
95
96 #define XT_ALIGN(s) ALIGN((s), __alignof__(struct _xt_align))
97
98 /* Standard return verdict, or do jump. */
99 #define XT_STANDARD_TARGET ""
100 /* Error verdict. */
101 #define XT_ERROR_TARGET "ERROR"
102
103 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
104 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
105
106 struct xt_counters {
107         __u64 pcnt, bcnt;                       /* Packet and byte counters */
108 };
109
110 /* The argument to IPT_SO_ADD_COUNTERS. */
111 struct xt_counters_info {
112         /* Which table. */
113         char name[XT_TABLE_MAXNAMELEN];
114
115         unsigned int num_counters;
116
117         /* The counters (actually `number' of these). */
118         struct xt_counters counters[0];
119 };
120
121 #define XT_INV_PROTO            0x40    /* Invert the sense of PROTO. */
122
123 /* fn returns 0 to continue iteration */
124 #define XT_MATCH_ITERATE(type, e, fn, args...)                  \
125 ({                                                              \
126         unsigned int __i;                                       \
127         int __ret = 0;                                          \
128         struct xt_entry_match *__m;                             \
129                                                                 \
130         for (__i = sizeof(type);                                \
131              __i < (e)->target_offset;                          \
132              __i += __m->u.match_size) {                        \
133                 __m = (void *)e + __i;                          \
134                                                                 \
135                 __ret = fn(__m , ## args);                      \
136                 if (__ret != 0)                                 \
137                         break;                                  \
138         }                                                       \
139         __ret;                                                  \
140 })
141
142 /* fn returns 0 to continue iteration */
143 #define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
144 ({                                                              \
145         unsigned int __i, __n;                                  \
146         int __ret = 0;                                          \
147         type *__entry;                                          \
148                                                                 \
149         for (__i = 0, __n = 0; __i < (size);                    \
150              __i += __entry->next_offset, __n++) {              \
151                 __entry = (void *)(entries) + __i;              \
152                 if (__n < n)                                    \
153                         continue;                               \
154                                                                 \
155                 __ret = fn(__entry , ## args);                  \
156                 if (__ret != 0)                                 \
157                         break;                                  \
158         }                                                       \
159         __ret;                                                  \
160 })
161
162 /* fn returns 0 to continue iteration */
163 #define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
164         XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
165
166 #ifdef __KERNEL__
167
168 #include <linux/netdevice.h>
169
170 /**
171  * struct xt_match_param - parameters for match extensions' match functions
172  *
173  * @in:         input netdevice
174  * @out:        output netdevice
175  * @match:      struct xt_match through which this function was invoked
176  * @matchinfo:  per-match data
177  * @fragoff:    packet is a fragment, this is the data offset
178  * @thoff:      position of transport header relative to skb->data
179  * @hook:       hook number given packet came from
180  * @family:     Actual NFPROTO_* through which the function is invoked
181  *              (helpful when match->family == NFPROTO_UNSPEC)
182  * @hotdrop:    drop packet if we had inspection problems
183  */
184 struct xt_match_param {
185         const struct net_device *in, *out;
186         const struct xt_match *match;
187         const void *matchinfo;
188         int fragoff;
189         unsigned int thoff;
190         unsigned int hooknum;
191         u_int8_t family;
192         bool *hotdrop;
193 };
194
195 /**
196  * struct xt_mtchk_param - parameters for match extensions'
197  * checkentry functions
198  *
199  * @table:      table the rule is tried to be inserted into
200  * @entryinfo:  the family-specific rule data
201  *              (struct ipt_ip, ip6t_ip, ebt_entry)
202  * @match:      struct xt_match through which this function was invoked
203  * @matchinfo:  per-match data
204  * @hook_mask:  via which hooks the new rule is reachable
205  */
206 struct xt_mtchk_param {
207         struct net *net;
208         const char *table;
209         const void *entryinfo;
210         const struct xt_match *match;
211         void *matchinfo;
212         unsigned int hook_mask;
213         u_int8_t family;
214 };
215
216 /* Match destructor parameters */
217 struct xt_mtdtor_param {
218         struct net *net;
219         const struct xt_match *match;
220         void *matchinfo;
221         u_int8_t family;
222 };
223
224 /**
225  * struct xt_target_param - parameters for target extensions' target functions
226  *
227  * @hooknum:    hook through which this target was invoked
228  * @target:     struct xt_target through which this function was invoked
229  * @targinfo:   per-target data
230  *
231  * Other fields see above.
232  */
233 struct xt_target_param {
234         const struct net_device *in, *out;
235         const struct xt_target *target;
236         const void *targinfo;
237         unsigned int hooknum;
238         u_int8_t family;
239 };
240
241 /**
242  * struct xt_tgchk_param - parameters for target extensions'
243  * checkentry functions
244  *
245  * @entryinfo:  the family-specific rule data
246  *              (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
247  *
248  * Other fields see above.
249  */
250 struct xt_tgchk_param {
251         struct net *net;
252         const char *table;
253         const void *entryinfo;
254         const struct xt_target *target;
255         void *targinfo;
256         unsigned int hook_mask;
257         u_int8_t family;
258 };
259
260 /* Target destructor parameters */
261 struct xt_tgdtor_param {
262         struct net *net;
263         const struct xt_target *target;
264         void *targinfo;
265         u_int8_t family;
266 };
267
268 struct xt_match {
269         struct list_head list;
270
271         const char name[XT_FUNCTION_MAXNAMELEN-1];
272         u_int8_t revision;
273
274         /* Return true or false: return FALSE and set *hotdrop = 1 to
275            force immediate packet drop. */
276         /* Arguments changed since 2.6.9, as this must now handle
277            non-linear skb, using skb_header_pointer and
278            skb_ip_make_writable. */
279         bool (*match)(const struct sk_buff *skb,
280                       const struct xt_match_param *);
281
282         /* Called when user tries to insert an entry of this type. */
283         bool (*checkentry)(const struct xt_mtchk_param *);
284
285         /* Called when entry of this type deleted. */
286         void (*destroy)(const struct xt_mtdtor_param *);
287 #ifdef CONFIG_COMPAT
288         /* Called when userspace align differs from kernel space one */
289         void (*compat_from_user)(void *dst, void *src);
290         int (*compat_to_user)(void __user *dst, void *src);
291 #endif
292         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
293         struct module *me;
294
295         /* Free to use by each match */
296         unsigned long data;
297
298         const char *table;
299         unsigned int matchsize;
300 #ifdef CONFIG_COMPAT
301         unsigned int compatsize;
302 #endif
303         unsigned int hooks;
304         unsigned short proto;
305
306         unsigned short family;
307 };
308
309 /* Registration hooks for targets. */
310 struct xt_target {
311         struct list_head list;
312
313         const char name[XT_FUNCTION_MAXNAMELEN-1];
314
315         /* Returns verdict. Argument order changed since 2.6.9, as this
316            must now handle non-linear skbs, using skb_copy_bits and
317            skb_ip_make_writable. */
318         unsigned int (*target)(struct sk_buff *skb,
319                                const struct xt_target_param *);
320
321         /* Called when user tries to insert an entry of this type:
322            hook_mask is a bitmask of hooks from which it can be
323            called. */
324         /* Should return true or false. */
325         bool (*checkentry)(const struct xt_tgchk_param *);
326
327         /* Called when entry of this type deleted. */
328         void (*destroy)(const struct xt_tgdtor_param *);
329 #ifdef CONFIG_COMPAT
330         /* Called when userspace align differs from kernel space one */
331         void (*compat_from_user)(void *dst, void *src);
332         int (*compat_to_user)(void __user *dst, void *src);
333 #endif
334         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
335         struct module *me;
336
337         const char *table;
338         unsigned int targetsize;
339 #ifdef CONFIG_COMPAT
340         unsigned int compatsize;
341 #endif
342         unsigned int hooks;
343         unsigned short proto;
344
345         unsigned short family;
346         u_int8_t revision;
347 };
348
349 /* Furniture shopping... */
350 struct xt_table {
351         struct list_head list;
352
353         /* What hooks you will enter on */
354         unsigned int valid_hooks;
355
356         /* Man behind the curtain... */
357         struct xt_table_info *private;
358
359         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
360         struct module *me;
361
362         u_int8_t af;            /* address/protocol family */
363         int priority;           /* hook order */
364
365         /* A unique name... */
366         const char name[XT_TABLE_MAXNAMELEN];
367 };
368
369 #include <linux/netfilter_ipv4.h>
370
371 /* The table itself */
372 struct xt_table_info {
373         /* Size per table */
374         unsigned int size;
375         /* Number of entries: FIXME. --RR */
376         unsigned int number;
377         /* Initial number of entries. Needed for module usage count */
378         unsigned int initial_entries;
379
380         /* Entry points and underflows */
381         unsigned int hook_entry[NF_INET_NUMHOOKS];
382         unsigned int underflow[NF_INET_NUMHOOKS];
383
384         /* ipt_entry tables: one per CPU */
385         /* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
386         void *entries[1];
387 };
388
389 #define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
390                           + nr_cpu_ids * sizeof(char *))
391 extern int xt_register_target(struct xt_target *target);
392 extern void xt_unregister_target(struct xt_target *target);
393 extern int xt_register_targets(struct xt_target *target, unsigned int n);
394 extern void xt_unregister_targets(struct xt_target *target, unsigned int n);
395
396 extern int xt_register_match(struct xt_match *target);
397 extern void xt_unregister_match(struct xt_match *target);
398 extern int xt_register_matches(struct xt_match *match, unsigned int n);
399 extern void xt_unregister_matches(struct xt_match *match, unsigned int n);
400
401 extern int xt_check_match(struct xt_mtchk_param *,
402                           unsigned int size, u_int8_t proto, bool inv_proto);
403 extern int xt_check_target(struct xt_tgchk_param *,
404                            unsigned int size, u_int8_t proto, bool inv_proto);
405
406 extern struct xt_table *xt_register_table(struct net *net,
407                                           const struct xt_table *table,
408                                           struct xt_table_info *bootstrap,
409                                           struct xt_table_info *newinfo);
410 extern void *xt_unregister_table(struct xt_table *table);
411
412 extern struct xt_table_info *xt_replace_table(struct xt_table *table,
413                                               unsigned int num_counters,
414                                               struct xt_table_info *newinfo,
415                                               int *error);
416
417 extern struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
418 extern struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
419 extern struct xt_target *xt_request_find_target(u8 af, const char *name,
420                                                 u8 revision);
421 extern int xt_find_revision(u8 af, const char *name, u8 revision,
422                             int target, int *err);
423
424 extern struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
425                                            const char *name);
426 extern void xt_table_unlock(struct xt_table *t);
427
428 extern int xt_proto_init(struct net *net, u_int8_t af);
429 extern void xt_proto_fini(struct net *net, u_int8_t af);
430
431 extern struct xt_table_info *xt_alloc_table_info(unsigned int size);
432 extern void xt_free_table_info(struct xt_table_info *info);
433
434 /*
435  * Per-CPU spinlock associated with per-cpu table entries, and
436  * with a counter for the "reading" side that allows a recursive
437  * reader to avoid taking the lock and deadlocking.
438  *
439  * "reading" is used by ip/arp/ip6 tables rule processing which runs per-cpu.
440  * It needs to ensure that the rules are not being changed while the packet
441  * is being processed. In some cases, the read lock will be acquired
442  * twice on the same CPU; this is okay because of the count.
443  *
444  * "writing" is used when reading counters.
445  *  During replace any readers that are using the old tables have to complete
446  *  before freeing the old table. This is handled by the write locking
447  *  necessary for reading the counters.
448  */
449 struct xt_info_lock {
450         spinlock_t lock;
451         unsigned char readers;
452 };
453 DECLARE_PER_CPU(struct xt_info_lock, xt_info_locks);
454
455 /*
456  * Note: we need to ensure that preemption is disabled before acquiring
457  * the per-cpu-variable, so we do it as a two step process rather than
458  * using "spin_lock_bh()".
459  *
460  * We _also_ need to disable bottom half processing before updating our
461  * nesting count, to make sure that the only kind of re-entrancy is this
462  * code being called by itself: since the count+lock is not an atomic
463  * operation, we can allow no races.
464  *
465  * _Only_ that special combination of being per-cpu and never getting
466  * re-entered asynchronously means that the count is safe.
467  */
468 static inline void xt_info_rdlock_bh(void)
469 {
470         struct xt_info_lock *lock;
471
472         local_bh_disable();
473         lock = &__get_cpu_var(xt_info_locks);
474         if (likely(!lock->readers++))
475                 spin_lock(&lock->lock);
476 }
477
478 static inline void xt_info_rdunlock_bh(void)
479 {
480         struct xt_info_lock *lock = &__get_cpu_var(xt_info_locks);
481
482         if (likely(!--lock->readers))
483                 spin_unlock(&lock->lock);
484         local_bh_enable();
485 }
486
487 /*
488  * The "writer" side needs to get exclusive access to the lock,
489  * regardless of readers.  This must be called with bottom half
490  * processing (and thus also preemption) disabled.
491  */
492 static inline void xt_info_wrlock(unsigned int cpu)
493 {
494         spin_lock(&per_cpu(xt_info_locks, cpu).lock);
495 }
496
497 static inline void xt_info_wrunlock(unsigned int cpu)
498 {
499         spin_unlock(&per_cpu(xt_info_locks, cpu).lock);
500 }
501
502 /*
503  * This helper is performance critical and must be inlined
504  */
505 static inline unsigned long ifname_compare_aligned(const char *_a,
506                                                    const char *_b,
507                                                    const char *_mask)
508 {
509         const unsigned long *a = (const unsigned long *)_a;
510         const unsigned long *b = (const unsigned long *)_b;
511         const unsigned long *mask = (const unsigned long *)_mask;
512         unsigned long ret;
513
514         ret = (a[0] ^ b[0]) & mask[0];
515         if (IFNAMSIZ > sizeof(unsigned long))
516                 ret |= (a[1] ^ b[1]) & mask[1];
517         if (IFNAMSIZ > 2 * sizeof(unsigned long))
518                 ret |= (a[2] ^ b[2]) & mask[2];
519         if (IFNAMSIZ > 3 * sizeof(unsigned long))
520                 ret |= (a[3] ^ b[3]) & mask[3];
521         BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
522         return ret;
523 }
524
525 extern struct nf_hook_ops *xt_hook_link(const struct xt_table *, nf_hookfn *);
526 extern void xt_hook_unlink(const struct xt_table *, struct nf_hook_ops *);
527
528 #ifdef CONFIG_COMPAT
529 #include <net/compat.h>
530
531 struct compat_xt_entry_match {
532         union {
533                 struct {
534                         u_int16_t match_size;
535                         char name[XT_FUNCTION_MAXNAMELEN - 1];
536                         u_int8_t revision;
537                 } user;
538                 struct {
539                         u_int16_t match_size;
540                         compat_uptr_t match;
541                 } kernel;
542                 u_int16_t match_size;
543         } u;
544         unsigned char data[0];
545 };
546
547 struct compat_xt_entry_target {
548         union {
549                 struct {
550                         u_int16_t target_size;
551                         char name[XT_FUNCTION_MAXNAMELEN - 1];
552                         u_int8_t revision;
553                 } user;
554                 struct {
555                         u_int16_t target_size;
556                         compat_uptr_t target;
557                 } kernel;
558                 u_int16_t target_size;
559         } u;
560         unsigned char data[0];
561 };
562
563 /* FIXME: this works only on 32 bit tasks
564  * need to change whole approach in order to calculate align as function of
565  * current task alignment */
566
567 struct compat_xt_counters {
568         compat_u64 pcnt, bcnt;                  /* Packet and byte counters */
569 };
570
571 struct compat_xt_counters_info {
572         char name[XT_TABLE_MAXNAMELEN];
573         compat_uint_t num_counters;
574         struct compat_xt_counters counters[0];
575 };
576
577 struct _compat_xt_align {
578         __u8 u8;
579         __u16 u16;
580         __u32 u32;
581         compat_u64 u64;
582 };
583
584 #define COMPAT_XT_ALIGN(s) ALIGN((s), __alignof__(struct _compat_xt_align))
585
586 extern void xt_compat_lock(u_int8_t af);
587 extern void xt_compat_unlock(u_int8_t af);
588
589 extern int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta);
590 extern void xt_compat_flush_offsets(u_int8_t af);
591 extern short xt_compat_calc_jump(u_int8_t af, unsigned int offset);
592
593 extern int xt_compat_match_offset(const struct xt_match *match);
594 extern int xt_compat_match_from_user(struct xt_entry_match *m,
595                                      void **dstptr, unsigned int *size);
596 extern int xt_compat_match_to_user(struct xt_entry_match *m,
597                                    void __user **dstptr, unsigned int *size);
598
599 extern int xt_compat_target_offset(const struct xt_target *target);
600 extern void xt_compat_target_from_user(struct xt_entry_target *t,
601                                        void **dstptr, unsigned int *size);
602 extern int xt_compat_target_to_user(struct xt_entry_target *t,
603                                     void __user **dstptr, unsigned int *size);
604
605 #endif /* CONFIG_COMPAT */
606 #endif /* __KERNEL__ */
607
608 #endif /* _X_TABLES_H */