1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/unique_fd.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <jni.h>
21 #include <nativehelper/JNIHelp.h>
22 #include <nativehelper/ScopedLocalRef.h>
23 #include <nativehelper/scoped_utf_chars.h>
24 #include <string.h>
25 #include <sys/socket.h>
26
27 #include "BpfSyscallWrappers.h"
28
29 namespace android {
30
31 using base::unique_fd;
32
33 // If attach fails throw error and return false.
com_android_net_module_util_BpfUtil_attachProgramToCgroup(JNIEnv * env,jobject clazz,jint type,jstring bpfProgPath,jstring cgroupPath,jint flags)34 static jboolean com_android_net_module_util_BpfUtil_attachProgramToCgroup(JNIEnv *env,
35 jobject clazz, jint type, jstring bpfProgPath, jstring cgroupPath, jint flags) {
36
37 ScopedUtfChars dirPath(env, cgroupPath);
38 unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
39 if (cg_fd == -1) {
40 jniThrowExceptionFmt(env, "java/io/IOException",
41 "Failed to open the cgroup directory %s: %s",
42 dirPath.c_str(), strerror(errno));
43 return false;
44 }
45
46 ScopedUtfChars bpfProg(env, bpfProgPath);
47 unique_fd bpf_fd(bpf::retrieveProgram(bpfProg.c_str()));
48 if (bpf_fd == -1) {
49 jniThrowExceptionFmt(env, "java/io/IOException",
50 "Failed to retrieve bpf program from %s: %s",
51 bpfProg.c_str(), strerror(errno));
52 return false;
53 }
54 if (bpf::attachProgram((bpf_attach_type) type, bpf_fd, cg_fd, flags)) {
55 jniThrowExceptionFmt(env, "java/io/IOException",
56 "Failed to attach bpf program %s to %s: %s",
57 bpfProg.c_str(), dirPath.c_str(), strerror(errno));
58 return false;
59 }
60 return true;
61 }
62
63 // If detach fails throw error and return false.
com_android_net_module_util_BpfUtil_detachProgramFromCgroup(JNIEnv * env,jobject clazz,jint type,jstring cgroupPath)64 static jboolean com_android_net_module_util_BpfUtil_detachProgramFromCgroup(JNIEnv *env,
65 jobject clazz, jint type, jstring cgroupPath) {
66
67 ScopedUtfChars dirPath(env, cgroupPath);
68 unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
69 if (cg_fd == -1) {
70 jniThrowExceptionFmt(env, "java/io/IOException",
71 "Failed to open the cgroup directory %s: %s",
72 dirPath.c_str(), strerror(errno));
73 return false;
74 }
75
76 if (bpf::detachProgram((bpf_attach_type) type, cg_fd)) {
77 jniThrowExceptionFmt(env, "Failed to detach bpf program from %s: %s",
78 dirPath.c_str(), strerror(errno));
79 return false;
80 }
81 return true;
82 }
83
84 // If detach single program fails throw error and return false.
com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup(JNIEnv * env,jobject clazz,jint type,jstring bpfProgPath,jstring cgroupPath)85 static jboolean com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup(JNIEnv *env,
86 jobject clazz, jint type, jstring bpfProgPath, jstring cgroupPath) {
87
88 ScopedUtfChars dirPath(env, cgroupPath);
89 unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
90 if (cg_fd == -1) {
91 jniThrowExceptionFmt(env, "java/io/IOException",
92 "Failed to open the cgroup directory %s: %s",
93 dirPath.c_str(), strerror(errno));
94 return false;
95 }
96
97 ScopedUtfChars bpfProg(env, bpfProgPath);
98 unique_fd bpf_fd(bpf::retrieveProgram(bpfProg.c_str()));
99 if (bpf_fd == -1) {
100 jniThrowExceptionFmt(env, "java/io/IOException",
101 "Failed to retrieve bpf program from %s: %s",
102 bpfProg.c_str(), strerror(errno));
103 return false;
104 }
105 if (bpf::detachSingleProgram((bpf_attach_type) type, bpf_fd, cg_fd)) {
106 jniThrowExceptionFmt(env, "Failed to detach bpf program %s from %s: %s",
107 bpfProg.c_str(), dirPath.c_str(), strerror(errno));
108 return false;
109 }
110 return true;
111 }
112
113 /*
114 * JNI registration.
115 */
116 static const JNINativeMethod gMethods[] = {
117 /* name, signature, funcPtr */
118 { "native_attachProgramToCgroup", "(ILjava/lang/String;Ljava/lang/String;I)Z",
119 (void*) com_android_net_module_util_BpfUtil_attachProgramToCgroup },
120 { "native_detachProgramFromCgroup", "(ILjava/lang/String;)Z",
121 (void*) com_android_net_module_util_BpfUtil_detachProgramFromCgroup },
122 { "native_detachSingleProgramFromCgroup", "(ILjava/lang/String;Ljava/lang/String;)Z",
123 (void*) com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup },
124 };
125
register_com_android_net_module_util_BpfUtils(JNIEnv * env,char const * class_name)126 int register_com_android_net_module_util_BpfUtils(JNIEnv* env, char const* class_name) {
127 return jniRegisterNativeMethods(env,
128 class_name,
129 gMethods, NELEM(gMethods));
130 }
131
132 }; // namespace android
133