Add a boolean to allow NAT from LAN or not, default to nat LAN (#2535)
[openwrt.git] / package / iptables / files / firewall.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
3
4 ## Please make changes in /etc/firewall.user
5 START=45
6 start() {
7 include /lib/network
8 scan_interfaces
9 config_load /var/state/network
10
11 config_get WAN wan ifname
12 config_get WANDEV wan device
13 config_get LAN lan ifname
14 config_get_bool NAT_LAN lan nat 1
15 if [ $NAT_LAN -ne 0 ]
16 then
17 config_get LAN_MASK lan netmask
18 config_get LAN_IP lan ipaddr
19 LAN_NET=$(/bin/ipcalc.sh $LAN_IP $LAN_MASK | grep NETWORK | cut -d= -f2)
20 fi
21
22 ## CLEAR TABLES
23 for T in filter nat; do
24 iptables -t $T -F
25 iptables -t $T -X
26 done
27
28 iptables -N input_rule
29 iptables -N input_wan
30 iptables -N output_rule
31 iptables -N forwarding_rule
32 iptables -N forwarding_wan
33
34 iptables -t nat -N NEW
35 iptables -t nat -N prerouting_rule
36 iptables -t nat -N prerouting_wan
37 iptables -t nat -N postrouting_rule
38
39 iptables -N LAN_ACCEPT
40 [ -z "$WAN" ] || iptables -A LAN_ACCEPT -i "$WAN" -j RETURN
41 [ -z "$WANDEV" -o "$WANDEV" = "$WAN" ] || iptables -A LAN_ACCEPT -i "$WANDEV" -j RETURN
42 iptables -A LAN_ACCEPT -j ACCEPT
43
44 ### INPUT
45 ### (connections with the router as destination)
46
47 # base case
48 iptables -P INPUT DROP
49 iptables -A INPUT -m state --state INVALID -j DROP
50 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
51 iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j DROP
52
53 #
54 # insert accept rule or to jump to new accept-check table here
55 #
56 iptables -A INPUT -j input_rule
57 [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
58
59 # allow
60 iptables -A INPUT -j LAN_ACCEPT # allow from lan/wifi interfaces
61 iptables -A INPUT -p icmp -j ACCEPT # allow ICMP
62 iptables -A INPUT -p gre -j ACCEPT # allow GRE
63
64 # reject (what to do with anything not allowed earlier)
65 iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
66 iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
67
68 ### OUTPUT
69 ### (connections with the router as source)
70
71 # base case
72 iptables -P OUTPUT DROP
73 iptables -A OUTPUT -m state --state INVALID -j DROP
74 iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
75
76 #
77 # insert accept rule or to jump to new accept-check table here
78 #
79 iptables -A OUTPUT -j output_rule
80
81 # allow
82 iptables -A OUTPUT -j ACCEPT #allow everything out
83
84 # reject (what to do with anything not allowed earlier)
85 iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
86 iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
87
88 ### FORWARDING
89 ### (connections routed through the router)
90
91 # base case
92 iptables -P FORWARD DROP
93 iptables -A FORWARD -m state --state INVALID -j DROP
94 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
95 iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
96
97 #
98 # insert accept rule or to jump to new accept-check table here
99 #
100 iptables -A FORWARD -j forwarding_rule
101 [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
102
103 # allow
104 iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
105 [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
106
107 # reject (what to do with anything not allowed earlier)
108 # uses the default -P DROP
109
110 ### MASQ
111 iptables -t nat -A PREROUTING -m state --state NEW -p tcp -j NEW
112 iptables -t nat -A PREROUTING -j prerouting_rule
113 [ -z "$WAN" ] || iptables -t nat -A PREROUTING -i "$WAN" -j prerouting_wan
114 iptables -t nat -A POSTROUTING -j postrouting_rule
115 ### Only LAN, unless told not to
116 if [ $NAT_LAN -ne 0 ]
117 then
118 [ -z "$WAN" ] || iptables -t nat -A POSTROUTING --src $LAN_NET/$LAN_MASK -o $WAN -j MASQUERADE
119 fi
120
121 iptables -t nat -A NEW -m limit --limit 50 --limit-burst 100 -j RETURN && \
122 iptables -t nat -A NEW -j DROP
123
124 ## USER RULES
125 [ -f /etc/firewall.user ] && . /etc/firewall.user
126 [ -n "$WAN" -a -e /etc/config/firewall ] && {
127 export WAN
128 awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
129 }
130 }
131
132 stop() {
133 iptables -P INPUT ACCEPT
134 iptables -P OUTPUT ACCEPT
135 iptables -P FORWARD ACCEPT
136 iptables -F
137 iptables -X
138 iptables -t nat -P PREROUTING ACCEPT
139 iptables -t nat -P POSTROUTING ACCEPT
140 iptables -t nat -P OUTPUT ACCEPT
141 iptables -t nat -F
142 iptables -t nat -X
143 }
This page took 0.057277 seconds and 5 git commands to generate.