This is a total rewrite of the firewall.sh. This was done in order to support dynamic addition/deletions of interfaces (both LAN and WAN) without resorting to grep/-I <line> guessing.
Few notes:
1) There now is a -A INPUT -i lo -j ACCEPT line. This is to allow loopback
2) All -t filter (INPUT/OUTPUT/FORWARD) will DROP any packets not accepted.
3) Addition of -A FORWARD -j TCPMSS --clamp-mss-to-pmtu line. I found that using PPPoE, I could not connect to certain sites (probably because they disable ICMP and therefore cannot PMTU properly). This is a HACK (or so the author of TCPMSS claims), but it works (and is also in the stock Linksys firmware - but in a different manner), and will support PPPoE very nicely.
4) Addition of LAN-IN/WAN-IN. This is packet terminating ON the router. If you want to provide services on the router itself, do it on WAN-SVC instead.
5) Addition of LAN-FWD/WAN-FWD. If you want to disable forwarding of certain packets (NAT'ed or not), insert additional rules here. If you want to provide Port Forwarding, do it on LAN-SVC instead.
6) Addition of LAN-SVC/WAN-SVC. LAN-SVC is kind of misleading, its what's commonly known as port forwarding. Note, there are TWO LAN-SVC, one for -t filter and one for -t nat, and the arguments are different. WAN-SVC is for services that the router is providing. Please look at the examples provided.
7) I've made it so that both ppp0 and vlan1 are covered. However, it should be that firewall.sh be called to add/delete interface by the correct script (ie. ip-up/default.renew), which is why the ONLY place there is a -i/-o <iface> is on the builtin chain. But since that would require rewriting the support script (networking.sh), it's the way it is.
8) This is currently a drop-in replacement for the existing firewall.sh.
#!/bin/sh
# OpenWRT Extended Firewall script
# Copyright (C) 2004, Kevin "Starfox" Arima, All rights reserved.
# This code is licensed under the terms of the GNU General Public License Version 2
PATH=/usr/bin:/bin:/usr/sbin:/sbin
ipt="$DEBUG iptables"
ipv4opt="$DEBUG ipv4opt"
inif="br0"
exif="vlan1 ppp0"
ipv4opt () {
echo $2 > /proc/sys/net/ipv4/$1
}
# Flush everything
tables=`cat /proc/net/ip_tables_names 2>/dev/null`
for table in $tables; do {
$ipt -t $table -F
$ipt -t $table -X
$ipt -t $table -Z
}; done
# Allow loopback
$ipt -t filter -A INPUT -i lo -j ACCEPT
# Create chains
for chain in LAN-IN WAN-IN LAN-FWD WAN-FWD LAN-SVC WAN-SVC; do {
$ipt -t filter -N $chain
}; done
for chain in LAN-SVC; do {
$ipt -t nat -N $chain
}; done
# Set default for chains
for chain in INPUT FORWARD OUTPUT; do {
$ipt -t filter -A $chain -m state --state INVALID -j DROP
$ipt -t filter -A $chain -m state --state RELATED,ESTABLISHED -j ACCEPT
$ipt -t filter -P $chain DROP
}; done
# INPUT
# Call LAN-IN/WAN-IN depending on interface
# LAN Input
$ipt -t filter -A LAN-IN -j ACCEPT
# WAN Input
$ipt -t filter -A WAN-IN -j WAN-SVC
$ipt -t filter -A WAN-IN -p tcp -j REJECT --reject-with tcp-reset
$ipt -t filter -A WAN-IN -j REJECT --reject-with icmp-port-unreachable
$ipt -t filter -A WAN-IN -j DROP
# FORWARD
$ipt -t filter -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# Call LAN-FWD/WAN-FWD depending on interface
# LAN Forward
$ipt -t filter -A LAN-FWD -j ACCEPT
# WAN Forward
$ipt -t filter -A WAN-FWD -j LAN-SVC
$ipt -t filter -A WAN-FWD -j DROP
# OUTPUT
$ipt -t filter -A OUTPUT -j ACCEPT
# PREROUTING
# Call LAN-SVC (Port-forwarding) from WAN interface
# POSTROUTING
# Call MASQUERADE from WAN interface
# LAN Services
# Example: Forward TCP Port 25 (SMTP) to 192.168.1.2
# $ipt -t filter -A LAN-SVC -p tcp --dport 25 -j DACCEPT
# $ipt -t nat -A LAN-SVC -p tcp --dport 25 -j DNAT --to 192.168.1.2
# WAN Services
$ipt -t filter -A WAN-SVC -p icmp -j ACCEPT
# Example: Accept TCP Port 22 (SSH) on the router
# $ipt -t filter -A WAN-SVC -p udp --dport 53 -j ACCEPT
# Internal interface
for if in $inif; do {
$ipt -t filter -A INPUT -i $if -j LAN-IN
$ipt -t filter -A FORWARD -i $if -j LAN-FWD
}; done
# External interface
for if in $exif; do {
$ipt -t filter -A INPUT -i $if -j WAN-IN
$ipt -t filter -A FORWARD -i $if -j WAN-FWD
$ipt -t nat -A PREROUTING -i $if -j LAN-SVC
$ipt -t nat -A POSTROUTING -o $if -j MASQUERADE
}; done
$ipv4opt ip_forward 1
$ipv4opt icmp_echo_ignore_broadcasts 1
$ipv4opt icmp_ignore_bogus_error_responses 1
$ipv4opt tcp_fin_timeout 30
$ipv4opt tcp_keepalive_time 120
$ipv4opt tcp_timestamps 0