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 IP6 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>
21 #include <linux/in6.h>
22 #include <linux/ipv6.h>
23 #include <linux/skbuff.h>
25 #include <linux/compiler.h>
26 #include <linux/netfilter_ipv6.h>
28 #define IP6T_FUNCTION_MAXNAMELEN 30
29 #define IP6T_TABLE_MAXNAMELEN 32
31 /* Yes, Virginia, you have to zero the padding. */
33 /* Source and destination IP6 addr */
34 struct in6_addr src, dst;
35 /* Mask for src and dest IP6 addr */
36 struct in6_addr smsk, dmsk;
37 char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
38 unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
40 /* ARGH, HopByHop uses 0, so can't do 0 = ANY,
41 instead IP6T_F_NOPROTO must be set */
43 /* TOS to match iff flags & IP6T_F_TOS */
52 /* FIXME: If alignment in kernel different from userspace? --RR */
53 struct ip6t_entry_match
59 /* Used by userspace */
60 char name[IP6T_FUNCTION_MAXNAMELEN];
65 /* Used inside the kernel */
66 struct ip6t_match *match;
73 unsigned char data[0];
76 struct ip6t_entry_target
80 u_int16_t target_size;
82 /* Used by userspace */
83 char name[IP6T_FUNCTION_MAXNAMELEN];
86 u_int16_t target_size;
88 /* Used inside the kernel */
89 struct ip6t_target *target;
93 u_int16_t target_size;
96 unsigned char data[0];
99 struct ip6t_standard_target
101 struct ip6t_entry_target target;
107 u_int64_t pcnt, bcnt; /* Packet and byte counters */
110 /* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
111 #define IP6T_F_PROTO 0x01 /* Set if rule cares about upper
113 #define IP6T_F_TOS 0x02 /* Match the TOS. */
114 #define IP6T_F_GOTO 0x04 /* Set if jump is a goto */
115 #define IP6T_F_MASK 0x07 /* All possible flag bits mask. */
117 /* Values for "inv" field in struct ip6t_ip6. */
118 #define IP6T_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
119 #define IP6T_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
120 #define IP6T_INV_TOS 0x04 /* Invert the sense of TOS. */
121 #define IP6T_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
122 #define IP6T_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
123 #define IP6T_INV_FRAG 0x20 /* Invert the sense of FRAG. */
124 #define IP6T_INV_PROTO 0x40 /* Invert the sense of PROTO. */
125 #define IP6T_INV_MASK 0x7F /* All possible flag bits mask. */
127 /* This structure defines each of the firewall rules. Consists of 3
128 parts which are 1) general IP header stuff 2) match specific
129 stuff 3) the target to perform if the rule matches */
132 struct ip6t_ip6 ipv6;
134 /* Mark with fields that we care about. */
135 unsigned int nfcache;
137 /* Size of ipt_entry + matches */
138 u_int16_t target_offset;
139 /* Size of ipt_entry + matches + target */
140 u_int16_t next_offset;
143 unsigned int comefrom;
145 /* Packet and byte counters. */
146 struct ip6t_counters counters;
148 /* The matches (if any), then the target. */
149 unsigned char elems[0];
153 * New IP firewall options for [gs]etsockopt at the RAW IP level.
154 * Unlike BSD Linux inherits IP options so you don't have to use
155 * a raw socket for this. Instead we check rights in the calls. */
156 #define IP6T_BASE_CTL 64 /* base for firewall socket options */
158 #define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL)
159 #define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1)
160 #define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS
162 #define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
163 #define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
164 #define IP6T_SO_GET_MAX IP6T_SO_GET_ENTRIES
166 /* CONTINUE verdict for targets */
167 #define IP6T_CONTINUE 0xFFFFFFFF
169 /* For standard target */
170 #define IP6T_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 IP6T_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
185 #define IP6T_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
186 #define IP6T_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */
187 #define IP6T_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */
188 #define IP6T_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 IP6T_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
200 #define IP6T_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
201 #define IP6T_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 IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */
214 /* The argument to IP6T_SO_GET_INFO */
217 /* Which table: caller fills this in. */
218 char name[IP6T_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_IP6_NUMHOOKS];
227 /* Underflow points. */
228 unsigned int underflow[NF_IP6_NUMHOOKS];
230 /* Number of entries */
231 unsigned int num_entries;
233 /* Size of entries. */
237 /* The argument to IP6T_SO_SET_REPLACE. */
241 char name[IP6T_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_IP6_NUMHOOKS];
256 /* Underflow points. */
257 unsigned int underflow[NF_IP6_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 ip6t_counters __user *counters;
265 /* The entries (hang off end: not really an array). */
266 struct ip6t_entry entries[0];
269 /* The argument to IP6T_SO_ADD_COUNTERS. */
270 struct ip6t_counters_info
273 char name[IP6T_TABLE_MAXNAMELEN];
275 unsigned int num_counters;
277 /* The counters (actually `number' of these). */
278 struct ip6t_counters counters[0];
281 /* The argument to IP6T_SO_GET_ENTRIES. */
282 struct ip6t_get_entries
284 /* Which table: user fills this in. */
285 char name[IP6T_TABLE_MAXNAMELEN];
287 /* User fills this in: total entry size. */
291 struct ip6t_entry entrytable[0];
294 /* Standard return verdict, or do jump. */
295 #define IP6T_STANDARD_TARGET ""
297 #define IP6T_ERROR_TARGET "ERROR"
299 /* Helper functions */
300 static __inline__ struct ip6t_entry_target *
301 ip6t_get_target(struct ip6t_entry *e)
303 return (void *)e + e->target_offset;
306 /* fn returns 0 to continue iteration */
307 #define IP6T_MATCH_ITERATE(e, fn, args...) \
311 struct ip6t_entry_match *__m; \
313 for (__i = sizeof(struct ip6t_entry); \
314 __i < (e)->target_offset; \
315 __i += __m->u.match_size) { \
316 __m = (void *)(e) + __i; \
318 __ret = fn(__m , ## args); \
325 /* fn returns 0 to continue iteration */
326 #define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
330 struct ip6t_entry *__e; \
332 for (__i = 0; __i < (size); __i += __e->next_offset) { \
333 __e = (void *)(entries) + __i; \
335 __ret = fn(__e , ## args); \
343 * Main firewall chains definitions and global var's definitions.
348 #include <linux/init.h>
349 extern void ip6t_init(void) __init;
353 struct list_head list;
355 const char name[IP6T_FUNCTION_MAXNAMELEN];
357 /* Return true or false: return FALSE and set *hotdrop = 1 to
358 force immediate packet drop. */
359 /* Arguments changed since 2.6.9, as this must now handle
360 non-linear skb, using skb_header_pointer and
361 skb_ip_make_writable. */
362 int (*match)(const struct sk_buff *skb,
363 const struct net_device *in,
364 const struct net_device *out,
365 const void *matchinfo,
367 unsigned int protoff,
370 /* Called when user tries to insert an entry of this type. */
371 /* Should return true or false. */
372 int (*checkentry)(const char *tablename,
373 const struct ip6t_ip6 *ip,
375 unsigned int matchinfosize,
376 unsigned int hook_mask);
378 /* Called when entry of this type deleted. */
379 void (*destroy)(void *matchinfo, unsigned int matchinfosize);
381 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
385 /* Registration hooks for targets. */
388 struct list_head list;
390 const char name[IP6T_FUNCTION_MAXNAMELEN];
392 /* Returns verdict. Argument order changed since 2.6.9, as this
393 must now handle non-linear skbs, using skb_copy_bits and
394 skb_ip_make_writable. */
395 unsigned int (*target)(struct sk_buff **pskb,
396 const struct net_device *in,
397 const struct net_device *out,
398 unsigned int hooknum,
399 const void *targinfo,
402 /* Called when user tries to insert an entry of this type:
403 hook_mask is a bitmask of hooks from which it can be
405 /* Should return true or false. */
406 int (*checkentry)(const char *tablename,
407 const struct ip6t_entry *e,
409 unsigned int targinfosize,
410 unsigned int hook_mask);
412 /* Called when entry of this type deleted. */
413 void (*destroy)(void *targinfo, unsigned int targinfosize);
415 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
419 extern int ip6t_register_target(struct ip6t_target *target);
420 extern void ip6t_unregister_target(struct ip6t_target *target);
422 extern int ip6t_register_match(struct ip6t_match *match);
423 extern void ip6t_unregister_match(struct ip6t_match *match);
425 /* Furniture shopping... */
428 struct list_head list;
430 /* A unique name... */
431 char name[IP6T_TABLE_MAXNAMELEN];
433 /* What hooks you will enter on */
434 unsigned int valid_hooks;
436 /* Lock for the curtain */
439 /* Man behind the curtain... */
440 struct ip6t_table_info *private;
442 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
446 extern int ip6t_register_table(struct ip6t_table *table,
447 const struct ip6t_replace *repl);
448 extern void ip6t_unregister_table(struct ip6t_table *table);
449 extern unsigned int ip6t_do_table(struct sk_buff **pskb,
451 const struct net_device *in,
452 const struct net_device *out,
453 struct ip6t_table *table,
456 /* Check for an extension */
457 extern int ip6t_ext_hdr(u8 nexthdr);
458 /* find specified header and get offset to it */
459 extern int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
462 #define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1))
464 #endif /*__KERNEL__*/
465 #endif /* _IP6_TABLES_H */