From 025430a96425d8e8ce6144190871a8423829acea Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Fri, 2 Dec 2022 00:10:02 +0800 Subject: [PATCH] net/icmpv6: Fix icmpv6_reply function Fix icmpv6_reply logic broken by commit 48311cc61f and 391b501639. - 48311cc61f "Fix unaligned memory access when creating ICMP Port Unreachable messages" - It removed `htonl` function outside `data`, then the byte order may be wrong, so add `htons` back. - 391b501639 "net: extract l3 header build code into new functions" - It mis-removed the `memmove`, and the icmpv6 has no payload copied after this commit. Signed-off-by: Zhe Weng --- net/icmpv6/icmpv6_reply.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/net/icmpv6/icmpv6_reply.c b/net/icmpv6/icmpv6_reply.c index 2784823d49..addc3d086f 100644 --- a/net/icmpv6/icmpv6_reply.c +++ b/net/icmpv6/icmpv6_reply.c @@ -112,6 +112,10 @@ void icmpv6_reply(FAR struct net_driver_s *dev, int type, int code, int data) dev->d_len = ipicmplen + datalen; + /* Copy fields from original packet */ + + memmove(icmpv6 + 1, ipv6, datalen); + ipv6_build_header(IPv6BUF, dev->d_len - IPv6_HDRLEN, IP_PROTO_ICMP6, dev->d_ipv6addr, ipv6->srcipaddr, 255); @@ -119,8 +123,8 @@ void icmpv6_reply(FAR struct net_driver_s *dev, int type, int code, int data) icmpv6->type = type; icmpv6->code = code; - icmpv6->data[0] = data >> 16; - icmpv6->data[1] = data & 0xffff; + icmpv6->data[0] = htons(data >> 16); + icmpv6->data[1] = htons(data & 0xffff); /* Calculate the ICMPv6 checksum over the ICMPv6 header and payload. */