From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796252984; b=pIroSA60UyGwq5mf6LBS8hS+7TmrCIomiVjLuaTDFCPzARV4yxVn6Eap9K5a8dkgyZgh 5FkFDvFDFvpUgOXaArdbvbSt2+WvEbrvwHRei3uCHG7IOsEk5kDYUSaQgPiGJdYgWAo1n bMMCahwR+MVTNC3ECyOMhOUUAdCs461ueyhkCwI3agrrIhXMAKBgl6I2HoPGiB8yLPUSp 6xKzxHbX92D6yu4vdJLfi4IK+U6EE2tCH5wQtOTpKlsq0MJC/KXJW/5olVMUvEViHaRgw G5v33dIm9hV9x4pDTNMMWFSTAkhnGiag/rk5OAxfJnO3WRJG/MAvcCSuJ957CAUy9vA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796252984; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=eWc2c6pnyz2V4N4fU7MMT9/h3QEf+tyGuDsba2s8y8g=; b=wsKLuFPfSyL9AJGq2zdqcEP84sWPyAmqYmeggxTwCWI6aBu6LhF7ua93BC8fN956Ttgp L+iR+JdNPPy+qiiuzMiDl2Qw0dx9SRXvhtHBDNc7jNDidI3sqdgAyHlgfdsWVzCZlMpUk Ort4MVV3mKuT62f9NfrewBzfgNnWI4/AiuIeT00zpH8ckpm4aZrAHlA3Z5xG8EGqIS1x4 v5MNNTs5SVKTVtrCqOPUuPcT15eBkq2oqEdJ+LGTzw8s9g/zojG9U6wlzEiL35sKM+hgW ozfGizTM6tj0h14QT+TGkK5wSR9zzDxYTAcDL8ND0svmC18dyDsvpmtSr4SK0naKO4w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=U3YL8YVB; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E108B606A32C; Wed, 2 Dec 2026 23:09:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E108B606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=U3YL8YVB Received: from pengutronix.de (unknown [IPv6:2a0e:1580:1000::2dff:fe0e:e982]) (Authenticated sender: mkl@pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 1D3E520582F; Thu, 03 Dec 2026 00:09:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796252978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eWc2c6pnyz2V4N4fU7MMT9/h3QEf+tyGuDsba2s8y8g=; b=U3YL8YVBIZoP35X4wvzgcXIbeIIpjIMYft/Bd32vKqP98/bVk15p5nWvvFM3Ogk9tp3IZz D+0fMkd6CCQ8Oz0sjQe0KtipfZMjj3G2wWeFc65WGClymA20gGsfSJRWEuP2PgrmcGeylS NrXUQiftwRViRUgwW7Vsc9LQvWa5Czb+jKzI/aPiK6H0XOYdgh20e50pNM7madJ7gBmAuh S1fUdReD0yeQkOGvap0Mp2KUzgzrsiZHbqpJ+JZRPMuTEOKqlM5FJ38TXCsZ4hglQINDbu insFMwo63sei8LYtxgFzwpDRI5LNyyYRuLiHfskqRXcG+erqfb9+x480fwdSzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796252978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eWc2c6pnyz2V4N4fU7MMT9/h3QEf+tyGuDsba2s8y8g=; b=Gl+ubEctN/1M6HqgFfPAKSMO3tir/rsh+/5HwNbXYXM8mZFhtWRG+dUljlgAKtH4GTk91S AIz6JZ3bhqAsLU4R1T2iftkBRDbQh/7kOIQufMtGoeXtW0Qec44HhE3k5bgl2D30uLFtwA 1+WiRuGiH1EqSxBXT5UPloICPkt2eZJUNnszoF/T9i5nIZf5J9UMNdAosq1dm6o2e4bksr 7/OojGphRzYopc39kEfO/slDeX/PRwmDQ5YiMtKLAiidXUaFLCxUz8ugehyDxYaL/YBnLS UVTzaF/YxjK598aq7+W0rjsIB2YxjXzJPlJ3Naeh9OLfpg8iWDViigtzN31OsA== ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796252978; a=rsa-sha256; cv=none; b=NU5u5C2nY0gTCuaKa/2EOi7GKvulXLKeGV28tvV71Wz1o8EDamAN37xZqNA/VrQXKfEl3O vN8M4ny2sADxDMvqMfHSTrhsESHUgVxqA+QwrRMGmLg66zPA1d4fdKyiOWk8SKyDmwwHAG gAVmdy+0hD0EeLxeS2uSpfyGLoDHJQsumiLmsg6ET0OeCq3qyT9I5MEMvI8Tw+4AFPJuoz F75pU2LzA2erWLBupTSMPcfz37VrcFXvClGn14RVwJ364PSnLq/knpXgG5iCTgiXjbke8j Op6gzAZoEwaMatRAqdhswr/3rZAok6rsLlMvC4KSwB/NKJeJc+JGpsY1UOkQCQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de From: Marc Kleine-Budde To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, Oleksij Rempel , Marc Kleine-Budde Subject: [PATCH net-next 09/23] selftests: can: check SO_J1939_PROMISC receive effect Date: Thu, 3 Dec 2026 00:04:34 +0100 Message-ID: <20261202230853.2665144-10-mkl@pengutronix.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202230853.2665144-1-mkl@pengutronix.de> References: <20261202230853.2665144-1-mkl@pengutronix.de> X-Mailing-List: netdev@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 From: Oleksij Rempel Add a test that SO_J1939_PROMISC changes what the RX path delivers. The existing coverage only round-trips the flag through set/getsockopt, so a kernel that stores the flag but never acts on it still passes. The test uses one probe per filter. One probe goes to a different address from the bound one. The other has a different PGN. The test sends the probes three times: before the flag is set, while the flag is set, and after the flag is cleared. The probes must arrive only while the flag is set. After the flag is cleared, the test also sends a frame for the bound address and PGN. That frame must arrive. Signed-off-by: Oleksij Rempel Link: https://patch.msgid.link/20261130-j1939-selftests-v7-9-2491693ccb8d@pengutronix.de [mkl: switch to static_assert()] Signed-off-by: Marc Kleine-Budde --- .../selftests/net/can/test_j1939_opts.c | 138 +++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/net/can/test_j1939_opts.c b/tools/testing/selftests/net/can/test_j1939_opts.c index b535acea198c..ab54119ecbd0 100644 --- a/tools/testing/selftests/net/can/test_j1939_opts.c +++ b/tools/testing/selftests/net/can/test_j1939_opts.c @@ -6,8 +6,9 @@ * and addressing: the SO_J1939_FILTER PGN whitelist, the SO_J1939_SEND_PRIO * send priority seen in the CAN id (via a CAN_RAW peer, including the kernel's * default), unconnected sendto(2), a wrong-family bind rejection, - * SO_J1939_ERRQUEUE TX-completion notifications, and set/getsockopt - * round-trips of SO_J1939_SEND_PRIO, SO_J1939_PROMISC and SO_J1939_ERRQUEUE. + * SO_J1939_ERRQUEUE TX-completion notifications, the SO_J1939_PROMISC receive + * effect, and set/getsockopt round-trips of SO_J1939_SEND_PRIO, + * SO_J1939_PROMISC and SO_J1939_ERRQUEUE. * * Run by test_j1939_opts.sh, see j1939_tests.h. */ @@ -18,6 +19,10 @@ /* arbitrary addresses */ #define SA_TX 0x80 #define SA_RX 0x90 +#define SA_OTHER 0x50 /* neither SA_TX nor SA_RX */ +static_assert(SA_OTHER != SA_TX && SA_OTHER != SA_RX && + SA_OTHER <= J1939_MAX_UNICAST_ADDR, + "SA_OTHER must be a third unicast address"); static_assert(SA_TX != SA_RX && SA_TX <= J1939_MAX_UNICAST_ADDR && SA_RX <= J1939_MAX_UNICAST_ADDR, "SA_TX and SA_RX must be two different unicast addresses"); @@ -172,6 +177,135 @@ TEST_F(j1939_opts, filter_pgn) TH_LOG("rx got another frame or an error after PGN_A"); } +/* + * rx binds to (PROMISC_BOUND_DA, PROMISC_BOUND_PGN). Without SO_J1939_PROMISC + * it must not get the probes below. Each probe differs from the binding only + * in the address or only in the PGN, so each filter is tested on its own. + */ +#define PROMISC_BOUND_DA SA_RX +#define PROMISC_BOUND_PGN PGN_A +#define PROMISC_OTHER_DA SA_OTHER +#define PROMISC_OTHER_PGN PGN_B +static_assert((PROMISC_OTHER_DA != PROMISC_BOUND_DA) && + (PROMISC_OTHER_PGN != PROMISC_BOUND_PGN), + "each probe must differ from the binding"); +static_assert(J1939_IS_PDU1(PROMISC_BOUND_PGN) && + PROMISC_BOUND_DA <= J1939_MAX_UNICAST_ADDR, + "the binding must be a unicast address and a PDU1 PGN"); +static_assert(J1939_IS_PDU1(PROMISC_OTHER_PGN) && + PROMISC_OTHER_DA <= J1939_MAX_UNICAST_ADDR, + "the probes must use a unicast address and a PDU1 PGN"); + +enum { PROBE_OTHER_PGN, PROBE_OTHER_DA, PROBE_NUM }; + +static const struct { + uint8_t da; + pgn_t pgn; +} promisc_probes[] = { + [PROBE_OTHER_PGN] = { PROMISC_BOUND_DA, PROMISC_OTHER_PGN }, + [PROBE_OTHER_DA] = { PROMISC_OTHER_DA, PROMISC_BOUND_PGN }, +}; + +static_assert(ARRAY_SIZE(promisc_probes) == PROBE_NUM, + "promisc_probes needs one row per probe"); + +/* + * Send every probe from tx and check that rx gets none of them. Keep + * sending after a probe arrives, so each filter is checked on its own. + * Return false if rx gets a probe or reports an error. Also return false + * at once if a probe cannot be sent. + */ +static bool no_probe_received_ok(FIXTURE_DATA(j1939_opts) *self, + const uint8_t *buf, size_t len) +{ + bool ok = true; + size_t i; + + for (i = 0; i < ARRAY_SIZE(promisc_probes); i++) { + if (!j1939_sendto_ok(self->tx, buf, len, self->ifindex, + J1939_NO_NAME, promisc_probes[i].da, + promisc_probes[i].pgn)) { + fprintf(stderr, "# sendto probe %zu failed: %d\n", i, + errno); + return false; + } + if (!recv_none_ok(self->rx)) { + fprintf(stderr, "# rx got probe %zu or an error\n", i); + ok = false; + } + } + return ok; +} + +/* SO_J1939_PROMISC delivers frames that do not match the bound (addr, pgn). */ +TEST_F(j1939_opts, promisc_receive) +{ + const pgn_t bound_pgn = PROMISC_BOUND_PGN; + struct sockaddr_can match_src = {}; + uint8_t match_buf[CAN_MAX_DLEN]; + uint8_t tx_buf[CAN_MAX_DLEN]; + size_t i; + + fill_pattern(tx_buf, sizeof(tx_buf)); + memcpy(match_buf, tx_buf, sizeof(match_buf)); + match_buf[0] ^= UINT8_MAX; + + self->rx = j1939_open(self->ifindex, PROMISC_BOUND_DA, + PROMISC_BOUND_PGN); + ASSERT_GE(self->rx, 0); + self->tx = j1939_open(self->ifindex, SA_TX, J1939_NO_PGN); + ASSERT_GE(self->tx, 0); + + EXPECT_TRUE(no_probe_received_ok(self, tx_buf, sizeof(tx_buf))) + TH_LOG("probe check without promisc failed"); + + ASSERT_TRUE(j1939_set_flag_ok(self->rx, SO_J1939_PROMISC, 1)) + TH_LOG("setting SO_J1939_PROMISC failed: %d", errno); + + for (i = 0; i < ARRAY_SIZE(promisc_probes); i++) { + struct sockaddr_can src = {}; + bool got; + + ASSERT_TRUE(j1939_sendto_ok(self->tx, tx_buf, sizeof(tx_buf), + self->ifindex, J1939_NO_NAME, + promisc_probes[i].da, + promisc_probes[i].pgn)) + TH_LOG("sendto probe %zu failed: %d", i, errno); + got = recvfrom_verify_ok(self->rx, tx_buf, sizeof(tx_buf), + J1939_RECV_TIMEOUT_MS, &src); + EXPECT_TRUE(got) + TH_LOG("probe %zu not received despite promisc", i); + if (!got) + continue; + + /* the probes carry the same payload, so check which one came */ + EXPECT_EQ(src.can_addr.j1939.pgn, promisc_probes[i].pgn) + TH_LOG("probe %zu came with PGN 0x%05x", i, + src.can_addr.j1939.pgn); + } + + /* with promisc cleared, the bound (addr, pgn) filters again */ + ASSERT_TRUE(j1939_set_flag_ok(self->rx, SO_J1939_PROMISC, 0)) + TH_LOG("clearing SO_J1939_PROMISC failed: %d", errno); + EXPECT_TRUE(no_probe_received_ok(self, tx_buf, sizeof(tx_buf))) + TH_LOG("probe check after clearing promisc failed"); + + /* + * Send a frame for the bound address and PGN. It must still + * arrive. A socket that receives nothing would pass the probe + * check after clearing, too. The frame has its own payload, so a + * late probe cannot be mistaken for it. + */ + ASSERT_TRUE(j1939_sendto_ok(self->tx, match_buf, sizeof(match_buf), + self->ifindex, J1939_NO_NAME, + PROMISC_BOUND_DA, PROMISC_BOUND_PGN)) + TH_LOG("sendto of the matching frame failed: %d", errno); + ASSERT_TRUE(recvfrom_verify_ok(self->rx, match_buf, sizeof(match_buf), + J1939_RECV_TIMEOUT_MS, &match_src)) + TH_LOG("no matching frame after promisc was cleared"); + EXPECT_EQ(match_src.can_addr.j1939.pgn, bound_pgn); +} + /* Without SO_J1939_SEND_PRIO the kernel's default priority is used. */ TEST_F(j1939_opts, send_prio_default) { -- 2.53.0