rewrite of the network scripts and configuration
[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 ### INPUT
27 ###  (connections with the router as destination)
28
29   # base case
30   iptables -P INPUT DROP
31   iptables -A INPUT -m state --state INVALID -j DROP
32   iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
33   iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j  DROP
34
35   #
36   # insert accept rule or to jump to new accept-check table here
37   #
38   iptables -A INPUT -j input_rule
39
40   # allow
41   iptables -A INPUT ${WAN:+-i \! $WAN}  -j ACCEPT       # allow from lan/wifi interfaces 
42   iptables -A INPUT -p icmp     -j ACCEPT       # allow ICMP
43   iptables -A INPUT -p gre      -j ACCEPT       # allow GRE
44
45   # reject (what to do with anything not allowed earlier)
46   iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
47   iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
48
49 ### OUTPUT
50 ### (connections with the router as source)
51
52   # base case
53   iptables -P OUTPUT DROP
54   iptables -A OUTPUT -m state --state INVALID -j DROP
55   iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
56
57   #
58   # insert accept rule or to jump to new accept-check table here
59   #
60   iptables -A OUTPUT -j output_rule
61
62   # allow
63   iptables -A OUTPUT -j ACCEPT          #allow everything out
64
65   # reject (what to do with anything not allowed earlier)
66   iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
67   iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
68
69 ### FORWARDING
70 ### (connections routed through the router)
71
72   # base case
73   iptables -P FORWARD DROP 
74   iptables -A FORWARD -m state --state INVALID -j DROP
75   iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
76   iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
77
78   #
79   # insert accept rule or to jump to new accept-check table here
80   #
81   iptables -A FORWARD -j forwarding_rule
82
83   # allow
84   iptables -A FORWARD -i br0 -o br0 -j ACCEPT
85   [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
86
87   # reject (what to do with anything not allowed earlier)
88   # uses the default -P DROP
89
90 ### MASQ
91   iptables -t nat -A PREROUTING -j prerouting_rule
92   iptables -t nat -A POSTROUTING -j postrouting_rule
93   [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
94
95 ## USER RULES
96 [ -f /etc/firewall.user ] && . /etc/firewall.user
97 [ -n "$WAN" -a -e /etc/config/firewall ] && {
98         awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
99 }