Module Name: src Committed By: christos Date: Sun Feb 14 20:58:35 UTC 2021
Modified Files: src/sys/net: if_arp.h if_bridge.c src/sys/netinet: icmp_private.h if_arp.c igmp_var.h in_l2tp.c ip_flow.c ip_input.c ip_private.h tcp_input.c tcp_private.h udp_private.h udp_usrreq.c src/sys/netinet6: icmp6.c in6_l2tp.c ip6_flow.c ip6_input.c ip6_private.h udp6_usrreq.c src/sys/sys: mbuf.h param.h Log Message: - centralize header align and pullup into a single inline function - use a single macro to align pointers and expose the alignment, instead of hard-coding 3 in 1/2 the macros. - fix an issue in the ipv6 lt2p where it was aligning for ipv4 and pulling for ipv6. To generate a diff of this commit: cvs rdiff -u -r1.39 -r1.40 src/sys/net/if_arp.h cvs rdiff -u -r1.177 -r1.178 src/sys/net/if_bridge.c cvs rdiff -u -r1.3 -r1.4 src/sys/netinet/icmp_private.h \ src/sys/netinet/ip_private.h src/sys/netinet/tcp_private.h \ src/sys/netinet/udp_private.h cvs rdiff -u -r1.301 -r1.302 src/sys/netinet/if_arp.c cvs rdiff -u -r1.25 -r1.26 src/sys/netinet/igmp_var.h cvs rdiff -u -r1.18 -r1.19 src/sys/netinet/in_l2tp.c cvs rdiff -u -r1.82 -r1.83 src/sys/netinet/ip_flow.c cvs rdiff -u -r1.397 -r1.398 src/sys/netinet/ip_input.c cvs rdiff -u -r1.424 -r1.425 src/sys/netinet/tcp_input.c cvs rdiff -u -r1.259 -r1.260 src/sys/netinet/udp_usrreq.c cvs rdiff -u -r1.247 -r1.248 src/sys/netinet6/icmp6.c cvs rdiff -u -r1.19 -r1.20 src/sys/netinet6/in6_l2tp.c cvs rdiff -u -r1.40 -r1.41 src/sys/netinet6/ip6_flow.c cvs rdiff -u -r1.222 -r1.223 src/sys/netinet6/ip6_input.c cvs rdiff -u -r1.3 -r1.4 src/sys/netinet6/ip6_private.h cvs rdiff -u -r1.148 -r1.149 src/sys/netinet6/udp6_usrreq.c cvs rdiff -u -r1.227 -r1.228 src/sys/sys/mbuf.h cvs rdiff -u -r1.684 -r1.685 src/sys/sys/param.h 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.39 src/sys/net/if_arp.h:1.40 --- src/sys/net/if_arp.h:1.39 Sun Feb 14 14:47:16 2021 +++ src/sys/net/if_arp.h Sun Feb 14 15:58:34 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: if_arp.h,v 1.39 2021/02/14 19:47:16 roy Exp $ */ +/* $NetBSD: if_arp.h,v 1.40 2021/02/14 20:58:34 christos Exp $ */ /* * Copyright (c) 1986, 1993 @@ -72,6 +72,7 @@ struct arphdr { uint8_t ar_tpa[]; /* target protocol address */ #endif }; +#define ARP_HDR_ALIGNMENT 3 static __inline uint8_t * ar_data(struct arphdr *ap) Index: src/sys/net/if_bridge.c diff -u src/sys/net/if_bridge.c:1.177 src/sys/net/if_bridge.c:1.178 --- src/sys/net/if_bridge.c:1.177 Mon Nov 2 07:14:59 2020 +++ src/sys/net/if_bridge.c Sun Feb 14 15:58:34 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: if_bridge.c,v 1.177 2020/11/02 12:14:59 roy Exp $ */ +/* $NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $ */ /* * Copyright 2001 Wasabi Systems, Inc. @@ -80,7 +80,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.177 2020/11/02 12:14:59 roy Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -2806,18 +2806,10 @@ bridge_ip_checkbasic(struct mbuf **mp) if (*mp == NULL) return -1; - if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { - if ((m = m_copyup(m, sizeof(struct ip), - (max_linkhdr + 3) & ~3)) == NULL) { - /* XXXJRT new stat, please */ - ip_statinc(IP_STAT_TOOSMALL); - goto bad; - } - } else if (__predict_false(m->m_len < sizeof (struct ip))) { - if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { - ip_statinc(IP_STAT_TOOSMALL); - goto bad; - } + if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) { + /* XXXJRT new stat, please */ + ip_statinc(IP_STAT_TOOSMALL); + goto bad; } ip = mtod(m, struct ip *); if (ip == NULL) goto bad; @@ -2908,22 +2900,12 @@ bridge_ip6_checkbasic(struct mbuf **mp) * it. Otherwise, if it is aligned, make sure the entire base * IPv6 header is in the first mbuf of the chain. */ - if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) { + if (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(*ip6), true) != 0) { struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m); - if ((m = m_copyup(m, sizeof(struct ip6_hdr), - (max_linkhdr + 3) & ~3)) == NULL) { - /* XXXJRT new stat, please */ - ip6_statinc(IP6_STAT_TOOSMALL); - in6_ifstat_inc(inifp, ifs6_in_hdrerr); - goto bad; - } - } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { - struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m); - if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { - ip6_statinc(IP6_STAT_TOOSMALL); - in6_ifstat_inc(inifp, ifs6_in_hdrerr); - goto bad; - } + /* XXXJRT new stat, please */ + ip6_statinc(IP6_STAT_TOOSMALL); + in6_ifstat_inc(inifp, ifs6_in_hdrerr); + goto bad; } ip6 = mtod(m, struct ip6_hdr *); Index: src/sys/netinet/icmp_private.h diff -u src/sys/netinet/icmp_private.h:1.3 src/sys/netinet/icmp_private.h:1.4 --- src/sys/netinet/icmp_private.h:1.3 Mon Apr 28 16:24:09 2008 +++ src/sys/netinet/icmp_private.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: icmp_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $ */ +/* $NetBSD: icmp_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -44,11 +44,7 @@ extern percpu_t *icmpstat_percpu; #define ICMP_STATINC(x) _NET_STATINC(icmpstat_percpu, x) -#ifdef __NO_STRICT_ALIGNMENT -#define ICMP_HDR_ALIGNED_P(ic) 1 -#else -#define ICMP_HDR_ALIGNED_P(ic) ((((vaddr_t) (ic)) & 3) == 0) -#endif +#define ICMP_HDR_ALIGNMENT 3 #endif /* _KERNEL_ */ #endif /* !_NETINET_ICMP_PRIVATE_H_ */ Index: src/sys/netinet/ip_private.h diff -u src/sys/netinet/ip_private.h:1.3 src/sys/netinet/ip_private.h:1.4 --- src/sys/netinet/ip_private.h:1.3 Mon Apr 28 16:24:09 2008 +++ src/sys/netinet/ip_private.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $ */ +/* $NetBSD: ip_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -43,11 +43,7 @@ extern percpu_t *ipstat_percpu; #define IP_STATINC(x) _NET_STATINC(ipstat_percpu, x) #define IP_STATDEC(x) _NET_STATDEC(ipstat_percpu, x) -#ifdef __NO_STRICT_ALIGNMENT -#define IP_HDR_ALIGNED_P(ip) 1 -#else -#define IP_HDR_ALIGNED_P(ip) ((((vaddr_t) (ip)) & 3) == 0) -#endif +#define IP_HDR_ALIGNMENT 3 #endif /* _KERNEL */ #endif /* !_NETINET_IP_PRIVATE_H_ */ Index: src/sys/netinet/tcp_private.h diff -u src/sys/netinet/tcp_private.h:1.3 src/sys/netinet/tcp_private.h:1.4 --- src/sys/netinet/tcp_private.h:1.3 Mon Apr 28 16:24:09 2008 +++ src/sys/netinet/tcp_private.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: tcp_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $ */ +/* $NetBSD: tcp_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -43,11 +43,7 @@ extern percpu_t *tcpstat_percpu; #define TCP_STATINC(x) _NET_STATINC(tcpstat_percpu, x) #define TCP_STATADD(x, v) _NET_STATADD(tcpstat_percpu, x, v) -#ifdef __NO_STRICT_ALIGNMENT -#define TCP_HDR_ALIGNED_P(th) 1 -#else -#define TCP_HDR_ALIGNED_P(th) ((((vaddr_t)(th)) & 3) == 0) -#endif /* __NO_STRICT_ALIGNMENT */ +#define TCP_HDR_ALIGNMENT 3 #endif /* _KERNEL */ #endif /* !_NETINET_TCP_PRIVATE_H_ */ Index: src/sys/netinet/udp_private.h diff -u src/sys/netinet/udp_private.h:1.3 src/sys/netinet/udp_private.h:1.4 --- src/sys/netinet/udp_private.h:1.3 Mon Apr 28 16:24:09 2008 +++ src/sys/netinet/udp_private.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: udp_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $ */ +/* $NetBSD: udp_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -39,11 +39,7 @@ extern percpu_t *udpstat_percpu; #define UDP_STATINC(x) _NET_STATINC(udpstat_percpu, x) -#ifdef __NO_STRICT_ALIGNMENT -#define UDP_HDR_ALIGNED_P(uh) 1 -#else -#define UDP_HDR_ALIGNED_P(uh) ((((vaddr_t) (uh)) & 3) == 0) -#endif +#define UDP_HDR_ALIGNMENT 3 #endif /* _KERNEL */ #endif /* !_NETINET_UDP_PRIVATE_H_ */ Index: src/sys/netinet/if_arp.c diff -u src/sys/netinet/if_arp.c:1.301 src/sys/netinet/if_arp.c:1.302 --- src/sys/netinet/if_arp.c:1.301 Sun Feb 14 14:47:17 2021 +++ src/sys/netinet/if_arp.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: if_arp.c,v 1.301 2021/02/14 19:47:17 roy Exp $ */ +/* $NetBSD: if_arp.c,v 1.302 2021/02/14 20:58:35 christos 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.301 2021/02/14 19:47:17 roy Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.302 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_ddb.h" @@ -712,7 +712,7 @@ arpintr(void) goto badlen; } ar = mtod(m, struct arphdr *); - KASSERT(ARP_HDR_ALIGNED_P(ar)); + KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT)); rcvif = m_get_rcvif(m, &s); if (__predict_false(rcvif == NULL)) { @@ -741,7 +741,7 @@ arpintr(void) if ((m = m_pullup(m, arplen)) == NULL) goto badlen; ar = mtod(m, struct arphdr *); - KASSERT(ARP_HDR_ALIGNED_P(ar)); + KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT)); } switch (ntohs(ar->ar_pro)) { Index: src/sys/netinet/igmp_var.h diff -u src/sys/netinet/igmp_var.h:1.25 src/sys/netinet/igmp_var.h:1.26 --- src/sys/netinet/igmp_var.h:1.25 Fri Sep 14 01:09:51 2018 +++ src/sys/netinet/igmp_var.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: igmp_var.h,v 1.25 2018/09/14 05:09:51 maxv Exp $ */ +/* $NetBSD: igmp_var.h,v 1.26 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (c) 1992, 1993 @@ -105,11 +105,7 @@ */ #define IGMP_RANDOM_DELAY(X) (cprng_fast32() % (X) + 1) -#ifdef __NO_STRICT_ALIGNMENT -#define IGMP_HDR_ALIGNED_P(ig) 1 -#else -#define IGMP_HDR_ALIGNED_P(ig) ((((vaddr_t) (ig)) & 3) == 0) -#endif +#define IGMP_HDR_ALIGNMENT 3 void igmp_init(void); void igmp_input(struct mbuf *, int, int); Index: src/sys/netinet/in_l2tp.c diff -u src/sys/netinet/in_l2tp.c:1.18 src/sys/netinet/in_l2tp.c:1.19 --- src/sys/netinet/in_l2tp.c:1.18 Tue Jan 28 23:37:24 2020 +++ src/sys/netinet/in_l2tp.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: in_l2tp.c,v 1.18 2020/01/29 04:37:24 thorpej Exp $ */ +/* $NetBSD: in_l2tp.c,v 1.19 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (c) 2017 Internet Initiative Japan Inc. @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.18 2020/01/29 04:37:24 thorpej Exp $"); +__KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.19 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_l2tp.h" @@ -197,13 +197,8 @@ in_l2tp_output(struct l2tp_variant *var, error = ENOBUFS; goto out; } - if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { - m = m_copyup(m, sizeof(struct ip), 0); - } else { - if (m->m_len < sizeof(struct ip)) - m = m_pullup(m, sizeof(struct ip)); - } - if (m == NULL) { + if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(iphdr), false) != 0) + { error = ENOBUFS; goto out; } Index: src/sys/netinet/ip_flow.c diff -u src/sys/netinet/ip_flow.c:1.82 src/sys/netinet/ip_flow.c:1.83 --- src/sys/netinet/ip_flow.c:1.82 Wed Apr 11 04:29:19 2018 +++ src/sys/netinet/ip_flow.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip_flow.c,v 1.82 2018/04/11 08:29:19 maxv Exp $ */ +/* $NetBSD: ip_flow.c,v 1.83 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 1998 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.82 2018/04/11 08:29:19 maxv Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.83 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_net_mpsafe.h" @@ -230,9 +230,8 @@ ipflow_fastforward(struct mbuf *m) /* * IP header with no option and valid version and length */ - if (IP_HDR_ALIGNED_P(mtod(m, const void *))) - ip = mtod(m, struct ip *); - else { + ip = mtod(m, struct ip *); + if (!POINTER_ALIGNED_P(ip, IP_HDR_ALIGNMENT) { memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store)); ip = &ip_store; } @@ -314,7 +313,7 @@ ipflow_fastforward(struct mbuf *m) * * XXX Use m_copyback_cow(9) here? --dyoung */ - if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) + if (!POINTER_ALIGNED_P(mtod(m, void *), IP_HDR_ALIGNMENT)) memcpy(mtod(m, void *), &ip_store, sizeof(ip_store)); /* Index: src/sys/netinet/ip_input.c diff -u src/sys/netinet/ip_input.c:1.397 src/sys/netinet/ip_input.c:1.398 --- src/sys/netinet/ip_input.c:1.397 Fri Aug 28 02:31:42 2020 +++ src/sys/netinet/ip_input.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip_input.c,v 1.397 2020/08/28 06:31:42 ozaki-r Exp $ */ +/* $NetBSD: ip_input.c,v 1.398 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -91,7 +91,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.397 2020/08/28 06:31:42 ozaki-r Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.398 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -454,18 +454,10 @@ ip_input(struct mbuf *m, struct ifnet *i * it. Otherwise, if it is aligned, make sure the entire * base IP header is in the first mbuf of the chain. */ - if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { - if ((m = m_copyup(m, sizeof(struct ip), - (max_linkhdr + 3) & ~3)) == NULL) { - /* XXXJRT new stat, please */ - IP_STATINC(IP_STAT_TOOSMALL); - goto out; - } - } else if (__predict_false(m->m_len < sizeof(struct ip))) { - if ((m = m_pullup(m, sizeof(struct ip))) == NULL) { - IP_STATINC(IP_STAT_TOOSMALL); - goto out; - } + if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) { + /* XXXJRT new stat, please */ + IP_STATINC(IP_STAT_TOOSMALL); + goto out; } ip = mtod(m, struct ip *); if (ip->ip_v != IPVERSION) { Index: src/sys/netinet/tcp_input.c diff -u src/sys/netinet/tcp_input.c:1.424 src/sys/netinet/tcp_input.c:1.425 --- src/sys/netinet/tcp_input.c:1.424 Mon Sep 28 22:58:53 2020 +++ src/sys/netinet/tcp_input.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: tcp_input.c,v 1.424 2020/09/29 02:58:53 msaitoh Exp $ */ +/* $NetBSD: tcp_input.c,v 1.425 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -148,7 +148,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.424 2020/09/29 02:58:53 msaitoh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.425 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -1274,7 +1274,7 @@ tcp_input(struct mbuf *m, int off, int p * Enforce alignment requirements that are violated in * some cases, see kern/50766 for details. */ - if (TCP_HDR_ALIGNED_P(th) == 0) { + if (POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT) == 0) { m = m_copyup(m, off + sizeof(struct tcphdr), 0); if (m == NULL) { TCP_STATINC(TCP_STAT_RCVSHORT); @@ -1282,7 +1282,7 @@ tcp_input(struct mbuf *m, int off, int p } th = (struct tcphdr *)(mtod(m, char *) + off); } - KASSERT(TCP_HDR_ALIGNED_P(th)); + KASSERT(POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT)); /* * Get IP and TCP header. @@ -1362,7 +1362,7 @@ tcp_input(struct mbuf *m, int off, int p TCP_STATINC(TCP_STAT_RCVSHORT); return; } - KASSERT(TCP_HDR_ALIGNED_P(th)); + KASSERT(POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT)); optlen = thlen - sizeof(struct tcphdr); optp = ((u_int8_t *)th) + sizeof(struct tcphdr); Index: src/sys/netinet/udp_usrreq.c diff -u src/sys/netinet/udp_usrreq.c:1.259 src/sys/netinet/udp_usrreq.c:1.260 --- src/sys/netinet/udp_usrreq.c:1.259 Thu Aug 20 17:21:32 2020 +++ src/sys/netinet/udp_usrreq.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: udp_usrreq.c,v 1.259 2020/08/20 21:21:32 riastradh Exp $ */ +/* $NetBSD: udp_usrreq.c,v 1.260 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -66,7 +66,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.259 2020/08/20 21:21:32 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.260 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -335,7 +335,7 @@ udp_input(struct mbuf *m, int off, int p * Enforce alignment requirements that are violated in * some cases, see kern/50766 for details. */ - if (UDP_HDR_ALIGNED_P(uh) == 0) { + if (POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT) == 0) { m = m_copyup(m, iphlen + sizeof(struct udphdr), 0); if (m == NULL) { UDP_STATINC(UDP_STAT_HDROPS); @@ -344,7 +344,7 @@ udp_input(struct mbuf *m, int off, int p ip = mtod(m, struct ip *); uh = (struct udphdr *)(mtod(m, char *) + iphlen); } - KASSERT(UDP_HDR_ALIGNED_P(uh)); + KASSERT(POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT)); /* destination port of 0 is illegal, based on RFC768. */ if (uh->uh_dport == 0) Index: src/sys/netinet6/icmp6.c diff -u src/sys/netinet6/icmp6.c:1.247 src/sys/netinet6/icmp6.c:1.248 --- src/sys/netinet6/icmp6.c:1.247 Fri Sep 11 11:03:33 2020 +++ src/sys/netinet6/icmp6.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: icmp6.c,v 1.247 2020/09/11 15:03:33 roy Exp $ */ +/* $NetBSD: icmp6.c,v 1.248 2021/02/14 20:58:35 christos Exp $ */ /* $KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $ */ /* @@ -62,7 +62,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.247 2020/09/11 15:03:33 roy Exp $"); +__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.248 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_compat_netbsd.h" @@ -538,7 +538,7 @@ _icmp6_input(struct mbuf *m, int off, in * Enforce alignment requirements that are violated in * some cases, see kern/50766 for details. */ - if (IP6_HDR_ALIGNED_P(icmp6) == 0) { + if (POINTER_ALIGNED_P(icmp6, ICMP6_HDR_ALIGNMENT) == 0) { m = m_copyup(m, off + sizeof(struct icmp6_hdr), 0); if (m == NULL) { ICMP6_STATINC(ICMP6_STAT_TOOSHORT); @@ -548,7 +548,7 @@ _icmp6_input(struct mbuf *m, int off, in ip6 = mtod(m, struct ip6_hdr *); icmp6 = (struct icmp6_hdr *)(mtod(m, char *) + off); } - KASSERT(IP6_HDR_ALIGNED_P(icmp6)); + KASSERT(POINTER_ALIGNED_P(icmp6, ICMP6_HDR_ALIGNMENT)); /* * calculate the checksum Index: src/sys/netinet6/in6_l2tp.c diff -u src/sys/netinet6/in6_l2tp.c:1.19 src/sys/netinet6/in6_l2tp.c:1.20 --- src/sys/netinet6/in6_l2tp.c:1.19 Tue Jan 28 23:38:06 2020 +++ src/sys/netinet6/in6_l2tp.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: in6_l2tp.c,v 1.19 2020/01/29 04:38:06 thorpej Exp $ */ +/* $NetBSD: in6_l2tp.c,v 1.20 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (c) 2017 Internet Initiative Japan Inc. @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: in6_l2tp.c,v 1.19 2020/01/29 04:38:06 thorpej Exp $"); +__KERNEL_RCSID(0, "$NetBSD: in6_l2tp.c,v 1.20 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_l2tp.h" @@ -58,6 +58,7 @@ __KERNEL_RCSID(0, "$NetBSD: in6_l2tp.c,v #include <netinet/ip6.h> #include <netinet6/ip6_var.h> +#include <netinet6/ip6_private.h> #include <netinet6/in6_l2tp.h> #ifdef ALTQ @@ -192,13 +193,8 @@ in6_l2tp_output(struct l2tp_variant *var M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); if (m == NULL) return ENOBUFS; - if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { - m = m_copyup(m, sizeof(struct ip), 0); - } else { - if (m->m_len < sizeof(struct ip6_hdr)) - m = m_pullup(m, sizeof(struct ip6_hdr)); - } - if (m == NULL) + if (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(ip6hdr), + false) != 0) return ENOBUFS; memcpy(mtod(m, struct ip6_hdr *), &ip6hdr, sizeof(struct ip6_hdr)); Index: src/sys/netinet6/ip6_flow.c diff -u src/sys/netinet6/ip6_flow.c:1.40 src/sys/netinet6/ip6_flow.c:1.41 --- src/sys/netinet6/ip6_flow.c:1.40 Mon Feb 5 22:37:00 2018 +++ src/sys/netinet6/ip6_flow.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip6_flow.c,v 1.40 2018/02/06 03:37:00 ozaki-r Exp $ */ +/* $NetBSD: ip6_flow.c,v 1.41 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (c) 2007 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.40 2018/02/06 03:37:00 ozaki-r Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.41 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_net_mpsafe.h" @@ -283,7 +283,7 @@ ip6flow_fastforward(struct mbuf **mp) if ((m->m_flags & (M_BCAST|M_MCAST)) != 0) goto out; - if (IP6_HDR_ALIGNED_P(mtod(m, const void *)) == 0) { + if (POINTER_ALIGNED_P(mtod(m, const void *), IP6_HDR_ALIGNMENT) == 0) { if ((m = m_copyup(m, sizeof(struct ip6_hdr), (max_linkhdr + 3) & ~3)) == NULL) { ret = 1; Index: src/sys/netinet6/ip6_input.c diff -u src/sys/netinet6/ip6_input.c:1.222 src/sys/netinet6/ip6_input.c:1.223 --- src/sys/netinet6/ip6_input.c:1.222 Fri Aug 28 02:32:24 2020 +++ src/sys/netinet6/ip6_input.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip6_input.c,v 1.222 2020/08/28 06:32:24 ozaki-r Exp $ */ +/* $NetBSD: ip6_input.c,v 1.223 2021/02/14 20:58:35 christos Exp $ */ /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $ */ /* @@ -62,7 +62,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.222 2020/08/28 06:32:24 ozaki-r Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.223 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_gateway.h" @@ -301,20 +301,11 @@ ip6_input(struct mbuf *m, struct ifnet * * it. Otherwise, if it is aligned, make sure the entire base * IPv6 header is in the first mbuf of the chain. */ - if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) { - if ((m = m_copyup(m, sizeof(struct ip6_hdr), - (max_linkhdr + 3) & ~3)) == NULL) { - /* XXXJRT new stat, please */ - IP6_STATINC(IP6_STAT_TOOSMALL); - in6_ifstat_inc(rcvif, ifs6_in_hdrerr); - return; - } - } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { - if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { - IP6_STATINC(IP6_STAT_TOOSMALL); - in6_ifstat_inc(rcvif, ifs6_in_hdrerr); - return; - } + if (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(*ip6), true) != 0) { + /* XXXJRT new stat, please */ + IP6_STATINC(IP6_STAT_TOOSMALL); + in6_ifstat_inc(rcvif, ifs6_in_hdrerr); + return; } ip6 = mtod(m, struct ip6_hdr *); @@ -611,7 +602,7 @@ hbhcheck: rtcache_percpu_putref(ip6_forward_rt_percpu); return; } - KASSERT(IP6_HDR_ALIGNED_P(hbh)); + KASSERT(POINTER_ALIGNED_P(hbh, IP6_HDR_ALIGNMENT)); nxt = hbh->ip6h_nxt; /* @@ -884,7 +875,7 @@ ip6_hopopts_input(u_int32_t *plenp, u_in IP6_STATINC(IP6_STAT_TOOSHORT); return -1; } - KASSERT(IP6_HDR_ALIGNED_P(hbh)); + KASSERT(POINTER_ALIGNED_P(hbh, IP6_HDR_ALIGNMENT)); off += hbhlen; hbhlen -= sizeof(struct ip6_hbh); @@ -1224,7 +1215,7 @@ ip6_savecontrol(struct in6pcb *in6p, str IP6_STATINC(IP6_STAT_TOOSHORT); return; } - KASSERT(IP6_HDR_ALIGNED_P(ip6e)); + KASSERT(POINTER_ALIGNED_P(ip6e, IP6_HDR_ALIGNMENT)); switch (nxt) { case IPPROTO_DSTOPTS: Index: src/sys/netinet6/ip6_private.h diff -u src/sys/netinet6/ip6_private.h:1.3 src/sys/netinet6/ip6_private.h:1.4 --- src/sys/netinet6/ip6_private.h:1.3 Mon Apr 28 16:24:10 2008 +++ src/sys/netinet6/ip6_private.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: ip6_private.h,v 1.3 2008/04/28 20:24:10 martin Exp $ */ +/* $NetBSD: ip6_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -43,11 +43,7 @@ extern percpu_t *ip6stat_percpu; #define IP6_STATINC(x) _NET_STATINC(ip6stat_percpu, x) #define IP6_STATDEC(x) _NET_STATDEC(ip6stat_percpu, x) -#ifdef __NO_STRICT_ALIGNMENT -#define IP6_HDR_ALIGNED_P(ip) 1 -#else -#define IP6_HDR_ALIGNED_P(ip) ((((vaddr_t) (ip)) & 3) == 0) -#endif +#define IP6_HDR_ALIGNMENT 3 #endif /* _KERNEL */ #endif /* !_NETINET_IP6_PRIVATE_H_ */ Index: src/sys/netinet6/udp6_usrreq.c diff -u src/sys/netinet6/udp6_usrreq.c:1.148 src/sys/netinet6/udp6_usrreq.c:1.149 --- src/sys/netinet6/udp6_usrreq.c:1.148 Thu Aug 20 17:21:32 2020 +++ src/sys/netinet6/udp6_usrreq.c Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: udp6_usrreq.c,v 1.148 2020/08/20 21:21:32 riastradh Exp $ */ +/* $NetBSD: udp6_usrreq.c,v 1.149 2021/02/14 20:58:35 christos Exp $ */ /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */ /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 itojun Exp $ */ @@ -63,7 +63,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.148 2020/08/20 21:21:32 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.149 2021/02/14 20:58:35 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -665,7 +665,7 @@ udp6_input(struct mbuf **mp, int *offp, * Enforce alignment requirements that are violated in * some cases, see kern/50766 for details. */ - if (UDP_HDR_ALIGNED_P(uh) == 0) { + if (POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT) == 0) { m = m_copyup(m, off + sizeof(struct udphdr), 0); if (m == NULL) { IP6_STATINC(IP6_STAT_TOOSHORT); @@ -674,7 +674,7 @@ udp6_input(struct mbuf **mp, int *offp, ip6 = mtod(m, struct ip6_hdr *); uh = (struct udphdr *)(mtod(m, char *) + off); } - KASSERT(UDP_HDR_ALIGNED_P(uh)); + KASSERT(POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT)); ulen = ntohs((u_short)uh->uh_ulen); /* Index: src/sys/sys/mbuf.h diff -u src/sys/sys/mbuf.h:1.227 src/sys/sys/mbuf.h:1.228 --- src/sys/sys/mbuf.h:1.227 Mon Apr 6 05:32:54 2020 +++ src/sys/sys/mbuf.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: mbuf.h,v 1.227 2020/04/06 09:32:54 jdolecek Exp $ */ +/* $NetBSD: mbuf.h,v 1.228 2021/02/14 20:58:35 christos Exp $ */ /* * Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc. @@ -843,6 +843,17 @@ m_copy_rcvif(struct mbuf *m, const struc m->m_pkthdr.rcvif_index = n->m_pkthdr.rcvif_index; } +static __inline int +m_get_aligned_hdr(struct mbuf **_m, int _align, size_t _hlen, bool _linkhdr) +{ + if (POINTER_ALIGNED_P(mtod(*_m, void *), _align) == 0) + *_m = m_copyup(*_m, _hlen, + _linkhdr ? (max_linkhdr + _align) & ~_align : 0); + else if (__predict_false((*_m)->m_len < _hlen)) + *_m = m_pullup(*_m, _hlen); + return *_m == NULL; +} + void m_print(const struct mbuf *, const char *, void (*)(const char *, ...) __printflike(1, 2)); Index: src/sys/sys/param.h diff -u src/sys/sys/param.h:1.684 src/sys/sys/param.h:1.685 --- src/sys/sys/param.h:1.684 Fri Feb 5 12:03:35 2021 +++ src/sys/sys/param.h Sun Feb 14 15:58:35 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: param.h,v 1.684 2021/02/05 17:03:35 thorpej Exp $ */ +/* $NetBSD: param.h,v 1.685 2021/02/14 20:58:35 christos Exp $ */ /*- * Copyright (c) 1982, 1986, 1989, 1993 @@ -287,6 +287,12 @@ #define ALIGNED_POINTER_LOAD(q,p,t) (*(q) = *((const t *)(p))) #endif +#ifdef __NO_STRICT_ALIGNMENT +#define POINTER_ALIGNED_P(p, a) 1 +#else +#define POINTER_ALIGNED_P(p, a) (((uintptr_t)(p) + (a)) & ~(a)) +#endif + /* * Historic priority levels. These are meaningless and remain only * for source compatibility. Do not use in new code.