blob: 0fecdf9d80d2bb8dc0802347d57469bb04c3ac9c [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
Jeff Sharkey4529bb62011-12-14 10:31:54 -080019import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070020import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070022import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080023import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import static android.net.NetworkStats.TAG_NONE;
25import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070026import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090027import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070028import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
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;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070044import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070046import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080047import android.net.wifi.WifiConfiguration;
48import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070049import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070050import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070051import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080052import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080053import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080054import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070055import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080056import android.os.RemoteCallbackList;
57import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070058import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070059import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080060import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070061import android.telephony.DataConnectionRealTimeInfo;
62import android.telephony.PhoneStateListener;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080063import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080064import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070065import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080066
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070067import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070068import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070069import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080070import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070071import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070072import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070073import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070074
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070075import java.io.BufferedReader;
76import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080077import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070078import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070079import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070080import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070081import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070082import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070083import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070084import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070085import java.net.InterfaceAddress;
86import java.net.NetworkInterface;
87import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070088import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070089import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080090import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070091import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092import java.util.NoSuchElementException;
93import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070094import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080095
96/**
97 * @hide
98 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070099public class NetworkManagementService extends INetworkManagementService.Stub
100 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700101 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700102 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700103 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900104 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700105
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800106 private static final String ADD = "add";
107 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700108
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700109 private static final String ALLOW = "allow";
110 private static final String DENY = "deny";
111
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700112 private static final String DEFAULT = "default";
113 private static final String SECONDARY = "secondary";
114
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700115 /**
116 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
117 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
118 */
119 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
120
San Mehat873f2142010-01-14 10:25:07 -0800121 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700122 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800123 public static final int InterfaceListResult = 110;
124 public static final int TetherInterfaceListResult = 111;
125 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800126 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700127 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800128
129 public static final int TetherStatusResult = 210;
130 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800131 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800132 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700133 public static final int InterfaceRxCounterResult = 216;
134 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700135 public static final int QuotaCounterResult = 220;
136 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800137 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900138 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700139 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800140
141 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700142 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700143 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900144 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900145 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800146 }
147
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700148 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
149
San Mehat873f2142010-01-14 10:25:07 -0800150 /**
151 * Binder context for this service
152 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700153 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800154
155 /**
156 * connector object for communicating with netd
157 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700158 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800159
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700160 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700161 private final Handler mDaemonHandler;
162 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700163
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800164 private IBatteryStats mBatteryStats;
165
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700166 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700167 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700168
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800169 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
170 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800171
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700172 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
173
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700174 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700175 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700176 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700177 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700178 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700179 /** Set of UIDs with active reject rules. */
180 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
181
Haoyu Bai04124232012-06-28 15:26:19 -0700182 private Object mIdleTimerLock = new Object();
183 /** Set of interfaces with active idle timers. */
184 private static class IdleTimerParams {
185 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800186 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700187 public int networkCount;
188
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800189 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700190 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800191 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700192 this.networkCount = 1;
193 }
194 }
195 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
196
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700197 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700198 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700199
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700200 private boolean mMobileActivityFromRadio = false;
201 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
202
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800203 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
204 new RemoteCallbackList<INetworkActivityListener>();
205 private boolean mNetworkActive;
206
San Mehat873f2142010-01-14 10:25:07 -0800207 /**
208 * Constructs a new NetworkManagementService instance
209 *
210 * @param context Binder context for this service
211 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900212 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800213 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800214
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700215 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
216 mFgHandler = new Handler(FgThread.get().getLooper());
217
Marco Nelissen62dbb222010-02-18 10:56:30 -0800218 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700219 mConnector = null;
220 mThread = null;
221 mDaemonHandler = null;
222 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800223 return;
224 }
225
Dianne Hackborn4590e522014-03-24 13:36:46 -0700226 // Don't need this wake lock, since we now have a time stamp for when
227 // the network actually went inactive. (It might be nice to still do this,
228 // but I don't want to do it through the power manager because that pollutes the
229 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700230 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700231 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800232
San Mehat873f2142010-01-14 10:25:07 -0800233 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700234 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
235 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700236 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700237
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700238 mDaemonHandler = new Handler(FgThread.get().getLooper());
239 mPhoneStateListener = new PhoneStateListener(mDaemonHandler.getLooper()) {
240 public void onDataConnectionRealTimeInfoChanged(
241 DataConnectionRealTimeInfo dcRtInfo) {
242 // Disabled for now, until we are getting good data.
243 //notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
244 // dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
245 }
246 };
247
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700248 // Add ourself to the Watchdog monitors.
249 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700250 }
251
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900252 static NetworkManagementService create(Context context,
253 String socket) throws InterruptedException {
254 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700255 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700256 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
257 service.mThread.start();
258 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700259 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700260 if (DBG) Slog.d(TAG, "Connected");
261 return service;
San Mehat873f2142010-01-14 10:25:07 -0800262 }
263
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900264 public static NetworkManagementService create(Context context) throws InterruptedException {
265 return create(context, NETD_SOCKET_NAME);
266 }
267
Jeff Sharkey350083e2011-06-29 10:45:16 -0700268 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700269 prepareNativeDaemon();
270 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700271 }
272
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800273 private IBatteryStats getBatteryStats() {
274 synchronized (this) {
275 if (mBatteryStats != null) {
276 return mBatteryStats;
277 }
278 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
279 BatteryStats.SERVICE_NAME));
280 return mBatteryStats;
281 }
282 }
283
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800284 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800287 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800288 }
289
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800290 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800291 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800292 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800293 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800294 }
295
296 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700297 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800298 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700299 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700301 try {
302 for (int i = 0; i < length; i++) {
303 try {
304 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
305 } catch (RemoteException e) {
306 } catch (RuntimeException e) {
307 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700308 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700309 } finally {
310 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700311 }
312 }
313
314 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700315 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700316 * (typically, an Ethernet cable has been plugged-in or unplugged).
317 */
318 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800319 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700320 try {
321 for (int i = 0; i < length; i++) {
322 try {
323 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
324 } catch (RemoteException e) {
325 } catch (RuntimeException e) {
326 }
San Mehat4d02d002010-01-22 16:07:46 -0800327 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700328 } finally {
329 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800330 }
331 }
332
333 /**
334 * Notify our observers of an interface addition.
335 */
336 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800337 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700338 try {
339 for (int i = 0; i < length; i++) {
340 try {
341 mObservers.getBroadcastItem(i).interfaceAdded(iface);
342 } catch (RemoteException e) {
343 } catch (RuntimeException e) {
344 }
San Mehat4d02d002010-01-22 16:07:46 -0800345 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700346 } finally {
347 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800348 }
349 }
350
351 /**
352 * Notify our observers of an interface removal.
353 */
354 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700355 // netd already clears out quota and alerts for removed ifaces; update
356 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700357 mActiveAlerts.remove(iface);
358 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700359
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800360 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700361 try {
362 for (int i = 0; i < length; i++) {
363 try {
364 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
365 } catch (RemoteException e) {
366 } catch (RuntimeException e) {
367 }
San Mehat4d02d002010-01-22 16:07:46 -0800368 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700369 } finally {
370 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800371 }
372 }
373
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700374 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700375 * Notify our observers of a limit reached.
376 */
377 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800378 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700379 try {
380 for (int i = 0; i < length; i++) {
381 try {
382 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
383 } catch (RemoteException e) {
384 } catch (RuntimeException e) {
385 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700386 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700387 } finally {
388 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700389 }
390 }
391
392 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700393 * Notify our observers of a change in the data activity state of the interface
394 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700395 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
396 boolean fromRadio) {
397 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
398 if (isMobile) {
399 if (!fromRadio) {
400 if (mMobileActivityFromRadio) {
401 // If this call is not coming from a report from the radio itself, but we
402 // have previously received reports from the radio, then we will take the
403 // power state to just be whatever the radio last reported.
404 powerState = mLastPowerStateFromRadio;
405 }
406 } else {
407 mMobileActivityFromRadio = true;
408 }
409 if (mLastPowerStateFromRadio != powerState) {
410 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700411 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700413 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700414 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700415 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700416 }
417
418 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
419 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
420
421 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
422 // Report the change in data activity. We don't do this if this is a change
423 // on the mobile network, that is not coming from the radio itself, and we
424 // have previously seen change reports from the radio. In that case only
425 // the radio is the authority for the current state.
426 final int length = mObservers.beginBroadcast();
427 try {
428 for (int i = 0; i < length; i++) {
429 try {
430 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
431 Integer.toString(type), isActive, tsNanos);
432 } catch (RemoteException e) {
433 } catch (RuntimeException e) {
434 }
435 }
436 } finally {
437 mObservers.finishBroadcast();
438 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700439 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800440
441 boolean report = false;
442 synchronized (mIdleTimerLock) {
443 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700444 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800445 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700446 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800447 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700448 if (mNetworkActive != isActive) {
449 mNetworkActive = isActive;
450 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800451 }
452 }
453 if (report) {
454 reportNetworkActive();
455 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700456 }
457
458 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700459 * Prepare native daemon once connected, enabling modules and pushing any
460 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700461 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700462 private void prepareNativeDaemon() {
463 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700464
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700465 // only enable bandwidth control when support exists
466 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
467 if (hasKernelSupport) {
468 Slog.d(TAG, "enabling bandwidth control");
469 try {
470 mConnector.execute("bandwidth", "enable");
471 mBandwidthControlEnabled = true;
472 } catch (NativeDaemonConnectorException e) {
473 Log.wtf(TAG, "problem enabling bandwidth controls", e);
474 }
475 } else {
476 Slog.d(TAG, "not enabling bandwidth control");
477 }
478
479 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
480
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700481 if (mBandwidthControlEnabled) {
482 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800483 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700484 } catch (RemoteException e) {
485 }
486 }
487
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700488 // push any existing quota or UID rules
489 synchronized (mQuotaLock) {
490 int size = mActiveQuotas.size();
491 if (size > 0) {
492 Slog.d(TAG, "pushing " + size + " active quota rules");
493 final HashMap<String, Long> activeQuotas = mActiveQuotas;
494 mActiveQuotas = Maps.newHashMap();
495 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
496 setInterfaceQuota(entry.getKey(), entry.getValue());
497 }
498 }
499
500 size = mActiveAlerts.size();
501 if (size > 0) {
502 Slog.d(TAG, "pushing " + size + " active alert rules");
503 final HashMap<String, Long> activeAlerts = mActiveAlerts;
504 mActiveAlerts = Maps.newHashMap();
505 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
506 setInterfaceAlert(entry.getKey(), entry.getValue());
507 }
508 }
509
510 size = mUidRejectOnQuota.size();
511 if (size > 0) {
512 Slog.d(TAG, "pushing " + size + " active uid rules");
513 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
514 mUidRejectOnQuota = new SparseBooleanArray();
515 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
516 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
517 }
518 }
519 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700520
521 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700522 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700523 }
San Mehat4d02d002010-01-22 16:07:46 -0800524
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900525 /**
526 * Notify our observers of a new or updated interface address.
527 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900528 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900529 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700530 try {
531 for (int i = 0; i < length; i++) {
532 try {
533 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
534 } catch (RemoteException e) {
535 } catch (RuntimeException e) {
536 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900537 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700538 } finally {
539 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900540 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900541 }
542
543 /**
544 * Notify our observers of a deleted interface address.
545 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900546 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900547 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700548 try {
549 for (int i = 0; i < length; i++) {
550 try {
551 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
552 } catch (RemoteException e) {
553 } catch (RuntimeException e) {
554 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900555 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700556 } finally {
557 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900558 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900559 }
560
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900561 /**
562 * Notify our observers of DNS server information received.
563 */
564 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
565 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700566 try {
567 for (int i = 0; i < length; i++) {
568 try {
569 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
570 addresses);
571 } catch (RemoteException e) {
572 } catch (RuntimeException e) {
573 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900574 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700575 } finally {
576 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900577 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900578 }
579
San Mehat873f2142010-01-14 10:25:07 -0800580 //
581 // Netd Callback handling
582 //
583
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700584 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
585 @Override
San Mehat873f2142010-01-14 10:25:07 -0800586 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700587 // event is dispatched from internal NDC thread, so we prepare the
588 // daemon back on main thread.
589 if (mConnectedSignal != null) {
590 mConnectedSignal.countDown();
591 mConnectedSignal = null;
592 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700593 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700594 @Override
595 public void run() {
596 prepareNativeDaemon();
597 }
598 });
599 }
San Mehat873f2142010-01-14 10:25:07 -0800600 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700601
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700602 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800603 public boolean onCheckHoldWakeLock(int code) {
604 return code == NetdResponseCode.InterfaceClassActivity;
605 }
606
607 @Override
San Mehat873f2142010-01-14 10:25:07 -0800608 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900609 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700610 switch (code) {
611 case NetdResponseCode.InterfaceChange:
612 /*
613 * a network interface change occured
614 * Format: "NNN Iface added <name>"
615 * "NNN Iface removed <name>"
616 * "NNN Iface changed <name> <up/down>"
617 * "NNN Iface linkstatus <name> <up/down>"
618 */
619 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900620 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700621 }
622 if (cooked[2].equals("added")) {
623 notifyInterfaceAdded(cooked[3]);
624 return true;
625 } else if (cooked[2].equals("removed")) {
626 notifyInterfaceRemoved(cooked[3]);
627 return true;
628 } else if (cooked[2].equals("changed") && cooked.length == 5) {
629 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
630 return true;
631 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
632 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
633 return true;
634 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900635 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700636 // break;
637 case NetdResponseCode.BandwidthControl:
638 /*
639 * Bandwidth control needs some attention
640 * Format: "NNN limit alert <alertName> <ifaceName>"
641 */
642 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900643 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700644 }
645 if (cooked[2].equals("alert")) {
646 notifyLimitReached(cooked[3], cooked[4]);
647 return true;
648 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900649 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700650 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700651 case NetdResponseCode.InterfaceClassActivity:
652 /*
653 * An network interface class state changed (active/idle)
654 * Format: "NNN IfaceClass <active/idle> <label>"
655 */
656 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900657 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700658 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700659 long timestampNanos = 0;
660 if (cooked.length == 5) {
661 try {
662 timestampNanos = Long.parseLong(cooked[4]);
663 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700664 } else {
665 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700666 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700667 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700668 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700669 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
670 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700671 return true;
672 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900673 case NetdResponseCode.InterfaceAddressChange:
674 /*
675 * A network address change occurred
676 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
677 * "NNN Address removed <addr> <iface> <flags> <scope>"
678 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900679 if (cooked.length < 7 || !cooked[1].equals("Address")) {
680 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900681 }
682
Lorenzo Colitti64483942013-11-15 18:43:52 +0900683 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900684 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900685 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900686 int flags = Integer.parseInt(cooked[5]);
687 int scope = Integer.parseInt(cooked[6]);
688 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900689 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
690 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900691 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900692 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900693 }
694
695 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900696 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900697 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900698 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900699 }
700 return true;
701 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900702 case NetdResponseCode.InterfaceDnsServerInfo:
703 /*
704 * Information about available DNS servers has been received.
705 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
706 */
707 long lifetime; // Actually a 32-bit unsigned integer.
708
709 if (cooked.length == 6 &&
710 cooked[1].equals("DnsInfo") &&
711 cooked[2].equals("servers")) {
712 try {
713 lifetime = Long.parseLong(cooked[4]);
714 } catch (NumberFormatException e) {
715 throw new IllegalStateException(errorMessage);
716 }
717 String[] servers = cooked[5].split(",");
718 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
719 }
720 return true;
721 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700722 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800723 }
724 return false;
San Mehat873f2142010-01-14 10:25:07 -0800725 }
726 }
727
San Mehated4fc8a2010-01-22 12:28:36 -0800728
San Mehat873f2142010-01-14 10:25:07 -0800729 //
730 // INetworkManagementService members
731 //
732
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800733 @Override
734 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800735 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700736 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800737 return NativeDaemonEvent.filterMessageList(
738 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700739 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800740 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700741 }
San Mehated4fc8a2010-01-22 12:28:36 -0800742 }
743
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800744 @Override
745 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800746 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800747
748 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700749 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800750 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700751 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800752 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700753 }
San Mehated4fc8a2010-01-22 12:28:36 -0800754
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800755 event.checkCode(InterfaceGetCfgResult);
756
757 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
758 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800759
Kenny Roota80ce062010-06-01 13:23:53 -0700760 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800761 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700762 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800763 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800764 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800765 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700766 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800767 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800768 } catch (IllegalArgumentException iae) {
769 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700770 }
771
772 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800773 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800774 } catch (NumberFormatException nfe) {
775 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700776 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800777
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800778 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
779 while (st.hasMoreTokens()) {
780 cfg.setFlag(st.nextToken());
781 }
Kenny Roota80ce062010-06-01 13:23:53 -0700782 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800783 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800784 }
San Mehated4fc8a2010-01-22 12:28:36 -0800785 return cfg;
786 }
787
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800788 @Override
789 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800790 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800791 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800792 if (linkAddr == null || linkAddr.getAddress() == null) {
793 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800794 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800795
796 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800797 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800798 linkAddr.getNetworkPrefixLength());
799 for (String flag : cfg.getFlags()) {
800 cmd.appendArg(flag);
801 }
802
Kenny Roota80ce062010-06-01 13:23:53 -0700803 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800804 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700805 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800806 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700807 }
San Mehat873f2142010-01-14 10:25:07 -0800808 }
809
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800810 @Override
811 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800812 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800813 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800814 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800815 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700816 }
817
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800818 @Override
819 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800820 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800821 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800822 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800823 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700824 }
825
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800826 @Override
827 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800828 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700829 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800830 mConnector.execute(
831 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700832 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800833 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700834 }
835 }
836
Irfan Sherifff5600612011-06-16 10:26:28 -0700837 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
838 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800839 @Override
840 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800841 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700842 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800843 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700844 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800845 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700846 }
847 }
848
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800849 @Override
850 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800851 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700852 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800853 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700854 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800855 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700856 }
857 }
858
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800859 @Override
860 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800861 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700862 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800863 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700864 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800865 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700866 }
867 }
868
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800869 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700870 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800871 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700872 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700873 }
874
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800875 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700876 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800877 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700878 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700879 }
880
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800881 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700882 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800883 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700884 modifyRoute(interfaceName, ADD, route, SECONDARY);
885 }
886
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800887 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700888 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800889 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700890 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
891 }
892
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800893 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
894 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700895
896 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800897 final LinkAddress la = route.getDestination();
898 cmd.appendArg(la.getAddress().getHostAddress());
899 cmd.appendArg(la.getNetworkPrefixLength());
900
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700901 if (route.getGateway() == null) {
902 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700904 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800905 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700906 }
907 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700909 }
910
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800911 try {
912 mConnector.execute(cmd);
913 } catch (NativeDaemonConnectorException e) {
914 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700915 }
916 }
917
918 private ArrayList<String> readRouteList(String filename) {
919 FileInputStream fstream = null;
920 ArrayList<String> list = new ArrayList<String>();
921
922 try {
923 fstream = new FileInputStream(filename);
924 DataInputStream in = new DataInputStream(fstream);
925 BufferedReader br = new BufferedReader(new InputStreamReader(in));
926 String s;
927
928 // throw away the title line
929
930 while (((s = br.readLine()) != null) && (s.length() != 0)) {
931 list.add(s);
932 }
933 } catch (IOException ex) {
934 // return current list, possibly empty
935 } finally {
936 if (fstream != null) {
937 try {
938 fstream.close();
939 } catch (IOException ex) {}
940 }
941 }
942
943 return list;
944 }
945
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800946 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700947 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800948 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700949 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
950
951 // v4 routes listed as:
952 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
953 for (String s : readRouteList("/proc/net/route")) {
954 String[] fields = s.split("\t");
955
956 if (fields.length > 7) {
957 String iface = fields[0];
958
959 if (interfaceName.equals(iface)) {
960 String dest = fields[1];
961 String gate = fields[2];
962 String flags = fields[3]; // future use?
963 String mask = fields[7];
964 try {
965 // address stored as a hex string, ex: 0014A8C0
966 InetAddress destAddr =
967 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
968 int prefixLength =
969 NetworkUtils.netmaskIntToPrefixLength(
970 (int)Long.parseLong(mask, 16));
971 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
972
973 // address stored as a hex string, ex 0014A8C0
974 InetAddress gatewayAddr =
975 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
976
Wink Saville7b5fd052013-03-15 05:07:04 +0000977 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700978 routes.add(route);
979 } catch (Exception e) {
980 Log.e(TAG, "Error parsing route " + s + " : " + e);
981 continue;
982 }
983 }
984 }
985 }
986
987 // v6 routes listed as:
988 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
989 for (String s : readRouteList("/proc/net/ipv6_route")) {
990 String[]fields = s.split("\\s+");
991 if (fields.length > 9) {
992 String iface = fields[9].trim();
993 if (interfaceName.equals(iface)) {
994 String dest = fields[0];
995 String prefix = fields[1];
996 String gate = fields[4];
997
998 try {
999 // prefix length stored as a hex string, ex 40
1000 int prefixLength = Integer.parseInt(prefix, 16);
1001
1002 // address stored as a 32 char hex string
1003 // ex fe800000000000000000000000000000
1004 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1005 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1006
1007 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1008
Wink Saville7b5fd052013-03-15 05:07:04 +00001009 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001010 routes.add(route);
1011 } catch (Exception e) {
1012 Log.e(TAG, "Error parsing route " + s + " : " + e);
1013 continue;
1014 }
1015 }
1016 }
1017 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001018 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001019 }
1020
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001021 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001022 public void setMtu(String iface, int mtu) {
1023 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1024
1025 final NativeDaemonEvent event;
1026 try {
1027 event = mConnector.execute("interface", "setmtu", iface, mtu);
1028 } catch (NativeDaemonConnectorException e) {
1029 throw e.rethrowAsParcelableException();
1030 }
1031 }
1032
1033 @Override
San Mehat873f2142010-01-14 10:25:07 -08001034 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001035 // TODO: remove from aidl if nobody calls externally
1036 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001037
Joe Onorato8a9b2202010-02-26 18:56:32 -08001038 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001039 }
1040
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001041 @Override
San Mehat873f2142010-01-14 10:25:07 -08001042 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001043 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001044
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001045 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001046 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001047 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001048 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001049 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001050 }
San Mehat873f2142010-01-14 10:25:07 -08001051
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001052 // 211 Forwarding enabled
1053 event.checkCode(IpFwdStatusResult);
1054 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001055 }
1056
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001057 @Override
1058 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001059 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001060 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001061 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001062 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001063 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001064 }
San Mehat873f2142010-01-14 10:25:07 -08001065 }
1066
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001067 @Override
1068 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001069 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001070 // cmd is "tether start first_start first_stop second_start second_stop ..."
1071 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072
1073 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001074 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001075 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001076 }
Kenny Roota80ce062010-06-01 13:23:53 -07001077
1078 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001079 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001080 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001081 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001082 }
San Mehat873f2142010-01-14 10:25:07 -08001083 }
1084
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001085 @Override
1086 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001087 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001088 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001089 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001090 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001091 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001092 }
San Mehat873f2142010-01-14 10:25:07 -08001093 }
1094
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001095 @Override
1096 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001098
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001099 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001100 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001101 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001102 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001103 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001104 }
San Mehat873f2142010-01-14 10:25:07 -08001105
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 // 210 Tethering services started
1107 event.checkCode(TetherStatusResult);
1108 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001109 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001110
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001111 @Override
1112 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001113 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001114 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001115 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001116 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001117 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001118 }
San Mehat873f2142010-01-14 10:25:07 -08001119 }
1120
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001121 @Override
San Mehat873f2142010-01-14 10:25:07 -08001122 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001123 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001124 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001125 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001126 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001127 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001128 }
San Mehat873f2142010-01-14 10:25:07 -08001129 }
1130
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001131 @Override
1132 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001133 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001134 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001135 return NativeDaemonEvent.filterMessageList(
1136 mConnector.executeForList("tether", "interface", "list"),
1137 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001138 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001139 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001140 }
San Mehat873f2142010-01-14 10:25:07 -08001141 }
1142
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001143 @Override
1144 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001145 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001146
1147 final Command cmd = new Command("tether", "dns", "set");
1148 for (String s : dns) {
1149 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1150 }
1151
San Mehat873f2142010-01-14 10:25:07 -08001152 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001153 mConnector.execute(cmd);
1154 } catch (NativeDaemonConnectorException e) {
1155 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001156 }
1157 }
1158
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001159 @Override
1160 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001161 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001162 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001163 return NativeDaemonEvent.filterMessageList(
1164 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001165 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001166 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001167 }
San Mehat873f2142010-01-14 10:25:07 -08001168 }
1169
jiaguo1da35f72014-01-09 16:39:59 +08001170 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1171 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1172 for (InterfaceAddress ia : addresses) {
1173 if (!ia.getAddress().isLinkLocalAddress())
1174 filtered.add(ia);
1175 }
1176 return filtered;
1177 }
1178
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001179 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001180 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001182
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001183 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1184 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001185 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001186 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001187 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001188 // Don't touch link-local routes, as link-local addresses aren't routable,
1189 // kernel creates link-local routes on all interfaces automatically
1190 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1191 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001192 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001193 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001194 InetAddress addr = NetworkUtils.getNetworkPart(
1195 ia.getAddress(), ia.getNetworkPrefixLength());
1196 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001197 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001198 }
1199
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001200 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001201 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001202 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001203 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001204 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001205 }
1206
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001207 @Override
1208 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001209 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001210 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001211 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 } catch (SocketException e) {
1213 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001214 }
San Mehat873f2142010-01-14 10:25:07 -08001215 }
1216
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001217 @Override
1218 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001219 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001220 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001221 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001222 } catch (SocketException e) {
1223 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001224 }
San Mehat873f2142010-01-14 10:25:07 -08001225 }
San Mehat72759df2010-01-19 13:50:37 -08001226
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001227 @Override
1228 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001229 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001230 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001231 return NativeDaemonEvent.filterMessageList(
1232 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001233 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001234 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001235 }
San Mehat72759df2010-01-19 13:50:37 -08001236 }
1237
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001238 @Override
1239 public void attachPppd(
1240 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001241 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001242 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001243 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001244 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1245 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1246 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001247 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001248 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001249 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001250 }
1251 }
1252
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001253 @Override
1254 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001255 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001256 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001257 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001258 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001259 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001260 }
San Mehat72759df2010-01-19 13:50:37 -08001261 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001262
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001263 @Override
1264 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001265 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001266 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001267 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001268 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001269 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001270 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001271 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001272 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001273 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001274 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001275 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001276 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001277 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001278 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001279 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001280 }
1281
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001282 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001283 switch (wifiConfig.getAuthType()) {
1284 case KeyMgmt.WPA_PSK:
1285 return "wpa-psk";
1286 case KeyMgmt.WPA2_PSK:
1287 return "wpa2-psk";
1288 default:
1289 return "open";
1290 }
1291 }
1292
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001293 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001294 @Override
1295 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001296 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001297 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001298 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001299 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001300 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001301 }
1302 }
1303
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001304 @Override
1305 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001306 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001307 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001308 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001309 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001310 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001311 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001312 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001313 }
1314
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001315 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001316 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001317 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001318 try {
1319 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001320 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001321 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001322 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001323 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001324 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001325 }
1326 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001327 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001328 }
1329 }
San Mehat91cac642010-03-31 14:31:36 -07001330
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001331 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001332 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001333 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1334
1335 if (DBG) Slog.d(TAG, "Adding idletimer");
1336
1337 synchronized (mIdleTimerLock) {
1338 IdleTimerParams params = mActiveIdleTimers.get(iface);
1339 if (params != null) {
1340 // the interface already has idletimer, update network count
1341 params.networkCount++;
1342 return;
1343 }
1344
1345 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001346 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1347 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001348 } catch (NativeDaemonConnectorException e) {
1349 throw e.rethrowAsParcelableException();
1350 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001351 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1352
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001353 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001354 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1355 mNetworkActive = false;
1356 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001357 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001358 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001359 notifyInterfaceClassActivity(type,
1360 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1361 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001362 }
1363 });
Haoyu Bai04124232012-06-28 15:26:19 -07001364 }
1365 }
1366
1367 @Override
1368 public void removeIdleTimer(String iface) {
1369 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1370
1371 if (DBG) Slog.d(TAG, "Removing idletimer");
1372
1373 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001374 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001375 if (params == null || --(params.networkCount) > 0) {
1376 return;
1377 }
1378
1379 try {
1380 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001381 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001382 } catch (NativeDaemonConnectorException e) {
1383 throw e.rethrowAsParcelableException();
1384 }
1385 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001386 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001387 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001388 notifyInterfaceClassActivity(params.type,
1389 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1390 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001391 }
1392 });
Haoyu Bai04124232012-06-28 15:26:19 -07001393 }
1394 }
1395
1396 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001397 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001398 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001399 try {
1400 return mStatsFactory.readNetworkStatsSummaryDev();
1401 } catch (IOException e) {
1402 throw new IllegalStateException(e);
1403 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001404 }
1405
1406 @Override
1407 public NetworkStats getNetworkStatsSummaryXt() {
1408 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001409 try {
1410 return mStatsFactory.readNetworkStatsSummaryXt();
1411 } catch (IOException e) {
1412 throw new IllegalStateException(e);
1413 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001414 }
1415
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001416 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001417 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001418 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001419 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001420 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001421 } catch (IOException e) {
1422 throw new IllegalStateException(e);
1423 }
San Mehat91cac642010-03-31 14:31:36 -07001424 }
1425
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001426 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001427 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001428 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001429
Jeff Sharkey350083e2011-06-29 10:45:16 -07001430 // silently discard when control disabled
1431 // TODO: eventually migrate to be always enabled
1432 if (!mBandwidthControlEnabled) return;
1433
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001434 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001435 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001436 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001437 }
1438
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001439 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001440 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001441 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001442 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001443 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001444 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001445 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001446 }
1447 }
1448
1449 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001450 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001451 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001452
Jeff Sharkey350083e2011-06-29 10:45:16 -07001453 // silently discard when control disabled
1454 // TODO: eventually migrate to be always enabled
1455 if (!mBandwidthControlEnabled) return;
1456
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001457 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001458 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001459 // TODO: eventually consider throwing
1460 return;
1461 }
1462
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001463 mActiveQuotas.remove(iface);
1464 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001465
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001466 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001467 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001468 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001469 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001470 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001471 }
1472 }
1473 }
1474
1475 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001476 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001477 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001478
1479 // silently discard when control disabled
1480 // TODO: eventually migrate to be always enabled
1481 if (!mBandwidthControlEnabled) return;
1482
1483 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001484 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001485 throw new IllegalStateException("setting alert requires existing quota on iface");
1486 }
1487
1488 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001489 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001490 throw new IllegalStateException("iface " + iface + " already has alert");
1491 }
1492
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001493 try {
1494 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001495 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001496 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001498 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001499 }
1500 }
1501 }
1502
1503 @Override
1504 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001505 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001506
1507 // silently discard when control disabled
1508 // TODO: eventually migrate to be always enabled
1509 if (!mBandwidthControlEnabled) return;
1510
1511 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001512 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001513 // TODO: eventually consider throwing
1514 return;
1515 }
1516
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001517 try {
1518 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001519 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001520 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001521 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001522 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001523 }
1524 }
1525 }
1526
1527 @Override
1528 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001529 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001530
1531 // silently discard when control disabled
1532 // TODO: eventually migrate to be always enabled
1533 if (!mBandwidthControlEnabled) return;
1534
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001535 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001536 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001537 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001538 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001539 }
1540 }
1541
1542 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001543 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001544 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001545
Jeff Sharkey350083e2011-06-29 10:45:16 -07001546 // silently discard when control disabled
1547 // TODO: eventually migrate to be always enabled
1548 if (!mBandwidthControlEnabled) return;
1549
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001550 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001551 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1552 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1553 // TODO: eventually consider throwing
1554 return;
1555 }
1556
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001557 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001558 mConnector.execute("bandwidth",
1559 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001560 if (rejectOnQuotaInterfaces) {
1561 mUidRejectOnQuota.put(uid, true);
1562 } else {
1563 mUidRejectOnQuota.delete(uid);
1564 }
1565 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001566 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001567 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001568 }
1569 }
1570
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001571 @Override
1572 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001573 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001574 return mBandwidthControlEnabled;
1575 }
1576
1577 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001578 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001579 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001580 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001581 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001582 } catch (IOException e) {
1583 throw new IllegalStateException(e);
1584 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001585 }
1586
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001587 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001588 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001589 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001590
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001591 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001592 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001593 final NativeDaemonEvent[] events = mConnector.executeForList(
1594 "bandwidth", "gettetherstats");
1595 for (NativeDaemonEvent event : events) {
1596 if (event.getCode() != TetheringStatsListResult) continue;
1597
1598 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1599 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1600 try {
1601 final String ifaceIn = tok.nextToken();
1602 final String ifaceOut = tok.nextToken();
1603
1604 final NetworkStats.Entry entry = new NetworkStats.Entry();
1605 entry.iface = ifaceOut;
1606 entry.uid = UID_TETHERING;
1607 entry.set = SET_DEFAULT;
1608 entry.tag = TAG_NONE;
1609 entry.rxBytes = Long.parseLong(tok.nextToken());
1610 entry.rxPackets = Long.parseLong(tok.nextToken());
1611 entry.txBytes = Long.parseLong(tok.nextToken());
1612 entry.txPackets = Long.parseLong(tok.nextToken());
1613 stats.combineValues(entry);
1614 } catch (NoSuchElementException e) {
1615 throw new IllegalStateException("problem parsing tethering stats: " + event);
1616 } catch (NumberFormatException e) {
1617 throw new IllegalStateException("problem parsing tethering stats: " + event);
1618 }
1619 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001620 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001621 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001622 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001623 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001624 }
1625
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001626 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001627 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001628 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001629 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001630 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001631 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001632 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001633 }
1634 }
1635
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001636 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001637 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001638 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001639
Robert Greenwalt8058f622012-11-09 10:52:27 -08001640 final Command cmd = new Command("resolver", "setifdns", iface,
1641 (domains == null ? "" : domains));
1642
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001643 for (String s : servers) {
1644 InetAddress a = NetworkUtils.numericToInetAddress(s);
1645 if (a.isAnyLocalAddress() == false) {
1646 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001647 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001648 }
1649
1650 try {
1651 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001652 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001653 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001654 }
1655 }
1656
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001657 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001658 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1659 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1660 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001661 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001662 "uid", "add", iface, uid_start, uid_end);
1663 } catch (NativeDaemonConnectorException e) {
1664 throw e.rethrowAsParcelableException();
1665 }
1666 }
1667
1668 @Override
1669 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1670 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1671 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001672 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001673 "uid", "remove", iface, uid_start, uid_end);
1674 } catch (NativeDaemonConnectorException e) {
1675 throw e.rethrowAsParcelableException();
1676 }
1677 }
1678
1679 @Override
1680 public void setMarkedForwarding(String iface) {
1681 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1682 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001683 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001684 } catch (NativeDaemonConnectorException e) {
1685 throw e.rethrowAsParcelableException();
1686 }
1687 }
1688
1689 @Override
1690 public void clearMarkedForwarding(String iface) {
1691 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1692 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001693 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1694 } catch (NativeDaemonConnectorException e) {
1695 throw e.rethrowAsParcelableException();
1696 }
1697 }
1698
1699 @Override
1700 public int getMarkForUid(int uid) {
1701 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1702 final NativeDaemonEvent event;
1703 try {
1704 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1705 } catch (NativeDaemonConnectorException e) {
1706 throw e.rethrowAsParcelableException();
1707 }
1708 event.checkCode(GetMarkResult);
1709 return Integer.parseInt(event.getMessage());
1710 }
1711
1712 @Override
1713 public int getMarkForProtect() {
1714 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1715 final NativeDaemonEvent event;
1716 try {
1717 event = mConnector.execute("interface", "fwmark", "get", "protect");
1718 } catch (NativeDaemonConnectorException e) {
1719 throw e.rethrowAsParcelableException();
1720 }
1721 event.checkCode(GetMarkResult);
1722 return Integer.parseInt(event.getMessage());
1723 }
1724
1725 @Override
1726 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1727 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1728 try {
1729 LinkAddress dest = route.getDestination();
1730 mConnector.execute("interface", "fwmark", "route", "add", iface,
1731 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1732 } catch (NativeDaemonConnectorException e) {
1733 throw e.rethrowAsParcelableException();
1734 }
1735 }
1736
1737 @Override
1738 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1739 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1740 try {
1741 LinkAddress dest = route.getDestination();
1742 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1743 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001744 } catch (NativeDaemonConnectorException e) {
1745 throw e.rethrowAsParcelableException();
1746 }
1747 }
1748
1749 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001750 public void setHostExemption(LinkAddress host) {
1751 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1752 try {
1753 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1754 } catch (NativeDaemonConnectorException e) {
1755 throw e.rethrowAsParcelableException();
1756 }
1757 }
1758
1759 @Override
1760 public void clearHostExemption(LinkAddress host) {
1761 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1762 try {
1763 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1764 } catch (NativeDaemonConnectorException e) {
1765 throw e.rethrowAsParcelableException();
1766 }
1767 }
1768
1769 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001770 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1771 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1772 try {
1773 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1774 } catch (NativeDaemonConnectorException e) {
1775 throw e.rethrowAsParcelableException();
1776 }
1777 }
1778
1779 @Override
Chad Brubakerc77261472014-03-21 21:02:43 +00001780 public void clearDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001781 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1782 try {
Chad Brubakerc77261472014-03-21 21:02:43 +00001783 mConnector.execute("resolver", "clearifaceforuidrange", iface, uid_start, uid_end);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001784 } catch (NativeDaemonConnectorException e) {
1785 throw e.rethrowAsParcelableException();
1786 }
1787 }
1788
1789 @Override
1790 public void clearDnsInterfaceMaps() {
1791 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1792 try {
1793 mConnector.execute("resolver", "clearifacemapping");
1794 } catch (NativeDaemonConnectorException e) {
1795 throw e.rethrowAsParcelableException();
1796 }
1797 }
1798
1799
1800 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001801 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001802 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001803 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001804 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001805 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001806 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001807 }
1808 }
1809
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001810 @Override
1811 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001812 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001813 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001814 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001815 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001816 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001817 }
1818 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001819
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001820 @Override
1821 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001822 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001823 try {
1824 mConnector.execute("firewall", enabled ? "enable" : "disable");
1825 mFirewallEnabled = enabled;
1826 } catch (NativeDaemonConnectorException e) {
1827 throw e.rethrowAsParcelableException();
1828 }
1829 }
1830
1831 @Override
1832 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001833 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001834 return mFirewallEnabled;
1835 }
1836
1837 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001838 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001839 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001840 Preconditions.checkState(mFirewallEnabled);
1841 final String rule = allow ? ALLOW : DENY;
1842 try {
1843 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1844 } catch (NativeDaemonConnectorException e) {
1845 throw e.rethrowAsParcelableException();
1846 }
1847 }
1848
1849 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001850 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001851 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001852 Preconditions.checkState(mFirewallEnabled);
1853 final String rule = allow ? ALLOW : DENY;
1854 try {
1855 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1856 } catch (NativeDaemonConnectorException e) {
1857 throw e.rethrowAsParcelableException();
1858 }
1859 }
1860
1861 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001862 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001863 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001864 Preconditions.checkState(mFirewallEnabled);
1865 final String rule = allow ? ALLOW : DENY;
1866 try {
1867 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1868 } catch (NativeDaemonConnectorException e) {
1869 throw e.rethrowAsParcelableException();
1870 }
1871 }
1872
1873 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001874 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001875 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001876 Preconditions.checkState(mFirewallEnabled);
1877 final String rule = allow ? ALLOW : DENY;
1878 try {
1879 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1880 } catch (NativeDaemonConnectorException e) {
1881 throw e.rethrowAsParcelableException();
1882 }
1883 }
1884
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001885 private static void enforceSystemUid() {
1886 final int uid = Binder.getCallingUid();
1887 if (uid != Process.SYSTEM_UID) {
1888 throw new SecurityException("Only available to AID_SYSTEM");
1889 }
1890 }
1891
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001892 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001893 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1894 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1895 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001896 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001897 } catch (NativeDaemonConnectorException e) {
1898 throw new IllegalStateException(
1899 "Error communicating with native deamon to set interface for pid" + iface, e);
1900 }
1901 }
1902
1903 @Override
1904 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1905 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1906 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001907 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001908 } catch (NativeDaemonConnectorException e) {
1909 throw new IllegalStateException(
1910 "Error communicating with native deamon to clear interface for pid " + pid, e);
1911 }
1912 }
1913
Lorenzo Colitti79751842013-02-28 16:16:03 +09001914 @Override
1915 public void startClatd(String interfaceName) throws IllegalStateException {
1916 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1917
1918 try {
1919 mConnector.execute("clatd", "start", interfaceName);
1920 } catch (NativeDaemonConnectorException e) {
1921 throw e.rethrowAsParcelableException();
1922 }
1923 }
1924
1925 @Override
1926 public void stopClatd() throws IllegalStateException {
1927 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1928
1929 try {
1930 mConnector.execute("clatd", "stop");
1931 } catch (NativeDaemonConnectorException e) {
1932 throw e.rethrowAsParcelableException();
1933 }
1934 }
1935
1936 @Override
1937 public boolean isClatdStarted() {
1938 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1939
1940 final NativeDaemonEvent event;
1941 try {
1942 event = mConnector.execute("clatd", "status");
1943 } catch (NativeDaemonConnectorException e) {
1944 throw e.rethrowAsParcelableException();
1945 }
1946
1947 event.checkCode(ClatdStatusResult);
1948 return event.getMessage().endsWith("started");
1949 }
1950
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001951 @Override
1952 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1953 mNetworkActivityListeners.register(listener);
1954 }
1955
1956 @Override
1957 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1958 mNetworkActivityListeners.unregister(listener);
1959 }
1960
1961 @Override
1962 public boolean isNetworkActive() {
1963 synchronized (mNetworkActivityListeners) {
1964 return mNetworkActive || mActiveIdleTimers.isEmpty();
1965 }
1966 }
1967
1968 private void reportNetworkActive() {
1969 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001970 try {
1971 for (int i = 0; i < length; i++) {
1972 try {
1973 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1974 } catch (RemoteException e) {
1975 } catch (RuntimeException e) {
1976 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001977 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001978 } finally {
1979 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001980 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001981 }
1982
Mattias Falk8b47b362011-08-23 14:15:13 +02001983 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001984 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001985 public void monitor() {
1986 if (mConnector != null) {
1987 mConnector.monitor();
1988 }
1989 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001990
1991 @Override
1992 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1993 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1994
Robert Greenwalt470fd722012-01-18 12:51:15 -08001995 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1996 mConnector.dump(fd, pw, args);
1997 pw.println();
1998
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001999 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002000 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
2001 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
2002 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002003
2004 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07002005 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
2006 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002007 }
2008
2009 synchronized (mUidRejectOnQuota) {
2010 pw.print("UID reject on quota ifaces: [");
2011 final int size = mUidRejectOnQuota.size();
2012 for (int i = 0; i < size; i++) {
2013 pw.print(mUidRejectOnQuota.keyAt(i));
2014 if (i < size - 1) pw.print(",");
2015 }
2016 pw.println("]");
2017 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002018
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002019 synchronized (mIdleTimerLock) {
2020 pw.println("Idle timers:");
2021 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
2022 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
2023 IdleTimerParams params = ent.getValue();
2024 pw.print(" timeout="); pw.print(params.timeout);
2025 pw.print(" type="); pw.print(params.type);
2026 pw.print(" networkCount="); pw.println(params.networkCount);
2027 }
2028 }
2029
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002030 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002031 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002032
Robert Greenwalt568891d2014-04-04 13:38:00 -07002033 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002034 public void createNetwork(int netId, String iface) {
2035 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2036
2037 try {
2038 mConnector.execute("network", "create", netId, iface);
2039 } catch (NativeDaemonConnectorException e) {
2040 throw e.rethrowAsParcelableException();
2041 }
2042 }
2043
Robert Greenwalt568891d2014-04-04 13:38:00 -07002044 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002045 public void removeNetwork(int netId) {
2046 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2047
2048 try {
2049 mConnector.execute("network", "destroy", netId);
2050 } catch (NativeDaemonConnectorException e) {
2051 throw e.rethrowAsParcelableException();
2052 }
2053 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002054
2055 @Override
2056 public void addDnsServersForNetId(int netId, String[] servers, String domains) {
2057 modifyDnsServersForNetId(netId, servers, domains, ADD);
2058 }
2059
2060 @Override
2061 public void removeDnsServersForNetId(int netId, String[] servers,
2062 String domains) {
2063 modifyDnsServersForNetId(netId, servers, domains, REMOVE);
2064 }
2065
2066 private void modifyDnsServersForNetId(int netId, String[] servers,
2067 String domains, String action) {
2068 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2069
2070 final Command cmd = new Command("network", "dns", action, netId, servers.length);
2071 for(int i=0; i<servers.length; i++) {
2072 cmd.appendArg(servers[i]);
2073 }
2074 cmd.appendArg((domains == null ? "" : domains));
2075
2076 try {
2077 mConnector.execute(cmd);
2078 } catch (NativeDaemonConnectorException e) {
2079 throw e.rethrowAsParcelableException();
2080 }
2081 }
2082
2083 @Override
2084 public void addRouteForNetId(RouteInfo routeInfo) {
2085 modifyRouteForNetId(routeInfo, ADD);
2086 }
2087
2088 @Override
2089 public void removeRouteForNetId(RouteInfo routeInfo) {
2090 modifyRouteForNetId(routeInfo, REMOVE);
2091 }
2092
2093 private void modifyRouteForNetId(RouteInfo routeInfo, String action) {
2094 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2095
2096 final Command cmd = new Command("network", "route", action);
2097
2098 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
2099 final LinkAddress la = routeInfo.getDestination();
2100 cmd.appendArg(la.getAddress().getHostAddress());
2101 cmd.appendArg(la.getNetworkPrefixLength());
2102
2103 if (routeInfo.getGateway() == null) {
2104 if (la.getAddress() instanceof Inet4Address) {
2105 cmd.appendArg("0.0.0.0");
2106 } else {
2107 cmd.appendArg("::0");
2108 }
2109 } else {
2110 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2111 }
2112
2113 cmd.appendArg(routeInfo.getInterface());
2114
2115 try {
2116 mConnector.execute(cmd);
2117 } catch (NativeDaemonConnectorException e) {
2118 throw e.rethrowAsParcelableException();
2119 }
2120 }
2121
2122 @Override
2123 public void addLegacyRouteForNetId(RouteInfo routeInfo, int uid) {
2124 modifyLegacyRouteForNetId(routeInfo, uid, ADD);
2125 }
2126
2127 @Override
2128 public void removeLegacyRouteForNetId(RouteInfo routeInfo, int uid) {
2129 modifyLegacyRouteForNetId(routeInfo, uid, REMOVE);
2130 }
2131
2132 private void modifyLegacyRouteForNetId(RouteInfo routeInfo, int uid, String action) {
2133 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2134
2135 final Command cmd = new Command("network", "legacy", uid, "route", action);
2136
2137 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
2138 final LinkAddress la = routeInfo.getDestination();
2139 cmd.appendArg(la.getAddress().getHostAddress());
2140 cmd.appendArg(la.getNetworkPrefixLength());
2141
2142 if (routeInfo.getGateway() == null) {
2143 if (la.getAddress() instanceof Inet4Address) {
2144 cmd.appendArg("0.0.0.0");
2145 } else {
2146 cmd.appendArg("::0");
2147 }
2148 } else {
2149 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2150 }
2151
2152 cmd.appendArg(routeInfo.getInterface());
2153
2154 try {
2155 mConnector.execute(cmd);
2156 } catch (NativeDaemonConnectorException e) {
2157 throw e.rethrowAsParcelableException();
2158 }
2159 }
2160
2161 @Override
2162 public void setDefaultNetId(int netId, boolean resetOldSockets) {
2163 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2164
2165 try {
2166 mConnector.execute("network", "default", "set", netId, resetOldSockets);
2167 } catch (NativeDaemonConnectorException e) {
2168 throw e.rethrowAsParcelableException();
2169 }
2170 }
2171
2172 @Override
2173 public void clearDefaultNetId() {
2174 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2175
2176 try {
2177 mConnector.execute("network", "default", "clear");
2178 } catch (NativeDaemonConnectorException e) {
2179 throw e.rethrowAsParcelableException();
2180 }
2181 }
2182
2183 @Override
2184 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2185 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2186
2187 final Command cmd = new Command("network", "permission", "set");
2188 if (internal) cmd.appendArg("CI");
2189 if (changeNetState) cmd.appendArg("CNS");
2190 for (int i=0; i<uids.length; i++) {
2191 cmd.appendArg(uids[i]);
2192 }
2193
2194 try {
2195 mConnector.execute(cmd);
2196 } catch (NativeDaemonConnectorException e) {
2197 throw e.rethrowAsParcelableException();
2198 }
2199 }
2200
2201 @Override
2202 public void clearPermission(int[] uids) {
2203 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2204
2205 final Command cmd = new Command("network", "permission", "clear");
2206 for (int i=0; i<uids.length; i++) {
2207 cmd.appendArg(uids[i]);
2208 }
2209
2210 try {
2211 mConnector.execute(cmd);
2212 } catch (NativeDaemonConnectorException e) {
2213 throw e.rethrowAsParcelableException();
2214 }
2215 }
San Mehat873f2142010-01-14 10:25:07 -08002216}