San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 com.android.server; |
| 18 | |
paulhu | 79ebe4f | 2019-12-06 16:45:38 +0800 | [diff] [blame] | 19 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Sehee Park | a9139bc | 2017-12-22 13:54:05 +0900 | [diff] [blame] | 20 | import static android.Manifest.permission.NETWORK_SETTINGS; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 21 | import static android.Manifest.permission.OBSERVE_NETWORK_POLICY; |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 22 | import static android.Manifest.permission.SHUTDOWN; |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 23 | import static android.net.INetd.FIREWALL_BLACKLIST; |
| 24 | import static android.net.INetd.FIREWALL_CHAIN_DOZABLE; |
| 25 | import static android.net.INetd.FIREWALL_CHAIN_NONE; |
| 26 | import static android.net.INetd.FIREWALL_CHAIN_POWERSAVE; |
| 27 | import static android.net.INetd.FIREWALL_CHAIN_STANDBY; |
| 28 | import static android.net.INetd.FIREWALL_RULE_ALLOW; |
| 29 | import static android.net.INetd.FIREWALL_RULE_DENY; |
| 30 | import static android.net.INetd.FIREWALL_WHITELIST; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 31 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_DOZABLE; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 32 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_POWERSAVE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 33 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_STANDBY; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 34 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 35 | import static android.net.NetworkStats.SET_DEFAULT; |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 36 | import static android.net.NetworkStats.STATS_PER_UID; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 37 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | ae2c181 | 2011-10-04 13:11:40 -0700 | [diff] [blame] | 38 | import static android.net.TrafficStats.UID_TETHERING; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 39 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 40 | import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED; |
Erik Kline | b2cfdfb | 2017-01-18 20:54:14 +0900 | [diff] [blame] | 41 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 42 | import android.annotation.NonNull; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 43 | import android.app.ActivityManager; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 44 | import android.content.Context; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 45 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 58967ba | 2016-02-02 17:21:21 +0900 | [diff] [blame] | 46 | import android.net.INetd; |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 47 | import android.net.INetdUnsolicitedEventListener; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 48 | import android.net.INetworkManagementEventObserver; |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 49 | import android.net.ITetheringStatsProvider; |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 50 | import android.net.InetAddresses; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 51 | import android.net.InterfaceConfiguration; |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 52 | import android.net.InterfaceConfigurationParcel; |
Lorenzo Colitti | c18cbfd | 2014-06-13 21:21:03 +0900 | [diff] [blame] | 53 | import android.net.IpPrefix; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 54 | import android.net.LinkAddress; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 55 | import android.net.Network; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 56 | import android.net.NetworkPolicyManager; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 57 | import android.net.NetworkStack; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 58 | import android.net.NetworkStats; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 59 | import android.net.NetworkUtils; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 60 | import android.net.RouteInfo; |
Chiachang Wang | afd43e4 | 2019-12-10 10:07:12 +0800 | [diff] [blame] | 61 | import android.net.TetherConfigParcel; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 62 | import android.net.TetherStatsParcel; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 63 | import android.net.UidRange; |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 64 | import android.net.UidRangeParcel; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 65 | import android.net.util.NetdService; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 66 | import android.os.BatteryStats; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 67 | import android.os.Binder; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 68 | import android.os.Handler; |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 69 | import android.os.IBinder; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 70 | import android.os.INetworkActivityListener; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 71 | import android.os.INetworkManagementService; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 72 | import android.os.Process; |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 73 | import android.os.RemoteCallbackList; |
| 74 | import android.os.RemoteException; |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 75 | import android.os.ServiceManager; |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 76 | import android.os.ServiceSpecificException; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 77 | import android.os.StrictMode; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 78 | import android.os.SystemClock; |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 79 | import android.os.SystemProperties; |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 80 | import android.os.Trace; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 81 | import android.telephony.DataConnectionRealTimeInfo; |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 82 | import android.text.TextUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 83 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 84 | import android.util.Slog; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 85 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 86 | import android.util.SparseIntArray; |
Bookatz | 0b028b1 | 2018-05-31 16:51:17 -0700 | [diff] [blame] | 87 | import android.util.StatsLog; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 88 | |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 89 | import com.android.internal.annotations.GuardedBy; |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 91 | import com.android.internal.app.IBatteryStats; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 92 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 93 | import com.android.internal.util.HexDump; |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 94 | import com.android.internal.util.Preconditions; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 95 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 96 | import com.google.android.collect.Maps; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 97 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 98 | import java.io.BufferedReader; |
| 99 | import java.io.DataInputStream; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 100 | import java.io.FileDescriptor; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 101 | import java.io.FileInputStream; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 102 | import java.io.IOException; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 103 | import java.io.InputStreamReader; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 104 | import java.io.PrintWriter; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 105 | import java.net.InetAddress; |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 106 | import java.net.InterfaceAddress; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 107 | import java.util.ArrayList; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 108 | import java.util.Arrays; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 109 | import java.util.HashMap; |
jiaguo | 1da35f7 | 2014-01-09 16:39:59 +0800 | [diff] [blame] | 110 | import java.util.List; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 111 | import java.util.Map; |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame^] | 112 | import java.util.Objects; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @hide |
| 116 | */ |
Luke Huang | 909b31a | 2019-03-16 21:21:16 +0800 | [diff] [blame] | 117 | public class NetworkManagementService extends INetworkManagementService.Stub { |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Helper class that encapsulates NetworkManagementService dependencies and makes them |
| 121 | * easier to mock in unit tests. |
| 122 | */ |
| 123 | static class SystemServices { |
| 124 | public IBinder getService(String name) { |
| 125 | return ServiceManager.getService(name); |
| 126 | } |
| 127 | public void registerLocalService(NetworkManagementInternal nmi) { |
| 128 | LocalServices.addService(NetworkManagementInternal.class, nmi); |
| 129 | } |
| 130 | public INetd getNetd() { |
| 131 | return NetdService.get(); |
| 132 | } |
| 133 | } |
| 134 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 135 | private static final String TAG = "NetworkManagement"; |
| 136 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 137 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 138 | private static final int MAX_UID_RANGES_PER_COMMAND = 10; |
| 139 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Name representing {@link #setGlobalAlert(long)} limit when delivered to |
| 142 | * {@link INetworkManagementEventObserver#limitReached(String, String)}. |
| 143 | */ |
| 144 | public static final String LIMIT_GLOBAL_ALERT = "globalAlert"; |
| 145 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 146 | static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1; |
| 147 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 148 | static final boolean MODIFY_OPERATION_ADD = true; |
| 149 | static final boolean MODIFY_OPERATION_REMOVE = false; |
| 150 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Binder context for this service |
| 153 | */ |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 154 | private final Context mContext; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 155 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 156 | private final Handler mDaemonHandler; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 157 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 158 | private final SystemServices mServices; |
| 159 | |
Lorenzo Colitti | 58967ba | 2016-02-02 17:21:21 +0900 | [diff] [blame] | 160 | private INetd mNetdService; |
| 161 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 162 | private final NetdUnsolicitedEventListener mNetdUnsolicitedEventListener; |
Luke Huang | d290dd5 | 2018-09-04 17:08:18 +0800 | [diff] [blame] | 163 | |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 164 | private IBatteryStats mBatteryStats; |
| 165 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 166 | private final RemoteCallbackList<INetworkManagementEventObserver> mObservers = |
| 167 | new RemoteCallbackList<>(); |
| 168 | |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 169 | @GuardedBy("mTetheringStatsProviders") |
| 170 | private final HashMap<ITetheringStatsProvider, String> |
| 171 | mTetheringStatsProviders = Maps.newHashMap(); |
| 172 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 173 | /** |
| 174 | * If both locks need to be held, then they should be obtained in the order: |
| 175 | * first {@link #mQuotaLock} and then {@link #mRulesLock}. |
| 176 | */ |
Andrew Scull | 45f533c | 2017-05-19 15:37:20 +0100 | [diff] [blame] | 177 | private final Object mQuotaLock = new Object(); |
Andrew Scull | 519291f | 2017-05-23 13:11:03 +0100 | [diff] [blame] | 178 | private final Object mRulesLock = new Object(); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 179 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 180 | /** Set of interfaces with active quotas. */ |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 181 | @GuardedBy("mQuotaLock") |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 182 | private HashMap<String, Long> mActiveQuotas = Maps.newHashMap(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 183 | /** Set of interfaces with active alerts. */ |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 184 | @GuardedBy("mQuotaLock") |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 185 | private HashMap<String, Long> mActiveAlerts = Maps.newHashMap(); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 186 | /** Set of UIDs blacklisted on metered networks. */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 187 | @GuardedBy("mRulesLock") |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 188 | private SparseBooleanArray mUidRejectOnMetered = new SparseBooleanArray(); |
| 189 | /** Set of UIDs whitelisted on metered networks. */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 190 | @GuardedBy("mRulesLock") |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 191 | private SparseBooleanArray mUidAllowOnMetered = new SparseBooleanArray(); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 192 | /** Set of UIDs with cleartext penalties. */ |
| 193 | @GuardedBy("mQuotaLock") |
| 194 | private SparseIntArray mUidCleartextPolicy = new SparseIntArray(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 195 | /** Set of UIDs that are to be blocked/allowed by firewall controller. */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 196 | @GuardedBy("mRulesLock") |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 197 | private SparseIntArray mUidFirewallRules = new SparseIntArray(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Set of UIDs that are to be blocked/allowed by firewall controller. This set of Ids matches |
| 200 | * to application idles. |
| 201 | */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 202 | @GuardedBy("mRulesLock") |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 203 | private SparseIntArray mUidFirewallStandbyRules = new SparseIntArray(); |
| 204 | /** |
| 205 | * Set of UIDs that are to be blocked/allowed by firewall controller. This set of Ids matches |
| 206 | * to device idles. |
| 207 | */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 208 | @GuardedBy("mRulesLock") |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 209 | private SparseIntArray mUidFirewallDozableRules = new SparseIntArray(); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Set of UIDs that are to be blocked/allowed by firewall controller. This set of Ids matches |
| 212 | * to device on power-save mode. |
| 213 | */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 214 | @GuardedBy("mRulesLock") |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 215 | private SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 216 | /** Set of states for the child firewall chains. True if the chain is active. */ |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 217 | @GuardedBy("mRulesLock") |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 218 | final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 219 | |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 220 | @GuardedBy("mQuotaLock") |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 221 | private volatile boolean mDataSaverMode; |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 222 | |
Andrew Scull | 45f533c | 2017-05-19 15:37:20 +0100 | [diff] [blame] | 223 | private final Object mIdleTimerLock = new Object(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 224 | /** Set of interfaces with active idle timers. */ |
| 225 | private static class IdleTimerParams { |
| 226 | public final int timeout; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 227 | public final int type; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 228 | public int networkCount; |
| 229 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 230 | IdleTimerParams(int timeout, int type) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 231 | this.timeout = timeout; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 232 | this.type = type; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 233 | this.networkCount = 1; |
| 234 | } |
| 235 | } |
| 236 | private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap(); |
| 237 | |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 238 | private volatile boolean mFirewallEnabled; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 239 | private volatile boolean mStrictEnabled; |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 240 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 241 | private boolean mMobileActivityFromRadio = false; |
| 242 | private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 243 | private int mLastPowerStateFromWifi = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 244 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 245 | private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners = |
Christopher Wiley | 212b95f | 2016-08-02 11:38:57 -0700 | [diff] [blame] | 246 | new RemoteCallbackList<>(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 247 | private boolean mNetworkActive; |
| 248 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Constructs a new NetworkManagementService instance |
| 251 | * |
| 252 | * @param context Binder context for this service |
| 253 | */ |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 254 | private NetworkManagementService( |
Luke Huang | 909b31a | 2019-03-16 21:21:16 +0800 | [diff] [blame] | 255 | Context context, SystemServices services) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 256 | mContext = context; |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 257 | mServices = services; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 258 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 259 | mDaemonHandler = new Handler(FgThread.get().getLooper()); |
Wink Saville | 67e0789 | 2014-06-18 16:43:14 -0700 | [diff] [blame] | 260 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 261 | mNetdUnsolicitedEventListener = new NetdUnsolicitedEventListener(); |
| 262 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 263 | mServices.registerLocalService(new LocalService()); |
Lorenzo Colitti | 8228eb3 | 2017-07-19 06:17:33 +0900 | [diff] [blame] | 264 | |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 265 | synchronized (mTetheringStatsProviders) { |
| 266 | mTetheringStatsProviders.put(new NetdTetheringStatsProvider(), "netd"); |
| 267 | } |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @VisibleForTesting |
| 271 | NetworkManagementService() { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 272 | mContext = null; |
| 273 | mDaemonHandler = null; |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 274 | mServices = null; |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 275 | mNetdUnsolicitedEventListener = null; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Luke Huang | 909b31a | 2019-03-16 21:21:16 +0800 | [diff] [blame] | 278 | static NetworkManagementService create(Context context, SystemServices services) |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 279 | throws InterruptedException { |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 280 | final NetworkManagementService service = |
Luke Huang | 909b31a | 2019-03-16 21:21:16 +0800 | [diff] [blame] | 281 | new NetworkManagementService(context, services); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 282 | if (DBG) Slog.d(TAG, "Creating NetworkManagementService"); |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 283 | if (DBG) Slog.d(TAG, "Connecting native netd service"); |
bohu | 07cc3bb | 2016-05-03 15:58:01 -0700 | [diff] [blame] | 284 | service.connectNativeNetdService(); |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 285 | if (DBG) Slog.d(TAG, "Connected"); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 286 | return service; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 289 | public static NetworkManagementService create(Context context) throws InterruptedException { |
Luke Huang | 909b31a | 2019-03-16 21:21:16 +0800 | [diff] [blame] | 290 | return create(context, new SystemServices()); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 293 | public void systemReady() { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 294 | if (DBG) { |
| 295 | final long start = System.currentTimeMillis(); |
| 296 | prepareNativeDaemon(); |
| 297 | final long delta = System.currentTimeMillis() - start; |
| 298 | Slog.d(TAG, "Prepared in " + delta + "ms"); |
| 299 | return; |
| 300 | } else { |
| 301 | prepareNativeDaemon(); |
| 302 | } |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 305 | private IBatteryStats getBatteryStats() { |
| 306 | synchronized (this) { |
| 307 | if (mBatteryStats != null) { |
| 308 | return mBatteryStats; |
| 309 | } |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 310 | mBatteryStats = |
| 311 | IBatteryStats.Stub.asInterface(mServices.getService(BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 312 | return mBatteryStats; |
| 313 | } |
| 314 | } |
| 315 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 316 | @Override |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 317 | public void registerObserver(INetworkManagementEventObserver observer) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 318 | NetworkStack.checkNetworkStackPermission(mContext); |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 319 | mObservers.register(observer); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 322 | @Override |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 323 | public void unregisterObserver(INetworkManagementEventObserver observer) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 324 | NetworkStack.checkNetworkStackPermission(mContext); |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 325 | mObservers.unregister(observer); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Erik Kline | b2cfdfb | 2017-01-18 20:54:14 +0900 | [diff] [blame] | 328 | @FunctionalInterface |
| 329 | private interface NetworkManagementEventCallback { |
| 330 | public void sendCallback(INetworkManagementEventObserver o) throws RemoteException; |
| 331 | } |
| 332 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 333 | private void invokeForAllObservers(NetworkManagementEventCallback eventCallback) { |
| 334 | final int length = mObservers.beginBroadcast(); |
| 335 | try { |
| 336 | for (int i = 0; i < length; i++) { |
| 337 | try { |
| 338 | eventCallback.sendCallback(mObservers.getBroadcastItem(i)); |
| 339 | } catch (RemoteException | RuntimeException e) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 340 | } |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 341 | } |
| 342 | } finally { |
| 343 | mObservers.finishBroadcast(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Notify our observers of an interface status change |
| 349 | */ |
| 350 | private void notifyInterfaceStatusChanged(String iface, boolean up) { |
| 351 | invokeForAllObservers(o -> o.interfaceStatusChanged(iface, up)); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Notify our observers of an interface link state change |
| 356 | * (typically, an Ethernet cable has been plugged-in or unplugged). |
| 357 | */ |
| 358 | private void notifyInterfaceLinkStateChanged(String iface, boolean up) { |
| 359 | invokeForAllObservers(o -> o.interfaceLinkStateChanged(iface, up)); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Notify our observers of an interface addition. |
| 364 | */ |
| 365 | private void notifyInterfaceAdded(String iface) { |
| 366 | invokeForAllObservers(o -> o.interfaceAdded(iface)); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Notify our observers of an interface removal. |
| 371 | */ |
| 372 | private void notifyInterfaceRemoved(String iface) { |
| 373 | // netd already clears out quota and alerts for removed ifaces; update |
| 374 | // our sanity-checking state. |
| 375 | mActiveAlerts.remove(iface); |
| 376 | mActiveQuotas.remove(iface); |
| 377 | invokeForAllObservers(o -> o.interfaceRemoved(iface)); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Notify our observers of a limit reached. |
| 382 | */ |
| 383 | private void notifyLimitReached(String limitName, String iface) { |
| 384 | invokeForAllObservers(o -> o.limitReached(limitName, iface)); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Notify our observers of a change in the data activity state of the interface |
| 389 | */ |
| 390 | private void notifyInterfaceClassActivity(int type, boolean isActive, long tsNanos, |
| 391 | int uid, boolean fromRadio) { |
| 392 | final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type); |
| 393 | int powerState = isActive |
| 394 | ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH |
| 395 | : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW; |
| 396 | if (isMobile) { |
| 397 | if (!fromRadio) { |
| 398 | if (mMobileActivityFromRadio) { |
| 399 | // If this call is not coming from a report from the radio itself, but we |
| 400 | // have previously received reports from the radio, then we will take the |
| 401 | // power state to just be whatever the radio last reported. |
| 402 | powerState = mLastPowerStateFromRadio; |
| 403 | } |
| 404 | } else { |
| 405 | mMobileActivityFromRadio = true; |
| 406 | } |
| 407 | if (mLastPowerStateFromRadio != powerState) { |
| 408 | mLastPowerStateFromRadio = powerState; |
| 409 | try { |
| 410 | getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos, uid); |
| 411 | } catch (RemoteException e) { |
Robert Greenwalt | 2c9f547 | 2014-04-21 14:50:28 -0700 | [diff] [blame] | 412 | } |
Bookatz | 0b028b1 | 2018-05-31 16:51:17 -0700 | [diff] [blame] | 413 | StatsLog.write_non_chained(StatsLog.MOBILE_RADIO_POWER_STATE_CHANGED, uid, null, |
| 414 | powerState); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 415 | } |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 416 | } |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 417 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 418 | if (ConnectivityManager.isNetworkTypeWifi(type)) { |
| 419 | if (mLastPowerStateFromWifi != powerState) { |
| 420 | mLastPowerStateFromWifi = powerState; |
| 421 | try { |
| 422 | getBatteryStats().noteWifiRadioPowerState(powerState, tsNanos, uid); |
| 423 | } catch (RemoteException e) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 424 | } |
Bookatz | 0b028b1 | 2018-05-31 16:51:17 -0700 | [diff] [blame] | 425 | StatsLog.write_non_chained(StatsLog.WIFI_RADIO_POWER_STATE_CHANGED, uid, null, |
| 426 | powerState); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 427 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 428 | } |
Lorenzo Colitti | d8bc829 | 2019-01-24 13:28:50 +0900 | [diff] [blame] | 429 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 430 | if (!isMobile || fromRadio || !mMobileActivityFromRadio) { |
| 431 | // Report the change in data activity. We don't do this if this is a change |
| 432 | // on the mobile network, that is not coming from the radio itself, and we |
| 433 | // have previously seen change reports from the radio. In that case only |
| 434 | // the radio is the authority for the current state. |
| 435 | final boolean active = isActive; |
| 436 | invokeForAllObservers(o -> o.interfaceClassDataActivityChanged( |
| 437 | Integer.toString(type), active, tsNanos)); |
Lorenzo Colitti | d8bc829 | 2019-01-24 13:28:50 +0900 | [diff] [blame] | 438 | } |
| 439 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 440 | boolean report = false; |
| 441 | synchronized (mIdleTimerLock) { |
| 442 | if (mActiveIdleTimers.isEmpty()) { |
| 443 | // If there are no idle timers, we are not monitoring activity, so we |
| 444 | // are always considered active. |
| 445 | isActive = true; |
| 446 | } |
| 447 | if (mNetworkActive != isActive) { |
| 448 | mNetworkActive = isActive; |
| 449 | report = isActive; |
| 450 | } |
| 451 | } |
| 452 | if (report) { |
| 453 | reportNetworkActive(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 454 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 457 | @Override |
| 458 | public void registerTetheringStatsProvider(ITetheringStatsProvider provider, String name) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 459 | NetworkStack.checkNetworkStackPermission(mContext); |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame^] | 460 | Objects.requireNonNull(provider); |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 461 | synchronized(mTetheringStatsProviders) { |
| 462 | mTetheringStatsProviders.put(provider, name); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void unregisterTetheringStatsProvider(ITetheringStatsProvider provider) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 468 | NetworkStack.checkNetworkStackPermission(mContext); |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 469 | synchronized(mTetheringStatsProviders) { |
| 470 | mTetheringStatsProviders.remove(provider); |
| 471 | } |
| 472 | } |
| 473 | |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 474 | @Override |
| 475 | public void tetherLimitReached(ITetheringStatsProvider provider) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 476 | NetworkStack.checkNetworkStackPermission(mContext); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 477 | synchronized(mTetheringStatsProviders) { |
| 478 | if (!mTetheringStatsProviders.containsKey(provider)) { |
| 479 | return; |
| 480 | } |
| 481 | // No current code examines the interface parameter in a global alert. Just pass null. |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 482 | mDaemonHandler.post(() -> notifyLimitReached(LIMIT_GLOBAL_ALERT, null)); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 486 | // Sync the state of the given chain with the native daemon. |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 487 | private void syncFirewallChainLocked(int chain, String name) { |
| 488 | SparseIntArray rules; |
| 489 | synchronized (mRulesLock) { |
| 490 | final SparseIntArray uidFirewallRules = getUidFirewallRulesLR(chain); |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 491 | // Make a copy of the current rules, and then clear them. This is because |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 492 | // setFirewallUidRuleInternal only pushes down rules to the native daemon if they |
| 493 | // are different from the current rules stored in the mUidFirewall*Rules array for |
| 494 | // the specified chain. If we don't clear the rules, setFirewallUidRuleInternal |
| 495 | // will do nothing. |
| 496 | rules = uidFirewallRules.clone(); |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 497 | uidFirewallRules.clear(); |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 498 | } |
| 499 | if (rules.size() > 0) { |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 500 | // Now push the rules. setFirewallUidRuleInternal will push each of these down to the |
| 501 | // native daemon, and also add them to the mUidFirewall*Rules array for the specified |
| 502 | // chain. |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 503 | if (DBG) Slog.d(TAG, "Pushing " + rules.size() + " active firewall " |
| 504 | + name + "UID rules"); |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 505 | for (int i = 0; i < rules.size(); i++) { |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 506 | setFirewallUidRuleLocked(chain, rules.keyAt(i), rules.valueAt(i)); |
Lorenzo Colitti | 9eb844e | 2016-03-23 23:22:49 +0900 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
bohu | 07cc3bb | 2016-05-03 15:58:01 -0700 | [diff] [blame] | 511 | private void connectNativeNetdService() { |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 512 | mNetdService = mServices.getNetd(); |
Luke Huang | d290dd5 | 2018-09-04 17:08:18 +0800 | [diff] [blame] | 513 | try { |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 514 | mNetdService.registerUnsolicitedEventListener(mNetdUnsolicitedEventListener); |
| 515 | if (DBG) Slog.d(TAG, "Register unsolicited event listener"); |
Luke Huang | d290dd5 | 2018-09-04 17:08:18 +0800 | [diff] [blame] | 516 | } catch (RemoteException | ServiceSpecificException e) { |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 517 | Slog.e(TAG, "Failed to set Netd unsolicited event listener " + e); |
Luke Huang | d290dd5 | 2018-09-04 17:08:18 +0800 | [diff] [blame] | 518 | } |
bohu | 07cc3bb | 2016-05-03 15:58:01 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Prepare native daemon once connected, enabling modules and pushing any |
| 523 | * existing in-memory rules. |
| 524 | */ |
| 525 | private void prepareNativeDaemon() { |
Lorenzo Colitti | 58967ba | 2016-02-02 17:21:21 +0900 | [diff] [blame] | 526 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 527 | // push any existing quota or UID rules |
| 528 | synchronized (mQuotaLock) { |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 529 | |
Luke Huang | 56a03a0 | 2018-09-07 12:02:16 +0800 | [diff] [blame] | 530 | // Netd unconditionally enable bandwidth control |
| 531 | SystemProperties.set(PROP_QTAGUID_ENABLED, "1"); |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 532 | |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 533 | mStrictEnabled = true; |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 534 | |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 535 | setDataSaverModeEnabled(mDataSaverMode); |
| 536 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 537 | int size = mActiveQuotas.size(); |
| 538 | if (size > 0) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 539 | if (DBG) Slog.d(TAG, "Pushing " + size + " active quota rules"); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 540 | final HashMap<String, Long> activeQuotas = mActiveQuotas; |
| 541 | mActiveQuotas = Maps.newHashMap(); |
| 542 | for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) { |
| 543 | setInterfaceQuota(entry.getKey(), entry.getValue()); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | size = mActiveAlerts.size(); |
| 548 | if (size > 0) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 549 | if (DBG) Slog.d(TAG, "Pushing " + size + " active alert rules"); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 550 | final HashMap<String, Long> activeAlerts = mActiveAlerts; |
| 551 | mActiveAlerts = Maps.newHashMap(); |
| 552 | for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) { |
| 553 | setInterfaceAlert(entry.getKey(), entry.getValue()); |
| 554 | } |
| 555 | } |
| 556 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 557 | SparseBooleanArray uidRejectOnQuota = null; |
| 558 | SparseBooleanArray uidAcceptOnQuota = null; |
| 559 | synchronized (mRulesLock) { |
| 560 | size = mUidRejectOnMetered.size(); |
| 561 | if (size > 0) { |
| 562 | if (DBG) Slog.d(TAG, "Pushing " + size + " UIDs to metered blacklist rules"); |
| 563 | uidRejectOnQuota = mUidRejectOnMetered; |
| 564 | mUidRejectOnMetered = new SparseBooleanArray(); |
| 565 | } |
| 566 | |
| 567 | size = mUidAllowOnMetered.size(); |
| 568 | if (size > 0) { |
| 569 | if (DBG) Slog.d(TAG, "Pushing " + size + " UIDs to metered whitelist rules"); |
| 570 | uidAcceptOnQuota = mUidAllowOnMetered; |
| 571 | mUidAllowOnMetered = new SparseBooleanArray(); |
| 572 | } |
| 573 | } |
| 574 | if (uidRejectOnQuota != null) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 575 | for (int i = 0; i < uidRejectOnQuota.size(); i++) { |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 576 | setUidMeteredNetworkBlacklist(uidRejectOnQuota.keyAt(i), |
| 577 | uidRejectOnQuota.valueAt(i)); |
| 578 | } |
| 579 | } |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 580 | if (uidAcceptOnQuota != null) { |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 581 | for (int i = 0; i < uidAcceptOnQuota.size(); i++) { |
| 582 | setUidMeteredNetworkWhitelist(uidAcceptOnQuota.keyAt(i), |
| 583 | uidAcceptOnQuota.valueAt(i)); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 586 | |
| 587 | size = mUidCleartextPolicy.size(); |
| 588 | if (size > 0) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 589 | if (DBG) Slog.d(TAG, "Pushing " + size + " active UID cleartext policies"); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 590 | final SparseIntArray local = mUidCleartextPolicy; |
| 591 | mUidCleartextPolicy = new SparseIntArray(); |
| 592 | for (int i = 0; i < local.size(); i++) { |
| 593 | setUidCleartextNetworkPolicy(local.keyAt(i), local.valueAt(i)); |
| 594 | } |
| 595 | } |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 596 | |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 597 | setFirewallEnabled(mFirewallEnabled); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 598 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 599 | syncFirewallChainLocked(FIREWALL_CHAIN_NONE, ""); |
| 600 | syncFirewallChainLocked(FIREWALL_CHAIN_STANDBY, "standby "); |
| 601 | syncFirewallChainLocked(FIREWALL_CHAIN_DOZABLE, "dozable "); |
| 602 | syncFirewallChainLocked(FIREWALL_CHAIN_POWERSAVE, "powersave "); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 603 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 604 | final int[] chains = |
| 605 | {FIREWALL_CHAIN_STANDBY, FIREWALL_CHAIN_DOZABLE, FIREWALL_CHAIN_POWERSAVE}; |
| 606 | for (int chain : chains) { |
| 607 | if (getFirewallChainState(chain)) { |
| 608 | setFirewallChainEnabled(chain, true); |
| 609 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 610 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 611 | } |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 612 | |
Luke Huang | 56a03a0 | 2018-09-07 12:02:16 +0800 | [diff] [blame] | 613 | |
| 614 | try { |
| 615 | getBatteryStats().noteNetworkStatsEnabled(); |
| 616 | } catch (RemoteException e) { |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 617 | } |
| 618 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 619 | } |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 620 | |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 621 | /** |
| 622 | * Notify our observers of a new or updated interface address. |
| 623 | */ |
| 624 | private void notifyAddressUpdated(String iface, LinkAddress address) { |
| 625 | invokeForAllObservers(o -> o.addressUpdated(iface, address)); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Notify our observers of a deleted interface address. |
| 630 | */ |
| 631 | private void notifyAddressRemoved(String iface, LinkAddress address) { |
| 632 | invokeForAllObservers(o -> o.addressRemoved(iface, address)); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Notify our observers of DNS server information received. |
| 637 | */ |
| 638 | private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) { |
| 639 | invokeForAllObservers(o -> o.interfaceDnsServerInfo(iface, lifetime, addresses)); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Notify our observers of a route change. |
| 644 | */ |
| 645 | private void notifyRouteChange(boolean updated, RouteInfo route) { |
| 646 | if (updated) { |
| 647 | invokeForAllObservers(o -> o.routeUpdated(route)); |
| 648 | } else { |
| 649 | invokeForAllObservers(o -> o.routeRemoved(route)); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | private class NetdUnsolicitedEventListener extends INetdUnsolicitedEventListener.Stub { |
| 654 | @Override |
| 655 | public void onInterfaceClassActivityChanged(boolean isActive, |
| 656 | int label, long timestamp, int uid) throws RemoteException { |
| 657 | final long timestampNanos; |
| 658 | if (timestamp <= 0) { |
| 659 | timestampNanos = SystemClock.elapsedRealtimeNanos(); |
| 660 | } else { |
| 661 | timestampNanos = timestamp; |
| 662 | } |
| 663 | mDaemonHandler.post(() -> |
| 664 | notifyInterfaceClassActivity(label, isActive, timestampNanos, uid, false)); |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public void onQuotaLimitReached(String alertName, String ifName) |
| 669 | throws RemoteException { |
| 670 | mDaemonHandler.post(() -> notifyLimitReached(alertName, ifName)); |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | public void onInterfaceDnsServerInfo(String ifName, |
| 675 | long lifetime, String[] servers) throws RemoteException { |
| 676 | mDaemonHandler.post(() -> notifyInterfaceDnsServerInfo(ifName, lifetime, servers)); |
| 677 | } |
| 678 | |
| 679 | @Override |
| 680 | public void onInterfaceAddressUpdated(String addr, |
| 681 | String ifName, int flags, int scope) throws RemoteException { |
| 682 | final LinkAddress address = new LinkAddress(addr, flags, scope); |
| 683 | mDaemonHandler.post(() -> notifyAddressUpdated(ifName, address)); |
| 684 | } |
| 685 | |
| 686 | @Override |
| 687 | public void onInterfaceAddressRemoved(String addr, |
| 688 | String ifName, int flags, int scope) throws RemoteException { |
| 689 | final LinkAddress address = new LinkAddress(addr, flags, scope); |
| 690 | mDaemonHandler.post(() -> notifyAddressRemoved(ifName, address)); |
| 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public void onInterfaceAdded(String ifName) throws RemoteException { |
| 695 | mDaemonHandler.post(() -> notifyInterfaceAdded(ifName)); |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | public void onInterfaceRemoved(String ifName) throws RemoteException { |
| 700 | mDaemonHandler.post(() -> notifyInterfaceRemoved(ifName)); |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public void onInterfaceChanged(String ifName, boolean up) |
| 705 | throws RemoteException { |
| 706 | mDaemonHandler.post(() -> notifyInterfaceStatusChanged(ifName, up)); |
| 707 | } |
| 708 | |
| 709 | @Override |
| 710 | public void onInterfaceLinkStateChanged(String ifName, boolean up) |
| 711 | throws RemoteException { |
| 712 | mDaemonHandler.post(() -> notifyInterfaceLinkStateChanged(ifName, up)); |
| 713 | } |
| 714 | |
| 715 | @Override |
| 716 | public void onRouteChanged(boolean updated, |
| 717 | String route, String gateway, String ifName) throws RemoteException { |
| 718 | final RouteInfo processRoute = new RouteInfo(new IpPrefix(route), |
| 719 | ("".equals(gateway)) ? null : InetAddresses.parseNumericAddress(gateway), |
| 720 | ifName); |
| 721 | mDaemonHandler.post(() -> notifyRouteChange(updated, processRoute)); |
| 722 | } |
| 723 | |
| 724 | @Override |
| 725 | public void onStrictCleartextDetected(int uid, String hex) throws RemoteException { |
| 726 | // Don't need to post to mDaemonHandler because the only thing |
| 727 | // that notifyCleartextNetwork does is post to a handler |
| 728 | ActivityManager.getService().notifyCleartextNetwork(uid, |
| 729 | HexDump.hexStringToByteArray(hex)); |
| 730 | } |
Remi NGUYEN VAN | eec0ed4 | 2019-04-09 14:01:51 +0900 | [diff] [blame] | 731 | |
| 732 | @Override |
| 733 | public int getInterfaceVersion() { |
| 734 | return INetdUnsolicitedEventListener.VERSION; |
| 735 | } |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 736 | } |
| 737 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 738 | // |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 739 | // INetworkManagementService members |
| 740 | // |
Erik Kline | 4e37b70 | 2016-07-05 11:34:21 +0900 | [diff] [blame] | 741 | @Override |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 742 | public String[] listInterfaces() { |
paulhu | 79ebe4f | 2019-12-06 16:45:38 +0800 | [diff] [blame] | 743 | // TODO: Remove CONNECTIVITY_INTERNAL after bluetooth tethering has no longer called these |
| 744 | // APIs. |
| 745 | NetworkStack.checkNetworkStackPermissionOr(mContext, CONNECTIVITY_INTERNAL); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 746 | try { |
Luke Huang | 1b4f92f | 2018-12-12 15:59:31 +0800 | [diff] [blame] | 747 | return mNetdService.interfaceGetList(); |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 748 | } catch (RemoteException | ServiceSpecificException e) { |
| 749 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 750 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 753 | /** |
| 754 | * Convert InterfaceConfiguration to InterfaceConfigurationParcel with given ifname. |
| 755 | */ |
| 756 | private static InterfaceConfigurationParcel toStableParcel(InterfaceConfiguration cfg, |
| 757 | String iface) { |
| 758 | InterfaceConfigurationParcel cfgParcel = new InterfaceConfigurationParcel(); |
| 759 | cfgParcel.ifName = iface; |
| 760 | String hwAddr = cfg.getHardwareAddress(); |
| 761 | if (!TextUtils.isEmpty(hwAddr)) { |
| 762 | cfgParcel.hwAddr = hwAddr; |
| 763 | } else { |
| 764 | cfgParcel.hwAddr = ""; |
| 765 | } |
| 766 | cfgParcel.ipv4Addr = cfg.getLinkAddress().getAddress().getHostAddress(); |
| 767 | cfgParcel.prefixLength = cfg.getLinkAddress().getPrefixLength(); |
| 768 | ArrayList<String> flags = new ArrayList<>(); |
| 769 | for (String flag : cfg.getFlags()) { |
| 770 | flags.add(flag); |
| 771 | } |
| 772 | cfgParcel.flags = flags.toArray(new String[0]); |
| 773 | |
| 774 | return cfgParcel; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Construct InterfaceConfiguration from InterfaceConfigurationParcel. |
| 779 | */ |
| 780 | public static InterfaceConfiguration fromStableParcel(InterfaceConfigurationParcel p) { |
| 781 | InterfaceConfiguration cfg = new InterfaceConfiguration(); |
| 782 | cfg.setHardwareAddress(p.hwAddr); |
| 783 | |
| 784 | final InetAddress addr = NetworkUtils.numericToInetAddress(p.ipv4Addr); |
| 785 | cfg.setLinkAddress(new LinkAddress(addr, p.prefixLength)); |
| 786 | for (String flag : p.flags) { |
| 787 | cfg.setFlag(flag); |
| 788 | } |
| 789 | |
| 790 | return cfg; |
| 791 | } |
| 792 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 793 | @Override |
| 794 | public InterfaceConfiguration getInterfaceConfig(String iface) { |
paulhu | 79ebe4f | 2019-12-06 16:45:38 +0800 | [diff] [blame] | 795 | // TODO: Remove CONNECTIVITY_INTERNAL after bluetooth tethering has no longer called these |
| 796 | // APIs. |
| 797 | NetworkStack.checkNetworkStackPermissionOr(mContext, CONNECTIVITY_INTERNAL); |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 798 | final InterfaceConfigurationParcel result; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 799 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 800 | result = mNetdService.interfaceGetCfg(iface); |
| 801 | } catch (RemoteException | ServiceSpecificException e) { |
| 802 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 803 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 804 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 805 | try { |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 806 | final InterfaceConfiguration cfg = fromStableParcel(result); |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 807 | return cfg; |
| 808 | } catch (IllegalArgumentException iae) { |
| 809 | throw new IllegalStateException("Invalid InterfaceConfigurationParcel", iae); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 810 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 813 | @Override |
| 814 | public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) { |
paulhu | 79ebe4f | 2019-12-06 16:45:38 +0800 | [diff] [blame] | 815 | // TODO: Remove CONNECTIVITY_INTERNAL after bluetooth tethering has no longer called these |
| 816 | // APIs. |
| 817 | NetworkStack.checkNetworkStackPermissionOr(mContext, CONNECTIVITY_INTERNAL); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 818 | LinkAddress linkAddr = cfg.getLinkAddress(); |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 819 | if (linkAddr == null || linkAddr.getAddress() == null) { |
| 820 | throw new IllegalStateException("Null LinkAddress given"); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 821 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 822 | |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 823 | final InterfaceConfigurationParcel cfgParcel = toStableParcel(cfg, iface); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 824 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 825 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 826 | mNetdService.interfaceSetCfg(cfgParcel); |
| 827 | } catch (RemoteException | ServiceSpecificException e) { |
| 828 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 829 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 830 | } |
| 831 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 832 | @Override |
| 833 | public void setInterfaceDown(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 834 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 835 | final InterfaceConfiguration ifcg = getInterfaceConfig(iface); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 836 | ifcg.setInterfaceDown(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 837 | setInterfaceConfig(iface, ifcg); |
Irfan Sheriff | 7244c97 | 2011-08-05 20:40:45 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 840 | @Override |
| 841 | public void setInterfaceUp(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 842 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 843 | final InterfaceConfiguration ifcg = getInterfaceConfig(iface); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 844 | ifcg.setInterfaceUp(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 845 | setInterfaceConfig(iface, ifcg); |
Irfan Sheriff | 7244c97 | 2011-08-05 20:40:45 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 848 | @Override |
| 849 | public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 850 | NetworkStack.checkNetworkStackPermission(mContext); |
Irfan Sheriff | 7329361 | 2011-09-14 12:31:56 -0700 | [diff] [blame] | 851 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 852 | mNetdService.interfaceSetIPv6PrivacyExtensions(iface, enable); |
| 853 | } catch (RemoteException | ServiceSpecificException e) { |
| 854 | throw new IllegalStateException(e); |
Irfan Sheriff | 7329361 | 2011-09-14 12:31:56 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 858 | /* TODO: This is right now a IPv4 only function. Works for wifi which loses its |
| 859 | IPv6 addresses on interface down, but we need to do full clean up here */ |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 860 | @Override |
| 861 | public void clearInterfaceAddresses(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 862 | NetworkStack.checkNetworkStackPermission(mContext); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 863 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 864 | mNetdService.interfaceClearAddrs(iface); |
| 865 | } catch (RemoteException | ServiceSpecificException e) { |
| 866 | throw new IllegalStateException(e); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 870 | @Override |
| 871 | public void enableIpv6(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 872 | NetworkStack.checkNetworkStackPermission(mContext); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 873 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 874 | mNetdService.interfaceSetEnableIPv6(iface, true); |
| 875 | } catch (RemoteException | ServiceSpecificException e) { |
| 876 | throw new IllegalStateException(e); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 880 | @Override |
Joel Scherpelz | 2db1074 | 2017-06-07 15:38:38 +0900 | [diff] [blame] | 881 | public void setIPv6AddrGenMode(String iface, int mode) throws ServiceSpecificException { |
| 882 | try { |
| 883 | mNetdService.setIPv6AddrGenMode(iface, mode); |
| 884 | } catch (RemoteException e) { |
| 885 | throw e.rethrowAsRuntimeException(); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | @Override |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 890 | public void disableIpv6(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 891 | NetworkStack.checkNetworkStackPermission(mContext); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 892 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 893 | mNetdService.interfaceSetEnableIPv6(iface, false); |
| 894 | } catch (RemoteException | ServiceSpecificException e) { |
| 895 | throw new IllegalStateException(e); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 899 | @Override |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 900 | public void addRoute(int netId, RouteInfo route) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 901 | modifyRoute(MODIFY_OPERATION_ADD, netId, route); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 904 | @Override |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 905 | public void removeRoute(int netId, RouteInfo route) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 906 | modifyRoute(MODIFY_OPERATION_REMOVE, netId, route); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 909 | private void modifyRoute(boolean add, int netId, RouteInfo route) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 910 | NetworkStack.checkNetworkStackPermission(mContext); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 911 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 912 | final String ifName = route.getInterface(); |
| 913 | final String dst = route.getDestination().toString(); |
| 914 | final String nextHop; |
Lorenzo Colitti | 4b0f8e6 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 915 | |
| 916 | switch (route.getType()) { |
| 917 | case RouteInfo.RTN_UNICAST: |
| 918 | if (route.hasGateway()) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 919 | nextHop = route.getGateway().getHostAddress(); |
| 920 | } else { |
| 921 | nextHop = INetd.NEXTHOP_NONE; |
Lorenzo Colitti | 4b0f8e6 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 922 | } |
| 923 | break; |
| 924 | case RouteInfo.RTN_UNREACHABLE: |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 925 | nextHop = INetd.NEXTHOP_UNREACHABLE; |
Lorenzo Colitti | 4b0f8e6 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 926 | break; |
| 927 | case RouteInfo.RTN_THROW: |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 928 | nextHop = INetd.NEXTHOP_THROW; |
| 929 | break; |
| 930 | default: |
| 931 | nextHop = INetd.NEXTHOP_NONE; |
Lorenzo Colitti | 4b0f8e6 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 932 | break; |
Sreeram Ramachandran | 1fbcb27 | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 933 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 934 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 935 | if (add) { |
| 936 | mNetdService.networkAddRoute(netId, ifName, dst, nextHop); |
| 937 | } else { |
| 938 | mNetdService.networkRemoveRoute(netId, ifName, dst, nextHop); |
| 939 | } |
| 940 | } catch (RemoteException | ServiceSpecificException e) { |
| 941 | throw new IllegalStateException(e); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | private ArrayList<String> readRouteList(String filename) { |
| 946 | FileInputStream fstream = null; |
Christopher Wiley | 212b95f | 2016-08-02 11:38:57 -0700 | [diff] [blame] | 947 | ArrayList<String> list = new ArrayList<>(); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 948 | |
| 949 | try { |
| 950 | fstream = new FileInputStream(filename); |
| 951 | DataInputStream in = new DataInputStream(fstream); |
| 952 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
| 953 | String s; |
| 954 | |
| 955 | // throw away the title line |
| 956 | |
| 957 | while (((s = br.readLine()) != null) && (s.length() != 0)) { |
| 958 | list.add(s); |
| 959 | } |
| 960 | } catch (IOException ex) { |
| 961 | // return current list, possibly empty |
| 962 | } finally { |
| 963 | if (fstream != null) { |
| 964 | try { |
| 965 | fstream.close(); |
| 966 | } catch (IOException ex) {} |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | return list; |
| 971 | } |
| 972 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 973 | @Override |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 974 | public void setMtu(String iface, int mtu) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 975 | NetworkStack.checkNetworkStackPermission(mContext); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 976 | |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 977 | try { |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 978 | mNetdService.interfaceSetMtu(iface, mtu); |
| 979 | } catch (RemoteException | ServiceSpecificException e) { |
| 980 | throw new IllegalStateException(e); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
| 984 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 985 | public void shutdown() { |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 986 | // TODO: remove from aidl if nobody calls externally |
| 987 | mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 988 | |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 989 | Slog.i(TAG, "Shutting down"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 992 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 993 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 994 | NetworkStack.checkNetworkStackPermission(mContext); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 995 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 996 | try { |
Luke Huang | 4db488b | 2018-08-16 15:37:31 +0800 | [diff] [blame] | 997 | final boolean isEnabled = mNetdService.ipfwdEnabled(); |
| 998 | return isEnabled; |
| 999 | } catch (RemoteException | ServiceSpecificException e) { |
| 1000 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1001 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1004 | @Override |
| 1005 | public void setIpForwardingEnabled(boolean enable) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1006 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 1007 | try { |
Luke Huang | 4db488b | 2018-08-16 15:37:31 +0800 | [diff] [blame] | 1008 | if (enable) { |
| 1009 | mNetdService.ipfwdEnableForwarding("tethering"); |
| 1010 | } else { |
| 1011 | mNetdService.ipfwdDisableForwarding("tethering"); |
| 1012 | } |
| 1013 | } catch (RemoteException | ServiceSpecificException e) { |
| 1014 | throw new IllegalStateException(e); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 1015 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1018 | @Override |
| 1019 | public void startTethering(String[] dhcpRange) { |
Luke Huang | b0d5246 | 2019-03-21 14:43:08 +0800 | [diff] [blame] | 1020 | startTetheringWithConfiguration(true, dhcpRange); |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | public void startTetheringWithConfiguration(boolean usingLegacyDnsProxy, String[] dhcpRange) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1025 | NetworkStack.checkNetworkStackPermission(mContext); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 1026 | // an odd number of addrs will fail |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1027 | try { |
Chiachang Wang | afd43e4 | 2019-12-10 10:07:12 +0800 | [diff] [blame] | 1028 | final TetherConfigParcel config = new TetherConfigParcel(); |
| 1029 | config.usingLegacyDnsProxy = usingLegacyDnsProxy; |
| 1030 | config.dhcpRanges = dhcpRange; |
| 1031 | mNetdService.tetherStartWithConfiguration(config); |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1032 | } catch (RemoteException | ServiceSpecificException e) { |
| 1033 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1034 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1037 | @Override |
| 1038 | public void stopTethering() { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1039 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1040 | try { |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1041 | mNetdService.tetherStop(); |
| 1042 | } catch (RemoteException | ServiceSpecificException e) { |
| 1043 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1044 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1047 | @Override |
| 1048 | public boolean isTetheringStarted() { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1049 | NetworkStack.checkNetworkStackPermission(mContext); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1050 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1051 | try { |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1052 | final boolean isEnabled = mNetdService.tetherIsEnabled(); |
| 1053 | return isEnabled; |
| 1054 | } catch (RemoteException | ServiceSpecificException e) { |
| 1055 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1056 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1057 | } |
Matthew Xie | fe19f12 | 2012-07-12 16:03:32 -0700 | [diff] [blame] | 1058 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1059 | @Override |
| 1060 | public void tetherInterface(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1061 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1062 | try { |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1063 | mNetdService.tetherInterfaceAdd(iface); |
| 1064 | } catch (RemoteException | ServiceSpecificException e) { |
| 1065 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1066 | } |
Christopher Wiley | 212b95f | 2016-08-02 11:38:57 -0700 | [diff] [blame] | 1067 | List<RouteInfo> routes = new ArrayList<>(); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 1068 | // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it |
| 1069 | // suitable to use as a route destination. |
| 1070 | routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface)); |
| 1071 | addInterfaceToLocalNetwork(iface, routes); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1074 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1075 | public void untetherInterface(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1076 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1077 | try { |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1078 | mNetdService.tetherInterfaceRemove(iface); |
| 1079 | } catch (RemoteException | ServiceSpecificException e) { |
| 1080 | throw new IllegalStateException(e); |
Erik Kline | 1f4278a | 2016-08-16 16:46:33 +0900 | [diff] [blame] | 1081 | } finally { |
| 1082 | removeInterfaceFromLocalNetwork(iface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1083 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1086 | @Override |
| 1087 | public String[] listTetheredInterfaces() { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1088 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1089 | try { |
Luke Huang | 1b4f92f | 2018-12-12 15:59:31 +0800 | [diff] [blame] | 1090 | return mNetdService.tetherInterfaceList(); |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1091 | } catch (RemoteException | ServiceSpecificException e) { |
| 1092 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1093 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1096 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1097 | public void setDnsForwarders(Network network, String[] dns) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1098 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1099 | |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1100 | int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET; |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1101 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1102 | try { |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1103 | mNetdService.tetherDnsSet(netId, dns); |
| 1104 | } catch (RemoteException | ServiceSpecificException e) { |
| 1105 | throw new IllegalStateException(e); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1109 | @Override |
| 1110 | public String[] getDnsForwarders() { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1111 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1112 | try { |
Luke Huang | 1b4f92f | 2018-12-12 15:59:31 +0800 | [diff] [blame] | 1113 | return mNetdService.tetherDnsList(); |
Luke Huang | 4a32bf4 | 2018-08-21 19:09:45 +0800 | [diff] [blame] | 1114 | } catch (RemoteException | ServiceSpecificException e) { |
| 1115 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1116 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
jiaguo | 1da35f7 | 2014-01-09 16:39:59 +0800 | [diff] [blame] | 1119 | private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) { |
Christopher Wiley | 212b95f | 2016-08-02 11:38:57 -0700 | [diff] [blame] | 1120 | ArrayList<InterfaceAddress> filtered = new ArrayList<>(addresses.size()); |
jiaguo | 1da35f7 | 2014-01-09 16:39:59 +0800 | [diff] [blame] | 1121 | for (InterfaceAddress ia : addresses) { |
| 1122 | if (!ia.getAddress().isLinkLocalAddress()) |
| 1123 | filtered.add(ia); |
| 1124 | } |
| 1125 | return filtered; |
| 1126 | } |
| 1127 | |
Lorenzo Colitti | 35e36db | 2015-02-26 01:25:36 +0900 | [diff] [blame] | 1128 | private void modifyInterfaceForward(boolean add, String fromIface, String toIface) { |
Lorenzo Colitti | 35e36db | 2015-02-26 01:25:36 +0900 | [diff] [blame] | 1129 | try { |
Luke Huang | 4db488b | 2018-08-16 15:37:31 +0800 | [diff] [blame] | 1130 | if (add) { |
| 1131 | mNetdService.ipfwdAddInterfaceForward(fromIface, toIface); |
| 1132 | } else { |
| 1133 | mNetdService.ipfwdRemoveInterfaceForward(fromIface, toIface); |
| 1134 | } |
| 1135 | } catch (RemoteException | ServiceSpecificException e) { |
| 1136 | throw new IllegalStateException(e); |
Lorenzo Colitti | 35e36db | 2015-02-26 01:25:36 +0900 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | @Override |
| 1141 | public void startInterfaceForwarding(String fromIface, String toIface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1142 | NetworkStack.checkNetworkStackPermission(mContext); |
Lorenzo Colitti | 35e36db | 2015-02-26 01:25:36 +0900 | [diff] [blame] | 1143 | modifyInterfaceForward(true, fromIface, toIface); |
| 1144 | } |
| 1145 | |
| 1146 | @Override |
| 1147 | public void stopInterfaceForwarding(String fromIface, String toIface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1148 | NetworkStack.checkNetworkStackPermission(mContext); |
Lorenzo Colitti | 35e36db | 2015-02-26 01:25:36 +0900 | [diff] [blame] | 1149 | modifyInterfaceForward(false, fromIface, toIface); |
| 1150 | } |
| 1151 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1152 | @Override |
| 1153 | public void enableNat(String internalInterface, String externalInterface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1154 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1155 | try { |
Luke Huang | a31e073 | 2018-10-22 13:23:10 +0900 | [diff] [blame] | 1156 | mNetdService.tetherAddForward(internalInterface, externalInterface); |
| 1157 | } catch (RemoteException | ServiceSpecificException e) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1158 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1159 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1162 | @Override |
| 1163 | public void disableNat(String internalInterface, String externalInterface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1164 | NetworkStack.checkNetworkStackPermission(mContext); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1165 | try { |
Luke Huang | a31e073 | 2018-10-22 13:23:10 +0900 | [diff] [blame] | 1166 | mNetdService.tetherRemoveForward(internalInterface, externalInterface); |
| 1167 | } catch (RemoteException | ServiceSpecificException e) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1168 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1169 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1170 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 1171 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1172 | @Override |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1173 | public void addIdleTimer(String iface, int timeout, final int type) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1174 | NetworkStack.checkNetworkStackPermission(mContext); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1175 | |
| 1176 | if (DBG) Slog.d(TAG, "Adding idletimer"); |
| 1177 | |
| 1178 | synchronized (mIdleTimerLock) { |
| 1179 | IdleTimerParams params = mActiveIdleTimers.get(iface); |
| 1180 | if (params != null) { |
| 1181 | // the interface already has idletimer, update network count |
| 1182 | params.networkCount++; |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | try { |
Luke Huang | a62d049 | 2018-07-27 20:08:21 +0800 | [diff] [blame] | 1187 | mNetdService.idletimerAddInterface(iface, timeout, Integer.toString(type)); |
| 1188 | } catch (RemoteException | ServiceSpecificException e) { |
| 1189 | throw new IllegalStateException(e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1190 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1191 | mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type)); |
| 1192 | |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 1193 | // Networks start up. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1194 | if (ConnectivityManager.isNetworkTypeMobile(type)) { |
| 1195 | mNetworkActive = false; |
| 1196 | } |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 1197 | mDaemonHandler.post(() -> notifyInterfaceClassActivity(type, true, |
| 1198 | SystemClock.elapsedRealtimeNanos(), -1, false)); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | @Override |
| 1203 | public void removeIdleTimer(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1204 | NetworkStack.checkNetworkStackPermission(mContext); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1205 | |
| 1206 | if (DBG) Slog.d(TAG, "Removing idletimer"); |
| 1207 | |
| 1208 | synchronized (mIdleTimerLock) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1209 | final IdleTimerParams params = mActiveIdleTimers.get(iface); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1210 | if (params == null || --(params.networkCount) > 0) { |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | try { |
Luke Huang | a62d049 | 2018-07-27 20:08:21 +0800 | [diff] [blame] | 1215 | mNetdService.idletimerRemoveInterface(iface, |
| 1216 | params.timeout, Integer.toString(params.type)); |
| 1217 | } catch (RemoteException | ServiceSpecificException e) { |
| 1218 | throw new IllegalStateException(e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1219 | } |
| 1220 | mActiveIdleTimers.remove(iface); |
Remi NGUYEN VAN | 9ebc3fc | 2019-01-29 19:12:13 +0900 | [diff] [blame] | 1221 | mDaemonHandler.post(() -> notifyInterfaceClassActivity(params.type, false, |
| 1222 | SystemClock.elapsedRealtimeNanos(), -1, false)); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1227 | public void setInterfaceQuota(String iface, long quotaBytes) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1228 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1229 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1230 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1231 | if (mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1232 | throw new IllegalStateException("iface " + iface + " already has quota"); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1235 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1236 | // TODO: support quota shared across interfaces |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1237 | mNetdService.bandwidthSetInterfaceQuota(iface, quotaBytes); |
| 1238 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1239 | mActiveQuotas.put(iface, quotaBytes); |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1240 | } catch (RemoteException | ServiceSpecificException e) { |
| 1241 | throw new IllegalStateException(e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1242 | } |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 1243 | |
| 1244 | synchronized (mTetheringStatsProviders) { |
| 1245 | for (ITetheringStatsProvider provider : mTetheringStatsProviders.keySet()) { |
| 1246 | try { |
| 1247 | provider.setInterfaceQuota(iface, quotaBytes); |
| 1248 | } catch (RemoteException e) { |
| 1249 | Log.e(TAG, "Problem setting tethering data limit on provider " + |
| 1250 | mTetheringStatsProviders.get(provider) + ": " + e); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | @Override |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1258 | public void removeInterfaceQuota(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1259 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1260 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1261 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1262 | if (!mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1263 | // TODO: eventually consider throwing |
| 1264 | return; |
| 1265 | } |
| 1266 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1267 | mActiveQuotas.remove(iface); |
| 1268 | mActiveAlerts.remove(iface); |
Jeff Sharkey | 38ddeaa | 2011-11-08 13:04:22 -0800 | [diff] [blame] | 1269 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1270 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1271 | // TODO: support quota shared across interfaces |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1272 | mNetdService.bandwidthRemoveInterfaceQuota(iface); |
| 1273 | } catch (RemoteException | ServiceSpecificException e) { |
| 1274 | throw new IllegalStateException(e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1275 | } |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 1276 | |
| 1277 | synchronized (mTetheringStatsProviders) { |
| 1278 | for (ITetheringStatsProvider provider : mTetheringStatsProviders.keySet()) { |
| 1279 | try { |
| 1280 | provider.setInterfaceQuota(iface, ITetheringStatsProvider.QUOTA_UNLIMITED); |
| 1281 | } catch (RemoteException e) { |
| 1282 | Log.e(TAG, "Problem removing tethering data limit on provider " + |
| 1283 | mTetheringStatsProviders.get(provider) + ": " + e); |
| 1284 | } |
| 1285 | } |
| 1286 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1291 | public void setInterfaceAlert(String iface, long alertBytes) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1292 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1293 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1294 | // quick sanity check |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1295 | if (!mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1296 | throw new IllegalStateException("setting alert requires existing quota on iface"); |
| 1297 | } |
| 1298 | |
| 1299 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1300 | if (mActiveAlerts.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1301 | throw new IllegalStateException("iface " + iface + " already has alert"); |
| 1302 | } |
| 1303 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1304 | try { |
| 1305 | // TODO: support alert shared across interfaces |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1306 | mNetdService.bandwidthSetInterfaceAlert(iface, alertBytes); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1307 | mActiveAlerts.put(iface, alertBytes); |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1308 | } catch (RemoteException | ServiceSpecificException e) { |
| 1309 | throw new IllegalStateException(e); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | @Override |
| 1315 | public void removeInterfaceAlert(String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1316 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1317 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1318 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1319 | if (!mActiveAlerts.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1320 | // TODO: eventually consider throwing |
| 1321 | return; |
| 1322 | } |
| 1323 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1324 | try { |
| 1325 | // TODO: support alert shared across interfaces |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1326 | mNetdService.bandwidthRemoveInterfaceAlert(iface); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1327 | mActiveAlerts.remove(iface); |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1328 | } catch (RemoteException | ServiceSpecificException e) { |
| 1329 | throw new IllegalStateException(e); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | @Override |
| 1335 | public void setGlobalAlert(long alertBytes) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1336 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1337 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1338 | try { |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1339 | mNetdService.bandwidthSetGlobalAlert(alertBytes); |
| 1340 | } catch (RemoteException | ServiceSpecificException e) { |
| 1341 | throw new IllegalStateException(e); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1345 | private void setUidOnMeteredNetworkList(int uid, boolean blacklist, boolean enable) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1346 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1347 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1348 | synchronized (mQuotaLock) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1349 | boolean oldEnable; |
| 1350 | SparseBooleanArray quotaList; |
| 1351 | synchronized (mRulesLock) { |
| 1352 | quotaList = blacklist ? mUidRejectOnMetered : mUidAllowOnMetered; |
| 1353 | oldEnable = quotaList.get(uid, false); |
| 1354 | } |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1355 | if (oldEnable == enable) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1356 | // TODO: eventually consider throwing |
| 1357 | return; |
| 1358 | } |
| 1359 | |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 1360 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "inetd bandwidth"); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1361 | try { |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1362 | if (blacklist) { |
| 1363 | if (enable) { |
| 1364 | mNetdService.bandwidthAddNaughtyApp(uid); |
| 1365 | } else { |
| 1366 | mNetdService.bandwidthRemoveNaughtyApp(uid); |
| 1367 | } |
| 1368 | } else { |
| 1369 | if (enable) { |
| 1370 | mNetdService.bandwidthAddNiceApp(uid); |
| 1371 | } else { |
| 1372 | mNetdService.bandwidthRemoveNiceApp(uid); |
| 1373 | } |
| 1374 | } |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1375 | synchronized (mRulesLock) { |
| 1376 | if (enable) { |
| 1377 | quotaList.put(uid, true); |
| 1378 | } else { |
| 1379 | quotaList.delete(uid); |
| 1380 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1381 | } |
Luke Huang | c7bea866 | 2018-08-07 16:04:26 +0800 | [diff] [blame] | 1382 | } catch (RemoteException | ServiceSpecificException e) { |
| 1383 | throw new IllegalStateException(e); |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 1384 | } finally { |
| 1385 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1386 | } |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 1390 | @Override |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1391 | public void setUidMeteredNetworkBlacklist(int uid, boolean enable) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1392 | setUidOnMeteredNetworkList(uid, true, enable); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | @Override |
| 1396 | public void setUidMeteredNetworkWhitelist(int uid, boolean enable) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1397 | setUidOnMeteredNetworkList(uid, false, enable); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | @Override |
| 1401 | public boolean setDataSaverModeEnabled(boolean enable) { |
Sehee Park | a9139bc | 2017-12-22 13:54:05 +0900 | [diff] [blame] | 1402 | mContext.enforceCallingOrSelfPermission(NETWORK_SETTINGS, TAG); |
| 1403 | |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1404 | if (DBG) Log.d(TAG, "setDataSaverMode: " + enable); |
| 1405 | synchronized (mQuotaLock) { |
| 1406 | if (mDataSaverMode == enable) { |
| 1407 | Log.w(TAG, "setDataSaverMode(): already " + mDataSaverMode); |
| 1408 | return true; |
| 1409 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 1410 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "bandwidthEnableDataSaver"); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1411 | try { |
| 1412 | final boolean changed = mNetdService.bandwidthEnableDataSaver(enable); |
| 1413 | if (changed) { |
| 1414 | mDataSaverMode = enable; |
| 1415 | } else { |
| 1416 | Log.w(TAG, "setDataSaverMode(" + enable + "): netd command silently failed"); |
| 1417 | } |
| 1418 | return changed; |
| 1419 | } catch (RemoteException e) { |
| 1420 | Log.w(TAG, "setDataSaverMode(" + enable + "): netd command failed", e); |
| 1421 | return false; |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 1422 | } finally { |
| 1423 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1428 | private static UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 1429 | UidRangeParcel range = new UidRangeParcel(); |
| 1430 | range.start = start; |
| 1431 | range.stop = stop; |
| 1432 | return range; |
| 1433 | } |
| 1434 | |
| 1435 | private static UidRangeParcel[] toStableParcels(UidRange[] ranges) { |
| 1436 | UidRangeParcel[] stableRanges = new UidRangeParcel[ranges.length]; |
| 1437 | for (int i = 0; i < ranges.length; i++) { |
| 1438 | stableRanges[i] = makeUidRangeParcel(ranges[i].start, ranges[i].stop); |
| 1439 | } |
| 1440 | return stableRanges; |
| 1441 | } |
| 1442 | |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1443 | @Override |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1444 | public void setAllowOnlyVpnForUids(boolean add, UidRange[] uidRanges) |
| 1445 | throws ServiceSpecificException { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1446 | NetworkStack.checkNetworkStackPermission(mContext); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1447 | try { |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1448 | mNetdService.networkRejectNonSecureVpn(add, toStableParcels(uidRanges)); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1449 | } catch (ServiceSpecificException e) { |
| 1450 | Log.w(TAG, "setAllowOnlyVpnForUids(" + add + ", " + Arrays.toString(uidRanges) + ")" |
| 1451 | + ": netd command failed", e); |
| 1452 | throw e; |
| 1453 | } catch (RemoteException e) { |
| 1454 | Log.w(TAG, "setAllowOnlyVpnForUids(" + add + ", " + Arrays.toString(uidRanges) + ")" |
| 1455 | + ": netd command failed", e); |
| 1456 | throw e.rethrowAsRuntimeException(); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1460 | private void applyUidCleartextNetworkPolicy(int uid, int policy) { |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1461 | final int policyValue; |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1462 | switch (policy) { |
| 1463 | case StrictMode.NETWORK_POLICY_ACCEPT: |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1464 | policyValue = INetd.PENALTY_POLICY_ACCEPT; |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1465 | break; |
| 1466 | case StrictMode.NETWORK_POLICY_LOG: |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1467 | policyValue = INetd.PENALTY_POLICY_LOG; |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1468 | break; |
| 1469 | case StrictMode.NETWORK_POLICY_REJECT: |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1470 | policyValue = INetd.PENALTY_POLICY_REJECT; |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1471 | break; |
| 1472 | default: |
| 1473 | throw new IllegalArgumentException("Unknown policy " + policy); |
| 1474 | } |
| 1475 | |
| 1476 | try { |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1477 | mNetdService.strictUidCleartextPenalty(uid, policyValue); |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1478 | mUidCleartextPolicy.put(uid, policy); |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1479 | } catch (RemoteException | ServiceSpecificException e) { |
| 1480 | throw new IllegalStateException(e); |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1484 | @Override |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1485 | public void setUidCleartextNetworkPolicy(int uid, int policy) { |
| 1486 | if (Binder.getCallingUid() != uid) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1487 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | synchronized (mQuotaLock) { |
| 1491 | final int oldPolicy = mUidCleartextPolicy.get(uid, StrictMode.NETWORK_POLICY_ACCEPT); |
| 1492 | if (oldPolicy == policy) { |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1493 | // This also ensures we won't needlessly apply an ACCEPT policy if we've just |
| 1494 | // enabled strict and the underlying iptables rules are empty. |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | |
Luke Huang | 473eb87 | 2018-07-26 17:33:14 +0800 | [diff] [blame] | 1498 | // TODO: remove this code after removing prepareNativeDaemon() |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1499 | if (!mStrictEnabled) { |
| 1500 | // Module isn't enabled yet; stash the requested policy away to |
| 1501 | // apply later once the daemon is connected. |
| 1502 | mUidCleartextPolicy.put(uid, policy); |
| 1503 | return; |
| 1504 | } |
| 1505 | |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1506 | // netd does not keep state on strict mode policies, and cannot replace a non-accept |
| 1507 | // policy without deleting it first. Rather than add state to netd, just always send |
| 1508 | // it an accept policy when switching between two non-accept policies. |
Lorenzo Colitti | 26364f1 | 2017-08-20 11:54:57 +0900 | [diff] [blame] | 1509 | // TODO: consider keeping state in netd so we can simplify this code. |
Lorenzo Colitti | 8c253ad | 2017-07-19 00:23:44 +0900 | [diff] [blame] | 1510 | if (oldPolicy != StrictMode.NETWORK_POLICY_ACCEPT && |
| 1511 | policy != StrictMode.NETWORK_POLICY_ACCEPT) { |
Lorenzo Colitti | 26364f1 | 2017-08-20 11:54:57 +0900 | [diff] [blame] | 1512 | applyUidCleartextNetworkPolicy(uid, StrictMode.NETWORK_POLICY_ACCEPT); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1513 | } |
Lorenzo Colitti | 26364f1 | 2017-08-20 11:54:57 +0900 | [diff] [blame] | 1514 | |
| 1515 | applyUidCleartextNetworkPolicy(uid, policy); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | @Override |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 1520 | public boolean isBandwidthControlEnabled() { |
Luke Huang | 56a03a0 | 2018-09-07 12:02:16 +0800 | [diff] [blame] | 1521 | return true; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1524 | private class NetdTetheringStatsProvider extends ITetheringStatsProvider.Stub { |
| 1525 | @Override |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1526 | public NetworkStats getTetherStats(int how) { |
| 1527 | // We only need to return per-UID stats. Per-device stats are already counted by |
| 1528 | // interface counters. |
| 1529 | if (how != STATS_PER_UID) { |
| 1530 | return new NetworkStats(SystemClock.elapsedRealtime(), 0); |
| 1531 | } |
| 1532 | |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1533 | final TetherStatsParcel[] tetherStatsVec; |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1534 | try { |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1535 | tetherStatsVec = mNetdService.tetherGetStats(); |
Lorenzo Colitti | 563dc45 | 2017-09-01 17:12:34 +0900 | [diff] [blame] | 1536 | } catch (RemoteException | ServiceSpecificException e) { |
| 1537 | throw new IllegalStateException("problem parsing tethering stats: ", e); |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1538 | } |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1539 | |
Lorenzo Colitti | 563dc45 | 2017-09-01 17:12:34 +0900 | [diff] [blame] | 1540 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1541 | tetherStatsVec.length); |
Lorenzo Colitti | 563dc45 | 2017-09-01 17:12:34 +0900 | [diff] [blame] | 1542 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 1543 | |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1544 | for (TetherStatsParcel tetherStats : tetherStatsVec) { |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1545 | try { |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1546 | entry.iface = tetherStats.iface; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1547 | entry.uid = UID_TETHERING; |
| 1548 | entry.set = SET_DEFAULT; |
| 1549 | entry.tag = TAG_NONE; |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1550 | entry.rxBytes = tetherStats.rxBytes; |
| 1551 | entry.rxPackets = tetherStats.rxPackets; |
| 1552 | entry.txBytes = tetherStats.txBytes; |
| 1553 | entry.txPackets = tetherStats.txPackets; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1554 | stats.combineValues(entry); |
Lorenzo Colitti | 563dc45 | 2017-09-01 17:12:34 +0900 | [diff] [blame] | 1555 | } catch (ArrayIndexOutOfBoundsException e) { |
Luke Huang | 13b79e8 | 2018-09-26 14:53:42 +0800 | [diff] [blame] | 1556 | throw new IllegalStateException("invalid tethering stats " + e); |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1557 | } |
| 1558 | } |
Lorenzo Colitti | 563dc45 | 2017-09-01 17:12:34 +0900 | [diff] [blame] | 1559 | |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1560 | return stats; |
| 1561 | } |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 1562 | |
| 1563 | @Override |
| 1564 | public void setInterfaceQuota(String iface, long quotaBytes) { |
| 1565 | // Do nothing. netd is already informed of quota changes in setInterfaceQuota. |
| 1566 | } |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | @Override |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1570 | public NetworkStats getNetworkStatsTethering(int how) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1571 | NetworkStack.checkNetworkStackPermission(mContext); |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1572 | |
| 1573 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1); |
| 1574 | synchronized (mTetheringStatsProviders) { |
| 1575 | for (ITetheringStatsProvider provider: mTetheringStatsProviders.keySet()) { |
| 1576 | try { |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1577 | stats.combineAllValues(provider.getTetherStats(how)); |
Lorenzo Colitti | 07f1304 | 2017-07-10 19:06:57 +0900 | [diff] [blame] | 1578 | } catch (RemoteException e) { |
| 1579 | Log.e(TAG, "Problem reading tethering stats from " + |
| 1580 | mTetheringStatsProviders.get(provider) + ": " + e); |
| 1581 | } |
| 1582 | } |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1583 | } |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1584 | return stats; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1587 | @Override |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1588 | public void addVpnUidRanges(int netId, UidRange[] ranges) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1589 | NetworkStack.checkNetworkStackPermission(mContext); |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 1590 | |
| 1591 | try { |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1592 | mNetdService.networkAddUidRanges(netId, toStableParcels(ranges)); |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 1593 | } catch (RemoteException | ServiceSpecificException e) { |
| 1594 | throw new IllegalStateException(e); |
Chad Brubaker | 3277620a | 2013-06-12 13:37:30 -0700 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | @Override |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1599 | public void removeVpnUidRanges(int netId, UidRange[] ranges) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1600 | NetworkStack.checkNetworkStackPermission(mContext); |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 1601 | try { |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1602 | mNetdService.networkRemoveUidRanges(netId, toStableParcels(ranges)); |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 1603 | } catch (RemoteException | ServiceSpecificException e) { |
| 1604 | throw new IllegalStateException(e); |
Chad Brubaker | cca54c4 | 2013-06-27 17:41:38 -0700 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | @Override |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1609 | public void setFirewallEnabled(boolean enabled) { |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 1610 | enforceSystemUid(); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1611 | try { |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1612 | mNetdService.firewallSetFirewallType( |
| 1613 | enabled ? INetd.FIREWALL_WHITELIST : INetd.FIREWALL_BLACKLIST); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1614 | mFirewallEnabled = enabled; |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1615 | } catch (RemoteException | ServiceSpecificException e) { |
| 1616 | throw new IllegalStateException(e); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | @Override |
| 1621 | public boolean isFirewallEnabled() { |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 1622 | enforceSystemUid(); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1623 | return mFirewallEnabled; |
| 1624 | } |
| 1625 | |
| 1626 | @Override |
Jeff Sharkey | 2c09298 | 2012-08-24 11:44:40 -0700 | [diff] [blame] | 1627 | public void setFirewallInterfaceRule(String iface, boolean allow) { |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 1628 | enforceSystemUid(); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1629 | Preconditions.checkState(mFirewallEnabled); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1630 | try { |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1631 | mNetdService.firewallSetInterfaceRule(iface, |
| 1632 | allow ? INetd.FIREWALL_RULE_ALLOW : INetd.FIREWALL_RULE_DENY); |
| 1633 | } catch (RemoteException | ServiceSpecificException e) { |
| 1634 | throw new IllegalStateException(e); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | |
Lorenzo Colitti | 3fef723 | 2016-04-29 18:00:03 +0900 | [diff] [blame] | 1638 | private void closeSocketsForFirewallChainLocked(int chain, String chainName) { |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1639 | // UID ranges to close sockets on. |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1640 | UidRangeParcel[] ranges; |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1641 | // UID ranges whose sockets we won't touch. |
| 1642 | int[] exemptUids; |
| 1643 | |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1644 | int numUids = 0; |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1645 | if (DBG) Slog.d(TAG, "Closing sockets after enabling chain " + chainName); |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1646 | if (getFirewallType(chain) == FIREWALL_WHITELIST) { |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1647 | // Close all sockets on all non-system UIDs... |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1648 | ranges = new UidRangeParcel[] { |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1649 | // TODO: is there a better way of finding all existing users? If so, we could |
| 1650 | // specify their ranges here. |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1651 | makeUidRangeParcel(Process.FIRST_APPLICATION_UID, Integer.MAX_VALUE), |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1652 | }; |
| 1653 | // ... except for the UIDs that have allow rules. |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1654 | synchronized (mRulesLock) { |
| 1655 | final SparseIntArray rules = getUidFirewallRulesLR(chain); |
| 1656 | exemptUids = new int[rules.size()]; |
| 1657 | for (int i = 0; i < exemptUids.length; i++) { |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1658 | if (rules.valueAt(i) == FIREWALL_RULE_ALLOW) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1659 | exemptUids[numUids] = rules.keyAt(i); |
| 1660 | numUids++; |
| 1661 | } |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | // Normally, whitelist chains only contain deny rules, so numUids == exemptUids.length. |
| 1665 | // But the code does not guarantee this in any way, and at least in one case - if we add |
| 1666 | // a UID rule to the firewall, and then disable the firewall - the chains can contain |
| 1667 | // the wrong type of rule. In this case, don't close connections that we shouldn't. |
| 1668 | // |
| 1669 | // TODO: tighten up this code by ensuring we never set the wrong type of rule, and |
| 1670 | // fix setFirewallEnabled to grab mQuotaLock and clear rules. |
| 1671 | if (numUids != exemptUids.length) { |
| 1672 | exemptUids = Arrays.copyOf(exemptUids, numUids); |
| 1673 | } |
| 1674 | } else { |
| 1675 | // Close sockets for every UID that has a deny rule... |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1676 | synchronized (mRulesLock) { |
| 1677 | final SparseIntArray rules = getUidFirewallRulesLR(chain); |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1678 | ranges = new UidRangeParcel[rules.size()]; |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1679 | for (int i = 0; i < ranges.length; i++) { |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1680 | if (rules.valueAt(i) == FIREWALL_RULE_DENY) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1681 | int uid = rules.keyAt(i); |
Lorenzo Colitti | cc7f1db | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 1682 | ranges[numUids] = makeUidRangeParcel(uid, uid); |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1683 | numUids++; |
| 1684 | } |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | // As above; usually numUids == ranges.length, but not always. |
| 1688 | if (numUids != ranges.length) { |
| 1689 | ranges = Arrays.copyOf(ranges, numUids); |
| 1690 | } |
| 1691 | // ... with no exceptions. |
| 1692 | exemptUids = new int[0]; |
| 1693 | } |
| 1694 | |
| 1695 | try { |
| 1696 | mNetdService.socketDestroy(ranges, exemptUids); |
| 1697 | } catch(RemoteException | ServiceSpecificException e) { |
| 1698 | Slog.e(TAG, "Error closing sockets after enabling chain " + chainName + ": " + e); |
| 1699 | } |
| 1700 | } |
| 1701 | |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1702 | @Override |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1703 | public void setFirewallChainEnabled(int chain, boolean enable) { |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 1704 | enforceSystemUid(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1705 | synchronized (mQuotaLock) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1706 | synchronized (mRulesLock) { |
| 1707 | if (getFirewallChainState(chain) == enable) { |
| 1708 | // All is the same, nothing to do. This relies on the fact that netd has child |
| 1709 | // chains default detached. |
| 1710 | return; |
| 1711 | } |
| 1712 | setFirewallChainState(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1713 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1714 | |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1715 | final String chainName = getFirewallChainName(chain); |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1716 | if (chain == FIREWALL_CHAIN_NONE) { |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1717 | throw new IllegalArgumentException("Bad child chain: " + chainName); |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1718 | } |
| 1719 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1720 | try { |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1721 | mNetdService.firewallEnableChildChain(chain, enable); |
| 1722 | } catch (RemoteException | ServiceSpecificException e) { |
| 1723 | throw new IllegalStateException(e); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1724 | } |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1725 | |
| 1726 | // Close any sockets that were opened by the affected UIDs. This has to be done after |
| 1727 | // disabling network connectivity, in case they react to the socket close by reopening |
| 1728 | // the connection and race with the iptables commands that enable the firewall. All |
| 1729 | // whitelist and blacklist chains allow RSTs through. |
| 1730 | if (enable) { |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1731 | closeSocketsForFirewallChainLocked(chain, chainName); |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 1732 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1733 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1736 | private String getFirewallChainName(int chain) { |
| 1737 | switch (chain) { |
| 1738 | case FIREWALL_CHAIN_STANDBY: |
| 1739 | return FIREWALL_CHAIN_NAME_STANDBY; |
| 1740 | case FIREWALL_CHAIN_DOZABLE: |
| 1741 | return FIREWALL_CHAIN_NAME_DOZABLE; |
| 1742 | case FIREWALL_CHAIN_POWERSAVE: |
| 1743 | return FIREWALL_CHAIN_NAME_POWERSAVE; |
| 1744 | default: |
| 1745 | throw new IllegalArgumentException("Bad child chain: " + chain); |
| 1746 | } |
| 1747 | } |
| 1748 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1749 | private int getFirewallType(int chain) { |
| 1750 | switch (chain) { |
| 1751 | case FIREWALL_CHAIN_STANDBY: |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1752 | return FIREWALL_BLACKLIST; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1753 | case FIREWALL_CHAIN_DOZABLE: |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1754 | return FIREWALL_WHITELIST; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1755 | case FIREWALL_CHAIN_POWERSAVE: |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1756 | return FIREWALL_WHITELIST; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1757 | default: |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1758 | return isFirewallEnabled() ? FIREWALL_WHITELIST : FIREWALL_BLACKLIST; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | @Override |
| 1763 | public void setFirewallUidRules(int chain, int[] uids, int[] rules) { |
| 1764 | enforceSystemUid(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1765 | synchronized (mQuotaLock) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1766 | synchronized (mRulesLock) { |
| 1767 | SparseIntArray uidFirewallRules = getUidFirewallRulesLR(chain); |
| 1768 | SparseIntArray newRules = new SparseIntArray(); |
| 1769 | // apply new set of rules |
| 1770 | for (int index = uids.length - 1; index >= 0; --index) { |
| 1771 | int uid = uids[index]; |
| 1772 | int rule = rules[index]; |
| 1773 | updateFirewallUidRuleLocked(chain, uid, rule); |
| 1774 | newRules.put(uid, rule); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1775 | } |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1776 | // collect the rules to remove. |
| 1777 | SparseIntArray rulesToRemove = new SparseIntArray(); |
| 1778 | for (int index = uidFirewallRules.size() - 1; index >= 0; --index) { |
| 1779 | int uid = uidFirewallRules.keyAt(index); |
| 1780 | if (newRules.indexOfKey(uid) < 0) { |
| 1781 | rulesToRemove.put(uid, FIREWALL_RULE_DEFAULT); |
| 1782 | } |
| 1783 | } |
| 1784 | // remove dead rules |
| 1785 | for (int index = rulesToRemove.size() - 1; index >= 0; --index) { |
| 1786 | int uid = rulesToRemove.keyAt(index); |
| 1787 | updateFirewallUidRuleLocked(chain, uid, FIREWALL_RULE_DEFAULT); |
| 1788 | } |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1789 | } |
| 1790 | try { |
| 1791 | switch (chain) { |
| 1792 | case FIREWALL_CHAIN_DOZABLE: |
| 1793 | mNetdService.firewallReplaceUidChain("fw_dozable", true, uids); |
| 1794 | break; |
| 1795 | case FIREWALL_CHAIN_STANDBY: |
| 1796 | mNetdService.firewallReplaceUidChain("fw_standby", false, uids); |
| 1797 | break; |
| 1798 | case FIREWALL_CHAIN_POWERSAVE: |
| 1799 | mNetdService.firewallReplaceUidChain("fw_powersave", true, uids); |
| 1800 | break; |
| 1801 | case FIREWALL_CHAIN_NONE: |
| 1802 | default: |
| 1803 | Slog.d(TAG, "setFirewallUidRules() called on invalid chain: " + chain); |
| 1804 | } |
| 1805 | } catch (RemoteException e) { |
| 1806 | Slog.w(TAG, "Error flushing firewall chain " + chain, e); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1807 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | @Override |
| 1812 | public void setFirewallUidRule(int chain, int uid, int rule) { |
| 1813 | enforceSystemUid(); |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1814 | synchronized (mQuotaLock) { |
| 1815 | setFirewallUidRuleLocked(chain, uid, rule); |
| 1816 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1819 | private void setFirewallUidRuleLocked(int chain, int uid, int rule) { |
| 1820 | if (updateFirewallUidRuleLocked(chain, uid, rule)) { |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1821 | final int ruleType = getFirewallRuleType(chain, rule); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1822 | try { |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1823 | mNetdService.firewallSetUidRule(chain, uid, ruleType); |
| 1824 | } catch (RemoteException | ServiceSpecificException e) { |
| 1825 | throw new IllegalStateException(e); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1826 | } |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1830 | // TODO: now that netd supports batching, NMS should not keep these data structures anymore... |
| 1831 | private boolean updateFirewallUidRuleLocked(int chain, int uid, int rule) { |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1832 | synchronized (mRulesLock) { |
| 1833 | SparseIntArray uidFirewallRules = getUidFirewallRulesLR(chain); |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1834 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1835 | final int oldUidFirewallRule = uidFirewallRules.get(uid, FIREWALL_RULE_DEFAULT); |
| 1836 | if (DBG) { |
| 1837 | Slog.d(TAG, "oldRule = " + oldUidFirewallRule |
| 1838 | + ", newRule=" + rule + " for uid=" + uid + " on chain " + chain); |
| 1839 | } |
| 1840 | if (oldUidFirewallRule == rule) { |
| 1841 | if (DBG) Slog.d(TAG, "!!!!! Skipping change"); |
| 1842 | // TODO: eventually consider throwing |
| 1843 | return false; |
| 1844 | } |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1845 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1846 | String ruleName = getFirewallRuleName(chain, rule); |
| 1847 | String oldRuleName = getFirewallRuleName(chain, oldUidFirewallRule); |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1848 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1849 | if (rule == NetworkPolicyManager.FIREWALL_RULE_DEFAULT) { |
| 1850 | uidFirewallRules.delete(uid); |
| 1851 | } else { |
| 1852 | uidFirewallRules.put(uid, rule); |
| 1853 | } |
| 1854 | return !ruleName.equals(oldRuleName); |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1855 | } |
Felipe Leme | a701cad | 2016-05-12 09:58:14 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1858 | private @NonNull String getFirewallRuleName(int chain, int rule) { |
| 1859 | String ruleName; |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1860 | if (getFirewallType(chain) == FIREWALL_WHITELIST) { |
| 1861 | if (rule == FIREWALL_RULE_ALLOW) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1862 | ruleName = "allow"; |
| 1863 | } else { |
| 1864 | ruleName = "deny"; |
| 1865 | } |
| 1866 | } else { // Blacklist mode |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1867 | if (rule == FIREWALL_RULE_DENY) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 1868 | ruleName = "deny"; |
| 1869 | } else { |
| 1870 | ruleName = "allow"; |
| 1871 | } |
| 1872 | } |
| 1873 | return ruleName; |
| 1874 | } |
| 1875 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1876 | @GuardedBy("mRulesLock") |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1877 | private @NonNull SparseIntArray getUidFirewallRulesLR(int chain) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1878 | switch (chain) { |
| 1879 | case FIREWALL_CHAIN_STANDBY: |
| 1880 | return mUidFirewallStandbyRules; |
| 1881 | case FIREWALL_CHAIN_DOZABLE: |
| 1882 | return mUidFirewallDozableRules; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1883 | case FIREWALL_CHAIN_POWERSAVE: |
| 1884 | return mUidFirewallPowerSaveRules; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1885 | case FIREWALL_CHAIN_NONE: |
| 1886 | return mUidFirewallRules; |
| 1887 | default: |
| 1888 | throw new IllegalArgumentException("Unknown chain:" + chain); |
| 1889 | } |
| 1890 | } |
| 1891 | |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 1892 | private int getFirewallRuleType(int chain, int rule) { |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1893 | if (rule == NetworkPolicyManager.FIREWALL_RULE_DEFAULT) { |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 1894 | return getFirewallType(chain) == FIREWALL_WHITELIST |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1895 | ? INetd.FIREWALL_RULE_DENY : INetd.FIREWALL_RULE_ALLOW; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1896 | } |
Luke Huang | 615e102 | 2018-10-25 11:54:05 +0900 | [diff] [blame] | 1897 | return rule; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 1900 | private static void enforceSystemUid() { |
| 1901 | final int uid = Binder.getCallingUid(); |
| 1902 | if (uid != Process.SYSTEM_UID) { |
| 1903 | throw new SecurityException("Only available to AID_SYSTEM"); |
| 1904 | } |
| 1905 | } |
| 1906 | |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1907 | @Override |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1908 | public void registerNetworkActivityListener(INetworkActivityListener listener) { |
| 1909 | mNetworkActivityListeners.register(listener); |
| 1910 | } |
| 1911 | |
| 1912 | @Override |
| 1913 | public void unregisterNetworkActivityListener(INetworkActivityListener listener) { |
| 1914 | mNetworkActivityListeners.unregister(listener); |
| 1915 | } |
| 1916 | |
| 1917 | @Override |
| 1918 | public boolean isNetworkActive() { |
| 1919 | synchronized (mNetworkActivityListeners) { |
| 1920 | return mNetworkActive || mActiveIdleTimers.isEmpty(); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | private void reportNetworkActive() { |
| 1925 | final int length = mNetworkActivityListeners.beginBroadcast(); |
Robert Greenwalt | 2c9f547 | 2014-04-21 14:50:28 -0700 | [diff] [blame] | 1926 | try { |
| 1927 | for (int i = 0; i < length; i++) { |
| 1928 | try { |
| 1929 | mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 1930 | } catch (RemoteException | RuntimeException e) { |
Robert Greenwalt | 2c9f547 | 2014-04-21 14:50:28 -0700 | [diff] [blame] | 1931 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1932 | } |
Robert Greenwalt | 2c9f547 | 2014-04-21 14:50:28 -0700 | [diff] [blame] | 1933 | } finally { |
| 1934 | mNetworkActivityListeners.finishBroadcast(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1935 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1938 | @Override |
| 1939 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1940 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1941 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 1942 | pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio); |
| 1943 | pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio); |
| 1944 | pw.print("mNetworkActive="); pw.println(mNetworkActive); |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1945 | |
| 1946 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1947 | pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString()); |
| 1948 | pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString()); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1949 | pw.print("Data saver mode: "); pw.println(mDataSaverMode); |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1950 | synchronized (mRulesLock) { |
| 1951 | dumpUidRuleOnQuotaLocked(pw, "blacklist", mUidRejectOnMetered); |
| 1952 | dumpUidRuleOnQuotaLocked(pw, "whitelist", mUidAllowOnMetered); |
| 1953 | } |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1954 | } |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1955 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1956 | synchronized (mRulesLock) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1957 | dumpUidFirewallRule(pw, "", mUidFirewallRules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1958 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1959 | pw.print("UID firewall standby chain enabled: "); pw.println( |
| 1960 | getFirewallChainState(FIREWALL_CHAIN_STANDBY)); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1961 | dumpUidFirewallRule(pw, FIREWALL_CHAIN_NAME_STANDBY, mUidFirewallStandbyRules); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1962 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1963 | pw.print("UID firewall dozable chain enabled: "); pw.println( |
| 1964 | getFirewallChainState(FIREWALL_CHAIN_DOZABLE)); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1965 | dumpUidFirewallRule(pw, FIREWALL_CHAIN_NAME_DOZABLE, mUidFirewallDozableRules); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1966 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 1967 | pw.println("UID firewall powersave chain enabled: " + |
| 1968 | getFirewallChainState(FIREWALL_CHAIN_POWERSAVE)); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 1969 | dumpUidFirewallRule(pw, FIREWALL_CHAIN_NAME_POWERSAVE, mUidFirewallPowerSaveRules); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1972 | synchronized (mIdleTimerLock) { |
| 1973 | pw.println("Idle timers:"); |
| 1974 | for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) { |
| 1975 | pw.print(" "); pw.print(ent.getKey()); pw.println(":"); |
| 1976 | IdleTimerParams params = ent.getValue(); |
| 1977 | pw.print(" timeout="); pw.print(params.timeout); |
| 1978 | pw.print(" type="); pw.print(params.type); |
| 1979 | pw.print(" networkCount="); pw.println(params.networkCount); |
| 1980 | } |
| 1981 | } |
| 1982 | |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 1983 | pw.print("Firewall enabled: "); pw.println(mFirewallEnabled); |
Felipe Leme | 65be302 | 2016-03-22 14:53:13 -0700 | [diff] [blame] | 1984 | pw.print("Netd service status: " ); |
| 1985 | if (mNetdService == null) { |
| 1986 | pw.println("disconnected"); |
| 1987 | } else { |
| 1988 | try { |
| 1989 | final boolean alive = mNetdService.isAlive(); |
| 1990 | pw.println(alive ? "alive": "dead"); |
| 1991 | } catch (RemoteException e) { |
| 1992 | pw.println("unreachable"); |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | private void dumpUidRuleOnQuotaLocked(PrintWriter pw, String name, SparseBooleanArray list) { |
| 1998 | pw.print("UID bandwith control "); |
| 1999 | pw.print(name); |
| 2000 | pw.print(" rule: ["); |
| 2001 | final int size = list.size(); |
| 2002 | for (int i = 0; i < size; i++) { |
| 2003 | pw.print(list.keyAt(i)); |
| 2004 | if (i < size - 1) pw.print(","); |
| 2005 | } |
| 2006 | pw.println("]"); |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 2007 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2008 | |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2009 | private void dumpUidFirewallRule(PrintWriter pw, String name, SparseIntArray rules) { |
Lorenzo Colitti | 4cb4240 | 2016-04-24 12:52:00 +0900 | [diff] [blame] | 2010 | pw.print("UID firewall "); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2011 | pw.print(name); |
| 2012 | pw.print(" rule: ["); |
| 2013 | final int size = rules.size(); |
| 2014 | for (int i = 0; i < size; i++) { |
| 2015 | pw.print(rules.keyAt(i)); |
| 2016 | pw.print(":"); |
| 2017 | pw.print(rules.valueAt(i)); |
| 2018 | if (i < size - 1) pw.print(","); |
| 2019 | } |
| 2020 | pw.println("]"); |
| 2021 | } |
| 2022 | |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2023 | @Override |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 2024 | public void addInterfaceToNetwork(String iface, int netId) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2025 | modifyInterfaceInNetwork(MODIFY_OPERATION_ADD, netId, iface); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | @Override |
| 2029 | public void removeInterfaceFromNetwork(String iface, int netId) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2030 | modifyInterfaceInNetwork(MODIFY_OPERATION_REMOVE, netId, iface); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2031 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 2032 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2033 | private void modifyInterfaceInNetwork(boolean add, int netId, String iface) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2034 | NetworkStack.checkNetworkStackPermission(mContext); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 2035 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2036 | if (add) { |
| 2037 | mNetdService.networkAddInterface(netId, iface); |
| 2038 | } else { |
| 2039 | mNetdService.networkRemoveInterface(netId, iface); |
| 2040 | } |
| 2041 | } catch (RemoteException | ServiceSpecificException e) { |
| 2042 | throw new IllegalStateException(e); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | @Override |
Robert Greenwalt | 913c895 | 2014-04-07 17:36:35 -0700 | [diff] [blame] | 2047 | public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2048 | NetworkStack.checkNetworkStackPermission(mContext); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2049 | |
Sreeram Ramachandran | cc91c7b | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 2050 | final LinkAddress la = routeInfo.getDestinationLinkAddress(); |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2051 | final String ifName = routeInfo.getInterface(); |
| 2052 | final String dst = la.toString(); |
| 2053 | final String nextHop; |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2054 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2055 | if (routeInfo.hasGateway()) { |
| 2056 | nextHop = routeInfo.getGateway().getHostAddress(); |
| 2057 | } else { |
| 2058 | nextHop = ""; |
| 2059 | } |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2060 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2061 | mNetdService.networkAddLegacyRoute(netId, ifName, dst, nextHop, uid); |
| 2062 | } catch (RemoteException | ServiceSpecificException e) { |
| 2063 | throw new IllegalStateException(e); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | @Override |
Sreeram Ramachandran | f047f2a | 2014-04-15 16:04:26 -0700 | [diff] [blame] | 2068 | public void setDefaultNetId(int netId) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2069 | NetworkStack.checkNetworkStackPermission(mContext); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2070 | |
| 2071 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2072 | mNetdService.networkSetDefault(netId); |
| 2073 | } catch (RemoteException | ServiceSpecificException e) { |
| 2074 | throw new IllegalStateException(e); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | @Override |
| 2079 | public void clearDefaultNetId() { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2080 | NetworkStack.checkNetworkStackPermission(mContext); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2081 | |
| 2082 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2083 | mNetdService.networkClearDefault(); |
| 2084 | } catch (RemoteException | ServiceSpecificException e) { |
| 2085 | throw new IllegalStateException(e); |
Robert Greenwalt | 568891d | 2014-04-04 13:38:00 -0700 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | @Override |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2090 | public void setNetworkPermission(int netId, int permission) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2091 | NetworkStack.checkNetworkStackPermission(mContext); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 2092 | |
| 2093 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2094 | mNetdService.networkSetPermissionForNetwork(netId, permission); |
| 2095 | } catch (RemoteException | ServiceSpecificException e) { |
| 2096 | throw new IllegalStateException(e); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2100 | @Override |
| 2101 | public void allowProtect(int uid) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2102 | NetworkStack.checkNetworkStackPermission(mContext); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2103 | |
| 2104 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2105 | mNetdService.networkSetProtectAllow(uid); |
| 2106 | } catch (RemoteException | ServiceSpecificException e) { |
| 2107 | throw new IllegalStateException(e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | @Override |
| 2112 | public void denyProtect(int uid) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2113 | NetworkStack.checkNetworkStackPermission(mContext); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2114 | |
| 2115 | try { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 2116 | mNetdService.networkSetProtectDeny(uid); |
| 2117 | } catch (RemoteException | ServiceSpecificException e) { |
| 2118 | throw new IllegalStateException(e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2122 | @Override |
| 2123 | public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) { |
Luke Huang | 706d7ab | 2018-10-16 15:42:15 +0800 | [diff] [blame] | 2124 | modifyInterfaceInNetwork(MODIFY_OPERATION_ADD, INetd.LOCAL_NET_ID, iface); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2125 | |
| 2126 | for (RouteInfo route : routes) { |
| 2127 | if (!route.isDefaultRoute()) { |
Luke Huang | 706d7ab | 2018-10-16 15:42:15 +0800 | [diff] [blame] | 2128 | modifyRoute(MODIFY_OPERATION_ADD, INetd.LOCAL_NET_ID, route); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2129 | } |
| 2130 | } |
Jimmy Chen | 086779d | 2019-03-07 14:15:36 +0800 | [diff] [blame] | 2131 | |
| 2132 | // IPv6 link local should be activated always. |
| 2133 | modifyRoute(MODIFY_OPERATION_ADD, INetd.LOCAL_NET_ID, |
| 2134 | new RouteInfo(new IpPrefix("fe80::/64"), null, iface)); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | @Override |
| 2138 | public void removeInterfaceFromLocalNetwork(String iface) { |
Luke Huang | 706d7ab | 2018-10-16 15:42:15 +0800 | [diff] [blame] | 2139 | modifyInterfaceInNetwork(MODIFY_OPERATION_REMOVE, INetd.LOCAL_NET_ID, iface); |
Sreeram Ramachandran | a77760d | 2014-07-17 17:09:07 -0700 | [diff] [blame] | 2140 | } |
Erik Kline | 6599ee8 | 2016-07-17 21:28:39 +0900 | [diff] [blame] | 2141 | |
| 2142 | @Override |
| 2143 | public int removeRoutesFromLocalNetwork(List<RouteInfo> routes) { |
| 2144 | int failures = 0; |
| 2145 | |
| 2146 | for (RouteInfo route : routes) { |
| 2147 | try { |
Luke Huang | 706d7ab | 2018-10-16 15:42:15 +0800 | [diff] [blame] | 2148 | modifyRoute(MODIFY_OPERATION_REMOVE, INetd.LOCAL_NET_ID, route); |
Erik Kline | 6599ee8 | 2016-07-17 21:28:39 +0900 | [diff] [blame] | 2149 | } catch (IllegalStateException e) { |
| 2150 | failures++; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | return failures; |
| 2155 | } |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 2156 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 2157 | @Override |
| 2158 | public boolean isNetworkRestricted(int uid) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2159 | mContext.enforceCallingOrSelfPermission(OBSERVE_NETWORK_POLICY, TAG); |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 2160 | return isNetworkRestrictedInternal(uid); |
| 2161 | } |
| 2162 | |
| 2163 | private boolean isNetworkRestrictedInternal(int uid) { |
| 2164 | synchronized (mRulesLock) { |
| 2165 | if (getFirewallChainState(FIREWALL_CHAIN_STANDBY) |
| 2166 | && mUidFirewallStandbyRules.get(uid) == FIREWALL_RULE_DENY) { |
| 2167 | if (DBG) Slog.d(TAG, "Uid " + uid + " restricted because of app standby mode"); |
| 2168 | return true; |
| 2169 | } |
| 2170 | if (getFirewallChainState(FIREWALL_CHAIN_DOZABLE) |
| 2171 | && mUidFirewallDozableRules.get(uid) != FIREWALL_RULE_ALLOW) { |
| 2172 | if (DBG) Slog.d(TAG, "Uid " + uid + " restricted because of device idle mode"); |
| 2173 | return true; |
| 2174 | } |
| 2175 | if (getFirewallChainState(FIREWALL_CHAIN_POWERSAVE) |
| 2176 | && mUidFirewallPowerSaveRules.get(uid) != FIREWALL_RULE_ALLOW) { |
| 2177 | if (DBG) Slog.d(TAG, "Uid " + uid + " restricted because of power saver mode"); |
| 2178 | return true; |
| 2179 | } |
| 2180 | if (mUidRejectOnMetered.get(uid)) { |
| 2181 | if (DBG) Slog.d(TAG, "Uid " + uid + " restricted because of no metered data" |
| 2182 | + " in the background"); |
| 2183 | return true; |
| 2184 | } |
| 2185 | if (mDataSaverMode && !mUidAllowOnMetered.get(uid)) { |
| 2186 | if (DBG) Slog.d(TAG, "Uid " + uid + " restricted because of data saver mode"); |
| 2187 | return true; |
| 2188 | } |
| 2189 | return false; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 2193 | private void setFirewallChainState(int chain, boolean state) { |
| 2194 | synchronized (mRulesLock) { |
| 2195 | mFirewallChainStates.put(chain, state); |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | private boolean getFirewallChainState(int chain) { |
| 2200 | synchronized (mRulesLock) { |
| 2201 | return mFirewallChainStates.get(chain); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | @VisibleForTesting |
| 2206 | class LocalService extends NetworkManagementInternal { |
| 2207 | @Override |
| 2208 | public boolean isNetworkRestrictedForUid(int uid) { |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 2209 | return isNetworkRestrictedInternal(uid); |
Sudheer Shanka | 62f5c17 | 2017-03-17 16:25:55 -0700 | [diff] [blame] | 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | @VisibleForTesting |
| 2214 | Injector getInjector() { |
| 2215 | return new Injector(); |
| 2216 | } |
| 2217 | |
| 2218 | @VisibleForTesting |
| 2219 | class Injector { |
| 2220 | void setDataSaverMode(boolean dataSaverMode) { |
| 2221 | mDataSaverMode = dataSaverMode; |
| 2222 | } |
| 2223 | |
| 2224 | void setFirewallChainState(int chain, boolean state) { |
| 2225 | NetworkManagementService.this.setFirewallChainState(chain, state); |
| 2226 | } |
| 2227 | |
| 2228 | void setFirewallRule(int chain, int uid, int rule) { |
| 2229 | synchronized (mRulesLock) { |
| 2230 | getUidFirewallRulesLR(chain).put(uid, rule); |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | void setUidOnMeteredNetworkList(boolean blacklist, int uid, boolean enable) { |
| 2235 | synchronized (mRulesLock) { |
| 2236 | if (blacklist) { |
| 2237 | mUidRejectOnMetered.put(uid, enable); |
| 2238 | } else { |
| 2239 | mUidAllowOnMetered.put(uid, enable); |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | void reset() { |
| 2245 | synchronized (mRulesLock) { |
| 2246 | setDataSaverMode(false); |
| 2247 | final int[] chains = { |
| 2248 | FIREWALL_CHAIN_DOZABLE, |
| 2249 | FIREWALL_CHAIN_STANDBY, |
| 2250 | FIREWALL_CHAIN_POWERSAVE |
| 2251 | }; |
| 2252 | for (int chain : chains) { |
| 2253 | setFirewallChainState(chain, false); |
| 2254 | getUidFirewallRulesLR(chain).clear(); |
| 2255 | } |
| 2256 | mUidAllowOnMetered.clear(); |
| 2257 | mUidRejectOnMetered.clear(); |
| 2258 | } |
| 2259 | } |
| 2260 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 2261 | } |