From 1a96fef16deb8eb9ed97123cb11eec4298c6338f Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 2 Jan 2020 18:45:32 -0800 Subject: [PATCH] [mesh-forwarder] use SuccessOrExit, update comments (#4453) --- src/core/thread/mesh_forwarder.cpp | 38 ++++++++++++-------------- src/core/thread/mesh_forwarder_ftd.cpp | 17 +++++++----- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index ec20e1aaa..f3a36eb08 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -283,13 +283,10 @@ otError MeshForwarder::UpdateIp6Route(Message &aMessage) VerifyOrExit(!ip6Header.GetSource().IsMulticast(), error = OT_ERROR_DROP); - // 1. Choose correct MAC Source Address. GetMacSourceAddress(ip6Header.GetSource(), mMacSource); - // 2. Choose correct MAC Destination Address. if (mle.GetRole() == OT_DEVICE_ROLE_DISABLED || mle.GetRole() == OT_DEVICE_ROLE_DETACHED) { - // Allow only for link-local unicasts and multicasts. if (ip6Header.GetDestination().IsLinkLocal() || ip6Header.GetDestination().IsLinkLocalMulticast()) { GetMacDestinationAddress(ip6Header.GetDestination(), mMacDest); @@ -304,8 +301,10 @@ otError MeshForwarder::UpdateIp6Route(Message &aMessage) if (ip6Header.GetDestination().IsMulticast()) { - // With the exception of MLE multicasts, a Thread End Device transmits multicasts, - // as IEEE 802.15.4 unicasts to its parent. + // With the exception of MLE multicasts, an End Device + // transmits multicasts, as IEEE 802.15.4 unicasts to its + // parent. + if (mle.GetRole() == OT_DEVICE_ROLE_CHILD && !aMessage.IsSubTypeMle()) { mMacDest.SetShort(mle.GetNextHop(Mac::kShortAddrBroadcast)); @@ -631,7 +630,8 @@ start: if (dstpan == Get().GetPanId()) { #if OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT - // Handle a special case in IEEE 802.15.4-2015, when Pan ID Compression is 0, but Src Pan ID is not present: + // Handle a special case in IEEE 802.15.4-2015, when PAN ID + // Compression is 0, but Src PAN ID is not present: // Dest Address: Extended // Src Address: Extended // Dest Pan ID: Present @@ -709,8 +709,8 @@ start: } } - // The hopsLft field MUST be incremented by one if the destination RLOC16 - // is not that of an active Router. + // The hopsLft field MUST be incremented by one if the + // destination RLOC16 is not that of an active Router. if (!Mle::Mle::IsActiveRouter(aMeshDest)) { hopsLeft += 1; @@ -1123,20 +1123,22 @@ void MeshForwarder::HandleFragment(uint8_t * aFrame, otError error = OT_ERROR_NONE; Lowpan::FragmentHeader fragmentHeader; Message * message = NULL; - int headerLength; // Check the fragment header - VerifyOrExit(fragmentHeader.Init(aFrame, aFrameLength) == OT_ERROR_NONE, error = OT_ERROR_PARSE); + SuccessOrExit(error = fragmentHeader.Init(aFrame, aFrameLength)); aFrame += fragmentHeader.GetHeaderLength(); aFrameLength -= fragmentHeader.GetHeaderLength(); if (fragmentHeader.GetDatagramOffset() == 0) { uint8_t priority; + int headerLength; SuccessOrExit(error = GetFramePriority(aFrame, aFrameLength, aMacSource, aMacDest, priority)); - VerifyOrExit((message = Get().New(Message::kTypeIp6, 0, priority)) != NULL, - error = OT_ERROR_NO_BUFS); + + message = Get().New(Message::kTypeIp6, 0, priority); + VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); + message->SetLinkSecurityEnabled(aLinkInfo.mLinkSecurity); message->SetPanId(aLinkInfo.mPanId); message->AddRss(aLinkInfo.mRss); @@ -1157,8 +1159,6 @@ void MeshForwarder::HandleFragment(uint8_t * aFrame, message->SetDatagramTag(fragmentHeader.GetDatagramTag()); message->SetTimeout(kReassemblyTimeout); - - // copy Fragment message->Write(message->GetOffset(), aFrameLength, aFrame); message->MoveOffset(aFrameLength); @@ -1181,7 +1181,7 @@ void MeshForwarder::HandleFragment(uint8_t * aFrame, mUpdateTimer.Start(kStateUpdatePeriod); } } - else + else // Received frame is a "next fragment". { for (message = mReassemblyList.GetHead(); message; message = message->GetNext()) { @@ -1202,17 +1202,13 @@ void MeshForwarder::HandleFragment(uint8_t * aFrame, // message with a new tag. In either case, we can safely clear any // remaining fragments stored in the reassembly list. - if (!GetRxOnWhenIdle()) + if (!GetRxOnWhenIdle() && (message == NULL) && aLinkInfo.mLinkSecurity) { - if ((message == NULL) && (aLinkInfo.mLinkSecurity)) - { - ClearReassemblyList(); - } + ClearReassemblyList(); } VerifyOrExit(message != NULL, error = OT_ERROR_DROP); - // copy Fragment message->Write(message->GetOffset(), aFrameLength, aFrame); message->MoveOffset(aFrameLength); message->AddRss(aLinkInfo.mRss); diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index 9e644d4cd..298252e58 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -507,8 +507,7 @@ void MeshForwarder::HandleMesh(uint8_t * aFrame, Mac::Address meshSource; Lowpan::MeshHeader meshHeader; - // Check the mesh header - VerifyOrExit(meshHeader.Init(aFrame, aFrameLength) == OT_ERROR_NONE, error = OT_ERROR_PARSE); + SuccessOrExit(error = meshHeader.Init(aFrame, aFrameLength)); // Security Check: only process Mesh Header frames that had security enabled. VerifyOrExit(aLinkInfo.mLinkSecurity && meshHeader.IsValid(), error = OT_ERROR_SECURITY); @@ -593,12 +592,16 @@ void MeshForwarder::UpdateRoutes(uint8_t * aFrame, if (Get().UpdateCacheEntry(ip6Header.GetSource(), aMeshSource.GetShort()) == OT_ERROR_NOT_FOUND) { - // Thread 1.1 Specification 5.5.2.2: - // FTDs MAY add/update EID-to-RLOC Map Cache entries by inspecting packets being received. - if ((Get().IsFullThreadDevice() /* only for FTD */ && - !Get().IsMinimalChild(aMeshSource.GetShort()) /* Exclude MTD child source */) && + // Thread 1.1 Specification 5.5.2.2: FTDs MAY add/update + // EID-to-RLOC Map Cache entries by inspecting packets + // being received. We exclude frames from an MTD child + // source and verify that the destination is the device + // itself or an MTD child of the device. + + if (Get().IsFullThreadDevice() && + !Get().IsMinimalChild(aMeshSource.GetShort()) && (aMeshDest.GetShort() == Get().GetShortAddress() || - Get().IsMinimalChild(aMeshDest.GetShort())) /* Received for itself or its MTD child */) + Get().IsMinimalChild(aMeshDest.GetShort()))) { Get().AddCacheEntry(ip6Header.GetSource(), aMeshSource.GetShort()); }