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/in.h> 21#include <linux/ip.h> 22#include <linux/skbuff.h> 23#endif 24#include <linux/types.h> 25#include <linux/compiler.h> 26#include <linux/netfilter_ipv4.h> 27 28#include <linux/netfilter/x_tables.h> 29 30#define IPT_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN 31#define IPT_TABLE_MAXNAMELEN XT_TABLE_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 36 37/* Yes, Virginia, you have to zero the padding. */ 38struct 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#define ipt_entry_match xt_entry_match 56#define ipt_entry_target xt_entry_target 57#define ipt_standard_target xt_standard_target 58 59#define ipt_counters xt_counters 60 61/* Values for "flag" field in struct ipt_ip (general ip structure). */ 62#define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */ 63#define IPT_F_GOTO 0x02 /* Set if jump is a goto */ 64#define IPT_F_MASK 0x03 /* All possible flag bits mask. */ 65#define IPT_F_NO_DEF_MATCH 0x80 /* Internal: no default match rules present */ 66 67/* Values for "inv" field in struct ipt_ip. */ 68#define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */ 69#define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */ 70#define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */ 71#define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */ 72#define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */ 73#define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */ 74#define IPT_INV_PROTO XT_INV_PROTO 75#define IPT_INV_MASK 0x7F /* All possible flag bits mask. */ 76 77/* This structure defines each of the firewall rules. Consists of 3 78 parts which are 1) general IP header stuff 2) match specific 79 stuff 3) the target to perform if the rule matches */ 80struct ipt_entry { 81 struct ipt_ip ip; 82 83 /* Mark with fields that we care about. */ 84 unsigned int nfcache; 85 86 /* Size of ipt_entry + matches */ 87 u_int16_t target_offset; 88 /* Size of ipt_entry + matches + target */ 89 u_int16_t next_offset; 90 91 /* Back pointer */ 92 unsigned int comefrom; 93 94 /* Packet and byte counters. */ 95 struct xt_counters counters; 96 97 /* The matches (if any), then the target. */ 98 unsigned char elems[0]; 99}; 100 101/* 102 * New IP firewall options for [gs]etsockopt at the RAW IP level. 103 * Unlike BSD Linux inherits IP options so you don't have to use a raw 104 * socket for this. Instead we check rights in the calls. 105 * 106 * ATTENTION: check linux/in.h before adding new number here. 107 */ 108#define IPT_BASE_CTL 64 109 110#define IPT_SO_SET_REPLACE (IPT_BASE_CTL) 111#define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1) 112#define IPT_SO_SET_MAX IPT_SO_SET_ADD_COUNTERS 113 114#define IPT_SO_GET_INFO (IPT_BASE_CTL) 115#define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1) 116#define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2) 117#define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3) 118#define IPT_SO_GET_MAX IPT_SO_GET_REVISION_TARGET 119 120#define IPT_CONTINUE XT_CONTINUE 121#define IPT_RETURN XT_RETURN 122 123#include <linux/netfilter/xt_tcpudp.h> 124#define ipt_udp xt_udp 125#define ipt_tcp xt_tcp 126 127#define IPT_TCP_INV_SRCPT XT_TCP_INV_SRCPT 128#define IPT_TCP_INV_DSTPT XT_TCP_INV_DSTPT 129#define IPT_TCP_INV_FLAGS XT_TCP_INV_FLAGS 130#define IPT_TCP_INV_OPTION XT_TCP_INV_OPTION 131#define IPT_TCP_INV_MASK XT_TCP_INV_MASK 132 133#define IPT_UDP_INV_SRCPT XT_UDP_INV_SRCPT 134#define IPT_UDP_INV_DSTPT XT_UDP_INV_DSTPT 135#define IPT_UDP_INV_MASK XT_UDP_INV_MASK 136 137/* ICMP matching stuff */ 138struct ipt_icmp { 139 u_int8_t type; /* type to match */ 140 u_int8_t code[2]; /* range of code */ 141 u_int8_t invflags; /* Inverse flags */ 142}; 143 144/* Values for "inv" field for struct ipt_icmp. */ 145#define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */ 146 147/* The argument to IPT_SO_GET_INFO */ 148struct ipt_getinfo { 149 /* Which table: caller fills this in. */ 150 char name[IPT_TABLE_MAXNAMELEN]; 151 152 /* Kernel fills these in. */ 153 /* Which hook entry points are valid: bitmask */ 154 unsigned int valid_hooks; 155 156 /* Hook entry points: one per netfilter hook. */ 157 unsigned int hook_entry[NF_INET_NUMHOOKS]; 158 159 /* Underflow points. */ 160 unsigned int underflow[NF_INET_NUMHOOKS]; 161 162 /* Number of entries */ 163 unsigned int num_entries; 164 165 /* Size of entries. */ 166 unsigned int size; 167}; 168 169/* The argument to IPT_SO_SET_REPLACE. */ 170struct ipt_replace { 171 /* Which table. */ 172 char name[IPT_TABLE_MAXNAMELEN]; 173 174 /* Which hook entry points are valid: bitmask. You can't 175 change this. */ 176 unsigned int valid_hooks; 177 178 /* Number of entries */ 179 unsigned int num_entries; 180 181 /* Total size of new entries */ 182 unsigned int size; 183 184 /* Hook entry points. */ 185 unsigned int hook_entry[NF_INET_NUMHOOKS]; 186 187 /* Underflow points. */ 188 unsigned int underflow[NF_INET_NUMHOOKS]; 189 190 /* Information about old entries: */ 191 /* Number of counters (must be equal to current number of entries). */ 192 unsigned int num_counters; 193 /* The old entries' counters. */ 194 struct xt_counters __user *counters; 195 196 /* The entries (hang off end: not really an array). */ 197 struct ipt_entry entries[0]; 198}; 199 200/* The argument to IPT_SO_ADD_COUNTERS. */ 201#define ipt_counters_info xt_counters_info 202 203/* The argument to IPT_SO_GET_ENTRIES. */ 204struct ipt_get_entries { 205 /* Which table: user fills this in. */ 206 char name[IPT_TABLE_MAXNAMELEN]; 207 208 /* User fills this in: total entry size. */ 209 unsigned int size; 210 211 /* The entries. */ 212 struct ipt_entry entrytable[0]; 213}; 214 215/* Standard return verdict, or do jump. */ 216#define IPT_STANDARD_TARGET XT_STANDARD_TARGET 217/* Error verdict. */ 218#define IPT_ERROR_TARGET XT_ERROR_TARGET 219 220/* Helper functions */ 221static __inline__ struct ipt_entry_target * 222ipt_get_target(struct ipt_entry *e) 223{ 224 return (void *)e + e->target_offset; 225} 226 227#ifndef __KERNEL__ 228/* fn returns 0 to continue iteration */ 229#define IPT_MATCH_ITERATE(e, fn, args...) \ 230 XT_MATCH_ITERATE(struct ipt_entry, e, fn, ## args) 231 232/* fn returns 0 to continue iteration */ 233#define IPT_ENTRY_ITERATE(entries, size, fn, args...) \ 234 XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args) 235#endif 236 237/* 238 * Main firewall chains definitions and global var's definitions. 239 */ 240#ifdef __KERNEL__ 241 242#include <linux/init.h> 243extern void ipt_init(void) __init; 244 245extern struct xt_table *ipt_register_table(struct net *net, 246 const struct xt_table *table, 247 const struct ipt_replace *repl); 248extern void ipt_unregister_table(struct net *net, struct xt_table *table); 249 250/* Standard entry. */ 251struct ipt_standard { 252 struct ipt_entry entry; 253 struct ipt_standard_target target; 254}; 255 256struct ipt_error_target { 257 struct ipt_entry_target target; 258 char errorname[IPT_FUNCTION_MAXNAMELEN]; 259}; 260 261struct ipt_error { 262 struct ipt_entry entry; 263 struct ipt_error_target target; 264}; 265 266#define IPT_ENTRY_INIT(__size) \ 267{ \ 268 .target_offset = sizeof(struct ipt_entry), \ 269 .next_offset = (__size), \ 270} 271 272#define IPT_STANDARD_INIT(__verdict) \ 273{ \ 274 .entry = IPT_ENTRY_INIT(sizeof(struct ipt_standard)), \ 275 .target = XT_TARGET_INIT(IPT_STANDARD_TARGET, \ 276 sizeof(struct xt_standard_target)), \ 277 .target.verdict = -(__verdict) - 1, \ 278} 279 280#define IPT_ERROR_INIT \ 281{ \ 282 .entry = IPT_ENTRY_INIT(sizeof(struct ipt_error)), \ 283 .target = XT_TARGET_INIT(IPT_ERROR_TARGET, \ 284 sizeof(struct ipt_error_target)), \ 285 .target.errorname = "ERROR", \ 286} 287 288extern void *ipt_alloc_initial_table(const struct xt_table *); 289extern unsigned int ipt_do_table(struct sk_buff *skb, 290 unsigned int hook, 291 const struct net_device *in, 292 const struct net_device *out, 293 struct xt_table *table); 294 295#define IPT_ALIGN(s) XT_ALIGN(s) 296 297#ifdef CONFIG_COMPAT 298#include <net/compat.h> 299 300struct compat_ipt_entry { 301 struct ipt_ip ip; 302 compat_uint_t nfcache; 303 u_int16_t target_offset; 304 u_int16_t next_offset; 305 compat_uint_t comefrom; 306 struct compat_xt_counters counters; 307 unsigned char elems[0]; 308}; 309 310/* Helper functions */ 311static inline struct ipt_entry_target * 312compat_ipt_get_target(struct compat_ipt_entry *e) 313{ 314 return (void *)e + e->target_offset; 315} 316 317#define COMPAT_IPT_ALIGN(s) COMPAT_XT_ALIGN(s) 318 319#endif /* CONFIG_COMPAT */ 320#endif /*__KERNEL__*/ 321#endif /* _IPTABLES_H */ 322