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