1207842Smm#!/bin/sh -
2207842Smm# Copyright (c) 1996  Poul-Henning Kamp
3208099Sdelphij# All rights reserved.
4207842Smm#
5207842Smm# Redistribution and use in source and binary forms, with or without
6207842Smm# modification, are permitted provided that the following conditions
7207842Smm# are met:
8207842Smm# 1. Redistributions of source code must retain the above copyright
9207842Smm#    notice, this list of conditions and the following disclaimer.
10207842Smm# 2. Redistributions in binary form must reproduce the above copyright
11207842Smm#    notice, this list of conditions and the following disclaimer in the
12207842Smm#    documentation and/or other materials provided with the distribution.
13207842Smm#
14207842Smm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15207842Smm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16207842Smm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17207842Smm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18207842Smm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19207842Smm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20207842Smm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21207844Smm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22207844Smm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23207842Smm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24207842Smm# SUCH DAMAGE.
25207842Smm#
26207842Smm# $FreeBSD: releng/10.3/tools/tools/tinybsd/conf/wireless/etc/rc.firewall 162485 2006-09-20 22:24:20Z julian $
27207842Smm#
28207842Smm
29207842Smm#
30207842Smm# Setup system for firewall service.
31207842Smm#
32207842Smm
33207842Smm# Suck in the configuration variables.
34207842Smmif [ -z "${source_rc_confs_defined}" ]; then
35207842Smm	if [ -r /etc/defaults/rc.conf ]; then
36207842Smm		. /etc/defaults/rc.conf
37207842Smm		source_rc_confs
38207842Smm	elif [ -r /etc/rc.conf ]; then
39207842Smm		. /etc/rc.conf
40207842Smm	fi
41207842Smmfi
42207842Smm
43207842Smm############
44207842Smm# Define the firewall type in /etc/rc.conf.  Valid values are:
45207842Smm#   open     - will allow anyone in
46207842Smm#   client   - will try to protect just this machine
47207842Smm#   simple   - will try to protect a whole network
48207842Smm#   closed   - totally disables IP services except via lo0 interface
49207842Smm#   UNKNOWN  - disables the loading of firewall rules.
50207842Smm#   filename - will load the rules in the given filename (full path required)
51207842Smm#
52207842Smm# For ``client'' and ``simple'' the entries below should be customized
53207842Smm# appropriately.
54207842Smm
55207842Smm############
56207842Smm#
57207842Smm# If you don't know enough about packet filtering, we suggest that you
58207842Smm# take time to read this book:
59207842Smm#
60207842Smm#	Building Internet Firewalls, 2nd Edition
61207842Smm#	Brent Chapman and Elizabeth Zwicky
62207842Smm#
63207842Smm#	O'Reilly & Associates, Inc
64207842Smm#	ISBN 1-56592-871-7
65207842Smm#	http://www.ora.com/
66207842Smm#	http://www.oreilly.com/catalog/fire2/
67207842Smm#
68207842Smm# For a more advanced treatment of Internet Security read:
69207842Smm#
70207842Smm#	Firewalls & Internet Security
71207842Smm#	Repelling the wily hacker
72207842Smm#	William R. Cheswick, Steven M. Bellowin
73207842Smm#
74207842Smm#	Addison-Wesley
75207842Smm#	ISBN 0-201-63357-4
76207842Smm#	http://www.awl.com/
77207842Smm#	http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
78207842Smm#
79207842Smm
80207842Smmsetup_loopback () {
81218723Sdim	############
82207842Smm	# Only in rare cases do you want to change these rules
83207842Smm	#
84207842Smm	${fwcmd} add 100 pass all from any to any via lo0
85207842Smm	${fwcmd} add 200 deny all from any to 127.0.0.0/8
86207842Smm	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
87207842Smm}
88207842Smm
89207842Smmif [ -n "${1}" ]; then
90207842Smm	firewall_type="${1}"
91207842Smmfi
92207842Smm
93207842Smm############
94207842Smm# Set quiet mode if requested
95207842Smm#
96207842Smmcase ${firewall_quiet} in
97207842Smm[Yy][Ee][Ss])
98207842Smm	fwcmd="/sbin/ipfw -q"
99207842Smm	;;
100207842Smm*)
101207842Smm	fwcmd="/sbin/ipfw"
102207842Smm	;;
103207842Smmesac
104207842Smm
105207842Smm############
106207842Smm# Flush out the list before we begin.
107207842Smm#
108207842Smm${fwcmd} -f flush
109207842Smm
110207842Smm############
111207842Smm# Network Address Translation.  All packets are passed to natd(8)
112207842Smm# before they encounter your remaining rules.  The firewall rules
113207842Smm# will then be run again on each packet after translation by natd
114207842Smm# starting at the rule number following the divert rule.
115207842Smm#
116207842Smm# For ``simple'' firewall type the divert rule should be put to a
117207842Smm# different place to not interfere with address-checking rules.
118207842Smm#
119207842Smmcase ${firewall_type} in
120207842Smm[Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
121207842Smm	case ${natd_enable} in
122207842Smm	[Yy][Ee][Ss])
123207842Smm		if [ -n "${natd_interface}" ]; then
124208099Sdelphij			${fwcmd} add 50 divert natd all from any to any via ${natd_interface}
125207842Smm		fi
126207842Smm		;;
127207842Smm	esac
128207842Smmesac
129207842Smm
130207842Smm############
131207842Smm# If you just configured ipfw in the kernel as a tool to solve network
132207842Smm# problems or you just want to disallow some particular kinds of traffic
133207842Smm# then you will want to change the default policy to open.  You can also
134207842Smm# do this as your only action by setting the firewall_type to ``open''.
135207842Smm#
136208099Sdelphij# ${fwcmd} add 65000 pass all from any to any
137208099Sdelphij
138208099Sdelphij
139208099Sdelphij# Prototype setups.
140207842Smm#
141case ${firewall_type} in
142[Oo][Pp][Ee][Nn])
143	setup_loopback
144	${fwcmd} add 65000 pass all from any to any
145	;;
146
147[Cc][Ll][Ii][Ee][Nn][Tt])
148	############
149	# This is a prototype setup that will protect your system somewhat
150	# against people from outside your own network.
151	############
152
153	# set these to your network and netmask and ip
154	net="192.0.2.0"
155	mask="255.255.255.0"
156	ip="192.0.2.1"
157
158	setup_loopback
159
160	# Allow any traffic to or from my own net.
161	${fwcmd} add pass all from ${ip} to ${net}:${mask}
162	${fwcmd} add pass all from ${net}:${mask} to ${ip}
163
164	# Allow TCP through if setup succeeded
165	${fwcmd} add pass tcp from any to any established
166
167	# Allow IP fragments to pass through
168	${fwcmd} add pass all from any to any frag
169
170	# Allow setup of incoming email
171	${fwcmd} add pass tcp from any to ${ip} 25 setup
172
173	# Allow setup of outgoing TCP connections only
174	${fwcmd} add pass tcp from ${ip} to any setup
175
176	# Disallow setup of all other TCP connections
177	${fwcmd} add deny tcp from any to any setup
178
179	# Allow DNS queries out in the world
180	${fwcmd} add pass udp from ${ip} to any 53 keep-state
181
182	# Allow NTP queries out in the world
183	${fwcmd} add pass udp from ${ip} to any 123 keep-state
184
185	# Everything else is denied by default, unless the
186	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
187	# config file.
188	;;
189
190[Ss][Ii][Mm][Pp][Ll][Ee])
191	############
192	# This is a prototype setup for a simple firewall.  Configure this
193	# machine as a DNS and NTP server, and point all the machines
194	# on the inside at this machine for those services.
195	############
196
197	# set these to your outside interface network and netmask and ip
198	oif="ed0"
199	onet="192.0.2.0"
200	omask="255.255.255.240"
201	oip="192.0.2.1"
202
203	# set these to your inside interface network and netmask and ip
204	iif="ed1"
205	inet="192.0.2.16"
206	imask="255.255.255.240"
207	iip="192.0.2.17"
208
209	setup_loopback
210
211	# Stop spoofing
212	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
213	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
214
215	# Stop RFC1918 nets on the outside interface
216	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
217	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
218	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
219
220	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
221	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
222	# on the outside interface
223	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
224	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
225	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
226	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
227	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
228
229	# Network Address Translation.  This rule is placed here deliberately
230	# so that it does not interfere with the surrounding address-checking
231	# rules.  If for example one of your internal LAN machines had its IP
232	# address set to 192.0.2.1 then an incoming packet for it after being
233	# translated by natd(8) would match the `deny' rule above.  Similarly
234	# an outgoing packet originated from it before being translated would
235	# match the `deny' rule below.
236	case ${natd_enable} in
237	[Yy][Ee][Ss])
238		if [ -n "${natd_interface}" ]; then
239			${fwcmd} add divert natd all from any to any via ${natd_interface}
240		fi
241		;;
242	esac
243
244	# Stop RFC1918 nets on the outside interface
245	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
246	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
247	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
248
249	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
250	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
251	# on the outside interface
252	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
253	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
254	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
255	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
256	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
257
258	# Allow TCP through if setup succeeded
259	${fwcmd} add pass tcp from any to any established
260
261	# Allow IP fragments to pass through
262	${fwcmd} add pass all from any to any frag
263
264	# Allow setup of incoming email
265	${fwcmd} add pass tcp from any to ${oip} 25 setup
266
267	# Allow access to our DNS
268	${fwcmd} add pass tcp from any to ${oip} 53 setup
269	${fwcmd} add pass udp from any to ${oip} 53
270	${fwcmd} add pass udp from ${oip} 53 to any
271
272	# Allow access to our WWW
273	${fwcmd} add pass tcp from any to ${oip} 80 setup
274
275	# Reject&Log all setup of incoming connections from the outside
276	${fwcmd} add deny log tcp from any to any in via ${oif} setup
277
278	# Allow setup of any other TCP connection
279	${fwcmd} add pass tcp from any to any setup
280
281	# Allow DNS queries out in the world
282	${fwcmd} add pass udp from ${oip} to any 53 keep-state
283
284	# Allow NTP queries out in the world
285	${fwcmd} add pass udp from ${oip} to any 123 keep-state
286
287	# Everything else is denied by default, unless the
288	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
289	# config file.
290	;;
291
292[Cc][Ll][Oo][Ss][Ee][Dd])
293	setup_loopback
294	;;
295[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
296	;;
297*)
298	if [ -r "${firewall_type}" ]; then
299		${fwcmd} ${firewall_flags} ${firewall_type}
300	fi
301	;;
302esac
303