Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd0d47e2 authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Automerger Merge Worker
Browse files

Support DNS64 synthesis using externally-discovered prefixes. am: 346737c0

Change-Id: Ied66c009438fcd1343547657d3f77b11a47d0c70
parents 0434e4fe 346737c0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ cc_library {
    // Link most things statically to minimize our dependence on system ABIs.
    stl: "libc++_static",
    static_libs: [
        "dnsresolver_aidl_interface-ndk_platform",
        "dnsresolver_aidl_interface-unstable-ndk_platform",
        "libbase",
        "libcutils",
        "libnetdutils",
+45 −6
Original line number Diff line number Diff line
@@ -216,10 +216,15 @@ bool Dns64Configuration::shouldContinueDiscovery(const Dns64Config& cfg) {
}

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);
    }
}

@@ -231,9 +236,43 @@ void Dns64Configuration::recordDns64Config(const Dns64Config& cfg) {
    mDns64Configs.emplace(std::make_pair(cfg.netId, cfg));

    reportNat64PrefixStatus(cfg.netId, PREFIX_ADDED, cfg.prefix64);
}

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);

    // TODO: consider extending INetdEventListener to report the DNS64 prefix
    // up to ConnectivityService to potentially include this in LinkProperties.
    return 0;
}

}  // namespace net
+12 −1
Original line number Diff line number Diff line
@@ -82,6 +82,9 @@ class Dns64Configuration {
    void stopPrefixDiscovery(unsigned netId);
    netdutils::IPPrefix getPrefix64(unsigned netId) const;

    int setPrefix64(unsigned netId, const netdutils::IPPrefix* pfx);
    int clearPrefix64(unsigned netId);

    void dump(netdutils::DumpWriter& dw, unsigned netId);

  private:
@@ -89,16 +92,24 @@ class Dns64Configuration {
        Dns64Config(unsigned pseudoRandomId, unsigned network)
            : discoveryId(pseudoRandomId), netId(network) {}

        // ID of the discovery operation, or kNoDiscoveryId if no discovery was performed (i.e., the
        // prefix was discovered and passed in via setPrefix64).
        const unsigned int discoveryId;
        const unsigned int netId;
        netdutils::IPPrefix prefix64{};

        bool isFromPrefixDiscovery() const { return discoveryId != kNoDiscoveryId; }
    };

    static constexpr int kNoDiscoveryId = 0;

    enum { PREFIX_REMOVED, PREFIX_ADDED };

    static bool doRfc7050PrefixDiscovery(const android_net_context& netcontext, Dns64Config* cfg);

    unsigned getNextId() REQUIRES(mMutex) { return mNextId++; }
    // Picks the next discovery ID. Never returns kNoDiscoveryId.
    unsigned getNextId() REQUIRES(mMutex) { return ++mNextId ? mNextId : ++mNextId; }

    netdutils::IPPrefix getPrefix64Locked(unsigned netId) const REQUIRES(mMutex);
    bool isDiscoveryInProgress(const Dns64Config& cfg) const REQUIRES(mMutex);
    bool reportNat64PrefixStatus(unsigned netId, bool added, const netdutils::IPPrefix& pfx)
+16 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ using aidl::android::net::ResolverParamsParcel;
using android::base::Join;
using android::base::StringPrintf;
using android::netdutils::DumpWriter;
using android::netdutils::IPPrefix;

namespace android {
namespace net {
@@ -240,6 +241,21 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num
    return statusFromErrcode(res);
}

::ndk::ScopedAStatus DnsResolverService::setPrefix64(int netId, const std::string& stringPrefix) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();

    if (stringPrefix.empty()) {
        return statusFromErrcode(gDnsResolv->resolverCtrl.clearPrefix64(netId));
    }

    IPPrefix prefix;
    if (!IPPrefix::forString(stringPrefix, &prefix)) {
        return statusFromErrcode(-EINVAL);
    }

    return statusFromErrcode(gDnsResolv->resolverCtrl.setPrefix64(netId, &prefix));
}

::ndk::ScopedAStatus DnsResolverService::setLogSeverity(int32_t logSeverity) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();

+2 −1
Original line number Diff line number Diff line
@@ -56,7 +56,8 @@ class DnsResolverService : public aidl::android::net::BnDnsResolver {
    ::ndk::ScopedAStatus startPrefix64Discovery(int32_t netId) override;
    ::ndk::ScopedAStatus stopPrefix64Discovery(int32_t netId) override;
    // (internal use only)
    ::ndk::ScopedAStatus getPrefix64(int netId, std::string* stringPrefix) override;
    ::ndk::ScopedAStatus getPrefix64(int32_t netId, std::string* stringPrefix) override;
    ::ndk::ScopedAStatus setPrefix64(int32_t netId, const std::string& stringPrefix) override;

    // Debug log command
    ::ndk::ScopedAStatus setLogSeverity(int32_t logSeverity) override;
Loading