Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[pandora-kernel.git] / include / linux / netfilter_ipv4 / ip_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 IP 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 _IPTABLES_H
16 #define _IPTABLES_H
17
18 #ifdef __KERNEL__
19 #include <linux/if.h>
20 #include <linux/types.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/skbuff.h>
24 #endif
25 #include <linux/compiler.h>
26 #include <linux/netfilter_ipv4.h>
27
28 #define IPT_FUNCTION_MAXNAMELEN 30
29 #define IPT_TABLE_MAXNAMELEN 32
30
31 /* Yes, Virginia, you have to zero the padding. */
32 struct ipt_ip {
33         /* Source and destination IP addr */
34         struct in_addr src, dst;
35         /* Mask for src and dest IP addr */
36         struct in_addr smsk, dmsk;
37         char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
38         unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
39
40         /* Protocol, 0 = ANY */
41         u_int16_t proto;
42
43         /* Flags word */
44         u_int8_t flags;
45         /* Inverse flags */
46         u_int8_t invflags;
47 };
48
49 struct ipt_entry_match
50 {
51         union {
52                 struct {
53                         u_int16_t match_size;
54
55                         /* Used by userspace */
56                         char name[IPT_FUNCTION_MAXNAMELEN-1];
57
58                         u_int8_t revision;
59                 } user;
60                 struct {
61                         u_int16_t match_size;
62
63                         /* Used inside the kernel */
64                         struct ipt_match *match;
65                 } kernel;
66
67                 /* Total length */
68                 u_int16_t match_size;
69         } u;
70
71         unsigned char data[0];
72 };
73
74 struct ipt_entry_target
75 {
76         union {
77                 struct {
78                         u_int16_t target_size;
79
80                         /* Used by userspace */
81                         char name[IPT_FUNCTION_MAXNAMELEN-1];
82
83                         u_int8_t revision;
84                 } user;
85                 struct {
86                         u_int16_t target_size;
87
88                         /* Used inside the kernel */
89                         struct ipt_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 ipt_standard_target
100 {
101         struct ipt_entry_target target;
102         int verdict;
103 };
104
105 struct ipt_counters
106 {
107         u_int64_t pcnt, bcnt;                   /* Packet and byte counters */
108 };
109
110 /* Values for "flag" field in struct ipt_ip (general ip structure). */
111 #define IPT_F_FRAG              0x01    /* Set if rule is a fragment rule */
112 #define IPT_F_GOTO              0x02    /* Set if jump is a goto */
113 #define IPT_F_MASK              0x03    /* All possible flag bits mask. */
114
115 /* Values for "inv" field in struct ipt_ip. */
116 #define IPT_INV_VIA_IN          0x01    /* Invert the sense of IN IFACE. */
117 #define IPT_INV_VIA_OUT         0x02    /* Invert the sense of OUT IFACE */
118 #define IPT_INV_TOS             0x04    /* Invert the sense of TOS. */
119 #define IPT_INV_SRCIP           0x08    /* Invert the sense of SRC IP. */
120 #define IPT_INV_DSTIP           0x10    /* Invert the sense of DST OP. */
121 #define IPT_INV_FRAG            0x20    /* Invert the sense of FRAG. */
122 #define IPT_INV_PROTO           0x40    /* Invert the sense of PROTO. */
123 #define IPT_INV_MASK            0x7F    /* All possible flag bits mask. */
124
125 /* This structure defines each of the firewall rules.  Consists of 3
126    parts which are 1) general IP header stuff 2) match specific
127    stuff 3) the target to perform if the rule matches */
128 struct ipt_entry
129 {
130         struct ipt_ip ip;
131
132         /* Mark with fields that we care about. */
133         unsigned int nfcache;
134
135         /* Size of ipt_entry + matches */
136         u_int16_t target_offset;
137         /* Size of ipt_entry + matches + target */
138         u_int16_t next_offset;
139
140         /* Back pointer */
141         unsigned int comefrom;
142
143         /* Packet and byte counters. */
144         struct ipt_counters counters;
145
146         /* The matches (if any), then the target. */
147         unsigned char elems[0];
148 };
149
150 /*
151  * New IP firewall options for [gs]etsockopt at the RAW IP level.
152  * Unlike BSD Linux inherits IP options so you don't have to use a raw
153  * socket for this. Instead we check rights in the calls. */
154 #define IPT_BASE_CTL            64      /* base for firewall socket options */
155
156 #define IPT_SO_SET_REPLACE      (IPT_BASE_CTL)
157 #define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1)
158 #define IPT_SO_SET_MAX          IPT_SO_SET_ADD_COUNTERS
159
160 #define IPT_SO_GET_INFO                 (IPT_BASE_CTL)
161 #define IPT_SO_GET_ENTRIES              (IPT_BASE_CTL + 1)
162 #define IPT_SO_GET_REVISION_MATCH       (IPT_BASE_CTL + 2)
163 #define IPT_SO_GET_REVISION_TARGET      (IPT_BASE_CTL + 3)
164 #define IPT_SO_GET_MAX                  IPT_SO_GET_REVISION_TARGET
165
166 /* CONTINUE verdict for targets */
167 #define IPT_CONTINUE 0xFFFFFFFF
168
169 /* For standard target */
170 #define IPT_RETURN (-NF_REPEAT - 1)
171
172 /* TCP matching stuff */
173 struct ipt_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 IPT_TCP_INV_SRCPT       0x01    /* Invert the sense of source ports. */
185 #define IPT_TCP_INV_DSTPT       0x02    /* Invert the sense of dest ports. */
186 #define IPT_TCP_INV_FLAGS       0x04    /* Invert the sense of TCP flags. */
187 #define IPT_TCP_INV_OPTION      0x08    /* Invert the sense of option test. */
188 #define IPT_TCP_INV_MASK        0x0F    /* All possible flags. */
189
190 /* UDP matching stuff */
191 struct ipt_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 IPT_UDP_INV_SRCPT       0x01    /* Invert the sense of source ports. */
200 #define IPT_UDP_INV_DSTPT       0x02    /* Invert the sense of dest ports. */
201 #define IPT_UDP_INV_MASK        0x03    /* All possible flags. */
202
203 /* ICMP matching stuff */
204 struct ipt_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 IPT_ICMP_INV    0x01    /* Invert the sense of type/code test */
213
214 /* The argument to IPT_SO_GET_INFO */
215 struct ipt_getinfo
216 {
217         /* Which table: caller fills this in. */
218         char name[IPT_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_IP_NUMHOOKS];
226
227         /* Underflow points. */
228         unsigned int underflow[NF_IP_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 IPT_SO_SET_REPLACE. */
238 struct ipt_replace
239 {
240         /* Which table. */
241         char name[IPT_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_IP_NUMHOOKS];
255
256         /* Underflow points. */
257         unsigned int underflow[NF_IP_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 ipt_counters __user *counters;
264
265         /* The entries (hang off end: not really an array). */
266         struct ipt_entry entries[0];
267 };
268
269 /* The argument to IPT_SO_ADD_COUNTERS. */
270 struct ipt_counters_info
271 {
272         /* Which table. */
273         char name[IPT_TABLE_MAXNAMELEN];
274
275         unsigned int num_counters;
276
277         /* The counters (actually `number' of these). */
278         struct ipt_counters counters[0];
279 };
280
281 /* The argument to IPT_SO_GET_ENTRIES. */
282 struct ipt_get_entries
283 {
284         /* Which table: user fills this in. */
285         char name[IPT_TABLE_MAXNAMELEN];
286
287         /* User fills this in: total entry size. */
288         unsigned int size;
289
290         /* The entries. */
291         struct ipt_entry entrytable[0];
292 };
293
294 /* The argument to IPT_SO_GET_REVISION_*.  Returns highest revision
295  * kernel supports, if >= revision. */
296 struct ipt_get_revision
297 {
298         char name[IPT_FUNCTION_MAXNAMELEN-1];
299
300         u_int8_t revision;
301 };
302
303 /* Standard return verdict, or do jump. */
304 #define IPT_STANDARD_TARGET ""
305 /* Error verdict. */
306 #define IPT_ERROR_TARGET "ERROR"
307
308 /* Helper functions */
309 static __inline__ struct ipt_entry_target *
310 ipt_get_target(struct ipt_entry *e)
311 {
312         return (void *)e + e->target_offset;
313 }
314
315 /* fn returns 0 to continue iteration */
316 #define IPT_MATCH_ITERATE(e, fn, args...)       \
317 ({                                              \
318         unsigned int __i;                       \
319         int __ret = 0;                          \
320         struct ipt_entry_match *__match;        \
321                                                 \
322         for (__i = sizeof(struct ipt_entry);    \
323              __i < (e)->target_offset;          \
324              __i += __match->u.match_size) {    \
325                 __match = (void *)(e) + __i;    \
326                                                 \
327                 __ret = fn(__match , ## args);  \
328                 if (__ret != 0)                 \
329                         break;                  \
330         }                                       \
331         __ret;                                  \
332 })
333
334 /* fn returns 0 to continue iteration */
335 #define IPT_ENTRY_ITERATE(entries, size, fn, args...)           \
336 ({                                                              \
337         unsigned int __i;                                       \
338         int __ret = 0;                                          \
339         struct ipt_entry *__entry;                              \
340                                                                 \
341         for (__i = 0; __i < (size); __i += __entry->next_offset) { \
342                 __entry = (void *)(entries) + __i;              \
343                                                                 \
344                 __ret = fn(__entry , ## args);                  \
345                 if (__ret != 0)                                 \
346                         break;                                  \
347         }                                                       \
348         __ret;                                                  \
349 })
350
351 /*
352  *      Main firewall chains definitions and global var's definitions.
353  */
354 #ifdef __KERNEL__
355
356 #include <linux/init.h>
357 extern void ipt_init(void) __init;
358
359 struct ipt_match
360 {
361         struct list_head list;
362
363         const char name[IPT_FUNCTION_MAXNAMELEN-1];
364
365         u_int8_t revision;
366
367         /* Return true or false: return FALSE and set *hotdrop = 1 to
368            force immediate packet drop. */
369         /* Arguments changed since 2.4, as this must now handle
370            non-linear skbs, using skb_copy_bits and
371            skb_ip_make_writable. */
372         int (*match)(const struct sk_buff *skb,
373                      const struct net_device *in,
374                      const struct net_device *out,
375                      const void *matchinfo,
376                      int offset,
377                      int *hotdrop);
378
379         /* Called when user tries to insert an entry of this type. */
380         /* Should return true or false. */
381         int (*checkentry)(const char *tablename,
382                           const struct ipt_ip *ip,
383                           void *matchinfo,
384                           unsigned int matchinfosize,
385                           unsigned int hook_mask);
386
387         /* Called when entry of this type deleted. */
388         void (*destroy)(void *matchinfo, unsigned int matchinfosize);
389
390         /* Set this to THIS_MODULE. */
391         struct module *me;
392 };
393
394 /* Registration hooks for targets. */
395 struct ipt_target
396 {
397         struct list_head list;
398
399         const char name[IPT_FUNCTION_MAXNAMELEN-1];
400
401         u_int8_t revision;
402
403         /* Called when user tries to insert an entry of this type:
404            hook_mask is a bitmask of hooks from which it can be
405            called. */
406         /* Should return true or false. */
407         int (*checkentry)(const char *tablename,
408                           const struct ipt_entry *e,
409                           void *targinfo,
410                           unsigned int targinfosize,
411                           unsigned int hook_mask);
412
413         /* Called when entry of this type deleted. */
414         void (*destroy)(void *targinfo, unsigned int targinfosize);
415
416         /* Returns verdict.  Argument order changed since 2.4, as this
417            must now handle non-linear skbs, using skb_copy_bits and
418            skb_ip_make_writable. */
419         unsigned int (*target)(struct sk_buff **pskb,
420                                const struct net_device *in,
421                                const struct net_device *out,
422                                unsigned int hooknum,
423                                const void *targinfo,
424                                void *userdata);
425
426         /* Set this to THIS_MODULE. */
427         struct module *me;
428 };
429
430 extern int ipt_register_target(struct ipt_target *target);
431 extern void ipt_unregister_target(struct ipt_target *target);
432
433 extern int ipt_register_match(struct ipt_match *match);
434 extern void ipt_unregister_match(struct ipt_match *match);
435
436 /* Furniture shopping... */
437 struct ipt_table
438 {
439         struct list_head list;
440
441         /* A unique name... */
442         char name[IPT_TABLE_MAXNAMELEN];
443
444         /* What hooks you will enter on */
445         unsigned int valid_hooks;
446
447         /* Lock for the curtain */
448         rwlock_t lock;
449
450         /* Man behind the curtain... */
451         struct ipt_table_info *private;
452
453         /* Set to THIS_MODULE. */
454         struct module *me;
455 };
456
457 /* net/sched/ipt.c: Gimme access to your targets!  Gets target->me. */
458 extern struct ipt_target *ipt_find_target(const char *name, u8 revision);
459
460 /* Standard entry. */
461 struct ipt_standard
462 {
463         struct ipt_entry entry;
464         struct ipt_standard_target target;
465 };
466
467 struct ipt_error_target
468 {
469         struct ipt_entry_target target;
470         char errorname[IPT_FUNCTION_MAXNAMELEN];
471 };
472
473 struct ipt_error
474 {
475         struct ipt_entry entry;
476         struct ipt_error_target target;
477 };
478
479 extern int ipt_register_table(struct ipt_table *table,
480                               const struct ipt_replace *repl);
481 extern void ipt_unregister_table(struct ipt_table *table);
482 extern unsigned int ipt_do_table(struct sk_buff **pskb,
483                                  unsigned int hook,
484                                  const struct net_device *in,
485                                  const struct net_device *out,
486                                  struct ipt_table *table,
487                                  void *userdata);
488
489 #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1))
490 #endif /*__KERNEL__*/
491 #endif /* _IPTABLES_H */