make the firewall script run after the network script again (required for working...
[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
15 ## CLEAR TABLES
16 for T in filter nat; do
17 iptables -t $T -F
18 iptables -t $T -X
19 done
20
21 iptables -N input_rule
22 iptables -N input_wan
23 iptables -N output_rule
24 iptables -N forwarding_rule
25 iptables -N forwarding_wan
26
27 iptables -t nat -N NEW
28 iptables -t nat -N prerouting_rule
29 iptables -t nat -N prerouting_wan
30 iptables -t nat -N postrouting_rule
31
32 iptables -N LAN_ACCEPT
33 [ -z "$WAN" ] || iptables -A LAN_ACCEPT -i "$WAN" -j RETURN
34 [ -z "$WANDEV" -o "$WANDEV" = "$WAN" ] || iptables -A LAN_ACCEPT -i "$WANDEV" -j RETURN
35 iptables -A LAN_ACCEPT -j ACCEPT
36
37 ### INPUT
38 ### (connections with the router as destination)
39
40 # base case
41 iptables -P INPUT DROP
42 iptables -A INPUT -m state --state INVALID -j DROP
43 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
44 iptables -A INPUT -p tcp --tcp-flags SYN SYN --tcp-option \! 2 -j DROP
45
46 #
47 # insert accept rule or to jump to new accept-check table here
48 #
49 iptables -A INPUT -j input_rule
50 [ -z "$WAN" ] || iptables -A INPUT -i $WAN -j input_wan
51
52 # allow
53 iptables -A INPUT -j LAN_ACCEPT # allow from lan/wifi interfaces
54 iptables -A INPUT -p icmp -j ACCEPT # allow ICMP
55 iptables -A INPUT -p gre -j ACCEPT # allow GRE
56
57 # reject (what to do with anything not allowed earlier)
58 iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
59 iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
60
61 ### OUTPUT
62 ### (connections with the router as source)
63
64 # base case
65 iptables -P OUTPUT DROP
66 iptables -A OUTPUT -m state --state INVALID -j DROP
67 iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
68
69 #
70 # insert accept rule or to jump to new accept-check table here
71 #
72 iptables -A OUTPUT -j output_rule
73
74 # allow
75 iptables -A OUTPUT -j ACCEPT #allow everything out
76
77 # reject (what to do with anything not allowed earlier)
78 iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
79 iptables -A OUTPUT -j REJECT --reject-with icmp-port-unreachable
80
81 ### FORWARDING
82 ### (connections routed through the router)
83
84 # base case
85 iptables -P FORWARD DROP
86 iptables -A FORWARD -m state --state INVALID -j DROP
87 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
88 iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
89
90 #
91 # insert accept rule or to jump to new accept-check table here
92 #
93 iptables -A FORWARD -j forwarding_rule
94 [ -z "$WAN" ] || iptables -A FORWARD -i $WAN -j forwarding_wan
95
96 # allow
97 iptables -A FORWARD -i $LAN -o $LAN -j ACCEPT
98 [ -z "$WAN" ] || iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
99
100 # reject (what to do with anything not allowed earlier)
101 # uses the default -P DROP
102
103 ### MASQ
104 iptables -t nat -A PREROUTING -m state --state NEW -p tcp -j NEW
105 iptables -t nat -A PREROUTING -j prerouting_rule
106 [ -z "$WAN" ] || iptables -t nat -A PREROUTING -i "$WAN" -j prerouting_wan
107 iptables -t nat -A POSTROUTING -j postrouting_rule
108 [ -z "$WAN" ] || iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
109
110 iptables -t nat -A NEW -m limit --limit 50 --limit-burst 100 -j RETURN && \
111 iptables -t nat -A NEW -j DROP
112
113 ## USER RULES
114 [ -f /etc/firewall.user ] && . /etc/firewall.user
115 [ -n "$WAN" -a -e /etc/config/firewall ] && {
116 export WAN
117 awk -f /usr/lib/common.awk -f /usr/lib/firewall.awk /etc/config/firewall | ash
118 }
119 }
120
121 stop() {
122 iptables -P INPUT ACCEPT
123 iptables -P OUTPUT ACCEPT
124 iptables -P FORWARD ACCEPT
125 iptables -F
126 iptables -X
127 iptables -t nat -P PREROUTING ACCEPT
128 iptables -t nat -P POSTROUTING ACCEPT
129 iptables -t nat -P OUTPUT ACCEPT
130 iptables -t nat -F
131 iptables -t nat -X
132 }
This page took 0.049867 seconds and 5 git commands to generate.