mirror of
https://github.com/espressif/openthread.git
synced 2026-08-04 01:47:47 +00:00
[ip6] add MessageOrigin to indicate origin or IPv6 message (#8036)
This commit adds a new enum `Ip6::MessageOrigin` which indicates the origin of the message: From Thread Netif or from host. In case the message is originating from host, it also indicates whether or not it is allowed to pass back the message to the host.
This commit is contained in:
@@ -135,7 +135,7 @@ void otIp6SetReceiveFilterEnabled(otInstance *aInstance, bool aEnabled)
|
||||
otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
|
||||
{
|
||||
return AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage),
|
||||
!OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS);
|
||||
OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS);
|
||||
}
|
||||
|
||||
otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
|
||||
|
||||
+34
-36
@@ -322,7 +322,7 @@ Error Ip6::InsertMplOption(Message &aMessage, Header &aHeader, MessageInfo &aMes
|
||||
|
||||
if ((messageCopy = aMessage.Clone()) != nullptr)
|
||||
{
|
||||
IgnoreError(HandleDatagram(*messageCopy, nullptr, nullptr, /* aFromHost */ true));
|
||||
IgnoreError(HandleDatagram(*messageCopy, kFromHostDisallowLoopBack));
|
||||
LogInfo("Message copy for indirect transmission to sleepy children");
|
||||
}
|
||||
else
|
||||
@@ -555,7 +555,7 @@ void Ip6::HandleSendQueue(void)
|
||||
while ((message = mSendQueue.GetHead()) != nullptr)
|
||||
{
|
||||
mSendQueue.Dequeue(*message);
|
||||
IgnoreError(HandleDatagram(*message, nullptr, nullptr, /* aFromHost */ false));
|
||||
IgnoreError(HandleDatagram(*message, kFromHostAllowLoopBack));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -699,7 +699,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Ip6::HandleFragment(Message &aMessage, Netif *aNetif, MessageInfo &aMessageInfo, bool aFromHost)
|
||||
Error Ip6::HandleFragment(Message &aMessage, MessageOrigin aOrigin, MessageInfo &aMessageInfo)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
Header header, headerBuffer;
|
||||
@@ -791,7 +791,7 @@ Error Ip6::HandleFragment(Message &aMessage, Netif *aNetif, MessageInfo &aMessag
|
||||
|
||||
mReassemblyList.Dequeue(*message);
|
||||
|
||||
IgnoreError(HandleDatagram(*message, aNetif, aMessageInfo.mLinkInfo, aFromHost));
|
||||
IgnoreError(HandleDatagram(*message, aOrigin, aMessageInfo.mLinkInfo));
|
||||
}
|
||||
|
||||
exit:
|
||||
@@ -878,11 +878,10 @@ Error Ip6::FragmentDatagram(Message &aMessage, uint8_t aIpProto)
|
||||
return kErrorNone;
|
||||
}
|
||||
|
||||
Error Ip6::HandleFragment(Message &aMessage, Netif *aNetif, MessageInfo &aMessageInfo, bool aFromHost)
|
||||
Error Ip6::HandleFragment(Message &aMessage, MessageOrigin aOrigin, MessageInfo &aMessageInfo)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aNetif);
|
||||
OT_UNUSED_VARIABLE(aOrigin);
|
||||
OT_UNUSED_VARIABLE(aMessageInfo);
|
||||
OT_UNUSED_VARIABLE(aFromHost);
|
||||
|
||||
Error error = kErrorNone;
|
||||
FragmentHeader fragmentHeader;
|
||||
@@ -898,16 +897,15 @@ exit:
|
||||
}
|
||||
#endif // OPENTHREAD_CONFIG_IP6_FRAGMENTATION_ENABLE
|
||||
|
||||
Error Ip6::HandleExtensionHeaders(Message & aMessage,
|
||||
Netif * aNetif,
|
||||
MessageInfo &aMessageInfo,
|
||||
Header & aHeader,
|
||||
uint8_t & aNextHeader,
|
||||
bool aFromHost,
|
||||
bool & aReceive)
|
||||
Error Ip6::HandleExtensionHeaders(Message & aMessage,
|
||||
MessageOrigin aOrigin,
|
||||
MessageInfo & aMessageInfo,
|
||||
Header & aHeader,
|
||||
uint8_t & aNextHeader,
|
||||
bool & aReceive)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
bool isOutbound = (aNetif == nullptr);
|
||||
bool isOutbound = (aOrigin != kFromThreadNetif);
|
||||
ExtensionHeader extHeader;
|
||||
|
||||
while (aReceive || aNextHeader == kProtoHopOpts)
|
||||
@@ -922,10 +920,10 @@ Error Ip6::HandleExtensionHeaders(Message & aMessage,
|
||||
|
||||
case kProtoFragment:
|
||||
#if !OPENTHREAD_CONFIG_IP6_FRAGMENTATION_ENABLE
|
||||
IgnoreError(ProcessReceiveCallback(aMessage, aMessageInfo, aNextHeader, aFromHost,
|
||||
IgnoreError(ProcessReceiveCallback(aMessage, aOrigin, aMessageInfo, aNextHeader,
|
||||
/* aAllowReceiveFilter */ false, Message::kCopyToUse));
|
||||
#endif
|
||||
SuccessOrExit(error = HandleFragment(aMessage, aNetif, aMessageInfo, aFromHost));
|
||||
SuccessOrExit(error = HandleFragment(aMessage, aOrigin, aMessageInfo));
|
||||
break;
|
||||
|
||||
case kProtoDstOpts:
|
||||
@@ -1020,16 +1018,17 @@ exit:
|
||||
}
|
||||
|
||||
Error Ip6::ProcessReceiveCallback(Message & aMessage,
|
||||
MessageOrigin aOrigin,
|
||||
const MessageInfo &aMessageInfo,
|
||||
uint8_t aIpProto,
|
||||
bool aFromHost,
|
||||
bool aAllowReceiveFilter,
|
||||
Message::Ownership aMessageOwnership)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
Message *message = &aMessage;
|
||||
|
||||
VerifyOrExit(!aFromHost, error = kErrorNoRoute);
|
||||
VerifyOrExit(aOrigin != kFromHostDisallowLoopBack, error = kErrorNoRoute);
|
||||
|
||||
VerifyOrExit(mReceiveIp6DatagramCallback != nullptr, error = kErrorNoRoute);
|
||||
|
||||
// Do not forward reassembled IPv6 packets.
|
||||
@@ -1121,7 +1120,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Ip6::SendRaw(Message &aMessage, bool aFromHost)
|
||||
Error Ip6::SendRaw(Message &aMessage, bool aAllowLoopBackToHost)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
Header header;
|
||||
@@ -1140,7 +1139,7 @@ Error Ip6::SendRaw(Message &aMessage, bool aFromHost)
|
||||
SuccessOrExit(error = InsertMplOption(aMessage, header, messageInfo));
|
||||
}
|
||||
|
||||
error = HandleDatagram(aMessage, nullptr, nullptr, aFromHost);
|
||||
error = HandleDatagram(aMessage, aAllowLoopBackToHost ? kFromHostAllowLoopBack : kFromHostDisallowLoopBack);
|
||||
freed = true;
|
||||
|
||||
exit:
|
||||
@@ -1153,7 +1152,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Ip6::HandleDatagram(Message &aMessage, Netif *aNetif, const void *aLinkMessageInfo, bool aFromHost)
|
||||
Error Ip6::HandleDatagram(Message &aMessage, MessageOrigin aOrigin, const void *aLinkMessageInfo)
|
||||
{
|
||||
Error error;
|
||||
MessageInfo messageInfo;
|
||||
@@ -1182,7 +1181,7 @@ start:
|
||||
// determine destination of packet
|
||||
if (header.GetDestination().IsMulticast())
|
||||
{
|
||||
if (aNetif != nullptr)
|
||||
if (aOrigin == kFromThreadNetif)
|
||||
{
|
||||
#if OPENTHREAD_FTD
|
||||
if (header.GetDestination().IsMulticastLargerThanRealmLocal() &&
|
||||
@@ -1199,7 +1198,7 @@ start:
|
||||
|
||||
forwardHost = header.GetDestination().IsMulticastLargerThanRealmLocal();
|
||||
|
||||
if ((aNetif != nullptr || aMessage.GetMulticastLoop()) &&
|
||||
if (((aOrigin == kFromThreadNetif) || aMessage.GetMulticastLoop()) &&
|
||||
Get<ThreadNetif>().IsMulticastSubscribed(header.GetDestination()))
|
||||
{
|
||||
receive = true;
|
||||
@@ -1220,12 +1219,12 @@ start:
|
||||
{
|
||||
forwardThread = true;
|
||||
}
|
||||
else if (aNetif == nullptr)
|
||||
else if (aOrigin != kFromThreadNetif)
|
||||
{
|
||||
forwardThread = true;
|
||||
}
|
||||
|
||||
if (forwardThread && !ShouldForwardToThread(messageInfo, aFromHost))
|
||||
if (forwardThread && !ShouldForwardToThread(messageInfo, aOrigin))
|
||||
{
|
||||
forwardThread = false;
|
||||
forwardHost = true;
|
||||
@@ -1236,8 +1235,7 @@ start:
|
||||
|
||||
// process IPv6 Extension Headers
|
||||
nextHeader = static_cast<uint8_t>(header.GetNextHeader());
|
||||
SuccessOrExit(error =
|
||||
HandleExtensionHeaders(aMessage, aNetif, messageInfo, header, nextHeader, aFromHost, receive));
|
||||
SuccessOrExit(error = HandleExtensionHeaders(aMessage, aOrigin, messageInfo, header, nextHeader, receive));
|
||||
|
||||
// process IPv6 Payload
|
||||
if (receive)
|
||||
@@ -1250,7 +1248,7 @@ start:
|
||||
goto start;
|
||||
}
|
||||
|
||||
error = ProcessReceiveCallback(aMessage, messageInfo, nextHeader, aFromHost,
|
||||
error = ProcessReceiveCallback(aMessage, aOrigin, messageInfo, nextHeader,
|
||||
/* aAllowReceiveFilter */ !forwardHost, Message::kCopyToUse);
|
||||
|
||||
if ((error == kErrorNone || error == kErrorNoRoute) && forwardHost)
|
||||
@@ -1266,7 +1264,7 @@ start:
|
||||
if (forwardHost)
|
||||
{
|
||||
// try passing to host
|
||||
error = ProcessReceiveCallback(aMessage, messageInfo, nextHeader, aFromHost, /* aAllowReceiveFilter */ false,
|
||||
error = ProcessReceiveCallback(aMessage, aOrigin, messageInfo, nextHeader, /* aAllowReceiveFilter */ false,
|
||||
forwardThread ? Message::kCopyToUse : Message::kTakeCustody);
|
||||
shouldFreeMessage = forwardThread;
|
||||
}
|
||||
@@ -1275,7 +1273,7 @@ start:
|
||||
{
|
||||
uint8_t hopLimit;
|
||||
|
||||
if (aNetif != nullptr)
|
||||
if (aOrigin == kFromThreadNetif)
|
||||
{
|
||||
VerifyOrExit(mForwardingEnabled);
|
||||
header.SetHopLimit(header.GetHopLimit() - 1);
|
||||
@@ -1304,7 +1302,7 @@ start:
|
||||
}
|
||||
|
||||
#if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
|
||||
if (aFromHost && (nextHeader == kProtoUdp))
|
||||
if ((aOrigin == kFromHostDisallowLoopBack) && (nextHeader == kProtoUdp))
|
||||
{
|
||||
uint16_t destPort;
|
||||
|
||||
@@ -1341,7 +1339,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
bool Ip6::ShouldForwardToThread(const MessageInfo &aMessageInfo, bool aFromHost) const
|
||||
bool Ip6::ShouldForwardToThread(const MessageInfo &aMessageInfo, MessageOrigin aOrigin) const
|
||||
{
|
||||
bool shouldForward = false;
|
||||
|
||||
@@ -1352,10 +1350,10 @@ bool Ip6::ShouldForwardToThread(const MessageInfo &aMessageInfo, bool aFromHost)
|
||||
else if (IsOnLink(aMessageInfo.GetSockAddr()))
|
||||
{
|
||||
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
|
||||
shouldForward =
|
||||
(aFromHost || !Get<BackboneRouter::Manager>().ShouldForwardDuaToBackbone(aMessageInfo.GetSockAddr()));
|
||||
shouldForward = ((aOrigin == kFromHostDisallowLoopBack) ||
|
||||
!Get<BackboneRouter::Manager>().ShouldForwardDuaToBackbone(aMessageInfo.GetSockAddr()));
|
||||
#else
|
||||
OT_UNUSED_VARIABLE(aFromHost);
|
||||
OT_UNUSED_VARIABLE(aOrigin);
|
||||
shouldForward = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
+28
-16
@@ -112,6 +112,20 @@ class Ip6 : public InstanceLocator, private NonCopyable
|
||||
friend class Mpl;
|
||||
|
||||
public:
|
||||
/**
|
||||
* This enumeration represents an IPv6 message origin.
|
||||
*
|
||||
* In case the message is originating from host, it also indicates whether or not it is allowed to passed back the
|
||||
* message to the host.
|
||||
*
|
||||
*/
|
||||
enum MessageOrigin : uint8_t
|
||||
{
|
||||
kFromThreadNetif, ///< Message originates from Thread Netif.
|
||||
kFromHostDisallowLoopBack, ///< Message originates from host and should not be passed back to host.
|
||||
kFromHostAllowLoopBack, ///< Message originates from host and can be passed back to host.
|
||||
};
|
||||
|
||||
/**
|
||||
* This constructor initializes the object.
|
||||
*
|
||||
@@ -187,8 +201,8 @@ public:
|
||||
* The caller transfers ownership of @p aMessage when making this call. OpenThread will free @p aMessage when
|
||||
* processing is complete, including when a value other than `kErrorNone` is returned.
|
||||
*
|
||||
* @param[in] aMessage A reference to the message.
|
||||
* @param[in] aFromHost TRUE if the message is originated from the host, FALSE otherwise.
|
||||
* @param[in] aMessage A reference to the message.
|
||||
* @param[in] aAllowLoopBackToHost Indicate whether or not the message is allowed to be passed back to host.
|
||||
*
|
||||
* @retval kErrorNone Successfully processed the message.
|
||||
* @retval kErrorDrop Message was well-formed but not fully processed due to packet processing rules.
|
||||
@@ -197,15 +211,14 @@ public:
|
||||
* @retval kErrorParse Encountered a malformed header when processing the message.
|
||||
*
|
||||
*/
|
||||
Error SendRaw(Message &aMessage, bool aFromHost);
|
||||
Error SendRaw(Message &aMessage, bool aAllowLoopBackToHost);
|
||||
|
||||
/**
|
||||
* This method processes a received IPv6 datagram.
|
||||
*
|
||||
* @param[in] aMessage A reference to the message.
|
||||
* @param[in] aNetif A pointer to the network interface that received the message.
|
||||
* @param[in] aOrigin The message oirgin.
|
||||
* @param[in] aLinkMessageInfo A pointer to link-specific message information.
|
||||
* @param[in] aFromHost TRUE if the message is originated from the host, FALSE otherwise.
|
||||
*
|
||||
* @retval kErrorNone Successfully processed the message.
|
||||
* @retval kErrorDrop Message was well-formed but not fully processed due to packet processing rules.
|
||||
@@ -214,7 +227,7 @@ public:
|
||||
* @retval kErrorParse Encountered a malformed header when processing the message.
|
||||
*
|
||||
*/
|
||||
Error HandleDatagram(Message &aMessage, Netif *aNetif, const void *aLinkMessageInfo, bool aFromHost);
|
||||
Error HandleDatagram(Message &aMessage, MessageOrigin aOrigin, const void *aLinkMessageInfo = nullptr);
|
||||
|
||||
/**
|
||||
* This method registers a callback to provide received raw IPv6 datagrams.
|
||||
@@ -338,20 +351,19 @@ private:
|
||||
|
||||
void EnqueueDatagram(Message &aMessage);
|
||||
Error ProcessReceiveCallback(Message & aMessage,
|
||||
MessageOrigin aOrigin,
|
||||
const MessageInfo &aMessageInfo,
|
||||
uint8_t aIpProto,
|
||||
bool aFromHost,
|
||||
bool aAllowReceiveFilter,
|
||||
Message::Ownership aMessageOwnership);
|
||||
Error HandleExtensionHeaders(Message & aMessage,
|
||||
Netif * aNetif,
|
||||
MessageInfo &aMessageInfo,
|
||||
Header & aHeader,
|
||||
uint8_t & aNextHeader,
|
||||
bool aFromHost,
|
||||
bool & aReceive);
|
||||
Error HandleExtensionHeaders(Message & aMessage,
|
||||
MessageOrigin aOrigin,
|
||||
MessageInfo & aMessageInfo,
|
||||
Header & aHeader,
|
||||
uint8_t & aNextHeader,
|
||||
bool & aReceive);
|
||||
Error FragmentDatagram(Message &aMessage, uint8_t aIpProto);
|
||||
Error HandleFragment(Message &aMessage, Netif *aNetif, MessageInfo &aMessageInfo, bool aFromHost);
|
||||
Error HandleFragment(Message &aMessage, MessageOrigin aOrigin, MessageInfo &aMessageInfo);
|
||||
#if OPENTHREAD_CONFIG_IP6_FRAGMENTATION_ENABLE
|
||||
void CleanupFragmentationBuffer(void);
|
||||
void HandleTimeTick(void);
|
||||
@@ -368,7 +380,7 @@ private:
|
||||
MessageInfo & aMessageInfo,
|
||||
uint8_t aIpProto,
|
||||
Message::Ownership aMessageOwnership);
|
||||
bool ShouldForwardToThread(const MessageInfo &aMessageInfo, bool aFromHost) const;
|
||||
bool ShouldForwardToThread(const MessageInfo &aMessageInfo, MessageOrigin aOrigin) const;
|
||||
bool IsOnLink(const Address &aAddress) const;
|
||||
|
||||
bool mForwardingEnabled;
|
||||
|
||||
@@ -1708,8 +1708,6 @@ exit:
|
||||
|
||||
Error MeshForwarder::HandleDatagram(Message &aMessage, const ThreadLinkInfo &aLinkInfo, const Mac::Address &aMacSource)
|
||||
{
|
||||
ThreadNetif &netif = Get<ThreadNetif>();
|
||||
|
||||
#if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE
|
||||
Get<Utils::HistoryTracker>().RecordRxMessage(aMessage, aMacSource);
|
||||
#endif
|
||||
@@ -1721,7 +1719,7 @@ Error MeshForwarder::HandleDatagram(Message &aMessage, const ThreadLinkInfo &aLi
|
||||
mIpCounters.mRxSuccess++;
|
||||
}
|
||||
|
||||
return Get<Ip6::Ip6>().HandleDatagram(aMessage, &netif, &aLinkInfo, false);
|
||||
return Get<Ip6::Ip6>().HandleDatagram(aMessage, Ip6::Ip6::kFromThreadNetif, &aLinkInfo);
|
||||
}
|
||||
|
||||
Error MeshForwarder::GetFramePriority(const FrameData & aFrameData,
|
||||
|
||||
@@ -185,7 +185,7 @@ void MeshForwarder::HandleResolved(const Ip6::Address &aEid, Error aError)
|
||||
hopLimit++;
|
||||
message.Write(Ip6::Header::kHopLimitFieldOffset, hopLimit);
|
||||
|
||||
IgnoreError(Get<Ip6::Ip6>().HandleDatagram(message, nullptr, nullptr, /* aFromHost */ false));
|
||||
IgnoreError(Get<Ip6::Ip6>().HandleDatagram(message, Ip6::Ip6::kFromHostAllowLoopBack));
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user