blob: 878d6c1f6eac346426ef6cf26fc5f8d41a06b8a7 [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());
Robert Greenwalt913c8952014-04-07 17:36:35 -0700900 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700901
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800902 try {
903 mConnector.execute(cmd);
904 } catch (NativeDaemonConnectorException e) {
905 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700906 }
907 }
908
909 private ArrayList<String> readRouteList(String filename) {
910 FileInputStream fstream = null;
911 ArrayList<String> list = new ArrayList<String>();
912
913 try {
914 fstream = new FileInputStream(filename);
915 DataInputStream in = new DataInputStream(fstream);
916 BufferedReader br = new BufferedReader(new InputStreamReader(in));
917 String s;
918
919 // throw away the title line
920
921 while (((s = br.readLine()) != null) && (s.length() != 0)) {
922 list.add(s);
923 }
924 } catch (IOException ex) {
925 // return current list, possibly empty
926 } finally {
927 if (fstream != null) {
928 try {
929 fstream.close();
930 } catch (IOException ex) {}
931 }
932 }
933
934 return list;
935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700938 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800939 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700940 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
941
942 // v4 routes listed as:
943 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
944 for (String s : readRouteList("/proc/net/route")) {
945 String[] fields = s.split("\t");
946
947 if (fields.length > 7) {
948 String iface = fields[0];
949
950 if (interfaceName.equals(iface)) {
951 String dest = fields[1];
952 String gate = fields[2];
953 String flags = fields[3]; // future use?
954 String mask = fields[7];
955 try {
956 // address stored as a hex string, ex: 0014A8C0
957 InetAddress destAddr =
958 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
959 int prefixLength =
960 NetworkUtils.netmaskIntToPrefixLength(
961 (int)Long.parseLong(mask, 16));
962 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
963
964 // address stored as a hex string, ex 0014A8C0
965 InetAddress gatewayAddr =
966 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
967
Wink Saville7b5fd052013-03-15 05:07:04 +0000968 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700969 routes.add(route);
970 } catch (Exception e) {
971 Log.e(TAG, "Error parsing route " + s + " : " + e);
972 continue;
973 }
974 }
975 }
976 }
977
978 // v6 routes listed as:
979 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
980 for (String s : readRouteList("/proc/net/ipv6_route")) {
981 String[]fields = s.split("\\s+");
982 if (fields.length > 9) {
983 String iface = fields[9].trim();
984 if (interfaceName.equals(iface)) {
985 String dest = fields[0];
986 String prefix = fields[1];
987 String gate = fields[4];
988
989 try {
990 // prefix length stored as a hex string, ex 40
991 int prefixLength = Integer.parseInt(prefix, 16);
992
993 // address stored as a 32 char hex string
994 // ex fe800000000000000000000000000000
995 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
996 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
997
998 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
999
Wink Saville7b5fd052013-03-15 05:07:04 +00001000 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001001 routes.add(route);
1002 } catch (Exception e) {
1003 Log.e(TAG, "Error parsing route " + s + " : " + e);
1004 continue;
1005 }
1006 }
1007 }
1008 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001009 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001010 }
1011
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001012 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001013 public void setMtu(String iface, int mtu) {
1014 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1015
1016 final NativeDaemonEvent event;
1017 try {
1018 event = mConnector.execute("interface", "setmtu", iface, mtu);
1019 } catch (NativeDaemonConnectorException e) {
1020 throw e.rethrowAsParcelableException();
1021 }
1022 }
1023
1024 @Override
San Mehat873f2142010-01-14 10:25:07 -08001025 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001026 // TODO: remove from aidl if nobody calls externally
1027 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001028
Joe Onorato8a9b2202010-02-26 18:56:32 -08001029 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001030 }
1031
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001032 @Override
San Mehat873f2142010-01-14 10:25:07 -08001033 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001034 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001035
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001036 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001037 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001038 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001039 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001040 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001041 }
San Mehat873f2142010-01-14 10:25:07 -08001042
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001043 // 211 Forwarding enabled
1044 event.checkCode(IpFwdStatusResult);
1045 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001046 }
1047
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001048 @Override
1049 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001050 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001051 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001052 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001053 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001054 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001055 }
San Mehat873f2142010-01-14 10:25:07 -08001056 }
1057
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001058 @Override
1059 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001060 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001061 // cmd is "tether start first_start first_stop second_start second_stop ..."
1062 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001063
1064 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001065 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001066 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001067 }
Kenny Roota80ce062010-06-01 13:23:53 -07001068
1069 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001070 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001071 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001072 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001073 }
San Mehat873f2142010-01-14 10:25:07 -08001074 }
1075
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001076 @Override
1077 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001078 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001079 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001080 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001081 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001082 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001083 }
San Mehat873f2142010-01-14 10:25:07 -08001084 }
1085
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001086 @Override
1087 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001088 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001089
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001090 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001091 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001092 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001093 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001094 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001095 }
San Mehat873f2142010-01-14 10:25:07 -08001096
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001097 // 210 Tethering services started
1098 event.checkCode(TetherStatusResult);
1099 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001100 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001101
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001102 @Override
1103 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001104 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001105 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001107 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001108 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001109 }
San Mehat873f2142010-01-14 10:25:07 -08001110 }
1111
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001112 @Override
San Mehat873f2142010-01-14 10:25:07 -08001113 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001115 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001116 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001117 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001118 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001119 }
San Mehat873f2142010-01-14 10:25:07 -08001120 }
1121
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001122 @Override
1123 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001124 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001125 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001126 return NativeDaemonEvent.filterMessageList(
1127 mConnector.executeForList("tether", "interface", "list"),
1128 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001129 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001130 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001131 }
San Mehat873f2142010-01-14 10:25:07 -08001132 }
1133
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001134 @Override
1135 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001136 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001137
1138 final Command cmd = new Command("tether", "dns", "set");
1139 for (String s : dns) {
1140 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1141 }
1142
San Mehat873f2142010-01-14 10:25:07 -08001143 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001144 mConnector.execute(cmd);
1145 } catch (NativeDaemonConnectorException e) {
1146 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001147 }
1148 }
1149
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001150 @Override
1151 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001152 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001153 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001154 return NativeDaemonEvent.filterMessageList(
1155 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001156 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001157 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001158 }
San Mehat873f2142010-01-14 10:25:07 -08001159 }
1160
jiaguo1da35f72014-01-09 16:39:59 +08001161 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1162 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1163 for (InterfaceAddress ia : addresses) {
1164 if (!ia.getAddress().isLinkLocalAddress())
1165 filtered.add(ia);
1166 }
1167 return filtered;
1168 }
1169
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001170 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001171 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001172 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001173
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001174 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1175 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001176 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001177 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001178 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001179 // Don't touch link-local routes, as link-local addresses aren't routable,
1180 // kernel creates link-local routes on all interfaces automatically
1181 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1182 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001183 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001184 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001185 InetAddress addr = NetworkUtils.getNetworkPart(
1186 ia.getAddress(), ia.getNetworkPrefixLength());
1187 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001188 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001189 }
1190
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001191 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001192 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001193 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001194 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001195 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001196 }
1197
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001198 @Override
1199 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001200 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001201 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001202 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001203 } catch (SocketException e) {
1204 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001205 }
San Mehat873f2142010-01-14 10:25:07 -08001206 }
1207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001208 @Override
1209 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001210 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001211 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001212 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001213 } catch (SocketException e) {
1214 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001215 }
San Mehat873f2142010-01-14 10:25:07 -08001216 }
San Mehat72759df2010-01-19 13:50:37 -08001217
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001218 @Override
1219 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001220 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001221 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001222 return NativeDaemonEvent.filterMessageList(
1223 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001224 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001225 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001226 }
San Mehat72759df2010-01-19 13:50:37 -08001227 }
1228
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001229 @Override
1230 public void attachPppd(
1231 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001232 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001233 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001234 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001235 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1236 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1237 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001239 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001240 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001241 }
1242 }
1243
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001244 @Override
1245 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001246 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001247 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001248 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001249 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001250 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001251 }
San Mehat72759df2010-01-19 13:50:37 -08001252 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001253
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001254 @Override
1255 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001256 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001257 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001258 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001259 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001260 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001261 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001262 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001263 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001264 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001265 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001266 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001267 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001268 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001269 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001270 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001271 }
1272
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001273 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001274 switch (wifiConfig.getAuthType()) {
1275 case KeyMgmt.WPA_PSK:
1276 return "wpa-psk";
1277 case KeyMgmt.WPA2_PSK:
1278 return "wpa2-psk";
1279 default:
1280 return "open";
1281 }
1282 }
1283
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001284 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001285 @Override
1286 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001287 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001288 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001289 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001290 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001291 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001292 }
1293 }
1294
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001295 @Override
1296 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001297 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001298 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001299 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001300 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001301 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001302 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001303 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001304 }
1305
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001306 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001307 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001308 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001309 try {
1310 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001311 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001312 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001313 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001314 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001315 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001316 }
1317 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001318 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001319 }
1320 }
San Mehat91cac642010-03-31 14:31:36 -07001321
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001322 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001323 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001324 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1325
1326 if (DBG) Slog.d(TAG, "Adding idletimer");
1327
1328 synchronized (mIdleTimerLock) {
1329 IdleTimerParams params = mActiveIdleTimers.get(iface);
1330 if (params != null) {
1331 // the interface already has idletimer, update network count
1332 params.networkCount++;
1333 return;
1334 }
1335
1336 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001337 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1338 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001339 } catch (NativeDaemonConnectorException e) {
1340 throw e.rethrowAsParcelableException();
1341 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001342 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1343
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001344 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001345 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1346 mNetworkActive = false;
1347 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001348 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001349 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001350 notifyInterfaceClassActivity(type,
1351 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1352 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001353 }
1354 });
Haoyu Bai04124232012-06-28 15:26:19 -07001355 }
1356 }
1357
1358 @Override
1359 public void removeIdleTimer(String iface) {
1360 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1361
1362 if (DBG) Slog.d(TAG, "Removing idletimer");
1363
1364 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001365 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001366 if (params == null || --(params.networkCount) > 0) {
1367 return;
1368 }
1369
1370 try {
1371 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001372 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001373 } catch (NativeDaemonConnectorException e) {
1374 throw e.rethrowAsParcelableException();
1375 }
1376 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001377 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001378 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001379 notifyInterfaceClassActivity(params.type,
1380 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1381 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001382 }
1383 });
Haoyu Bai04124232012-06-28 15:26:19 -07001384 }
1385 }
1386
1387 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001388 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001389 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001390 try {
1391 return mStatsFactory.readNetworkStatsSummaryDev();
1392 } catch (IOException e) {
1393 throw new IllegalStateException(e);
1394 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001395 }
1396
1397 @Override
1398 public NetworkStats getNetworkStatsSummaryXt() {
1399 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001400 try {
1401 return mStatsFactory.readNetworkStatsSummaryXt();
1402 } catch (IOException e) {
1403 throw new IllegalStateException(e);
1404 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001405 }
1406
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001407 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001408 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001409 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001410 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001411 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001412 } catch (IOException e) {
1413 throw new IllegalStateException(e);
1414 }
San Mehat91cac642010-03-31 14:31:36 -07001415 }
1416
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001417 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001418 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001419 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001420
Jeff Sharkey350083e2011-06-29 10:45:16 -07001421 // silently discard when control disabled
1422 // TODO: eventually migrate to be always enabled
1423 if (!mBandwidthControlEnabled) return;
1424
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001425 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001426 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001427 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001428 }
1429
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001430 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001431 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001432 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001433 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001434 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001435 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001436 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001437 }
1438 }
1439
1440 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001441 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001442 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001443
Jeff Sharkey350083e2011-06-29 10:45:16 -07001444 // silently discard when control disabled
1445 // TODO: eventually migrate to be always enabled
1446 if (!mBandwidthControlEnabled) return;
1447
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001448 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001449 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001450 // TODO: eventually consider throwing
1451 return;
1452 }
1453
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001454 mActiveQuotas.remove(iface);
1455 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001456
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001457 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001458 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001459 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001460 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001461 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001462 }
1463 }
1464 }
1465
1466 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001467 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001468 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001469
1470 // silently discard when control disabled
1471 // TODO: eventually migrate to be always enabled
1472 if (!mBandwidthControlEnabled) return;
1473
1474 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001475 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001476 throw new IllegalStateException("setting alert requires existing quota on iface");
1477 }
1478
1479 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001480 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001481 throw new IllegalStateException("iface " + iface + " already has alert");
1482 }
1483
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001484 try {
1485 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001486 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001487 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001488 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001489 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001490 }
1491 }
1492 }
1493
1494 @Override
1495 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001496 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497
1498 // silently discard when control disabled
1499 // TODO: eventually migrate to be always enabled
1500 if (!mBandwidthControlEnabled) return;
1501
1502 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001503 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001504 // TODO: eventually consider throwing
1505 return;
1506 }
1507
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001508 try {
1509 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001510 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001511 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001512 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001513 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001514 }
1515 }
1516 }
1517
1518 @Override
1519 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001520 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001521
1522 // silently discard when control disabled
1523 // TODO: eventually migrate to be always enabled
1524 if (!mBandwidthControlEnabled) return;
1525
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001526 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001527 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001528 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001529 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001530 }
1531 }
1532
1533 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001534 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001535 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001536
Jeff Sharkey350083e2011-06-29 10:45:16 -07001537 // silently discard when control disabled
1538 // TODO: eventually migrate to be always enabled
1539 if (!mBandwidthControlEnabled) return;
1540
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001541 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001542 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1543 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1544 // TODO: eventually consider throwing
1545 return;
1546 }
1547
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001548 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001549 mConnector.execute("bandwidth",
1550 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001551 if (rejectOnQuotaInterfaces) {
1552 mUidRejectOnQuota.put(uid, true);
1553 } else {
1554 mUidRejectOnQuota.delete(uid);
1555 }
1556 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001557 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001558 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001559 }
1560 }
1561
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001562 @Override
1563 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001564 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001565 return mBandwidthControlEnabled;
1566 }
1567
1568 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001569 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001570 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001571 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001572 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001573 } catch (IOException e) {
1574 throw new IllegalStateException(e);
1575 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001576 }
1577
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001578 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001579 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001580 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001581
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001582 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001583 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001584 final NativeDaemonEvent[] events = mConnector.executeForList(
1585 "bandwidth", "gettetherstats");
1586 for (NativeDaemonEvent event : events) {
1587 if (event.getCode() != TetheringStatsListResult) continue;
1588
1589 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1590 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1591 try {
1592 final String ifaceIn = tok.nextToken();
1593 final String ifaceOut = tok.nextToken();
1594
1595 final NetworkStats.Entry entry = new NetworkStats.Entry();
1596 entry.iface = ifaceOut;
1597 entry.uid = UID_TETHERING;
1598 entry.set = SET_DEFAULT;
1599 entry.tag = TAG_NONE;
1600 entry.rxBytes = Long.parseLong(tok.nextToken());
1601 entry.rxPackets = Long.parseLong(tok.nextToken());
1602 entry.txBytes = Long.parseLong(tok.nextToken());
1603 entry.txPackets = Long.parseLong(tok.nextToken());
1604 stats.combineValues(entry);
1605 } catch (NoSuchElementException e) {
1606 throw new IllegalStateException("problem parsing tethering stats: " + event);
1607 } catch (NumberFormatException e) {
1608 throw new IllegalStateException("problem parsing tethering stats: " + event);
1609 }
1610 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001611 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001612 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001613 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001614 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001615 }
1616
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001617 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001618 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001619 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001620 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001621 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001622 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001623 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001624 }
1625 }
1626
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001627 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001628 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001629 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001630
Robert Greenwalt8058f622012-11-09 10:52:27 -08001631 final Command cmd = new Command("resolver", "setifdns", iface,
1632 (domains == null ? "" : domains));
1633
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001634 for (String s : servers) {
1635 InetAddress a = NetworkUtils.numericToInetAddress(s);
1636 if (a.isAnyLocalAddress() == false) {
1637 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001638 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001639 }
1640
1641 try {
1642 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001643 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001644 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001645 }
1646 }
1647
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001648 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001649 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1650 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1651 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001652 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001653 "uid", "add", iface, uid_start, uid_end);
1654 } catch (NativeDaemonConnectorException e) {
1655 throw e.rethrowAsParcelableException();
1656 }
1657 }
1658
1659 @Override
1660 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1661 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1662 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001663 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001664 "uid", "remove", iface, uid_start, uid_end);
1665 } catch (NativeDaemonConnectorException e) {
1666 throw e.rethrowAsParcelableException();
1667 }
1668 }
1669
1670 @Override
1671 public void setMarkedForwarding(String iface) {
1672 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1673 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001674 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001675 } catch (NativeDaemonConnectorException e) {
1676 throw e.rethrowAsParcelableException();
1677 }
1678 }
1679
1680 @Override
1681 public void clearMarkedForwarding(String iface) {
1682 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1683 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001684 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1685 } catch (NativeDaemonConnectorException e) {
1686 throw e.rethrowAsParcelableException();
1687 }
1688 }
1689
1690 @Override
1691 public int getMarkForUid(int uid) {
1692 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1693 final NativeDaemonEvent event;
1694 try {
1695 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1696 } catch (NativeDaemonConnectorException e) {
1697 throw e.rethrowAsParcelableException();
1698 }
1699 event.checkCode(GetMarkResult);
1700 return Integer.parseInt(event.getMessage());
1701 }
1702
1703 @Override
1704 public int getMarkForProtect() {
1705 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1706 final NativeDaemonEvent event;
1707 try {
1708 event = mConnector.execute("interface", "fwmark", "get", "protect");
1709 } catch (NativeDaemonConnectorException e) {
1710 throw e.rethrowAsParcelableException();
1711 }
1712 event.checkCode(GetMarkResult);
1713 return Integer.parseInt(event.getMessage());
1714 }
1715
1716 @Override
1717 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1718 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1719 try {
1720 LinkAddress dest = route.getDestination();
1721 mConnector.execute("interface", "fwmark", "route", "add", iface,
1722 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1723 } catch (NativeDaemonConnectorException e) {
1724 throw e.rethrowAsParcelableException();
1725 }
1726 }
1727
1728 @Override
1729 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1730 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1731 try {
1732 LinkAddress dest = route.getDestination();
1733 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1734 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001735 } catch (NativeDaemonConnectorException e) {
1736 throw e.rethrowAsParcelableException();
1737 }
1738 }
1739
1740 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001741 public void setHostExemption(LinkAddress host) {
1742 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1743 try {
1744 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1745 } catch (NativeDaemonConnectorException e) {
1746 throw e.rethrowAsParcelableException();
1747 }
1748 }
1749
1750 @Override
1751 public void clearHostExemption(LinkAddress host) {
1752 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1753 try {
1754 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1755 } catch (NativeDaemonConnectorException e) {
1756 throw e.rethrowAsParcelableException();
1757 }
1758 }
1759
1760 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001761 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1762 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1763 try {
1764 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1765 } catch (NativeDaemonConnectorException e) {
1766 throw e.rethrowAsParcelableException();
1767 }
1768 }
1769
1770 @Override
Chad Brubakerc77261472014-03-21 21:02:43 +00001771 public void clearDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001772 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1773 try {
Chad Brubakerc77261472014-03-21 21:02:43 +00001774 mConnector.execute("resolver", "clearifaceforuidrange", iface, uid_start, uid_end);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001775 } catch (NativeDaemonConnectorException e) {
1776 throw e.rethrowAsParcelableException();
1777 }
1778 }
1779
1780 @Override
1781 public void clearDnsInterfaceMaps() {
1782 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1783 try {
1784 mConnector.execute("resolver", "clearifacemapping");
1785 } catch (NativeDaemonConnectorException e) {
1786 throw e.rethrowAsParcelableException();
1787 }
1788 }
1789
1790
1791 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001792 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001793 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001794 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001795 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001796 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001797 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001798 }
1799 }
1800
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001801 @Override
1802 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001803 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001804 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001805 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001806 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001807 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001808 }
1809 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001810
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001811 @Override
1812 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001813 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001814 try {
1815 mConnector.execute("firewall", enabled ? "enable" : "disable");
1816 mFirewallEnabled = enabled;
1817 } catch (NativeDaemonConnectorException e) {
1818 throw e.rethrowAsParcelableException();
1819 }
1820 }
1821
1822 @Override
1823 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001824 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001825 return mFirewallEnabled;
1826 }
1827
1828 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001829 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001830 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001831 Preconditions.checkState(mFirewallEnabled);
1832 final String rule = allow ? ALLOW : DENY;
1833 try {
1834 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1835 } catch (NativeDaemonConnectorException e) {
1836 throw e.rethrowAsParcelableException();
1837 }
1838 }
1839
1840 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001841 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001842 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001843 Preconditions.checkState(mFirewallEnabled);
1844 final String rule = allow ? ALLOW : DENY;
1845 try {
1846 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1847 } catch (NativeDaemonConnectorException e) {
1848 throw e.rethrowAsParcelableException();
1849 }
1850 }
1851
1852 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001853 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001854 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001855 Preconditions.checkState(mFirewallEnabled);
1856 final String rule = allow ? ALLOW : DENY;
1857 try {
1858 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1859 } catch (NativeDaemonConnectorException e) {
1860 throw e.rethrowAsParcelableException();
1861 }
1862 }
1863
1864 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001865 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001866 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001867 Preconditions.checkState(mFirewallEnabled);
1868 final String rule = allow ? ALLOW : DENY;
1869 try {
1870 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1871 } catch (NativeDaemonConnectorException e) {
1872 throw e.rethrowAsParcelableException();
1873 }
1874 }
1875
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001876 private static void enforceSystemUid() {
1877 final int uid = Binder.getCallingUid();
1878 if (uid != Process.SYSTEM_UID) {
1879 throw new SecurityException("Only available to AID_SYSTEM");
1880 }
1881 }
1882
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001883 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001884 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1885 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1886 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001887 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001888 } catch (NativeDaemonConnectorException e) {
1889 throw new IllegalStateException(
1890 "Error communicating with native deamon to set interface for pid" + iface, e);
1891 }
1892 }
1893
1894 @Override
1895 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1896 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1897 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001898 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001899 } catch (NativeDaemonConnectorException e) {
1900 throw new IllegalStateException(
1901 "Error communicating with native deamon to clear interface for pid " + pid, e);
1902 }
1903 }
1904
Lorenzo Colitti79751842013-02-28 16:16:03 +09001905 @Override
1906 public void startClatd(String interfaceName) throws IllegalStateException {
1907 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1908
1909 try {
1910 mConnector.execute("clatd", "start", interfaceName);
1911 } catch (NativeDaemonConnectorException e) {
1912 throw e.rethrowAsParcelableException();
1913 }
1914 }
1915
1916 @Override
1917 public void stopClatd() throws IllegalStateException {
1918 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1919
1920 try {
1921 mConnector.execute("clatd", "stop");
1922 } catch (NativeDaemonConnectorException e) {
1923 throw e.rethrowAsParcelableException();
1924 }
1925 }
1926
1927 @Override
1928 public boolean isClatdStarted() {
1929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1930
1931 final NativeDaemonEvent event;
1932 try {
1933 event = mConnector.execute("clatd", "status");
1934 } catch (NativeDaemonConnectorException e) {
1935 throw e.rethrowAsParcelableException();
1936 }
1937
1938 event.checkCode(ClatdStatusResult);
1939 return event.getMessage().endsWith("started");
1940 }
1941
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001942 @Override
1943 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1944 mNetworkActivityListeners.register(listener);
1945 }
1946
1947 @Override
1948 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1949 mNetworkActivityListeners.unregister(listener);
1950 }
1951
1952 @Override
1953 public boolean isNetworkActive() {
1954 synchronized (mNetworkActivityListeners) {
1955 return mNetworkActive || mActiveIdleTimers.isEmpty();
1956 }
1957 }
1958
1959 private void reportNetworkActive() {
1960 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001961 try {
1962 for (int i = 0; i < length; i++) {
1963 try {
1964 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1965 } catch (RemoteException e) {
1966 } catch (RuntimeException e) {
1967 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001968 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001969 } finally {
1970 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001971 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001972 }
1973
Mattias Falk8b47b362011-08-23 14:15:13 +02001974 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001975 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001976 public void monitor() {
1977 if (mConnector != null) {
1978 mConnector.monitor();
1979 }
1980 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001981
1982 @Override
1983 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1984 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1985
Robert Greenwalt470fd722012-01-18 12:51:15 -08001986 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1987 mConnector.dump(fd, pw, args);
1988 pw.println();
1989
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001990 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001991 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1992 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1993 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001994
1995 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001996 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1997 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001998 }
1999
2000 synchronized (mUidRejectOnQuota) {
2001 pw.print("UID reject on quota ifaces: [");
2002 final int size = mUidRejectOnQuota.size();
2003 for (int i = 0; i < size; i++) {
2004 pw.print(mUidRejectOnQuota.keyAt(i));
2005 if (i < size - 1) pw.print(",");
2006 }
2007 pw.println("]");
2008 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002009
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002010 synchronized (mIdleTimerLock) {
2011 pw.println("Idle timers:");
2012 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
2013 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
2014 IdleTimerParams params = ent.getValue();
2015 pw.print(" timeout="); pw.print(params.timeout);
2016 pw.print(" type="); pw.print(params.type);
2017 pw.print(" networkCount="); pw.println(params.networkCount);
2018 }
2019 }
2020
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002021 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002022 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002023
Robert Greenwalt568891d2014-04-04 13:38:00 -07002024 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002025 public void createNetwork(int netId, String iface) {
2026 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2027
2028 try {
2029 mConnector.execute("network", "create", netId, iface);
2030 } catch (NativeDaemonConnectorException e) {
2031 throw e.rethrowAsParcelableException();
2032 }
2033 }
2034
Robert Greenwalt568891d2014-04-04 13:38:00 -07002035 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002036 public void removeNetwork(int netId) {
2037 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2038
2039 try {
2040 mConnector.execute("network", "destroy", netId);
2041 } catch (NativeDaemonConnectorException e) {
2042 throw e.rethrowAsParcelableException();
2043 }
2044 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002045
2046 @Override
2047 public void addDnsServersForNetId(int netId, String[] servers, String domains) {
2048 modifyDnsServersForNetId(netId, servers, domains, ADD);
2049 }
2050
2051 @Override
2052 public void removeDnsServersForNetId(int netId, String[] servers,
2053 String domains) {
2054 modifyDnsServersForNetId(netId, servers, domains, REMOVE);
2055 }
2056
2057 private void modifyDnsServersForNetId(int netId, String[] servers,
2058 String domains, String action) {
2059 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2060
2061 final Command cmd = new Command("network", "dns", action, netId, servers.length);
2062 for(int i=0; i<servers.length; i++) {
2063 cmd.appendArg(servers[i]);
2064 }
2065 cmd.appendArg((domains == null ? "" : domains));
2066
2067 try {
2068 mConnector.execute(cmd);
2069 } catch (NativeDaemonConnectorException e) {
2070 throw e.rethrowAsParcelableException();
2071 }
2072 }
2073
2074 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002075 public void addRouteForNetId(int netId, RouteInfo routeInfo) {
2076 modifyRouteForNetId(netId, routeInfo, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002077 }
2078
2079 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002080 public void removeRouteForNetId(int netId, RouteInfo routeInfo) {
2081 modifyRouteForNetId(netId, routeInfo, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002082 }
2083
Robert Greenwalt913c8952014-04-07 17:36:35 -07002084 private void modifyRouteForNetId(int netId, RouteInfo routeInfo, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002085 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2086
Robert Greenwalt913c8952014-04-07 17:36:35 -07002087 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002088
2089 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
2090 final LinkAddress la = routeInfo.getDestination();
2091 cmd.appendArg(la.getAddress().getHostAddress());
2092 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -07002093 cmd.appendArg(routeInfo.getGateway().getHostAddress());
Robert Greenwalt568891d2014-04-04 13:38:00 -07002094 cmd.appendArg(routeInfo.getInterface());
2095
2096 try {
2097 mConnector.execute(cmd);
2098 } catch (NativeDaemonConnectorException e) {
2099 throw e.rethrowAsParcelableException();
2100 }
2101 }
2102
2103 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002104 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2105 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002106 }
2107
2108 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002109 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2110 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002111 }
2112
Robert Greenwalt913c8952014-04-07 17:36:35 -07002113 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2115
Robert Greenwalt913c8952014-04-07 17:36:35 -07002116 final Command cmd = new Command("network", "legacy", uid, "route", action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002117
2118 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
2119 final LinkAddress la = routeInfo.getDestination();
2120 cmd.appendArg(la.getAddress().getHostAddress());
2121 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -07002122 cmd.appendArg(routeInfo.getGateway().getHostAddress());
Robert Greenwalt568891d2014-04-04 13:38:00 -07002123 cmd.appendArg(routeInfo.getInterface());
2124
2125 try {
2126 mConnector.execute(cmd);
2127 } catch (NativeDaemonConnectorException e) {
2128 throw e.rethrowAsParcelableException();
2129 }
2130 }
2131
2132 @Override
2133 public void setDefaultNetId(int netId, boolean resetOldSockets) {
2134 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2135
2136 try {
2137 mConnector.execute("network", "default", "set", netId, resetOldSockets);
2138 } catch (NativeDaemonConnectorException e) {
2139 throw e.rethrowAsParcelableException();
2140 }
2141 }
2142
2143 @Override
2144 public void clearDefaultNetId() {
2145 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2146
2147 try {
2148 mConnector.execute("network", "default", "clear");
2149 } catch (NativeDaemonConnectorException e) {
2150 throw e.rethrowAsParcelableException();
2151 }
2152 }
2153
2154 @Override
2155 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2157
2158 final Command cmd = new Command("network", "permission", "set");
2159 if (internal) cmd.appendArg("CI");
2160 if (changeNetState) cmd.appendArg("CNS");
2161 for (int i=0; i<uids.length; i++) {
2162 cmd.appendArg(uids[i]);
2163 }
2164
2165 try {
2166 mConnector.execute(cmd);
2167 } catch (NativeDaemonConnectorException e) {
2168 throw e.rethrowAsParcelableException();
2169 }
2170 }
2171
2172 @Override
2173 public void clearPermission(int[] uids) {
2174 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2175
2176 final Command cmd = new Command("network", "permission", "clear");
2177 for (int i=0; i<uids.length; i++) {
2178 cmd.appendArg(uids[i]);
2179 }
2180
2181 try {
2182 mConnector.execute(cmd);
2183 } catch (NativeDaemonConnectorException e) {
2184 throw e.rethrowAsParcelableException();
2185 }
2186 }
San Mehat873f2142010-01-14 10:25:07 -08002187}