1212# In addition this script also checks if forcing a specific field in the
1313# outer header is working.
1414
15+ # Return 4 by default (Kselftest SKIP code)
16+ ERR=4
17+
1518if [ " $( id -u) " != " 0" ]; then
1619 echo " Please run as root."
17- exit 0
20+ exit $ERR
1821fi
1922if ! which tcpdump > /dev/null 2>&1 ; then
2023 echo " No tcpdump found. Required for this test."
21- exit 0
24+ exit $ERR
2225fi
2326
2427expected_tos=" 0x00"
2528expected_ttl=" 0"
2629failed=false
2730
31+ readonly NS0=$( mktemp -u ns0-XXXXXXXX)
32+ readonly NS1=$( mktemp -u ns1-XXXXXXXX)
33+
34+ RUN_NS0=" ip netns exec ${NS0} "
35+
2836get_random_tos () {
2937 # Get a random hex tos value between 0x00 and 0xfc, a multiple of 4
3038 echo " 0x$( tr -dc ' 0-9a-f' < /dev/urandom | head -c 1) \
@@ -61,7 +69,6 @@ setup() {
6169 local vlan=" $5 "
6270 local test_tos=" 0x00"
6371 local test_ttl=" 0"
64- local ns=" ip netns exec testing"
6572
6673 # We don't want a test-tos of 0x00,
6774 # because this is the value that we get when no tos is set.
@@ -94,14 +101,15 @@ setup() {
94101 printf " │%7s │%6s │%6s │%13s │%13s │%6s │" \
95102 " $type " " $outer " " $inner " " $tos " " $ttl " " $vlan "
96103
97- # Create 'testing' netns, veth pair and connect main ns with testing ns
98- ip netns add testing
99- ip link add type veth
100- ip link set veth1 netns testing
101- ip link set veth0 up
102- $ns ip link set veth1 up
103- ip addr flush dev veth0
104- $ns ip addr flush dev veth1
104+ # Create netns NS0 and NS1 and connect them with a veth pair
105+ ip netns add " ${NS0} "
106+ ip netns add " ${NS1} "
107+ ip link add name veth0 netns " ${NS0} " type veth \
108+ peer name veth1 netns " ${NS1} "
109+ ip -netns " ${NS0} " link set dev veth0 up
110+ ip -netns " ${NS1} " link set dev veth1 up
111+ ip -netns " ${NS0} " address flush dev veth0
112+ ip -netns " ${NS1} " address flush dev veth1
105113
106114 local local_addr1=" "
107115 local local_addr2=" "
@@ -127,51 +135,59 @@ setup() {
127135 if [ " $type " = " gre" ]; then
128136 type=" gretap"
129137 fi
130- ip addr add 198.18.0.1/24 dev veth0
131- $ns ip addr add 198.18.0.2/24 dev veth1
132- ip link add name tep0 type $type $local_addr1 remote \
133- 198.18.0.2 tos $test_tos ttl $test_ttl $vxlan $geneve
134- $ns ip link add name tep1 type $type $local_addr2 remote \
135- 198.18.0.1 tos $test_tos ttl $test_ttl $vxlan $geneve
138+ ip -netns " ${NS0} " address add 198.18.0.1/24 dev veth0
139+ ip -netns " ${NS1} " address add 198.18.0.2/24 dev veth1
140+ ip -netns " ${NS0} " link add name tep0 type $type $local_addr1 \
141+ remote 198.18.0.2 tos $test_tos ttl $test_ttl \
142+ $vxlan $geneve
143+ ip -netns " ${NS1} " link add name tep1 type $type $local_addr2 \
144+ remote 198.18.0.1 tos $test_tos ttl $test_ttl \
145+ $vxlan $geneve
136146 elif [ " $outer " = " 6" ]; then
137147 if [ " $type " = " gre" ]; then
138148 type=" ip6gretap"
139149 fi
140- ip addr add fdd1:ced0:5d88:3fce::1/64 dev veth0
141- $ns ip addr add fdd1:ced0:5d88:3fce::2/64 dev veth1
142- ip link add name tep0 type $type $local_addr1 \
143- remote fdd1:ced0:5d88:3fce::2 tos $test_tos ttl $test_ttl \
144- $vxlan $geneve
145- $ns ip link add name tep1 type $type $local_addr2 \
146- remote fdd1:ced0:5d88:3fce::1 tos $test_tos ttl $test_ttl \
147- $vxlan $geneve
150+ ip -netns " ${NS0} " address add fdd1:ced0:5d88:3fce::1/64 \
151+ dev veth0 nodad
152+ ip -netns " ${NS1} " address add fdd1:ced0:5d88:3fce::2/64 \
153+ dev veth1 nodad
154+ ip -netns " ${NS0} " link add name tep0 type $type $local_addr1 \
155+ remote fdd1:ced0:5d88:3fce::2 tos $test_tos \
156+ ttl $test_ttl $vxlan $geneve
157+ ip -netns " ${NS1} " link add name tep1 type $type $local_addr2 \
158+ remote fdd1:ced0:5d88:3fce::1 tos $test_tos \
159+ ttl $test_ttl $vxlan $geneve
148160 fi
149161
150162 # Bring L2-tunnel link up and create VLAN on top
151- ip link set tep0 up
152- $ns ip link set tep1 up
153- ip addr flush dev tep0
154- $ns ip addr flush dev tep1
163+ ip -netns " ${NS0} " link set tep0 up
164+ ip -netns " ${NS1} " link set tep1 up
165+ ip -netns " ${NS0} " address flush dev tep0
166+ ip -netns " ${NS1} " address flush dev tep1
155167 local parent
156168 if $vlan ; then
157169 parent=" vlan99-"
158- ip link add link tep0 name ${parent} 0 type vlan id 99
159- $ns ip link add link tep1 name ${parent} 1 type vlan id 99
160- ip link set ${parent} 0 up
161- $ns ip link set ${parent} 1 up
162- ip addr flush dev ${parent} 0
163- $ns ip addr flush dev ${parent} 1
170+ ip -netns " ${NS0} " link add link tep0 name ${parent} 0 \
171+ type vlan id 99
172+ ip -netns " ${NS1} " link add link tep1 name ${parent} 1 \
173+ type vlan id 99
174+ ip -netns " ${NS0} " link set dev ${parent} 0 up
175+ ip -netns " ${NS1} " link set dev ${parent} 1 up
176+ ip -netns " ${NS0} " address flush dev ${parent} 0
177+ ip -netns " ${NS1} " address flush dev ${parent} 1
164178 else
165179 parent=" tep"
166180 fi
167181
168182 # Assign inner IPv4/IPv6 addresses
169183 if [ " $inner " = " 4" ] || [ " $inner " = " other" ]; then
170- ip addr add 198.19.0.1/24 brd + dev ${parent} 0
171- $ns ip addr add 198.19.0.2/24 brd + dev ${parent} 1
184+ ip -netns " ${NS0} " address add 198.19.0.1/24 brd + dev ${parent} 0
185+ ip -netns " ${NS1} " address add 198.19.0.2/24 brd + dev ${parent} 1
172186 elif [ " $inner " = " 6" ]; then
173- ip addr add fdd4:96cf:4eae:443b::1/64 dev ${parent} 0
174- $ns ip addr add fdd4:96cf:4eae:443b::2/64 dev ${parent} 1
187+ ip -netns " ${NS0} " address add fdd4:96cf:4eae:443b::1/64 \
188+ dev ${parent} 0 nodad
189+ ip -netns " ${NS1} " address add fdd4:96cf:4eae:443b::2/64 \
190+ dev ${parent} 1 nodad
175191 fi
176192}
177193
@@ -192,10 +208,10 @@ verify() {
192208 ping_dst=" 198.19.0.3" # Generates ARPs which are not IPv4/IPv6
193209 fi
194210 if [ " $tos_ttl " = " inherit" ]; then
195- ping -i 0.1 $ping_dst -Q " $expected_tos " -t " $expected_ttl " \
196- 2> /dev/null 1>&2 & ping_pid=" $! "
211+ ${RUN_NS0} ping -i 0.1 $ping_dst -Q " $expected_tos " \
212+ -t " $expected_ttl " 2> /dev/null 1>&2 & ping_pid=" $! "
197213 else
198- ping -i 0.1 $ping_dst 2> /dev/null 1>&2 & ping_pid=" $! "
214+ ${RUN_NS0} ping -i 0.1 $ping_dst 2> /dev/null 1>&2 & ping_pid=" $! "
199215 fi
200216 local tunnel_type_offset tunnel_type_proto req_proto_offset req_offset
201217 if [ " $type " = " gre" ]; then
@@ -216,10 +232,12 @@ verify() {
216232 req_proto_offset=" $(( req_proto_offset + 4 )) "
217233 req_offset=" $(( req_offset + 4 )) "
218234 fi
219- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
220- ip[$tunnel_type_offset ] = $tunnel_type_proto and \
221- ip[$req_proto_offset ] = 0x01 and \
222- ip[$req_offset ] = 0x08 2> /dev/null | head -n 1) "
235+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
236+ -i veth0 -n \
237+ ip[$tunnel_type_offset ] = $tunnel_type_proto and \
238+ ip[$req_proto_offset ] = 0x01 and \
239+ ip[$req_offset ] = 0x08 2> /dev/null \
240+ | head -n 1) "
223241 elif [ " $inner " = " 6" ]; then
224242 req_proto_offset=" 44"
225243 req_offset=" 78"
@@ -231,10 +249,12 @@ verify() {
231249 req_proto_offset=" $(( req_proto_offset + 4 )) "
232250 req_offset=" $(( req_offset + 4 )) "
233251 fi
234- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
235- ip[$tunnel_type_offset ] = $tunnel_type_proto and \
236- ip[$req_proto_offset ] = 0x3a and \
237- ip[$req_offset ] = 0x80 2> /dev/null | head -n 1) "
252+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
253+ -i veth0 -n \
254+ ip[$tunnel_type_offset ] = $tunnel_type_proto and \
255+ ip[$req_proto_offset ] = 0x3a and \
256+ ip[$req_offset ] = 0x80 2> /dev/null \
257+ | head -n 1) "
238258 elif [ " $inner " = " other" ]; then
239259 req_proto_offset=" 36"
240260 req_offset=" 45"
@@ -250,11 +270,13 @@ verify() {
250270 expected_tos=" 0x00"
251271 expected_ttl=" 64"
252272 fi
253- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
254- ip[$tunnel_type_offset ] = $tunnel_type_proto and \
255- ip[$req_proto_offset ] = 0x08 and \
256- ip[$(( req_proto_offset + 1 )) ] = 0x06 and \
257- ip[$req_offset ] = 0x01 2> /dev/null | head -n 1) "
273+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
274+ -i veth0 -n \
275+ ip[$tunnel_type_offset ] = $tunnel_type_proto and \
276+ ip[$req_proto_offset ] = 0x08 and \
277+ ip[$(( req_proto_offset + 1 )) ] = 0x06 and \
278+ ip[$req_offset ] = 0x01 2> /dev/null \
279+ | head -n 1) "
258280 fi
259281 elif [ " $outer " = " 6" ]; then
260282 if [ " $type " = " gre" ]; then
@@ -273,10 +295,12 @@ verify() {
273295 req_proto_offset=" $(( req_proto_offset + 4 )) "
274296 req_offset=" $(( req_offset + 4 )) "
275297 fi
276- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
277- ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
278- ip6[$req_proto_offset ] = 0x01 and \
279- ip6[$req_offset ] = 0x08 2> /dev/null | head -n 1) "
298+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
299+ -i veth0 -n \
300+ ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
301+ ip6[$req_proto_offset ] = 0x01 and \
302+ ip6[$req_offset ] = 0x08 2> /dev/null \
303+ | head -n 1) "
280304 elif [ " $inner " = " 6" ]; then
281305 local req_proto_offset=" 72"
282306 local req_offset=" 106"
@@ -288,10 +312,12 @@ verify() {
288312 req_proto_offset=" $(( req_proto_offset + 4 )) "
289313 req_offset=" $(( req_offset + 4 )) "
290314 fi
291- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
292- ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
293- ip6[$req_proto_offset ] = 0x3a and \
294- ip6[$req_offset ] = 0x80 2> /dev/null | head -n 1) "
315+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
316+ -i veth0 -n \
317+ ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
318+ ip6[$req_proto_offset ] = 0x3a and \
319+ ip6[$req_offset ] = 0x80 2> /dev/null \
320+ | head -n 1) "
295321 elif [ " $inner " = " other" ]; then
296322 local req_proto_offset=" 64"
297323 local req_offset=" 73"
@@ -307,15 +333,17 @@ verify() {
307333 expected_tos=" 0x00"
308334 expected_ttl=" 64"
309335 fi
310- out=" $( tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \
311- ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
312- ip6[$req_proto_offset ] = 0x08 and \
313- ip6[$(( req_proto_offset + 1 )) ] = 0x06 and \
314- ip6[$req_offset ] = 0x01 2> /dev/null | head -n 1) "
336+ out=" $( ${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
337+ -i veth0 -n \
338+ ip6[$tunnel_type_offset ] = $tunnel_type_proto and \
339+ ip6[$req_proto_offset ] = 0x08 and \
340+ ip6[$(( req_proto_offset + 1 )) ] = 0x06 and \
341+ ip6[$req_offset ] = 0x01 2> /dev/null \
342+ | head -n 1) "
315343 fi
316344 fi
317345 kill -9 $ping_pid
318- wait $ping_pid 2> /dev/null
346+ wait $ping_pid 2> /dev/null || true
319347 result=" FAIL"
320348 if [ " $outer " = " 4" ]; then
321349 captured_ttl=" $( get_field " ttl" " $out " ) "
@@ -351,11 +379,35 @@ verify() {
351379}
352380
353381cleanup () {
354- ip link del veth0 2> /dev/null
355- ip netns del testing 2> /dev/null
356- ip link del tep0 2> /dev/null
382+ ip netns del " ${NS0} " 2> /dev/null
383+ ip netns del " ${NS1} " 2> /dev/null
357384}
358385
386+ exit_handler () {
387+ # Don't exit immediately if one of the intermediate commands fails.
388+ # We might be called at the end of the script, when the network
389+ # namespaces have already been deleted. So cleanup() may fail, but we
390+ # still need to run until 'exit $ERR' or the script won't return the
391+ # correct error code.
392+ set +e
393+
394+ cleanup
395+
396+ exit $ERR
397+ }
398+
399+ # Restore the default SIGINT handler (just in case) and exit.
400+ # The exit handler will take care of cleaning everything up.
401+ interrupted () {
402+ trap - INT
403+
404+ exit $ERR
405+ }
406+
407+ set -e
408+ trap exit_handler EXIT
409+ trap interrupted INT
410+
359411printf " ┌────────┬───────┬───────┬──────────────┬"
360412printf " ──────────────┬───────┬────────┐\n"
361413for type in gre vxlan geneve; do
@@ -385,6 +437,10 @@ done
385437printf " └────────┴───────┴───────┴──────────────┴"
386438printf " ──────────────┴───────┴────────┘\n"
387439
440+ # All tests done.
441+ # Set ERR appropriately: it will be returned by the exit handler.
388442if $failed ; then
389- exit 1
443+ ERR=1
444+ else
445+ ERR=0
390446fi
0 commit comments