• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.net.apf;
17 
18 import java.util.List;
19 
20 /**
21  * The class contains the helper method for interacting with native apf code.
22  */
23 public class ApfJniUtils {
24     static final int APF_INTERPRETER_VERSION_V6 = 6000;
25     static final int APF_INTERPRETER_VERSION_NEXT = 99999999;
ApfJniUtils(int apfInterpreterVersion)26     public ApfJniUtils(int apfInterpreterVersion) {
27         // Load up native shared library containing APF interpreter exposed via JNI.
28         if (apfInterpreterVersion == APF_INTERPRETER_VERSION_V6) {
29             System.loadLibrary("apfjniv6");
30         } else if (apfInterpreterVersion == APF_INTERPRETER_VERSION_NEXT) {
31             System.loadLibrary("apfjninext");
32         } else {
33             throw new IllegalArgumentException(
34                 "apfInterpreterVersion must be "
35                     + APF_INTERPRETER_VERSION_V6 + " or "
36                     + APF_INTERPRETER_VERSION_NEXT);
37         }
38     }
39 
40     /**
41      * Call the APF interpreter to run {@code program} on {@code packet} with persistent memory
42      * segment {@data} pretending the filter was installed {@code filter_age} seconds ago.
43      */
apfSimulate(int apfVersion, byte[] program, byte[] packet, byte[] data, int filterAge)44     public native int apfSimulate(int apfVersion, byte[] program, byte[] packet,
45             byte[] data, int filterAge);
46 
47     /**
48      * Compile a tcpdump human-readable filter (e.g. "icmp" or "tcp port 54") into a BPF
49      * prorgam and return a human-readable dump of the BPF program identical to "tcpdump -d".
50      */
compileToBpf(String filter)51     public native String compileToBpf(String filter);
52 
53     /**
54      * Open packet capture file {@code pcap_filename} and filter the packets using tcpdump
55      * human-readable filter (e.g. "icmp" or "tcp port 54") compiled to a BPF program and
56      * at the same time using APF program {@code apf_program}.  Return {@code true} if
57      * both APF and BPF programs filter out exactly the same packets.
58      */
compareBpfApf(int apfVersion, String filter, String pcapFilename, byte[] apfProgram)59     public native boolean compareBpfApf(int apfVersion, String filter,
60             String pcapFilename, byte[] apfProgram);
61 
62     /**
63      * Open packet capture file {@code pcapFilename} and run it through APF filter. Then
64      * checks whether all the packets are dropped and populates data[] {@code data} with
65      * the APF counters.
66      */
dropsAllPackets(int apfVersion, byte[] program, byte[] data, String pcapFilename)67     public native boolean dropsAllPackets(int apfVersion, byte[] program, byte[] data,
68             String pcapFilename);
69 
70     /**
71      * Disassemble the Apf program into human-readable text.
72      */
disassembleApf(byte[] program)73     public native String[] disassembleApf(byte[] program);
74 
75     /**
76      * Get all transmitted packets.
77      */
getAllTransmittedPackets()78     public native List<byte[]> getAllTransmittedPackets();
79 
80     /**
81      * Reset the memory region that stored the transmitted packet.
82      */
resetTransmittedPacketMemory()83     public native void resetTransmittedPacketMemory();
84 }
85