Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 01 Feb 2012 05:04:39 +0100
From:      Dirk Engling <erdgeist@erdgeist.org>
To:        freebsd-rc@freebsd.org
Subject:   Proposal ipv6_addrs_common
Message-ID:  <4F28B9D7.4010602@erdgeist.org>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------040100090500080408060203
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Dear FreeBSD rc team,

echoing Philipp Wuensches great ipv4_addrs patch I'd like to contribute
the corresponding patch for v6 addresses, as their config clobbers my
rc.conf considerably.

The attached network6.subr is a shell script demonstrating the
ipv6_addrs_common function inside, for playing around one can use some
of the values the supplied get_if_var dummy function returns.

The function handles one single range but allows it in any parts of the
ip address, so

  2002:50ed:991c-991f:50ed:c401::/64
  2002:50ed:991c:50ed:c401-c409::

are both valid and work as expected. It can also parse ranges in
v6mapped v4 addresses, again anywhere, so configuring a router that
listens on all subnets on address .1 is possible:

  ::ffff:192.168.2-16.1

I would have the function called in ifn_start and ifn_stop.

Feedback is very welcome. Thank you for your attention.

  erdgeist

P.S.: Since I am not sure the .subr file will pass the mailing list
filters, I copied the file to http://erdgeist.org/network6.subr

--------------040100090500080408060203
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
	name="network6.subr"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="network6.subr"

#!/bin/sh

get_if_var() {
# echo "2002:50ed:991c:50ed:c401-c409::"
# echo "2002:50ed:991c:50ed:c401-c409::c409"
 echo "::ffff:192.168.2-4.2/12"
# echo "::ffff:192.168.2.23-25/12"
# echo "::ffff:192.168.2.23/12"
# echo "2002:50ed:991c:50ed:c401::"
# echo "2002:50ed:991c-991f:50ed:c401::"
}

ipv6_addrs_common()
{
    _ret=1
    _if=$1
    _action=$2

    # get ipv6-addresses
    cidr_addr=`get_if_var $_if ipv6_addrs_IF`

    for _cidr in ${cidr_addr}; do
        _ipaddr="${_cidr%%/*}"

        [ "${_ipaddr}" != "${_cidr}" ] && _netmask="/"${_cidr##*/} || unset _netmask
        [ "$_action" = "-alias" ] && unset _netmask

        if [ "${_ipaddr%:*.*.*.*}" = "${_ipaddr}" ]; then
            _ipleft=${_ipaddr%-*}
            _ipright=${_ipaddr#*-}
            _iplow=${_ipleft##*:}
            _iphigh=${_ipright%%:*}
            _ipleft=${_ipleft%:*}
            _ipright=${_ipright#*:}

            [ "${_iphigh}" = "${_ipright}" ] && unset _ipright || _ipright=:$_ipright

            if [ "${_iplow}" ]; then
                while [ $(( 0x$_iplow )) -le $(( 0x$_iphigh )) ]; do
                    echo ifconfig ${_if} inet6 ${_ipleft}:${_iplow}${_ipright}${_netmask} ${_action}
                    _iplow=`printf %04x $(( 0x$_iplow + 1 ))`
                    _ret=0
                done
            else
                # no range
                echo ifconfig ${_if} inet6 ${_ipaddr}${_netmask} ${_action}
                _ret=0
            fi
        else
            # v4 range
            _ipv6part=${_ipaddr%:*}
            _ipv4part=${_ipaddr##*:}
            _ipleft=${_ipv4part%-*}
            _ipright=${_ipv4part#*-}

            _iplow=${_ipleft##*.}
            _iphigh=${_ipright%%.*}
            _ipleft=${_ipleft%.*}
            _ipright=${_ipright#*.}

            [ "${_iphigh}" = "${_ipright}" ] && unset _ipright || _ipright=.$_ipright

            if [ "${_iplow}" ]; then
                while [ ${_iplow} -le ${_iphigh} ]; do
                    echo ifconfig ${_if} inet6 ${_ipv6part}:${_ipleft}.${_iplow}${_ipright}${_netmask} ${_action}
                    : $(( _iplow++ ))
                    _ret=0
                done
            else
                # no range
                echo ifconfig ${_if} inet6 ${_ipaddr}${_netmask} ${_action}
                _ret=0
            fi
        fi
    done
    return $_ret
}

ipv6_addrs_common if0 alias

--------------040100090500080408060203--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4F28B9D7.4010602>