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 25 static { 26 // Load up native shared library containing APF interpreter exposed via JNI. 27 System.loadLibrary("networkstacktestsjni"); 28 } 29 30 /** 31 * Call the APF interpreter to run {@code program} on {@code packet} with persistent memory 32 * segment {@data} pretending the filter was installed {@code filter_age} seconds ago. 33 */ apfSimulate(int apfVersion, byte[] program, byte[] packet, byte[] data, int filterAge)34 public static native int apfSimulate(int apfVersion, byte[] program, byte[] packet, 35 byte[] data, int filterAge); 36 37 /** 38 * Compile a tcpdump human-readable filter (e.g. "icmp" or "tcp port 54") into a BPF 39 * prorgam and return a human-readable dump of the BPF program identical to "tcpdump -d". 40 */ compileToBpf(String filter)41 public static native String compileToBpf(String filter); 42 43 /** 44 * Open packet capture file {@code pcap_filename} and filter the packets using tcpdump 45 * human-readable filter (e.g. "icmp" or "tcp port 54") compiled to a BPF program and 46 * at the same time using APF program {@code apf_program}. Return {@code true} if 47 * both APF and BPF programs filter out exactly the same packets. 48 */ compareBpfApf(int apfVersion, String filter, String pcapFilename, byte[] apfProgram)49 public static native boolean compareBpfApf(int apfVersion, String filter, 50 String pcapFilename, byte[] apfProgram); 51 52 /** 53 * Open packet capture file {@code pcapFilename} and run it through APF filter. Then 54 * checks whether all the packets are dropped and populates data[] {@code data} with 55 * the APF counters. 56 */ dropsAllPackets(int apfVersion, byte[] program, byte[] data, String pcapFilename)57 public static native boolean dropsAllPackets(int apfVersion, byte[] program, byte[] data, 58 String pcapFilename); 59 60 /** 61 * Disassemble the Apf program into human-readable text. 62 */ disassembleApf(byte[] program)63 public static native String[] disassembleApf(byte[] program); 64 65 /** 66 * Get all transmitted packets. 67 */ getAllTransmittedPackets()68 public static native List<byte[]> getAllTransmittedPackets(); 69 70 /** 71 * Reset the memory region that stored the transmitted packet. 72 */ resetTransmittedPacketMemory()73 public static native void resetTransmittedPacketMemory(); 74 } 75