* [Qemu-devel] [PATCH v4] slirp: fix ICMP handling on macOS hosts
@ 2018-08-16 0:18 andrew
2018-08-16 0:22 ` Samuel Thibault
0 siblings, 1 reply; 2+ messages in thread
From: andrew @ 2018-08-16 0:18 UTC (permalink / raw)
To: peter.maydell, samuel.thibault, jan.kiszka, qemu-devel; +Cc: Andrew Oates
From: Andrew Oates <aoates@google.com>
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 <aoates@google.com>
---
v2: check validity of inner_hlen and update len appropriately
v3: CONFIG_DARWIN -> CONFIG_BSD; add comment explaining #ifdef
v4: drop packets that are too short for an IP header
slirp/ip_icmp.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c
index 0b667a429a..da100d1f55 100644
--- a/slirp/ip_icmp.c
+++ b/slirp/ip_icmp.c
@@ -420,7 +420,32 @@ void icmp_receive(struct socket *so)
icp = mtod(m, struct icmp *);
id = icp->icmp_id;
- len = qemu_recv(so->s, icp, m->m_len, 0);
+ len = qemu_recv(so->s, icp, M_ROOM(m), 0);
+ /*
+ * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
+ * 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 >= sizeof(struct ip)) {
+ struct ip *inner_ip = mtod(m, struct ip *);
+ int inner_hlen = inner_ip->ip_hl << 2;
+ if (inner_hlen > len) {
+ len = -1;
+ errno = -EINVAL;
+ } else {
+ len -= inner_hlen;
+ memmove(icp, (unsigned char *)icp + inner_hlen, len);
+ }
+ } else {
+ len = -1;
+ errno = -EINVAL;
+ }
+#endif
icp->icmp_id = id;
m->m_data -= hlen;
--
2.17.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH v4] slirp: fix ICMP handling on macOS hosts
2018-08-16 0:18 [Qemu-devel] [PATCH v4] slirp: fix ICMP handling on macOS hosts andrew
@ 2018-08-16 0:22 ` Samuel Thibault
0 siblings, 0 replies; 2+ messages in thread
From: Samuel Thibault @ 2018-08-16 0:22 UTC (permalink / raw)
To: andrew; +Cc: peter.maydell, jan.kiszka, qemu-devel, Andrew Oates
andrew@andrewoates.com, le mer. 15 août 2018 20:18:45 -0400, a ecrit:
> v4: drop packets that are too short for an IP header
Applied to my tree, thanks!
Samuel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-08-16 0:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-16 0:18 [Qemu-devel] [PATCH v4] slirp: fix ICMP handling on macOS hosts andrew
2018-08-16 0:22 ` Samuel Thibault
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).