1 # Copyright (C) 2009-2010 OpenWrt.org
3 fw_config_get_redirect
() {
4 [ "${redirect_NAME}" != "$1" ] ||
return
5 fw_config_get_section
"$1" redirect
{ \
17 string proto
"tcpudp" \
19 string target
"DNAT" \
22 [ -n "$redirect_name" ] || redirect_name
=$redirect__name
26 fw_config_get_redirect
"$1"
28 fw_callback pre redirect
30 local fwdchain natchain natopt nataddr natports srcdaddr srcdports
31 if [ "$redirect_target" == "DNAT" ]; then
32 [ -n "${redirect_src#*}" -a -n "$redirect_dest_ip$redirect_dest_port" ] ||
{
33 fw_log error
"DNAT redirect ${redirect_name}: needs src and dest_ip or dest_port, skipping"
37 fwdchain
="zone_${redirect_src}${redirect_dest_ip:+_forward}"
39 natopt
="--to-destination"
40 natchain
="zone_${redirect_src}_prerouting"
41 nataddr
="$redirect_dest_ip"
42 fw_get_port_range natports
"${redirect_dest_port#!}" "-"
44 fw_get_negation srcdaddr
'-d' "${redirect_src_dip:+$redirect_src_dip/$redirect_src_dip_prefixlen}"
45 fw_get_port_range srcdports
"$redirect_src_dport" ":"
46 fw_get_negation srcdports
'--dport' "$srcdports"
48 list_contains FW_CONNTRACK_ZONES
$redirect_src || \
49 append FW_CONNTRACK_ZONES
$redirect_src
51 elif [ "$redirect_target" == "SNAT" ]; then
52 [ -n "${redirect_dest#*}" -a -n "$redirect_src_dip" ] ||
{
53 fw_log error
"SNAT redirect ${redirect_name}: needs dest and src_dip, skipping"
57 fwdchain
="${redirect_src:+zone_${redirect_src}_forward}"
60 natchain
="zone_${redirect_dest}_nat"
61 nataddr
="$redirect_src_dip"
62 fw_get_port_range natports
"${redirect_src_dport#!}" "-"
64 fw_get_negation srcdaddr
'-d' "${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
65 fw_get_port_range srcdports
"$redirect_dest_port" ":"
66 fw_get_negation srcdports
'--dport' "$srcdports"
68 list_contains FW_CONNTRACK_ZONES
$redirect_dest || \
69 append FW_CONNTRACK_ZONES
$redirect_dest
72 fw_log error
"redirect ${redirect_name}: target must be either DNAT or SNAT, skipping"
77 fw_get_family_mode mode
${redirect_family:-x} ${redirect_src:-$redirect_dest} I
80 fw_get_negation srcaddr
'-s' "${redirect_src_ip:+$redirect_src_ip/$redirect_src_ip_prefixlen}"
83 fw_get_port_range srcports
"$redirect_src_port" ":"
84 fw_get_negation srcports
'--sport' "$srcports"
87 fw_get_negation destaddr
'-d' "${redirect_dest_ip:+$redirect_dest_ip/$redirect_dest_ip_prefixlen}"
90 fw_get_port_range destports
"${redirect_dest_port:-$redirect_src_dport}" ":"
91 fw_get_negation destports
'--dport' "$destports"
93 [ "$redirect_proto" == "tcpudp" ] && redirect_proto
="tcp udp"
94 local pr; for pr in $redirect_proto; do
95 fw_get_negation
pr '-p' "$pr"
96 local sm
; for sm
in ${redirect_src_mac:-""}; do
97 fw_get_negation sm
'--mac-source' "$sm"
98 fw add
$mode n
$natchain $redirect_target + \
99 { $redirect_src_ip $redirect_dest_ip } { \
100 $srcaddr $srcdaddr $pr \
101 $srcports $srcdports \
103 $natopt $nataddr${natports:+:$natports} \
107 fw add
$mode f
${fwdchain:-forward} ACCEPT
+ \
108 { $redirect_src_ip $redirect_dest_ip } { \
109 $srcaddr ${destaddr:--m conntrack --ctstate DNAT} \
111 $srcports $destports \
118 fw_callback post redirect