be6d5368453924214d0622e1ee9ef878abf973c0
[openwrt/svn-archive/archive.git] / package / network / ipv6 / 6rd / files / 6rd.sh
1 #!/bin/sh
2 # 6rd.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 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_6rd_setup() {
13 local cfg="$1"
14 local iface="$2"
15 local link="6rd-$cfg"
16
17 local mtu ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink sourcerouting
18 json_get_vars mtu ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink sourcerouting
19
20 [ -z "$ip6prefix" -o -z "$peeraddr" ] && {
21 proto_notify_error "$cfg" "MISSING_ADDRESS"
22 proto_block_restart "$cfg"
23 return
24 }
25
26 ( proto_add_host_dependency "$cfg" 0.0.0.0 )
27
28 [ -z "$ipaddr" ] && {
29 local wanif
30 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
31 proto_notify_error "$cfg" "NO_WAN_LINK"
32 return
33 fi
34 }
35
36 # Determine the relay prefix.
37 local ip4prefixlen="${ip4prefixlen:-0}"
38 local ip4prefix=$(ipcalc.sh "$ipaddr/$ip4prefixlen" | grep NETWORK)
39 ip4prefix="${ip4prefix#NETWORK=}"
40
41 # Determine our IPv6 address.
42 local ip6subnet=$(6rdcalc "$ip6prefix/$ip6prefixlen" "$ipaddr/$ip4prefixlen")
43 local ip6addr="${ip6subnet%%::*}::1"
44
45 # Determine the IPv6 prefix
46 local ip6lanprefix="$ip6subnet/$(($ip6prefixlen + 32 - $ip4prefixlen))"
47
48 proto_init_update "$link" 1
49 proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
50 proto_add_ipv6_prefix "$ip6lanprefix"
51
52 if [ "$sourcerouting" != "0" ]; then
53 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "::/128"
54 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6addr/$ip6prefixlen"
55 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6lanprefix"
56 else
57 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096
58 fi
59
60 proto_add_tunnel
61 json_add_string mode sit
62 json_add_int mtu "${mtu:-1280}"
63 json_add_int ttl "${ttl:-64}"
64 json_add_string local "$ipaddr"
65 json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
66 json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
67 [ -n "$tunlink" ] && json_add_string link "$tunlink"
68 proto_close_tunnel
69
70 proto_send_update "$cfg"
71 }
72
73 proto_6rd_teardown() {
74 local cfg="$1"
75 }
76
77 proto_6rd_init_config() {
78 no_device=1
79 available=1
80
81 proto_config_add_int "mtu"
82 proto_config_add_int "ttl"
83 proto_config_add_string "ipaddr"
84 proto_config_add_string "peeraddr"
85 proto_config_add_string "ip6prefix"
86 proto_config_add_string "ip6prefixlen"
87 proto_config_add_string "ip4prefixlen"
88 proto_config_add_string "tunlink"
89 proto_config_add_boolean "sourcerouting"
90 }
91
92 [ -n "$INCLUDE_ONLY" ] || {
93 add_protocol 6rd
94 }