4 . /lib/functions/network.sh
6 if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
8 network_get_ipaddr wanip wan || return
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
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
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
31 config_get name "$cfg" name
33 [ "$name" = "$zone" ] && {
35 config_get network "$cfg" network
37 echo ${network:-$zone}
42 config_foreach find_networks_cb zone "$1"
49 config_get_bool reflection "$cfg" reflection 1
50 [ "$reflection" == 1 ] || return
53 config_get src "$cfg" src
56 config_get target "$cfg" target DNAT
58 [ "$src" = wan ] && [ "$target" = DNAT ] && {
60 config_get dest "$cfg" dest "lan"
61 [ "$dest" != "*" ] || return
64 for net in $(find_networks "$dest"); do
66 network_get_subnet lannet "$net" || return
69 config_get proto "$cfg" proto
71 local epmin epmax extport
72 config_get extport "$cfg" src_dport "1-65535"
73 [ -n "$extport" ] || return
75 epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
76 [ "${epmin#!}" != "$epmax" ] || epmax=""
78 local ipmin ipmax intport
79 config_get intport "$cfg" dest_port "$extport"
81 ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
82 [ "${ipmin#!}" != "$ipmax" ] || ipmax=""
85 config_get exthost "$cfg" src_dip "$wanip"
88 config_get inthost "$cfg" dest_ip
89 [ -n "$inthost" ] || return
91 [ "$proto" = all ] && proto="tcp udp"
92 [ "$proto" = tcpudp ] && proto="tcp udp"
94 [ "${inthost#!}" = "$inthost" ] || return 0
95 [ "${exthost#!}" = "$exthost" ] || return 0
97 [ "${epmin#!}" != "$epmin" ] && \
98 extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
99 extport="--dport $epmin${epmax:+:$epmax}"
101 [ "${ipmin#!}" != "$ipmin" ] && \
102 intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
103 intport="--dport $ipmin${ipmax:+:$ipmax}"
106 for p in ${proto:-tcp udp}; do
109 iptables -t nat -A nat_reflection_in \
110 -s $lannet -d $exthost \
112 -j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
114 iptables -t nat -A nat_reflection_out \
115 -s $lannet -d $inthost \
117 -j SNAT --to-source ${lannet%%/*}
119 iptables -t filter -A nat_reflection_fwd \
120 -s $lannet -d $inthost \
131 config_foreach setup_fwd redirect