2 # SPDX-License-Identifier: GPL-2.0
4 # IPv4 and IPv6 onlink tests
7 PAUSE_ON_FAIL
=${PAUSE_ON_FAIL:=no}
11 # - odd in current namespace; even in peer ns
26 V4ADDRS
[p1
]=169.254.1.1
27 V4ADDRS
[p2
]=169.254.1.2
28 V4ADDRS
[p3
]=169.254.3.1
29 V4ADDRS
[p4
]=169.254.3.2
30 V4ADDRS
[p5
]=169.254.5.1
31 V4ADDRS
[p6
]=169.254.5.2
32 V4ADDRS
[p7
]=169.254.7.1
33 V4ADDRS
[p8
]=169.254.7.2
37 V6ADDRS
[p1
]=2001:db8
:101::1
38 V6ADDRS
[p2
]=2001:db8
:101::2
39 V6ADDRS
[p3
]=2001:db8
:301::1
40 V6ADDRS
[p4
]=2001:db8
:301::2
41 V6ADDRS
[p5
]=2001:db8
:501::1
42 V6ADDRS
[p6
]=2001:db8
:501::2
43 V6ADDRS
[p7
]=2001:db8
:701::1
44 V6ADDRS
[p8
]=2001:db8
:701::2
52 TEST_NET4
[1]=169.254.101
53 TEST_NET4
[2]=169.254.102
56 TEST_NET6
[1]=2001:db8
:101
57 TEST_NET6
[2]=2001:db8
:102
60 CONGW
[1]=169.254.1.254
61 CONGW
[2]=169.254.3.254
62 CONGW
[3]=169.254.5.254
65 RECGW4
[1]=169.254.11.254
66 RECGW4
[2]=169.254.12.254
67 RECGW6
[1]=2001:db8
:11::64
68 RECGW6
[2]=2001:db8
:12::64
71 declare -A TEST_NET4IN6IN6
72 TEST_NET4IN6
[1]=10.1.1.254
73 TEST_NET4IN6
[2]=10.2.1.254
82 ################################################################################
91 if [ ${rc} -eq ${expected} ]; then
92 nsuccess
=$
((nsuccess
+1))
93 printf " TEST: %-50s [ OK ]\n" "${msg}"
96 printf " TEST: %-50s [FAIL]\n" "${msg}"
97 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
99 echo "hit enter to continue, 'q' to quit"
101 [ "$a" = "q" ] && exit 1
109 echo "######################################################################"
110 echo "TEST SECTION: $*"
111 echo "######################################################################"
117 echo "#########################################"
118 echo "TEST SUBSECTION: $*"
127 if [ "$VERBOSE" = "1" ]; then
128 printf " COMMAND: $cmd\n"
131 out
=$
(eval $cmd 2>&1)
133 if [ "$VERBOSE" = "1" -a -n "$out" ]; then
137 [ "$VERBOSE" = "1" ] && echo
148 addr
=$
(${pfx} ip
-6 -br addr show dev
${dev} | \
150 for (i = 3; i <= NF; ++i) {
158 [ -z "$addr" ] && return 1
165 ################################################################################
171 echo "########################################"
172 echo "Configuring interfaces"
180 ip li add
${VRF} type vrf table
${VRF_TABLE}
182 ip ro add table
${VRF_TABLE} unreachable default metric
8192
183 ip
-6 ro add table
${VRF_TABLE} unreachable default metric
8192
185 # create test interfaces
186 ip li add
${NETIFS[p1]} type veth peer name
${NETIFS[p2]}
187 ip li add
${NETIFS[p3]} type veth peer name
${NETIFS[p4]}
188 ip li add
${NETIFS[p5]} type veth peer name
${NETIFS[p6]}
189 ip li add
${NETIFS[p7]} type veth peer name
${NETIFS[p8]}
191 # enslave vrf interfaces
193 ip li
set ${NETIFS[p${n}]} vrf
${VRF}
198 ip li
set ${NETIFS[p${n}]} up
199 ip addr add
${V4ADDRS[p${n}]}/24 dev ${NETIFS[p${n}]}
200 ip addr add
${V6ADDRS[p${n}]}/64 dev ${NETIFS[p${n}]} nodad
203 # move peer interfaces to namespace and add addresses
205 ip li
set ${NETIFS[p${n}]} netns
${PEER_NS} up
206 ip
-netns ${PEER_NS} addr add ${V4ADDRS[p${n}]}/24 dev ${NETIFS[p${n}]}
207 ip
-netns ${PEER_NS} addr add ${V6ADDRS[p${n}]}/64 dev ${NETIFS[p${n}]} nodad
210 ip
-6 ro add default via
${V6ADDRS[p3]/::[0-9]/::64}
211 ip
-6 ro add table
${VRF_TABLE} default via
${V6ADDRS[p7]/::[0-9]/::64}
218 # make sure we start from a clean slate
219 cleanup_ns
${PEER_NS} 2>/dev
/null
221 ip link del
${NETIFS[p${n}]} 2>/dev
/null
223 ip link del
${VRF} 2>/dev
/null
224 ip ro flush table
${VRF_TABLE}
225 ip
-6 ro flush table
${VRF_TABLE}
228 ################################################################################
241 # dev arg may be empty
242 [ -n "${dev}" ] && dev
="dev ${dev}"
244 run_cmd ip ro add table
"${table}" "${prefix}"/32 via "${gw}" "${dev}" onlink
245 log_test $?
${exp_rc} "${desc}"
257 # dev arg may be empty
258 [ -n "${dev}" ] && dev
="dev ${dev}"
260 run_cmd ip ro add table
"${table}" "${prefix}"/32 \
261 nexthop via
${nh1} nexthop via
${nh2}
262 log_test $?
${exp_rc} "${desc}"
267 # - unicast connected, unicast recursive
269 log_subsection
"default VRF - main table"
271 run_ip
254 ${TEST_NET4[1]}.1 ${CONGW[1]} ${NETIFS[p1]} 0 "unicast connected"
272 run_ip
254 ${TEST_NET4[1]}.2 ${RECGW4[1]} ${NETIFS[p1]} 0 "unicast recursive"
274 log_subsection
"VRF ${VRF}"
276 run_ip
${VRF_TABLE} ${TEST_NET4[2]}.1 ${CONGW[3]} ${NETIFS[p5]} 0 "unicast connected"
277 run_ip
${VRF_TABLE} ${TEST_NET4[2]}.2 ${RECGW4[2]} ${NETIFS[p5]} 0 "unicast recursive"
279 log_subsection
"VRF device, PBR table"
281 run_ip
${PBR_TABLE} ${TEST_NET4[2]}.3 ${CONGW[3]} ${NETIFS[p5]} 0 "unicast connected"
282 run_ip
${PBR_TABLE} ${TEST_NET4[2]}.4 ${RECGW4[2]} ${NETIFS[p5]} 0 "unicast recursive"
286 log_subsection
"default VRF - main table - multipath"
288 run_ip_mpath
254 ${TEST_NET4[1]}.5 \
289 "${CONGW[1]} dev ${NETIFS[p1]} onlink" \
290 "${CONGW[2]} dev ${NETIFS[p3]} onlink" \
291 0 "unicast connected - multipath"
293 run_ip_mpath
254 ${TEST_NET4[1]}.6 \
294 "${RECGW4[1]} dev ${NETIFS[p1]} onlink" \
295 "${RECGW4[2]} dev ${NETIFS[p3]} onlink" \
296 0 "unicast recursive - multipath"
298 run_ip_mpath
254 ${TEST_NET4[1]}.7 \
299 "${CONGW[1]} dev ${NETIFS[p1]}" \
300 "${CONGW[2]} dev ${NETIFS[p3]} onlink" \
301 0 "unicast connected - multipath onlink first only"
303 run_ip_mpath
254 ${TEST_NET4[1]}.8 \
304 "${CONGW[1]} dev ${NETIFS[p1]} onlink" \
305 "${CONGW[2]} dev ${NETIFS[p3]}" \
306 0 "unicast connected - multipath onlink second only"
309 invalid_onlink_ipv4
()
311 run_ip
254 ${TEST_NET4[1]}.11 ${V4ADDRS[p1]} ${NETIFS[p1]} 2 \
312 "Invalid gw - local unicast address"
314 run_ip
${VRF_TABLE} ${TEST_NET4[2]}.11 ${V4ADDRS[p5]} ${NETIFS[p5]} 2 \
315 "Invalid gw - local unicast address, VRF"
317 run_ip
254 ${TEST_NET4[1]}.101 ${V4ADDRS[p1]} "" 2 "No nexthop device given"
319 run_ip
254 ${TEST_NET4[1]}.102 ${V4ADDRS[p3]} ${NETIFS[p1]} 2 \
320 "Gateway resolves to wrong nexthop device"
322 run_ip
${VRF_TABLE} ${TEST_NET4[2]}.103 ${V4ADDRS[p7]} ${NETIFS[p5]} 2 \
323 "Gateway resolves to wrong nexthop device - VRF"
326 ################################################################################
339 # dev arg may be empty
340 [ -n "${dev}" ] && dev
="dev ${dev}"
342 run_cmd ip
-6 ro add table
"${table}" "${prefix}"/128 via "${gw}" "${dev}" onlink
343 log_test $?
${exp_rc} "${desc}"
356 run_cmd ip
-6 ro add table
"${table}" "${prefix}"/128 "${opts}" \
357 nexthop via
${nh1} nexthop via
${nh2}
358 log_test $?
${exp_rc} "${desc}"
363 # - unicast connected, unicast recursive, v4-mapped
365 log_subsection
"default VRF - main table"
367 run_ip6
254 ${TEST_NET6[1]}::1 ${V6ADDRS[p1]/::*}::64 ${NETIFS[p1]} 0 "unicast connected"
368 run_ip6
254 ${TEST_NET6[1]}::2 ${RECGW6[1]} ${NETIFS[p1]} 0 "unicast recursive"
369 run_ip6
254 ${TEST_NET6[1]}::3 ::ffff:${TEST_NET4IN6[1]} ${NETIFS[p1]} 0 "v4-mapped"
371 log_subsection
"VRF ${VRF}"
373 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::1 ${V6ADDRS[p5]/::*}::64 ${NETIFS[p5]} 0 "unicast connected"
374 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::2 ${RECGW6[2]} ${NETIFS[p5]} 0 "unicast recursive"
375 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::3 ::ffff:${TEST_NET4IN6[2]} ${NETIFS[p5]} 0 "v4-mapped"
377 log_subsection
"VRF device, PBR table"
379 run_ip6
${PBR_TABLE} ${TEST_NET6[2]}::4 ${V6ADDRS[p5]/::*}::64 ${NETIFS[p5]} 0 "unicast connected"
380 run_ip6
${PBR_TABLE} ${TEST_NET6[2]}::5 ${RECGW6[2]} ${NETIFS[p5]} 0 "unicast recursive"
381 run_ip6
${PBR_TABLE} ${TEST_NET6[2]}::6 ::ffff:${TEST_NET4IN6[2]} ${NETIFS[p5]} 0 "v4-mapped"
385 log_subsection
"default VRF - main table - multipath"
387 run_ip6_mpath
254 ${TEST_NET6[1]}::4 "onlink" \
388 "${V6ADDRS[p1]/::*}::64 dev ${NETIFS[p1]}" \
389 "${V6ADDRS[p3]/::*}::64 dev ${NETIFS[p3]}" \
390 0 "unicast connected - multipath onlink"
392 run_ip6_mpath
254 ${TEST_NET6[1]}::5 "onlink" \
393 "${RECGW6[1]} dev ${NETIFS[p1]}" \
394 "${RECGW6[2]} dev ${NETIFS[p3]}" \
395 0 "unicast recursive - multipath onlink"
397 run_ip6_mpath
254 ${TEST_NET6[1]}::6 "onlink" \
398 "::ffff:${TEST_NET4IN6[1]} dev ${NETIFS[p1]}" \
399 "::ffff:${TEST_NET4IN6[2]} dev ${NETIFS[p3]}" \
400 0 "v4-mapped - multipath onlink"
402 run_ip6_mpath
254 ${TEST_NET6[1]}::7 "" \
403 "${V6ADDRS[p1]/::*}::64 dev ${NETIFS[p1]} onlink" \
404 "${V6ADDRS[p3]/::*}::64 dev ${NETIFS[p3]} onlink" \
405 0 "unicast connected - multipath onlink both nexthops"
407 run_ip6_mpath
254 ${TEST_NET6[1]}::8 "" \
408 "${V6ADDRS[p1]/::*}::64 dev ${NETIFS[p1]} onlink" \
409 "${V6ADDRS[p3]/::*}::64 dev ${NETIFS[p3]}" \
410 0 "unicast connected - multipath onlink first only"
412 run_ip6_mpath
254 ${TEST_NET6[1]}::9 "" \
413 "${V6ADDRS[p1]/::*}::64 dev ${NETIFS[p1]}" \
414 "${V6ADDRS[p3]/::*}::64 dev ${NETIFS[p3]} onlink" \
415 0 "unicast connected - multipath onlink second only"
418 invalid_onlink_ipv6
()
422 lladdr
=$
(get_linklocal
${NETIFS[p1]}) ||
return 1
424 run_ip6
254 ${TEST_NET6[1]}::11 ${V6ADDRS[p1]} ${NETIFS[p1]} 2 \
425 "Invalid gw - local unicast address"
426 run_ip6
254 ${TEST_NET6[1]}::12 ${lladdr} ${NETIFS[p1]} 2 \
427 "Invalid gw - local linklocal address"
428 run_ip6
254 ${TEST_NET6[1]}::12 ${MCAST6} ${NETIFS[p1]} 2 \
429 "Invalid gw - multicast address"
431 lladdr
=$
(get_linklocal
${NETIFS[p5]}) ||
return 1
432 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::11 ${V6ADDRS[p5]} ${NETIFS[p5]} 2 \
433 "Invalid gw - local unicast address, VRF"
434 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::12 ${lladdr} ${NETIFS[p5]} 2 \
435 "Invalid gw - local linklocal address, VRF"
436 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::12 ${MCAST6} ${NETIFS[p5]} 2 \
437 "Invalid gw - multicast address, VRF"
439 run_ip6
254 ${TEST_NET6[1]}::101 ${V6ADDRS[p1]} "" 2 \
440 "No nexthop device given"
442 # default VRF validation is done against LOCAL table
443 # run_ip6 254 ${TEST_NET6[1]}::102 ${V6ADDRS[p3]/::[0-9]/::64} ${NETIFS[p1]} 2 \
444 # "Gateway resolves to wrong nexthop device"
446 run_ip6
${VRF_TABLE} ${TEST_NET6[2]}::103 ${V6ADDRS[p7]/::[0-9]/::64} ${NETIFS[p5]} 2 \
447 "Gateway resolves to wrong nexthop device - VRF"
452 log_section
"IPv4 onlink"
453 log_subsection
"Valid onlink commands"
455 log_subsection
"Invalid onlink commands"
458 log_section
"IPv6 onlink"
459 log_subsection
"Valid onlink commands"
461 log_subsection
"Invalid onlink commands"
465 ################################################################################
474 -v verbose mode (show commands and output)
478 ################################################################################
484 while getopts :t
:pPhv o
487 p
) PAUSE_ON_FAIL
=yes;;
488 v
) VERBOSE
=$
(($VERBOSE + 1));;
499 if [ "$TESTS" != "none" ]; then
500 printf "\nTests passed: %3d\n" ${nsuccess}
501 printf "Tests failed: %3d\n" ${nfail}