Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: mctp: Test conflicts of connect() with bind()

The addition of connect() adds new conflict cases to test.

Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
Link: https://patch.msgid.link/20250710-mctp-bind-v4-7-8ec2f6460c56@codeconstruct.com.au
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Matt Johnston and committed by
Paolo Abeni
b7e28129 3549eb08

+59 -4
+41 -4
net/mctp/test/sock-test.c
··· 245 245 .bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 2, 246 246 }; 247 247 248 + static const struct mctp_test_bind_setup bind_addrany_net2_type1_peer9 = { 249 + .bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 1, 250 + .have_peer = true, .peer_addr = 9, .peer_net = 2, 251 + }; 252 + 248 253 struct mctp_bind_pair_test { 249 254 const struct mctp_test_bind_setup *bind1; 250 255 const struct mctp_test_bind_setup *bind2; ··· 283 278 * vs ADDR_ANY, explicit default net 1, OK 284 279 */ 285 280 { &bind_addrany_netdefault_type1, &bind_addrany_net1_type1, 0 }, 281 + 282 + /* specific remote peer doesn't conflict with any-peer bind */ 283 + { &bind_addrany_net2_type1_peer9, &bind_addrany_net2_type1, 0 }, 284 + 285 + /* bind() NET_ANY is allowed with a connect() net */ 286 + { &bind_addrany_net2_type1_peer9, &bind_addrany_netdefault_type1, 0 }, 286 287 }; 287 288 288 289 static void mctp_bind_pair_desc(const struct mctp_bind_pair_test *t, char *desc) 289 290 { 291 + char peer1[25] = {0}, peer2[25] = {0}; 292 + 293 + if (t->bind1->have_peer) 294 + snprintf(peer1, sizeof(peer1), ", peer %d net %d", 295 + t->bind1->peer_addr, t->bind1->peer_net); 296 + if (t->bind2->have_peer) 297 + snprintf(peer2, sizeof(peer2), ", peer %d net %d", 298 + t->bind2->peer_addr, t->bind2->peer_net); 299 + 290 300 snprintf(desc, KUNIT_PARAM_DESC_SIZE, 291 - "{bind(addr %d, type %d, net %d)} {bind(addr %d, type %d, net %d)} -> error %d", 292 - t->bind1->bind_addr, t->bind1->bind_type, t->bind1->bind_net, 293 - t->bind2->bind_addr, t->bind2->bind_type, t->bind2->bind_net, 294 - t->error); 301 + "{bind(addr %d, type %d, net %d%s)} {bind(addr %d, type %d, net %d%s)} -> error %d", 302 + t->bind1->bind_addr, t->bind1->bind_type, 303 + t->bind1->bind_net, peer1, 304 + t->bind2->bind_addr, t->bind2->bind_type, 305 + t->bind2->bind_net, peer2, t->error); 295 306 } 296 307 297 308 KUNIT_ARRAY_PARAM(mctp_bind_pair, mctp_bind_pair_tests, mctp_bind_pair_desc); 309 + 310 + static void mctp_test_bind_invalid(struct kunit *test) 311 + { 312 + struct socket *sock; 313 + int rc; 314 + 315 + /* bind() fails if the bind() vs connect() networks mismatch. */ 316 + const struct mctp_test_bind_setup bind_connect_net_mismatch = { 317 + .bind_addr = MCTP_ADDR_ANY, .bind_net = 1, .bind_type = 1, 318 + .have_peer = true, .peer_addr = 9, .peer_net = 2, 319 + }; 320 + mctp_test_bind_run(test, &bind_connect_net_mismatch, &rc, &sock); 321 + KUNIT_EXPECT_EQ(test, -rc, EINVAL); 322 + sock_release(sock); 323 + } 298 324 299 325 static int 300 326 mctp_test_bind_conflicts_inner(struct kunit *test, ··· 384 348 KUNIT_CASE(mctp_test_sock_sendmsg_extaddr), 385 349 KUNIT_CASE(mctp_test_sock_recvmsg_extaddr), 386 350 KUNIT_CASE_PARAM(mctp_test_bind_conflicts, mctp_bind_pair_gen_params), 351 + KUNIT_CASE(mctp_test_bind_invalid), 387 352 {} 388 353 }; 389 354
+14
net/mctp/test/utils.c
··· 271 271 rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, sock); 272 272 KUNIT_ASSERT_EQ(test, rc, 0); 273 273 274 + /* connect() if requested */ 275 + if (setup->have_peer) { 276 + memset(&addr, 0x0, sizeof(addr)); 277 + addr.smctp_family = AF_MCTP; 278 + addr.smctp_network = setup->peer_net; 279 + addr.smctp_addr.s_addr = setup->peer_addr; 280 + /* connect() type must match bind() type */ 281 + addr.smctp_type = setup->bind_type; 282 + rc = kernel_connect(*sock, (struct sockaddr *)&addr, 283 + sizeof(addr), 0); 284 + KUNIT_EXPECT_EQ(test, rc, 0); 285 + } 286 + 287 + /* bind() */ 274 288 memset(&addr, 0x0, sizeof(addr)); 275 289 addr.smctp_family = AF_MCTP; 276 290 addr.smctp_network = setup->bind_net;
+4
net/mctp/test/utils.h
··· 35 35 mctp_eid_t bind_addr; 36 36 int bind_net; 37 37 u8 bind_type; 38 + 39 + bool have_peer; 40 + mctp_eid_t peer_addr; 41 + int peer_net; 38 42 }; 39 43 40 44 struct mctp_test_dev *mctp_test_create_dev(void);