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