blob: b31050d20ae45316104b6eed9cade06bcb2db897 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 25-Jul-1998 Major changes to allow for ip chain table
3 *
4 * 3-Jan-2000 Named tables to allow packet selection for different uses.
5 */
6
7/*
8 * Format of an IP6 firewall descriptor
9 *
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.
13 */
14
15#ifndef _IP6_TABLES_H
16#define _IP6_TABLES_H
17
18#ifdef __KERNEL__
19#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/in6.h>
21#include <linux/ipv6.h>
22#include <linux/skbuff.h>
23#endif
Patrick McHardyc8942f12008-05-21 14:08:38 -070024#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/compiler.h>
26#include <linux/netfilter_ipv6.h>
27
Harald Welte2e4e6a12006-01-12 13:30:04 -080028#include <linux/netfilter/x_tables.h>
29
30#define IP6T_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
31#define IP6T_TABLE_MAXNAMELEN XT_TABLE_MAXNAMELEN
32
33#define ip6t_match xt_match
34#define ip6t_target xt_target
35#define ip6t_table xt_table
36#define ip6t_get_revision xt_get_revision
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Yes, Virginia, you have to zero the padding. */
39struct ip6t_ip6 {
40 /* Source and destination IP6 addr */
41 struct in6_addr src, dst;
42 /* Mask for src and dest IP6 addr */
43 struct in6_addr smsk, dmsk;
44 char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
45 unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
46
Yasuyuki Kozakai7bfe2462007-07-07 22:14:23 -070047 /* Upper protocol number
48 * - The allowed value is 0 (any) or protocol number of last parsable
49 * header, which is 50 (ESP), 59 (No Next Header), 135 (MH), or
50 * the non IPv6 extension headers.
51 * - The protocol numbers of IPv6 extension headers except of ESP and
52 * MH do not match any packets.
53 * - You also need to set IP6T_FLAGS_PROTO to "flags" to check protocol.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 u_int16_t proto;
56 /* TOS to match iff flags & IP6T_F_TOS */
57 u_int8_t tos;
58
59 /* Flags word */
60 u_int8_t flags;
61 /* Inverse flags */
62 u_int8_t invflags;
63};
64
Dmitry Mishin1e30a012006-03-22 13:56:56 -080065#define ip6t_entry_match xt_entry_match
66#define ip6t_entry_target xt_entry_target
67#define ip6t_standard_target xt_standard_target
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Harald Welte2e4e6a12006-01-12 13:30:04 -080069#define ip6t_counters xt_counters
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
72#define IP6T_F_PROTO 0x01 /* Set if rule cares about upper
73 protocols */
74#define IP6T_F_TOS 0x02 /* Match the TOS. */
Patrick McHardy05465342005-08-21 23:31:43 -070075#define IP6T_F_GOTO 0x04 /* Set if jump is a goto */
76#define IP6T_F_MASK 0x07 /* All possible flag bits mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* Values for "inv" field in struct ip6t_ip6. */
79#define IP6T_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
80#define IP6T_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
81#define IP6T_INV_TOS 0x04 /* Invert the sense of TOS. */
82#define IP6T_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
83#define IP6T_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
84#define IP6T_INV_FRAG 0x20 /* Invert the sense of FRAG. */
Harald Welte2e4e6a12006-01-12 13:30:04 -080085#define IP6T_INV_PROTO XT_INV_PROTO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define IP6T_INV_MASK 0x7F /* All possible flag bits mask. */
87
88/* This structure defines each of the firewall rules. Consists of 3
89 parts which are 1) general IP header stuff 2) match specific
90 stuff 3) the target to perform if the rule matches */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080091struct ip6t_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct ip6t_ip6 ipv6;
93
94 /* Mark with fields that we care about. */
95 unsigned int nfcache;
96
97 /* Size of ipt_entry + matches */
98 u_int16_t target_offset;
99 /* Size of ipt_entry + matches + target */
100 u_int16_t next_offset;
101
102 /* Back pointer */
103 unsigned int comefrom;
104
105 /* Packet and byte counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800106 struct xt_counters counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* The matches (if any), then the target. */
109 unsigned char elems[0];
110};
111
Patrick McHardy9934e812007-02-07 15:14:28 -0800112/* Standard entry */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800113struct ip6t_standard {
Patrick McHardy9934e812007-02-07 15:14:28 -0800114 struct ip6t_entry entry;
115 struct ip6t_standard_target target;
116};
117
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800118struct ip6t_error_target {
Patrick McHardy9934e812007-02-07 15:14:28 -0800119 struct ip6t_entry_target target;
120 char errorname[IP6T_FUNCTION_MAXNAMELEN];
121};
122
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800123struct ip6t_error {
Patrick McHardy9934e812007-02-07 15:14:28 -0800124 struct ip6t_entry entry;
125 struct ip6t_error_target target;
126};
127
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700128#define IP6T_ENTRY_INIT(__size) \
129{ \
130 .target_offset = sizeof(struct ip6t_entry), \
131 .next_offset = (__size), \
132}
133
134#define IP6T_STANDARD_INIT(__verdict) \
135{ \
136 .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_standard)), \
137 .target = XT_TARGET_INIT(IP6T_STANDARD_TARGET, \
138 sizeof(struct ip6t_standard_target)), \
139 .target.verdict = -(__verdict) - 1, \
140}
141
142#define IP6T_ERROR_INIT \
143{ \
144 .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_error)), \
145 .target = XT_TARGET_INIT(IP6T_ERROR_TARGET, \
146 sizeof(struct ip6t_error_target)), \
147 .target.errorname = "ERROR", \
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
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
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800153 * a raw socket for this. Instead we check rights in the calls.
154 *
155 * ATTENTION: check linux/in6.h before adding new number here.
156 */
157#define IP6T_BASE_CTL 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800159#define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL)
160#define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1)
161#define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800163#define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
164#define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
165#define IP6T_SO_GET_REVISION_MATCH (IP6T_BASE_CTL + 4)
166#define IP6T_SO_GET_REVISION_TARGET (IP6T_BASE_CTL + 5)
167#define IP6T_SO_GET_MAX IP6T_SO_GET_REVISION_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* CONTINUE verdict for targets */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800170#define IP6T_CONTINUE XT_CONTINUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* For standard target */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800173#define IP6T_RETURN XT_RETURN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Harald Welte2e4e6a12006-01-12 13:30:04 -0800175/* TCP/UDP matching stuff */
176#include <linux/netfilter/xt_tcpudp.h>
177
178#define ip6t_tcp xt_tcp
179#define ip6t_udp xt_udp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Values for "inv" field in struct ipt_tcp. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800182#define IP6T_TCP_INV_SRCPT XT_TCP_INV_SRCPT
183#define IP6T_TCP_INV_DSTPT XT_TCP_INV_DSTPT
184#define IP6T_TCP_INV_FLAGS XT_TCP_INV_FLAGS
185#define IP6T_TCP_INV_OPTION XT_TCP_INV_OPTION
186#define IP6T_TCP_INV_MASK XT_TCP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* Values for "invflags" field in struct ipt_udp. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800189#define IP6T_UDP_INV_SRCPT XT_UDP_INV_SRCPT
190#define IP6T_UDP_INV_DSTPT XT_UDP_INV_DSTPT
191#define IP6T_UDP_INV_MASK XT_UDP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* ICMP matching stuff */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800194struct ip6t_icmp {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 u_int8_t type; /* type to match */
196 u_int8_t code[2]; /* range of code */
197 u_int8_t invflags; /* Inverse flags */
198};
199
200/* Values for "inv" field for struct ipt_icmp. */
201#define IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */
202
203/* The argument to IP6T_SO_GET_INFO */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800204struct ip6t_getinfo {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Which table: caller fills this in. */
206 char name[IP6T_TABLE_MAXNAMELEN];
207
208 /* Kernel fills these in. */
209 /* Which hook entry points are valid: bitmask */
210 unsigned int valid_hooks;
211
212 /* Hook entry points: one per netfilter hook. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800213 unsigned int hook_entry[NF_INET_NUMHOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Underflow points. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800216 unsigned int underflow[NF_INET_NUMHOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* Number of entries */
219 unsigned int num_entries;
220
221 /* Size of entries. */
222 unsigned int size;
223};
224
225/* The argument to IP6T_SO_SET_REPLACE. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800226struct ip6t_replace {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Which table. */
228 char name[IP6T_TABLE_MAXNAMELEN];
229
230 /* Which hook entry points are valid: bitmask. You can't
231 change this. */
232 unsigned int valid_hooks;
233
234 /* Number of entries */
235 unsigned int num_entries;
236
237 /* Total size of new entries */
238 unsigned int size;
239
240 /* Hook entry points. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800241 unsigned int hook_entry[NF_INET_NUMHOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* Underflow points. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800244 unsigned int underflow[NF_INET_NUMHOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Information about old entries: */
247 /* Number of counters (must be equal to current number of entries). */
248 unsigned int num_counters;
249 /* The old entries' counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800250 struct xt_counters __user *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* The entries (hang off end: not really an array). */
253 struct ip6t_entry entries[0];
254};
255
256/* The argument to IP6T_SO_ADD_COUNTERS. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800257#define ip6t_counters_info xt_counters_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/* The argument to IP6T_SO_GET_ENTRIES. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800260struct ip6t_get_entries {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Which table: user fills this in. */
262 char name[IP6T_TABLE_MAXNAMELEN];
263
264 /* User fills this in: total entry size. */
265 unsigned int size;
266
267 /* The entries. */
268 struct ip6t_entry entrytable[0];
269};
270
271/* Standard return verdict, or do jump. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800272#define IP6T_STANDARD_TARGET XT_STANDARD_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* Error verdict. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274#define IP6T_ERROR_TARGET XT_ERROR_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/* Helper functions */
277static __inline__ struct ip6t_entry_target *
278ip6t_get_target(struct ip6t_entry *e)
279{
280 return (void *)e + e->target_offset;
281}
282
283/* fn returns 0 to continue iteration */
Patrick McHardy89c002d2007-12-17 21:46:59 -0800284#define IP6T_MATCH_ITERATE(e, fn, args...) \
285 XT_MATCH_ITERATE(struct ip6t_entry, e, fn, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/* fn returns 0 to continue iteration */
Patrick McHardy89c002d2007-12-17 21:46:59 -0800288#define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
289 XT_ENTRY_ITERATE(struct ip6t_entry, entries, size, fn, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*
292 * Main firewall chains definitions and global var's definitions.
293 */
294
295#ifdef __KERNEL__
296
297#include <linux/init.h>
298extern void ip6t_init(void) __init;
299
Alexey Dobriyan336b5172008-01-31 04:03:45 -0800300extern struct xt_table *ip6t_register_table(struct net *net,
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200301 const struct xt_table *table,
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800302 const struct ip6t_replace *repl);
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800303extern void ip6t_unregister_table(struct xt_table *table);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700304extern unsigned int ip6t_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 unsigned int hook,
306 const struct net_device *in,
307 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800308 struct xt_table *table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310/* Check for an extension */
311extern int ip6t_ext_hdr(u8 nexthdr);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -0700312/* find specified header and get offset to it */
313extern int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800314 int target, unsigned short *fragoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Patrick McHardy22dea562006-01-05 12:21:34 -0800316extern int ip6_masked_addrcmp(const struct in6_addr *addr1,
317 const struct in6_addr *mask,
318 const struct in6_addr *addr2);
319
Patrick McHardy06e13742007-12-17 21:53:40 -0800320#define IP6T_ALIGN(s) XT_ALIGN(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800322#ifdef CONFIG_COMPAT
323#include <net/compat.h>
324
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800325struct compat_ip6t_entry {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800326 struct ip6t_ip6 ipv6;
327 compat_uint_t nfcache;
328 u_int16_t target_offset;
329 u_int16_t next_offset;
330 compat_uint_t comefrom;
331 struct compat_xt_counters counters;
332 unsigned char elems[0];
333};
334
335static inline struct ip6t_entry_target *
336compat_ip6t_get_target(struct compat_ip6t_entry *e)
337{
338 return (void *)e + e->target_offset;
339}
340
341#define COMPAT_IP6T_ALIGN(s) COMPAT_XT_ALIGN(s)
342
343/* fn returns 0 to continue iteration */
344#define COMPAT_IP6T_MATCH_ITERATE(e, fn, args...) \
345 XT_MATCH_ITERATE(struct compat_ip6t_entry, e, fn, ## args)
346
347/* fn returns 0 to continue iteration */
348#define COMPAT_IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
349 XT_ENTRY_ITERATE(struct compat_ip6t_entry, entries, size, fn, ## args)
350
351#define COMPAT_IP6T_ENTRY_ITERATE_CONTINUE(entries, size, n, fn, args...) \
352 XT_ENTRY_ITERATE_CONTINUE(struct compat_ip6t_entry, entries, size, n, \
353 fn, ## args)
354
355#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#endif /*__KERNEL__*/
357#endif /* _IP6_TABLES_H */