6rd: Tos support
[openwrt/openwrt.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 df ttl tos ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink sourcerouting zone
18 json_get_vars mtu df ttl tos ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink sourcerouting zone
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="$tunlink"
30 if [ -z $wanif ] && ! network_find_wan wanif; then
31 proto_notify_error "$cfg" "NO_WAN_LINK"
32 return
33 fi
34
35 if ! network_get_ipaddr ipaddr "$wanif"; then
36 proto_notify_error "$cfg" "NO_WAN_LINK"
37 return
38 fi
39 }
40
41 # Determine the relay prefix.
42 local ip4prefixlen="${ip4prefixlen:-0}"
43 local ip4prefix=$(ipcalc.sh "$ipaddr/$ip4prefixlen" | grep NETWORK)
44 ip4prefix="${ip4prefix#NETWORK=}"
45
46 # Determine our IPv6 address.
47 local ip6subnet=$(6rdcalc "$ip6prefix/$ip6prefixlen" "$ipaddr/$ip4prefixlen")
48 local ip6addr="${ip6subnet%%::*}::1"
49
50 # Determine the IPv6 prefix
51 local ip6lanprefix="$ip6subnet/$(($ip6prefixlen + 32 - $ip4prefixlen))"
52
53 proto_init_update "$link" 1
54 proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
55 proto_add_ipv6_prefix "$ip6lanprefix"
56
57 if [ "$sourcerouting" != "0" ]; then
58 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "::/128"
59 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6addr/$ip6prefixlen"
60 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6lanprefix"
61 else
62 proto_add_ipv6_route "::" 0 "::$peeraddr" 4096
63 fi
64
65 proto_add_tunnel
66 json_add_string mode sit
67 json_add_int mtu "${mtu:-1280}"
68 json_add_boolean df "${df:-1}"
69 json_add_int ttl "${ttl:-64}"
70 [ -n "$tos" ] && json_add_string tos "$tos"
71 json_add_string local "$ipaddr"
72 json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
73 json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
74 [ -n "$tunlink" ] && json_add_string link "$tunlink"
75 proto_close_tunnel
76
77 proto_add_data
78 [ -n "$zone" ] && json_add_string zone "$zone"
79 proto_close_data
80
81 proto_send_update "$cfg"
82 }
83
84 proto_6rd_teardown() {
85 local cfg="$1"
86 }
87
88 proto_6rd_init_config() {
89 no_device=1
90 available=1
91
92 proto_config_add_int "mtu"
93 proto_config_add_boolean "df"
94 proto_config_add_int "ttl"
95 proto_config_add_string "tos"
96 proto_config_add_string "ipaddr"
97 proto_config_add_string "peeraddr"
98 proto_config_add_string "ip6prefix"
99 proto_config_add_string "ip6prefixlen"
100 proto_config_add_string "ip4prefixlen"
101 proto_config_add_string "tunlink"
102 proto_config_add_boolean "sourcerouting"
103 proto_config_add_string "zone"
104 }
105
106 [ -n "$INCLUDE_ONLY" ] || {
107 add_protocol 6rd
108 }