Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net if_ether: Ensure that ether_header is aligned



details:   https://anonhg.NetBSD.org/src/rev/6386fc8dfaa2
branches:  trunk
changeset: 959406:6386fc8dfaa2
user:      roy <roy%NetBSD.org@localhost>
date:      Sat Feb 13 07:28:04 2021 +0000

description:
if_ether: Ensure that ether_header is aligned

diffstat:

 sys/net/if_ether.h     |   8 ++++++--
 sys/net/if_ethersubr.c |  19 ++++++++++++++-----
 2 files changed, 20 insertions(+), 7 deletions(-)

diffs (62 lines):

diff -r c363f100a970 -r 6386fc8dfaa2 sys/net/if_ether.h
--- a/sys/net/if_ether.h        Sat Feb 13 07:08:45 2021 +0000
+++ b/sys/net/if_ether.h        Sat Feb 13 07:28:04 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ether.h,v 1.84 2021/02/03 18:13:13 roy Exp $        */
+/*     $NetBSD: if_ether.h,v 1.85 2021/02/13 07:28:04 roy Exp $        */
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -89,7 +89,11 @@
        uint8_t  ether_shost[ETHER_ADDR_LEN];
        uint16_t ether_type;
 };
-
+#ifdef __NO_STRICT_ALIGNMENT
+#define        ETHER_HDR_ALIGNED_P(eh) 1
+#else
+#define        ETHER_HDR_ALIGNED_P(eh) ((((vaddr_t) (eh)) & 3) == 0)
+#endif
 #ifdef __CTASSERT
 __CTASSERT(sizeof(struct ether_addr) == 6);
 __CTASSERT(sizeof(struct ether_header) == 14);
diff -r c363f100a970 -r 6386fc8dfaa2 sys/net/if_ethersubr.c
--- a/sys/net/if_ethersubr.c    Sat Feb 13 07:08:45 2021 +0000
+++ b/sys/net/if_ethersubr.c    Sat Feb 13 07:28:04 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ethersubr.c,v 1.289 2020/09/26 18:38:09 roy Exp $   */
+/*     $NetBSD: if_ethersubr.c,v 1.290 2021/02/13 07:28:04 roy Exp $   */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.289 2020/09/26 18:38:09 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.290 2021/02/13 07:28:04 roy Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -648,9 +648,18 @@
 
        if ((ifp->if_flags & IFF_UP) == 0)
                goto drop;
-       if (m->m_len < sizeof(*eh)) {
-               m = m_pullup(m, sizeof(*eh));
-               if (m == NULL)
+
+       /* If the Ethernet 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 Ethernet header is in the first mbuf of the chain.
+        */
+       if (ETHER_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
+               if ((m = m_copyup(m, sizeof(*eh),
+                   (max_linkhdr + 3) & ~3)) == NULL)
+                       goto dropped;
+       } else if (__predict_false(m->m_len < sizeof(*eh))) {
+               if ((m = m_pullup(m, sizeof(*eh))) == NULL)
                        goto dropped;
        }
 



Home | Main Index | Thread Index | Old Index