[package] firewall:
[openwrt/svn-archive/archive.git] / package / firewall / files / reflection.hotplug
1 #!/bin/sh
2
3 . /etc/functions.sh
4
5 if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
6 local wanip=$(uci -P/var/state get network.wan.ipaddr)
7
8 iptables -t nat -F nat_reflection_in 2>/dev/null || {
9 iptables -t nat -N nat_reflection_in
10 iptables -t nat -A prerouting_rule -j nat_reflection_in
11 }
12
13 iptables -t nat -F nat_reflection_out 2>/dev/null || {
14 iptables -t nat -N nat_reflection_out
15 iptables -t nat -A postrouting_rule -j nat_reflection_out
16 }
17
18 iptables -t filter -F nat_reflection_fwd 2>/dev/null || {
19 iptables -t filter -N nat_reflection_fwd
20 iptables -t filter -A forwarding_rule -j nat_reflection_fwd
21 }
22
23 find_networks() {
24 find_networks_cb() {
25 local cfg="$1"
26 local zone="$2"
27
28 local name
29 config_get name "$cfg" name
30
31 [ "$name" = "$zone" ] && {
32 local network
33 config_get network "$cfg" network
34
35 echo ${network:-$zone}
36 return 1
37 }
38 }
39
40 config_foreach find_networks_cb zone "$1"
41 }
42
43 setup_fwd() {
44 local cfg="$1"
45
46 local reflection
47 config_get_bool reflection "$cfg" reflection 1
48 [ "$reflection" == 1 ] || return
49
50 local src
51 config_get src "$cfg" src
52
53 local target
54 config_get target "$cfg" target DNAT
55
56 [ "$src" = wan ] && [ "$target" = DNAT ] && {
57 local dest
58 config_get dest "$cfg" dest "lan"
59 [ "$dest" != "*" ] || return
60
61 local net
62 for net in $(find_networks "$dest"); do
63 local lanip=$(uci -P/var/state get network.$net.ipaddr)
64 local lanmk=$(uci -P/var/state get network.$net.netmask)
65
66 local proto
67 config_get proto "$cfg" proto
68
69 local epmin epmax extport
70 config_get extport "$cfg" src_dport
71 [ -n "$extport" ] || return
72
73 epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
74 [ "$epmin" != "$epmax" ] || epmax=""
75
76 local ipmin ipmax intport
77 config_get intport "$cfg" dest_port "$extport"
78
79 ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
80 [ "$ipmin" != "$ipmax" ] || ipmax=""
81
82 local exthost
83 config_get exthost "$cfg" src_dip "$wanip"
84
85 local inthost
86 config_get inthost "$cfg" dest_ip
87 [ -n "$inthost" ] || return
88
89 [ "$proto" = tcpudp ] && proto="tcp udp"
90
91 [ "${inthost#!}" = "$inthost" ] || return 0
92 [ "${exthost#!}" = "$exthost" ] || return 0
93
94 local p
95 for p in ${proto:-tcp udp}; do
96 case "$p" in
97 tcp|udp)
98 iptables -t nat -A nat_reflection_in \
99 -s $lanip/$lanmk -d $exthost \
100 -p $p --dport $epmin${epmax:+:$epmax} \
101 -j DNAT --to $inthost:$ipmin${ipmax:+-$ipmax}
102
103 iptables -t nat -A nat_reflection_out \
104 -s $lanip/$lanmk -d $inthost \
105 -p $p --dport $ipmin${ipmax:+:$ipmax} \
106 -j SNAT --to-source $lanip
107
108 iptables -t filter -A nat_reflection_fwd \
109 -s $lanip/$lanmk -d $inthost \
110 -p $p --dport $ipmin${ipmax:+:$ipmax} \
111 -j ACCEPT
112 ;;
113 esac
114 done
115 done
116 }
117 }
118
119 config_load firewall
120 config_foreach setup_fwd redirect
121 fi