1# $OpenBSD: ifalocal.sh,v 1.1 2015/10/26 08:04:10 vgross 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} 55 56cleanup_and_die() 57{ 58 echo "$1" >&2 59 cleanup 60 exit 1 61} 62 63test_rc=true 64 65fail_test() 66{ 67 test_rc=false 68} 69 70if false; then 71if iface_exists vether3 || iface_exists vether4 || 72 iface_exists vether5 || iface_exists vether6 ; then 73 cat >&2 << EOM 74this test needs to create interfaces vether0, vether1, vether2 and vether3, 75please destroy them before running this test. 76EOM 77 exit 1 78fi 79fi 80 81ifconfig vether3 create rdomain 3 || cleanup_and_die "Error while creating vether3" 82ifconfig vether4 create rdomain 3 || cleanup_and_die "Error while creating vether4" 83ifconfig vether5 create rdomain 5 || cleanup_and_die "Error while creating vether5" 84ifconfig vether6 create rdomain 5 || cleanup_and_die "Error while creating vether6" 85 86rtable_exists 3 || cleanup_and_die "Error: rtable 3 does not exist but should" 87rtable_exists 5 || cleanup_and_die "Error: rtable 5 does not exist but should" 88 89ifconfig vether3 10.0.0.1/24 up 90route -T 4 add -iface -ifp vether3 10.0.0.0/28 10.0.0.1 91rtable_exists 4 || cleanup_and_die "Error: rtable 4 does not exist but should" 92 93local_route_count 10.0.0.1 vether3 3 1 || test_fail 94local_route_count 10.0.0.1 vether3 4 0 || test_fail 95 96route -T 4 exec ifconfig vether4 10.0.0.2/24 up 97local_route_count 10.0.0.2 vether4 3 1 || test_fail 98local_route_count 10.0.0.2 vether4 4 0 || test_fail 99local_route_count 10.0.0.1 vether3 3 1 || test_fail 100local_route_count 10.0.0.1 vether3 4 0 || test_fail 101 102ifconfig vether5 10.0.0.1/24 up 103route -T 6 add -iface -ifp vether5 10.0.0.0/28 10.0.0.1 104rtable_exists 6 || cleanup_and_die "Error: rtable 6 does not exist but should" 105 106local_route_count 10.0.0.1 vether5 5 1 || test_fail 107local_route_count 10.0.0.1 vether5 6 0 || test_fail 108 109route -T 6 exec ifconfig vether6 10.0.0.2/24 up 110local_route_count 10.0.0.2 vether6 5 1 || test_fail 111local_route_count 10.0.0.2 vether6 6 0 || test_fail 112local_route_count 10.0.0.1 vether5 5 1 || test_fail 113local_route_count 10.0.0.1 vether5 6 0 || test_fail 114 115cleanup 116 117$test_rc 118