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