diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp index 0272bc14b2..9603139734 100644 --- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp +++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp @@ -303,6 +303,25 @@ static jint com_android_server_connectivity_ClatCoordinator_maybeStartBpf( return 0; // TODO: return forked clatd pid. } +// TODO: stop clatd and rename to .._stopClatd. +static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv* env, jobject clazz, + jstring iface, jstring pfx96, + jstring v4, jstring v6, + jint pid /* unused */) { + ScopedUtfChars ifaceStr(env, iface); + ScopedUtfChars pfx96Str(env, pfx96); + ScopedUtfChars v4Str(env, v4); + ScopedUtfChars v6Str(env, v6); + + if (!net::clat::initMaps()) { + net::clat::ClatdTracker tracker = {}; + if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(), + &tracker)) { + net::clat::maybeStopBpf(tracker); + } + } +} + /* * JNI registration. */ @@ -329,6 +348,9 @@ static const JNINativeMethod gMethods[] = { "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/lang/" "String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", (void*)com_android_server_connectivity_ClatCoordinator_maybeStartBpf}, + {"native_maybeStopBpf", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V", + (void*)com_android_server_connectivity_ClatCoordinator_maybeStopBpf}, }; 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 a7859745a8..5a5e24a99d 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -74,6 +74,12 @@ public class ClatCoordinator { private final Dependencies mDeps; @Nullable private String mIface = null; + @Nullable + private String mNat64Prefix = null; + @Nullable + private String mXlatLocalAddress4 = null; + @Nullable + private String mXlatLocalAddress6 = null; private int mPid = INVALID_PID; @VisibleForTesting @@ -171,6 +177,14 @@ public class ClatCoordinator { @NonNull String v4, @NonNull String v6) throws IOException { return native_maybeStartBpf(tunfd, readsock6, writesock6, iface, pfx96, v4, v6); } + + /** + * Maybe stop bpf. + */ + public void maybeStopBpf(String iface, String pfx96, String v4, String v6, int pid) + throws IOException { + native_maybeStopBpf(iface, pfx96, v4, v6, pid); + } } @VisibleForTesting @@ -317,6 +331,10 @@ public class ClatCoordinator { try { mDeps.maybeStartBpf(tunFd.getFileDescriptor(), readSock6.getFileDescriptor(), writeSock6.getFileDescriptor(), iface, pfx96, v4, v6); + mIface = iface; + mNat64Prefix = pfx96; + mXlatLocalAddress4 = v4; + mXlatLocalAddress6 = v6; } catch (IOException e) { throw new IOException("Error start bpf on " + iface + ": " + e); } @@ -325,6 +343,22 @@ public class ClatCoordinator { return null; } + /** + * Stop clatd + */ + public void clatStop() throws IOException { + mDeps.maybeStopBpf(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, + mPid /* unused */); + // TODO: remove setIptablesDropRule + + Log.i(TAG, "clatd on " + mIface + " stopped"); + + mIface = null; + mNat64Prefix = null; + mXlatLocalAddress4 = null; + mXlatLocalAddress6 = null; + } + private static native String native_selectIpv4Address(String v4addr, int prefixlen) throws IOException; private static native String native_generateIpv6Address(String iface, String v4, @@ -341,4 +375,6 @@ public class ClatCoordinator { private static native int native_maybeStartBpf(FileDescriptor tunfd, FileDescriptor readsock6, FileDescriptor writesock6, String iface, String pfx96, String v4, String v6) throws IOException; + private static native void native_maybeStopBpf(String iface, String pfx96, String v4, + String v6, int pid) throws IOException; }