diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp index 3c8fd0de83..eeaad186b3 100644 --- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp +++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -425,16 +426,20 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd( return 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 */) { +static void com_android_server_connectivity_ClatCoordinator_stopClatd(JNIEnv* env, jobject clazz, + jstring iface, jstring pfx96, + jstring v4, jstring v6, + jint pid) { ScopedUtfChars ifaceStr(env, iface); ScopedUtfChars pfx96Str(env, pfx96); ScopedUtfChars v4Str(env, v4); ScopedUtfChars v6Str(env, v6); + if (pid <= 0) { + jniThrowExceptionFmt(env, "java/io/IOException", "Invalid pid"); + return; + } + if (!net::clat::initMaps()) { net::clat::ClatdTracker tracker = {}; if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(), @@ -442,6 +447,9 @@ static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv* net::clat::maybeStopBpf(tracker); } } + + kill(pid, SIGTERM); + waitpid(pid, nullptr, 0); // Should we block in JNI? } /* @@ -470,9 +478,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_startClatd}, - {"native_maybeStopBpf", + {"native_stopClatd", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V", - (void*)com_android_server_connectivity_ClatCoordinator_maybeStopBpf}, + (void*)com_android_server_connectivity_ClatCoordinator_stopClatd}, }; 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 45e8125699..9cdc08619a 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -179,11 +179,11 @@ public class ClatCoordinator { } /** - * Maybe stop bpf. + * Stop clatd. */ - public void maybeStopBpf(String iface, String pfx96, String v4, String v6, int pid) + public void stopClatd(String iface, String pfx96, String v4, String v6, int pid) throws IOException { - native_maybeStopBpf(iface, pfx96, v4, v6, pid); + native_stopClatd(iface, pfx96, v4, v6, pid); } } @@ -349,8 +349,12 @@ public class ClatCoordinator { * Stop clatd */ public void clatStop() throws IOException { - mDeps.maybeStopBpf(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, - mPid /* unused */); + if (mPid == INVALID_PID) { + throw new IOException("Clatd has not started"); + } + Log.i(TAG, "Stopping clatd pid=" + mPid + " on " + mIface); + + mDeps.stopClatd(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, mPid); // TODO: remove setIptablesDropRule Log.i(TAG, "clatd on " + mIface + " stopped"); @@ -359,6 +363,7 @@ public class ClatCoordinator { mNat64Prefix = null; mXlatLocalAddress4 = null; mXlatLocalAddress6 = null; + mPid = INVALID_PID; } private static native String native_selectIpv4Address(String v4addr, int prefixlen) @@ -377,6 +382,6 @@ public class ClatCoordinator { private static native int native_startClatd(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; + private static native void native_stopClatd(String iface, String pfx96, String v4, String v6, + int pid) throws IOException; }