Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 28 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* Yes, Virginia, you have to zero the padding. */ |
| 39 | struct 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 Kozakai | 7bfe246 | 2007-07-07 22:14:23 -0700 | [diff] [blame] | 47 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 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 Mishin | 1e30a01 | 2006-03-22 13:56:56 -0800 | [diff] [blame] | 65 | #define ip6t_entry_match xt_entry_match |
| 66 | #define ip6t_entry_target xt_entry_target |
| 67 | #define ip6t_standard_target xt_standard_target |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 69 | #define ip6t_counters xt_counters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 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 McHardy | 0546534 | 2005-08-21 23:31:43 -0700 | [diff] [blame] | 75 | #define IP6T_F_GOTO 0x04 /* Set if jump is a goto */ |
| 76 | #define IP6T_F_MASK 0x07 /* All possible flag bits mask. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 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 Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 85 | #define IP6T_INV_PROTO XT_INV_PROTO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #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 */ |
| 91 | struct ip6t_entry |
| 92 | { |
| 93 | struct ip6t_ip6 ipv6; |
| 94 | |
| 95 | /* Mark with fields that we care about. */ |
| 96 | unsigned int nfcache; |
| 97 | |
| 98 | /* Size of ipt_entry + matches */ |
| 99 | u_int16_t target_offset; |
| 100 | /* Size of ipt_entry + matches + target */ |
| 101 | u_int16_t next_offset; |
| 102 | |
| 103 | /* Back pointer */ |
| 104 | unsigned int comefrom; |
| 105 | |
| 106 | /* Packet and byte counters. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 107 | struct xt_counters counters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* The matches (if any), then the target. */ |
| 110 | unsigned char elems[0]; |
| 111 | }; |
| 112 | |
Patrick McHardy | 9934e81 | 2007-02-07 15:14:28 -0800 | [diff] [blame] | 113 | /* Standard entry */ |
| 114 | struct ip6t_standard |
| 115 | { |
| 116 | struct ip6t_entry entry; |
| 117 | struct ip6t_standard_target target; |
| 118 | }; |
| 119 | |
| 120 | struct ip6t_error_target |
| 121 | { |
| 122 | struct ip6t_entry_target target; |
| 123 | char errorname[IP6T_FUNCTION_MAXNAMELEN]; |
| 124 | }; |
| 125 | |
| 126 | struct ip6t_error |
| 127 | { |
| 128 | struct ip6t_entry entry; |
| 129 | struct ip6t_error_target target; |
| 130 | }; |
| 131 | |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 132 | #define IP6T_ENTRY_INIT(__size) \ |
| 133 | { \ |
| 134 | .target_offset = sizeof(struct ip6t_entry), \ |
| 135 | .next_offset = (__size), \ |
| 136 | } |
| 137 | |
| 138 | #define IP6T_STANDARD_INIT(__verdict) \ |
| 139 | { \ |
| 140 | .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_standard)), \ |
| 141 | .target = XT_TARGET_INIT(IP6T_STANDARD_TARGET, \ |
| 142 | sizeof(struct ip6t_standard_target)), \ |
| 143 | .target.verdict = -(__verdict) - 1, \ |
| 144 | } |
| 145 | |
| 146 | #define IP6T_ERROR_INIT \ |
| 147 | { \ |
| 148 | .entry = IP6T_ENTRY_INIT(sizeof(struct ip6t_error)), \ |
| 149 | .target = XT_TARGET_INIT(IP6T_ERROR_TARGET, \ |
| 150 | sizeof(struct ip6t_error_target)), \ |
| 151 | .target.errorname = "ERROR", \ |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* |
| 155 | * New IP firewall options for [gs]etsockopt at the RAW IP level. |
| 156 | * Unlike BSD Linux inherits IP options so you don't have to use |
Yasuyuki Kozakai | b96e7ec | 2006-11-14 19:48:48 -0800 | [diff] [blame] | 157 | * a raw socket for this. Instead we check rights in the calls. |
| 158 | * |
| 159 | * ATTENTION: check linux/in6.h before adding new number here. |
| 160 | */ |
| 161 | #define IP6T_BASE_CTL 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Yasuyuki Kozakai | b96e7ec | 2006-11-14 19:48:48 -0800 | [diff] [blame] | 163 | #define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL) |
| 164 | #define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1) |
| 165 | #define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Yasuyuki Kozakai | b96e7ec | 2006-11-14 19:48:48 -0800 | [diff] [blame] | 167 | #define IP6T_SO_GET_INFO (IP6T_BASE_CTL) |
| 168 | #define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1) |
| 169 | #define IP6T_SO_GET_REVISION_MATCH (IP6T_BASE_CTL + 4) |
| 170 | #define IP6T_SO_GET_REVISION_TARGET (IP6T_BASE_CTL + 5) |
| 171 | #define IP6T_SO_GET_MAX IP6T_SO_GET_REVISION_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* CONTINUE verdict for targets */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 174 | #define IP6T_CONTINUE XT_CONTINUE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* For standard target */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 177 | #define IP6T_RETURN XT_RETURN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 179 | /* TCP/UDP matching stuff */ |
| 180 | #include <linux/netfilter/xt_tcpudp.h> |
| 181 | |
| 182 | #define ip6t_tcp xt_tcp |
| 183 | #define ip6t_udp xt_udp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* Values for "inv" field in struct ipt_tcp. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 186 | #define IP6T_TCP_INV_SRCPT XT_TCP_INV_SRCPT |
| 187 | #define IP6T_TCP_INV_DSTPT XT_TCP_INV_DSTPT |
| 188 | #define IP6T_TCP_INV_FLAGS XT_TCP_INV_FLAGS |
| 189 | #define IP6T_TCP_INV_OPTION XT_TCP_INV_OPTION |
| 190 | #define IP6T_TCP_INV_MASK XT_TCP_INV_MASK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | /* Values for "invflags" field in struct ipt_udp. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 193 | #define IP6T_UDP_INV_SRCPT XT_UDP_INV_SRCPT |
| 194 | #define IP6T_UDP_INV_DSTPT XT_UDP_INV_DSTPT |
| 195 | #define IP6T_UDP_INV_MASK XT_UDP_INV_MASK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | /* ICMP matching stuff */ |
| 198 | struct ip6t_icmp |
| 199 | { |
| 200 | u_int8_t type; /* type to match */ |
| 201 | u_int8_t code[2]; /* range of code */ |
| 202 | u_int8_t invflags; /* Inverse flags */ |
| 203 | }; |
| 204 | |
| 205 | /* Values for "inv" field for struct ipt_icmp. */ |
| 206 | #define IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */ |
| 207 | |
| 208 | /* The argument to IP6T_SO_GET_INFO */ |
| 209 | struct ip6t_getinfo |
| 210 | { |
| 211 | /* Which table: caller fills this in. */ |
| 212 | char name[IP6T_TABLE_MAXNAMELEN]; |
| 213 | |
| 214 | /* Kernel fills these in. */ |
| 215 | /* Which hook entry points are valid: bitmask */ |
| 216 | unsigned int valid_hooks; |
| 217 | |
| 218 | /* Hook entry points: one per netfilter hook. */ |
| 219 | unsigned int hook_entry[NF_IP6_NUMHOOKS]; |
| 220 | |
| 221 | /* Underflow points. */ |
| 222 | unsigned int underflow[NF_IP6_NUMHOOKS]; |
| 223 | |
| 224 | /* Number of entries */ |
| 225 | unsigned int num_entries; |
| 226 | |
| 227 | /* Size of entries. */ |
| 228 | unsigned int size; |
| 229 | }; |
| 230 | |
| 231 | /* The argument to IP6T_SO_SET_REPLACE. */ |
| 232 | struct ip6t_replace |
| 233 | { |
| 234 | /* Which table. */ |
| 235 | char name[IP6T_TABLE_MAXNAMELEN]; |
| 236 | |
| 237 | /* Which hook entry points are valid: bitmask. You can't |
| 238 | change this. */ |
| 239 | unsigned int valid_hooks; |
| 240 | |
| 241 | /* Number of entries */ |
| 242 | unsigned int num_entries; |
| 243 | |
| 244 | /* Total size of new entries */ |
| 245 | unsigned int size; |
| 246 | |
| 247 | /* Hook entry points. */ |
| 248 | unsigned int hook_entry[NF_IP6_NUMHOOKS]; |
| 249 | |
| 250 | /* Underflow points. */ |
| 251 | unsigned int underflow[NF_IP6_NUMHOOKS]; |
| 252 | |
| 253 | /* Information about old entries: */ |
| 254 | /* Number of counters (must be equal to current number of entries). */ |
| 255 | unsigned int num_counters; |
| 256 | /* The old entries' counters. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 257 | struct xt_counters __user *counters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | /* The entries (hang off end: not really an array). */ |
| 260 | struct ip6t_entry entries[0]; |
| 261 | }; |
| 262 | |
| 263 | /* The argument to IP6T_SO_ADD_COUNTERS. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 264 | #define ip6t_counters_info xt_counters_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | /* The argument to IP6T_SO_GET_ENTRIES. */ |
| 267 | struct ip6t_get_entries |
| 268 | { |
| 269 | /* Which table: user fills this in. */ |
| 270 | char name[IP6T_TABLE_MAXNAMELEN]; |
| 271 | |
| 272 | /* User fills this in: total entry size. */ |
| 273 | unsigned int size; |
| 274 | |
| 275 | /* The entries. */ |
| 276 | struct ip6t_entry entrytable[0]; |
| 277 | }; |
| 278 | |
| 279 | /* Standard return verdict, or do jump. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 280 | #define IP6T_STANDARD_TARGET XT_STANDARD_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | /* Error verdict. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 282 | #define IP6T_ERROR_TARGET XT_ERROR_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* Helper functions */ |
| 285 | static __inline__ struct ip6t_entry_target * |
| 286 | ip6t_get_target(struct ip6t_entry *e) |
| 287 | { |
| 288 | return (void *)e + e->target_offset; |
| 289 | } |
| 290 | |
| 291 | /* fn returns 0 to continue iteration */ |
| 292 | #define IP6T_MATCH_ITERATE(e, fn, args...) \ |
| 293 | ({ \ |
| 294 | unsigned int __i; \ |
| 295 | int __ret = 0; \ |
| 296 | struct ip6t_entry_match *__m; \ |
| 297 | \ |
| 298 | for (__i = sizeof(struct ip6t_entry); \ |
| 299 | __i < (e)->target_offset; \ |
| 300 | __i += __m->u.match_size) { \ |
| 301 | __m = (void *)(e) + __i; \ |
| 302 | \ |
| 303 | __ret = fn(__m , ## args); \ |
| 304 | if (__ret != 0) \ |
| 305 | break; \ |
| 306 | } \ |
| 307 | __ret; \ |
| 308 | }) |
| 309 | |
| 310 | /* fn returns 0 to continue iteration */ |
| 311 | #define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \ |
| 312 | ({ \ |
| 313 | unsigned int __i; \ |
| 314 | int __ret = 0; \ |
| 315 | struct ip6t_entry *__e; \ |
| 316 | \ |
| 317 | for (__i = 0; __i < (size); __i += __e->next_offset) { \ |
| 318 | __e = (void *)(entries) + __i; \ |
| 319 | \ |
| 320 | __ret = fn(__e , ## args); \ |
| 321 | if (__ret != 0) \ |
| 322 | break; \ |
| 323 | } \ |
| 324 | __ret; \ |
| 325 | }) |
| 326 | |
| 327 | /* |
| 328 | * Main firewall chains definitions and global var's definitions. |
| 329 | */ |
| 330 | |
| 331 | #ifdef __KERNEL__ |
| 332 | |
| 333 | #include <linux/init.h> |
| 334 | extern void ip6t_init(void) __init; |
| 335 | |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 336 | extern int ip6t_register_table(struct xt_table *table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | const struct ip6t_replace *repl); |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 338 | extern void ip6t_unregister_table(struct xt_table *table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | extern unsigned int ip6t_do_table(struct sk_buff **pskb, |
| 340 | unsigned int hook, |
| 341 | const struct net_device *in, |
| 342 | const struct net_device *out, |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 343 | struct xt_table *table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | /* Check for an extension */ |
| 346 | extern int ip6t_ext_hdr(u8 nexthdr); |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 347 | /* find specified header and get offset to it */ |
| 348 | extern int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, |
Patrick McHardy | b777e0c | 2006-01-05 12:21:16 -0800 | [diff] [blame] | 349 | int target, unsigned short *fragoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Patrick McHardy | 22dea56 | 2006-01-05 12:21:34 -0800 | [diff] [blame] | 351 | extern int ip6_masked_addrcmp(const struct in6_addr *addr1, |
| 352 | const struct in6_addr *mask, |
| 353 | const struct in6_addr *addr2); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | #define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1)) |
| 356 | |
| 357 | #endif /*__KERNEL__*/ |
| 358 | #endif /* _IP6_TABLES_H */ |