sync firewall script with whiterussian changes
[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/functions.sh
7 include network
8
9 scan_interfaces
10 config_get WAN wan ifname
11 config_get LAN lan ifname
12
13 ## CLEAR TABLES
14 for T in filter nat; do
15   iptables -t $T -F
16   iptables -t $T -X
17 done
18
19 iptables -N input_rule
20 iptables -N output_rule
21 iptables -N forwarding_rule
22
23 iptables -t nat -N prerouting_rule
24 iptables -t nat -N postrouting_rule
25
26 iptables -N LAN_ACCEPT
27 [ -z "$WAN" ] || iptables -A LAN_ACCEPT -i "$WAN" -j RETURN
28 iptables -A LAN_ACCEPT -j ACCEPT
29
30 ### INPUT
31 ###  (connections with the router as destination)
32
33   # base case
34   iptables -P INPUT DROP
35   iptables -A INPUT -m state --state INVALID -j DROP
36   iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
37   iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j  DROP
38
39   #
40   # insert accept rule or to jump to new accept-check table here
41   #
42   iptables -A INPUT -j input_rule
43
44   # allow
45   iptables -A INPUT -j LAN_ACCEPT       # allow from lan/wifi interfaces 
46   iptables -A INPUT -p icmp     -j ACCEPT       # allow ICMP
47   iptables -A INPUT -p gre      -j ACCEPT       # allow GRE
48
49   # reject (what to do with anything not allowed earlier)
50   iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
51   iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
52
53 ### OUTPUT
54 ### (connections with the router as source)
55
56   # base case
57   iptables -P OUTPUT DROP
58   iptables -A OUTPUT -m state --state INVALID -j DROP
59   iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
60
61   #
62   # insert accept rule or to jump to new accept-check table here
63   #
64   iptables -A OUTPUT -j output_rule
65
66   # allow
67   iptables -A OUTPUT -j ACCEPT          #allow everything out
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 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
80   iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
81
82   #
83   # insert accept rule or to jump to new accept-check table here
84   #
85   iptables -A FORWARD -j forwarding_rule
86
87   # allow
88   iptables -A FORWARD -i br0 -o br0 -j ACCEPT
89   [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
90
91   # reject (what to do with anything not allowed earlier)
92   # uses the default -P DROP
93
94 ### MASQ
95   iptables -t nat -A PREROUTING -j prerouting_rule
96   iptables -t nat -A POSTROUTING -j postrouting_rule
97   [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
98
99 ## USER RULES
100 [ -f /etc/firewall.user ] && . /etc/firewall.user
101 [ -n "$WAN" -a -e /etc/config/firewall ] && {
102         awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
103 }