3786ef06f9a0e5bdee0b177f4dc909062cdc1ac7
[openwrt/staging/lynxis/omap.git] / package / 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 tun_error() {
13 local cfg="$1"; shift;
14
15 [ -n "$1" ] && proto_notify_error "$cfg" "$@"
16 proto_block_restart "$cfg"
17 }
18
19 proto_6rd_setup() {
20 local cfg="$1"
21 local iface="$2"
22 local link="6rd-$cfg"
23
24 local mtu ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen
25 json_get_vars mtu ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen
26
27 [ -z "$ip6prefix" -o -z "$peeraddr" ] && {
28 tun_error "$cfg" "MISSING_ADDRESS"
29 return
30 }
31
32 [ -z "$ipaddr" ] && {
33 local wanif
34 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
35 tun_error "$cfg" "NO_WAN_LINK"
36 return
37 fi
38 }
39
40 # Determine the relay prefix.
41 local ip4prefixlen="${ip4prefixlen:-0}"
42 local ip4prefix=$(ipcalc.sh "$ipaddr/$ip4prefixlen" | grep NETWORK)
43 ip4prefix="${ip4prefix#NETWORK=}"
44
45 # Determine our IPv6 address.
46 local ip6subnet=$(6rdcalc "$ip6prefix/$ip6prefixlen" "$ipaddr/$ip4prefixlen")
47 local ip6addr="${ip6subnet%%::*}::1"
48
49 proto_init_update "$link" 1
50 proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
51 proto_add_ipv6_route "::" 0 "::$peeraddr"
52
53 proto_add_tunnel
54 json_add_string mode sit
55 json_add_int mtu "${mtu:-1280}"
56 json_add_int ttl "${ttl:-64}"
57 json_add_string local "$ipaddr"
58 json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
59 json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
60 proto_close_tunnel
61
62 proto_send_update "$cfg"
63 }
64
65 proto_6rd_teardown() {
66 local cfg="$1"
67 }
68
69 proto_6rd_init_config() {
70 no_device=1
71 available=1
72
73 proto_config_add_int "mtu"
74 proto_config_add_int "ttl"
75 proto_config_add_string "peeraddr"
76 proto_config_add_string "ip6prefix"
77 proto_config_add_string "ip6prefixlen"
78 proto_config_add_string "ip4prefixlen"
79 }
80
81 [ -n "$INCLUDE_ONLY" ] || {
82 add_protocol 6rd
83 }