[backfire] merge r22908
[openwrt-10.03/.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         find_networks() {
19                 find_networks_cb() {
20                         local cfg="$1"
21                         local zone="$2"
22
23                         local name
24                         config_get name "$cfg" name
25
26                         [ "$name" = "$zone" ] && {
27                                 local network
28                                 config_get network "$cfg" network
29
30                                 echo ${network:-$zone}
31                                 return 1
32                         }
33                 }
34
35                 config_foreach find_networks_cb zone "$1"
36         }
37         
38         setup_fwd() {
39                 local cfg="$1"
40
41                 local src
42                 config_get src "$cfg" src
43
44                 [ "$src" = wan ] && {
45                         local dest
46                         config_get dest "$cfg" dest "lan"
47
48                         local net
49                         for net in $(find_networks "$dest"); do
50                                 local lanip=$(uci -P/var/state get network.$net.ipaddr)
51                                 local lanmk=$(uci -P/var/state get network.$net.netmask)
52
53                                 local proto
54                                 config_get proto "$cfg" proto
55
56                                 local reflection
57                                 config_get_bool reflection "$cfg" reflection 1
58                                 [ "$reflection" == 1 ] || return
59
60                                 local epmin epmax extport
61                                 config_get extport "$cfg" src_dport
62                                 [ -n "$extport" ] || return
63
64                                 epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
65                                 [ "$epmin" != "$epmax" ] || epmax=""
66
67                                 local ipmin ipmax intport
68                                 config_get intport "$cfg" dest_port "$extport"
69
70                                 ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
71                                 [ "$ipmin" != "$ipmax" ] || ipmax=""
72
73                                 local exthost
74                                 config_get exthost "$cfg" src_dip "$wanip"
75
76                                 local inthost
77                                 config_get inthost "$cfg" dest_ip
78                                 [ -n "$inthost" ] || return
79
80                                 [ "$proto" = tcpudp ] && proto="tcp udp"
81
82                                 local p
83                                 for p in ${proto:-tcp udp}; do
84                                         case "$p" in
85                                                 tcp|udp)
86                                                         iptables -t nat -A nat_reflection_in \
87                                                                 -s $lanip/$lanmk -d $exthost \
88                                                                 -p $p --dport $epmin${epmax:+:$epmax} \
89                                                                 -j DNAT --to $inthost:$ipmin${ipmax:+-$ipmax}
90
91                                                         iptables -t nat -A nat_reflection_out \
92                                                                 -s $lanip/$lanmk -d $inthost \
93                                                                 -p $p --dport $ipmin${ipmax:+:$ipmax} \
94                                                                 -j SNAT --to-source $lanip
95                                                 ;;
96                                         esac
97                                 done
98                         done
99                 }
100         }
101
102         config_load firewall
103         config_foreach setup_fwd redirect
104 fi
105