Home | History | Annotate | Line # | Download | only in arp
      1 #	$NetBSD: t_dad.sh,v 1.16 2024/08/20 08:21:48 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 SOCKLOCAL=unix://commsock1
     29 SOCKPEER=unix://commsock2
     30 
     31 DEBUG=${DEBUG:-false}
     32 
     33 atf_test_case dad_basic cleanup
     34 atf_test_case dad_duplicated cleanup
     35 atf_test_case dad_duplicated_nodad cleanup
     36 
     37 dad_basic_head()
     38 {
     39 	atf_set "descr" "Tests for IPv4 DAD basic behavior"
     40 	atf_set "require.progs" "rump_server"
     41 }
     42 
     43 dad_duplicated_head()
     44 {
     45 	atf_set "descr" "Tests for IPv4 DAD duplicated state"
     46 	atf_set "require.progs" "rump_server"
     47 }
     48 
     49 dad_duplicated_nodad_head()
     50 {
     51 	atf_set "descr" "Tests for IPv4 DAD duplicated state w/o DAD"
     52 	atf_set "require.progs" "rump_server"
     53 }
     54 
     55 setup_server()
     56 {
     57 	local sock=$1
     58 	local ip=$2
     59 
     60 	rump_server_add_iface $sock shmif0 bus1
     61 
     62 	export RUMP_SERVER=$sock
     63 	atf_check -s exit:0 rump.ifconfig shmif0 inet $ip/24
     64 	atf_check -s exit:0 rump.ifconfig shmif0 up
     65 	atf_check -s exit:0 rump.ifconfig -w 10
     66 
     67 	$DEBUG && rump.ifconfig shmif0
     68 }
     69 
     70 make_pkt_str()
     71 {
     72 	local target=$1
     73 	local sender=$2
     74 	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP \(0x0806\), length 42:"
     75 	pkt="$pkt Request who-has $target tell $sender, length 28"
     76 	echo $pkt
     77 }
     78 
     79 make_reply_str()
     80 {
     81 	local srcmac=$1
     82 	local dstmac=$2
     83 	local ip=$3
     84 	pkt="$srcmac > $dstmac, ethertype ARP \(0x0806\), length 42:"
     85 	pkt="Reply $ip is-at $srcmac, length 28"
     86 	echo $pkt
     87 }
     88 
     89 dad_basic_body()
     90 {
     91 	local pkt=
     92 
     93 	rump_server_start $SOCKLOCAL
     94 	rump_server_add_iface $SOCKLOCAL shmif0 bus1
     95 
     96 	export RUMP_SERVER=$SOCKLOCAL
     97 
     98 	# Increase the number of trials, which makes the tests stable
     99 	atf_check -s exit:0 -o match:'3 -> 5' \
    100 	    rump.sysctl -w net.inet.ip.dad_count=5
    101 
    102 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
    103 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
    104 	$DEBUG && rump.ifconfig shmif0
    105 
    106 	atf_check -s exit:0 rump.ifconfig shmif0 up
    107 	rump.ifconfig shmif0 > ./out
    108 	$DEBUG && cat ./out
    109 
    110 	# The primary address doesn't start with tentative state
    111 	atf_check -s exit:0 -o not-match:'10\.0\.0\.1.+TENTATIVE' cat ./out
    112 	# The alias address starts with tentative state
    113 	atf_check -s exit:0 -o match:'10\.0\.0\.2.+TENTATIVE' cat ./out
    114 
    115 	atf_check -s exit:0 sleep 2
    116 	extract_new_packets bus1 > ./out
    117 	$DEBUG && cat ./out
    118 
    119 	# Check DAD probe packets
    120 	pkt=$(make_pkt_str 10.0.0.2 0.0.0.0)
    121 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    122 	# No DAD for the primary address
    123 	pkt=$(make_pkt_str 10.0.0.1 0.0.0.0)
    124 	atf_check -s exit:0 -o not-match:"$pkt" cat ./out
    125 
    126 	# Waiting for DAD complete
    127 	atf_check -s exit:0 rump.ifconfig -w 10
    128 	# Give a chance to send a DAD announce packet
    129 	atf_check -s exit:0 sleep 2
    130 	extract_new_packets bus1 > ./out
    131 	$DEBUG && cat ./out
    132 
    133 	# Check the DAD announce packet
    134 	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
    135 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    136 	# The alias address left tentative
    137 	atf_check -s exit:0 -o not-match:'10\.0\.0\.2.+TENTATIVE' \
    138 	    rump.ifconfig shmif0
    139 
    140 	#
    141 	# Add a new address on the fly
    142 	#
    143 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24 alias
    144 
    145 	# The new address starts with tentative state
    146 	atf_check -s exit:0 -o match:'10\.0\.0\.3.+TENTATIVE' \
    147 	    rump.ifconfig shmif0
    148 
    149 	# Check DAD probe packets
    150 	atf_check -s exit:0 sleep 2
    151 	extract_new_packets bus1 > ./out
    152 	$DEBUG && cat ./out
    153 	pkt=$(make_pkt_str 10.0.0.3 0.0.0.0)
    154 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    155 
    156 	# Waiting for DAD complete
    157 	atf_check -s exit:0 rump.ifconfig -w 10
    158 	# Give a chance to send a DAD announce packet
    159 	atf_check -s exit:0 sleep 2
    160 	extract_new_packets bus1 > ./out
    161 	$DEBUG && cat ./out
    162 
    163 	# Check the DAD announce packet
    164 	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
    165 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    166 	# The new address left tentative
    167 	atf_check -s exit:0 -o not-match:'10\.0\.0\.3.+TENTATIVE' \
    168 	    rump.ifconfig shmif0
    169 
    170 	rump_server_destroy_ifaces
    171 }
    172 
    173 dad_duplicated_body()
    174 {
    175 	local localip1=10.0.1.1
    176 	local localip2=10.0.1.11
    177 	local peerip=10.0.1.2
    178 
    179 	rump_server_start $SOCKLOCAL
    180 	rump_server_start $SOCKPEER
    181 
    182 	setup_server $SOCKLOCAL $localip1
    183 	setup_server $SOCKPEER $peerip
    184 
    185 	export RUMP_SERVER=$SOCKLOCAL
    186 
    187 	# The primary address isn't marked as duplicated
    188 	atf_check -s exit:0 -o not-match:"${localip1}.+DUPLICATED" \
    189 	    rump.ifconfig shmif0
    190 
    191 	#
    192 	# Add a new address duplicated with the peer server
    193 	#
    194 	atf_check -s exit:0 rump.ifconfig shmif0 inet $peerip alias
    195 	atf_check -s exit:0 sleep 2
    196 
    197 	# The new address is marked as duplicated
    198 	atf_check -s exit:0 -o match:"${peerip}.+DUPLICATED" \
    199 	    rump.ifconfig shmif0
    200 
    201 	# A unique address isn't marked as duplicated
    202 	atf_check -s exit:0 rump.ifconfig shmif0 inet $localip2 alias
    203 	atf_check -s exit:0 sleep 2
    204 	atf_check -s exit:0 -o not-match:"${localip2}.+DUPLICATED" \
    205 	    rump.ifconfig shmif0
    206 
    207 	rump_server_destroy_ifaces
    208 }
    209 
    210 dad_duplicated_nodad_body()
    211 {
    212 	local localip1=10.0.1.1
    213 	local localip2=10.0.1.11
    214 	local peerip=10.0.1.2
    215 	local lmac= pmac=
    216 
    217 	rump_server_start $SOCKLOCAL
    218 	rump_server_start $SOCKPEER
    219 
    220 	export RUMP_SERVER=$SOCKLOCAL
    221 	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.dad_count=0
    222 	export RUMP_SERVER=$SOCKPEER
    223 	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.ip.dad_count=0
    224 
    225 	setup_server $SOCKLOCAL $localip1
    226 	setup_server $SOCKPEER $peerip
    227 
    228 	export RUMP_SERVER=$SOCKLOCAL
    229 
    230 	# The primary address isn't marked as duplicated
    231 	atf_check -s exit:0 -o not-match:"${localip1}.+DUPLICATED" \
    232 	    rump.ifconfig shmif0
    233 
    234 	extract_new_packets bus1 > ./out
    235 
    236 	# GARP packets are sent
    237 	pkt=$(make_pkt_str $localip1 $localip1)
    238 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    239 	pkt=$(make_pkt_str $peerip $peerip)
    240 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    241 
    242 	# No DAD probe packets are sent
    243 	pkt=$(make_pkt_str $localip1 0.0.0.0)
    244 	atf_check -s exit:0 -o not-match:"$pkt" cat ./out
    245 	pkt=$(make_pkt_str $peerip 0.0.0.0)
    246 	atf_check -s exit:0 -o not-match:"$pkt" cat ./out
    247 
    248 	#
    249 	# Add a new address duplicated with the peer server
    250 	#
    251 	atf_check -s exit:0 rump.ifconfig shmif0 inet $peerip alias
    252 	atf_check -s exit:0 sleep 2
    253 
    254 	# The new address is NOT marked as duplicated
    255 	atf_check -s exit:0 -o not-match:"${peerip}.+DUPLICATED" \
    256 	    rump.ifconfig shmif0
    257 
    258 	lmac=$(get_macaddr $SOCKLOCAL)
    259 	pmac=$(get_macaddr $SOCKPEER)
    260 	extract_new_packets bus1 > ./out
    261 
    262 	# The peer just replies a GARP of the peer
    263 	pkt=$(make_reply_str $pmac $lmac $peerip)
    264 	atf_check -s exit:0 -o match:"$pkt" cat ./out
    265 
    266 	# A unique address isn't marked as duplicated
    267 	atf_check -s exit:0 rump.ifconfig shmif0 inet $localip2 alias
    268 	atf_check -s exit:0 sleep 2
    269 	atf_check -s exit:0 -o not-match:"${localip2}.+DUPLICATED" \
    270 	    rump.ifconfig shmif0
    271 
    272 	rump_server_destroy_ifaces
    273 }
    274 
    275 dad_basic_cleanup()
    276 {
    277 	$DEBUG && dump
    278 	cleanup
    279 }
    280 
    281 dad_duplicated_cleanup()
    282 {
    283 	$DEBUG && dump
    284 	cleanup
    285 }
    286 
    287 dad_duplicated_nodad_cleanup()
    288 {
    289 
    290 	$DEBUG && dump
    291 	cleanup
    292 }
    293 
    294 atf_init_test_cases()
    295 {
    296 	atf_add_test_case dad_basic
    297 	atf_add_test_case dad_duplicated
    298 	atf_add_test_case dad_duplicated_nodad
    299 }
    300