From nobody Wed Nov 5 10:51:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534300598570261.3713512697451; Tue, 14 Aug 2018 19:36:38 -0700 (PDT) Received: from localhost ([::1]:46943 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fplff-000756-Vt for importer@patchew.org; Tue, 14 Aug 2018 22:36:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53024) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from <3bJFzWwYKCj8bpbufthpphmf.dpnrfnv-efwfmopohov.psh@flex--aoates.bounces.google.com>) id 1fpleg-0006lS-Vk for qemu-devel@nongnu.org; Tue, 14 Aug 2018 22:35:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from <3bJFzWwYKCj8bpbufthpphmf.dpnrfnv-efwfmopohov.psh@flex--aoates.bounces.google.com>) id 1fpleg-0003ed-1A for qemu-devel@nongnu.org; Tue, 14 Aug 2018 22:35:26 -0400 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]:48999) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from <3bJFzWwYKCj8bpbufthpphmf.dpnrfnv-efwfmopohov.psh@flex--aoates.bounces.google.com>) id 1fplef-0003eK-GK for qemu-devel@nongnu.org; Tue, 14 Aug 2018 22:35:25 -0400 Received: by mail-ua1-x949.google.com with SMTP id k17-v6so11641779uao.15 for ; Tue, 14 Aug 2018 19:35:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=1HOtNO2VzWnRByrJtYAwIpqUMk50TDE8+lZQKtXSoac=; b=Kvds+jsjjpLcIZyLewecYiQ/viMqNyrSJsSJpDC/QhGbXHF7QYUtjHXsb6sdxwO6gr 5LYKzePLpqWNWpG7hOFqld2JpS4yNJ43rCPulQIgOeMOWHFV9nx5ywF91e9J9YWPRbkb U59RfGC1s5lRdLXe1+ehGiU00VyRdF+ZU8BYWa0oqdPN0xjzvNpkz/+ZXD27qib1opAS nPZRnh2BCW1DDQ4p9JdV409IM2KASdjDtuQoAJR/v8mtQq8268lzX5KyKbsrF6q5wePU I+p6arO4yU4dLP7vO1U+jVsdj55defXGsA+6wpL7N+ZyeXLJ6Stu4FFgHOQHQpZML9qs Ljmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=1HOtNO2VzWnRByrJtYAwIpqUMk50TDE8+lZQKtXSoac=; b=evigNTYlCLjCGIkFbYXRYgklGlyDBi+8PJv4YNQXujOO414P/DhLFuvREGJsB/Ph1N Xxo/hTgXyW5bW9CpkjhWX89UnIrUT++v9WhYMLsgOV0pkCjz+hRPQonMC8og2FgTJ5Af MjKWE9RSTrV96gZ/balSyDVd+Os8fYM/jEEXmrGQMZT6vhretGwi+k1dLuHvzrK8QFhp +6S/U34Xt5/1uki66eCKvdtesgk+UuXQMiXrqCovFLgxTO+XI/AgdXseG3wVyvZ6fOCk oO4Z48ZtdpVSfvmGLANZvS/GeKQ8W4jj9IJF52u8eRXPPp3Enrp4FTd2YR29SihOQ/k2 7f+w== X-Gm-Message-State: AOUpUlGu8DjkWhfOwDfi4ba98NhJ6hdGVxFrj9L35DJK1HVNjTH5UwJC VJQpYa1MX2QmLZ4qh9isSo4CZEFot/A= X-Google-Smtp-Source: AA+uWPwZSh/RKD835jztLoSQxjcTHesuaikf6ELyEqbso7ZGimAAE0mnOXgxZ0RqCEVA26mUP0pJYRJY6jk= X-Received: by 2002:a1f:2413:: with SMTP id k19-v6mr13869153vkk.4.1534300524416; Tue, 14 Aug 2018 19:35:24 -0700 (PDT) Date: Tue, 14 Aug 2018 22:35:21 -0400 Message-Id: <20180815023521.185705-1-aoates@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.18.0.865.gffc8e1a3cd6-goog To: peter.maydell@linaro.org, samuel.thibault@ens-lyon.org, jan.kiszka@siemens.com, qemu-devel@nongnu.org X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::949 Subject: [Qemu-devel] [PATCH v3] slirp: fix ICMP handling on macOS hosts X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andrew Oates via Qemu-devel Reply-To: Andrew Oates Cc: Andrew Oates Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On Linux, SOCK_DGRAM+IPPROTO_ICMP sockets give only the ICMP packet when read from. On macOS, however, the socket acts like a SOCK_RAW socket and includes the IP header as well. This change strips the extra IP header from the received packet on macOS before sending it to the guest. SOCK_DGRAM ICMP sockets aren't supported on other BSDs, but we enable this behavior for them as well to treat the sockets the same as raw sockets. Signed-off-by: Andrew Oates Reviewed-by: Peter Maydell --- v2: check validity of inner_hlen and update len appropriately v3: CONFIG_DARWIN -> CONFIG_BSD; add comment explaining #ifdef slirp/ip_icmp.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c index 0b667a429a..0e289fd9d9 100644 --- a/slirp/ip_icmp.c +++ b/slirp/ip_icmp.c @@ -420,7 +420,29 @@ void icmp_receive(struct socket *so) icp =3D mtod(m, struct icmp *); =20 id =3D icp->icmp_id; - len =3D qemu_recv(so->s, icp, m->m_len, 0); + len =3D qemu_recv(so->s, icp, M_ROOM(m), 0); + /* + * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsis= tent + * between host OSes. On Linux, only the ICMP header and payload is + * included. On macOS/Darwin, the socket acts like a raw socket and + * includes the IP header as well. On other BSDs, SOCK_DGRAM+IPPROTO_= ICMP + * sockets aren't supported at all, so we treat them like raw sockets.= It + * isn't possible to detect this difference at runtime, so we must use= an + * #ifdef to determine if we need to remove the IP header. + */ +#ifdef CONFIG_BSD + if (len > 0) { + struct ip *inner_ip =3D mtod(m, struct ip *); + int inner_hlen =3D inner_ip->ip_hl << 2; + if (inner_hlen > len) { + len =3D -1; + errno =3D -EINVAL; + } else { + len -=3D inner_hlen; + memmove(icp, (unsigned char *)icp + inner_hlen, len); + } + } +#endif icp->icmp_id =3D id; =20 m->m_data -=3D hlen; --=20 2.18.0.865.gffc8e1a3cd6-goog