Initial support for MAP-E and Lightweight 4over6 protocol
[openwrt/openwrt.git] / package / network / ipv6 / map / files / map.sh
1 #!/bin/sh
2 # map.sh - IPv4-in-IPv6 tunnel backend
3 #
4 # Author: Steven Barth <cyrus@openwrt.org>
5 # Copyright (c) 2014 cisco Systems, Inc.
6 #
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
10 #
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.
15
16 [ -n "$INCLUDE_ONLY" ] || {
17 . /lib/functions.sh
18 . /lib/functions/network.sh
19 . ../netifd-proto.sh
20 init_proto "$@"
21 }
22
23 proto_map_setup() {
24 local cfg="$1"
25 local iface="$2"
26 local link="map-$cfg"
27
28 # uncomment for legacy MAP0 mode
29 #export LEGACY=1
30
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
35
36 [ -z "$zone" ] && zone="wan"
37 [ -z "$type" ] && type="map-e"
38 [ -z "$ip4prefixlen" ] && ip4prefixlen=32
39
40 ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
41
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"
49 fi
50
51 RULE_DATA=$(mapcalc ${tunlink:-\*} $rule)
52 if [ "$?" != 0 ]; then
53 proto_notify_error "$cfg" "INVALID_MAP_RULE"
54 proto_block_restart "$cfg"
55 return
56 fi
57
58 eval $RULE_DATA
59
60 if [ -z "$RULE_BMR" ]; then
61 proto_notify_error "$cfg" "NO_MATCHING_PD"
62 proto_block_restart "$cfg"
63 return
64 fi
65
66 k=$RULE_BMR
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") "" "" ""
70
71 proto_add_tunnel
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")
78
79 if [ "$type" = "map-e" ]; then
80 json_add_array "fmrs"
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"
87 done
88 json_close_array
89 fi
90
91 proto_close_tunnel
92 else
93 proto_notify_error "$cfg" "UNSUPPORTED_TYPE"
94 proto_block_restart "$cfg"
95 fi
96
97 proto_add_ipv4_route "0.0.0.0" 0
98 proto_add_data
99 [ "$zone" != "-" ] && json_add_string zone "$zone"
100
101 json_add_array firewall
102 for portset in $(eval "echo \$RULE_${k}_PORTSETS"); do
103 for proto in icmp tcp udp; do
104 json_add_object ""
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"
112 json_close_object
113 done
114 done
115 json_close_array
116 proto_close_data
117
118 proto_send_update "$cfg"
119
120 if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
121 json_init
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")"
127 json_close_array
128 json_close_object
129 ubus call network add_dynamic "$(json_dump)"
130 fi
131 }
132
133 proto_map_teardown() {
134 local cfg="$1"
135 ifdown "${cfg}_local"
136 }
137
138 proto_map_init_config() {
139 no_device=1
140 available=1
141
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"
151
152 proto_config_add_string "tunlink"
153 proto_config_add_int "mtu"
154 proto_config_add_int "ttl"
155 proto_config_add_string "zone"
156 }
157
158 [ -n "$INCLUDE_ONLY" ] || {
159 add_protocol map
160 }