1# $OpenBSD: ifalocal.sh,v 1.2 2023/10/19 18:36:41 anton Exp $ 2 3# Copyright (c) 2015 Vincent Gross <vgross@openbsd.org> 4# 5# Permission to use, copy, modify, and distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17 18iface_exists() 19{ 20 ifconfig_out=`ifconfig "$1" 2>&1` 21 [ "${ifconfig_out}" != "$1: no such interface" ] 22} 23 24rtable_exists() 25{ 26 route_show_out=`route -T "$1" -n show -inet 2>&1` 27 [ "$route_show_out" != "route: routing table $1: No such file or directory" ] 28} 29 30local_route_count() 31{ 32 matches=`route -T $3 -n show | awk -v "ifp=$2" -v "dest=$1" \ 33 '$1 == dest && $3 == "UHLl" && $8 == ifp { matches++ } END { print 0 + matches }'` 34 if [ "$matches" -ne "$4" ]; then 35 fmt >&2 << EOM 36expected $4 routes to $1 with ifp $2 in rtable $3, found $matches 37EOM 38 return 1 39 fi 40 return 0 41} 42 43 44cleanup() 45{ 46 route -T 6 flush 47 ifconfig vether6 destroy 48 route -T 5 flush 49 ifconfig vether5 destroy 50 route -T 4 flush 51 ifconfig vether4 destroy 52 route -T 3 flush 53 ifconfig vether3 destroy 54 ifconfig lo5 destroy 55 ifconfig lo3 destroy 56} 57 58cleanup_and_die() 59{ 60 echo "$1" >&2 61 cleanup 62 exit 1 63} 64 65test_rc=true 66 67fail_test() 68{ 69 test_rc=false 70} 71 72if false; then 73if iface_exists vether3 || iface_exists vether4 || 74 iface_exists vether5 || iface_exists vether6 ; then 75 cat >&2 << EOM 76this test needs to create interfaces vether0, vether1, vether2 and vether3, 77please destroy them before running this test. 78EOM 79 exit 1 80fi 81fi 82 83ifconfig vether3 create rdomain 3 || cleanup_and_die "Error while creating vether3" 84ifconfig vether4 create rdomain 3 || cleanup_and_die "Error while creating vether4" 85ifconfig vether5 create rdomain 5 || cleanup_and_die "Error while creating vether5" 86ifconfig vether6 create rdomain 5 || cleanup_and_die "Error while creating vether6" 87 88rtable_exists 3 || cleanup_and_die "Error: rtable 3 does not exist but should" 89rtable_exists 5 || cleanup_and_die "Error: rtable 5 does not exist but should" 90 91ifconfig vether3 10.0.0.1/24 up 92route -T 4 add -iface -ifp vether3 10.0.0.0/28 10.0.0.1 93rtable_exists 4 || cleanup_and_die "Error: rtable 4 does not exist but should" 94 95local_route_count 10.0.0.1 vether3 3 1 || test_fail 96local_route_count 10.0.0.1 vether3 4 0 || test_fail 97 98route -T 4 exec ifconfig vether4 10.0.0.2/24 up 99local_route_count 10.0.0.2 vether4 3 1 || test_fail 100local_route_count 10.0.0.2 vether4 4 0 || test_fail 101local_route_count 10.0.0.1 vether3 3 1 || test_fail 102local_route_count 10.0.0.1 vether3 4 0 || test_fail 103 104ifconfig vether5 10.0.0.1/24 up 105route -T 6 add -iface -ifp vether5 10.0.0.0/28 10.0.0.1 106rtable_exists 6 || cleanup_and_die "Error: rtable 6 does not exist but should" 107 108local_route_count 10.0.0.1 vether5 5 1 || test_fail 109local_route_count 10.0.0.1 vether5 6 0 || test_fail 110 111route -T 6 exec ifconfig vether6 10.0.0.2/24 up 112local_route_count 10.0.0.2 vether6 5 1 || test_fail 113local_route_count 10.0.0.2 vether6 6 0 || test_fail 114local_route_count 10.0.0.1 vether5 5 1 || test_fail 115local_route_count 10.0.0.1 vether5 6 0 || test_fail 116 117cleanup 118 119$test_rc 120