[nat64] ensure translator is active before translation (#11894)

This change adds an explicit check for `mState == kStateActive` at the
beginning of the translation functions.

This single check replaces individual validations for a valid IPv4
CIDR and NAT64 prefix. This simplifies the entry logic and fixes a
bug where the `Translator` could continue performing translations
even after it was explicitly disabled via `SetEnabled(false)`.

Additionally, this change fixes a bug in `TranslateToIp6()` where an
IPv4 message would be incorrectly marked as `kForward` when no IPv4
CIDR was configured (`mIp4Cidr.mLength == 0`). The correct behavior
is to drop the packet, so the result is now set to `kDrop`.
This commit is contained in:
Abtin Keshavarzian
2025-09-04 10:53:44 -07:00
committed by GitHub
parent 2421ba9a98
commit 5ac9ccfaed
2 changed files with 3 additions and 16 deletions
+2 -16
View File
@@ -121,10 +121,7 @@ Translator::Result Translator::TranslateFromIp6(Message &aMessage)
uint16_t srcPortOrId = 0;
Mapping *mapping = nullptr;
if (mIp4Cidr.mLength == 0 || !mNat64Prefix.IsValidNat64())
{
ExitNow(result = kNotTranslated);
}
VerifyOrExit(mState == kStateActive, result = kNotTranslated);
// `ParseFrom()` will do basic checks for the message, including
// the message length and IP protocol version.
@@ -241,21 +238,10 @@ Translator::Result Translator::TranslateToIp6(Message &aMessage)
// datagram, forward it directly.
VerifyOrExit(ip6Header.ParseFrom(aMessage) != kErrorNone, result = kNotTranslated);
if (mIp4Cidr.mLength == 0)
{
LogWarn("Incoming message is an IPv4 datagram but no IPv4 CIDR for NAT64 configured, drop");
ExitNow(result = kForward);
}
if (!mNat64Prefix.IsValidNat64())
{
LogWarn("Incoming message is an IPv4 datagram but no NAT64 prefix configured, drop");
ExitNow(result = kDrop);
}
VerifyOrExit(mState == kStateActive, result = kDrop);
if (ip4Headers.ParseFrom(aMessage) != kErrorNone)
{
LogWarn("Incoming message is neither IPv4 nor an IPv6 datagram, drop");
dropReason = kReasonIllegalPacket;
ExitNow(result = kDrop);
}
+1
View File
@@ -236,6 +236,7 @@ void TestNat64Translation(void)
SuccessOrQuit(sInstance->Get<Translator>().SetIp4Cidr(cidr));
sInstance->Get<Translator>().SetNat64Prefix(prefix);
sInstance->Get<Translator>().SetEnabled(true);
{
// fd02::1 fd01::ac10:f3c5 UDP 52 43981 → 4660 Len=4