1 /* 2 * IP firewalling code. This is taken from 4.4BSD. Please note the 3 * copyright message below. As per the GPL it must be maintained 4 * and the licenses thus do not conflict. While this port is subject 5 * to the GPL I also place my modifications under the original 6 * license in recognition of the original copyright. 7 * 8 * Ported from BSD to Linux, 9 * Alan Cox 22/Nov/1994. 10 * Merged and included the FreeBSD-Current changes at Ugen's request 11 * (but hey it's a lot cleaner now). Ugen would prefer in some ways 12 * we waited for his final product but since Linux 1.2.0 is about to 13 * appear it's not practical - Read: It works, it's not clean but please 14 * don't consider it to be his standard of finished work. 15 * Alan. 16 * 17 * Fixes: 18 * Pauline Middelink : Added masquerading. 19 * 20 * All the real work was done by ..... 21 */ 22
23 /* 24 * Copyright (c) 1993 Daniel Boulet 25 * Copyright (c) 1994 Ugen J.S.Antsilevich 26 * 27 * Redistribution and use in source forms, with and without modification, 28 * are permitted provided that this entire comment appears intact. 29 * 30 * Redistribution in binary form may occur without any restrictions. 31 * Obviously, it would be nice if you gave credit where credit is due 32 * but requiring it would be too onerous. 33 * 34 * This software is provided ``AS IS'' without any warranties of any kind. 35 */ 36
37 /* 38 * Format of an IP firewall descriptor 39 * 40 * src, dst, src_mask, dst_mask are always stored in network byte order. 41 * flags and num_*_ports are stored in host byte order (of course). 42 * Port numbers are stored in HOST byte order. 43 */ 44
45 #ifndef_IP_FW_H 46 #define_IP_FW_H 47
48 structip_fw 49 { 50 structip_fw *fw_next; /* Next firewall on chain */ 51 structin_addrfw_src, fw_dst; /* Source and destination IP addr */ 52 structin_addrfw_smsk, fw_dmsk; /* Mask for src and dest IP addr */ 53 structin_addrfw_via; /* IP address of interface "via" */ 54 unsignedshortfw_flg; /* Flags word */ 55 unsignedshortfw_nsp, fw_ndp; /* N'of src ports and # of dst ports */ 56 /* in ports array (dst ports follow */ 57 /* src ports; max of 10 ports in all; */ 58 /* count of 0 means match all ports) */ 59 #defineIP_FW_MAX_PORTS 10 /* A reasonable maximum */ 60 unsignedshortfw_pts[IP_FW_MAX_PORTS]; /* Array of port numbers to match */ 61 unsignedlongfw_pcnt,fw_bcnt; /* Packet and byte counters */ 62 };
63
64 /* 65 * Values for "flags" field . 66 */ 67
68 #defineIP_FW_F_ALL 0x000 /* This is a universal packet firewall*/ 69 #defineIP_FW_F_TCP 0x001 /* This is a TCP packet firewall */ 70 #defineIP_FW_F_UDP 0x002 /* This is a UDP packet firewall */ 71 #defineIP_FW_F_ICMP 0x003 /* This is a ICMP packet firewall */ 72 #defineIP_FW_F_KIND 0x003 /* Mask to isolate firewall kind */ 73 #defineIP_FW_F_ACCEPT 0x004 /* This is an accept firewall (as * 74 * opposed to a deny firewall)* 75 * */ 76 #defineIP_FW_F_SRNG 0x008 /* The first two src ports are a min * 77 * and max range (stored in host byte * 78 * order). * 79 * */ 80 #defineIP_FW_F_DRNG 0x010 /* The first two dst ports are a min * 81 * and max range (stored in host byte * 82 * order). * 83 * (ports[0] <= port <= ports[1]) * 84 * */ 85 #defineIP_FW_F_PRN 0x020 /* In verbose mode print this firewall*/ 86 #defineIP_FW_F_BIDIR 0x040 /* For bidirectional firewalls */ 87 #defineIP_FW_F_TCPSYN 0x080 /* For tcp packets-check SYN only */ 88 #defineIP_FW_F_ICMPRPL 0x100 /* Send back icmp unreachable packet */ 89 #defineIP_FW_F_MASQ 0x200 /* Masquerading */ 90 #defineIP_FW_F_TCPACK 0x400 /* For tcp-packets match if ACK is set*/ 91 #defineIP_FW_F_MASK 0x7FF /* All possible flag bits mask */ 92
93 /* 94 * New IP firewall options for [gs]etsockopt at the RAW IP level. 95 * Unlike BSD Linux inherits IP options so you don't have to use 96 * a raw socket for this. Instead we check rights in the calls. 97 */ 98
99 #defineIP_FW_BASE_CTL 64
100
101 #defineIP_FW_ADD_BLK (IP_FW_BASE_CTL)
102 #defineIP_FW_ADD_FWD (IP_FW_BASE_CTL+1)
103 #defineIP_FW_CHK_BLK (IP_FW_BASE_CTL+2)
104 #defineIP_FW_CHK_FWD (IP_FW_BASE_CTL+3)
105 #defineIP_FW_DEL_BLK (IP_FW_BASE_CTL+4)
106 #defineIP_FW_DEL_FWD (IP_FW_BASE_CTL+5)
107 #defineIP_FW_FLUSH_BLK (IP_FW_BASE_CTL+6)
108 #defineIP_FW_FLUSH_FWD (IP_FW_BASE_CTL+7)
109 #defineIP_FW_ZERO_BLK (IP_FW_BASE_CTL+8)
110 #defineIP_FW_ZERO_FWD (IP_FW_BASE_CTL+9)
111 #defineIP_FW_POLICY_BLK (IP_FW_BASE_CTL+10)
112 #defineIP_FW_POLICY_FWD (IP_FW_BASE_CTL+11)
113
114 #defineIP_ACCT_ADD (IP_FW_BASE_CTL+16)
115 #defineIP_ACCT_DEL (IP_FW_BASE_CTL+17)
116 #defineIP_ACCT_FLUSH (IP_FW_BASE_CTL+18)
117 #defineIP_ACCT_ZERO (IP_FW_BASE_CTL+19)
118
119 structip_fwpkt 120 { 121 structiphdrfwp_iph; /* IP header */ 122 union{ 123 structtcphdr fwp_tcph; /* TCP header or */ 124 structudphdr fwp_udph; /* UDP header */ 125 } fwp_protoh;
126 structin_addrfwp_via; /* interface address */ 127 };
128
129 /* 130 * Main firewall chains definitions and global var's definitions. 131 */ 132
133 #ifdef__KERNEL__ 134
135 #include <linux/config.h>
136
137 #ifdefCONFIG_IP_MASQUERADE 138 structip_masq{ 139 structip_masq *next; /* next member in list */ 140 structtimer_listtimer; /* Expiration timer */ 141 __u16protocol; /* Which protocol are we talking? */ 142 __u32src, dst; /* Source and destination IP addresses */ 143 __u16sport,dport; /* Source and destoination ports */ 144 __u16mport; /* Masquaraded port */ 145 __u32init_seq; /* Add delta from this seq. on */ 146 shortdelta; /* Delta in sequence numbers */ 147 charsawfin; /* Did we saw an FIN packet? */ 148 };
149 externstructip_masq *ip_msq_hosts;
150 externvoidip_fw_masquerade(structsk_buff **, structdevice *);
151 externintip_fw_demasquerade(structsk_buff *);
152 #endif 153 #ifdefCONFIG_IP_FIREWALL 154 externstructip_fw *ip_fw_blk_chain;
155 externstructip_fw *ip_fw_fwd_chain;
156 externintip_fw_blk_policy;
157 externintip_fw_fwd_policy;
158 externintip_fw_ctl(int, void *, int);
159 #endif 160 #ifdefCONFIG_IP_ACCT 161 externstructip_fw *ip_acct_chain;
162 externvoidip_acct_cnt(structiphdr *, structdevice *, structip_fw *);
163 externintip_acct_ctl(int, void *, int);
164 #endif 165
166 #defineFW_BLOCK 0
167 #defineFW_ACCEPT 1
168 #defineFW_REJECT (-1)
169 #defineFW_MASQUERADE 2
170
171 externintip_fw_chk(structiphdr *, structdevice *rif,structip_fw *, int, int);
172 externvoidip_fw_init(void);
173 #endif/* KERNEL */ 174
175 #ifdefCONFIG_IP_MASQUERADE 176
177 #undefDEBUG_MASQ 178
179 #defineMASQUERADE_EXPIRE_TCP 15*60*HZ 180 #defineMASQUERADE_EXPIRE_TCP_FIN 2*60*HZ 181 #defineMASQUERADE_EXPIRE_UDP 5*60*HZ 182
183 /* 184 * Linux ports don't normally get allocated above 32K. I used an extra 4K port-space 185 */ 186
187 #definePORT_MASQ_BEGIN 60000
188 #definePORT_MASQ_END (PORT_MASQ_BEGIN+4096)
189 #endif 190
191 #endif/* _IP_FW_H */