xref: /freebsd/tests/etc/rc.d/routing_test.sh (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
1*7afb8adfSEnji Cooper#
2*7afb8adfSEnji Cooper#  Copyright (c) 2014 Spectra Logic Corporation
3*7afb8adfSEnji Cooper#  All rights reserved.
4*7afb8adfSEnji Cooper#
5*7afb8adfSEnji Cooper#  Redistribution and use in source and binary forms, with or without
6*7afb8adfSEnji Cooper#  modification, are permitted provided that the following conditions
7*7afb8adfSEnji Cooper#  are met:
8*7afb8adfSEnji Cooper#  1. Redistributions of source code must retain the above copyright
9*7afb8adfSEnji Cooper#     notice, this list of conditions, and the following disclaimer,
10*7afb8adfSEnji Cooper#     without modification.
11*7afb8adfSEnji Cooper#  2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7afb8adfSEnji Cooper#     substantially similar to the "NO WARRANTY" disclaimer below
13*7afb8adfSEnji Cooper#     ("Disclaimer") and any redistribution must be conditioned upon
14*7afb8adfSEnji Cooper#     including a substantially similar Disclaimer requirement for further
15*7afb8adfSEnji Cooper#     binary redistribution.
16*7afb8adfSEnji Cooper#
17*7afb8adfSEnji Cooper#  NO WARRANTY
18*7afb8adfSEnji Cooper#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7afb8adfSEnji Cooper#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7afb8adfSEnji Cooper#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7afb8adfSEnji Cooper#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7afb8adfSEnji Cooper#  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7afb8adfSEnji Cooper#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7afb8adfSEnji Cooper#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7afb8adfSEnji Cooper#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7afb8adfSEnji Cooper#  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7afb8adfSEnji Cooper#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7afb8adfSEnji Cooper#  POSSIBILITY OF SUCH DAMAGES.
29*7afb8adfSEnji Cooper#
30*7afb8adfSEnji Cooper#  Authors: Alan Somers         (Spectra Logic Corporation)
31*7afb8adfSEnji Cooper#
32*7afb8adfSEnji Cooper# $FreeBSD$
33*7afb8adfSEnji Cooper
34*7afb8adfSEnji Cooperatf_test_case static_ipv6_loopback_route_for_each_fib cleanup
35*7afb8adfSEnji Cooperstatic_ipv6_loopback_route_for_each_fib_head()
36*7afb8adfSEnji Cooper{
37*7afb8adfSEnji Cooper	atf_set "descr" "Every FIB should have a static IPv6 loopback route"
38*7afb8adfSEnji Cooper	atf_set "require.user" "root"
39*7afb8adfSEnji Cooper	atf_set "require.config" "fibs"
40*7afb8adfSEnji Cooper	atf_set "require.progs" "sysrc"
41*7afb8adfSEnji Cooper}
42*7afb8adfSEnji Cooperstatic_ipv6_loopback_route_for_each_fib_body()
43*7afb8adfSEnji Cooper{
44*7afb8adfSEnji Cooper	# Configure the TAP interface to use an RFC5737 nonrouteable address
45*7afb8adfSEnji Cooper	# and a non-default fib
46*7afb8adfSEnji Cooper	ADDR="192.0.2.2"
47*7afb8adfSEnji Cooper	SUBNET="192.0.2.0"
48*7afb8adfSEnji Cooper	MASK="24"
49*7afb8adfSEnji Cooper
50*7afb8adfSEnji Cooper	# Check system configuration
51*7afb8adfSEnji Cooper	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
52*7afb8adfSEnji Cooper		atf_skip "This test requires net.add_addr_allfibs=0"
53*7afb8adfSEnji Cooper	fi
54*7afb8adfSEnji Cooper
55*7afb8adfSEnji Cooper	get_fibs 1
56*7afb8adfSEnji Cooper	get_tap
57*7afb8adfSEnji Cooper
58*7afb8adfSEnji Cooper	# Configure a TAP interface in /etc/rc.conf.  Register the sysrc
59*7afb8adfSEnji Cooper	# variable for cleanup.
60*7afb8adfSEnji Cooper	echo "ifconfig_${TAP}" >> "sysrc_vars_to_cleanup"
61*7afb8adfSEnji Cooper	sysrc ifconfig_${TAP}="${ADDR}/${MASK} fib ${FIB0}"
62*7afb8adfSEnji Cooper
63*7afb8adfSEnji Cooper	# Start the interface
64*7afb8adfSEnji Cooper	service netif start ${TAP}
65*7afb8adfSEnji Cooper	# Check for an IPv6 loopback route
66*7afb8adfSEnji Cooper	setfib ${FIB0} netstat -rn -f inet6 | grep -q "^::1.*lo0$"
67*7afb8adfSEnji Cooper	if [ 0 -eq $? ]; then
68*7afb8adfSEnji Cooper		atf_pass
69*7afb8adfSEnji Cooper	else
70*7afb8adfSEnji Cooper		setfib ${FIB0} netstat -rn -f inet6
71*7afb8adfSEnji Cooper		atf_fail "Did not find an IPv6 loopback route"
72*7afb8adfSEnji Cooper	fi
73*7afb8adfSEnji Cooper}
74*7afb8adfSEnji Cooperstatic_ipv6_loopback_route_for_each_fib_cleanup()
75*7afb8adfSEnji Cooper{
76*7afb8adfSEnji Cooper	cleanup_sysrc
77*7afb8adfSEnji Cooper	cleanup_tap
78*7afb8adfSEnji Cooper}
79*7afb8adfSEnji Cooper
80*7afb8adfSEnji Cooperatf_init_test_cases()
81*7afb8adfSEnji Cooper{
82*7afb8adfSEnji Cooper	atf_add_test_case static_ipv6_loopback_route_for_each_fib
83*7afb8adfSEnji Cooper}
84*7afb8adfSEnji Cooper
85*7afb8adfSEnji Cooper# Looks up one or more fibs from the configuration data and validates them.
86*7afb8adfSEnji Cooper# Returns the results in the env varilables FIB0, FIB1, etc.
87*7afb8adfSEnji Cooper# parameter numfibs	The number of fibs to lookup
88*7afb8adfSEnji Cooperget_fibs()
89*7afb8adfSEnji Cooper{
90*7afb8adfSEnji Cooper	NUMFIBS=$1
91*7afb8adfSEnji Cooper	net_fibs=`sysctl -n net.fibs`
92*7afb8adfSEnji Cooper	i=0
93*7afb8adfSEnji Cooper	while [ $i -lt "$NUMFIBS" ]; do
94*7afb8adfSEnji Cooper		fib=`atf_config_get "fibs" | \
95*7afb8adfSEnji Cooper			awk -v i=$(( i + 1 )) '{print $i}'`
96*7afb8adfSEnji Cooper		echo "fib is ${fib}"
97*7afb8adfSEnji Cooper		eval FIB${i}=${fib}
98*7afb8adfSEnji Cooper		if [ "$fib" -ge "$net_fibs" ]; then
99*7afb8adfSEnji Cooper			msg="The ${i}th configured fib is ${fub}, which is "
100*7afb8adfSEnji Cooper			msg="$msg not less than net.fibs (${net_fibs})"
101*7afb8adfSEnji Cooper			atf_skip "$msg"
102*7afb8adfSEnji Cooper		fi
103*7afb8adfSEnji Cooper		i=$(( $i + 1 ))
104*7afb8adfSEnji Cooper	done
105*7afb8adfSEnji Cooper}
106*7afb8adfSEnji Cooper
107*7afb8adfSEnji Cooper
108*7afb8adfSEnji Cooper# Creates a new tap(4) interface, registers it for cleanup, and returns the
109*7afb8adfSEnji Cooper# name via the environment variable TAP
110*7afb8adfSEnji Cooperget_tap()
111*7afb8adfSEnji Cooper{
112*7afb8adfSEnji Cooper	local TAPN=0
113*7afb8adfSEnji Cooper	while ! ifconfig tap${TAPN} create > /dev/null 2>&1; do
114*7afb8adfSEnji Cooper		if [ "$TAPN" -ge 8 ]; then
115*7afb8adfSEnji Cooper			atf_skip "Could not create a tap(4) interface"
116*7afb8adfSEnji Cooper		else
117*7afb8adfSEnji Cooper			TAPN=$(($TAPN + 1))
118*7afb8adfSEnji Cooper		fi
119*7afb8adfSEnji Cooper	done
120*7afb8adfSEnji Cooper	local TAPD=tap${TAPN}
121*7afb8adfSEnji Cooper	# Record the TAP device so we can clean it up later
122*7afb8adfSEnji Cooper	echo ${TAPD} >> "tap_devices_to_cleanup"
123*7afb8adfSEnji Cooper	TAP=${TAPD}
124*7afb8adfSEnji Cooper}
125*7afb8adfSEnji Cooper
126*7afb8adfSEnji Coopercleanup_sysrc()
127*7afb8adfSEnji Cooper{
128*7afb8adfSEnji Cooper	for var in `cat "sysrc_vars_to_cleanup"`; do
129*7afb8adfSEnji Cooper		sysrc -x $var
130*7afb8adfSEnji Cooper	done
131*7afb8adfSEnji Cooper}
132*7afb8adfSEnji Cooper
133*7afb8adfSEnji Coopercleanup_tap()
134*7afb8adfSEnji Cooper{
135*7afb8adfSEnji Cooper	for TAPD in `cat "tap_devices_to_cleanup"`; do
136*7afb8adfSEnji Cooper		ifconfig ${TAPD} destroy
137*7afb8adfSEnji Cooper	done
138*7afb8adfSEnji Cooper}
139