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