#!/bin/sh
-. /etc/functions.sh
+. /lib/functions.sh
+. /lib/functions/network.sh
if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
- local wanip=$(uci -P/var/state get network.wan.ipaddr)
+ local wanip
+ network_get_ipaddr wanip wan || return
iptables -t nat -F nat_reflection_in 2>/dev/null || {
iptables -t nat -N nat_reflection_in
local net
for net in $(find_networks "$dest"); do
- local lanip=$(uci -P/var/state get network.$net.ipaddr)
- local lanmk=$(uci -P/var/state get network.$net.netmask)
+ local lannet
+ network_get_subnet lannet "$net" || return
local proto
config_get proto "$cfg" proto
local epmin epmax extport
- config_get extport "$cfg" src_dport
+ config_get extport "$cfg" src_dport "1-65535"
[ -n "$extport" ] || return
epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
config_get inthost "$cfg" dest_ip
[ -n "$inthost" ] || return
+ [ "$proto" = all ] && proto="tcp udp"
[ "$proto" = tcpudp ] && proto="tcp udp"
[ "${inthost#!}" = "$inthost" ] || return 0
case "$p" in
tcp|udp|6|17)
iptables -t nat -A nat_reflection_in \
- -s $lanip/$lanmk -d $exthost \
+ -s $lannet -d $exthost \
-p $p $extport \
-j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
iptables -t nat -A nat_reflection_out \
- -s $lanip/$lanmk -d $inthost \
+ -s $lannet -d $inthost \
-p $p $intport \
- -j SNAT --to-source $lanip
+ -j SNAT --to-source ${lannet%%/*}
iptables -t filter -A nat_reflection_fwd \
- -s $lanip/$lanmk -d $inthost \
+ -s $lannet -d $inthost \
-p $p $intport \
-j ACCEPT
;;