2009-05-01 19:41:40 +05:30
|
|
|
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
|
2011-06-30 05:16:31 +05:30
|
|
|
# Released under the 2-clause BSD license.
|
2007-11-14 20:52:04 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
_config_vars="$_config_vars link suffix relay"
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
ip6to4_depend()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
after interface
|
2012-01-09 05:54:03 +05:30
|
|
|
program ip
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2011-12-27 06:37:41 +05:30
|
|
|
ip6to4_pre_start()
|
2008-01-11 20:38:57 +05:30
|
|
|
{
|
2011-12-27 06:37:41 +05:30
|
|
|
# ALL interfaces run pre_start blocks, not just those with something
|
|
|
|
# assigned, so we must check if we need to run on this interface before we
|
|
|
|
# do so.
|
|
|
|
local config
|
|
|
|
eval config=\$config_${IFVAR}
|
|
|
|
[ "$config" = "ip6to4" ] || return 0
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
case " ${MODULES} " in
|
|
|
|
*" ifconfig "*)
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${IFACE}" != "sit0" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eerror "ip6to4 can only work on the sit0 interface using ifconfig"
|
|
|
|
eerror "emerge sys-apps/iproute2 to use other interfaces"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
esac
|
|
|
|
|
2011-12-27 06:37:41 +05:30
|
|
|
local host= suffix= relay= addr= iface=${IFACE} config_ip6to4= localip=
|
2007-04-05 16:48:42 +05:30
|
|
|
eval host=\$link_${IFVAR}
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -z "${host}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eerror "link_${IFVAR} not set"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval suffix=\${suffix_${IFVAR}:-1}
|
|
|
|
eval relay=\${relay_${IFVAR}:-192.88.99.1}
|
|
|
|
|
|
|
|
IFACE=${host}
|
|
|
|
addrs=$(_get_inet_addresses)
|
|
|
|
IFACE=${iface}
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -z "${addrs}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eerror "${host} is not configured with an IPv4 address"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
for addr in ${addrs}; do
|
2007-04-05 16:48:42 +05:30
|
|
|
# Strip the subnet
|
|
|
|
local ip="${addr%/*}" subnet="${addr#*/}"
|
|
|
|
# We don't work on private IPv4 addresses
|
|
|
|
case "${ip}" in
|
2007-11-28 21:15:03 +05:30
|
|
|
127.*) continue;;
|
|
|
|
10.*) continue;;
|
|
|
|
192.168.*) continue;;
|
2007-04-05 16:48:42 +05:30
|
|
|
172.*)
|
|
|
|
local i=16
|
2007-11-28 21:15:03 +05:30
|
|
|
while [ ${i} -lt 32 ]; do
|
2007-04-05 16:48:42 +05:30
|
|
|
case "${ip}" in
|
2007-11-28 21:15:03 +05:30
|
|
|
172.${i}.*) break;;
|
2007-04-05 16:48:42 +05:30
|
|
|
esac
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( i += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
[ ${i} -lt 32 ] && continue
|
|
|
|
;;
|
|
|
|
esac
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
veinfo "IPv4 address on ${host}: ${ip}/${subnet}"
|
2007-10-30 17:08:23 +05:30
|
|
|
local ipa= ip6= IFS="${IFS}."
|
|
|
|
for i in ${ip}; do
|
2007-04-05 16:48:42 +05:30
|
|
|
ipa="${ipa} ${i}"
|
|
|
|
done
|
2007-10-30 17:08:23 +05:30
|
|
|
unset IFS
|
2007-04-05 16:48:42 +05:30
|
|
|
eval ip6="$(printf "2002:%02x%02x:%02x%02x::%s" ${ipa} ${suffix})"
|
|
|
|
veinfo "Derived IPv6 address: ${ip6}"
|
|
|
|
|
|
|
|
# Now apply our IPv6 address to our config
|
2011-12-27 06:37:41 +05:30
|
|
|
config_ip6to4="${config_ip6to4}${config_ip6to4:+ }${ip6}/48"
|
2007-05-03 17:49:18 +05:30
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -n "${localip}" ]; then
|
2007-05-03 17:49:18 +05:30
|
|
|
localip="any"
|
|
|
|
else
|
|
|
|
localip="${ip}"
|
|
|
|
fi
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
|
2011-12-27 06:37:41 +05:30
|
|
|
if [ -z "${config_ip6to4}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
eerror "No global IPv4 addresses found on interface ${host}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${IFACE}" != "sit0" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "Creating 6to4 tunnel on ${IFACE}"
|
2007-05-03 17:49:18 +05:30
|
|
|
_tunnel add "${IFACE}" mode sit ttl 255 remote any local "${localip}"
|
2007-04-05 16:48:42 +05:30
|
|
|
eend $? || return 1
|
|
|
|
_up
|
|
|
|
fi
|
2011-12-27 06:37:41 +05:30
|
|
|
routes_ip6to4="2003::/3 via ::${relay} metric 2147483647"
|
|
|
|
service_set_value "config_ip6to4_$IFVAR" "$config_ip6to4"
|
|
|
|
service_set_value "routes_ip6to4_$IFVAR" "$routes_ip6to4"
|
|
|
|
}
|
|
|
|
|
|
|
|
ip6to4_start()
|
|
|
|
{
|
|
|
|
local config_ip6to4=$(service_get_value "config_ip6to4_$IFVAR")
|
|
|
|
local routes_ip6to4=$(service_get_value "routes_ip6to4_$IFVAR")
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
# Now apply our config
|
2011-12-27 06:37:41 +05:30
|
|
|
eval config_${config_index}=\'"${config_ip6to4}"\'
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( config_index -= 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
# Add a route for us, ensuring we don't delete anything else
|
2007-10-29 15:15:49 +05:30
|
|
|
local routes="$(_get_array "routes_${IFVAR}")
|
2011-12-27 06:37:41 +05:30
|
|
|
$routes_ip6to4"
|
2007-10-29 15:15:49 +05:30
|
|
|
eval routes_${IFVAR}=\$routes
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|