diff --git a/src/core/net/icmp6.cpp b/src/core/net/icmp6.cpp index b392209be..e28ed20e7 100644 --- a/src/core/net/icmp6.cpp +++ b/src/core/net/icmp6.cpp @@ -98,12 +98,23 @@ otError Icmp::SendError(IcmpHeader::Type aType, IcmpHeader icmp6Header; Header ip6Header; + VerifyOrExit(aMessage.GetLength() >= sizeof(ip6Header), error = OT_ERROR_INVALID_ARGS); + + aMessage.Read(0, sizeof(ip6Header), &ip6Header); + + if (ip6Header.GetNextHeader() == kProtoIcmp6) + { + VerifyOrExit(aMessage.GetLength() >= (sizeof(ip6Header) + sizeof(icmp6Header))); + + aMessage.Read(sizeof(ip6Header), sizeof(icmp6Header), &icmp6Header); + VerifyOrExit(!icmp6Header.IsError()); + } + messageInfoLocal = aMessageInfo; VerifyOrExit((message = Get().NewMessage(0)) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetLength(sizeof(icmp6Header) + sizeof(ip6Header))); - aMessage.Read(0, sizeof(ip6Header), &ip6Header); message->Write(sizeof(icmp6Header), sizeof(ip6Header), &ip6Header); icmp6Header.Init(); diff --git a/src/core/net/icmp6.hpp b/src/core/net/icmp6.hpp index b15cbb9ac..29a152f54 100644 --- a/src/core/net/icmp6.hpp +++ b/src/core/net/icmp6.hpp @@ -101,6 +101,15 @@ public: kCodeFragmReasTimeEx = OT_ICMP6_CODE_FRAGM_REAS_TIME_EX, ///< Fragment Reassembly Time Exceeded }; + /** + * This method indicates whether the ICMPv6 message is an error message. + * + * @retval TRUE if the ICMPv6 message is an error message. + * @retval FALSE if the ICMPv6 message is an informational message. + * + */ + bool IsError(void) const { return mType < OT_ICMP6_TYPE_ECHO_REQUEST; } + /** * This method returns the ICMPv6 message type. *