xref: /linux/tools/testing/selftests/net/unicast_extensions.sh (revision 9e56ff53b4115875667760445b028357848b4748)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# By Seth Schoen (c) 2021, for the IPv4 Unicast Extensions Project
5# Thanks to David Ahern for help and advice on nettest modifications.
6#
7# Self-tests for IPv4 address extensions: the kernel's ability to accept
8# certain traditionally unused or unallocated IPv4 addresses. For each kind
9# of address, we test for interface assignment, ping, TCP, and forwarding.
10# Must be run as root (to manipulate network namespaces and virtual
11# interfaces).
12#
13# Things we test for here:
14#
15# * Currently the kernel accepts addresses in 0/8 and 240/4 as valid.
16#
17# * Notwithstanding that, 0.0.0.0 and 255.255.255.255 cannot be assigned.
18#
19# * Currently the kernel DOES NOT accept unicast use of the lowest
20#   address in an IPv4 subnet (e.g. 192.168.100.0/32 in 192.168.100.0/24).
21#   This is treated as a second broadcast address, for compatibility
22#   with 4.2BSD (!).
23#
24# * Currently the kernel DOES NOT accept unicast use of any of 127/8.
25#
26# * Currently the kernel DOES NOT accept unicast use of any of 224/4.
27#
28# These tests provide an easy way to flip the expected result of any
29# of these behaviors for testing kernel patches that change them.
30
31source lib.sh
32
33# nettest can be run from PATH or from same directory as this selftest
34if ! which nettest >/dev/null; then
35	PATH=$PWD:$PATH
36	if ! which nettest >/dev/null; then
37		echo "'nettest' command not found; skipping tests"
38		exit $ksft_skip
39	fi
40fi
41
42result=0
43
44hide_output(){ exec 3>&1 4>&2 >/dev/null 2>/dev/null; }
45show_output(){ exec >&3 2>&4; }
46
47show_result(){
48	if [ $1 -eq 0 ]; then
49		printf "TEST: %-60s  [ OK ]\n" "${2}"
50	else
51		printf "TEST: %-60s  [FAIL]\n" "${2}"
52		result=1
53	fi
54}
55
56_do_segmenttest(){
57	# Perform a simple set of link tests between a pair of
58	# IP addresses on a shared (virtual) segment, using
59	# ping and nettest.
60	# foo --- bar
61	# Arguments: ip_a ip_b prefix_length test_description
62	#
63	# Caller must set up $foo_ns and $bar_ns namespaces
64	# containing linked veth devices foo and bar,
65	# respectively.
66
67	ip -n $foo_ns address add $1/$3 dev foo || return 1
68	ip -n $foo_ns link set foo up || return 1
69	ip -n $bar_ns address add $2/$3 dev bar || return 1
70	ip -n $bar_ns link set bar up || return 1
71
72	ip netns exec $foo_ns timeout 2 ping -c 1 $2 || return 1
73	ip netns exec $bar_ns timeout 2 ping -c 1 $1 || return 1
74
75	nettest -B -N $bar_ns -O $foo_ns -r $1 || return 1
76	nettest -B -N $foo_ns -O $bar_ns -r $2 || return 1
77
78	return 0
79}
80
81_do_route_test(){
82	# Perform a simple set of gateway tests.
83	#
84	# [foo] <---> [foo1]-[bar1] <---> [bar]   /prefix
85	#  host          gateway          host
86	#
87	# Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
88	# Displays test result and returns success or failure.
89
90	# Caller must set up $foo_ns, $bar_ns, and $router_ns
91	# containing linked veth devices foo-foo1, bar1-bar
92	# (foo in $foo_ns, foo1 and bar1 in $router_ns, and
93	# bar in $bar_ns).
94
95	ip -n $foo_ns address add $1/$5 dev foo || return 1
96	ip -n $foo_ns link set foo up || return 1
97	ip -n $foo_ns route add default via $2 || return 1
98	ip -n $bar_ns address add $4/$5 dev bar || return 1
99	ip -n $bar_ns link set bar up || return 1
100	ip -n $bar_ns route add default via $3 || return 1
101	ip -n $router_ns address add $2/$5 dev foo1 || return 1
102	ip -n $router_ns link set foo1 up || return 1
103	ip -n $router_ns address add $3/$5 dev bar1 || return 1
104	ip -n $router_ns link set bar1 up || return 1
105
106	echo 1 | ip netns exec $router_ns tee /proc/sys/net/ipv4/ip_forward
107
108	ip netns exec $foo_ns timeout 2 ping -c 1 $2 || return 1
109	ip netns exec $foo_ns timeout 2 ping -c 1 $4 || return 1
110	ip netns exec $bar_ns timeout 2 ping -c 1 $3 || return 1
111	ip netns exec $bar_ns timeout 2 ping -c 1 $1 || return 1
112
113	nettest -B -N $bar_ns -O $foo_ns -r $1 || return 1
114	nettest -B -N $foo_ns -O $bar_ns -r $4 || return 1
115
116	return 0
117}
118
119segmenttest(){
120	# Sets up veth link and tries to connect over it.
121	# Arguments: ip_a ip_b prefix_len test_description
122	hide_output
123	setup_ns foo_ns bar_ns
124	ip link add foo netns $foo_ns type veth peer name bar netns $bar_ns
125
126	test_result=0
127	_do_segmenttest "$@" || test_result=1
128
129	ip netns pids $foo_ns | xargs -r kill -9
130	ip netns pids $bar_ns | xargs -r kill -9
131	cleanup_ns $foo_ns $bar_ns
132	show_output
133
134	# inverted tests will expect failure instead of success
135	[ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
136
137	show_result $test_result "$4"
138}
139
140route_test(){
141	# Sets up a simple gateway and tries to connect through it.
142	# [foo] <---> [foo1]-[bar1] <---> [bar]   /prefix
143	# Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
144	# Returns success or failure.
145
146	hide_output
147	setup_ns foo_ns bar_ns router_ns
148	ip link add foo netns $foo_ns type veth peer name foo1 netns $router_ns
149	ip link add bar netns $bar_ns type veth peer name bar1 netns $router_ns
150
151	test_result=0
152	_do_route_test "$@" || test_result=1
153
154	ip netns pids $foo_ns | xargs -r kill -9
155	ip netns pids $bar_ns | xargs -r kill -9
156	ip netns pids $router_ns | xargs -r kill -9
157	cleanup_ns $foo_ns $bar_ns $router_ns
158
159	show_output
160
161	# inverted tests will expect failure instead of success
162	[ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
163	show_result $test_result "$6"
164}
165
166echo "###########################################################################"
167echo "Unicast address extensions tests (behavior of reserved IPv4 addresses)"
168echo "###########################################################################"
169#
170# Test support for 240/4
171segmenttest 240.1.2.1   240.1.2.4    24 "assign and ping within 240/4 (1 of 2) (is allowed)"
172segmenttest 250.100.2.1 250.100.30.4 16 "assign and ping within 240/4 (2 of 2) (is allowed)"
173#
174# Test support for 0/8
175segmenttest 0.1.2.17    0.1.2.23  24 "assign and ping within 0/8 (1 of 2) (is allowed)"
176segmenttest 0.77.240.17 0.77.2.23 16 "assign and ping within 0/8 (2 of 2) (is allowed)"
177#
178# Even 255.255/16 is OK!
179segmenttest 255.255.3.1 255.255.50.77 16 "assign and ping inside 255.255/16 (is allowed)"
180#
181# Or 255.255.255/24
182segmenttest 255.255.255.1 255.255.255.254 24 "assign and ping inside 255.255.255/24 (is allowed)"
183#
184# Routing between different networks
185route_test 240.5.6.7 240.5.6.1  255.1.2.1    255.1.2.3      24 "route between 240.5.6/24 and 255.1.2/24 (is allowed)"
186route_test 0.200.6.7 0.200.38.1 245.99.101.1 245.99.200.111 16 "route between 0.200/16 and 245.99/16 (is allowed)"
187#
188# Test support for lowest address ending in .0
189segmenttest 5.10.15.20 5.10.15.0 24 "assign and ping lowest address (/24)"
190#
191# Test support for lowest address not ending in .0
192segmenttest 192.168.101.192 192.168.101.193 26 "assign and ping lowest address (/26)"
193#
194# Routing using lowest address as a gateway/endpoint
195route_test 192.168.42.1 192.168.42.0 9.8.7.6 9.8.7.0 24 "routing using lowest address"
196#
197# ==============================================
198# ==== TESTS THAT CURRENTLY EXPECT FAILURE =====
199# ==============================================
200expect_failure=true
201# It should still not be possible to use 0.0.0.0 or 255.255.255.255
202# as a unicast address.  Thus, these tests expect failure.
203segmenttest 0.0.1.5       0.0.0.0         16 "assigning 0.0.0.0 (is forbidden)"
204segmenttest 255.255.255.1 255.255.255.255 16 "assigning 255.255.255.255 (is forbidden)"
205#
206# Test support for not having all of 127 be loopback
207# Currently Linux does not allow this, so this should fail too
208segmenttest 127.99.4.5 127.99.4.6 16 "assign and ping inside 127/8 (is forbidden)"
209#
210# Test support for unicast use of class D
211# Currently Linux does not allow this, so this should fail too
212segmenttest 225.1.2.3 225.1.2.200 24 "assign and ping class D address (is forbidden)"
213#
214# Routing using class D as a gateway
215route_test 225.1.42.1 225.1.42.2 9.8.7.6 9.8.7.1 24 "routing using class D (is forbidden)"
216#
217# Routing using 127/8
218# Currently Linux does not allow this, so this should fail too
219route_test 127.99.2.3 127.99.2.4 200.1.2.3 200.1.2.4 24 "routing using 127/8 (is forbidden)"
220#
221unset expect_failure
222# =====================================================
223# ==== END OF TESTS THAT CURRENTLY EXPECT FAILURE =====
224# =====================================================
225exit ${result}
226