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: stable/10/tests/sys/netinet/fibs_test.sh 317093 2017-04-18 16:17:38Z 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{ 101317093Sasomers if [ -f processes_to_kill ]; then 102317093Sasomers for pid in $(cat processes_to_kill); do 103317093Sasomers kill "${pid}" 104317093Sasomers done 105317093Sasomers rm -f processes_to_kill 106317093Sasomers 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 219267195Sasomers# Regression test for PR kern/189089 220267195Sasomers# Create two tap interfaces and assign them both the same IP address but with 221267195Sasomers# different netmasks, and both on the default FIB. Then remove one's IP 222267195Sasomers# address. Hopefully the machine won't panic. 223267195Sasomersatf_test_case same_ip_multiple_ifaces_fib0 cleanup 224267195Sasomerssame_ip_multiple_ifaces_fib0_head() 225267195Sasomers{ 226267195Sasomers atf_set "descr" "Can remove an IP alias from an interface when the same IP is also assigned to another interface." 227267195Sasomers atf_set "require.user" "root" 228267195Sasomers atf_set "require.config" "fibs" 229267195Sasomers} 230267195Sasomerssame_ip_multiple_ifaces_fib0_body() 231267195Sasomers{ 232267195Sasomers ADDR="192.0.2.2" 233267195Sasomers MASK0="24" 234267195Sasomers MASK1="32" 235267195Sasomers 236267195Sasomers # Unlike most of the tests in this file, this is applicable regardless 237267195Sasomers # of net.add_addr_allfibs 238267195Sasomers 239267195Sasomers # Setup the interfaces, then remove one alias. It should not panic. 240267195Sasomers setup_tap 0 ${ADDR} ${MASK0} 241267195Sasomers TAP0=${TAP} 242267195Sasomers setup_tap 0 ${ADDR} ${MASK1} 243267195Sasomers TAP1=${TAP} 244267195Sasomers ifconfig ${TAP1} -alias ${ADDR} 245267195Sasomers 246267195Sasomers # Do it again, in the opposite order. It should not panic. 247267195Sasomers setup_tap 0 ${ADDR} ${MASK0} 248267195Sasomers TAP0=${TAP} 249267195Sasomers setup_tap 0 ${ADDR} ${MASK1} 250267195Sasomers TAP1=${TAP} 251267195Sasomers ifconfig ${TAP0} -alias ${ADDR} 252267195Sasomers} 253267195Sasomerssame_ip_multiple_ifaces_fib0_cleanup() 254267195Sasomers{ 255267195Sasomers cleanup_tap 256267195Sasomers} 257267195Sasomers 258267195Sasomers# Regression test for PR kern/189088 259267195Sasomers# Test that removing an IP address works even if the same IP is assigned to a 260267195Sasomers# different interface, on a different FIB. Tests the same code that whose 261267195Sasomers# panic was regressed by same_ip_multiple_ifaces_fib0. 262267195Sasomers# Create two tap interfaces and assign them both the same IP address but with 263267195Sasomers# different netmasks, and on different FIBs. Then remove one's IP 264267195Sasomers# address. Hopefully the machine won't panic. Also, the IP's hostroute should 265267195Sasomers# dissappear from the correct fib. 266267195Sasomersatf_test_case same_ip_multiple_ifaces cleanup 267267195Sasomerssame_ip_multiple_ifaces_head() 268267195Sasomers{ 269267195Sasomers 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." 270267195Sasomers atf_set "require.user" "root" 271267195Sasomers atf_set "require.config" "fibs" 272267195Sasomers} 273267195Sasomerssame_ip_multiple_ifaces_body() 274267195Sasomers{ 275267195Sasomers atf_expect_fail "kern/189088 Assigning the same IP to multiple interfaces in different FIBs creates a host route for only one" 276267195Sasomers ADDR="192.0.2.2" 277267195Sasomers MASK0="24" 278267195Sasomers MASK1="32" 279267195Sasomers 280267195Sasomers # Unlike most of the tests in this file, this is applicable regardless 281267195Sasomers # of net.add_addr_allfibs 282267195Sasomers get_fibs 2 283267195Sasomers 284267195Sasomers # Setup the interfaces, then remove one alias. It should not panic. 285267195Sasomers setup_tap ${FIB0} ${ADDR} ${MASK0} 286267195Sasomers TAP0=${TAP} 287267195Sasomers setup_tap ${FIB1} ${ADDR} ${MASK1} 288267195Sasomers TAP1=${TAP} 289267195Sasomers ifconfig ${TAP1} -alias ${ADDR} 290267195Sasomers atf_check -o not-match:"^${ADDR}[[:space:]]" \ 291267195Sasomers setfib ${FIB1} netstat -rn -f inet 292267195Sasomers 293267195Sasomers # Do it again, in the opposite order. It should not panic. 294267195Sasomers setup_tap ${FIB0} ${ADDR} ${MASK0} 295267195Sasomers TAP0=${TAP} 296267195Sasomers setup_tap ${FIB1} ${ADDR} ${MASK1} 297267195Sasomers TAP1=${TAP} 298267195Sasomers ifconfig ${TAP0} -alias ${ADDR} 299267195Sasomers atf_check -o not-match:"^${ADDR}[[:space:]]" \ 300267195Sasomers setfib ${FIB0} netstat -rn -f inet 301267195Sasomers} 302267195Sasomerssame_ip_multiple_ifaces_cleanup() 303267195Sasomers{ 304267195Sasomers # Due to PR kern/189088, we must destroy the interfaces in LIFO order 305267195Sasomers # in order for the routes to be correctly cleaned up. 306267195Sasomers for TAPD in `tail -r "tap_devices_to_cleanup"`; do 307267195Sasomers ifconfig ${TAPD} destroy 308267195Sasomers done 309267195Sasomers} 310267195Sasomers 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 349263445Sasomers# Test that source address selection works correctly for UDP packets with 350263445Sasomers# 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 353267186Sasomers# 354267186Sasomers# The root cause was that ifa_ifwithnet() did not have a fib argument. It 355267186Sasomers# would return an address from an interface on any FIB that had a subnet route 356267186Sasomers# for the destination. If more than one were available, it would choose the 357267186Sasomers# most specific. This is most easily tested by creating a FIB without a 358267186Sasomers# default route, then trying to send a UDP packet with SO_DONTROUTE set to an 359267186Sasomers# address which is not routable on that FIB. Absent the fix for this bug, 360267186Sasomers# in_pcbladdr would choose an interface on any FIB with a default route. With 361267186Sasomers# the fix, you will get EUNREACH or ENETUNREACH. 362263445Sasomersatf_test_case udp_dontroute cleanup 363263445Sasomersudp_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 370263445Sasomersudp_dontroute_body() 371263445Sasomers{ 372263445Sasomers atf_expect_fail "kern/187553 Source address selection for UDP packets with SO_DONTROUTE uses the default FIB" 373263445Sasomers # Configure the TAP interface to use an RFC5737 nonrouteable address 374263445Sasomers # and a non-default fib 375267186Sasomers ADDR0="192.0.2.2" 376267186Sasomers ADDR1="192.0.2.3" 377263445Sasomers SUBNET="192.0.2.0" 378263445Sasomers MASK="24" 379263445Sasomers # Use a different IP on the same subnet as the target 380263445Sasomers TARGET="192.0.2.100" 381267186Sasomers SRCDIR=`atf_get_srcdir` 382263445Sasomers 383263445Sasomers # Check system configuration 384263445Sasomers if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then 385263445Sasomers atf_skip "This test requires net.add_addr_allfibs=0" 386263445Sasomers fi 387267186Sasomers get_fibs 2 388263445Sasomers 389267186Sasomers # Configure the TAP interfaces 390267186Sasomers setup_tap ${FIB0} ${ADDR0} ${MASK} 391267186Sasomers TARGET_TAP=${TAP} 392267186Sasomers setup_tap ${FIB1} ${ADDR1} ${MASK} 393263445Sasomers 394263445Sasomers # Send a UDP packet with SO_DONTROUTE. In the failure case, it will 395267186Sasomers # return ENETUNREACH, or send the packet to the wrong tap 396267186Sasomers atf_check -o ignore setfib ${FIB0} \ 397267186Sasomers ${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP} 398267186Sasomers cleanup_tap 399267186Sasomers 400267186Sasomers # Repeat, but this time target the other tap 401267186Sasomers setup_tap ${FIB0} ${ADDR0} ${MASK} 402267186Sasomers setup_tap ${FIB1} ${ADDR1} ${MASK} 403267186Sasomers TARGET_TAP=${TAP} 404267186Sasomers 405267186Sasomers atf_check -o ignore setfib ${FIB1} \ 406267186Sasomers ${SRCDIR}/udp_dontroute ${TARGET} /dev/${TARGET_TAP} 407263445Sasomers} 408263445Sasomers 409263445Sasomersudp_dontroute_cleanup() 410263445Sasomers{ 411263445Sasomers cleanup_tap 412263445Sasomers} 413263445Sasomers 414263445Sasomers 415263445Sasomersatf_init_test_cases() 416263445Sasomers{ 417263445Sasomers atf_add_test_case arpresolve_checks_interface_fib 418265586Sasomers atf_add_test_case loopback_and_network_routes_on_nondefault_fib 419265586Sasomers atf_add_test_case default_route_with_multiple_fibs_on_same_subnet 420267195Sasomers atf_add_test_case same_ip_multiple_ifaces_fib0 421267195Sasomers atf_add_test_case same_ip_multiple_ifaces 422265586Sasomers atf_add_test_case subnet_route_with_multiple_fibs_on_same_subnet 423263445Sasomers atf_add_test_case udp_dontroute 424263445Sasomers} 425263445Sasomers 426263445Sasomers# Looks up one or more fibs from the configuration data and validates them. 427263445Sasomers# Returns the results in the env varilables FIB0, FIB1, etc. 428263445Sasomers 429263445Sasomers# parameter numfibs The number of fibs to lookup 430263445Sasomersget_fibs() 431263445Sasomers{ 432263445Sasomers NUMFIBS=$1 433263445Sasomers net_fibs=`sysctl -n net.fibs` 434263445Sasomers i=0 435263445Sasomers while [ $i -lt "$NUMFIBS" ]; do 436263445Sasomers fib=`atf_config_get "fibs" | \ 437263445Sasomers awk -v i=$(( i + 1 )) '{print $i}'` 438263445Sasomers echo "fib is ${fib}" 439263445Sasomers eval FIB${i}=${fib} 440263445Sasomers if [ "$fib" -ge "$net_fibs" ]; then 441263445Sasomers atf_skip "The ${i}th configured fib is ${fib}, which is not less than net.fibs, which is ${net_fibs}" 442263445Sasomers fi 443263445Sasomers i=$(( $i + 1 )) 444263445Sasomers done 445263445Sasomers} 446263445Sasomers 447263445Sasomers# Creates a new tap(4) interface, registers it for cleanup, and returns the 448263445Sasomers# name via the environment variable TAP 449263445Sasomersget_tap() 450263445Sasomers{ 451263445Sasomers local TAPN=0 452263445Sasomers while ! ifconfig tap${TAPN} create > /dev/null 2>&1; do 453263445Sasomers if [ "$TAPN" -ge 8 ]; then 454263445Sasomers atf_skip "Could not create a tap(4) interface" 455263445Sasomers else 456263445Sasomers TAPN=$(($TAPN + 1)) 457263445Sasomers fi 458263445Sasomers done 459263445Sasomers local TAPD=tap${TAPN} 460263445Sasomers # Record the TAP device so we can clean it up later 461263445Sasomers echo ${TAPD} >> "tap_devices_to_cleanup" 462263445Sasomers TAP=${TAPD} 463263445Sasomers} 464263445Sasomers 465263445Sasomers# Create a tap(4) interface, configure it, and register it for cleanup. 466263445Sasomers# parameters: 467263445Sasomers# fib 468263445Sasomers# IP address 469263445Sasomers# Netmask in number of bits (eg 24 or 8) 470263445Sasomers# Return: the tap interface name as the env variable TAP 471263445Sasomerssetup_tap() 472263445Sasomers{ 473263445Sasomers local FIB=$1 474263445Sasomers local ADDR=$2 475263445Sasomers local MASK=$3 476263445Sasomers get_tap 477263445Sasomers echo setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB 478263445Sasomers setfib ${FIB} ifconfig $TAP ${ADDR}/${MASK} fib $FIB 479263445Sasomers} 480263445Sasomers 481263445Sasomerscleanup_tap() 482263445Sasomers{ 483317093Sasomers if [ -f tap_devices_to_cleanup ]; then 484317093Sasomers for tap_device in $(cat tap_devices_to_cleanup); do 485317093Sasomers ifconfig "${tap_device}" destroy 486317093Sasomers done 487317093Sasomers rm -f tap_devices_to_cleanup 488317093Sasomers fi 489263445Sasomers} 490