Home | History | Annotate | Line # | Download | only in arp
t_arp.sh revision 1.5
      1 #	$NetBSD: t_arp.sh,v 1.5 2015/07/31 02:39:12 ozaki-r 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 inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
     29 HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK=sysctl=yes"
     30 
     31 SOCKSRC=unix://commsock1
     32 SOCKDST=unix://commsock2
     33 IP4SRC=10.0.1.1
     34 IP4DST=10.0.1.2
     35 
     36 DEBUG=false
     37 TIMEOUT=1
     38 
     39 atf_test_case cache_expiration_5s cleanup
     40 atf_test_case cache_expiration_10s cleanup
     41 atf_test_case command cleanup
     42 atf_test_case garp cleanup
     43 atf_test_case cache_overwriting cleanup
     44 
     45 cache_expiration_5s_head()
     46 {
     47 	atf_set "descr" "Tests for ARP cache expiration (5s)"
     48 	atf_set "require.progs" "rump_server"
     49 }
     50 
     51 cache_expiration_10s_head()
     52 {
     53 	atf_set "descr" "Tests for ARP cache expiration (10s)"
     54 	atf_set "require.progs" "rump_server"
     55 }
     56 
     57 command_head()
     58 {
     59 	atf_set "descr" "Tests for commands of arp(8)"
     60 	atf_set "require.progs" "rump_server"
     61 }
     62 
     63 garp_head()
     64 {
     65 	atf_set "descr" "Tests for GARP"
     66 	atf_set "require.progs" "rump_server"
     67 }
     68 
     69 cache_overwriting_head()
     70 {
     71 	atf_set "descr" "Tests for behavior of overwriting ARP caches"
     72 	atf_set "require.progs" "rump_server"
     73 }
     74 
     75 setup_dst_server()
     76 {
     77 	export RUMP_SERVER=$SOCKDST
     78 	atf_check -s exit:0 rump.ifconfig shmif0 create
     79 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
     80 	atf_check -s exit:0 rump.ifconfig shmif0 inet $IP4DST/24
     81 	atf_check -s exit:0 rump.ifconfig shmif0 up
     82 	atf_check -s exit:0 rump.ifconfig -w 10
     83 
     84 	$DEBUG && rump.ifconfig shmif0
     85 	$DEBUG && rump.arp -n -a
     86 }
     87 
     88 setup_src_server()
     89 {
     90 	local prune=$1
     91 	local keep=$2
     92 
     93 	export RUMP_SERVER=$SOCKSRC
     94 
     95 	# Adjust ARP parameters
     96 	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.prune=$prune
     97 	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.keep=$keep
     98 	# Don't refresh to test expiration easily
     99 	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.refresh=0
    100 
    101 	# Setup an interface
    102 	atf_check -s exit:0 rump.ifconfig shmif0 create
    103 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
    104 	atf_check -s exit:0 rump.ifconfig shmif0 inet $IP4SRC/24
    105 	atf_check -s exit:0 rump.ifconfig shmif0 up
    106 	atf_check -s exit:0 rump.ifconfig -w 10
    107 
    108 	# Sanity check
    109 	$DEBUG && rump.ifconfig shmif0
    110 	$DEBUG && rump.arp -n -a
    111 	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
    112 	atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
    113 }
    114 
    115 test_cache_expiration()
    116 {
    117 	local arp_prune=1
    118 	local arp_keep=$1
    119 	local bonus=2
    120 
    121 	atf_check -s exit:0 ${inetserver} $SOCKSRC
    122 	atf_check -s exit:0 ${inetserver} $SOCKDST
    123 
    124 	setup_dst_server
    125 	setup_src_server $arp_prune $arp_keep
    126 
    127 	#
    128 	# Check if a cache is expired expectedly
    129 	#
    130 	export RUMP_SERVER=$SOCKSRC
    131 	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
    132 
    133 	$DEBUG && rump.arp -n -a
    134 	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
    135 	# Should be cached
    136 	atf_check -s exit:0 -o ignore rump.arp -n $IP4DST
    137 
    138 	atf_check -s exit:0 sleep $(($arp_keep + $arp_prune + $bonus))
    139 
    140 	$DEBUG && rump.arp -n -a
    141 	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
    142 	# Should be expired
    143 	#atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
    144 	atf_check -s exit:0 -o match:'incomplete' rump.arp -n $IP4DST
    145 }
    146 
    147 cache_expiration_5s_body()
    148 {
    149 	test_cache_expiration 5
    150 }
    151 
    152 cache_expiration_10s_body()
    153 {
    154 	test_cache_expiration 10
    155 }
    156 
    157 command_body()
    158 {
    159 	local arp_prune=1
    160 	local arp_keep=5
    161 	local bonus=2
    162 
    163 	atf_check -s exit:0 ${inetserver} $SOCKSRC
    164 	atf_check -s exit:0 ${inetserver} $SOCKDST
    165 
    166 	setup_dst_server
    167 	setup_src_server $arp_prune $arp_keep
    168 
    169 	export RUMP_SERVER=$SOCKSRC
    170 
    171 	# Add and delete a static entry
    172 	$DEBUG && rump.arp -n -a
    173 	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10
    174 	$DEBUG && rump.arp -n -a
    175 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.10
    176 	atf_check -s exit:0 -o ignore rump.arp -d 10.0.1.10
    177 	$DEBUG && rump.arp -n -a
    178 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.10
    179 
    180 	# Add multiple entries via a file
    181 	cat - > ./list <<-EOF
    182 	10.0.1.11 b2:a0:20:00:00:11
    183 	10.0.1.12 b2:a0:20:00:00:12
    184 	10.0.1.13 b2:a0:20:00:00:13
    185 	10.0.1.14 b2:a0:20:00:00:14
    186 	10.0.1.15 b2:a0:20:00:00:15
    187 	EOF
    188 	$DEBUG && rump.arp -n -a
    189 	atf_check -s exit:0 -o ignore rump.arp -f ./list
    190 	$DEBUG && rump.arp -n -a
    191 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.11
    192 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.12
    193 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.13
    194 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.14
    195 	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.15
    196 
    197 	# Test arp -a
    198 	atf_check -s exit:0 -o match:'10.0.1.11' rump.arp -n -a
    199 	atf_check -s exit:0 -o match:'10.0.1.12' rump.arp -n -a
    200 	atf_check -s exit:0 -o match:'10.0.1.13' rump.arp -n -a
    201 	atf_check -s exit:0 -o match:'10.0.1.14' rump.arp -n -a
    202 	atf_check -s exit:0 -o match:'10.0.1.15' rump.arp -n -a
    203 
    204 	# Flush all entries
    205 	$DEBUG && rump.arp -n -a
    206 	atf_check -s exit:0 -o ignore rump.arp -d -a
    207 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.11
    208 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.12
    209 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.13
    210 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.14
    211 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.15
    212 	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.1
    213 
    214 	# Test temp option
    215 	$DEBUG && rump.arp -n -a
    216 	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10 temp
    217 	$DEBUG && rump.arp -n -a
    218 	atf_check -s exit:0 -o not-match:'permanent' rump.arp -n 10.0.1.10
    219 
    220 	# Hm? the cache doesn't expire...
    221 	atf_check -s exit:0 sleep $(($arp_keep + $arp_prune + $bonus))
    222 	$DEBUG && rump.arp -n -a
    223 	#atf_check -s exit:0 -o match:'incomplete' rump.arp -n 10.0.1.10
    224 }
    225 
    226 make_pkt_str()
    227 {
    228 	local target=$1
    229 	local sender=$2
    230 	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
    231 	pkt="$pkt Request who-has $target tell $sender, length 28"
    232 	echo $pkt
    233 }
    234 
    235 garp_body()
    236 {
    237 	local pkt=
    238 
    239 	atf_check -s exit:0 ${inetserver} $SOCKSRC
    240 	export RUMP_SERVER=$SOCKSRC
    241 
    242 	# Setup an interface
    243 	atf_check -s exit:0 rump.ifconfig shmif0 create
    244 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
    245 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
    246 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
    247 	atf_check -s exit:0 rump.ifconfig shmif0 up
    248 	$DEBUG && rump.ifconfig shmif0
    249 
    250 	atf_check -s exit:0 sleep 1
    251 	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
    252 
    253 	# A GARP packet is sent for the primary address
    254 	pkt=$(make_pkt_str 10.0.0.1 10.0.0.1)
    255 	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
    256 	# No GARP packet is sent for the alias address
    257 	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
    258 	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
    259 
    260 	atf_check -s exit:0 rump.ifconfig -w 10
    261 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24
    262 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.4/24 alias
    263 
    264 	# No GARP packets are sent during IFF_UP
    265 	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
    266 	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
    267 	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
    268 	pkt=$(make_pkt_str 10.0.0.4 10.0.0.4)
    269 	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
    270 }
    271 
    272 cache_overwriting_body()
    273 {
    274 	local arp_prune=1
    275 	local arp_keep=5
    276 	local bonus=2
    277 
    278 	atf_check -s exit:0 ${inetserver} $SOCKSRC
    279 	atf_check -s exit:0 ${inetserver} $SOCKDST
    280 
    281 	setup_dst_server
    282 	setup_src_server $arp_prune $arp_keep
    283 
    284 	export RUMP_SERVER=$SOCKSRC
    285 
    286 	# Cannot overwrite a permanent cache
    287 	atf_check -s not-exit:0 -e ignore rump.arp -s $IP4SRC b2:a0:20:00:00:ff
    288 	$DEBUG && rump.arp -n -a
    289 
    290 	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
    291 	$DEBUG && rump.arp -n -a
    292 	# Can overwrite a dynamic cache
    293 	atf_check -s exit:0 -o ignore rump.arp -s $IP4DST b2:a0:20:00:00:00
    294 	$DEBUG && rump.arp -n -a
    295 	atf_check -s exit:0 -o match:'permanent' rump.arp -n $IP4DST
    296 
    297 	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10 temp
    298 	$DEBUG && rump.arp -n -a
    299 	atf_check -s exit:0 -o not-match:'permanent' rump.arp -n 10.0.1.10
    300 	# Cannot overwrite a temp cache
    301 	atf_check -s not-exit:0 -e ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:ff
    302 	$DEBUG && rump.arp -n -a
    303 }
    304 
    305 cleanup()
    306 {
    307 	env RUMP_SERVER=$SOCKSRC rump.halt
    308 	env RUMP_SERVER=$SOCKDST rump.halt
    309 }
    310 
    311 dump_src()
    312 {
    313 	export RUMP_SERVER=$SOCKSRC
    314 	rump.netstat -nr
    315 	rump.arp -n -a
    316 	rump.ifconfig
    317 	$HIJACKING dmesg
    318 }
    319 
    320 dump_dst()
    321 {
    322 	export RUMP_SERVER=$SOCKDST
    323 	rump.netstat -nr
    324 	rump.arp -n -a
    325 	rump.ifconfig
    326 	$HIJACKING dmesg
    327 }
    328 
    329 dump()
    330 {
    331 	dump_src
    332 	dump_dst
    333 	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
    334 }
    335 
    336 cache_expiration_5s_cleanup()
    337 {
    338 	$DEBUG && dump
    339 	cleanup
    340 }
    341 
    342 cache_expiration_10s_cleanup()
    343 {
    344 	$DEBUG && dump
    345 	cleanup
    346 }
    347 
    348 command_cleanup()
    349 {
    350 	$DEBUG && dump
    351 	cleanup
    352 }
    353 
    354 garp_cleanup()
    355 {
    356 	$DEBUG && dump_src
    357 	$DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
    358 	env RUMP_SERVER=$SOCKSRC rump.halt
    359 }
    360 
    361 cache_overwriting_cleanup()
    362 {
    363 	$DEBUG && dump
    364 	cleanup
    365 }
    366 
    367 atf_init_test_cases()
    368 {
    369 	atf_add_test_case cache_expiration_5s
    370 	atf_add_test_case cache_expiration_10s
    371 	atf_add_test_case command
    372 	atf_add_test_case garp
    373 	atf_add_test_case cache_overwriting
    374 }
    375