From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796185944; b=qEPodfARz3/iMRGyOK3gtWXM6/eSzEtH1jp/829HwWNr5Uxut/AJVpkEw7hNn7FjcTHt jPaYTvlU0deXkmpvN00jI4wG1JiDy2ZW2qJdEOBnD/R5rO9B/F7Wj+TQT8v7VILWHTyzA CrMW5Fo9KdUMdvLejdCKXaD0oL76+BjJZ2COOYIVdeDHomqV82Nf2WCMGeXU2z7U6P9Td bEpfCFzWrTINALP/WSWfBLlEC54ihf0uSuWex5Ff2tBoY8PJTZKwKiSwqIpDw5z0zPpig II9+pGaUDaCpqhq7YoJNT8N5COhSlLFhQ/MIs6Ud2IWm9SluwjmZn8f3Wt8SFTpgLAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796185944; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=DQpqtzeQ4tdaVP+jaKIuJFHXkd9SQXHV+nknxhEl++M=; b=fcw0brmZvHFeUWNiA/uZg90Ow2Q+rmzCU4zU0F9zxGfejdSu9vYLwes3E0QAyr7a7pjB /BHp67w7aQkj+cS6CDbbDtb40uY5NGUOa+JX8SwOcei54OC0AcK8Yx4zO9kjWRJ+Cf/yd bcSggDRfChfhsAovcAq6XnczzJ2Y3OxwGyLP2fedSQ+zmu1QhVJz4vWISA9FyAOKV0I5u uJEylHb8vJIdEs0yBlPsZJUS+qfQYSl4Gy7VyV+diRZtoePWEG/Alkjyu8VcmNQHtU8cj 1P4ICkw/tVe0YLZWzaPOfjy8hgXYcfB8DBBiqU6mRy77D53JufN0uH25wB+4A11fN2A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=blockcast.net; spf=pass smtp.mailfrom=blockcast.net; dkim=pass header.d=blockcast.net header.i=@blockcast.net header.a=rsa-sha256 header.s=google header.b=JOQSqXGS; arc=none smtp.remote-ip=74.125.82.178 Received: from mail-dy1-f178.google.com (mail-dy1-f178.google.com [74.125.82.178]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64517606A6A4 for ; Wed, 2 Dec 2026 04:32:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 64517606A6A4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=blockcast.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=blockcast.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=blockcast.net header.i=@blockcast.net header.a=rsa-sha256 header.s=google header.b=JOQSqXGS Received: by mail-dy1-f178.google.com with SMTP id 5a478bee46e88-3866a784546so17532635eec.0 for ; Tue, 01 Dec 2026 20:32:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blockcast.net; s=google; t=1796185942; x=1796790742; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DQpqtzeQ4tdaVP+jaKIuJFHXkd9SQXHV+nknxhEl++M=; b=JOQSqXGS8SoOVuucP39ijvo1RGDWuxw9gLvQeXiiUikpOqf3mQo4H90Y7W0tCOKm7U jxUf6QoUebOx52JjfPKgPvRK3mZR6Mf1gbEp++El+AXjpi9VVpNL/Fa2GbLG2DsxI+ZE FHTL/QXzpKL0FCvQ+2Qv6yBEqCgLtSZxzthHDuPpsZDCcUh4sCL+oRPJYNN7v3/0Pf5K 640OVzIJpl/lp8v1IyqbAn59iV6qMYabCcw7HrHlD87SRdZaejViuEOC5rj34lycPy0J 3uB+KbwTcR3xSnmx8zixS132Xhl/INYf449DinbiHmsdV6MkvghQ8zaCGNC+8fu2yEUH Gv0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796185942; x=1796790742; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=DQpqtzeQ4tdaVP+jaKIuJFHXkd9SQXHV+nknxhEl++M=; b=oX1rrgIhKBMkiR33Lwrh/t+DdyTdAAmNcj2GlSkkLBpHh4uslDFBQgAmTJGPlK0St7 BFHdt1s8gInc+34e1RvrLk6hrCgBNJeesI3aO25TcRDo2btOxFni9HYSBvWMkYNNRih+ QecOvF68kHuNqCHJIf0KomKDJqu1u5XHY0hF3WRD98N8eeZRrdqO4DO4oovliscPSfYW rlrPvFQFxLO7yDmFLQQIQgfgDBgeG10xcAWpvcdpBD7717030dIRKb9MToQ7HHIjyVz+ IU0/7mhUvo2Fdcns5lSm0ZDdSvfpPaGn76IZsAFPqC6TpBtTAYY2wt0zhB3hwLncf553 Htxw== X-Forwarded-Encrypted: i=1; AL+hErEtIiCcgDHgmOh2lwUpboFYxW2SOe8YrHEESeygAEXetSgbz3P/gQaaZp1vZsCNoXhGd4W+1YjywSLdENE=@vger.kernel.org X-Gm-Message-State: APuU4Z/tPRBZ/aUVf52fAQDXjOydUT1eNd8PHpbj6mDl5dl67yIO9PPg 9TIM8+20ZCXywJO0f/m5Pyp5vgUI1AdKRnWovs4hpfFmyRDiLpXI0mvXU+sgjvdBtds= X-Gm-Gg: AZFdTZdOcFwjKUFhc2/UkLyKzMCvZmhJi/HL5XJA8jwg8pcSGB6DUH1rLStRmpmKt8B rqATIpl5ffww7awxlxQRNNJLlqXZN3DydJfr1feZDtzJt1AGPXzSqGVF+ZOulCQmlJVMTgJmTBZ whjPDYOR+NAzrC6rFXB/qb6qzifMbh7ZnDU3KvkycPSciyobFOqwQVb4ZNgFRmetED1sck9ZEcA 4duzBjq1Ht9Je9LDZMh80MignQzvsN3b63cMlRP4bD7wsp6BeXdP7AunjMbCQWSWW9zo36ecbli kxABcmGrU7xdxXoHbbmUXdrIsHyQKUOieBoYm367J9ayMotZSniC4PLlFLy1pU573nhpymwPTLz rb3nt7ScMNLaKnuYxonJD4/WHT5ueKe83Uohf90fi4Fm9SX0O7RtN0yr6nuH4AiV9Sn91oWHAtd 1GNYKXbBD2TzTgQpV6bGfugr1zgC1Fq9XwEcGZrmpnKpRmVY0eOlDXnjL6iLA2LNRzNF9I1rg7r UBL9Okwq9+ceCQoD7/6B9qq4WhUH7CC4X+ys4nuYxgldHFXVfuupIOOtol4A6J2seq330Grw8JP Ts8INfSvFtZ9EqTpTPukjLDlXwGbV0vt6uSxMrgXryL9sQlvxz7z+AMY4XtoT3bStaUdoiF8Xek ap21UX6l56nKE5ue/J35RQZUCKjQ= X-Received: by 2002:a05:7300:dd02:b0:386:9957:750e with SMTP id 5a478bee46e88-386995777d2mr34164680eec.27.1796185942249; Tue, 01 Dec 2026 20:32:22 -0800 (PST) Received: from devbox.ts.blockcast.net ([2602:f74d:1::32]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386d605c894sm54632028eec.2.2026.12.01.20.32.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 20:32:21 -0800 (PST) From: Omar Ramadan To: Taehee Yoo , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan Cc: Simon Horman , netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Willem de Bruijn , David Ahern , Ido Schimmel Subject: [PATCH net-next v6 11/12] selftests: net: add amt_v6.sh for an IPv6 outer transport Date: Wed, 2 Dec 2026 04:31:59 +0000 Message-ID: <20261202043200.286260-12-omar@blockcast.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202043200.286260-1-omar@blockcast.net> References: <20261202043200.286260-1-omar@blockcast.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Nothing covers the IPv6 outer transport. amt_v6.sh is amt.sh with the gateway<->relay link moved to IPv6, and a second gateway on it: amtg: mode gateway local 2001:db8:a::1 discovery 2001:db8:a::3 amtg2: mode gateway local 2001:db8:a::4 discovery 2001:db8:a::3 amtr: mode relay local 2001:db8:a::2 The namespaces, inner addresses, smcroute routes and tools are those of amt.sh, plus tcpdump for the Discovery checks, which skip without it. The forwarding checks cover the whole handshake, as the relay forwards only after the gateway's Update. It checks that: - amtg learns 2001:db8:a::2 from an Advertisement requested through the relay's second address, so the Advertisement comes from the address the Discovery was sent to; - IPv4 and IPv6 multicast reach the listener through the tunnel, and IPv4 multicast reaches a listener on the second gateway, which shares the first one's /64 and port; - with max_tunnels 16 the relay allows two tunnels per /64 source: a third gateway in it gets no tunnel and no multicast, one in another /64 does; - the relay's General Queries do not leave through amtr; - with the route MTU to the gateway lowered, an IPv6 payload above the tunnel MTU is dropped and counted in tx_dropped, neither fragmented nor sent to IPv6 output, where it would count in Ip6FragFails; - its source is sent a Packet Too Big if the group is SSM, ff3e::5:6, and none if it is ASM, ff0e::5:6 (RFC 7450 s5.3.3.6.2.2); - a Discovery to ff02::1 draws no reply, while one to the discovery address, the positive control, does; - a Discovery from the wire with the relay's own address, unicast or the subnet-router anycast one, as its source is dropped, counted in rx_dropped and not answered on lo, while the same Discovery over lo, as from a gateway on the relay's host, is answered; - taking amtg down clears the learned relay address, and bringing it up discovers the relay again. A probe creates a throwaway IPv6 relay first and skips when iproute2 does not know amt, when the kernel ignores IFLA_AMT_LOCAL_IP6 ("Local attribute is required") or has no amt link type ("Unknown device type"), and when iproute2 cannot pass the address: it rejects the literal ("expected rather than"), or packs it into the 4-byte IFLA_AMT_LOCAL_IP, which this kernel refuses with "binary attribute size out of range" and an older one creates as an IPv4 relay, read back as an address without a colon. Any other failure is a FAIL. The script skips without CONFIG_IPV6 before it creates a link. A listener that must get nothing has to be alive at the end, so one that never started cannot pass. disc_answer() and local_src_answers() fail with a reason, and tcpdump's own text, when tcpdump does not start or ends early, or a send fails, so a harness failure is not read as "no reply". gw_l is up before it joins the gateway's bridge, and amtg comes up only once its bridge port forwards, so that the relay's first Query reaches the listener. br_gw's MAC is set, as a bridge takes its lowest port's and a port added later would leave the gateway a stale neighbour entry for the relay. The iproute2 side is posted to iproute2-next separately. Assisted-by: LLM Signed-off-by: Omar Ramadan --- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/amt_v6.sh | 716 ++++++++++++++++++++++++++ 2 files changed, 717 insertions(+) create mode 100755 tools/testing/selftests/net/amt_v6.sh diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index fb0a64662..a31f19e9f 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -9,6 +9,7 @@ CFLAGS += -I../ TEST_PROGS := \ altnames.sh \ amt.sh \ + amt_v6.sh \ arp_ndisc_evict_nocarrier.sh \ arp_ndisc_untracked_subnets.sh \ bareudp.sh \ diff --git a/tools/testing/selftests/net/amt_v6.sh b/tools/testing/selftests/net/amt_v6.sh new file mode 100755 index 000000000..c5447ffea --- /dev/null +++ b/tools/testing/selftests/net/amt_v6.sh @@ -0,0 +1,716 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# amt.sh with an IPv6 outer transport: the gateways and the relay speak AMT +# over IPv6 and carry IPv4 and IPv6 multicast inside. The gateway<->relay link +# differs from amt.sh, and a second gateway shares it: +# +# LISTENER l_gw -- gw_l [br0: gw_l, amtg] GATEWAY gw_relay 2001:db8:a::1 +# | IPv6 outer +# [amtg2] GATEWAY2 gw2_relay 2001:db8:a::4 +# | +# SOURCE src_relay -- relay_src [amtr] RELAY br_gw 2001:db8:a::2 +# [br_gw: relay_gw, relay_gw2] 2001:db8:a::3 +# +# The gateways discover the relay (::2) through 2001:db8:a::3, so the Relay +# Advertisement has to come from the address the Discovery was sent to. + +source lib.sh + +readonly GW6="2001:db8:a::1" +readonly GW2_6="2001:db8:a::4" +readonly RELAY6="2001:db8:a::2" +readonly DISC6="2001:db8:a::3" +# A third gateway in the first /64, and one in a /64 of its own. +readonly GW4_6="2001:db8:a::5" +readonly GW3_6="2001:db8:b::5" +readonly DISC6B="2001:db8:b::3" +# An SSM group (RFC 4607, ff3x::/32), next to the ASM group ff0e::5:6. +readonly SSM6="ff3e::5:6" +SMCDIR= + +cleanup() +{ + cleanup_all_ns + [ -n "$SMCDIR" ] && rm -rf "$SMCDIR" +} + +setup_fail() +{ + echo "FAIL: setup failed at line $1" >&2 + exit "$ksft_fail" +} + +amt_field() +{ + ip -n "$1" -d -j link show "$2" | + jq -r ".[0].linkinfo.info_data.$3 // empty" +} + +# remote_is [ ]: the relay address a gateway learned. +remote_is() +{ + [ "$(amt_field "${2:-$GATEWAY}" "${3:-amtg}" remote)" = "$1" ] +} + +gw_l_forwarding() +{ + [ "$(ip netns exec "$GATEWAY" bridge -j link show dev gw_l | + jq -r '.[0].state')" = forwarding ] +} + +snmp6() +{ + ip netns exec "$1" awk -v k="$2" '$1 == k { print $2 }' /proc/net/snmp6 +} + +setup_links() +{ + local ns + + # No DAD: the gateway and relay addresses must not be tentative. + for ns in "$LISTENER" "$GATEWAY" "$GATEWAY2" "$RELAY" "$SOURCE"; do + ip netns exec "$ns" sysctl -wq \ + net.ipv6.conf.all.accept_dad=0 \ + net.ipv6.conf.default.accept_dad=0 + done + + ip -n "$LISTENER" link add l_gw type veth peer name gw_l \ + netns "$GATEWAY" + ip -n "$GATEWAY" link add gw_relay type veth peer name relay_gw \ + netns "$RELAY" + ip -n "$GATEWAY2" link add gw2_relay type veth peer name relay_gw2 \ + netns "$RELAY" + ip -n "$RELAY" link add relay_src type veth peer name src_relay \ + netns "$SOURCE" + + ip -n "$GATEWAY" addr add "$GW6/64" dev gw_relay + ip -n "$GATEWAY" link set gw_relay up + ip -n "$GATEWAY2" addr add "$GW2_6/64" dev gw2_relay + ip -n "$GATEWAY2" link set gw2_relay up + ip -n "$RELAY" link add br_gw type bridge + # A bridge takes its lowest port MAC; a later port must not change it. + ip -n "$RELAY" link set br_gw address 02:00:00:00:0a:03 + ip -n "$RELAY" link set relay_gw master br_gw up + ip -n "$RELAY" link set relay_gw2 master br_gw up + ip -n "$RELAY" addr add "$RELAY6/64" dev br_gw + ip -n "$RELAY" addr add "$DISC6/64" dev br_gw + ip -n "$RELAY" link set br_gw up +} + +# Skip only when iproute2 or the kernel lacks IPv6 AMT support: an iproute2 +# without it rejects the literal or packs it into the 4-byte +# IFLA_AMT_LOCAL_IP (refused here, read back as IPv4 on an older kernel); a +# kernel without it ignores IFLA_AMT_LOCAL_IP6 or does not know amt. +probe_v6_relay() +{ + local err got + + if ! ip link help 2>&1 | grep -q amt; then + echo "SKIP: missing amt support in iproute2" + exit "$ksft_skip" + fi + + if ! err=$(ip -n "$RELAY" link add amtprobe type amt mode relay \ + local "$RELAY6" dev br_gw 2>&1); then + case "$err" in + *"Local attribute is required"*|*"expected rather than"*|\ + *"binary attribute size out of range"*|*"Unknown device type"*) + echo "SKIP: no IPv6 AMT support: $err" + exit "$ksft_skip" + ;; + esac + echo "FAIL: cannot create an IPv6 relay: $err" + exit "$ksft_fail" + fi + got=$(amt_field "$RELAY" amtprobe local) + ip -n "$RELAY" link del amtprobe + if [[ "$got" != *:* ]]; then + echo "SKIP: iproute2 lacks IPv6 AMT support (read back '$got')" + exit "$ksft_skip" + fi +} + +setup_topology() +{ + ip -n "$LISTENER" addr add 192.168.0.2/24 dev l_gw + ip -n "$LISTENER" addr add 2001:db8::2/64 dev l_gw + ip -n "$LISTENER" link set l_gw up + ip -n "$LISTENER" route add default via 192.168.0.1 dev l_gw + ip -n "$LISTENER" route add default via 2001:db8::1 dev l_gw + ip -n "$LISTENER" addr add 239.0.0.1/32 dev l_gw autojoin + ip -n "$LISTENER" addr add ff0e::5:6/128 dev l_gw autojoin + ip -n "$LISTENER" addr add "$SSM6/128" dev l_gw autojoin + + ip -n "$GATEWAY" addr add 192.168.0.1/24 dev gw_l + ip -n "$GATEWAY" addr add 2001:db8::1/64 dev gw_l + ip -n "$GATEWAY" link add br0 type bridge + ip -n "$GATEWAY" link set br0 up + ip -n "$GATEWAY" link set gw_l up + ip -n "$GATEWAY" link set gw_l master br0 + # The relay's first Query must not be flooded past the listener. + slowwait 5 gw_l_forwarding + ip -n "$GATEWAY" link add amtg master br0 type amt mode gateway \ + local "$GW6" discovery "$DISC6" dev gw_relay \ + gateway_port 2268 relay_port 2268 + + # rp_filter off: amtg2's listener gets data from a source with no route. + ip netns exec "$GATEWAY2" sysctl -wq net.ipv4.conf.all.rp_filter=0 \ + net.ipv4.conf.default.rp_filter=0 + ip -n "$GATEWAY2" link add amtg2 type amt mode gateway \ + local "$GW2_6" discovery "$DISC6" dev gw2_relay \ + gateway_port 2268 relay_port 2268 + + ip -n "$RELAY" link add amtr type amt mode relay local "$RELAY6" \ + dev br_gw relay_port 2268 max_tunnels 16 + # max_tunnels 16 allows two tunnels per /64 source (max_tunnels / 8). + # Count the IGMP packets and MLD queries leaving through amtr, expected + # none. + ip netns exec "$RELAY" tc qdisc add dev amtr clsact + ip netns exec "$RELAY" tc filter add dev amtr egress pref 1 \ + protocol ip flower ip_proto 0x2 action pass + ip netns exec "$RELAY" tc filter add dev amtr egress pref 2 \ + protocol ipv6 flower ip_proto icmpv6 type 130 action pass + ip -n "$RELAY" addr add 172.17.0.1/24 dev relay_src + ip -n "$RELAY" addr add 2001:db8:3::1/64 dev relay_src + ip -n "$RELAY" link set relay_src up + ip netns exec "$RELAY" sysctl -wq net.ipv4.ip_forward=1 + ip netns exec "$RELAY" iptables -t mangle -I PREROUTING \ + -d 239.0.0.1 -j TTL --ttl-set 2 + # Only the group: a hop limit rewrite would break Neighbour Discovery. + ip netns exec "$RELAY" ip6tables -t mangle -I PREROUTING \ + -d ff0e::5:6 -j HL --hl-set 2 + ip netns exec "$RELAY" ip6tables -t mangle -I PREROUTING \ + -d "$SSM6" -j HL --hl-set 2 + + ip -n "$SOURCE" addr add 172.17.0.2/24 dev src_relay + ip -n "$SOURCE" addr add 2001:db8:3::2/64 dev src_relay + ip -n "$SOURCE" link set src_relay up + ip -n "$SOURCE" route add default via 172.17.0.1 dev src_relay + ip -n "$SOURCE" route add default via 2001:db8:3::1 dev src_relay + + ip -n "$RELAY" link set amtr up + ip -n "$GATEWAY" link set amtg up + ip -n "$GATEWAY2" link set amtg2 up + + SMCDIR=$(mktemp -d) + ip netns exec "$RELAY" smcrouted -P "$SMCDIR/pid" -u "$SMCDIR/sock" + slowwait 5 test -S "$SMCDIR/sock" + ip netns exec "$RELAY" smcroutectl -u "$SMCDIR/sock" \ + a relay_src 172.17.0.2 239.0.0.1 amtr + ip netns exec "$RELAY" smcroutectl -u "$SMCDIR/sock" \ + a relay_src 2001:db8:3::2 ff0e::5:6 amtr + ip netns exec "$RELAY" smcroutectl -u "$SMCDIR/sock" \ + a relay_src 2001:db8:3::2 "$SSM6" amtr +} + +test_discovery() +{ + RET=0 + slowwait 10 remote_is "$RELAY6" + check_err $? "remote is '$(amt_field "$GATEWAY" amtg remote)'" + log_test "IPv6 discovery through a secondary relay address" +} + +# test_forward +# [none] +# The listener gets the data, or, with "none", nothing. +test_forward() +{ + local ns=$1 addr=$2 port=$3 grp=$4 src=$5 desc=$6 want=${7:-data} + local out pid i rc + + RET=0 + out=$(mktemp) + ip netns exec "$ns" timeout 20 \ + socat -u "$addr,readbytes=128" - > "$out" & + pid=$! + wait_local_port_listen "$ns" "$port" udp + # Paced to outlast a Request/Query refresh of the gateway. + for i in $(seq 40); do + ip netns exec "$SOURCE" bash -c \ + "printf '%s %128s' $src | nc -w 1 -u $grp $port" + [ "$want" = data ] && grep -q "$src" "$out" && break + sleep 0.25 + done + wait "$pid" + rc=$? + if [ "$want" = data ]; then + grep -q "$src" "$out" + check_err $? "nothing from $src reached the listener" + else + ! grep -q "$src" "$out" + check_err $? "$src reached a listener that should get no tunnel" + # Only the timeout ends a live listener; anything else means it + # never ran. + [ "$rc" -eq 124 ] + check_err $? "listener ended early, status $rc" + fi + rm -f "$out" + log_test "$desc" +} + +tx_dropped() +{ + ip -n "$RELAY" -s -j link show amtr | jq -r '.[0].stats64.tx.dropped' +} + +# With the route MTU to the gateway below the payload, the relay must drop the +# payload without fragmenting it, and send its source a Packet Too Big only +# for an SSM group (RFC 7450 s5.3.3.6.2.2). +# test_tmtu +test_tmtu() +{ + local grp=$1 want=$2 desc=$3 + local ptb frag fragfail drop i + + RET=0 + ip -n "$RELAY" -6 route add "$GW6/128" dev br_gw mtu 1400 + ptb=$(snmp6 "$SOURCE" Icmp6InPktTooBigs) + frag=$(snmp6 "$RELAY" Ip6FragCreates) + fragfail=$(snmp6 "$RELAY" Ip6FragFails) + drop=$(tx_dropped) + for i in 1 2 3; do + ip netns exec "$SOURCE" bash -c \ + "printf '%1352s' x | nc -w 1 -u $grp 6000" + done + # The drops show that the payload reached the tunnel, so that "none" is + # not a send that never ran. -gt: the first Packet Too Big caches a path + # MTU in the source, so later datagrams may be fragmented, not dropped. + [ "$(tx_dropped)" -gt "$drop" ] + check_err $? "the relay did not drop the payload" + if [ "$want" = ptb ]; then + [ "$(snmp6 "$SOURCE" Icmp6InPktTooBigs)" -gt "$ptb" ] + check_err $? "the source got no Packet Too Big" + else + [ "$(snmp6 "$SOURCE" Icmp6InPktTooBigs)" -eq "$ptb" ] + check_err $? "the source of an ASM group got a Packet Too Big" + fi + [ "$(snmp6 "$RELAY" Ip6FragCreates)" -eq "$frag" ] + check_err $? "the relay fragmented an AMT datagram" + # amt_udp_xmit() clears ignore_df: an oversized datagram past the tunnel + # MTU check would count as a FragFail, not a FragCreate. + [ "$(snmp6 "$RELAY" Ip6FragFails)" -eq "$fragfail" ] + check_err $? "an oversized AMT datagram reached IPv6 output" + ip -n "$RELAY" -6 route del "$GW6/128" dev br_gw mtu 1400 + log_test "$desc" +} + +# A second gateway in the same /64 and with the same port needs its own +# Membership Query: one sent to the wrong gateway fails the nonce check. +test_second_gateway() +{ + RET=0 + slowwait 10 remote_is "$RELAY6" "$GATEWAY2" amtg2 + check_err $? "amtg2 remote is '$(amt_field "$GATEWAY2" amtg2 remote)'" + log_test "second IPv6 gateway on the link discovers the relay" + + test_forward "$GATEWAY2" \ + UDP4-RECV:4000,ip-add-membership=239.0.0.1:amtg2 \ + 4000 239.0.0.1 172.17.0.2 \ + "IPv4 multicast to a second gateway in the same /64" +} + +# amtg and amtg2 hold the two tunnels the relay allows per /64 source. A +# third gateway in that /64 must get no Query, so no tunnel and no multicast, +# while one in another /64 does. +test_source_cap() +{ + RET=0 + # Each extra gateway gets a link of its own, as a gateway binds to its + # link. + ip -n "$RELAY" addr add "$DISC6B/64" dev br_gw + ip -n "$GATEWAY2" link add gw3_relay type veth peer name relay_gw3 \ + netns "$RELAY" + ip -n "$GATEWAY2" link add gw4_relay type veth peer name relay_gw4 \ + netns "$RELAY" + ip -n "$RELAY" link set relay_gw3 master br_gw up + ip -n "$RELAY" link set relay_gw4 master br_gw up + ip -n "$GATEWAY2" addr add "$GW3_6/64" dev gw3_relay + ip -n "$GATEWAY2" addr add "$GW4_6/64" dev gw4_relay + ip -n "$GATEWAY2" link set gw3_relay up + # The relay answers from its own address in the first /64: reach it here + # too. + ip -n "$GATEWAY2" route add 2001:db8:a::/64 dev gw3_relay metric 2048 + ip -n "$GATEWAY2" link set gw4_relay up + ip -n "$GATEWAY2" link add amtg3 type amt mode gateway \ + local "$GW3_6" discovery "$DISC6B" dev gw3_relay \ + gateway_port 2268 relay_port 2268 + ip -n "$GATEWAY2" link add amtg4 type amt mode gateway \ + local "$GW4_6" discovery "$DISC6" dev gw4_relay \ + gateway_port 2268 relay_port 2268 + ip -n "$GATEWAY2" link set amtg3 up + ip -n "$GATEWAY2" link set amtg4 up + slowwait 10 remote_is "$RELAY6" "$GATEWAY2" amtg3 + check_err $? "amtg3 remote is '$(amt_field "$GATEWAY2" amtg3 remote)'" + slowwait 10 remote_is "$RELAY6" "$GATEWAY2" amtg4 + check_err $? "amtg4 remote is '$(amt_field "$GATEWAY2" amtg4 remote)'" + log_test "gateways in the same /64 and in another discover the relay" + + test_forward "$GATEWAY2" \ + UDP4-RECV:4000,ip-add-membership=239.0.0.1:amtg3 \ + 4000 239.0.0.1 172.17.0.2 \ + "IPv4 multicast to a gateway in another /64" + test_forward "$GATEWAY2" \ + UDP4-RECV:4000,ip-add-membership=239.0.0.1:amtg4 \ + 4000 239.0.0.1 172.17.0.2 \ + "a third gateway in a /64 at its cap gets no tunnel" none + # amtg4 is refused every time it asks, and each refusal counts in + # rx_dropped, which test_local_source() reads. + ip -n "$GATEWAY2" link del amtg4 +} + +# The relay's General Queries leave through the stream device, not amtr. +test_query_egress() +{ + local jqf='[.[].options.actions[0].stats.packets // empty] | add // 0' + local n4 n6 + + RET=0 + n4=$(ip netns exec "$RELAY" tc -s -j filter show dev amtr egress \ + pref 1 | jq "$jqf") + n6=$(ip netns exec "$RELAY" tc -s -j filter show dev amtr egress \ + pref 2 | jq "$jqf") + [ "$n4" -eq 0 ] + check_err $? "$n4 IGMP packets left through amtr" + [ "$n6" -eq 0 ] + check_err $? "$n6 MLD queries left through amtr" + log_test "relay queries bypass the amt device" +} + +# Why the last disc_answer() or local_src_answers() failed: DISC_WHY starts +# with "relay:" or "harness:", DISC_ERR is tcpdump's own text. +DISC_WHY= +DISC_ERR= + +cap_text() +{ + tr '\n' ' ' < "$1" | cut -c1-300 +} + +cap_note() +{ + echo "${DISC_ERR:+ [tcpdump: $DISC_ERR]}" +} + +# relay_state: the relay's neighbour entry for the gateway, its rx_dropped, +# br_gw's MAC and the gateway's entry for the discovery address. +relay_state() +{ + echo "relay: neigh $(ip -n "$RELAY" -6 neigh show "$GW6" dev br_gw | + tr '\n' ' ')rx_dropped $(rx_dropped)" \ + "br_gw $(ip -n "$RELAY" -j link show br_gw | + jq -r '.[0].address')" \ + "gateway: neigh $(ip -n "$GATEWAY" -6 neigh show "$DISC6" \ + dev gw_relay | tr '\n' ' ')" +} + +disc_msg() +{ + echo "$1${DISC_WHY:+ ($DISC_WHY)}$(cap_note)" +} + +why() +{ + DISC_WHY="${DISC_WHY:+$DISC_WHY; }$*" +} + +# disc_answer : send a Relay Discovery from the gateway to +# []:2268 and wait for gw_relay to see a packet matching . Returns +# 0 if it did, 1 if none came, 2 if the harness failed (see DISC_WHY). +disc_answer() +{ + local dst=$1 filter=$2 pid i rc st out cap + local to="UDP6-SENDTO:[$dst]:2268,sourceport=40000" + + DISC_WHY= + DISC_ERR= + cap=$(mktemp) + ip netns exec "$GATEWAY" timeout 30 \ + tcpdump -nni gw_relay -c 1 "$filter" > /dev/null 2> "$cap" & + pid=$! + # Nothing tcpdump reports means anything until it is listening. + for i in $(seq 50); do + grep -q "listening on" "$cap" && break + kill -0 "$pid" 2> /dev/null || break + sleep 0.1 + done + if ! grep -q "listening on" "$cap"; then + if kill -0 "$pid" 2> /dev/null; then + kill "$pid" 2> /dev/null + wait "$pid" 2> /dev/null + why "harness: tcpdump never listened within 5s" + else + wait "$pid" + why "harness: tcpdump exited with status $?" \ + "before listening" + fi + DISC_ERR=$(cap_text "$cap") + rm -f "$cap" + return 2 + fi + # Type 1 (Relay Discovery), version 0, then a nonce. + for i in 1 2 3; do + if ! out=$(printf '\x01\x00\x00\x00\x12\x34\x56\x78' | + ip netns exec "$GATEWAY" socat -u - \ + "$to,so-bindtodevice=gw_relay" 2>&1); then + rc=2 + why "harness: socat failed to send to $dst:" \ + "${out//$'\n'/ }" + fi + sleep 0.5 + done + # Five seconds after the sends, whatever tcpdump's start-up took; + # timeout is only a backstop. + for i in $(seq 50); do + kill -0 "$pid" 2> /dev/null || break + sleep 0.1 + done + if kill -0 "$pid" 2> /dev/null; then + kill "$pid" 2> /dev/null + wait "$pid" 2> /dev/null + why "relay: no Advertisement within 5s after the sends" + DISC_ERR=$(cap_text "$cap") + rm -f "$cap" + return "${rc:-1}" + fi + wait "$pid" + st=$? + DISC_ERR=$(cap_text "$cap") + rm -f "$cap" + case $st in + 0) return "${rc:-0}" ;; + *) + why "harness: tcpdump exited with status $st" + return 2 + ;; + esac +} + +# The relay's socket also receives a Discovery sent to ff02::1, which must not +# be answered: a multicast address cannot be a source (RFC 4291 s2.7). The +# unicast Discovery is the positive control. +test_mcast_discovery() +{ + local rc + + RET=0 + if ! command -v tcpdump > /dev/null; then + log_test_skip "Discovery to ff02::1 is not answered" \ + "tcpdump not installed" + return + fi + # disc_answer's status: 0 answered, 1 no reply, 2 the harness failed. + disc_answer "$DISC6" "udp and src port 2268 and dst port 40000" + rc=$? + [ $rc -eq 0 ] || why "$(relay_state)" + check_err $rc "$(disc_msg "no Advertisement for a unicast Discovery")" + disc_answer ff02::1 "udp and src port 2268 and dst port 40000" + rc=$? + [ $rc -eq 1 ] + check_err $? "$(disc_msg "ff02::1 status $rc, want 1")" + log_test "Discovery to ff02::1 is not answered" +} + +# A namespace on the relay's bridge, holding the relay's address on its lo. +setup_spoof() +{ + setup_ns SPOOF + # No DAD, as in setup_links(): a tentative fe80:: stalls ND. + ip netns exec "$SPOOF" sysctl -wq net.ipv6.conf.all.accept_dad=0 \ + net.ipv6.conf.default.accept_dad=0 + ip -n "$SPOOF" link add spoof_relay type veth peer name relay_spoof \ + netns "$RELAY" + ip -n "$RELAY" link set relay_spoof master br_gw up + ip -n "$SPOOF" addr add 2001:db8:a::7/64 dev spoof_relay nodad + ip -n "$SPOOF" link set spoof_relay up + ip -n "$SPOOF" addr add "$RELAY6/128" dev lo nodad +} + +rx_dropped() +{ + ip -n "$RELAY" -s -j link show amtr | jq -r '.[0].stats64.rx.dropped' +} + +# local_src_answers : send three Relay Discoveries +# from , with as their source, to the relay's second address, and +# set ANSWERS to the Advertisements seen on the relay's lo. Status 2: the +# harness failed (see DISC_WHY). +local_src_answers() +{ + local ns=$1 src=$2 opts=$3 pid i cap err rc=0 out + local to="UDP6-SENDTO:[$DISC6]:2268,bind=[$src]:40000$opts" + + ANSWERS=0 + DISC_WHY= + DISC_ERR= + cap=$(mktemp) + err=$(mktemp) + ip netns exec "$RELAY" timeout 30 tcpdump -nnl -i lo \ + "udp and src port 2268 and dst port 40000" > "$cap" 2> "$err" & + pid=$! + for i in $(seq 50); do + grep -q "listening on" "$err" && break + kill -0 "$pid" 2> /dev/null || break + sleep 0.1 + done + if ! grep -q "listening on" "$err"; then + if kill -0 "$pid" 2> /dev/null; then + kill "$pid" 2> /dev/null + wait "$pid" 2> /dev/null + why "harness: tcpdump never listened within 5s" + else + wait "$pid" + why "harness: tcpdump exited with status $?" \ + "before listening" + fi + DISC_ERR=$(cap_text "$err") + rm -f "$cap" "$err" + return 2 + fi + # Type 1 (Relay Discovery), version 0, then a nonce. + for i in 1 2 3; do + if ! out=$(printf '\x01\x00\x00\x00\x12\x34\x56\x78' | + ip netns exec "$ns" socat -u - "$to" 2>&1); then + rc=2 + why "harness: socat failed to send from $src:" \ + "${out//$'\n'/ }" + fi + sleep 0.3 + done + sleep 1 + # A capture that ended before the sends saw nothing. + if ! kill -0 "$pid" 2> /dev/null; then + rc=2 + why "harness: tcpdump ended before the sends were done" + fi + kill "$pid" 2> /dev/null + wait "$pid" 2> /dev/null + ANSWERS=$(grep -c "UDP" "$cap") + DISC_ERR=$(cap_text "$err") + rm -f "$cap" "$err" + return "$rc" +} + +# A Discovery from the wire with one of the relay's own addresses as its source +# is not answered, or the Advertisement would go to this host over lo, which +# IPv4 input prevents as a martian. SPOOF holds the address on its lo only and +# sends from its veth; each drop counts in rx_dropped, which shows that the +# Discovery arrived. +# test_local_source +test_local_source() +{ + local src=$1 desc=$2 + local d0 d1 rc + + RET=0 + if ! command -v tcpdump > /dev/null; then + log_test_skip "$desc" "tcpdump not installed" + return + fi + d0=$(rx_dropped) + local_src_answers "$SPOOF" "$src" ",so-bindtodevice=spoof_relay" + rc=$? + check_err $rc "$(disc_msg "capture on lo failed")" + d1=$(rx_dropped) + [ "$ANSWERS" -eq 0 ] + check_err $? "the relay answered $ANSWERS times$(cap_note)" + [ $((d1 - d0)) -ge 3 ] + check_err $? "dropped $((d1 - d0)) of 3 Discoveries$(cap_note)" + log_test "$desc" +} + +# The same address over lo is a gateway on the relay's host, and is answered. +test_local_source_lo() +{ + local desc="Discovery over lo from the relay's own address is answered" + local rc + + RET=0 + if ! command -v tcpdump > /dev/null; then + log_test_skip "$desc" "tcpdump not installed" + return + fi + local_src_answers "$RELAY" "$RELAY6" "" + rc=$? + check_err $rc "$(disc_msg "capture on lo failed")" + [ "$ANSWERS" -ge 1 ] + check_err $? "a Discovery over lo was not answered$(cap_note)" + log_test "$desc" +} + +# The subnet-router anycast address of the stream device's prefix, which the +# relay holds once forwarding is on, is the relay's too. +test_local_source_anycast() +{ + local desc="Discovery from the relay's anycast address is not answered" + local anycast=2001:db8:a:: key=20010db8000a00000000000000000000 + + RET=0 + ip netns exec "$RELAY" sysctl -wq net.ipv6.conf.br_gw.forwarding=1 + # Fails, not skips, if the relay does not hold the address. + ip netns exec "$RELAY" grep -q "$key" /proc/net/anycast6 + check_err $? "the relay does not hold $anycast as an anycast address" + ip -n "$SPOOF" addr add "$anycast/128" dev lo nodad + if [ "$RET" -eq 0 ]; then + test_local_source "$anycast" "$desc" + else + log_test "$desc" + fi + ip netns exec "$RELAY" sysctl -wq net.ipv6.conf.br_gw.forwarding=0 +} + +test_down_up() +{ + RET=0 + ip -n "$GATEWAY" link set amtg down + remote_is "" + check_err $? "relay still reported while down" + ip -n "$GATEWAY" link set amtg up + slowwait 10 remote_is "$RELAY6" + check_err $? "relay not rediscovered after up" + log_test "gateway forgets its IPv6 relay on link down" +} + +for cmd in jq socat nc tc smcrouted smcroutectl iptables ip6tables bridge; do + require_command "$cmd" +done + +# Without CONFIG_IPV6 setup_links() would fail first and report FAIL. +if [ ! -e /proc/net/if_inet6 ]; then + echo "SKIP: IPv6 support is disabled" + exit "$ksft_skip" +fi + +trap cleanup EXIT +setup_ns LISTENER GATEWAY GATEWAY2 RELAY SOURCE || exit "$ksft_skip" + +set -E +trap 'setup_fail $LINENO' ERR +setup_links +trap - ERR +probe_v6_relay +trap 'setup_fail $LINENO' ERR +setup_topology +trap - ERR +set +E + +test_discovery +test_forward "$LISTENER" UDP4-LISTEN:4000 4000 239.0.0.1 172.17.0.2 \ + "IPv4 multicast over an IPv6 tunnel" +test_forward "$LISTENER" UDP6-LISTEN:6000 6000 ff0e::5:6 2001:db8:3::2 \ + "IPv6 multicast over an IPv6 tunnel" +test_second_gateway +test_source_cap +test_tmtu "$SSM6" ptb "IPv6 payload above the tunnel MTU, SSM group" +test_tmtu ff0e::5:6 none "IPv6 payload above the tunnel MTU, ASM group" +test_mcast_discovery +setup_spoof +test_local_source "$RELAY6" \ + "Discovery from the relay's own address is not answered" +test_local_source_lo +test_local_source_anycast +test_down_up +test_query_egress + +exit "$EXIT_STATUS"