Files
Clara 82be378e72 [posix] tolerate EADDRNOTAVAIL when leaving a link-local group (#13672)
On macOS (and FreeBSD) `UpdateMulticast()` tolerates `EINVAL` from
`IPV6_JOIN_GROUP` for a link-local group: on the first use of a tun
interface the join of `ff02::1` fails this way (the "FIX ME" block;
the cause is still unknown and remains open). The group is then never
joined, so the matching `IPV6_LEAVE_GROUP` fails with `EADDRNOTAVAIL`,
which is not tolerated: `UpdateMulticast()` logs the failure and
`SuccessOrDie()` exits the process with status 1.

OpenThread leaves its multicast groups whenever the Thread interface
goes down while it is up: `ifconfig down`, `reset`, and
`otInstanceFinalize()` at shutdown. So on a fresh macOS host the first
`ifconfig down` kills ot-daemon, and a border router (otbr-agent)
cannot shut down cleanly on its first start. Observed on GitHub's
macos-14 runner: otbr-agent logs `Ignoring IPV6_JOIN_GROUP failure
(EINVAL) for MC LINKLOCAL address (ff02::1)` at start, and at shutdown
its last line is `UpdateMulticast() at netif.cpp:992: Failure`, exit
status 1. The leave's own warning is not logged at that point: the
instance is being finalized and only critical messages pass. That the
leave fails with `EADDRNOTAVAIL` follows from the test below: `EINVAL`
was already tolerated for the leave and the test failed; with
`EADDRNOTAVAIL` tolerated it passes. On a host where the tun interface
had been used before, the join succeeds and so does the leave.

This commit tolerates `EADDRNOTAVAIL` from `IPV6_LEAVE_GROUP` of a
link-local group on macOS and FreeBSD, mirroring the tolerated join:
a link-local leave fails this way only when the group was never
joined, which is the case for a join that was tolerated. The warning
now names the errno and returns right away, so a tolerated failure no
longer also logs "Added/Removed multicast address". Leaves of other
groups and all other errors stay fatal as before, and Linux is
unchanged.

`script/check-posix-pty` now ends the `OT_DAEMON=on` run by taking the
Thread interface down with `ot-ctl ifconfig down` and checking that
ot-daemon is still running and answers. The interactive ot-ctl of the
expect session is ended first on both platforms (ot-daemon serves one
session at a time and interactive ot-ctl reconnects when displaced);
on Linux this is the existing step before the multicast join test.
The daemon's output goes to its own file (`tmp/ot-daemon-output`)
rather than through `tee`, so that its job can be waited for; when the
check fails, the daemon's exit status is printed and its output is
dumped at exit. The check fails on current macOS
(`pty-macos OT_DAEMON=on`: "ot-daemon exited with status 1 on ifconfig
down", with the `UpdateMulticast()` failure in the output) and passes
with this fix; on Linux the join succeeds and the check passes with
and without it. The existing `reset` and `factoryreset` of the test
run before `ifconfig up`, which is why the failure never showed there.
2026-10-05 21:35:06 -07:00

313 lines
9.7 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright (c) 2018, The OpenThread Authors.
# 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.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
#
set -e
set -x
die()
{
echo " *** ERROR: " "$*"
exit 1
}
at_exit()
{
EXIT_CODE=$?
sudo killall expect || true
sudo killall ot-ctl || true
sudo killall ot-daemon || true
sudo killall ot-cli || true
sudo killall ot-rcp || true
sudo killall socat || true
# The daemon's output no longer streams through tee; show it when the check failed.
if [[ ${EXIT_CODE} != 0 && -f ${OT_DAEMON_OUTPUT:-} ]]; then
cat "${OT_DAEMON_OUTPUT}"
fi
exit $EXIT_CODE
}
wait_for_leader()
{
if grep -q leader "$OT_OUTPUT"; then
return 0
else
echo 'Still waiting for leader'
fi
return 1
}
timeout_run()
{
local count="$1"
local exit_code
shift 1
while [[ $count != 0 && $exit_code != 0 ]]; do
count=$((count - 1))
"$@" && return 0 || exit_code=$?
sleep 1
done
return $exit_code
}
do_build()
{
./script/cmake-build simulation
./script/cmake-build posix -DOT_PLATFORM_NETIF=1 -DOT_PLATFORM_UDP=1 -DOT_UDP_FORWARD=0 -DOT_POSIX_MAX_POWER_TABLE=1 -DOT_DAEMON="${OT_DAEMON}" -DOT_READLINE="${OT_READLINE}"
}
# Taking the Thread interface down makes OpenThread leave its multicast groups, which
# ot-daemon treats as fatal when it fails. Verify that ot-daemon survives it.
check_daemon_netif_down()
{
local status
[[ ${OT_DAEMON} == 'on' ]] || return 0
"${OT_CTL[@]}" -I "${NETIF_NAME}" ifconfig down || true
sleep 1
# OT_DAEMON_JOB is sudo (or the sudo function of script/_maybe_isolate), which
# lives as long as ot-daemon and exits with its status.
if ! ps -p "${OT_DAEMON_JOB}" >/dev/null; then
wait "${OT_DAEMON_JOB}" && status=0 || status=$?
die "ot-daemon exited with status ${status} on ifconfig down"
fi
"${OT_CTL[@]}" -I "${NETIF_NAME}" ifconfig | grep 'down' || die 'ot-daemon does not respond after ifconfig down'
}
do_check()
{
trap at_exit INT TERM EXIT
sudo rm -rf tmp
mkdir tmp
SOCAT_PTY="$PWD/tmp/ot-rcp-pty"
OT_OUTPUT="$PWD/tmp/ot-output"
OT_DAEMON_OUTPUT="$PWD/tmp/ot-daemon-output"
RADIO_NCP_PATH="$PWD/build/simulation/examples/apps/ncp/ot-rcp"
socat pty,raw,echo=0,link="${SOCAT_PTY}" exec:"${RADIO_NCP_PATH} 1" &
timeout_run 10 test -c "${SOCAT_PTY}"
echo 'SOCAT_PTY' "${SOCAT_PTY}"
# Cover setting a valid network interface name.
VALID_NETIF_NAME="wan$(date +%H%M%S)"
readonly VALID_NETIF_NAME
RADIO_URL="spinel+hdlc+uart://${SOCAT_PTY}?region=US&max-power-table=11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26"
if [[ ${OT_DAEMON} == 'on' ]]; then
# The log file is in our own tmp directory, so the redirect does not need sudo.
# shellcheck disable=SC2024
sudo -E "$PWD/build/posix/src/posix/ot-daemon" -d7 -v -I "${VALID_NETIF_NAME}" "${RADIO_URL}" >"${OT_DAEMON_OUTPUT}" 2>&1 &
OT_DAEMON_JOB=$!
sleep 3
# macOS cannot explicitly set network interface name
NETIF_NAME=$(grep -o 'Thread interface: .\+' "${OT_DAEMON_OUTPUT}" | cut -d: -f2 | tr -d ' \r\n')
OT_CTL_PATH="$PWD/build/posix/src/posix/ot-ctl"
if [[ ${OT_DAEMON_ALLOW_ALL} == 1 ]] || [[ -n ${OT_ISOLATED_REEXEC:-} ]]; then
OT_CTL=("${OT_CTL_PATH}")
else
OT_CTL=(sudo "${OT_CTL_PATH}")
fi
"${OT_CTL[@]}" -I "${NETIF_NAME}" panid 0xface | grep 'Done' || die 'failed to set panid with ot-ctl'
# verify supports options in OpenThread commands without separator --
"${OT_CTL[@]}" -I "${NETIF_NAME}" pskc -p 123456 | grep 'Done' || die 'unable to set pskc'
# verify this reset and factoryreset end immediately
"${OT_CTL[@]}" -I "${NETIF_NAME}" reset
# sleep a while for daemon ready
sleep 2
"${OT_CTL[@]}" -I "${NETIF_NAME}" factoryreset
# sleep a while for daemon ready
sleep 2
OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH=640
readonly OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH
local -r kMaxStringLength="$((OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH - 1))"
# verify success if command length doesn't exceed the limit
for len in $(seq 1 ${kMaxStringLength}); do
"${OT_CTL[@]}" -I "${NETIF_NAME}" "$(printf '1%.0s' $(seq 1 "${len}"))"
done
# verify failure if command length exceeds the limit
len=${OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH}
if "${OT_CTL[@]}" -I "${NETIF_NAME}" "$(printf '1%.0s' $(seq 1 "${len}"))"; then
die
fi
OT_CLI_CMD="${OT_CTL[*]} -I ${NETIF_NAME}"
else
OT_CLI="$PWD/build/posix/src/posix/ot-cli"
sudo "${OT_CLI}" -I "${VALID_NETIF_NAME}" -n "${RADIO_URL}"
# Cover setting a too long(max is 15 characters) network interface name.
# Expect exit code to be 2(OT_EXIT_INVALID_ARGUMENTS).
INVALID_NETIF_NAME="wan0123456789123"
readonly INVALID_NETIF_NAME
sudo "${OT_CLI}" -I "${INVALID_NETIF_NAME}" -n "${RADIO_URL}" || test $? = 2
OT_CLI_CMD="$PWD/build/posix/src/posix/ot-cli ${RADIO_URL}"
fi
sudo expect <<EOF | tee "${OT_OUTPUT}" &
spawn ${OT_CLI_CMD}
expect_after {
timeout { error }
}
send "region\r\n"
expect "US"
expect "Done"
send "dataset init new\r\n"
expect "Done"
send "dataset commit active\r\n"
expect "Done"
send "routerselectionjitter 1\r\n"
expect "Done"
send "ifconfig up\r\n"
expect "Done"
send "thread start\r\n"
expect "Done"
sleep 10
send "state\r\n"
expect "leader"
expect "Done"
send "extaddr\r\n"
expect "Done"
send "dataset active\r\n"
expect "Done"
send "ipaddr\r\n"
expect "Done"
send "coex\r\n"
expect "Done"
send "coap start\r\n"
expect "Done"
send "coap resource TestResource\r\n"
expect "Done"
send "coap set TestContent\r\n"
expect "Done"
set timeout -1
expect eof
EOF
sleep 5
# wait until the node becomes leader
timeout_run 10 wait_for_leader
# wait coap service start
sleep 5
sudo lsof -iUDP:5683 -nP || die 'Application CoAP port is not available!'
extaddr=$(grep -ao -A +1 'extaddr' "$OT_OUTPUT" | tail -n1 | tr -d '\r\n\0')
echo "Extended address is: ${extaddr}"
prefix=$(grep -ao 'Mesh Local Prefix: [0-9a-f:]\+' "$OT_OUTPUT" | cut -d: -f2- | tr -d ' \r\n')
LEADER_ALOC="${prefix}ff:fe00:fc00"
if [[ ${OT_DAEMON} == 'on' ]]; then
# ot-daemon serves one session at a time and the interactive ot-ctl of the expect
# session reconnects whenever it is displaced, so end it before using ot-ctl below.
sudo killall -9 expect || true
sudo killall -9 ot-ctl || true
fi
# skip testing CoAP for https://github.com/openthread/openthread/issues/6363
if [[ $OSTYPE != "linux-gnu"* ]]; then
check_daemon_netif_down
return 0
fi
if [[ ${OT_DAEMON} == 'on' ]]; then
NETIF_INDEX=$(ip link show "${NETIF_NAME}" | cut -f 1 -d ":" | head -n 1)
sudo env PATH="$(dirname "${OT_CTL_PATH}"):${PATH}" \
python3 "$PWD/tests/scripts/misc/test_multicast_join.py" "${NETIF_INDEX}" "${NETIF_NAME}" \
|| die 'multicast group join failed'
fi
# Retrievie test resource through application CoAP
coap_response=$(coap-client -B 5 -m GET "coap://[${LEADER_ALOC}]:5683/TestResource")
echo "CoAP response is: ${coap_response}"
# Verify CoAP response contains the test content
if [[ ${coap_response} == *TestContent* ]]; then
echo 'Success'
else
die 'Failed to access application CoAP'
fi
# Retrievie extended address through network diagnostic get
coap_response=$(echo -n '120100' | xxd -r -p | coap-client -B 5 -m POST "coap://[${LEADER_ALOC}]:61631/d/dg" -f-)
# Verify Tmf CoAP is blocked
if [[ -z ${coap_response} ]]; then
die 'Tmf is not blocked'
fi
check_daemon_netif_down
}
main()
{
if [[ $# == 0 ]]; then
do_build
do_check
return 0
fi
while [[ $# != 0 ]]; do
case $1 in
build)
do_build
;;
check)
# shellcheck source=script/_maybe_isolate
source script/_maybe_isolate
do_check
;;
*)
echo "Unknown action: $1"
return 1
;;
esac
shift
done
}
main "$@"