From nobody Wed Nov 27 11:50:26 2024 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A92A1A00ED; Thu, 10 Oct 2024 10:26:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556007; cv=none; b=UoHhAPepv0PmpkN9q6q/AAtkyorRbs+j1MdqVzpFuk62GWZUemuweIBSdhImt4xJqM79qFvduWSimOVnf0LrbwLJ87xLNtgRlfjUlZqjNtXK2Ss9DXprPusXpCB1e9rP01pTbMdN+Br2dUbsfRrwLBAGOWp7A2+kGvI9jDnjR0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556007; c=relaxed/simple; bh=2QFNQC4hlBgvHrnE16j+dyLIZp6idLhknvErN++4uFo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qbg0wTZeNZ57SLJI+1Pj/wmcBfBPDpP1y4VCXGpQXueoOIFoxoAWjuNm4HjVbQOAy98WLwwLN7rRtH4zpfOHgh477x8eGxBoG3iwGkaOU9LPdgCmX3ska7u9S5suAVFe8u/fY279PzcIS4jnCKKLQwDbNkYC5on0mYqLJypll9Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4XPQr22C83z6K6gm; Thu, 10 Oct 2024 18:25:22 +0800 (CST) Received: from frapeml500005.china.huawei.com (unknown [7.182.85.13]) by mail.maildlp.com (Postfix) with ESMTPS id D2EA6140CB9; Thu, 10 Oct 2024 18:26:42 +0800 (CST) Received: from china (10.200.201.82) by frapeml500005.china.huawei.com (7.182.85.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 10 Oct 2024 12:26:37 +0200 From: Gur Stavi To: Gur Stavi CC: , , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Willem de Bruijn , Subject: [PATCH net-next v03 1/3] af_packet: allow fanout_add when socket is not RUNNING Date: Thu, 10 Oct 2024 13:25:12 +0300 Message-ID: <9e15c0c2cd19d94207a1791de0dc9051a5abb95a.1728555449.git.gur.stavi@huawei.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To frapeml500005.china.huawei.com (7.182.85.13) Content-Type: text/plain; charset="utf-8" PACKET socket can retain its fanout membership through link down and up and leave a fanout while closed regardless of link state. However, socket was forbidden from joining a fanout while it was not RUNNING. This patch allows PACKET socket to join fanout while not RUNNING. The previous test for RUNNING also implicitly tested that the socket is bound to a device. An explicit test of ifindex was added instead. Signed-off-by: Gur Stavi --- net/packet/af_packet.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index f8942062f776..8137c33ab0fd 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1843,26 +1843,29 @@ static int fanout_add(struct sock *sk, struct fanou= t_args *args) match->prot_hook.ignore_outgoing =3D type_flags & PACKET_FANOUT_FLAG_IGN= ORE_OUTGOING; list_add(&match->list, &fanout_list); } - err =3D -EINVAL; =20 spin_lock(&po->bind_lock); - if (packet_sock_flag(po, PACKET_SOCK_RUNNING) && - match->type =3D=3D type && - match->prot_hook.type =3D=3D po->prot_hook.type && - match->prot_hook.dev =3D=3D po->prot_hook.dev) { + if (po->ifindex =3D=3D -1 || po->num =3D=3D 0) { + /* Socket can not receive packets */ + err =3D -ENXIO; + } else if (match->type !=3D type || + match->prot_hook.type !=3D po->prot_hook.type || + match->prot_hook.dev !=3D po->prot_hook.dev) { + /* Joining an existing group, properties must be identical */ + err =3D -EINVAL; + } else if (refcount_read(&match->sk_ref) >=3D match->max_num_members) { err =3D -ENOSPC; - if (refcount_read(&match->sk_ref) < match->max_num_members) { + } else { + /* Paired with packet_setsockopt(PACKET_FANOUT_DATA) */ + WRITE_ONCE(po->fanout, match); + po->rollover =3D rollover; + rollover =3D NULL; + refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1); + if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) { __dev_remove_pack(&po->prot_hook); - - /* Paired with packet_setsockopt(PACKET_FANOUT_DATA) */ - WRITE_ONCE(po->fanout, match); - - po->rollover =3D rollover; - rollover =3D NULL; - refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1); __fanout_link(sk, po); - err =3D 0; } + err =3D 0; } spin_unlock(&po->bind_lock); =20 @@ -3452,8 +3455,12 @@ static int packet_create(struct net *net, struct soc= ket *sock, int protocol, po->prot_hook.af_packet_net =3D sock_net(sk); =20 if (proto) { + /* Implicitly bind socket to "any interface" */ + po->ifindex =3D 0; po->prot_hook.type =3D proto; __register_prot_hook(sk); + } else { + po->ifindex =3D -1; } =20 mutex_lock(&net->packet.sklist_lock); --=20 2.45.2 From nobody Wed Nov 27 11:50:26 2024 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97F871BF324; Thu, 10 Oct 2024 10:26:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556012; cv=none; b=H1FIG+7BxK9xlnAUDyVuhlyLQcLDwj2BR4jWeL9ry2Vnj3HaJnlRl4/gReAjZdKgAVaFFe1xid1yFvfoD49CyJ+iQpb7uepr5ju2ypVwXWirsm4JdkPPJR9deNFZWjsW0u0CazELdsc9FqqfA5yFlgX7DdsFnMyn7+dR9VieH5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556012; c=relaxed/simple; bh=n6Pyknfvtq8jCstOTEB1lqtTghs0zco2bDfrzky9UhI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bskAck0AQi6v7TKIZKzFGYFu1FY9U6mjLsLXTbHWdTHws8oovNyh86HqRIja36So5HOhKzsJ38q9Lm03Xq9M3/50Xrc8/4tGYAvv3hBmXosVuGNUp8uKLqUBvnuOUUJM230dR4JZK0TlYGvOmQ9HZ2oDzswZPAxGShdol6gCIXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4XPQmh115qz6LCfc; Thu, 10 Oct 2024 18:22:28 +0800 (CST) Received: from frapeml500005.china.huawei.com (unknown [7.182.85.13]) by mail.maildlp.com (Postfix) with ESMTPS id BFBF9140A36; Thu, 10 Oct 2024 18:26:48 +0800 (CST) Received: from china (10.200.201.82) by frapeml500005.china.huawei.com (7.182.85.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 10 Oct 2024 12:26:43 +0200 From: Gur Stavi To: Gur Stavi CC: , , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Willem de Bruijn , Subject: [PATCH net-next v03 2/3] selftests: net/psock_fanout: socket joins fanout when link is down Date: Thu, 10 Oct 2024 13:25:13 +0300 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To frapeml500005.china.huawei.com (7.182.85.13) Content-Type: text/plain; charset="utf-8" Modify test_control_group to have toggle parameter. When toggle is non-zero, loopback device will be set down for the initialization of fd[1] which is still expected to successfully join the fanout. Signed-off-by: Gur Stavi --- tools/testing/selftests/net/psock_fanout.c | 42 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/net/psock_fanout.c b/tools/testing/sel= ftests/net/psock_fanout.c index 4f31e92ebd96..acdfae8f8a9a 100644 --- a/tools/testing/selftests/net/psock_fanout.c +++ b/tools/testing/selftests/net/psock_fanout.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,33 @@ =20 static uint32_t cfg_max_num_members; =20 +static void loopback_set_up_down(int state_up) +{ + struct ifreq ifreq =3D {}; + int fd, err; + + fd =3D socket(AF_PACKET, SOCK_RAW, 0); + if (fd < 0) { + perror("socket loopback"); + exit(1); + } + strcpy(ifreq.ifr_name, "lo"); + err =3D ioctl(fd, SIOCGIFFLAGS, &ifreq); + if (err) { + perror("SIOCGIFFLAGS"); + exit(1); + } + if (state_up !=3D !!(ifreq.ifr_flags & IFF_UP)) { + ifreq.ifr_flags ^=3D IFF_UP; + err =3D ioctl(fd, SIOCSIFFLAGS, &ifreq); + if (err) { + perror("SIOCSIFFLAGS"); + exit(1); + } + } + close(fd); +} + /* Open a socket in a given fanout mode. * @return -1 if mode is bad, a valid socket otherwise */ static int sock_fanout_open(uint16_t typeflags, uint16_t group_id) @@ -264,17 +292,22 @@ static void test_control_single(void) } =20 /* Test illegal group with different modes or flags */ -static void test_control_group(void) +static void test_control_group(int toggle) { int fds[2]; =20 - fprintf(stderr, "test: control multiple sockets\n"); + if (toggle) + fprintf(stderr, "test: control multiple sockets with link down toggle\n"= ); + else + fprintf(stderr, "test: control multiple sockets\n"); =20 fds[0] =3D sock_fanout_open(PACKET_FANOUT_HASH, 0); if (fds[0] =3D=3D -1) { fprintf(stderr, "ERROR: failed to open HASH socket\n"); exit(1); } + if (toggle) + loopback_set_up_down(0); if (sock_fanout_open(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG, 0) !=3D -1) { fprintf(stderr, "ERROR: joined group with wrong flag defrag\n"); @@ -294,6 +327,8 @@ static void test_control_group(void) fprintf(stderr, "ERROR: failed to join group\n"); exit(1); } + if (toggle) + loopback_set_up_down(1); if (close(fds[1]) || close(fds[0])) { fprintf(stderr, "ERROR: closing sockets\n"); exit(1); @@ -489,7 +524,8 @@ int main(int argc, char **argv) int port_off =3D 2, tries =3D 20, ret; =20 test_control_single(); - test_control_group(); + test_control_group(0); + test_control_group(1); test_control_group_max_num_members(); test_unique_fanout_group_ids(); =20 --=20 2.45.2 From nobody Wed Nov 27 11:50:26 2024 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89DB229AF; Thu, 10 Oct 2024 10:26:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556017; cv=none; b=AiFG8q/0/U/LAsCLrNFDoq0PXazIsawTuHqs5NI73UZIvJ2mJG5izaji7WwJTnQYBHpxDYbVzyjgX4B5Izp8HpyV/pMN+zO2yC/d3gYislf3HphByKXIqu9p9kAfK7yc7Uq1qunabpkE+rSTPFpF0JBAaYiWAB/qgOjOAjpnp7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728556017; c=relaxed/simple; bh=CFzNbzW1eEnEF6SnRyQ57jkQR8QwGYNI0Fag43gdHio=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZwEKNF+x87CV5lI6WQWW7Ri2XuIDV5wAI69x9weU806OuhQR4M1eoz2TBLhq+zIu6iWmAaQyMM0jNvsUdHT+aniUKea59+MpQ8PAIBrByYlcj/4//bGaQdkxVL8ckdPh9E16bEOtoeoLrivWQZOEGBzkRUjnOkj7L01S0E7AEfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.31]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4XPQmp0YYfz6L75q; Thu, 10 Oct 2024 18:22:34 +0800 (CST) Received: from frapeml500005.china.huawei.com (unknown [7.182.85.13]) by mail.maildlp.com (Postfix) with ESMTPS id AF0D01400DD; Thu, 10 Oct 2024 18:26:54 +0800 (CST) Received: from china (10.200.201.82) by frapeml500005.china.huawei.com (7.182.85.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 10 Oct 2024 12:26:49 +0200 From: Gur Stavi To: Gur Stavi CC: , , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Willem de Bruijn , Subject: [PATCH net-next v03 3/3] selftests: net/psock_fanout: unbound socket fanout Date: Thu, 10 Oct 2024 13:25:14 +0300 Message-ID: <1ad413755ce7b113d70063adfee9caf7cafcc7b9.1728555449.git.gur.stavi@huawei.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To frapeml500005.china.huawei.com (7.182.85.13) Content-Type: text/plain; charset="utf-8" Add a test that validates that an unbound packet socket cannot create/join a fanout group. Signed-off-by: Gur Stavi --- tools/testing/selftests/net/psock_fanout.c | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tools/testing/selftests/net/psock_fanout.c b/tools/testing/sel= ftests/net/psock_fanout.c index acdfae8f8a9a..84c524357075 100644 --- a/tools/testing/selftests/net/psock_fanout.c +++ b/tools/testing/selftests/net/psock_fanout.c @@ -279,6 +279,41 @@ static int sock_fanout_read(int fds[], char *rings[], = const int expect[]) return 0; } =20 +/* Test that creating/joining a fanout group fails for unbound socket with= out + * a specified protocol + */ +static void test_unbound_fanout(void) +{ + int val, fd0, fd1, err; + + fprintf(stderr, "test: unbound fanout\n"); + fd0 =3D socket(PF_PACKET, SOCK_RAW, 0); + if (fd0 < 0) { + perror("socket packet"); + exit(1); + } + /* Try to create a new fanout group. Should fail. */ + val =3D (PACKET_FANOUT_HASH << 16) | 1; + err =3D setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val)); + if (!err) { + fprintf(stderr, "ERROR: unbound socket fanout create\n"); + exit(1); + } + fd1 =3D sock_fanout_open(PACKET_FANOUT_HASH, 1); + if (fd1 =3D=3D -1) { + fprintf(stderr, "ERROR: failed to open HASH socket\n"); + exit(1); + } + /* Try to join an existing fanout group. Should fail. */ + err =3D setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val)); + if (!err) { + fprintf(stderr, "ERROR: unbound socket fanout join\n"); + exit(1); + } + close(fd0); + close(fd1); +} + /* Test illegal mode + flag combination */ static void test_control_single(void) { @@ -523,6 +558,7 @@ int main(int argc, char **argv) const int expect_uniqueid[2][2] =3D { { 20, 20}, { 20, 20 } }; int port_off =3D 2, tries =3D 20, ret; =20 + test_unbound_fanout(); test_control_single(); test_control_group(0); test_control_group(1); --=20 2.45.2