blob: 24af6b61f9f2aae89bdb9ba558cd261e74bed3fe [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -070019import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkey4529bb62011-12-14 10:31:54 -080020import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070021import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080022import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070023import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080024import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.NetworkStats.TAG_NONE;
26import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070027import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090028import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070029import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080032import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070036import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080037import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070038import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070039
San Mehat873f2142010-01-14 10:25:07 -080040import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080041import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080042import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070045import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080046import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070047import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080048import android.net.wifi.WifiConfiguration;
49import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070050import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070052import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080053import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080054import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080055import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070056import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080057import android.os.RemoteCallbackList;
58import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070059import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070060import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080061import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070062import android.telephony.DataConnectionRealTimeInfo;
63import android.telephony.PhoneStateListener;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080064import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080065import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070066import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080067
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070068import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070069import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070070import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080071import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070072import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070073import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070074import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070075
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070076import java.io.BufferedReader;
77import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080078import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070079import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070080import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070081import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070082import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070083import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070084import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070085import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070086import java.net.InterfaceAddress;
87import java.net.NetworkInterface;
88import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070089import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070090import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080091import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070092import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070093import java.util.NoSuchElementException;
94import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070095import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080096
97/**
98 * @hide
99 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700100public class NetworkManagementService extends INetworkManagementService.Stub
101 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700102 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700103 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700104 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900105 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700106
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800107 private static final String ADD = "add";
108 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700109
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700110 private static final String ALLOW = "allow";
111 private static final String DENY = "deny";
112
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700113 private static final String DEFAULT = "default";
114 private static final String SECONDARY = "secondary";
115
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700116 /**
117 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
118 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
119 */
120 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
121
San Mehat873f2142010-01-14 10:25:07 -0800122 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700123 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800124 public static final int InterfaceListResult = 110;
125 public static final int TetherInterfaceListResult = 111;
126 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800127 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700128 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800129
130 public static final int TetherStatusResult = 210;
131 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800132 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800133 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700134 public static final int InterfaceRxCounterResult = 216;
135 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700136 public static final int QuotaCounterResult = 220;
137 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800138 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900139 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700140 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800141
142 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700143 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700144 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900145 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900146 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800147 }
148
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700149 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
150
San Mehat873f2142010-01-14 10:25:07 -0800151 /**
152 * Binder context for this service
153 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700154 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800155
156 /**
157 * connector object for communicating with netd
158 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700159 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800160
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700161 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700162 private final Handler mDaemonHandler;
163 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700164
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800165 private IBatteryStats mBatteryStats;
166
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700167 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700168 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700169
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800170 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
171 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800172
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700173 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
174
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700175 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700176 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700177 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700178 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700179 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700180 /** Set of UIDs with active reject rules. */
181 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
182
Haoyu Bai04124232012-06-28 15:26:19 -0700183 private Object mIdleTimerLock = new Object();
184 /** Set of interfaces with active idle timers. */
185 private static class IdleTimerParams {
186 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800187 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700188 public int networkCount;
189
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800190 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700191 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800192 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700193 this.networkCount = 1;
194 }
195 }
196 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
197
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700198 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700199 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700200
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700201 private boolean mMobileActivityFromRadio = false;
202 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
203
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800204 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
205 new RemoteCallbackList<INetworkActivityListener>();
206 private boolean mNetworkActive;
207
San Mehat873f2142010-01-14 10:25:07 -0800208 /**
209 * Constructs a new NetworkManagementService instance
210 *
211 * @param context Binder context for this service
212 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900213 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800214 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800215
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700216 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
217 mFgHandler = new Handler(FgThread.get().getLooper());
218
Marco Nelissen62dbb222010-02-18 10:56:30 -0800219 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700220 mConnector = null;
221 mThread = null;
222 mDaemonHandler = null;
223 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800224 return;
225 }
226
Dianne Hackborn4590e522014-03-24 13:36:46 -0700227 // Don't need this wake lock, since we now have a time stamp for when
228 // the network actually went inactive. (It might be nice to still do this,
229 // but I don't want to do it through the power manager because that pollutes the
230 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700231 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700232 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800233
San Mehat873f2142010-01-14 10:25:07 -0800234 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700235 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
236 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700237 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700238
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700239 mDaemonHandler = new Handler(FgThread.get().getLooper());
240 mPhoneStateListener = new PhoneStateListener(mDaemonHandler.getLooper()) {
241 public void onDataConnectionRealTimeInfoChanged(
242 DataConnectionRealTimeInfo dcRtInfo) {
243 // Disabled for now, until we are getting good data.
244 //notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
245 // dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
246 }
247 };
248
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700249 // Add ourself to the Watchdog monitors.
250 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700251 }
252
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900253 static NetworkManagementService create(Context context,
254 String socket) throws InterruptedException {
255 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700256 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700257 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
258 service.mThread.start();
259 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700260 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700261 if (DBG) Slog.d(TAG, "Connected");
262 return service;
San Mehat873f2142010-01-14 10:25:07 -0800263 }
264
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900265 public static NetworkManagementService create(Context context) throws InterruptedException {
266 return create(context, NETD_SOCKET_NAME);
267 }
268
Jeff Sharkey350083e2011-06-29 10:45:16 -0700269 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700270 prepareNativeDaemon();
271 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700272 }
273
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800274 private IBatteryStats getBatteryStats() {
275 synchronized (this) {
276 if (mBatteryStats != null) {
277 return mBatteryStats;
278 }
279 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
280 BatteryStats.SERVICE_NAME));
281 return mBatteryStats;
282 }
283 }
284
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800285 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800286 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800287 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800288 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800289 }
290
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800291 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800292 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800293 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800294 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800295 }
296
297 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700298 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800299 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700300 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800301 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700302 try {
303 for (int i = 0; i < length; i++) {
304 try {
305 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
306 } catch (RemoteException e) {
307 } catch (RuntimeException e) {
308 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700309 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700310 } finally {
311 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700312 }
313 }
314
315 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700316 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700317 * (typically, an Ethernet cable has been plugged-in or unplugged).
318 */
319 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800320 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700321 try {
322 for (int i = 0; i < length; i++) {
323 try {
324 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
325 } catch (RemoteException e) {
326 } catch (RuntimeException e) {
327 }
San Mehat4d02d002010-01-22 16:07:46 -0800328 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700329 } finally {
330 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800331 }
332 }
333
334 /**
335 * Notify our observers of an interface addition.
336 */
337 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800338 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700339 try {
340 for (int i = 0; i < length; i++) {
341 try {
342 mObservers.getBroadcastItem(i).interfaceAdded(iface);
343 } catch (RemoteException e) {
344 } catch (RuntimeException e) {
345 }
San Mehat4d02d002010-01-22 16:07:46 -0800346 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700347 } finally {
348 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800349 }
350 }
351
352 /**
353 * Notify our observers of an interface removal.
354 */
355 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700356 // netd already clears out quota and alerts for removed ifaces; update
357 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700358 mActiveAlerts.remove(iface);
359 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700360
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800361 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700362 try {
363 for (int i = 0; i < length; i++) {
364 try {
365 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
366 } catch (RemoteException e) {
367 } catch (RuntimeException e) {
368 }
San Mehat4d02d002010-01-22 16:07:46 -0800369 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700370 } finally {
371 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800372 }
373 }
374
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700375 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700376 * Notify our observers of a limit reached.
377 */
378 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800379 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700380 try {
381 for (int i = 0; i < length; i++) {
382 try {
383 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
384 } catch (RemoteException e) {
385 } catch (RuntimeException e) {
386 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700387 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700388 } finally {
389 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700390 }
391 }
392
393 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700394 * Notify our observers of a change in the data activity state of the interface
395 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700396 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
397 boolean fromRadio) {
398 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
399 if (isMobile) {
400 if (!fromRadio) {
401 if (mMobileActivityFromRadio) {
402 // If this call is not coming from a report from the radio itself, but we
403 // have previously received reports from the radio, then we will take the
404 // power state to just be whatever the radio last reported.
405 powerState = mLastPowerStateFromRadio;
406 }
407 } else {
408 mMobileActivityFromRadio = true;
409 }
410 if (mLastPowerStateFromRadio != powerState) {
411 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700412 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700413 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700414 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700415 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700416 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700417 }
418
419 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
420 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
421
422 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
423 // Report the change in data activity. We don't do this if this is a change
424 // on the mobile network, that is not coming from the radio itself, and we
425 // have previously seen change reports from the radio. In that case only
426 // the radio is the authority for the current state.
427 final int length = mObservers.beginBroadcast();
428 try {
429 for (int i = 0; i < length; i++) {
430 try {
431 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
432 Integer.toString(type), isActive, tsNanos);
433 } catch (RemoteException e) {
434 } catch (RuntimeException e) {
435 }
436 }
437 } finally {
438 mObservers.finishBroadcast();
439 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700440 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800441
442 boolean report = false;
443 synchronized (mIdleTimerLock) {
444 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700445 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800446 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700447 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800448 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700449 if (mNetworkActive != isActive) {
450 mNetworkActive = isActive;
451 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800452 }
453 }
454 if (report) {
455 reportNetworkActive();
456 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700457 }
458
459 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700460 * Prepare native daemon once connected, enabling modules and pushing any
461 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700462 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700463 private void prepareNativeDaemon() {
464 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700465
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700466 // only enable bandwidth control when support exists
467 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
468 if (hasKernelSupport) {
469 Slog.d(TAG, "enabling bandwidth control");
470 try {
471 mConnector.execute("bandwidth", "enable");
472 mBandwidthControlEnabled = true;
473 } catch (NativeDaemonConnectorException e) {
474 Log.wtf(TAG, "problem enabling bandwidth controls", e);
475 }
476 } else {
477 Slog.d(TAG, "not enabling bandwidth control");
478 }
479
480 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
481
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700482 if (mBandwidthControlEnabled) {
483 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800484 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700485 } catch (RemoteException e) {
486 }
487 }
488
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700489 // push any existing quota or UID rules
490 synchronized (mQuotaLock) {
491 int size = mActiveQuotas.size();
492 if (size > 0) {
493 Slog.d(TAG, "pushing " + size + " active quota rules");
494 final HashMap<String, Long> activeQuotas = mActiveQuotas;
495 mActiveQuotas = Maps.newHashMap();
496 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
497 setInterfaceQuota(entry.getKey(), entry.getValue());
498 }
499 }
500
501 size = mActiveAlerts.size();
502 if (size > 0) {
503 Slog.d(TAG, "pushing " + size + " active alert rules");
504 final HashMap<String, Long> activeAlerts = mActiveAlerts;
505 mActiveAlerts = Maps.newHashMap();
506 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
507 setInterfaceAlert(entry.getKey(), entry.getValue());
508 }
509 }
510
511 size = mUidRejectOnQuota.size();
512 if (size > 0) {
513 Slog.d(TAG, "pushing " + size + " active uid rules");
514 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
515 mUidRejectOnQuota = new SparseBooleanArray();
516 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
517 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
518 }
519 }
520 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700521
522 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700523 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700524 }
San Mehat4d02d002010-01-22 16:07:46 -0800525
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900526 /**
527 * Notify our observers of a new or updated interface address.
528 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900529 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900530 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700531 try {
532 for (int i = 0; i < length; i++) {
533 try {
534 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
535 } catch (RemoteException e) {
536 } catch (RuntimeException e) {
537 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900538 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700539 } finally {
540 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900541 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900542 }
543
544 /**
545 * Notify our observers of a deleted interface address.
546 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900547 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900548 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700549 try {
550 for (int i = 0; i < length; i++) {
551 try {
552 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
553 } catch (RemoteException e) {
554 } catch (RuntimeException e) {
555 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900556 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700557 } finally {
558 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900559 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900560 }
561
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900562 /**
563 * Notify our observers of DNS server information received.
564 */
565 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
566 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700567 try {
568 for (int i = 0; i < length; i++) {
569 try {
570 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
571 addresses);
572 } catch (RemoteException e) {
573 } catch (RuntimeException e) {
574 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900575 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700576 } finally {
577 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900578 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900579 }
580
San Mehat873f2142010-01-14 10:25:07 -0800581 //
582 // Netd Callback handling
583 //
584
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700585 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
586 @Override
San Mehat873f2142010-01-14 10:25:07 -0800587 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700588 // event is dispatched from internal NDC thread, so we prepare the
589 // daemon back on main thread.
590 if (mConnectedSignal != null) {
591 mConnectedSignal.countDown();
592 mConnectedSignal = null;
593 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700594 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700595 @Override
596 public void run() {
597 prepareNativeDaemon();
598 }
599 });
600 }
San Mehat873f2142010-01-14 10:25:07 -0800601 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700602
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700603 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800604 public boolean onCheckHoldWakeLock(int code) {
605 return code == NetdResponseCode.InterfaceClassActivity;
606 }
607
608 @Override
San Mehat873f2142010-01-14 10:25:07 -0800609 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900610 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700611 switch (code) {
612 case NetdResponseCode.InterfaceChange:
613 /*
614 * a network interface change occured
615 * Format: "NNN Iface added <name>"
616 * "NNN Iface removed <name>"
617 * "NNN Iface changed <name> <up/down>"
618 * "NNN Iface linkstatus <name> <up/down>"
619 */
620 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900621 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700622 }
623 if (cooked[2].equals("added")) {
624 notifyInterfaceAdded(cooked[3]);
625 return true;
626 } else if (cooked[2].equals("removed")) {
627 notifyInterfaceRemoved(cooked[3]);
628 return true;
629 } else if (cooked[2].equals("changed") && cooked.length == 5) {
630 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
631 return true;
632 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
633 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
634 return true;
635 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900636 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700637 // break;
638 case NetdResponseCode.BandwidthControl:
639 /*
640 * Bandwidth control needs some attention
641 * Format: "NNN limit alert <alertName> <ifaceName>"
642 */
643 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900644 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700645 }
646 if (cooked[2].equals("alert")) {
647 notifyLimitReached(cooked[3], cooked[4]);
648 return true;
649 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900650 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700651 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700652 case NetdResponseCode.InterfaceClassActivity:
653 /*
654 * An network interface class state changed (active/idle)
655 * Format: "NNN IfaceClass <active/idle> <label>"
656 */
657 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900658 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700659 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700660 long timestampNanos = 0;
661 if (cooked.length == 5) {
662 try {
663 timestampNanos = Long.parseLong(cooked[4]);
664 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700665 } else {
666 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700667 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700668 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700669 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700670 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
671 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700672 return true;
673 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900674 case NetdResponseCode.InterfaceAddressChange:
675 /*
676 * A network address change occurred
677 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
678 * "NNN Address removed <addr> <iface> <flags> <scope>"
679 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900680 if (cooked.length < 7 || !cooked[1].equals("Address")) {
681 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900682 }
683
Lorenzo Colitti64483942013-11-15 18:43:52 +0900684 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900685 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900686 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900687 int flags = Integer.parseInt(cooked[5]);
688 int scope = Integer.parseInt(cooked[6]);
689 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900690 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
691 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900692 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900693 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900694 }
695
696 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900697 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900698 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900699 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900700 }
701 return true;
702 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900703 case NetdResponseCode.InterfaceDnsServerInfo:
704 /*
705 * Information about available DNS servers has been received.
706 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
707 */
708 long lifetime; // Actually a 32-bit unsigned integer.
709
710 if (cooked.length == 6 &&
711 cooked[1].equals("DnsInfo") &&
712 cooked[2].equals("servers")) {
713 try {
714 lifetime = Long.parseLong(cooked[4]);
715 } catch (NumberFormatException e) {
716 throw new IllegalStateException(errorMessage);
717 }
718 String[] servers = cooked[5].split(",");
719 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
720 }
721 return true;
722 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700723 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800724 }
725 return false;
San Mehat873f2142010-01-14 10:25:07 -0800726 }
727 }
728
San Mehated4fc8a2010-01-22 12:28:36 -0800729
San Mehat873f2142010-01-14 10:25:07 -0800730 //
731 // INetworkManagementService members
732 //
733
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800734 @Override
735 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800736 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700737 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800738 return NativeDaemonEvent.filterMessageList(
739 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700740 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800741 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700742 }
San Mehated4fc8a2010-01-22 12:28:36 -0800743 }
744
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800745 @Override
746 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800747 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800748
749 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700750 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800751 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700752 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800753 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700754 }
San Mehated4fc8a2010-01-22 12:28:36 -0800755
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800756 event.checkCode(InterfaceGetCfgResult);
757
758 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
759 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800760
Kenny Roota80ce062010-06-01 13:23:53 -0700761 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800762 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700763 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800764 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800765 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800766 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700767 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800769 } catch (IllegalArgumentException iae) {
770 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700771 }
772
773 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800774 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800775 } catch (NumberFormatException nfe) {
776 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700777 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800778
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800779 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
780 while (st.hasMoreTokens()) {
781 cfg.setFlag(st.nextToken());
782 }
Kenny Roota80ce062010-06-01 13:23:53 -0700783 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800784 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800785 }
San Mehated4fc8a2010-01-22 12:28:36 -0800786 return cfg;
787 }
788
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800789 @Override
790 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800791 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800792 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800793 if (linkAddr == null || linkAddr.getAddress() == null) {
794 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800795 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800796
797 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800798 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799 linkAddr.getNetworkPrefixLength());
800 for (String flag : cfg.getFlags()) {
801 cmd.appendArg(flag);
802 }
803
Kenny Roota80ce062010-06-01 13:23:53 -0700804 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700806 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800807 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700808 }
San Mehat873f2142010-01-14 10:25:07 -0800809 }
810
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800811 @Override
812 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800813 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800814 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800815 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800816 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700817 }
818
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800819 @Override
820 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800821 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800822 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800823 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800824 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700825 }
826
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800827 @Override
828 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800829 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700830 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800831 mConnector.execute(
832 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700833 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800834 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700835 }
836 }
837
Irfan Sherifff5600612011-06-16 10:26:28 -0700838 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
839 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800840 @Override
841 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800842 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700843 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800844 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700845 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800846 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700847 }
848 }
849
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800850 @Override
851 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800852 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700853 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800854 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700855 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800856 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700857 }
858 }
859
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800860 @Override
861 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800862 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700863 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800864 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700865 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800866 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700867 }
868 }
869
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800870 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700871 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800872 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700873 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700874 }
875
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800876 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700877 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800878 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700879 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700880 }
881
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800882 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700883 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800884 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700885 modifyRoute(interfaceName, ADD, route, SECONDARY);
886 }
887
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800888 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700889 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800890 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700891 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
892 }
893
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800894 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
895 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700896
897 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800898 final LinkAddress la = route.getDestination();
899 cmd.appendArg(la.getAddress().getHostAddress());
900 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -0700901 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700902
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 try {
904 mConnector.execute(cmd);
905 } catch (NativeDaemonConnectorException e) {
906 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700907 }
908 }
909
910 private ArrayList<String> readRouteList(String filename) {
911 FileInputStream fstream = null;
912 ArrayList<String> list = new ArrayList<String>();
913
914 try {
915 fstream = new FileInputStream(filename);
916 DataInputStream in = new DataInputStream(fstream);
917 BufferedReader br = new BufferedReader(new InputStreamReader(in));
918 String s;
919
920 // throw away the title line
921
922 while (((s = br.readLine()) != null) && (s.length() != 0)) {
923 list.add(s);
924 }
925 } catch (IOException ex) {
926 // return current list, possibly empty
927 } finally {
928 if (fstream != null) {
929 try {
930 fstream.close();
931 } catch (IOException ex) {}
932 }
933 }
934
935 return list;
936 }
937
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800938 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700939 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800940 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700941 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
942
943 // v4 routes listed as:
944 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
945 for (String s : readRouteList("/proc/net/route")) {
946 String[] fields = s.split("\t");
947
948 if (fields.length > 7) {
949 String iface = fields[0];
950
951 if (interfaceName.equals(iface)) {
952 String dest = fields[1];
953 String gate = fields[2];
954 String flags = fields[3]; // future use?
955 String mask = fields[7];
956 try {
957 // address stored as a hex string, ex: 0014A8C0
958 InetAddress destAddr =
959 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
960 int prefixLength =
961 NetworkUtils.netmaskIntToPrefixLength(
962 (int)Long.parseLong(mask, 16));
963 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
964
965 // address stored as a hex string, ex 0014A8C0
966 InetAddress gatewayAddr =
967 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
968
Wink Saville7b5fd052013-03-15 05:07:04 +0000969 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700970 routes.add(route);
971 } catch (Exception e) {
972 Log.e(TAG, "Error parsing route " + s + " : " + e);
973 continue;
974 }
975 }
976 }
977 }
978
979 // v6 routes listed as:
980 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
981 for (String s : readRouteList("/proc/net/ipv6_route")) {
982 String[]fields = s.split("\\s+");
983 if (fields.length > 9) {
984 String iface = fields[9].trim();
985 if (interfaceName.equals(iface)) {
986 String dest = fields[0];
987 String prefix = fields[1];
988 String gate = fields[4];
989
990 try {
991 // prefix length stored as a hex string, ex 40
992 int prefixLength = Integer.parseInt(prefix, 16);
993
994 // address stored as a 32 char hex string
995 // ex fe800000000000000000000000000000
996 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
997 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
998
999 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1000
Wink Saville7b5fd052013-03-15 05:07:04 +00001001 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001002 routes.add(route);
1003 } catch (Exception e) {
1004 Log.e(TAG, "Error parsing route " + s + " : " + e);
1005 continue;
1006 }
1007 }
1008 }
1009 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001010 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001011 }
1012
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001013 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001014 public void setMtu(String iface, int mtu) {
1015 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1016
1017 final NativeDaemonEvent event;
1018 try {
1019 event = mConnector.execute("interface", "setmtu", iface, mtu);
1020 } catch (NativeDaemonConnectorException e) {
1021 throw e.rethrowAsParcelableException();
1022 }
1023 }
1024
1025 @Override
San Mehat873f2142010-01-14 10:25:07 -08001026 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001027 // TODO: remove from aidl if nobody calls externally
1028 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001029
Joe Onorato8a9b2202010-02-26 18:56:32 -08001030 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001031 }
1032
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001033 @Override
San Mehat873f2142010-01-14 10:25:07 -08001034 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001035 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001036
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001037 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001038 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001039 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001040 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001041 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001042 }
San Mehat873f2142010-01-14 10:25:07 -08001043
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001044 // 211 Forwarding enabled
1045 event.checkCode(IpFwdStatusResult);
1046 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001047 }
1048
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001049 @Override
1050 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001052 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001054 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001055 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001056 }
San Mehat873f2142010-01-14 10:25:07 -08001057 }
1058
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001059 @Override
1060 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001061 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001062 // cmd is "tether start first_start first_stop second_start second_stop ..."
1063 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001064
1065 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001066 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001067 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001068 }
Kenny Roota80ce062010-06-01 13:23:53 -07001069
1070 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001071 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001072 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001073 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001074 }
San Mehat873f2142010-01-14 10:25:07 -08001075 }
1076
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001077 @Override
1078 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001079 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001080 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001081 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001082 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001083 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001084 }
San Mehat873f2142010-01-14 10:25:07 -08001085 }
1086
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001087 @Override
1088 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001089 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001090
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001091 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001092 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001093 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001094 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001095 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001096 }
San Mehat873f2142010-01-14 10:25:07 -08001097
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001098 // 210 Tethering services started
1099 event.checkCode(TetherStatusResult);
1100 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001101 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001102
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001103 @Override
1104 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001105 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001106 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001107 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001108 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001109 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001110 }
San Mehat873f2142010-01-14 10:25:07 -08001111 }
1112
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001113 @Override
San Mehat873f2142010-01-14 10:25:07 -08001114 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001116 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001117 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001118 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001119 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001120 }
San Mehat873f2142010-01-14 10:25:07 -08001121 }
1122
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001123 @Override
1124 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001126 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001127 return NativeDaemonEvent.filterMessageList(
1128 mConnector.executeForList("tether", "interface", "list"),
1129 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001130 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001131 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001132 }
San Mehat873f2142010-01-14 10:25:07 -08001133 }
1134
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001135 @Override
1136 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001137 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001138
1139 final Command cmd = new Command("tether", "dns", "set");
1140 for (String s : dns) {
1141 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1142 }
1143
San Mehat873f2142010-01-14 10:25:07 -08001144 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001145 mConnector.execute(cmd);
1146 } catch (NativeDaemonConnectorException e) {
1147 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001148 }
1149 }
1150
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001151 @Override
1152 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001153 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001154 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001155 return NativeDaemonEvent.filterMessageList(
1156 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001157 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001158 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001159 }
San Mehat873f2142010-01-14 10:25:07 -08001160 }
1161
jiaguo1da35f72014-01-09 16:39:59 +08001162 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1163 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1164 for (InterfaceAddress ia : addresses) {
1165 if (!ia.getAddress().isLinkLocalAddress())
1166 filtered.add(ia);
1167 }
1168 return filtered;
1169 }
1170
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001171 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001172 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001173 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001174
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001175 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1176 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001177 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001178 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001179 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001180 // Don't touch link-local routes, as link-local addresses aren't routable,
1181 // kernel creates link-local routes on all interfaces automatically
1182 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1183 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001184 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001185 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001186 InetAddress addr = NetworkUtils.getNetworkPart(
1187 ia.getAddress(), ia.getNetworkPrefixLength());
1188 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001189 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001190 }
1191
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001192 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001193 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001194 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001195 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001196 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001197 }
1198
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001199 @Override
1200 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001201 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001202 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001203 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001204 } catch (SocketException e) {
1205 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001206 }
San Mehat873f2142010-01-14 10:25:07 -08001207 }
1208
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001209 @Override
1210 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001211 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001212 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001213 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001214 } catch (SocketException e) {
1215 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001216 }
San Mehat873f2142010-01-14 10:25:07 -08001217 }
San Mehat72759df2010-01-19 13:50:37 -08001218
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001219 @Override
1220 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001221 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001222 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001223 return NativeDaemonEvent.filterMessageList(
1224 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001225 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001226 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001227 }
San Mehat72759df2010-01-19 13:50:37 -08001228 }
1229
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001230 @Override
1231 public void attachPppd(
1232 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001233 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001234 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001235 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001236 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1237 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1238 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001239 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001240 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001241 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001242 }
1243 }
1244
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001245 @Override
1246 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001247 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001248 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001249 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001250 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001251 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001252 }
San Mehat72759df2010-01-19 13:50:37 -08001253 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001254
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001255 @Override
1256 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001257 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001258 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001259 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001260 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001261 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001262 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001263 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001264 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001265 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001266 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001267 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001268 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001269 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001270 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001271 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001272 }
1273
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001274 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001275 switch (wifiConfig.getAuthType()) {
1276 case KeyMgmt.WPA_PSK:
1277 return "wpa-psk";
1278 case KeyMgmt.WPA2_PSK:
1279 return "wpa2-psk";
1280 default:
1281 return "open";
1282 }
1283 }
1284
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001285 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001286 @Override
1287 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001288 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001289 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001290 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001291 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001292 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001293 }
1294 }
1295
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001296 @Override
1297 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001298 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001299 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001300 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001301 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001302 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001303 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001304 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001305 }
1306
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001307 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001308 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001309 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001310 try {
1311 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001312 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001313 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001314 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001315 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001316 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001317 }
1318 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001319 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001320 }
1321 }
San Mehat91cac642010-03-31 14:31:36 -07001322
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001323 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001324 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001325 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1326
1327 if (DBG) Slog.d(TAG, "Adding idletimer");
1328
1329 synchronized (mIdleTimerLock) {
1330 IdleTimerParams params = mActiveIdleTimers.get(iface);
1331 if (params != null) {
1332 // the interface already has idletimer, update network count
1333 params.networkCount++;
1334 return;
1335 }
1336
1337 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001338 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1339 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001340 } catch (NativeDaemonConnectorException e) {
1341 throw e.rethrowAsParcelableException();
1342 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001343 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1344
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001345 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001346 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1347 mNetworkActive = false;
1348 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001349 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001350 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001351 notifyInterfaceClassActivity(type,
1352 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1353 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001354 }
1355 });
Haoyu Bai04124232012-06-28 15:26:19 -07001356 }
1357 }
1358
1359 @Override
1360 public void removeIdleTimer(String iface) {
1361 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1362
1363 if (DBG) Slog.d(TAG, "Removing idletimer");
1364
1365 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001366 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001367 if (params == null || --(params.networkCount) > 0) {
1368 return;
1369 }
1370
1371 try {
1372 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001373 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001374 } catch (NativeDaemonConnectorException e) {
1375 throw e.rethrowAsParcelableException();
1376 }
1377 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001378 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001379 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001380 notifyInterfaceClassActivity(params.type,
1381 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1382 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001383 }
1384 });
Haoyu Bai04124232012-06-28 15:26:19 -07001385 }
1386 }
1387
1388 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001389 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001390 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001391 try {
1392 return mStatsFactory.readNetworkStatsSummaryDev();
1393 } catch (IOException e) {
1394 throw new IllegalStateException(e);
1395 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001396 }
1397
1398 @Override
1399 public NetworkStats getNetworkStatsSummaryXt() {
1400 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001401 try {
1402 return mStatsFactory.readNetworkStatsSummaryXt();
1403 } catch (IOException e) {
1404 throw new IllegalStateException(e);
1405 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001406 }
1407
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001408 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001409 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001410 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001411 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001412 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001413 } catch (IOException e) {
1414 throw new IllegalStateException(e);
1415 }
San Mehat91cac642010-03-31 14:31:36 -07001416 }
1417
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001418 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001419 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001420 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001421
Jeff Sharkey350083e2011-06-29 10:45:16 -07001422 // silently discard when control disabled
1423 // TODO: eventually migrate to be always enabled
1424 if (!mBandwidthControlEnabled) return;
1425
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001426 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001427 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001428 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001429 }
1430
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001431 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001432 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001433 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001434 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001435 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001436 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001437 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001438 }
1439 }
1440
1441 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001442 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001443 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001444
Jeff Sharkey350083e2011-06-29 10:45:16 -07001445 // silently discard when control disabled
1446 // TODO: eventually migrate to be always enabled
1447 if (!mBandwidthControlEnabled) return;
1448
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001449 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001450 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001451 // TODO: eventually consider throwing
1452 return;
1453 }
1454
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001455 mActiveQuotas.remove(iface);
1456 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001457
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001458 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001459 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001460 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001461 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001462 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001463 }
1464 }
1465 }
1466
1467 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001468 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001469 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001470
1471 // silently discard when control disabled
1472 // TODO: eventually migrate to be always enabled
1473 if (!mBandwidthControlEnabled) return;
1474
1475 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001476 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001477 throw new IllegalStateException("setting alert requires existing quota on iface");
1478 }
1479
1480 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001481 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001482 throw new IllegalStateException("iface " + iface + " already has alert");
1483 }
1484
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001485 try {
1486 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001487 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001488 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001489 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001490 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001491 }
1492 }
1493 }
1494
1495 @Override
1496 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001497 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001498
1499 // silently discard when control disabled
1500 // TODO: eventually migrate to be always enabled
1501 if (!mBandwidthControlEnabled) return;
1502
1503 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001504 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001505 // TODO: eventually consider throwing
1506 return;
1507 }
1508
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001509 try {
1510 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001511 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001512 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001513 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001514 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001515 }
1516 }
1517 }
1518
1519 @Override
1520 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001521 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001522
1523 // silently discard when control disabled
1524 // TODO: eventually migrate to be always enabled
1525 if (!mBandwidthControlEnabled) return;
1526
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001527 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001528 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001529 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001530 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001531 }
1532 }
1533
1534 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001535 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001536 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001537
Jeff Sharkey350083e2011-06-29 10:45:16 -07001538 // silently discard when control disabled
1539 // TODO: eventually migrate to be always enabled
1540 if (!mBandwidthControlEnabled) return;
1541
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001542 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001543 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1544 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1545 // TODO: eventually consider throwing
1546 return;
1547 }
1548
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001549 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001550 mConnector.execute("bandwidth",
1551 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001552 if (rejectOnQuotaInterfaces) {
1553 mUidRejectOnQuota.put(uid, true);
1554 } else {
1555 mUidRejectOnQuota.delete(uid);
1556 }
1557 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001558 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001559 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001560 }
1561 }
1562
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001563 @Override
1564 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001565 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001566 return mBandwidthControlEnabled;
1567 }
1568
1569 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001570 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001571 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001572 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001573 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001574 } catch (IOException e) {
1575 throw new IllegalStateException(e);
1576 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001577 }
1578
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001579 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001580 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001581 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001582
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001583 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001584 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001585 final NativeDaemonEvent[] events = mConnector.executeForList(
1586 "bandwidth", "gettetherstats");
1587 for (NativeDaemonEvent event : events) {
1588 if (event.getCode() != TetheringStatsListResult) continue;
1589
1590 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1591 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1592 try {
1593 final String ifaceIn = tok.nextToken();
1594 final String ifaceOut = tok.nextToken();
1595
1596 final NetworkStats.Entry entry = new NetworkStats.Entry();
1597 entry.iface = ifaceOut;
1598 entry.uid = UID_TETHERING;
1599 entry.set = SET_DEFAULT;
1600 entry.tag = TAG_NONE;
1601 entry.rxBytes = Long.parseLong(tok.nextToken());
1602 entry.rxPackets = Long.parseLong(tok.nextToken());
1603 entry.txBytes = Long.parseLong(tok.nextToken());
1604 entry.txPackets = Long.parseLong(tok.nextToken());
1605 stats.combineValues(entry);
1606 } catch (NoSuchElementException e) {
1607 throw new IllegalStateException("problem parsing tethering stats: " + event);
1608 } catch (NumberFormatException e) {
1609 throw new IllegalStateException("problem parsing tethering stats: " + event);
1610 }
1611 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001612 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001613 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001614 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001615 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001616 }
1617
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001618 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001619 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001620 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001621
Paul Jensen13e817d2014-04-10 14:16:37 -04001622 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001623 (domains == null ? "" : domains));
1624
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001625 for (String s : servers) {
1626 InetAddress a = NetworkUtils.numericToInetAddress(s);
1627 if (a.isAnyLocalAddress() == false) {
1628 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001629 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001630 }
1631
1632 try {
1633 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001634 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001635 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001636 }
1637 }
1638
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001639 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001640 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001641 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1642 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001643 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001644 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001645 } catch (NativeDaemonConnectorException e) {
1646 throw e.rethrowAsParcelableException();
1647 }
1648 }
1649
1650 @Override
1651 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1652 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1653 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001654 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001655 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001656 } catch (NativeDaemonConnectorException e) {
1657 throw e.rethrowAsParcelableException();
1658 }
1659 }
1660
1661 @Override
1662 public void setMarkedForwarding(String iface) {
1663 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1664 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001665 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001666 } catch (NativeDaemonConnectorException e) {
1667 throw e.rethrowAsParcelableException();
1668 }
1669 }
1670
1671 @Override
1672 public void clearMarkedForwarding(String iface) {
1673 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1674 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001675 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1676 } catch (NativeDaemonConnectorException e) {
1677 throw e.rethrowAsParcelableException();
1678 }
1679 }
1680
1681 @Override
1682 public int getMarkForUid(int uid) {
1683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1684 final NativeDaemonEvent event;
1685 try {
1686 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1687 } catch (NativeDaemonConnectorException e) {
1688 throw e.rethrowAsParcelableException();
1689 }
1690 event.checkCode(GetMarkResult);
1691 return Integer.parseInt(event.getMessage());
1692 }
1693
1694 @Override
1695 public int getMarkForProtect() {
1696 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1697 final NativeDaemonEvent event;
1698 try {
1699 event = mConnector.execute("interface", "fwmark", "get", "protect");
1700 } catch (NativeDaemonConnectorException e) {
1701 throw e.rethrowAsParcelableException();
1702 }
1703 event.checkCode(GetMarkResult);
1704 return Integer.parseInt(event.getMessage());
1705 }
1706
1707 @Override
1708 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1709 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1710 try {
1711 LinkAddress dest = route.getDestination();
1712 mConnector.execute("interface", "fwmark", "route", "add", iface,
1713 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1714 } catch (NativeDaemonConnectorException e) {
1715 throw e.rethrowAsParcelableException();
1716 }
1717 }
1718
1719 @Override
1720 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1721 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1722 try {
1723 LinkAddress dest = route.getDestination();
1724 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1725 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001726 } catch (NativeDaemonConnectorException e) {
1727 throw e.rethrowAsParcelableException();
1728 }
1729 }
1730
1731 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001732 public void setHostExemption(LinkAddress host) {
1733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1734 try {
1735 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1736 } catch (NativeDaemonConnectorException e) {
1737 throw e.rethrowAsParcelableException();
1738 }
1739 }
1740
1741 @Override
1742 public void clearHostExemption(LinkAddress host) {
1743 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1744 try {
1745 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1746 } catch (NativeDaemonConnectorException e) {
1747 throw e.rethrowAsParcelableException();
1748 }
1749 }
1750
1751 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001752 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001753 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1754 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001755 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001756 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001757 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001758 }
1759 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001760
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001761 @Override
1762 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001763 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001764 try {
1765 mConnector.execute("firewall", enabled ? "enable" : "disable");
1766 mFirewallEnabled = enabled;
1767 } catch (NativeDaemonConnectorException e) {
1768 throw e.rethrowAsParcelableException();
1769 }
1770 }
1771
1772 @Override
1773 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001774 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001775 return mFirewallEnabled;
1776 }
1777
1778 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001779 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001780 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001781 Preconditions.checkState(mFirewallEnabled);
1782 final String rule = allow ? ALLOW : DENY;
1783 try {
1784 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1785 } catch (NativeDaemonConnectorException e) {
1786 throw e.rethrowAsParcelableException();
1787 }
1788 }
1789
1790 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001791 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001792 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001793 Preconditions.checkState(mFirewallEnabled);
1794 final String rule = allow ? ALLOW : DENY;
1795 try {
1796 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1797 } catch (NativeDaemonConnectorException e) {
1798 throw e.rethrowAsParcelableException();
1799 }
1800 }
1801
1802 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001803 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001804 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001805 Preconditions.checkState(mFirewallEnabled);
1806 final String rule = allow ? ALLOW : DENY;
1807 try {
1808 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1809 } catch (NativeDaemonConnectorException e) {
1810 throw e.rethrowAsParcelableException();
1811 }
1812 }
1813
1814 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001815 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001816 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001817 Preconditions.checkState(mFirewallEnabled);
1818 final String rule = allow ? ALLOW : DENY;
1819 try {
1820 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1821 } catch (NativeDaemonConnectorException e) {
1822 throw e.rethrowAsParcelableException();
1823 }
1824 }
1825
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001826 private static void enforceSystemUid() {
1827 final int uid = Binder.getCallingUid();
1828 if (uid != Process.SYSTEM_UID) {
1829 throw new SecurityException("Only available to AID_SYSTEM");
1830 }
1831 }
1832
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001833 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001834 public void startClatd(String interfaceName) throws IllegalStateException {
1835 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1836
1837 try {
1838 mConnector.execute("clatd", "start", interfaceName);
1839 } catch (NativeDaemonConnectorException e) {
1840 throw e.rethrowAsParcelableException();
1841 }
1842 }
1843
1844 @Override
1845 public void stopClatd() throws IllegalStateException {
1846 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1847
1848 try {
1849 mConnector.execute("clatd", "stop");
1850 } catch (NativeDaemonConnectorException e) {
1851 throw e.rethrowAsParcelableException();
1852 }
1853 }
1854
1855 @Override
1856 public boolean isClatdStarted() {
1857 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1858
1859 final NativeDaemonEvent event;
1860 try {
1861 event = mConnector.execute("clatd", "status");
1862 } catch (NativeDaemonConnectorException e) {
1863 throw e.rethrowAsParcelableException();
1864 }
1865
1866 event.checkCode(ClatdStatusResult);
1867 return event.getMessage().endsWith("started");
1868 }
1869
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001870 @Override
1871 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1872 mNetworkActivityListeners.register(listener);
1873 }
1874
1875 @Override
1876 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1877 mNetworkActivityListeners.unregister(listener);
1878 }
1879
1880 @Override
1881 public boolean isNetworkActive() {
1882 synchronized (mNetworkActivityListeners) {
1883 return mNetworkActive || mActiveIdleTimers.isEmpty();
1884 }
1885 }
1886
1887 private void reportNetworkActive() {
1888 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001889 try {
1890 for (int i = 0; i < length; i++) {
1891 try {
1892 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1893 } catch (RemoteException e) {
1894 } catch (RuntimeException e) {
1895 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001896 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001897 } finally {
1898 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001899 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001900 }
1901
Mattias Falk8b47b362011-08-23 14:15:13 +02001902 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001903 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001904 public void monitor() {
1905 if (mConnector != null) {
1906 mConnector.monitor();
1907 }
1908 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001909
1910 @Override
1911 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1912 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1913
Robert Greenwalt470fd722012-01-18 12:51:15 -08001914 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1915 mConnector.dump(fd, pw, args);
1916 pw.println();
1917
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001918 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001919 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1920 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1921 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001922
1923 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001924 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1925 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001926 }
1927
1928 synchronized (mUidRejectOnQuota) {
1929 pw.print("UID reject on quota ifaces: [");
1930 final int size = mUidRejectOnQuota.size();
1931 for (int i = 0; i < size; i++) {
1932 pw.print(mUidRejectOnQuota.keyAt(i));
1933 if (i < size - 1) pw.print(",");
1934 }
1935 pw.println("]");
1936 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001937
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001938 synchronized (mIdleTimerLock) {
1939 pw.println("Idle timers:");
1940 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1941 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1942 IdleTimerParams params = ent.getValue();
1943 pw.print(" timeout="); pw.print(params.timeout);
1944 pw.print(" type="); pw.print(params.type);
1945 pw.print(" networkCount="); pw.println(params.networkCount);
1946 }
1947 }
1948
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001949 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001950 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001951
Robert Greenwalt568891d2014-04-04 13:38:00 -07001952 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001953 public void createNetwork(int netId, String iface) {
1954 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1955
1956 try {
1957 mConnector.execute("network", "create", netId, iface);
1958 } catch (NativeDaemonConnectorException e) {
1959 throw e.rethrowAsParcelableException();
1960 }
1961 }
1962
Robert Greenwalt568891d2014-04-04 13:38:00 -07001963 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001964 public void removeNetwork(int netId) {
1965 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1966
1967 try {
1968 mConnector.execute("network", "destroy", netId);
1969 } catch (NativeDaemonConnectorException e) {
1970 throw e.rethrowAsParcelableException();
1971 }
1972 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001973
1974 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001975 public void addRouteForNetId(int netId, RouteInfo routeInfo) {
1976 modifyRouteForNetId(netId, routeInfo, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001977 }
1978
1979 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001980 public void removeRouteForNetId(int netId, RouteInfo routeInfo) {
1981 modifyRouteForNetId(netId, routeInfo, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001982 }
1983
Robert Greenwalt913c8952014-04-07 17:36:35 -07001984 private void modifyRouteForNetId(int netId, RouteInfo routeInfo, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001985 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1986
Robert Greenwalt913c8952014-04-07 17:36:35 -07001987 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001988
1989 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
1990 final LinkAddress la = routeInfo.getDestination();
1991 cmd.appendArg(la.getAddress().getHostAddress());
1992 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -07001993 cmd.appendArg(routeInfo.getGateway().getHostAddress());
Robert Greenwalt568891d2014-04-04 13:38:00 -07001994 cmd.appendArg(routeInfo.getInterface());
1995
1996 try {
1997 mConnector.execute(cmd);
1998 } catch (NativeDaemonConnectorException e) {
1999 throw e.rethrowAsParcelableException();
2000 }
2001 }
2002
2003 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002004 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2005 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002006 }
2007
2008 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002009 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2010 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002011 }
2012
Robert Greenwalt913c8952014-04-07 17:36:35 -07002013 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002014 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2015
Robert Greenwalt913c8952014-04-07 17:36:35 -07002016 final Command cmd = new Command("network", "legacy", uid, "route", action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002017
2018 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
2019 final LinkAddress la = routeInfo.getDestination();
2020 cmd.appendArg(la.getAddress().getHostAddress());
2021 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -07002022 cmd.appendArg(routeInfo.getGateway().getHostAddress());
Robert Greenwalt568891d2014-04-04 13:38:00 -07002023 cmd.appendArg(routeInfo.getInterface());
2024
2025 try {
2026 mConnector.execute(cmd);
2027 } catch (NativeDaemonConnectorException e) {
2028 throw e.rethrowAsParcelableException();
2029 }
2030 }
2031
2032 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002033 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002034 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2035
2036 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002037 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002038 } catch (NativeDaemonConnectorException e) {
2039 throw e.rethrowAsParcelableException();
2040 }
2041 }
2042
2043 @Override
2044 public void clearDefaultNetId() {
2045 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2046
2047 try {
2048 mConnector.execute("network", "default", "clear");
2049 } catch (NativeDaemonConnectorException e) {
2050 throw e.rethrowAsParcelableException();
2051 }
2052 }
2053
2054 @Override
2055 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2056 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2057
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002058 final Command cmd = new Command("network", "permission", "user", "set");
2059 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2060 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002061 for (int i=0; i<uids.length; i++) {
2062 cmd.appendArg(uids[i]);
2063 }
2064
2065 try {
2066 mConnector.execute(cmd);
2067 } catch (NativeDaemonConnectorException e) {
2068 throw e.rethrowAsParcelableException();
2069 }
2070 }
2071
2072 @Override
2073 public void clearPermission(int[] uids) {
2074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2075
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002076 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002077 for (int i=0; i<uids.length; i++) {
2078 cmd.appendArg(uids[i]);
2079 }
2080
2081 try {
2082 mConnector.execute(cmd);
2083 } catch (NativeDaemonConnectorException e) {
2084 throw e.rethrowAsParcelableException();
2085 }
2086 }
San Mehat873f2142010-01-14 10:25:07 -08002087}