# $NetBSD: t_arp.sh,v 1.48 2024/09/09 07:26:42 ozaki-r Exp $
#
# Copyright (c) 2015 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Shorten the expire time of cache entries
if [ $keep != 0 ]; then
# Convert to ms
keep=$(($keep * 1000))
atf_check -s exit:0 -o ignore \
rump.sysctl -w net.inet.arp.nd_reachable=$keep
fi
# Make a permanent cache entry to avoid sending an NS packet disturbing
# the test
macaddr=$(get_macaddr $SOCKSRC shmif0)
export RUMP_SERVER=$SOCKDST
atf_check -s exit:0 -o ignore rump.arp -s $IP4SRC $macaddr
export RUMP_SERVER=$SOCKSRC
#
# Check if a cache is expired expectedly
#
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
$DEBUG && rump.arp -n -a
atf_check -s not-exit:0 -o ignore -e match:'no entry' rump.arp -n $IP4SRC
# Should be cached
atf_check -s exit:0 -o not-match:'permanent' rump.arp -n $IP4DST
timeout=$(get_timeout $IP4DST)
atf_check -s exit:0 sleep $(($timeout + 1))
$DEBUG && rump.arp -n -a
atf_check -s not-exit:0 -o ignore -e match:'no entry' rump.arp -n $IP4SRC
# Expired but remains until GC sweaps it (1 day)
atf_check -s exit:0 -o match:"$ONEDAYISH" rump.arp -n $IP4DST
rump_server_destroy_ifaces
}
check_arp_static_entry()
{
local ip=$1
local mac=$2
local type=$3
local flags=
#
# Assign an address to an interface without IFF_UP
#
# A GARP packet is sent for the primary address
pkt=$(make_pkt_str_arpreq 10.0.0.1 10.0.0.1)
atf_check -s exit:0 -o match:"$pkt" cat ./out
atf_check -s exit:0 rump.ifconfig shmif0 down
atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
pkt=$(make_pkt_str_arpreq 10.0.0.3 10.0.0.3)
if $no_dad; then
# A GARP packet is sent
atf_check -s exit:0 -o match:"$pkt" cat ./out
else
# No GARP packet is sent
atf_check -s exit:0 -o not-match:"$pkt" cat ./out
fi
# Alias address
atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.4/24 alias
pkt=$(make_pkt_str_arpreq 10.0.0.4 10.0.0.4)
if $no_dad; then
# A GARP packet is sent
atf_check -s exit:0 -o match:"$pkt" cat ./out
else
# No GARP packet is sent
atf_check -s exit:0 -o not-match:"$pkt" cat ./out
fi
#
# GARP on Link up
#
atf_check -s exit:0 rump.ifconfig shmif0 media none
extract_new_packets bus1 > ./out
atf_check -s exit:0 rump.ifconfig shmif0 media auto
if [ "$type" = "pub" ]; then
opts="pub"
else
opts="pub proxy"
fi
# Always proxy only since migrating to lltable/llentry
title='published \(proxy only\)'
#
# Test#1: First setup an endpoint then create proxy arp entry
#
export RUMP_SERVER=$SOCKDST
rump_server_add_iface $SOCKDST tap1
atf_check -s exit:0 rump.ifconfig tap1 $IP4DST_PROXYARP1/24 up
atf_check -s exit:0 rump.ifconfig -w 10
# Try to ping (should fail w/o proxy arp)
export RUMP_SERVER=$SOCKSRC
atf_check -s not-exit:0 -o ignore -e ignore \
rump.ping -n -w 1 -c 1 $IP4DST_PROXYARP1
# Remove ARP entry as it may hang around in WAITDELETE a few seconds
atf_check -s ignore rump.arp -d $IP4DST_PROXYARP1
#
# Test#2: Create proxy arp entry then set up an endpoint
#
export RUMP_SERVER=$SOCKDST
atf_check -s exit:0 -o ignore \
rump.arp -s $IP4DST_PROXYARP2 $macaddr_dst $opts
atf_check -s exit:0 -o match:"$title" rump.arp -n $IP4DST_PROXYARP2
$DEBUG && rump.netstat -nr -f inet
# Try to ping (should fail because no endpoint exists)
export RUMP_SERVER=$SOCKSRC
atf_check -s not-exit:0 -o ignore -e ignore \
rump.ping -n -w 1 -c 1 $IP4DST_PROXYARP2
# Remove ARP entry as it may hang around in WAITDELETE a few seconds
atf_check -s ignore rump.arp -d $IP4DST_PROXYARP2
# Set a (valid) static ARP entry for the src server
export RUMP_SERVER=$SOCKDST
$DEBUG && rump.arp -n -a
atf_check -s exit:0 -o ignore rump.arp -s $IP4SRC $macaddr_src
$DEBUG && rump.arp -n -a
# Test receiving an ARP request with the static ARP entry (as spa/sha)
export RUMP_SERVER=$SOCKSRC
atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $IP4DST
rump_server_destroy_ifaces
}
test_rtm()
{
local macaddr_src= macaddr_dst=
local file=./tmp
local pid= hdr= what= addr=
# ARP cache entries are not created for DAD/GARP packets.
export RUMP_SERVER=$SOCKSRC
atf_check -s exit:0 -o empty rump.arp -n -a
export RUMP_SERVER=$SOCKDST
atf_check -s exit:0 -o empty rump.arp -n -a
atf_check -s exit:0 -o match:"\? \(10.0.1.2\) at $macaddr_dst on shmif0 [0-9]+s R" \
rump.arp -n -a
export RUMP_SERVER=$SOCKDST
# An entry was first created as stale then sending an ARP reply made it delay.
atf_check -s exit:0 -o match:"\? \(10.0.1.1\) at $macaddr_src on shmif0 [0-9]+s D" \
rump.arp -n -a
# The sender resolves the receiver's address.
pkt=$(make_pkt_str_arpreq 10.0.1.2 10.0.1.1)
atf_check -s exit:0 -o match:"$pkt" cat ./out
# The receiver doesn't resolv the sender's address because the ARP request
# from the sender has let make an entry already.
pkt=$(make_pkt_str_arpreq 10.0.1.1 10.0.1.2)
atf_check -s exit:0 -o not-match:"$pkt" cat ./out
add_test cache_expiration "Tests for ARP cache expiration"
add_test command "Tests for arp_commands of arp(8)"
add_test garp "Tests for GARP"
add_test garp_without_dad "Tests for GARP with DAD disabled"
add_test cache_overwriting "Tests for behavior of overwriting ARP caches"
add_test proxy_arp_pub "Tests for Proxy ARP (pub)"
add_test proxy_arp_pubproxy "Tests for Proxy ARP (pub proxy)"
add_test link_activation "Tests for activating a new MAC address"
add_test static "Tests for static ARP entries"
add_test rtm "Tests for routing messages on operations of ARP entries"
add_test purge_on_route_change "Tests if ARP entries are removed on route change"
add_test purge_on_route_delete "Tests if ARP entries are removed on route delete"
add_test purge_on_ifdown "Tests if ARP entries are removed on interface down"
add_test stray_entries "Tests if ARP entries are removed on route change"
add_test cache_creation "Tests for ARP cache creation"
add_test cache_creation_nodad "Tests for ARP cache creation without DAD"
}