blob: 2434f71256470203b1cd08fecec2833027a8a51a [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
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
17package com.android.server;
18
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -070019import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkey4529bb62011-12-14 10:31:54 -080020import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070021import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080022import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070023import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080024import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.NetworkStats.TAG_NONE;
26import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070027import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090028import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080029import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080031import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070035import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080036import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070037import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
San Mehat873f2142010-01-14 10:25:07 -080039import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080041import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090043import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070045import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080046import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070047import android.net.RouteInfo;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040048import android.net.UidRange;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080049import android.net.wifi.WifiConfiguration;
50import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070051import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070052import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070053import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080054import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080055import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080056import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070057import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080058import android.os.RemoteCallbackList;
59import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070060import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070061import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080062import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070063import android.telephony.DataConnectionRealTimeInfo;
64import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070065import android.telephony.SubscriptionManager;
Wink Saville67e07892014-06-18 16:43:14 -070066import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080067import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070069import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080070
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070071import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070072import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070073import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080074import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070075import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070076import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070077import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070078
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070079import java.io.BufferedReader;
80import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080081import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070082import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070084import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070085import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070086import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070087import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070088import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070089import java.net.InterfaceAddress;
90import java.net.NetworkInterface;
91import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092import java.util.ArrayList;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040093import java.util.Arrays;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070094import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080095import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070096import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070097import java.util.NoSuchElementException;
98import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070099import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -0800100
101/**
102 * @hide
103 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700104public class NetworkManagementService extends INetworkManagementService.Stub
105 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700106 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700107 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700108 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900109 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700110
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800111 private static final String ADD = "add";
112 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700113
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700114 private static final String ALLOW = "allow";
115 private static final String DENY = "deny";
116
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700117 private static final String DEFAULT = "default";
118 private static final String SECONDARY = "secondary";
119
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400120 private static final int MAX_UID_RANGES_PER_COMMAND = 10;
121
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700122 /**
123 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
124 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
125 */
126 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
127
San Mehat873f2142010-01-14 10:25:07 -0800128 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700129 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800130 public static final int InterfaceListResult = 110;
131 public static final int TetherInterfaceListResult = 111;
132 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800133 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700134 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800135
136 public static final int TetherStatusResult = 210;
137 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800138 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800139 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700140 public static final int InterfaceRxCounterResult = 216;
141 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700142 public static final int QuotaCounterResult = 220;
143 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800144 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900145 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800146
147 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700148 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700149 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900150 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900151 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900152 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800153 }
154
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700155 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
156
San Mehat873f2142010-01-14 10:25:07 -0800157 /**
158 * Binder context for this service
159 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700160 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800161
162 /**
163 * connector object for communicating with netd
164 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700165 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800166
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700167 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700168 private final Handler mDaemonHandler;
169 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700170
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800171 private IBatteryStats mBatteryStats;
172
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700173 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700174 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700175
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800176 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
177 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800178
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700179 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
180
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700181 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700182 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700183 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700184 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700185 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700186 /** Set of UIDs with active reject rules. */
187 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
188
Haoyu Bai04124232012-06-28 15:26:19 -0700189 private Object mIdleTimerLock = new Object();
190 /** Set of interfaces with active idle timers. */
191 private static class IdleTimerParams {
192 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800193 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700194 public int networkCount;
195
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800196 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700197 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800198 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700199 this.networkCount = 1;
200 }
201 }
202 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
203
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700204 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700205 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700206
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700207 private boolean mMobileActivityFromRadio = false;
208 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
209
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800210 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
211 new RemoteCallbackList<INetworkActivityListener>();
212 private boolean mNetworkActive;
213
San Mehat873f2142010-01-14 10:25:07 -0800214 /**
215 * Constructs a new NetworkManagementService instance
216 *
217 * @param context Binder context for this service
218 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900219 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800220 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800221
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700222 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
223 mFgHandler = new Handler(FgThread.get().getLooper());
224
Marco Nelissen62dbb222010-02-18 10:56:30 -0800225 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700226 mConnector = null;
227 mThread = null;
228 mDaemonHandler = null;
229 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800230 return;
231 }
232
Dianne Hackborn4590e522014-03-24 13:36:46 -0700233 // Don't need this wake lock, since we now have a time stamp for when
234 // the network actually went inactive. (It might be nice to still do this,
235 // but I don't want to do it through the power manager because that pollutes the
236 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700237 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700238 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800239
San Mehat873f2142010-01-14 10:25:07 -0800240 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700241 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
242 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700243 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700244
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700245 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Saville67e07892014-06-18 16:43:14 -0700246
247 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUB_ID,
Wink Savillefb40dd42014-06-12 17:02:31 -0700248 mDaemonHandler.getLooper()) {
Wink Saville67e07892014-06-18 16:43:14 -0700249 @Override
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700250 public void onDataConnectionRealTimeInfoChanged(
251 DataConnectionRealTimeInfo dcRtInfo) {
Wink Saville67e07892014-06-18 16:43:14 -0700252 if (DBG) Slog.d(TAG, "onDataConnectionRealTimeInfoChanged: " + dcRtInfo);
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700253 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
254 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700255 }
256 };
Wink Saville67e07892014-06-18 16:43:14 -0700257 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
258 if (tm != null) {
259 tm.listen(mPhoneStateListener,
260 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO);
261 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700262
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700263 // Add ourself to the Watchdog monitors.
264 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700265 }
266
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900267 static NetworkManagementService create(Context context,
268 String socket) throws InterruptedException {
269 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700270 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700271 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
272 service.mThread.start();
273 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700274 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700275 if (DBG) Slog.d(TAG, "Connected");
276 return service;
San Mehat873f2142010-01-14 10:25:07 -0800277 }
278
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900279 public static NetworkManagementService create(Context context) throws InterruptedException {
280 return create(context, NETD_SOCKET_NAME);
281 }
282
Jeff Sharkey350083e2011-06-29 10:45:16 -0700283 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700284 prepareNativeDaemon();
285 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700286 }
287
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800288 private IBatteryStats getBatteryStats() {
289 synchronized (this) {
290 if (mBatteryStats != null) {
291 return mBatteryStats;
292 }
293 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
294 BatteryStats.SERVICE_NAME));
295 return mBatteryStats;
296 }
297 }
298
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800299 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800301 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800302 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800303 }
304
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800305 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800306 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800307 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800308 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800309 }
310
311 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700312 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800313 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700314 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800315 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700316 try {
317 for (int i = 0; i < length; i++) {
318 try {
319 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
320 } catch (RemoteException e) {
321 } catch (RuntimeException e) {
322 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700323 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700324 } finally {
325 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700326 }
327 }
328
329 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700330 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700331 * (typically, an Ethernet cable has been plugged-in or unplugged).
332 */
333 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800334 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700335 try {
336 for (int i = 0; i < length; i++) {
337 try {
338 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
339 } catch (RemoteException e) {
340 } catch (RuntimeException e) {
341 }
San Mehat4d02d002010-01-22 16:07:46 -0800342 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700343 } finally {
344 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800345 }
346 }
347
348 /**
349 * Notify our observers of an interface addition.
350 */
351 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800352 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700353 try {
354 for (int i = 0; i < length; i++) {
355 try {
356 mObservers.getBroadcastItem(i).interfaceAdded(iface);
357 } catch (RemoteException e) {
358 } catch (RuntimeException e) {
359 }
San Mehat4d02d002010-01-22 16:07:46 -0800360 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700361 } finally {
362 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800363 }
364 }
365
366 /**
367 * Notify our observers of an interface removal.
368 */
369 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700370 // netd already clears out quota and alerts for removed ifaces; update
371 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700372 mActiveAlerts.remove(iface);
373 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700374
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800375 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700376 try {
377 for (int i = 0; i < length; i++) {
378 try {
379 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
380 } catch (RemoteException e) {
381 } catch (RuntimeException e) {
382 }
San Mehat4d02d002010-01-22 16:07:46 -0800383 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700384 } finally {
385 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800386 }
387 }
388
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700389 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700390 * Notify our observers of a limit reached.
391 */
392 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800393 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700394 try {
395 for (int i = 0; i < length; i++) {
396 try {
397 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
398 } catch (RemoteException e) {
399 } catch (RuntimeException e) {
400 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700401 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700402 } finally {
403 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700404 }
405 }
406
407 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700408 * Notify our observers of a change in the data activity state of the interface
409 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700410 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
411 boolean fromRadio) {
412 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
413 if (isMobile) {
414 if (!fromRadio) {
415 if (mMobileActivityFromRadio) {
416 // If this call is not coming from a report from the radio itself, but we
417 // have previously received reports from the radio, then we will take the
418 // power state to just be whatever the radio last reported.
419 powerState = mLastPowerStateFromRadio;
420 }
421 } else {
422 mMobileActivityFromRadio = true;
423 }
424 if (mLastPowerStateFromRadio != powerState) {
425 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700426 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700427 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700428 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700429 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700430 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700431 }
432
433 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
434 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
435
436 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
437 // Report the change in data activity. We don't do this if this is a change
438 // on the mobile network, that is not coming from the radio itself, and we
439 // have previously seen change reports from the radio. In that case only
440 // the radio is the authority for the current state.
441 final int length = mObservers.beginBroadcast();
442 try {
443 for (int i = 0; i < length; i++) {
444 try {
445 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
446 Integer.toString(type), isActive, tsNanos);
447 } catch (RemoteException e) {
448 } catch (RuntimeException e) {
449 }
450 }
451 } finally {
452 mObservers.finishBroadcast();
453 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700454 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800455
456 boolean report = false;
457 synchronized (mIdleTimerLock) {
458 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700459 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800460 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700461 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800462 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700463 if (mNetworkActive != isActive) {
464 mNetworkActive = isActive;
465 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800466 }
467 }
468 if (report) {
469 reportNetworkActive();
470 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700471 }
472
473 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700474 * Prepare native daemon once connected, enabling modules and pushing any
475 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700476 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700477 private void prepareNativeDaemon() {
478 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700479
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700480 // only enable bandwidth control when support exists
481 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
482 if (hasKernelSupport) {
483 Slog.d(TAG, "enabling bandwidth control");
484 try {
485 mConnector.execute("bandwidth", "enable");
486 mBandwidthControlEnabled = true;
487 } catch (NativeDaemonConnectorException e) {
488 Log.wtf(TAG, "problem enabling bandwidth controls", e);
489 }
490 } else {
491 Slog.d(TAG, "not enabling bandwidth control");
492 }
493
494 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
495
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700496 if (mBandwidthControlEnabled) {
497 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800498 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700499 } catch (RemoteException e) {
500 }
501 }
502
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700503 // push any existing quota or UID rules
504 synchronized (mQuotaLock) {
505 int size = mActiveQuotas.size();
506 if (size > 0) {
507 Slog.d(TAG, "pushing " + size + " active quota rules");
508 final HashMap<String, Long> activeQuotas = mActiveQuotas;
509 mActiveQuotas = Maps.newHashMap();
510 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
511 setInterfaceQuota(entry.getKey(), entry.getValue());
512 }
513 }
514
515 size = mActiveAlerts.size();
516 if (size > 0) {
517 Slog.d(TAG, "pushing " + size + " active alert rules");
518 final HashMap<String, Long> activeAlerts = mActiveAlerts;
519 mActiveAlerts = Maps.newHashMap();
520 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
521 setInterfaceAlert(entry.getKey(), entry.getValue());
522 }
523 }
524
525 size = mUidRejectOnQuota.size();
526 if (size > 0) {
527 Slog.d(TAG, "pushing " + size + " active uid rules");
528 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
529 mUidRejectOnQuota = new SparseBooleanArray();
530 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
531 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
532 }
533 }
534 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700535
536 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700537 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700538 }
San Mehat4d02d002010-01-22 16:07:46 -0800539
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900540 /**
541 * Notify our observers of a new or updated interface address.
542 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900543 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900544 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700545 try {
546 for (int i = 0; i < length; i++) {
547 try {
548 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
549 } catch (RemoteException e) {
550 } catch (RuntimeException e) {
551 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900552 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700553 } finally {
554 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900555 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900556 }
557
558 /**
559 * Notify our observers of a deleted interface address.
560 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900561 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900562 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700563 try {
564 for (int i = 0; i < length; i++) {
565 try {
566 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
567 } catch (RemoteException e) {
568 } catch (RuntimeException e) {
569 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900570 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700571 } finally {
572 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900573 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900574 }
575
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900576 /**
577 * Notify our observers of DNS server information received.
578 */
579 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
580 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700581 try {
582 for (int i = 0; i < length; i++) {
583 try {
584 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
585 addresses);
586 } catch (RemoteException e) {
587 } catch (RuntimeException e) {
588 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900589 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700590 } finally {
591 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900592 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900593 }
594
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900595 /**
596 * Notify our observers of a route change.
597 */
598 private void notifyRouteChange(String action, RouteInfo route) {
599 final int length = mObservers.beginBroadcast();
600 try {
601 for (int i = 0; i < length; i++) {
602 try {
603 if (action.equals("updated")) {
604 mObservers.getBroadcastItem(i).routeUpdated(route);
605 } else {
606 mObservers.getBroadcastItem(i).routeRemoved(route);
607 }
608 } catch (RemoteException e) {
609 } catch (RuntimeException e) {
610 }
611 }
612 } finally {
613 mObservers.finishBroadcast();
614 }
615 }
616
San Mehat873f2142010-01-14 10:25:07 -0800617 //
618 // Netd Callback handling
619 //
620
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700621 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
622 @Override
San Mehat873f2142010-01-14 10:25:07 -0800623 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700624 // event is dispatched from internal NDC thread, so we prepare the
625 // daemon back on main thread.
626 if (mConnectedSignal != null) {
627 mConnectedSignal.countDown();
628 mConnectedSignal = null;
629 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700630 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700631 @Override
632 public void run() {
633 prepareNativeDaemon();
634 }
635 });
636 }
San Mehat873f2142010-01-14 10:25:07 -0800637 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700638
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700639 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800640 public boolean onCheckHoldWakeLock(int code) {
641 return code == NetdResponseCode.InterfaceClassActivity;
642 }
643
644 @Override
San Mehat873f2142010-01-14 10:25:07 -0800645 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900646 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700647 switch (code) {
648 case NetdResponseCode.InterfaceChange:
649 /*
650 * a network interface change occured
651 * Format: "NNN Iface added <name>"
652 * "NNN Iface removed <name>"
653 * "NNN Iface changed <name> <up/down>"
654 * "NNN Iface linkstatus <name> <up/down>"
655 */
656 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900657 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700658 }
659 if (cooked[2].equals("added")) {
660 notifyInterfaceAdded(cooked[3]);
661 return true;
662 } else if (cooked[2].equals("removed")) {
663 notifyInterfaceRemoved(cooked[3]);
664 return true;
665 } else if (cooked[2].equals("changed") && cooked.length == 5) {
666 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
667 return true;
668 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
669 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
670 return true;
671 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900672 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700673 // break;
674 case NetdResponseCode.BandwidthControl:
675 /*
676 * Bandwidth control needs some attention
677 * Format: "NNN limit alert <alertName> <ifaceName>"
678 */
679 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900680 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700681 }
682 if (cooked[2].equals("alert")) {
683 notifyLimitReached(cooked[3], cooked[4]);
684 return true;
685 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900686 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700687 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700688 case NetdResponseCode.InterfaceClassActivity:
689 /*
690 * An network interface class state changed (active/idle)
691 * Format: "NNN IfaceClass <active/idle> <label>"
692 */
693 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900694 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700695 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700696 long timestampNanos = 0;
697 if (cooked.length == 5) {
698 try {
699 timestampNanos = Long.parseLong(cooked[4]);
700 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700701 } else {
702 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700703 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700704 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700705 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700706 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
707 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700708 return true;
709 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900710 case NetdResponseCode.InterfaceAddressChange:
711 /*
712 * A network address change occurred
713 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
714 * "NNN Address removed <addr> <iface> <flags> <scope>"
715 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900716 if (cooked.length < 7 || !cooked[1].equals("Address")) {
717 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900718 }
719
Lorenzo Colitti64483942013-11-15 18:43:52 +0900720 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900721 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900722 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900723 int flags = Integer.parseInt(cooked[5]);
724 int scope = Integer.parseInt(cooked[6]);
725 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900726 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
727 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900728 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900729 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900730 }
731
732 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900733 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900734 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900735 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900736 }
737 return true;
738 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900739 case NetdResponseCode.InterfaceDnsServerInfo:
740 /*
741 * Information about available DNS servers has been received.
742 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
743 */
744 long lifetime; // Actually a 32-bit unsigned integer.
745
746 if (cooked.length == 6 &&
747 cooked[1].equals("DnsInfo") &&
748 cooked[2].equals("servers")) {
749 try {
750 lifetime = Long.parseLong(cooked[4]);
751 } catch (NumberFormatException e) {
752 throw new IllegalStateException(errorMessage);
753 }
754 String[] servers = cooked[5].split(",");
755 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
756 }
757 return true;
758 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900759 case NetdResponseCode.RouteChange:
760 /*
761 * A route has been updated or removed.
762 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
763 */
764 if (!cooked[1].equals("Route") || cooked.length < 6) {
765 throw new IllegalStateException(errorMessage);
766 }
767
768 String via = null;
769 String dev = null;
770 boolean valid = true;
771 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
772 if (cooked[i].equals("dev")) {
773 if (dev == null) {
774 dev = cooked[i+1];
775 } else {
776 valid = false; // Duplicate interface.
777 }
778 } else if (cooked[i].equals("via")) {
779 if (via == null) {
780 via = cooked[i+1];
781 } else {
782 valid = false; // Duplicate gateway.
783 }
784 } else {
785 valid = false; // Unknown syntax.
786 }
787 }
788 if (valid) {
789 try {
790 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
791 InetAddress gateway = null;
792 if (via != null) gateway = InetAddress.parseNumericAddress(via);
793 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
794 notifyRouteChange(cooked[2], route);
795 return true;
796 } catch (IllegalArgumentException e) {}
797 }
798 throw new IllegalStateException(errorMessage);
799 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700800 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800801 }
802 return false;
San Mehat873f2142010-01-14 10:25:07 -0800803 }
804 }
805
San Mehated4fc8a2010-01-22 12:28:36 -0800806
San Mehat873f2142010-01-14 10:25:07 -0800807 //
808 // INetworkManagementService members
809 //
810
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800811 @Override
812 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800813 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700814 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800815 return NativeDaemonEvent.filterMessageList(
816 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700817 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800818 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700819 }
San Mehated4fc8a2010-01-22 12:28:36 -0800820 }
821
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800822 @Override
823 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800824 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800825
826 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700827 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800828 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700829 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800830 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700831 }
San Mehated4fc8a2010-01-22 12:28:36 -0800832
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800833 event.checkCode(InterfaceGetCfgResult);
834
835 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
836 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800837
Kenny Roota80ce062010-06-01 13:23:53 -0700838 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800839 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700840 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800841 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800842 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800843 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700844 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800845 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800846 } catch (IllegalArgumentException iae) {
847 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700848 }
849
850 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800851 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800852 } catch (NumberFormatException nfe) {
853 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700854 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800855
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800856 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
857 while (st.hasMoreTokens()) {
858 cfg.setFlag(st.nextToken());
859 }
Kenny Roota80ce062010-06-01 13:23:53 -0700860 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800861 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800862 }
San Mehated4fc8a2010-01-22 12:28:36 -0800863 return cfg;
864 }
865
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800866 @Override
867 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800868 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800869 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800870 if (linkAddr == null || linkAddr.getAddress() == null) {
871 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800872 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800873
874 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800875 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900876 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800877 for (String flag : cfg.getFlags()) {
878 cmd.appendArg(flag);
879 }
880
Kenny Roota80ce062010-06-01 13:23:53 -0700881 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800882 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700883 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800884 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700885 }
San Mehat873f2142010-01-14 10:25:07 -0800886 }
887
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800888 @Override
889 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800890 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800891 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800892 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800893 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700894 }
895
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800896 @Override
897 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800898 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800899 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800900 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800901 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700902 }
903
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800904 @Override
905 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800906 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700907 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 mConnector.execute(
909 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700910 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800911 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700912 }
913 }
914
Irfan Sherifff5600612011-06-16 10:26:28 -0700915 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
916 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800917 @Override
918 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800919 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700920 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700922 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800923 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700924 }
925 }
926
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800927 @Override
928 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700930 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700932 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800933 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700934 }
935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
938 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800939 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700940 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800941 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700942 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800943 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700944 }
945 }
946
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700948 public void addRoute(int netId, RouteInfo route) {
949 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700950 }
951
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800952 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700953 public void removeRoute(int netId, RouteInfo route) {
954 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700955 }
956
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700957 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800958 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700959
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700960 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700961
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700962 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700963 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700964 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900965 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700966 if (route.hasGateway()) {
967 cmd.appendArg(route.getGateway().getHostAddress());
968 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700969
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800970 try {
971 mConnector.execute(cmd);
972 } catch (NativeDaemonConnectorException e) {
973 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700974 }
975 }
976
977 private ArrayList<String> readRouteList(String filename) {
978 FileInputStream fstream = null;
979 ArrayList<String> list = new ArrayList<String>();
980
981 try {
982 fstream = new FileInputStream(filename);
983 DataInputStream in = new DataInputStream(fstream);
984 BufferedReader br = new BufferedReader(new InputStreamReader(in));
985 String s;
986
987 // throw away the title line
988
989 while (((s = br.readLine()) != null) && (s.length() != 0)) {
990 list.add(s);
991 }
992 } catch (IOException ex) {
993 // return current list, possibly empty
994 } finally {
995 if (fstream != null) {
996 try {
997 fstream.close();
998 } catch (IOException ex) {}
999 }
1000 }
1001
1002 return list;
1003 }
1004
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001005 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001006 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001007 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001008 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1009
1010 // v4 routes listed as:
1011 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1012 for (String s : readRouteList("/proc/net/route")) {
1013 String[] fields = s.split("\t");
1014
1015 if (fields.length > 7) {
1016 String iface = fields[0];
1017
1018 if (interfaceName.equals(iface)) {
1019 String dest = fields[1];
1020 String gate = fields[2];
1021 String flags = fields[3]; // future use?
1022 String mask = fields[7];
1023 try {
1024 // address stored as a hex string, ex: 0014A8C0
1025 InetAddress destAddr =
1026 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1027 int prefixLength =
1028 NetworkUtils.netmaskIntToPrefixLength(
1029 (int)Long.parseLong(mask, 16));
1030 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1031
1032 // address stored as a hex string, ex 0014A8C0
1033 InetAddress gatewayAddr =
1034 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1035
Wink Saville7b5fd052013-03-15 05:07:04 +00001036 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001037 routes.add(route);
1038 } catch (Exception e) {
1039 Log.e(TAG, "Error parsing route " + s + " : " + e);
1040 continue;
1041 }
1042 }
1043 }
1044 }
1045
1046 // v6 routes listed as:
1047 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1048 for (String s : readRouteList("/proc/net/ipv6_route")) {
1049 String[]fields = s.split("\\s+");
1050 if (fields.length > 9) {
1051 String iface = fields[9].trim();
1052 if (interfaceName.equals(iface)) {
1053 String dest = fields[0];
1054 String prefix = fields[1];
1055 String gate = fields[4];
1056
1057 try {
1058 // prefix length stored as a hex string, ex 40
1059 int prefixLength = Integer.parseInt(prefix, 16);
1060
1061 // address stored as a 32 char hex string
1062 // ex fe800000000000000000000000000000
1063 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1064 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1065
1066 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1067
Wink Saville7b5fd052013-03-15 05:07:04 +00001068 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001069 routes.add(route);
1070 } catch (Exception e) {
1071 Log.e(TAG, "Error parsing route " + s + " : " + e);
1072 continue;
1073 }
1074 }
1075 }
1076 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001077 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001078 }
1079
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001080 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001081 public void setMtu(String iface, int mtu) {
1082 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1083
1084 final NativeDaemonEvent event;
1085 try {
1086 event = mConnector.execute("interface", "setmtu", iface, mtu);
1087 } catch (NativeDaemonConnectorException e) {
1088 throw e.rethrowAsParcelableException();
1089 }
1090 }
1091
1092 @Override
San Mehat873f2142010-01-14 10:25:07 -08001093 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001094 // TODO: remove from aidl if nobody calls externally
1095 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001096
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001098 }
1099
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001100 @Override
San Mehat873f2142010-01-14 10:25:07 -08001101 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001102 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001103
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001104 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001105 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001107 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001108 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001109 }
San Mehat873f2142010-01-14 10:25:07 -08001110
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001111 // 211 Forwarding enabled
1112 event.checkCode(IpFwdStatusResult);
1113 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001114 }
1115
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001116 @Override
1117 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001118 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001119 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001120 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001121 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001122 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001123 }
San Mehat873f2142010-01-14 10:25:07 -08001124 }
1125
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001126 @Override
1127 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001128 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001129 // cmd is "tether start first_start first_stop second_start second_stop ..."
1130 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001131
1132 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001133 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001134 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001135 }
Kenny Roota80ce062010-06-01 13:23:53 -07001136
1137 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001138 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001139 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001140 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001141 }
San Mehat873f2142010-01-14 10:25:07 -08001142 }
1143
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001144 @Override
1145 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001146 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001147 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001148 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001149 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001150 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001151 }
San Mehat873f2142010-01-14 10:25:07 -08001152 }
1153
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001154 @Override
1155 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001157
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001158 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001159 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001160 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001161 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001162 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001163 }
San Mehat873f2142010-01-14 10:25:07 -08001164
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001165 // 210 Tethering services started
1166 event.checkCode(TetherStatusResult);
1167 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001168 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001169
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001170 @Override
1171 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001172 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001173 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001174 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001175 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001176 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001177 }
San Mehat873f2142010-01-14 10:25:07 -08001178 }
1179
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001180 @Override
San Mehat873f2142010-01-14 10:25:07 -08001181 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001182 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001183 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001184 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001185 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001186 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001187 }
San Mehat873f2142010-01-14 10:25:07 -08001188 }
1189
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001190 @Override
1191 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001192 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001193 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001194 return NativeDaemonEvent.filterMessageList(
1195 mConnector.executeForList("tether", "interface", "list"),
1196 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001197 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001198 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001199 }
San Mehat873f2142010-01-14 10:25:07 -08001200 }
1201
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001202 @Override
1203 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001204 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001205
1206 final Command cmd = new Command("tether", "dns", "set");
1207 for (String s : dns) {
1208 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1209 }
1210
San Mehat873f2142010-01-14 10:25:07 -08001211 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 mConnector.execute(cmd);
1213 } catch (NativeDaemonConnectorException e) {
1214 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001215 }
1216 }
1217
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001218 @Override
1219 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001220 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001221 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001222 return NativeDaemonEvent.filterMessageList(
1223 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001224 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001225 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001226 }
San Mehat873f2142010-01-14 10:25:07 -08001227 }
1228
jiaguo1da35f72014-01-09 16:39:59 +08001229 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1230 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1231 for (InterfaceAddress ia : addresses) {
1232 if (!ia.getAddress().isLinkLocalAddress())
1233 filtered.add(ia);
1234 }
1235 return filtered;
1236 }
1237
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001239 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001240 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001241
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001242 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1243 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001244 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001245 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001246 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001247 // Don't touch link-local routes, as link-local addresses aren't routable,
1248 // kernel creates link-local routes on all interfaces automatically
1249 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1250 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001251 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001252 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 InetAddress addr = NetworkUtils.getNetworkPart(
1254 ia.getAddress(), ia.getNetworkPrefixLength());
1255 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001256 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001257 }
1258
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001259 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001260 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001261 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001262 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001263 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001264 }
1265
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001266 @Override
1267 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001268 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001269 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001270 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001271 } catch (SocketException e) {
1272 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001273 }
San Mehat873f2142010-01-14 10:25:07 -08001274 }
1275
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001276 @Override
1277 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001278 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001279 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001280 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001281 } catch (SocketException e) {
1282 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001283 }
San Mehat873f2142010-01-14 10:25:07 -08001284 }
San Mehat72759df2010-01-19 13:50:37 -08001285
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001286 @Override
1287 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001288 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001289 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001290 return NativeDaemonEvent.filterMessageList(
1291 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001292 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001293 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001294 }
San Mehat72759df2010-01-19 13:50:37 -08001295 }
1296
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001297 @Override
1298 public void attachPppd(
1299 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001300 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001301 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001302 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001303 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1304 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1305 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001306 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001307 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001308 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001309 }
1310 }
1311
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001312 @Override
1313 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001314 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001315 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001316 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001317 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001318 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001319 }
San Mehat72759df2010-01-19 13:50:37 -08001320 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001321
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001322 @Override
1323 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001324 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001325 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001326 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001327 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001328 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001329 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001330 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001331 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001332 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001333 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001334 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001335 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001336 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001337 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001338 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001339 }
1340
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001341 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001342 switch (wifiConfig.getAuthType()) {
1343 case KeyMgmt.WPA_PSK:
1344 return "wpa-psk";
1345 case KeyMgmt.WPA2_PSK:
1346 return "wpa2-psk";
1347 default:
1348 return "open";
1349 }
1350 }
1351
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001352 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001353 @Override
1354 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001355 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001356 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001357 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001358 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001359 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001360 }
1361 }
1362
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001363 @Override
1364 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001365 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001366 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001367 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001368 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001369 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001370 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001371 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001372 }
1373
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001374 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001375 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001376 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001377 try {
1378 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001379 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001380 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001381 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001382 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001383 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001384 }
1385 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001386 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001387 }
1388 }
San Mehat91cac642010-03-31 14:31:36 -07001389
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001390 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001391 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001392 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1393
1394 if (DBG) Slog.d(TAG, "Adding idletimer");
1395
1396 synchronized (mIdleTimerLock) {
1397 IdleTimerParams params = mActiveIdleTimers.get(iface);
1398 if (params != null) {
1399 // the interface already has idletimer, update network count
1400 params.networkCount++;
1401 return;
1402 }
1403
1404 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001405 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1406 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001407 } catch (NativeDaemonConnectorException e) {
1408 throw e.rethrowAsParcelableException();
1409 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001410 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1411
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001412 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001413 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1414 mNetworkActive = false;
1415 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001416 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001417 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001418 notifyInterfaceClassActivity(type,
1419 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1420 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001421 }
1422 });
Haoyu Bai04124232012-06-28 15:26:19 -07001423 }
1424 }
1425
1426 @Override
1427 public void removeIdleTimer(String iface) {
1428 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1429
1430 if (DBG) Slog.d(TAG, "Removing idletimer");
1431
1432 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001433 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001434 if (params == null || --(params.networkCount) > 0) {
1435 return;
1436 }
1437
1438 try {
1439 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001440 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001441 } catch (NativeDaemonConnectorException e) {
1442 throw e.rethrowAsParcelableException();
1443 }
1444 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001445 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001446 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001447 notifyInterfaceClassActivity(params.type,
1448 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1449 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001450 }
1451 });
Haoyu Bai04124232012-06-28 15:26:19 -07001452 }
1453 }
1454
1455 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001456 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001457 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001458 try {
1459 return mStatsFactory.readNetworkStatsSummaryDev();
1460 } catch (IOException e) {
1461 throw new IllegalStateException(e);
1462 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001463 }
1464
1465 @Override
1466 public NetworkStats getNetworkStatsSummaryXt() {
1467 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001468 try {
1469 return mStatsFactory.readNetworkStatsSummaryXt();
1470 } catch (IOException e) {
1471 throw new IllegalStateException(e);
1472 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001473 }
1474
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001475 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001476 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001477 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001478 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001479 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001480 } catch (IOException e) {
1481 throw new IllegalStateException(e);
1482 }
San Mehat91cac642010-03-31 14:31:36 -07001483 }
1484
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001485 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001486 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001487 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001488
Jeff Sharkey350083e2011-06-29 10:45:16 -07001489 // silently discard when control disabled
1490 // TODO: eventually migrate to be always enabled
1491 if (!mBandwidthControlEnabled) return;
1492
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001493 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001494 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001495 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001496 }
1497
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001498 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001499 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001500 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001501 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001502 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001503 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001504 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001505 }
1506 }
1507
1508 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001509 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001510 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001511
Jeff Sharkey350083e2011-06-29 10:45:16 -07001512 // silently discard when control disabled
1513 // TODO: eventually migrate to be always enabled
1514 if (!mBandwidthControlEnabled) return;
1515
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001516 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001517 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001518 // TODO: eventually consider throwing
1519 return;
1520 }
1521
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001522 mActiveQuotas.remove(iface);
1523 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001524
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001525 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001526 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001527 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001528 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001529 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001530 }
1531 }
1532 }
1533
1534 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001535 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001536 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001537
1538 // silently discard when control disabled
1539 // TODO: eventually migrate to be always enabled
1540 if (!mBandwidthControlEnabled) return;
1541
1542 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001543 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001544 throw new IllegalStateException("setting alert requires existing quota on iface");
1545 }
1546
1547 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001548 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001549 throw new IllegalStateException("iface " + iface + " already has alert");
1550 }
1551
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001552 try {
1553 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001554 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001555 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001556 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001557 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001558 }
1559 }
1560 }
1561
1562 @Override
1563 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001564 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001565
1566 // silently discard when control disabled
1567 // TODO: eventually migrate to be always enabled
1568 if (!mBandwidthControlEnabled) return;
1569
1570 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001571 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001572 // TODO: eventually consider throwing
1573 return;
1574 }
1575
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001576 try {
1577 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001578 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001579 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001580 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001581 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001582 }
1583 }
1584 }
1585
1586 @Override
1587 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001588 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001589
1590 // silently discard when control disabled
1591 // TODO: eventually migrate to be always enabled
1592 if (!mBandwidthControlEnabled) return;
1593
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001594 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001595 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001596 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001597 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001598 }
1599 }
1600
1601 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001602 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001603 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001604
Jeff Sharkey350083e2011-06-29 10:45:16 -07001605 // silently discard when control disabled
1606 // TODO: eventually migrate to be always enabled
1607 if (!mBandwidthControlEnabled) return;
1608
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001609 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001610 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1611 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1612 // TODO: eventually consider throwing
1613 return;
1614 }
1615
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001616 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001617 mConnector.execute("bandwidth",
1618 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001619 if (rejectOnQuotaInterfaces) {
1620 mUidRejectOnQuota.put(uid, true);
1621 } else {
1622 mUidRejectOnQuota.delete(uid);
1623 }
1624 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001625 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001626 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001627 }
1628 }
1629
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001630 @Override
1631 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001632 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001633 return mBandwidthControlEnabled;
1634 }
1635
1636 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001637 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001638 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001639 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001640 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001641 } catch (IOException e) {
1642 throw new IllegalStateException(e);
1643 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001644 }
1645
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001646 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001647 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001648 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001649
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001650 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001651 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001652 final NativeDaemonEvent[] events = mConnector.executeForList(
1653 "bandwidth", "gettetherstats");
1654 for (NativeDaemonEvent event : events) {
1655 if (event.getCode() != TetheringStatsListResult) continue;
1656
1657 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1658 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1659 try {
1660 final String ifaceIn = tok.nextToken();
1661 final String ifaceOut = tok.nextToken();
1662
1663 final NetworkStats.Entry entry = new NetworkStats.Entry();
1664 entry.iface = ifaceOut;
1665 entry.uid = UID_TETHERING;
1666 entry.set = SET_DEFAULT;
1667 entry.tag = TAG_NONE;
1668 entry.rxBytes = Long.parseLong(tok.nextToken());
1669 entry.rxPackets = Long.parseLong(tok.nextToken());
1670 entry.txBytes = Long.parseLong(tok.nextToken());
1671 entry.txPackets = Long.parseLong(tok.nextToken());
1672 stats.combineValues(entry);
1673 } catch (NoSuchElementException e) {
1674 throw new IllegalStateException("problem parsing tethering stats: " + event);
1675 } catch (NumberFormatException e) {
1676 throw new IllegalStateException("problem parsing tethering stats: " + event);
1677 }
1678 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001679 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001680 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001681 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001682 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001683 }
1684
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001685 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001686 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001687 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001688
Paul Jensen13e817d2014-04-10 14:16:37 -04001689 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001690 (domains == null ? "" : domains));
1691
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001692 for (String s : servers) {
1693 InetAddress a = NetworkUtils.numericToInetAddress(s);
1694 if (a.isAnyLocalAddress() == false) {
1695 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001696 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001697 }
1698
1699 try {
1700 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001701 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001702 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001703 }
1704 }
1705
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001706 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001707 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001708 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001709 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1710 argv[0] = "users";
1711 argv[1] = "add";
1712 argv[2] = netId;
1713 int argc = 3;
1714 // Avoid overly long commands by limiting number of UID ranges per command.
1715 for (int i = 0; i < ranges.length; i++) {
1716 argv[argc++] = ranges[i].toString();
1717 if (i == (ranges.length - 1) || argc == argv.length) {
1718 try {
1719 mConnector.execute("network", Arrays.copyOf(argv, argc));
1720 } catch (NativeDaemonConnectorException e) {
1721 throw e.rethrowAsParcelableException();
1722 }
1723 argc = 3;
1724 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001725 }
1726 }
1727
1728 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001729 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001730 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001731 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1732 argv[0] = "users";
1733 argv[1] = "remove";
1734 argv[2] = netId;
1735 int argc = 3;
1736 // Avoid overly long commands by limiting number of UID ranges per command.
1737 for (int i = 0; i < ranges.length; i++) {
1738 argv[argc++] = ranges[i].toString();
1739 if (i == (ranges.length - 1) || argc == argv.length) {
1740 try {
1741 mConnector.execute("network", Arrays.copyOf(argv, argc));
1742 } catch (NativeDaemonConnectorException e) {
1743 throw e.rethrowAsParcelableException();
1744 }
1745 argc = 3;
1746 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001747 }
1748 }
1749
1750 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001751 public void setHostExemption(LinkAddress host) {
1752 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1753 try {
1754 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1755 } catch (NativeDaemonConnectorException e) {
1756 throw e.rethrowAsParcelableException();
1757 }
1758 }
1759
1760 @Override
1761 public void clearHostExemption(LinkAddress host) {
1762 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1763 try {
1764 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1765 } catch (NativeDaemonConnectorException e) {
1766 throw e.rethrowAsParcelableException();
1767 }
1768 }
1769
1770 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001771 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001772 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1773 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001774 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001775 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001776 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001777 }
1778 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001779
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001780 @Override
1781 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001782 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001783 try {
1784 mConnector.execute("firewall", enabled ? "enable" : "disable");
1785 mFirewallEnabled = enabled;
1786 } catch (NativeDaemonConnectorException e) {
1787 throw e.rethrowAsParcelableException();
1788 }
1789 }
1790
1791 @Override
1792 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001793 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001794 return mFirewallEnabled;
1795 }
1796
1797 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001798 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001799 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001800 Preconditions.checkState(mFirewallEnabled);
1801 final String rule = allow ? ALLOW : DENY;
1802 try {
1803 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1804 } catch (NativeDaemonConnectorException e) {
1805 throw e.rethrowAsParcelableException();
1806 }
1807 }
1808
1809 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001810 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001811 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001812 Preconditions.checkState(mFirewallEnabled);
1813 final String rule = allow ? ALLOW : DENY;
1814 try {
1815 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1816 } catch (NativeDaemonConnectorException e) {
1817 throw e.rethrowAsParcelableException();
1818 }
1819 }
1820
1821 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001822 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001823 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001824 Preconditions.checkState(mFirewallEnabled);
1825 final String rule = allow ? ALLOW : DENY;
1826 try {
1827 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1828 } catch (NativeDaemonConnectorException e) {
1829 throw e.rethrowAsParcelableException();
1830 }
1831 }
1832
1833 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001834 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001835 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001836 Preconditions.checkState(mFirewallEnabled);
1837 final String rule = allow ? ALLOW : DENY;
1838 try {
1839 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1840 } catch (NativeDaemonConnectorException e) {
1841 throw e.rethrowAsParcelableException();
1842 }
1843 }
1844
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001845 private static void enforceSystemUid() {
1846 final int uid = Binder.getCallingUid();
1847 if (uid != Process.SYSTEM_UID) {
1848 throw new SecurityException("Only available to AID_SYSTEM");
1849 }
1850 }
1851
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001852 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001853 public void startClatd(String interfaceName) throws IllegalStateException {
1854 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1855
1856 try {
1857 mConnector.execute("clatd", "start", interfaceName);
1858 } catch (NativeDaemonConnectorException e) {
1859 throw e.rethrowAsParcelableException();
1860 }
1861 }
1862
1863 @Override
1864 public void stopClatd() throws IllegalStateException {
1865 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1866
1867 try {
1868 mConnector.execute("clatd", "stop");
1869 } catch (NativeDaemonConnectorException e) {
1870 throw e.rethrowAsParcelableException();
1871 }
1872 }
1873
1874 @Override
1875 public boolean isClatdStarted() {
1876 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1877
1878 final NativeDaemonEvent event;
1879 try {
1880 event = mConnector.execute("clatd", "status");
1881 } catch (NativeDaemonConnectorException e) {
1882 throw e.rethrowAsParcelableException();
1883 }
1884
1885 event.checkCode(ClatdStatusResult);
1886 return event.getMessage().endsWith("started");
1887 }
1888
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001889 @Override
1890 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1891 mNetworkActivityListeners.register(listener);
1892 }
1893
1894 @Override
1895 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1896 mNetworkActivityListeners.unregister(listener);
1897 }
1898
1899 @Override
1900 public boolean isNetworkActive() {
1901 synchronized (mNetworkActivityListeners) {
1902 return mNetworkActive || mActiveIdleTimers.isEmpty();
1903 }
1904 }
1905
1906 private void reportNetworkActive() {
1907 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001908 try {
1909 for (int i = 0; i < length; i++) {
1910 try {
1911 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1912 } catch (RemoteException e) {
1913 } catch (RuntimeException e) {
1914 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001915 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001916 } finally {
1917 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001918 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001919 }
1920
Mattias Falk8b47b362011-08-23 14:15:13 +02001921 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001922 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001923 public void monitor() {
1924 if (mConnector != null) {
1925 mConnector.monitor();
1926 }
1927 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001928
1929 @Override
1930 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1931 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1932
Robert Greenwalt470fd722012-01-18 12:51:15 -08001933 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1934 mConnector.dump(fd, pw, args);
1935 pw.println();
1936
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001937 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001938 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1939 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1940 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001941
1942 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001943 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1944 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001945 }
1946
1947 synchronized (mUidRejectOnQuota) {
1948 pw.print("UID reject on quota ifaces: [");
1949 final int size = mUidRejectOnQuota.size();
1950 for (int i = 0; i < size; i++) {
1951 pw.print(mUidRejectOnQuota.keyAt(i));
1952 if (i < size - 1) pw.print(",");
1953 }
1954 pw.println("]");
1955 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001956
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001957 synchronized (mIdleTimerLock) {
1958 pw.println("Idle timers:");
1959 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1960 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1961 IdleTimerParams params = ent.getValue();
1962 pw.print(" timeout="); pw.print(params.timeout);
1963 pw.print(" type="); pw.print(params.type);
1964 pw.print(" networkCount="); pw.println(params.networkCount);
1965 }
1966 }
1967
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001968 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001969 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001970
Robert Greenwalt568891d2014-04-04 13:38:00 -07001971 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001972 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001973 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1974
1975 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001976 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001977 } catch (NativeDaemonConnectorException e) {
1978 throw e.rethrowAsParcelableException();
1979 }
1980 }
1981
Robert Greenwalt568891d2014-04-04 13:38:00 -07001982 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001983 public void createVirtualNetwork(int netId, boolean hasDNS) {
1984 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1985
1986 try {
1987 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0");
1988 } catch (NativeDaemonConnectorException e) {
1989 throw e.rethrowAsParcelableException();
1990 }
1991 }
1992
1993 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001994 public void removeNetwork(int netId) {
1995 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1996
1997 try {
1998 mConnector.execute("network", "destroy", netId);
1999 } catch (NativeDaemonConnectorException e) {
2000 throw e.rethrowAsParcelableException();
2001 }
2002 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002003
2004 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002005 public void addInterfaceToNetwork(String iface, int netId) {
2006 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2007
2008 try {
2009 mConnector.execute("network", "addiface", netId, iface);
2010 } catch (NativeDaemonConnectorException e) {
2011 throw e.rethrowAsParcelableException();
2012 }
2013 }
2014
2015 @Override
2016 public void removeInterfaceFromNetwork(String iface, int netId) {
2017 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2018
2019 try {
2020 mConnector.execute("network", "removeiface", netId, iface);
2021 } catch (NativeDaemonConnectorException e) {
2022 throw e.rethrowAsParcelableException();
2023 }
2024 }
2025
2026 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002027 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2028 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002029 }
2030
2031 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002032 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2033 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002034 }
2035
Robert Greenwalt913c8952014-04-07 17:36:35 -07002036 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002037 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2038
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002039 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002040
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002041 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002042 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002043 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002044 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002045 if (routeInfo.hasGateway()) {
2046 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2047 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002048
2049 try {
2050 mConnector.execute(cmd);
2051 } catch (NativeDaemonConnectorException e) {
2052 throw e.rethrowAsParcelableException();
2053 }
2054 }
2055
2056 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002057 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002058 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2059
2060 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002061 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002062 } catch (NativeDaemonConnectorException e) {
2063 throw e.rethrowAsParcelableException();
2064 }
2065 }
2066
2067 @Override
2068 public void clearDefaultNetId() {
2069 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2070
2071 try {
2072 mConnector.execute("network", "default", "clear");
2073 } catch (NativeDaemonConnectorException e) {
2074 throw e.rethrowAsParcelableException();
2075 }
2076 }
2077
2078 @Override
2079 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2080 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2081
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002082 final Command cmd = new Command("network", "permission", "user", "set");
2083 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2084 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002085 for (int i=0; i<uids.length; i++) {
2086 cmd.appendArg(uids[i]);
2087 }
2088
2089 try {
2090 mConnector.execute(cmd);
2091 } catch (NativeDaemonConnectorException e) {
2092 throw e.rethrowAsParcelableException();
2093 }
2094 }
2095
2096 @Override
2097 public void clearPermission(int[] uids) {
2098 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2099
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002100 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002101 for (int i=0; i<uids.length; i++) {
2102 cmd.appendArg(uids[i]);
2103 }
2104
2105 try {
2106 mConnector.execute(cmd);
2107 } catch (NativeDaemonConnectorException e) {
2108 throw e.rethrowAsParcelableException();
2109 }
2110 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002111
2112 @Override
2113 public void allowProtect(int uid) {
2114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2115
2116 try {
2117 mConnector.execute("network", "protect", "allow", uid);
2118 } catch (NativeDaemonConnectorException e) {
2119 throw e.rethrowAsParcelableException();
2120 }
2121 }
2122
2123 @Override
2124 public void denyProtect(int uid) {
2125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2126
2127 try {
2128 mConnector.execute("network", "protect", "deny", uid);
2129 } catch (NativeDaemonConnectorException e) {
2130 throw e.rethrowAsParcelableException();
2131 }
2132 }
2133
San Mehat873f2142010-01-14 10:25:07 -08002134}