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