[CLATJ#13] ClatCoordinator: open raw socket

Move the raw socket initialization from netd to mainline because
mainline module is going to launch clatd. Need to provide raw
socket for writing local 464xlat IPv6 packets.

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

Change-Id: I6d1da4f1b400eeed87771ae8197b0c58ec50804b
This commit is contained in:
Hungming Chen
2021-12-25 21:05:49 +08:00
parent 79d34befb1
commit d292f45e16
2 changed files with 42 additions and 0 deletions

View File

@@ -26,6 +26,9 @@
#include "libclat/clatutils.h" #include "libclat/clatutils.h"
#include "nativehelper/scoped_utf_chars.h" #include "nativehelper/scoped_utf_chars.h"
// Sync from system/netd/include/netid_client.h
#define MARK_UNSET 0u
namespace android { namespace android {
static void throwIOException(JNIEnv* env, const char* msg, int error) { static void throwIOException(JNIEnv* env, const char* msg, int error) {
jniThrowExceptionFmt(env, "java/io/IOException", "%s: %s", msg, strerror(error)); jniThrowExceptionFmt(env, "java/io/IOException", "%s: %s", msg, strerror(error));
@@ -161,6 +164,25 @@ static jint com_android_server_connectivity_ClatCoordinator_openPacketSocket(JNI
return sock; return sock;
} }
static jint com_android_server_connectivity_ClatCoordinator_openRawSocket6(JNIEnv* env,
jobject clazz,
jint mark) {
int sock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_RAW);
if (sock < 0) {
throwIOException(env, "raw socket failed", errno);
return -1;
}
// TODO: check the mark validation
if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
throwIOException(env, "could not set mark on raw socket", errno);
close(sock);
return -1;
}
return sock;
}
/* /*
* JNI registration. * JNI registration.
*/ */
@@ -177,6 +199,8 @@ static const JNINativeMethod gMethods[] = {
(void*)com_android_server_connectivity_ClatCoordinator_detectMtu}, (void*)com_android_server_connectivity_ClatCoordinator_detectMtu},
{"openPacketSocket", "()I", {"openPacketSocket", "()I",
(void*)com_android_server_connectivity_ClatCoordinator_openPacketSocket}, (void*)com_android_server_connectivity_ClatCoordinator_openPacketSocket},
{"openRawSocket6", "(I)I",
(void*)com_android_server_connectivity_ClatCoordinator_openRawSocket6},
}; };
int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) { int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) {

View File

@@ -128,6 +128,13 @@ public class ClatCoordinator {
public int jniOpenPacketSocket() throws IOException { public int jniOpenPacketSocket() throws IOException {
return openPacketSocket(); return openPacketSocket();
} }
/**
* Open IPv6 raw socket and set SO_MARK.
*/
public int jniOpenRawSocket6(int mark) throws IOException {
return openRawSocket6(mark);
}
} }
@VisibleForTesting @VisibleForTesting
@@ -241,6 +248,16 @@ public class ClatCoordinator {
throw new IOException("Open packet socket failed: " + e); throw new IOException("Open packet socket failed: " + e);
} }
// Opens a raw socket with a given fwmark to send IPv6 packets in clatd.
final ParcelFileDescriptor writeSock6;
try {
// Use a JNI call to get native file descriptor instead of Os.socket(). See above
// reason why we use jniOpenPacketSocket6().
writeSock6 = mDeps.adoptFd(mDeps.jniOpenRawSocket6(fwmark));
} catch (IOException e) {
throw new IOException("Open raw socket failed: " + e);
}
// TODO: start clatd and returns local xlat464 v6 address. // TODO: start clatd and returns local xlat464 v6 address.
return null; return null;
} }
@@ -253,4 +270,5 @@ public class ClatCoordinator {
private static native int detectMtu(String platSubnet, int platSuffix, int mark) private static native int detectMtu(String platSubnet, int platSuffix, int mark)
throws IOException; throws IOException;
private static native int openPacketSocket() throws IOException; private static native int openPacketSocket() throws IOException;
private static native int openRawSocket6(int mark) throws IOException;
} }