Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # ns: h1               | ns: h2
0005 #   192.168.0.1/24     |
0006 #            eth0      |
0007 #                      |       192.168.1.1/32
0008 #            veth0 <---|---> veth1
0009 # Validate source address selection for route without gateway
0010 
0011 PAUSE_ON_FAIL=no
0012 VERBOSE=0
0013 ret=0
0014 
0015 ################################################################################
0016 # helpers
0017 
0018 log_test()
0019 {
0020         local rc=$1
0021         local expected=$2
0022         local msg="$3"
0023 
0024         if [ ${rc} -eq ${expected} ]; then
0025                 printf "TEST: %-60s  [ OK ]\n" "${msg}"
0026                 nsuccess=$((nsuccess+1))
0027         else
0028                 ret=1
0029                 nfail=$((nfail+1))
0030                 printf "TEST: %-60s  [FAIL]\n" "${msg}"
0031                 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
0032                         echo
0033                         echo "hit enter to continue, 'q' to quit"
0034                         read a
0035                         [ "$a" = "q" ] && exit 1
0036                 fi
0037         fi
0038 
0039         [ "$VERBOSE" = "1" ] && echo
0040 }
0041 
0042 run_cmd()
0043 {
0044         local cmd="$*"
0045         local out
0046         local rc
0047 
0048         if [ "$VERBOSE" = "1" ]; then
0049                 echo "COMMAND: $cmd"
0050         fi
0051 
0052         out=$(eval $cmd 2>&1)
0053         rc=$?
0054         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
0055                 echo "$out"
0056         fi
0057 
0058         [ "$VERBOSE" = "1" ] && echo
0059 
0060         return $rc
0061 }
0062 
0063 ################################################################################
0064 # config
0065 setup()
0066 {
0067         ip netns add h1
0068         ip -n h1 link set lo up
0069         ip netns add h2
0070         ip -n h2 link set lo up
0071 
0072         # Add a fake eth0 to support an ip address
0073         ip -n h1 link add name eth0 type dummy
0074         ip -n h1 link set eth0 up
0075         ip -n h1 address add 192.168.0.1/24 dev eth0
0076 
0077         # Configure veths (same @mac, arp off)
0078         ip -n h1 link add name veth0 type veth peer name veth1 netns h2
0079         ip -n h1 link set veth0 up
0080 
0081         ip -n h2 link set veth1 up
0082 
0083         # Configure @IP in the peer netns
0084         ip -n h2 address add 192.168.1.1/32 dev veth1
0085         ip -n h2 route add default dev veth1
0086 
0087         # Add a nexthop without @gw and use it in a route
0088         ip -n h1 nexthop add id 1 dev veth0
0089         ip -n h1 route add 192.168.1.1 nhid 1
0090 }
0091 
0092 cleanup()
0093 {
0094         ip netns del h1 2>/dev/null
0095         ip netns del h2 2>/dev/null
0096 }
0097 
0098 trap cleanup EXIT
0099 
0100 ################################################################################
0101 # main
0102 
0103 while getopts :pv o
0104 do
0105         case $o in
0106                 p) PAUSE_ON_FAIL=yes;;
0107                 v) VERBOSE=1;;
0108         esac
0109 done
0110 
0111 cleanup
0112 setup
0113 
0114 run_cmd ip -netns h1 route get 192.168.1.1
0115 log_test $? 0 "nexthop: get route with nexthop without gw"
0116 run_cmd ip netns exec h1 ping -c1 192.168.1.1
0117 log_test $? 0 "nexthop: ping through nexthop without gw"
0118 
0119 exit $ret