1b6a6006bSGuillaume Nault#!/bin/bash 2b6a6006bSGuillaume Nault# SPDX-License-Identifier: GPL-2.0 3b6a6006bSGuillaume Nault 4b6a6006bSGuillaume Naultsource ./lib.sh 5b6a6006bSGuillaume Nault 6b6a6006bSGuillaume NaultPAUSE_ON_FAIL="no" 7b6a6006bSGuillaume Nault 8b6a6006bSGuillaume Nault# The trap function handler 9b6a6006bSGuillaume Nault# 10b6a6006bSGuillaume Naultexit_cleanup_all() 11b6a6006bSGuillaume Nault{ 12b6a6006bSGuillaume Nault cleanup_all_ns 13b6a6006bSGuillaume Nault 14b6a6006bSGuillaume Nault exit "${EXIT_STATUS}" 15b6a6006bSGuillaume Nault} 16b6a6006bSGuillaume Nault 17b6a6006bSGuillaume Nault# Add fake IPv4 and IPv6 networks on the loopback device, to be used as 18b6a6006bSGuillaume Nault# underlay by future GRE devices. 19b6a6006bSGuillaume Nault# 20b6a6006bSGuillaume Naultsetup_basenet() 21b6a6006bSGuillaume Nault{ 22b6a6006bSGuillaume Nault ip -netns "${NS0}" link set dev lo up 23b6a6006bSGuillaume Nault ip -netns "${NS0}" address add dev lo 192.0.2.10/24 24b6a6006bSGuillaume Nault ip -netns "${NS0}" address add dev lo 2001:db8::10/64 nodad 25b6a6006bSGuillaume Nault} 26b6a6006bSGuillaume Nault 27*4d61a8a7SGuillaume Nault# Check the IPv6 configuration of a network device. 28*4d61a8a7SGuillaume Nault# 29*4d61a8a7SGuillaume Nault# We currently check the generation of the link-local IPv6 address and the 30*4d61a8a7SGuillaume Nault# creation of the ff00::/8 multicast route. 31b6a6006bSGuillaume Nault# 32b6a6006bSGuillaume Nault# Parameters: 33b6a6006bSGuillaume Nault# 34b6a6006bSGuillaume Nault# * $1: The network device to test 35b6a6006bSGuillaume Nault# * $2: An extra regular expression that should be matched (to verify the 36b6a6006bSGuillaume Nault# presence of extra attributes) 37b6a6006bSGuillaume Nault# * $3: The expected return code from grep (to allow checking the absence of 38b6a6006bSGuillaume Nault# a link-local address) 39b6a6006bSGuillaume Nault# * $4: The user visible name for the scenario being tested 40b6a6006bSGuillaume Nault# 41*4d61a8a7SGuillaume Naultcheck_ipv6_device_config() 42b6a6006bSGuillaume Nault{ 43b6a6006bSGuillaume Nault local DEV="$1" 44b6a6006bSGuillaume Nault local EXTRA_MATCH="$2" 45b6a6006bSGuillaume Nault local XRET="$3" 46b6a6006bSGuillaume Nault local MSG="$4" 47b6a6006bSGuillaume Nault 48b6a6006bSGuillaume Nault RET=0 49b6a6006bSGuillaume Nault set +e 50b6a6006bSGuillaume Nault ip -netns "${NS0}" -6 address show dev "${DEV}" scope link | grep "fe80::" | grep -q "${EXTRA_MATCH}" 51*4d61a8a7SGuillaume Nault check_err_fail "${XRET}" $? "IPv6 link-local address generation" 52*4d61a8a7SGuillaume Nault 53*4d61a8a7SGuillaume Nault ip -netns "${NS0}" -6 route show table local type multicast ff00::/8 proto kernel | grep -q "${DEV}" 54*4d61a8a7SGuillaume Nault check_err_fail 0 $? "IPv6 multicast route creation" 55*4d61a8a7SGuillaume Nault 56b6a6006bSGuillaume Nault log_test "${MSG}" 57b6a6006bSGuillaume Nault set -e 58b6a6006bSGuillaume Nault} 59b6a6006bSGuillaume Nault 60b6a6006bSGuillaume Nault# Create a GRE device and verify that it gets an IPv6 link-local address as 61b6a6006bSGuillaume Nault# expected. 62b6a6006bSGuillaume Nault# 63b6a6006bSGuillaume Nault# Parameters: 64b6a6006bSGuillaume Nault# 65b6a6006bSGuillaume Nault# * $1: The device type (gre, ip6gre, gretap or ip6gretap) 66b6a6006bSGuillaume Nault# * $2: The local underlay IP address (can be an IPv4, an IPv6 or "any") 67b6a6006bSGuillaume Nault# * $3: The remote underlay IP address (can be an IPv4, an IPv6 or "any") 68b6a6006bSGuillaume Nault# * $4: The IPv6 interface identifier generation mode to use for the GRE 69b6a6006bSGuillaume Nault# device (eui64, none, stable-privacy or random). 70b6a6006bSGuillaume Nault# 71b6a6006bSGuillaume Naulttest_gre_device() 72b6a6006bSGuillaume Nault{ 73b6a6006bSGuillaume Nault local GRE_TYPE="$1" 74b6a6006bSGuillaume Nault local LOCAL_IP="$2" 75b6a6006bSGuillaume Nault local REMOTE_IP="$3" 76b6a6006bSGuillaume Nault local MODE="$4" 77b6a6006bSGuillaume Nault local ADDR_GEN_MODE 78b6a6006bSGuillaume Nault local MATCH_REGEXP 79b6a6006bSGuillaume Nault local MSG 80b6a6006bSGuillaume Nault 81b6a6006bSGuillaume Nault ip link add netns "${NS0}" name gretest type "${GRE_TYPE}" local "${LOCAL_IP}" remote "${REMOTE_IP}" 82b6a6006bSGuillaume Nault 83b6a6006bSGuillaume Nault case "${MODE}" in 84b6a6006bSGuillaume Nault "eui64") 85b6a6006bSGuillaume Nault ADDR_GEN_MODE=0 86b6a6006bSGuillaume Nault MATCH_REGEXP="" 87b6a6006bSGuillaume Nault MSG="${GRE_TYPE}, mode: 0 (EUI64), ${LOCAL_IP} -> ${REMOTE_IP}" 88b6a6006bSGuillaume Nault XRET=0 89b6a6006bSGuillaume Nault ;; 90b6a6006bSGuillaume Nault "none") 91b6a6006bSGuillaume Nault ADDR_GEN_MODE=1 92b6a6006bSGuillaume Nault MATCH_REGEXP="" 93b6a6006bSGuillaume Nault MSG="${GRE_TYPE}, mode: 1 (none), ${LOCAL_IP} -> ${REMOTE_IP}" 94b6a6006bSGuillaume Nault XRET=1 # No link-local address should be generated 95b6a6006bSGuillaume Nault ;; 96b6a6006bSGuillaume Nault "stable-privacy") 97b6a6006bSGuillaume Nault ADDR_GEN_MODE=2 98b6a6006bSGuillaume Nault MATCH_REGEXP="stable-privacy" 99b6a6006bSGuillaume Nault MSG="${GRE_TYPE}, mode: 2 (stable privacy), ${LOCAL_IP} -> ${REMOTE_IP}" 100b6a6006bSGuillaume Nault XRET=0 101b6a6006bSGuillaume Nault # Initialise stable_secret (required for stable-privacy mode) 102b6a6006bSGuillaume Nault ip netns exec "${NS0}" sysctl -qw net.ipv6.conf.gretest.stable_secret="2001:db8::abcd" 103b6a6006bSGuillaume Nault ;; 104b6a6006bSGuillaume Nault "random") 105b6a6006bSGuillaume Nault ADDR_GEN_MODE=3 106b6a6006bSGuillaume Nault MATCH_REGEXP="stable-privacy" 107b6a6006bSGuillaume Nault MSG="${GRE_TYPE}, mode: 3 (random), ${LOCAL_IP} -> ${REMOTE_IP}" 108b6a6006bSGuillaume Nault XRET=0 109b6a6006bSGuillaume Nault ;; 110b6a6006bSGuillaume Nault esac 111b6a6006bSGuillaume Nault 112*4d61a8a7SGuillaume Nault # Check the IPv6 device configuration when it goes up 113b6a6006bSGuillaume Nault ip netns exec "${NS0}" sysctl -qw net.ipv6.conf.gretest.addr_gen_mode="${ADDR_GEN_MODE}" 114b6a6006bSGuillaume Nault ip -netns "${NS0}" link set dev gretest up 115*4d61a8a7SGuillaume Nault check_ipv6_device_config gretest "${MATCH_REGEXP}" "${XRET}" "config: ${MSG}" 116b6a6006bSGuillaume Nault 117b6a6006bSGuillaume Nault # Now disable link-local address generation 118b6a6006bSGuillaume Nault ip -netns "${NS0}" link set dev gretest down 119b6a6006bSGuillaume Nault ip netns exec "${NS0}" sysctl -qw net.ipv6.conf.gretest.addr_gen_mode=1 120b6a6006bSGuillaume Nault ip -netns "${NS0}" link set dev gretest up 121b6a6006bSGuillaume Nault 122*4d61a8a7SGuillaume Nault # Check the IPv6 device configuration when link-local address 123*4d61a8a7SGuillaume Nault # generation is re-enabled while the device is already up 124b6a6006bSGuillaume Nault ip netns exec "${NS0}" sysctl -qw net.ipv6.conf.gretest.addr_gen_mode="${ADDR_GEN_MODE}" 125*4d61a8a7SGuillaume Nault check_ipv6_device_config gretest "${MATCH_REGEXP}" "${XRET}" "update: ${MSG}" 126b6a6006bSGuillaume Nault 127b6a6006bSGuillaume Nault ip -netns "${NS0}" link del dev gretest 128b6a6006bSGuillaume Nault} 129b6a6006bSGuillaume Nault 130b6a6006bSGuillaume Naulttest_gre4() 131b6a6006bSGuillaume Nault{ 132b6a6006bSGuillaume Nault local GRE_TYPE 133b6a6006bSGuillaume Nault local MODE 134b6a6006bSGuillaume Nault 135b6a6006bSGuillaume Nault for GRE_TYPE in "gre" "gretap"; do 136*4d61a8a7SGuillaume Nault printf "\n####\nTesting IPv6 configuration of ${GRE_TYPE} devices\n####\n\n" 137b6a6006bSGuillaume Nault 138b6a6006bSGuillaume Nault for MODE in "eui64" "none" "stable-privacy" "random"; do 139b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" 192.0.2.10 192.0.2.11 "${MODE}" 140b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" any 192.0.2.11 "${MODE}" 141b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" 192.0.2.10 any "${MODE}" 142b6a6006bSGuillaume Nault done 143b6a6006bSGuillaume Nault done 144b6a6006bSGuillaume Nault} 145b6a6006bSGuillaume Nault 146b6a6006bSGuillaume Naulttest_gre6() 147b6a6006bSGuillaume Nault{ 148b6a6006bSGuillaume Nault local GRE_TYPE 149b6a6006bSGuillaume Nault local MODE 150b6a6006bSGuillaume Nault 151b6a6006bSGuillaume Nault for GRE_TYPE in "ip6gre" "ip6gretap"; do 152*4d61a8a7SGuillaume Nault printf "\n####\nTesting IPv6 configuration of ${GRE_TYPE} devices\n####\n\n" 153b6a6006bSGuillaume Nault 154b6a6006bSGuillaume Nault for MODE in "eui64" "none" "stable-privacy" "random"; do 155b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" 2001:db8::10 2001:db8::11 "${MODE}" 156b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" any 2001:db8::11 "${MODE}" 157b6a6006bSGuillaume Nault test_gre_device "${GRE_TYPE}" 2001:db8::10 any "${MODE}" 158b6a6006bSGuillaume Nault done 159b6a6006bSGuillaume Nault done 160b6a6006bSGuillaume Nault} 161b6a6006bSGuillaume Nault 162b6a6006bSGuillaume Naultusage() 163b6a6006bSGuillaume Nault{ 164b6a6006bSGuillaume Nault echo "Usage: $0 [-p]" 165b6a6006bSGuillaume Nault exit 1 166b6a6006bSGuillaume Nault} 167b6a6006bSGuillaume Nault 168b6a6006bSGuillaume Naultwhile getopts :p o 169b6a6006bSGuillaume Naultdo 170b6a6006bSGuillaume Nault case $o in 171b6a6006bSGuillaume Nault p) PAUSE_ON_FAIL="yes";; 172b6a6006bSGuillaume Nault *) usage;; 173b6a6006bSGuillaume Nault esac 174b6a6006bSGuillaume Naultdone 175b6a6006bSGuillaume Nault 176b6a6006bSGuillaume Naultsetup_ns NS0 177b6a6006bSGuillaume Nault 178b6a6006bSGuillaume Naultset -e 179b6a6006bSGuillaume Naulttrap exit_cleanup_all EXIT 180b6a6006bSGuillaume Nault 181b6a6006bSGuillaume Naultsetup_basenet 182b6a6006bSGuillaume Nault 183b6a6006bSGuillaume Naulttest_gre4 184b6a6006bSGuillaume Naulttest_gre6 185