Home | History | Annotate | Line # | Download | only in route
t_flags.sh revision 1.11
      1 #	$NetBSD: t_flags.sh,v 1.11 2016/08/10 23:00:39 roy Exp $
      2 #
      3 # Copyright (c) 2015 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 netserver=\
     29 "rump_server -lrumpdev -lrumpnet -lrumpnet_net -lrumpnet_netinet \
     30 	-lrumpnet_shmif"
     31 SOCK_LOCAL=unix://commsock1
     32 SOCK_PEER=unix://commsock2
     33 SOCK_GW=unix://commsock3
     34 BUS=bus1
     35 BUS2=bus2
     36 
     37 DEBUG=false
     38 
     39 setup_local()
     40 {
     41 
     42 	atf_check -s exit:0 ${netserver} ${SOCK_LOCAL}
     43 
     44 	export RUMP_SERVER=$SOCK_LOCAL
     45 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     46 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     47 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.2/24
     48 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     49 
     50 	$DEBUG && rump.ifconfig
     51 	$DEBUG && rump.netstat -rn -f inet
     52 }
     53 
     54 setup_peer()
     55 {
     56 
     57 	atf_check -s exit:0 ${netserver} ${SOCK_PEER}
     58 
     59 	export RUMP_SERVER=$SOCK_PEER
     60 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     61 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     62 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.1/24
     63 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     64 
     65 	$DEBUG && rump.ifconfig
     66 	$DEBUG && rump.netstat -rn -f inet
     67 }
     68 
     69 setup_gw()
     70 {
     71 
     72 	atf_check -s exit:0 ${netserver} ${SOCK_GW}
     73 
     74 	export RUMP_SERVER=$SOCK_GW
     75 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     76 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     77 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.254/24
     78 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     79 
     80 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 create
     81 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 linkstr ${BUS2}
     82 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 10.0.2.1/24
     83 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 alias 10.0.2.2/24
     84 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 up
     85 
     86 	# Wait until DAD completes (10 sec at most)
     87 	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
     88 	atf_check -s not-exit:0 -x "rump.ifconfig shmif1 |grep -q tentative"
     89 
     90 	$DEBUG && rump.ifconfig
     91 	$DEBUG && rump.netstat -rn -f inet
     92 }
     93 
     94 teardown_gw()
     95 {
     96 
     97 	env RUMP_SERVER=$SOCK_GW rump.halt
     98 }
     99 
    100 check_entry_flags()
    101 {
    102 	local ip=$(echo $1 |sed 's/\./\\./g')
    103 	local flags=$2
    104 
    105 	atf_check -s exit:0 -o match:" $flags " -e ignore -x \
    106 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    107 }
    108 
    109 check_entry_gw()
    110 {
    111 	local ip=$(echo $1 |sed 's/\./\\./g')
    112 	local gw=$2
    113 
    114 	atf_check -s exit:0 -o match:" $gw " -e ignore -x \
    115 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    116 }
    117 
    118 check_entry_fail()
    119 {
    120 	ip=$(echo $1 |sed 's/\./\\./g')
    121 	flags=$2  # Not used currently
    122 
    123 	atf_check -s not-exit:0 -e ignore -x \
    124 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    125 }
    126 
    127 test_lo()
    128 {
    129 
    130 	export RUMP_SERVER=$SOCK_LOCAL
    131 
    132 	# Up, Host, local
    133 	check_entry_flags 127.0.0.1 UHl
    134 }
    135 
    136 test_connected()
    137 {
    138 
    139 	export RUMP_SERVER=$SOCK_LOCAL
    140 
    141 	# Up, Host, LLINFO, local
    142 	check_entry_flags 10.0.0.2 UHl
    143 
    144 	# Up, Cloning
    145 	check_entry_flags 10.0.0/24 UC
    146 }
    147 
    148 test_default_gateway()
    149 {
    150 
    151 	export RUMP_SERVER=$SOCK_LOCAL
    152 
    153 	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
    154 	$DEBUG && rump.netstat -rn -f inet
    155 
    156 	# Up, Gateway, Static
    157 	check_entry_flags default UGS
    158 }
    159 
    160 test_static()
    161 {
    162 
    163 	export RUMP_SERVER=$SOCK_LOCAL
    164 
    165 	# Static route to host
    166 	atf_check -s exit:0 -o ignore rump.route add 10.0.1.1 10.0.0.1
    167 	$DEBUG && rump.netstat -rn -f inet
    168 
    169 	# Up, Gateway, Host, Static
    170 	check_entry_flags 10.0.1.1 UGHS
    171 
    172 	# Static route to network
    173 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.1
    174 	$DEBUG && rump.netstat -rn -f inet
    175 
    176 	# Up, Gateway, Static
    177 	check_entry_flags 10.0.2/24 UGS
    178 }
    179 
    180 test_blackhole()
    181 {
    182 
    183 	export RUMP_SERVER=$SOCK_LOCAL
    184 
    185 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.0.1
    186 
    187 	# Delete an existing route first
    188 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    189 
    190 	# Gateway must be lo0
    191 	atf_check -s exit:0 -o ignore \
    192 	    rump.route add -net 10.0.0.0/24 127.0.0.1 -blackhole
    193 	$DEBUG && rump.netstat -rn -f inet
    194 
    195 	# Up, Gateway, Blackhole, Static
    196 	check_entry_flags 10.0.0/24 UGBS
    197 
    198 	atf_check -s not-exit:0 -o match:'100.0% packet loss' \
    199 	    rump.ping -n -w 1 -c 1 10.0.0.1
    200 	$DEBUG && rump.netstat -rn -f inet
    201 
    202 	# Shouldn't be created
    203 	check_entry_fail 10.0.0.1 UH
    204 }
    205 
    206 test_reject()
    207 {
    208 
    209 	export RUMP_SERVER=$SOCK_LOCAL
    210 
    211 	# Delete an existing route first
    212 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    213 
    214 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -reject
    215 	$DEBUG && rump.netstat -rn -f inet
    216 
    217 	# Up, Gateway, Reject, Static
    218 	check_entry_flags 10.0.0/24 UGRS
    219 
    220 	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
    221 	    rump.ping -n -w 1 -c 1 10.0.0.1
    222 	$DEBUG && rump.netstat -rn -f inet
    223 
    224 	# Shouldn't be created
    225 	check_entry_fail 10.0.0.1 UH
    226 
    227 	# Gateway is lo0 (RTF_GATEWAY)
    228 
    229 	# Delete an existing route first
    230 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    231 
    232 	atf_check -s exit:0 -o ignore \
    233 	    rump.route add -net 10.0.0.0/24 127.0.0.1 -reject
    234 	$DEBUG && rump.netstat -rn -f inet
    235 
    236 	# Up, Gateway, Reject, Static
    237 	check_entry_flags 10.0.0/24 UGRS
    238 
    239 	atf_check -s not-exit:0 -o ignore -e match:'Network is unreachable' \
    240 	    rump.ping -n -w 1 -c 1 10.0.0.1
    241 	$DEBUG && rump.netstat -rn -f inet
    242 
    243 	# Shouldn't be created
    244 	check_entry_fail 10.0.0.1 UH
    245 
    246 	# Gateway is lo0 (RTF_HOST)
    247 
    248 	# Delete an existing route first
    249 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    250 
    251 	atf_check -s exit:0 -o ignore \
    252 	    rump.route add -host 10.0.0.1/24 127.0.0.1 -iface -reject
    253 	$DEBUG && rump.netstat -rn -f inet
    254 
    255 	# Up, Host, Reject, Static
    256 	check_entry_flags 10.0.0.1 UHRS
    257 
    258 	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
    259 	    rump.ping -n -w 1 -c 1 10.0.0.1
    260 	$DEBUG && rump.netstat -rn -f inet
    261 
    262 	return 0
    263 }
    264 
    265 test_icmp_redirect()
    266 {
    267 
    268 	### Testing Dynamic flag ###
    269 
    270 	#
    271 	# Setup a gateway 10.0.0.254. 10.0.2.1 is behind it.
    272 	#
    273 	setup_gw
    274 
    275 	#
    276 	# Teach the peer that 10.0.2.* is behind 10.0.0.254
    277 	#
    278 	export RUMP_SERVER=$SOCK_PEER
    279 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.254
    280 	# Up, Gateway, Static
    281 	check_entry_flags 10.0.2/24 UGS
    282 
    283 	#
    284 	# Setup the default gateway to the peer, 10.0.0.1
    285 	#
    286 	export RUMP_SERVER=$SOCK_LOCAL
    287 	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
    288 	# Up, Gateway, Static
    289 	check_entry_flags default UGS
    290 
    291 	# Try ping 10.0.2.1
    292 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
    293 	$DEBUG && rump.netstat -rn -f inet
    294 
    295 	# Up, Gateway, Host, Dynamic
    296 	check_entry_flags 10.0.2.1 UGHD
    297 	check_entry_gw 10.0.2.1 10.0.0.254
    298 
    299 	export RUMP_SERVER=$SOCK_PEER
    300 	$DEBUG && rump.netstat -rn -f inet
    301 
    302 	### Testing Modified flag ###
    303 
    304 	#
    305 	# Teach a wrong route to 10.0.2.2
    306 	#
    307 	export RUMP_SERVER=$SOCK_LOCAL
    308 	atf_check -s exit:0 -o ignore rump.route add 10.0.2.2 10.0.0.1
    309 	# Up, Gateway, Host, Static
    310 	check_entry_flags 10.0.2.2 UGHS
    311 	check_entry_gw 10.0.2.2 10.0.0.1
    312 
    313 	# Try ping 10.0.2.2
    314 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.2
    315 	$DEBUG && rump.netstat -rn -f inet
    316 
    317 	# Up, Gateway, Host, Modified, Static
    318 	check_entry_flags 10.0.2.2 UGHMS
    319 	check_entry_gw 10.0.2.2 10.0.0.254
    320 
    321 	teardown_gw
    322 }
    323 
    324 test_announce()
    325 {
    326 	export RUMP_SERVER=$SOCK_LOCAL
    327 
    328 	# Delete an existing route first
    329 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    330 
    331 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -proxy
    332 	$DEBUG && rump.netstat -rn -f inet
    333 
    334 	# Up, Gateway, Static, proxy
    335 	check_entry_flags 10.0.0/24 UGSp
    336 
    337 	# TODO test its behavior
    338 }
    339 
    340 cleanup()
    341 {
    342 	$DEBUG && /usr/bin/shmif_dumpbus -p - $BUS 2>/dev/null | \
    343 	    /usr/sbin/tcpdump -n -e -r -
    344 	env RUMP_SERVER=$SOCK_LOCAL rump.halt
    345 	env RUMP_SERVER=$SOCK_PEER rump.halt
    346 }
    347 
    348 add_test()
    349 {
    350 	local name=$1
    351 	local desc="$2"
    352 
    353 	atf_test_case "route_flags_${name}" cleanup
    354 	eval "route_flags_${name}_head() { \
    355 			atf_set \"descr\" \"${desc}\"; \
    356 			atf_set \"require.progs\" \"rump_server\"; \
    357 		}; \
    358 	    route_flags_${name}_body() { \
    359 			setup_local; \
    360 			setup_peer; \
    361 			test_${name}; \
    362 		}; \
    363 	    route_flags_${name}_cleanup() { \
    364 			cleanup; \
    365 		}"
    366 	atf_add_test_case "route_flags_${name}"
    367 }
    368 
    369 atf_init_test_cases()
    370 {
    371 
    372 	add_test lo              "Tests route flags: loop back interface"
    373 	add_test connected       "Tests route flags: connected route"
    374 	add_test default_gateway "Tests route flags: default gateway"
    375 	add_test static          "Tests route flags: static route"
    376 	add_test blackhole       "Tests route flags: blackhole route"
    377 	add_test reject          "Tests route flags: reject route"
    378 	add_test icmp_redirect   "Tests route flags: icmp redirect"
    379 	add_test announce        "Tests route flags: announce flag"
    380 }
    381