Paul Jensen | f21b4dc | 2016-03-18 12:22:09 -0400 | [diff] [blame] | 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 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
| 20 | import android.annotation.TestApi; |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 21 | import android.content.Context; |
Remi NGUYEN VAN | 3ac2392 | 2019-03-14 03:33:46 +0900 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.R; |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 26 | |
Paul Jensen | f21b4dc | 2016-03-18 12:22:09 -0400 | [diff] [blame] | 27 | /** |
| 28 | * APF program support capabilities. |
| 29 | * |
Remi NGUYEN VAN | 3ac2392 | 2019-03-14 03:33:46 +0900 | [diff] [blame] | 30 | * This class is immutable. |
Paul Jensen | f21b4dc | 2016-03-18 12:22:09 -0400 | [diff] [blame] | 31 | * @hide |
| 32 | */ |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 33 | @SystemApi |
| 34 | @TestApi |
Remi NGUYEN VAN | 3ac2392 | 2019-03-14 03:33:46 +0900 | [diff] [blame] | 35 | public final class ApfCapabilities implements Parcelable { |
Paul Jensen | f21b4dc | 2016-03-18 12:22:09 -0400 | [diff] [blame] | 36 | /** |
| 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 VAN | 6b0b2b7 | 2019-01-18 18:49:16 +0900 | [diff] [blame] | 52 | public ApfCapabilities( |
| 53 | int apfVersionSupported, int maximumApfProgramSize, int apfPacketFormat) { |
Paul Jensen | f21b4dc | 2016-03-18 12:22:09 -0400 | [diff] [blame] | 54 | this.apfVersionSupported = apfVersionSupported; |
| 55 | this.maximumApfProgramSize = maximumApfProgramSize; |
| 56 | this.apfPacketFormat = apfPacketFormat; |
| 57 | } |
Lorenzo Colitti | 37127f8 | 2016-03-28 15:22:30 +0900 | [diff] [blame] | 58 | |
Remi NGUYEN VAN | 3ac2392 | 2019-03-14 03:33:46 +0900 | [diff] [blame] | 59 | 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 VAN | 6b0b2b7 | 2019-01-18 18:49:16 +0900 | [diff] [blame] | 90 | @Override |
Lorenzo Colitti | 37127f8 | 2016-03-28 15:22:30 +0900 | [diff] [blame] | 91 | public String toString() { |
Erik Kline | af579eb | 2017-04-21 17:56:55 +0900 | [diff] [blame] | 92 | return String.format("%s{version: %d, maxSize: %d, format: %d}", getClass().getSimpleName(), |
Lorenzo Colitti | 37127f8 | 2016-03-28 15:22:30 +0900 | [diff] [blame] | 93 | apfVersionSupported, maximumApfProgramSize, apfPacketFormat); |
| 94 | } |
Bernie Innocenti | ab30db7 | 2018-04-24 01:25:42 +0900 | [diff] [blame] | 95 | |
Remi NGUYEN VAN | 6b0b2b7 | 2019-01-18 18:49:16 +0900 | [diff] [blame] | 96 | @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 Innocenti | ab30db7 | 2018-04-24 01:25:42 +0900 | [diff] [blame] | 105 | /** |
| 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 VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * @return Whether the APF Filter in the device should filter out IEEE 802.3 Frames. |
| 117 | */ |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 118 | public static boolean getApfDrop8023Frames(Context context) { |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 119 | 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 VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 125 | public static int[] getApfEthTypeBlackList(Context context) { |
Remi NGUYEN VAN | 53b0314 | 2019-01-23 23:11:12 +0900 | [diff] [blame] | 126 | return context.getResources().getIntArray(R.array.config_apfEthTypeBlackList); |
| 127 | } |
Lorenzo Colitti | 37127f8 | 2016-03-28 15:22:30 +0900 | [diff] [blame] | 128 | } |