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 | |
| 28 | #define IPT_FUNCTION_MAXNAMELEN 30 |
| 29 | #define IPT_TABLE_MAXNAMELEN 32 |
| 30 | |
| 31 | /* Yes, Virginia, you have to zero the padding. */ |
| 32 | struct ipt_ip { |
| 33 | /* Source and destination IP addr */ |
| 34 | struct in_addr src, dst; |
| 35 | /* Mask for src and dest IP addr */ |
| 36 | struct in_addr smsk, dmsk; |
| 37 | char iniface[IFNAMSIZ], outiface[IFNAMSIZ]; |
| 38 | unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ]; |
| 39 | |
| 40 | /* Protocol, 0 = ANY */ |
| 41 | u_int16_t proto; |
| 42 | |
| 43 | /* Flags word */ |
| 44 | u_int8_t flags; |
| 45 | /* Inverse flags */ |
| 46 | u_int8_t invflags; |
| 47 | }; |
| 48 | |
| 49 | struct ipt_entry_match |
| 50 | { |
| 51 | union { |
| 52 | struct { |
| 53 | u_int16_t match_size; |
| 54 | |
| 55 | /* Used by userspace */ |
| 56 | char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 57 | |
| 58 | u_int8_t revision; |
| 59 | } user; |
| 60 | struct { |
| 61 | u_int16_t match_size; |
| 62 | |
| 63 | /* Used inside the kernel */ |
| 64 | struct ipt_match *match; |
| 65 | } kernel; |
| 66 | |
| 67 | /* Total length */ |
| 68 | u_int16_t match_size; |
| 69 | } u; |
| 70 | |
| 71 | unsigned char data[0]; |
| 72 | }; |
| 73 | |
| 74 | struct ipt_entry_target |
| 75 | { |
| 76 | union { |
| 77 | struct { |
| 78 | u_int16_t target_size; |
| 79 | |
| 80 | /* Used by userspace */ |
| 81 | char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 82 | |
| 83 | u_int8_t revision; |
| 84 | } user; |
| 85 | struct { |
| 86 | u_int16_t target_size; |
| 87 | |
| 88 | /* Used inside the kernel */ |
| 89 | struct ipt_target *target; |
| 90 | } kernel; |
| 91 | |
| 92 | /* Total length */ |
| 93 | u_int16_t target_size; |
| 94 | } u; |
| 95 | |
| 96 | unsigned char data[0]; |
| 97 | }; |
| 98 | |
| 99 | struct ipt_standard_target |
| 100 | { |
| 101 | struct ipt_entry_target target; |
| 102 | int verdict; |
| 103 | }; |
| 104 | |
| 105 | struct ipt_counters |
| 106 | { |
| 107 | u_int64_t pcnt, bcnt; /* Packet and byte counters */ |
| 108 | }; |
| 109 | |
| 110 | /* Values for "flag" field in struct ipt_ip (general ip structure). */ |
| 111 | #define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */ |
| 112 | #define IPT_F_MASK 0x01 /* All possible flag bits mask. */ |
| 113 | |
| 114 | /* Values for "inv" field in struct ipt_ip. */ |
| 115 | #define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */ |
| 116 | #define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */ |
| 117 | #define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */ |
| 118 | #define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */ |
| 119 | #define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */ |
| 120 | #define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */ |
| 121 | #define IPT_INV_PROTO 0x40 /* Invert the sense of PROTO. */ |
| 122 | #define IPT_INV_MASK 0x7F /* All possible flag bits mask. */ |
| 123 | |
| 124 | /* This structure defines each of the firewall rules. Consists of 3 |
| 125 | parts which are 1) general IP header stuff 2) match specific |
| 126 | stuff 3) the target to perform if the rule matches */ |
| 127 | struct ipt_entry |
| 128 | { |
| 129 | struct ipt_ip ip; |
| 130 | |
| 131 | /* Mark with fields that we care about. */ |
| 132 | unsigned int nfcache; |
| 133 | |
| 134 | /* Size of ipt_entry + matches */ |
| 135 | u_int16_t target_offset; |
| 136 | /* Size of ipt_entry + matches + target */ |
| 137 | u_int16_t next_offset; |
| 138 | |
| 139 | /* Back pointer */ |
| 140 | unsigned int comefrom; |
| 141 | |
| 142 | /* Packet and byte counters. */ |
| 143 | struct ipt_counters counters; |
| 144 | |
| 145 | /* The matches (if any), then the target. */ |
| 146 | unsigned char elems[0]; |
| 147 | }; |
| 148 | |
| 149 | /* |
| 150 | * New IP firewall options for [gs]etsockopt at the RAW IP level. |
| 151 | * Unlike BSD Linux inherits IP options so you don't have to use a raw |
| 152 | * socket for this. Instead we check rights in the calls. */ |
| 153 | #define IPT_BASE_CTL 64 /* base for firewall socket options */ |
| 154 | |
| 155 | #define IPT_SO_SET_REPLACE (IPT_BASE_CTL) |
| 156 | #define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1) |
| 157 | #define IPT_SO_SET_MAX IPT_SO_SET_ADD_COUNTERS |
| 158 | |
| 159 | #define IPT_SO_GET_INFO (IPT_BASE_CTL) |
| 160 | #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1) |
| 161 | #define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2) |
| 162 | #define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3) |
| 163 | #define IPT_SO_GET_MAX IPT_SO_GET_REVISION_TARGET |
| 164 | |
| 165 | /* CONTINUE verdict for targets */ |
| 166 | #define IPT_CONTINUE 0xFFFFFFFF |
| 167 | |
| 168 | /* For standard target */ |
| 169 | #define IPT_RETURN (-NF_REPEAT - 1) |
| 170 | |
| 171 | /* TCP matching stuff */ |
| 172 | struct ipt_tcp |
| 173 | { |
| 174 | u_int16_t spts[2]; /* Source port range. */ |
| 175 | u_int16_t dpts[2]; /* Destination port range. */ |
| 176 | u_int8_t option; /* TCP Option iff non-zero*/ |
| 177 | u_int8_t flg_mask; /* TCP flags mask byte */ |
| 178 | u_int8_t flg_cmp; /* TCP flags compare byte */ |
| 179 | u_int8_t invflags; /* Inverse flags */ |
| 180 | }; |
| 181 | |
| 182 | /* Values for "inv" field in struct ipt_tcp. */ |
| 183 | #define IPT_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */ |
| 184 | #define IPT_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */ |
| 185 | #define IPT_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */ |
| 186 | #define IPT_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */ |
| 187 | #define IPT_TCP_INV_MASK 0x0F /* All possible flags. */ |
| 188 | |
| 189 | /* UDP matching stuff */ |
| 190 | struct ipt_udp |
| 191 | { |
| 192 | u_int16_t spts[2]; /* Source port range. */ |
| 193 | u_int16_t dpts[2]; /* Destination port range. */ |
| 194 | u_int8_t invflags; /* Inverse flags */ |
| 195 | }; |
| 196 | |
| 197 | /* Values for "invflags" field in struct ipt_udp. */ |
| 198 | #define IPT_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */ |
| 199 | #define IPT_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */ |
| 200 | #define IPT_UDP_INV_MASK 0x03 /* All possible flags. */ |
| 201 | |
| 202 | /* ICMP matching stuff */ |
| 203 | struct ipt_icmp |
| 204 | { |
| 205 | u_int8_t type; /* type to match */ |
| 206 | u_int8_t code[2]; /* range of code */ |
| 207 | u_int8_t invflags; /* Inverse flags */ |
| 208 | }; |
| 209 | |
| 210 | /* Values for "inv" field for struct ipt_icmp. */ |
| 211 | #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */ |
| 212 | |
| 213 | /* The argument to IPT_SO_GET_INFO */ |
| 214 | struct ipt_getinfo |
| 215 | { |
| 216 | /* Which table: caller fills this in. */ |
| 217 | char name[IPT_TABLE_MAXNAMELEN]; |
| 218 | |
| 219 | /* Kernel fills these in. */ |
| 220 | /* Which hook entry points are valid: bitmask */ |
| 221 | unsigned int valid_hooks; |
| 222 | |
| 223 | /* Hook entry points: one per netfilter hook. */ |
| 224 | unsigned int hook_entry[NF_IP_NUMHOOKS]; |
| 225 | |
| 226 | /* Underflow points. */ |
| 227 | unsigned int underflow[NF_IP_NUMHOOKS]; |
| 228 | |
| 229 | /* Number of entries */ |
| 230 | unsigned int num_entries; |
| 231 | |
| 232 | /* Size of entries. */ |
| 233 | unsigned int size; |
| 234 | }; |
| 235 | |
| 236 | /* The argument to IPT_SO_SET_REPLACE. */ |
| 237 | struct ipt_replace |
| 238 | { |
| 239 | /* Which table. */ |
| 240 | char name[IPT_TABLE_MAXNAMELEN]; |
| 241 | |
| 242 | /* Which hook entry points are valid: bitmask. You can't |
| 243 | change this. */ |
| 244 | unsigned int valid_hooks; |
| 245 | |
| 246 | /* Number of entries */ |
| 247 | unsigned int num_entries; |
| 248 | |
| 249 | /* Total size of new entries */ |
| 250 | unsigned int size; |
| 251 | |
| 252 | /* Hook entry points. */ |
| 253 | unsigned int hook_entry[NF_IP_NUMHOOKS]; |
| 254 | |
| 255 | /* Underflow points. */ |
| 256 | unsigned int underflow[NF_IP_NUMHOOKS]; |
| 257 | |
| 258 | /* Information about old entries: */ |
| 259 | /* Number of counters (must be equal to current number of entries). */ |
| 260 | unsigned int num_counters; |
| 261 | /* The old entries' counters. */ |
| 262 | struct ipt_counters __user *counters; |
| 263 | |
| 264 | /* The entries (hang off end: not really an array). */ |
| 265 | struct ipt_entry entries[0]; |
| 266 | }; |
| 267 | |
| 268 | /* The argument to IPT_SO_ADD_COUNTERS. */ |
| 269 | struct ipt_counters_info |
| 270 | { |
| 271 | /* Which table. */ |
| 272 | char name[IPT_TABLE_MAXNAMELEN]; |
| 273 | |
| 274 | unsigned int num_counters; |
| 275 | |
| 276 | /* The counters (actually `number' of these). */ |
| 277 | struct ipt_counters counters[0]; |
| 278 | }; |
| 279 | |
| 280 | /* The argument to IPT_SO_GET_ENTRIES. */ |
| 281 | struct ipt_get_entries |
| 282 | { |
| 283 | /* Which table: user fills this in. */ |
| 284 | char name[IPT_TABLE_MAXNAMELEN]; |
| 285 | |
| 286 | /* User fills this in: total entry size. */ |
| 287 | unsigned int size; |
| 288 | |
| 289 | /* The entries. */ |
| 290 | struct ipt_entry entrytable[0]; |
| 291 | }; |
| 292 | |
| 293 | /* The argument to IPT_SO_GET_REVISION_*. Returns highest revision |
| 294 | * kernel supports, if >= revision. */ |
| 295 | struct ipt_get_revision |
| 296 | { |
| 297 | char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 298 | |
| 299 | u_int8_t revision; |
| 300 | }; |
| 301 | |
| 302 | /* Standard return verdict, or do jump. */ |
| 303 | #define IPT_STANDARD_TARGET "" |
| 304 | /* Error verdict. */ |
| 305 | #define IPT_ERROR_TARGET "ERROR" |
| 306 | |
| 307 | /* Helper functions */ |
| 308 | static __inline__ struct ipt_entry_target * |
| 309 | ipt_get_target(struct ipt_entry *e) |
| 310 | { |
| 311 | return (void *)e + e->target_offset; |
| 312 | } |
| 313 | |
| 314 | /* fn returns 0 to continue iteration */ |
| 315 | #define IPT_MATCH_ITERATE(e, fn, args...) \ |
| 316 | ({ \ |
| 317 | unsigned int __i; \ |
| 318 | int __ret = 0; \ |
| 319 | struct ipt_entry_match *__match; \ |
| 320 | \ |
| 321 | for (__i = sizeof(struct ipt_entry); \ |
| 322 | __i < (e)->target_offset; \ |
| 323 | __i += __match->u.match_size) { \ |
| 324 | __match = (void *)(e) + __i; \ |
| 325 | \ |
| 326 | __ret = fn(__match , ## args); \ |
| 327 | if (__ret != 0) \ |
| 328 | break; \ |
| 329 | } \ |
| 330 | __ret; \ |
| 331 | }) |
| 332 | |
| 333 | /* fn returns 0 to continue iteration */ |
| 334 | #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \ |
| 335 | ({ \ |
| 336 | unsigned int __i; \ |
| 337 | int __ret = 0; \ |
| 338 | struct ipt_entry *__entry; \ |
| 339 | \ |
| 340 | for (__i = 0; __i < (size); __i += __entry->next_offset) { \ |
| 341 | __entry = (void *)(entries) + __i; \ |
| 342 | \ |
| 343 | __ret = fn(__entry , ## args); \ |
| 344 | if (__ret != 0) \ |
| 345 | break; \ |
| 346 | } \ |
| 347 | __ret; \ |
| 348 | }) |
| 349 | |
| 350 | /* |
| 351 | * Main firewall chains definitions and global var's definitions. |
| 352 | */ |
| 353 | #ifdef __KERNEL__ |
| 354 | |
| 355 | #include <linux/init.h> |
| 356 | extern void ipt_init(void) __init; |
| 357 | |
| 358 | struct ipt_match |
| 359 | { |
| 360 | struct list_head list; |
| 361 | |
| 362 | const char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 363 | |
| 364 | u_int8_t revision; |
| 365 | |
| 366 | /* Return true or false: return FALSE and set *hotdrop = 1 to |
| 367 | force immediate packet drop. */ |
| 368 | /* Arguments changed since 2.4, as this must now handle |
| 369 | non-linear skbs, using skb_copy_bits and |
| 370 | skb_ip_make_writable. */ |
| 371 | int (*match)(const struct sk_buff *skb, |
| 372 | const struct net_device *in, |
| 373 | const struct net_device *out, |
| 374 | const void *matchinfo, |
| 375 | int offset, |
| 376 | int *hotdrop); |
| 377 | |
| 378 | /* Called when user tries to insert an entry of this type. */ |
| 379 | /* Should return true or false. */ |
| 380 | int (*checkentry)(const char *tablename, |
| 381 | const struct ipt_ip *ip, |
| 382 | void *matchinfo, |
| 383 | unsigned int matchinfosize, |
| 384 | unsigned int hook_mask); |
| 385 | |
| 386 | /* Called when entry of this type deleted. */ |
| 387 | void (*destroy)(void *matchinfo, unsigned int matchinfosize); |
| 388 | |
| 389 | /* Set this to THIS_MODULE. */ |
| 390 | struct module *me; |
| 391 | }; |
| 392 | |
| 393 | /* Registration hooks for targets. */ |
| 394 | struct ipt_target |
| 395 | { |
| 396 | struct list_head list; |
| 397 | |
| 398 | const char name[IPT_FUNCTION_MAXNAMELEN-1]; |
| 399 | |
| 400 | u_int8_t revision; |
| 401 | |
| 402 | /* Called when user tries to insert an entry of this type: |
| 403 | hook_mask is a bitmask of hooks from which it can be |
| 404 | called. */ |
| 405 | /* Should return true or false. */ |
| 406 | int (*checkentry)(const char *tablename, |
| 407 | const struct ipt_entry *e, |
| 408 | void *targinfo, |
| 409 | unsigned int targinfosize, |
| 410 | unsigned int hook_mask); |
| 411 | |
| 412 | /* Called when entry of this type deleted. */ |
| 413 | void (*destroy)(void *targinfo, unsigned int targinfosize); |
| 414 | |
| 415 | /* Returns verdict. Argument order changed since 2.4, as this |
| 416 | must now handle non-linear skbs, using skb_copy_bits and |
| 417 | skb_ip_make_writable. */ |
| 418 | unsigned int (*target)(struct sk_buff **pskb, |
| 419 | const struct net_device *in, |
| 420 | const struct net_device *out, |
| 421 | unsigned int hooknum, |
| 422 | const void *targinfo, |
| 423 | void *userdata); |
| 424 | |
| 425 | /* Set this to THIS_MODULE. */ |
| 426 | struct module *me; |
| 427 | }; |
| 428 | |
| 429 | extern int ipt_register_target(struct ipt_target *target); |
| 430 | extern void ipt_unregister_target(struct ipt_target *target); |
| 431 | |
| 432 | extern int ipt_register_match(struct ipt_match *match); |
| 433 | extern void ipt_unregister_match(struct ipt_match *match); |
| 434 | |
| 435 | /* Furniture shopping... */ |
| 436 | struct ipt_table |
| 437 | { |
| 438 | struct list_head list; |
| 439 | |
| 440 | /* A unique name... */ |
| 441 | char name[IPT_TABLE_MAXNAMELEN]; |
| 442 | |
| 443 | /* What hooks you will enter on */ |
| 444 | unsigned int valid_hooks; |
| 445 | |
| 446 | /* Lock for the curtain */ |
| 447 | rwlock_t lock; |
| 448 | |
| 449 | /* Man behind the curtain... */ |
| 450 | struct ipt_table_info *private; |
| 451 | |
| 452 | /* Set to THIS_MODULE. */ |
| 453 | struct module *me; |
| 454 | }; |
| 455 | |
| 456 | /* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */ |
| 457 | extern struct ipt_target *ipt_find_target(const char *name, u8 revision); |
| 458 | |
| 459 | /* Standard entry. */ |
| 460 | struct ipt_standard |
| 461 | { |
| 462 | struct ipt_entry entry; |
| 463 | struct ipt_standard_target target; |
| 464 | }; |
| 465 | |
| 466 | struct ipt_error_target |
| 467 | { |
| 468 | struct ipt_entry_target target; |
| 469 | char errorname[IPT_FUNCTION_MAXNAMELEN]; |
| 470 | }; |
| 471 | |
| 472 | struct ipt_error |
| 473 | { |
| 474 | struct ipt_entry entry; |
| 475 | struct ipt_error_target target; |
| 476 | }; |
| 477 | |
| 478 | extern int ipt_register_table(struct ipt_table *table, |
| 479 | const struct ipt_replace *repl); |
| 480 | extern void ipt_unregister_table(struct ipt_table *table); |
| 481 | extern unsigned int ipt_do_table(struct sk_buff **pskb, |
| 482 | unsigned int hook, |
| 483 | const struct net_device *in, |
| 484 | const struct net_device *out, |
| 485 | struct ipt_table *table, |
| 486 | void *userdata); |
| 487 | |
| 488 | #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1)) |
| 489 | #endif /*__KERNEL__*/ |
| 490 | #endif /* _IPTABLES_H */ |