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 package com.android.net.module.util;
17 
18 import android.os.Build;
19 
20 import androidx.annotation.NonNull;
21 import androidx.annotation.RequiresApi;
22 
23 import java.io.IOException;
24 
25 /**
26  * The classes and the methods for BPF utilization.
27  *
28  * {@hide}
29  */
30 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
31 public class BpfUtils {
32     static {
JniUtil.getJniLibraryName()33         System.loadLibrary(JniUtil.getJniLibraryName(BpfUtils.class.getPackage()));
34     }
35 
36     // Defined in include/uapi/linux/bpf.h. Only adding the CGROUPS currently being used for now.
37     public static final int BPF_CGROUP_INET_INGRESS = 0;
38     public static final int BPF_CGROUP_INET_EGRESS = 1;
39     public static final int BPF_CGROUP_INET_SOCK_CREATE = 2;
40     public static final int BPF_CGROUP_INET4_BIND = 8;
41     public static final int BPF_CGROUP_INET6_BIND = 9;
42     public static final int BPF_CGROUP_INET4_CONNECT = 10;
43     public static final int BPF_CGROUP_INET6_CONNECT = 11;
44     public static final int BPF_CGROUP_UDP4_SENDMSG = 14;
45     public static final int BPF_CGROUP_UDP6_SENDMSG = 15;
46     public static final int BPF_CGROUP_UDP4_RECVMSG = 19;
47     public static final int BPF_CGROUP_UDP6_RECVMSG = 20;
48     public static final int BPF_CGROUP_GETSOCKOPT = 21;
49     public static final int BPF_CGROUP_SETSOCKOPT = 22;
50     public static final int BPF_CGROUP_INET_SOCK_RELEASE = 34;
51 
52     // Note: This is only guaranteed to be accurate on U+ devices. It is likely to be accurate
53     // on T+ devices as well, but this is not guaranteed.
54     public static final String CGROUP_PATH = "/sys/fs/cgroup/";
55 
56     /**
57      * Get BPF program Id from CGROUP.
58      *
59      * Note: This requires a 4.19 kernel which is only guaranteed on V+.
60      *
61      * @param attachType Bpf attach type. See bpf_attach_type in include/uapi/linux/bpf.h.
62      * @return Positive integer for a Program Id. 0 if no program is attached.
63      * @throws IOException if failed to open the cgroup directory or query bpf program.
64      */
getProgramId(int attachType)65     public static int getProgramId(int attachType) throws IOException {
66         return native_getProgramIdFromCgroup(attachType, CGROUP_PATH);
67     }
68 
native_getProgramIdFromCgroup(int type, String cgroupPath)69     private static native int native_getProgramIdFromCgroup(int type, String cgroupPath)
70             throws IOException;
71 }
72