[package] firewall:
[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 string family "" \
20 } || return
21 [ -n "$redirect_name" ] || redirect_name=$redirect__name
22 }
23
24 fw_load_redirect() {
25 fw_config_get_redirect "$1"
26
27 fw_callback pre redirect
28
29 [ -n "$redirect_src" -a -n "$redirect_dest_ip" ] || {
30 fw_die "redirect ${redirect_name}: needs src and dest_ip"
31 }
32
33 local mode=$(fw_get_family_mode ${redirect_family:-x} $redirect_src I)
34
35 local nat_dest_port=$redirect_dest_port
36 redirect_dest_port=$(fw_get_port_range $redirect_dest_port)
37 redirect_src_port=$(fw_get_port_range $redirect_src_port)
38 redirect_src_dport=$(fw_get_port_range $redirect_src_dport)
39 local fwd_dest_port=${redirect_dest_port:-$redirect_src_dport}
40
41 [ "$redirect_proto" == "tcpudp" ] && redirect_proto="tcp udp"
42 for redirect_proto in $redirect_proto; do
43 fw add $mode n zone_${redirect_src}_prerouting DNAT $ { $redirect_src_ip $redirect_dest_ip } { \
44 ${redirect_proto:+-p $redirect_proto} \
45 ${redirect_src_ip:+-s $redirect_src_ip} \
46 ${redirect_src_dip:+-d $redirect_src_dip} \
47 ${redirect_src_port:+--sport $redirect_src_port} \
48 ${redirect_src_dport:+--dport $redirect_src_dport} \
49 ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
50 --to-destination ${redirect_dest_ip}${redirect_dest_port:+:$nat_dest_port} \
51 }
52
53 fw add $mode f zone_${redirect_src}_forward ACCEPT ^ { $redirect_src_ip $redirect_dest_ip } { \
54 -d $redirect_dest_ip \
55 ${redirect_proto:+-p $redirect_proto} \
56 ${redirect_src_ip:+-s $redirect_src_ip} \
57 ${redirect_src_port:+--sport $redirect_src_port} \
58 ${fwd_dest_port:+--dport $fwd_dest_port} \
59 ${redirect_src_mac:+-m mac --mac-source $redirect_src_mac} \
60 }
61 done
62
63 fw_callback post redirect
64 }