Module Name:    src
Committed By:   roy
Date:           Sun Feb 14 19:47:17 UTC 2021

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

Log Message:
if_arp: Just KASSERT that arphrd is aligned

While here improve readability of checking ARP IEEE1394 matches interface.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/sys/net/if_arp.h
cvs rdiff -u -r1.300 -r1.301 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.38 src/sys/net/if_arp.h:1.39
--- src/sys/net/if_arp.h:1.38	Sat Feb 13 07:57:09 2021
+++ src/sys/net/if_arp.h	Sun Feb 14 19:47:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.h,v 1.38 2021/02/13 07:57:09 roy Exp $	*/
+/*	$NetBSD: if_arp.h,v 1.39 2021/02/14 19:47:16 roy Exp $	*/
 
 /*
  * Copyright (c) 1986, 1993
@@ -72,14 +72,6 @@ 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
 
 static __inline uint8_t *
 ar_data(struct arphdr *ap)

Index: src/sys/netinet/if_arp.c
diff -u src/sys/netinet/if_arp.c:1.300 src/sys/netinet/if_arp.c:1.301
--- src/sys/netinet/if_arp.c:1.300	Sat Feb 13 13:00:16 2021
+++ src/sys/netinet/if_arp.c	Sun Feb 14 19:47:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.c,v 1.300 2021/02/13 13:00:16 roy Exp $	*/
+/*	$NetBSD: if_arp.c,v 1.301 2021/02/14 19:47:17 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.300 2021/02/13 13:00:16 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.301 2021/02/14 19:47:17 roy Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -133,6 +133,12 @@ __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1
  */
 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
 
+#ifdef __NO_STRICT_ALIGNMENT
+#define	ARP_HDR_ALIGNED_P(ar)	1
+#else
+#define	ARP_HDR_ALIGNED_P(ar)	((((vaddr_t) (ar)) & 1) == 0)
+#endif
+
 /* timers */
 static int arp_reachable = REACHABLE_TIME;
 static int arp_retrans = RETRANS_TIMER;
@@ -684,10 +690,11 @@ arpintr(void)
 	struct arphdr *ar;
 	int s;
 	int arplen;
+	struct ifnet *rcvif;
+	bool badhrd;
 
 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
 	for (;;) {
-		struct ifnet *rcvif;
 
 		IFQ_LOCK(&arpintrq);
 		IF_DEQUEUE(&arpintrq, m);
@@ -700,15 +707,12 @@ arpintr(void)
 		MCLAIM(m, &arpdomain.dom_mowner);
 		ARP_STATINC(ARP_STAT_RCVTOTAL);
 
-		/* Enforce alignment */
-		if (ARP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
-			if ((m = m_copyup(m, sizeof(*ar), 0)) == NULL)
-				goto badlen;
-		} else if (__predict_false(m->m_len < sizeof(*ar))) {
+		if (__predict_false(m->m_len < sizeof(*ar))) {
 			if ((m = m_pullup(m, sizeof(*ar))) == NULL)
 				goto badlen;
 		}
 		ar = mtod(m, struct arphdr *);
+		KASSERT(ARP_HDR_ALIGNED_P(ar));
 
 		rcvif = m_get_rcvif(m, &s);
 		if (__predict_false(rcvif == NULL)) {
@@ -720,27 +724,20 @@ arpintr(void)
 		 * We don't want non-IEEE1394 ARP packets on IEEE1394
 		 * interfaces, and vice versa. Our life depends on that.
 		 */
-		switch (rcvif->if_type) {
-		case IFT_IEEE1394:
-			if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
-				m_put_rcvif(rcvif, &s);
-				ARP_STATINC(ARP_STAT_RCVBADPROTO);
-				goto free;
-			}
-			break;
-		default:
-			if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
-				m_put_rcvif(rcvif, &s);
-				ARP_STATINC(ARP_STAT_RCVBADPROTO);
-				goto free;
-			}
-			break;
-		}
+		if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394)
+			badhrd = rcvif->if_type != IFT_IEEE1394;
+		else
+			badhrd = rcvif->if_type == IFT_IEEE1394;
 
 		m_put_rcvif(rcvif, &s);
 
+		if (badhrd) {
+			ARP_STATINC(ARP_STAT_RCVBADPROTO);
+			goto free;
+		}
+
 		arplen = sizeof(*ar) + 2 * ar->ar_hln + 2 * ar->ar_pln;
-		if (m->m_len < arplen) {
+		if (__predict_false(m->m_len < arplen)) {
 			if ((m = m_pullup(m, arplen)) == NULL)
 				goto badlen;
 			ar = mtod(m, struct arphdr *);

Reply via email to