VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / include / linux / netfilter_ipv6 / ip6_tables.h
1 /*
2  * 25-Jul-1998 Major changes to allow for ip chain table
3  *
4  * 3-Jan-2000 Named tables to allow packet selection for different uses.
5  */
6
7 /*
8  *      Format of an IP6 firewall descriptor
9  *
10  *      src, dst, src_mask, dst_mask are always stored in network byte order.
11  *      flags are stored in host byte order (of course).
12  *      Port numbers are stored in HOST byte order.
13  */
14
15 #ifndef _IP6_TABLES_H
16 #define _IP6_TABLES_H
17
18 #ifdef __KERNEL__
19 #include <linux/if.h>
20 #include <linux/types.h>
21 #include <linux/in6.h>
22 #include <linux/ipv6.h>
23 #include <linux/skbuff.h>
24 #endif
25 #include <linux/compiler.h>
26 #include <linux/netfilter_ipv6.h>
27
28 #define IP6T_FUNCTION_MAXNAMELEN 30
29 #define IP6T_TABLE_MAXNAMELEN 32
30
31 /* Yes, Virginia, you have to zero the padding. */
32 struct ip6t_ip6 {
33         /* Source and destination IP6 addr */
34         struct in6_addr src, dst;               
35         /* Mask for src and dest IP6 addr */
36         struct in6_addr smsk, dmsk;
37         char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
38         unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
39
40         /* ARGH, HopByHop uses 0, so can't do 0 = ANY,
41            instead IP6T_F_NOPROTO must be set */
42         u_int16_t proto;
43         /* TOS to match iff flags & IP6T_F_TOS */
44         u_int8_t tos;
45
46         /* Flags word */
47         u_int8_t flags;
48         /* Inverse flags */
49         u_int8_t invflags;
50 };
51
52 /* FIXME: If alignment in kernel different from userspace? --RR */
53 struct ip6t_entry_match
54 {
55         union {
56                 struct {
57                         u_int16_t match_size;
58
59                         /* Used by userspace */
60                         char name[IP6T_FUNCTION_MAXNAMELEN];
61                 } user;
62                 struct {
63                         u_int16_t match_size;
64
65                         /* Used inside the kernel */
66                         struct ip6t_match *match;
67                 } kernel;
68
69                 /* Total length */
70                 u_int16_t match_size;
71         } u;
72
73         unsigned char data[0];
74 };
75
76 struct ip6t_entry_target
77 {
78         union {
79                 struct {
80                         u_int16_t target_size;
81
82                         /* Used by userspace */
83                         char name[IP6T_FUNCTION_MAXNAMELEN];
84                 } user;
85                 struct {
86                         u_int16_t target_size;
87
88                         /* Used inside the kernel */
89                         struct ip6t_target *target;
90                 } kernel;
91
92                 /* Total length */
93                 u_int16_t target_size;
94         } u;
95
96         unsigned char data[0];
97 };
98
99 struct ip6t_standard_target
100 {
101         struct ip6t_entry_target target;
102         int verdict;
103 };
104
105 struct ip6t_counters
106 {
107         u_int64_t pcnt, bcnt;                   /* Packet and byte counters */
108 };
109
110 #ifdef __KERNEL__
111 static DECLARE_MUTEX(ip6t_mutex);
112 #endif
113
114 /* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
115 #define IP6T_F_PROTO            0x01    /* Set if rule cares about upper 
116                                            protocols */
117 #define IP6T_F_TOS              0x02    /* Match the TOS. */
118 #define IP6T_F_MASK             0x03    /* All possible flag bits mask. */
119
120 /* Values for "inv" field in struct ip6t_ip6. */
121 #define IP6T_INV_VIA_IN         0x01    /* Invert the sense of IN IFACE. */
122 #define IP6T_INV_VIA_OUT                0x02    /* Invert the sense of OUT IFACE */
123 #define IP6T_INV_TOS            0x04    /* Invert the sense of TOS. */
124 #define IP6T_INV_SRCIP          0x08    /* Invert the sense of SRC IP. */
125 #define IP6T_INV_DSTIP          0x10    /* Invert the sense of DST OP. */
126 #define IP6T_INV_FRAG           0x20    /* Invert the sense of FRAG. */
127 #define IP6T_INV_PROTO          0x40    /* Invert the sense of PROTO. */
128 #define IP6T_INV_MASK           0x7F    /* All possible flag bits mask. */
129
130 /* This structure defines each of the firewall rules.  Consists of 3
131    parts which are 1) general IP header stuff 2) match specific
132    stuff 3) the target to perform if the rule matches */
133 struct ip6t_entry
134 {
135         struct ip6t_ip6 ipv6;
136
137         /* Mark with fields that we care about. */
138         unsigned int nfcache;
139
140         /* Size of ipt_entry + matches */
141         u_int16_t target_offset;
142         /* Size of ipt_entry + matches + target */
143         u_int16_t next_offset;
144
145         /* Back pointer */
146         unsigned int comefrom;
147
148         /* Packet and byte counters. */
149         struct ip6t_counters counters;
150
151         /* The matches (if any), then the target. */
152         unsigned char elems[0];
153 };
154
155 /*
156  * New IP firewall options for [gs]etsockopt at the RAW IP level.
157  * Unlike BSD Linux inherits IP options so you don't have to use
158  * a raw socket for this. Instead we check rights in the calls. */
159 #define IP6T_BASE_CTL                   64      /* base for firewall socket options */
160
161 #define IP6T_SO_SET_REPLACE             (IP6T_BASE_CTL)
162 #define IP6T_SO_SET_ADD_COUNTERS        (IP6T_BASE_CTL + 1)
163 #define IP6T_SO_SET_MAX                 IP6T_SO_SET_ADD_COUNTERS
164
165 #define IP6T_SO_GET_INFO                (IP6T_BASE_CTL)
166 #define IP6T_SO_GET_ENTRIES             (IP6T_BASE_CTL + 1)
167 #define IP6T_SO_GET_MAX                 IP6T_SO_GET_ENTRIES
168
169 /* CONTINUE verdict for targets */
170 #define IP6T_CONTINUE 0xFFFFFFFF
171
172 /* For standard target */
173 #define IP6T_RETURN (-NF_MAX_VERDICT - 1)
174
175 /* TCP matching stuff */
176 struct ip6t_tcp
177 {
178         u_int16_t spts[2];                      /* Source port range. */
179         u_int16_t dpts[2];                      /* Destination port range. */
180         u_int8_t option;                        /* TCP Option iff non-zero*/
181         u_int8_t flg_mask;                      /* TCP flags mask byte */
182         u_int8_t flg_cmp;                       /* TCP flags compare byte */
183         u_int8_t invflags;                      /* Inverse flags */
184 };
185
186 /* Values for "inv" field in struct ipt_tcp. */
187 #define IP6T_TCP_INV_SRCPT      0x01    /* Invert the sense of source ports. */
188 #define IP6T_TCP_INV_DSTPT      0x02    /* Invert the sense of dest ports. */
189 #define IP6T_TCP_INV_FLAGS      0x04    /* Invert the sense of TCP flags. */
190 #define IP6T_TCP_INV_OPTION     0x08    /* Invert the sense of option test. */
191 #define IP6T_TCP_INV_MASK       0x0F    /* All possible flags. */
192
193 /* UDP matching stuff */
194 struct ip6t_udp
195 {
196         u_int16_t spts[2];                      /* Source port range. */
197         u_int16_t dpts[2];                      /* Destination port range. */
198         u_int8_t invflags;                      /* Inverse flags */
199 };
200
201 /* Values for "invflags" field in struct ipt_udp. */
202 #define IP6T_UDP_INV_SRCPT      0x01    /* Invert the sense of source ports. */
203 #define IP6T_UDP_INV_DSTPT      0x02    /* Invert the sense of dest ports. */
204 #define IP6T_UDP_INV_MASK       0x03    /* All possible flags. */
205
206 /* ICMP matching stuff */
207 struct ip6t_icmp
208 {
209         u_int8_t type;                          /* type to match */
210         u_int8_t code[2];                       /* range of code */
211         u_int8_t invflags;                      /* Inverse flags */
212 };
213
214 /* Values for "inv" field for struct ipt_icmp. */
215 #define IP6T_ICMP_INV   0x01    /* Invert the sense of type/code test */
216
217 /* The argument to IP6T_SO_GET_INFO */
218 struct ip6t_getinfo
219 {
220         /* Which table: caller fills this in. */
221         char name[IP6T_TABLE_MAXNAMELEN];
222
223         /* Kernel fills these in. */
224         /* Which hook entry points are valid: bitmask */
225         unsigned int valid_hooks;
226
227         /* Hook entry points: one per netfilter hook. */
228         unsigned int hook_entry[NF_IP6_NUMHOOKS];
229
230         /* Underflow points. */
231         unsigned int underflow[NF_IP6_NUMHOOKS];
232
233         /* Number of entries */
234         unsigned int num_entries;
235
236         /* Size of entries. */
237         unsigned int size;
238 };
239
240 /* The argument to IP6T_SO_SET_REPLACE. */
241 struct ip6t_replace
242 {
243         /* Which table. */
244         char name[IP6T_TABLE_MAXNAMELEN];
245
246         /* Which hook entry points are valid: bitmask.  You can't
247            change this. */
248         unsigned int valid_hooks;
249
250         /* Number of entries */
251         unsigned int num_entries;
252
253         /* Total size of new entries */
254         unsigned int size;
255
256         /* Hook entry points. */
257         unsigned int hook_entry[NF_IP6_NUMHOOKS];
258
259         /* Underflow points. */
260         unsigned int underflow[NF_IP6_NUMHOOKS];
261
262         /* Information about old entries: */
263         /* Number of counters (must be equal to current number of entries). */
264         unsigned int num_counters;
265         /* The old entries' counters. */
266         struct ip6t_counters __user *counters;
267
268         /* The entries (hang off end: not really an array). */
269         struct ip6t_entry entries[0];
270 };
271
272 /* The argument to IP6T_SO_ADD_COUNTERS. */
273 struct ip6t_counters_info
274 {
275         /* Which table. */
276         char name[IP6T_TABLE_MAXNAMELEN];
277
278         unsigned int num_counters;
279
280         /* The counters (actually `number' of these). */
281         struct ip6t_counters counters[0];
282 };
283
284 /* The argument to IP6T_SO_GET_ENTRIES. */
285 struct ip6t_get_entries
286 {
287         /* Which table: user fills this in. */
288         char name[IP6T_TABLE_MAXNAMELEN];
289
290         /* User fills this in: total entry size. */
291         unsigned int size;
292
293         /* The entries. */
294         struct ip6t_entry entrytable[0];
295 };
296
297 /* Standard return verdict, or do jump. */
298 #define IP6T_STANDARD_TARGET ""
299 /* Error verdict. */
300 #define IP6T_ERROR_TARGET "ERROR"
301
302 /* Helper functions */
303 static __inline__ struct ip6t_entry_target *
304 ip6t_get_target(struct ip6t_entry *e)
305 {
306         return (void *)e + e->target_offset;
307 }
308
309 /* fn returns 0 to continue iteration */
310 #define IP6T_MATCH_ITERATE(e, fn, args...)      \
311 ({                                              \
312         unsigned int __i;                       \
313         int __ret = 0;                          \
314         struct ip6t_entry_match *__m;           \
315                                                 \
316         for (__i = sizeof(struct ip6t_entry);   \
317              __i < (e)->target_offset;          \
318              __i += __m->u.match_size) {        \
319                 __m = (void *)(e) + __i;        \
320                                                 \
321                 __ret = fn(__m , ## args);      \
322                 if (__ret != 0)                 \
323                         break;                  \
324         }                                       \
325         __ret;                                  \
326 })
327
328 /* fn returns 0 to continue iteration */
329 #define IP6T_ENTRY_ITERATE(entries, size, fn, args...)          \
330 ({                                                              \
331         unsigned int __i;                                       \
332         int __ret = 0;                                          \
333         struct ip6t_entry *__e;                                 \
334                                                                 \
335         for (__i = 0; __i < (size); __i += __e->next_offset) {  \
336                 __e = (void *)(entries) + __i;                  \
337                                                                 \
338                 __ret = fn(__e , ## args);                      \
339                 if (__ret != 0)                                 \
340                         break;                                  \
341         }                                                       \
342         __ret;                                                  \
343 })
344
345 /*
346  *      Main firewall chains definitions and global var's definitions.
347  */
348
349 #ifdef __KERNEL__
350
351 #include <linux/init.h>
352 extern void ip6t_init(void) __init;
353
354 struct ip6t_match
355 {
356         struct list_head list;
357
358         const char name[IP6T_FUNCTION_MAXNAMELEN];
359
360         /* Return true or false: return FALSE and set *hotdrop = 1 to
361            force immediate packet drop. */
362         int (*match)(const struct sk_buff *skb,
363                      const struct net_device *in,
364                      const struct net_device *out,
365                      const void *matchinfo,
366                      int offset,
367                      const void *hdr,
368                      u_int16_t datalen,
369                      int *hotdrop);
370
371         /* Called when user tries to insert an entry of this type. */
372         /* Should return true or false. */
373         int (*checkentry)(const char *tablename,
374                           const struct ip6t_ip6 *ip,
375                           void *matchinfo,
376                           unsigned int matchinfosize,
377                           unsigned int hook_mask);
378
379         /* Called when entry of this type deleted. */
380         void (*destroy)(void *matchinfo, unsigned int matchinfosize);
381
382         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
383         struct module *me;
384 };
385
386 /* Registration hooks for targets. */
387 struct ip6t_target
388 {
389         struct list_head list;
390
391         const char name[IP6T_FUNCTION_MAXNAMELEN];
392
393         /* Returns verdict. */
394         unsigned int (*target)(struct sk_buff **pskb,
395                                unsigned int hooknum,
396                                const struct net_device *in,
397                                const struct net_device *out,
398                                const void *targinfo,
399                                void *userdata);
400
401         /* Called when user tries to insert an entry of this type:
402            hook_mask is a bitmask of hooks from which it can be
403            called. */
404         /* Should return true or false. */
405         int (*checkentry)(const char *tablename,
406                           const struct ip6t_entry *e,
407                           void *targinfo,
408                           unsigned int targinfosize,
409                           unsigned int hook_mask);
410
411         /* Called when entry of this type deleted. */
412         void (*destroy)(void *targinfo, unsigned int targinfosize);
413
414         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
415         struct module *me;
416 };
417
418 extern int ip6t_register_target(struct ip6t_target *target);
419 extern void ip6t_unregister_target(struct ip6t_target *target);
420
421 extern int ip6t_register_match(struct ip6t_match *match);
422 extern void ip6t_unregister_match(struct ip6t_match *match);
423
424 /* Furniture shopping... */
425 struct ip6t_table
426 {
427         struct list_head list;
428
429         /* A unique name... */
430         char name[IP6T_TABLE_MAXNAMELEN];
431
432         /* Seed table: copied in register_table */
433         struct ip6t_replace *table;
434
435         /* What hooks you will enter on */
436         unsigned int valid_hooks;
437
438         /* Lock for the curtain */
439         rwlock_t lock;
440
441         /* Man behind the curtain... */
442         struct ip6t_table_info *private;
443
444         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
445         struct module *me;
446 };
447
448 extern int ip6t_register_table(struct ip6t_table *table);
449 extern void ip6t_unregister_table(struct ip6t_table *table);
450 extern unsigned int ip6t_do_table(struct sk_buff **pskb,
451                                   unsigned int hook,
452                                   const struct net_device *in,
453                                   const struct net_device *out,
454                                   struct ip6t_table *table,
455                                   void *userdata);
456
457 /* Check for an extension */
458 extern int ip6t_ext_hdr(u8 nexthdr);
459
460 #define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1))
461
462 #endif /*__KERNEL__*/
463 #endif /* _IP6_TABLES_H */