blob: 70222944b2a676e09415aa651ce930e7bffda771 [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -070019import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkey4529bb62011-12-14 10:31:54 -080020import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070021import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080022import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070023import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080024import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.NetworkStats.TAG_NONE;
26import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070027import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090028import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070029import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080032import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070036import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080037import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070038import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070039
San Mehat873f2142010-01-14 10:25:07 -080040import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080041import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080042import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090044import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070046import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080047import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070048import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080049import android.net.wifi.WifiConfiguration;
50import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070051import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070052import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070053import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080054import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080055import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080056import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070057import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080058import android.os.RemoteCallbackList;
59import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070060import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070061import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080062import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070063import android.telephony.DataConnectionRealTimeInfo;
64import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070065import android.telephony.SubscriptionManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080066import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080067import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070068import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080069
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070070import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070071import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070072import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080073import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070074import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070075import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070076import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070077
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070078import java.io.BufferedReader;
79import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080080import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070081import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070082import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070084import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070085import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070086import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070087import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070088import java.net.InterfaceAddress;
89import java.net.NetworkInterface;
90import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070091import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070092import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080093import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070094import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070095import java.util.NoSuchElementException;
96import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070097import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080098
99/**
100 * @hide
101 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700102public class NetworkManagementService extends INetworkManagementService.Stub
103 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700104 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700105 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700106 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900107 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700108
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800109 private static final String ADD = "add";
110 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700111
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700112 private static final String ALLOW = "allow";
113 private static final String DENY = "deny";
114
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700115 private static final String DEFAULT = "default";
116 private static final String SECONDARY = "secondary";
117
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700118 /**
119 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
120 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
121 */
122 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
123
San Mehat873f2142010-01-14 10:25:07 -0800124 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700125 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800126 public static final int InterfaceListResult = 110;
127 public static final int TetherInterfaceListResult = 111;
128 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800129 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700130 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800131
132 public static final int TetherStatusResult = 210;
133 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800134 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800135 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700136 public static final int InterfaceRxCounterResult = 216;
137 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700138 public static final int QuotaCounterResult = 220;
139 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800140 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900141 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700142 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800143
144 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700145 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700146 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900147 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900148 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900149 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800150 }
151
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700152 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
153
San Mehat873f2142010-01-14 10:25:07 -0800154 /**
155 * Binder context for this service
156 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700157 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800158
159 /**
160 * connector object for communicating with netd
161 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700162 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800163
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700164 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700165 private final Handler mDaemonHandler;
166 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700167
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800168 private IBatteryStats mBatteryStats;
169
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700170 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700171 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700172
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800173 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
174 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800175
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700176 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
177
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700178 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700179 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700180 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700181 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700182 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700183 /** Set of UIDs with active reject rules. */
184 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
185
Haoyu Bai04124232012-06-28 15:26:19 -0700186 private Object mIdleTimerLock = new Object();
187 /** Set of interfaces with active idle timers. */
188 private static class IdleTimerParams {
189 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800190 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700191 public int networkCount;
192
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800193 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700194 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800195 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700196 this.networkCount = 1;
197 }
198 }
199 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
200
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700201 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700202 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700203
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700204 private boolean mMobileActivityFromRadio = false;
205 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
206
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800207 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
208 new RemoteCallbackList<INetworkActivityListener>();
209 private boolean mNetworkActive;
210
San Mehat873f2142010-01-14 10:25:07 -0800211 /**
212 * Constructs a new NetworkManagementService instance
213 *
214 * @param context Binder context for this service
215 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900216 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800217 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800218
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700219 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
220 mFgHandler = new Handler(FgThread.get().getLooper());
221
Marco Nelissen62dbb222010-02-18 10:56:30 -0800222 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700223 mConnector = null;
224 mThread = null;
225 mDaemonHandler = null;
226 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800227 return;
228 }
229
Dianne Hackborn4590e522014-03-24 13:36:46 -0700230 // Don't need this wake lock, since we now have a time stamp for when
231 // the network actually went inactive. (It might be nice to still do this,
232 // but I don't want to do it through the power manager because that pollutes the
233 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700234 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700235 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800236
San Mehat873f2142010-01-14 10:25:07 -0800237 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700238 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
239 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700240 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700241
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700242 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Savillefb40dd42014-06-12 17:02:31 -0700243 mPhoneStateListener = new PhoneStateListener(
244 SubscriptionManager.DEFAULT_SUB_ID, // FIXME: What Subscription should be used??
245 mDaemonHandler.getLooper()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700246 public void onDataConnectionRealTimeInfoChanged(
247 DataConnectionRealTimeInfo dcRtInfo) {
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700248 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
249 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700250 }
251 };
252
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700253 // Add ourself to the Watchdog monitors.
254 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700255 }
256
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900257 static NetworkManagementService create(Context context,
258 String socket) throws InterruptedException {
259 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700260 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700261 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
262 service.mThread.start();
263 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700264 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700265 if (DBG) Slog.d(TAG, "Connected");
266 return service;
San Mehat873f2142010-01-14 10:25:07 -0800267 }
268
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900269 public static NetworkManagementService create(Context context) throws InterruptedException {
270 return create(context, NETD_SOCKET_NAME);
271 }
272
Jeff Sharkey350083e2011-06-29 10:45:16 -0700273 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700274 prepareNativeDaemon();
275 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700276 }
277
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800278 private IBatteryStats getBatteryStats() {
279 synchronized (this) {
280 if (mBatteryStats != null) {
281 return mBatteryStats;
282 }
283 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
284 BatteryStats.SERVICE_NAME));
285 return mBatteryStats;
286 }
287 }
288
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800289 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800291 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800292 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800293 }
294
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800295 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800296 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800297 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800298 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800299 }
300
301 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700302 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800303 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700304 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800305 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700306 try {
307 for (int i = 0; i < length; i++) {
308 try {
309 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
310 } catch (RemoteException e) {
311 } catch (RuntimeException e) {
312 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700313 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700314 } finally {
315 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700316 }
317 }
318
319 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700320 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700321 * (typically, an Ethernet cable has been plugged-in or unplugged).
322 */
323 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800324 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700325 try {
326 for (int i = 0; i < length; i++) {
327 try {
328 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
329 } catch (RemoteException e) {
330 } catch (RuntimeException e) {
331 }
San Mehat4d02d002010-01-22 16:07:46 -0800332 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700333 } finally {
334 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800335 }
336 }
337
338 /**
339 * Notify our observers of an interface addition.
340 */
341 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800342 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700343 try {
344 for (int i = 0; i < length; i++) {
345 try {
346 mObservers.getBroadcastItem(i).interfaceAdded(iface);
347 } catch (RemoteException e) {
348 } catch (RuntimeException e) {
349 }
San Mehat4d02d002010-01-22 16:07:46 -0800350 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700351 } finally {
352 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800353 }
354 }
355
356 /**
357 * Notify our observers of an interface removal.
358 */
359 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700360 // netd already clears out quota and alerts for removed ifaces; update
361 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700362 mActiveAlerts.remove(iface);
363 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700364
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800365 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700366 try {
367 for (int i = 0; i < length; i++) {
368 try {
369 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
370 } catch (RemoteException e) {
371 } catch (RuntimeException e) {
372 }
San Mehat4d02d002010-01-22 16:07:46 -0800373 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700374 } finally {
375 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800376 }
377 }
378
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700379 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700380 * Notify our observers of a limit reached.
381 */
382 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800383 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700384 try {
385 for (int i = 0; i < length; i++) {
386 try {
387 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
388 } catch (RemoteException e) {
389 } catch (RuntimeException e) {
390 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700391 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700392 } finally {
393 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700394 }
395 }
396
397 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700398 * Notify our observers of a change in the data activity state of the interface
399 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700400 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
401 boolean fromRadio) {
402 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
403 if (isMobile) {
404 if (!fromRadio) {
405 if (mMobileActivityFromRadio) {
406 // If this call is not coming from a report from the radio itself, but we
407 // have previously received reports from the radio, then we will take the
408 // power state to just be whatever the radio last reported.
409 powerState = mLastPowerStateFromRadio;
410 }
411 } else {
412 mMobileActivityFromRadio = true;
413 }
414 if (mLastPowerStateFromRadio != powerState) {
415 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700416 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700417 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700418 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700419 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700420 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700421 }
422
423 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
424 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
425
426 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
427 // Report the change in data activity. We don't do this if this is a change
428 // on the mobile network, that is not coming from the radio itself, and we
429 // have previously seen change reports from the radio. In that case only
430 // the radio is the authority for the current state.
431 final int length = mObservers.beginBroadcast();
432 try {
433 for (int i = 0; i < length; i++) {
434 try {
435 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
436 Integer.toString(type), isActive, tsNanos);
437 } catch (RemoteException e) {
438 } catch (RuntimeException e) {
439 }
440 }
441 } finally {
442 mObservers.finishBroadcast();
443 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700444 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800445
446 boolean report = false;
447 synchronized (mIdleTimerLock) {
448 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700449 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800450 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700451 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800452 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700453 if (mNetworkActive != isActive) {
454 mNetworkActive = isActive;
455 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800456 }
457 }
458 if (report) {
459 reportNetworkActive();
460 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700461 }
462
463 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700464 * Prepare native daemon once connected, enabling modules and pushing any
465 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700466 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700467 private void prepareNativeDaemon() {
468 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700469
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700470 // only enable bandwidth control when support exists
471 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
472 if (hasKernelSupport) {
473 Slog.d(TAG, "enabling bandwidth control");
474 try {
475 mConnector.execute("bandwidth", "enable");
476 mBandwidthControlEnabled = true;
477 } catch (NativeDaemonConnectorException e) {
478 Log.wtf(TAG, "problem enabling bandwidth controls", e);
479 }
480 } else {
481 Slog.d(TAG, "not enabling bandwidth control");
482 }
483
484 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
485
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700486 if (mBandwidthControlEnabled) {
487 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800488 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700489 } catch (RemoteException e) {
490 }
491 }
492
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700493 // push any existing quota or UID rules
494 synchronized (mQuotaLock) {
495 int size = mActiveQuotas.size();
496 if (size > 0) {
497 Slog.d(TAG, "pushing " + size + " active quota rules");
498 final HashMap<String, Long> activeQuotas = mActiveQuotas;
499 mActiveQuotas = Maps.newHashMap();
500 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
501 setInterfaceQuota(entry.getKey(), entry.getValue());
502 }
503 }
504
505 size = mActiveAlerts.size();
506 if (size > 0) {
507 Slog.d(TAG, "pushing " + size + " active alert rules");
508 final HashMap<String, Long> activeAlerts = mActiveAlerts;
509 mActiveAlerts = Maps.newHashMap();
510 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
511 setInterfaceAlert(entry.getKey(), entry.getValue());
512 }
513 }
514
515 size = mUidRejectOnQuota.size();
516 if (size > 0) {
517 Slog.d(TAG, "pushing " + size + " active uid rules");
518 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
519 mUidRejectOnQuota = new SparseBooleanArray();
520 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
521 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
522 }
523 }
524 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700525
526 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700527 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700528 }
San Mehat4d02d002010-01-22 16:07:46 -0800529
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900530 /**
531 * Notify our observers of a new or updated interface address.
532 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900533 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900534 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700535 try {
536 for (int i = 0; i < length; i++) {
537 try {
538 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
539 } catch (RemoteException e) {
540 } catch (RuntimeException e) {
541 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900542 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700543 } finally {
544 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900545 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900546 }
547
548 /**
549 * Notify our observers of a deleted interface address.
550 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900551 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900552 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700553 try {
554 for (int i = 0; i < length; i++) {
555 try {
556 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
557 } catch (RemoteException e) {
558 } catch (RuntimeException e) {
559 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900560 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700561 } finally {
562 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900563 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900564 }
565
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900566 /**
567 * Notify our observers of DNS server information received.
568 */
569 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
570 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700571 try {
572 for (int i = 0; i < length; i++) {
573 try {
574 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
575 addresses);
576 } catch (RemoteException e) {
577 } catch (RuntimeException e) {
578 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900579 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700580 } finally {
581 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900582 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900583 }
584
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900585 /**
586 * Notify our observers of a route change.
587 */
588 private void notifyRouteChange(String action, RouteInfo route) {
589 final int length = mObservers.beginBroadcast();
590 try {
591 for (int i = 0; i < length; i++) {
592 try {
593 if (action.equals("updated")) {
594 mObservers.getBroadcastItem(i).routeUpdated(route);
595 } else {
596 mObservers.getBroadcastItem(i).routeRemoved(route);
597 }
598 } catch (RemoteException e) {
599 } catch (RuntimeException e) {
600 }
601 }
602 } finally {
603 mObservers.finishBroadcast();
604 }
605 }
606
San Mehat873f2142010-01-14 10:25:07 -0800607 //
608 // Netd Callback handling
609 //
610
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700611 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
612 @Override
San Mehat873f2142010-01-14 10:25:07 -0800613 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700614 // event is dispatched from internal NDC thread, so we prepare the
615 // daemon back on main thread.
616 if (mConnectedSignal != null) {
617 mConnectedSignal.countDown();
618 mConnectedSignal = null;
619 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700620 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700621 @Override
622 public void run() {
623 prepareNativeDaemon();
624 }
625 });
626 }
San Mehat873f2142010-01-14 10:25:07 -0800627 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700628
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700629 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800630 public boolean onCheckHoldWakeLock(int code) {
631 return code == NetdResponseCode.InterfaceClassActivity;
632 }
633
634 @Override
San Mehat873f2142010-01-14 10:25:07 -0800635 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900636 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700637 switch (code) {
638 case NetdResponseCode.InterfaceChange:
639 /*
640 * a network interface change occured
641 * Format: "NNN Iface added <name>"
642 * "NNN Iface removed <name>"
643 * "NNN Iface changed <name> <up/down>"
644 * "NNN Iface linkstatus <name> <up/down>"
645 */
646 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900647 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700648 }
649 if (cooked[2].equals("added")) {
650 notifyInterfaceAdded(cooked[3]);
651 return true;
652 } else if (cooked[2].equals("removed")) {
653 notifyInterfaceRemoved(cooked[3]);
654 return true;
655 } else if (cooked[2].equals("changed") && cooked.length == 5) {
656 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
657 return true;
658 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
659 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
660 return true;
661 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900662 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700663 // break;
664 case NetdResponseCode.BandwidthControl:
665 /*
666 * Bandwidth control needs some attention
667 * Format: "NNN limit alert <alertName> <ifaceName>"
668 */
669 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900670 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700671 }
672 if (cooked[2].equals("alert")) {
673 notifyLimitReached(cooked[3], cooked[4]);
674 return true;
675 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900676 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700677 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700678 case NetdResponseCode.InterfaceClassActivity:
679 /*
680 * An network interface class state changed (active/idle)
681 * Format: "NNN IfaceClass <active/idle> <label>"
682 */
683 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900684 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700685 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700686 long timestampNanos = 0;
687 if (cooked.length == 5) {
688 try {
689 timestampNanos = Long.parseLong(cooked[4]);
690 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700691 } else {
692 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700693 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700694 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700695 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700696 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
697 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700698 return true;
699 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900700 case NetdResponseCode.InterfaceAddressChange:
701 /*
702 * A network address change occurred
703 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
704 * "NNN Address removed <addr> <iface> <flags> <scope>"
705 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900706 if (cooked.length < 7 || !cooked[1].equals("Address")) {
707 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900708 }
709
Lorenzo Colitti64483942013-11-15 18:43:52 +0900710 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900711 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900712 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900713 int flags = Integer.parseInt(cooked[5]);
714 int scope = Integer.parseInt(cooked[6]);
715 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900716 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
717 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900718 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900719 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900720 }
721
722 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900723 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900724 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900725 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900726 }
727 return true;
728 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900729 case NetdResponseCode.InterfaceDnsServerInfo:
730 /*
731 * Information about available DNS servers has been received.
732 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
733 */
734 long lifetime; // Actually a 32-bit unsigned integer.
735
736 if (cooked.length == 6 &&
737 cooked[1].equals("DnsInfo") &&
738 cooked[2].equals("servers")) {
739 try {
740 lifetime = Long.parseLong(cooked[4]);
741 } catch (NumberFormatException e) {
742 throw new IllegalStateException(errorMessage);
743 }
744 String[] servers = cooked[5].split(",");
745 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
746 }
747 return true;
748 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900749 case NetdResponseCode.RouteChange:
750 /*
751 * A route has been updated or removed.
752 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
753 */
754 if (!cooked[1].equals("Route") || cooked.length < 6) {
755 throw new IllegalStateException(errorMessage);
756 }
757
758 String via = null;
759 String dev = null;
760 boolean valid = true;
761 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
762 if (cooked[i].equals("dev")) {
763 if (dev == null) {
764 dev = cooked[i+1];
765 } else {
766 valid = false; // Duplicate interface.
767 }
768 } else if (cooked[i].equals("via")) {
769 if (via == null) {
770 via = cooked[i+1];
771 } else {
772 valid = false; // Duplicate gateway.
773 }
774 } else {
775 valid = false; // Unknown syntax.
776 }
777 }
778 if (valid) {
779 try {
780 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
781 InetAddress gateway = null;
782 if (via != null) gateway = InetAddress.parseNumericAddress(via);
783 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
784 notifyRouteChange(cooked[2], route);
785 return true;
786 } catch (IllegalArgumentException e) {}
787 }
788 throw new IllegalStateException(errorMessage);
789 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700790 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800791 }
792 return false;
San Mehat873f2142010-01-14 10:25:07 -0800793 }
794 }
795
San Mehated4fc8a2010-01-22 12:28:36 -0800796
San Mehat873f2142010-01-14 10:25:07 -0800797 //
798 // INetworkManagementService members
799 //
800
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800801 @Override
802 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800803 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700804 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 return NativeDaemonEvent.filterMessageList(
806 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700807 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800808 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700809 }
San Mehated4fc8a2010-01-22 12:28:36 -0800810 }
811
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800812 @Override
813 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800814 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800815
816 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700817 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800818 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700819 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800820 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700821 }
San Mehated4fc8a2010-01-22 12:28:36 -0800822
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 event.checkCode(InterfaceGetCfgResult);
824
825 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
826 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800827
Kenny Roota80ce062010-06-01 13:23:53 -0700828 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800829 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700830 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800831 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800832 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800833 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700834 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800835 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800836 } catch (IllegalArgumentException iae) {
837 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700838 }
839
840 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800841 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800842 } catch (NumberFormatException nfe) {
843 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700844 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800845
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800846 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
847 while (st.hasMoreTokens()) {
848 cfg.setFlag(st.nextToken());
849 }
Kenny Roota80ce062010-06-01 13:23:53 -0700850 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800851 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800852 }
San Mehated4fc8a2010-01-22 12:28:36 -0800853 return cfg;
854 }
855
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800856 @Override
857 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800858 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800859 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800860 if (linkAddr == null || linkAddr.getAddress() == null) {
861 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800862 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800863
864 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800865 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900866 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800867 for (String flag : cfg.getFlags()) {
868 cmd.appendArg(flag);
869 }
870
Kenny Roota80ce062010-06-01 13:23:53 -0700871 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800872 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700873 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800874 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700875 }
San Mehat873f2142010-01-14 10:25:07 -0800876 }
877
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800878 @Override
879 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800880 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800881 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800882 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800883 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700884 }
885
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800886 @Override
887 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800888 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800889 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800890 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800891 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700892 }
893
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800894 @Override
895 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800896 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700897 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800898 mConnector.execute(
899 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700900 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800901 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700902 }
903 }
904
Irfan Sherifff5600612011-06-16 10:26:28 -0700905 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
906 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800907 @Override
908 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800909 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700910 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800911 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700912 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800913 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700914 }
915 }
916
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800917 @Override
918 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800919 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700920 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700922 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800923 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700924 }
925 }
926
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800927 @Override
928 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700930 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700932 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800933 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700934 }
935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700938 public void addRoute(int netId, RouteInfo route) {
939 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700940 }
941
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800942 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700943 public void removeRoute(int netId, RouteInfo route) {
944 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700945 }
946
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700947 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800948 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700949
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700950 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700951
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700952 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700953 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700954 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900955 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700956 if (route.hasGateway()) {
957 cmd.appendArg(route.getGateway().getHostAddress());
958 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700959
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800960 try {
961 mConnector.execute(cmd);
962 } catch (NativeDaemonConnectorException e) {
963 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700964 }
965 }
966
967 private ArrayList<String> readRouteList(String filename) {
968 FileInputStream fstream = null;
969 ArrayList<String> list = new ArrayList<String>();
970
971 try {
972 fstream = new FileInputStream(filename);
973 DataInputStream in = new DataInputStream(fstream);
974 BufferedReader br = new BufferedReader(new InputStreamReader(in));
975 String s;
976
977 // throw away the title line
978
979 while (((s = br.readLine()) != null) && (s.length() != 0)) {
980 list.add(s);
981 }
982 } catch (IOException ex) {
983 // return current list, possibly empty
984 } finally {
985 if (fstream != null) {
986 try {
987 fstream.close();
988 } catch (IOException ex) {}
989 }
990 }
991
992 return list;
993 }
994
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800995 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700996 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800997 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700998 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
999
1000 // v4 routes listed as:
1001 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1002 for (String s : readRouteList("/proc/net/route")) {
1003 String[] fields = s.split("\t");
1004
1005 if (fields.length > 7) {
1006 String iface = fields[0];
1007
1008 if (interfaceName.equals(iface)) {
1009 String dest = fields[1];
1010 String gate = fields[2];
1011 String flags = fields[3]; // future use?
1012 String mask = fields[7];
1013 try {
1014 // address stored as a hex string, ex: 0014A8C0
1015 InetAddress destAddr =
1016 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1017 int prefixLength =
1018 NetworkUtils.netmaskIntToPrefixLength(
1019 (int)Long.parseLong(mask, 16));
1020 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1021
1022 // address stored as a hex string, ex 0014A8C0
1023 InetAddress gatewayAddr =
1024 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1025
Wink Saville7b5fd052013-03-15 05:07:04 +00001026 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001027 routes.add(route);
1028 } catch (Exception e) {
1029 Log.e(TAG, "Error parsing route " + s + " : " + e);
1030 continue;
1031 }
1032 }
1033 }
1034 }
1035
1036 // v6 routes listed as:
1037 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1038 for (String s : readRouteList("/proc/net/ipv6_route")) {
1039 String[]fields = s.split("\\s+");
1040 if (fields.length > 9) {
1041 String iface = fields[9].trim();
1042 if (interfaceName.equals(iface)) {
1043 String dest = fields[0];
1044 String prefix = fields[1];
1045 String gate = fields[4];
1046
1047 try {
1048 // prefix length stored as a hex string, ex 40
1049 int prefixLength = Integer.parseInt(prefix, 16);
1050
1051 // address stored as a 32 char hex string
1052 // ex fe800000000000000000000000000000
1053 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1054 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1055
1056 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1057
Wink Saville7b5fd052013-03-15 05:07:04 +00001058 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001059 routes.add(route);
1060 } catch (Exception e) {
1061 Log.e(TAG, "Error parsing route " + s + " : " + e);
1062 continue;
1063 }
1064 }
1065 }
1066 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001067 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001068 }
1069
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001070 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001071 public void setMtu(String iface, int mtu) {
1072 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1073
1074 final NativeDaemonEvent event;
1075 try {
1076 event = mConnector.execute("interface", "setmtu", iface, mtu);
1077 } catch (NativeDaemonConnectorException e) {
1078 throw e.rethrowAsParcelableException();
1079 }
1080 }
1081
1082 @Override
San Mehat873f2142010-01-14 10:25:07 -08001083 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001084 // TODO: remove from aidl if nobody calls externally
1085 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001086
Joe Onorato8a9b2202010-02-26 18:56:32 -08001087 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001088 }
1089
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001090 @Override
San Mehat873f2142010-01-14 10:25:07 -08001091 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001092 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001093
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001094 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001095 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001096 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001097 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001098 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001099 }
San Mehat873f2142010-01-14 10:25:07 -08001100
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001101 // 211 Forwarding enabled
1102 event.checkCode(IpFwdStatusResult);
1103 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001104 }
1105
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001106 @Override
1107 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001108 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001109 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001110 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001111 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001112 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001113 }
San Mehat873f2142010-01-14 10:25:07 -08001114 }
1115
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001116 @Override
1117 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001118 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001119 // cmd is "tether start first_start first_stop second_start second_stop ..."
1120 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001121
1122 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001123 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001124 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001125 }
Kenny Roota80ce062010-06-01 13:23:53 -07001126
1127 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001128 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001129 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001130 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001131 }
San Mehat873f2142010-01-14 10:25:07 -08001132 }
1133
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001134 @Override
1135 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001136 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001137 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001138 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001139 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001140 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001141 }
San Mehat873f2142010-01-14 10:25:07 -08001142 }
1143
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001144 @Override
1145 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001146 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001147
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001148 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001149 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001150 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001151 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001152 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001153 }
San Mehat873f2142010-01-14 10:25:07 -08001154
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001155 // 210 Tethering services started
1156 event.checkCode(TetherStatusResult);
1157 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001158 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001159
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001160 @Override
1161 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001163 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001164 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001165 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001166 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001167 }
San Mehat873f2142010-01-14 10:25:07 -08001168 }
1169
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001170 @Override
San Mehat873f2142010-01-14 10:25:07 -08001171 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001172 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001173 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001174 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001175 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001176 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001177 }
San Mehat873f2142010-01-14 10:25:07 -08001178 }
1179
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001180 @Override
1181 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001182 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001183 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001184 return NativeDaemonEvent.filterMessageList(
1185 mConnector.executeForList("tether", "interface", "list"),
1186 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001187 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001188 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001189 }
San Mehat873f2142010-01-14 10:25:07 -08001190 }
1191
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001192 @Override
1193 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001194 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001195
1196 final Command cmd = new Command("tether", "dns", "set");
1197 for (String s : dns) {
1198 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1199 }
1200
San Mehat873f2142010-01-14 10:25:07 -08001201 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001202 mConnector.execute(cmd);
1203 } catch (NativeDaemonConnectorException e) {
1204 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001205 }
1206 }
1207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001208 @Override
1209 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001210 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001211 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 return NativeDaemonEvent.filterMessageList(
1213 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001214 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001215 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001216 }
San Mehat873f2142010-01-14 10:25:07 -08001217 }
1218
jiaguo1da35f72014-01-09 16:39:59 +08001219 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1220 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1221 for (InterfaceAddress ia : addresses) {
1222 if (!ia.getAddress().isLinkLocalAddress())
1223 filtered.add(ia);
1224 }
1225 return filtered;
1226 }
1227
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001228 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001229 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001230 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001231
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001232 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1233 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001234 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001235 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001236 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001237 // Don't touch link-local routes, as link-local addresses aren't routable,
1238 // kernel creates link-local routes on all interfaces automatically
1239 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1240 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001241 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001242 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001243 InetAddress addr = NetworkUtils.getNetworkPart(
1244 ia.getAddress(), ia.getNetworkPrefixLength());
1245 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001246 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001247 }
1248
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001249 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001250 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001251 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001252 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001253 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001254 }
1255
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001256 @Override
1257 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001258 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001259 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001260 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001261 } catch (SocketException e) {
1262 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001263 }
San Mehat873f2142010-01-14 10:25:07 -08001264 }
1265
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001266 @Override
1267 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001268 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001269 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001270 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001271 } catch (SocketException e) {
1272 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001273 }
San Mehat873f2142010-01-14 10:25:07 -08001274 }
San Mehat72759df2010-01-19 13:50:37 -08001275
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001276 @Override
1277 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001278 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001279 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001280 return NativeDaemonEvent.filterMessageList(
1281 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001282 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001283 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001284 }
San Mehat72759df2010-01-19 13:50:37 -08001285 }
1286
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001287 @Override
1288 public void attachPppd(
1289 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001290 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001291 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001292 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001293 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1294 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1295 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001296 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001297 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001298 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001299 }
1300 }
1301
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001302 @Override
1303 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001304 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001305 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001306 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001307 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001308 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001309 }
San Mehat72759df2010-01-19 13:50:37 -08001310 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001311
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001312 @Override
1313 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001314 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001315 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001316 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001317 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001318 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001319 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001320 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001321 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001322 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001323 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001324 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001325 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001326 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001327 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001328 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001329 }
1330
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001331 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001332 switch (wifiConfig.getAuthType()) {
1333 case KeyMgmt.WPA_PSK:
1334 return "wpa-psk";
1335 case KeyMgmt.WPA2_PSK:
1336 return "wpa2-psk";
1337 default:
1338 return "open";
1339 }
1340 }
1341
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001342 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001343 @Override
1344 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001345 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001346 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001347 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001348 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001349 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001350 }
1351 }
1352
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001353 @Override
1354 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001355 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001356 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001357 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001358 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001359 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001360 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001361 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001362 }
1363
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001364 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001365 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001366 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001367 try {
1368 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001369 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001370 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001371 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001372 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001373 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001374 }
1375 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001376 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001377 }
1378 }
San Mehat91cac642010-03-31 14:31:36 -07001379
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001380 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001381 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001382 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1383
1384 if (DBG) Slog.d(TAG, "Adding idletimer");
1385
1386 synchronized (mIdleTimerLock) {
1387 IdleTimerParams params = mActiveIdleTimers.get(iface);
1388 if (params != null) {
1389 // the interface already has idletimer, update network count
1390 params.networkCount++;
1391 return;
1392 }
1393
1394 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001395 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1396 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001397 } catch (NativeDaemonConnectorException e) {
1398 throw e.rethrowAsParcelableException();
1399 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001400 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1401
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001402 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001403 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1404 mNetworkActive = false;
1405 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001406 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001407 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001408 notifyInterfaceClassActivity(type,
1409 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1410 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001411 }
1412 });
Haoyu Bai04124232012-06-28 15:26:19 -07001413 }
1414 }
1415
1416 @Override
1417 public void removeIdleTimer(String iface) {
1418 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1419
1420 if (DBG) Slog.d(TAG, "Removing idletimer");
1421
1422 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001423 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001424 if (params == null || --(params.networkCount) > 0) {
1425 return;
1426 }
1427
1428 try {
1429 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001430 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001431 } catch (NativeDaemonConnectorException e) {
1432 throw e.rethrowAsParcelableException();
1433 }
1434 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001435 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001436 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001437 notifyInterfaceClassActivity(params.type,
1438 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1439 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001440 }
1441 });
Haoyu Bai04124232012-06-28 15:26:19 -07001442 }
1443 }
1444
1445 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001446 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001447 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001448 try {
1449 return mStatsFactory.readNetworkStatsSummaryDev();
1450 } catch (IOException e) {
1451 throw new IllegalStateException(e);
1452 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001453 }
1454
1455 @Override
1456 public NetworkStats getNetworkStatsSummaryXt() {
1457 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001458 try {
1459 return mStatsFactory.readNetworkStatsSummaryXt();
1460 } catch (IOException e) {
1461 throw new IllegalStateException(e);
1462 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001463 }
1464
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001465 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001466 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001467 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001468 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001469 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001470 } catch (IOException e) {
1471 throw new IllegalStateException(e);
1472 }
San Mehat91cac642010-03-31 14:31:36 -07001473 }
1474
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001475 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001476 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001477 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001478
Jeff Sharkey350083e2011-06-29 10:45:16 -07001479 // silently discard when control disabled
1480 // TODO: eventually migrate to be always enabled
1481 if (!mBandwidthControlEnabled) return;
1482
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001483 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001484 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001485 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001486 }
1487
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001488 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001489 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001490 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001491 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001492 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001493 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001494 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001495 }
1496 }
1497
1498 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001499 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001500 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001501
Jeff Sharkey350083e2011-06-29 10:45:16 -07001502 // silently discard when control disabled
1503 // TODO: eventually migrate to be always enabled
1504 if (!mBandwidthControlEnabled) return;
1505
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001506 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001507 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001508 // TODO: eventually consider throwing
1509 return;
1510 }
1511
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001512 mActiveQuotas.remove(iface);
1513 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001514
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001515 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001516 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001517 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001518 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001519 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001520 }
1521 }
1522 }
1523
1524 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001525 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001526 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001527
1528 // silently discard when control disabled
1529 // TODO: eventually migrate to be always enabled
1530 if (!mBandwidthControlEnabled) return;
1531
1532 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001533 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001534 throw new IllegalStateException("setting alert requires existing quota on iface");
1535 }
1536
1537 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001538 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001539 throw new IllegalStateException("iface " + iface + " already has alert");
1540 }
1541
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001542 try {
1543 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001544 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001545 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001546 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001547 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001548 }
1549 }
1550 }
1551
1552 @Override
1553 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001554 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001555
1556 // silently discard when control disabled
1557 // TODO: eventually migrate to be always enabled
1558 if (!mBandwidthControlEnabled) return;
1559
1560 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001561 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001562 // TODO: eventually consider throwing
1563 return;
1564 }
1565
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001566 try {
1567 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001568 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001569 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001570 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001571 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001572 }
1573 }
1574 }
1575
1576 @Override
1577 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001578 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001579
1580 // silently discard when control disabled
1581 // TODO: eventually migrate to be always enabled
1582 if (!mBandwidthControlEnabled) return;
1583
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001584 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001585 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001586 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001587 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001588 }
1589 }
1590
1591 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001592 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001593 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001594
Jeff Sharkey350083e2011-06-29 10:45:16 -07001595 // silently discard when control disabled
1596 // TODO: eventually migrate to be always enabled
1597 if (!mBandwidthControlEnabled) return;
1598
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001599 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001600 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1601 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1602 // TODO: eventually consider throwing
1603 return;
1604 }
1605
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001606 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001607 mConnector.execute("bandwidth",
1608 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001609 if (rejectOnQuotaInterfaces) {
1610 mUidRejectOnQuota.put(uid, true);
1611 } else {
1612 mUidRejectOnQuota.delete(uid);
1613 }
1614 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001615 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001616 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001617 }
1618 }
1619
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001620 @Override
1621 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001622 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001623 return mBandwidthControlEnabled;
1624 }
1625
1626 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001627 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001628 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001629 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001630 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001631 } catch (IOException e) {
1632 throw new IllegalStateException(e);
1633 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001634 }
1635
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001636 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001637 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001638 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001639
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001640 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001641 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001642 final NativeDaemonEvent[] events = mConnector.executeForList(
1643 "bandwidth", "gettetherstats");
1644 for (NativeDaemonEvent event : events) {
1645 if (event.getCode() != TetheringStatsListResult) continue;
1646
1647 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1648 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1649 try {
1650 final String ifaceIn = tok.nextToken();
1651 final String ifaceOut = tok.nextToken();
1652
1653 final NetworkStats.Entry entry = new NetworkStats.Entry();
1654 entry.iface = ifaceOut;
1655 entry.uid = UID_TETHERING;
1656 entry.set = SET_DEFAULT;
1657 entry.tag = TAG_NONE;
1658 entry.rxBytes = Long.parseLong(tok.nextToken());
1659 entry.rxPackets = Long.parseLong(tok.nextToken());
1660 entry.txBytes = Long.parseLong(tok.nextToken());
1661 entry.txPackets = Long.parseLong(tok.nextToken());
1662 stats.combineValues(entry);
1663 } catch (NoSuchElementException e) {
1664 throw new IllegalStateException("problem parsing tethering stats: " + event);
1665 } catch (NumberFormatException e) {
1666 throw new IllegalStateException("problem parsing tethering stats: " + event);
1667 }
1668 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001669 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001670 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001671 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001672 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001673 }
1674
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001675 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001676 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001677 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001678
Paul Jensen13e817d2014-04-10 14:16:37 -04001679 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001680 (domains == null ? "" : domains));
1681
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001682 for (String s : servers) {
1683 InetAddress a = NetworkUtils.numericToInetAddress(s);
1684 if (a.isAnyLocalAddress() == false) {
1685 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001686 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001687 }
1688
1689 try {
1690 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001691 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001692 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001693 }
1694 }
1695
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001696 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001697 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001698 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1699 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001700 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001701 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001702 } catch (NativeDaemonConnectorException e) {
1703 throw e.rethrowAsParcelableException();
1704 }
1705 }
1706
1707 @Override
1708 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1709 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1710 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001711 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001712 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001713 } catch (NativeDaemonConnectorException e) {
1714 throw e.rethrowAsParcelableException();
1715 }
1716 }
1717
1718 @Override
1719 public void setMarkedForwarding(String iface) {
1720 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1721 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001722 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001723 } catch (NativeDaemonConnectorException e) {
1724 throw e.rethrowAsParcelableException();
1725 }
1726 }
1727
1728 @Override
1729 public void clearMarkedForwarding(String iface) {
1730 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1731 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001732 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1733 } catch (NativeDaemonConnectorException e) {
1734 throw e.rethrowAsParcelableException();
1735 }
1736 }
1737
1738 @Override
1739 public int getMarkForUid(int uid) {
1740 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1741 final NativeDaemonEvent event;
1742 try {
1743 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1744 } catch (NativeDaemonConnectorException e) {
1745 throw e.rethrowAsParcelableException();
1746 }
1747 event.checkCode(GetMarkResult);
1748 return Integer.parseInt(event.getMessage());
1749 }
1750
1751 @Override
1752 public int getMarkForProtect() {
1753 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1754 final NativeDaemonEvent event;
1755 try {
1756 event = mConnector.execute("interface", "fwmark", "get", "protect");
1757 } catch (NativeDaemonConnectorException e) {
1758 throw e.rethrowAsParcelableException();
1759 }
1760 event.checkCode(GetMarkResult);
1761 return Integer.parseInt(event.getMessage());
1762 }
1763
1764 @Override
1765 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1766 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1767 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001768 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001769 mConnector.execute("interface", "fwmark", "route", "add", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001770 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubakercca54c42013-06-27 17:41:38 -07001771 } catch (NativeDaemonConnectorException e) {
1772 throw e.rethrowAsParcelableException();
1773 }
1774 }
1775
1776 @Override
1777 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1778 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1779 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001780 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001781 mConnector.execute("interface", "fwmark", "route", "remove", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001782 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001783 } catch (NativeDaemonConnectorException e) {
1784 throw e.rethrowAsParcelableException();
1785 }
1786 }
1787
1788 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001789 public void setHostExemption(LinkAddress host) {
1790 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1791 try {
1792 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1793 } catch (NativeDaemonConnectorException e) {
1794 throw e.rethrowAsParcelableException();
1795 }
1796 }
1797
1798 @Override
1799 public void clearHostExemption(LinkAddress host) {
1800 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1801 try {
1802 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1803 } catch (NativeDaemonConnectorException e) {
1804 throw e.rethrowAsParcelableException();
1805 }
1806 }
1807
1808 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001809 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001810 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1811 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001812 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001813 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001814 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001815 }
1816 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001817
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001818 @Override
1819 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001820 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001821 try {
1822 mConnector.execute("firewall", enabled ? "enable" : "disable");
1823 mFirewallEnabled = enabled;
1824 } catch (NativeDaemonConnectorException e) {
1825 throw e.rethrowAsParcelableException();
1826 }
1827 }
1828
1829 @Override
1830 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001831 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001832 return mFirewallEnabled;
1833 }
1834
1835 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001836 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001837 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001838 Preconditions.checkState(mFirewallEnabled);
1839 final String rule = allow ? ALLOW : DENY;
1840 try {
1841 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1842 } catch (NativeDaemonConnectorException e) {
1843 throw e.rethrowAsParcelableException();
1844 }
1845 }
1846
1847 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001848 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001849 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001850 Preconditions.checkState(mFirewallEnabled);
1851 final String rule = allow ? ALLOW : DENY;
1852 try {
1853 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1854 } catch (NativeDaemonConnectorException e) {
1855 throw e.rethrowAsParcelableException();
1856 }
1857 }
1858
1859 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001860 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001861 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001862 Preconditions.checkState(mFirewallEnabled);
1863 final String rule = allow ? ALLOW : DENY;
1864 try {
1865 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1866 } catch (NativeDaemonConnectorException e) {
1867 throw e.rethrowAsParcelableException();
1868 }
1869 }
1870
1871 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001872 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001873 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001874 Preconditions.checkState(mFirewallEnabled);
1875 final String rule = allow ? ALLOW : DENY;
1876 try {
1877 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1878 } catch (NativeDaemonConnectorException e) {
1879 throw e.rethrowAsParcelableException();
1880 }
1881 }
1882
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001883 private static void enforceSystemUid() {
1884 final int uid = Binder.getCallingUid();
1885 if (uid != Process.SYSTEM_UID) {
1886 throw new SecurityException("Only available to AID_SYSTEM");
1887 }
1888 }
1889
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001890 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001891 public void startClatd(String interfaceName) throws IllegalStateException {
1892 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1893
1894 try {
1895 mConnector.execute("clatd", "start", interfaceName);
1896 } catch (NativeDaemonConnectorException e) {
1897 throw e.rethrowAsParcelableException();
1898 }
1899 }
1900
1901 @Override
1902 public void stopClatd() throws IllegalStateException {
1903 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1904
1905 try {
1906 mConnector.execute("clatd", "stop");
1907 } catch (NativeDaemonConnectorException e) {
1908 throw e.rethrowAsParcelableException();
1909 }
1910 }
1911
1912 @Override
1913 public boolean isClatdStarted() {
1914 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1915
1916 final NativeDaemonEvent event;
1917 try {
1918 event = mConnector.execute("clatd", "status");
1919 } catch (NativeDaemonConnectorException e) {
1920 throw e.rethrowAsParcelableException();
1921 }
1922
1923 event.checkCode(ClatdStatusResult);
1924 return event.getMessage().endsWith("started");
1925 }
1926
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001927 @Override
1928 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1929 mNetworkActivityListeners.register(listener);
1930 }
1931
1932 @Override
1933 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1934 mNetworkActivityListeners.unregister(listener);
1935 }
1936
1937 @Override
1938 public boolean isNetworkActive() {
1939 synchronized (mNetworkActivityListeners) {
1940 return mNetworkActive || mActiveIdleTimers.isEmpty();
1941 }
1942 }
1943
1944 private void reportNetworkActive() {
1945 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001946 try {
1947 for (int i = 0; i < length; i++) {
1948 try {
1949 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1950 } catch (RemoteException e) {
1951 } catch (RuntimeException e) {
1952 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001953 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001954 } finally {
1955 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001956 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001957 }
1958
Mattias Falk8b47b362011-08-23 14:15:13 +02001959 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001960 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001961 public void monitor() {
1962 if (mConnector != null) {
1963 mConnector.monitor();
1964 }
1965 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001966
1967 @Override
1968 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1969 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1970
Robert Greenwalt470fd722012-01-18 12:51:15 -08001971 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1972 mConnector.dump(fd, pw, args);
1973 pw.println();
1974
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001975 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001976 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1977 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1978 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001979
1980 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001981 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1982 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001983 }
1984
1985 synchronized (mUidRejectOnQuota) {
1986 pw.print("UID reject on quota ifaces: [");
1987 final int size = mUidRejectOnQuota.size();
1988 for (int i = 0; i < size; i++) {
1989 pw.print(mUidRejectOnQuota.keyAt(i));
1990 if (i < size - 1) pw.print(",");
1991 }
1992 pw.println("]");
1993 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001994
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001995 synchronized (mIdleTimerLock) {
1996 pw.println("Idle timers:");
1997 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1998 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1999 IdleTimerParams params = ent.getValue();
2000 pw.print(" timeout="); pw.print(params.timeout);
2001 pw.print(" type="); pw.print(params.type);
2002 pw.print(" networkCount="); pw.println(params.networkCount);
2003 }
2004 }
2005
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002006 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002007 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002008
Robert Greenwalt568891d2014-04-04 13:38:00 -07002009 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002010 public void createNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002011 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2012
2013 try {
Paul Jensen992f2522014-04-28 10:33:11 -04002014 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002015 } catch (NativeDaemonConnectorException e) {
2016 throw e.rethrowAsParcelableException();
2017 }
2018 }
2019
Robert Greenwalt568891d2014-04-04 13:38:00 -07002020 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002021 public void removeNetwork(int netId) {
2022 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2023
2024 try {
2025 mConnector.execute("network", "destroy", netId);
2026 } catch (NativeDaemonConnectorException e) {
2027 throw e.rethrowAsParcelableException();
2028 }
2029 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002030
2031 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002032 public void addInterfaceToNetwork(String iface, int netId) {
2033 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2034
2035 try {
2036 mConnector.execute("network", "addiface", netId, iface);
2037 } catch (NativeDaemonConnectorException e) {
2038 throw e.rethrowAsParcelableException();
2039 }
2040 }
2041
2042 @Override
2043 public void removeInterfaceFromNetwork(String iface, int netId) {
2044 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2045
2046 try {
2047 mConnector.execute("network", "removeiface", netId, iface);
2048 } catch (NativeDaemonConnectorException e) {
2049 throw e.rethrowAsParcelableException();
2050 }
2051 }
2052
2053 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002054 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2055 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002056 }
2057
2058 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002059 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2060 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002061 }
2062
Robert Greenwalt913c8952014-04-07 17:36:35 -07002063 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002064 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2065
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002066 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002067
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002068 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002069 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002070 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002071 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002072 if (routeInfo.hasGateway()) {
2073 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2074 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002075
2076 try {
2077 mConnector.execute(cmd);
2078 } catch (NativeDaemonConnectorException e) {
2079 throw e.rethrowAsParcelableException();
2080 }
2081 }
2082
2083 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002084 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002085 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2086
2087 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002088 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002089 } catch (NativeDaemonConnectorException e) {
2090 throw e.rethrowAsParcelableException();
2091 }
2092 }
2093
2094 @Override
2095 public void clearDefaultNetId() {
2096 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2097
2098 try {
2099 mConnector.execute("network", "default", "clear");
2100 } catch (NativeDaemonConnectorException e) {
2101 throw e.rethrowAsParcelableException();
2102 }
2103 }
2104
2105 @Override
2106 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2107 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2108
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002109 final Command cmd = new Command("network", "permission", "user", "set");
2110 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2111 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002112 for (int i=0; i<uids.length; i++) {
2113 cmd.appendArg(uids[i]);
2114 }
2115
2116 try {
2117 mConnector.execute(cmd);
2118 } catch (NativeDaemonConnectorException e) {
2119 throw e.rethrowAsParcelableException();
2120 }
2121 }
2122
2123 @Override
2124 public void clearPermission(int[] uids) {
2125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2126
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002127 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002128 for (int i=0; i<uids.length; i++) {
2129 cmd.appendArg(uids[i]);
2130 }
2131
2132 try {
2133 mConnector.execute(cmd);
2134 } catch (NativeDaemonConnectorException e) {
2135 throw e.rethrowAsParcelableException();
2136 }
2137 }
San Mehat873f2142010-01-14 10:25:07 -08002138}