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/netfilter_ipv4.h>
27 #define IPT_FUNCTION_MAXNAMELEN 30
28 #define IPT_TABLE_MAXNAMELEN 32
30 /* Yes, Virginia, you have to zero the padding. */
32 /* Source and destination IP addr */
33 struct in_addr src, dst;
34 /* Mask for src and dest IP addr */
35 struct in_addr smsk, dmsk;
36 char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
37 unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
39 /* Protocol, 0 = ANY */
48 struct ipt_entry_match
54 /* Used by userspace */
55 char name[IPT_FUNCTION_MAXNAMELEN];
60 /* Used inside the kernel */
61 struct ipt_match *match;
68 unsigned char data[0];
71 struct ipt_entry_target
75 u_int16_t target_size;
77 /* Used by userspace */
78 char name[IPT_FUNCTION_MAXNAMELEN];
81 u_int16_t target_size;
83 /* Used inside the kernel */
84 struct ipt_target *target;
88 u_int16_t target_size;
91 unsigned char data[0];
94 struct ipt_standard_target
96 struct ipt_entry_target target;
102 u_int64_t pcnt, bcnt; /* Packet and byte counters */
105 /* Values for "flag" field in struct ipt_ip (general ip structure). */
106 #define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */
107 #define IPT_F_MASK 0x01 /* All possible flag bits mask. */
109 /* Values for "inv" field in struct ipt_ip. */
110 #define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
111 #define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
112 #define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */
113 #define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
114 #define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
115 #define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */
116 #define IPT_INV_PROTO 0x40 /* Invert the sense of PROTO. */
117 #define IPT_INV_MASK 0x7F /* All possible flag bits mask. */
119 /* This structure defines each of the firewall rules. Consists of 3
120 parts which are 1) general IP header stuff 2) match specific
121 stuff 3) the target to perform if the rule matches */
126 /* Mark with fields that we care about. */
127 unsigned int nfcache;
129 /* Size of ipt_entry + matches */
130 u_int16_t target_offset;
131 /* Size of ipt_entry + matches + target */
132 u_int16_t next_offset;
135 unsigned int comefrom;
137 /* Packet and byte counters. */
138 struct ipt_counters counters;
140 /* The matches (if any), then the target. */
141 unsigned char elems[0];
145 * New IP firewall options for [gs]etsockopt at the RAW IP level.
146 * Unlike BSD Linux inherits IP options so you don't have to use a raw
147 * socket for this. Instead we check rights in the calls. */
148 #define IPT_BASE_CTL 64 /* base for firewall socket options */
150 #define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
151 #define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1)
152 #define IPT_SO_SET_MAX IPT_SO_SET_ADD_COUNTERS
154 #define IPT_SO_GET_INFO (IPT_BASE_CTL)
155 #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)
156 #define IPT_SO_GET_MAX IPT_SO_GET_ENTRIES
158 /* CONTINUE verdict for targets */
159 #define IPT_CONTINUE 0xFFFFFFFF
161 /* For standard target */
162 #define IPT_RETURN (-NF_MAX_VERDICT - 1)
164 /* TCP matching stuff */
167 u_int16_t spts[2]; /* Source port range. */
168 u_int16_t dpts[2]; /* Destination port range. */
169 u_int8_t option; /* TCP Option iff non-zero*/
170 u_int8_t flg_mask; /* TCP flags mask byte */
171 u_int8_t flg_cmp; /* TCP flags compare byte */
172 u_int8_t invflags; /* Inverse flags */
175 /* Values for "inv" field in struct ipt_tcp. */
176 #define IPT_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
177 #define IPT_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
178 #define IPT_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */
179 #define IPT_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */
180 #define IPT_TCP_INV_MASK 0x0F /* All possible flags. */
182 /* UDP matching stuff */
185 u_int16_t spts[2]; /* Source port range. */
186 u_int16_t dpts[2]; /* Destination port range. */
187 u_int8_t invflags; /* Inverse flags */
190 /* Values for "invflags" field in struct ipt_udp. */
191 #define IPT_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
192 #define IPT_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
193 #define IPT_UDP_INV_MASK 0x03 /* All possible flags. */
195 /* ICMP matching stuff */
198 u_int8_t type; /* type to match */
199 u_int8_t code[2]; /* range of code */
200 u_int8_t invflags; /* Inverse flags */
203 /* Values for "inv" field for struct ipt_icmp. */
204 #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */
206 /* The argument to IPT_SO_GET_INFO */
209 /* Which table: caller fills this in. */
210 char name[IPT_TABLE_MAXNAMELEN];
212 /* Kernel fills these in. */
213 /* Which hook entry points are valid: bitmask */
214 unsigned int valid_hooks;
216 /* Hook entry points: one per netfilter hook. */
217 unsigned int hook_entry[NF_IP_NUMHOOKS];
219 /* Underflow points. */
220 unsigned int underflow[NF_IP_NUMHOOKS];
222 /* Number of entries */
223 unsigned int num_entries;
225 /* Size of entries. */
229 /* The argument to IPT_SO_SET_REPLACE. */
233 char name[IPT_TABLE_MAXNAMELEN];
235 /* Which hook entry points are valid: bitmask. You can't
237 unsigned int valid_hooks;
239 /* Number of entries */
240 unsigned int num_entries;
242 /* Total size of new entries */
245 /* Hook entry points. */
246 unsigned int hook_entry[NF_IP_NUMHOOKS];
248 /* Underflow points. */
249 unsigned int underflow[NF_IP_NUMHOOKS];
251 /* Information about old entries: */
252 /* Number of counters (must be equal to current number of entries). */
253 unsigned int num_counters;
254 /* The old entries' counters. */
255 struct ipt_counters __user *counters;
257 /* The entries (hang off end: not really an array). */
258 struct ipt_entry entries[0];
261 /* The argument to IPT_SO_ADD_COUNTERS. */
262 struct ipt_counters_info
265 char name[IPT_TABLE_MAXNAMELEN];
267 unsigned int num_counters;
269 /* The counters (actually `number' of these). */
270 struct ipt_counters counters[0];
273 /* The argument to IPT_SO_GET_ENTRIES. */
274 struct ipt_get_entries
276 /* Which table: user fills this in. */
277 char name[IPT_TABLE_MAXNAMELEN];
279 /* User fills this in: total entry size. */
283 struct ipt_entry entrytable[0];
286 extern struct semaphore ipt_mutex;
288 /* Standard return verdict, or do jump. */
289 #define IPT_STANDARD_TARGET ""
291 #define IPT_ERROR_TARGET "ERROR"
293 /* Helper functions */
294 static __inline__ struct ipt_entry_target *
295 ipt_get_target(struct ipt_entry *e)
297 return (void *)e + e->target_offset;
300 /* fn returns 0 to continue iteration */
301 #define IPT_MATCH_ITERATE(e, fn, args...) \
305 struct ipt_entry_match *__match; \
307 for (__i = sizeof(struct ipt_entry); \
308 __i < (e)->target_offset; \
309 __i += __match->u.match_size) { \
310 __match = (void *)(e) + __i; \
312 __ret = fn(__match , ## args); \
319 /* fn returns 0 to continue iteration */
320 #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
324 struct ipt_entry *__entry; \
326 for (__i = 0; __i < (size); __i += __entry->next_offset) { \
327 __entry = (void *)(entries) + __i; \
329 __ret = fn(__entry , ## args); \
337 * Main firewall chains definitions and global var's definitions.
339 static DECLARE_MUTEX(ipt_mutex);
342 #include <linux/init.h>
343 extern void ipt_init(void) __init;
347 struct list_head list;
349 const char name[IPT_FUNCTION_MAXNAMELEN];
351 /* Return true or false: return FALSE and set *hotdrop = 1 to
352 force immediate packet drop. */
353 /* Arguments changed since 2.4, as this must now handle
354 non-linear skbs, using skb_copy_bits and
355 skb_ip_make_writable. */
356 int (*match)(const struct sk_buff *skb,
357 const struct net_device *in,
358 const struct net_device *out,
359 const void *matchinfo,
363 /* Called when user tries to insert an entry of this type. */
364 /* Should return true or false. */
365 int (*checkentry)(const char *tablename,
366 const struct ipt_ip *ip,
368 unsigned int matchinfosize,
369 unsigned int hook_mask);
371 /* Called when entry of this type deleted. */
372 void (*destroy)(void *matchinfo, unsigned int matchinfosize);
374 /* Set this to THIS_MODULE. */
378 /* Registration hooks for targets. */
381 struct list_head list;
383 const char name[IPT_FUNCTION_MAXNAMELEN];
385 /* Called when user tries to insert an entry of this type:
386 hook_mask is a bitmask of hooks from which it can be
388 /* Should return true or false. */
389 int (*checkentry)(const char *tablename,
390 const struct ipt_entry *e,
392 unsigned int targinfosize,
393 unsigned int hook_mask);
395 /* Called when entry of this type deleted. */
396 void (*destroy)(void *targinfo, unsigned int targinfosize);
398 /* Returns verdict. Argument order changed since 2.4, as this
399 must now handle non-linear skbs, using skb_copy_bits and
400 skb_ip_make_writable. */
401 unsigned int (*target)(struct sk_buff **pskb,
402 const struct net_device *in,
403 const struct net_device *out,
404 unsigned int hooknum,
405 const void *targinfo,
408 /* Set this to THIS_MODULE. */
412 extern struct ipt_target *
413 ipt_find_target_lock(const char *name, int *error, struct semaphore *mutex);
414 extern struct arpt_target *
415 arpt_find_target_lock(const char *name, int *error, struct semaphore *mutex);
417 extern int ipt_register_target(struct ipt_target *target);
418 extern void ipt_unregister_target(struct ipt_target *target);
420 extern int ipt_register_match(struct ipt_match *match);
421 extern void ipt_unregister_match(struct ipt_match *match);
423 /* Furniture shopping... */
426 struct list_head list;
428 /* A unique name... */
429 char name[IPT_TABLE_MAXNAMELEN];
431 /* Seed table: copied in register_table */
432 struct ipt_replace *table;
434 /* What hooks you will enter on */
435 unsigned int valid_hooks;
437 /* Lock for the curtain */
440 /* Man behind the curtain... */
441 struct ipt_table_info *private;
443 /* Set to THIS_MODULE. */
447 extern int ipt_register_table(struct ipt_table *table);
448 extern void ipt_unregister_table(struct ipt_table *table);
449 extern unsigned int ipt_do_table(struct sk_buff **pskb,
451 const struct net_device *in,
452 const struct net_device *out,
453 struct ipt_table *table,
456 #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1))
457 #endif /*__KERNEL__*/
458 #endif /* _IPTABLES_H */