blob: 705862ab8b977fb34e3152fd5fd3e0a2609e4364 [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey4529bb62011-12-14 10:31:54 -080019import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070020import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070022import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080023import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import static android.net.NetworkStats.TAG_NONE;
25import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070026import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090027import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070028import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080029import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080031import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070035import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080036import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070037import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
San Mehat873f2142010-01-14 10:25:07 -080039import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080041import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070044import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070046import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080047import android.net.wifi.WifiConfiguration;
48import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070049import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070050import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070051import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080052import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080053import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080054import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070055import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080056import android.os.RemoteCallbackList;
57import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070058import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070059import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080060import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080061import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080062import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070063import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080064
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070065import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070066import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070067import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080068import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070069import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070070import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070071import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070072
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070073import java.io.BufferedReader;
74import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080075import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070076import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070077import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070078import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070079import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070080import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070081import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070082import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070083import java.net.InterfaceAddress;
84import java.net.NetworkInterface;
85import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070086import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070087import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080088import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070089import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070090import java.util.NoSuchElementException;
91import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070092import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080093
94/**
95 * @hide
96 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070097public class NetworkManagementService extends INetworkManagementService.Stub
98 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070099 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700100 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700101 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900102 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700103
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800104 private static final String ADD = "add";
105 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700106
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700107 private static final String ALLOW = "allow";
108 private static final String DENY = "deny";
109
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700110 private static final String DEFAULT = "default";
111 private static final String SECONDARY = "secondary";
112
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700113 /**
114 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
115 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
116 */
117 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
118
San Mehat873f2142010-01-14 10:25:07 -0800119 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700120 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800121 public static final int InterfaceListResult = 110;
122 public static final int TetherInterfaceListResult = 111;
123 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800124 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700125 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800126
127 public static final int TetherStatusResult = 210;
128 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800129 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800130 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700131 public static final int InterfaceRxCounterResult = 216;
132 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700133 public static final int QuotaCounterResult = 220;
134 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800135 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900136 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700137 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800138
139 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700140 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700141 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900142 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900143 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800144 }
145
146 /**
147 * Binder context for this service
148 */
149 private Context mContext;
150
151 /**
152 * connector object for communicating with netd
153 */
154 private NativeDaemonConnector mConnector;
155
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700156 private final Handler mFgHandler;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700157
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800158 private IBatteryStats mBatteryStats;
159
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700160 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700161 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700162
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800163 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
164 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800165
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700166 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
167
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700168 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700169 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700170 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700171 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700172 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700173 /** Set of UIDs with active reject rules. */
174 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
175
Haoyu Bai04124232012-06-28 15:26:19 -0700176 private Object mIdleTimerLock = new Object();
177 /** Set of interfaces with active idle timers. */
178 private static class IdleTimerParams {
179 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800180 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700181 public int networkCount;
182
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800183 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700184 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800185 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700186 this.networkCount = 1;
187 }
188 }
189 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
190
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700191 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700192 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700193
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800194 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
195 new RemoteCallbackList<INetworkActivityListener>();
196 private boolean mNetworkActive;
197
San Mehat873f2142010-01-14 10:25:07 -0800198 /**
199 * Constructs a new NetworkManagementService instance
200 *
201 * @param context Binder context for this service
202 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900203 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800204 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800205
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700206 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
207 mFgHandler = new Handler(FgThread.get().getLooper());
208
Marco Nelissen62dbb222010-02-18 10:56:30 -0800209 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
210 return;
211 }
212
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800213 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700214 // Don't need this wake lock, since we now have a time stamp for when
215 // the network actually went inactive. (It might be nice to still do this,
216 // but I don't want to do it through the power manager because that pollutes the
217 // battery stats history with pointless noise.)
218 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800219
San Mehat873f2142010-01-14 10:25:07 -0800220 mConnector = new NativeDaemonConnector(
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800221 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700222 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700223
224 // Add ourself to the Watchdog monitors.
225 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700226 }
227
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900228 static NetworkManagementService create(Context context,
229 String socket) throws InterruptedException {
230 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700231 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700232 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
233 service.mThread.start();
234 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700235 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700236 if (DBG) Slog.d(TAG, "Connected");
237 return service;
San Mehat873f2142010-01-14 10:25:07 -0800238 }
239
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900240 public static NetworkManagementService create(Context context) throws InterruptedException {
241 return create(context, NETD_SOCKET_NAME);
242 }
243
Jeff Sharkey350083e2011-06-29 10:45:16 -0700244 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700245 prepareNativeDaemon();
246 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700247 }
248
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800249 private IBatteryStats getBatteryStats() {
250 synchronized (this) {
251 if (mBatteryStats != null) {
252 return mBatteryStats;
253 }
254 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
255 BatteryStats.SERVICE_NAME));
256 return mBatteryStats;
257 }
258 }
259
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800260 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800262 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800263 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800264 }
265
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800266 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800267 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800268 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800270 }
271
272 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700273 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800274 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700275 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800276 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700277 try {
278 for (int i = 0; i < length; i++) {
279 try {
280 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
281 } catch (RemoteException e) {
282 } catch (RuntimeException e) {
283 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700284 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700285 } finally {
286 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700287 }
288 }
289
290 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700291 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700292 * (typically, an Ethernet cable has been plugged-in or unplugged).
293 */
294 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700296 try {
297 for (int i = 0; i < length; i++) {
298 try {
299 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
300 } catch (RemoteException e) {
301 } catch (RuntimeException e) {
302 }
San Mehat4d02d002010-01-22 16:07:46 -0800303 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700304 } finally {
305 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800306 }
307 }
308
309 /**
310 * Notify our observers of an interface addition.
311 */
312 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800313 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700314 try {
315 for (int i = 0; i < length; i++) {
316 try {
317 mObservers.getBroadcastItem(i).interfaceAdded(iface);
318 } catch (RemoteException e) {
319 } catch (RuntimeException e) {
320 }
San Mehat4d02d002010-01-22 16:07:46 -0800321 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700322 } finally {
323 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800324 }
325 }
326
327 /**
328 * Notify our observers of an interface removal.
329 */
330 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700331 // netd already clears out quota and alerts for removed ifaces; update
332 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700333 mActiveAlerts.remove(iface);
334 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700335
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800336 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700337 try {
338 for (int i = 0; i < length; i++) {
339 try {
340 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
341 } catch (RemoteException e) {
342 } catch (RuntimeException e) {
343 }
San Mehat4d02d002010-01-22 16:07:46 -0800344 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700345 } finally {
346 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800347 }
348 }
349
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700350 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700351 * Notify our observers of a limit reached.
352 */
353 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800354 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700355 try {
356 for (int i = 0; i < length; i++) {
357 try {
358 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
359 } catch (RemoteException e) {
360 } catch (RuntimeException e) {
361 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700362 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700363 } finally {
364 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700365 }
366 }
367
368 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700369 * Notify our observers of a change in the data activity state of the interface
370 */
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700371 private void notifyInterfaceClassActivity(int type, boolean active, long tsNanos) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800372 try {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700373 getBatteryStats().noteDataConnectionActive(type, active, tsNanos);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800374 } catch (RemoteException e) {
375 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800376
Haoyu Baidb3c8672012-06-20 14:29:57 -0700377 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700378 try {
379 for (int i = 0; i < length; i++) {
380 try {
381 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
382 Integer.toString(type), active, tsNanos);
383 } catch (RemoteException e) {
384 } catch (RuntimeException e) {
385 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700386 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700387 } finally {
388 mObservers.finishBroadcast();
Haoyu Baidb3c8672012-06-20 14:29:57 -0700389 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800390
391 boolean report = false;
392 synchronized (mIdleTimerLock) {
393 if (mActiveIdleTimers.isEmpty()) {
394 // If there are no idle times, we are not monitoring activity, so we
395 // are always considered active.
396 active = true;
397 }
398 if (mNetworkActive != active) {
399 mNetworkActive = active;
400 report = active;
401 }
402 }
403 if (report) {
404 reportNetworkActive();
405 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700406 }
407
408 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700409 * Prepare native daemon once connected, enabling modules and pushing any
410 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700411 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700412 private void prepareNativeDaemon() {
413 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700414
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700415 // only enable bandwidth control when support exists
416 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
417 if (hasKernelSupport) {
418 Slog.d(TAG, "enabling bandwidth control");
419 try {
420 mConnector.execute("bandwidth", "enable");
421 mBandwidthControlEnabled = true;
422 } catch (NativeDaemonConnectorException e) {
423 Log.wtf(TAG, "problem enabling bandwidth controls", e);
424 }
425 } else {
426 Slog.d(TAG, "not enabling bandwidth control");
427 }
428
429 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
430
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700431 if (mBandwidthControlEnabled) {
432 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800433 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700434 } catch (RemoteException e) {
435 }
436 }
437
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700438 // push any existing quota or UID rules
439 synchronized (mQuotaLock) {
440 int size = mActiveQuotas.size();
441 if (size > 0) {
442 Slog.d(TAG, "pushing " + size + " active quota rules");
443 final HashMap<String, Long> activeQuotas = mActiveQuotas;
444 mActiveQuotas = Maps.newHashMap();
445 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
446 setInterfaceQuota(entry.getKey(), entry.getValue());
447 }
448 }
449
450 size = mActiveAlerts.size();
451 if (size > 0) {
452 Slog.d(TAG, "pushing " + size + " active alert rules");
453 final HashMap<String, Long> activeAlerts = mActiveAlerts;
454 mActiveAlerts = Maps.newHashMap();
455 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
456 setInterfaceAlert(entry.getKey(), entry.getValue());
457 }
458 }
459
460 size = mUidRejectOnQuota.size();
461 if (size > 0) {
462 Slog.d(TAG, "pushing " + size + " active uid rules");
463 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
464 mUidRejectOnQuota = new SparseBooleanArray();
465 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
466 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
467 }
468 }
469 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700470
471 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700472 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700473 }
San Mehat4d02d002010-01-22 16:07:46 -0800474
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900475 /**
476 * Notify our observers of a new or updated interface address.
477 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900478 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900479 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700480 try {
481 for (int i = 0; i < length; i++) {
482 try {
483 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
484 } catch (RemoteException e) {
485 } catch (RuntimeException e) {
486 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900487 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700488 } finally {
489 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900490 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900491 }
492
493 /**
494 * Notify our observers of a deleted interface address.
495 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900496 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900497 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700498 try {
499 for (int i = 0; i < length; i++) {
500 try {
501 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
502 } catch (RemoteException e) {
503 } catch (RuntimeException e) {
504 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900505 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700506 } finally {
507 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900508 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900509 }
510
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900511 /**
512 * Notify our observers of DNS server information received.
513 */
514 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
515 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700516 try {
517 for (int i = 0; i < length; i++) {
518 try {
519 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
520 addresses);
521 } catch (RemoteException e) {
522 } catch (RuntimeException e) {
523 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900524 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700525 } finally {
526 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900527 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900528 }
529
San Mehat873f2142010-01-14 10:25:07 -0800530 //
531 // Netd Callback handling
532 //
533
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700534 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
535 @Override
San Mehat873f2142010-01-14 10:25:07 -0800536 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700537 // event is dispatched from internal NDC thread, so we prepare the
538 // daemon back on main thread.
539 if (mConnectedSignal != null) {
540 mConnectedSignal.countDown();
541 mConnectedSignal = null;
542 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700543 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700544 @Override
545 public void run() {
546 prepareNativeDaemon();
547 }
548 });
549 }
San Mehat873f2142010-01-14 10:25:07 -0800550 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700551
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700552 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800553 public boolean onCheckHoldWakeLock(int code) {
554 return code == NetdResponseCode.InterfaceClassActivity;
555 }
556
557 @Override
San Mehat873f2142010-01-14 10:25:07 -0800558 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900559 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700560 switch (code) {
561 case NetdResponseCode.InterfaceChange:
562 /*
563 * a network interface change occured
564 * Format: "NNN Iface added <name>"
565 * "NNN Iface removed <name>"
566 * "NNN Iface changed <name> <up/down>"
567 * "NNN Iface linkstatus <name> <up/down>"
568 */
569 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900570 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700571 }
572 if (cooked[2].equals("added")) {
573 notifyInterfaceAdded(cooked[3]);
574 return true;
575 } else if (cooked[2].equals("removed")) {
576 notifyInterfaceRemoved(cooked[3]);
577 return true;
578 } else if (cooked[2].equals("changed") && cooked.length == 5) {
579 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
580 return true;
581 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
582 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
583 return true;
584 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900585 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700586 // break;
587 case NetdResponseCode.BandwidthControl:
588 /*
589 * Bandwidth control needs some attention
590 * Format: "NNN limit alert <alertName> <ifaceName>"
591 */
592 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900593 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700594 }
595 if (cooked[2].equals("alert")) {
596 notifyLimitReached(cooked[3], cooked[4]);
597 return true;
598 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900599 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700600 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700601 case NetdResponseCode.InterfaceClassActivity:
602 /*
603 * An network interface class state changed (active/idle)
604 * Format: "NNN IfaceClass <active/idle> <label>"
605 */
606 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900607 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700608 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700609 long timestampNanos = 0;
610 if (cooked.length == 5) {
611 try {
612 timestampNanos = Long.parseLong(cooked[4]);
613 } catch(NumberFormatException ne) {}
614 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700615 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700616 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
617 isActive, timestampNanos);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700618 return true;
619 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900620 case NetdResponseCode.InterfaceAddressChange:
621 /*
622 * A network address change occurred
623 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
624 * "NNN Address removed <addr> <iface> <flags> <scope>"
625 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900626 if (cooked.length < 7 || !cooked[1].equals("Address")) {
627 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900628 }
629
Lorenzo Colitti64483942013-11-15 18:43:52 +0900630 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900631 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900632 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900633 int flags = Integer.parseInt(cooked[5]);
634 int scope = Integer.parseInt(cooked[6]);
635 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900636 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
637 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900638 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900639 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900640 }
641
642 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900643 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900644 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900645 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900646 }
647 return true;
648 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900649 case NetdResponseCode.InterfaceDnsServerInfo:
650 /*
651 * Information about available DNS servers has been received.
652 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
653 */
654 long lifetime; // Actually a 32-bit unsigned integer.
655
656 if (cooked.length == 6 &&
657 cooked[1].equals("DnsInfo") &&
658 cooked[2].equals("servers")) {
659 try {
660 lifetime = Long.parseLong(cooked[4]);
661 } catch (NumberFormatException e) {
662 throw new IllegalStateException(errorMessage);
663 }
664 String[] servers = cooked[5].split(",");
665 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
666 }
667 return true;
668 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700669 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800670 }
671 return false;
San Mehat873f2142010-01-14 10:25:07 -0800672 }
673 }
674
San Mehated4fc8a2010-01-22 12:28:36 -0800675
San Mehat873f2142010-01-14 10:25:07 -0800676 //
677 // INetworkManagementService members
678 //
679
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800680 @Override
681 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800682 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700683 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800684 return NativeDaemonEvent.filterMessageList(
685 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700686 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800687 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700688 }
San Mehated4fc8a2010-01-22 12:28:36 -0800689 }
690
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800691 @Override
692 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800693 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800694
695 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700696 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800697 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700698 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800699 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700700 }
San Mehated4fc8a2010-01-22 12:28:36 -0800701
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800702 event.checkCode(InterfaceGetCfgResult);
703
704 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
705 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800706
Kenny Roota80ce062010-06-01 13:23:53 -0700707 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800708 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700709 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800710 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800711 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800712 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700713 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800714 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800715 } catch (IllegalArgumentException iae) {
716 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700717 }
718
719 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800720 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800721 } catch (NumberFormatException nfe) {
722 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700723 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800724
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800725 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
726 while (st.hasMoreTokens()) {
727 cfg.setFlag(st.nextToken());
728 }
Kenny Roota80ce062010-06-01 13:23:53 -0700729 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800730 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800731 }
San Mehated4fc8a2010-01-22 12:28:36 -0800732 return cfg;
733 }
734
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800735 @Override
736 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800737 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800738 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800739 if (linkAddr == null || linkAddr.getAddress() == null) {
740 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800741 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800742
743 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800744 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800745 linkAddr.getNetworkPrefixLength());
746 for (String flag : cfg.getFlags()) {
747 cmd.appendArg(flag);
748 }
749
Kenny Roota80ce062010-06-01 13:23:53 -0700750 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800751 mConnector.execute(cmd);
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 Mehat873f2142010-01-14 10:25:07 -0800755 }
756
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800757 @Override
758 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800759 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800760 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800761 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800762 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700763 }
764
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800765 @Override
766 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800767 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800768 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800769 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800770 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700771 }
772
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800773 @Override
774 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800775 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700776 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800777 mConnector.execute(
778 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700779 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800780 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700781 }
782 }
783
Irfan Sherifff5600612011-06-16 10:26:28 -0700784 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
785 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800786 @Override
787 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800788 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700789 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800790 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700791 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800792 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700793 }
794 }
795
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800796 @Override
797 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800798 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700799 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800800 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700801 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800802 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700803 }
804 }
805
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800806 @Override
807 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800808 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700809 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800810 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700811 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800812 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700813 }
814 }
815
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800816 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700817 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800818 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700819 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700820 }
821
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800822 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700823 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800824 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700825 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700826 }
827
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800828 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700829 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800830 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700831 modifyRoute(interfaceName, ADD, route, SECONDARY);
832 }
833
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800834 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700835 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800836 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700837 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
838 }
839
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800840 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
841 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700842
843 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800844 final LinkAddress la = route.getDestination();
845 cmd.appendArg(la.getAddress().getHostAddress());
846 cmd.appendArg(la.getNetworkPrefixLength());
847
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700848 if (route.getGateway() == null) {
849 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800850 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700851 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800852 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700853 }
854 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800855 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700856 }
857
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800858 try {
859 mConnector.execute(cmd);
860 } catch (NativeDaemonConnectorException e) {
861 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700862 }
863 }
864
865 private ArrayList<String> readRouteList(String filename) {
866 FileInputStream fstream = null;
867 ArrayList<String> list = new ArrayList<String>();
868
869 try {
870 fstream = new FileInputStream(filename);
871 DataInputStream in = new DataInputStream(fstream);
872 BufferedReader br = new BufferedReader(new InputStreamReader(in));
873 String s;
874
875 // throw away the title line
876
877 while (((s = br.readLine()) != null) && (s.length() != 0)) {
878 list.add(s);
879 }
880 } catch (IOException ex) {
881 // return current list, possibly empty
882 } finally {
883 if (fstream != null) {
884 try {
885 fstream.close();
886 } catch (IOException ex) {}
887 }
888 }
889
890 return list;
891 }
892
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800893 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700894 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800895 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700896 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
897
898 // v4 routes listed as:
899 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
900 for (String s : readRouteList("/proc/net/route")) {
901 String[] fields = s.split("\t");
902
903 if (fields.length > 7) {
904 String iface = fields[0];
905
906 if (interfaceName.equals(iface)) {
907 String dest = fields[1];
908 String gate = fields[2];
909 String flags = fields[3]; // future use?
910 String mask = fields[7];
911 try {
912 // address stored as a hex string, ex: 0014A8C0
913 InetAddress destAddr =
914 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
915 int prefixLength =
916 NetworkUtils.netmaskIntToPrefixLength(
917 (int)Long.parseLong(mask, 16));
918 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
919
920 // address stored as a hex string, ex 0014A8C0
921 InetAddress gatewayAddr =
922 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
923
Wink Saville7b5fd052013-03-15 05:07:04 +0000924 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700925 routes.add(route);
926 } catch (Exception e) {
927 Log.e(TAG, "Error parsing route " + s + " : " + e);
928 continue;
929 }
930 }
931 }
932 }
933
934 // v6 routes listed as:
935 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
936 for (String s : readRouteList("/proc/net/ipv6_route")) {
937 String[]fields = s.split("\\s+");
938 if (fields.length > 9) {
939 String iface = fields[9].trim();
940 if (interfaceName.equals(iface)) {
941 String dest = fields[0];
942 String prefix = fields[1];
943 String gate = fields[4];
944
945 try {
946 // prefix length stored as a hex string, ex 40
947 int prefixLength = Integer.parseInt(prefix, 16);
948
949 // address stored as a 32 char hex string
950 // ex fe800000000000000000000000000000
951 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
952 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
953
954 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
955
Wink Saville7b5fd052013-03-15 05:07:04 +0000956 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700957 routes.add(route);
958 } catch (Exception e) {
959 Log.e(TAG, "Error parsing route " + s + " : " + e);
960 continue;
961 }
962 }
963 }
964 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800965 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700966 }
967
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800968 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900969 public void setMtu(String iface, int mtu) {
970 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
971
972 final NativeDaemonEvent event;
973 try {
974 event = mConnector.execute("interface", "setmtu", iface, mtu);
975 } catch (NativeDaemonConnectorException e) {
976 throw e.rethrowAsParcelableException();
977 }
978 }
979
980 @Override
San Mehat873f2142010-01-14 10:25:07 -0800981 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800982 // TODO: remove from aidl if nobody calls externally
983 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800984
Joe Onorato8a9b2202010-02-26 18:56:32 -0800985 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800986 }
987
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800988 @Override
San Mehat873f2142010-01-14 10:25:07 -0800989 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800990 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800991
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800992 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700993 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800994 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700995 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800996 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700997 }
San Mehat873f2142010-01-14 10:25:07 -0800998
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800999 // 211 Forwarding enabled
1000 event.checkCode(IpFwdStatusResult);
1001 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001002 }
1003
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001004 @Override
1005 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001006 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001007 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001008 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001009 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001010 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001011 }
San Mehat873f2142010-01-14 10:25:07 -08001012 }
1013
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001014 @Override
1015 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001016 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001017 // cmd is "tether start first_start first_stop second_start second_stop ..."
1018 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001019
1020 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001021 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001022 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001023 }
Kenny Roota80ce062010-06-01 13:23:53 -07001024
1025 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001026 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001027 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001028 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001029 }
San Mehat873f2142010-01-14 10:25:07 -08001030 }
1031
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001032 @Override
1033 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001034 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001035 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001036 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001037 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001038 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001039 }
San Mehat873f2142010-01-14 10:25:07 -08001040 }
1041
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001042 @Override
1043 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001044 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001045
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001046 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001047 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001048 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001049 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001050 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001051 }
San Mehat873f2142010-01-14 10:25:07 -08001052
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053 // 210 Tethering services started
1054 event.checkCode(TetherStatusResult);
1055 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001056 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001057
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001058 @Override
1059 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001060 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001061 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001062 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001063 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001064 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001065 }
San Mehat873f2142010-01-14 10:25:07 -08001066 }
1067
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001068 @Override
San Mehat873f2142010-01-14 10:25:07 -08001069 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001070 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001071 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001073 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001074 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001075 }
San Mehat873f2142010-01-14 10:25:07 -08001076 }
1077
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001078 @Override
1079 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001080 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001081 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001082 return NativeDaemonEvent.filterMessageList(
1083 mConnector.executeForList("tether", "interface", "list"),
1084 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001085 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001086 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001087 }
San Mehat873f2142010-01-14 10:25:07 -08001088 }
1089
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001090 @Override
1091 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001092 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001093
1094 final Command cmd = new Command("tether", "dns", "set");
1095 for (String s : dns) {
1096 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1097 }
1098
San Mehat873f2142010-01-14 10:25:07 -08001099 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001100 mConnector.execute(cmd);
1101 } catch (NativeDaemonConnectorException e) {
1102 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001103 }
1104 }
1105
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001106 @Override
1107 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001108 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001109 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001110 return NativeDaemonEvent.filterMessageList(
1111 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001112 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001113 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001114 }
San Mehat873f2142010-01-14 10:25:07 -08001115 }
1116
jiaguo1da35f72014-01-09 16:39:59 +08001117 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1118 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1119 for (InterfaceAddress ia : addresses) {
1120 if (!ia.getAddress().isLinkLocalAddress())
1121 filtered.add(ia);
1122 }
1123 return filtered;
1124 }
1125
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001126 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001127 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001128 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001129
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001130 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1131 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001132 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001133 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001134 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001135 // Don't touch link-local routes, as link-local addresses aren't routable,
1136 // kernel creates link-local routes on all interfaces automatically
1137 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1138 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001140 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001141 InetAddress addr = NetworkUtils.getNetworkPart(
1142 ia.getAddress(), ia.getNetworkPrefixLength());
1143 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001144 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001145 }
1146
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001147 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001148 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001149 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001150 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001151 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001152 }
1153
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001154 @Override
1155 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001157 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001158 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001159 } catch (SocketException e) {
1160 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001161 }
San Mehat873f2142010-01-14 10:25:07 -08001162 }
1163
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001164 @Override
1165 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001166 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001167 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001168 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001169 } catch (SocketException e) {
1170 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001171 }
San Mehat873f2142010-01-14 10:25:07 -08001172 }
San Mehat72759df2010-01-19 13:50:37 -08001173
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001174 @Override
1175 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001176 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001177 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001178 return NativeDaemonEvent.filterMessageList(
1179 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001180 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001181 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001182 }
San Mehat72759df2010-01-19 13:50:37 -08001183 }
1184
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001185 @Override
1186 public void attachPppd(
1187 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001188 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001189 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001190 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001191 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1192 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1193 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001194 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001195 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001196 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001197 }
1198 }
1199
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001200 @Override
1201 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001202 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001203 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001204 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001205 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001206 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001207 }
San Mehat72759df2010-01-19 13:50:37 -08001208 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001209
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001210 @Override
1211 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001212 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001213 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001214 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001215 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001216 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001217 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001218 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001219 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001220 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001221 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001222 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001223 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001224 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001225 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001226 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001227 }
1228
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001229 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001230 switch (wifiConfig.getAuthType()) {
1231 case KeyMgmt.WPA_PSK:
1232 return "wpa-psk";
1233 case KeyMgmt.WPA2_PSK:
1234 return "wpa2-psk";
1235 default:
1236 return "open";
1237 }
1238 }
1239
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001240 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001241 @Override
1242 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001243 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001244 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001245 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001246 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001247 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001248 }
1249 }
1250
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001251 @Override
1252 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001253 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001254 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001255 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001256 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001257 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001258 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001259 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001260 }
1261
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001262 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001263 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001264 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001265 try {
1266 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001267 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001268 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001269 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001270 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001271 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001272 }
1273 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001274 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001275 }
1276 }
San Mehat91cac642010-03-31 14:31:36 -07001277
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001278 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001279 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001280 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1281
1282 if (DBG) Slog.d(TAG, "Adding idletimer");
1283
1284 synchronized (mIdleTimerLock) {
1285 IdleTimerParams params = mActiveIdleTimers.get(iface);
1286 if (params != null) {
1287 // the interface already has idletimer, update network count
1288 params.networkCount++;
1289 return;
1290 }
1291
1292 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001293 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1294 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001295 } catch (NativeDaemonConnectorException e) {
1296 throw e.rethrowAsParcelableException();
1297 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001298 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1299
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001300 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001301 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1302 mNetworkActive = false;
1303 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001304 mFgHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001305 @Override public void run() {
Ashish Sharma0535a9f2014-03-12 18:42:23 -07001306 notifyInterfaceClassActivity(type, true, SystemClock.elapsedRealtimeNanos());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001307 }
1308 });
Haoyu Bai04124232012-06-28 15:26:19 -07001309 }
1310 }
1311
1312 @Override
1313 public void removeIdleTimer(String iface) {
1314 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1315
1316 if (DBG) Slog.d(TAG, "Removing idletimer");
1317
1318 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001319 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001320 if (params == null || --(params.networkCount) > 0) {
1321 return;
1322 }
1323
1324 try {
1325 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001326 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001327 } catch (NativeDaemonConnectorException e) {
1328 throw e.rethrowAsParcelableException();
1329 }
1330 mActiveIdleTimers.remove(iface);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001331 mFgHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001332 @Override public void run() {
Ashish Sharma0535a9f2014-03-12 18:42:23 -07001333 notifyInterfaceClassActivity(params.type, false,
1334 SystemClock.elapsedRealtimeNanos());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001335 }
1336 });
Haoyu Bai04124232012-06-28 15:26:19 -07001337 }
1338 }
1339
1340 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001341 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001342 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001343 try {
1344 return mStatsFactory.readNetworkStatsSummaryDev();
1345 } catch (IOException e) {
1346 throw new IllegalStateException(e);
1347 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001348 }
1349
1350 @Override
1351 public NetworkStats getNetworkStatsSummaryXt() {
1352 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001353 try {
1354 return mStatsFactory.readNetworkStatsSummaryXt();
1355 } catch (IOException e) {
1356 throw new IllegalStateException(e);
1357 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001358 }
1359
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001360 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001361 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001362 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001363 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001364 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001365 } catch (IOException e) {
1366 throw new IllegalStateException(e);
1367 }
San Mehat91cac642010-03-31 14:31:36 -07001368 }
1369
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001370 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001371 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001372 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001373
Jeff Sharkey350083e2011-06-29 10:45:16 -07001374 // silently discard when control disabled
1375 // TODO: eventually migrate to be always enabled
1376 if (!mBandwidthControlEnabled) return;
1377
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001378 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001379 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001380 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001381 }
1382
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001383 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001384 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001385 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001386 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001387 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001388 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001389 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001390 }
1391 }
1392
1393 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001394 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001395 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001396
Jeff Sharkey350083e2011-06-29 10:45:16 -07001397 // silently discard when control disabled
1398 // TODO: eventually migrate to be always enabled
1399 if (!mBandwidthControlEnabled) return;
1400
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001401 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001402 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001403 // TODO: eventually consider throwing
1404 return;
1405 }
1406
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001407 mActiveQuotas.remove(iface);
1408 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001409
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001410 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001411 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001412 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001413 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001414 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001415 }
1416 }
1417 }
1418
1419 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001420 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001421 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001422
1423 // silently discard when control disabled
1424 // TODO: eventually migrate to be always enabled
1425 if (!mBandwidthControlEnabled) return;
1426
1427 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001428 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001429 throw new IllegalStateException("setting alert requires existing quota on iface");
1430 }
1431
1432 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001433 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001434 throw new IllegalStateException("iface " + iface + " already has alert");
1435 }
1436
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001437 try {
1438 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001439 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001440 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001441 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001442 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001443 }
1444 }
1445 }
1446
1447 @Override
1448 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001449 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001450
1451 // silently discard when control disabled
1452 // TODO: eventually migrate to be always enabled
1453 if (!mBandwidthControlEnabled) return;
1454
1455 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001456 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001457 // TODO: eventually consider throwing
1458 return;
1459 }
1460
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001461 try {
1462 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001463 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001464 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001465 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001466 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001467 }
1468 }
1469 }
1470
1471 @Override
1472 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001473 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001474
1475 // silently discard when control disabled
1476 // TODO: eventually migrate to be always enabled
1477 if (!mBandwidthControlEnabled) return;
1478
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001479 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001480 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001481 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001482 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001483 }
1484 }
1485
1486 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001487 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001488 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001489
Jeff Sharkey350083e2011-06-29 10:45:16 -07001490 // silently discard when control disabled
1491 // TODO: eventually migrate to be always enabled
1492 if (!mBandwidthControlEnabled) return;
1493
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001494 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001495 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1496 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1497 // TODO: eventually consider throwing
1498 return;
1499 }
1500
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001501 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001502 mConnector.execute("bandwidth",
1503 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001504 if (rejectOnQuotaInterfaces) {
1505 mUidRejectOnQuota.put(uid, true);
1506 } else {
1507 mUidRejectOnQuota.delete(uid);
1508 }
1509 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001510 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001511 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001512 }
1513 }
1514
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001515 @Override
1516 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001517 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001518 return mBandwidthControlEnabled;
1519 }
1520
1521 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001522 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001523 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001524 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001525 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001526 } catch (IOException e) {
1527 throw new IllegalStateException(e);
1528 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001529 }
1530
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001531 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001532 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001533 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001534
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001535 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001536 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001537 final NativeDaemonEvent[] events = mConnector.executeForList(
1538 "bandwidth", "gettetherstats");
1539 for (NativeDaemonEvent event : events) {
1540 if (event.getCode() != TetheringStatsListResult) continue;
1541
1542 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1543 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1544 try {
1545 final String ifaceIn = tok.nextToken();
1546 final String ifaceOut = tok.nextToken();
1547
1548 final NetworkStats.Entry entry = new NetworkStats.Entry();
1549 entry.iface = ifaceOut;
1550 entry.uid = UID_TETHERING;
1551 entry.set = SET_DEFAULT;
1552 entry.tag = TAG_NONE;
1553 entry.rxBytes = Long.parseLong(tok.nextToken());
1554 entry.rxPackets = Long.parseLong(tok.nextToken());
1555 entry.txBytes = Long.parseLong(tok.nextToken());
1556 entry.txPackets = Long.parseLong(tok.nextToken());
1557 stats.combineValues(entry);
1558 } catch (NoSuchElementException e) {
1559 throw new IllegalStateException("problem parsing tethering stats: " + event);
1560 } catch (NumberFormatException e) {
1561 throw new IllegalStateException("problem parsing tethering stats: " + event);
1562 }
1563 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001564 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001565 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001566 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001567 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001568 }
1569
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001570 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001571 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001572 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001573 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001574 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001575 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001576 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001577 }
1578 }
1579
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001580 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001581 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001582 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001583
Robert Greenwalt8058f622012-11-09 10:52:27 -08001584 final Command cmd = new Command("resolver", "setifdns", iface,
1585 (domains == null ? "" : domains));
1586
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001587 for (String s : servers) {
1588 InetAddress a = NetworkUtils.numericToInetAddress(s);
1589 if (a.isAnyLocalAddress() == false) {
1590 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001591 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001592 }
1593
1594 try {
1595 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001596 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001597 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001598 }
1599 }
1600
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001601 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001602 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1603 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1604 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001605 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001606 "uid", "add", iface, uid_start, uid_end);
1607 } catch (NativeDaemonConnectorException e) {
1608 throw e.rethrowAsParcelableException();
1609 }
1610 }
1611
1612 @Override
1613 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1614 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1615 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001616 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001617 "uid", "remove", iface, uid_start, uid_end);
1618 } catch (NativeDaemonConnectorException e) {
1619 throw e.rethrowAsParcelableException();
1620 }
1621 }
1622
1623 @Override
1624 public void setMarkedForwarding(String iface) {
1625 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1626 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001627 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001628 } catch (NativeDaemonConnectorException e) {
1629 throw e.rethrowAsParcelableException();
1630 }
1631 }
1632
1633 @Override
1634 public void clearMarkedForwarding(String iface) {
1635 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1636 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001637 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1638 } catch (NativeDaemonConnectorException e) {
1639 throw e.rethrowAsParcelableException();
1640 }
1641 }
1642
1643 @Override
1644 public int getMarkForUid(int uid) {
1645 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1646 final NativeDaemonEvent event;
1647 try {
1648 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1649 } catch (NativeDaemonConnectorException e) {
1650 throw e.rethrowAsParcelableException();
1651 }
1652 event.checkCode(GetMarkResult);
1653 return Integer.parseInt(event.getMessage());
1654 }
1655
1656 @Override
1657 public int getMarkForProtect() {
1658 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1659 final NativeDaemonEvent event;
1660 try {
1661 event = mConnector.execute("interface", "fwmark", "get", "protect");
1662 } catch (NativeDaemonConnectorException e) {
1663 throw e.rethrowAsParcelableException();
1664 }
1665 event.checkCode(GetMarkResult);
1666 return Integer.parseInt(event.getMessage());
1667 }
1668
1669 @Override
1670 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1671 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1672 try {
1673 LinkAddress dest = route.getDestination();
1674 mConnector.execute("interface", "fwmark", "route", "add", iface,
1675 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1676 } catch (NativeDaemonConnectorException e) {
1677 throw e.rethrowAsParcelableException();
1678 }
1679 }
1680
1681 @Override
1682 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1684 try {
1685 LinkAddress dest = route.getDestination();
1686 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1687 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001688 } catch (NativeDaemonConnectorException e) {
1689 throw e.rethrowAsParcelableException();
1690 }
1691 }
1692
1693 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001694 public void setHostExemption(LinkAddress host) {
1695 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1696 try {
1697 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1698 } catch (NativeDaemonConnectorException e) {
1699 throw e.rethrowAsParcelableException();
1700 }
1701 }
1702
1703 @Override
1704 public void clearHostExemption(LinkAddress host) {
1705 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1706 try {
1707 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1708 } catch (NativeDaemonConnectorException e) {
1709 throw e.rethrowAsParcelableException();
1710 }
1711 }
1712
1713 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001714 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1715 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1716 try {
1717 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1718 } catch (NativeDaemonConnectorException e) {
1719 throw e.rethrowAsParcelableException();
1720 }
1721 }
1722
1723 @Override
Chad Brubakerc77261472014-03-21 21:02:43 +00001724 public void clearDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001725 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1726 try {
Chad Brubakerc77261472014-03-21 21:02:43 +00001727 mConnector.execute("resolver", "clearifaceforuidrange", iface, uid_start, uid_end);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001728 } catch (NativeDaemonConnectorException e) {
1729 throw e.rethrowAsParcelableException();
1730 }
1731 }
1732
1733 @Override
1734 public void clearDnsInterfaceMaps() {
1735 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1736 try {
1737 mConnector.execute("resolver", "clearifacemapping");
1738 } catch (NativeDaemonConnectorException e) {
1739 throw e.rethrowAsParcelableException();
1740 }
1741 }
1742
1743
1744 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001745 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001746 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001747 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001748 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001749 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001750 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001751 }
1752 }
1753
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001754 @Override
1755 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001756 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001757 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001758 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001759 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001760 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001761 }
1762 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001763
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001764 @Override
1765 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001766 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001767 try {
1768 mConnector.execute("firewall", enabled ? "enable" : "disable");
1769 mFirewallEnabled = enabled;
1770 } catch (NativeDaemonConnectorException e) {
1771 throw e.rethrowAsParcelableException();
1772 }
1773 }
1774
1775 @Override
1776 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001777 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001778 return mFirewallEnabled;
1779 }
1780
1781 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001782 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001783 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001784 Preconditions.checkState(mFirewallEnabled);
1785 final String rule = allow ? ALLOW : DENY;
1786 try {
1787 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1788 } catch (NativeDaemonConnectorException e) {
1789 throw e.rethrowAsParcelableException();
1790 }
1791 }
1792
1793 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001794 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001795 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001796 Preconditions.checkState(mFirewallEnabled);
1797 final String rule = allow ? ALLOW : DENY;
1798 try {
1799 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1800 } catch (NativeDaemonConnectorException e) {
1801 throw e.rethrowAsParcelableException();
1802 }
1803 }
1804
1805 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001806 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001807 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001808 Preconditions.checkState(mFirewallEnabled);
1809 final String rule = allow ? ALLOW : DENY;
1810 try {
1811 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1812 } catch (NativeDaemonConnectorException e) {
1813 throw e.rethrowAsParcelableException();
1814 }
1815 }
1816
1817 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001818 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001819 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001820 Preconditions.checkState(mFirewallEnabled);
1821 final String rule = allow ? ALLOW : DENY;
1822 try {
1823 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1824 } catch (NativeDaemonConnectorException e) {
1825 throw e.rethrowAsParcelableException();
1826 }
1827 }
1828
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001829 private static void enforceSystemUid() {
1830 final int uid = Binder.getCallingUid();
1831 if (uid != Process.SYSTEM_UID) {
1832 throw new SecurityException("Only available to AID_SYSTEM");
1833 }
1834 }
1835
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001836 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001837 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1838 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1839 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001840 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001841 } catch (NativeDaemonConnectorException e) {
1842 throw new IllegalStateException(
1843 "Error communicating with native deamon to set interface for pid" + iface, e);
1844 }
1845 }
1846
1847 @Override
1848 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1849 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1850 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001851 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001852 } catch (NativeDaemonConnectorException e) {
1853 throw new IllegalStateException(
1854 "Error communicating with native deamon to clear interface for pid " + pid, e);
1855 }
1856 }
1857
Lorenzo Colitti79751842013-02-28 16:16:03 +09001858 @Override
1859 public void startClatd(String interfaceName) throws IllegalStateException {
1860 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1861
1862 try {
1863 mConnector.execute("clatd", "start", interfaceName);
1864 } catch (NativeDaemonConnectorException e) {
1865 throw e.rethrowAsParcelableException();
1866 }
1867 }
1868
1869 @Override
1870 public void stopClatd() throws IllegalStateException {
1871 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1872
1873 try {
1874 mConnector.execute("clatd", "stop");
1875 } catch (NativeDaemonConnectorException e) {
1876 throw e.rethrowAsParcelableException();
1877 }
1878 }
1879
1880 @Override
1881 public boolean isClatdStarted() {
1882 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1883
1884 final NativeDaemonEvent event;
1885 try {
1886 event = mConnector.execute("clatd", "status");
1887 } catch (NativeDaemonConnectorException e) {
1888 throw e.rethrowAsParcelableException();
1889 }
1890
1891 event.checkCode(ClatdStatusResult);
1892 return event.getMessage().endsWith("started");
1893 }
1894
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001895 @Override
1896 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1897 mNetworkActivityListeners.register(listener);
1898 }
1899
1900 @Override
1901 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1902 mNetworkActivityListeners.unregister(listener);
1903 }
1904
1905 @Override
1906 public boolean isNetworkActive() {
1907 synchronized (mNetworkActivityListeners) {
1908 return mNetworkActive || mActiveIdleTimers.isEmpty();
1909 }
1910 }
1911
1912 private void reportNetworkActive() {
1913 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001914 try {
1915 for (int i = 0; i < length; i++) {
1916 try {
1917 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1918 } catch (RemoteException e) {
1919 } catch (RuntimeException e) {
1920 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001921 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001922 } finally {
1923 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001924 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001925 }
1926
Mattias Falk8b47b362011-08-23 14:15:13 +02001927 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001928 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001929 public void monitor() {
1930 if (mConnector != null) {
1931 mConnector.monitor();
1932 }
1933 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001934
1935 @Override
1936 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1937 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1938
Robert Greenwalt470fd722012-01-18 12:51:15 -08001939 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1940 mConnector.dump(fd, pw, args);
1941 pw.println();
1942
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001943 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1944
1945 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001946 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1947 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001948 }
1949
1950 synchronized (mUidRejectOnQuota) {
1951 pw.print("UID reject on quota ifaces: [");
1952 final int size = mUidRejectOnQuota.size();
1953 for (int i = 0; i < size; i++) {
1954 pw.print(mUidRejectOnQuota.keyAt(i));
1955 if (i < size - 1) pw.print(",");
1956 }
1957 pw.println("]");
1958 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001959
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001960 synchronized (mIdleTimerLock) {
1961 pw.println("Idle timers:");
1962 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1963 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1964 IdleTimerParams params = ent.getValue();
1965 pw.print(" timeout="); pw.print(params.timeout);
1966 pw.print(" type="); pw.print(params.type);
1967 pw.print(" networkCount="); pw.println(params.networkCount);
1968 }
1969 }
1970
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001971 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001972 }
San Mehat873f2142010-01-14 10:25:07 -08001973}