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