xref: /dflybsd-src/etc/rc.firewall (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino#!/bin/sh
286d7f5d3SJohn Marino#
386d7f5d3SJohn Marino# Copyright (c) 2004 The DragonFly Project.  All rights reserved.
486d7f5d3SJohn Marino#
586d7f5d3SJohn Marino# This code is derived from software contributed to The DragonFly Project
686d7f5d3SJohn Marino# by Andreas Hauser <andy-dragonfly@splashground.de>
786d7f5d3SJohn Marino#
886d7f5d3SJohn Marino# Redistribution and use in source and binary forms, with or without
986d7f5d3SJohn Marino# modification, are permitted provided that the following conditions
1086d7f5d3SJohn Marino# are met:
1186d7f5d3SJohn Marino#
1286d7f5d3SJohn Marino# 1. Redistributions of source code must retain the above copyright
1386d7f5d3SJohn Marino#    notice, this list of conditions and the following disclaimer.
1486d7f5d3SJohn Marino# 2. Redistributions in binary form must reproduce the above copyright
1586d7f5d3SJohn Marino#    notice, this list of conditions and the following disclaimer in
1686d7f5d3SJohn Marino#    the documentation and/or other materials provided with the
1786d7f5d3SJohn Marino#    distribution.
1886d7f5d3SJohn Marino# 3. Neither the name of The DragonFly Project nor the names of its
1986d7f5d3SJohn Marino#    contributors may be used to endorse or promote products derived
2086d7f5d3SJohn Marino#    from this software without specific, prior written permission.
2186d7f5d3SJohn Marino#
2286d7f5d3SJohn Marino# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2386d7f5d3SJohn Marino# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2486d7f5d3SJohn Marino# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2586d7f5d3SJohn Marino# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2686d7f5d3SJohn Marino# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2786d7f5d3SJohn Marino# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2886d7f5d3SJohn Marino# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2986d7f5d3SJohn Marino# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
3086d7f5d3SJohn Marino# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3186d7f5d3SJohn Marino# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3286d7f5d3SJohn Marino# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3386d7f5d3SJohn Marino# SUCH DAMAGE.
3486d7f5d3SJohn Marino#
3586d7f5d3SJohn Marino# $DragonFly: src/etc/rc.firewall,v 1.6 2007/06/02 09:16:49 swildner Exp $
3686d7f5d3SJohn Marino
3786d7f5d3SJohn Marino# A simple packetfilter configurable via /etc/rc.conf
3886d7f5d3SJohn Marino#
3986d7f5d3SJohn Marino# Variables in rc.conf:
4086d7f5d3SJohn Marino#
4186d7f5d3SJohn Marino# firewall_type
4286d7f5d3SJohn Marino#     UNKNOWN  - disables the loading of firewall rules.
4386d7f5d3SJohn Marino#     open     - will allow anyone in
4486d7f5d3SJohn Marino#     client   - enables the packetfilter
4586d7f5d3SJohn Marino#     simple   - enables the packetfilter
4686d7f5d3SJohn Marino#     closed   - totally disables IP services except via lo0 interface
4786d7f5d3SJohn Marino#     filename - will load the rules in the given filename (full path required)
4886d7f5d3SJohn Marino#
4986d7f5d3SJohn Marino#  firewall_trusted_nets
5086d7f5d3SJohn Marino#  firewall_trusted_interfaces
5186d7f5d3SJohn Marino#  firewall_allowed_icmp_types
5286d7f5d3SJohn Marino#  firewall_open_tcp_ports
5386d7f5d3SJohn Marino#  firewall_open_udp_ports
5486d7f5d3SJohn Marino
5586d7f5d3SJohn Marinoif [ -z "${source_rc_confs_defined}" ]; then
5686d7f5d3SJohn Marino        if [ -r /etc/defaults/rc.conf ]; then
5786d7f5d3SJohn Marino                . /etc/defaults/rc.conf
5886d7f5d3SJohn Marino                source_rc_confs
5986d7f5d3SJohn Marino        elif [ -r /etc/rc.conf ]; then
6086d7f5d3SJohn Marino                . /etc/rc.conf
6186d7f5d3SJohn Marino        fi
6286d7f5d3SJohn Marinofi
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marinocase ${firewall_quiet} in
6586d7f5d3SJohn Marino[Yy][Ee][Ss])
6686d7f5d3SJohn Marino        fwcmd="/sbin/ipfw -q"
6786d7f5d3SJohn Marino        ;;
6886d7f5d3SJohn Marino*)
6986d7f5d3SJohn Marino        fwcmd="/sbin/ipfw"
7086d7f5d3SJohn Marino        ;;
7186d7f5d3SJohn Marinoesac
7286d7f5d3SJohn Marino
7386d7f5d3SJohn Marinocase ${firewall_logging} in
7486d7f5d3SJohn Marino[Yy][Ee][Ss])
7586d7f5d3SJohn Marino        log="log"
7686d7f5d3SJohn Marino        ;;
7786d7f5d3SJohn Marino*)
7886d7f5d3SJohn Marino        log=""
7986d7f5d3SJohn Marino        ;;
8086d7f5d3SJohn Marinoesac
8186d7f5d3SJohn Marino
8286d7f5d3SJohn Marino# we handle start, stop, firewall_type and nothing as argument
8386d7f5d3SJohn Marinoif [ -n "$1" ]; then
8486d7f5d3SJohn Marino    case $1 in
8586d7f5d3SJohn Marino        start)
8686d7f5d3SJohn Marino        ;;
8786d7f5d3SJohn Marino        stop)
8886d7f5d3SJohn Marino        firewall_type="open"
8986d7f5d3SJohn Marino        ;;
9086d7f5d3SJohn Marino        *)
9186d7f5d3SJohn Marino        firewall_type="$1"
9286d7f5d3SJohn Marino        ;;
9386d7f5d3SJohn Marino    esac
9486d7f5d3SJohn Marinofi
9586d7f5d3SJohn Marino
9686d7f5d3SJohn Marinodivert_nat() {
9786d7f5d3SJohn Marino    case ${natd_enable} in
9886d7f5d3SJohn Marino	[Yy][Ee][Ss])
9986d7f5d3SJohn Marino        if [ -n "${natd_interface}" ]; then
10086d7f5d3SJohn Marino                ${fwcmd} add divert natd all from any to any via ${natd_interface}
10186d7f5d3SJohn Marino        fi
10286d7f5d3SJohn Marino    esac
10386d7f5d3SJohn Marino}
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marinoallow_loopback() {
10686d7f5d3SJohn Marino    ${fwcmd} add pass all from any to any via lo0
10786d7f5d3SJohn Marino    ${fwcmd} add deny ${log} all from any to 127.0.0.0/8
10886d7f5d3SJohn Marino    ${fwcmd} add deny ${log} ip from 127.0.0.0/8 to any
10986d7f5d3SJohn Marino}
11086d7f5d3SJohn Marino
11186d7f5d3SJohn Marinodeny_spoof() {
11286d7f5d3SJohn Marino    # XXX we don't have verrevpath yet
11386d7f5d3SJohn Marino    # ${fwcmd} add deny ${log} ip from any to any not verrevpath in
11486d7f5d3SJohn Marino    echo no verrevpath yet, so no anti-spoof
11586d7f5d3SJohn Marino}
11686d7f5d3SJohn Marino
11786d7f5d3SJohn Marinoallow_icmp_types() {
11886d7f5d3SJohn Marino    for type in $*; do
11986d7f5d3SJohn Marino        ${fwcmd} add allow icmp from any to any icmptypes ${type}
12086d7f5d3SJohn Marino    done
12186d7f5d3SJohn Marino}
12286d7f5d3SJohn Marino
12386d7f5d3SJohn Marinoallow_trusted_nets() {
12486d7f5d3SJohn Marino    for net in $*; do
12586d7f5d3SJohn Marino        ${fwcmd} add pass all from me to ${net}
12686d7f5d3SJohn Marino        ${fwcmd} add pass all from ${net} to me
12786d7f5d3SJohn Marino    done
12886d7f5d3SJohn Marino}
12986d7f5d3SJohn Marino
13086d7f5d3SJohn Marinoallow_trusted_interfaces() {
13186d7f5d3SJohn Marino    for interface in $*; do
13286d7f5d3SJohn Marino        ${fwcmd} add pass all from any to any via ${interface}
13386d7f5d3SJohn Marino    done
13486d7f5d3SJohn Marino}
13586d7f5d3SJohn Marino
13686d7f5d3SJohn Marinoallow_connections() {
13786d7f5d3SJohn Marino    ${fwcmd} add pass tcp from any to any established
13886d7f5d3SJohn Marino    ${fwcmd} add pass all from any to any frag
13986d7f5d3SJohn Marino    ${fwcmd} add pass tcp from me to any setup
14086d7f5d3SJohn Marino    ${fwcmd} add pass udp from me to any keep-state
14186d7f5d3SJohn Marino}
14286d7f5d3SJohn Marino
14386d7f5d3SJohn Marinoopen_tcp_ports() {
14486d7f5d3SJohn Marino    for port in $*; do
14586d7f5d3SJohn Marino        ${fwcmd} add pass tcp from any to me ${port} setup
14686d7f5d3SJohn Marino    done
14786d7f5d3SJohn Marino}
14886d7f5d3SJohn Marino
14986d7f5d3SJohn Marinoopen_udp_ports() {
15086d7f5d3SJohn Marino    for port in $*; do
15186d7f5d3SJohn Marino        ${fwcmd} add pass udp from any to me ${port}
15286d7f5d3SJohn Marino        ${fwcmd} add pass udp from me ${port} to any
15386d7f5d3SJohn Marino    done
15486d7f5d3SJohn Marino}
15586d7f5d3SJohn Marino
15686d7f5d3SJohn Marinodeny_not_routed_nets()
15786d7f5d3SJohn Marino{
15886d7f5d3SJohn Marino    # These nets should not be routed
15986d7f5d3SJohn Marino    nets="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 0.0.0.0/8 \
16086d7f5d3SJohn Marino        169.254.0.0/16 192.0.2.0/24 224.0.0.0/4 240.0.0.0/4"
16186d7f5d3SJohn Marino    for net in ${nets} ; do
16286d7f5d3SJohn Marino        ${fwcmd} add deny ${log} all from $net to any
16386d7f5d3SJohn Marino    done
16486d7f5d3SJohn Marino}
16586d7f5d3SJohn Marino
16686d7f5d3SJohn Marinodeny_rest() {
16786d7f5d3SJohn Marino    ${fwcmd} add 65000 deny ${log} all from any to any
16886d7f5d3SJohn Marino}
16986d7f5d3SJohn Marino
17086d7f5d3SJohn Marinoallow_rest() {
17186d7f5d3SJohn Marino    ${fwcmd} add 65000 pass all from any to any
17286d7f5d3SJohn Marino}
17386d7f5d3SJohn Marino
17486d7f5d3SJohn Marino
17586d7f5d3SJohn Marino${fwcmd} -f flush
17686d7f5d3SJohn Marino
17786d7f5d3SJohn Marinocase ${firewall_type} in
17886d7f5d3SJohn Marino    [Oo][Pp][Ee][Nn])
17986d7f5d3SJohn Marino        allow_loopback
18086d7f5d3SJohn Marino        deny_spoof
18186d7f5d3SJohn Marino        divert_nat
18286d7f5d3SJohn Marino        allow_rest
18386d7f5d3SJohn Marino    ;;
18486d7f5d3SJohn Marino
18586d7f5d3SJohn Marino    # historical names
18686d7f5d3SJohn Marino    [Cc][Ll][Ii][Ee][Nn][Tt]|[Ss][Ii][Mm][Pp][Ll][Ee]|"")
18786d7f5d3SJohn Marino        allow_loopback
18886d7f5d3SJohn Marino        deny_spoof
18986d7f5d3SJohn Marino        divert_nat
19086d7f5d3SJohn Marino        allow_trusted_nets ${firewall_trusted_nets}
19186d7f5d3SJohn Marino        allow_trusted_interfaces ${firewall_trusted_interfaces}
19286d7f5d3SJohn Marino        allow_connections
19386d7f5d3SJohn Marino        allow_icmp_types ${firewall_allowed_icmp_types}
19486d7f5d3SJohn Marino        deny_not_routed_nets
19586d7f5d3SJohn Marino        open_tcp_ports ${firewall_open_tcp_ports}
19686d7f5d3SJohn Marino        open_udp_ports ${firewall_open_udp_ports}
19786d7f5d3SJohn Marino        deny_rest
19886d7f5d3SJohn Marino    ;;
19986d7f5d3SJohn Marino
20086d7f5d3SJohn Marino    [Cc][Ll][Oo][Ss][Ee][Dd])
20186d7f5d3SJohn Marino        allow_loopback
20286d7f5d3SJohn Marino        deny_rest
20386d7f5d3SJohn Marino    ;;
20486d7f5d3SJohn Marino
20586d7f5d3SJohn Marino    [Uu][Nn][Kk][Nn][Oo][Ww][Nn])
20686d7f5d3SJohn Marino    ;;
20786d7f5d3SJohn Marino
20886d7f5d3SJohn Marino    *)
20986d7f5d3SJohn Marino        if [ -r "${firewall_type}" ]; then
21086d7f5d3SJohn Marino            ${fwcmd} ${firewall_flags} ${firewall_type}
21186d7f5d3SJohn Marino        fi
21286d7f5d3SJohn Marino    ;;
21386d7f5d3SJohn Marinoesac
214