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 */
111 static DECLARE_MUTEX(ip6t_mutex);
114 /* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
115 #define IP6T_F_PROTO 0x01 /* Set if rule cares about upper
117 #define IP6T_F_TOS 0x02 /* Match the TOS. */
118 #define IP6T_F_MASK 0x03 /* All possible flag bits mask. */
120 /* Values for "inv" field in struct ip6t_ip6. */
121 #define IP6T_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
122 #define IP6T_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
123 #define IP6T_INV_TOS 0x04 /* Invert the sense of TOS. */
124 #define IP6T_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
125 #define IP6T_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
126 #define IP6T_INV_FRAG 0x20 /* Invert the sense of FRAG. */
127 #define IP6T_INV_PROTO 0x40 /* Invert the sense of PROTO. */
128 #define IP6T_INV_MASK 0x7F /* All possible flag bits mask. */
130 /* This structure defines each of the firewall rules. Consists of 3
131 parts which are 1) general IP header stuff 2) match specific
132 stuff 3) the target to perform if the rule matches */
135 struct ip6t_ip6 ipv6;
137 /* Mark with fields that we care about. */
138 unsigned int nfcache;
140 /* Size of ipt_entry + matches */
141 u_int16_t target_offset;
142 /* Size of ipt_entry + matches + target */
143 u_int16_t next_offset;
146 unsigned int comefrom;
148 /* Packet and byte counters. */
149 struct ip6t_counters counters;
151 /* The matches (if any), then the target. */
152 unsigned char elems[0];
156 * New IP firewall options for [gs]etsockopt at the RAW IP level.
157 * Unlike BSD Linux inherits IP options so you don't have to use
158 * a raw socket for this. Instead we check rights in the calls. */
159 #define IP6T_BASE_CTL 64 /* base for firewall socket options */
161 #define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL)
162 #define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1)
163 #define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS
165 #define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
166 #define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
167 #define IP6T_SO_GET_MAX IP6T_SO_GET_ENTRIES
169 /* CONTINUE verdict for targets */
170 #define IP6T_CONTINUE 0xFFFFFFFF
172 /* For standard target */
173 #define IP6T_RETURN (-NF_MAX_VERDICT - 1)
175 /* TCP matching stuff */
178 u_int16_t spts[2]; /* Source port range. */
179 u_int16_t dpts[2]; /* Destination port range. */
180 u_int8_t option; /* TCP Option iff non-zero*/
181 u_int8_t flg_mask; /* TCP flags mask byte */
182 u_int8_t flg_cmp; /* TCP flags compare byte */
183 u_int8_t invflags; /* Inverse flags */
186 /* Values for "inv" field in struct ipt_tcp. */
187 #define IP6T_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
188 #define IP6T_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
189 #define IP6T_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */
190 #define IP6T_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */
191 #define IP6T_TCP_INV_MASK 0x0F /* All possible flags. */
193 /* UDP matching stuff */
196 u_int16_t spts[2]; /* Source port range. */
197 u_int16_t dpts[2]; /* Destination port range. */
198 u_int8_t invflags; /* Inverse flags */
201 /* Values for "invflags" field in struct ipt_udp. */
202 #define IP6T_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
203 #define IP6T_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
204 #define IP6T_UDP_INV_MASK 0x03 /* All possible flags. */
206 /* ICMP matching stuff */
209 u_int8_t type; /* type to match */
210 u_int8_t code[2]; /* range of code */
211 u_int8_t invflags; /* Inverse flags */
214 /* Values for "inv" field for struct ipt_icmp. */
215 #define IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */
217 /* The argument to IP6T_SO_GET_INFO */
220 /* Which table: caller fills this in. */
221 char name[IP6T_TABLE_MAXNAMELEN];
223 /* Kernel fills these in. */
224 /* Which hook entry points are valid: bitmask */
225 unsigned int valid_hooks;
227 /* Hook entry points: one per netfilter hook. */
228 unsigned int hook_entry[NF_IP6_NUMHOOKS];
230 /* Underflow points. */
231 unsigned int underflow[NF_IP6_NUMHOOKS];
233 /* Number of entries */
234 unsigned int num_entries;
236 /* Size of entries. */
240 /* The argument to IP6T_SO_SET_REPLACE. */
244 char name[IP6T_TABLE_MAXNAMELEN];
246 /* Which hook entry points are valid: bitmask. You can't
248 unsigned int valid_hooks;
250 /* Number of entries */
251 unsigned int num_entries;
253 /* Total size of new entries */
256 /* Hook entry points. */
257 unsigned int hook_entry[NF_IP6_NUMHOOKS];
259 /* Underflow points. */
260 unsigned int underflow[NF_IP6_NUMHOOKS];
262 /* Information about old entries: */
263 /* Number of counters (must be equal to current number of entries). */
264 unsigned int num_counters;
265 /* The old entries' counters. */
266 struct ip6t_counters __user *counters;
268 /* The entries (hang off end: not really an array). */
269 struct ip6t_entry entries[0];
272 /* The argument to IP6T_SO_ADD_COUNTERS. */
273 struct ip6t_counters_info
276 char name[IP6T_TABLE_MAXNAMELEN];
278 unsigned int num_counters;
280 /* The counters (actually `number' of these). */
281 struct ip6t_counters counters[0];
284 /* The argument to IP6T_SO_GET_ENTRIES. */
285 struct ip6t_get_entries
287 /* Which table: user fills this in. */
288 char name[IP6T_TABLE_MAXNAMELEN];
290 /* User fills this in: total entry size. */
294 struct ip6t_entry entrytable[0];
297 /* Standard return verdict, or do jump. */
298 #define IP6T_STANDARD_TARGET ""
300 #define IP6T_ERROR_TARGET "ERROR"
302 /* Helper functions */
303 static __inline__ struct ip6t_entry_target *
304 ip6t_get_target(struct ip6t_entry *e)
306 return (void *)e + e->target_offset;
309 /* fn returns 0 to continue iteration */
310 #define IP6T_MATCH_ITERATE(e, fn, args...) \
314 struct ip6t_entry_match *__m; \
316 for (__i = sizeof(struct ip6t_entry); \
317 __i < (e)->target_offset; \
318 __i += __m->u.match_size) { \
319 __m = (void *)(e) + __i; \
321 __ret = fn(__m , ## args); \
328 /* fn returns 0 to continue iteration */
329 #define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
333 struct ip6t_entry *__e; \
335 for (__i = 0; __i < (size); __i += __e->next_offset) { \
336 __e = (void *)(entries) + __i; \
338 __ret = fn(__e , ## args); \
346 * Main firewall chains definitions and global var's definitions.
351 #include <linux/init.h>
352 extern void ip6t_init(void) __init;
356 struct list_head list;
358 const char name[IP6T_FUNCTION_MAXNAMELEN];
360 /* Return true or false: return FALSE and set *hotdrop = 1 to
361 force immediate packet drop. */
362 int (*match)(const struct sk_buff *skb,
363 const struct net_device *in,
364 const struct net_device *out,
365 const void *matchinfo,
371 /* Called when user tries to insert an entry of this type. */
372 /* Should return true or false. */
373 int (*checkentry)(const char *tablename,
374 const struct ip6t_ip6 *ip,
376 unsigned int matchinfosize,
377 unsigned int hook_mask);
379 /* Called when entry of this type deleted. */
380 void (*destroy)(void *matchinfo, unsigned int matchinfosize);
382 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
386 /* Registration hooks for targets. */
389 struct list_head list;
391 const char name[IP6T_FUNCTION_MAXNAMELEN];
393 /* Returns verdict. */
394 unsigned int (*target)(struct sk_buff **pskb,
395 unsigned int hooknum,
396 const struct net_device *in,
397 const struct net_device *out,
398 const void *targinfo,
401 /* Called when user tries to insert an entry of this type:
402 hook_mask is a bitmask of hooks from which it can be
404 /* Should return true or false. */
405 int (*checkentry)(const char *tablename,
406 const struct ip6t_entry *e,
408 unsigned int targinfosize,
409 unsigned int hook_mask);
411 /* Called when entry of this type deleted. */
412 void (*destroy)(void *targinfo, unsigned int targinfosize);
414 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
418 extern int ip6t_register_target(struct ip6t_target *target);
419 extern void ip6t_unregister_target(struct ip6t_target *target);
421 extern int ip6t_register_match(struct ip6t_match *match);
422 extern void ip6t_unregister_match(struct ip6t_match *match);
424 /* Furniture shopping... */
427 struct list_head list;
429 /* A unique name... */
430 char name[IP6T_TABLE_MAXNAMELEN];
432 /* Seed table: copied in register_table */
433 struct ip6t_replace *table;
435 /* What hooks you will enter on */
436 unsigned int valid_hooks;
438 /* Lock for the curtain */
441 /* Man behind the curtain... */
442 struct ip6t_table_info *private;
444 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
448 extern int ip6t_register_table(struct ip6t_table *table);
449 extern void ip6t_unregister_table(struct ip6t_table *table);
450 extern unsigned int ip6t_do_table(struct sk_buff **pskb,
452 const struct net_device *in,
453 const struct net_device *out,
454 struct ip6t_table *table,
457 /* Check for an extension */
458 extern int ip6t_ext_hdr(u8 nexthdr);
460 #define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1))
462 #endif /*__KERNEL__*/
463 #endif /* _IP6_TABLES_H */