blob: 75a7878b99c4fadd68ee797e8b0326098fb7d089 [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 Sharkeyba2896e2011-11-30 18:13:54 -080029import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080031import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070035import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080036import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070037import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
San Mehat873f2142010-01-14 10:25:07 -080039import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080041import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090043import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070045import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080046import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070047import android.net.RouteInfo;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040048import android.net.UidRange;
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;
Wink Saville67e07892014-06-18 16:43:14 -070066import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080067import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070069import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080070
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070071import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070072import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070073import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080074import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070075import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070076import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070077import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070078
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070079import java.io.BufferedReader;
80import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080081import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070082import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070084import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070085import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070086import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070087import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070088import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070089import java.net.InterfaceAddress;
90import java.net.NetworkInterface;
91import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092import java.util.ArrayList;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040093import java.util.Arrays;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070094import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080095import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070096import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070097import java.util.NoSuchElementException;
98import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070099import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -0800100
101/**
102 * @hide
103 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700104public class NetworkManagementService extends INetworkManagementService.Stub
105 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700106 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700107 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700108 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900109 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700110
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400111 private static final int MAX_UID_RANGES_PER_COMMAND = 10;
112
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700113 /**
114 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
115 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
116 */
117 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
118
San Mehat873f2142010-01-14 10:25:07 -0800119 class NetdResponseCode {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -0700120 /* Keep in sync with system/netd/server/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800121 public static final int InterfaceListResult = 110;
122 public static final int TetherInterfaceListResult = 111;
123 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800124 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700125 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800126
127 public static final int TetherStatusResult = 210;
128 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800129 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800130 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700131 public static final int InterfaceRxCounterResult = 216;
132 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700133 public static final int QuotaCounterResult = 220;
134 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800135 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900136 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800137
138 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700139 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700140 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900141 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900142 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900143 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800144 }
145
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700146 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
147
San Mehat873f2142010-01-14 10:25:07 -0800148 /**
149 * Binder context for this service
150 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700151 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800152
153 /**
154 * connector object for communicating with netd
155 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700156 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800157
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700158 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700159 private final Handler mDaemonHandler;
160 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700161
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800162 private IBatteryStats mBatteryStats;
163
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700164 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700165 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700166
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800167 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
168 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800169
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700170 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
171
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700172 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700173 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700174 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700175 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700176 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700177 /** Set of UIDs with active reject rules. */
178 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
179
Haoyu Bai04124232012-06-28 15:26:19 -0700180 private Object mIdleTimerLock = new Object();
181 /** Set of interfaces with active idle timers. */
182 private static class IdleTimerParams {
183 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800184 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700185 public int networkCount;
186
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800187 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700188 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800189 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700190 this.networkCount = 1;
191 }
192 }
193 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
194
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700195 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700196 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700197
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700198 private boolean mMobileActivityFromRadio = false;
199 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
200
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800201 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
202 new RemoteCallbackList<INetworkActivityListener>();
203 private boolean mNetworkActive;
204
San Mehat873f2142010-01-14 10:25:07 -0800205 /**
206 * Constructs a new NetworkManagementService instance
207 *
208 * @param context Binder context for this service
209 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900210 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800211 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800212
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700213 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
214 mFgHandler = new Handler(FgThread.get().getLooper());
215
Marco Nelissen62dbb222010-02-18 10:56:30 -0800216 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700217 mConnector = null;
218 mThread = null;
219 mDaemonHandler = null;
220 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800221 return;
222 }
223
Dianne Hackborn4590e522014-03-24 13:36:46 -0700224 // Don't need this wake lock, since we now have a time stamp for when
225 // the network actually went inactive. (It might be nice to still do this,
226 // but I don't want to do it through the power manager because that pollutes the
227 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700228 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700229 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800230
San Mehat873f2142010-01-14 10:25:07 -0800231 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700232 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
233 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700234 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700235
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700236 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Saville67e07892014-06-18 16:43:14 -0700237
238 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUB_ID,
Wink Savillefb40dd42014-06-12 17:02:31 -0700239 mDaemonHandler.getLooper()) {
Wink Saville67e07892014-06-18 16:43:14 -0700240 @Override
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700241 public void onDataConnectionRealTimeInfoChanged(
242 DataConnectionRealTimeInfo dcRtInfo) {
Wink Saville67e07892014-06-18 16:43:14 -0700243 if (DBG) Slog.d(TAG, "onDataConnectionRealTimeInfoChanged: " + dcRtInfo);
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700244 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
245 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700246 }
247 };
Wink Saville67e07892014-06-18 16:43:14 -0700248 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
249 if (tm != null) {
250 tm.listen(mPhoneStateListener,
251 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO);
252 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700253
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700254 // Add ourself to the Watchdog monitors.
255 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700256 }
257
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900258 static NetworkManagementService create(Context context,
259 String socket) throws InterruptedException {
260 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700261 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700262 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
263 service.mThread.start();
264 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700265 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700266 if (DBG) Slog.d(TAG, "Connected");
267 return service;
San Mehat873f2142010-01-14 10:25:07 -0800268 }
269
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900270 public static NetworkManagementService create(Context context) throws InterruptedException {
271 return create(context, NETD_SOCKET_NAME);
272 }
273
Jeff Sharkey350083e2011-06-29 10:45:16 -0700274 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700275 prepareNativeDaemon();
276 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700277 }
278
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800279 private IBatteryStats getBatteryStats() {
280 synchronized (this) {
281 if (mBatteryStats != null) {
282 return mBatteryStats;
283 }
284 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
285 BatteryStats.SERVICE_NAME));
286 return mBatteryStats;
287 }
288 }
289
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800290 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800291 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800292 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800293 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800294 }
295
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800296 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800298 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800299 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800300 }
301
302 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700303 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800304 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700305 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800306 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700307 try {
308 for (int i = 0; i < length; i++) {
309 try {
310 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
311 } catch (RemoteException e) {
312 } catch (RuntimeException e) {
313 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700314 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700315 } finally {
316 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700317 }
318 }
319
320 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700321 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700322 * (typically, an Ethernet cable has been plugged-in or unplugged).
323 */
324 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800325 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700326 try {
327 for (int i = 0; i < length; i++) {
328 try {
329 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
330 } catch (RemoteException e) {
331 } catch (RuntimeException e) {
332 }
San Mehat4d02d002010-01-22 16:07:46 -0800333 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700334 } finally {
335 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800336 }
337 }
338
339 /**
340 * Notify our observers of an interface addition.
341 */
342 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800343 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700344 try {
345 for (int i = 0; i < length; i++) {
346 try {
347 mObservers.getBroadcastItem(i).interfaceAdded(iface);
348 } catch (RemoteException e) {
349 } catch (RuntimeException e) {
350 }
San Mehat4d02d002010-01-22 16:07:46 -0800351 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700352 } finally {
353 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800354 }
355 }
356
357 /**
358 * Notify our observers of an interface removal.
359 */
360 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700361 // netd already clears out quota and alerts for removed ifaces; update
362 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700363 mActiveAlerts.remove(iface);
364 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700365
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800366 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700367 try {
368 for (int i = 0; i < length; i++) {
369 try {
370 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
371 } catch (RemoteException e) {
372 } catch (RuntimeException e) {
373 }
San Mehat4d02d002010-01-22 16:07:46 -0800374 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700375 } finally {
376 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800377 }
378 }
379
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700380 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700381 * Notify our observers of a limit reached.
382 */
383 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800384 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700385 try {
386 for (int i = 0; i < length; i++) {
387 try {
388 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
389 } catch (RemoteException e) {
390 } catch (RuntimeException e) {
391 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700392 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700393 } finally {
394 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700395 }
396 }
397
398 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700399 * Notify our observers of a change in the data activity state of the interface
400 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700401 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
402 boolean fromRadio) {
403 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
404 if (isMobile) {
405 if (!fromRadio) {
406 if (mMobileActivityFromRadio) {
407 // If this call is not coming from a report from the radio itself, but we
408 // have previously received reports from the radio, then we will take the
409 // power state to just be whatever the radio last reported.
410 powerState = mLastPowerStateFromRadio;
411 }
412 } else {
413 mMobileActivityFromRadio = true;
414 }
415 if (mLastPowerStateFromRadio != powerState) {
416 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700417 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700418 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700419 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700420 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700421 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700422 }
423
424 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
425 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
426
427 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
428 // Report the change in data activity. We don't do this if this is a change
429 // on the mobile network, that is not coming from the radio itself, and we
430 // have previously seen change reports from the radio. In that case only
431 // the radio is the authority for the current state.
432 final int length = mObservers.beginBroadcast();
433 try {
434 for (int i = 0; i < length; i++) {
435 try {
436 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
437 Integer.toString(type), isActive, tsNanos);
438 } catch (RemoteException e) {
439 } catch (RuntimeException e) {
440 }
441 }
442 } finally {
443 mObservers.finishBroadcast();
444 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700445 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800446
447 boolean report = false;
448 synchronized (mIdleTimerLock) {
449 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700450 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800451 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700452 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800453 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700454 if (mNetworkActive != isActive) {
455 mNetworkActive = isActive;
456 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800457 }
458 }
459 if (report) {
460 reportNetworkActive();
461 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700462 }
463
464 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700465 * Prepare native daemon once connected, enabling modules and pushing any
466 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700467 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700468 private void prepareNativeDaemon() {
469 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700470
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700471 // only enable bandwidth control when support exists
472 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
473 if (hasKernelSupport) {
474 Slog.d(TAG, "enabling bandwidth control");
475 try {
476 mConnector.execute("bandwidth", "enable");
477 mBandwidthControlEnabled = true;
478 } catch (NativeDaemonConnectorException e) {
479 Log.wtf(TAG, "problem enabling bandwidth controls", e);
480 }
481 } else {
482 Slog.d(TAG, "not enabling bandwidth control");
483 }
484
485 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
486
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700487 if (mBandwidthControlEnabled) {
488 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800489 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700490 } catch (RemoteException e) {
491 }
492 }
493
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700494 // push any existing quota or UID rules
495 synchronized (mQuotaLock) {
496 int size = mActiveQuotas.size();
497 if (size > 0) {
498 Slog.d(TAG, "pushing " + size + " active quota rules");
499 final HashMap<String, Long> activeQuotas = mActiveQuotas;
500 mActiveQuotas = Maps.newHashMap();
501 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
502 setInterfaceQuota(entry.getKey(), entry.getValue());
503 }
504 }
505
506 size = mActiveAlerts.size();
507 if (size > 0) {
508 Slog.d(TAG, "pushing " + size + " active alert rules");
509 final HashMap<String, Long> activeAlerts = mActiveAlerts;
510 mActiveAlerts = Maps.newHashMap();
511 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
512 setInterfaceAlert(entry.getKey(), entry.getValue());
513 }
514 }
515
516 size = mUidRejectOnQuota.size();
517 if (size > 0) {
518 Slog.d(TAG, "pushing " + size + " active uid rules");
519 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
520 mUidRejectOnQuota = new SparseBooleanArray();
521 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
522 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
523 }
524 }
525 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700526
527 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700528 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700529 }
San Mehat4d02d002010-01-22 16:07:46 -0800530
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900531 /**
532 * Notify our observers of a new or updated interface address.
533 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900534 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900535 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700536 try {
537 for (int i = 0; i < length; i++) {
538 try {
539 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
540 } catch (RemoteException e) {
541 } catch (RuntimeException e) {
542 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900543 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700544 } finally {
545 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900546 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900547 }
548
549 /**
550 * Notify our observers of a deleted interface address.
551 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900552 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900553 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700554 try {
555 for (int i = 0; i < length; i++) {
556 try {
557 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
558 } catch (RemoteException e) {
559 } catch (RuntimeException e) {
560 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900561 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700562 } finally {
563 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900564 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900565 }
566
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900567 /**
568 * Notify our observers of DNS server information received.
569 */
570 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
571 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700572 try {
573 for (int i = 0; i < length; i++) {
574 try {
575 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
576 addresses);
577 } catch (RemoteException e) {
578 } catch (RuntimeException e) {
579 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900580 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700581 } finally {
582 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900583 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900584 }
585
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900586 /**
587 * Notify our observers of a route change.
588 */
589 private void notifyRouteChange(String action, RouteInfo route) {
590 final int length = mObservers.beginBroadcast();
591 try {
592 for (int i = 0; i < length; i++) {
593 try {
594 if (action.equals("updated")) {
595 mObservers.getBroadcastItem(i).routeUpdated(route);
596 } else {
597 mObservers.getBroadcastItem(i).routeRemoved(route);
598 }
599 } catch (RemoteException e) {
600 } catch (RuntimeException e) {
601 }
602 }
603 } finally {
604 mObservers.finishBroadcast();
605 }
606 }
607
San Mehat873f2142010-01-14 10:25:07 -0800608 //
609 // Netd Callback handling
610 //
611
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700612 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
613 @Override
San Mehat873f2142010-01-14 10:25:07 -0800614 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700615 // event is dispatched from internal NDC thread, so we prepare the
616 // daemon back on main thread.
617 if (mConnectedSignal != null) {
618 mConnectedSignal.countDown();
619 mConnectedSignal = null;
620 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700621 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700622 @Override
623 public void run() {
624 prepareNativeDaemon();
625 }
626 });
627 }
San Mehat873f2142010-01-14 10:25:07 -0800628 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700629
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700630 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800631 public boolean onCheckHoldWakeLock(int code) {
632 return code == NetdResponseCode.InterfaceClassActivity;
633 }
634
635 @Override
San Mehat873f2142010-01-14 10:25:07 -0800636 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900637 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700638 switch (code) {
639 case NetdResponseCode.InterfaceChange:
640 /*
641 * a network interface change occured
642 * Format: "NNN Iface added <name>"
643 * "NNN Iface removed <name>"
644 * "NNN Iface changed <name> <up/down>"
645 * "NNN Iface linkstatus <name> <up/down>"
646 */
647 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900648 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700649 }
650 if (cooked[2].equals("added")) {
651 notifyInterfaceAdded(cooked[3]);
652 return true;
653 } else if (cooked[2].equals("removed")) {
654 notifyInterfaceRemoved(cooked[3]);
655 return true;
656 } else if (cooked[2].equals("changed") && cooked.length == 5) {
657 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
658 return true;
659 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
660 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
661 return true;
662 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900663 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700664 // break;
665 case NetdResponseCode.BandwidthControl:
666 /*
667 * Bandwidth control needs some attention
668 * Format: "NNN limit alert <alertName> <ifaceName>"
669 */
670 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900671 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700672 }
673 if (cooked[2].equals("alert")) {
674 notifyLimitReached(cooked[3], cooked[4]);
675 return true;
676 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900677 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700678 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700679 case NetdResponseCode.InterfaceClassActivity:
680 /*
681 * An network interface class state changed (active/idle)
682 * Format: "NNN IfaceClass <active/idle> <label>"
683 */
684 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900685 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700686 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700687 long timestampNanos = 0;
688 if (cooked.length == 5) {
689 try {
690 timestampNanos = Long.parseLong(cooked[4]);
691 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700692 } else {
693 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700694 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700695 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700696 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700697 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
698 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700699 return true;
700 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900701 case NetdResponseCode.InterfaceAddressChange:
702 /*
703 * A network address change occurred
704 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
705 * "NNN Address removed <addr> <iface> <flags> <scope>"
706 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900707 if (cooked.length < 7 || !cooked[1].equals("Address")) {
708 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900709 }
710
Lorenzo Colitti64483942013-11-15 18:43:52 +0900711 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900712 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900713 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900714 int flags = Integer.parseInt(cooked[5]);
715 int scope = Integer.parseInt(cooked[6]);
716 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900717 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
718 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900719 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900720 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900721 }
722
723 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900724 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900725 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900726 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900727 }
728 return true;
729 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900730 case NetdResponseCode.InterfaceDnsServerInfo:
731 /*
732 * Information about available DNS servers has been received.
733 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
734 */
735 long lifetime; // Actually a 32-bit unsigned integer.
736
737 if (cooked.length == 6 &&
738 cooked[1].equals("DnsInfo") &&
739 cooked[2].equals("servers")) {
740 try {
741 lifetime = Long.parseLong(cooked[4]);
742 } catch (NumberFormatException e) {
743 throw new IllegalStateException(errorMessage);
744 }
745 String[] servers = cooked[5].split(",");
746 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
747 }
748 return true;
749 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900750 case NetdResponseCode.RouteChange:
751 /*
752 * A route has been updated or removed.
753 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
754 */
755 if (!cooked[1].equals("Route") || cooked.length < 6) {
756 throw new IllegalStateException(errorMessage);
757 }
758
759 String via = null;
760 String dev = null;
761 boolean valid = true;
762 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
763 if (cooked[i].equals("dev")) {
764 if (dev == null) {
765 dev = cooked[i+1];
766 } else {
767 valid = false; // Duplicate interface.
768 }
769 } else if (cooked[i].equals("via")) {
770 if (via == null) {
771 via = cooked[i+1];
772 } else {
773 valid = false; // Duplicate gateway.
774 }
775 } else {
776 valid = false; // Unknown syntax.
777 }
778 }
779 if (valid) {
780 try {
781 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
782 InetAddress gateway = null;
783 if (via != null) gateway = InetAddress.parseNumericAddress(via);
784 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
785 notifyRouteChange(cooked[2], route);
786 return true;
787 } catch (IllegalArgumentException e) {}
788 }
789 throw new IllegalStateException(errorMessage);
790 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700791 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800792 }
793 return false;
San Mehat873f2142010-01-14 10:25:07 -0800794 }
795 }
796
San Mehated4fc8a2010-01-22 12:28:36 -0800797
San Mehat873f2142010-01-14 10:25:07 -0800798 //
799 // INetworkManagementService members
800 //
801
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800802 @Override
803 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800804 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700805 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800806 return NativeDaemonEvent.filterMessageList(
807 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700808 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800809 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700810 }
San Mehated4fc8a2010-01-22 12:28:36 -0800811 }
812
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800813 @Override
814 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800815 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800816
817 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700818 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800819 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700820 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800821 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700822 }
San Mehated4fc8a2010-01-22 12:28:36 -0800823
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800824 event.checkCode(InterfaceGetCfgResult);
825
826 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
827 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800828
Kenny Roota80ce062010-06-01 13:23:53 -0700829 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800830 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700831 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800832 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800833 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800834 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700835 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800836 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800837 } catch (IllegalArgumentException iae) {
838 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700839 }
840
841 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800842 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800843 } catch (NumberFormatException nfe) {
844 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700845 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800846
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800847 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
848 while (st.hasMoreTokens()) {
849 cfg.setFlag(st.nextToken());
850 }
Kenny Roota80ce062010-06-01 13:23:53 -0700851 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800852 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800853 }
San Mehated4fc8a2010-01-22 12:28:36 -0800854 return cfg;
855 }
856
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800857 @Override
858 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800859 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800860 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800861 if (linkAddr == null || linkAddr.getAddress() == null) {
862 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800863 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800864
865 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800866 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900867 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800868 for (String flag : cfg.getFlags()) {
869 cmd.appendArg(flag);
870 }
871
Kenny Roota80ce062010-06-01 13:23:53 -0700872 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800873 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700874 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800875 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700876 }
San Mehat873f2142010-01-14 10:25:07 -0800877 }
878
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800879 @Override
880 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800881 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800882 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800883 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800884 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700885 }
886
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800887 @Override
888 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800889 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800890 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800891 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800892 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700893 }
894
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800895 @Override
896 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800897 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700898 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800899 mConnector.execute(
900 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700901 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800902 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700903 }
904 }
905
Irfan Sherifff5600612011-06-16 10:26:28 -0700906 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
907 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800908 @Override
909 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800910 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700911 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800912 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700913 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800914 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700915 }
916 }
917
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800918 @Override
919 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800920 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700921 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800922 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700923 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800924 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700925 }
926 }
927
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800928 @Override
929 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800930 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700931 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800932 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700933 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800934 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700935 }
936 }
937
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800938 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700939 public void addRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700940 modifyRoute("add", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700941 }
942
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800943 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700944 public void removeRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700945 modifyRoute("remove", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700946 }
947
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700948 private void modifyRoute(String action, String netId, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800949 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700950
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700951 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700952
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700953 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700954 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700955 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900956 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700957 if (route.hasGateway()) {
958 cmd.appendArg(route.getGateway().getHostAddress());
959 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700960
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800961 try {
962 mConnector.execute(cmd);
963 } catch (NativeDaemonConnectorException e) {
964 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700965 }
966 }
967
968 private ArrayList<String> readRouteList(String filename) {
969 FileInputStream fstream = null;
970 ArrayList<String> list = new ArrayList<String>();
971
972 try {
973 fstream = new FileInputStream(filename);
974 DataInputStream in = new DataInputStream(fstream);
975 BufferedReader br = new BufferedReader(new InputStreamReader(in));
976 String s;
977
978 // throw away the title line
979
980 while (((s = br.readLine()) != null) && (s.length() != 0)) {
981 list.add(s);
982 }
983 } catch (IOException ex) {
984 // return current list, possibly empty
985 } finally {
986 if (fstream != null) {
987 try {
988 fstream.close();
989 } catch (IOException ex) {}
990 }
991 }
992
993 return list;
994 }
995
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800996 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700997 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800998 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700999 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1000
1001 // v4 routes listed as:
1002 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1003 for (String s : readRouteList("/proc/net/route")) {
1004 String[] fields = s.split("\t");
1005
1006 if (fields.length > 7) {
1007 String iface = fields[0];
1008
1009 if (interfaceName.equals(iface)) {
1010 String dest = fields[1];
1011 String gate = fields[2];
1012 String flags = fields[3]; // future use?
1013 String mask = fields[7];
1014 try {
1015 // address stored as a hex string, ex: 0014A8C0
1016 InetAddress destAddr =
1017 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1018 int prefixLength =
1019 NetworkUtils.netmaskIntToPrefixLength(
1020 (int)Long.parseLong(mask, 16));
1021 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1022
1023 // address stored as a hex string, ex 0014A8C0
1024 InetAddress gatewayAddr =
1025 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1026
Wink Saville7b5fd052013-03-15 05:07:04 +00001027 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001028 routes.add(route);
1029 } catch (Exception e) {
1030 Log.e(TAG, "Error parsing route " + s + " : " + e);
1031 continue;
1032 }
1033 }
1034 }
1035 }
1036
1037 // v6 routes listed as:
1038 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1039 for (String s : readRouteList("/proc/net/ipv6_route")) {
1040 String[]fields = s.split("\\s+");
1041 if (fields.length > 9) {
1042 String iface = fields[9].trim();
1043 if (interfaceName.equals(iface)) {
1044 String dest = fields[0];
1045 String prefix = fields[1];
1046 String gate = fields[4];
1047
1048 try {
1049 // prefix length stored as a hex string, ex 40
1050 int prefixLength = Integer.parseInt(prefix, 16);
1051
1052 // address stored as a 32 char hex string
1053 // ex fe800000000000000000000000000000
1054 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1055 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1056
1057 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1058
Wink Saville7b5fd052013-03-15 05:07:04 +00001059 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001060 routes.add(route);
1061 } catch (Exception e) {
1062 Log.e(TAG, "Error parsing route " + s + " : " + e);
1063 continue;
1064 }
1065 }
1066 }
1067 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001068 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001069 }
1070
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001071 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001072 public void setMtu(String iface, int mtu) {
1073 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1074
1075 final NativeDaemonEvent event;
1076 try {
1077 event = mConnector.execute("interface", "setmtu", iface, mtu);
1078 } catch (NativeDaemonConnectorException e) {
1079 throw e.rethrowAsParcelableException();
1080 }
1081 }
1082
1083 @Override
San Mehat873f2142010-01-14 10:25:07 -08001084 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001085 // TODO: remove from aidl if nobody calls externally
1086 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001087
Joe Onorato8a9b2202010-02-26 18:56:32 -08001088 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001089 }
1090
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001091 @Override
San Mehat873f2142010-01-14 10:25:07 -08001092 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001093 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001094
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001095 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001096 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001097 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001098 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001099 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001100 }
San Mehat873f2142010-01-14 10:25:07 -08001101
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001102 // 211 Forwarding enabled
1103 event.checkCode(IpFwdStatusResult);
1104 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001105 }
1106
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001107 @Override
1108 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001109 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001110 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001111 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001112 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001113 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001114 }
San Mehat873f2142010-01-14 10:25:07 -08001115 }
1116
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001117 @Override
1118 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001119 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001120 // cmd is "tether start first_start first_stop second_start second_stop ..."
1121 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001122
1123 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001124 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001125 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001126 }
Kenny Roota80ce062010-06-01 13:23:53 -07001127
1128 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001129 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001130 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001131 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001132 }
San Mehat873f2142010-01-14 10:25:07 -08001133 }
1134
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001135 @Override
1136 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001137 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001138 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001140 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001141 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001142 }
San Mehat873f2142010-01-14 10:25:07 -08001143 }
1144
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001145 @Override
1146 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001147 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001148
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001149 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001150 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001151 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001152 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001153 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001154 }
San Mehat873f2142010-01-14 10:25:07 -08001155
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001156 // 210 Tethering services started
1157 event.checkCode(TetherStatusResult);
1158 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001159 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001160
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001161 @Override
1162 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001163 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001164 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001165 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001166 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001167 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001168 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001169 List<RouteInfo> routes = new ArrayList<RouteInfo>();
1170 // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it
1171 // suitable to use as a route destination.
1172 routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface));
1173 addInterfaceToLocalNetwork(iface, routes);
San Mehat873f2142010-01-14 10:25:07 -08001174 }
1175
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001176 @Override
San Mehat873f2142010-01-14 10:25:07 -08001177 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001178 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001179 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001180 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001181 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001182 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001183 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001184 removeInterfaceFromLocalNetwork(iface);
San Mehat873f2142010-01-14 10:25:07 -08001185 }
1186
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001187 @Override
1188 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001189 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001190 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001191 return NativeDaemonEvent.filterMessageList(
1192 mConnector.executeForList("tether", "interface", "list"),
1193 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001194 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001195 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001196 }
San Mehat873f2142010-01-14 10:25:07 -08001197 }
1198
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001199 @Override
1200 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001201 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001202
1203 final Command cmd = new Command("tether", "dns", "set");
1204 for (String s : dns) {
1205 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1206 }
1207
San Mehat873f2142010-01-14 10:25:07 -08001208 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001209 mConnector.execute(cmd);
1210 } catch (NativeDaemonConnectorException e) {
1211 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001212 }
1213 }
1214
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001215 @Override
1216 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001217 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001218 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001219 return NativeDaemonEvent.filterMessageList(
1220 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001221 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001222 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001223 }
San Mehat873f2142010-01-14 10:25:07 -08001224 }
1225
jiaguo1da35f72014-01-09 16:39:59 +08001226 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1227 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1228 for (InterfaceAddress ia : addresses) {
1229 if (!ia.getAddress().isLinkLocalAddress())
1230 filtered.add(ia);
1231 }
1232 return filtered;
1233 }
1234
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001235 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001236 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001237 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001238
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001239 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1240 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001241 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001242 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001243 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001244 // Don't touch link-local routes, as link-local addresses aren't routable,
1245 // kernel creates link-local routes on all interfaces automatically
1246 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1247 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001248 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001249 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001250 InetAddress addr = NetworkUtils.getNetworkPart(
1251 ia.getAddress(), ia.getNetworkPrefixLength());
1252 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001253 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001254 }
1255
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001256 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001257 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001258 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001259 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001260 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001261 }
1262
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001263 @Override
1264 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001265 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001266 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001267 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001268 } catch (SocketException e) {
1269 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001270 }
San Mehat873f2142010-01-14 10:25:07 -08001271 }
1272
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001273 @Override
1274 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001275 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001276 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001277 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001278 } catch (SocketException e) {
1279 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001280 }
San Mehat873f2142010-01-14 10:25:07 -08001281 }
San Mehat72759df2010-01-19 13:50:37 -08001282
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001283 @Override
1284 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001285 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001286 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001287 return NativeDaemonEvent.filterMessageList(
1288 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001289 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001290 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001291 }
San Mehat72759df2010-01-19 13:50:37 -08001292 }
1293
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001294 @Override
1295 public void attachPppd(
1296 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001297 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001298 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001299 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001300 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1301 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1302 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001303 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001304 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001305 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001306 }
1307 }
1308
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001309 @Override
1310 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001311 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001312 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001313 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001314 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001315 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001316 }
San Mehat72759df2010-01-19 13:50:37 -08001317 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001318
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001319 @Override
1320 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001321 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001322 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001323 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001324 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001325 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001326 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001327 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001328 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001329 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001330 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001331 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001332 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001333 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001334 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001335 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001336 }
1337
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001338 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001339 switch (wifiConfig.getAuthType()) {
1340 case KeyMgmt.WPA_PSK:
1341 return "wpa-psk";
1342 case KeyMgmt.WPA2_PSK:
1343 return "wpa2-psk";
1344 default:
1345 return "open";
1346 }
1347 }
1348
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001349 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001350 @Override
1351 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001352 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001353 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001354 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001355 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001356 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001357 }
1358 }
1359
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001360 @Override
1361 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001362 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001363 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001364 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001365 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001366 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001367 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001368 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001369 }
1370
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001371 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001372 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001373 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001374 try {
1375 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001376 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001377 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001378 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001379 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001380 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001381 }
1382 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001383 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001384 }
1385 }
San Mehat91cac642010-03-31 14:31:36 -07001386
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001387 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001388 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001389 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1390
1391 if (DBG) Slog.d(TAG, "Adding idletimer");
1392
1393 synchronized (mIdleTimerLock) {
1394 IdleTimerParams params = mActiveIdleTimers.get(iface);
1395 if (params != null) {
1396 // the interface already has idletimer, update network count
1397 params.networkCount++;
1398 return;
1399 }
1400
1401 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001402 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1403 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001404 } catch (NativeDaemonConnectorException e) {
1405 throw e.rethrowAsParcelableException();
1406 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001407 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1408
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001409 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001410 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1411 mNetworkActive = false;
1412 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001413 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001414 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001415 notifyInterfaceClassActivity(type,
1416 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1417 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001418 }
1419 });
Haoyu Bai04124232012-06-28 15:26:19 -07001420 }
1421 }
1422
1423 @Override
1424 public void removeIdleTimer(String iface) {
1425 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1426
1427 if (DBG) Slog.d(TAG, "Removing idletimer");
1428
1429 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001430 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001431 if (params == null || --(params.networkCount) > 0) {
1432 return;
1433 }
1434
1435 try {
1436 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001437 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001438 } catch (NativeDaemonConnectorException e) {
1439 throw e.rethrowAsParcelableException();
1440 }
1441 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001442 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001443 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001444 notifyInterfaceClassActivity(params.type,
1445 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1446 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001447 }
1448 });
Haoyu Bai04124232012-06-28 15:26:19 -07001449 }
1450 }
1451
1452 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001453 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001454 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001455 try {
1456 return mStatsFactory.readNetworkStatsSummaryDev();
1457 } catch (IOException e) {
1458 throw new IllegalStateException(e);
1459 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001460 }
1461
1462 @Override
1463 public NetworkStats getNetworkStatsSummaryXt() {
1464 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001465 try {
1466 return mStatsFactory.readNetworkStatsSummaryXt();
1467 } catch (IOException e) {
1468 throw new IllegalStateException(e);
1469 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001470 }
1471
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001472 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001473 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001475 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001476 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001477 } catch (IOException e) {
1478 throw new IllegalStateException(e);
1479 }
San Mehat91cac642010-03-31 14:31:36 -07001480 }
1481
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001482 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001483 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001484 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001485
Jeff Sharkey350083e2011-06-29 10:45:16 -07001486 // silently discard when control disabled
1487 // TODO: eventually migrate to be always enabled
1488 if (!mBandwidthControlEnabled) return;
1489
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001490 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001491 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001492 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001493 }
1494
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001495 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001496 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001497 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001498 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001499 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001500 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001501 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001502 }
1503 }
1504
1505 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001506 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001507 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001508
Jeff Sharkey350083e2011-06-29 10:45:16 -07001509 // silently discard when control disabled
1510 // TODO: eventually migrate to be always enabled
1511 if (!mBandwidthControlEnabled) return;
1512
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001513 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001514 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001515 // TODO: eventually consider throwing
1516 return;
1517 }
1518
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001519 mActiveQuotas.remove(iface);
1520 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001521
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001522 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001523 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001524 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001525 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001526 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001527 }
1528 }
1529 }
1530
1531 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001532 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001533 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001534
1535 // silently discard when control disabled
1536 // TODO: eventually migrate to be always enabled
1537 if (!mBandwidthControlEnabled) return;
1538
1539 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001540 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001541 throw new IllegalStateException("setting alert requires existing quota on iface");
1542 }
1543
1544 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001545 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001546 throw new IllegalStateException("iface " + iface + " already has alert");
1547 }
1548
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001549 try {
1550 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001551 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001552 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001553 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001554 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001555 }
1556 }
1557 }
1558
1559 @Override
1560 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001561 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001562
1563 // silently discard when control disabled
1564 // TODO: eventually migrate to be always enabled
1565 if (!mBandwidthControlEnabled) return;
1566
1567 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001568 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001569 // TODO: eventually consider throwing
1570 return;
1571 }
1572
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001573 try {
1574 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001575 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001576 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001577 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001578 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001579 }
1580 }
1581 }
1582
1583 @Override
1584 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001585 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001586
1587 // silently discard when control disabled
1588 // TODO: eventually migrate to be always enabled
1589 if (!mBandwidthControlEnabled) return;
1590
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001591 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001592 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001593 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001594 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001595 }
1596 }
1597
1598 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001599 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001600 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001601
Jeff Sharkey350083e2011-06-29 10:45:16 -07001602 // silently discard when control disabled
1603 // TODO: eventually migrate to be always enabled
1604 if (!mBandwidthControlEnabled) return;
1605
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001606 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001607 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1608 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1609 // TODO: eventually consider throwing
1610 return;
1611 }
1612
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001613 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001614 mConnector.execute("bandwidth",
1615 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001616 if (rejectOnQuotaInterfaces) {
1617 mUidRejectOnQuota.put(uid, true);
1618 } else {
1619 mUidRejectOnQuota.delete(uid);
1620 }
1621 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001622 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001623 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001624 }
1625 }
1626
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001627 @Override
1628 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001629 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001630 return mBandwidthControlEnabled;
1631 }
1632
1633 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001634 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001635 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001636 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001637 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001638 } catch (IOException e) {
1639 throw new IllegalStateException(e);
1640 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001641 }
1642
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001643 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001644 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001645 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001646
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001647 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001648 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001649 final NativeDaemonEvent[] events = mConnector.executeForList(
1650 "bandwidth", "gettetherstats");
1651 for (NativeDaemonEvent event : events) {
1652 if (event.getCode() != TetheringStatsListResult) continue;
1653
1654 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1655 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1656 try {
1657 final String ifaceIn = tok.nextToken();
1658 final String ifaceOut = tok.nextToken();
1659
1660 final NetworkStats.Entry entry = new NetworkStats.Entry();
1661 entry.iface = ifaceOut;
1662 entry.uid = UID_TETHERING;
1663 entry.set = SET_DEFAULT;
1664 entry.tag = TAG_NONE;
1665 entry.rxBytes = Long.parseLong(tok.nextToken());
1666 entry.rxPackets = Long.parseLong(tok.nextToken());
1667 entry.txBytes = Long.parseLong(tok.nextToken());
1668 entry.txPackets = Long.parseLong(tok.nextToken());
1669 stats.combineValues(entry);
1670 } catch (NoSuchElementException e) {
1671 throw new IllegalStateException("problem parsing tethering stats: " + event);
1672 } catch (NumberFormatException e) {
1673 throw new IllegalStateException("problem parsing tethering stats: " + event);
1674 }
1675 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001676 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001677 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001678 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001679 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001680 }
1681
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001682 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001683 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001685
Paul Jensen13e817d2014-04-10 14:16:37 -04001686 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001687 (domains == null ? "" : domains));
1688
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001689 for (String s : servers) {
1690 InetAddress a = NetworkUtils.numericToInetAddress(s);
1691 if (a.isAnyLocalAddress() == false) {
1692 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001693 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001694 }
1695
1696 try {
1697 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001698 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001699 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001700 }
1701 }
1702
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001703 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001704 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001705 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001706 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1707 argv[0] = "users";
1708 argv[1] = "add";
1709 argv[2] = netId;
1710 int argc = 3;
1711 // Avoid overly long commands by limiting number of UID ranges per command.
1712 for (int i = 0; i < ranges.length; i++) {
1713 argv[argc++] = ranges[i].toString();
1714 if (i == (ranges.length - 1) || argc == argv.length) {
1715 try {
1716 mConnector.execute("network", Arrays.copyOf(argv, argc));
1717 } catch (NativeDaemonConnectorException e) {
1718 throw e.rethrowAsParcelableException();
1719 }
1720 argc = 3;
1721 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001722 }
1723 }
1724
1725 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001726 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001727 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001728 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1729 argv[0] = "users";
1730 argv[1] = "remove";
1731 argv[2] = netId;
1732 int argc = 3;
1733 // Avoid overly long commands by limiting number of UID ranges per command.
1734 for (int i = 0; i < ranges.length; i++) {
1735 argv[argc++] = ranges[i].toString();
1736 if (i == (ranges.length - 1) || argc == argv.length) {
1737 try {
1738 mConnector.execute("network", Arrays.copyOf(argv, argc));
1739 } catch (NativeDaemonConnectorException e) {
1740 throw e.rethrowAsParcelableException();
1741 }
1742 argc = 3;
1743 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001744 }
1745 }
1746
1747 @Override
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001748 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001749 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001750 try {
1751 mConnector.execute("firewall", enabled ? "enable" : "disable");
1752 mFirewallEnabled = enabled;
1753 } catch (NativeDaemonConnectorException e) {
1754 throw e.rethrowAsParcelableException();
1755 }
1756 }
1757
1758 @Override
1759 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001760 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001761 return mFirewallEnabled;
1762 }
1763
1764 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001765 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001766 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001767 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001768 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001769 try {
1770 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1771 } catch (NativeDaemonConnectorException e) {
1772 throw e.rethrowAsParcelableException();
1773 }
1774 }
1775
1776 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001777 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001778 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001779 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001780 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001781 try {
1782 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1783 } catch (NativeDaemonConnectorException e) {
1784 throw e.rethrowAsParcelableException();
1785 }
1786 }
1787
1788 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001789 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001790 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001791 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001792 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001793 try {
1794 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1795 } catch (NativeDaemonConnectorException e) {
1796 throw e.rethrowAsParcelableException();
1797 }
1798 }
1799
1800 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001801 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001802 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001803 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001804 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001805 try {
1806 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1807 } catch (NativeDaemonConnectorException e) {
1808 throw e.rethrowAsParcelableException();
1809 }
1810 }
1811
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001812 private static void enforceSystemUid() {
1813 final int uid = Binder.getCallingUid();
1814 if (uid != Process.SYSTEM_UID) {
1815 throw new SecurityException("Only available to AID_SYSTEM");
1816 }
1817 }
1818
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001819 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001820 public void startClatd(String interfaceName) throws IllegalStateException {
1821 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1822
1823 try {
1824 mConnector.execute("clatd", "start", interfaceName);
1825 } catch (NativeDaemonConnectorException e) {
1826 throw e.rethrowAsParcelableException();
1827 }
1828 }
1829
1830 @Override
1831 public void stopClatd() throws IllegalStateException {
1832 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1833
1834 try {
1835 mConnector.execute("clatd", "stop");
1836 } catch (NativeDaemonConnectorException e) {
1837 throw e.rethrowAsParcelableException();
1838 }
1839 }
1840
1841 @Override
1842 public boolean isClatdStarted() {
1843 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1844
1845 final NativeDaemonEvent event;
1846 try {
1847 event = mConnector.execute("clatd", "status");
1848 } catch (NativeDaemonConnectorException e) {
1849 throw e.rethrowAsParcelableException();
1850 }
1851
1852 event.checkCode(ClatdStatusResult);
1853 return event.getMessage().endsWith("started");
1854 }
1855
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001856 @Override
1857 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1858 mNetworkActivityListeners.register(listener);
1859 }
1860
1861 @Override
1862 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1863 mNetworkActivityListeners.unregister(listener);
1864 }
1865
1866 @Override
1867 public boolean isNetworkActive() {
1868 synchronized (mNetworkActivityListeners) {
1869 return mNetworkActive || mActiveIdleTimers.isEmpty();
1870 }
1871 }
1872
1873 private void reportNetworkActive() {
1874 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001875 try {
1876 for (int i = 0; i < length; i++) {
1877 try {
1878 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1879 } catch (RemoteException e) {
1880 } catch (RuntimeException e) {
1881 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001882 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001883 } finally {
1884 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001885 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001886 }
1887
Mattias Falk8b47b362011-08-23 14:15:13 +02001888 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001889 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001890 public void monitor() {
1891 if (mConnector != null) {
1892 mConnector.monitor();
1893 }
1894 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001895
1896 @Override
1897 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1898 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1899
Robert Greenwalt470fd722012-01-18 12:51:15 -08001900 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1901 mConnector.dump(fd, pw, args);
1902 pw.println();
1903
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001904 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001905 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1906 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1907 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001908
1909 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001910 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1911 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001912 }
1913
1914 synchronized (mUidRejectOnQuota) {
1915 pw.print("UID reject on quota ifaces: [");
1916 final int size = mUidRejectOnQuota.size();
1917 for (int i = 0; i < size; i++) {
1918 pw.print(mUidRejectOnQuota.keyAt(i));
1919 if (i < size - 1) pw.print(",");
1920 }
1921 pw.println("]");
1922 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001923
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001924 synchronized (mIdleTimerLock) {
1925 pw.println("Idle timers:");
1926 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1927 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1928 IdleTimerParams params = ent.getValue();
1929 pw.print(" timeout="); pw.print(params.timeout);
1930 pw.print(" type="); pw.print(params.type);
1931 pw.print(" networkCount="); pw.println(params.networkCount);
1932 }
1933 }
1934
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001935 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001936 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001937
Robert Greenwalt568891d2014-04-04 13:38:00 -07001938 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001939 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001940 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1941
1942 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001943 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001944 } catch (NativeDaemonConnectorException e) {
1945 throw e.rethrowAsParcelableException();
1946 }
1947 }
1948
Robert Greenwalt568891d2014-04-04 13:38:00 -07001949 @Override
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001950 public void createVirtualNetwork(int netId, boolean hasDNS, boolean secure) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001951 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1952
1953 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001954 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0",
1955 secure ? "1" : "0");
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001956 } catch (NativeDaemonConnectorException e) {
1957 throw e.rethrowAsParcelableException();
1958 }
1959 }
1960
1961 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001962 public void removeNetwork(int netId) {
1963 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1964
1965 try {
1966 mConnector.execute("network", "destroy", netId);
1967 } catch (NativeDaemonConnectorException e) {
1968 throw e.rethrowAsParcelableException();
1969 }
1970 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001971
1972 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001973 public void addInterfaceToNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001974 modifyInterfaceInNetwork("add", "" + netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04001975 }
1976
1977 @Override
1978 public void removeInterfaceFromNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001979 modifyInterfaceInNetwork("remove", "" + netId, iface);
1980 }
Paul Jensen992f2522014-04-28 10:33:11 -04001981
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001982 private void modifyInterfaceInNetwork(String action, String netId, String iface) {
1983 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen992f2522014-04-28 10:33:11 -04001984 try {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001985 mConnector.execute("network", "interface", action, netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04001986 } catch (NativeDaemonConnectorException e) {
1987 throw e.rethrowAsParcelableException();
1988 }
1989 }
1990
1991 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001992 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001993 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1994
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001995 final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001996
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07001997 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001998 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07001999 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002000 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002001 if (routeInfo.hasGateway()) {
2002 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2003 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002004
2005 try {
2006 mConnector.execute(cmd);
2007 } catch (NativeDaemonConnectorException e) {
2008 throw e.rethrowAsParcelableException();
2009 }
2010 }
2011
2012 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002013 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002014 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2015
2016 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002017 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002018 } catch (NativeDaemonConnectorException e) {
2019 throw e.rethrowAsParcelableException();
2020 }
2021 }
2022
2023 @Override
2024 public void clearDefaultNetId() {
2025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2026
2027 try {
2028 mConnector.execute("network", "default", "clear");
2029 } catch (NativeDaemonConnectorException e) {
2030 throw e.rethrowAsParcelableException();
2031 }
2032 }
2033
2034 @Override
2035 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2036 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2037
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002038 final Command cmd = new Command("network", "permission", "user", "set");
2039 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2040 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002041 for (int i=0; i<uids.length; i++) {
2042 cmd.appendArg(uids[i]);
2043 }
2044
2045 try {
2046 mConnector.execute(cmd);
2047 } catch (NativeDaemonConnectorException e) {
2048 throw e.rethrowAsParcelableException();
2049 }
2050 }
2051
2052 @Override
2053 public void clearPermission(int[] uids) {
2054 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2055
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002056 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002057 for (int i=0; i<uids.length; i++) {
2058 cmd.appendArg(uids[i]);
2059 }
2060
2061 try {
2062 mConnector.execute(cmd);
2063 } catch (NativeDaemonConnectorException e) {
2064 throw e.rethrowAsParcelableException();
2065 }
2066 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002067
2068 @Override
2069 public void allowProtect(int uid) {
2070 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2071
2072 try {
2073 mConnector.execute("network", "protect", "allow", uid);
2074 } catch (NativeDaemonConnectorException e) {
2075 throw e.rethrowAsParcelableException();
2076 }
2077 }
2078
2079 @Override
2080 public void denyProtect(int uid) {
2081 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2082
2083 try {
2084 mConnector.execute("network", "protect", "deny", uid);
2085 } catch (NativeDaemonConnectorException e) {
2086 throw e.rethrowAsParcelableException();
2087 }
2088 }
2089
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002090 @Override
2091 public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) {
2092 modifyInterfaceInNetwork("add", "local", iface);
2093
2094 for (RouteInfo route : routes) {
2095 if (!route.isDefaultRoute()) {
2096 modifyRoute("add", "local", route);
2097 }
2098 }
2099 }
2100
2101 @Override
2102 public void removeInterfaceFromLocalNetwork(String iface) {
2103 modifyInterfaceInNetwork("remove", "local", iface);
2104 }
San Mehat873f2142010-01-14 10:25:07 -08002105}