From nobody Sat May 11 18:12:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=ispras.ru Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1651452558789685.8941044866061; Sun, 1 May 2022 17:49:18 -0700 (PDT) Received: from localhost ([::1]:49396 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nlKFY-00066I-Tz for importer@patchew.org; Sun, 01 May 2022 20:49:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50028) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nlKEs-0005Ql-SW for qemu-devel@nongnu.org; Sun, 01 May 2022 20:48:34 -0400 Received: from mail.ispras.ru ([83.149.199.84]:55582) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nlKEp-0007Ks-U7 for qemu-devel@nongnu.org; Sun, 01 May 2022 20:48:33 -0400 Received: from localhost.localdomain (unknown [77.37.166.174]) by mail.ispras.ru (Postfix) with ESMTPSA id CA95240D403D; Mon, 2 May 2022 00:38:54 +0000 (UTC) From: Vitaly Cheptsov To: qemu-devel@nongnu.org Subject: [PATCH] net: fix multicast support with BSD (macOS) socket implementations Date: Mon, 2 May 2022 03:38:30 +0300 Message-Id: <20220502003830.31062-1-cheptsov@ispras.ru> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=83.149.199.84; envelope-from=cheptsov@ispras.ru; helo=mail.ispras.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , Vitaly Cheptsov , "Daniel P . Berrange" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1651452562917100001 This patch fixes socket communication with QEMU -> host on macOS, which was originally impossible due to QEMU and host program having to bind to the same ip/port in a way not supported by BSD sockets. The change was tested on both Linux and macOS. As per BSD manual pages SO_REUSEPORT allows completely duplicate bindings by multiple processes, permitting multiple instances of a program to each receive UDP/IP multicast datagrams destined for the bound port. Without this option macOS, unlike Linux, which (ab)uses SO_REUSEADDR for this purpose, will return "Address already in use" on bind(). As per BSD manual pages binding to any address, even one not bound to any available network interface in the system, should be IP_BINDANY. Without binding to INADDR_ANY macOS will return "Can't assign requested address" on send(). Cc: Jason Wang Cc: Daniel P. Berrange Cc: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Vitaly Cheptsov --- net/socket.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/net/socket.c b/net/socket.c index ea5220a2eb..8b2c6c4bb8 100644 --- a/net/socket.c +++ b/net/socket.c @@ -252,10 +252,24 @@ static int net_socket_mcast_create(struct sockaddr_in= *mcastaddr, goto fail; } =20 - ret =3D bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); + val =3D 1; + ret =3D qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val= )); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't set socket option SO_REUSEPORT"); + goto fail; + } + + struct sockaddr_in bindaddr; + memset(&bindaddr, 0, sizeof(bindaddr)); + bindaddr.sin_family =3D AF_INET; + bindaddr.sin_addr.s_addr =3D htonl(INADDR_ANY); + bindaddr.sin_port =3D mcastaddr->sin_port; + ret =3D bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)); + if (ret < 0) { error_setg_errno(errp, errno, "can't bind ip=3D%s to socket", - inet_ntoa(mcastaddr->sin_addr)); + inet_ntoa(bindaddr.sin_addr)); goto fail; } =20 --=20 2.32.0 (Apple Git-132)