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