1 /* 2 * Copyright (C) 2016 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 package android.net.apf; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.net.ConnectivityResources; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 28 /** 29 * APF program support capabilities. APF stands for Android Packet Filtering and it is a flexible 30 * way to drop unwanted network packets to save power. 31 * 32 * See documentation at hardware/google/apf/apf.h 33 * 34 * This class is immutable. 35 * @hide 36 */ 37 @SystemApi 38 public final class ApfCapabilities implements Parcelable { 39 private static ConnectivityResources sResources; 40 41 /** 42 * Version of APF instruction set supported for packet filtering. 0 indicates no support for 43 * packet filtering using APF programs. 44 */ 45 public final int apfVersionSupported; 46 47 /** 48 * Maximum size of APF program allowed. 49 */ 50 public final int maximumApfProgramSize; 51 52 /** 53 * Format of packets passed to APF filter. Should be one of ARPHRD_* 54 */ 55 public final int apfPacketFormat; 56 ApfCapabilities( int apfVersionSupported, int maximumApfProgramSize, int apfPacketFormat)57 public ApfCapabilities( 58 int apfVersionSupported, int maximumApfProgramSize, int apfPacketFormat) { 59 this.apfVersionSupported = apfVersionSupported; 60 this.maximumApfProgramSize = maximumApfProgramSize; 61 this.apfPacketFormat = apfPacketFormat; 62 } 63 ApfCapabilities(Parcel in)64 private ApfCapabilities(Parcel in) { 65 apfVersionSupported = in.readInt(); 66 maximumApfProgramSize = in.readInt(); 67 apfPacketFormat = in.readInt(); 68 } 69 70 @NonNull getResources(@onNull Context ctx)71 private static synchronized ConnectivityResources getResources(@NonNull Context ctx) { 72 if (sResources == null) { 73 sResources = new ConnectivityResources(ctx); 74 } 75 return sResources; 76 } 77 78 79 @Override describeContents()80 public int describeContents() { 81 return 0; 82 } 83 84 @Override writeToParcel(Parcel dest, int flags)85 public void writeToParcel(Parcel dest, int flags) { 86 dest.writeInt(apfVersionSupported); 87 dest.writeInt(maximumApfProgramSize); 88 dest.writeInt(apfPacketFormat); 89 } 90 91 public static final Creator<ApfCapabilities> CREATOR = new Creator<ApfCapabilities>() { 92 @Override 93 public ApfCapabilities createFromParcel(Parcel in) { 94 return new ApfCapabilities(in); 95 } 96 97 @Override 98 public ApfCapabilities[] newArray(int size) { 99 return new ApfCapabilities[size]; 100 } 101 }; 102 103 @NonNull 104 @Override toString()105 public String toString() { 106 return String.format("%s{version: %d, maxSize: %d, format: %d}", getClass().getSimpleName(), 107 apfVersionSupported, maximumApfProgramSize, apfPacketFormat); 108 } 109 110 @Override equals(@ullable Object obj)111 public boolean equals(@Nullable Object obj) { 112 if (!(obj instanceof ApfCapabilities)) return false; 113 final ApfCapabilities other = (ApfCapabilities) obj; 114 return apfVersionSupported == other.apfVersionSupported 115 && maximumApfProgramSize == other.maximumApfProgramSize 116 && apfPacketFormat == other.apfPacketFormat; 117 } 118 119 /** 120 * Determines whether the APF interpreter advertises support for the data buffer access opcodes 121 * LDDW (LoaD Data Word) and STDW (STore Data Word). Full LDDW (LoaD Data Word) and 122 * STDW (STore Data Word) support is present from APFv4 on. 123 * 124 * @return {@code true} if the IWifiStaIface#readApfPacketFilterData is supported. 125 */ hasDataAccess()126 public boolean hasDataAccess() { 127 return apfVersionSupported >= 4; 128 } 129 130 /** 131 * @return Whether the APF Filter in the device should filter out IEEE 802.3 Frames. 132 */ getApfDrop8023Frames()133 public static boolean getApfDrop8023Frames() { 134 // TODO: deprecate/remove this method (now unused in the platform), as the resource was 135 // moved to NetworkStack. 136 final Resources systemRes = Resources.getSystem(); 137 final int id = systemRes.getIdentifier("config_apfDrop802_3Frames", "bool", "android"); 138 return systemRes.getBoolean(id); 139 } 140 141 /** 142 * @return An array of denylisted EtherType, packets with EtherTypes within it will be dropped. 143 */ getApfEtherTypeBlackList()144 public static @NonNull int[] getApfEtherTypeBlackList() { 145 // TODO: deprecate/remove this method (now unused in the platform), as the resource was 146 // moved to NetworkStack. 147 final Resources systemRes = Resources.getSystem(); 148 final int id = systemRes.getIdentifier("config_apfEthTypeBlackList", "array", "android"); 149 return systemRes.getIntArray(id); 150 } 151 } 152