VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.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];
57                 } user;
58                 struct {
59                         u_int16_t match_size;
60
61                         /* Used inside the kernel */
62                         struct ipt_match *match;
63                 } kernel;
64
65                 /* Total length */
66                 u_int16_t match_size;
67         } u;
68
69         unsigned char data[0];
70 };
71
72 struct ipt_entry_target
73 {
74         union {
75                 struct {
76                         u_int16_t target_size;
77
78                         /* Used by userspace */
79                         char name[IPT_FUNCTION_MAXNAMELEN];
80                 } user;
81                 struct {
82                         u_int16_t target_size;
83
84                         /* Used inside the kernel */
85                         struct ipt_target *target;
86                 } kernel;
87
88                 /* Total length */
89                 u_int16_t target_size;
90         } u;
91
92         unsigned char data[0];
93 };
94
95 struct ipt_standard_target
96 {
97         struct ipt_entry_target target;
98         int verdict;
99 };
100
101 struct ipt_counters
102 {
103         u_int64_t pcnt, bcnt;                   /* Packet and byte counters */
104 };
105
106 /* Values for "flag" field in struct ipt_ip (general ip structure). */
107 #define IPT_F_FRAG              0x01    /* Set if rule is a fragment rule */
108 #define IPT_F_MASK              0x01    /* All possible flag bits mask. */
109
110 /* Values for "inv" field in struct ipt_ip. */
111 #define IPT_INV_VIA_IN          0x01    /* Invert the sense of IN IFACE. */
112 #define IPT_INV_VIA_OUT         0x02    /* Invert the sense of OUT IFACE */
113 #define IPT_INV_TOS             0x04    /* Invert the sense of TOS. */
114 #define IPT_INV_SRCIP           0x08    /* Invert the sense of SRC IP. */
115 #define IPT_INV_DSTIP           0x10    /* Invert the sense of DST OP. */
116 #define IPT_INV_FRAG            0x20    /* Invert the sense of FRAG. */
117 #define IPT_INV_PROTO           0x40    /* Invert the sense of PROTO. */
118 #define IPT_INV_MASK            0x7F    /* All possible flag bits mask. */
119
120 /* This structure defines each of the firewall rules.  Consists of 3
121    parts which are 1) general IP header stuff 2) match specific
122    stuff 3) the target to perform if the rule matches */
123 struct ipt_entry
124 {
125         struct ipt_ip ip;
126
127         /* Mark with fields that we care about. */
128         unsigned int nfcache;
129
130         /* Size of ipt_entry + matches */
131         u_int16_t target_offset;
132         /* Size of ipt_entry + matches + target */
133         u_int16_t next_offset;
134
135         /* Back pointer */
136         unsigned int comefrom;
137
138         /* Packet and byte counters. */
139         struct ipt_counters counters;
140
141         /* The matches (if any), then the target. */
142         unsigned char elems[0];
143 };
144
145 /*
146  * New IP firewall options for [gs]etsockopt at the RAW IP level.
147  * Unlike BSD Linux inherits IP options so you don't have to use a raw
148  * socket for this. Instead we check rights in the calls. */
149 #define IPT_BASE_CTL            64      /* base for firewall socket options */
150
151 #define IPT_SO_SET_REPLACE      (IPT_BASE_CTL)
152 #define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1)
153 #define IPT_SO_SET_MAX          IPT_SO_SET_ADD_COUNTERS
154
155 #define IPT_SO_GET_INFO         (IPT_BASE_CTL)
156 #define IPT_SO_GET_ENTRIES      (IPT_BASE_CTL + 1)
157 #define IPT_SO_GET_MAX          IPT_SO_GET_ENTRIES
158
159 /* CONTINUE verdict for targets */
160 #define IPT_CONTINUE 0xFFFFFFFF
161
162 /* For standard target */
163 #define IPT_RETURN (-NF_MAX_VERDICT - 1)
164
165 /* TCP matching stuff */
166 struct ipt_tcp
167 {
168         u_int16_t spts[2];                      /* Source port range. */
169         u_int16_t dpts[2];                      /* Destination port range. */
170         u_int8_t option;                        /* TCP Option iff non-zero*/
171         u_int8_t flg_mask;                      /* TCP flags mask byte */
172         u_int8_t flg_cmp;                       /* TCP flags compare byte */
173         u_int8_t invflags;                      /* Inverse flags */
174 };
175
176 /* Values for "inv" field in struct ipt_tcp. */
177 #define IPT_TCP_INV_SRCPT       0x01    /* Invert the sense of source ports. */
178 #define IPT_TCP_INV_DSTPT       0x02    /* Invert the sense of dest ports. */
179 #define IPT_TCP_INV_FLAGS       0x04    /* Invert the sense of TCP flags. */
180 #define IPT_TCP_INV_OPTION      0x08    /* Invert the sense of option test. */
181 #define IPT_TCP_INV_MASK        0x0F    /* All possible flags. */
182
183 /* UDP matching stuff */
184 struct ipt_udp
185 {
186         u_int16_t spts[2];                      /* Source port range. */
187         u_int16_t dpts[2];                      /* Destination port range. */
188         u_int8_t invflags;                      /* Inverse flags */
189 };
190
191 /* Values for "invflags" field in struct ipt_udp. */
192 #define IPT_UDP_INV_SRCPT       0x01    /* Invert the sense of source ports. */
193 #define IPT_UDP_INV_DSTPT       0x02    /* Invert the sense of dest ports. */
194 #define IPT_UDP_INV_MASK        0x03    /* All possible flags. */
195
196 /* ICMP matching stuff */
197 struct ipt_icmp
198 {
199         u_int8_t type;                          /* type to match */
200         u_int8_t code[2];                       /* range of code */
201         u_int8_t invflags;                      /* Inverse flags */
202 };
203
204 /* Values for "inv" field for struct ipt_icmp. */
205 #define IPT_ICMP_INV    0x01    /* Invert the sense of type/code test */
206
207 /* The argument to IPT_SO_GET_INFO */
208 struct ipt_getinfo
209 {
210         /* Which table: caller fills this in. */
211         char name[IPT_TABLE_MAXNAMELEN];
212
213         /* Kernel fills these in. */
214         /* Which hook entry points are valid: bitmask */
215         unsigned int valid_hooks;
216
217         /* Hook entry points: one per netfilter hook. */
218         unsigned int hook_entry[NF_IP_NUMHOOKS];
219
220         /* Underflow points. */
221         unsigned int underflow[NF_IP_NUMHOOKS];
222
223         /* Number of entries */
224         unsigned int num_entries;
225
226         /* Size of entries. */
227         unsigned int size;
228 };
229
230 /* The argument to IPT_SO_SET_REPLACE. */
231 struct ipt_replace
232 {
233         /* Which table. */
234         char name[IPT_TABLE_MAXNAMELEN];
235
236         /* Which hook entry points are valid: bitmask.  You can't
237            change this. */
238         unsigned int valid_hooks;
239
240         /* Number of entries */
241         unsigned int num_entries;
242
243         /* Total size of new entries */
244         unsigned int size;
245
246         /* Hook entry points. */
247         unsigned int hook_entry[NF_IP_NUMHOOKS];
248
249         /* Underflow points. */
250         unsigned int underflow[NF_IP_NUMHOOKS];
251
252         /* Information about old entries: */
253         /* Number of counters (must be equal to current number of entries). */
254         unsigned int num_counters;
255         /* The old entries' counters. */
256         struct ipt_counters __user *counters;
257
258         /* The entries (hang off end: not really an array). */
259         struct ipt_entry entries[0];
260 };
261
262 /* The argument to IPT_SO_ADD_COUNTERS. */
263 struct ipt_counters_info
264 {
265         /* Which table. */
266         char name[IPT_TABLE_MAXNAMELEN];
267
268         unsigned int num_counters;
269
270         /* The counters (actually `number' of these). */
271         struct ipt_counters counters[0];
272 };
273
274 /* The argument to IPT_SO_GET_ENTRIES. */
275 struct ipt_get_entries
276 {
277         /* Which table: user fills this in. */
278         char name[IPT_TABLE_MAXNAMELEN];
279
280         /* User fills this in: total entry size. */
281         unsigned int size;
282
283         /* The entries. */
284         struct ipt_entry entrytable[0];
285 };
286
287 /* Standard return verdict, or do jump. */
288 #define IPT_STANDARD_TARGET ""
289 /* Error verdict. */
290 #define IPT_ERROR_TARGET "ERROR"
291
292 /* Helper functions */
293 static __inline__ struct ipt_entry_target *
294 ipt_get_target(struct ipt_entry *e)
295 {
296         return (void *)e + e->target_offset;
297 }
298
299 /* fn returns 0 to continue iteration */
300 #define IPT_MATCH_ITERATE(e, fn, args...)       \
301 ({                                              \
302         unsigned int __i;                       \
303         int __ret = 0;                          \
304         struct ipt_entry_match *__match;        \
305                                                 \
306         for (__i = sizeof(struct ipt_entry);    \
307              __i < (e)->target_offset;          \
308              __i += __match->u.match_size) {    \
309                 __match = (void *)(e) + __i;    \
310                                                 \
311                 __ret = fn(__match , ## args);  \
312                 if (__ret != 0)                 \
313                         break;                  \
314         }                                       \
315         __ret;                                  \
316 })
317
318 /* fn returns 0 to continue iteration */
319 #define IPT_ENTRY_ITERATE(entries, size, fn, args...)           \
320 ({                                                              \
321         unsigned int __i;                                       \
322         int __ret = 0;                                          \
323         struct ipt_entry *__entry;                              \
324                                                                 \
325         for (__i = 0; __i < (size); __i += __entry->next_offset) { \
326                 __entry = (void *)(entries) + __i;              \
327                                                                 \
328                 __ret = fn(__entry , ## args);                  \
329                 if (__ret != 0)                                 \
330                         break;                                  \
331         }                                                       \
332         __ret;                                                  \
333 })
334
335 /*
336  *      Main firewall chains definitions and global var's definitions.
337  */
338 #ifdef __KERNEL__
339 static DECLARE_MUTEX(ipt_mutex);
340
341 #include <linux/init.h>
342 extern void ipt_init(void) __init;
343
344 struct ipt_match
345 {
346         struct list_head list;
347
348         const char name[IPT_FUNCTION_MAXNAMELEN];
349
350         /* Return true or false: return FALSE and set *hotdrop = 1 to
351            force immediate packet drop. */
352         /* Arguments changed since 2.4, as this must now handle
353            non-linear skbs, using skb_copy_bits and
354            skb_ip_make_writable. */
355         int (*match)(const struct sk_buff *skb,
356                      const struct net_device *in,
357                      const struct net_device *out,
358                      const void *matchinfo,
359                      int offset,
360                      int *hotdrop);
361
362         /* Called when user tries to insert an entry of this type. */
363         /* Should return true or false. */
364         int (*checkentry)(const char *tablename,
365                           const struct ipt_ip *ip,
366                           void *matchinfo,
367                           unsigned int matchinfosize,
368                           unsigned int hook_mask);
369
370         /* Called when entry of this type deleted. */
371         void (*destroy)(void *matchinfo, unsigned int matchinfosize);
372
373         /* Set this to THIS_MODULE. */
374         struct module *me;
375 };
376
377 /* Registration hooks for targets. */
378 struct ipt_target
379 {
380         struct list_head list;
381
382         const char name[IPT_FUNCTION_MAXNAMELEN];
383
384         /* Called when user tries to insert an entry of this type:
385            hook_mask is a bitmask of hooks from which it can be
386            called. */
387         /* Should return true or false. */
388         int (*checkentry)(const char *tablename,
389                           const struct ipt_entry *e,
390                           void *targinfo,
391                           unsigned int targinfosize,
392                           unsigned int hook_mask);
393
394         /* Called when entry of this type deleted. */
395         void (*destroy)(void *targinfo, unsigned int targinfosize);
396
397         /* Returns verdict.  Argument order changed since 2.4, as this
398            must now handle non-linear skbs, using skb_copy_bits and
399            skb_ip_make_writable. */
400         unsigned int (*target)(struct sk_buff **pskb,
401                                const struct net_device *in,
402                                const struct net_device *out,
403                                unsigned int hooknum,
404                                const void *targinfo,
405                                void *userdata);
406
407         /* Set this to THIS_MODULE. */
408         struct module *me;
409 };
410
411 extern struct ipt_target *
412 ipt_find_target_lock(const char *name, int *error, struct semaphore *mutex);
413 extern struct arpt_target *
414 arpt_find_target_lock(const char *name, int *error, struct semaphore *mutex);
415
416 extern int ipt_register_target(struct ipt_target *target);
417 extern void ipt_unregister_target(struct ipt_target *target);
418
419 extern int ipt_register_match(struct ipt_match *match);
420 extern void ipt_unregister_match(struct ipt_match *match);
421
422 /* Furniture shopping... */
423 struct ipt_table
424 {
425         struct list_head list;
426
427         /* A unique name... */
428         char name[IPT_TABLE_MAXNAMELEN];
429
430         /* Seed table: copied in register_table */
431         struct ipt_replace *table;
432
433         /* What hooks you will enter on */
434         unsigned int valid_hooks;
435
436         /* Lock for the curtain */
437         rwlock_t lock;
438
439         /* Man behind the curtain... */
440         struct ipt_table_info *private;
441
442         /* Set to THIS_MODULE. */
443         struct module *me;
444 };
445
446 extern int ipt_register_table(struct ipt_table *table);
447 extern void ipt_unregister_table(struct ipt_table *table);
448 extern unsigned int ipt_do_table(struct sk_buff **pskb,
449                                  unsigned int hook,
450                                  const struct net_device *in,
451                                  const struct net_device *out,
452                                  struct ipt_table *table,
453                                  void *userdata);
454
455 #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1))
456 #endif /*__KERNEL__*/
457 #endif /* _IPTABLES_H */