[CLATJ#7] ClatCoordinator: select IPv4 address for clatd

Introduce ClatCoordinator to replace netd/ClatdController.
This is a startup commit. Starting clatd needs to prepare local
464xlat ipv4 address, local 464xlat ipv6 address, tun interface,
clat read/write sockets. These will be implemented from this
and followup commits. This commit implements that prepare local
464xlat ipv4 address.

Bug: 212345928
Test: flash and boot
Run "atest ClatCoordinatorTest" in a followup commit.

Change-Id: I486db8aef0181ee1aedd7cd7702f95d5a1cef812
This commit is contained in:
Hungming Chen
2021-12-25 17:05:41 +08:00
parent 8ff032b990
commit e2cf055590
4 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.connectivity;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.INetd;
import android.net.IpPrefix;
import com.android.internal.annotations.VisibleForTesting;
import java.io.IOException;
/**
* This coordinator is responsible for providing clat relevant functionality.
*
* {@hide}
*/
public class ClatCoordinator {
private static final String TAG = ClatCoordinator.class.getSimpleName();
// For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses
// in 192.0.0.0/29 (RFC 7335).
@VisibleForTesting
static final String INIT_V4ADDR_STRING = "192.0.0.4";
@VisibleForTesting
static final int INIT_V4ADDR_PREFIX_LEN = 29;
private static final int INVALID_PID = 0;
@NonNull
private final INetd mNetd;
@NonNull
private final Dependencies mDeps;
@Nullable
private String mIface = null;
private int mPid = INVALID_PID;
@VisibleForTesting
abstract static class Dependencies {
/**
* Get netd.
*/
@NonNull
public abstract INetd getNetd();
/**
* Pick an IPv4 address for clat.
*/
@NonNull
public String jniSelectIpv4Address(@NonNull String v4addr, int prefixlen)
throws IOException {
return selectIpv4Address(v4addr, prefixlen);
}
}
public ClatCoordinator(@NonNull Dependencies deps) {
mDeps = deps;
mNetd = mDeps.getNetd();
}
/**
* Start clatd for a given interface and NAT64 prefix.
*/
public String clatStart(final String iface, final int netId,
@NonNull final IpPrefix nat64Prefix)
throws IOException {
if (nat64Prefix.getPrefixLength() != 96) {
throw new IOException("Prefix must be 96 bits long: " + nat64Prefix);
}
// [1] Pick an IPv4 address from 192.0.0.4, 192.0.0.5, 192.0.0.6 ..
final String v4;
try {
v4 = mDeps.jniSelectIpv4Address(INIT_V4ADDR_STRING, INIT_V4ADDR_PREFIX_LEN);
} catch (IOException e) {
throw new IOException("no IPv4 addresses were available for clat: " + e);
}
// TODO: start clatd and returns local xlat464 v6 address.
return null;
}
private static native String selectIpv4Address(String v4addr, int prefixlen)
throws IOException;
}