[Thread] use NetlinkUtils methods to update addresses

The helper methods makes the code simpler.

Test: atest ThreadNetworkIntegrationTests
Change-Id: Icf5aadc446ebba8707b45cd878222c826261815f
This commit is contained in:
Kangping Dong 2024-04-08 22:21:59 +08:00
commit 0aeee52ac1

View file

@ -23,23 +23,18 @@ import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.RouteInfo;
import android.net.util.SocketUtils;
import android.os.ParcelFileDescriptor;
import android.os.SystemClock;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Log;
import com.android.net.module.util.LinkPropertiesUtils.CompareResult;
import com.android.net.module.util.netlink.NetlinkUtils;
import com.android.net.module.util.netlink.RtNetlinkAddressMessage;
import com.android.server.thread.openthread.Ipv6AddressInfo;
import com.android.server.thread.openthread.OnMeshPrefixConfig;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -62,14 +57,13 @@ public class TunInterfaceController {
private final String mIfName;
private final LinkProperties mLinkProperties = new LinkProperties();
private ParcelFileDescriptor mParcelTunFd;
private FileDescriptor mNetlinkSocket;
private static int sNetlinkSeqNo = 0;
private final MulticastSocket mMulticastSocket; // For join group and leave group
private NetworkInterface mNetworkInterface;
private final List<InetAddress> mMulticastAddresses = new ArrayList<>();
private final List<RouteInfo> mNetDataPrefixes = new ArrayList<>();
private ParcelFileDescriptor mParcelTunFd;
private NetworkInterface mNetworkInterface;
/** Creates a new {@link TunInterfaceController} instance for given interface. */
public TunInterfaceController(String interfaceName) {
mIfName = interfaceName;
@ -90,11 +84,6 @@ public class TunInterfaceController {
*/
public void createTunInterface() throws IOException {
mParcelTunFd = ParcelFileDescriptor.adoptFd(nativeCreateTunInterface(mIfName, MTU));
try {
mNetlinkSocket = NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_ROUTE);
} catch (ErrnoException e) {
throw new IOException("Failed to create netlink socket", e);
}
try {
mNetworkInterface = NetworkInterface.getByName(mIfName);
} catch (SocketException e) {
@ -105,12 +94,10 @@ public class TunInterfaceController {
public void destroyTunInterface() {
try {
mParcelTunFd.close();
SocketUtils.closeSocket(mNetlinkSocket);
} catch (IOException e) {
// Should never fail
}
mParcelTunFd = null;
mNetlinkSocket = null;
mNetworkInterface = null;
}
@ -138,14 +125,14 @@ public class TunInterfaceController {
public void addAddress(LinkAddress address) {
Log.d(TAG, "Adding address " + address + " with flags: " + address.getFlags());
long validLifetimeSeconds;
long preferredLifetimeSeconds;
long validLifetimeSeconds;
if (address.getDeprecationTime() == LinkAddress.LIFETIME_PERMANENT
|| address.getDeprecationTime() == LinkAddress.LIFETIME_UNKNOWN) {
validLifetimeSeconds = INFINITE_LIFETIME;
preferredLifetimeSeconds = INFINITE_LIFETIME;
} else {
validLifetimeSeconds =
preferredLifetimeSeconds =
Math.max(
(address.getDeprecationTime() - SystemClock.elapsedRealtime()) / 1000L,
0L);
@ -153,28 +140,23 @@ public class TunInterfaceController {
if (address.getExpirationTime() == LinkAddress.LIFETIME_PERMANENT
|| address.getExpirationTime() == LinkAddress.LIFETIME_UNKNOWN) {
preferredLifetimeSeconds = INFINITE_LIFETIME;
validLifetimeSeconds = INFINITE_LIFETIME;
} else {
preferredLifetimeSeconds =
validLifetimeSeconds =
Math.max(
(address.getExpirationTime() - SystemClock.elapsedRealtime()) / 1000L,
0L);
}
byte[] message =
RtNetlinkAddressMessage.newRtmNewAddressMessage(
sNetlinkSeqNo++,
address.getAddress(),
(short) address.getPrefixLength(),
address.getFlags(),
(byte) address.getScope(),
Os.if_nametoindex(mIfName),
validLifetimeSeconds,
preferredLifetimeSeconds);
try {
Os.write(mNetlinkSocket, message, 0, message.length);
} catch (ErrnoException | InterruptedIOException e) {
Log.e(TAG, "Failed to add address " + address, e);
if (!NetlinkUtils.sendRtmNewAddressRequest(
Os.if_nametoindex(mIfName),
address.getAddress(),
(short) address.getPrefixLength(),
address.getFlags(),
(byte) address.getScope(),
preferredLifetimeSeconds,
validLifetimeSeconds)) {
Log.w(TAG, "Failed to add address " + address.getAddress().getHostAddress());
return;
}
mLinkProperties.addLinkAddress(address);
@ -184,22 +166,17 @@ public class TunInterfaceController {
/** Removes an address from the interface. */
public void removeAddress(LinkAddress address) {
Log.d(TAG, "Removing address " + address);
byte[] message =
RtNetlinkAddressMessage.newRtmDelAddressMessage(
sNetlinkSeqNo++,
address.getAddress(),
(short) address.getPrefixLength(),
Os.if_nametoindex(mIfName));
// Intentionally update the mLinkProperties before send netlink message because the
// address is already removed from ot-daemon and apps can't reach to the address even
// when the netlink request below fails
mLinkProperties.removeLinkAddress(address);
mLinkProperties.removeRoute(getRouteForAddress(address));
try {
Os.write(mNetlinkSocket, message, 0, message.length);
} catch (ErrnoException | InterruptedIOException e) {
Log.e(TAG, "Failed to remove address " + address, e);
if (!NetlinkUtils.sendRtmDelAddressRequest(
Os.if_nametoindex(mIfName),
(Inet6Address) address.getAddress(),
(short) address.getPrefixLength())) {
Log.w(TAG, "Failed to remove address " + address.getAddress().getHostAddress());
}
}