blob: ee262b5344e11b773bf5bf315201e1eef0a5383a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Harald Welte2e4e6a12006-01-12 13:30:04 -080028#include <linux/netfilter/x_tables.h>
29
30#define IPT_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
31#define IPT_TABLE_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
32#define ipt_match xt_match
33#define ipt_target xt_target
34#define ipt_table xt_table
35#define ipt_get_revision xt_get_revision
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Yes, Virginia, you have to zero the padding. */
38struct ipt_ip {
39 /* Source and destination IP addr */
40 struct in_addr src, dst;
41 /* Mask for src and dest IP addr */
42 struct in_addr smsk, dmsk;
43 char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
44 unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
45
46 /* Protocol, 0 = ANY */
47 u_int16_t proto;
48
49 /* Flags word */
50 u_int8_t flags;
51 /* Inverse flags */
52 u_int8_t invflags;
53};
54
55struct ipt_entry_match
56{
57 union {
58 struct {
59 u_int16_t match_size;
60
61 /* Used by userspace */
62 char name[IPT_FUNCTION_MAXNAMELEN-1];
63
64 u_int8_t revision;
65 } user;
66 struct {
67 u_int16_t match_size;
68
69 /* Used inside the kernel */
70 struct ipt_match *match;
71 } kernel;
72
73 /* Total length */
74 u_int16_t match_size;
75 } u;
76
77 unsigned char data[0];
78};
79
80struct ipt_entry_target
81{
82 union {
83 struct {
84 u_int16_t target_size;
85
86 /* Used by userspace */
87 char name[IPT_FUNCTION_MAXNAMELEN-1];
88
89 u_int8_t revision;
90 } user;
91 struct {
92 u_int16_t target_size;
93
94 /* Used inside the kernel */
95 struct ipt_target *target;
96 } kernel;
97
98 /* Total length */
99 u_int16_t target_size;
100 } u;
101
102 unsigned char data[0];
103};
104
105struct ipt_standard_target
106{
107 struct ipt_entry_target target;
108 int verdict;
109};
110
Harald Welte2e4e6a12006-01-12 13:30:04 -0800111#define ipt_counters xt_counters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* Values for "flag" field in struct ipt_ip (general ip structure). */
114#define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */
Patrick McHardy05465342005-08-21 23:31:43 -0700115#define IPT_F_GOTO 0x02 /* Set if jump is a goto */
116#define IPT_F_MASK 0x03 /* All possible flag bits mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* Values for "inv" field in struct ipt_ip. */
119#define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
120#define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
121#define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */
122#define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
123#define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
124#define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125#define IPT_INV_PROTO XT_INV_PROTO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define IPT_INV_MASK 0x7F /* All possible flag bits mask. */
127
128/* This structure defines each of the firewall rules. Consists of 3
129 parts which are 1) general IP header stuff 2) match specific
130 stuff 3) the target to perform if the rule matches */
131struct ipt_entry
132{
133 struct ipt_ip ip;
134
135 /* Mark with fields that we care about. */
136 unsigned int nfcache;
137
138 /* Size of ipt_entry + matches */
139 u_int16_t target_offset;
140 /* Size of ipt_entry + matches + target */
141 u_int16_t next_offset;
142
143 /* Back pointer */
144 unsigned int comefrom;
145
146 /* Packet and byte counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147 struct xt_counters counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* The matches (if any), then the target. */
150 unsigned char elems[0];
151};
152
153/*
154 * New IP firewall options for [gs]etsockopt at the RAW IP level.
155 * Unlike BSD Linux inherits IP options so you don't have to use a raw
156 * socket for this. Instead we check rights in the calls. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157#define IPT_BASE_CTL XT_BASE_CTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Harald Welte2e4e6a12006-01-12 13:30:04 -0800159#define IPT_SO_SET_REPLACE XT_SO_SET_REPLACE
160#define IPT_SO_SET_ADD_COUNTERS XT_SO_SET_ADD_COUNTERS
161#define IPT_SO_SET_MAX XT_SO_SET_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Harald Welte2e4e6a12006-01-12 13:30:04 -0800163#define IPT_SO_GET_INFO XT_SO_GET_INFO
164#define IPT_SO_GET_ENTRIES XT_SO_GET_ENTRIES
165#define IPT_SO_GET_REVISION_MATCH XT_SO_GET_REVISION_MATCH
166#define IPT_SO_GET_REVISION_TARGET XT_SO_GET_REVISION_TARGET
167#define IPT_SO_GET_MAX XT_SO_GET_REVISION_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Harald Welte2e4e6a12006-01-12 13:30:04 -0800169#define IPT_CONTINUE XT_CONTINUE
170#define IPT_RETURN XT_RETURN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172#include <linux/netfilter/xt_tcpudp.h>
173#define ipt_udp xt_udp
174#define ipt_tcp xt_tcp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Harald Welte2e4e6a12006-01-12 13:30:04 -0800176#define IPT_TCP_INV_SRCPT XT_TCP_INV_SRCPT
177#define IPT_TCP_INV_DSTPT XT_TCP_INV_DSTPT
178#define IPT_TCP_INV_FLAGS XT_TCP_INV_FLAGS
179#define IPT_TCP_INV_OPTION XT_TCP_INV_OPTION
180#define IPT_TCP_INV_MASK XT_TCP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Harald Welte2e4e6a12006-01-12 13:30:04 -0800182#define IPT_UDP_INV_SRCPT XT_UDP_INV_SRCPT
183#define IPT_UDP_INV_DSTPT XT_UDP_INV_DSTPT
184#define IPT_UDP_INV_MASK XT_UDP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* ICMP matching stuff */
187struct ipt_icmp
188{
189 u_int8_t type; /* type to match */
190 u_int8_t code[2]; /* range of code */
191 u_int8_t invflags; /* Inverse flags */
192};
193
194/* Values for "inv" field for struct ipt_icmp. */
195#define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */
196
197/* The argument to IPT_SO_GET_INFO */
198struct ipt_getinfo
199{
200 /* Which table: caller fills this in. */
201 char name[IPT_TABLE_MAXNAMELEN];
202
203 /* Kernel fills these in. */
204 /* Which hook entry points are valid: bitmask */
205 unsigned int valid_hooks;
206
207 /* Hook entry points: one per netfilter hook. */
208 unsigned int hook_entry[NF_IP_NUMHOOKS];
209
210 /* Underflow points. */
211 unsigned int underflow[NF_IP_NUMHOOKS];
212
213 /* Number of entries */
214 unsigned int num_entries;
215
216 /* Size of entries. */
217 unsigned int size;
218};
219
220/* The argument to IPT_SO_SET_REPLACE. */
221struct ipt_replace
222{
223 /* Which table. */
224 char name[IPT_TABLE_MAXNAMELEN];
225
226 /* Which hook entry points are valid: bitmask. You can't
227 change this. */
228 unsigned int valid_hooks;
229
230 /* Number of entries */
231 unsigned int num_entries;
232
233 /* Total size of new entries */
234 unsigned int size;
235
236 /* Hook entry points. */
237 unsigned int hook_entry[NF_IP_NUMHOOKS];
238
239 /* Underflow points. */
240 unsigned int underflow[NF_IP_NUMHOOKS];
241
242 /* Information about old entries: */
243 /* Number of counters (must be equal to current number of entries). */
244 unsigned int num_counters;
245 /* The old entries' counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800246 struct xt_counters __user *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* The entries (hang off end: not really an array). */
249 struct ipt_entry entries[0];
250};
251
252/* The argument to IPT_SO_ADD_COUNTERS. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800253#define ipt_counters_info xt_counters_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/* The argument to IPT_SO_GET_ENTRIES. */
256struct ipt_get_entries
257{
258 /* Which table: user fills this in. */
259 char name[IPT_TABLE_MAXNAMELEN];
260
261 /* User fills this in: total entry size. */
262 unsigned int size;
263
264 /* The entries. */
265 struct ipt_entry entrytable[0];
266};
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* Standard return verdict, or do jump. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800269#define IPT_STANDARD_TARGET XT_STANDARD_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/* Error verdict. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800271#define IPT_ERROR_TARGET XT_ERROR_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/* Helper functions */
274static __inline__ struct ipt_entry_target *
275ipt_get_target(struct ipt_entry *e)
276{
277 return (void *)e + e->target_offset;
278}
279
280/* fn returns 0 to continue iteration */
281#define IPT_MATCH_ITERATE(e, fn, args...) \
282({ \
283 unsigned int __i; \
284 int __ret = 0; \
285 struct ipt_entry_match *__match; \
286 \
287 for (__i = sizeof(struct ipt_entry); \
288 __i < (e)->target_offset; \
289 __i += __match->u.match_size) { \
290 __match = (void *)(e) + __i; \
291 \
292 __ret = fn(__match , ## args); \
293 if (__ret != 0) \
294 break; \
295 } \
296 __ret; \
297})
298
299/* fn returns 0 to continue iteration */
300#define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
301({ \
302 unsigned int __i; \
303 int __ret = 0; \
304 struct ipt_entry *__entry; \
305 \
306 for (__i = 0; __i < (size); __i += __entry->next_offset) { \
307 __entry = (void *)(entries) + __i; \
308 \
309 __ret = fn(__entry , ## args); \
310 if (__ret != 0) \
311 break; \
312 } \
313 __ret; \
314})
315
316/*
317 * Main firewall chains definitions and global var's definitions.
318 */
319#ifdef __KERNEL__
320
321#include <linux/init.h>
322extern void ipt_init(void) __init;
323
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800324#define ipt_register_target(tgt) \
325({ (tgt)->family = AF_INET; \
326 xt_register_target(tgt); })
327#define ipt_unregister_target(tgt) xt_unregister_target(tgt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800329#define ipt_register_match(mtch) \
330({ (mtch)->family = AF_INET; \
331 xt_register_match(mtch); })
332#define ipt_unregister_match(mtch) xt_unregister_match(mtch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Harald Welte2e4e6a12006-01-12 13:30:04 -0800334//#define ipt_register_table(tbl, repl) xt_register_table(AF_INET, tbl, repl)
335//#define ipt_unregister_table(tbl) xt_unregister_table(AF_INET, tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Harald Welte2e4e6a12006-01-12 13:30:04 -0800337extern int ipt_register_table(struct ipt_table *table,
338 const struct ipt_replace *repl);
339extern void ipt_unregister_table(struct ipt_table *table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */
342extern struct ipt_target *ipt_find_target(const char *name, u8 revision);
343
344/* Standard entry. */
345struct ipt_standard
346{
347 struct ipt_entry entry;
348 struct ipt_standard_target target;
349};
350
351struct ipt_error_target
352{
353 struct ipt_entry_target target;
354 char errorname[IPT_FUNCTION_MAXNAMELEN];
355};
356
357struct ipt_error
358{
359 struct ipt_entry entry;
360 struct ipt_error_target target;
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363extern unsigned int ipt_do_table(struct sk_buff **pskb,
364 unsigned int hook,
365 const struct net_device *in,
366 const struct net_device *out,
367 struct ipt_table *table,
368 void *userdata);
369
Harald Welte2e4e6a12006-01-12 13:30:04 -0800370#define IPT_ALIGN(s) XT_ALIGN(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#endif /*__KERNEL__*/
372#endif /* _IPTABLES_H */