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

selftests: net/psock_fanout: unbound socket fanout

Add a test that validates that an unbound packet socket cannot create/join
a fanout group.

Signed-off-by: Gur Stavi <gur.stavi@huawei.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/7612fa90f613100e2b64c563cab3d7fdf36010db.1728802323.git.gur.stavi@huawei.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Gur Stavi and committed by
Jakub Kicinski
7ec02a3a 9317e893

+36
+36
tools/testing/selftests/net/psock_fanout.c
··· 279 279 return 0; 280 280 } 281 281 282 + /* Test that creating/joining a fanout group fails for unbound socket without 283 + * a specified protocol 284 + */ 285 + static void test_unbound_fanout(void) 286 + { 287 + int val, fd0, fd1, err; 288 + 289 + fprintf(stderr, "test: unbound fanout\n"); 290 + fd0 = socket(PF_PACKET, SOCK_RAW, 0); 291 + if (fd0 < 0) { 292 + perror("socket packet"); 293 + exit(1); 294 + } 295 + /* Try to create a new fanout group. Should fail. */ 296 + val = (PACKET_FANOUT_HASH << 16) | 1; 297 + err = setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val)); 298 + if (!err) { 299 + fprintf(stderr, "ERROR: unbound socket fanout create\n"); 300 + exit(1); 301 + } 302 + fd1 = sock_fanout_open(PACKET_FANOUT_HASH, 1); 303 + if (fd1 == -1) { 304 + fprintf(stderr, "ERROR: failed to open HASH socket\n"); 305 + exit(1); 306 + } 307 + /* Try to join an existing fanout group. Should fail. */ 308 + err = setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val)); 309 + if (!err) { 310 + fprintf(stderr, "ERROR: unbound socket fanout join\n"); 311 + exit(1); 312 + } 313 + close(fd0); 314 + close(fd1); 315 + } 316 + 282 317 /* Test illegal mode + flag combination */ 283 318 static void test_control_single(void) 284 319 { ··· 558 523 const int expect_uniqueid[2][2] = { { 20, 20}, { 20, 20 } }; 559 524 int port_off = 2, tries = 20, ret; 560 525 526 + test_unbound_fanout(); 561 527 test_control_single(); 562 528 test_control_group(0); 563 529 test_control_group(1);