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