[packages] firewall: fix nat reflection after netifd status format change
[openwrt/svn-archive/archive.git] / package / firewall / files / reflection.hotplug
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/network.sh
5
6 if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
7 local wanip
8 network_get_ipaddr wanip wan || return
9
10 iptables -t nat -F nat_reflection_in 2>/dev/null || {
11 iptables -t nat -N nat_reflection_in
12 iptables -t nat -A prerouting_rule -j nat_reflection_in
13 }
14
15 iptables -t nat -F nat_reflection_out 2>/dev/null || {
16 iptables -t nat -N nat_reflection_out
17 iptables -t nat -A postrouting_rule -j nat_reflection_out
18 }
19
20 iptables -t filter -F nat_reflection_fwd 2>/dev/null || {
21 iptables -t filter -N nat_reflection_fwd
22 iptables -t filter -A forwarding_rule -j nat_reflection_fwd
23 }
24
25 find_networks() {
26 find_networks_cb() {
27 local cfg="$1"
28 local zone="$2"
29
30 local name
31 config_get name "$cfg" name
32
33 [ "$name" = "$zone" ] && {
34 local network
35 config_get network "$cfg" network
36
37 echo ${network:-$zone}
38 return 1
39 }
40 }
41
42 config_foreach find_networks_cb zone "$1"
43 }
44
45 setup_fwd() {
46 local cfg="$1"
47
48 local reflection
49 config_get_bool reflection "$cfg" reflection 1
50 [ "$reflection" == 1 ] || return
51
52 local src
53 config_get src "$cfg" src
54
55 local target
56 config_get target "$cfg" target DNAT
57
58 [ "$src" = wan ] && [ "$target" = DNAT ] && {
59 local dest
60 config_get dest "$cfg" dest "lan"
61 [ "$dest" != "*" ] || return
62
63 local net
64 for net in $(find_networks "$dest"); do
65 local lannet
66 network_get_subnet lannet "$net" || return
67
68 local proto
69 config_get proto "$cfg" proto
70
71 local epmin epmax extport
72 config_get extport "$cfg" src_dport
73 [ -n "$extport" ] || return
74
75 epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
76 [ "${epmin#!}" != "$epmax" ] || epmax=""
77
78 local ipmin ipmax intport
79 config_get intport "$cfg" dest_port "$extport"
80
81 ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
82 [ "${ipmin#!}" != "$ipmax" ] || ipmax=""
83
84 local exthost
85 config_get exthost "$cfg" src_dip "$wanip"
86
87 local inthost
88 config_get inthost "$cfg" dest_ip
89 [ -n "$inthost" ] || return
90
91 [ "$proto" = tcpudp ] && proto="tcp udp"
92
93 [ "${inthost#!}" = "$inthost" ] || return 0
94 [ "${exthost#!}" = "$exthost" ] || return 0
95
96 [ "${epmin#!}" != "$epmin" ] && \
97 extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
98 extport="--dport $epmin${epmax:+:$epmax}"
99
100 [ "${ipmin#!}" != "$ipmin" ] && \
101 intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
102 intport="--dport $ipmin${ipmax:+:$ipmax}"
103
104 local p
105 for p in ${proto:-tcp udp}; do
106 case "$p" in
107 tcp|udp|6|17)
108 iptables -t nat -A nat_reflection_in \
109 -s $lannet -d $exthost \
110 -p $p $extport \
111 -j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
112
113 iptables -t nat -A nat_reflection_out \
114 -s $lannet -d $inthost \
115 -p $p $intport \
116 -j SNAT --to-source ${lannet%%/*}
117
118 iptables -t filter -A nat_reflection_fwd \
119 -s $lannet -d $inthost \
120 -p $p $intport \
121 -j ACCEPT
122 ;;
123 esac
124 done
125 done
126 }
127 }
128
129 config_load firewall
130 config_foreach setup_fwd redirect
131 fi