blob: a09d6059f2813920fd380f7b26f774905dd3215a [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 */
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700349 private void notifyInterfaceClassActivity(int type, boolean active, long tsNanos) {
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(
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700359 Integer.toString(type), active, tsNanos);
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 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700574 long timestampNanos = 0;
575 if (cooked.length == 5) {
576 try {
577 timestampNanos = Long.parseLong(cooked[4]);
578 } catch(NumberFormatException ne) {}
579 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700580 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700581 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
582 isActive, timestampNanos);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700583 return true;
584 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900585 case NetdResponseCode.InterfaceAddressChange:
586 /*
587 * A network address change occurred
588 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
589 * "NNN Address removed <addr> <iface> <flags> <scope>"
590 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900591 if (cooked.length < 7 || !cooked[1].equals("Address")) {
592 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900593 }
594
Lorenzo Colitti64483942013-11-15 18:43:52 +0900595 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900596 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900597 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900598 int flags = Integer.parseInt(cooked[5]);
599 int scope = Integer.parseInt(cooked[6]);
600 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900601 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
602 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900603 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900604 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900605 }
606
607 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900608 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900609 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900610 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900611 }
612 return true;
613 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900614 case NetdResponseCode.InterfaceDnsServerInfo:
615 /*
616 * Information about available DNS servers has been received.
617 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
618 */
619 long lifetime; // Actually a 32-bit unsigned integer.
620
621 if (cooked.length == 6 &&
622 cooked[1].equals("DnsInfo") &&
623 cooked[2].equals("servers")) {
624 try {
625 lifetime = Long.parseLong(cooked[4]);
626 } catch (NumberFormatException e) {
627 throw new IllegalStateException(errorMessage);
628 }
629 String[] servers = cooked[5].split(",");
630 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
631 }
632 return true;
633 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700634 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800635 }
636 return false;
San Mehat873f2142010-01-14 10:25:07 -0800637 }
638 }
639
San Mehated4fc8a2010-01-22 12:28:36 -0800640
San Mehat873f2142010-01-14 10:25:07 -0800641 //
642 // INetworkManagementService members
643 //
644
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800645 @Override
646 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800647 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700648 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800649 return NativeDaemonEvent.filterMessageList(
650 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700651 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800652 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700653 }
San Mehated4fc8a2010-01-22 12:28:36 -0800654 }
655
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800656 @Override
657 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800658 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800659
660 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700661 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800662 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700663 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800664 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700665 }
San Mehated4fc8a2010-01-22 12:28:36 -0800666
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800667 event.checkCode(InterfaceGetCfgResult);
668
669 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
670 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800671
Kenny Roota80ce062010-06-01 13:23:53 -0700672 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800673 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700674 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800675 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800676 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800677 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700678 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800679 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800680 } catch (IllegalArgumentException iae) {
681 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700682 }
683
684 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800685 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800686 } catch (NumberFormatException nfe) {
687 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700688 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800689
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800690 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
691 while (st.hasMoreTokens()) {
692 cfg.setFlag(st.nextToken());
693 }
Kenny Roota80ce062010-06-01 13:23:53 -0700694 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800695 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800696 }
San Mehated4fc8a2010-01-22 12:28:36 -0800697 return cfg;
698 }
699
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800700 @Override
701 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800702 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800703 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800704 if (linkAddr == null || linkAddr.getAddress() == null) {
705 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800706 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800707
708 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800709 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800710 linkAddr.getNetworkPrefixLength());
711 for (String flag : cfg.getFlags()) {
712 cmd.appendArg(flag);
713 }
714
Kenny Roota80ce062010-06-01 13:23:53 -0700715 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800716 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700717 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800718 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700719 }
San Mehat873f2142010-01-14 10:25:07 -0800720 }
721
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800722 @Override
723 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800724 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800725 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800726 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800727 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700728 }
729
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800730 @Override
731 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800732 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800733 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800734 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800735 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700736 }
737
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800738 @Override
739 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800740 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700741 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800742 mConnector.execute(
743 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700744 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800745 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700746 }
747 }
748
Irfan Sherifff5600612011-06-16 10:26:28 -0700749 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
750 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800751 @Override
752 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800753 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700754 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800755 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700756 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800757 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700758 }
759 }
760
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800761 @Override
762 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800763 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700764 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800765 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700766 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800767 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700768 }
769 }
770
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800771 @Override
772 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800773 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700774 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800775 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700776 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800777 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700778 }
779 }
780
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800781 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700782 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800783 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700784 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700785 }
786
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800787 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700788 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800789 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700790 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700791 }
792
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800793 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700794 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800795 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700796 modifyRoute(interfaceName, ADD, route, SECONDARY);
797 }
798
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800799 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700800 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800801 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700802 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
803 }
804
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
806 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700807
808 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800809 final LinkAddress la = route.getDestination();
810 cmd.appendArg(la.getAddress().getHostAddress());
811 cmd.appendArg(la.getNetworkPrefixLength());
812
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700813 if (route.getGateway() == null) {
814 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800815 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700816 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800817 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700818 }
819 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800820 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700821 }
822
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 try {
824 mConnector.execute(cmd);
825 } catch (NativeDaemonConnectorException e) {
826 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700827 }
828 }
829
830 private ArrayList<String> readRouteList(String filename) {
831 FileInputStream fstream = null;
832 ArrayList<String> list = new ArrayList<String>();
833
834 try {
835 fstream = new FileInputStream(filename);
836 DataInputStream in = new DataInputStream(fstream);
837 BufferedReader br = new BufferedReader(new InputStreamReader(in));
838 String s;
839
840 // throw away the title line
841
842 while (((s = br.readLine()) != null) && (s.length() != 0)) {
843 list.add(s);
844 }
845 } catch (IOException ex) {
846 // return current list, possibly empty
847 } finally {
848 if (fstream != null) {
849 try {
850 fstream.close();
851 } catch (IOException ex) {}
852 }
853 }
854
855 return list;
856 }
857
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800858 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700859 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800860 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700861 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
862
863 // v4 routes listed as:
864 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
865 for (String s : readRouteList("/proc/net/route")) {
866 String[] fields = s.split("\t");
867
868 if (fields.length > 7) {
869 String iface = fields[0];
870
871 if (interfaceName.equals(iface)) {
872 String dest = fields[1];
873 String gate = fields[2];
874 String flags = fields[3]; // future use?
875 String mask = fields[7];
876 try {
877 // address stored as a hex string, ex: 0014A8C0
878 InetAddress destAddr =
879 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
880 int prefixLength =
881 NetworkUtils.netmaskIntToPrefixLength(
882 (int)Long.parseLong(mask, 16));
883 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
884
885 // address stored as a hex string, ex 0014A8C0
886 InetAddress gatewayAddr =
887 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
888
Wink Saville7b5fd052013-03-15 05:07:04 +0000889 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700890 routes.add(route);
891 } catch (Exception e) {
892 Log.e(TAG, "Error parsing route " + s + " : " + e);
893 continue;
894 }
895 }
896 }
897 }
898
899 // v6 routes listed as:
900 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
901 for (String s : readRouteList("/proc/net/ipv6_route")) {
902 String[]fields = s.split("\\s+");
903 if (fields.length > 9) {
904 String iface = fields[9].trim();
905 if (interfaceName.equals(iface)) {
906 String dest = fields[0];
907 String prefix = fields[1];
908 String gate = fields[4];
909
910 try {
911 // prefix length stored as a hex string, ex 40
912 int prefixLength = Integer.parseInt(prefix, 16);
913
914 // address stored as a 32 char hex string
915 // ex fe800000000000000000000000000000
916 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
917 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
918
919 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
920
Wink Saville7b5fd052013-03-15 05:07:04 +0000921 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700922 routes.add(route);
923 } catch (Exception e) {
924 Log.e(TAG, "Error parsing route " + s + " : " + e);
925 continue;
926 }
927 }
928 }
929 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800930 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700931 }
932
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800933 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900934 public void setMtu(String iface, int mtu) {
935 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
936
937 final NativeDaemonEvent event;
938 try {
939 event = mConnector.execute("interface", "setmtu", iface, mtu);
940 } catch (NativeDaemonConnectorException e) {
941 throw e.rethrowAsParcelableException();
942 }
943 }
944
945 @Override
San Mehat873f2142010-01-14 10:25:07 -0800946 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 // TODO: remove from aidl if nobody calls externally
948 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800949
Joe Onorato8a9b2202010-02-26 18:56:32 -0800950 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800951 }
952
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800953 @Override
San Mehat873f2142010-01-14 10:25:07 -0800954 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800955 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800956
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800957 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700958 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800959 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700960 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800961 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700962 }
San Mehat873f2142010-01-14 10:25:07 -0800963
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800964 // 211 Forwarding enabled
965 event.checkCode(IpFwdStatusResult);
966 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800967 }
968
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800969 @Override
970 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800971 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800972 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800973 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800974 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800975 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800976 }
San Mehat873f2142010-01-14 10:25:07 -0800977 }
978
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800979 @Override
980 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800981 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700982 // cmd is "tether start first_start first_stop second_start second_stop ..."
983 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800984
985 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700986 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800987 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700988 }
Kenny Roota80ce062010-06-01 13:23:53 -0700989
990 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800991 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700992 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800993 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700994 }
San Mehat873f2142010-01-14 10:25:07 -0800995 }
996
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800997 @Override
998 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800999 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001000 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001001 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001002 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001003 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001004 }
San Mehat873f2142010-01-14 10:25:07 -08001005 }
1006
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001007 @Override
1008 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001009 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001010
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001011 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001012 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001013 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001014 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001015 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001016 }
San Mehat873f2142010-01-14 10:25:07 -08001017
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001018 // 210 Tethering services started
1019 event.checkCode(TetherStatusResult);
1020 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001021 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001022
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001023 @Override
1024 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001026 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001028 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001029 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001030 }
San Mehat873f2142010-01-14 10:25:07 -08001031 }
1032
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001033 @Override
San Mehat873f2142010-01-14 10:25:07 -08001034 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001035 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001036 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001037 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001038 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001039 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001040 }
San Mehat873f2142010-01-14 10:25:07 -08001041 }
1042
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001043 @Override
1044 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001045 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001046 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001047 return NativeDaemonEvent.filterMessageList(
1048 mConnector.executeForList("tether", "interface", "list"),
1049 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001050 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001051 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001052 }
San Mehat873f2142010-01-14 10:25:07 -08001053 }
1054
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001055 @Override
1056 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001057 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001058
1059 final Command cmd = new Command("tether", "dns", "set");
1060 for (String s : dns) {
1061 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1062 }
1063
San Mehat873f2142010-01-14 10:25:07 -08001064 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001065 mConnector.execute(cmd);
1066 } catch (NativeDaemonConnectorException e) {
1067 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001068 }
1069 }
1070
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001071 @Override
1072 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001073 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001074 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001075 return NativeDaemonEvent.filterMessageList(
1076 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001077 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001078 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001079 }
San Mehat873f2142010-01-14 10:25:07 -08001080 }
1081
jiaguo1da35f72014-01-09 16:39:59 +08001082 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1083 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1084 for (InterfaceAddress ia : addresses) {
1085 if (!ia.getAddress().isLinkLocalAddress())
1086 filtered.add(ia);
1087 }
1088 return filtered;
1089 }
1090
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001091 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001092 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001093 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001094
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001095 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1096 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001097 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001098 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001099 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001100 // Don't touch link-local routes, as link-local addresses aren't routable,
1101 // kernel creates link-local routes on all interfaces automatically
1102 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1103 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001104 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001105 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 InetAddress addr = NetworkUtils.getNetworkPart(
1107 ia.getAddress(), ia.getNetworkPrefixLength());
1108 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001109 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001110 }
1111
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001112 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001113 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001114 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001115 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001116 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001117 }
1118
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001119 @Override
1120 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001121 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001122 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001123 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001124 } catch (SocketException e) {
1125 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001126 }
San Mehat873f2142010-01-14 10:25:07 -08001127 }
1128
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001129 @Override
1130 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001131 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001132 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001133 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001134 } catch (SocketException e) {
1135 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001136 }
San Mehat873f2142010-01-14 10:25:07 -08001137 }
San Mehat72759df2010-01-19 13:50:37 -08001138
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001139 @Override
1140 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001142 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 return NativeDaemonEvent.filterMessageList(
1144 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001145 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001146 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001147 }
San Mehat72759df2010-01-19 13:50:37 -08001148 }
1149
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001150 @Override
1151 public void attachPppd(
1152 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001153 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001154 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001155 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001156 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1157 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1158 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001159 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001160 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001161 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001162 }
1163 }
1164
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001165 @Override
1166 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001167 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001168 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001169 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001170 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001171 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001172 }
San Mehat72759df2010-01-19 13:50:37 -08001173 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001174
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001175 @Override
1176 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001177 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001178 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001179 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001180 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001181 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001182 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001183 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001184 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001185 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001186 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001187 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001188 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001189 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001190 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001191 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001192 }
1193
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001194 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001195 switch (wifiConfig.getAuthType()) {
1196 case KeyMgmt.WPA_PSK:
1197 return "wpa-psk";
1198 case KeyMgmt.WPA2_PSK:
1199 return "wpa2-psk";
1200 default:
1201 return "open";
1202 }
1203 }
1204
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001205 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001206 @Override
1207 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001208 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001209 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001210 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001211 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001212 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001213 }
1214 }
1215
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001216 @Override
1217 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001218 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001219 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001220 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001221 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001222 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001223 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001224 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001225 }
1226
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001227 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001228 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001229 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001230 try {
1231 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001232 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001233 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001234 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001235 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001236 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001237 }
1238 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001239 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001240 }
1241 }
San Mehat91cac642010-03-31 14:31:36 -07001242
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001243 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001244 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001245 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1246
1247 if (DBG) Slog.d(TAG, "Adding idletimer");
1248
1249 synchronized (mIdleTimerLock) {
1250 IdleTimerParams params = mActiveIdleTimers.get(iface);
1251 if (params != null) {
1252 // the interface already has idletimer, update network count
1253 params.networkCount++;
1254 return;
1255 }
1256
1257 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001258 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1259 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001260 } catch (NativeDaemonConnectorException e) {
1261 throw e.rethrowAsParcelableException();
1262 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001263 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1264
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001265 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001266 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1267 mNetworkActive = false;
1268 }
1269 mMainHandler.post(new Runnable() {
1270 @Override public void run() {
Ashish Sharma0535a9f2014-03-12 18:42:23 -07001271 notifyInterfaceClassActivity(type, true, SystemClock.elapsedRealtimeNanos());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001272 }
1273 });
Haoyu Bai04124232012-06-28 15:26:19 -07001274 }
1275 }
1276
1277 @Override
1278 public void removeIdleTimer(String iface) {
1279 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1280
1281 if (DBG) Slog.d(TAG, "Removing idletimer");
1282
1283 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001284 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001285 if (params == null || --(params.networkCount) > 0) {
1286 return;
1287 }
1288
1289 try {
1290 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001291 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001292 } catch (NativeDaemonConnectorException e) {
1293 throw e.rethrowAsParcelableException();
1294 }
1295 mActiveIdleTimers.remove(iface);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001296 mMainHandler.post(new Runnable() {
1297 @Override public void run() {
Ashish Sharma0535a9f2014-03-12 18:42:23 -07001298 notifyInterfaceClassActivity(params.type, false,
1299 SystemClock.elapsedRealtimeNanos());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001300 }
1301 });
Haoyu Bai04124232012-06-28 15:26:19 -07001302 }
1303 }
1304
1305 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001306 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001307 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001308 try {
1309 return mStatsFactory.readNetworkStatsSummaryDev();
1310 } catch (IOException e) {
1311 throw new IllegalStateException(e);
1312 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001313 }
1314
1315 @Override
1316 public NetworkStats getNetworkStatsSummaryXt() {
1317 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001318 try {
1319 return mStatsFactory.readNetworkStatsSummaryXt();
1320 } catch (IOException e) {
1321 throw new IllegalStateException(e);
1322 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001323 }
1324
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001325 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001326 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001327 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001328 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001329 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001330 } catch (IOException e) {
1331 throw new IllegalStateException(e);
1332 }
San Mehat91cac642010-03-31 14:31:36 -07001333 }
1334
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001335 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001336 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001337 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001338
Jeff Sharkey350083e2011-06-29 10:45:16 -07001339 // silently discard when control disabled
1340 // TODO: eventually migrate to be always enabled
1341 if (!mBandwidthControlEnabled) return;
1342
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001343 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001344 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001345 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001346 }
1347
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001348 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001349 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001350 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001351 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001352 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001353 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001354 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001355 }
1356 }
1357
1358 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001359 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001360 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001361
Jeff Sharkey350083e2011-06-29 10:45:16 -07001362 // silently discard when control disabled
1363 // TODO: eventually migrate to be always enabled
1364 if (!mBandwidthControlEnabled) return;
1365
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001366 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001367 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001368 // TODO: eventually consider throwing
1369 return;
1370 }
1371
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001372 mActiveQuotas.remove(iface);
1373 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001374
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001375 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001376 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001377 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001378 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001379 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001380 }
1381 }
1382 }
1383
1384 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001385 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001386 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001387
1388 // silently discard when control disabled
1389 // TODO: eventually migrate to be always enabled
1390 if (!mBandwidthControlEnabled) return;
1391
1392 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001393 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001394 throw new IllegalStateException("setting alert requires existing quota on iface");
1395 }
1396
1397 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001398 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001399 throw new IllegalStateException("iface " + iface + " already has alert");
1400 }
1401
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001402 try {
1403 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001404 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001405 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001406 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001407 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001408 }
1409 }
1410 }
1411
1412 @Override
1413 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001414 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001415
1416 // silently discard when control disabled
1417 // TODO: eventually migrate to be always enabled
1418 if (!mBandwidthControlEnabled) return;
1419
1420 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001421 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001422 // TODO: eventually consider throwing
1423 return;
1424 }
1425
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001426 try {
1427 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001428 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001429 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001430 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001431 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001432 }
1433 }
1434 }
1435
1436 @Override
1437 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001438 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001439
1440 // silently discard when control disabled
1441 // TODO: eventually migrate to be always enabled
1442 if (!mBandwidthControlEnabled) return;
1443
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001444 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001445 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001446 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001447 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001448 }
1449 }
1450
1451 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001452 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001453 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001454
Jeff Sharkey350083e2011-06-29 10:45:16 -07001455 // silently discard when control disabled
1456 // TODO: eventually migrate to be always enabled
1457 if (!mBandwidthControlEnabled) return;
1458
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001459 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001460 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1461 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1462 // TODO: eventually consider throwing
1463 return;
1464 }
1465
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001466 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001467 mConnector.execute("bandwidth",
1468 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001469 if (rejectOnQuotaInterfaces) {
1470 mUidRejectOnQuota.put(uid, true);
1471 } else {
1472 mUidRejectOnQuota.delete(uid);
1473 }
1474 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001475 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001476 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001477 }
1478 }
1479
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001480 @Override
1481 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001482 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001483 return mBandwidthControlEnabled;
1484 }
1485
1486 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001487 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001488 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001489 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001490 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001491 } catch (IOException e) {
1492 throw new IllegalStateException(e);
1493 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001494 }
1495
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001496 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001497 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001498 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001499
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001500 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001501 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001502 final NativeDaemonEvent[] events = mConnector.executeForList(
1503 "bandwidth", "gettetherstats");
1504 for (NativeDaemonEvent event : events) {
1505 if (event.getCode() != TetheringStatsListResult) continue;
1506
1507 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1508 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1509 try {
1510 final String ifaceIn = tok.nextToken();
1511 final String ifaceOut = tok.nextToken();
1512
1513 final NetworkStats.Entry entry = new NetworkStats.Entry();
1514 entry.iface = ifaceOut;
1515 entry.uid = UID_TETHERING;
1516 entry.set = SET_DEFAULT;
1517 entry.tag = TAG_NONE;
1518 entry.rxBytes = Long.parseLong(tok.nextToken());
1519 entry.rxPackets = Long.parseLong(tok.nextToken());
1520 entry.txBytes = Long.parseLong(tok.nextToken());
1521 entry.txPackets = Long.parseLong(tok.nextToken());
1522 stats.combineValues(entry);
1523 } catch (NoSuchElementException e) {
1524 throw new IllegalStateException("problem parsing tethering stats: " + event);
1525 } catch (NumberFormatException e) {
1526 throw new IllegalStateException("problem parsing tethering stats: " + event);
1527 }
1528 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001529 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001530 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001531 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001532 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001533 }
1534
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001535 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001536 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001537 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001538 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001539 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001540 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001541 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001542 }
1543 }
1544
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001545 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001546 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001547 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001548
Robert Greenwalt8058f622012-11-09 10:52:27 -08001549 final Command cmd = new Command("resolver", "setifdns", iface,
1550 (domains == null ? "" : domains));
1551
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001552 for (String s : servers) {
1553 InetAddress a = NetworkUtils.numericToInetAddress(s);
1554 if (a.isAnyLocalAddress() == false) {
1555 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001556 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001557 }
1558
1559 try {
1560 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001561 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001562 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001563 }
1564 }
1565
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001566 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001567 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1568 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1569 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001570 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001571 "uid", "add", iface, uid_start, uid_end);
1572 } catch (NativeDaemonConnectorException e) {
1573 throw e.rethrowAsParcelableException();
1574 }
1575 }
1576
1577 @Override
1578 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1579 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1580 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001581 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001582 "uid", "remove", iface, uid_start, uid_end);
1583 } catch (NativeDaemonConnectorException e) {
1584 throw e.rethrowAsParcelableException();
1585 }
1586 }
1587
1588 @Override
1589 public void setMarkedForwarding(String iface) {
1590 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1591 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001592 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001593 } catch (NativeDaemonConnectorException e) {
1594 throw e.rethrowAsParcelableException();
1595 }
1596 }
1597
1598 @Override
1599 public void clearMarkedForwarding(String iface) {
1600 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1601 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001602 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1603 } catch (NativeDaemonConnectorException e) {
1604 throw e.rethrowAsParcelableException();
1605 }
1606 }
1607
1608 @Override
1609 public int getMarkForUid(int uid) {
1610 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1611 final NativeDaemonEvent event;
1612 try {
1613 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1614 } catch (NativeDaemonConnectorException e) {
1615 throw e.rethrowAsParcelableException();
1616 }
1617 event.checkCode(GetMarkResult);
1618 return Integer.parseInt(event.getMessage());
1619 }
1620
1621 @Override
1622 public int getMarkForProtect() {
1623 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1624 final NativeDaemonEvent event;
1625 try {
1626 event = mConnector.execute("interface", "fwmark", "get", "protect");
1627 } catch (NativeDaemonConnectorException e) {
1628 throw e.rethrowAsParcelableException();
1629 }
1630 event.checkCode(GetMarkResult);
1631 return Integer.parseInt(event.getMessage());
1632 }
1633
1634 @Override
1635 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1636 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1637 try {
1638 LinkAddress dest = route.getDestination();
1639 mConnector.execute("interface", "fwmark", "route", "add", iface,
1640 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1641 } catch (NativeDaemonConnectorException e) {
1642 throw e.rethrowAsParcelableException();
1643 }
1644 }
1645
1646 @Override
1647 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1648 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1649 try {
1650 LinkAddress dest = route.getDestination();
1651 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1652 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001653 } catch (NativeDaemonConnectorException e) {
1654 throw e.rethrowAsParcelableException();
1655 }
1656 }
1657
1658 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001659 public void setHostExemption(LinkAddress host) {
1660 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1661 try {
1662 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1663 } catch (NativeDaemonConnectorException e) {
1664 throw e.rethrowAsParcelableException();
1665 }
1666 }
1667
1668 @Override
1669 public void clearHostExemption(LinkAddress host) {
1670 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1671 try {
1672 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1673 } catch (NativeDaemonConnectorException e) {
1674 throw e.rethrowAsParcelableException();
1675 }
1676 }
1677
1678 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001679 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1680 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1681 try {
1682 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1683 } catch (NativeDaemonConnectorException e) {
1684 throw e.rethrowAsParcelableException();
1685 }
1686 }
1687
1688 @Override
1689 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1690 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1691 try {
1692 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1693 } catch (NativeDaemonConnectorException e) {
1694 throw e.rethrowAsParcelableException();
1695 }
1696 }
1697
1698 @Override
1699 public void clearDnsInterfaceMaps() {
1700 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1701 try {
1702 mConnector.execute("resolver", "clearifacemapping");
1703 } catch (NativeDaemonConnectorException e) {
1704 throw e.rethrowAsParcelableException();
1705 }
1706 }
1707
1708
1709 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001710 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001711 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001712 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001713 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001714 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001715 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001716 }
1717 }
1718
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001719 @Override
1720 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001721 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001722 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001723 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001724 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001725 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001726 }
1727 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001728
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001729 @Override
1730 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001731 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001732 try {
1733 mConnector.execute("firewall", enabled ? "enable" : "disable");
1734 mFirewallEnabled = enabled;
1735 } catch (NativeDaemonConnectorException e) {
1736 throw e.rethrowAsParcelableException();
1737 }
1738 }
1739
1740 @Override
1741 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001742 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001743 return mFirewallEnabled;
1744 }
1745
1746 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001747 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001748 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001749 Preconditions.checkState(mFirewallEnabled);
1750 final String rule = allow ? ALLOW : DENY;
1751 try {
1752 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1753 } catch (NativeDaemonConnectorException e) {
1754 throw e.rethrowAsParcelableException();
1755 }
1756 }
1757
1758 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001759 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001760 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001761 Preconditions.checkState(mFirewallEnabled);
1762 final String rule = allow ? ALLOW : DENY;
1763 try {
1764 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1765 } catch (NativeDaemonConnectorException e) {
1766 throw e.rethrowAsParcelableException();
1767 }
1768 }
1769
1770 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001771 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001772 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001773 Preconditions.checkState(mFirewallEnabled);
1774 final String rule = allow ? ALLOW : DENY;
1775 try {
1776 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1777 } catch (NativeDaemonConnectorException e) {
1778 throw e.rethrowAsParcelableException();
1779 }
1780 }
1781
1782 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001783 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001784 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001785 Preconditions.checkState(mFirewallEnabled);
1786 final String rule = allow ? ALLOW : DENY;
1787 try {
1788 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1789 } catch (NativeDaemonConnectorException e) {
1790 throw e.rethrowAsParcelableException();
1791 }
1792 }
1793
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001794 private static void enforceSystemUid() {
1795 final int uid = Binder.getCallingUid();
1796 if (uid != Process.SYSTEM_UID) {
1797 throw new SecurityException("Only available to AID_SYSTEM");
1798 }
1799 }
1800
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001801 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001802 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1803 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1804 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001805 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001806 } catch (NativeDaemonConnectorException e) {
1807 throw new IllegalStateException(
1808 "Error communicating with native deamon to set interface for pid" + iface, e);
1809 }
1810 }
1811
1812 @Override
1813 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1814 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1815 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001816 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001817 } catch (NativeDaemonConnectorException e) {
1818 throw new IllegalStateException(
1819 "Error communicating with native deamon to clear interface for pid " + pid, e);
1820 }
1821 }
1822
Lorenzo Colitti79751842013-02-28 16:16:03 +09001823 @Override
1824 public void startClatd(String interfaceName) throws IllegalStateException {
1825 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1826
1827 try {
1828 mConnector.execute("clatd", "start", interfaceName);
1829 } catch (NativeDaemonConnectorException e) {
1830 throw e.rethrowAsParcelableException();
1831 }
1832 }
1833
1834 @Override
1835 public void stopClatd() throws IllegalStateException {
1836 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1837
1838 try {
1839 mConnector.execute("clatd", "stop");
1840 } catch (NativeDaemonConnectorException e) {
1841 throw e.rethrowAsParcelableException();
1842 }
1843 }
1844
1845 @Override
1846 public boolean isClatdStarted() {
1847 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1848
1849 final NativeDaemonEvent event;
1850 try {
1851 event = mConnector.execute("clatd", "status");
1852 } catch (NativeDaemonConnectorException e) {
1853 throw e.rethrowAsParcelableException();
1854 }
1855
1856 event.checkCode(ClatdStatusResult);
1857 return event.getMessage().endsWith("started");
1858 }
1859
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001860 @Override
1861 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1862 mNetworkActivityListeners.register(listener);
1863 }
1864
1865 @Override
1866 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1867 mNetworkActivityListeners.unregister(listener);
1868 }
1869
1870 @Override
1871 public boolean isNetworkActive() {
1872 synchronized (mNetworkActivityListeners) {
1873 return mNetworkActive || mActiveIdleTimers.isEmpty();
1874 }
1875 }
1876
1877 private void reportNetworkActive() {
1878 final int length = mNetworkActivityListeners.beginBroadcast();
1879 for (int i = 0; i < length; i++) {
1880 try {
1881 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1882 } catch (RemoteException e) {
1883 } catch (RuntimeException e) {
1884 }
1885 }
1886 mNetworkActivityListeners.finishBroadcast();
1887 }
1888
Mattias Falk8b47b362011-08-23 14:15:13 +02001889 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001890 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001891 public void monitor() {
1892 if (mConnector != null) {
1893 mConnector.monitor();
1894 }
1895 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001896
1897 @Override
1898 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1899 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1900
Robert Greenwalt470fd722012-01-18 12:51:15 -08001901 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1902 mConnector.dump(fd, pw, args);
1903 pw.println();
1904
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001905 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1906
1907 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001908 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1909 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001910 }
1911
1912 synchronized (mUidRejectOnQuota) {
1913 pw.print("UID reject on quota ifaces: [");
1914 final int size = mUidRejectOnQuota.size();
1915 for (int i = 0; i < size; i++) {
1916 pw.print(mUidRejectOnQuota.keyAt(i));
1917 if (i < size - 1) pw.print(",");
1918 }
1919 pw.println("]");
1920 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001921
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001922 synchronized (mIdleTimerLock) {
1923 pw.println("Idle timers:");
1924 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1925 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1926 IdleTimerParams params = ent.getValue();
1927 pw.print(" timeout="); pw.print(params.timeout);
1928 pw.print(" type="); pw.print(params.type);
1929 pw.print(" networkCount="); pw.println(params.networkCount);
1930 }
1931 }
1932
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001933 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001934 }
San Mehat873f2142010-01-14 10:25:07 -08001935}