51c35b03a1191d9269cb207ef2023b0951b2cd31
[oweals/openwrt.git] / openwrt / target / default / target_skeleton / etc / init.d / S45firewall
1 #!/bin/sh
2 ${FAILSAFE:+return}
3 . /etc/functions.sh
4 WAN=$(nvram get wan_ifname)
5 LAN=$(nvram get lan_ifname)
6
7 ## CLEAR TABLES
8 for T in filter nat mangle; do
9   iptables -t $T -F
10   iptables -t $T -X
11 done
12
13 iptables -N input_rule
14 iptables -N output_rule
15 iptables -N forwarding_rule
16
17 iptables -t nat -N prerouting_rule
18 iptables -t nat -N postrouting_rule
19
20 ### Allow SSH from WAN
21 # iptables -t nat -A prerouting_rule -i $WAN -p tcp --dport 22 -j ACCEPT 
22 # iptables        -A input_rule      -i $WAN -p tcp --dport 22 -j ACCEPT
23
24 ### Port forwarding
25 # iptables -t nat -A prerouting_rule -i $WAN -p tcp --dport 22 -j DNAT --to 192.168.1.2
26 # iptables        -A forwarding_rule -i $WAN -p tcp --dport 22 -d 192.168.1.2 -j ACCEPT
27
28 ### DMZ (should be placed after port forwarding / accept rules)
29 # iptables -t nat -A prerouting_rule -i $WAN -j DNAT --to 192.168.1.2
30 # iptables        -A forwarding_rule -i $WAN -d 192.168.1.2 -j ACCEPT
31
32 ### INPUT
33 ###  (connections with the router as destination)
34
35   # base case
36   iptables -P INPUT DROP
37   iptables -A INPUT -m state --state INVALID -j DROP
38   iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
39   iptables -A INPUT -p tcp --syn --tcp-option \! 2 -j  DROP
40
41   # allow
42   iptables -A INPUT -i \! $WAN  -j ACCEPT       # allow from lan/wifi interfaces 
43   iptables -A INPUT -p icmp     -j ACCEPT       # allow ICMP
44   iptables -A INPUT -p gre      -j ACCEPT       # allow GRE
45   #
46   # insert accept rule or to jump to new accept-check table here
47   #
48   iptables -A INPUT -j input_rule
49
50   # reject (what to do with anything not allowed earlier)
51   iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
52   iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
53
54 ### OUTPUT
55 ### (connections with the router as source)
56
57   # base case
58   iptables -P OUTPUT DROP
59   iptables -A OUTPUT -m state --state INVALID -j DROP
60   iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
61
62   # allow
63   iptables -A OUTPUT -j ACCEPT          #allow everything out
64   #
65   # insert accept rule or to jump to new accept-check table here
66   #
67   iptables -A OUTPUT -j output_rule
68
69   # reject (what to do with anything not allowed earlier)
70   iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
71   iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
72
73 ### FORWARDING
74 ### (connections routed through the router)
75
76   # base case
77   iptables -P FORWARD DROP 
78   iptables -A FORWARD -m state --state INVALID -j DROP
79   iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
80   iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
81
82   # allow
83   iptables -A FORWARD -i br0 -o br0 -j ACCEPT
84   iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
85   #
86   # insert accept rule or to jump to new accept-check table here
87   #
88   iptables -A FORWARD -j forwarding_rule
89
90   # reject (what to do with anything not allowed earlier)
91   # uses the default -P DROP
92
93 ### MASQ
94   iptables -t nat -A PREROUTING -j prerouting_rule
95   iptables -t nat -A POSTROUTING -j postrouting_rule
96   iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE