2 # Copyright (C) 2008 John Crispin <blogic@openwrt.org>
6 IPTABLES="echo iptables"
17 ZONE_LIST=$CONFIG_SECTION
24 config_get input $1 input
25 config_get output $1 output
26 config_get forward $1 forward
28 [ -z "$input" ] && input=$DEF_INPUT
29 [ -z "$output" ] && output=$DEF_OUTPUT
30 [ -z "$forward" ] && forward=$DEF_FORWARD
36 [ "$1" == "loopback" ] && return
38 config_get exists $ZONE_LIST $1
39 [ -n "$exists" ] && return
40 config_set $ZONE_LIST $1 1
43 $IPTABLES -N zone_$1_ACCEPT
44 $IPTABLES -N zone_$1_DROP
45 $IPTABLES -N zone_$1_REJECT
46 $IPTABLES -N zone_$1_forward
47 $IPTABLES -A zone_$1_forward -j zone_$1_$5
48 $IPTABLES -A zone_$1 -j zone_$1_$3
49 $IPTABLES -A OUTPUT -j zone_$1_$4
50 $IPTABLES -N zone_$1_nat -t nat
51 $IPTABLES -N zone_$1_prerouting -t nat
52 [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$2_nat
56 logger "adding $1 to firewall zone $2"
57 $IPTABLES -A INPUT -i $1 -j zone_$2
58 $IPTABLES -I zone_$2_ACCEPT 1 -o $1 -j ACCEPT
59 $IPTABLES -I zone_$2_DROP 1 -o $1 -j DROP
60 $IPTABLES -I zone_$2_REJECT 1 -o $1 -j REJECT
61 $IPTABLES -I zone_$2_ACCEPT 1 -i $1 -j ACCEPT
62 $IPTABLES -I zone_$2_DROP 1 -i $1 -j DROP
63 $IPTABLES -I zone_$2_REJECT 1 -i $1 -j REJECT
64 $IPTABLES -I zone_$2_nat 1 -t nat -o $1 -j MASQUERADE
65 $IPTABLES -I PREROUTING 1 -t nat -i $1 -j zone_$2_prerouting
66 $IPTABLES -A FORWARD -i $1 -j zone_$2_forward
70 logger "removing $1 from firewall zone $2"
71 $IPTABLES -D INPUT -i $1 -j zone_$2
72 $IPTABLES -D zone_$2_ACCEPT -o $1 -j ACCEPT
73 $IPTABLES -D zone_$2_DROP -o $1 -j DROP
74 $IPTABLES -D zone_$2_REJECT -o $1 -j REJECT
75 $IPTABLES -D zone_$2_ACCEPT -i $1 -j ACCEPT
76 $IPTABLES -D zone_$2_DROP -i $1 -j DROP
77 $IPTABLES -D zone_$2_REJECT -i $1 -j REJECT
78 $IPTABLES -D zone_$2_nat -t nat -o $1 -j MASQUERADE
79 $IPTABLES -D PREROUTING -t nat -i $1 -j zone_$2_prerouting
80 $IPTABLES -D FORWARD -i $1 -j zone_$2_forward
84 echo "Loading synflood protection"
85 $IPTABLES -N SYN_FLOOD
86 $IPTABLES -A SYN_FLOOD -p tcp --syn -m limit --limit ${1}/second --limit-burst $2 -j RETURN
87 $IPTABLES -A SYN_FLOOD -p ! tcp -j RETURN
88 $IPTABLES -A SYN_FLOOD -p tcp ! --syn -j RETURN
89 $IPTABLES -A SYN_FLOOD -j LOG --log-prefix "syn_flood: "
90 $IPTABLES -A SYN_FLOOD -j DROP
91 $IPTABLES -A INPUT -p tcp --syn -j SYN_FLOOD
100 echo 1 > /proc/sys/net/ipv4/tcp_syncookies
101 for f in /proc/sys/net/ipv4/conf/*/accept_redirects
105 for f in /proc/sys/net/ipv4/conf/*/accept_source_route
112 $IPTABLES -t mangle -F
116 $IPTABLES -P INPUT $input
117 $IPTABLES -A INPUT -m state --state INVALID -j DROP
118 $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
120 $IPTABLES -P OUTPUT $output
121 $IPTABLES -A OUTPUT -m state --state INVALID -j DROP
122 $IPTABLES -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
124 $IPTABLES -P FORWARD $forward
125 $IPTABLES -A FORWARD -m state --state INVALID -j DROP
126 $IPTABLES -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
127 $IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
129 $IPTABLES -A INPUT -i lo -j ACCEPT
130 $IPTABLES -A OUTPUT -o lo -j ACCEPT
132 config_get syn_flood $1 syn_flood
133 config_get syn_rate $1 syn_rate
134 config_get syn_burst $1 syn_burst
136 [ -z "$syn_rate" ] && syn_rate=25
137 [ -z "$syn_burst" ] && syn_burst=50
138 [ "$syn_flood" == "1" ] && load_synflood $syn_rate $syn_burst
146 config_get name $1 name
147 config_get network $1 network
148 config_get masq $1 masq
151 [ -z "$network" ] && network=$name
152 create_zone "$name" "$network" "$input" "$output" "$forward" "$masq"
168 config_get src $1 src
169 config_get src_ip $1 src_ip
170 config_get src_mac $1 src_mac
171 config_get src_port $1 src_port
172 config_get dest $1 dest
173 config_get dest_ip $1 dest_ip
174 config_get dest_port $1 dest_port
175 config_get proto $1 proto
176 config_get target $1 target
177 config_get ruleset $1 ruleset
179 [ -z "$target" ] && target=DROP
180 [ -n "$src" ] && ZONE=zone_$src || ZONE=INPUT
181 [ -n "$dest" ] && TARGET=zone_${dest}_$target || TARGET=$target
182 [ -n "$dest_port" -a -z "$proto" ] && { \
183 echo "dport may only be used it proto is defined"; return; }
184 [ -n "$src_port" -a -z "$proto" ] && { \
185 echo "sport may only be used it proto is defined"; return; }
186 $IPTABLES -I $ZONE 1 \
187 ${proto:+-p $proto} \
188 ${src_ip:+-s $src_ip} \
189 ${src_port:+--sport $src_port} \
190 ${src_mac:+-m mac --mac-source $src_mac} \
191 ${dest_ip:+-d $dest_ip} \
192 ${dest_port:+--dport $dest_port} \
201 config_get src $1 src
202 config_get dest $1 dest
203 [ -n "$src" ] && z_src=zone_${src}_forward || z_src=FORWARD
204 [ -n "$dest" ] && z_dest=zone_${dest}_ACCEPT || z_dest=ACCEPT
205 $IPTABLES -I $z_src 1 -j $z_dest
218 config_get src $1 src
219 config_get src_ip $1 src_ip
220 config_get src_port $1 src_port
221 config_get src_dport $1 src_dport
222 config_get src_mac $1 src_mac
223 config_get dest_ip $1 dest_ip
224 config_get dest_port $1 dest_port
225 config_get proto $1 proto
226 [ -z "$src" -o -z "$dest_ip" ] && { \
227 echo "redirect needs src and dest_ip"; return ; }
228 [ -n "$dest_port" -a -z "$proto" ] && { \
229 echo "dport may only be used it proto is defined"; return; }
230 [ -n "$src_port" -a -z "$proto" ] && { \
231 echo "sport may only be used it proto is defined"; return; }
232 $IPTABLES -A zone_${src}_prerouting -t nat \
233 ${proto:+-p $proto} \
234 ${src_ip:+-s $src_ip} \
235 ${src_port:+--sport $src_port} \
236 ${src_dport:+--dport $src_dport} \
237 ${src_mac:+-m mac --mac-source $src_mac} \
238 -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
239 $IPTABLES -I zone_${src}_forward 1 \
240 ${proto:+-p $proto} \
242 ${src_ip:+-s $src_ip} \
243 ${src_port:+--sport $src_port} \
244 ${dest_port:+--dport $dest_port} \
245 ${src_mac:+-m mac --mac-source $src_mac} \
251 config_get path $1 path
252 [ -e $path ] && . $path
259 config_get ifname $1 ifname
260 [ -n "$up" ] || return 0
261 (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
265 $IPTABLES -N input_rule
266 $IPTABLES -N output_rule
267 $IPTABLES -N forward_rule
268 $IPTABLES -N prerouting_rule -t nat
269 $IPTABLES -N postrouting_rule -t nat
270 $IPTABLES -A INPUT -j input_rule
271 $IPTABLES -A OUTPUT -j output_rule
272 $IPTABLES -A FORWARD -j forward_rule
273 $IPTABLES -A PREROUTING -t nat -j prerouting_rule
274 $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
278 echo "Loading defaults"
279 config_foreach fw_defaults defaults
281 config_foreach fw_zone zone
283 config_foreach fw_rule rule
284 echo "Loading forwarding"
285 config_foreach fw_forwarding forwarding
286 echo "Loading redirects"
287 config_foreach fw_redirect redirect
288 echo "Loading includes"
289 config_foreach fw_include include
290 echo "Adding custom chains"
293 uci_set_state firewall core "" firewall_state
294 uci_set_state firewall core loaded 1
297 config_foreach fw_addif interface
303 $IPTABLES -t mangle -F
306 $IPTABLES -P INPUT ACCEPT
307 $IPTABLES -P OUTPUT ACCEPT
308 $IPTABLES -P FORWARD ACCEPT