2010-07-31 13:06:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
. /etc/functions.sh
|
2012-05-16 13:03:54 +00:00
|
|
|
. /usr/share/libubox/jshn.sh
|
|
|
|
|
|
|
|
find_iface_address()
|
|
|
|
{
|
|
|
|
local iface="$1"
|
|
|
|
local ipaddr="$2"
|
|
|
|
local prefix="$3"
|
|
|
|
|
|
|
|
local tmp="$(ubus call network.interface."$iface" status 2>/dev/null)"
|
|
|
|
|
|
|
|
json_load "${tmp:-{}}"
|
|
|
|
json_get_type tmp address
|
|
|
|
|
|
|
|
if [ "$tmp" = array ]; then
|
|
|
|
|
|
|
|
json_select address
|
|
|
|
json_get_type tmp 1
|
|
|
|
|
|
|
|
if [ "$tmp" = object ]; then
|
|
|
|
|
|
|
|
json_select 1
|
|
|
|
[ -n "$ipaddr" ] && json_get_var "$ipaddr" address
|
|
|
|
[ -n "$prefix" ] && json_get_var "$prefix" mask
|
|
|
|
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-09-04 15:49:13 +00:00
|
|
|
if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
|
2012-05-16 13:03:54 +00:00
|
|
|
local wanip
|
|
|
|
find_iface_address wan wanip
|
|
|
|
[ -n "$wanip" ] || return
|
2010-07-31 13:06:14 +00:00
|
|
|
|
|
|
|
iptables -t nat -F nat_reflection_in 2>/dev/null || {
|
|
|
|
iptables -t nat -N nat_reflection_in
|
|
|
|
iptables -t nat -A prerouting_rule -j nat_reflection_in
|
|
|
|
}
|
|
|
|
|
|
|
|
iptables -t nat -F nat_reflection_out 2>/dev/null || {
|
|
|
|
iptables -t nat -N nat_reflection_out
|
|
|
|
iptables -t nat -A postrouting_rule -j nat_reflection_out
|
|
|
|
}
|
|
|
|
|
2010-10-03 18:11:59 +00:00
|
|
|
iptables -t filter -F nat_reflection_fwd 2>/dev/null || {
|
|
|
|
iptables -t filter -N nat_reflection_fwd
|
|
|
|
iptables -t filter -A forwarding_rule -j nat_reflection_fwd
|
|
|
|
}
|
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
find_networks() {
|
|
|
|
find_networks_cb() {
|
|
|
|
local cfg="$1"
|
|
|
|
local zone="$2"
|
|
|
|
|
|
|
|
local name
|
|
|
|
config_get name "$cfg" name
|
|
|
|
|
|
|
|
[ "$name" = "$zone" ] && {
|
|
|
|
local network
|
|
|
|
config_get network "$cfg" network
|
|
|
|
|
|
|
|
echo ${network:-$zone}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config_foreach find_networks_cb zone "$1"
|
|
|
|
}
|
2010-10-03 18:11:59 +00:00
|
|
|
|
2010-07-31 13:06:14 +00:00
|
|
|
setup_fwd() {
|
|
|
|
local cfg="$1"
|
|
|
|
|
2010-10-03 18:11:59 +00:00
|
|
|
local reflection
|
|
|
|
config_get_bool reflection "$cfg" reflection 1
|
|
|
|
[ "$reflection" == 1 ] || return
|
|
|
|
|
2010-07-31 13:06:14 +00:00
|
|
|
local src
|
|
|
|
config_get src "$cfg" src
|
|
|
|
|
2010-09-05 19:03:17 +00:00
|
|
|
local target
|
|
|
|
config_get target "$cfg" target DNAT
|
|
|
|
|
|
|
|
[ "$src" = wan ] && [ "$target" = DNAT ] && {
|
2010-07-31 13:06:14 +00:00
|
|
|
local dest
|
|
|
|
config_get dest "$cfg" dest "lan"
|
2011-06-30 01:31:23 +00:00
|
|
|
[ "$dest" != "*" ] || return
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local net
|
|
|
|
for net in $(find_networks "$dest"); do
|
2012-05-16 13:03:54 +00:00
|
|
|
local lanip lanmk
|
|
|
|
find_iface_address "$net" lanip lanmk
|
|
|
|
[ -n "$lanip" ] || return
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local proto
|
|
|
|
config_get proto "$cfg" proto
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local epmin epmax extport
|
|
|
|
config_get extport "$cfg" src_dport
|
|
|
|
[ -n "$extport" ] || return
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
|
2011-07-01 11:50:48 +00:00
|
|
|
[ "${epmin#!}" != "$epmax" ] || epmax=""
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local ipmin ipmax intport
|
|
|
|
config_get intport "$cfg" dest_port "$extport"
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
|
2011-07-01 11:50:48 +00:00
|
|
|
[ "${ipmin#!}" != "$ipmax" ] || ipmax=""
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local exthost
|
|
|
|
config_get exthost "$cfg" src_dip "$wanip"
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local inthost
|
|
|
|
config_get inthost "$cfg" dest_ip
|
|
|
|
[ -n "$inthost" ] || return
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
[ "$proto" = tcpudp ] && proto="tcp udp"
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-09-28 11:11:11 +00:00
|
|
|
[ "${inthost#!}" = "$inthost" ] || return 0
|
|
|
|
[ "${exthost#!}" = "$exthost" ] || return 0
|
|
|
|
|
2011-07-01 11:48:14 +00:00
|
|
|
[ "${epmin#!}" != "$epmin" ] && \
|
|
|
|
extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
|
|
|
|
extport="--dport $epmin${epmax:+:$epmax}"
|
|
|
|
|
|
|
|
[ "${ipmin#!}" != "$ipmin" ] && \
|
|
|
|
intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
|
|
|
|
intport="--dport $ipmin${ipmax:+:$ipmax}"
|
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
local p
|
|
|
|
for p in ${proto:-tcp udp}; do
|
|
|
|
case "$p" in
|
2011-08-14 00:33:29 +00:00
|
|
|
tcp|udp|6|17)
|
2010-07-31 13:25:56 +00:00
|
|
|
iptables -t nat -A nat_reflection_in \
|
|
|
|
-s $lanip/$lanmk -d $exthost \
|
2011-07-01 11:48:14 +00:00
|
|
|
-p $p $extport \
|
|
|
|
-j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
|
2010-07-31 13:06:14 +00:00
|
|
|
|
2010-07-31 13:25:56 +00:00
|
|
|
iptables -t nat -A nat_reflection_out \
|
|
|
|
-s $lanip/$lanmk -d $inthost \
|
2011-07-01 11:48:14 +00:00
|
|
|
-p $p $intport \
|
2010-07-31 13:25:56 +00:00
|
|
|
-j SNAT --to-source $lanip
|
2010-10-03 18:11:59 +00:00
|
|
|
|
|
|
|
iptables -t filter -A nat_reflection_fwd \
|
|
|
|
-s $lanip/$lanmk -d $inthost \
|
2011-07-01 11:48:14 +00:00
|
|
|
-p $p $intport \
|
2010-10-03 18:11:59 +00:00
|
|
|
-j ACCEPT
|
2010-07-31 13:25:56 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2010-07-31 13:06:14 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config_load firewall
|
|
|
|
config_foreach setup_fwd redirect
|
|
|
|
fi
|