fibs_test.sh revision 285117
1263445Sasomers#
2263445Sasomers#  Copyright (c) 2014 Spectra Logic Corporation
3263445Sasomers#  All rights reserved.
4264917Sasomers#
5263445Sasomers#  Redistribution and use in source and binary forms, with or without
6263445Sasomers#  modification, are permitted provided that the following conditions
7263445Sasomers#  are met:
8263445Sasomers#  1. Redistributions of source code must retain the above copyright
9263445Sasomers#     notice, this list of conditions, and the following disclaimer,
10263445Sasomers#     without modification.
11263445Sasomers#  2. Redistributions in binary form must reproduce at minimum a disclaimer
12263445Sasomers#     substantially similar to the "NO WARRANTY" disclaimer below
13263445Sasomers#     ("Disclaimer") and any redistribution must be conditioned upon
14263445Sasomers#     including a substantially similar Disclaimer requirement for further
15263445Sasomers#     binary redistribution.
16264917Sasomers#
17263445Sasomers#  NO WARRANTY
18263445Sasomers#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19263445Sasomers#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20263445Sasomers#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21263445Sasomers#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22263445Sasomers#  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23263445Sasomers#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24263445Sasomers#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25263445Sasomers#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26263445Sasomers#  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27263445Sasomers#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28263445Sasomers#  POSSIBILITY OF SUCH DAMAGES.
29264917Sasomers#
30263445Sasomers#  Authors: Alan Somers         (Spectra Logic Corporation)
31263445Sasomers#
32263445Sasomers# $FreeBSD: head/tests/sys/netinet/fibs_test.sh 285117 2015-07-04 02:22:26Z jmmv $
33263445Sasomers
34263445Sasomers# All of the tests in this file requires the test-suite config variable "fibs"
35263445Sasomers# to be defined to a space-delimited list of FIBs that may be used for testing.
36263445Sasomers
37263445Sasomers# arpresolve should check the interface fib for routes to a target when
38263445Sasomers# creating an ARP table entry.  This is a regression for kern/167947, where
39263445Sasomers# arpresolve only checked the default route.
40263445Sasomers#
41263445Sasomers# Outline:
42263445Sasomers# Create two tap(4) interfaces
43263445Sasomers# Simulate a crossover cable between them by using net/socat
44263445Sasomers# Use nping (from security/nmap) to send an ICMP echo request from one
45263445Sasomers# interface to the other, spoofing the source IP.  The source IP must be
46264917Sasomers# spoofed, or else it will already have an entry in the arp table.
47263445Sasomers# Check whether an arp entry exists for the spoofed IP
48263445Sasomersatf_test_case arpresolve_checks_interface_fib cleanup
49263445Sasomersarpresolve_checks_interface_fib_head()
50263445Sasomers{
51263445Sasomers	atf_set "descr" "arpresolve should check the interface fib, not the default fib, for routes"
52263445Sasomers	atf_set "require.user" "root"
53263445Sasomers	atf_set "require.config" "fibs"
54263445Sasomers	atf_set "require.progs" "socat nping"
55263445Sasomers}
56263445Sasomersarpresolve_checks_interface_fib_body()
57263445Sasomers{
58263445Sasomers	# Configure the TAP interfaces to use a RFC5737 nonrouteable addresses
59263445Sasomers	# and a non-default fib
60263445Sasomers	ADDR0="192.0.2.2"
61263445Sasomers	ADDR1="192.0.2.3"
62263445Sasomers	SUBNET="192.0.2.0"
63263445Sasomers	# Due to bug TBD (regressed by multiple_fibs_on_same_subnet) we need
64263445Sasomers	# diffferent subnet masks, or FIB1 won't have a subnet route.
65263445Sasomers	MASK0="24"
66263445Sasomers	MASK1="25"
67263445Sasomers	# Spoof a MAC that is reserved per RFC7042
68263445Sasomers	SPOOF_ADDR="192.0.2.4"
69263445Sasomers	SPOOF_MAC="00:00:5E:00:53:00"
70263445Sasomers
71263445Sasomers	# Check system configuration
72263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
73263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
74263445Sasomers	fi
75263445Sasomers	get_fibs 2
76263445Sasomers
77263445Sasomers	# Configure TAP interfaces
78263445Sasomers	setup_tap "$FIB0" ${ADDR0} ${MASK0}
79263445Sasomers	TAP0=$TAP
80263445Sasomers	setup_tap "$FIB1" ${ADDR1} ${MASK1}
81263445Sasomers	TAP1=$TAP
82263445Sasomers
83263445Sasomers	# Simulate a crossover cable
84263445Sasomers	socat /dev/${TAP0} /dev/${TAP1} &
85263445Sasomers	SOCAT_PID=$!
86263445Sasomers	echo ${SOCAT_PID} >> "processes_to_kill"
87264917Sasomers
88263445Sasomers	# Send an ICMP echo request with a spoofed source IP
89263445Sasomers	setfib 2 nping -c 1 -e ${TAP0} -S ${SPOOF_ADDR} \
90263445Sasomers		--source-mac ${SPOOF_MAC} --icmp --icmp-type "echo-request" \
91263445Sasomers		--icmp-code 0 --icmp-id 0xdead --icmp-seq 1 --data 0xbeef \
92263445Sasomers		${ADDR1}
93263445Sasomers	# For informational and debugging purposes only, look for the
94263445Sasomers	# characteristic error message
95263445Sasomers	dmesg | grep "llinfo.*${SPOOF_ADDR}"
96263445Sasomers	# Check that the ARP entry exists
97263445Sasomers	atf_check -o match:"${SPOOF_ADDR}.*expires" setfib 3 arp ${SPOOF_ADDR}
98263445Sasomers}
99263445Sasomersarpresolve_checks_interface_fib_cleanup()
100263445Sasomers{
101285117Sjmmv	if [ -f processes_to_kill ]; then
102285117Sjmmv		for pid in $(cat processes_to_kill); do
103285117Sjmmv			kill "${pid}"
104285117Sjmmv		done
105285117Sjmmv		rm -f processes_to_kill
106285117Sjmmv	fi
107263445Sasomers	cleanup_tap
108263445Sasomers}
109263445Sasomers
110263445Sasomers
111263445Sasomers# Regression test for kern/187549
112263445Sasomersatf_test_case loopback_and_network_routes_on_nondefault_fib cleanup
113263445Sasomersloopback_and_network_routes_on_nondefault_fib_head()
114263445Sasomers{
115263445Sasomers	atf_set "descr" "When creating and deleting loopback routes, use the interface's fib"
116263445Sasomers	atf_set "require.user" "root"
117263445Sasomers	atf_set "require.config" "fibs"
118263445Sasomers}
119263445Sasomers
120263445Sasomersloopback_and_network_routes_on_nondefault_fib_body()
121263445Sasomers{
122263445Sasomers	# Configure the TAP interface to use an RFC5737 nonrouteable address
123263445Sasomers	# and a non-default fib
124263445Sasomers	ADDR="192.0.2.2"
125263445Sasomers	SUBNET="192.0.2.0"
126263445Sasomers	MASK="24"
127263445Sasomers
128263445Sasomers	# Check system configuration
129263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
130263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
131263445Sasomers	fi
132263445Sasomers	get_fibs 1
133263445Sasomers
134263445Sasomers	# Configure a TAP interface
135263445Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK}
136263445Sasomers
137263445Sasomers	# Check whether the host route exists in only the correct FIB
138263445Sasomers	setfib ${FIB0} netstat -rn -f inet | grep -q "^${ADDR}.*UHS.*lo0"
139263445Sasomers	if [ 0 -ne $? ]; then
140263445Sasomers		setfib ${FIB0} netstat -rn -f inet
141263445Sasomers		atf_fail "Host route did not appear in the correct FIB"
142263445Sasomers	fi
143263445Sasomers	setfib 0 netstat -rn -f inet | grep -q "^${ADDR}.*UHS.*lo0"
144263445Sasomers	if [ 0 -eq $? ]; then
145263445Sasomers		setfib 0 netstat -rn -f inet
146263445Sasomers		atf_fail "Host route appeared in the wrong FIB"
147263445Sasomers	fi
148263445Sasomers
149263445Sasomers	# Check whether the network route exists in only the correct FIB
150263445Sasomers	setfib ${FIB0} netstat -rn -f inet | \
151263445Sasomers		grep -q "^${SUBNET}/${MASK}.*${TAPD}"
152263445Sasomers	if [ 0 -ne $? ]; then
153263445Sasomers		setfib ${FIB0} netstat -rn -f inet
154263445Sasomers		atf_fail "Network route did not appear in the correct FIB"
155263445Sasomers	fi
156263445Sasomers	setfib 0 netstat -rn -f inet | \
157263445Sasomers		grep -q "^${SUBNET}/${MASK}.*${TAPD}"
158263445Sasomers	if [ 0 -eq $? ]; then
159263445Sasomers		setfib ${FIB0} netstat -rn -f inet
160263445Sasomers		atf_fail "Network route appeared in the wrong FIB"
161263445Sasomers	fi
162263445Sasomers}
163263445Sasomers
164263445Sasomersloopback_and_network_routes_on_nondefault_fib_cleanup()
165263445Sasomers{
166263445Sasomers	cleanup_tap
167263445Sasomers}
168263445Sasomers
169263445Sasomers
170263445Sasomers# Regression test for kern/187552
171263445Sasomersatf_test_case default_route_with_multiple_fibs_on_same_subnet cleanup
172263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_head()
173263445Sasomers{
174263445Sasomers	atf_set "descr" "Multiple interfaces on the same subnet but with different fibs can both have default routes"
175263445Sasomers	atf_set "require.user" "root"
176263445Sasomers	atf_set "require.config" "fibs"
177263445Sasomers}
178263445Sasomers
179263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_body()
180263445Sasomers{
181263445Sasomers	# Configure the TAP interfaces to use a RFC5737 nonrouteable addresses
182263445Sasomers	# and a non-default fib
183263445Sasomers	ADDR0="192.0.2.2"
184263445Sasomers	ADDR1="192.0.2.3"
185263445Sasomers	GATEWAY="192.0.2.1"
186263445Sasomers	SUBNET="192.0.2.0"
187263445Sasomers	MASK="24"
188263445Sasomers
189263445Sasomers	# Check system configuration
190263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
191263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
192263445Sasomers	fi
193263445Sasomers	get_fibs 2
194263445Sasomers
195263445Sasomers	# Configure TAP interfaces
196263445Sasomers	setup_tap "$FIB0" ${ADDR0} ${MASK}
197263445Sasomers	TAP0=$TAP
198263445Sasomers	setup_tap "$FIB1" ${ADDR1} ${MASK}
199263445Sasomers	TAP1=$TAP
200263445Sasomers
201263445Sasomers	# Attempt to add default routes
202263445Sasomers	setfib ${FIB0} route add default ${GATEWAY}
203263445Sasomers	setfib ${FIB1} route add default ${GATEWAY}
204263445Sasomers
205263445Sasomers	# Verify that the default route exists for both fibs, with their
206263445Sasomers	# respective interfaces.
207263445Sasomers	atf_check -o match:"^default.*${TAP0}$" \
208263445Sasomers		setfib ${FIB0} netstat -rn -f inet
209263445Sasomers	atf_check -o match:"^default.*${TAP1}$" \
210263445Sasomers		setfib ${FIB1} netstat -rn -f inet
211263445Sasomers}
212263445Sasomers
213263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_cleanup()
214263445Sasomers{
215263445Sasomers	cleanup_tap
216263445Sasomers}
217263445Sasomers
218263445Sasomers
219265092Sasomers# Regression test for PR kern/189089
220265092Sasomers# Create two tap interfaces and assign them both the same IP address but with
221265092Sasomers# different netmasks, and both on the default FIB.  Then remove one's IP
222265092Sasomers# address.  Hopefully the machine won't panic.
223265092Sasomersatf_test_case same_ip_multiple_ifaces_fib0 cleanup
224265092Sasomerssame_ip_multiple_ifaces_fib0_head()
225265092Sasomers{
226265092Sasomers	atf_set "descr" "Can remove an IP alias from an interface when the same IP is also assigned to another interface."
227265092Sasomers	atf_set "require.user" "root"
228265092Sasomers	atf_set "require.config" "fibs"
229265092Sasomers}
230265092Sasomerssame_ip_multiple_ifaces_fib0_body()
231265092Sasomers{
232265092Sasomers	ADDR="192.0.2.2"
233265092Sasomers	MASK0="24"
234265092Sasomers	MASK1="32"
235265092Sasomers
236265092Sasomers	# Unlike most of the tests in this file, this is applicable regardless
237265092Sasomers	# of net.add_addr_allfibs
238265092Sasomers
239265092Sasomers	# Setup the interfaces, then remove one alias.  It should not panic.
240265092Sasomers	setup_tap 0 ${ADDR} ${MASK0}
241265092Sasomers	TAP0=${TAP}
242265092Sasomers	setup_tap 0 ${ADDR} ${MASK1}
243265092Sasomers	TAP1=${TAP}
244265092Sasomers	ifconfig ${TAP1} -alias ${ADDR}
245265092Sasomers
246265092Sasomers	# Do it again, in the opposite order.  It should not panic.
247265092Sasomers	setup_tap 0 ${ADDR} ${MASK0}
248265092Sasomers	TAP0=${TAP}
249265092Sasomers	setup_tap 0 ${ADDR} ${MASK1}
250265092Sasomers	TAP1=${TAP}
251265092Sasomers	ifconfig ${TAP0} -alias ${ADDR}
252265092Sasomers}
253265092Sasomerssame_ip_multiple_ifaces_fib0_cleanup()
254265092Sasomers{
255265092Sasomers	cleanup_tap
256265092Sasomers}
257265092Sasomers
258265094Sasomers# Regression test for PR kern/189088
259265094Sasomers# Test that removing an IP address works even if the same IP is assigned to a
260265094Sasomers# different interface, on a different FIB.  Tests the same code that whose
261265094Sasomers# panic was regressed by same_ip_multiple_ifaces_fib0.  
262265094Sasomers# Create two tap interfaces and assign them both the same IP address but with
263265094Sasomers# different netmasks, and on different FIBs.  Then remove one's IP
264265094Sasomers# address.  Hopefully the machine won't panic.  Also, the IP's hostroute should
265265094Sasomers# dissappear from the correct fib.
266265094Sasomersatf_test_case same_ip_multiple_ifaces cleanup
267265094Sasomerssame_ip_multiple_ifaces_head()
268265094Sasomers{
269265094Sasomers	atf_set "descr" "Can remove an IP alias from an interface when the same IP is also assigned to another interface, on non-default FIBs."
270265094Sasomers	atf_set "require.user" "root"
271265094Sasomers	atf_set "require.config" "fibs"
272265094Sasomers}
273265094Sasomerssame_ip_multiple_ifaces_body()
274265094Sasomers{
275265094Sasomers	atf_expect_fail "kern/189088 Assigning the same IP to multiple interfaces in different FIBs creates a host route for only one"
276265094Sasomers	ADDR="192.0.2.2"
277265094Sasomers	MASK0="24"
278265094Sasomers	MASK1="32"
279265094Sasomers
280265094Sasomers	# Unlike most of the tests in this file, this is applicable regardless
281265094Sasomers	# of net.add_addr_allfibs
282265094Sasomers	get_fibs 2
283265094Sasomers
284265094Sasomers	# Setup the interfaces, then remove one alias.  It should not panic.
285265094Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK0}
286265094Sasomers	TAP0=${TAP}
287265094Sasomers	setup_tap ${FIB1} ${ADDR} ${MASK1}
288265094Sasomers	TAP1=${TAP}
289265094Sasomers	ifconfig ${TAP1} -alias ${ADDR}
290265094Sasomers	atf_check -o not-match:"^${ADDR}[[:space:]]" \
291265094Sasomers		setfib ${FIB1} netstat -rn -f inet
292265094Sasomers
293265094Sasomers	# Do it again, in the opposite order.  It should not panic.
294265094Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK0}
295265094Sasomers	TAP0=${TAP}
296265094Sasomers	setup_tap ${FIB1} ${ADDR} ${MASK1}
297265094Sasomers	TAP1=${TAP}
298265094Sasomers	ifconfig ${TAP0} -alias ${ADDR}
299265094Sasomers	atf_check -o not-match:"^${ADDR}[[:space:]]" \
300265094Sasomers		setfib ${FIB0} netstat -rn -f inet
301265094Sasomers}
302265094Sasomerssame_ip_multiple_ifaces_cleanup()
303265094Sasomers{
304265094Sasomers	# Due to PR kern/189088, we must destroy the interfaces in LIFO order
305265094Sasomers	# in order for the routes to be correctly cleaned up.
306265094Sasomers	for TAPD in `tail -r "tap_devices_to_cleanup"`; do
307265094Sasomers		ifconfig ${TAPD} destroy
308265094Sasomers	done
309265094Sasomers}
310265094Sasomers
311263445Sasomers# Regression test for kern/187550
312263445Sasomersatf_test_case subnet_route_with_multiple_fibs_on_same_subnet cleanup
313263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_head()
314263445Sasomers{
315263445Sasomers	atf_set "descr" "Multiple FIBs can have subnet routes for the same subnet"
316263445Sasomers	atf_set "require.user" "root"
317263445Sasomers	atf_set "require.config" "fibs"
318263445Sasomers}
319263445Sasomers
320263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_body()
321263445Sasomers{
322263445Sasomers	# Configure the TAP interfaces to use a RFC5737 nonrouteable addresses
323263445Sasomers	# and a non-default fib
324263445Sasomers	ADDR0="192.0.2.2"
325263445Sasomers	ADDR1="192.0.2.3"
326263445Sasomers	SUBNET="192.0.2.0"
327263445Sasomers	MASK="24"
328263445Sasomers
329263445Sasomers	# Check system configuration
330263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
331263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
332263445Sasomers	fi
333263445Sasomers	get_fibs 2
334263445Sasomers
335263445Sasomers	# Configure TAP interfaces
336263445Sasomers	setup_tap "$FIB0" ${ADDR0} ${MASK}
337263445Sasomers	setup_tap "$FIB1" ${ADDR1} ${MASK}
338263445Sasomers
339263445Sasomers	# Check that a subnet route exists on both fibs
340263445Sasomers	atf_check -o ignore setfib "$FIB0" route get $ADDR1
341263445Sasomers	atf_check -o ignore setfib "$FIB1" route get $ADDR0
342263445Sasomers}
343263445Sasomers
344263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_cleanup()
345263445Sasomers{
346263445Sasomers	cleanup_tap
347263445Sasomers}
348263445Sasomers
349264905Sasomers# Test that source address selection works correctly for UDP packets with
350264905Sasomers# SO_DONTROUTE set that are sent on non-default FIBs.
351263445Sasomers# This bug was discovered with "setfib 1 netperf -t UDP_STREAM -H some_host"
352263445Sasomers# Regression test for kern/187553
353264905Sasomers#
354263738Sasomers# The root cause was that ifa_ifwithnet() did not have a fib argument.  It
355263738Sasomers# would return an address from an interface on any FIB that had a subnet route
356263738Sasomers# for the destination.  If more than one were available, it would choose the
357264905Sasomers# most specific.  This is most easily tested by creating a FIB without a
358264905Sasomers# default route, then trying to send a UDP packet with SO_DONTROUTE set to an
359264905Sasomers# address which is not routable on that FIB.  Absent the fix for this bug,
360264905Sasomers# in_pcbladdr would choose an interface on any FIB with a default route.  With
361264905Sasomers# the fix, you will get EUNREACH or ENETUNREACH.
362264905Sasomersatf_test_case udp_dontroute cleanup
363264905Sasomersudp_dontroute_head()
364263445Sasomers{
365263445Sasomers	atf_set "descr" "Source address selection for UDP packets with SO_DONTROUTE on non-default FIBs works"
366263445Sasomers	atf_set "require.user" "root"
367263445Sasomers	atf_set "require.config" "fibs"
368263445Sasomers}
369263445Sasomers
370264905Sasomersudp_dontroute_body()
371263445Sasomers{
372263445Sasomers	# Configure the TAP interface to use an RFC5737 nonrouteable address
373263445Sasomers	# and a non-default fib
374266860Sasomers	ADDR0="192.0.2.2"
375266860Sasomers	ADDR1="192.0.2.3"
376263445Sasomers	SUBNET="192.0.2.0"
377264905Sasomers	MASK="24"
378264905Sasomers	# Use a different IP on the same subnet as the target
379264905Sasomers	TARGET="192.0.2.100"
380266860Sasomers	SRCDIR=`atf_get_srcdir`
381263445Sasomers
382263445Sasomers	# Check system configuration
383263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
384263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
385263445Sasomers	fi
386266860Sasomers	get_fibs 2
387263445Sasomers
388266860Sasomers	# Configure the TAP interfaces
389266860Sasomers	setup_tap ${FIB0} ${ADDR0} ${MASK}
390266860Sasomers	TARGET_TAP=${TAP}
391266860Sasomers	setup_tap ${FIB1} ${ADDR1} ${MASK}
392263445Sasomers
393264905Sasomers	# Send a UDP packet with SO_DONTROUTE.  In the failure case, it will
394266860Sasomers	# return ENETUNREACH, or send the packet to the wrong tap
395266860Sasomers	atf_check -o ignore setfib ${FIB0} \
396266860Sasomers		${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP}
397266860Sasomers	cleanup_tap
398266860Sasomers
399266860Sasomers	# Repeat, but this time target the other tap
400266860Sasomers	setup_tap ${FIB0} ${ADDR0} ${MASK}
401266860Sasomers	setup_tap ${FIB1} ${ADDR1} ${MASK}
402266860Sasomers	TARGET_TAP=${TAP}
403266860Sasomers
404266860Sasomers	atf_check -o ignore setfib ${FIB1} \
405266860Sasomers		${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP}
406263445Sasomers}
407263445Sasomers
408264905Sasomersudp_dontroute_cleanup()
409263445Sasomers{
410263445Sasomers	cleanup_tap
411263445Sasomers}
412263445Sasomers
413263445Sasomers
414263445Sasomersatf_init_test_cases()
415263445Sasomers{
416263445Sasomers	atf_add_test_case arpresolve_checks_interface_fib
417264917Sasomers	atf_add_test_case loopback_and_network_routes_on_nondefault_fib
418264917Sasomers	atf_add_test_case default_route_with_multiple_fibs_on_same_subnet
419265092Sasomers	atf_add_test_case same_ip_multiple_ifaces_fib0
420265094Sasomers	atf_add_test_case same_ip_multiple_ifaces
421264917Sasomers	atf_add_test_case subnet_route_with_multiple_fibs_on_same_subnet
422264905Sasomers	atf_add_test_case udp_dontroute
423263445Sasomers}
424263445Sasomers
425263445Sasomers# Looks up one or more fibs from the configuration data and validates them.
426263445Sasomers# Returns the results in the env varilables FIB0, FIB1, etc.
427263445Sasomers
428263445Sasomers# parameter numfibs	The number of fibs to lookup
429263445Sasomersget_fibs()
430263445Sasomers{
431263445Sasomers	NUMFIBS=$1
432263445Sasomers	net_fibs=`sysctl -n net.fibs`
433263445Sasomers	i=0
434263445Sasomers	while [ $i -lt "$NUMFIBS" ]; do
435263445Sasomers		fib=`atf_config_get "fibs" | \
436263445Sasomers			awk -v i=$(( i + 1 )) '{print $i}'`
437263445Sasomers		echo "fib is ${fib}"
438263445Sasomers		eval FIB${i}=${fib}
439263445Sasomers		if [ "$fib" -ge "$net_fibs" ]; then
440263445Sasomers			atf_skip "The ${i}th configured fib is ${fib}, which is not less than net.fibs, which is ${net_fibs}"
441263445Sasomers		fi
442263445Sasomers		i=$(( $i + 1 ))
443263445Sasomers	done
444263445Sasomers}
445263445Sasomers
446263445Sasomers# Creates a new tap(4) interface, registers it for cleanup, and returns the
447263445Sasomers# name via the environment variable TAP
448263445Sasomersget_tap()
449263445Sasomers{
450263445Sasomers	local TAPN=0
451263445Sasomers	while ! ifconfig tap${TAPN} create > /dev/null 2>&1; do
452263445Sasomers		if [ "$TAPN" -ge 8 ]; then
453263445Sasomers			atf_skip "Could not create a tap(4) interface"
454263445Sasomers		else
455263445Sasomers			TAPN=$(($TAPN + 1))
456263445Sasomers		fi
457263445Sasomers	done
458263445Sasomers	local TAPD=tap${TAPN}
459263445Sasomers	# Record the TAP device so we can clean it up later
460263445Sasomers	echo ${TAPD} >> "tap_devices_to_cleanup"
461263445Sasomers	TAP=${TAPD}
462263445Sasomers}
463263445Sasomers
464263445Sasomers# Create a tap(4) interface, configure it, and register it for cleanup.
465263445Sasomers# parameters:
466263445Sasomers# fib
467263445Sasomers# IP address
468263445Sasomers# Netmask in number of bits (eg 24 or 8)
469263445Sasomers# Return: the tap interface name as the env variable TAP
470263445Sasomerssetup_tap()
471263445Sasomers{
472263445Sasomers	local FIB=$1
473263445Sasomers	local ADDR=$2
474263445Sasomers	local MASK=$3
475263445Sasomers	get_tap
476263445Sasomers	echo setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB
477263445Sasomers	setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB
478263445Sasomers}
479263445Sasomers
480263445Sasomerscleanup_tap()
481263445Sasomers{
482285117Sjmmv	if [ -f tap_devices_to_cleanup ]; then
483285117Sjmmv		for tap_device in $(cat tap_devices_to_cleanup); do
484285117Sjmmv			ifconfig "${tap_device}" destroy
485285117Sjmmv		done
486285117Sjmmv		rm -f tap_devices_to_cleanup
487285117Sjmmv	fi
488263445Sasomers}
489