2 * 25-Jul-1998 Major changes to allow for ip chain table
4 * 3-Jan-2000 Named tables to allow packet selection for different uses.
8 * Format of an IP firewall descriptor
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.
20 #include <linux/types.h>
23 #include <linux/skbuff.h>
25 #include <linux/compiler.h>
26 #include <linux/netfilter_ipv4.h>
28 #include <linux/netfilter/x_tables.h>
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
37 /* Yes, Virginia, you have to zero the padding. */
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];
46 /* Protocol, 0 = ANY */
55 struct ipt_entry_match
61 /* Used by userspace */
62 char name[IPT_FUNCTION_MAXNAMELEN-1];
69 /* Used inside the kernel */
70 struct ipt_match *match;
77 unsigned char data[0];
80 struct ipt_entry_target
84 u_int16_t target_size;
86 /* Used by userspace */
87 char name[IPT_FUNCTION_MAXNAMELEN-1];
92 u_int16_t target_size;
94 /* Used inside the kernel */
95 struct ipt_target *target;
99 u_int16_t target_size;
102 unsigned char data[0];
105 struct ipt_standard_target
107 struct ipt_entry_target target;
111 #define ipt_counters xt_counters
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 */
115 #define IPT_F_GOTO 0x02 /* Set if jump is a goto */
116 #define IPT_F_MASK 0x03 /* All possible flag bits mask. */
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. */
125 #define IPT_INV_PROTO XT_INV_PROTO
126 #define IPT_INV_MASK 0x7F /* All possible flag bits mask. */
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 */
135 /* Mark with fields that we care about. */
136 unsigned int nfcache;
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;
144 unsigned int comefrom;
146 /* Packet and byte counters. */
147 struct xt_counters counters;
149 /* The matches (if any), then the target. */
150 unsigned char elems[0];
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. */
157 #define IPT_BASE_CTL XT_BASE_CTL
159 #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
163 #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
169 #define IPT_CONTINUE XT_CONTINUE
170 #define IPT_RETURN XT_RETURN
172 #include <linux/netfilter/xt_tcpudp.h>
173 #define ipt_udp xt_udp
174 #define ipt_tcp xt_tcp
176 #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
182 #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
186 /* ICMP matching stuff */
189 u_int8_t type; /* type to match */
190 u_int8_t code[2]; /* range of code */
191 u_int8_t invflags; /* Inverse flags */
194 /* Values for "inv" field for struct ipt_icmp. */
195 #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */
197 /* The argument to IPT_SO_GET_INFO */
200 /* Which table: caller fills this in. */
201 char name[IPT_TABLE_MAXNAMELEN];
203 /* Kernel fills these in. */
204 /* Which hook entry points are valid: bitmask */
205 unsigned int valid_hooks;
207 /* Hook entry points: one per netfilter hook. */
208 unsigned int hook_entry[NF_IP_NUMHOOKS];
210 /* Underflow points. */
211 unsigned int underflow[NF_IP_NUMHOOKS];
213 /* Number of entries */
214 unsigned int num_entries;
216 /* Size of entries. */
220 /* The argument to IPT_SO_SET_REPLACE. */
224 char name[IPT_TABLE_MAXNAMELEN];
226 /* Which hook entry points are valid: bitmask. You can't
228 unsigned int valid_hooks;
230 /* Number of entries */
231 unsigned int num_entries;
233 /* Total size of new entries */
236 /* Hook entry points. */
237 unsigned int hook_entry[NF_IP_NUMHOOKS];
239 /* Underflow points. */
240 unsigned int underflow[NF_IP_NUMHOOKS];
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. */
246 struct xt_counters __user *counters;
248 /* The entries (hang off end: not really an array). */
249 struct ipt_entry entries[0];
252 /* The argument to IPT_SO_ADD_COUNTERS. */
253 #define ipt_counters_info xt_counters_info
255 /* The argument to IPT_SO_GET_ENTRIES. */
256 struct ipt_get_entries
258 /* Which table: user fills this in. */
259 char name[IPT_TABLE_MAXNAMELEN];
261 /* User fills this in: total entry size. */
265 struct ipt_entry entrytable[0];
268 /* Standard return verdict, or do jump. */
269 #define IPT_STANDARD_TARGET XT_STANDARD_TARGET
271 #define IPT_ERROR_TARGET XT_ERROR_TARGET
273 /* Helper functions */
274 static __inline__ struct ipt_entry_target *
275 ipt_get_target(struct ipt_entry *e)
277 return (void *)e + e->target_offset;
280 /* fn returns 0 to continue iteration */
281 #define IPT_MATCH_ITERATE(e, fn, args...) \
285 struct ipt_entry_match *__match; \
287 for (__i = sizeof(struct ipt_entry); \
288 __i < (e)->target_offset; \
289 __i += __match->u.match_size) { \
290 __match = (void *)(e) + __i; \
292 __ret = fn(__match , ## args); \
299 /* fn returns 0 to continue iteration */
300 #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
304 struct ipt_entry *__entry; \
306 for (__i = 0; __i < (size); __i += __entry->next_offset) { \
307 __entry = (void *)(entries) + __i; \
309 __ret = fn(__entry , ## args); \
317 * Main firewall chains definitions and global var's definitions.
321 #include <linux/init.h>
322 extern void ipt_init(void) __init;
324 #define ipt_register_target(tgt) xt_register_target(AF_INET, tgt)
325 #define ipt_unregister_target(tgt) xt_unregister_target(AF_INET, tgt)
327 #define ipt_register_match(mtch) xt_register_match(AF_INET, mtch)
328 #define ipt_unregister_match(mtch) xt_unregister_match(AF_INET, mtch)
330 //#define ipt_register_table(tbl, repl) xt_register_table(AF_INET, tbl, repl)
331 //#define ipt_unregister_table(tbl) xt_unregister_table(AF_INET, tbl)
333 extern int ipt_register_table(struct ipt_table *table,
334 const struct ipt_replace *repl);
335 extern void ipt_unregister_table(struct ipt_table *table);
337 /* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */
338 extern struct ipt_target *ipt_find_target(const char *name, u8 revision);
340 /* Standard entry. */
343 struct ipt_entry entry;
344 struct ipt_standard_target target;
347 struct ipt_error_target
349 struct ipt_entry_target target;
350 char errorname[IPT_FUNCTION_MAXNAMELEN];
355 struct ipt_entry entry;
356 struct ipt_error_target target;
359 extern unsigned int ipt_do_table(struct sk_buff **pskb,
361 const struct net_device *in,
362 const struct net_device *out,
363 struct ipt_table *table,
366 #define IPT_ALIGN(s) XT_ALIGN(s)
367 #endif /*__KERNEL__*/
368 #endif /* _IPTABLES_H */