From: Oleksij Rempel <o.re…@pengutronix.de> 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 <o.re…@pengutronix.de> Link: https://patch.msgid.link/2026…@pengutronix.de [mkl: switch to static_assert()] Signed-off-by: Marc Kleine-Budde <m…@pengutronix.de> --- .../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