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 IP 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 _IPTABLES_H |
| 16 | #define _IPTABLES_H |
| 17 | |
| 18 | #ifdef __KERNEL__ |
| 19 | #include <linux/if.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/in.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #endif |
| 25 | #include <linux/compiler.h> |
| 26 | #include <linux/netfilter_ipv4.h> |
| 27 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 28 | #include <linux/netfilter/x_tables.h> |
| 29 | |
| 30 | #define IPT_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN |
| 31 | #define IPT_TABLE_MAXNAMELEN XT_FUNCTION_MAXNAMELEN |
| 32 | #define ipt_match xt_match |
| 33 | #define ipt_target xt_target |
| 34 | #define ipt_table xt_table |
| 35 | #define ipt_get_revision xt_get_revision |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Yes, Virginia, you have to zero the padding. */ |
| 38 | struct ipt_ip { |
| 39 | /* Source and destination IP addr */ |
| 40 | struct in_addr src, dst; |
| 41 | /* Mask for src and dest IP addr */ |
| 42 | struct in_addr smsk, dmsk; |
| 43 | char iniface[IFNAMSIZ], outiface[IFNAMSIZ]; |
| 44 | unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ]; |
| 45 | |
| 46 | /* Protocol, 0 = ANY */ |
| 47 | u_int16_t proto; |
| 48 | |
| 49 | /* Flags word */ |
| 50 | u_int8_t flags; |
| 51 | /* Inverse flags */ |
| 52 | u_int8_t invflags; |
| 53 | }; |
| 54 | |
| 55 | struct ipt_entry_match |
| 56 | { |
| 57 | union { |
| 58 | struct { |
| 59 | u_int16_t match_size; |
| 60 | |
| 61 | /* Used by userspace */ |
| 62 | char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 63 | |
| 64 | u_int8_t revision; |
| 65 | } user; |
| 66 | struct { |
| 67 | u_int16_t match_size; |
| 68 | |
| 69 | /* Used inside the kernel */ |
| 70 | struct ipt_match *match; |
| 71 | } kernel; |
| 72 | |
| 73 | /* Total length */ |
| 74 | u_int16_t match_size; |
| 75 | } u; |
| 76 | |
| 77 | unsigned char data[0]; |
| 78 | }; |
| 79 | |
| 80 | struct ipt_entry_target |
| 81 | { |
| 82 | union { |
| 83 | struct { |
| 84 | u_int16_t target_size; |
| 85 | |
| 86 | /* Used by userspace */ |
| 87 | char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 88 | |
| 89 | u_int8_t revision; |
| 90 | } user; |
| 91 | struct { |
| 92 | u_int16_t target_size; |
| 93 | |
| 94 | /* Used inside the kernel */ |
| 95 | struct ipt_target *target; |
| 96 | } kernel; |
| 97 | |
| 98 | /* Total length */ |
| 99 | u_int16_t target_size; |
| 100 | } u; |
| 101 | |
| 102 | unsigned char data[0]; |
| 103 | }; |
| 104 | |
| 105 | struct ipt_standard_target |
| 106 | { |
| 107 | struct ipt_entry_target target; |
| 108 | int verdict; |
| 109 | }; |
| 110 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 111 | #define ipt_counters xt_counters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* Values for "flag" field in struct ipt_ip (general ip structure). */ |
| 114 | #define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */ |
Patrick McHardy | 0546534 | 2005-08-21 23:31:43 -0700 | [diff] [blame] | 115 | #define IPT_F_GOTO 0x02 /* Set if jump is a goto */ |
| 116 | #define IPT_F_MASK 0x03 /* All possible flag bits mask. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /* Values for "inv" field in struct ipt_ip. */ |
| 119 | #define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */ |
| 120 | #define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */ |
| 121 | #define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */ |
| 122 | #define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */ |
| 123 | #define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */ |
| 124 | #define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | #define IPT_INV_PROTO XT_INV_PROTO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | #define IPT_INV_MASK 0x7F /* All possible flag bits mask. */ |
| 127 | |
| 128 | /* This structure defines each of the firewall rules. Consists of 3 |
| 129 | parts which are 1) general IP header stuff 2) match specific |
| 130 | stuff 3) the target to perform if the rule matches */ |
| 131 | struct ipt_entry |
| 132 | { |
| 133 | struct ipt_ip ip; |
| 134 | |
| 135 | /* Mark with fields that we care about. */ |
| 136 | unsigned int nfcache; |
| 137 | |
| 138 | /* Size of ipt_entry + matches */ |
| 139 | u_int16_t target_offset; |
| 140 | /* Size of ipt_entry + matches + target */ |
| 141 | u_int16_t next_offset; |
| 142 | |
| 143 | /* Back pointer */ |
| 144 | unsigned int comefrom; |
| 145 | |
| 146 | /* Packet and byte counters. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 147 | struct xt_counters counters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* The matches (if any), then the target. */ |
| 150 | unsigned char elems[0]; |
| 151 | }; |
| 152 | |
| 153 | /* |
| 154 | * New IP firewall options for [gs]etsockopt at the RAW IP level. |
| 155 | * Unlike BSD Linux inherits IP options so you don't have to use a raw |
| 156 | * socket for this. Instead we check rights in the calls. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 157 | #define IPT_BASE_CTL XT_BASE_CTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 159 | #define IPT_SO_SET_REPLACE XT_SO_SET_REPLACE |
| 160 | #define IPT_SO_SET_ADD_COUNTERS XT_SO_SET_ADD_COUNTERS |
| 161 | #define IPT_SO_SET_MAX XT_SO_SET_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 163 | #define IPT_SO_GET_INFO XT_SO_GET_INFO |
| 164 | #define IPT_SO_GET_ENTRIES XT_SO_GET_ENTRIES |
| 165 | #define IPT_SO_GET_REVISION_MATCH XT_SO_GET_REVISION_MATCH |
| 166 | #define IPT_SO_GET_REVISION_TARGET XT_SO_GET_REVISION_TARGET |
| 167 | #define IPT_SO_GET_MAX XT_SO_GET_REVISION_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 169 | #define IPT_CONTINUE XT_CONTINUE |
| 170 | #define IPT_RETURN XT_RETURN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 172 | #include <linux/netfilter/xt_tcpudp.h> |
| 173 | #define ipt_udp xt_udp |
| 174 | #define ipt_tcp xt_tcp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 176 | #define IPT_TCP_INV_SRCPT XT_TCP_INV_SRCPT |
| 177 | #define IPT_TCP_INV_DSTPT XT_TCP_INV_DSTPT |
| 178 | #define IPT_TCP_INV_FLAGS XT_TCP_INV_FLAGS |
| 179 | #define IPT_TCP_INV_OPTION XT_TCP_INV_OPTION |
| 180 | #define IPT_TCP_INV_MASK XT_TCP_INV_MASK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 182 | #define IPT_UDP_INV_SRCPT XT_UDP_INV_SRCPT |
| 183 | #define IPT_UDP_INV_DSTPT XT_UDP_INV_DSTPT |
| 184 | #define IPT_UDP_INV_MASK XT_UDP_INV_MASK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* ICMP matching stuff */ |
| 187 | struct ipt_icmp |
| 188 | { |
| 189 | u_int8_t type; /* type to match */ |
| 190 | u_int8_t code[2]; /* range of code */ |
| 191 | u_int8_t invflags; /* Inverse flags */ |
| 192 | }; |
| 193 | |
| 194 | /* Values for "inv" field for struct ipt_icmp. */ |
| 195 | #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */ |
| 196 | |
| 197 | /* The argument to IPT_SO_GET_INFO */ |
| 198 | struct ipt_getinfo |
| 199 | { |
| 200 | /* Which table: caller fills this in. */ |
| 201 | char name[IPT_TABLE_MAXNAMELEN]; |
| 202 | |
| 203 | /* Kernel fills these in. */ |
| 204 | /* Which hook entry points are valid: bitmask */ |
| 205 | unsigned int valid_hooks; |
| 206 | |
| 207 | /* Hook entry points: one per netfilter hook. */ |
| 208 | unsigned int hook_entry[NF_IP_NUMHOOKS]; |
| 209 | |
| 210 | /* Underflow points. */ |
| 211 | unsigned int underflow[NF_IP_NUMHOOKS]; |
| 212 | |
| 213 | /* Number of entries */ |
| 214 | unsigned int num_entries; |
| 215 | |
| 216 | /* Size of entries. */ |
| 217 | unsigned int size; |
| 218 | }; |
| 219 | |
| 220 | /* The argument to IPT_SO_SET_REPLACE. */ |
| 221 | struct ipt_replace |
| 222 | { |
| 223 | /* Which table. */ |
| 224 | char name[IPT_TABLE_MAXNAMELEN]; |
| 225 | |
| 226 | /* Which hook entry points are valid: bitmask. You can't |
| 227 | change this. */ |
| 228 | unsigned int valid_hooks; |
| 229 | |
| 230 | /* Number of entries */ |
| 231 | unsigned int num_entries; |
| 232 | |
| 233 | /* Total size of new entries */ |
| 234 | unsigned int size; |
| 235 | |
| 236 | /* Hook entry points. */ |
| 237 | unsigned int hook_entry[NF_IP_NUMHOOKS]; |
| 238 | |
| 239 | /* Underflow points. */ |
| 240 | unsigned int underflow[NF_IP_NUMHOOKS]; |
| 241 | |
| 242 | /* Information about old entries: */ |
| 243 | /* Number of counters (must be equal to current number of entries). */ |
| 244 | unsigned int num_counters; |
| 245 | /* The old entries' counters. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 246 | struct xt_counters __user *counters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | /* The entries (hang off end: not really an array). */ |
| 249 | struct ipt_entry entries[0]; |
| 250 | }; |
| 251 | |
| 252 | /* The argument to IPT_SO_ADD_COUNTERS. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 253 | #define ipt_counters_info xt_counters_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /* The argument to IPT_SO_GET_ENTRIES. */ |
| 256 | struct ipt_get_entries |
| 257 | { |
| 258 | /* Which table: user fills this in. */ |
| 259 | char name[IPT_TABLE_MAXNAMELEN]; |
| 260 | |
| 261 | /* User fills this in: total entry size. */ |
| 262 | unsigned int size; |
| 263 | |
| 264 | /* The entries. */ |
| 265 | struct ipt_entry entrytable[0]; |
| 266 | }; |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | /* Standard return verdict, or do jump. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 269 | #define IPT_STANDARD_TARGET XT_STANDARD_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* Error verdict. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 271 | #define IPT_ERROR_TARGET XT_ERROR_TARGET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | /* Helper functions */ |
| 274 | static __inline__ struct ipt_entry_target * |
| 275 | ipt_get_target(struct ipt_entry *e) |
| 276 | { |
| 277 | return (void *)e + e->target_offset; |
| 278 | } |
| 279 | |
| 280 | /* fn returns 0 to continue iteration */ |
| 281 | #define IPT_MATCH_ITERATE(e, fn, args...) \ |
| 282 | ({ \ |
| 283 | unsigned int __i; \ |
| 284 | int __ret = 0; \ |
| 285 | struct ipt_entry_match *__match; \ |
| 286 | \ |
| 287 | for (__i = sizeof(struct ipt_entry); \ |
| 288 | __i < (e)->target_offset; \ |
| 289 | __i += __match->u.match_size) { \ |
| 290 | __match = (void *)(e) + __i; \ |
| 291 | \ |
| 292 | __ret = fn(__match , ## args); \ |
| 293 | if (__ret != 0) \ |
| 294 | break; \ |
| 295 | } \ |
| 296 | __ret; \ |
| 297 | }) |
| 298 | |
| 299 | /* fn returns 0 to continue iteration */ |
| 300 | #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \ |
| 301 | ({ \ |
| 302 | unsigned int __i; \ |
| 303 | int __ret = 0; \ |
| 304 | struct ipt_entry *__entry; \ |
| 305 | \ |
| 306 | for (__i = 0; __i < (size); __i += __entry->next_offset) { \ |
| 307 | __entry = (void *)(entries) + __i; \ |
| 308 | \ |
| 309 | __ret = fn(__entry , ## args); \ |
| 310 | if (__ret != 0) \ |
| 311 | break; \ |
| 312 | } \ |
| 313 | __ret; \ |
| 314 | }) |
| 315 | |
| 316 | /* |
| 317 | * Main firewall chains definitions and global var's definitions. |
| 318 | */ |
| 319 | #ifdef __KERNEL__ |
| 320 | |
| 321 | #include <linux/init.h> |
| 322 | extern void ipt_init(void) __init; |
| 323 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 324 | #define ipt_register_target(tgt) xt_register_target(AF_INET, tgt) |
| 325 | #define ipt_unregister_target(tgt) xt_unregister_target(AF_INET, tgt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 327 | #define ipt_register_match(mtch) xt_register_match(AF_INET, mtch) |
| 328 | #define ipt_unregister_match(mtch) xt_unregister_match(AF_INET, mtch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 330 | //#define ipt_register_table(tbl, repl) xt_register_table(AF_INET, tbl, repl) |
| 331 | //#define ipt_unregister_table(tbl) xt_unregister_table(AF_INET, tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 333 | extern int ipt_register_table(struct ipt_table *table, |
| 334 | const struct ipt_replace *repl); |
| 335 | extern void ipt_unregister_table(struct ipt_table *table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | /* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */ |
| 338 | extern struct ipt_target *ipt_find_target(const char *name, u8 revision); |
| 339 | |
| 340 | /* Standard entry. */ |
| 341 | struct ipt_standard |
| 342 | { |
| 343 | struct ipt_entry entry; |
| 344 | struct ipt_standard_target target; |
| 345 | }; |
| 346 | |
| 347 | struct ipt_error_target |
| 348 | { |
| 349 | struct ipt_entry_target target; |
| 350 | char errorname[IPT_FUNCTION_MAXNAMELEN]; |
| 351 | }; |
| 352 | |
| 353 | struct ipt_error |
| 354 | { |
| 355 | struct ipt_entry entry; |
| 356 | struct ipt_error_target target; |
| 357 | }; |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | extern unsigned int ipt_do_table(struct sk_buff **pskb, |
| 360 | unsigned int hook, |
| 361 | const struct net_device *in, |
| 362 | const struct net_device *out, |
| 363 | struct ipt_table *table, |
| 364 | void *userdata); |
| 365 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 366 | #define IPT_ALIGN(s) XT_ALIGN(s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | #endif /*__KERNEL__*/ |
| 368 | #endif /* _IPTABLES_H */ |