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