
# syntax for rc.conf
ipv4_fxp0_addr="23.23.1.1/28 23.23.2.1-2/24 23.23.3.2-3/24 23.23.4.5-8/24"

# ipv4_addrs_common if action
#   Evaluate the ifconfig_if_ipv4 arguments for interface $if
#   and use $action to add or remove IPv4 addresses from $if.
ipv4_addr_common()
{
    _if=$1
    _action=$2
    
    # get ipv4-addresses
    eval cidr_addr=\${ipv4_${_if}_addr}
    
    for _cidr in ${cidr_addr}; do
        _ipaddr=${_cidr%%/*}
        _netmask="/"${_cidr##*/}
        _range=${_ipaddr##*.}
        _ipnet=${_ipaddr%.*}
        _iplow=${_range%-*}
        _iphigh=${_range#*-}
                
        # clear netmask when removing aliases
        if [ "${_action}" = "-alias" ]; then
            _netmask=""
        fi
        
        _ipcount=${_iplow}
        while [ "${_ipcount}" -le "${_iphigh}" ]; do
            echo "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
            _ipcount=$((${_ipcount}+1))
            
            # only the first ipaddr in a subnet need the real netmask
            if [ "${_action}" != "-alias" ]; then
                _netmask="/32"
            fi
        done
    done
}

ipv4_up()
{
    _if=$1
    # ifalias_up ${_if}
    ipv4_addr_common ${_if} alias
}

ipv4_down()
{
    _if=$1
    # ifalias_down ${_if}
    ipv4_addr_common ${_if} -alias
}

ipv4_up fxp0
ipv4_down fxp0