2 # map.sh - IPv4-in-IPv6 tunnel backend
4 # Author: Steven Barth <cyrus@openwrt.org>
5 # Copyright (c) 2014 cisco Systems, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2
9 # as published by the Free Software Foundation
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 [ -n "$INCLUDE_ONLY" ] || {
18 . /lib/functions/network.sh
28 # uncomment for legacy MAP0 mode
31 local type mtu ttl tunlink zone
32 local rule ipaddr ip4prefixlen ip6prefix ip6prefixlen peeraddr ealen psidlen psid offset
33 json_get_vars type mtu ttl tunlink zone
34 json_get_vars rule ipaddr ip4prefixlen ip6prefix ip6prefixlen peeraddr ealen psidlen psid offset
36 [ -z "$zone" ] && zone="wan"
37 [ -z "$type" ] && type="map-e"
38 [ -z "$ip4prefixlen" ] && ip4prefixlen=32
40 ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
42 if [ -z "$rule" ]; then
43 rule="type=$type,ipv6prefix=$ip6prefix,prefix6len=$ip6prefixlen,ipv4prefix=$ipaddr,prefix4len=$ip4prefixlen"
44 [ -n "$psid" ] && rule="$rule,psid=$psid"
45 [ -n "$psidlen" ] && rule="$rule,psidlen=$psidlen"
46 [ -n "$offset" ] && rule="$rule,offset=$offset"
47 [ -n "$ealen" ] && rule="$rule,ealen=$ealen"
48 if [ "$type" = "map-t" ]; then
49 rule="$rule,dmr=$peeraddr"
51 rule="$rule,br=$peeraddr"
55 echo "rule=$rule" > /tmp/map-$cfg.rules
56 RULE_DATA=$(mapcalc ${tunlink:-\*} $rule)
57 if [ "$?" != 0 ]; then
58 proto_notify_error "$cfg" "INVALID_MAP_RULE"
59 proto_block_restart "$cfg"
63 echo "$RULE_DATA" >> /tmp/map-$cfg.rules
66 if [ -z "$RULE_BMR" ]; then
67 proto_notify_error "$cfg" "NO_MATCHING_PD"
68 proto_block_restart "$cfg"
73 if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
74 proto_init_update "$link" 1
75 proto_add_ipv4_address $(eval "echo \$RULE_${k}_IPV4ADDR") "" "" ""
78 json_add_string mode ipip6
79 json_add_int mtu "${mtu:-1280}"
80 json_add_int ttl "${ttl:-64}"
81 json_add_string local $(eval "echo \$RULE_${k}_IPV6ADDR")
82 json_add_string remote $(eval "echo \$RULE_${k}_BR")
83 json_add_string link $(eval "echo \$RULE_${k}_PD6IFACE")
85 if [ "$type" = "map-e" ]; then
87 for i in $(seq $RULE_COUNT); do
88 [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
89 fmr="$(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
90 fmr="$fmr,$(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
91 fmr="$fmr,$(eval "echo \$RULE_${i}_EALEN"),$(eval "echo \$RULE_${i}_OFFSET")"
92 json_add_string "" "$fmr"
98 elif [ "$type" = "map-t" -a -f "/proc/net/nat46/control" ]; then
99 proto_init_update "$link" 1
101 [ "$LEGACY" = 1 ] && style="MAP0"
103 echo add $link > /proc/net/nat46/control
104 local cfgstr="local.style $style local.v4 $(eval "echo \$RULE_${k}_IPV4PREFIX")/$(eval "echo \$RULE_${k}_PREFIX4LEN")"
105 cfgstr="$cfgstr local.v6 $(eval "echo \$RULE_${k}_IPV6PREFIX")/$(eval "echo \$RULE_${k}_PREFIX6LEN")"
106 cfgstr="$cfgstr local.ea-len $(eval "echo \$RULE_${k}_EALEN") local.psid-offset $(eval "echo \$RULE_${k}_OFFSET")"
107 cfgstr="$cfgstr remote.v4 0.0.0.0/0 remote.v6 $(eval "echo \$RULE_${k}_DMR") remote.style RFC6052 remote.ea-len 0 remote.psid-offset 0"
108 echo config $link $cfgstr > /proc/net/nat46/control
110 for i in $(seq $RULE_COUNT); do
111 [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
112 local cfgstr="remote.style $style remote.v4 $(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
113 cfgstr="$cfgstr remote.v6 $(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
114 cfgstr="$cfgstr remote.ea-len $(eval "echo \$RULE_${i}_EALEN") remote.psid-offset $(eval "echo \$RULE_${i}_OFFSET")"
115 echo insert $link $cfgstr > /proc/net/nat46/control
118 proto_notify_error "$cfg" "UNSUPPORTED_TYPE"
119 proto_block_restart "$cfg"
122 proto_add_ipv4_route "0.0.0.0" 0
124 [ "$zone" != "-" ] && json_add_string zone "$zone"
126 json_add_array firewall
127 if [ -z "$(eval "echo \$RULE_${k}_PORTSETS")" ]; then
129 json_add_string type nat
130 json_add_string target SNAT
131 json_add_string family inet
132 json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
135 for portset in $(eval "echo \$RULE_${k}_PORTSETS"); do
136 for proto in icmp tcp udp; do
138 json_add_string type nat
139 json_add_string target SNAT
140 json_add_string family inet
141 json_add_string proto "$proto"
142 json_add_boolean connlimit_ports 1
143 json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
144 json_add_string snat_port "$portset"
149 if [ "$type" = "map-t" ]; then
151 json_add_string type rule
152 json_add_string family inet6
153 json_add_string proto all
154 json_add_string direction in
155 json_add_string dest "$zone"
156 json_add_string src "$zone"
157 json_add_string src_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
158 json_add_string target ACCEPT
161 json_add_string type rule
162 json_add_string family inet6
163 json_add_string proto all
164 json_add_string direction out
165 json_add_string dest "$zone"
166 json_add_string src "$zone"
167 json_add_string dest_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
168 json_add_string target ACCEPT
170 proto_add_ipv6_route $(eval "echo \$RULE_${k}_IPV6ADDR") 128
175 proto_send_update "$cfg"
177 if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
179 json_add_string name "${cfg}_local"
180 json_add_string ifname "@$(eval "echo \$RULE_${k}_PD6IFACE")"
181 json_add_string proto "static"
182 json_add_array ip6addr
183 json_add_string "" "$(eval "echo \$RULE_${k}_IPV6ADDR")"
186 ubus call network add_dynamic "$(json_dump)"
190 proto_map_teardown() {
192 ifdown "${cfg}_local"
193 rm -f /tmp/map-$cfg.rules
196 proto_map_init_config() {
200 proto_config_add_string "rule"
201 proto_config_add_string "ipaddr"
202 proto_config_add_int "ip4prefixlen"
203 proto_config_add_string "ip6prefix"
204 proto_config_add_int "ip6prefixlen"
205 proto_config_add_string "peeraddr"
206 proto_config_add_int "psidlen"
207 proto_config_add_int "psid"
208 proto_config_add_int "offset"
210 proto_config_add_string "tunlink"
211 proto_config_add_int "mtu"
212 proto_config_add_int "ttl"
213 proto_config_add_string "zone"
216 [ -n "$INCLUDE_ONLY" ] || {