blob: 4686f8342cbd73db22f6fd847f51f647f6e6c661 [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>
20#include <linux/types.h>
21#include <linux/in6.h>
22#include <linux/ipv6.h>
23#include <linux/skbuff.h>
24#endif
25#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
47 /* ARGH, HopByHop uses 0, so can't do 0 = ANY,
48 instead IP6T_F_NOPROTO must be set */
49 u_int16_t proto;
50 /* TOS to match iff flags & IP6T_F_TOS */
51 u_int8_t tos;
52
53 /* Flags word */
54 u_int8_t flags;
55 /* Inverse flags */
56 u_int8_t invflags;
57};
58
Dmitry Mishin1e30a012006-03-22 13:56:56 -080059#define ip6t_entry_match xt_entry_match
60#define ip6t_entry_target xt_entry_target
61#define ip6t_standard_target xt_standard_target
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Harald Welte2e4e6a12006-01-12 13:30:04 -080063#define ip6t_counters xt_counters
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
66#define IP6T_F_PROTO 0x01 /* Set if rule cares about upper
67 protocols */
68#define IP6T_F_TOS 0x02 /* Match the TOS. */
Patrick McHardy05465342005-08-21 23:31:43 -070069#define IP6T_F_GOTO 0x04 /* Set if jump is a goto */
70#define IP6T_F_MASK 0x07 /* All possible flag bits mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* Values for "inv" field in struct ip6t_ip6. */
73#define IP6T_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
74#define IP6T_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
75#define IP6T_INV_TOS 0x04 /* Invert the sense of TOS. */
76#define IP6T_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
77#define IP6T_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
78#define IP6T_INV_FRAG 0x20 /* Invert the sense of FRAG. */
Harald Welte2e4e6a12006-01-12 13:30:04 -080079#define IP6T_INV_PROTO XT_INV_PROTO
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define IP6T_INV_MASK 0x7F /* All possible flag bits mask. */
81
82/* This structure defines each of the firewall rules. Consists of 3
83 parts which are 1) general IP header stuff 2) match specific
84 stuff 3) the target to perform if the rule matches */
85struct ip6t_entry
86{
87 struct ip6t_ip6 ipv6;
88
89 /* Mark with fields that we care about. */
90 unsigned int nfcache;
91
92 /* Size of ipt_entry + matches */
93 u_int16_t target_offset;
94 /* Size of ipt_entry + matches + target */
95 u_int16_t next_offset;
96
97 /* Back pointer */
98 unsigned int comefrom;
99
100 /* Packet and byte counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800101 struct xt_counters counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* The matches (if any), then the target. */
104 unsigned char elems[0];
105};
106
Patrick McHardy9934e812007-02-07 15:14:28 -0800107/* Standard entry */
108struct ip6t_standard
109{
110 struct ip6t_entry entry;
111 struct ip6t_standard_target target;
112};
113
114struct ip6t_error_target
115{
116 struct ip6t_entry_target target;
117 char errorname[IP6T_FUNCTION_MAXNAMELEN];
118};
119
120struct ip6t_error
121{
122 struct ip6t_entry entry;
123 struct ip6t_error_target target;
124};
125
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700126#define IP6T_ENTRY_INIT(__size) \
127{ \
128 .target_offset = sizeof(struct ip6t_entry), \
129 .next_offset = (__size), \
130}
131
132#define IP6T_STANDARD_INIT(__verdict) \
133{ \
134 .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_standard)), \
135 .target = XT_TARGET_INIT(IP6T_STANDARD_TARGET, \
136 sizeof(struct ip6t_standard_target)), \
137 .target.verdict = -(__verdict) - 1, \
138}
139
140#define IP6T_ERROR_INIT \
141{ \
142 .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_error)), \
143 .target = XT_TARGET_INIT(IP6T_ERROR_TARGET, \
144 sizeof(struct ip6t_error_target)), \
145 .target.errorname = "ERROR", \
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * New IP firewall options for [gs]etsockopt at the RAW IP level.
150 * Unlike BSD Linux inherits IP options so you don't have to use
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800151 * a raw socket for this. Instead we check rights in the calls.
152 *
153 * ATTENTION: check linux/in6.h before adding new number here.
154 */
155#define IP6T_BASE_CTL 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800157#define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL)
158#define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1)
159#define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800161#define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
162#define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
163#define IP6T_SO_GET_REVISION_MATCH (IP6T_BASE_CTL + 4)
164#define IP6T_SO_GET_REVISION_TARGET (IP6T_BASE_CTL + 5)
165#define IP6T_SO_GET_MAX IP6T_SO_GET_REVISION_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* CONTINUE verdict for targets */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800168#define IP6T_CONTINUE XT_CONTINUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/* For standard target */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800171#define IP6T_RETURN XT_RETURN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Harald Welte2e4e6a12006-01-12 13:30:04 -0800173/* TCP/UDP matching stuff */
174#include <linux/netfilter/xt_tcpudp.h>
175
176#define ip6t_tcp xt_tcp
177#define ip6t_udp xt_udp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* Values for "inv" field in struct ipt_tcp. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800180#define IP6T_TCP_INV_SRCPT XT_TCP_INV_SRCPT
181#define IP6T_TCP_INV_DSTPT XT_TCP_INV_DSTPT
182#define IP6T_TCP_INV_FLAGS XT_TCP_INV_FLAGS
183#define IP6T_TCP_INV_OPTION XT_TCP_INV_OPTION
184#define IP6T_TCP_INV_MASK XT_TCP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* Values for "invflags" field in struct ipt_udp. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800187#define IP6T_UDP_INV_SRCPT XT_UDP_INV_SRCPT
188#define IP6T_UDP_INV_DSTPT XT_UDP_INV_DSTPT
189#define IP6T_UDP_INV_MASK XT_UDP_INV_MASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* ICMP matching stuff */
192struct ip6t_icmp
193{
194 u_int8_t type; /* type to match */
195 u_int8_t code[2]; /* range of code */
196 u_int8_t invflags; /* Inverse flags */
197};
198
199/* Values for "inv" field for struct ipt_icmp. */
200#define IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */
201
202/* The argument to IP6T_SO_GET_INFO */
203struct ip6t_getinfo
204{
205 /* 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. */
213 unsigned int hook_entry[NF_IP6_NUMHOOKS];
214
215 /* Underflow points. */
216 unsigned int underflow[NF_IP6_NUMHOOKS];
217
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. */
226struct ip6t_replace
227{
228 /* Which table. */
229 char name[IP6T_TABLE_MAXNAMELEN];
230
231 /* Which hook entry points are valid: bitmask. You can't
232 change this. */
233 unsigned int valid_hooks;
234
235 /* Number of entries */
236 unsigned int num_entries;
237
238 /* Total size of new entries */
239 unsigned int size;
240
241 /* Hook entry points. */
242 unsigned int hook_entry[NF_IP6_NUMHOOKS];
243
244 /* Underflow points. */
245 unsigned int underflow[NF_IP6_NUMHOOKS];
246
247 /* Information about old entries: */
248 /* Number of counters (must be equal to current number of entries). */
249 unsigned int num_counters;
250 /* The old entries' counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800251 struct xt_counters __user *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* The entries (hang off end: not really an array). */
254 struct ip6t_entry entries[0];
255};
256
257/* The argument to IP6T_SO_ADD_COUNTERS. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800258#define ip6t_counters_info xt_counters_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/* The argument to IP6T_SO_GET_ENTRIES. */
261struct ip6t_get_entries
262{
263 /* Which table: user fills this in. */
264 char name[IP6T_TABLE_MAXNAMELEN];
265
266 /* User fills this in: total entry size. */
267 unsigned int size;
268
269 /* The entries. */
270 struct ip6t_entry entrytable[0];
271};
272
273/* Standard return verdict, or do jump. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274#define IP6T_STANDARD_TARGET XT_STANDARD_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* Error verdict. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800276#define IP6T_ERROR_TARGET XT_ERROR_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Helper functions */
279static __inline__ struct ip6t_entry_target *
280ip6t_get_target(struct ip6t_entry *e)
281{
282 return (void *)e + e->target_offset;
283}
284
285/* fn returns 0 to continue iteration */
286#define IP6T_MATCH_ITERATE(e, fn, args...) \
287({ \
288 unsigned int __i; \
289 int __ret = 0; \
290 struct ip6t_entry_match *__m; \
291 \
292 for (__i = sizeof(struct ip6t_entry); \
293 __i < (e)->target_offset; \
294 __i += __m->u.match_size) { \
295 __m = (void *)(e) + __i; \
296 \
297 __ret = fn(__m , ## args); \
298 if (__ret != 0) \
299 break; \
300 } \
301 __ret; \
302})
303
304/* fn returns 0 to continue iteration */
305#define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
306({ \
307 unsigned int __i; \
308 int __ret = 0; \
309 struct ip6t_entry *__e; \
310 \
311 for (__i = 0; __i < (size); __i += __e->next_offset) { \
312 __e = (void *)(entries) + __i; \
313 \
314 __ret = fn(__e , ## args); \
315 if (__ret != 0) \
316 break; \
317 } \
318 __ret; \
319})
320
321/*
322 * Main firewall chains definitions and global var's definitions.
323 */
324
325#ifdef __KERNEL__
326
327#include <linux/init.h>
328extern void ip6t_init(void) __init;
329
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800330extern int ip6t_register_table(struct xt_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 const struct ip6t_replace *repl);
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800332extern void ip6t_unregister_table(struct xt_table *table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333extern unsigned int ip6t_do_table(struct sk_buff **pskb,
334 unsigned int hook,
335 const struct net_device *in,
336 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800337 struct xt_table *table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* Check for an extension */
340extern int ip6t_ext_hdr(u8 nexthdr);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -0700341/* find specified header and get offset to it */
342extern int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800343 int target, unsigned short *fragoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick McHardy22dea562006-01-05 12:21:34 -0800345extern int ip6_masked_addrcmp(const struct in6_addr *addr1,
346 const struct in6_addr *mask,
347 const struct in6_addr *addr2);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1))
350
351#endif /*__KERNEL__*/
352#endif /* _IP6_TABLES_H */