Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 4 May 2025 17:09:28 GMT
From:      Michal Meloun <mmel@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 9ee759f3676f - main - Decorate IPv4 structures used for byte buffer overlays as packed.
Message-ID:  <202505041709.544H9SU1060480@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=9ee759f3676f700f8224a95216f659f87f5d9ae9

commit 9ee759f3676f700f8224a95216f659f87f5d9ae9
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2025-05-01 08:54:05 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2025-05-04 11:48:04 +0000

    Decorate IPv4 structures used for byte buffer overlays as packed.
    
    The C language only allows pointer casting to another type if both sides have
    compatible alignments, unaligned casts causes undefined behavior.
    Since we do not have declared (and therefore not checked)  mbuf alignments for
    the various input functions in the IP stack, the worst case (alignment to
    char*) should be expected.
    
     A lot of work still needs to be done on IPv6, especially on the terrible
    accesses to in6_addr members.
    
    It should have no performance impact on all unaligned architectures.
    
    MFC after:      1 month
    Reviewed by:    kib
    PR:             272965
    Differential Revision:  https://reviews.freebsd.org/D50103
---
 sys/netinet/dccp.h                |  2 +-
 sys/netinet/ip.h                  |  4 ++--
 sys/netinet/ip_var.h              |  2 +-
 sys/netinet/pim.h                 |  2 +-
 sys/netinet/tcp.h                 |  2 +-
 sys/netinet/udp.h                 |  2 +-
 tests/sys/netinet/ip_reass_test.c | 12 ++++++++----
 7 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/sys/netinet/dccp.h b/sys/netinet/dccp.h
index 4fb6a0d2ab3e..da83a1b06861 100644
--- a/sys/netinet/dccp.h
+++ b/sys/netinet/dccp.h
@@ -64,7 +64,7 @@ struct dccphdr {
                        uint8_t seq[6];
                } longseq;
        } d_seqno;
-};
+}  __packed;
 
 #define d_seqno_short d_seqno.shortseq;
 #define d_seqno_long d_seqno.longseq.seq;
diff --git a/sys/netinet/ip.h b/sys/netinet/ip.h
index 44db466912d0..6de41a7e79fa 100644
--- a/sys/netinet/ip.h
+++ b/sys/netinet/ip.h
@@ -67,7 +67,7 @@ struct ip {
 	u_char	ip_p;			/* protocol */
 	u_short	ip_sum;			/* checksum */
 	struct	in_addr ip_src,ip_dst;	/* source and dest address */
-} __packed __aligned(2);
+} __packed;
 
 #define	IP_MAXPACKET	65535		/* maximum packet size */
 
@@ -187,7 +187,7 @@ struct	ip_timestamp {
 			uint32_t ipt_time;	/* network format */
 		} ipt_ta[1];
 	} ipt_timestamp;
-};
+} __packed;
 
 /* Flag bits for ipt_flg. */
 #define	IPOPT_TS_TSONLY		0		/* timestamps only */
diff --git a/sys/netinet/ip_var.h b/sys/netinet/ip_var.h
index 18ca5861a40e..f782ebc53eb0 100644
--- a/sys/netinet/ip_var.h
+++ b/sys/netinet/ip_var.h
@@ -47,7 +47,7 @@ struct ipovly {
 	u_short	ih_len;			/* protocol length */
 	struct	in_addr ih_src;		/* source internet address */
 	struct	in_addr ih_dst;		/* destination internet address */
-};
+} __packed;
 
 #ifdef _KERNEL
 /*
diff --git a/sys/netinet/pim.h b/sys/netinet/pim.h
index 98230fc6ae2d..4744ffc7e9d8 100644
--- a/sys/netinet/pim.h
+++ b/sys/netinet/pim.h
@@ -71,7 +71,7 @@ struct pim {
 #endif /* ! _PIM_VT  */
 	uint8_t		pim_reserved;	/* Reserved			*/
 	uint16_t	pim_cksum;	/* IP-style checksum		*/
-};
+} __packed;
 /* KAME-related name backward compatibility */
 #define pim_ver pim_vers
 #define pim_rsv pim_reserved
diff --git a/sys/netinet/tcp.h b/sys/netinet/tcp.h
index 94d41ff67836..41a49b318cd5 100644
--- a/sys/netinet/tcp.h
+++ b/sys/netinet/tcp.h
@@ -77,7 +77,7 @@ struct tcphdr {
 	u_short	th_win;			/* window */
 	u_short	th_sum;			/* checksum */
 	u_short	th_urp;			/* urgent pointer */
-};
+} __packed;
 
 static __inline uint16_t
 __tcp_get_flags(const struct tcphdr *th)
diff --git a/sys/netinet/udp.h b/sys/netinet/udp.h
index edff456ba70e..010f2210b516 100644
--- a/sys/netinet/udp.h
+++ b/sys/netinet/udp.h
@@ -44,7 +44,7 @@ struct udphdr {
 	u_short	uh_dport;		/* destination port */
 	u_short	uh_ulen;		/* udp length */
 	u_short	uh_sum;			/* udp checksum */
-};
+} __packed;
 
 /*
  * User-settable options (used with setsockopt).
diff --git a/tests/sys/netinet/ip_reass_test.c b/tests/sys/netinet/ip_reass_test.c
index a65bfa34e1d4..538815bd7a2c 100644
--- a/tests/sys/netinet/ip_reass_test.c
+++ b/tests/sys/netinet/ip_reass_test.c
@@ -60,12 +60,16 @@ update_cksum(struct ip *ip)
 {
 	size_t i;
 	uint32_t cksum;
-	uint16_t *cksump;
+	uint8_t  *cksump;
+	uint16_t tmp;
 
 	ip->ip_sum = 0;
-	cksump = (uint16_t *)ip;
-	for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(*cksump); cksump++, i++)
-		cksum += ntohs(*cksump);
+	cksump = (char *)ip;
+	for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(uint16_t); i++) {
+		tmp = *cksump++;
+		tmp = tmp << 8 | *cksump++;
+		cksum += ntohs(tmp);
+	}
 	cksum = (cksum >> 16) + (cksum & 0xffff);
 	cksum = ~(cksum + (cksum >> 16));
 	ip->ip_sum = htons((uint16_t)cksum);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202505041709.544H9SU1060480>