blob: d6023d701762badea25f0b9291c0c5cde35a1127 [file] [log] [blame]
Paul Jensenf21b4dc2016-03-18 12:22:09 -04001/*
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
17package android.net.apf;
18
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090019import android.annotation.SystemApi;
20import android.annotation.TestApi;
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +090021import android.content.Context;
Remi NGUYEN VAN3ac23922019-03-14 03:33:46 +090022import android.os.Parcel;
23import android.os.Parcelable;
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +090024
25import com.android.internal.R;
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090026
Paul Jensenf21b4dc2016-03-18 12:22:09 -040027/**
28 * APF program support capabilities.
29 *
Remi NGUYEN VAN3ac23922019-03-14 03:33:46 +090030 * This class is immutable.
Paul Jensenf21b4dc2016-03-18 12:22:09 -040031 * @hide
32 */
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090033@SystemApi
34@TestApi
Remi NGUYEN VAN3ac23922019-03-14 03:33:46 +090035public final class ApfCapabilities implements Parcelable {
Paul Jensenf21b4dc2016-03-18 12:22:09 -040036 /**
37 * Version of APF instruction set supported for packet filtering. 0 indicates no support for
38 * packet filtering using APF programs.
39 */
40 public final int apfVersionSupported;
41
42 /**
43 * Maximum size of APF program allowed.
44 */
45 public final int maximumApfProgramSize;
46
47 /**
48 * Format of packets passed to APF filter. Should be one of ARPHRD_*
49 */
50 public final int apfPacketFormat;
51
Remi NGUYEN VAN6b0b2b72019-01-18 18:49:16 +090052 public ApfCapabilities(
53 int apfVersionSupported, int maximumApfProgramSize, int apfPacketFormat) {
Paul Jensenf21b4dc2016-03-18 12:22:09 -040054 this.apfVersionSupported = apfVersionSupported;
55 this.maximumApfProgramSize = maximumApfProgramSize;
56 this.apfPacketFormat = apfPacketFormat;
57 }
Lorenzo Colitti37127f82016-03-28 15:22:30 +090058
Remi NGUYEN VAN3ac23922019-03-14 03:33:46 +090059 private ApfCapabilities(Parcel in) {
60 apfVersionSupported = in.readInt();
61 maximumApfProgramSize = in.readInt();
62 apfPacketFormat = in.readInt();
63 }
64
65
66 @Override
67 public int describeContents() {
68 return 0;
69 }
70
71 @Override
72 public void writeToParcel(Parcel dest, int flags) {
73 dest.writeInt(apfVersionSupported);
74 dest.writeInt(maximumApfProgramSize);
75 dest.writeInt(apfPacketFormat);
76 }
77
78 public static final Creator<ApfCapabilities> CREATOR = new Creator<ApfCapabilities>() {
79 @Override
80 public ApfCapabilities createFromParcel(Parcel in) {
81 return new ApfCapabilities(in);
82 }
83
84 @Override
85 public ApfCapabilities[] newArray(int size) {
86 return new ApfCapabilities[size];
87 }
88 };
89
Remi NGUYEN VAN6b0b2b72019-01-18 18:49:16 +090090 @Override
Lorenzo Colitti37127f82016-03-28 15:22:30 +090091 public String toString() {
Erik Klineaf579eb2017-04-21 17:56:55 +090092 return String.format("%s{version: %d, maxSize: %d, format: %d}", getClass().getSimpleName(),
Lorenzo Colitti37127f82016-03-28 15:22:30 +090093 apfVersionSupported, maximumApfProgramSize, apfPacketFormat);
94 }
Bernie Innocentiab30db72018-04-24 01:25:42 +090095
Remi NGUYEN VAN6b0b2b72019-01-18 18:49:16 +090096 @Override
97 public boolean equals(Object obj) {
98 if (!(obj instanceof ApfCapabilities)) return false;
99 final ApfCapabilities other = (ApfCapabilities) obj;
100 return apfVersionSupported == other.apfVersionSupported
101 && maximumApfProgramSize == other.maximumApfProgramSize
102 && apfPacketFormat == other.apfPacketFormat;
103 }
104
Bernie Innocentiab30db72018-04-24 01:25:42 +0900105 /**
106 * Returns true if the APF interpreter advertises support for the data buffer access opcodes
107 * LDDW and STDW.
108 *
109 * Full LDDW and STDW support is present from APFv4 on.
110 */
111 public boolean hasDataAccess() {
112 return apfVersionSupported >= 4;
113 }
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900114
115 /**
116 * @return Whether the APF Filter in the device should filter out IEEE 802.3 Frames.
117 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900118 public static boolean getApfDrop8023Frames(Context context) {
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900119 return context.getResources().getBoolean(R.bool.config_apfDrop802_3Frames);
120 }
121
122 /**
123 * @return An array of blacklisted EtherType, packets with EtherTypes within it will be dropped.
124 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900125 public static int[] getApfEthTypeBlackList(Context context) {
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900126 return context.getResources().getIntArray(R.array.config_apfEthTypeBlackList);
127 }
Lorenzo Colitti37127f82016-03-28 15:22:30 +0900128}