[CLATJ#15] ClatCoordinator: add socket filter to packet socket

Update our packet socket filter to reflect the new 464xlat IP address

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

Change-Id: Ic50dc122731f311ad00ab8bff5472cb3bc41f5f1
diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp
index 84c4cff..b37827e 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_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 @@
          (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 578379d..2623c50 100644
--- a/service/src/com/android/server/connectivity/ClatCoordinator.java
+++ b/service/src/com/android/server/connectivity/ClatCoordinator.java
@@ -154,6 +154,14 @@
                 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 @@
             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 @@
     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;
 }