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