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 #define IPT_FUNCTION_MAXNAMELEN 30
29 #define IPT_TABLE_MAXNAMELEN 32
31 /* Yes, Virginia, you have to zero the padding. */
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];
40 /* Protocol, 0 = ANY */
49 struct ipt_entry_match
55 /* Used by userspace */
56 char name[IPT_FUNCTION_MAXNAMELEN-1];
63 /* Used inside the kernel */
64 struct ipt_match *match;
71 unsigned char data[0];
74 struct ipt_entry_target
78 u_int16_t target_size;
80 /* Used by userspace */
81 char name[IPT_FUNCTION_MAXNAMELEN-1];
86 u_int16_t target_size;
88 /* Used inside the kernel */
89 struct ipt_target *target;
93 u_int16_t target_size;
96 unsigned char data[0];
99 struct ipt_standard_target
101 struct ipt_entry_target target;
107 u_int64_t pcnt, bcnt; /* Packet and byte counters */
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. */
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. */
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 */
132 /* Mark with fields that we care about. */
133 unsigned int nfcache;
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;
141 unsigned int comefrom;
143 /* Packet and byte counters. */
144 struct ipt_counters counters;
146 /* The matches (if any), then the target. */
147 unsigned char elems[0];
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 */
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
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
166 /* CONTINUE verdict for targets */
167 #define IPT_CONTINUE 0xFFFFFFFF
169 /* For standard target */
170 #define IPT_RETURN (-NF_REPEAT - 1)
172 /* TCP matching stuff */
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 */
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. */
190 /* UDP matching stuff */
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 */
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. */
203 /* ICMP matching stuff */
206 u_int8_t type; /* type to match */
207 u_int8_t code[2]; /* range of code */
208 u_int8_t invflags; /* Inverse flags */
211 /* Values for "inv" field for struct ipt_icmp. */
212 #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */
214 /* The argument to IPT_SO_GET_INFO */
217 /* Which table: caller fills this in. */
218 char name[IPT_TABLE_MAXNAMELEN];
220 /* Kernel fills these in. */
221 /* Which hook entry points are valid: bitmask */
222 unsigned int valid_hooks;
224 /* Hook entry points: one per netfilter hook. */
225 unsigned int hook_entry[NF_IP_NUMHOOKS];
227 /* Underflow points. */
228 unsigned int underflow[NF_IP_NUMHOOKS];
230 /* Number of entries */
231 unsigned int num_entries;
233 /* Size of entries. */
237 /* The argument to IPT_SO_SET_REPLACE. */
241 char name[IPT_TABLE_MAXNAMELEN];
243 /* Which hook entry points are valid: bitmask. You can't
245 unsigned int valid_hooks;
247 /* Number of entries */
248 unsigned int num_entries;
250 /* Total size of new entries */
253 /* Hook entry points. */
254 unsigned int hook_entry[NF_IP_NUMHOOKS];
256 /* Underflow points. */
257 unsigned int underflow[NF_IP_NUMHOOKS];
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;
265 /* The entries (hang off end: not really an array). */
266 struct ipt_entry entries[0];
269 /* The argument to IPT_SO_ADD_COUNTERS. */
270 struct ipt_counters_info
273 char name[IPT_TABLE_MAXNAMELEN];
275 unsigned int num_counters;
277 /* The counters (actually `number' of these). */
278 struct ipt_counters counters[0];
281 /* The argument to IPT_SO_GET_ENTRIES. */
282 struct ipt_get_entries
284 /* Which table: user fills this in. */
285 char name[IPT_TABLE_MAXNAMELEN];
287 /* User fills this in: total entry size. */
291 struct ipt_entry entrytable[0];
294 /* The argument to IPT_SO_GET_REVISION_*. Returns highest revision
295 * kernel supports, if >= revision. */
296 struct ipt_get_revision
298 char name[IPT_FUNCTION_MAXNAMELEN-1];
303 /* Standard return verdict, or do jump. */
304 #define IPT_STANDARD_TARGET ""
306 #define IPT_ERROR_TARGET "ERROR"
308 /* Helper functions */
309 static __inline__ struct ipt_entry_target *
310 ipt_get_target(struct ipt_entry *e)
312 return (void *)e + e->target_offset;
315 /* fn returns 0 to continue iteration */
316 #define IPT_MATCH_ITERATE(e, fn, args...) \
320 struct ipt_entry_match *__match; \
322 for (__i = sizeof(struct ipt_entry); \
323 __i < (e)->target_offset; \
324 __i += __match->u.match_size) { \
325 __match = (void *)(e) + __i; \
327 __ret = fn(__match , ## args); \
334 /* fn returns 0 to continue iteration */
335 #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
339 struct ipt_entry *__entry; \
341 for (__i = 0; __i < (size); __i += __entry->next_offset) { \
342 __entry = (void *)(entries) + __i; \
344 __ret = fn(__entry , ## args); \
352 * Main firewall chains definitions and global var's definitions.
356 #include <linux/init.h>
357 extern void ipt_init(void) __init;
361 struct list_head list;
363 const char name[IPT_FUNCTION_MAXNAMELEN-1];
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,
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,
384 unsigned int matchinfosize,
385 unsigned int hook_mask);
387 /* Called when entry of this type deleted. */
388 void (*destroy)(void *matchinfo, unsigned int matchinfosize);
390 /* Set this to THIS_MODULE. */
394 /* Registration hooks for targets. */
397 struct list_head list;
399 const char name[IPT_FUNCTION_MAXNAMELEN-1];
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
406 /* Should return true or false. */
407 int (*checkentry)(const char *tablename,
408 const struct ipt_entry *e,
410 unsigned int targinfosize,
411 unsigned int hook_mask);
413 /* Called when entry of this type deleted. */
414 void (*destroy)(void *targinfo, unsigned int targinfosize);
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,
426 /* Set this to THIS_MODULE. */
430 extern int ipt_register_target(struct ipt_target *target);
431 extern void ipt_unregister_target(struct ipt_target *target);
433 extern int ipt_register_match(struct ipt_match *match);
434 extern void ipt_unregister_match(struct ipt_match *match);
436 /* Furniture shopping... */
439 struct list_head list;
441 /* A unique name... */
442 char name[IPT_TABLE_MAXNAMELEN];
444 /* What hooks you will enter on */
445 unsigned int valid_hooks;
447 /* Lock for the curtain */
450 /* Man behind the curtain... */
451 struct ipt_table_info *private;
453 /* Set to THIS_MODULE. */
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);
460 /* Standard entry. */
463 struct ipt_entry entry;
464 struct ipt_standard_target target;
467 struct ipt_error_target
469 struct ipt_entry_target target;
470 char errorname[IPT_FUNCTION_MAXNAMELEN];
475 struct ipt_entry entry;
476 struct ipt_error_target target;
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,
484 const struct net_device *in,
485 const struct net_device *out,
486 struct ipt_table *table,
489 #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1))
490 #endif /*__KERNEL__*/
491 #endif /* _IPTABLES_H */