Module Name:    src
Committed By:   roy
Date:           Sat Feb 13 07:57:09 UTC 2021

Modified Files:
        src/sys/net: if_arp.h
        src/sys/netinet: if_arp.c

Log Message:
if_arp: Ensure that arphdr is aligned


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/sys/net/if_arp.h
cvs rdiff -u -r1.298 -r1.299 src/sys/netinet/if_arp.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if_arp.h
diff -u src/sys/net/if_arp.h:1.37 src/sys/net/if_arp.h:1.38
--- src/sys/net/if_arp.h:1.37	Wed Feb  3 18:13:13 2021
+++ src/sys/net/if_arp.h	Sat Feb 13 07:57:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.h,v 1.37 2021/02/03 18:13:13 roy Exp $	*/
+/*	$NetBSD: if_arp.h,v 1.38 2021/02/13 07:57:09 roy Exp $	*/
 
 /*
  * Copyright (c) 1986, 1993
@@ -72,6 +72,11 @@ struct	arphdr {
 	uint8_t  ar_tpa[];	/* target protocol address */
 #endif
 };
+#ifdef __NO_STRICT_ALIGNMENT
+#define	ARP_HDR_ALIGNED_P(ah)	1
+#else
+#define	ARP_HDR_ALIGNED_P(ah)	((((vaddr_t) (ah)) & 3) == 0)
+#endif
 #ifdef __CTASSERT
 __CTASSERT(sizeof(struct arphdr) == 8);
 #endif

Index: src/sys/netinet/if_arp.c
diff -u src/sys/netinet/if_arp.c:1.298 src/sys/netinet/if_arp.c:1.299
--- src/sys/netinet/if_arp.c:1.298	Tue Feb  2 10:48:33 2021
+++ src/sys/netinet/if_arp.c	Sat Feb 13 07:57:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.c,v 1.298 2021/02/02 10:48:33 yamt Exp $	*/
+/*	$NetBSD: if_arp.c,v 1.299 2021/02/13 07:57:09 roy Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.298 2021/02/02 10:48:33 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.299 2021/02/13 07:57:09 roy Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -700,9 +700,19 @@ arpintr(void)
 		MCLAIM(m, &arpdomain.dom_mowner);
 		ARP_STATINC(ARP_STAT_RCVTOTAL);
 
-		arplen = sizeof(struct arphdr);
-		if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
-			goto badlen;
+		/* If the ARP header is not aligned, slurp it up into a new
+		 * mbuf with space for link headers, in the event we forward
+		 * it.  Otherwise, if it is aligned, make sure the entire
+		 * base ARP header is in the first mbuf of the chain.
+		 */
+		if (ARP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
+			if ((m = m_copyup(m, sizeof(*ar),
+			    (max_linkhdr + 3) & ~3)) == NULL)
+				goto badlen;
+		} else if (__predict_false(m->m_len < sizeof(*ar))) {
+			if ((m = m_pullup(m, sizeof(*ar))) == NULL)
+				goto badlen;
+		}
 		ar = mtod(m, struct arphdr *);
 
 		rcvif = m_get_rcvif(m, &s);

Reply via email to