]> git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blobdiff - src/initscripts/system/firewall
firewall: initialize IFACE if it is empty
[people/mfischer/ipfire-2.x.git] / src / initscripts / system / firewall
index 9a79cb1aa98e8ae814ca1f1217ce1b0097adea98..577c10c89d8c71c6125b120fd0899f4011bb0c76 100644 (file)
@@ -7,11 +7,21 @@ eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
+if [ -z $IFACE]; then
+       IFACE="red0"
+fi
 
 if [ -f /var/ipfire/red/device ]; then
        DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
 fi
 
+NAT_MASK="0x0f000000"
+
+IPS_REPEAT_MARK="0x80000000"
+IPS_REPEAT_MASK="0x80000000"
+IPS_BYPASS_MARK="0x40000000"
+IPS_BYPASS_MASK="0x40000000"
+
 function iptables() {
        /sbin/iptables --wait "$@"
 }
@@ -32,33 +42,55 @@ iptables_init() {
        iptables -P FORWARD DROP
        iptables -P OUTPUT ACCEPT
 
+       # Enable TRACE logging to syslog
+       modprobe nf_log_ipv4
+       sysctl -q -w net.netfilter.nf_log.2=nf_log_ipv4
+
+       # IPS Bypass Chain which stores the BYPASS bit in connection tracking
+       iptables -N IPSBYPASS
+       iptables -A IPSBYPASS -j CONNMARK --save-mark --mask "$(( ~IPS_REPEAT_MASK & 0xffffffff ))"
+
+       # Jump into bypass chain when the BYPASS bit is set
+       for chain in INPUT FORWARD OUTPUT; do
+               iptables -A "${chain}" -m mark \
+                       --mark "$(( IPS_REPEAT_MARK | IPS_BYPASS_MARK ))/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j IPSBYPASS
+       done
+
        # Empty LOG_DROP and LOG_REJECT chains
        iptables -N LOG_DROP
-       iptables -A LOG_DROP   -m limit --limit 10/minute -j LOG
+       iptables -A LOG_DROP   -m limit --limit 10/second -j LOG
        iptables -A LOG_DROP   -j DROP
        iptables -N LOG_REJECT
-       iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
+       iptables -A LOG_REJECT -m limit --limit 10/second -j LOG
        iptables -A LOG_REJECT -j REJECT
 
        # This chain will log, then DROPs packets with certain bad combinations
-       # of flags might indicate a port-scan attempt (xmas, null, etc)
+       # of flags might indicate a port-scan attempt (xmas, null, etc.)
        iptables -N PSCAN
        if [ "$DROPPORTSCAN" == "on" ]; then
-               iptables -A PSCAN -p tcp  -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
-               iptables -A PSCAN -p udp  -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
-               iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
-               iptables -A PSCAN -f      -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
+               iptables -A PSCAN -p tcp  -m limit --limit 10/second -j LOG --log-prefix "DROP_TCP Scan "  -m comment --comment "DROP_TCP PScan"
+               iptables -A PSCAN -p udp  -m limit --limit 10/second -j LOG --log-prefix "DROP_UDP Scan "  -m comment --comment "DROP_UDP PScan"
+               iptables -A PSCAN -p icmp -m limit --limit 10/second -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
+               iptables -A PSCAN -f      -m limit --limit 10/second -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
        fi
        iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
 
        # New tcp packets without SYN set - could well be an obscure type of port scan
-       # that's not covered above, may just be a broken windows machine
+       # that's not covered above, may just be a broken Windows machine
        iptables -N NEWNOTSYN
        if [ "$DROPNEWNOTSYN" == "on" ]; then
-               iptables -A NEWNOTSYN  -m limit --limit 10/minute -j LOG  --log-prefix "DROP_NEWNOTSYN "
+               iptables -A NEWNOTSYN  -m limit --limit 10/second -j LOG  --log-prefix "DROP_NEWNOTSYN "
        fi
        iptables -A NEWNOTSYN  -j DROP -m comment --comment "DROP_NEWNOTSYN"
 
+       # Log and subsequently drop spoofed packets or "martians", arriving from sources
+       # on interfaces where we don't expect them
+       iptables -N SPOOFED_MARTIAN
+       if [ "$DROPSPOOFEDMARTIAN" == "on" ]; then
+               iptables -A SPOOFED_MARTIAN  -m limit --limit 10/second -j LOG  --log-prefix "DROP_SPOOFED_MARTIAN "
+       fi
+       iptables -A SPOOFED_MARTIAN -j DROP -m comment --comment "DROP_SPOOFED_MARTIAN"
+
        # Chain to contain all the rules relating to bad TCP flags
        iptables -N BADTCP
 
@@ -89,77 +121,13 @@ iptables_init() {
        # Connection tracking chains
        iptables -N CONNTRACK
        iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED -j ACCEPT
-       iptables -A CONNTRACK -m conntrack --ctstate INVALID -j DROP
+       iptables -A CONNTRACK -m conntrack --ctstate INVALID -j LOG_DROP
        iptables -A CONNTRACK -p icmp -m conntrack --ctstate RELATED -j ACCEPT
-       iptables -t raw -N CONNTRACK
-       iptables -t raw -A PREROUTING -j CONNTRACK
-
-       # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
-
-       # SIP
-       if [ "${CONNTRACK_SIP}" = "on" ]; then
-               modprobe nf_nat_sip
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper sip -j ACCEPT
-               for proto in udp tcp; do
-                       iptables -t raw -A CONNTRACK -p "${proto}" --dport 5060 -j CT --helper sip
-               done
-       fi
-
-       # H.323
-       if [ "${CONNTRACK_H323}" = "on" ]; then
-               modprobe nf_nat_h323
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper h323 -j ACCEPT
 
-               # Gatekeeper RAS
-               iptables -t raw -A CONNTRACK -p udp --dport 1719 -j CT --helper RAS
+       # Restore any connection marks
+       iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
 
-               # Q.931
-               iptables -t raw -A CONNTRACK -p tcp --dport 1720 -j CT --helper Q.931
-       fi
-
-       # FTP
-       if [ "${CONNTRACK_FTP}" = "on" ]; then
-               modprobe nf_nat_ftp
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper ftp -p tcp --dport 1024: -j ACCEPT
-               iptables -t raw -A CONNTRACK -p tcp --dport 21 -j CT --helper ftp
-       fi
-
-       # PPTP
-       if [ "${CONNTRACK_PPTP}" = "on" ]; then
-               modprobe nf_nat_pptp
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper pptp -j ACCEPT
-               iptables -t raw -A CONNTRACK -p tcp --dport 1723 -j CT --helper pptp
-       fi
-
-       # TFTP
-       if [ "${CONNTRACK_TFTP}" = "on" ]; then
-               modprobe nf_nat_tftp
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper tftp -j ACCEPT
-               iptables -t raw -A CONNTRACK -p udp --dport 69 -j CT --helper tftp
-       fi
-
-       # IRC
-       if [ "${CONNTRACK_IRC}" = "on" ]; then
-               modprobe nf_nat_irc
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper irc -j ACCEPT
-               iptables -t raw -A CONNTRACK -p tcp --dport 6667 -j CT --helper irc
-       fi
-
-       # Amanda
-       if [ "${CONNTRACK_AMANDA}" = "on" ]; then
-               modprobe nf_nat_amanda
-               iptables -A CONNTRACK -m conntrack --ctstate RELATED \
-                       -m helper --helper amanda -j ACCEPT
-               iptables -t raw -A CONNTRACK -p tcp -j CT --helper amanda
-       fi
-
-       # Fix for braindead ISP's
+       # Fix for braindead ISPs
        iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
 
        # CUSTOM chains, can be used by the users themselves
@@ -174,22 +142,29 @@ iptables_init() {
        iptables -t nat -N CUSTOMPOSTROUTING
        iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
 
+       # Log and drop any traffic from and to networks known as being hostile, posing
+       # a technical threat to our users (i. e. listed at Spamhaus DROP et al.)
+       iptables -N HOSTILE
+       if [ "$DROPHOSTILE" == "on" ]; then
+               iptables -A HOSTILE -m limit --limit 10/second -j LOG  --log-prefix "DROP_HOSTILE "
+               iptables -A INPUT   -i $IFACE -m geoip --src-cc XD -j HOSTILE
+               iptables -A FORWARD -i $IFACE -m geoip --src-cc XD -j HOSTILE
+               iptables -A FORWARD -o $IFACE -m geoip --dst-cc XD -j HOSTILE
+               iptables -A OUTPUT  -o $IFACE -m geoip --src-cc XD -j HOSTILE
+       fi
+       iptables -A HOSTILE -j DROP -m comment --comment "DROP_HOSTILE"
+
        # P2PBLOCK
        iptables -N P2PBLOCK
        iptables -A INPUT -j P2PBLOCK
        iptables -A FORWARD -j P2PBLOCK
        iptables -A OUTPUT -j P2PBLOCK
-       
-       # Guardian (IPS) chains
+
+       # IPS (Guardian) chains
        iptables -N GUARDIAN
        iptables -A INPUT -j GUARDIAN
        iptables -A FORWARD -j GUARDIAN
 
-       # IPS (suricata) chains
-       iptables -N IPS
-       iptables -A INPUT -j IPS
-       iptables -A FORWARD -j IPS
-
        # Block non-established IPsec networks
        iptables -N IPSECBLOCK
        iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
@@ -201,6 +176,15 @@ iptables_init() {
        iptables -A FORWARD -i tun+ -j OVPNBLOCK
        iptables -A FORWARD -o tun+ -j OVPNBLOCK
 
+       # IPS (Suricata) chains
+       iptables -N IPS_INPUT
+       iptables -N IPS_FORWARD
+       iptables -N IPS_OUTPUT
+
+       for chain in INPUT FORWARD OUTPUT; do
+               iptables -A "${chain}" -m mark --mark "0x0/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j "IPS_${chain}"
+       done
+
        # OpenVPN transfer network translation
        iptables -t nat -N OVPNNAT
        iptables -t nat -A POSTROUTING -j OVPNNAT
@@ -216,14 +200,18 @@ iptables_init() {
        iptables -A INPUT -j ICMPINPUT
        iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
 
-       # Accept everything on loopback
+       # Accept everything on loopback if source/destination is loopback space...
        iptables -N LOOPBACK
-       iptables -A LOOPBACK -i lo -j ACCEPT
-       iptables -A LOOPBACK -o lo -j ACCEPT
+       iptables -A LOOPBACK -i lo -s 127.0.0.0/8 -j ACCEPT
+       iptables -A LOOPBACK -o lo -d 127.0.0.0/8 -j ACCEPT
 
-       # Filter all packets with loopback addresses on non-loopback interfaces.
-       iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
-       iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
+       # ... and drop everything else on the loopback interface, since no other traffic should appear there
+       iptables -A LOOPBACK -i lo -j SPOOFED_MARTIAN
+       iptables -A LOOPBACK -o lo -j SPOOFED_MARTIAN
+
+       # Filter all packets with loopback addresses on non-loopback interfaces (spoofed)
+       iptables -A LOOPBACK -s 127.0.0.0/8 -j SPOOFED_MARTIAN
+       iptables -A LOOPBACK -d 127.0.0.0/8 -j SPOOFED_MARTIAN
 
        for i in INPUT FORWARD OUTPUT; do
                iptables -A ${i} -j LOOPBACK
@@ -258,7 +246,7 @@ iptables_init() {
                iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
        fi
 
-       # allow DHCP on BLUE to be turned on/off
+       # Allow DHCP on BLUE to be turned on/off
        iptables -N DHCPBLUEINPUT
        iptables -N DHCPBLUEOUTPUT
        if [ -n "${BLUE_DEV}" ]; then
@@ -266,10 +254,14 @@ iptables_init() {
                iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
        fi
 
-       # GeoIP block
-       iptables -N GEOIPBLOCK
-       iptables -A INPUT -j GEOIPBLOCK
-       iptables -A FORWARD -j GEOIPBLOCK
+       # Tor (inbound)
+       iptables -N TOR_INPUT
+       iptables -A INPUT -j TOR_INPUT
+
+       # Location Block
+       iptables -N LOCATIONBLOCK
+       iptables -A INPUT -j LOCATIONBLOCK
+       iptables -A FORWARD -j LOCATIONBLOCK
 
        # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
        iptables -N IPSECINPUT
@@ -299,10 +291,10 @@ iptables_init() {
        iptables -N OVPNINPUT
        iptables -A INPUT -j OVPNINPUT
 
-       # TOR
-       iptables -N TOR_INPUT
-       iptables -A INPUT -j TOR_INPUT
-       
+       # Tor (outbound)
+       iptables -N TOR_OUTPUT
+       iptables -A OUTPUT -j TOR_OUTPUT
+
        # Jump into the actual firewall ruleset.
        iptables -N INPUTFW
        iptables -A INPUT -j INPUTFW
@@ -338,25 +330,19 @@ iptables_init() {
 
        if [ -n "${GREEN_ADDRESS}" ]; then
                iptables -t nat -A NAT_DESTINATION_FIX \
-                       -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
+                       -m mark --mark "0x01000000/${NAT_MASK}" -j SNAT --to-source "${GREEN_ADDRESS}"
        fi
 
        if [ -n "${BLUE_ADDRESS}" ]; then
                iptables -t nat -A NAT_DESTINATION_FIX \
-                       -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
+                       -m mark --mark "0x02000000/${NAT_MASK}" -j SNAT --to-source "${BLUE_ADDRESS}"
        fi
 
        if [ -n "${ORANGE_ADDRESS}" ]; then
                iptables -t nat -A NAT_DESTINATION_FIX \
-                       -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
+                       -m mark --mark "0x04000000/${NAT_MASK}" -j SNAT --to-source "${ORANGE_ADDRESS}"
        fi
 
-       # upnp chain for our upnp daemon
-       iptables -t nat -N UPNPFW
-       iptables -t nat -A PREROUTING -j UPNPFW
-       iptables -N UPNPFW
-       iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
-
        # RED chain, used for the red interface
        iptables -N REDINPUT
        iptables -A INPUT -j REDINPUT
@@ -365,8 +351,8 @@ iptables_init() {
        iptables -t nat -N REDNAT
        iptables -t nat -A POSTROUTING -j REDNAT
 
-       # Populate IPsec block chain
-       /usr/lib/firewall/ipsec-block
+       # Populate IPsec chains
+       /usr/lib/firewall/ipsec-policy
 
        # Apply OpenVPN firewall rules
        /usr/local/bin/openvpnctrl --firewall-rules
@@ -374,6 +360,9 @@ iptables_init() {
        # run wirelessctrl
        /usr/local/bin/wirelessctrl
 
+       # run captivectrl
+       /usr/local/bin/captivectrl
+
        # POLICY CHAIN
        iptables -N POLICYIN
        iptables -A INPUT -j POLICYIN
@@ -400,6 +389,17 @@ iptables_red_up() {
        iptables -F REDFORWARD
        iptables -t nat -F REDNAT
 
+       # Prohibit spoofing our own IP address on RED
+       if [ -f /var/ipfire/red/active ]; then
+               REDIP="$( cat /var/ipfire/red/local-ipaddress )";
+
+               if [ "$IFACE" != "" ]; then
+                       iptables -A REDINPUT -s $REDIP -i $IFACE -j SPOOFED_MARTIAN
+               elif [ "$DEVICE" != "" ]; then
+                       iptables -A REDINPUT -s $REDIP -i $DEVICE -j SPOOFED_MARTIAN
+               fi
+       fi
+
        # PPPoE / PPTP Device
        if [ "$IFACE" != "" ]; then
                # PPPoE / PPTP
@@ -419,15 +419,6 @@ iptables_red_up() {
                iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
        fi
 
-       # Orange pinholes
-       if [ "$ORANGE_DEV" != "" ]; then
-               # This rule enables a host on ORANGE network to connect to the outside
-               # (only if we have a red connection)
-               if [ "$IFACE" != "" ]; then
-                       iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
-               fi
-       fi
-
        if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
                # DHCP
                if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
@@ -439,8 +430,8 @@ iptables_red_up() {
                        iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
                fi
 
-               # Outgoing masquerading (don't masqerade IPSEC (mark 50))
-               iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
+               # Outgoing masquerading (don't masqerade IPsec)
+               iptables -t nat -A REDNAT -m policy --pol ipsec --dir=out -o "${IFACE}" -j RETURN
 
                if [ "${IFACE}" = "${GREEN_DEV}" ]; then
                        iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
@@ -475,7 +466,7 @@ iptables_red_up() {
 
 iptables_red_down() {
        # Prohibit packets to reach the masquerading rule
-       # while the wan interface is down - this is required to
+       # while the WAN interface is down - this is required to
        # circumvent udp related NAT issues
        # http://forum.ipfire.org/index.php?topic=11127.0
        if [ -n "${IFACE}" ]; then