2012-03-15 09:49:33 +00:00
|
|
|
#!/bin/sh
|
2010-11-14 23:51:16 +00:00
|
|
|
# 6to4.sh - IPv6-in-IPv4 tunnel backend
|
2012-03-15 09:49:33 +00:00
|
|
|
# Copyright (c) 2010-2012 OpenWrt.org
|
|
|
|
|
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
2012-06-05 16:04:23 +00:00
|
|
|
. /lib/functions.sh
|
2012-06-17 13:17:39 +00:00
|
|
|
. /lib/functions/network.sh
|
2012-03-15 09:49:33 +00:00
|
|
|
. ../netifd-proto.sh
|
|
|
|
init_proto "$@"
|
|
|
|
}
|
2010-11-14 23:51:16 +00:00
|
|
|
|
|
|
|
find_6to4_prefix() {
|
|
|
|
local ip4="$1"
|
|
|
|
local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
|
|
|
|
|
|
|
|
printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
|
|
|
|
}
|
|
|
|
|
2011-06-12 18:59:40 +00:00
|
|
|
test_6to4_rfc1918()
|
|
|
|
{
|
|
|
|
local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
|
|
|
|
[ $1 -eq 10 ] && return 0
|
|
|
|
[ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
|
|
|
|
[ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
|
2012-04-23 10:12:00 +00:00
|
|
|
|
|
|
|
# RFC 6598
|
|
|
|
[ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0
|
|
|
|
|
2011-06-12 18:59:40 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_6to4_setup() {
|
|
|
|
local cfg="$1"
|
|
|
|
local iface="$2"
|
2010-11-14 23:51:16 +00:00
|
|
|
local link="6to4-$cfg"
|
|
|
|
|
2014-01-17 13:59:47 +00:00
|
|
|
local mtu ttl ipaddr sourcerouting
|
|
|
|
json_get_vars mtu ttl ipaddr sourcerouting
|
2010-11-14 23:51:16 +00:00
|
|
|
|
2012-06-18 21:40:34 +00:00
|
|
|
( proto_add_host_dependency "$cfg" 0.0.0.0 )
|
|
|
|
|
2012-09-27 18:30:07 +00:00
|
|
|
local wanif
|
|
|
|
if ! network_find_wan wanif; then
|
|
|
|
proto_notify_error "$cfg" "NO_WAN_LINK"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2012-06-17 13:17:39 +00:00
|
|
|
[ -z "$ipaddr" ] && {
|
2012-09-27 18:30:07 +00:00
|
|
|
if ! network_get_ipaddr ipaddr "$wanif"; then
|
|
|
|
proto_notify_error "$cfg" "NO_WAN_ADDRESS"
|
2012-06-17 13:17:39 +00:00
|
|
|
return
|
|
|
|
fi
|
2011-06-12 18:59:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 13:17:39 +00:00
|
|
|
test_6to4_rfc1918 "$ipaddr" && {
|
2012-06-18 21:40:34 +00:00
|
|
|
proto_notify_error "$cfg" "INVALID_LOCAL_ADDRESS"
|
2012-03-15 09:49:33 +00:00
|
|
|
return
|
|
|
|
}
|
2010-11-14 23:51:16 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
# find our local prefix
|
2012-06-17 13:17:39 +00:00
|
|
|
local prefix6=$(find_6to4_prefix "$ipaddr")
|
2012-03-15 09:49:33 +00:00
|
|
|
local local6="$prefix6::1"
|
2010-12-02 22:41:03 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_init_update "$link" 1
|
|
|
|
proto_add_ipv6_address "$local6" 16
|
2013-01-15 13:08:10 +00:00
|
|
|
proto_add_ipv6_prefix "$prefix6::/48"
|
2014-01-17 13:59:47 +00:00
|
|
|
|
|
|
|
if [ "$sourcerouting" != "0" ]; then
|
|
|
|
proto_add_ipv6_route "::" 0 "::192.88.99.1" "" "" "::/128"
|
|
|
|
proto_add_ipv6_route "::" 0 "::192.88.99.1" "" "" "$local6/16"
|
|
|
|
proto_add_ipv6_route "::" 0 "::192.88.99.1" "" "" "$prefix6::/48"
|
|
|
|
else
|
|
|
|
proto_add_ipv6_route "::" 0 "::192.88.99.1"
|
|
|
|
fi
|
2010-12-02 22:41:03 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_add_tunnel
|
|
|
|
json_add_string mode sit
|
|
|
|
json_add_int mtu "${mtu:-1280}"
|
|
|
|
json_add_int ttl "${ttl:-64}"
|
2012-06-17 13:17:39 +00:00
|
|
|
json_add_string local "$ipaddr"
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_close_tunnel
|
2010-12-02 22:41:03 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_send_update "$cfg"
|
2010-11-14 23:51:16 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_6to4_teardown() {
|
2010-11-14 23:51:16 +00:00
|
|
|
local cfg="$1"
|
2012-03-15 09:49:33 +00:00
|
|
|
}
|
2010-11-14 23:51:16 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
proto_6to4_init_config() {
|
2012-04-23 10:12:00 +00:00
|
|
|
no_device=1
|
2012-03-15 09:49:33 +00:00
|
|
|
available=1
|
|
|
|
|
|
|
|
proto_config_add_string "ipaddr"
|
|
|
|
proto_config_add_int "mtu"
|
|
|
|
proto_config_add_int "ttl"
|
2014-01-17 13:59:47 +00:00
|
|
|
proto_config_add_boolean "sourcerouting"
|
2012-03-15 09:49:33 +00:00
|
|
|
}
|
2010-11-14 23:51:16 +00:00
|
|
|
|
2012-03-15 09:49:33 +00:00
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
|
|
|
add_protocol 6to4
|
2010-11-14 23:51:16 +00:00
|
|
|
}
|