1263445Sasomers#
2263445Sasomers#  Copyright (c) 2014 Spectra Logic Corporation
3263445Sasomers#  All rights reserved.
4265586Sasomers#
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.
16265586Sasomers#
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.
29265586Sasomers#
30263445Sasomers#  Authors: Alan Somers         (Spectra Logic Corporation)
31263445Sasomers#
32263445Sasomers# $FreeBSD: releng/10.2/tests/sys/netinet/fibs_test.sh 267195 2014-06-06 22:14:25Z asomers $
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
46265586Sasomers# 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"
87265586Sasomers
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{
101263445Sasomers	for PID in `cat "processes_to_kill"`; do
102263445Sasomers		kill $PID
103263445Sasomers	done
104263445Sasomers	cleanup_tap
105263445Sasomers}
106263445Sasomers
107263445Sasomers
108263445Sasomers# Regression test for kern/187549
109263445Sasomersatf_test_case loopback_and_network_routes_on_nondefault_fib cleanup
110263445Sasomersloopback_and_network_routes_on_nondefault_fib_head()
111263445Sasomers{
112263445Sasomers	atf_set "descr" "When creating and deleting loopback routes, use the interface's fib"
113263445Sasomers	atf_set "require.user" "root"
114263445Sasomers	atf_set "require.config" "fibs"
115263445Sasomers}
116263445Sasomers
117263445Sasomersloopback_and_network_routes_on_nondefault_fib_body()
118263445Sasomers{
119263445Sasomers	# Configure the TAP interface to use an RFC5737 nonrouteable address
120263445Sasomers	# and a non-default fib
121263445Sasomers	ADDR="192.0.2.2"
122263445Sasomers	SUBNET="192.0.2.0"
123263445Sasomers	MASK="24"
124263445Sasomers
125263445Sasomers	# Check system configuration
126263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
127263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
128263445Sasomers	fi
129263445Sasomers	get_fibs 1
130263445Sasomers
131263445Sasomers	# Configure a TAP interface
132263445Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK}
133263445Sasomers
134263445Sasomers	# Check whether the host route exists in only the correct FIB
135263445Sasomers	setfib ${FIB0} netstat -rn -f inet | grep -q "^${ADDR}.*UHS.*lo0"
136263445Sasomers	if [ 0 -ne $? ]; then
137263445Sasomers		setfib ${FIB0} netstat -rn -f inet
138263445Sasomers		atf_fail "Host route did not appear in the correct FIB"
139263445Sasomers	fi
140263445Sasomers	setfib 0 netstat -rn -f inet | grep -q "^${ADDR}.*UHS.*lo0"
141263445Sasomers	if [ 0 -eq $? ]; then
142263445Sasomers		setfib 0 netstat -rn -f inet
143263445Sasomers		atf_fail "Host route appeared in the wrong FIB"
144263445Sasomers	fi
145263445Sasomers
146263445Sasomers	# Check whether the network route exists in only the correct FIB
147263445Sasomers	setfib ${FIB0} netstat -rn -f inet | \
148263445Sasomers		grep -q "^${SUBNET}/${MASK}.*${TAPD}"
149263445Sasomers	if [ 0 -ne $? ]; then
150263445Sasomers		setfib ${FIB0} netstat -rn -f inet
151263445Sasomers		atf_fail "Network route did not appear in the correct FIB"
152263445Sasomers	fi
153263445Sasomers	setfib 0 netstat -rn -f inet | \
154263445Sasomers		grep -q "^${SUBNET}/${MASK}.*${TAPD}"
155263445Sasomers	if [ 0 -eq $? ]; then
156263445Sasomers		setfib ${FIB0} netstat -rn -f inet
157263445Sasomers		atf_fail "Network route appeared in the wrong FIB"
158263445Sasomers	fi
159263445Sasomers}
160263445Sasomers
161263445Sasomersloopback_and_network_routes_on_nondefault_fib_cleanup()
162263445Sasomers{
163263445Sasomers	cleanup_tap
164263445Sasomers}
165263445Sasomers
166263445Sasomers
167263445Sasomers# Regression test for kern/187552
168263445Sasomersatf_test_case default_route_with_multiple_fibs_on_same_subnet cleanup
169263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_head()
170263445Sasomers{
171263445Sasomers	atf_set "descr" "Multiple interfaces on the same subnet but with different fibs can both have default routes"
172263445Sasomers	atf_set "require.user" "root"
173263445Sasomers	atf_set "require.config" "fibs"
174263445Sasomers}
175263445Sasomers
176263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_body()
177263445Sasomers{
178263445Sasomers	# Configure the TAP interfaces to use a RFC5737 nonrouteable addresses
179263445Sasomers	# and a non-default fib
180263445Sasomers	ADDR0="192.0.2.2"
181263445Sasomers	ADDR1="192.0.2.3"
182263445Sasomers	GATEWAY="192.0.2.1"
183263445Sasomers	SUBNET="192.0.2.0"
184263445Sasomers	MASK="24"
185263445Sasomers
186263445Sasomers	# Check system configuration
187263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
188263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
189263445Sasomers	fi
190263445Sasomers	get_fibs 2
191263445Sasomers
192263445Sasomers	# Configure TAP interfaces
193263445Sasomers	setup_tap "$FIB0" ${ADDR0} ${MASK}
194263445Sasomers	TAP0=$TAP
195263445Sasomers	setup_tap "$FIB1" ${ADDR1} ${MASK}
196263445Sasomers	TAP1=$TAP
197263445Sasomers
198263445Sasomers	# Attempt to add default routes
199263445Sasomers	setfib ${FIB0} route add default ${GATEWAY}
200263445Sasomers	setfib ${FIB1} route add default ${GATEWAY}
201263445Sasomers
202263445Sasomers	# Verify that the default route exists for both fibs, with their
203263445Sasomers	# respective interfaces.
204263445Sasomers	atf_check -o match:"^default.*${TAP0}$" \
205263445Sasomers		setfib ${FIB0} netstat -rn -f inet
206263445Sasomers	atf_check -o match:"^default.*${TAP1}$" \
207263445Sasomers		setfib ${FIB1} netstat -rn -f inet
208263445Sasomers}
209263445Sasomers
210263445Sasomersdefault_route_with_multiple_fibs_on_same_subnet_cleanup()
211263445Sasomers{
212263445Sasomers	cleanup_tap
213263445Sasomers}
214263445Sasomers
215263445Sasomers
216267195Sasomers# Regression test for PR kern/189089
217267195Sasomers# Create two tap interfaces and assign them both the same IP address but with
218267195Sasomers# different netmasks, and both on the default FIB.  Then remove one's IP
219267195Sasomers# address.  Hopefully the machine won't panic.
220267195Sasomersatf_test_case same_ip_multiple_ifaces_fib0 cleanup
221267195Sasomerssame_ip_multiple_ifaces_fib0_head()
222267195Sasomers{
223267195Sasomers	atf_set "descr" "Can remove an IP alias from an interface when the same IP is also assigned to another interface."
224267195Sasomers	atf_set "require.user" "root"
225267195Sasomers	atf_set "require.config" "fibs"
226267195Sasomers}
227267195Sasomerssame_ip_multiple_ifaces_fib0_body()
228267195Sasomers{
229267195Sasomers	ADDR="192.0.2.2"
230267195Sasomers	MASK0="24"
231267195Sasomers	MASK1="32"
232267195Sasomers
233267195Sasomers	# Unlike most of the tests in this file, this is applicable regardless
234267195Sasomers	# of net.add_addr_allfibs
235267195Sasomers
236267195Sasomers	# Setup the interfaces, then remove one alias.  It should not panic.
237267195Sasomers	setup_tap 0 ${ADDR} ${MASK0}
238267195Sasomers	TAP0=${TAP}
239267195Sasomers	setup_tap 0 ${ADDR} ${MASK1}
240267195Sasomers	TAP1=${TAP}
241267195Sasomers	ifconfig ${TAP1} -alias ${ADDR}
242267195Sasomers
243267195Sasomers	# Do it again, in the opposite order.  It should not panic.
244267195Sasomers	setup_tap 0 ${ADDR} ${MASK0}
245267195Sasomers	TAP0=${TAP}
246267195Sasomers	setup_tap 0 ${ADDR} ${MASK1}
247267195Sasomers	TAP1=${TAP}
248267195Sasomers	ifconfig ${TAP0} -alias ${ADDR}
249267195Sasomers}
250267195Sasomerssame_ip_multiple_ifaces_fib0_cleanup()
251267195Sasomers{
252267195Sasomers	cleanup_tap
253267195Sasomers}
254267195Sasomers
255267195Sasomers# Regression test for PR kern/189088
256267195Sasomers# Test that removing an IP address works even if the same IP is assigned to a
257267195Sasomers# different interface, on a different FIB.  Tests the same code that whose
258267195Sasomers# panic was regressed by same_ip_multiple_ifaces_fib0.  
259267195Sasomers# Create two tap interfaces and assign them both the same IP address but with
260267195Sasomers# different netmasks, and on different FIBs.  Then remove one's IP
261267195Sasomers# address.  Hopefully the machine won't panic.  Also, the IP's hostroute should
262267195Sasomers# dissappear from the correct fib.
263267195Sasomersatf_test_case same_ip_multiple_ifaces cleanup
264267195Sasomerssame_ip_multiple_ifaces_head()
265267195Sasomers{
266267195Sasomers	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."
267267195Sasomers	atf_set "require.user" "root"
268267195Sasomers	atf_set "require.config" "fibs"
269267195Sasomers}
270267195Sasomerssame_ip_multiple_ifaces_body()
271267195Sasomers{
272267195Sasomers	atf_expect_fail "kern/189088 Assigning the same IP to multiple interfaces in different FIBs creates a host route for only one"
273267195Sasomers	ADDR="192.0.2.2"
274267195Sasomers	MASK0="24"
275267195Sasomers	MASK1="32"
276267195Sasomers
277267195Sasomers	# Unlike most of the tests in this file, this is applicable regardless
278267195Sasomers	# of net.add_addr_allfibs
279267195Sasomers	get_fibs 2
280267195Sasomers
281267195Sasomers	# Setup the interfaces, then remove one alias.  It should not panic.
282267195Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK0}
283267195Sasomers	TAP0=${TAP}
284267195Sasomers	setup_tap ${FIB1} ${ADDR} ${MASK1}
285267195Sasomers	TAP1=${TAP}
286267195Sasomers	ifconfig ${TAP1} -alias ${ADDR}
287267195Sasomers	atf_check -o not-match:"^${ADDR}[[:space:]]" \
288267195Sasomers		setfib ${FIB1} netstat -rn -f inet
289267195Sasomers
290267195Sasomers	# Do it again, in the opposite order.  It should not panic.
291267195Sasomers	setup_tap ${FIB0} ${ADDR} ${MASK0}
292267195Sasomers	TAP0=${TAP}
293267195Sasomers	setup_tap ${FIB1} ${ADDR} ${MASK1}
294267195Sasomers	TAP1=${TAP}
295267195Sasomers	ifconfig ${TAP0} -alias ${ADDR}
296267195Sasomers	atf_check -o not-match:"^${ADDR}[[:space:]]" \
297267195Sasomers		setfib ${FIB0} netstat -rn -f inet
298267195Sasomers}
299267195Sasomerssame_ip_multiple_ifaces_cleanup()
300267195Sasomers{
301267195Sasomers	# Due to PR kern/189088, we must destroy the interfaces in LIFO order
302267195Sasomers	# in order for the routes to be correctly cleaned up.
303267195Sasomers	for TAPD in `tail -r "tap_devices_to_cleanup"`; do
304267195Sasomers		ifconfig ${TAPD} destroy
305267195Sasomers	done
306267195Sasomers}
307267195Sasomers
308263445Sasomers# Regression test for kern/187550
309263445Sasomersatf_test_case subnet_route_with_multiple_fibs_on_same_subnet cleanup
310263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_head()
311263445Sasomers{
312263445Sasomers	atf_set "descr" "Multiple FIBs can have subnet routes for the same subnet"
313263445Sasomers	atf_set "require.user" "root"
314263445Sasomers	atf_set "require.config" "fibs"
315263445Sasomers}
316263445Sasomers
317263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_body()
318263445Sasomers{
319263445Sasomers	# Configure the TAP interfaces to use a RFC5737 nonrouteable addresses
320263445Sasomers	# and a non-default fib
321263445Sasomers	ADDR0="192.0.2.2"
322263445Sasomers	ADDR1="192.0.2.3"
323263445Sasomers	SUBNET="192.0.2.0"
324263445Sasomers	MASK="24"
325263445Sasomers
326263445Sasomers	# Check system configuration
327263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
328263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
329263445Sasomers	fi
330263445Sasomers	get_fibs 2
331263445Sasomers
332263445Sasomers	# Configure TAP interfaces
333263445Sasomers	setup_tap "$FIB0" ${ADDR0} ${MASK}
334263445Sasomers	setup_tap "$FIB1" ${ADDR1} ${MASK}
335263445Sasomers
336263445Sasomers	# Check that a subnet route exists on both fibs
337263445Sasomers	atf_check -o ignore setfib "$FIB0" route get $ADDR1
338263445Sasomers	atf_check -o ignore setfib "$FIB1" route get $ADDR0
339263445Sasomers}
340263445Sasomers
341263445Sasomerssubnet_route_with_multiple_fibs_on_same_subnet_cleanup()
342263445Sasomers{
343263445Sasomers	cleanup_tap
344263445Sasomers}
345263445Sasomers
346263445Sasomers# Test that source address selection works correctly for UDP packets with
347263445Sasomers# SO_DONTROUTE set that are sent on non-default FIBs.
348263445Sasomers# This bug was discovered with "setfib 1 netperf -t UDP_STREAM -H some_host"
349263445Sasomers# Regression test for kern/187553
350267186Sasomers#
351267186Sasomers# The root cause was that ifa_ifwithnet() did not have a fib argument.  It
352267186Sasomers# would return an address from an interface on any FIB that had a subnet route
353267186Sasomers# for the destination.  If more than one were available, it would choose the
354267186Sasomers# most specific.  This is most easily tested by creating a FIB without a
355267186Sasomers# default route, then trying to send a UDP packet with SO_DONTROUTE set to an
356267186Sasomers# address which is not routable on that FIB.  Absent the fix for this bug,
357267186Sasomers# in_pcbladdr would choose an interface on any FIB with a default route.  With
358267186Sasomers# the fix, you will get EUNREACH or ENETUNREACH.
359263445Sasomersatf_test_case udp_dontroute cleanup
360263445Sasomersudp_dontroute_head()
361263445Sasomers{
362263445Sasomers	atf_set "descr" "Source address selection for UDP packets with SO_DONTROUTE on non-default FIBs works"
363263445Sasomers	atf_set "require.user" "root"
364263445Sasomers	atf_set "require.config" "fibs"
365263445Sasomers}
366263445Sasomers
367263445Sasomersudp_dontroute_body()
368263445Sasomers{
369263445Sasomers	atf_expect_fail "kern/187553 Source address selection for UDP packets with SO_DONTROUTE uses the default FIB"
370263445Sasomers	# Configure the TAP interface to use an RFC5737 nonrouteable address
371263445Sasomers	# and a non-default fib
372267186Sasomers	ADDR0="192.0.2.2"
373267186Sasomers	ADDR1="192.0.2.3"
374263445Sasomers	SUBNET="192.0.2.0"
375263445Sasomers	MASK="24"
376263445Sasomers	# Use a different IP on the same subnet as the target
377263445Sasomers	TARGET="192.0.2.100"
378267186Sasomers	SRCDIR=`atf_get_srcdir`
379263445Sasomers
380263445Sasomers	# Check system configuration
381263445Sasomers	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
382263445Sasomers		atf_skip "This test requires net.add_addr_allfibs=0"
383263445Sasomers	fi
384267186Sasomers	get_fibs 2
385263445Sasomers
386267186Sasomers	# Configure the TAP interfaces
387267186Sasomers	setup_tap ${FIB0} ${ADDR0} ${MASK}
388267186Sasomers	TARGET_TAP=${TAP}
389267186Sasomers	setup_tap ${FIB1} ${ADDR1} ${MASK}
390263445Sasomers
391263445Sasomers	# Send a UDP packet with SO_DONTROUTE.  In the failure case, it will
392267186Sasomers	# return ENETUNREACH, or send the packet to the wrong tap
393267186Sasomers	atf_check -o ignore setfib ${FIB0} \
394267186Sasomers		${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP}
395267186Sasomers	cleanup_tap
396267186Sasomers
397267186Sasomers	# Repeat, but this time target the other tap
398267186Sasomers	setup_tap ${FIB0} ${ADDR0} ${MASK}
399267186Sasomers	setup_tap ${FIB1} ${ADDR1} ${MASK}
400267186Sasomers	TARGET_TAP=${TAP}
401267186Sasomers
402267186Sasomers	atf_check -o ignore setfib ${FIB1} \
403267186Sasomers		${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP}
404263445Sasomers}
405263445Sasomers
406263445Sasomersudp_dontroute_cleanup()
407263445Sasomers{
408263445Sasomers	cleanup_tap
409263445Sasomers}
410263445Sasomers
411263445Sasomers
412263445Sasomersatf_init_test_cases()
413263445Sasomers{
414263445Sasomers	atf_add_test_case arpresolve_checks_interface_fib
415265586Sasomers	atf_add_test_case loopback_and_network_routes_on_nondefault_fib
416265586Sasomers	atf_add_test_case default_route_with_multiple_fibs_on_same_subnet
417267195Sasomers	atf_add_test_case same_ip_multiple_ifaces_fib0
418267195Sasomers	atf_add_test_case same_ip_multiple_ifaces
419265586Sasomers	atf_add_test_case subnet_route_with_multiple_fibs_on_same_subnet
420263445Sasomers	atf_add_test_case udp_dontroute
421263445Sasomers}
422263445Sasomers
423263445Sasomers# Looks up one or more fibs from the configuration data and validates them.
424263445Sasomers# Returns the results in the env varilables FIB0, FIB1, etc.
425263445Sasomers
426263445Sasomers# parameter numfibs	The number of fibs to lookup
427263445Sasomersget_fibs()
428263445Sasomers{
429263445Sasomers	NUMFIBS=$1
430263445Sasomers	net_fibs=`sysctl -n net.fibs`
431263445Sasomers	i=0
432263445Sasomers	while [ $i -lt "$NUMFIBS" ]; do
433263445Sasomers		fib=`atf_config_get "fibs" | \
434263445Sasomers			awk -v i=$(( i + 1 )) '{print $i}'`
435263445Sasomers		echo "fib is ${fib}"
436263445Sasomers		eval FIB${i}=${fib}
437263445Sasomers		if [ "$fib" -ge "$net_fibs" ]; then
438263445Sasomers			atf_skip "The ${i}th configured fib is ${fib}, which is not less than net.fibs, which is ${net_fibs}"
439263445Sasomers		fi
440263445Sasomers		i=$(( $i + 1 ))
441263445Sasomers	done
442263445Sasomers}
443263445Sasomers
444263445Sasomers# Creates a new tap(4) interface, registers it for cleanup, and returns the
445263445Sasomers# name via the environment variable TAP
446263445Sasomersget_tap()
447263445Sasomers{
448263445Sasomers	local TAPN=0
449263445Sasomers	while ! ifconfig tap${TAPN} create > /dev/null 2>&1; do
450263445Sasomers		if [ "$TAPN" -ge 8 ]; then
451263445Sasomers			atf_skip "Could not create a tap(4) interface"
452263445Sasomers		else
453263445Sasomers			TAPN=$(($TAPN + 1))
454263445Sasomers		fi
455263445Sasomers	done
456263445Sasomers	local TAPD=tap${TAPN}
457263445Sasomers	# Record the TAP device so we can clean it up later
458263445Sasomers	echo ${TAPD} >> "tap_devices_to_cleanup"
459263445Sasomers	TAP=${TAPD}
460263445Sasomers}
461263445Sasomers
462263445Sasomers# Create a tap(4) interface, configure it, and register it for cleanup.
463263445Sasomers# parameters:
464263445Sasomers# fib
465263445Sasomers# IP address
466263445Sasomers# Netmask in number of bits (eg 24 or 8)
467263445Sasomers# Return: the tap interface name as the env variable TAP
468263445Sasomerssetup_tap()
469263445Sasomers{
470263445Sasomers	local FIB=$1
471263445Sasomers	local ADDR=$2
472263445Sasomers	local MASK=$3
473263445Sasomers	get_tap
474263445Sasomers	echo setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB
475263445Sasomers	setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB
476263445Sasomers}
477263445Sasomers
478263445Sasomerscleanup_tap()
479263445Sasomers{
480263445Sasomers	for TAPD in `cat "tap_devices_to_cleanup"`; do
481263445Sasomers		ifconfig ${TAPD} destroy
482263445Sasomers	done
483267186Sasomers	rm "tap_devices_to_cleanup"
484263445Sasomers}
485