e849b15b94e0c8fe6c54c28d5a506f01c83e5682
[openwrt.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
17         local mtu ttl peeraddr ip6addr tunlink zone weakif
18         json_get_vars mtu ttl peeraddr ip6addr tunlink zone weakif
19
20         [ -z "$peeraddr" ] && {
21                 proto_notify_error "$cfg" "MISSING_ADDRESS"
22                 proto_block_restart "$cfg"
23                 return
24         }
25
26         ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
27
28         peeraddr=$(resolveip -6 $peeraddr)
29         if [ -z "$peeraddr" ]; then
30                 sleep 3
31                 peeraddr=$(resolveip -6 $peeraddr)
32                 if [ -z "$peeraddr" ]; then
33                         proto_notify_error "$cfg" "AFTR_DNS_FAIL"
34                         return
35                 fi
36         fi
37         peeraddr="${peeraddr%% *}"
38
39         [ -z "$ip6addr" ] && {
40                 local wanif="$tunlink"
41                 if [ -z "$wanif" ] && ! network_find_wan6 wanif; then
42                         proto_notify_error "$cfg" "NO_WAN_LINK"
43                         return
44                 fi
45
46                 if ! network_get_ipaddr6 ip6addr "$wanif"; then
47                         [ -z "$weakif" ] && weakif="lan"
48                         if ! network_get_ipaddr6 ip6addr "$weakif"; then
49                                 proto_notify_error "$cfg" "NO_WAN_LINK"
50                                 return
51                         fi
52                 fi
53         }
54
55         proto_init_update "$link" 1
56         proto_add_ipv4_route "0.0.0.0" 0
57         proto_add_ipv4_address "192.0.0.2" "" "" "192.0.0.1"
58
59         proto_add_tunnel
60         json_add_string mode ipip6
61         json_add_int mtu "${mtu:-1280}"
62         json_add_int ttl "${ttl:-64}"
63         json_add_string local "$ip6addr"
64         json_add_string remote "$peeraddr"
65         [ -n "$tunlink" ] && json_add_string link "$tunlink"
66         proto_close_tunnel
67
68         proto_add_data
69         [ -n "$zone" ] && json_add_string zone "$zone"
70
71         json_add_array firewall
72           json_add_object ""
73             json_add_string type nat
74             json_add_string target ACCEPT
75           json_close_object
76         json_close_array
77         proto_close_data
78
79         proto_send_update "$cfg"
80 }
81
82 proto_dslite_teardown() {
83         local cfg="$1"
84 }
85
86 proto_dslite_init_config() {
87         no_device=1             
88         available=1
89
90         proto_config_add_string "ip6addr"
91         proto_config_add_string "peeraddr"
92         proto_config_add_string "tunlink"
93         proto_config_add_int "mtu"
94         proto_config_add_int "ttl"
95         proto_config_add_string "zone"
96         proto_config_add_string "weakif"
97 }
98
99 [ -n "$INCLUDE_ONLY" ] || {
100         add_protocol dslite
101 }