diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp index 84c4cff90f..b37827ea28 100644 --- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp +++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp @@ -209,6 +209,30 @@ static void com_android_server_connectivity_ClatCoordinator_addAnycastSetsockopt } } +static void com_android_server_connectivity_ClatCoordinator_configurePacketSocket( + JNIEnv* env, jobject clazz, jobject javaFd, jstring addr6, jint ifindex) { + ScopedUtfChars addrStr(env, addr6); + + int sock = netjniutils::GetNativeFileDescriptor(env, javaFd); + if (sock < 0) { + jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor"); + return; + } + + in6_addr addr; + if (inet_pton(AF_INET6, addrStr.c_str(), &addr) != 1) { + jniThrowExceptionFmt(env, "java/io/IOException", "Invalid IPv6 address %s", + addrStr.c_str()); + return; + } + + int ret = net::clat::configure_packet_socket(sock, &addr, ifindex); + if (ret < 0) { + throwIOException(env, "configure packet socket failed", -ret); + return; + } +} + /* * JNI registration. */ @@ -229,6 +253,8 @@ static const JNINativeMethod gMethods[] = { (void*)com_android_server_connectivity_ClatCoordinator_openRawSocket6}, {"addAnycastSetsockopt", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V", (void*)com_android_server_connectivity_ClatCoordinator_addAnycastSetsockopt}, + {"configurePacketSocket", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V", + (void*)com_android_server_connectivity_ClatCoordinator_configurePacketSocket}, }; int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) { diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java index 578379d74e..2623c50988 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -154,6 +154,14 @@ public class ClatCoordinator { throws IOException { addAnycastSetsockopt(sock, v6, ifindex); } + + /** + * Configure packet socket. + */ + public void jniConfigurePacketSocket(@NonNull FileDescriptor sock, String v6, int ifindex) + throws IOException { + configurePacketSocket(sock, v6, ifindex); + } } @VisibleForTesting @@ -289,6 +297,13 @@ public class ClatCoordinator { throw new IOException("add anycast sockopt failed: " + e); } + // Update our packet socket filter to reflect the new 464xlat IP address. + try { + mDeps.jniConfigurePacketSocket(readSock6.getFileDescriptor(), v6, ifaceIndex); + } catch (IOException e) { + throw new IOException("configure packet socket failed: " + e); + } + // TODO: start clatd and returns local xlat464 v6 address. return null; } @@ -304,4 +319,6 @@ public class ClatCoordinator { private static native int openRawSocket6(int mark) throws IOException; private static native void addAnycastSetsockopt(FileDescriptor sock, String v6, int ifindex) throws IOException; + private static native void configurePacketSocket(FileDescriptor sock, String v6, int ifindex) + throws IOException; }