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