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 rule
="$rule,br=$peeraddr"
51 RULE_DATA
=$
(mapcalc
${tunlink:-\*} $rule)
52 if [ "$?" != 0 ]; then
53 proto_notify_error
"$cfg" "INVALID_MAP_RULE"
54 proto_block_restart
"$cfg"
60 if [ -z "$RULE_BMR" ]; then
61 proto_notify_error
"$cfg" "NO_MATCHING_PD"
62 proto_block_restart
"$cfg"
67 if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
68 proto_init_update
"$link" 1
69 proto_add_ipv4_address $
(eval "echo \$RULE_${k}_IPV4ADDR") "" "" ""
72 json_add_string mode ipip6
73 json_add_int mtu
"${mtu:-1280}"
74 json_add_int ttl
"${ttl:-64}"
75 json_add_string
local $
(eval "echo \$RULE_${k}_IPV6ADDR")
76 json_add_string remote $
(eval "echo \$RULE_${k}_BR")
77 json_add_string link $
(eval "echo \$RULE_${k}_PD6IFACE")
79 if [ "$type" = "map-e" ]; then
81 for i
in $
(seq $RULE_COUNT); do
82 [ "$(eval "echo \
$RULE_${i}_FMR
")" != 1 ] && continue
83 fmr
="$(eval "echo \
$RULE_${i}_IPV6PREFIX
")/$(eval "echo \
$RULE_${i}_PREFIX6LEN
")"
84 fmr
="$fmr,$(eval "echo \
$RULE_${i}_IPV4PREFIX
")/$(eval "echo \
$RULE_${i}_PREFIX4LEN
")"
85 fmr
="$fmr,$(eval "echo \
$RULE_${i}_EALEN
"),$(eval "echo \
$RULE_${i}_OFFSET
")"
86 json_add_string
"" "$fmr"
93 proto_notify_error
"$cfg" "UNSUPPORTED_TYPE"
94 proto_block_restart
"$cfg"
97 proto_add_ipv4_route
"0.0.0.0" 0
99 [ "$zone" != "-" ] && json_add_string zone
"$zone"
101 json_add_array firewall
102 for portset
in $
(eval "echo \$RULE_${k}_PORTSETS"); do
103 for proto
in icmp tcp udp
; do
105 json_add_string
type nat
106 json_add_string target SNAT
107 json_add_string family inet
108 json_add_string proto
"$proto"
109 json_add_boolean connlimit_ports
1
110 json_add_string snat_ip $
(eval "echo \$RULE_${k}_IPV4ADDR")
111 json_add_string snat_port
"$portset"
118 proto_send_update
"$cfg"
120 if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
122 json_add_string name
"${cfg}_local"
123 json_add_string ifname
"@$(eval "echo \
$RULE_${k}_PD6IFACE
")"
124 json_add_string proto
"static"
125 json_add_array ip6addr
126 json_add_string
"" "$(eval "echo \
$RULE_${k}_IPV6ADDR
")"
129 ubus call network add_dynamic
"$(json_dump)"
133 proto_map_teardown
() {
135 ifdown
"${cfg}_local"
138 proto_map_init_config
() {
142 proto_config_add_string
"rule"
143 proto_config_add_string
"ipaddr"
144 proto_config_add_int
"ip4prefixlen"
145 proto_config_add_string
"ip6prefix"
146 proto_config_add_int
"ip6prefixlen"
147 proto_config_add_string
"peeraddr"
148 proto_config_add_int
"psidlen"
149 proto_config_add_int
"psid"
150 proto_config_add_int
"offset"
152 proto_config_add_string
"tunlink"
153 proto_config_add_int
"mtu"
154 proto_config_add_int
"ttl"
155 proto_config_add_string
"zone"
158 [ -n "$INCLUDE_ONLY" ] ||
{