Merge r4092 r4120 r4124
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 25 Jan 2009 16:54:42 +0000 (16:54 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 25 Jan 2009 16:54:42 +0000 (16:54 +0000)
applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua
modules/freifunk/root/etc/firewall.freifunk [new file with mode: 0644]
modules/freifunk/root/etc/hotplug.d/iface/22-firewall-nat-fix [new file with mode: 0644]

index 87bbe4d1f29cf480239d977351523a9d734aac96..6373c7a8075f2d9010ecff568c65b4ad102db38c 100644 (file)
@@ -172,7 +172,7 @@ function main.write(self, section, value)
        uci:save("wireless")
 
        -- Create firewall zone and add default rules (first time)
-       local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
+       local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
        if newzone then
                uci:foreach("freifunk", "fw_forwarding", function(section)
                        uci:section("firewall", "forwarding", nil, section)
@@ -187,10 +187,40 @@ function main.write(self, section, value)
                uci:foreach(external, "fw_rule", function(section)
                        uci:section("firewall", "rule", nil, section)
                end)
+       end
+
+       -- Enforce firewall include
+       local has_include = false
+       uci:foreach("firewall", "include",
+               function(section)
+                       if section.path == "/etc/firewall.freifunk" then
+                               has_include = true
+                       end
+               end)
+
+       if not has_include then
+               uci:section("firewall", "include", nil,
+                       { path = "/etc/firewall.freifunk" })
+       end
 
-               uci:save("firewall")
+       -- Allow state: invalid packets
+       uci:foreach("firewall", "defaults",
+               function(section)
+                       uci:set("firewall", section[".name"], "drop_invalid", "0")
+               end)
+
+       -- Prepare advanced config
+       local has_advanced = false
+       uci:foreach("firewall", "advanced",
+               function(section) has_advanced = true end)
+
+       if not has_advanced then
+               uci:section("firewall", "advanced", nil,
+                       { tcp_ecn = "0" })
        end
 
+       uci:save("firewall")
+
 
        -- Crate network interface
        local netconfig = uci:get_all("freifunk", "interface")
diff --git a/modules/freifunk/root/etc/firewall.freifunk b/modules/freifunk/root/etc/firewall.freifunk
new file mode 100644 (file)
index 0000000..ac3fcc5
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/sh
+# Freifunk Firewall addons
+# $Id$
+
+
+#
+# Apply advanced settings
+#
+apply_advanced() {
+       local tcp_ecn
+       local tcp_window_scaling
+       local accept_redirects
+       local accept_source_route
+
+       config_get_bool tcp_ecn $1 tcp_ecn 1
+       config_get_bool tcp_window_scaling $1 tcp_window_scaling 1
+       config_get_bool accept_redirects $1 accept_redirects 0
+       config_get_bool accept_source_route $1 accept_source_route 0
+
+       logger -t firewall.freifunk "tcp_ecn is $tcp_ecn"
+       logger -t firewall.freifunk "tcp_window_scaling is $tcp_window_scaling"
+       logger -t firewall.freifunk "accept_redirects is $accept_redirects"
+       logger -t firewall.freifunk "accept_source_route is $accept_source_route"
+
+       sysctl -w net.ipv4.tcp_ecn=$tcp_ecn >/dev/null
+       sysctl -w net.ipv4.tcp_window_scaling=$tcp_window_scaling >/dev/null
+
+       for f in /proc/sys/net/ipv4/conf/*/accept_redirects; do
+               echo $accept_redirects > $f
+       done
+
+       for f in /proc/sys/net/ipv4/conf/*/accept_source_route; do
+               echo $accept_source_route > $f
+       done
+}
+
+config_foreach apply_advanced advanced
+
+
+#
+# Apply fixes for masquerading rules
+#
+apply_nat_fix() {
+       local up
+       local ifname
+       config_get up $1 up
+       [ -n "$up" ] || return 0
+       (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/22-firewall-nat-fix )
+}
+
+config_foreach apply_nat_fix interface
diff --git a/modules/freifunk/root/etc/hotplug.d/iface/22-firewall-nat-fix b/modules/freifunk/root/etc/hotplug.d/iface/22-firewall-nat-fix
new file mode 100644 (file)
index 0000000..634f432
--- /dev/null
@@ -0,0 +1,61 @@
+. /lib/firewall/uci_firewall.sh
+unset ZONE
+config_get ifname $INTERFACE ifname
+[ "$ifname" == "lo" ] && exit 0
+
+load_zones() {
+       local name
+       local network
+       config_get name $1 name
+       config_get network $1 network
+       [ -z "$network" ] && network=$name
+       for n in $network; do
+               [ "$n" = "$INTERFACE" ] && ZONE="$ZONE $name"
+       done
+}
+
+config_foreach load_zones zone
+
+[ -z "$ZONE" ] && exit 0
+
+natfix_addr_add() {
+       local network=$1
+       local iface=$2
+
+       config_get parent "$1" interface
+       [ "$network" != "$INTERFACE" -a "$parent" != "$INTERFACE" ] && return 0
+
+       config_get ipaddr "$network" ipaddr
+       [ -n "$ipaddr" ] || return 0
+       config_get netmask "$network" netmask
+       [ -n "$netmask" ] || return 0
+       eval "$(ipcalc.sh $ipaddr $netmask)"
+
+       logger -t firewall.freifunk "adding nat rule for $iface($NETWORK/$PREFIX)"
+       iptables -t nat -A "natfix_$iface" -s "$NETWORK/$PREFIX" -d "$NETWORK/$PREFIX" -j ACCEPT
+}
+
+[ ifup = "$ACTION" ] && {
+       iptables -t nat -N "natfix_$ifname"
+       natfix_addr_add "$INTERFACE" "$ifname"
+       config_foreach natfix_addr_add alias "$ifname"
+
+       for z in $ZONE; do
+               local loaded
+               config_get loaded core loaded
+               [ -n "$loaded" ] && {
+                       logger -t firewall.freifunk "applying nat rules on zone $z"
+                       iptables -t nat -I "zone_${z}_nat" 1 -o "$ifname" -j "natfix_$ifname"
+               }
+       done
+}
+
+[ ifdown = "$ACTION" ] && {
+       for z in $ZONE; do
+               local up
+               config_get up $z up
+               iptables -t nat -D "zone_${z}_nat" -o "$ifname" -j "natfix_$ifname" 2>/dev/null
+       done
+       iptables -t nat -F "natfix_$ifname" 2>/dev/null
+       iptables -t nat -X "natfix_$ifname" 2>/dev/null
+}