xref: /linux/tools/testing/selftests/net/fib_nexthop_nongw.sh (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# ns: h1               | ns: h2
5#   192.168.0.1/24     |
6#            eth0      |
7#                      |       192.168.1.1/32
8#            veth0 <---|---> veth1
9# Validate source address selection for route without gateway
10
11PAUSE_ON_FAIL=no
12VERBOSE=0
13ret=0
14
15################################################################################
16# helpers
17
18log_test()
19{
20	local rc=$1
21	local expected=$2
22	local msg="$3"
23
24	if [ ${rc} -eq ${expected} ]; then
25		printf "TEST: %-60s  [ OK ]\n" "${msg}"
26		nsuccess=$((nsuccess+1))
27	else
28		ret=1
29		nfail=$((nfail+1))
30		printf "TEST: %-60s  [FAIL]\n" "${msg}"
31		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
32			echo
33			echo "hit enter to continue, 'q' to quit"
34			read a
35			[ "$a" = "q" ] && exit 1
36		fi
37	fi
38
39	[ "$VERBOSE" = "1" ] && echo
40}
41
42run_cmd()
43{
44	local cmd="$*"
45	local out
46	local rc
47
48	if [ "$VERBOSE" = "1" ]; then
49		echo "COMMAND: $cmd"
50	fi
51
52	out=$(eval $cmd 2>&1)
53	rc=$?
54	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
55		echo "$out"
56	fi
57
58	[ "$VERBOSE" = "1" ] && echo
59
60	return $rc
61}
62
63################################################################################
64# config
65setup()
66{
67	ip netns add h1
68	ip -n h1 link set lo up
69	ip netns add h2
70	ip -n h2 link set lo up
71
72	# Add a fake eth0 to support an ip address
73	ip -n h1 link add name eth0 type dummy
74	ip -n h1 link set eth0 up
75	ip -n h1 address add 192.168.0.1/24 dev eth0
76
77	# Configure veths (same @mac, arp off)
78	ip -n h1 link add name veth0 type veth peer name veth1 netns h2
79	ip -n h1 link set veth0 up
80
81	ip -n h2 link set veth1 up
82
83	# Configure @IP in the peer netns
84	ip -n h2 address add 192.168.1.1/32 dev veth1
85	ip -n h2 route add default dev veth1
86
87	# Add a nexthop without @gw and use it in a route
88	ip -n h1 nexthop add id 1 dev veth0
89	ip -n h1 route add 192.168.1.1 nhid 1
90}
91
92cleanup()
93{
94	ip netns del h1 2>/dev/null
95	ip netns del h2 2>/dev/null
96}
97
98trap cleanup EXIT
99
100################################################################################
101# main
102
103while getopts :pv o
104do
105	case $o in
106		p) PAUSE_ON_FAIL=yes;;
107		v) VERBOSE=1;;
108	esac
109done
110
111cleanup
112setup
113
114run_cmd ip -netns h1 route get 192.168.1.1
115log_test $? 0 "nexthop: get route with nexthop without gw"
116run_cmd ip netns exec h1 ping -c1 192.168.1.1
117log_test $? 0 "nexthop: ping through nexthop without gw"
118
119exit $ret
120