0f0ccffe0000bbb443f7cb08a50345ff05300a41
[openwrt/svn-archive/archive.git] / package / firewall / files / lib / core_redirect.sh
1 # Copyright (C) 2009-2010 OpenWrt.org
2
3 fw_config_get_redirect() {
4 [ "${redirect_NAME}" != "$1" ] || return
5 fw_config_get_section "$1" redirect { \
6 string _name "$1" \
7 string name "" \
8 string src "" \
9 ipaddr src_ip "" \
10 ipaddr src_dip "" \
11 string src_mac "" \
12 string src_port "" \
13 string src_dport "" \
14 string dest "" \
15 ipaddr dest_ip "" \
16 string dest_mac "" \
17 string dest_port "" \
18 string proto "tcpudp" \
19 } || return
20 [ -n "$redirect_name" ] || redirect_name=$redirect__name
21 }
22
23 fw_load_redirect() {
24 fw_config_get_redirect "$1"
25
26 fw_callback pre redirect
27
28 [ -n "$redirect_src" -a -n "$redirect_dest_ip" ] || {
29 fw_die "redirect ${redirect_name}: needs src and dest_ip"
30 }
31
32 local nat_dest_port=$redirect_dest_port
33 redirect_dest_port=$(fw_get_port_range $redirect_dest_port)
34 redirect_src_port=$(fw_get_port_range $redirect_src_port)
35 redirect_src_dport=$(fw_get_port_range $redirect_src_dport)
36 local fwd_dest_port=${redirect_dest_port:-$redirect_src_dport}
37
38 [ "$redirect_proto" == "tcpudp" ] && redirect_proto="tcp udp"
39 for redirect_proto in $redirect_proto; do
40 fw add I n zone_${redirect_src}_prerouting DNAT $ { $redirect_src_ip $redirect_dest_ip } { \
41 ${redirect_proto:+-p $redirect_proto} \
42 ${redirect_src_ip:+-s $redirect_src_ip} \
43 ${redirect_src_dip:+-d $redirect_src_dip} \
44 ${redirect_src_port:+--sport $redirect_src_port} \
45 ${redirect_src_dport:+--dport $redirect_src_dport} \
46 ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
47 --to-destination ${redirect_dest_ip}${redirect_dest_port:+:$nat_dest_port} \
48 }
49
50 fw add I f zone_${redirect_src}_forward ACCEPT ^ { $redirect_src_ip $redirect_dest_ip } { \
51 -d $redirect_dest_ip \
52 ${redirect_proto:+-p $redirect_proto} \
53 ${redirect_src_ip:+-s $redirect_src_ip} \
54 ${redirect_src_port:+--sport $redirect_src_port} \
55 ${fwd_dest_port:+--dport $fwd_dest_port} \
56 ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
57 }
58 done
59
60 fw_callback post redirect
61 }