blob: a0da2c068bcffb465353fb14671267256637778b [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
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700871 public void addRoute(int netId, RouteInfo route) {
872 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700873 }
874
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800875 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700876 public void removeRoute(int netId, RouteInfo route) {
877 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700878 }
879
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700880 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800881 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700882
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700883 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700884
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700885 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800886 final LinkAddress la = route.getDestination();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700887 cmd.appendArg(route.getInterface());
888 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -0700889 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700890
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800891 try {
892 mConnector.execute(cmd);
893 } catch (NativeDaemonConnectorException e) {
894 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700895 }
896 }
897
898 private ArrayList<String> readRouteList(String filename) {
899 FileInputStream fstream = null;
900 ArrayList<String> list = new ArrayList<String>();
901
902 try {
903 fstream = new FileInputStream(filename);
904 DataInputStream in = new DataInputStream(fstream);
905 BufferedReader br = new BufferedReader(new InputStreamReader(in));
906 String s;
907
908 // throw away the title line
909
910 while (((s = br.readLine()) != null) && (s.length() != 0)) {
911 list.add(s);
912 }
913 } catch (IOException ex) {
914 // return current list, possibly empty
915 } finally {
916 if (fstream != null) {
917 try {
918 fstream.close();
919 } catch (IOException ex) {}
920 }
921 }
922
923 return list;
924 }
925
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800926 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700927 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800928 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700929 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
930
931 // v4 routes listed as:
932 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
933 for (String s : readRouteList("/proc/net/route")) {
934 String[] fields = s.split("\t");
935
936 if (fields.length > 7) {
937 String iface = fields[0];
938
939 if (interfaceName.equals(iface)) {
940 String dest = fields[1];
941 String gate = fields[2];
942 String flags = fields[3]; // future use?
943 String mask = fields[7];
944 try {
945 // address stored as a hex string, ex: 0014A8C0
946 InetAddress destAddr =
947 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
948 int prefixLength =
949 NetworkUtils.netmaskIntToPrefixLength(
950 (int)Long.parseLong(mask, 16));
951 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
952
953 // address stored as a hex string, ex 0014A8C0
954 InetAddress gatewayAddr =
955 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
956
Wink Saville7b5fd052013-03-15 05:07:04 +0000957 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700958 routes.add(route);
959 } catch (Exception e) {
960 Log.e(TAG, "Error parsing route " + s + " : " + e);
961 continue;
962 }
963 }
964 }
965 }
966
967 // v6 routes listed as:
968 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
969 for (String s : readRouteList("/proc/net/ipv6_route")) {
970 String[]fields = s.split("\\s+");
971 if (fields.length > 9) {
972 String iface = fields[9].trim();
973 if (interfaceName.equals(iface)) {
974 String dest = fields[0];
975 String prefix = fields[1];
976 String gate = fields[4];
977
978 try {
979 // prefix length stored as a hex string, ex 40
980 int prefixLength = Integer.parseInt(prefix, 16);
981
982 // address stored as a 32 char hex string
983 // ex fe800000000000000000000000000000
984 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
985 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
986
987 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
988
Wink Saville7b5fd052013-03-15 05:07:04 +0000989 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700990 routes.add(route);
991 } catch (Exception e) {
992 Log.e(TAG, "Error parsing route " + s + " : " + e);
993 continue;
994 }
995 }
996 }
997 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800998 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700999 }
1000
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001001 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001002 public void setMtu(String iface, int mtu) {
1003 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1004
1005 final NativeDaemonEvent event;
1006 try {
1007 event = mConnector.execute("interface", "setmtu", iface, mtu);
1008 } catch (NativeDaemonConnectorException e) {
1009 throw e.rethrowAsParcelableException();
1010 }
1011 }
1012
1013 @Override
San Mehat873f2142010-01-14 10:25:07 -08001014 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001015 // TODO: remove from aidl if nobody calls externally
1016 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001017
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001019 }
1020
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001021 @Override
San Mehat873f2142010-01-14 10:25:07 -08001022 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001023 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001024
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001025 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001026 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001028 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001029 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001030 }
San Mehat873f2142010-01-14 10:25:07 -08001031
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001032 // 211 Forwarding enabled
1033 event.checkCode(IpFwdStatusResult);
1034 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001035 }
1036
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001037 @Override
1038 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001039 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001040 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001041 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001042 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001043 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001044 }
San Mehat873f2142010-01-14 10:25:07 -08001045 }
1046
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001047 @Override
1048 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001049 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001050 // cmd is "tether start first_start first_stop second_start second_stop ..."
1051 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001052
1053 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001054 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001055 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001056 }
Kenny Roota80ce062010-06-01 13:23:53 -07001057
1058 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001059 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001060 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001061 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001062 }
San Mehat873f2142010-01-14 10:25:07 -08001063 }
1064
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001065 @Override
1066 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001067 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001068 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001069 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001070 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001071 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001072 }
San Mehat873f2142010-01-14 10:25:07 -08001073 }
1074
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001075 @Override
1076 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001077 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001078
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001079 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001080 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001081 event = mConnector.execute("tether", "status");
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
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001086 // 210 Tethering services started
1087 event.checkCode(TetherStatusResult);
1088 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001089 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001090
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001091 @Override
1092 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001093 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001094 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001095 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001096 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001097 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001098 }
San Mehat873f2142010-01-14 10:25:07 -08001099 }
1100
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001101 @Override
San Mehat873f2142010-01-14 10:25:07 -08001102 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001103 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001104 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001105 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001106 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001107 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001108 }
San Mehat873f2142010-01-14 10:25:07 -08001109 }
1110
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001111 @Override
1112 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001113 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001114 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001115 return NativeDaemonEvent.filterMessageList(
1116 mConnector.executeForList("tether", "interface", "list"),
1117 TetherInterfaceListResult);
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 void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001126
1127 final Command cmd = new Command("tether", "dns", "set");
1128 for (String s : dns) {
1129 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1130 }
1131
San Mehat873f2142010-01-14 10:25:07 -08001132 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001133 mConnector.execute(cmd);
1134 } catch (NativeDaemonConnectorException e) {
1135 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001136 }
1137 }
1138
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001139 @Override
1140 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001142 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 return NativeDaemonEvent.filterMessageList(
1144 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001145 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001146 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001147 }
San Mehat873f2142010-01-14 10:25:07 -08001148 }
1149
jiaguo1da35f72014-01-09 16:39:59 +08001150 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1151 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1152 for (InterfaceAddress ia : addresses) {
1153 if (!ia.getAddress().isLinkLocalAddress())
1154 filtered.add(ia);
1155 }
1156 return filtered;
1157 }
1158
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001159 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001160 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001161 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001162
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001163 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1164 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001165 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001166 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001167 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001168 // Don't touch link-local routes, as link-local addresses aren't routable,
1169 // kernel creates link-local routes on all interfaces automatically
1170 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1171 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001172 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001173 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001174 InetAddress addr = NetworkUtils.getNetworkPart(
1175 ia.getAddress(), ia.getNetworkPrefixLength());
1176 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001177 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001178 }
1179
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001180 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001182 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001183 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001184 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001185 }
1186
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001187 @Override
1188 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001189 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001190 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001191 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001192 } catch (SocketException e) {
1193 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001194 }
San Mehat873f2142010-01-14 10:25:07 -08001195 }
1196
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001197 @Override
1198 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001199 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001200 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001201 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001202 } catch (SocketException e) {
1203 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001204 }
San Mehat873f2142010-01-14 10:25:07 -08001205 }
San Mehat72759df2010-01-19 13:50:37 -08001206
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001207 @Override
1208 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001209 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001210 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001211 return NativeDaemonEvent.filterMessageList(
1212 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001213 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001214 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001215 }
San Mehat72759df2010-01-19 13:50:37 -08001216 }
1217
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001218 @Override
1219 public void attachPppd(
1220 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001221 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001222 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001223 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001224 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1225 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1226 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001227 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001228 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001229 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001230 }
1231 }
1232
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001233 @Override
1234 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001235 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001236 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001237 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001238 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001239 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001240 }
San Mehat72759df2010-01-19 13:50:37 -08001241 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001242
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001243 @Override
1244 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001245 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001246 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001247 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001248 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001249 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001250 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001251 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001252 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001253 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001254 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001255 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001256 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001257 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001258 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001259 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001260 }
1261
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001262 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001263 switch (wifiConfig.getAuthType()) {
1264 case KeyMgmt.WPA_PSK:
1265 return "wpa-psk";
1266 case KeyMgmt.WPA2_PSK:
1267 return "wpa2-psk";
1268 default:
1269 return "open";
1270 }
1271 }
1272
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001273 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001274 @Override
1275 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001276 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001277 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001278 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001279 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001280 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001281 }
1282 }
1283
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001284 @Override
1285 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001287 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001288 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001289 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001290 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001291 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001292 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001293 }
1294
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001295 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001296 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001297 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001298 try {
1299 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001300 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001301 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001302 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001303 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001304 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001305 }
1306 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001307 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001308 }
1309 }
San Mehat91cac642010-03-31 14:31:36 -07001310
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001311 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001312 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001313 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1314
1315 if (DBG) Slog.d(TAG, "Adding idletimer");
1316
1317 synchronized (mIdleTimerLock) {
1318 IdleTimerParams params = mActiveIdleTimers.get(iface);
1319 if (params != null) {
1320 // the interface already has idletimer, update network count
1321 params.networkCount++;
1322 return;
1323 }
1324
1325 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001326 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1327 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001328 } catch (NativeDaemonConnectorException e) {
1329 throw e.rethrowAsParcelableException();
1330 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001331 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1332
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001333 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001334 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1335 mNetworkActive = false;
1336 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001337 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001338 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001339 notifyInterfaceClassActivity(type,
1340 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1341 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001342 }
1343 });
Haoyu Bai04124232012-06-28 15:26:19 -07001344 }
1345 }
1346
1347 @Override
1348 public void removeIdleTimer(String iface) {
1349 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1350
1351 if (DBG) Slog.d(TAG, "Removing idletimer");
1352
1353 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001354 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001355 if (params == null || --(params.networkCount) > 0) {
1356 return;
1357 }
1358
1359 try {
1360 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001361 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001362 } catch (NativeDaemonConnectorException e) {
1363 throw e.rethrowAsParcelableException();
1364 }
1365 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001366 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001367 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001368 notifyInterfaceClassActivity(params.type,
1369 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1370 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001371 }
1372 });
Haoyu Bai04124232012-06-28 15:26:19 -07001373 }
1374 }
1375
1376 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001377 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001378 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001379 try {
1380 return mStatsFactory.readNetworkStatsSummaryDev();
1381 } catch (IOException e) {
1382 throw new IllegalStateException(e);
1383 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001384 }
1385
1386 @Override
1387 public NetworkStats getNetworkStatsSummaryXt() {
1388 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001389 try {
1390 return mStatsFactory.readNetworkStatsSummaryXt();
1391 } catch (IOException e) {
1392 throw new IllegalStateException(e);
1393 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001394 }
1395
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001396 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001397 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001398 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001399 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001400 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001401 } catch (IOException e) {
1402 throw new IllegalStateException(e);
1403 }
San Mehat91cac642010-03-31 14:31:36 -07001404 }
1405
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001406 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001407 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001408 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001409
Jeff Sharkey350083e2011-06-29 10:45:16 -07001410 // silently discard when control disabled
1411 // TODO: eventually migrate to be always enabled
1412 if (!mBandwidthControlEnabled) return;
1413
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001414 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001415 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001416 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001417 }
1418
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001419 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001420 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001421 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001422 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001423 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001424 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001425 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001426 }
1427 }
1428
1429 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001430 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001431 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001432
Jeff Sharkey350083e2011-06-29 10:45:16 -07001433 // silently discard when control disabled
1434 // TODO: eventually migrate to be always enabled
1435 if (!mBandwidthControlEnabled) return;
1436
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001437 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001438 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001439 // TODO: eventually consider throwing
1440 return;
1441 }
1442
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001443 mActiveQuotas.remove(iface);
1444 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001445
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001446 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001447 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001448 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001449 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001450 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001451 }
1452 }
1453 }
1454
1455 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001456 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001457 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001458
1459 // silently discard when control disabled
1460 // TODO: eventually migrate to be always enabled
1461 if (!mBandwidthControlEnabled) return;
1462
1463 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001464 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001465 throw new IllegalStateException("setting alert requires existing quota on iface");
1466 }
1467
1468 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001469 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001470 throw new IllegalStateException("iface " + iface + " already has alert");
1471 }
1472
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001473 try {
1474 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001475 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001476 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001477 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001478 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001479 }
1480 }
1481 }
1482
1483 @Override
1484 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001485 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001486
1487 // silently discard when control disabled
1488 // TODO: eventually migrate to be always enabled
1489 if (!mBandwidthControlEnabled) return;
1490
1491 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001492 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001493 // TODO: eventually consider throwing
1494 return;
1495 }
1496
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497 try {
1498 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001499 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001500 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001501 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001502 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001503 }
1504 }
1505 }
1506
1507 @Override
1508 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001509 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001510
1511 // silently discard when control disabled
1512 // TODO: eventually migrate to be always enabled
1513 if (!mBandwidthControlEnabled) return;
1514
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001515 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001516 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001517 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001518 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001519 }
1520 }
1521
1522 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001523 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001524 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001525
Jeff Sharkey350083e2011-06-29 10:45:16 -07001526 // silently discard when control disabled
1527 // TODO: eventually migrate to be always enabled
1528 if (!mBandwidthControlEnabled) return;
1529
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001530 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001531 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1532 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1533 // TODO: eventually consider throwing
1534 return;
1535 }
1536
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001537 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001538 mConnector.execute("bandwidth",
1539 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001540 if (rejectOnQuotaInterfaces) {
1541 mUidRejectOnQuota.put(uid, true);
1542 } else {
1543 mUidRejectOnQuota.delete(uid);
1544 }
1545 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001546 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001547 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001548 }
1549 }
1550
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001551 @Override
1552 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001553 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001554 return mBandwidthControlEnabled;
1555 }
1556
1557 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001558 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001559 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001560 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001561 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001562 } catch (IOException e) {
1563 throw new IllegalStateException(e);
1564 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001565 }
1566
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001567 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001568 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001569 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001570
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001571 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001572 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001573 final NativeDaemonEvent[] events = mConnector.executeForList(
1574 "bandwidth", "gettetherstats");
1575 for (NativeDaemonEvent event : events) {
1576 if (event.getCode() != TetheringStatsListResult) continue;
1577
1578 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1579 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1580 try {
1581 final String ifaceIn = tok.nextToken();
1582 final String ifaceOut = tok.nextToken();
1583
1584 final NetworkStats.Entry entry = new NetworkStats.Entry();
1585 entry.iface = ifaceOut;
1586 entry.uid = UID_TETHERING;
1587 entry.set = SET_DEFAULT;
1588 entry.tag = TAG_NONE;
1589 entry.rxBytes = Long.parseLong(tok.nextToken());
1590 entry.rxPackets = Long.parseLong(tok.nextToken());
1591 entry.txBytes = Long.parseLong(tok.nextToken());
1592 entry.txPackets = Long.parseLong(tok.nextToken());
1593 stats.combineValues(entry);
1594 } catch (NoSuchElementException e) {
1595 throw new IllegalStateException("problem parsing tethering stats: " + event);
1596 } catch (NumberFormatException e) {
1597 throw new IllegalStateException("problem parsing tethering stats: " + event);
1598 }
1599 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001600 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001601 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001602 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001603 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001604 }
1605
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001606 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001607 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001608 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001609
Paul Jensen13e817d2014-04-10 14:16:37 -04001610 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001611 (domains == null ? "" : domains));
1612
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001613 for (String s : servers) {
1614 InetAddress a = NetworkUtils.numericToInetAddress(s);
1615 if (a.isAnyLocalAddress() == false) {
1616 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001617 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001618 }
1619
1620 try {
1621 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001622 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001623 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001624 }
1625 }
1626
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001627 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001628 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001629 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1630 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001631 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001632 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001633 } catch (NativeDaemonConnectorException e) {
1634 throw e.rethrowAsParcelableException();
1635 }
1636 }
1637
1638 @Override
1639 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1640 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1641 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001642 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001643 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001644 } catch (NativeDaemonConnectorException e) {
1645 throw e.rethrowAsParcelableException();
1646 }
1647 }
1648
1649 @Override
1650 public void setMarkedForwarding(String iface) {
1651 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1652 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001653 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001654 } catch (NativeDaemonConnectorException e) {
1655 throw e.rethrowAsParcelableException();
1656 }
1657 }
1658
1659 @Override
1660 public void clearMarkedForwarding(String iface) {
1661 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1662 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001663 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1664 } catch (NativeDaemonConnectorException e) {
1665 throw e.rethrowAsParcelableException();
1666 }
1667 }
1668
1669 @Override
1670 public int getMarkForUid(int uid) {
1671 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1672 final NativeDaemonEvent event;
1673 try {
1674 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1675 } catch (NativeDaemonConnectorException e) {
1676 throw e.rethrowAsParcelableException();
1677 }
1678 event.checkCode(GetMarkResult);
1679 return Integer.parseInt(event.getMessage());
1680 }
1681
1682 @Override
1683 public int getMarkForProtect() {
1684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1685 final NativeDaemonEvent event;
1686 try {
1687 event = mConnector.execute("interface", "fwmark", "get", "protect");
1688 } catch (NativeDaemonConnectorException e) {
1689 throw e.rethrowAsParcelableException();
1690 }
1691 event.checkCode(GetMarkResult);
1692 return Integer.parseInt(event.getMessage());
1693 }
1694
1695 @Override
1696 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1697 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1698 try {
1699 LinkAddress dest = route.getDestination();
1700 mConnector.execute("interface", "fwmark", "route", "add", iface,
1701 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1702 } catch (NativeDaemonConnectorException e) {
1703 throw e.rethrowAsParcelableException();
1704 }
1705 }
1706
1707 @Override
1708 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1709 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1710 try {
1711 LinkAddress dest = route.getDestination();
1712 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1713 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001714 } catch (NativeDaemonConnectorException e) {
1715 throw e.rethrowAsParcelableException();
1716 }
1717 }
1718
1719 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001720 public void setHostExemption(LinkAddress host) {
1721 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1722 try {
1723 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1724 } catch (NativeDaemonConnectorException e) {
1725 throw e.rethrowAsParcelableException();
1726 }
1727 }
1728
1729 @Override
1730 public void clearHostExemption(LinkAddress host) {
1731 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1732 try {
1733 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1734 } catch (NativeDaemonConnectorException e) {
1735 throw e.rethrowAsParcelableException();
1736 }
1737 }
1738
1739 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001740 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001741 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1742 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001743 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001744 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001745 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001746 }
1747 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001748
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001749 @Override
1750 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001751 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001752 try {
1753 mConnector.execute("firewall", enabled ? "enable" : "disable");
1754 mFirewallEnabled = enabled;
1755 } catch (NativeDaemonConnectorException e) {
1756 throw e.rethrowAsParcelableException();
1757 }
1758 }
1759
1760 @Override
1761 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001762 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001763 return mFirewallEnabled;
1764 }
1765
1766 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001767 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001768 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001769 Preconditions.checkState(mFirewallEnabled);
1770 final String rule = allow ? ALLOW : DENY;
1771 try {
1772 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1773 } catch (NativeDaemonConnectorException e) {
1774 throw e.rethrowAsParcelableException();
1775 }
1776 }
1777
1778 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001779 public void setFirewallEgressSourceRule(String addr, 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_egress_source_rule", addr, rule);
1785 } catch (NativeDaemonConnectorException e) {
1786 throw e.rethrowAsParcelableException();
1787 }
1788 }
1789
1790 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001791 public void setFirewallEgressDestRule(String addr, int port, 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_dest_rule", addr, port, rule);
1797 } catch (NativeDaemonConnectorException e) {
1798 throw e.rethrowAsParcelableException();
1799 }
1800 }
1801
1802 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001803 public void setFirewallUidRule(int uid, 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_uid_rule", uid, rule);
1809 } catch (NativeDaemonConnectorException e) {
1810 throw e.rethrowAsParcelableException();
1811 }
1812 }
1813
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001814 private static void enforceSystemUid() {
1815 final int uid = Binder.getCallingUid();
1816 if (uid != Process.SYSTEM_UID) {
1817 throw new SecurityException("Only available to AID_SYSTEM");
1818 }
1819 }
1820
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001821 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001822 public void startClatd(String interfaceName) throws IllegalStateException {
1823 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1824
1825 try {
1826 mConnector.execute("clatd", "start", interfaceName);
1827 } catch (NativeDaemonConnectorException e) {
1828 throw e.rethrowAsParcelableException();
1829 }
1830 }
1831
1832 @Override
1833 public void stopClatd() throws IllegalStateException {
1834 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1835
1836 try {
1837 mConnector.execute("clatd", "stop");
1838 } catch (NativeDaemonConnectorException e) {
1839 throw e.rethrowAsParcelableException();
1840 }
1841 }
1842
1843 @Override
1844 public boolean isClatdStarted() {
1845 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1846
1847 final NativeDaemonEvent event;
1848 try {
1849 event = mConnector.execute("clatd", "status");
1850 } catch (NativeDaemonConnectorException e) {
1851 throw e.rethrowAsParcelableException();
1852 }
1853
1854 event.checkCode(ClatdStatusResult);
1855 return event.getMessage().endsWith("started");
1856 }
1857
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001858 @Override
1859 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1860 mNetworkActivityListeners.register(listener);
1861 }
1862
1863 @Override
1864 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1865 mNetworkActivityListeners.unregister(listener);
1866 }
1867
1868 @Override
1869 public boolean isNetworkActive() {
1870 synchronized (mNetworkActivityListeners) {
1871 return mNetworkActive || mActiveIdleTimers.isEmpty();
1872 }
1873 }
1874
1875 private void reportNetworkActive() {
1876 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001877 try {
1878 for (int i = 0; i < length; i++) {
1879 try {
1880 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1881 } catch (RemoteException e) {
1882 } catch (RuntimeException e) {
1883 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001884 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001885 } finally {
1886 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001887 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001888 }
1889
Mattias Falk8b47b362011-08-23 14:15:13 +02001890 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001891 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001892 public void monitor() {
1893 if (mConnector != null) {
1894 mConnector.monitor();
1895 }
1896 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001897
1898 @Override
1899 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1900 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1901
Robert Greenwalt470fd722012-01-18 12:51:15 -08001902 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1903 mConnector.dump(fd, pw, args);
1904 pw.println();
1905
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001906 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001907 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1908 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1909 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001910
1911 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001912 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1913 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001914 }
1915
1916 synchronized (mUidRejectOnQuota) {
1917 pw.print("UID reject on quota ifaces: [");
1918 final int size = mUidRejectOnQuota.size();
1919 for (int i = 0; i < size; i++) {
1920 pw.print(mUidRejectOnQuota.keyAt(i));
1921 if (i < size - 1) pw.print(",");
1922 }
1923 pw.println("]");
1924 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001925
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001926 synchronized (mIdleTimerLock) {
1927 pw.println("Idle timers:");
1928 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1929 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1930 IdleTimerParams params = ent.getValue();
1931 pw.print(" timeout="); pw.print(params.timeout);
1932 pw.print(" type="); pw.print(params.type);
1933 pw.print(" networkCount="); pw.println(params.networkCount);
1934 }
1935 }
1936
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001937 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001938 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001939
Robert Greenwalt568891d2014-04-04 13:38:00 -07001940 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001941 public void createNetwork(int netId, String iface) {
1942 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1943
1944 try {
1945 mConnector.execute("network", "create", netId, iface);
1946 } catch (NativeDaemonConnectorException e) {
1947 throw e.rethrowAsParcelableException();
1948 }
1949 }
1950
Robert Greenwalt568891d2014-04-04 13:38:00 -07001951 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001952 public void removeNetwork(int netId) {
1953 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1954
1955 try {
1956 mConnector.execute("network", "destroy", netId);
1957 } catch (NativeDaemonConnectorException e) {
1958 throw e.rethrowAsParcelableException();
1959 }
1960 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001961
1962 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001963 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1964 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001965 }
1966
1967 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001968 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1969 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001970 }
1971
Robert Greenwalt913c8952014-04-07 17:36:35 -07001972 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001973 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1974
Robert Greenwalt913c8952014-04-07 17:36:35 -07001975 final Command cmd = new Command("network", "legacy", uid, "route", action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001976
1977 // create quadlet: dest-ip-addr prefixlength gateway-ip-addr iface
1978 final LinkAddress la = routeInfo.getDestination();
1979 cmd.appendArg(la.getAddress().getHostAddress());
1980 cmd.appendArg(la.getNetworkPrefixLength());
Robert Greenwalt913c8952014-04-07 17:36:35 -07001981 cmd.appendArg(routeInfo.getGateway().getHostAddress());
Robert Greenwalt568891d2014-04-04 13:38:00 -07001982 cmd.appendArg(routeInfo.getInterface());
1983
1984 try {
1985 mConnector.execute(cmd);
1986 } catch (NativeDaemonConnectorException e) {
1987 throw e.rethrowAsParcelableException();
1988 }
1989 }
1990
1991 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07001992 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001993 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1994
1995 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07001996 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001997 } catch (NativeDaemonConnectorException e) {
1998 throw e.rethrowAsParcelableException();
1999 }
2000 }
2001
2002 @Override
2003 public void clearDefaultNetId() {
2004 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2005
2006 try {
2007 mConnector.execute("network", "default", "clear");
2008 } catch (NativeDaemonConnectorException e) {
2009 throw e.rethrowAsParcelableException();
2010 }
2011 }
2012
2013 @Override
2014 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2015 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2016
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002017 final Command cmd = new Command("network", "permission", "user", "set");
2018 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2019 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002020 for (int i=0; i<uids.length; i++) {
2021 cmd.appendArg(uids[i]);
2022 }
2023
2024 try {
2025 mConnector.execute(cmd);
2026 } catch (NativeDaemonConnectorException e) {
2027 throw e.rethrowAsParcelableException();
2028 }
2029 }
2030
2031 @Override
2032 public void clearPermission(int[] uids) {
2033 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2034
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002035 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002036 for (int i=0; i<uids.length; i++) {
2037 cmd.appendArg(uids[i]);
2038 }
2039
2040 try {
2041 mConnector.execute(cmd);
2042 } catch (NativeDaemonConnectorException e) {
2043 throw e.rethrowAsParcelableException();
2044 }
2045 }
San Mehat873f2142010-01-14 10:25:07 -08002046}