Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / network / ipv6 / ds-lite / files / dslite.sh
1 #!/bin/sh
2 # dslite.sh - IPv4-in-IPv6 tunnel backend
3 # Copyright (c) 2013 OpenWrt.org
4
5 [ -n "$INCLUDE_ONLY" ] || {
6         . /lib/functions.sh
7         . /lib/functions/network.sh
8         . ../netifd-proto.sh
9         init_proto "$@"
10 }
11
12 proto_dslite_setup() {
13         local cfg="$1"
14         local iface="$2"
15         local link="ds-$cfg"
16         local remoteip6
17
18         local mtu ttl peeraddr ip6addr tunlink zone weakif encaplimit
19         json_get_vars mtu ttl peeraddr ip6addr tunlink zone weakif encaplimit
20
21         [ -z "$peeraddr" ] && {
22                 proto_notify_error "$cfg" "MISSING_ADDRESS"
23                 proto_block_restart "$cfg"
24                 return
25         }
26
27         ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
28
29         remoteip6=$(resolveip -6 "$peeraddr")
30         if [ -z "$remoteip6" ]; then
31                 sleep 3
32                 remoteip6=$(resolveip -6 "$peeraddr")
33                 if [ -z "$remoteip6" ]; then
34                         proto_notify_error "$cfg" "AFTR_DNS_FAIL"
35                         return
36                 fi
37         fi
38
39         for ip6 in $remoteip6; do
40                 peeraddr=$ip6
41                 break
42         done
43
44         [ -z "$ip6addr" ] && {
45                 local wanif="$tunlink"
46                 if [ -z "$wanif" ] && ! network_find_wan6 wanif; then
47                         proto_notify_error "$cfg" "NO_WAN_LINK"
48                         return
49                 fi
50
51                 if ! network_get_ipaddr6 ip6addr "$wanif"; then
52                         [ -z "$weakif" ] && weakif="lan"
53                         if ! network_get_ipaddr6 ip6addr "$weakif"; then
54                                 proto_notify_error "$cfg" "NO_WAN_LINK"
55                                 return
56                         fi
57                 fi
58         }
59
60         proto_init_update "$link" 1
61         proto_add_ipv4_route "0.0.0.0" 0
62         proto_add_ipv4_address "192.0.0.2" "" "" "192.0.0.1"
63
64         proto_add_tunnel
65         json_add_string mode ipip6
66         json_add_int mtu "${mtu:-1280}"
67         json_add_int ttl "${ttl:-64}"
68         json_add_string local "$ip6addr"
69         json_add_string remote "$peeraddr"
70         [ -n "$tunlink" ] && json_add_string link "$tunlink"
71         json_add_object "data"
72           [ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit"
73         json_close_object
74         proto_close_tunnel
75
76         proto_add_data
77         [ -n "$zone" ] && json_add_string zone "$zone"
78
79         json_add_array firewall
80           json_add_object ""
81             json_add_string type nat
82             json_add_string target ACCEPT
83           json_close_object
84         json_close_array
85         proto_close_data
86
87         proto_send_update "$cfg"
88 }
89
90 proto_dslite_teardown() {
91         local cfg="$1"
92 }
93
94 proto_dslite_init_config() {
95         no_device=1             
96         available=1
97
98         proto_config_add_string "ip6addr"
99         proto_config_add_string "peeraddr"
100         proto_config_add_string "tunlink"
101         proto_config_add_int "mtu"
102         proto_config_add_int "ttl"
103         proto_config_add_string "encaplimit"
104         proto_config_add_string "zone"
105         proto_config_add_string "weakif"
106 }
107
108 [ -n "$INCLUDE_ONLY" ] || {
109         add_protocol dslite
110 }