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