rc.firewall revision 65257
1############
2# Setup system for firewall service.
3# $FreeBSD: head/etc/rc.firewall 65257 2000-08-30 13:14:32Z ru $
4
5# Suck in the configuration variables.
6if [ -r /etc/defaults/rc.conf ]; then
7	. /etc/defaults/rc.conf
8	source_rc_confs
9elif [ -r /etc/rc.conf ]; then
10	. /etc/rc.conf
11fi
12
13############
14# Define the firewall type in /etc/rc.conf.  Valid values are:
15#   open     - will allow anyone in
16#   client   - will try to protect just this machine
17#   simple   - will try to protect a whole network
18#   closed   - totally disables IP services except via lo0 interface
19#   UNKNOWN  - disables the loading of firewall rules.
20#   filename - will load the rules in the given filename (full path required)
21#
22# For ``client'' and ``simple'' the entries below should be customized
23# appropriately.
24
25############
26#
27# If you don't know enough about packet filtering, we suggest that you
28# take time to read this book:
29#
30#	Building Internet Firewalls
31#	Brent Chapman and Elizabeth Zwicky
32#
33#	O'Reilly & Associates, Inc
34#	ISBN 1-56592-124-0
35#	http://www.ora.com/
36#
37# For a more advanced treatment of Internet Security read:
38#
39#	Firewalls & Internet Security
40#	Repelling the wily hacker
41#	William R. Cheswick, Steven M. Bellowin
42#
43#	Addison-Wesley
44#	ISBN 0-201-6337-4
45#	http://www.awl.com/
46#
47
48if [ -n "${1}" ]; then
49	firewall_type="${1}"
50fi
51
52############
53# Set quiet mode if requested
54#
55case ${firewall_quiet} in
56[Yy][Ee][Ss])
57	fwcmd="/sbin/ipfw -q"
58	;;
59*)
60	fwcmd="/sbin/ipfw"
61	;;
62esac
63
64############
65# Flush out the list before we begin.
66#
67${fwcmd} -f flush
68
69############
70# Network Address Translation.  All packets are passed to natd(8)
71# before they encounter your remaining rules.  The firewall rules
72# will then be run again on each packet after translation by natd
73# starting at the rule number following the divert rule.
74#
75# For ``simple'' firewall type the divert rule should be put to a
76# different place to not interfere with address-checking rules.
77# 
78case ${firewall_type} in
79[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
80	case ${natd_enable} in
81	[Yy][Ee][Ss])
82		if [ -n "${natd_interface}" ]; then
83			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
84		fi
85		;;
86	esac
87esac
88
89############
90# If you just configured ipfw in the kernel as a tool to solve network
91# problems or you just want to disallow some particular kinds of traffic
92# then you will want to change the default policy to open.  You can also
93# do this as your only action by setting the firewall_type to ``open''.
94#
95# ${fwcmd} add 65000 pass all from any to any
96
97############
98# Only in rare cases do you want to change these rules
99#
100${fwcmd} add 100 pass all from any to any via lo0
101${fwcmd} add 200 deny all from any to 127.0.0.0/8
102# If you're using 'options BRIDGE', uncomment the following line to pass ARP
103#${fwcmd} add 300 pass udp from 0.0.0.0 2054 to 0.0.0.0
104
105
106# Prototype setups.
107#
108case ${firewall_type} in
109[Oo][Pp][Ee][Nn])
110	${fwcmd} add 65000 pass all from any to any
111	;;
112
113[Cc][Ll][Ii][Ee][Nn][Tt])
114	############
115	# This is a prototype setup that will protect your system somewhat
116	# against people from outside your own network.
117	############
118
119	# set these to your network and netmask and ip
120	net="192.0.2.0"
121	mask="255.255.255.0"
122	ip="192.0.2.1"
123
124	# Allow any traffic to or from my own net.
125	${fwcmd} add pass all from ${ip} to ${net}:${mask}
126	${fwcmd} add pass all from ${net}:${mask} to ${ip}
127
128	# Allow TCP through if setup succeeded
129	${fwcmd} add pass tcp from any to any established
130
131	# Allow IP fragments to pass through
132	${fwcmd} add pass all from any to any frag
133
134	# Allow setup of incoming email
135	${fwcmd} add pass tcp from any to ${ip} 25 setup
136
137	# Allow setup of outgoing TCP connections only
138	${fwcmd} add pass tcp from ${ip} to any setup
139
140	# Disallow setup of all other TCP connections
141	${fwcmd} add deny tcp from any to any setup
142
143	# Allow DNS queries out in the world
144	${fwcmd} add pass udp from any 53 to ${ip}
145	${fwcmd} add pass udp from ${ip} to any 53
146
147	# Allow NTP queries out in the world
148	${fwcmd} add pass udp from any 123 to ${ip}
149	${fwcmd} add pass udp from ${ip} to any 123
150
151	# Everything else is denied by default, unless the
152	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
153	# config file.
154	;;
155
156[Ss][Ii][Mm][Pp][Ll][Ee])
157	############
158	# This is a prototype setup for a simple firewall.  Configure this
159	# machine as a named server and ntp server, and point all the machines
160	# on the inside at this machine for those services.
161	############
162
163	# set these to your outside interface network and netmask and ip
164	oif="ed0"
165	onet="192.0.2.0"
166	omask="255.255.255.240"
167	oip="192.0.2.1"
168
169	# set these to your inside interface network and netmask and ip
170	iif="ed1"
171	inet="192.0.2.16"
172	imask="255.255.255.240"
173	iip="192.0.2.17"
174
175	# Stop spoofing
176	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
177	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
178
179	# Stop RFC1918 nets on the outside interface
180	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
181	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
182	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
183
184	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
185	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
186	# on the outside interface
187	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
188	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
189	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
190	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
191	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
192
193	# Network Address Translation.  This rule is placed here deliberately
194	# so that it does not interfere with the surrounding address-checking
195	# rules.  If for example one of your internal LAN machines had its IP
196	# address set to 192.0.2.1 then an incoming packet for it after being
197	# translated by natd(8) would match the `deny' rule above.  Similarly
198	# an outgoing packet originated from it before being translated would
199	# match the `deny' rule below.
200	case ${natd_enable} in
201	[Yy][Ee][Ss])
202		if [ -n "${natd_interface}" ]; then
203			${fwcmd} add divert natd all from any to any via ${natd_interface}
204		fi
205		;;
206	esac
207
208	# Stop RFC1918 nets on the outside interface
209	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
210	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
211	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
212
213	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
214	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
215	# on the outside interface
216	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
217	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
218	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
219	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
220	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
221
222	# Allow TCP through if setup succeeded
223	${fwcmd} add pass tcp from any to any established
224
225	# Allow IP fragments to pass through
226	${fwcmd} add pass all from any to any frag
227
228	# Allow setup of incoming email
229	${fwcmd} add pass tcp from any to ${oip} 25 setup
230
231	# Allow access to our DNS
232	${fwcmd} add pass tcp from any to ${oip} 53 setup
233	${fwcmd} add pass udp from any to ${oip} 53
234	${fwcmd} add pass udp from ${oip} 53 to any
235
236	# Allow access to our WWW
237	${fwcmd} add pass tcp from any to ${oip} 80 setup
238
239	# Reject&Log all setup of incoming connections from the outside
240	${fwcmd} add deny log tcp from any to any in via ${oif} setup
241
242	# Allow setup of any other TCP connection
243	${fwcmd} add pass tcp from any to any setup
244
245	# Allow DNS queries out in the world
246	${fwcmd} add pass udp from any 53 to ${oip}
247	${fwcmd} add pass udp from ${oip} to any 53
248
249	# Allow NTP queries out in the world
250	${fwcmd} add pass udp from any 123 to ${oip}
251	${fwcmd} add pass udp from ${oip} to any 123
252
253	# Everything else is denied by default, unless the
254	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
255	# config file.
256	;;
257
258[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
259	;;
260*)
261	if [ -r "${firewall_type}" ]; then
262		${fwcmd} ${firewall_flags} ${firewall_type}
263	fi
264	;;
265esac
266