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