116 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# ns: h1 | ns: h25# 192.168.0.1/24 |6# eth0 |7# | 192.168.1.1/328# veth0 <---|---> veth19# Validate source address selection for route without gateway10 11source lib.sh12PAUSE_ON_FAIL=no13VERBOSE=014ret=015 16################################################################################17# helpers18 19log_test()20{21 local rc=$122 local expected=$223 local msg="$3"24 25 if [ ${rc} -eq ${expected} ]; then26 printf "TEST: %-60s [ OK ]\n" "${msg}"27 nsuccess=$((nsuccess+1))28 else29 ret=130 nfail=$((nfail+1))31 printf "TEST: %-60s [FAIL]\n" "${msg}"32 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then33 echo34 echo "hit enter to continue, 'q' to quit"35 read a36 [ "$a" = "q" ] && exit 137 fi38 fi39 40 [ "$VERBOSE" = "1" ] && echo41}42 43run_cmd()44{45 local cmd="$*"46 local out47 local rc48 49 if [ "$VERBOSE" = "1" ]; then50 echo "COMMAND: $cmd"51 fi52 53 out=$(eval $cmd 2>&1)54 rc=$?55 if [ "$VERBOSE" = "1" -a -n "$out" ]; then56 echo "$out"57 fi58 59 [ "$VERBOSE" = "1" ] && echo60 61 return $rc62}63 64################################################################################65# config66setup()67{68 setup_ns h1 h269 70 # Add a fake eth0 to support an ip address71 ip -n $h1 link add name eth0 type dummy72 ip -n $h1 link set eth0 up73 ip -n $h1 address add 192.168.0.1/24 dev eth074 75 # Configure veths (same @mac, arp off)76 ip -n $h1 link add name veth0 type veth peer name veth1 netns $h277 ip -n $h1 link set veth0 up78 79 ip -n $h2 link set veth1 up80 81 # Configure @IP in the peer netns82 ip -n $h2 address add 192.168.1.1/32 dev veth183 ip -n $h2 route add default dev veth184 85 # Add a nexthop without @gw and use it in a route86 ip -n $h1 nexthop add id 1 dev veth087 ip -n $h1 route add 192.168.1.1 nhid 188}89 90cleanup()91{92 cleanup_ns $h1 $h293}94 95trap cleanup EXIT96 97################################################################################98# main99 100while getopts :pv o101do102 case $o in103 p) PAUSE_ON_FAIL=yes;;104 v) VERBOSE=1;;105 esac106done107 108setup109 110run_cmd ip -netns $h1 route get 192.168.1.1111log_test $? 0 "nexthop: get route with nexthop without gw"112run_cmd ip netns exec $h1 ping -c1 192.168.1.1113log_test $? 0 "nexthop: ping through nexthop without gw"114 115exit $ret116