[posix] add unified platform API for setting NAT64 CIDR during runtime (#8947)

We should notice that the default CIDR (`192.168.255.0/24`) is not
safe to use, however, in most cases, the CIDR used can only be
determined during runtime since the single binary might be distributed
to BRs in different network conditions. And we cannot conclude an
always safe CIDR in all conditions.

This commit will emit an event when NAT64 CIDR is changed. So the
platform driver can update the route for NAT64.
This commit is contained in:
Song GUO
2023-05-30 23:16:43 -07:00
committed by GitHub
parent fa81b21f4c
commit 9c7e679ed1
8 changed files with 145 additions and 52 deletions
@@ -63,6 +63,9 @@ SMALL_NAT64_PREFIX = "fd00:00:00:01:00:00::/96"
# So the BR will remove the random-generated one.
LARGE_NAT64_PREFIX = "ff00:00:00:01:00:00::/96"
DEFAULT_NAT64_CIDR = ipaddress.IPv4Network("192.168.255.0/24")
UPDATED_NAT64_CIDR = ipaddress.IPv4Network("100.64.64.0/24")
class Nat64SingleBorderRouter(thread_cert.TestCase):
USE_MESSAGE_FACTORY = False
@@ -235,7 +238,16 @@ class Nat64SingleBorderRouter(thread_cert.TestCase):
self.assertEqual(counters['protocol']['ICMP']['6to4']['packets'], 2)
#
# Case 6. Disable and re-enable ethernet on the border router.
# Case 6. Change the CIDR used by NAT64 during runtime
#
self.assertEqual(br.nat64_cidr, DEFAULT_NAT64_CIDR)
br.nat64_cidr = UPDATED_NAT64_CIDR
self.assertEqual(br.nat64_cidr, UPDATED_NAT64_CIDR)
self.assertTrue(router.ping(ipaddr=host_ip))
#
# Case 7. Disable and re-enable ethernet on the border router.
# Note: disable_ether will remove default route but enable_ether won't add it back,
# NAT64 connectivity tests will fail after this.
# TODO: Add a default IPv4 route after enable_ether.
+13
View File
@@ -413,6 +413,19 @@ class OtbrDocker:
def nat64_set_enabled(self, enable):
return self.call_dbus_method('io.openthread.BorderRouter', 'SetNat64Enabled', enable)
@property
def nat64_cidr(self):
self.send_command('nat64 cidr')
cidr = self._expect_command_output()[0].strip()
return ipaddress.IPv4Network(cidr, strict=False)
@nat64_cidr.setter
def nat64_cidr(self, cidr: ipaddress.IPv4Network):
if not isinstance(cidr, ipaddress.IPv4Network):
raise ValueError("cidr is expected to be an instance of ipaddress.IPv4Network")
self.send_command(f'nat64 cidr {cidr}')
self._expect_done()
@property
def nat64_state(self):
state = self.get_dbus_property('Nat64State')