From 48311cc61f9bef0143992408bc1353a11e19ea41 Mon Sep 17 00:00:00 2001 From: Norman Rasmussen Date: Mon, 27 Dec 2021 22:32:42 -0800 Subject: [PATCH] Fix unaligned memory access when creating ICMP Port Unreachable messages commit 3b69d09c80b4d21681475c43d9f165838bbf1a5c corrected the unreachable handling for net/udp/icmp but introduced an unaligned store. This splits the uint32_t data field into a two element uint16_t data field to avoid the unaligned store. --- include/nuttx/net/icmp.h | 2 +- include/nuttx/net/icmpv6.h | 2 +- net/icmp/icmp_reply.c | 3 ++- net/icmpv6/icmpv6_reply.c | 7 ++++--- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/nuttx/net/icmp.h b/include/nuttx/net/icmp.h index eeb52a308b1..9665753590f 100644 --- a/include/nuttx/net/icmp.h +++ b/include/nuttx/net/icmp.h @@ -141,7 +141,7 @@ struct icmp_hdr_s uint16_t seqno; /* " " "" " " " " " " " " */ }; - uint32_t data; + uint16_t data[2]; }; }; diff --git a/include/nuttx/net/icmpv6.h b/include/nuttx/net/icmpv6.h index 457619d223b..e47566aef68 100644 --- a/include/nuttx/net/icmpv6.h +++ b/include/nuttx/net/icmpv6.h @@ -157,7 +157,7 @@ struct icmpv6_hdr_s * message type indicated by the Type and Code fields. */ - uint32_t data; + uint16_t data[2]; }; /* The ICMPv6 and IPv6 headers */ diff --git a/net/icmp/icmp_reply.c b/net/icmp/icmp_reply.c index c98a5f7993d..77d05c2415b 100644 --- a/net/icmp/icmp_reply.c +++ b/net/icmp/icmp_reply.c @@ -153,7 +153,8 @@ void icmp_reply(FAR struct net_driver_s *dev, int type, int code) icmp->type = type; icmp->icode = code; - icmp->data = 0; + icmp->data[0] = 0; + icmp->data[1] = 0; /* Calculate the ICMP checksum. */ diff --git a/net/icmpv6/icmpv6_reply.c b/net/icmpv6/icmpv6_reply.c index 179c31b52be..72258294cb6 100644 --- a/net/icmpv6/icmpv6_reply.c +++ b/net/icmpv6/icmpv6_reply.c @@ -133,9 +133,10 @@ void icmpv6_reply(FAR struct net_driver_s *dev, int type, int code, int data) /* Initialize the ICMPv6 header */ - icmpv6->type = type; - icmpv6->code = code; - icmpv6->data = htonl(data); + icmpv6->type = type; + icmpv6->code = code; + icmpv6->data[0] = data >> 16; + icmpv6->data[1] = data & 0xffff; /* Calculate the ICMPv6 checksum over the ICMPv6 header and payload. */