aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Oates <aoates@google.com>2018-08-15 20:18:45 -0400
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2018-10-07 19:40:30 +0200
commitbaa2090e66b0b717951f365126258de0793d8b46 (patch)
treee0bdf822f9343020d65b5ca5dbf7248d531c9d5e
parentbc092a72f209ee44326b633c938e2f601b2687bb (diff)
downloadslirp-baa2090e66b0b717951f365126258de0793d8b46.zip
slirp-baa2090e66b0b717951f365126258de0793d8b46.tar.gz
slirp-baa2090e66b0b717951f365126258de0793d8b46.tar.bz2
slirp: fix ICMP handling on macOS hosts
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> Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
-rw-r--r--ip_icmp.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/ip_icmp.c b/ip_icmp.c
index 0dd2011..a032cb4 100644
--- a/ip_icmp.c
+++ b/ip_icmp.c
@@ -430,7 +430,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;