83de4cc4dc52d49413429ee702fb7b41dfa585fe
[14.07/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                 if [ "$type" = "map-t" ]; then
49                         rule="$rule,dmr=$peeraddr"
50                 else
51                         rule="$rule,br=$peeraddr"
52                 fi
53         fi
54
55         RULE_DATA=$(mapcalc ${tunlink:-\*} $rule)
56         if [ "$?" != 0 ]; then
57                 proto_notify_error "$cfg" "INVALID_MAP_RULE"
58                 proto_block_restart "$cfg"
59                 return
60         fi
61
62         eval $RULE_DATA
63         
64         if [ -z "$RULE_BMR" ]; then
65                 proto_notify_error "$cfg" "NO_MATCHING_PD"
66                 proto_block_restart "$cfg"
67                 return
68         fi
69
70         k=$RULE_BMR
71         if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
72                 proto_init_update "$link" 1
73                 proto_add_ipv4_address $(eval "echo \$RULE_${k}_IPV4ADDR") "" "" ""
74         
75                 proto_add_tunnel
76                 json_add_string mode ipip6
77                 json_add_int mtu "${mtu:-1280}"
78                 json_add_int ttl "${ttl:-64}"
79                 json_add_string local $(eval "echo \$RULE_${k}_IPV6ADDR")
80                 json_add_string remote $(eval "echo \$RULE_${k}_BR")
81                 json_add_string link $(eval "echo \$RULE_${k}_PD6IFACE")
82
83                 if [ "$type" = "map-e" ]; then
84                         json_add_array "fmrs"
85                                 for i in $(seq $RULE_COUNT); do
86                                         [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
87                                         fmr="$(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
88                                         fmr="$fmr,$(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
89                                         fmr="$fmr,$(eval "echo \$RULE_${i}_EALEN"),$(eval "echo \$RULE_${i}_OFFSET")"
90                                         json_add_string "" "$fmr"
91                                 done
92                         json_close_array
93                 fi
94
95                 proto_close_tunnel
96         elif [ "$type" = "map-t" -a -f "/proc/net/nat46/control" ]; then
97                 proto_init_update "$link" 1
98                 local style="MAP"
99                 [ "$LEGACY" = 1 ] && style="MAP0"
100
101                 echo add $link > /proc/net/nat46/control
102                 local cfgstr="local.style $style local.v4 $(eval "echo \$RULE_${k}_IPV4PREFIX")/$(eval "echo \$RULE_${k}_PREFIX4LEN")"
103                 cfgstr="$cfgstr local.v6 $(eval "echo \$RULE_${k}_IPV6PREFIX")/$(eval "echo \$RULE_${k}_PREFIX6LEN")"
104                 cfgstr="$cfgstr local.ea-len $(eval "echo \$RULE_${k}_EALEN") local.psid-offset $(eval "echo \$RULE_${k}_OFFSET")"
105                 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"
106                 echo config $link $cfgstr > /proc/net/nat46/control
107
108                 for i in $(seq $RULE_COUNT); do
109                         [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
110                         local cfgstr="remote.style $style remote.v4 $(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
111                         cfgstr="$cfgstr remote.v6 $(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
112                         cfgstr="$cfgstr remote.ea-len $(eval "echo \$RULE_${i}_EALEN") remote.psid-offset $(eval "echo \$RULE_${i}_OFFSET")"
113                         echo insert $link $cfgstr > /proc/net/nat46/control
114                 done
115         else
116                 proto_notify_error "$cfg" "UNSUPPORTED_TYPE"
117                 proto_block_restart "$cfg"
118         fi
119
120         proto_add_ipv4_route "0.0.0.0" 0
121         proto_add_data
122         [ "$zone" != "-" ] && json_add_string zone "$zone"
123
124         json_add_array firewall
125           if [ -z "$(eval "echo \$RULE_${k}_PORTSETS")" ]; then
126             json_add_object ""
127               json_add_string type nat
128               json_add_string target SNAT
129               json_add_string family inet
130               json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
131             json_close_object
132           else
133             for portset in $(eval "echo \$RULE_${k}_PORTSETS"); do
134               for proto in icmp tcp udp; do
135                 json_add_object ""
136                   json_add_string type nat
137                   json_add_string target SNAT
138                   json_add_string family inet
139                   json_add_string proto "$proto"
140                   json_add_boolean connlimit_ports 1
141                   json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
142                   json_add_string snat_port "$portset"
143                 json_close_object
144               done
145             done
146           fi
147           if [ "$type" = "map-t" ]; then
148                 json_add_object ""
149                         json_add_string type rule
150                         json_add_string family inet6
151                         json_add_string proto all
152                         json_add_string direction in
153                         json_add_string dest "$zone"
154                         json_add_string src "$zone"
155                         json_add_string src_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
156                         json_add_string target ACCEPT
157                 json_close_object
158                 json_add_object ""
159                         json_add_string type rule
160                         json_add_string family inet6
161                         json_add_string proto all
162                         json_add_string direction out
163                         json_add_string dest "$zone"
164                         json_add_string src "$zone"
165                         json_add_string dest_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
166                         json_add_string target ACCEPT
167                 json_close_object
168                 proto_add_ipv6_route $(eval "echo \$RULE_${k}_IPV6ADDR") 128
169           fi
170         json_close_array
171         proto_close_data
172
173         proto_send_update "$cfg"
174
175         if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
176                 json_init
177                 json_add_string name "${cfg}_local"
178                 json_add_string ifname "@$(eval "echo \$RULE_${k}_PD6IFACE")"
179                 json_add_string proto "static"
180                 json_add_array ip6addr
181                 json_add_string "" "$(eval "echo \$RULE_${k}_IPV6ADDR")"
182                 json_close_array
183                 json_close_object
184                 ubus call network add_dynamic "$(json_dump)"
185         fi
186 }
187
188 proto_map_teardown() {
189         local cfg="$1"
190         ifdown "${cfg}_local"
191 }
192
193 proto_map_init_config() {
194         no_device=1             
195         available=1
196
197         proto_config_add_string "rule"
198         proto_config_add_string "ipaddr"
199         proto_config_add_int "ip4prefixlen"
200         proto_config_add_string "ip6prefix"
201         proto_config_add_int "ip6prefixlen"
202         proto_config_add_string "peeraddr"
203         proto_config_add_int "psidlen"
204         proto_config_add_int "psid"
205         proto_config_add_int "offset"
206
207         proto_config_add_string "tunlink"
208         proto_config_add_int "mtu"
209         proto_config_add_int "ttl"
210         proto_config_add_string "zone"
211 }
212
213 [ -n "$INCLUDE_ONLY" ] || {
214         add_protocol map
215 }