blob: bfc966beb04f8117c0e79f67cfef31c8ff750833 [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
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700156 private final Handler mMainHandler = new Handler();
157
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
Marco Nelissen62dbb222010-02-18 10:56:30 -0800206 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
207 return;
208 }
209
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800210 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
211 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
212
San Mehat873f2142010-01-14 10:25:07 -0800213 mConnector = new NativeDaemonConnector(
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800214 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700215 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700216
217 // Add ourself to the Watchdog monitors.
218 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700219 }
220
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900221 static NetworkManagementService create(Context context,
222 String socket) throws InterruptedException {
223 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700224 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700225 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
226 service.mThread.start();
227 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700228 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700229 if (DBG) Slog.d(TAG, "Connected");
230 return service;
San Mehat873f2142010-01-14 10:25:07 -0800231 }
232
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900233 public static NetworkManagementService create(Context context) throws InterruptedException {
234 return create(context, NETD_SOCKET_NAME);
235 }
236
Jeff Sharkey350083e2011-06-29 10:45:16 -0700237 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700238 prepareNativeDaemon();
239 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700240 }
241
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800242 private IBatteryStats getBatteryStats() {
243 synchronized (this) {
244 if (mBatteryStats != null) {
245 return mBatteryStats;
246 }
247 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
248 BatteryStats.SERVICE_NAME));
249 return mBatteryStats;
250 }
251 }
252
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800253 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800254 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800255 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800256 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800257 }
258
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800259 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800260 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800261 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800262 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800263 }
264
265 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700266 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800267 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700268 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 final int length = mObservers.beginBroadcast();
270 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800271 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800272 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
273 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900274 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700275 }
276 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700278 }
279
280 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700281 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700282 * (typically, an Ethernet cable has been plugged-in or unplugged).
283 */
284 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 final int length = mObservers.beginBroadcast();
286 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700287 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800288 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
289 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900290 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800291 }
292 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800293 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800294 }
295
296 /**
297 * Notify our observers of an interface addition.
298 */
299 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 final int length = mObservers.beginBroadcast();
301 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800302 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800303 mObservers.getBroadcastItem(i).interfaceAdded(iface);
304 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900305 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800306 }
307 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800308 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800309 }
310
311 /**
312 * Notify our observers of an interface removal.
313 */
314 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700315 // netd already clears out quota and alerts for removed ifaces; update
316 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700317 mActiveAlerts.remove(iface);
318 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700319
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800320 final int length = mObservers.beginBroadcast();
321 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800322 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800323 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
324 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900325 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800326 }
327 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800328 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800329 }
330
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700331 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700332 * Notify our observers of a limit reached.
333 */
334 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800335 final int length = mObservers.beginBroadcast();
336 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700337 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800338 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
339 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900340 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700341 }
342 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800343 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700344 }
345
346 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700347 * Notify our observers of a change in the data activity state of the interface
348 */
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800349 private void notifyInterfaceClassActivity(int type, boolean active) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800350 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800351 getBatteryStats().noteDataConnectionActive(type, active);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800352 } catch (RemoteException e) {
353 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800354
Haoyu Baidb3c8672012-06-20 14:29:57 -0700355 final int length = mObservers.beginBroadcast();
356 for (int i = 0; i < length; i++) {
357 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800358 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
359 Integer.toString(type), active);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700360 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900361 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700362 }
363 }
364 mObservers.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800365
366 boolean report = false;
367 synchronized (mIdleTimerLock) {
368 if (mActiveIdleTimers.isEmpty()) {
369 // If there are no idle times, we are not monitoring activity, so we
370 // are always considered active.
371 active = true;
372 }
373 if (mNetworkActive != active) {
374 mNetworkActive = active;
375 report = active;
376 }
377 }
378 if (report) {
379 reportNetworkActive();
380 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700381 }
382
383 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700384 * Prepare native daemon once connected, enabling modules and pushing any
385 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700386 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700387 private void prepareNativeDaemon() {
388 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700389
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700390 // only enable bandwidth control when support exists
391 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
392 if (hasKernelSupport) {
393 Slog.d(TAG, "enabling bandwidth control");
394 try {
395 mConnector.execute("bandwidth", "enable");
396 mBandwidthControlEnabled = true;
397 } catch (NativeDaemonConnectorException e) {
398 Log.wtf(TAG, "problem enabling bandwidth controls", e);
399 }
400 } else {
401 Slog.d(TAG, "not enabling bandwidth control");
402 }
403
404 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
405
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700406 if (mBandwidthControlEnabled) {
407 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800408 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700409 } catch (RemoteException e) {
410 }
411 }
412
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700413 // push any existing quota or UID rules
414 synchronized (mQuotaLock) {
415 int size = mActiveQuotas.size();
416 if (size > 0) {
417 Slog.d(TAG, "pushing " + size + " active quota rules");
418 final HashMap<String, Long> activeQuotas = mActiveQuotas;
419 mActiveQuotas = Maps.newHashMap();
420 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
421 setInterfaceQuota(entry.getKey(), entry.getValue());
422 }
423 }
424
425 size = mActiveAlerts.size();
426 if (size > 0) {
427 Slog.d(TAG, "pushing " + size + " active alert rules");
428 final HashMap<String, Long> activeAlerts = mActiveAlerts;
429 mActiveAlerts = Maps.newHashMap();
430 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
431 setInterfaceAlert(entry.getKey(), entry.getValue());
432 }
433 }
434
435 size = mUidRejectOnQuota.size();
436 if (size > 0) {
437 Slog.d(TAG, "pushing " + size + " active uid rules");
438 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
439 mUidRejectOnQuota = new SparseBooleanArray();
440 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
441 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
442 }
443 }
444 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700445
446 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700447 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700448 }
San Mehat4d02d002010-01-22 16:07:46 -0800449
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900450 /**
451 * Notify our observers of a new or updated interface address.
452 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900453 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900454 final int length = mObservers.beginBroadcast();
455 for (int i = 0; i < length; i++) {
456 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900457 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900458 } catch (RemoteException e) {
459 } catch (RuntimeException e) {
460 }
461 }
462 mObservers.finishBroadcast();
463 }
464
465 /**
466 * Notify our observers of a deleted interface address.
467 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900468 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900469 final int length = mObservers.beginBroadcast();
470 for (int i = 0; i < length; i++) {
471 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900472 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900473 } catch (RemoteException e) {
474 } catch (RuntimeException e) {
475 }
476 }
477 mObservers.finishBroadcast();
478 }
479
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900480 /**
481 * Notify our observers of DNS server information received.
482 */
483 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
484 final int length = mObservers.beginBroadcast();
485 for (int i = 0; i < length; i++) {
486 try {
487 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime, addresses);
488 } catch (RemoteException e) {
489 } catch (RuntimeException e) {
490 }
491 }
492 mObservers.finishBroadcast();
493 }
494
San Mehat873f2142010-01-14 10:25:07 -0800495 //
496 // Netd Callback handling
497 //
498
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700499 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
500 @Override
San Mehat873f2142010-01-14 10:25:07 -0800501 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700502 // event is dispatched from internal NDC thread, so we prepare the
503 // daemon back on main thread.
504 if (mConnectedSignal != null) {
505 mConnectedSignal.countDown();
506 mConnectedSignal = null;
507 } else {
508 mMainHandler.post(new Runnable() {
509 @Override
510 public void run() {
511 prepareNativeDaemon();
512 }
513 });
514 }
San Mehat873f2142010-01-14 10:25:07 -0800515 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700516
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700517 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800518 public boolean onCheckHoldWakeLock(int code) {
519 return code == NetdResponseCode.InterfaceClassActivity;
520 }
521
522 @Override
San Mehat873f2142010-01-14 10:25:07 -0800523 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900524 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700525 switch (code) {
526 case NetdResponseCode.InterfaceChange:
527 /*
528 * a network interface change occured
529 * Format: "NNN Iface added <name>"
530 * "NNN Iface removed <name>"
531 * "NNN Iface changed <name> <up/down>"
532 * "NNN Iface linkstatus <name> <up/down>"
533 */
534 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900535 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700536 }
537 if (cooked[2].equals("added")) {
538 notifyInterfaceAdded(cooked[3]);
539 return true;
540 } else if (cooked[2].equals("removed")) {
541 notifyInterfaceRemoved(cooked[3]);
542 return true;
543 } else if (cooked[2].equals("changed") && cooked.length == 5) {
544 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
545 return true;
546 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
547 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
548 return true;
549 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900550 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700551 // break;
552 case NetdResponseCode.BandwidthControl:
553 /*
554 * Bandwidth control needs some attention
555 * Format: "NNN limit alert <alertName> <ifaceName>"
556 */
557 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900558 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700559 }
560 if (cooked[2].equals("alert")) {
561 notifyLimitReached(cooked[3], cooked[4]);
562 return true;
563 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900564 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700565 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700566 case NetdResponseCode.InterfaceClassActivity:
567 /*
568 * An network interface class state changed (active/idle)
569 * Format: "NNN IfaceClass <active/idle> <label>"
570 */
571 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900572 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700573 }
574 boolean isActive = cooked[2].equals("active");
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800575 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]), isActive);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700576 return true;
577 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900578 case NetdResponseCode.InterfaceAddressChange:
579 /*
580 * A network address change occurred
581 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
582 * "NNN Address removed <addr> <iface> <flags> <scope>"
583 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900584 if (cooked.length < 7 || !cooked[1].equals("Address")) {
585 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900586 }
587
Lorenzo Colitti64483942013-11-15 18:43:52 +0900588 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900589 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900590 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900591 int flags = Integer.parseInt(cooked[5]);
592 int scope = Integer.parseInt(cooked[6]);
593 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900594 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
595 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900596 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900597 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900598 }
599
600 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900601 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900602 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900603 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900604 }
605 return true;
606 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900607 case NetdResponseCode.InterfaceDnsServerInfo:
608 /*
609 * Information about available DNS servers has been received.
610 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
611 */
612 long lifetime; // Actually a 32-bit unsigned integer.
613
614 if (cooked.length == 6 &&
615 cooked[1].equals("DnsInfo") &&
616 cooked[2].equals("servers")) {
617 try {
618 lifetime = Long.parseLong(cooked[4]);
619 } catch (NumberFormatException e) {
620 throw new IllegalStateException(errorMessage);
621 }
622 String[] servers = cooked[5].split(",");
623 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
624 }
625 return true;
626 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700627 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800628 }
629 return false;
San Mehat873f2142010-01-14 10:25:07 -0800630 }
631 }
632
San Mehated4fc8a2010-01-22 12:28:36 -0800633
San Mehat873f2142010-01-14 10:25:07 -0800634 //
635 // INetworkManagementService members
636 //
637
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800638 @Override
639 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800640 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700641 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800642 return NativeDaemonEvent.filterMessageList(
643 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700644 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800645 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700646 }
San Mehated4fc8a2010-01-22 12:28:36 -0800647 }
648
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800649 @Override
650 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800651 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800652
653 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700654 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800655 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700656 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800657 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700658 }
San Mehated4fc8a2010-01-22 12:28:36 -0800659
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800660 event.checkCode(InterfaceGetCfgResult);
661
662 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
663 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800664
Kenny Roota80ce062010-06-01 13:23:53 -0700665 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800666 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700667 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800668 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800669 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800670 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700671 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800672 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800673 } catch (IllegalArgumentException iae) {
674 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700675 }
676
677 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800678 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800679 } catch (NumberFormatException nfe) {
680 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700681 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800682
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800683 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
684 while (st.hasMoreTokens()) {
685 cfg.setFlag(st.nextToken());
686 }
Kenny Roota80ce062010-06-01 13:23:53 -0700687 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800688 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800689 }
San Mehated4fc8a2010-01-22 12:28:36 -0800690 return cfg;
691 }
692
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800693 @Override
694 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800695 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800696 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800697 if (linkAddr == null || linkAddr.getAddress() == null) {
698 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800699 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800700
701 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800702 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800703 linkAddr.getNetworkPrefixLength());
704 for (String flag : cfg.getFlags()) {
705 cmd.appendArg(flag);
706 }
707
Kenny Roota80ce062010-06-01 13:23:53 -0700708 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800709 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700710 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800711 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700712 }
San Mehat873f2142010-01-14 10:25:07 -0800713 }
714
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800715 @Override
716 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800718 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800719 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800720 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700721 }
722
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800723 @Override
724 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800725 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800726 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800727 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800728 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700729 }
730
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800731 @Override
732 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700734 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800735 mConnector.execute(
736 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700737 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800738 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700739 }
740 }
741
Irfan Sherifff5600612011-06-16 10:26:28 -0700742 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
743 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800744 @Override
745 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800746 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700747 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800748 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700749 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800750 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700751 }
752 }
753
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800754 @Override
755 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800756 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700757 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800758 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700759 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800760 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700761 }
762 }
763
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800764 @Override
765 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800766 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700767 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700769 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800770 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700771 }
772 }
773
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800774 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700775 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800776 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700777 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700778 }
779
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800780 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700781 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800782 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700783 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700784 }
785
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800786 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700787 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800788 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700789 modifyRoute(interfaceName, ADD, route, SECONDARY);
790 }
791
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800792 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700793 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800794 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700795 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
796 }
797
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800798 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
799 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700800
801 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800802 final LinkAddress la = route.getDestination();
803 cmd.appendArg(la.getAddress().getHostAddress());
804 cmd.appendArg(la.getNetworkPrefixLength());
805
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700806 if (route.getGateway() == null) {
807 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800808 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700809 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800810 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700811 }
812 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800813 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700814 }
815
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800816 try {
817 mConnector.execute(cmd);
818 } catch (NativeDaemonConnectorException e) {
819 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700820 }
821 }
822
823 private ArrayList<String> readRouteList(String filename) {
824 FileInputStream fstream = null;
825 ArrayList<String> list = new ArrayList<String>();
826
827 try {
828 fstream = new FileInputStream(filename);
829 DataInputStream in = new DataInputStream(fstream);
830 BufferedReader br = new BufferedReader(new InputStreamReader(in));
831 String s;
832
833 // throw away the title line
834
835 while (((s = br.readLine()) != null) && (s.length() != 0)) {
836 list.add(s);
837 }
838 } catch (IOException ex) {
839 // return current list, possibly empty
840 } finally {
841 if (fstream != null) {
842 try {
843 fstream.close();
844 } catch (IOException ex) {}
845 }
846 }
847
848 return list;
849 }
850
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800851 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700852 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800853 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700854 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
855
856 // v4 routes listed as:
857 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
858 for (String s : readRouteList("/proc/net/route")) {
859 String[] fields = s.split("\t");
860
861 if (fields.length > 7) {
862 String iface = fields[0];
863
864 if (interfaceName.equals(iface)) {
865 String dest = fields[1];
866 String gate = fields[2];
867 String flags = fields[3]; // future use?
868 String mask = fields[7];
869 try {
870 // address stored as a hex string, ex: 0014A8C0
871 InetAddress destAddr =
872 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
873 int prefixLength =
874 NetworkUtils.netmaskIntToPrefixLength(
875 (int)Long.parseLong(mask, 16));
876 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
877
878 // address stored as a hex string, ex 0014A8C0
879 InetAddress gatewayAddr =
880 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
881
Wink Saville7b5fd052013-03-15 05:07:04 +0000882 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700883 routes.add(route);
884 } catch (Exception e) {
885 Log.e(TAG, "Error parsing route " + s + " : " + e);
886 continue;
887 }
888 }
889 }
890 }
891
892 // v6 routes listed as:
893 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
894 for (String s : readRouteList("/proc/net/ipv6_route")) {
895 String[]fields = s.split("\\s+");
896 if (fields.length > 9) {
897 String iface = fields[9].trim();
898 if (interfaceName.equals(iface)) {
899 String dest = fields[0];
900 String prefix = fields[1];
901 String gate = fields[4];
902
903 try {
904 // prefix length stored as a hex string, ex 40
905 int prefixLength = Integer.parseInt(prefix, 16);
906
907 // address stored as a 32 char hex string
908 // ex fe800000000000000000000000000000
909 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
910 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
911
912 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
913
Wink Saville7b5fd052013-03-15 05:07:04 +0000914 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700915 routes.add(route);
916 } catch (Exception e) {
917 Log.e(TAG, "Error parsing route " + s + " : " + e);
918 continue;
919 }
920 }
921 }
922 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800923 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700924 }
925
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800926 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900927 public void setMtu(String iface, int mtu) {
928 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
929
930 final NativeDaemonEvent event;
931 try {
932 event = mConnector.execute("interface", "setmtu", iface, mtu);
933 } catch (NativeDaemonConnectorException e) {
934 throw e.rethrowAsParcelableException();
935 }
936 }
937
938 @Override
San Mehat873f2142010-01-14 10:25:07 -0800939 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800940 // TODO: remove from aidl if nobody calls externally
941 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800942
Joe Onorato8a9b2202010-02-26 18:56:32 -0800943 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800944 }
945
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800946 @Override
San Mehat873f2142010-01-14 10:25:07 -0800947 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800948 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800949
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800950 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700951 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800952 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700953 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800954 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700955 }
San Mehat873f2142010-01-14 10:25:07 -0800956
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800957 // 211 Forwarding enabled
958 event.checkCode(IpFwdStatusResult);
959 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800960 }
961
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800962 @Override
963 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800964 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800965 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800966 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800967 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800968 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800969 }
San Mehat873f2142010-01-14 10:25:07 -0800970 }
971
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800972 @Override
973 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800974 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700975 // cmd is "tether start first_start first_stop second_start second_stop ..."
976 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977
978 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700979 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800980 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700981 }
Kenny Roota80ce062010-06-01 13:23:53 -0700982
983 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800984 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700985 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800986 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700987 }
San Mehat873f2142010-01-14 10:25:07 -0800988 }
989
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800990 @Override
991 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800992 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700993 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800994 mConnector.execute("tether", "stop");
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 }
999
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001000 @Override
1001 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001002 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001003
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001004 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001005 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001006 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001007 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001008 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001009 }
San Mehat873f2142010-01-14 10:25:07 -08001010
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001011 // 210 Tethering services started
1012 event.checkCode(TetherStatusResult);
1013 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001014 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001015
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001016 @Override
1017 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001018 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001019 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001020 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001021 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001022 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001023 }
San Mehat873f2142010-01-14 10:25:07 -08001024 }
1025
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001026 @Override
San Mehat873f2142010-01-14 10:25:07 -08001027 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001028 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001029 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001030 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001031 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001032 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001033 }
San Mehat873f2142010-01-14 10:25:07 -08001034 }
1035
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001036 @Override
1037 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001038 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001039 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001040 return NativeDaemonEvent.filterMessageList(
1041 mConnector.executeForList("tether", "interface", "list"),
1042 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001043 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001044 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001045 }
San Mehat873f2142010-01-14 10:25:07 -08001046 }
1047
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001048 @Override
1049 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001050 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001051
1052 final Command cmd = new Command("tether", "dns", "set");
1053 for (String s : dns) {
1054 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1055 }
1056
San Mehat873f2142010-01-14 10:25:07 -08001057 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001058 mConnector.execute(cmd);
1059 } catch (NativeDaemonConnectorException e) {
1060 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001061 }
1062 }
1063
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001064 @Override
1065 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001066 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001067 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001068 return NativeDaemonEvent.filterMessageList(
1069 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001070 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001071 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001072 }
San Mehat873f2142010-01-14 10:25:07 -08001073 }
1074
jiaguo1da35f72014-01-09 16:39:59 +08001075 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1076 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1077 for (InterfaceAddress ia : addresses) {
1078 if (!ia.getAddress().isLinkLocalAddress())
1079 filtered.add(ia);
1080 }
1081 return filtered;
1082 }
1083
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001084 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001085 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001086 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001087
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001088 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1089 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001090 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001091 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001092 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001093 // Don't touch link-local routes, as link-local addresses aren't routable,
1094 // kernel creates link-local routes on all interfaces automatically
1095 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1096 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001097 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001098 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001099 InetAddress addr = NetworkUtils.getNetworkPart(
1100 ia.getAddress(), ia.getNetworkPrefixLength());
1101 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001102 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001103 }
1104
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001105 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001107 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001108 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001109 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001110 }
1111
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001112 @Override
1113 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001115 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001116 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001117 } catch (SocketException e) {
1118 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001119 }
San Mehat873f2142010-01-14 10:25:07 -08001120 }
1121
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001122 @Override
1123 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001124 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001125 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001126 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001127 } catch (SocketException e) {
1128 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001129 }
San Mehat873f2142010-01-14 10:25:07 -08001130 }
San Mehat72759df2010-01-19 13:50:37 -08001131
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001132 @Override
1133 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001134 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001135 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136 return NativeDaemonEvent.filterMessageList(
1137 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001138 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001139 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001140 }
San Mehat72759df2010-01-19 13:50:37 -08001141 }
1142
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001143 @Override
1144 public void attachPppd(
1145 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001146 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001147 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001148 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001149 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1150 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1151 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001152 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001153 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001154 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001155 }
1156 }
1157
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001158 @Override
1159 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001160 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001161 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001162 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001163 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001164 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001165 }
San Mehat72759df2010-01-19 13:50:37 -08001166 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001167
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001168 @Override
1169 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001170 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001171 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001172 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001173 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001174 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001175 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001176 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001177 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001178 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001179 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001180 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001182 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001183 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001184 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001185 }
1186
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001187 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001188 switch (wifiConfig.getAuthType()) {
1189 case KeyMgmt.WPA_PSK:
1190 return "wpa-psk";
1191 case KeyMgmt.WPA2_PSK:
1192 return "wpa2-psk";
1193 default:
1194 return "open";
1195 }
1196 }
1197
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001198 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001199 @Override
1200 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001201 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001202 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001203 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001204 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001205 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001206 }
1207 }
1208
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001209 @Override
1210 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001211 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001212 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001213 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001214 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001215 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001216 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001217 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001218 }
1219
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001220 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001221 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001223 try {
1224 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001225 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001226 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001227 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001228 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001229 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001230 }
1231 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001232 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001233 }
1234 }
San Mehat91cac642010-03-31 14:31:36 -07001235
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001236 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001237 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001238 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1239
1240 if (DBG) Slog.d(TAG, "Adding idletimer");
1241
1242 synchronized (mIdleTimerLock) {
1243 IdleTimerParams params = mActiveIdleTimers.get(iface);
1244 if (params != null) {
1245 // the interface already has idletimer, update network count
1246 params.networkCount++;
1247 return;
1248 }
1249
1250 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001251 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1252 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001253 } catch (NativeDaemonConnectorException e) {
1254 throw e.rethrowAsParcelableException();
1255 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001256 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1257
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001258 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001259 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1260 mNetworkActive = false;
1261 }
1262 mMainHandler.post(new Runnable() {
1263 @Override public void run() {
1264 notifyInterfaceClassActivity(type, true);
1265 }
1266 });
Haoyu Bai04124232012-06-28 15:26:19 -07001267 }
1268 }
1269
1270 @Override
1271 public void removeIdleTimer(String iface) {
1272 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1273
1274 if (DBG) Slog.d(TAG, "Removing idletimer");
1275
1276 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001277 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001278 if (params == null || --(params.networkCount) > 0) {
1279 return;
1280 }
1281
1282 try {
1283 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001284 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001285 } catch (NativeDaemonConnectorException e) {
1286 throw e.rethrowAsParcelableException();
1287 }
1288 mActiveIdleTimers.remove(iface);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001289 mMainHandler.post(new Runnable() {
1290 @Override public void run() {
1291 notifyInterfaceClassActivity(params.type, false);
1292 }
1293 });
Haoyu Bai04124232012-06-28 15:26:19 -07001294 }
1295 }
1296
1297 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001298 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001299 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001300 try {
1301 return mStatsFactory.readNetworkStatsSummaryDev();
1302 } catch (IOException e) {
1303 throw new IllegalStateException(e);
1304 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001305 }
1306
1307 @Override
1308 public NetworkStats getNetworkStatsSummaryXt() {
1309 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001310 try {
1311 return mStatsFactory.readNetworkStatsSummaryXt();
1312 } catch (IOException e) {
1313 throw new IllegalStateException(e);
1314 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001315 }
1316
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001317 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001318 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001319 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001320 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001321 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001322 } catch (IOException e) {
1323 throw new IllegalStateException(e);
1324 }
San Mehat91cac642010-03-31 14:31:36 -07001325 }
1326
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001327 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001328 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001329 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001330
Jeff Sharkey350083e2011-06-29 10:45:16 -07001331 // silently discard when control disabled
1332 // TODO: eventually migrate to be always enabled
1333 if (!mBandwidthControlEnabled) return;
1334
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001335 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001336 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001337 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001338 }
1339
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001340 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001341 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001342 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001343 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001344 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001345 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001346 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001347 }
1348 }
1349
1350 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001351 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001352 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001353
Jeff Sharkey350083e2011-06-29 10:45:16 -07001354 // silently discard when control disabled
1355 // TODO: eventually migrate to be always enabled
1356 if (!mBandwidthControlEnabled) return;
1357
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001358 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001359 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001360 // TODO: eventually consider throwing
1361 return;
1362 }
1363
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001364 mActiveQuotas.remove(iface);
1365 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001366
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001367 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001368 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001369 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001370 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001371 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001372 }
1373 }
1374 }
1375
1376 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001377 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001378 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001379
1380 // silently discard when control disabled
1381 // TODO: eventually migrate to be always enabled
1382 if (!mBandwidthControlEnabled) return;
1383
1384 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001385 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001386 throw new IllegalStateException("setting alert requires existing quota on iface");
1387 }
1388
1389 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001390 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001391 throw new IllegalStateException("iface " + iface + " already has alert");
1392 }
1393
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001394 try {
1395 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001396 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001397 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001398 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001399 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001400 }
1401 }
1402 }
1403
1404 @Override
1405 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001406 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001407
1408 // silently discard when control disabled
1409 // TODO: eventually migrate to be always enabled
1410 if (!mBandwidthControlEnabled) return;
1411
1412 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001413 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001414 // TODO: eventually consider throwing
1415 return;
1416 }
1417
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001418 try {
1419 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001420 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001421 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001422 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001423 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001424 }
1425 }
1426 }
1427
1428 @Override
1429 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001430 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001431
1432 // silently discard when control disabled
1433 // TODO: eventually migrate to be always enabled
1434 if (!mBandwidthControlEnabled) return;
1435
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001436 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001437 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001438 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001439 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001440 }
1441 }
1442
1443 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001444 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001445 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001446
Jeff Sharkey350083e2011-06-29 10:45:16 -07001447 // silently discard when control disabled
1448 // TODO: eventually migrate to be always enabled
1449 if (!mBandwidthControlEnabled) return;
1450
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001451 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001452 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1453 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1454 // TODO: eventually consider throwing
1455 return;
1456 }
1457
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001458 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001459 mConnector.execute("bandwidth",
1460 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001461 if (rejectOnQuotaInterfaces) {
1462 mUidRejectOnQuota.put(uid, true);
1463 } else {
1464 mUidRejectOnQuota.delete(uid);
1465 }
1466 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001467 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001468 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001469 }
1470 }
1471
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001472 @Override
1473 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001475 return mBandwidthControlEnabled;
1476 }
1477
1478 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001479 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001481 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001482 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001483 } catch (IOException e) {
1484 throw new IllegalStateException(e);
1485 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001486 }
1487
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001488 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001489 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001490 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001491
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001492 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001493 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001494 final NativeDaemonEvent[] events = mConnector.executeForList(
1495 "bandwidth", "gettetherstats");
1496 for (NativeDaemonEvent event : events) {
1497 if (event.getCode() != TetheringStatsListResult) continue;
1498
1499 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1500 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1501 try {
1502 final String ifaceIn = tok.nextToken();
1503 final String ifaceOut = tok.nextToken();
1504
1505 final NetworkStats.Entry entry = new NetworkStats.Entry();
1506 entry.iface = ifaceOut;
1507 entry.uid = UID_TETHERING;
1508 entry.set = SET_DEFAULT;
1509 entry.tag = TAG_NONE;
1510 entry.rxBytes = Long.parseLong(tok.nextToken());
1511 entry.rxPackets = Long.parseLong(tok.nextToken());
1512 entry.txBytes = Long.parseLong(tok.nextToken());
1513 entry.txPackets = Long.parseLong(tok.nextToken());
1514 stats.combineValues(entry);
1515 } catch (NoSuchElementException e) {
1516 throw new IllegalStateException("problem parsing tethering stats: " + event);
1517 } catch (NumberFormatException e) {
1518 throw new IllegalStateException("problem parsing tethering stats: " + event);
1519 }
1520 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001521 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001522 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001523 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001524 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001525 }
1526
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001527 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001528 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001529 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001530 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001531 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001532 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001533 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001534 }
1535 }
1536
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001537 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001538 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001539 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001540
Robert Greenwalt8058f622012-11-09 10:52:27 -08001541 final Command cmd = new Command("resolver", "setifdns", iface,
1542 (domains == null ? "" : domains));
1543
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001544 for (String s : servers) {
1545 InetAddress a = NetworkUtils.numericToInetAddress(s);
1546 if (a.isAnyLocalAddress() == false) {
1547 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001548 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001549 }
1550
1551 try {
1552 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001553 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001554 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001555 }
1556 }
1557
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001558 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001559 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1560 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1561 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001562 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001563 "uid", "add", iface, uid_start, uid_end);
1564 } catch (NativeDaemonConnectorException e) {
1565 throw e.rethrowAsParcelableException();
1566 }
1567 }
1568
1569 @Override
1570 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1571 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1572 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001573 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001574 "uid", "remove", iface, uid_start, uid_end);
1575 } catch (NativeDaemonConnectorException e) {
1576 throw e.rethrowAsParcelableException();
1577 }
1578 }
1579
1580 @Override
1581 public void setMarkedForwarding(String iface) {
1582 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1583 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001584 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001585 } catch (NativeDaemonConnectorException e) {
1586 throw e.rethrowAsParcelableException();
1587 }
1588 }
1589
1590 @Override
1591 public void clearMarkedForwarding(String iface) {
1592 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1593 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001594 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1595 } catch (NativeDaemonConnectorException e) {
1596 throw e.rethrowAsParcelableException();
1597 }
1598 }
1599
1600 @Override
1601 public int getMarkForUid(int uid) {
1602 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1603 final NativeDaemonEvent event;
1604 try {
1605 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1606 } catch (NativeDaemonConnectorException e) {
1607 throw e.rethrowAsParcelableException();
1608 }
1609 event.checkCode(GetMarkResult);
1610 return Integer.parseInt(event.getMessage());
1611 }
1612
1613 @Override
1614 public int getMarkForProtect() {
1615 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1616 final NativeDaemonEvent event;
1617 try {
1618 event = mConnector.execute("interface", "fwmark", "get", "protect");
1619 } catch (NativeDaemonConnectorException e) {
1620 throw e.rethrowAsParcelableException();
1621 }
1622 event.checkCode(GetMarkResult);
1623 return Integer.parseInt(event.getMessage());
1624 }
1625
1626 @Override
1627 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1628 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1629 try {
1630 LinkAddress dest = route.getDestination();
1631 mConnector.execute("interface", "fwmark", "route", "add", iface,
1632 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1633 } catch (NativeDaemonConnectorException e) {
1634 throw e.rethrowAsParcelableException();
1635 }
1636 }
1637
1638 @Override
1639 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1640 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1641 try {
1642 LinkAddress dest = route.getDestination();
1643 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1644 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001645 } catch (NativeDaemonConnectorException e) {
1646 throw e.rethrowAsParcelableException();
1647 }
1648 }
1649
1650 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001651 public void setHostExemption(LinkAddress host) {
1652 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1653 try {
1654 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1655 } catch (NativeDaemonConnectorException e) {
1656 throw e.rethrowAsParcelableException();
1657 }
1658 }
1659
1660 @Override
1661 public void clearHostExemption(LinkAddress host) {
1662 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1663 try {
1664 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1665 } catch (NativeDaemonConnectorException e) {
1666 throw e.rethrowAsParcelableException();
1667 }
1668 }
1669
1670 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001671 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1672 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1673 try {
1674 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1675 } catch (NativeDaemonConnectorException e) {
1676 throw e.rethrowAsParcelableException();
1677 }
1678 }
1679
1680 @Override
1681 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1682 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1683 try {
1684 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1685 } catch (NativeDaemonConnectorException e) {
1686 throw e.rethrowAsParcelableException();
1687 }
1688 }
1689
1690 @Override
1691 public void clearDnsInterfaceMaps() {
1692 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1693 try {
1694 mConnector.execute("resolver", "clearifacemapping");
1695 } catch (NativeDaemonConnectorException e) {
1696 throw e.rethrowAsParcelableException();
1697 }
1698 }
1699
1700
1701 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001702 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001703 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001704 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001705 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001706 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001707 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001708 }
1709 }
1710
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001711 @Override
1712 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001713 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001714 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001715 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001716 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001717 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001718 }
1719 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001720
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001721 @Override
1722 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001723 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001724 try {
1725 mConnector.execute("firewall", enabled ? "enable" : "disable");
1726 mFirewallEnabled = enabled;
1727 } catch (NativeDaemonConnectorException e) {
1728 throw e.rethrowAsParcelableException();
1729 }
1730 }
1731
1732 @Override
1733 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001734 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001735 return mFirewallEnabled;
1736 }
1737
1738 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001739 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001740 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001741 Preconditions.checkState(mFirewallEnabled);
1742 final String rule = allow ? ALLOW : DENY;
1743 try {
1744 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1745 } catch (NativeDaemonConnectorException e) {
1746 throw e.rethrowAsParcelableException();
1747 }
1748 }
1749
1750 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001751 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001752 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001753 Preconditions.checkState(mFirewallEnabled);
1754 final String rule = allow ? ALLOW : DENY;
1755 try {
1756 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1757 } catch (NativeDaemonConnectorException e) {
1758 throw e.rethrowAsParcelableException();
1759 }
1760 }
1761
1762 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001763 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001764 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001765 Preconditions.checkState(mFirewallEnabled);
1766 final String rule = allow ? ALLOW : DENY;
1767 try {
1768 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1769 } catch (NativeDaemonConnectorException e) {
1770 throw e.rethrowAsParcelableException();
1771 }
1772 }
1773
1774 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001775 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001776 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001777 Preconditions.checkState(mFirewallEnabled);
1778 final String rule = allow ? ALLOW : DENY;
1779 try {
1780 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1781 } catch (NativeDaemonConnectorException e) {
1782 throw e.rethrowAsParcelableException();
1783 }
1784 }
1785
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001786 private static void enforceSystemUid() {
1787 final int uid = Binder.getCallingUid();
1788 if (uid != Process.SYSTEM_UID) {
1789 throw new SecurityException("Only available to AID_SYSTEM");
1790 }
1791 }
1792
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001793 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001794 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1795 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1796 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001797 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001798 } catch (NativeDaemonConnectorException e) {
1799 throw new IllegalStateException(
1800 "Error communicating with native deamon to set interface for pid" + iface, e);
1801 }
1802 }
1803
1804 @Override
1805 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1806 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1807 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001808 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001809 } catch (NativeDaemonConnectorException e) {
1810 throw new IllegalStateException(
1811 "Error communicating with native deamon to clear interface for pid " + pid, e);
1812 }
1813 }
1814
Lorenzo Colitti79751842013-02-28 16:16:03 +09001815 @Override
1816 public void startClatd(String interfaceName) throws IllegalStateException {
1817 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1818
1819 try {
1820 mConnector.execute("clatd", "start", interfaceName);
1821 } catch (NativeDaemonConnectorException e) {
1822 throw e.rethrowAsParcelableException();
1823 }
1824 }
1825
1826 @Override
1827 public void stopClatd() throws IllegalStateException {
1828 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1829
1830 try {
1831 mConnector.execute("clatd", "stop");
1832 } catch (NativeDaemonConnectorException e) {
1833 throw e.rethrowAsParcelableException();
1834 }
1835 }
1836
1837 @Override
1838 public boolean isClatdStarted() {
1839 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1840
1841 final NativeDaemonEvent event;
1842 try {
1843 event = mConnector.execute("clatd", "status");
1844 } catch (NativeDaemonConnectorException e) {
1845 throw e.rethrowAsParcelableException();
1846 }
1847
1848 event.checkCode(ClatdStatusResult);
1849 return event.getMessage().endsWith("started");
1850 }
1851
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001852 @Override
1853 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1854 mNetworkActivityListeners.register(listener);
1855 }
1856
1857 @Override
1858 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1859 mNetworkActivityListeners.unregister(listener);
1860 }
1861
1862 @Override
1863 public boolean isNetworkActive() {
1864 synchronized (mNetworkActivityListeners) {
1865 return mNetworkActive || mActiveIdleTimers.isEmpty();
1866 }
1867 }
1868
1869 private void reportNetworkActive() {
1870 final int length = mNetworkActivityListeners.beginBroadcast();
1871 for (int i = 0; i < length; i++) {
1872 try {
1873 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1874 } catch (RemoteException e) {
1875 } catch (RuntimeException e) {
1876 }
1877 }
1878 mNetworkActivityListeners.finishBroadcast();
1879 }
1880
Mattias Falk8b47b362011-08-23 14:15:13 +02001881 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001882 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001883 public void monitor() {
1884 if (mConnector != null) {
1885 mConnector.monitor();
1886 }
1887 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001888
1889 @Override
1890 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1891 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1892
Robert Greenwalt470fd722012-01-18 12:51:15 -08001893 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1894 mConnector.dump(fd, pw, args);
1895 pw.println();
1896
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001897 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1898
1899 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001900 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1901 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001902 }
1903
1904 synchronized (mUidRejectOnQuota) {
1905 pw.print("UID reject on quota ifaces: [");
1906 final int size = mUidRejectOnQuota.size();
1907 for (int i = 0; i < size; i++) {
1908 pw.print(mUidRejectOnQuota.keyAt(i));
1909 if (i < size - 1) pw.print(",");
1910 }
1911 pw.println("]");
1912 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001913
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001914 synchronized (mIdleTimerLock) {
1915 pw.println("Idle timers:");
1916 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1917 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1918 IdleTimerParams params = ent.getValue();
1919 pw.print(" timeout="); pw.print(params.timeout);
1920 pw.print(" type="); pw.print(params.type);
1921 pw.print(" networkCount="); pw.println(params.networkCount);
1922 }
1923 }
1924
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001925 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001926 }
San Mehat873f2142010-01-14 10:25:07 -08001927}