From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RvQUD-0000uh-Qu for garchives@archives.gentoo.org; Thu, 09 Feb 2012 09:40:18 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D93E6E0639; Thu, 9 Feb 2012 09:40:09 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 9A238E0639 for ; Thu, 9 Feb 2012 09:40:09 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 7E8131B4041 for ; Thu, 9 Feb 2012 09:40:08 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 3806EE53FF for ; Thu, 9 Feb 2012 09:40:07 +0000 (UTC) From: "Robin H. Johnson" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Robin H. Johnson" Message-ID: <659f7c2bc82c43088fd4a479d8fee849fe89141e.robbat2@gentoo> Subject: [gentoo-commits] proj/openrc:master commit in: net/ X-VCS-Repository: proj/openrc X-VCS-Files: net/ip6rd.sh X-VCS-Directories: net/ X-VCS-Committer: robbat2 X-VCS-Committer-Name: Robin H. Johnson X-VCS-Revision: 659f7c2bc82c43088fd4a479d8fee849fe89141e Date: Thu, 9 Feb 2012 09:40:07 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: ed8b697a-26a0-4334-9d68-264560ea7c1b X-Archives-Hash: d5e9e146df0537804b1767d1b74a764a commit: 659f7c2bc82c43088fd4a479d8fee849fe89141e Author: Salah Coronya gmail com> AuthorDate: Thu Feb 9 09:39:40 2012 +0000 Commit: Robin H. Johnson gentoo org> CommitDate: Thu Feb 9 09:39:40 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/openrc.git;a=3D= commit;h=3D659f7c2b oldnet: ip6rd support X-Gentoo-Bug: 392223 X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=3D392223 Signed-off-by: Robin H. Johnson gentoo.org> --- net/ip6rd.sh | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++ 1 files changed, 168 insertions(+), 0 deletions(-) diff --git a/net/ip6rd.sh b/net/ip6rd.sh new file mode 100644 index 0000000..9da0576 --- /dev/null +++ b/net/ip6rd.sh @@ -0,0 +1,168 @@ +# Copyright (c) 2011 by Gentoo Foundation +# Released under the 2-clause BSD license. + +_config_vars=3D"$_config_vars link prefix suffix ipv4mask relay" + +ip6rd_depend() +{ + program ip + after interface +} + +ip6rd_pre_start() +{ + # 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=3D\$config_${IFVAR} + [ "$config" =3D "ip6rd" ] || return 0 + + case "${MODULES}" in + *" ifconfig "*) + eerror "ifconfig is not supported for 6rd" + eerror "Please emerge sys-apps/iproute2" + return 1 + ;; + esac + + local host=3D suffix=3D relay=3D addr=3D iface=3D${IFACE} config_ip6rd=3D= localip=3D ipv4mask=3D + eval host=3D\$link_${IFVAR} + if [ -z "${host}" ]; then + eerror "link_${IFVAR} not set" + return 1 + fi + + eval host=3D\${link_${IFVAR}} + eval ipv4mask=3D\${ipv4mask_${IFVAR}:-0} + eval suffix=3D\${suffix_${IFVAR}:-1} + eval relay=3D\${relay_${IFVAR}} + eval prefix=3D\${prefix_${IFVAR}} + + IFACE=3D${host} + addrs=3D$(_get_inet_addresses) + IFACE=3D${iface} + if [ -z "${addrs}" ]; then + eerror "${host} is not configured with an IPv4 address" + return 1 + fi + # TODO: Get this settings from DHCP (Option 212)=20 + if [ -z "${prefix}" ]; then + eerror "prefix_${IFVAR} not set" + return 1 + fi + if [ -z "${relay}" ]; then + eerror "relay_${IFVAR} not set" + return 1 + fi + for addr in ${addrs}; do + # Strip the subnet + local ip=3D"${addr%/*}" subnet=3D"${addr#*/}" + # We don't work on private IPv4 addresses + if _ip6rd_inet_is_private_network "${ip}" + then + continue + fi + + local ip6=3D ip6_prefix=3D"${prefix%::/*}" ip6_subnet=3D"${prefix#*/}" + ip6_subnet=3D$((ip6_subnet + (32-ipv4mask))) + eval ip6=3D"$(printf "${ip6_prefix}:%s::%s" \ + $(_ip6rd_prefix_shave_bits ${ip} ${ipv4mask}) ${suffix})" + veinfo "Derived IPv6 address: ${ip6}" + + # Now apply our IPv6 address to our config + config_ip6rd=3D"${config_ip6rd}${config_ip6rd:+ }${ip6}/${ip6_subnet}" + + if [ -n "${localip}" ]; then + localip=3D"any" + else + localip=3D"${ip}" + fi + done + + if [ -z "${config_ip6rd}" ]; then + eerror "No global IPv4 addresses found on interface ${host}" + return 1 + fi + + ebegin "Creating 6rd tunnel ${IFACE}" + if [ "${IFACE}" !=3D "sit0" ]; then + _tunnel add "${IFACE}" mode sit ttl 255 remote any local "${localip}" + fi + _tunnel 6rd dev "${IFACE}" 6rd-prefix "${prefix}" + eend $? || return 1 + _up + + routes_ip6rd=3D"2003::/3 via ::${relay} metric 2147483647" + service_set_value "config_ip6rd_$IFVAR" "$config_ip6rd" + service_set_value "routes_ip6rd_$IFVAR" "$routes_ip6rd" +} + +ip6rd_start() +{ + local config_ip6rd=3D$(service_get_value "config_ip6rd_$IFVAR") + local routes_ip6rd=3D$(service_get_value "routes_ip6rd_$IFVAR") + + # Now apply our config + eval config_${config_index}=3D\'"${config_ip6rd}"\' + : $(( config_index -=3D 1 )) + + # Add a route for us, ensuring we don't delete anything else + local routes=3D"$(_get_array "routes_${IFVAR}") +$routes_ip6rd" + eval routes_${IFVAR}=3D\$routes +} + +_ip6rd_inet_atoi() +{ + local IFS=3D"${IFS}." ipi=3D0 j=3D3 + for i in $1 ; do + ipi=3D$(( ipi | i << 8*j-- )) + done + echo ${ipi} +} + +_ip6rd_inet_itoa() +{ + local ipi=3D$1 + for i in 0 1 2 3; do + if [ $i !=3D 3 ] ; then + printf "%d." $(( (ipi & ~((1<<24)-1)) >> 24 )) + ipi=3D$(( (ipi & ((1<<24)-1)) << 8)) + else + printf "%d\n" $(( (ipi & ~((1<<24)-1)) >> 24 )) + fi + done +} + +_ip6rd_inet_get_network() +{ + echo $(_ip6rd_inet_itoa $(( ($(_ip6rd_inet_atoi $1) & ((1<<$2)-1) << (3= 2-$2) ) )) ) +} + +_ip6rd_inet_is_private_network() +{ + if [ "$(_ip6rd_inet_get_network $1 16)" =3D "192.168.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 8)" =3D "10.0.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 12)" =3D "172.16.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 16)" =3D "169.254.0.0" ] + then + return 0; + fi + return 1; +} + +_ip6rd_prefix_shave_bits() +{ + local ipi=3D + ipi=3D$(( ($(_ip6rd_inet_atoi $1) & (1<<(32-$2))-1) << $2)) + if [ $2 -le 16 ] + then + printf "%04x:%0$(( (16-$2>>2)+(($2%4)?1:0) ))x" \ + $((ipi >> 16)) $((ipi & (1<<(16-$2))-1)) + elif [ $2 -lt 32 ] + then + printf "%0$(( (32-$2>>2)+(($2%4)?1:0) ))x" \ + $((ipi >> 16)) + fi=09 +}