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