xref: /dflybsd-src/etc/rc.firewall (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino#!/bin/sh
2*86d7f5d3SJohn Marino#
3*86d7f5d3SJohn Marino# Copyright (c) 2004 The DragonFly Project.  All rights reserved.
4*86d7f5d3SJohn Marino#
5*86d7f5d3SJohn Marino# This code is derived from software contributed to The DragonFly Project
6*86d7f5d3SJohn Marino# by Andreas Hauser <andy-dragonfly@splashground.de>
7*86d7f5d3SJohn Marino#
8*86d7f5d3SJohn Marino# Redistribution and use in source and binary forms, with or without
9*86d7f5d3SJohn Marino# modification, are permitted provided that the following conditions
10*86d7f5d3SJohn Marino# are met:
11*86d7f5d3SJohn Marino#
12*86d7f5d3SJohn Marino# 1. Redistributions of source code must retain the above copyright
13*86d7f5d3SJohn Marino#    notice, this list of conditions and the following disclaimer.
14*86d7f5d3SJohn Marino# 2. Redistributions in binary form must reproduce the above copyright
15*86d7f5d3SJohn Marino#    notice, this list of conditions and the following disclaimer in
16*86d7f5d3SJohn Marino#    the documentation and/or other materials provided with the
17*86d7f5d3SJohn Marino#    distribution.
18*86d7f5d3SJohn Marino# 3. Neither the name of The DragonFly Project nor the names of its
19*86d7f5d3SJohn Marino#    contributors may be used to endorse or promote products derived
20*86d7f5d3SJohn Marino#    from this software without specific, prior written permission.
21*86d7f5d3SJohn Marino#
22*86d7f5d3SJohn Marino# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*86d7f5d3SJohn Marino# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*86d7f5d3SJohn Marino# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25*86d7f5d3SJohn Marino# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26*86d7f5d3SJohn Marino# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27*86d7f5d3SJohn Marino# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28*86d7f5d3SJohn Marino# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29*86d7f5d3SJohn Marino# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30*86d7f5d3SJohn Marino# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31*86d7f5d3SJohn Marino# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32*86d7f5d3SJohn Marino# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*86d7f5d3SJohn Marino# SUCH DAMAGE.
34*86d7f5d3SJohn Marino#
35*86d7f5d3SJohn Marino# $DragonFly: src/etc/rc.firewall,v 1.6 2007/06/02 09:16:49 swildner Exp $
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino# A simple packetfilter configurable via /etc/rc.conf
38*86d7f5d3SJohn Marino#
39*86d7f5d3SJohn Marino# Variables in rc.conf:
40*86d7f5d3SJohn Marino#
41*86d7f5d3SJohn Marino# firewall_type
42*86d7f5d3SJohn Marino#     UNKNOWN  - disables the loading of firewall rules.
43*86d7f5d3SJohn Marino#     open     - will allow anyone in
44*86d7f5d3SJohn Marino#     client   - enables the packetfilter
45*86d7f5d3SJohn Marino#     simple   - enables the packetfilter
46*86d7f5d3SJohn Marino#     closed   - totally disables IP services except via lo0 interface
47*86d7f5d3SJohn Marino#     filename - will load the rules in the given filename (full path required)
48*86d7f5d3SJohn Marino#
49*86d7f5d3SJohn Marino#  firewall_trusted_nets
50*86d7f5d3SJohn Marino#  firewall_trusted_interfaces
51*86d7f5d3SJohn Marino#  firewall_allowed_icmp_types
52*86d7f5d3SJohn Marino#  firewall_open_tcp_ports
53*86d7f5d3SJohn Marino#  firewall_open_udp_ports
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marinoif [ -z "${source_rc_confs_defined}" ]; then
56*86d7f5d3SJohn Marino        if [ -r /etc/defaults/rc.conf ]; then
57*86d7f5d3SJohn Marino                . /etc/defaults/rc.conf
58*86d7f5d3SJohn Marino                source_rc_confs
59*86d7f5d3SJohn Marino        elif [ -r /etc/rc.conf ]; then
60*86d7f5d3SJohn Marino                . /etc/rc.conf
61*86d7f5d3SJohn Marino        fi
62*86d7f5d3SJohn Marinofi
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marinocase ${firewall_quiet} in
65*86d7f5d3SJohn Marino[Yy][Ee][Ss])
66*86d7f5d3SJohn Marino        fwcmd="/sbin/ipfw -q"
67*86d7f5d3SJohn Marino        ;;
68*86d7f5d3SJohn Marino*)
69*86d7f5d3SJohn Marino        fwcmd="/sbin/ipfw"
70*86d7f5d3SJohn Marino        ;;
71*86d7f5d3SJohn Marinoesac
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marinocase ${firewall_logging} in
74*86d7f5d3SJohn Marino[Yy][Ee][Ss])
75*86d7f5d3SJohn Marino        log="log"
76*86d7f5d3SJohn Marino        ;;
77*86d7f5d3SJohn Marino*)
78*86d7f5d3SJohn Marino        log=""
79*86d7f5d3SJohn Marino        ;;
80*86d7f5d3SJohn Marinoesac
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino# we handle start, stop, firewall_type and nothing as argument
83*86d7f5d3SJohn Marinoif [ -n "$1" ]; then
84*86d7f5d3SJohn Marino    case $1 in
85*86d7f5d3SJohn Marino        start)
86*86d7f5d3SJohn Marino        ;;
87*86d7f5d3SJohn Marino        stop)
88*86d7f5d3SJohn Marino        firewall_type="open"
89*86d7f5d3SJohn Marino        ;;
90*86d7f5d3SJohn Marino        *)
91*86d7f5d3SJohn Marino        firewall_type="$1"
92*86d7f5d3SJohn Marino        ;;
93*86d7f5d3SJohn Marino    esac
94*86d7f5d3SJohn Marinofi
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marinodivert_nat() {
97*86d7f5d3SJohn Marino    case ${natd_enable} in
98*86d7f5d3SJohn Marino	[Yy][Ee][Ss])
99*86d7f5d3SJohn Marino        if [ -n "${natd_interface}" ]; then
100*86d7f5d3SJohn Marino                ${fwcmd} add divert natd all from any to any via ${natd_interface}
101*86d7f5d3SJohn Marino        fi
102*86d7f5d3SJohn Marino    esac
103*86d7f5d3SJohn Marino}
104*86d7f5d3SJohn Marino
105*86d7f5d3SJohn Marinoallow_loopback() {
106*86d7f5d3SJohn Marino    ${fwcmd} add pass all from any to any via lo0
107*86d7f5d3SJohn Marino    ${fwcmd} add deny ${log} all from any to 127.0.0.0/8
108*86d7f5d3SJohn Marino    ${fwcmd} add deny ${log} ip from 127.0.0.0/8 to any
109*86d7f5d3SJohn Marino}
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marinodeny_spoof() {
112*86d7f5d3SJohn Marino    # XXX we don't have verrevpath yet
113*86d7f5d3SJohn Marino    # ${fwcmd} add deny ${log} ip from any to any not verrevpath in
114*86d7f5d3SJohn Marino    echo no verrevpath yet, so no anti-spoof
115*86d7f5d3SJohn Marino}
116*86d7f5d3SJohn Marino
117*86d7f5d3SJohn Marinoallow_icmp_types() {
118*86d7f5d3SJohn Marino    for type in $*; do
119*86d7f5d3SJohn Marino        ${fwcmd} add allow icmp from any to any icmptypes ${type}
120*86d7f5d3SJohn Marino    done
121*86d7f5d3SJohn Marino}
122*86d7f5d3SJohn Marino
123*86d7f5d3SJohn Marinoallow_trusted_nets() {
124*86d7f5d3SJohn Marino    for net in $*; do
125*86d7f5d3SJohn Marino        ${fwcmd} add pass all from me to ${net}
126*86d7f5d3SJohn Marino        ${fwcmd} add pass all from ${net} to me
127*86d7f5d3SJohn Marino    done
128*86d7f5d3SJohn Marino}
129*86d7f5d3SJohn Marino
130*86d7f5d3SJohn Marinoallow_trusted_interfaces() {
131*86d7f5d3SJohn Marino    for interface in $*; do
132*86d7f5d3SJohn Marino        ${fwcmd} add pass all from any to any via ${interface}
133*86d7f5d3SJohn Marino    done
134*86d7f5d3SJohn Marino}
135*86d7f5d3SJohn Marino
136*86d7f5d3SJohn Marinoallow_connections() {
137*86d7f5d3SJohn Marino    ${fwcmd} add pass tcp from any to any established
138*86d7f5d3SJohn Marino    ${fwcmd} add pass all from any to any frag
139*86d7f5d3SJohn Marino    ${fwcmd} add pass tcp from me to any setup
140*86d7f5d3SJohn Marino    ${fwcmd} add pass udp from me to any keep-state
141*86d7f5d3SJohn Marino}
142*86d7f5d3SJohn Marino
143*86d7f5d3SJohn Marinoopen_tcp_ports() {
144*86d7f5d3SJohn Marino    for port in $*; do
145*86d7f5d3SJohn Marino        ${fwcmd} add pass tcp from any to me ${port} setup
146*86d7f5d3SJohn Marino    done
147*86d7f5d3SJohn Marino}
148*86d7f5d3SJohn Marino
149*86d7f5d3SJohn Marinoopen_udp_ports() {
150*86d7f5d3SJohn Marino    for port in $*; do
151*86d7f5d3SJohn Marino        ${fwcmd} add pass udp from any to me ${port}
152*86d7f5d3SJohn Marino        ${fwcmd} add pass udp from me ${port} to any
153*86d7f5d3SJohn Marino    done
154*86d7f5d3SJohn Marino}
155*86d7f5d3SJohn Marino
156*86d7f5d3SJohn Marinodeny_not_routed_nets()
157*86d7f5d3SJohn Marino{
158*86d7f5d3SJohn Marino    # These nets should not be routed
159*86d7f5d3SJohn Marino    nets="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 0.0.0.0/8 \
160*86d7f5d3SJohn Marino        169.254.0.0/16 192.0.2.0/24 224.0.0.0/4 240.0.0.0/4"
161*86d7f5d3SJohn Marino    for net in ${nets} ; do
162*86d7f5d3SJohn Marino        ${fwcmd} add deny ${log} all from $net to any
163*86d7f5d3SJohn Marino    done
164*86d7f5d3SJohn Marino}
165*86d7f5d3SJohn Marino
166*86d7f5d3SJohn Marinodeny_rest() {
167*86d7f5d3SJohn Marino    ${fwcmd} add 65000 deny ${log} all from any to any
168*86d7f5d3SJohn Marino}
169*86d7f5d3SJohn Marino
170*86d7f5d3SJohn Marinoallow_rest() {
171*86d7f5d3SJohn Marino    ${fwcmd} add 65000 pass all from any to any
172*86d7f5d3SJohn Marino}
173*86d7f5d3SJohn Marino
174*86d7f5d3SJohn Marino
175*86d7f5d3SJohn Marino${fwcmd} -f flush
176*86d7f5d3SJohn Marino
177*86d7f5d3SJohn Marinocase ${firewall_type} in
178*86d7f5d3SJohn Marino    [Oo][Pp][Ee][Nn])
179*86d7f5d3SJohn Marino        allow_loopback
180*86d7f5d3SJohn Marino        deny_spoof
181*86d7f5d3SJohn Marino        divert_nat
182*86d7f5d3SJohn Marino        allow_rest
183*86d7f5d3SJohn Marino    ;;
184*86d7f5d3SJohn Marino
185*86d7f5d3SJohn Marino    # historical names
186*86d7f5d3SJohn Marino    [Cc][Ll][Ii][Ee][Nn][Tt]|[Ss][Ii][Mm][Pp][Ll][Ee]|"")
187*86d7f5d3SJohn Marino        allow_loopback
188*86d7f5d3SJohn Marino        deny_spoof
189*86d7f5d3SJohn Marino        divert_nat
190*86d7f5d3SJohn Marino        allow_trusted_nets ${firewall_trusted_nets}
191*86d7f5d3SJohn Marino        allow_trusted_interfaces ${firewall_trusted_interfaces}
192*86d7f5d3SJohn Marino        allow_connections
193*86d7f5d3SJohn Marino        allow_icmp_types ${firewall_allowed_icmp_types}
194*86d7f5d3SJohn Marino        deny_not_routed_nets
195*86d7f5d3SJohn Marino        open_tcp_ports ${firewall_open_tcp_ports}
196*86d7f5d3SJohn Marino        open_udp_ports ${firewall_open_udp_ports}
197*86d7f5d3SJohn Marino        deny_rest
198*86d7f5d3SJohn Marino    ;;
199*86d7f5d3SJohn Marino
200*86d7f5d3SJohn Marino    [Cc][Ll][Oo][Ss][Ee][Dd])
201*86d7f5d3SJohn Marino        allow_loopback
202*86d7f5d3SJohn Marino        deny_rest
203*86d7f5d3SJohn Marino    ;;
204*86d7f5d3SJohn Marino
205*86d7f5d3SJohn Marino    [Uu][Nn][Kk][Nn][Oo][Ww][Nn])
206*86d7f5d3SJohn Marino    ;;
207*86d7f5d3SJohn Marino
208*86d7f5d3SJohn Marino    *)
209*86d7f5d3SJohn Marino        if [ -r "${firewall_type}" ]; then
210*86d7f5d3SJohn Marino            ${fwcmd} ${firewall_flags} ${firewall_type}
211*86d7f5d3SJohn Marino        fi
212*86d7f5d3SJohn Marino    ;;
213*86d7f5d3SJohn Marinoesac
214