Module Name:    src
Committed By:   christos
Date:           Fri Feb 19 14:52:00 UTC 2021

Modified Files:
        src/sys/net: if_arp.h if_bridge.c
        src/sys/netinet: if_arp.c 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:
- Make ALIGNED_POINTER use __alignof(t) instead of sizeof(t). This is more
  correct because it works with non-primitive types and provides the ABI
  alignment for the type the compiler will use.
- Remove all the *_HDR_ALIGNMENT macros and asserts
- Replace POINTER_ALIGNED_P with ACCESSIBLE_POINTER which is identical to
  ALIGNED_POINTER, but returns that the pointer is always aligned if the
  CPU supports unaligned accesses.
[ as proposed in tech-kern ]


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/sys/net/if_arp.h
cvs rdiff -u -r1.178 -r1.179 src/sys/net/if_bridge.c
cvs rdiff -u -r1.306 -r1.307 src/sys/netinet/if_arp.c
cvs rdiff -u -r1.19 -r1.20 src/sys/netinet/in_l2tp.c
cvs rdiff -u -r1.84 -r1.85 src/sys/netinet/ip_flow.c
cvs rdiff -u -r1.398 -r1.399 src/sys/netinet/ip_input.c
cvs rdiff -u -r1.5 -r1.6 src/sys/netinet/ip_private.h \
    src/sys/netinet/tcp_private.h src/sys/netinet/udp_private.h
cvs rdiff -u -r1.425 -r1.426 src/sys/netinet/tcp_input.c
cvs rdiff -u -r1.260 -r1.261 src/sys/netinet/udp_usrreq.c
cvs rdiff -u -r1.249 -r1.250 src/sys/netinet6/icmp6.c
cvs rdiff -u -r1.20 -r1.21 src/sys/netinet6/in6_l2tp.c
cvs rdiff -u -r1.41 -r1.42 src/sys/netinet6/ip6_flow.c
cvs rdiff -u -r1.223 -r1.224 src/sys/netinet6/ip6_input.c
cvs rdiff -u -r1.5 -r1.6 src/sys/netinet6/ip6_private.h
cvs rdiff -u -r1.149 -r1.150 src/sys/netinet6/udp6_usrreq.c
cvs rdiff -u -r1.231 -r1.232 src/sys/sys/mbuf.h
cvs rdiff -u -r1.689 -r1.690 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.42 src/sys/net/if_arp.h:1.43
--- src/sys/net/if_arp.h:1.42	Wed Feb 17 17:32:04 2021
+++ src/sys/net/if_arp.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.h,v 1.42 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: if_arp.h,v 1.43 2021/02/19 14:51:59 christos Exp $	*/
 
 /*
  * Copyright (c) 1986, 1993
@@ -72,8 +72,6 @@ struct	arphdr {
 	uint8_t  ar_tpa[];	/* target protocol address */
 #endif
 };
-#define	ARP_HDR_ALIGNMENT	__alignof(struct arphdr)
-__CTASSERT(ARP_HDR_ALIGNMENT == 2);
 
 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.178 src/sys/net/if_bridge.c:1.179
--- src/sys/net/if_bridge.c:1.178	Sun Feb 14 15:58:34 2021
+++ src/sys/net/if_bridge.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $	*/
+/*	$NetBSD: if_bridge.c,v 1.179 2021/02/19 14:51:59 christos Exp $	*/
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -80,7 +80,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.179 2021/02/19 14:51:59 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -2806,7 +2806,7 @@ bridge_ip_checkbasic(struct mbuf **mp)
 	if (*mp == NULL)
 		return -1;
 
-	if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) {
+	if (M_GET_ALIGNED_HDR(&m, struct ip, true) != 0) {
 		/* XXXJRT new stat, please */
 		ip_statinc(IP_STAT_TOOSMALL);
 		goto bad;
@@ -2900,7 +2900,7 @@ 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 (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(*ip6), true) != 0) {
+	if (M_GET_ALIGNED_HDR(&m, struct ip6_hdr, true) != 0) {
 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
 		/* XXXJRT new stat, please */
 		ip6_statinc(IP6_STAT_TOOSMALL);

Index: src/sys/netinet/if_arp.c
diff -u src/sys/netinet/if_arp.c:1.306 src/sys/netinet/if_arp.c:1.307
--- src/sys/netinet/if_arp.c:1.306	Tue Feb 16 05:22:52 2021
+++ src/sys/netinet/if_arp.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.c,v 1.306 2021/02/16 10:22:52 martin Exp $	*/
+/*	$NetBSD: if_arp.c,v 1.307 2021/02/19 14:51:59 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.306 2021/02/16 10:22:52 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.307 2021/02/19 14:51:59 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -706,7 +706,7 @@ arpintr(void)
 				goto badlen;
 		}
 		ar = mtod(m, struct arphdr *);
-		KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT));
+		KASSERT(ACCESSIBLE_POINTER(ar, struct arphdr));
 
 		rcvif = m_get_rcvif(m, &s);
 		if (__predict_false(rcvif == NULL)) {
@@ -735,7 +735,7 @@ arpintr(void)
 			if ((m = m_pullup(m, arplen)) == NULL)
 				goto badlen;
 			ar = mtod(m, struct arphdr *);
-			KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT));
+			KASSERT(ACCESSIBLE_POINTER(ar, struct arphdr));
 		}
 
 		switch (ntohs(ar->ar_pro)) {

Index: src/sys/netinet/in_l2tp.c
diff -u src/sys/netinet/in_l2tp.c:1.19 src/sys/netinet/in_l2tp.c:1.20
--- src/sys/netinet/in_l2tp.c:1.19	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet/in_l2tp.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: in_l2tp.c,v 1.19 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: in_l2tp.c,v 1.20 2021/02/19 14:51:59 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.19 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.20 2021/02/19 14:51:59 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_l2tp.h"
@@ -197,8 +197,7 @@ in_l2tp_output(struct l2tp_variant *var,
 		error = ENOBUFS;
 		goto out;
 	}
-	if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(iphdr), false) != 0)
-	{
+	if (M_GET_ALIGNED_HDR(&m, struct ip, false) != 0) {
 		error = ENOBUFS;
 		goto out;
 	}

Index: src/sys/netinet/ip_flow.c
diff -u src/sys/netinet/ip_flow.c:1.84 src/sys/netinet/ip_flow.c:1.85
--- src/sys/netinet/ip_flow.c:1.84	Sun Feb 14 22:41:01 2021
+++ src/sys/netinet/ip_flow.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_flow.c,v 1.84 2021/02/15 03:41:01 knakahara Exp $	*/
+/*	$NetBSD: ip_flow.c,v 1.85 2021/02/19 14:51:59 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.84 2021/02/15 03:41:01 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.85 2021/02/19 14:51:59 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_net_mpsafe.h"
@@ -231,7 +231,7 @@ ipflow_fastforward(struct mbuf *m)
 	 * IP header with no option and valid version and length
 	 */
 	ip = mtod(m, struct ip *);
-	if (!POINTER_ALIGNED_P(ip, IP_HDR_ALIGNMENT)) {
+	if (!ACCESSIBLE_POINTER(ip, struct ip)) {
 		memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
 		ip = &ip_store;
 	}
@@ -313,7 +313,7 @@ ipflow_fastforward(struct mbuf *m)
 	 *
 	 * XXX Use m_copyback_cow(9) here? --dyoung
 	 */
-	if (!POINTER_ALIGNED_P(mtod(m, void *), IP_HDR_ALIGNMENT))
+	if (!ACCESSIBLE_POINTER(mtod(m, void *), struct ip))
 		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.398 src/sys/netinet/ip_input.c:1.399
--- src/sys/netinet/ip_input.c:1.398	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet/ip_input.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_input.c,v 1.398 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: ip_input.c,v 1.399 2021/02/19 14:51:59 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.398 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.399 2021/02/19 14:51:59 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -454,7 +454,7 @@ 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 (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) {
+	if (M_GET_ALIGNED_HDR(&m, struct ip, true) != 0) {
 		/* XXXJRT new stat, please */
 		IP_STATINC(IP_STAT_TOOSMALL);
 		goto out;

Index: src/sys/netinet/ip_private.h
diff -u src/sys/netinet/ip_private.h:1.5 src/sys/netinet/ip_private.h:1.6
--- src/sys/netinet/ip_private.h:1.5	Wed Feb 17 17:32:04 2021
+++ src/sys/netinet/ip_private.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_private.h,v 1.5 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: ip_private.h,v 1.6 2021/02/19 14:51:59 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -43,8 +43,6 @@ extern	percpu_t *ipstat_percpu;
 #define	IP_STATINC(x)		_NET_STATINC(ipstat_percpu, x)
 #define	IP_STATDEC(x)		_NET_STATDEC(ipstat_percpu, x)
 
-#define	IP_HDR_ALIGNMENT	__alignof(struct ip)
-__CTASSERT(IP_HDR_ALIGNMENT == 4);
 #endif /* _KERNEL */
 
 #endif /* !_NETINET_IP_PRIVATE_H_ */
Index: src/sys/netinet/tcp_private.h
diff -u src/sys/netinet/tcp_private.h:1.5 src/sys/netinet/tcp_private.h:1.6
--- src/sys/netinet/tcp_private.h:1.5	Wed Feb 17 17:32:04 2021
+++ src/sys/netinet/tcp_private.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tcp_private.h,v 1.5 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: tcp_private.h,v 1.6 2021/02/19 14:51:59 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -43,8 +43,6 @@ 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)
 
-#define	TCP_HDR_ALIGNMENT	__alignof(struct tcphdr)
-__CTASSERT(TCP_HDR_ALIGNMENT == 4);
 #endif /* _KERNEL */
 
 #endif /* !_NETINET_TCP_PRIVATE_H_ */
Index: src/sys/netinet/udp_private.h
diff -u src/sys/netinet/udp_private.h:1.5 src/sys/netinet/udp_private.h:1.6
--- src/sys/netinet/udp_private.h:1.5	Wed Feb 17 17:32:04 2021
+++ src/sys/netinet/udp_private.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: udp_private.h,v 1.5 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: udp_private.h,v 1.6 2021/02/19 14:51:59 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -39,8 +39,6 @@ extern	percpu_t *udpstat_percpu;
 
 #define	UDP_STATINC(x)		_NET_STATINC(udpstat_percpu, x)
 
-#define	UDP_HDR_ALIGNMENT	__alignof(struct udphdr)
-__CTASSERT(UDP_HDR_ALIGNMENT == 2);
 #endif /* _KERNEL */
 
 #endif /* !_NETINET_UDP_PRIVATE_H_ */

Index: src/sys/netinet/tcp_input.c
diff -u src/sys/netinet/tcp_input.c:1.425 src/sys/netinet/tcp_input.c:1.426
--- src/sys/netinet/tcp_input.c:1.425	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet/tcp_input.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tcp_input.c,v 1.425 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: tcp_input.c,v 1.426 2021/02/19 14:51:59 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.425 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.426 2021/02/19 14:51:59 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 (POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT) == 0) {
+	if (ACCESSIBLE_POINTER(th, struct tcp_hdr) == 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(POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT));
+	KASSERT(ACCESSIBLE_POINTER(th, struct tcp_hdr));
 
 	/*
 	 * 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(POINTER_ALIGNED_P(th, TCP_HDR_ALIGNMENT));
+		KASSERT(ACCESSIBLE_POINTER(th, struct tcp_hdr));
 		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.260 src/sys/netinet/udp_usrreq.c:1.261
--- src/sys/netinet/udp_usrreq.c:1.260	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet/udp_usrreq.c	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: udp_usrreq.c,v 1.260 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: udp_usrreq.c,v 1.261 2021/02/19 14:51:59 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.260 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.261 2021/02/19 14:51:59 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 (POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT) == 0) {
+	if (ACCESSIBLE_POINTER(uh, struct udphdr) == 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(POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT));
+	KASSERT(ACCESSIBLE_POINTER(uh, struct udphdr));
 
 	/* 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.249 src/sys/netinet6/icmp6.c:1.250
--- src/sys/netinet6/icmp6.c:1.249	Mon Feb 15 05:13:45 2021
+++ src/sys/netinet6/icmp6.c	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: icmp6.c,v 1.249 2021/02/15 10:13:45 martin Exp $	*/
+/*	$NetBSD: icmp6.c,v 1.250 2021/02/19 14:52:00 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.249 2021/02/15 10:13:45 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.250 2021/02/19 14:52:00 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 (POINTER_ALIGNED_P(icmp6, IP6_HDR_ALIGNMENT) == 0) {
+	if (ACCESSIBLE_POINTER(icmp6, struct ip6_hdr) == 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(POINTER_ALIGNED_P(icmp6, IP6_HDR_ALIGNMENT));
+	KASSERT(ACCESSIBLE_POINTER(icmp6, struct ip6_hdr));
 
 	/*
 	 * calculate the checksum

Index: src/sys/netinet6/in6_l2tp.c
diff -u src/sys/netinet6/in6_l2tp.c:1.20 src/sys/netinet6/in6_l2tp.c:1.21
--- src/sys/netinet6/in6_l2tp.c:1.20	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet6/in6_l2tp.c	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: in6_l2tp.c,v 1.20 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: in6_l2tp.c,v 1.21 2021/02/19 14:52:00 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.20 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in6_l2tp.c,v 1.21 2021/02/19 14:52:00 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_l2tp.h"
@@ -193,8 +193,7 @@ in6_l2tp_output(struct l2tp_variant *var
 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
 	if (m == NULL)
 		return ENOBUFS;
-	if (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(ip6hdr),
-	    false) != 0)
+	if (M_GET_ALIGNED_HDR(&m, struct ip6_hdr, 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.41 src/sys/netinet6/ip6_flow.c:1.42
--- src/sys/netinet6/ip6_flow.c:1.41	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet6/ip6_flow.c	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip6_flow.c,v 1.41 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: ip6_flow.c,v 1.42 2021/02/19 14:52:00 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.41 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.42 2021/02/19 14:52:00 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 (POINTER_ALIGNED_P(mtod(m, const void *), IP6_HDR_ALIGNMENT) == 0) {
+	if (ACCESSIBLE_POINTER(mtod(m, const void *), struct ip6_hdr) == 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.223 src/sys/netinet6/ip6_input.c:1.224
--- src/sys/netinet6/ip6_input.c:1.223	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet6/ip6_input.c	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip6_input.c,v 1.223 2021/02/14 20:58:35 christos Exp $	*/
+/*	$NetBSD: ip6_input.c,v 1.224 2021/02/19 14:52:00 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.223 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.224 2021/02/19 14:52:00 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_gateway.h"
@@ -301,7 +301,7 @@ 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 (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(*ip6), true) != 0) {
+	if (M_GET_ALIGNED_HDR(&m, struct ip6_hdr, true) != 0) {
 		/* XXXJRT new stat, please */
 		IP6_STATINC(IP6_STAT_TOOSMALL);
 		in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
@@ -602,7 +602,7 @@ hbhcheck:
 			rtcache_percpu_putref(ip6_forward_rt_percpu);
 			return;
 		}
-		KASSERT(POINTER_ALIGNED_P(hbh, IP6_HDR_ALIGNMENT));
+		KASSERT(ACCESSIBLE_POINTER(hbh, struct ip6_hdr));
 		nxt = hbh->ip6h_nxt;
 
 		/*
@@ -875,7 +875,7 @@ ip6_hopopts_input(u_int32_t *plenp, u_in
 		IP6_STATINC(IP6_STAT_TOOSHORT);
 		return -1;
 	}
-	KASSERT(POINTER_ALIGNED_P(hbh, IP6_HDR_ALIGNMENT));
+	KASSERT(ACCESSIBLE_POINTER(hbh, struct ip6_hdr));
 	off += hbhlen;
 	hbhlen -= sizeof(struct ip6_hbh);
 
@@ -1215,7 +1215,7 @@ ip6_savecontrol(struct in6pcb *in6p, str
 				IP6_STATINC(IP6_STAT_TOOSHORT);
 				return;
 			}
-			KASSERT(POINTER_ALIGNED_P(ip6e, IP6_HDR_ALIGNMENT));
+			KASSERT(ACCESSIBLE_POINTER(ip6e, struct ip6_hdr));
 
 			switch (nxt) {
 			case IPPROTO_DSTOPTS:

Index: src/sys/netinet6/ip6_private.h
diff -u src/sys/netinet6/ip6_private.h:1.5 src/sys/netinet6/ip6_private.h:1.6
--- src/sys/netinet6/ip6_private.h:1.5	Wed Feb 17 17:32:04 2021
+++ src/sys/netinet6/ip6_private.h	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip6_private.h,v 1.5 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: ip6_private.h,v 1.6 2021/02/19 14:52:00 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -43,8 +43,6 @@ extern	percpu_t *ip6stat_percpu;
 #define	IP6_STATINC(x)		_NET_STATINC(ip6stat_percpu, x)
 #define	IP6_STATDEC(x)		_NET_STATDEC(ip6stat_percpu, x)
 
-#define	IP6_HDR_ALIGNMENT	__alignof(struct ip6_hdr)
-__CTASSERT(IP6_HDR_ALIGNMENT == 4);
 #endif /* _KERNEL */
 
 #endif /* !_NETINET_IP6_PRIVATE_H_ */

Index: src/sys/netinet6/udp6_usrreq.c
diff -u src/sys/netinet6/udp6_usrreq.c:1.149 src/sys/netinet6/udp6_usrreq.c:1.150
--- src/sys/netinet6/udp6_usrreq.c:1.149	Sun Feb 14 15:58:35 2021
+++ src/sys/netinet6/udp6_usrreq.c	Fri Feb 19 09:52:00 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: udp6_usrreq.c,v 1.149 2021/02/14 20:58:35 christos Exp $ */
+/* $NetBSD: udp6_usrreq.c,v 1.150 2021/02/19 14:52:00 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.149 2021/02/14 20:58:35 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.150 2021/02/19 14:52:00 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 (POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT) == 0) {
+	if (ACCESSIBLE_POINTER(uh, struct udphdr) == 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(POINTER_ALIGNED_P(uh, UDP_HDR_ALIGNMENT));
+	KASSERT(ACCESSIBLE_POINTER(uh, struct udphdr));
 	ulen = ntohs((u_short)uh->uh_ulen);
 
 	/*

Index: src/sys/sys/mbuf.h
diff -u src/sys/sys/mbuf.h:1.231 src/sys/sys/mbuf.h:1.232
--- src/sys/sys/mbuf.h:1.231	Wed Feb 17 17:32:04 2021
+++ src/sys/sys/mbuf.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mbuf.h,v 1.231 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: mbuf.h,v 1.232 2021/02/19 14:51:59 christos Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc.
@@ -843,15 +843,21 @@ m_copy_rcvif(struct mbuf *m, const struc
 	m->m_pkthdr.rcvif_index = n->m_pkthdr.rcvif_index;
 }
 
+#define M_GET_ALIGNED_HDR(m, type, linkhdr) \
+    m_get_aligned_hdr((m), __alignof(type) - 1, sizeof(type), (linkhdr))
+
 static __inline int
-m_get_aligned_hdr(struct mbuf **m, int align, size_t hlen, bool linkhdr)
+m_get_aligned_hdr(struct mbuf **m, int mask, size_t hlen, bool linkhdr)
 {
-	if (POINTER_ALIGNED_P(mtod(*m, void *), align) == 0) {
-		--align;	// Turn into mask
+#ifndef __NO_STRICT_ALIGNMENT
+	if (((uintptr_t)mtod(*m, void *) & mask) != 0)
 		*m = m_copyup(*m, hlen, 
-		      linkhdr ? (max_linkhdr + align) & ~align : 0);
-	} else if (__predict_false((size_t)(*m)->m_len < hlen))
+		      linkhdr ? (max_linkhdr + mask) & ~mask : 0);
+	else
+#endif
+	if (__predict_false((size_t)(*m)->m_len < hlen))
 		*m = m_pullup(*m, hlen);
+
 	return *m == NULL;
 }
 

Index: src/sys/sys/param.h
diff -u src/sys/sys/param.h:1.689 src/sys/sys/param.h:1.690
--- src/sys/sys/param.h:1.689	Wed Feb 17 17:32:04 2021
+++ src/sys/sys/param.h	Fri Feb 19 09:51:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.689 2021/02/17 22:32:04 christos Exp $	*/
+/*	$NetBSD: param.h,v 1.690 2021/02/19 14:51:59 christos Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -281,16 +281,24 @@
 #define	ALIGN(p)		(((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
 #endif
 #ifndef ALIGNED_POINTER
-#define	ALIGNED_POINTER(p,t)	((((uintptr_t)(p)) & (sizeof(t) - 1)) == 0)
+#define	ALIGNED_POINTER(p,t)	((((uintptr_t)(p)) & (__alignof(t) - 1)) == 0)
 #endif
 #ifndef ALIGNED_POINTER_LOAD
 #define	ALIGNED_POINTER_LOAD(q,p,t)	(*(q) = *((const t *)(p)))
 #endif
 
+/*
+ * Return if pointer p is accessible for type t. For primitive types
+ * this means that the pointer itself can be dereferenced; for structures
+ * and unions this means that any field can be dereferenced. On CPUs
+ * that allow unaligned pointer access, we always return that the pointer
+ * is accessible to prevent unnecessary copies, although this might not be
+ * necessarily faster.
+ */
 #ifdef __NO_STRICT_ALIGNMENT
-#define	POINTER_ALIGNED_P(p, a)		1
+#define	ACCESSIBLE_POINTER(p, t)	1
 #else
-#define	POINTER_ALIGNED_P(p, a)		(((uintptr_t)(p) & ((a) - 1)) == 0)
+#define	ACCESSIBLE_POINTER(p, t)	ALIGNED_POINTER(p, t)
 #endif
 
 /*

Reply via email to