Support DNS64 synthesis using externally-discovered prefixes.
Currently, the DNS resolver supports DNS64 synthesis only for
prefixes that it discovered itself, and not for NAT64 prefixes
discovered via other means (e.g., RA).
Add a way to set a NAT64 prefix that was discovered by other
means. This new IPC is mutually exclusive with the existing
prefix discovery mechansisms:
- Setting the prefix has no effect if prefix discovery is
started.
- Starting (or stopping) prefix discovery clears the prefix.
- Setting the prefix does not result in any NAT64 prefix update
callback.
It is the responsibility of the caller (ConnectivityService,
Nat464Xlat) to ensure that prefix discovery is stopped before
setting the prefix.
This does not add any significant complexity to the connectivity
code, and it ensures that the behaviour of the existing IPCs
(startPrefix64Discovery and stopPrefix64Discovery) are unchanged.
This is necessary to ensure that DNS64 synthesis continues to
work on Q devices.
Disallowing concurrent use of prefix discovery and externally-set
prefixes also simplifies the implementation because it allows
reuse of most of the data structures and teardown code in
Dns64Configuration. The externally-set prefix is represented by a
Dns64Configuration with a special discovery ID of kNoDiscoveryId
(== 0), which cannot be used by any discovery attempt. That way,
if discovery is started, then stopped, and then the prefix is
set, if a stale discovery thread then completes, it will be
ignored because the thread's ID cannot be kNoDiscoveryId.
Bug: 153694684
Bug: 156914456
Test: new tests in resolv_integration_test
Change-Id: I7c63fb62b70635a1b5cc7a21d60f091ba2705d72
diff --git a/Dns64Configuration.cpp b/Dns64Configuration.cpp
index 64c491c..f646d81 100644
--- a/Dns64Configuration.cpp
+++ b/Dns64Configuration.cpp
@@ -214,10 +214,15 @@
}
void Dns64Configuration::removeDns64Config(unsigned netId) REQUIRES(mMutex) {
- IPPrefix prefix = getPrefix64Locked(netId);
- mDns64Configs.erase(netId);
- if (!prefix.isUninitialized()) {
- reportNat64PrefixStatus(netId, PREFIX_REMOVED, prefix);
+ const auto& iter = mDns64Configs.find(netId);
+ if (iter == mDns64Configs.end()) return;
+
+ Dns64Config cfg = iter->second;
+ mDns64Configs.erase(iter);
+
+ // Only report a prefix removed event if the prefix was discovered, not if it was set.
+ if (cfg.isFromPrefixDiscovery() && !cfg.prefix64.isUninitialized()) {
+ reportNat64PrefixStatus(netId, PREFIX_REMOVED, cfg.prefix64);
}
}
@@ -229,9 +234,43 @@
mDns64Configs.emplace(std::make_pair(cfg.netId, cfg));
reportNat64PrefixStatus(cfg.netId, PREFIX_ADDED, cfg.prefix64);
+}
- // TODO: consider extending INetdEventListener to report the DNS64 prefix
- // up to ConnectivityService to potentially include this in LinkProperties.
+int Dns64Configuration::setPrefix64(unsigned netId, const IPPrefix* pfx) {
+ if (pfx->isUninitialized() || pfx->family() != AF_INET6 || pfx->length() != 96) {
+ return -EINVAL;
+ }
+
+ std::lock_guard guard(mMutex);
+
+ // This method may only be called if prefix discovery has been stopped or was never started.
+ auto iter = mDns64Configs.find(netId);
+ if (iter != mDns64Configs.end()) {
+ if (iter->second.isFromPrefixDiscovery()) {
+ return -EEXIST;
+ } else {
+ mDns64Configs.erase(iter);
+ }
+ }
+
+ Dns64Config cfg(kNoDiscoveryId, netId);
+ cfg.prefix64 = *pfx;
+ mDns64Configs.emplace(std::make_pair(netId, cfg));
+
+ return 0;
+}
+
+int Dns64Configuration::clearPrefix64(unsigned netId) {
+ std::lock_guard guard(mMutex);
+
+ const auto& iter = mDns64Configs.find(netId);
+ if (iter == mDns64Configs.end() || iter->second.isFromPrefixDiscovery()) {
+ return -ENOENT;
+ }
+
+ mDns64Configs.erase(iter);
+
+ return 0;
}
} // namespace net