Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d0a17ee9 authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

Move socket tagging implementation to mainline.

Currently, socket tagging is implemented in the framework. The
Java code is in NetworkManagmentSocketTagger.java and the JNI is
in libandroid and loaded by the general framework JNI loader.

- Move the Java implementation to TrafficStats, which is the only
  caller of NetworkManagmentSocketTagger. This simplifies the
  code a bit because a number of methods can be deleted.

- Move the JNI code to a new JNI library in the APEX. The library
  depends only on the NDK and statically links the NDK-only
  version of libnativehelper. Its size is only 5k on ARM and 10k
  on ARM64.

- Temporarily make the framework depend on this library until the
  rest of the T connectivity code moves to the APEX.

Test: atest NetworkUsageStatsTest CtsNetTestCases:TrafficStatsTest
Change-Id: I050c7c515237f68b78d08987bc443f50a7949c06
parent 09d2a4b1
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -195,7 +195,6 @@ cc_library_shared {
                "android_util_FileObserver.cpp",
                "android/opengl/poly_clip.cpp", // TODO: .arm
                "android/opengl/util.cpp",
                "android_server_NetworkManagementSocketTagger.cpp",
                "android_ddm_DdmHandleNativeHeap.cpp",
                "android_backup_BackupDataInput.cpp",
                "android_backup_BackupDataOutput.cpp",
@@ -299,6 +298,8 @@ cc_library_shared {
                "libdl_android",
                "libtimeinstate",
                "server_configurable_flags",
                // TODO: delete when ConnectivityT moves to APEX.
                "libframework-connectivity-tiramisu-jni",
            ],
            export_shared_lib_headers: [
                // our headers include libnativewindow's public headers
+0 −2
Original line number Diff line number Diff line
@@ -164,7 +164,6 @@ extern int register_android_text_AndroidCharacter(JNIEnv *env);
extern int register_android_text_Hyphenator(JNIEnv *env);
extern int register_android_opengl_classes(JNIEnv *env);
extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
extern int register_android_backup_BackupDataInput(JNIEnv *env);
extern int register_android_backup_BackupDataOutput(JNIEnv *env);
extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
@@ -1618,7 +1617,6 @@ static const RegJNIRec gRegJNI[] = {
        REG_JNI(register_android_media_midi),

        REG_JNI(register_android_opengl_classes),
        REG_JNI(register_android_server_NetworkManagementSocketTagger),
        REG_JNI(register_android_ddm_DdmHandleNativeHeap),
        REG_JNI(register_android_backup_BackupDataInput),
        REG_JNI(register_android_backup_BackupDataOutput),
+31 −1
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ filegroup {
        "src/android/net/TrafficStats.java",
        "src/android/net/UnderlyingNetworkInfo.*",
        "src/android/net/netstats/**/*.*",
        "src/com/android/server/NetworkManagementSocketTagger.java",
    ],
    path: "src",
    visibility: [
@@ -176,3 +175,34 @@ filegroup {
        "//packages/modules/Connectivity:__subpackages__",
    ],
}

cc_library_shared {
    name: "libframework-connectivity-tiramisu-jni",
    min_sdk_version: "30",
    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-unused-parameter",
        // Don't warn about S API usage even with
        // min_sdk 30: the library is only loaded
        // on S+ devices
        "-Wno-unguarded-availability",
        "-Wthread-safety",
    ],
    srcs: [
        "jni/android_net_TrafficStats.cpp",
        "jni/onload.cpp",
    ],
    shared_libs: [
        "liblog",
    ],
    static_libs: [
        "libnativehelper_compat_libc++",
    ],
    stl: "none",
    apex_available: [
        "com.android.tethering",
        // TODO: remove when ConnectivityT moves to APEX.
        "//apex_available:platform",
    ],
}
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright 2011, The Android Open Source Project
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -14,60 +14,33 @@
 * limitations under the License.
 */

#define LOG_TAG "NMST_QTagUidNative"

#include <android/file_descriptor_jni.h>
#include <android/multinetwork.h>
#include <cutils/qtaguid.h>
#include <errno.h>
#include <fcntl.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <utils/Log.h>
#include <utils/misc.h>

#include "jni.h"
#include <nativehelper/JNIHelp.h>

namespace android {

static jint tagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor,
                        jint tagNum, jint uid) {
  int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);

  if (env->ExceptionCheck()) {
    ALOGE("Can't get FileDescriptor num");
    return (jint)-1;
  }

  int res = android_tag_socket_with_uid(userFd, tagNum, uid);
  if (res < 0) {
    return (jint)-errno;
  }
  return (jint)res;
static jint tagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor, jint tag, jint uid) {
  int fd = AFileDescriptor_getFd(env, fileDescriptor);
  if (fd == -1) return -EBADF;
  return android_tag_socket_with_uid(fd, tag, uid);
}

static jint untagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor) {
  int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);

  if (env->ExceptionCheck()) {
    ALOGE("Can't get FileDescriptor num");
    return (jint)-1;
  }

  int res = android_untag_socket(userFd);
  if (res < 0) {
    return (jint)-errno;
  }
  return (jint)res;
  int fd = AFileDescriptor_getFd(env, fileDescriptor);
  if (fd == -1) return -EBADF;
  return android_untag_socket(fd);
}

static const JNINativeMethod gQTagUidMethods[] = {
static const JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "native_tagSocketFd", "(Ljava/io/FileDescriptor;II)I", (void*) tagSocketFd },
    { "native_untagSocketFd", "(Ljava/io/FileDescriptor;)I", (void*) untagSocketFd },
};

int register_android_server_NetworkManagementSocketTagger(JNIEnv* env) {
  return jniRegisterNativeMethods(env, "com/android/server/NetworkManagementSocketTagger", gQTagUidMethods, NELEM(gQTagUidMethods));
int register_android_net_TrafficStats(JNIEnv* env) {
    return jniRegisterNativeMethods(env, "android/net/TrafficStats", gMethods, NELEM(gMethods));
}

};
};  // namespace android
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "FrameworkConnectivityJNI"

#include <log/log.h>
#include <nativehelper/JNIHelp.h>

namespace android {

int register_android_net_TrafficStats(JNIEnv* env);

extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
    JNIEnv *env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: GetEnv failed");
        return JNI_ERR;
    }

    if (register_android_net_TrafficStats(env) < 0) return JNI_ERR;

    return JNI_VERSION_1_6;
}

};  // namespace android
Loading