2 # SPDX-License-Identifier: GPL-2.0
4 # Check that route PMTU values match expectations, and that initial device MTU
5 # values are assigned correctly
7 # Tests currently implemented:
9 # - pmtu_vti4_exception
10 # Set up vti tunnel on top of veth, with xfrm states and policies, in two
11 # namespaces with matching endpoints. Check that route exception is not
12 # created if link layer MTU is not exceeded, then exceed it and check that
13 # exception is created with the expected PMTU. The approach described
14 # below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
15 # changes alone won't affect PMTU
17 # - pmtu_vti6_exception
18 # Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
19 # namespaces with matching endpoints. Check that route exception is
20 # created by exceeding link layer MTU with ping to other endpoint. Then
21 # decrease and increase MTU of tunnel, checking that route exception PMTU
24 # - pmtu_vti4_default_mtu
25 # Set up vti4 tunnel on top of veth, in two namespaces with matching
26 # endpoints. Check that MTU assigned to vti interface is the MTU of the
27 # lower layer (veth) minus additional lower layer headers (zero, for veth)
28 # minus IPv4 header length
30 # - pmtu_vti6_default_mtu
31 # Same as above, for IPv6
33 # - pmtu_vti4_link_add_mtu
34 # Set up vti4 interface passing MTU value at link creation, check MTU is
35 # configured, and that link is not created with invalid MTU values
37 # - pmtu_vti6_link_add_mtu
38 # Same as above, for IPv6
40 # - pmtu_vti6_link_change_mtu
41 # Set up two dummy interfaces with different MTUs, create a vti6 tunnel
42 # and check that configured MTU is used on link creation and changes, and
43 # that MTU is properly calculated instead when MTU is not configured from
46 # Kselftest framework requirement - SKIP code is 4.
49 # Some systems don't have a ping6 binary anymore
50 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
53 pmtu_vti6_exception vti6: PMTU exceptions
54 pmtu_vti4_exception vti4: PMTU exceptions
55 pmtu_vti4_default_mtu vti4: default MTU assignment
56 pmtu_vti6_default_mtu vti6: default MTU assignment
57 pmtu_vti4_link_add_mtu vti4: MTU setting on link creation
58 pmtu_vti6_link_add_mtu vti6: MTU setting on link creation
59 pmtu_vti6_link_change_mtu vti6: MTU changes on link changes"
61 NS_A="ns-$(mktemp -u XXXXXX)"
62 NS_B="ns-$(mktemp -u XXXXXX)"
63 ns_a="ip netns exec ${NS_A}"
64 ns_b="ip netns exec ${NS_B}"
66 veth4_a_addr="192.168.1.1"
67 veth4_b_addr="192.168.1.2"
69 veth6_a_addr="fd00:1::a"
70 veth6_b_addr="fd00:1::b"
73 vti4_a_addr="192.168.2.1"
74 vti4_b_addr="192.168.2.2"
76 vti6_a_addr="fd00:2::a"
77 vti6_b_addr="fd00:2::b"
80 dummy6_0_addr="fc00:1000::0"
81 dummy6_1_addr="fc00:1001::0"
88 err_buf="${err_buf}${1}
98 ip netns add ${NS_A} || return 1
103 ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
104 ${ns_a} ip link set veth_b netns ${NS_B}
106 ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
107 ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
109 ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
110 ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
112 ${ns_a} ip link set veth_a up
113 ${ns_b} ip link set veth_b up
124 [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
126 ${ns_a} ip link add vti${proto}_a type ${vti_type} local ${veth_a_addr} remote ${veth_b_addr} key 10 || return 1
127 ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
129 ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
130 ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
132 ${ns_a} ip link set vti${proto}_a up
133 ${ns_b} ip link set vti${proto}_b up
139 setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${vti4_a_addr} ${vti4_b_addr} ${vti4_mask}
143 setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${vti6_a_addr} ${vti6_b_addr} ${vti6_mask}
151 ${ns_a} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel || return 1
152 ${ns_a} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
153 ${ns_a} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
154 ${ns_a} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
156 ${ns_b} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
157 ${ns_b} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
158 ${ns_b} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
159 ${ns_b} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
163 setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
167 setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
171 [ "$(id -u)" -ne 0 ] && echo " need to run as root" && return $ksft_skip
175 eval setup_${arg} || { echo " ${arg} not supported"; return 1; }
180 [ ${cleanup_done} -eq 1 ] && return
181 ip netns del ${NS_A} 2> /dev/null
182 ip netns del ${NS_B} 2> /dev/null
191 ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
198 for i in ${input}; do
199 [ ${next} -eq 1 ] && echo "${i}" && return
200 [ "${i}" = "mtu" ] && next=1
208 ${ns_cmd} ip link show dev "${name}"
215 mtu_parse "$(link_get "${ns_cmd}" ${name})"
218 route_get_dst_exception() {
222 ${ns_cmd} ip route get "${dst}"
225 route_get_dst_pmtu_from_exception() {
229 mtu_parse "$(route_get_dst_exception "${ns_cmd}" ${dst})"
232 test_pmtu_vti4_exception() {
233 setup namespaces veth vti4 xfrm4 || return 2
236 vti_mtu=$((veth_mtu - 20))
238 # SPI SN IV ICV pad length next header
239 esp_payload_rfc4106=$((vti_mtu - 4 - 4 - 8 - 16 - 1 - 1))
240 ping_payload=$((esp_payload_rfc4106 - 28))
242 mtu "${ns_a}" veth_a ${veth_mtu}
243 mtu "${ns_b}" veth_b ${veth_mtu}
244 mtu "${ns_a}" vti4_a ${vti_mtu}
245 mtu "${ns_b}" vti4_b ${vti_mtu}
247 # Send DF packet without exceeding link layer MTU, check that no
248 # exception is created
249 ${ns_a} ping -q -M want -i 0.1 -w 2 -s ${ping_payload} ${vti4_b_addr} > /dev/null
250 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
251 if [ "${pmtu}" != "" ]; then
252 err " unexpected exception created with PMTU ${pmtu} for IP payload length ${esp_payload_rfc4106}"
256 # Now exceed link layer MTU by one byte, check that exception is created
257 ${ns_a} ping -q -M want -i 0.1 -w 2 -s $((ping_payload + 1)) ${vti4_b_addr} > /dev/null
258 pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
259 if [ "${pmtu}" = "" ]; then
260 err " exception not created for IP payload length $((esp_payload_rfc4106 + 1))"
264 # ...with the right PMTU value
265 if [ ${pmtu} -ne ${esp_payload_rfc4106} ]; then
266 err " wrong PMTU ${pmtu} in exception, expected: ${esp_payload_rfc4106}"
271 test_pmtu_vti6_exception() {
272 setup namespaces veth vti6 xfrm6 || return 2
275 # Create route exception by exceeding link layer MTU
276 mtu "${ns_a}" veth_a 4000
277 mtu "${ns_b}" veth_b 4000
278 mtu "${ns_a}" vti6_a 5000
279 mtu "${ns_b}" vti6_b 5000
280 ${ns_a} ${ping6} -q -i 0.1 -w 2 -s 60000 ${vti6_b_addr} > /dev/null
282 # Check that exception was created
283 if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" = "" ]; then
284 err " tunnel exceeding link layer MTU didn't create route exception"
288 # Decrease tunnel MTU, check for PMTU decrease in route exception
289 mtu "${ns_a}" vti6_a 3000
291 if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" -ne 3000 ]; then
292 err " decreasing tunnel MTU didn't decrease route exception PMTU"
296 # Increase tunnel MTU, check for PMTU increase in route exception
297 mtu "${ns_a}" vti6_a 9000
298 if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" -ne 9000 ]; then
299 err " increasing tunnel MTU didn't increase route exception PMTU"
306 test_pmtu_vti4_default_mtu() {
307 setup namespaces veth vti4 || return 2
309 # Check that MTU of vti device is MTU of veth minus IPv4 header length
310 veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
311 vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
312 if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
313 err " vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
318 test_pmtu_vti6_default_mtu() {
319 setup namespaces veth vti6 || return 2
321 # Check that MTU of vti device is MTU of veth minus IPv6 header length
322 veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
323 vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
324 if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
325 err " vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
330 test_pmtu_vti4_link_add_mtu() {
331 setup namespaces || return 2
333 ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
334 [ $? -ne 0 ] && err " vti not supported" && return 2
335 ${ns_a} ip link del vti4_a
341 # Check invalid values first
342 for v in $((min - 1)) $((max + 1)); do
343 ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10 2>/dev/null
344 # This can fail, or MTU can be adjusted to a proper value
345 [ $? -ne 0 ] && continue
346 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
347 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
348 err " vti tunnel created with invalid MTU ${mtu}"
351 ${ns_a} ip link del vti4_a
354 # Now check valid values
355 for v in ${min} 1300 ${max}; do
356 ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
357 mtu="$(link_get_mtu "${ns_a}" vti4_a)"
358 ${ns_a} ip link del vti4_a
359 if [ "${mtu}" != "${v}" ]; then
360 err " vti MTU ${mtu} doesn't match configured value ${v}"
368 test_pmtu_vti6_link_add_mtu() {
369 setup namespaces || return 2
371 ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
372 [ $? -ne 0 ] && err " vti6 not supported" && return 2
373 ${ns_a} ip link del vti6_a
377 min=68 # vti6 can carry IPv4 packets too
379 # Check invalid values first
380 for v in $((min - 1)) $((max + 1)); do
381 ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10 2>/dev/null
382 # This can fail, or MTU can be adjusted to a proper value
383 [ $? -ne 0 ] && continue
384 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
385 if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
386 err " vti6 tunnel created with invalid MTU ${v}"
389 ${ns_a} ip link del vti6_a
392 # Now check valid values
393 for v in 68 1280 1300 $((65535 - 40)); do
394 ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
395 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
396 ${ns_a} ip link del vti6_a
397 if [ "${mtu}" != "${v}" ]; then
398 err " vti6 MTU ${mtu} doesn't match configured value ${v}"
406 test_pmtu_vti6_link_change_mtu() {
407 setup namespaces || return 2
409 ${ns_a} ip link add dummy0 mtu 1500 type dummy
410 [ $? -ne 0 ] && err " dummy not supported" && return 2
411 ${ns_a} ip link add dummy1 mtu 3000 type dummy
412 ${ns_a} ip link set dummy0 up
413 ${ns_a} ip link set dummy1 up
415 ${ns_a} ip addr add ${dummy6_0_addr}/${dummy6_mask} dev dummy0
416 ${ns_a} ip addr add ${dummy6_1_addr}/${dummy6_mask} dev dummy1
420 # Create vti6 interface bound to device, passing MTU, check it
421 ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
422 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
423 if [ ${mtu} -ne 1300 ]; then
424 err " vti6 MTU ${mtu} doesn't match configured value 1300"
428 # Move to another device with different MTU, without passing MTU, check
430 ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_addr} local ${dummy6_1_addr}
431 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
432 if [ ${mtu} -ne $((3000 - 40)) ]; then
433 err " vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
437 # Move it back, passing MTU, check MTU is not overridden
438 ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
439 mtu="$(link_get_mtu "${ns_a}" vti6_a)"
440 if [ ${mtu} -ne 1280 ]; then
441 err " vti6 MTU ${mtu} doesn't match configured value 1280"
454 for t in ${tests}; do
455 [ $desc -eq 0 ] && name="${t}" && desc=1 && continue || desc=0
463 if [ $ret -eq 0 ]; then
464 printf "TEST: %-60s [ OK ]\n" "${t}"
465 elif [ $ret -eq 1 ]; then
466 printf "TEST: %-60s [FAIL]\n" "${t}"
469 elif [ $ret -eq 2 ]; then
470 printf "TEST: %-60s [SKIP]\n" "${t}"
474 [ $? -ne 0 ] && exitcode=1