Blame network-scripts/network-functions-ipv6

rpm-build 133ac4
# network-functions-ipv6
rpm-build 133ac4
#
rpm-build 133ac4
# Taken from: network-functions-ipv6
rpm-build 133ac4
# (P) & (C) 1997-2005 by Peter Bieringer <pb@bieringer.de>
rpm-build 133ac4
#
rpm-build 133ac4
#  You will find more information on the initscripts-ipv6 homepage at
rpm-build 133ac4
#   http://www.deepspace6.net/projects/initscripts-ipv6.html
rpm-build 133ac4
#
rpm-build 133ac4
# Version: 2006-08-03
rpm-build 133ac4
#
rpm-build 133ac4
#
rpm-build 133ac4
Packit Service 8029ae
# Source network-functions due to need of set_link_up()
Packit Service 8029ae
. ./network-functions
rpm-build 133ac4
rpm-build 133ac4
##### Test for IPv6 capabilities
rpm-build 133ac4
# $1: (optional) testflag: currently supported: "testonly" (do not load a module)
rpm-build 133ac4
# return code: 0=ok 2=IPv6 test fails
rpm-build 133ac4
ipv6_test() {
rpm-build 133ac4
    local fn="ipv6_test"
rpm-build 133ac4
rpm-build 133ac4
    local testflag=$1
rpm-build 133ac4
rpm-build 133ac4
    if ! [ -f /proc/net/if_inet6 ]; then
rpm-build 133ac4
        if [ "$testflag" = "testonly" ]; then
rpm-build 133ac4
            return 2
rpm-build 133ac4
        else
rpm-build 133ac4
            modprobe ipv6
rpm-build 133ac4
rpm-build 133ac4
            if ! [ -f /proc/net/if_inet6 ]; then
rpm-build 133ac4
                return 2
rpm-build 133ac4
            fi
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if ! [ -d /proc/sys/net/ipv6/conf/ ]; then
rpm-build 133ac4
        return 2
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
##### Static IPv6 route configuration
rpm-build 133ac4
rpm-build 133ac4
# Set static IPv6 route
rpm-build 133ac4
#  $1: <IPv6 network> : to route
rpm-build 133ac4
#  $2: <IPv6 gateway> : over which $1 should be routed (if "::", gw will be skipped)
rpm-build 133ac4
#  $3: [<Interface>] : (optional)
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem adding route
rpm-build 133ac4
ipv6_add_route() {
rpm-build 133ac4
    local fn="ipv6_add_route"
rpm-build 133ac4
rpm-build 133ac4
    local networkipv6=$1
rpm-build 133ac4
    local gatewayipv6=$2
rpm-build 133ac4
    local device=$3        # maybe empty
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$networkipv6" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv6-network' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$gatewayipv6" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv6-gateway' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_ipv6_addr_valid $networkipv6 || return 2
rpm-build 133ac4
    ipv6_test_ipv6_addr_valid $gatewayipv6 || return 2
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        local returntxt="$(/sbin/ip -6 route add $networkipv6 via $gatewayipv6 metric 1 2>&1)"
rpm-build 133ac4
    else
rpm-build 133ac4
        if [ "$gatewayipv6" = "::" ]; then
rpm-build 133ac4
            local returntxt="$(/sbin/ip -6 route add $networkipv6 dev $device metric 1 2>&1)"
rpm-build 133ac4
        else
rpm-build 133ac4
            local returntxt="$(/sbin/ip -6 route add $networkipv6 via $gatewayipv6 dev $device metric 1 2>&1)"
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -n "$returntxt" ]; then
rpm-build 133ac4
        if echo $returntxt | LC_ALL=C grep -q "File exists"; then
rpm-build 133ac4
            # Netlink: "File exists"
rpm-build 133ac4
            true
rpm-build 133ac4
        elif echo $returntxt | LC_ALL=C grep -q "No route to host"; then
rpm-build 133ac4
            # Netlink: "No route to host"
rpm-build 133ac4
            net_log $"'No route to host' adding route '$networkipv6' via gateway '$gatewayipv6' through device '$device'" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        else
rpm-build 133ac4
            net_log $"Unknown error" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
##### automatic tunneling configuration
rpm-build 133ac4
rpm-build 133ac4
## Configure automatic tunneling up
rpm-build 133ac4
# return code: 0=ok 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_enable_autotunnel() {
rpm-build 133ac4
    local fn="ipv6_enable_autotunnel"
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
    # enable IPv6-over-IPv4 tunnels
rpm-build 133ac4
    if ipv6_test_device_status sit0; then
rpm-build 133ac4
        true
rpm-build 133ac4
    else
rpm-build 133ac4
        # bring up basic tunnel device
Packit Service 8029ae
        set_link_up sit0
rpm-build 133ac4
rpm-build 133ac4
            if ! ipv6_test_device_status sit0; then
rpm-build 133ac4
                net_log $"Tunnel device 'sit0' enabling didn't work" err $fn
rpm-build 133ac4
                return 3
rpm-build 133ac4
            fi
rpm-build 133ac4
rpm-build 133ac4
        # Set sysctls proper (regardless "default")
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.sit0.forwarding=1 >/dev/null 2>&1
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.sit0.accept_ra=0 >/dev/null 2>&1
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.sit0.accept_redirects=0 >/dev/null 2>&1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
##### Interface configuration
rpm-build 133ac4
rpm-build 133ac4
## Add an IPv6 address for given interface
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
#  $2: <IPv6 address[/prefix]>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_add_addr_on_device() {
rpm-build 133ac4
    local fn="ipv6_add_addr_on_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local address=$2
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$address" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv6-address' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_ipv6_addr_valid $address || return 1
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_device_status $device
rpm-build 133ac4
    local result=$?
rpm-build 133ac4
rpm-build 133ac4
    if [ "$result" = "0" ]; then
rpm-build 133ac4
        true
rpm-build 133ac4
    elif [ "$result" != "11" ]; then
rpm-build 133ac4
        net_log $"Device '$device' doesn't exist" err $fn
rpm-build 133ac4
        return 3
rpm-build 133ac4
    else
Packit Service 8029ae
        set_link_up $device
rpm-build 133ac4
rpm-build 133ac4
            if ! ipv6_test_device_status $device; then
rpm-build 133ac4
                net_log $"Device '$device' enabling didn't work" err $fn
rpm-build 133ac4
                return 3
rpm-build 133ac4
            fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Extract address parts
rpm-build 133ac4
    local prefixlength_implicit="$(echo $address | awk -F/ '{ print $2 }')"
rpm-build 133ac4
    local address_implicit="${address%%/*}"
rpm-build 133ac4
rpm-build 133ac4
    # Check prefix length and using '64' as default
rpm-build 133ac4
    if [ -z "$prefixlength_implicit" ]; then
rpm-build 133ac4
        local prefixlength_implicit="64"
rpm-build 133ac4
        local address="$address_implicit/$prefixlength_implicit"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    /sbin/ip -6 addr replace $address dev $device
rpm-build 133ac4
    local result=$?
rpm-build 133ac4
rpm-build 133ac4
    if [ $result -eq 2 ]; then
rpm-build 133ac4
        return 0
rpm-build 133ac4
    elif [ $result -ne 0 ]; then
rpm-build 133ac4
        net_log $"Cannot add IPv6 address '$address' on dev '$device'" err $fn
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Remove all IPv6 routes and addresses on given interface (cleanup to prevent kernel crashes)
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_cleanup_device() {
rpm-build 133ac4
    local fn="ipv6_cleanup_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Remove all IPv6 routes through this device (but not "lo")
rpm-build 133ac4
    if [ "$device" != "lo" ]; then
rpm-build 133ac4
        /sbin/ip -6 route flush dev $device scope global >/dev/null 2>&1
rpm-build 133ac4
        /sbin/ip -6 route flush dev $device scope site     >/dev/null 2>&1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Remove all IPv6 addresses on this interface
rpm-build 133ac4
    /sbin/ip -6 addr flush dev $device scope global >/dev/null 2>&1
rpm-build 133ac4
    /sbin/ip -6 addr flush dev $device scope site     >/dev/null 2>&1
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Remove all IPv6 6to4 related routes and addresses on given interface
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_cleanup_6to4_device() {
rpm-build 133ac4
    local fn="ipv6_cleanup_6to4_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Cleanup 6to4 addresses on this device
rpm-build 133ac4
    /sbin/ip -6 addr show dev $device scope global permanent | awk '/\<inet6\>/ && $2 ~ /^2002:/ { print $2 }' | while read addr; do
rpm-build 133ac4
        /sbin/ip -6 addr del ${addr} dev ${device}
rpm-build 133ac4
    done
rpm-build 133ac4
rpm-build 133ac4
    # Get all IPv6 routes through given interface related to 6to4 and remove them
rpm-build 133ac4
    /sbin/ip -6 route show dev $device | LC_ALL=C grep "^2002:" | while read ipv6net dummy; do
rpm-build 133ac4
        /sbin/ip -6 route del $ipv6net dev $device
rpm-build 133ac4
    done
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
##### Some address test functions
rpm-build 133ac4
rpm-build 133ac4
## Test a given IPv6 address for validity
rpm-build 133ac4
#  $1: <IPv6 address>
rpm-build 133ac4
# return code: 0=ok 1=not valid
rpm-build 133ac4
ipv6_test_ipv6_addr_valid() {
rpm-build 133ac4
    ipcalc -cs6 $1
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Test a given IPv4 address for validity
rpm-build 133ac4
#  $1: <IPv4 address>
rpm-build 133ac4
# return code: 0=ok 1=not valid
rpm-build 133ac4
ipv6_test_ipv4_addr_valid() {
rpm-build 133ac4
    ipcalc -cs4 $1
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Test a given IPv4 address for not a private but unicast one
rpm-build 133ac4
#  $1: <IPv4 address>
rpm-build 133ac4
# return code: 0=ok 1=argument error 10=private or not unicast
rpm-build 133ac4
ipv6_test_ipv4_addr_global_usable() {
rpm-build 133ac4
    local fn="ipv6_test_ipv4_addr_global_usable"
rpm-build 133ac4
rpm-build 133ac4
    local testipv4addr_globalusable=$1
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$testipv4addr_globalusable" ]; then
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Test for a globally usable IPv4 address now
rpm-build 133ac4
        # test 0.0.0.0/8
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0     | LC_ALL=C grep -q "NETWORK=0\.0\.0\.0"         && return 10
rpm-build 133ac4
        # test 10.0.0.0/8         (RFC 1918 / private)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0     | LC_ALL=C grep -q "NETWORK=10\.0\.0\.0"        && return 10
rpm-build 133ac4
        # test 127.0.0.0/8        (loopback)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0     | LC_ALL=C grep -q "NETWORK=127\.0\.0\.0"     && return 10
rpm-build 133ac4
        # test 169.254.0.0/16 (APIPA / DHCP link local)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=169\.254\.0\.0" && return 10
rpm-build 133ac4
        # test 172.16.0.0/12    (RFC 1918 / private)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.240.0.0 | LC_ALL=C grep -q "NETWORK=172\.16\.0\.0"    && return 10
rpm-build 133ac4
        # test 192.168.0.0/16 (RFC 1918 / private)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=192\.168\.0\.0" && return 10
rpm-build 133ac4
        # test 224.0.0.0/3        (multicast and reserved, broadcast)
rpm-build 133ac4
        /bin/ipcalc --network $testipv4addr_globalusable 224.0.0.0     | LC_ALL=C grep -q "NETWORK=224\.0\.0\.0"     && return 10
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Test a given device for status
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# return code: 0=ok 1=argument error 10=not exists 11=down
rpm-build 133ac4
ipv6_test_device_status() {
rpm-build 133ac4
    local fn="ipv6_test_device_status"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Test if device exists
rpm-build 133ac4
    if [ ! -d "/sys/class/net/${device}" ]; then
rpm-build 133ac4
        # not exists
rpm-build 133ac4
        return 10
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Test if device is up
rpm-build 133ac4
    if /sbin/ip link show dev $device 2>/dev/null | LC_ALL=C grep -q "UP"; then
rpm-build 133ac4
        # up
rpm-build 133ac4
        return 0
rpm-build 133ac4
    else
rpm-build 133ac4
        # down
rpm-build 133ac4
        return 11
rpm-build 133ac4
    fi
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Create 6to4 prefix
rpm-build 133ac4
#  $1: <IPv4 address>
rpm-build 133ac4
# stdout: <6to4address>
rpm-build 133ac4
# return code: 0=ok 1=argument error
rpm-build 133ac4
ipv6_create_6to4_prefix() {
rpm-build 133ac4
    local fn="ipv6_create_6to4_prefix"
rpm-build 133ac4
rpm-build 133ac4
    local ipv4addr=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$ipv4addr" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv4 address' (arg 1)" err $fn
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    local major1="${ipv4addr%%.*}"
rpm-build 133ac4
    local minor1="$(echo $ipv4addr | awk -F. '{ print $2 }')"
rpm-build 133ac4
    local major2="$(echo $ipv4addr | awk -F. '{ print $3 }')"
rpm-build 133ac4
    local minor2="$(echo $ipv4addr | awk -F. '{ print $4 }')"
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$major1" -o -z "$minor1" -o -z "$major2" -o -z "$minor2" ]; then
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ $major1 -eq 0 ]; then
rpm-build 133ac4
        local block1="$(printf "%x" $minor1)"
rpm-build 133ac4
    else
rpm-build 133ac4
        local block1="$(printf "%x%02x" $major1 $minor1)"
rpm-build 133ac4
    fi
rpm-build 133ac4
    if [ $major2 -eq 0 ]; then
rpm-build 133ac4
        local block2="$(printf "%x" $minor2)"
rpm-build 133ac4
    else
rpm-build 133ac4
        local block2="$(printf "%x%02x" $major2 $minor2)"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    local prefix6to4="2002:$block1:$block2"
rpm-build 133ac4
rpm-build 133ac4
    echo "$prefix6to4"
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Check and create 6to4 tunnel relay address
rpm-build 133ac4
#  $1: <IPv4 address|IPv6to4 address>
rpm-build 133ac4
# stdout: <tunnel relay address>
rpm-build 133ac4
# return code: 0=ok 1=argument error
rpm-build 133ac4
ipv6_create_6to4_relay_address() {
rpm-build 133ac4
    local fn="ipv6_create_6to4_relay_address"
rpm-build 133ac4
rpm-build 133ac4
    local addr=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$addr" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'address' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Check
rpm-build 133ac4
    if ipv6_test_ipv4_addr_valid $addr ; then
rpm-build 133ac4
        # ok, an IPv4 one
rpm-build 133ac4
        if ipv6_test_ipv4_addr_global_usable $addr; then
rpm-build 133ac4
            # IPv4 globally usable
rpm-build 133ac4
            local ipv6to4_relay="::$addr"
rpm-build 133ac4
        else
rpm-build 133ac4
            net_log $"Given address '$addr' is not a global IPv4 one (arg 1)" err $fn
rpm-build 133ac4
            return 1
rpm-build 133ac4
        fi
rpm-build 133ac4
    else
rpm-build 133ac4
        net_log $"Given address '$addr' is not a valid IPv4 one (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    echo "$ipv6to4_relay"
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
##### 6to4 tunneling setup
rpm-build 133ac4
rpm-build 133ac4
## Configure 6to4 tunneling up
rpm-build 133ac4
#  $1: <Interface> : only "tun6to4" is supported
rpm-build 133ac4
#  $2: <IPv4 address> : global IPv4 address of interface (will be used to generate 6to4 prefix)
rpm-build 133ac4
#  $3: [<IPv6 suffix>] : for 6to4 prefix (optional, default is "::1")
rpm-build 133ac4
#  $4: [<MTU>] : MTU of tunnel device (optional, default is automatic)
rpm-build 133ac4
#  $5: [<IPv4 address>] : local IPv4 address of tunnel interface (required in case of 6to4 behind NAT)
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_add_6to4_tunnel() {
rpm-build 133ac4
    local fn="ipv6_add_6to4_tunnel"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local globalipv4=$2
rpm-build 133ac4
    local globalipv6to4suffix=$3
rpm-build 133ac4
    local mtu=$4
rpm-build 133ac4
    local localipv4=$5
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$globalipv4" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'global IPv4 address' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Check device
rpm-build 133ac4
    if [ "$device" != "tun6to4" ]; then
rpm-build 133ac4
        net_log $"Given device '$device' is not supported (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Copy global IPv4 address to local if last one is not given
rpm-build 133ac4
    if [ -z "$localipv4" ]; then
rpm-build 133ac4
        localipv4="$globalipv4"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Generate 6to4 address
rpm-build 133ac4
    local prefix6to4="$(ipv6_create_6to4_prefix $globalipv4)"
rpm-build 133ac4
    if [ $? -ne 0 -o -z "$prefix6to4" ]; then
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$globalipv6to4suffix" ]; then
rpm-build 133ac4
        local address6to4="${prefix6to4}::1/16"
rpm-build 133ac4
    else
rpm-build 133ac4
        local address6to4="${prefix6to4}::${globalipv6to4suffix}/16"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
        ipv6_add_tunnel_device tun6to4 0.0.0.0 $address6to4 $localipv4
rpm-build 133ac4
        if [ $? -ne 0 ]; then
rpm-build 133ac4
            local retval=3
rpm-build 133ac4
        else
rpm-build 133ac4
            local retval=0
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Add unspecific unreachable route for local 6to4 address space
rpm-build 133ac4
        /sbin/ip route add unreach ${prefix6to4}::/48
rpm-build 133ac4
rpm-build 133ac4
    # Set MTU, if given
rpm-build 133ac4
    if [ -n "$mtu" ]; then
rpm-build 133ac4
        ipv6_set_mtu $device $mtu
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return $retval
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Configure all 6to4 tunneling down
rpm-build 133ac4
#  $1: <Interface> : only "tun6to4" is supported
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_cleanup_6to4_tunnels() {
rpm-build 133ac4
    local fn="ipv6_cleanup_6to4_tunnels"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Check device
rpm-build 133ac4
    if [ "$device" != "tun6to4" ]; then
rpm-build 133ac4
        net_log $"Given device '$device' is not supported (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
        ipv6_del_tunnel_device tun6to4
rpm-build 133ac4
rpm-build 133ac4
        # Remove all unspecific unreachable routes for local 6to4 address space
rpm-build 133ac4
        /sbin/ip -6 route | LC_ALL=C grep "^unreachable 2002:.*/48 dev lo" | while read token net rest; do
rpm-build 133ac4
            /sbin/ip route del unreach $net
rpm-build 133ac4
        done
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Configure 6to4 tunneling down
rpm-build 133ac4
#  $1: <Interface> : only "tun6to4" is supported
rpm-build 133ac4
#  $2: <IPv4 address> : global address of local interface
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_del_6to4_tunnel() {
rpm-build 133ac4
    local fn="ipv6_del_6to4_tunnel"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local localipv4=$2
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$localipv4" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'local IPv4 address' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Check device
rpm-build 133ac4
    if [ "$device" != "tun6to4" ]; then
rpm-build 133ac4
        net_log $"Given device '$device' is not supported (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
        ipv6_del_tunnel_device tun6to4
rpm-build 133ac4
        local retval=$?
rpm-build 133ac4
rpm-build 133ac4
        # Remove unspecific unreachable route for local 6to4 address space
rpm-build 133ac4
        /sbin/ip route del unreach ${prefix6to4}::/48
rpm-build 133ac4
rpm-build 133ac4
    return $retval
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Configure a static tunnel device up
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
#  $2: <IPv4 address> : of foreign tunnel
rpm-build 133ac4
#  $3: [<IPv6 address>] : local one of a P-t-P tunnel (optional)
rpm-build 133ac4
#  $4: [<IPv4 address>] : local one of tunnel (optional)
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_add_tunnel_device() {
rpm-build 133ac4
    local fn="ipv6_add_tunnel_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local addressipv4tunnel=$2
rpm-build 133ac4
    local addressipv6local=$3
rpm-build 133ac4
    local addressipv4tunnellocal=$4
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$addressipv4tunnel" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$addressipv4tunnellocal" ]; then
rpm-build 133ac4
        local addressipv4tunnellocal="any"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test || return 2
rpm-build 133ac4
rpm-build 133ac4
    if ! ipv6_test_device_status $device; then
rpm-build 133ac4
        local ttldefault="$(/sbin/sysctl -e net.ipv4.ip_default_ttl | awk '{ print $3 }')"
rpm-build 133ac4
        if [ -z "$ttldefault" ]; then
rpm-build 133ac4
            local ttldefault=64
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Test whether remote IPv4 address was already applied to another tunnel
rpm-build 133ac4
        if [ "$addressipv4tunnel" != "0.0.0.0" -a "$addressipv4tunnel" != "any" ]; then
rpm-build 133ac4
            /sbin/ip tunnel show remote $addressipv4tunnel 2>/dev/null | LC_ALL=C grep -w "ipv6/ip" | while IFS=":" read devnew rest; do
rpm-build 133ac4
                if [ "$devnew" != "$device" ]; then
rpm-build 133ac4
                    net_log $"Given remote address '$addressipv4tunnel' on tunnel device '$device' is already configured on device '$devnew'" err $fn
rpm-build 133ac4
                    return 3
rpm-build 133ac4
                fi
rpm-build 133ac4
            done
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        /sbin/ip tunnel add $device mode sit ttl $ttldefault remote $addressipv4tunnel local $addressipv4tunnellocal
rpm-build 133ac4
        if [ $? -ne 0 ]; then
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Test, whether "ip tunnel show" reports valid content
rpm-build 133ac4
        if ! /sbin/ip tunnel show $device 2>/dev/null | LC_ALL=C grep -q -w "remote"; then
rpm-build 133ac4
            net_log $"Tunnel device '$device' creation didn't work" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
Packit Service 8029ae
        set_link_up $device
rpm-build 133ac4
rpm-build 133ac4
        if ! ipv6_test_device_status $device; then
rpm-build 133ac4
            net_log $"Tunnel device '$device' bringing up didn't work" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Set sysctls proper (regardless "default")
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.forwarding=1 >/dev/null 2>&1
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.accept_ra=0 >/dev/null 2>&1
rpm-build 133ac4
        /sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.accept_redirects=0 >/dev/null 2>&1
rpm-build 133ac4
rpm-build 133ac4
        if [ -n "$addressipv6local" ]; then
rpm-build 133ac4
            # Setup P-t-P address
rpm-build 133ac4
            ipv6_add_addr_on_device $device $addressipv6local
rpm-build 133ac4
            if [ $? -ne 0 ]; then
rpm-build 133ac4
                return 3
rpm-build 133ac4
            fi
rpm-build 133ac4
        fi
rpm-build 133ac4
    else
rpm-build 133ac4
        false
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Configure a static tunnel device down
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_del_tunnel_device() {
rpm-build 133ac4
    local fn="ipv6_del_tunnel_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    if ipv6_test_device_status $device; then
rpm-build 133ac4
        ipv6_cleanup_device $device
rpm-build 133ac4
    else
rpm-build 133ac4
        if [ "$device" != "sit0" ]; then
rpm-build 133ac4
            false
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ "$device" != "sit0" ]; then
rpm-build 133ac4
        if /sbin/ip tunnel show $device 2>/dev/null | LC_ALL=C grep -q -w "ipv6/ip"; then
rpm-build 133ac4
            /sbin/ip tunnel del $device
rpm-build 133ac4
rpm-build 133ac4
            if ipv6_test_device_status $device; then
rpm-build 133ac4
                return 3
rpm-build 133ac4
            fi
rpm-build 133ac4
        else
rpm-build 133ac4
            false
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Cleanup all dedicated tunnel devices
rpm-build 133ac4
ipv6_cleanup_tunnel_devices() {
rpm-build 133ac4
    local fn="ipv6_cleanup_tunnel_devices"
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Find still existing tunnel devices and shutdown and delete them
rpm-build 133ac4
rpm-build 133ac4
    /sbin/ip tunnel show | awk -F: '/\<ipv6\/ip\>/ { print $1 }' | while read device; do
rpm-build 133ac4
        ipv6_del_tunnel_device $device
rpm-build 133ac4
    done
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Get address of a dedicated tunnel
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
#  $2: local|remote : local or remote address
rpm-build 133ac4
# stdout: <IPv4 address> if available
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_get_ipv4addr_of_tunnel() {
rpm-build 133ac4
    local fn="ipv6_get_local_ipv4_of_tunnel"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local selection=$2
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$selection" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'selection' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
    if [ "$selection" != "local" -a "$selection" != "remote" ]; then
rpm-build 133ac4
        net_log $"Unsupported selection '$selection' specified (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_device_status $device
rpm-build 133ac4
rpm-build 133ac4
    if [ $? != 0 -a $? != 11 ]; then
rpm-build 133ac4
        # Device doesn't exist
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Device exists, retrieve address
rpm-build 133ac4
    if [ "$selection" = "local" ]; then
rpm-build 133ac4
        local tunnel_local_ipv4addr="$(/sbin/ip tunnel show $device | awk '{ print $6 }')"
rpm-build 133ac4
    elif [ "$selection" = "remote" ]; then
rpm-build 133ac4
        local tunnel_local_ipv4addr="$(/sbin/ip tunnel show $device | awk '{ print $4 }')"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ $? != 0 ]; then
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ "$tunnel_local_ipv4addr" = "any" ]; then
rpm-build 133ac4
        local tunnel_local_ipv4addr="0.0.0.0"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    echo "$tunnel_local_ipv4addr"
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Get IPv4 address of a device
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# stdout: <IPv4 address> if available
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem (more than one IPv4 address applied)
rpm-build 133ac4
ipv6_get_ipv4addr_of_device() {
rpm-build 133ac4
    local fn="ipv6_get_ipv4addr_of_device"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_device_status $device
rpm-build 133ac4
rpm-build 133ac4
    if [ $? != 0 -a $? != 11 ]; then
rpm-build 133ac4
        # Device doesn't exist
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Device exists, retrieve the first address only
rpm-build 133ac4
    local ipv4addr="$(/sbin/ip -o -4 addr show dev $device | awk '{ print $4 }' | awk -F/ '{ print $1; exit }')"
rpm-build 133ac4
rpm-build 133ac4
    if [ $? != 0 ]; then
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ "$ipv4addr" = "any" ]; then
rpm-build 133ac4
        local ipv4addr="0.0.0.0"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    echo "$ipv4addr"
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Set IPv6 MTU for a device
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
#  $2: <IPv6 MTU>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_set_mtu() {
rpm-build 133ac4
    local fn="ipv6_set_mtu"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local ipv6_mtu=$2
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$ipv6_mtu" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'IPv6 MTU' (arg 2)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Check range
rpm-build 133ac4
    if [ $ipv6_mtu -lt 1280 -o $ipv6_mtu -gt 65535 ]; then
rpm-build 133ac4
        net_log $"Given IPv6 MTU '$ipv6_mtu' is out of range" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Set value
rpm-build 133ac4
    /sbin/ip link set dev $device mtu $ipv6_mtu
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Set a default route
rpm-build 133ac4
#  $1: <IPv6 address> : gateway, can also contain scope suffix (device name), cause a warning if not matching with $2 (but will have precedence)
rpm-build 133ac4
#  $2: <gateway device>: gateway device (optional in case of $1 is a global address or $1 contains scope suffix)
rpm-build 133ac4
#  $3: <check device>: (optional) device to check scope and gateway device against (setup is skipped, if not matching)
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_set_default_route() {
rpm-build 133ac4
    local fn="ipv6_set_default_route"
rpm-build 133ac4
rpm-build 133ac4
    local address=$1
rpm-build 133ac4
    local device=$2
rpm-build 133ac4
    local device_check=$3
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    # Map the unspecified address to nothing
rpm-build 133ac4
    if [ "$address" = "::" ]; then
rpm-build 133ac4
        local address=""
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -n "$address" ]; then
rpm-build 133ac4
        local addressgw=${address%%%*}
rpm-build 133ac4
        local device_scope=$(echo $address | awk -F% '{ print $2 }')
rpm-build 133ac4
rpm-build 133ac4
        if [ -z "$addressgw" ]; then
rpm-build 133ac4
            net_log $"Given IPv6 default gateway '$address' is not in proper format" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Scope device has precedence
rpm-build 133ac4
        if [ -n "$device_scope" -a -n "$device" -a "$device_scope" != "$device" ]; then
rpm-build 133ac4
            net_log $"Given IPv6 default gateway '$address' has scope '$device_scope' defined, given default gateway device '$device' will be not used" info $fn
rpm-build 133ac4
            local device=""
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Link local addresses require a device
rpm-build 133ac4
        if echo $addressgw | LC_ALL=C grep -qi "^fe80:"; then
rpm-build 133ac4
            if [ -z "$device_scope" ]; then
rpm-build 133ac4
                if [ -z "$device" ]; then
rpm-build 133ac4
                    net_log $"Given IPv6 default gateway '$address' is link-local, but no scope or gateway device is specified" err $fn
rpm-build 133ac4
                    return 3
rpm-build 133ac4
                fi
rpm-build 133ac4
            fi
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Check whether the route belongs to the specific given interface
rpm-build 133ac4
        if [ -n "$device_check" ]; then
rpm-build 133ac4
            # Check whether scope device matches given check device
rpm-build 133ac4
            if [ -n "$device_scope" -a "$device_check" != "$device_scope" ]; then
rpm-build 133ac4
                # scope device != specific given -> skip
rpm-build 133ac4
                return 0
rpm-build 133ac4
            elif [ -n "$device" -a "$device_check" != "$device" ]; then
rpm-build 133ac4
                # gateway device != specific given -> skip
rpm-build 133ac4
                return 0
rpm-build 133ac4
            fi
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Set device now, if not given
rpm-build 133ac4
        if [ -z "$device" ]; then
rpm-build 133ac4
            local device="$device_scope"
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        if [ -z "$device" ]; then
rpm-build 133ac4
            # Note: this can cause a warning and a not installed route, if given address is not reachable on the link
rpm-build 133ac4
            ipv6_add_route ::/0 $addressgw
rpm-build 133ac4
        else
rpm-build 133ac4
            ipv6_add_route ::/0 $addressgw $device
rpm-build 133ac4
        fi
rpm-build 133ac4
    elif [ -n "$device" ]; then
rpm-build 133ac4
        # Check whether the route belongs to the specific given interface
rpm-build 133ac4
        if [ -n "$device_check" -a "$device_check" != "$device" ]; then
rpm-build 133ac4
            # gateway device != specific given -> skip
rpm-build 133ac4
            return 0
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        ipv6_test_route_requires_next_hop $device
rpm-build 133ac4
        local result=$?
rpm-build 133ac4
rpm-build 133ac4
        if [ $result = 0 ]; then
rpm-build 133ac4
            net_log $"Given IPv6 default device '$device' requires an explicit nexthop" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        elif [ $result != 10 ]; then
rpm-build 133ac4
            net_log $"Given IPv6 default device '$device' doesn't exist or isn't up" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        ipv6_add_route ::/0 :: $device
rpm-build 133ac4
    else
rpm-build 133ac4
        net_log $"No parameters given to setup a default route" err $fn
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Resolve need of explicit next hop for an interface
rpm-build 133ac4
#  $1: <Interface>
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem 10=needs no explicit hop
rpm-build 133ac4
ipv6_test_route_requires_next_hop() {
rpm-build 133ac4
    local fn="ipv6_test_route_requires_next_hop"
rpm-build 133ac4
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test testonly || return 2
rpm-build 133ac4
rpm-build 133ac4
    ipv6_test_device_status $device
rpm-build 133ac4
rpm-build 133ac4
    if [ $? != 0 ]; then
rpm-build 133ac4
        return 3
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ "$device" = "sit0" ]; then
rpm-build 133ac4
        return 10
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if /sbin/ip -o link show $device 2>/dev/null |    LC_ALL=C grep -q "POINTOPOINT"; then
rpm-build 133ac4
        return 10
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
## Trigger radvd
rpm-build 133ac4
#  $1: up|down : device reason for triggering (coming up or going down)
rpm-build 133ac4
#  $2: [startstop|restart|reload|SIGHUP] : triger mechanism (default is "SIGHUP")
rpm-build 133ac4
#        "startstop" : reason=up -> start, reason=down -> stop
rpm-build 133ac4
#  $3: [<filename>] : alternative pid file    [optional]
rpm-build 133ac4
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
rpm-build 133ac4
ipv6_trigger_radvd() {
rpm-build 133ac4
    local fn="ipv6_trigger_radvd"
rpm-build 133ac4
rpm-build 133ac4
    local reason=$1
rpm-build 133ac4
    local mechanism=$2
rpm-build 133ac4
    local pidfile=$3
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$reason" ]; then
rpm-build 133ac4
        net_log $"No reason given for sending trigger to radvd" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ "$reason" != "up" -a "$reason" != "down" ]; then
rpm-build 133ac4
        net_log $"Unsupported reason '$reason' for sending trigger to radvd" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$mechanism" ]; then
rpm-build 133ac4
        # Take default
rpm-build 133ac4
        local mechanism="SIGHUP"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$pidfile" ]; then
rpm-build 133ac4
        local pidfile="/run/radvd/radvd.pid"
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    # Print message and select action
rpm-build 133ac4
    case $mechanism in
rpm-build 133ac4
        'startstop')
rpm-build 133ac4
            case $reason in
rpm-build 133ac4
            up)
rpm-build 133ac4
                local action="start"
rpm-build 133ac4
                ;;
rpm-build 133ac4
            down)
rpm-build 133ac4
                local action="stop"
rpm-build 133ac4
                ;;
rpm-build 133ac4
            esac
rpm-build 133ac4
            ;;
rpm-build 133ac4
        'reload'|'restart'|'SIGHUP')
rpm-build 133ac4
            local action="$mechanism"
rpm-build 133ac4
            ;;
rpm-build 133ac4
        *)
rpm-build 133ac4
            net_log $"Unsupported mechanism '$mechanism' for sending trigger to radvd" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
            ;;
rpm-build 133ac4
    esac
rpm-build 133ac4
rpm-build 133ac4
    # PID file needed?
rpm-build 133ac4
    if [ "$action" = "SIGHUP" ]; then
rpm-build 133ac4
        if ! [ -f "$pidfile" ]; then
rpm-build 133ac4
            if [ "$reason" = "down" ]; then
rpm-build 133ac4
                # be quiet because triggering may have been disabled
rpm-build 133ac4
                true
rpm-build 133ac4
            else
rpm-build 133ac4
                net_log $"Given pidfile '$pidfile' doesn't exist, cannot send trigger to radvd" err $fn
rpm-build 133ac4
            fi
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        # Get PID
rpm-build 133ac4
        local pid="$(cat $pidfile)"
rpm-build 133ac4
        if [ -z "$pid" ]; then
rpm-build 133ac4
            # pidfile empty - strange
rpm-build 133ac4
            net_log $"Pidfile '$pidfile' is empty, cannot send trigger to radvd" err $fn
rpm-build 133ac4
            return 3
rpm-build 133ac4
        fi
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
rpm-build 133ac4
    # Do action
rpm-build 133ac4
    case $action in
rpm-build 133ac4
        'SIGHUP')
rpm-build 133ac4
            kill -HUP $pid
rpm-build 133ac4
            ;;
rpm-build 133ac4
        'reload'|'restart'|'stop'|'start')
rpm-build 133ac4
            if ! /sbin/chkconfig --list radvd >/dev/null 2>&1; then
rpm-build 133ac4
                if [ "$reason" = "down" ]; then
rpm-build 133ac4
                    # be quiet because triggering may have been disabled
rpm-build 133ac4
                    true
rpm-build 133ac4
                else
rpm-build 133ac4
                    net_log $"radvd not (properly) installed, triggering failed" err $fn
rpm-build 133ac4
                fi
rpm-build 133ac4
                return 3
rpm-build 133ac4
            else
rpm-build 133ac4
                /sbin/service radvd $action >/dev/null 2>&1
rpm-build 133ac4
            fi
rpm-build 133ac4
            ;;
rpm-build 133ac4
        *)
rpm-build 133ac4
            # Normally not reached, "action" is set above to proper value
rpm-build 133ac4
            ;;
rpm-build 133ac4
    esac
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}
rpm-build 133ac4
rpm-build 133ac4
#https://www.vaspects.com/2013/12/11/services-dont-bind-to-ipv6-address/
rpm-build 133ac4
ipv6_wait_tentative() {
rpm-build 133ac4
    local fn="ipv6_wait_tentative"
rpm-build 133ac4
    local device=$1
rpm-build 133ac4
    local countdown=30
rpm-build 133ac4
    local ip_output=""
rpm-build 133ac4
rpm-build 133ac4
    if [ -z "$device" ]; then
rpm-build 133ac4
        net_log $"Missing parameter 'device' (arg 1)" err $fn
rpm-build 133ac4
        return 1
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    [ "$device" = lo ] && return 0
rpm-build 133ac4
rpm-build 133ac4
    while [ ${countdown} -gt 0 ]; do
rpm-build 133ac4
        ip_output="$(ip -6 addr show dev ${device} tentative)"
rpm-build 133ac4
rpm-build 133ac4
        if [ -z "$ip_output" ]; then
rpm-build 133ac4
            return 0;
rpm-build 133ac4
        elif echo "$ip_output" | grep "dadfailed" > /dev/null; then
rpm-build 133ac4
            net_log $"Duplicate Address Detection: Duplicate addresses detected" err $fn
rpm-build 133ac4
            net_log $"Duplicate Address Detection: Please, fix your network configuration" err $fn
rpm-build 133ac4
            return 1
rpm-build 133ac4
        fi
rpm-build 133ac4
rpm-build 133ac4
        net_log $"Waiting for interface ${device} IPv6 address(es) to leave the 'tentative' state" info $fn
rpm-build 133ac4
        sleep 1
rpm-build 133ac4
        countdown=$(($countdown - 1))
rpm-build 133ac4
    done
rpm-build 133ac4
rpm-build 133ac4
    ip_output="$(ip -6 addr show dev ${device} tentative)"
rpm-build 133ac4
rpm-build 133ac4
    if [ -n "$ip_output" ]; then
rpm-build 133ac4
        net_log $"Some IPv6 address(es) of ${device} remain still in 'tentative' state" warning $fn
rpm-build 133ac4
        net_log $"Run 'ip -6 addr show dev ${device} tentative' to see more" warning $fn
rpm-build 133ac4
    fi
rpm-build 133ac4
rpm-build 133ac4
    return 0
rpm-build 133ac4
}