add missing copyright notices
[openwrt-10.03/.git] / package / iptables / files / firewall.init
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 ## Please make changes in /etc/firewall.user
5
6 . /etc/config/network
7 WAN="$wan_ifname"
8 LAN="$lan_ifname"
9
10 ## CLEAR TABLES
11 for T in filter nat; do
12   iptables -t $T -F
13   iptables -t $T -X
14 done
15
16 iptables -N input_rule
17 iptables -N output_rule
18 iptables -N forwarding_rule
19
20 iptables -t nat -N prerouting_rule
21 iptables -t nat -N postrouting_rule
22
23 ### INPUT
24 ###  (connections with the router as destination)
25
26   # base case
27   iptables -P INPUT DROP
28   iptables -A INPUT -m state --state INVALID -j DROP
29   iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
30   iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j  DROP
31
32   #
33   # insert accept rule or to jump to new accept-check table here
34   #
35   iptables -A INPUT -j input_rule
36
37   # allow
38   [ -z "$WAN" ] || iptables -A INPUT -i \! $WAN -j ACCEPT       # allow from lan/wifi interfaces 
39   iptables -A INPUT -p icmp     -j ACCEPT       # allow ICMP
40   iptables -A INPUT -p gre      -j ACCEPT       # allow GRE
41
42   # reject (what to do with anything not allowed earlier)
43   iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
44   iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
45
46 ### OUTPUT
47 ### (connections with the router as source)
48
49   # base case
50   iptables -P OUTPUT DROP
51   iptables -A OUTPUT -m state --state INVALID -j DROP
52   iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
53
54   #
55   # insert accept rule or to jump to new accept-check table here
56   #
57   iptables -A OUTPUT -j output_rule
58
59   # allow
60   iptables -A OUTPUT -j ACCEPT          #allow everything out
61
62   # reject (what to do with anything not allowed earlier)
63   iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
64   iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
65
66 ### FORWARDING
67 ### (connections routed through the router)
68
69   # base case
70   iptables -P FORWARD DROP 
71   iptables -A FORWARD -m state --state INVALID -j DROP
72   iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
73   iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
74
75   #
76   # insert accept rule or to jump to new accept-check table here
77   #
78   iptables -A FORWARD -j forwarding_rule
79
80   # allow
81   iptables -A FORWARD -i br0 -o br0 -j ACCEPT
82   [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
83
84   # reject (what to do with anything not allowed earlier)
85   # uses the default -P DROP
86
87 ### MASQ
88   iptables -t nat -A PREROUTING -j prerouting_rule
89   iptables -t nat -A POSTROUTING -j postrouting_rule
90   [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
91
92 ## USER RULES
93 [ -f /etc/firewall.user ] && . /etc/firewall.user
94 [ -n "$WAN" -a -e /etc/config/firewall ] && {
95         awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
96 }