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 * Jos Vos : Separate input and output firewall
20 * chains, new "insert" and "append"
21 * commands to replace "add" commands,
22 * add ICMP header to struct ip_fwpkt.
23 * Jos Vos : Add support for matching device names.
24 *
25 * All the real work was done by .....
26 */
27
28 /*
29 * Copyright (c) 1993 Daniel Boulet
30 * Copyright (c) 1994 Ugen J.S.Antsilevich
31 *
32 * Redistribution and use in source forms, with and without modification,
33 * are permitted provided that this entire comment appears intact.
34 *
35 * Redistribution in binary form may occur without any restrictions.
36 * Obviously, it would be nice if you gave credit where credit is due
37 * but requiring it would be too onerous.
38 *
39 * This software is provided ``AS IS'' without any warranties of any kind.
40 */
41
42 /*
43 * Format of an IP firewall descriptor
44 *
45 * src, dst, src_mask, dst_mask are always stored in network byte order.
46 * flags and num_*_ports are stored in host byte order (of course).
47 * Port numbers are stored in HOST byte order.
48 */
49
50 #ifndef _IP_FW_H
51 #define _IP_FW_H
52
53 struct ip_fw
54 {
55 struct ip_fw *fw_next; /* Next firewall on chain */
56 struct in_addr fw_src, fw_dst; /* Source and destination IP addr */
57 struct in_addr fw_smsk, fw_dmsk; /* Mask for src and dest IP addr */
58 struct in_addr fw_via; /* IP address of interface "via" */
59 struct device *fw_viadev; /* device of interface "via" */
60 unsigned short fw_flg; /* Flags word */
61 unsigned short fw_nsp, fw_ndp; /* N'of src ports and # of dst ports */
62 /* in ports array (dst ports follow */
63 /* src ports; max of 10 ports in all; */
64 /* count of 0 means match all ports) */
65 #define IP_FW_MAX_PORTS 10 /* A reasonable maximum */
66 unsigned short fw_pts[IP_FW_MAX_PORTS]; /* Array of port numbers to match */
67 unsigned long fw_pcnt,fw_bcnt; /* Packet and byte counters */
68 unsigned char fw_tosand, fw_tosxor; /* Revised packet priority */
69 char fw_vianame[IFNAMSIZ]; /* name of interface "via" */
70 };
71
72 /*
73 * Values for "flags" field .
74 */
75
76 #define IP_FW_F_ALL 0x000 /* This is a universal packet firewall*/
77 #define IP_FW_F_TCP 0x001 /* This is a TCP packet firewall */
78 #define IP_FW_F_UDP 0x002 /* This is a UDP packet firewall */
79 #define IP_FW_F_ICMP 0x003 /* This is a ICMP packet firewall */
80 #define IP_FW_F_KIND 0x003 /* Mask to isolate firewall kind */
81 #define IP_FW_F_ACCEPT 0x004 /* This is an accept firewall (as *
82 * opposed to a deny firewall)*
83 * */
84 #define IP_FW_F_SRNG 0x008 /* The first two src ports are a min *
85 * and max range (stored in host byte *
86 * order). *
87 * */
88 #define IP_FW_F_DRNG 0x010 /* The first two dst ports are a min *
89 * and max range (stored in host byte *
90 * order). *
91 * (ports[0] <= port <= ports[1]) *
92 * */
93 #define IP_FW_F_PRN 0x020 /* In verbose mode print this firewall*/
94 #define IP_FW_F_BIDIR 0x040 /* For bidirectional firewalls */
95 #define IP_FW_F_TCPSYN 0x080 /* For tcp packets-check SYN only */
96 #define IP_FW_F_ICMPRPL 0x100 /* Send back icmp unreachable packet */
97 #define IP_FW_F_MASQ 0x200 /* Masquerading */
98 #define IP_FW_F_TCPACK 0x400 /* For tcp-packets match if ACK is set*/
99
100 #define IP_FW_F_MASK 0x7FF /* All possible flag bits mask */
101
102 /*
103 * New IP firewall options for [gs]etsockopt at the RAW IP level.
104 * Unlike BSD Linux inherits IP options so you don't have to use
105 * a raw socket for this. Instead we check rights in the calls.
106 */
107
108 #define IP_FW_BASE_CTL 64 /* base for firewall socket options */
109
110 #define IP_FW_COMMAND 0x00FF /* mask for command without chain */
111 #define IP_FW_TYPE 0x0300 /* mask for type (chain) */
112 #define IP_FW_SHIFT 8 /* shift count for type (chain) */
113
114 #define IP_FW_FWD 0
115 #define IP_FW_IN 1
116 #define IP_FW_OUT 2
117 #define IP_FW_ACCT 3
118 #define IP_FW_CHAINS 4 /* total number of ip_fw chains */
119
120 #define IP_FW_INSERT (IP_FW_BASE_CTL)
121 #define IP_FW_APPEND (IP_FW_BASE_CTL+1)
122 #define IP_FW_DELETE (IP_FW_BASE_CTL+2)
123 #define IP_FW_FLUSH (IP_FW_BASE_CTL+3)
124 #define IP_FW_ZERO (IP_FW_BASE_CTL+4)
125 #define IP_FW_POLICY (IP_FW_BASE_CTL+5)
126 #define IP_FW_CHECK (IP_FW_BASE_CTL+6)
127
128 #define IP_FW_INSERT_FWD (IP_FW_INSERT | (IP_FW_FWD << IP_FW_SHIFT))
129 #define IP_FW_APPEND_FWD (IP_FW_APPEND | (IP_FW_FWD << IP_FW_SHIFT))
130 #define IP_FW_DELETE_FWD (IP_FW_DELETE | (IP_FW_FWD << IP_FW_SHIFT))
131 #define IP_FW_FLUSH_FWD (IP_FW_FLUSH | (IP_FW_FWD << IP_FW_SHIFT))
132 #define IP_FW_ZERO_FWD (IP_FW_ZERO | (IP_FW_FWD << IP_FW_SHIFT))
133 #define IP_FW_POLICY_FWD (IP_FW_POLICY | (IP_FW_FWD << IP_FW_SHIFT))
134 #define IP_FW_CHECK_FWD (IP_FW_CHECK | (IP_FW_FWD << IP_FW_SHIFT))
135
136 #define IP_FW_INSERT_IN (IP_FW_INSERT | (IP_FW_IN << IP_FW_SHIFT))
137 #define IP_FW_APPEND_IN (IP_FW_APPEND | (IP_FW_IN << IP_FW_SHIFT))
138 #define IP_FW_DELETE_IN (IP_FW_DELETE | (IP_FW_IN << IP_FW_SHIFT))
139 #define IP_FW_FLUSH_IN (IP_FW_FLUSH | (IP_FW_IN << IP_FW_SHIFT))
140 #define IP_FW_ZERO_IN (IP_FW_ZERO | (IP_FW_IN << IP_FW_SHIFT))
141 #define IP_FW_POLICY_IN (IP_FW_POLICY | (IP_FW_IN << IP_FW_SHIFT))
142 #define IP_FW_CHECK_IN (IP_FW_CHECK | (IP_FW_IN << IP_FW_SHIFT))
143
144 #define IP_FW_INSERT_OUT (IP_FW_INSERT | (IP_FW_OUT << IP_FW_SHIFT))
145 #define IP_FW_APPEND_OUT (IP_FW_APPEND | (IP_FW_OUT << IP_FW_SHIFT))
146 #define IP_FW_DELETE_OUT (IP_FW_DELETE | (IP_FW_OUT << IP_FW_SHIFT))
147 #define IP_FW_FLUSH_OUT (IP_FW_FLUSH | (IP_FW_OUT << IP_FW_SHIFT))
148 #define IP_FW_ZERO_OUT (IP_FW_ZERO | (IP_FW_OUT << IP_FW_SHIFT))
149 #define IP_FW_POLICY_OUT (IP_FW_POLICY | (IP_FW_OUT << IP_FW_SHIFT))
150 #define IP_FW_CHECK_OUT (IP_FW_CHECK | (IP_FW_OUT << IP_FW_SHIFT))
151
152 #define IP_ACCT_INSERT (IP_FW_INSERT | (IP_FW_ACCT << IP_FW_SHIFT))
153 #define IP_ACCT_APPEND (IP_FW_APPEND | (IP_FW_ACCT << IP_FW_SHIFT))
154 #define IP_ACCT_DELETE (IP_FW_DELETE | (IP_FW_ACCT << IP_FW_SHIFT))
155 #define IP_ACCT_FLUSH (IP_FW_FLUSH | (IP_FW_ACCT << IP_FW_SHIFT))
156 #define IP_ACCT_ZERO (IP_FW_ZERO | (IP_FW_ACCT << IP_FW_SHIFT))
157
158 struct ip_fwpkt
159 {
160 struct iphdr fwp_iph; /* IP header */
161 union {
162 struct tcphdr fwp_tcph; /* TCP header or */
163 struct udphdr fwp_udph; /* UDP header */
164 struct icmphdr fwp_icmph; /* ICMP header */
165 } fwp_protoh;
166 struct in_addr fwp_via; /* interface address */
167 char fwp_vianame[IFNAMSIZ]; /* interface name */
168 };
169
170 /*
171 * Main firewall chains definitions and global var's definitions.
172 */
173
174 #ifdef __KERNEL__
175
176 #include <linux/config.h>
177
178 #ifdef CONFIG_IP_MASQUERADE
179 struct ip_masq {
180 struct ip_masq *next; /* next member in list */
181 struct timer_list timer; /* Expiration timer */
182 __u16 protocol; /* Which protocol are we talking? */
183 __u32 src, dst; /* Source and destination IP addresses */
184 __u16 sport,dport; /* Source and destination ports */
185 __u16 mport; /* Masquaraded port */
186 __u32 init_seq; /* Add delta from this seq. on */
187 short delta; /* Delta in sequence numbers */
188 short previous_delta; /* Delta in sequence numbers before last resized PORT command */
189 char sawfin; /* Did we saw an FIN packet? */
190 };
191 extern struct ip_masq *ip_msq_hosts;
192 extern void ip_fw_masquerade(struct sk_buff **, struct device *);
193 extern int ip_fw_demasquerade(struct sk_buff *);
194 #endif
195 #ifdef CONFIG_IP_FIREWALL
196 extern struct ip_fw *ip_fw_in_chain;
197 extern struct ip_fw *ip_fw_out_chain;
198 extern struct ip_fw *ip_fw_fwd_chain;
199 extern int ip_fw_in_policy;
200 extern int ip_fw_out_policy;
201 extern int ip_fw_fwd_policy;
202 extern int ip_fw_ctl(int, void *, int);
203 #endif
204 #ifdef CONFIG_IP_ACCT
205 extern struct ip_fw *ip_acct_chain;
206 extern void ip_acct_cnt(struct iphdr *, struct device *, struct ip_fw *);
207 extern int ip_acct_ctl(int, void *, int);
208 #endif
209
210
211 extern int ip_fw_chk(struct iphdr *, struct device *rif,struct ip_fw *, int, int);
212 extern void ip_fw_init(void);
213 #endif /* KERNEL */
214
215 #ifdef CONFIG_IP_MASQUERADE
216
217 #undef DEBUG_MASQ
218
219 #define MASQUERADE_EXPIRE_TCP 15*60*HZ
220 #define MASQUERADE_EXPIRE_TCP_FIN 2*60*HZ
221 #define MASQUERADE_EXPIRE_UDP 5*60*HZ
222
223 /*
224 * Linux ports don't normally get allocated above 32K. I used an extra 4K port-space
225 */
226
227 #define PORT_MASQ_BEGIN 60000
228 #define PORT_MASQ_END (PORT_MASQ_BEGIN+4096)
229 #define FTP_DPORT_TBD (PORT_MASQ_END+1) /* Avoid using hardcoded port 20 for ftp data connection */
230 #endif
231
232 #endif /* _IP_FW_H */