rc.firewall revision 51805
127897Sfsmp############
2208291Suqs# Setup system for firewall service.
327897Sfsmp# $FreeBSD: head/etc/rc.firewall 51805 1999-09-30 04:55:23Z mpp $
427897Sfsmp
527897Sfsmp# Suck in the configuration variables.
627897Sfsmpif [ -r /etc/defaults/rc.conf ]; then
727897Sfsmp	. /etc/defaults/rc.conf
827897Sfsmpelif [ -r /etc/rc.conf ]; then
927897Sfsmp	. /etc/rc.conf
1027897Sfsmpfi
1127897Sfsmp
1227897Sfsmp############
1327897Sfsmp# Define the firewall type in /etc/rc.conf.  Valid values are:
1427897Sfsmp#   open     - will allow anyone in
1527897Sfsmp#   client   - will try to protect just this machine
1627897Sfsmp#   simple   - will try to protect a whole network
1727897Sfsmp#   closed   - totally disables IP services except via lo0 interface
1827897Sfsmp#   UNKNOWN  - disables the loading of firewall rules.
1927897Sfsmp#   filename - will load the rules in the given filename (full path required)
2027897Sfsmp#
2127897Sfsmp# For ``client'' and ``simple'' the entries below should be customized
2252676Sphantom# appropriately.
2348795Snik
2450476Speter############
2548795Snik#
26191953Sivoras# If you don't know enough about packet filtering, we suggest that you
2727897Sfsmp# take time to read this book:
2879538Sru#
2927897Sfsmp#	Building Internet Firewalls
3027897Sfsmp#	Brent Chapman and Elizabeth Zwicky
31138999Sjkoshy#
3227897Sfsmp#	O'Reilly & Associates, Inc
33107664Srwatson#	ISBN 1-56592-124-0
3427897Sfsmp#	http://www.ora.com/
3527897Sfsmp#
3679727Sschweikh# For a more advanced treatment of Internet Security read:
37138999Sjkoshy#
3827897Sfsmp#	Firewalls & Internet Security
39113820Sjhb#	Repelling the wily hacker
40107700Sru#	William R. Cheswick, Steven M. Bellowin
41107700Sru#
42158621Swilko#	Addison-Wesley
43107664Srwatson#	ISBN 0-201-6337-4
44107664Srwatson#	http://www.awl.com/
45127378Sjhb#
46127378Sjhb
47127378Sjhbif [ -n "${1}" ]; then
48107663Srwatson	firewall_type="${1}"
4927897Sfsmpfi
5027897Sfsmp
5127897Sfsmp############
52107664Srwatson# Set quiet mode if requested
53107664Srwatson#
54107664Srwatsoncase ${firewall_quiet} in
55127378Sjhb[Yy][Ee][Ss])
56107664Srwatson	fwcmd="/sbin/ipfw -q"
57107663Srwatson	;;
58107663Srwatson*)
59113820Sjhb	fwcmd="/sbin/ipfw"
60138999Sjkoshy	;;
61138999Sjkoshyesac
62138999Sjkoshy
63138999Sjkoshy############
64113820Sjhb# Flush out the list before we begin.
65138999Sjkoshy#
66224528Savg${fwcmd} -f flush
67224528Savg
68224528Savg############
69224528Savg# These rules are required for using natd.  All packets are passed to
70138999Sjkoshy# natd before they encounter your remaining rules.  The firewall rules
71138999Sjkoshy# will then be run again on each packet after translation by natd,
72191953Sivoras# minus any divert rules (see natd(8)).
73191953Sivoras#
74191953Sivorascase ${natd_enable} in
75191953Sivoras[Yy][Ee][Ss])
76191953Sivoras	if [ -n "${natd_interface}" ]; then
77191953Sivoras	      ${fwcmd} add divert natd all from any to any via ${natd_interface}
78191953Sivoras	fi
79191953Sivoras	;;
80191953Sivorasesac
81191953Sivoras
82191953Sivoras############
83191953Sivoras# If you just configured ipfw in the kernel as a tool to solve network
84191953Sivoras# problems or you just want to disallow some particular kinds of traffic
85191953Sivoras# then you will want to change the default policy to open.  You can also
86191953Sivoras# do this as your only action by setting the firewall_type to ``open''.
87191953Sivoras#
88191953Sivoras# ${fwcmd} add 65000 pass all from any to any
89191953Sivoras
90191953Sivoras############
91191953Sivoras# Only in rare cases do you want to change these rules
92191953Sivoras#
93191953Sivoras${fwcmd} add 100 pass all from any to any via lo0
94191953Sivoras${fwcmd} add 200 deny all from any to 127.0.0.0/8
95191953Sivoras
96191953Sivoras
97191953Sivoras# Prototype setups.
98191953Sivoras#
99191953Sivorascase ${firewall_type} in
100191953Sivoras[Oo][Pp][Ee][Nn])
101191953Sivoras	${fwcmd} add 65000 pass all from any to any
102191953Sivoras	;;
103191953Sivoras[Cc][Ll][Ii][Ee][Nn][Tt])
104191953Sivoras
105191953Sivoras	############
106191953Sivoras	# This is a prototype setup that will protect your system somewhat
107191953Sivoras	# against people from outside your own network.
108191953Sivoras	############
109191953Sivoras
110191953Sivoras	# set these to your network and netmask and ip
111191953Sivoras	net="192.168.4.0"
112191953Sivoras	mask="255.255.255.0"
113191953Sivoras	ip="192.168.4.17"
114191953Sivoras
115191953Sivoras	# Allow any traffic to or from my own net.
116191953Sivoras	${fwcmd} add pass all from ${ip} to ${net}:${mask}
117191953Sivoras	${fwcmd} add pass all from ${net}:${mask} to ${ip}
118191953Sivoras
119191953Sivoras	# Allow TCP through if setup succeeded
120191953Sivoras	${fwcmd} add pass tcp from any to any established
121138999Sjkoshy
122191953Sivoras	# Allow setup of incoming email
123225902Sgabor	${fwcmd} add pass tcp from any to ${ip} 25 setup
124130582Sru
125224528Savg	# Allow setup of outgoing TCP connections only
126224528Savg	${fwcmd} add pass tcp from ${ip} to any setup
12727897Sfsmp
128225902Sgabor	# Disallow setup of all other TCP connections
12929966Swosch	${fwcmd} add deny tcp from any to any setup
130225902Sgabor
131225902Sgabor	# Allow DNS queries out in the world
132224528Savg	${fwcmd} add pass udp from any 53 to ${ip}
133138999Sjkoshy	${fwcmd} add pass udp from ${ip} to any 53
134107663Srwatson
135107663Srwatson	# Allow NTP queries out in the world
136107663Srwatson	${fwcmd} add pass udp from any 123 to ${ip}
137107663Srwatson	${fwcmd} add pass udp from ${ip} to any 123
138225902Sgabor
139107663Srwatson	# Everything else is denied by default, unless the
140225902Sgabor	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
14127897Sfsmp	# config file.
14227897Sfsmp	;;
14327897Sfsmp
144117011Sru[Ss][Ii][Mm][Pp][Ll][Ee])
145117011Sru
14627897Sfsmp	############
147117011Sru	# This is a prototype setup for a simple firewall.  Configure this
148117011Sru	# machine as a named server and ntp server, and point all the machines
14927897Sfsmp	# on the inside at this machine for those services.
150107663Srwatson	############
151107663Srwatson
152107663Srwatson	# set these to your outside interface network and netmask and ip
153107663Srwatson	oif="ed0"
154107663Srwatson	onet="192.168.4.0"
155107663Srwatson	omask="255.255.255.0"
156107663Srwatson	oip="192.168.4.17"
157107700Sru
158107700Sru	# set these to your inside interface network and netmask and ip
159113820Sjhb	iif="ed1"
160113820Sjhb	inet="192.168.3.0"
161162404Sru	imask="255.255.255.0"
16227897Sfsmp	iip="192.168.3.17"
16334504Scharnier
164	# Stop spoofing
165	${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
166	${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
167
168	# Stop RFC1918 nets on the outside interface
169	${fwcmd} add deny all from 192.168.0.0:255.255.0.0 to any via ${oif}
170	${fwcmd} add deny all from any to 192.168.0.0:255.255.0.0 via ${oif}
171	${fwcmd} add deny all from 172.16.0.0:255.240.0.0 to any via ${oif}
172	${fwcmd} add deny all from any to 172.16.0.0:255.240.0.0 via ${oif}
173	${fwcmd} add deny all from 10.0.0.0:255.0.0.0 to any via ${oif}
174	${fwcmd} add deny all from any to 10.0.0.0:255.0.0.0 via ${oif}
175
176	# Allow TCP through if setup succeeded
177	${fwcmd} add pass tcp from any to any established
178
179	# Allow setup of incoming email
180	${fwcmd} add pass tcp from any to ${oip} 25 setup
181
182	# Allow access to our DNS
183	${fwcmd} add pass tcp from any to ${oip} 53 setup
184
185	# Allow access to our WWW
186	${fwcmd} add pass tcp from any to ${oip} 80 setup
187
188	# Reject&Log all setup of incoming connections from the outside
189	${fwcmd} add deny log tcp from any to any in via ${oif} setup
190
191	# Allow setup of any other TCP connection
192	${fwcmd} add pass tcp from any to any setup
193
194	# Allow DNS queries out in the world
195	${fwcmd} add pass udp from any 53 to ${oip}
196	${fwcmd} add pass udp from ${oip} to any 53
197
198	# Allow NTP queries out in the world
199	${fwcmd} add pass udp from any 123 to ${oip}
200	${fwcmd} add pass udp from ${oip} to any 123
201
202	# Everything else is denied by default, unless the
203	# IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
204	# config file.
205	;;
206
207[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
208	;;
209*)
210	if [ -r "${firewall_type}" ]; then
211		${fwcmd} ${firewall_type}
212	fi
213	;;
214esac
215