blob: 6144078cf552f799369f4e7d3b40934788126051 [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 Colitti4b0f8e62014-09-19 01:49:05 +090028import static android.net.RouteInfo.RTN_THROW;
29import static android.net.RouteInfo.RTN_UNICAST;
30import static android.net.RouteInfo.RTN_UNREACHABLE;
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -070031import static android.system.OsConstants.AF_INET;
32import static android.system.OsConstants.AF_INET6;
Lorenzo Colitti79751842013-02-28 16:16:03 +090033import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080034import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080036import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
37import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
38import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
39import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070040import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080041import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070042import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070043
San Mehat873f2142010-01-14 10:25:07 -080044import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080045import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080046import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070047import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090048import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080049import android.net.LinkAddress;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070050import android.net.Network;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070051import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080052import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070053import android.net.RouteInfo;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040054import android.net.UidRange;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080055import android.net.wifi.WifiConfiguration;
56import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070057import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070058import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070059import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080060import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080061import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080062import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070063import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080064import android.os.RemoteCallbackList;
65import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070066import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070067import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080068import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070069import android.telephony.DataConnectionRealTimeInfo;
70import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070071import android.telephony.SubscriptionManager;
Wink Saville67e07892014-06-18 16:43:14 -070072import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080073import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080074import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070075import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080076
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070077import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070078import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070079import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080080import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070081import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070082import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070083import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070084
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070085import java.io.BufferedReader;
86import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080087import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070088import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070089import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070090import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070091import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070092import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070093import java.net.Inet4Address;
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -070094import java.net.Inet6Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070095import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070096import java.net.InterfaceAddress;
97import java.net.NetworkInterface;
98import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070099import java.util.ArrayList;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400100import java.util.Arrays;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700101import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +0800102import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700103import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700104import java.util.NoSuchElementException;
105import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700106import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -0800107
108/**
109 * @hide
110 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700111public class NetworkManagementService extends INetworkManagementService.Stub
112 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700113 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700114 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700115 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900116 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700117
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400118 private static final int MAX_UID_RANGES_PER_COMMAND = 10;
119
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700120 /**
121 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
122 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
123 */
124 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
125
San Mehat873f2142010-01-14 10:25:07 -0800126 class NetdResponseCode {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -0700127 /* Keep in sync with system/netd/server/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800128 public static final int InterfaceListResult = 110;
129 public static final int TetherInterfaceListResult = 111;
130 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800131 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700132 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800133
134 public static final int TetherStatusResult = 210;
135 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800136 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800137 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700138 public static final int InterfaceRxCounterResult = 216;
139 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700140 public static final int QuotaCounterResult = 220;
141 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800142 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900143 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800144
145 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700146 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700147 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900148 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900149 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900150 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800151 }
152
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700153 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
154
San Mehat873f2142010-01-14 10:25:07 -0800155 /**
156 * Binder context for this service
157 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700158 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800159
160 /**
161 * connector object for communicating with netd
162 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700163 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800164
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700165 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700166 private final Handler mDaemonHandler;
167 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700168
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800169 private IBatteryStats mBatteryStats;
170
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700171 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700172 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700173
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800174 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
175 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800176
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700177 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
178
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700179 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700180 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700181 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700182 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700183 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700184 /** Set of UIDs with active reject rules. */
185 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
186
Haoyu Bai04124232012-06-28 15:26:19 -0700187 private Object mIdleTimerLock = new Object();
188 /** Set of interfaces with active idle timers. */
189 private static class IdleTimerParams {
190 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800191 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700192 public int networkCount;
193
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800194 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700195 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800196 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700197 this.networkCount = 1;
198 }
199 }
200 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
201
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700202 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700203 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700204
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700205 private boolean mMobileActivityFromRadio = false;
206 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
207
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800208 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
209 new RemoteCallbackList<INetworkActivityListener>();
210 private boolean mNetworkActive;
211
San Mehat873f2142010-01-14 10:25:07 -0800212 /**
213 * Constructs a new NetworkManagementService instance
214 *
215 * @param context Binder context for this service
216 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900217 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800218 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800219
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700220 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
221 mFgHandler = new Handler(FgThread.get().getLooper());
222
Marco Nelissen62dbb222010-02-18 10:56:30 -0800223 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700224 mConnector = null;
225 mThread = null;
226 mDaemonHandler = null;
227 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800228 return;
229 }
230
Dianne Hackborn4590e522014-03-24 13:36:46 -0700231 // Don't need this wake lock, since we now have a time stamp for when
232 // the network actually went inactive. (It might be nice to still do this,
233 // but I don't want to do it through the power manager because that pollutes the
234 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700235 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700236 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800237
San Mehat873f2142010-01-14 10:25:07 -0800238 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700239 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
240 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700241 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700242
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700243 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Saville67e07892014-06-18 16:43:14 -0700244
245 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUB_ID,
Wink Savillefb40dd42014-06-12 17:02:31 -0700246 mDaemonHandler.getLooper()) {
Wink Saville67e07892014-06-18 16:43:14 -0700247 @Override
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700248 public void onDataConnectionRealTimeInfoChanged(
249 DataConnectionRealTimeInfo dcRtInfo) {
Wink Saville67e07892014-06-18 16:43:14 -0700250 if (DBG) Slog.d(TAG, "onDataConnectionRealTimeInfoChanged: " + dcRtInfo);
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700251 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
252 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700253 }
254 };
Wink Saville67e07892014-06-18 16:43:14 -0700255 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
256 if (tm != null) {
257 tm.listen(mPhoneStateListener,
258 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO);
259 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700260
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700261 // Add ourself to the Watchdog monitors.
262 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700263 }
264
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900265 static NetworkManagementService create(Context context,
266 String socket) throws InterruptedException {
267 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700268 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700269 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
270 service.mThread.start();
271 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700272 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700273 if (DBG) Slog.d(TAG, "Connected");
274 return service;
San Mehat873f2142010-01-14 10:25:07 -0800275 }
276
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900277 public static NetworkManagementService create(Context context) throws InterruptedException {
278 return create(context, NETD_SOCKET_NAME);
279 }
280
Jeff Sharkey350083e2011-06-29 10:45:16 -0700281 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700282 prepareNativeDaemon();
283 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700284 }
285
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800286 private IBatteryStats getBatteryStats() {
287 synchronized (this) {
288 if (mBatteryStats != null) {
289 return mBatteryStats;
290 }
291 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
292 BatteryStats.SERVICE_NAME));
293 return mBatteryStats;
294 }
295 }
296
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800297 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800298 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800299 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800301 }
302
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800303 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800304 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800305 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800306 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800307 }
308
309 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700310 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800311 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700312 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800313 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700314 try {
315 for (int i = 0; i < length; i++) {
316 try {
317 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
318 } catch (RemoteException e) {
319 } catch (RuntimeException e) {
320 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700321 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700322 } finally {
323 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700324 }
325 }
326
327 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700328 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700329 * (typically, an Ethernet cable has been plugged-in or unplugged).
330 */
331 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800332 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700333 try {
334 for (int i = 0; i < length; i++) {
335 try {
336 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
337 } catch (RemoteException e) {
338 } catch (RuntimeException e) {
339 }
San Mehat4d02d002010-01-22 16:07:46 -0800340 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700341 } finally {
342 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800343 }
344 }
345
346 /**
347 * Notify our observers of an interface addition.
348 */
349 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800350 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700351 try {
352 for (int i = 0; i < length; i++) {
353 try {
354 mObservers.getBroadcastItem(i).interfaceAdded(iface);
355 } catch (RemoteException e) {
356 } catch (RuntimeException e) {
357 }
San Mehat4d02d002010-01-22 16:07:46 -0800358 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700359 } finally {
360 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800361 }
362 }
363
364 /**
365 * Notify our observers of an interface removal.
366 */
367 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700368 // netd already clears out quota and alerts for removed ifaces; update
369 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700370 mActiveAlerts.remove(iface);
371 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700372
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800373 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700374 try {
375 for (int i = 0; i < length; i++) {
376 try {
377 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
378 } catch (RemoteException e) {
379 } catch (RuntimeException e) {
380 }
San Mehat4d02d002010-01-22 16:07:46 -0800381 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700382 } finally {
383 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800384 }
385 }
386
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700387 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700388 * Notify our observers of a limit reached.
389 */
390 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800391 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700392 try {
393 for (int i = 0; i < length; i++) {
394 try {
395 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
396 } catch (RemoteException e) {
397 } catch (RuntimeException e) {
398 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700399 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700400 } finally {
401 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700402 }
403 }
404
405 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700406 * Notify our observers of a change in the data activity state of the interface
407 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700408 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
409 boolean fromRadio) {
410 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
411 if (isMobile) {
412 if (!fromRadio) {
413 if (mMobileActivityFromRadio) {
414 // If this call is not coming from a report from the radio itself, but we
415 // have previously received reports from the radio, then we will take the
416 // power state to just be whatever the radio last reported.
417 powerState = mLastPowerStateFromRadio;
418 }
419 } else {
420 mMobileActivityFromRadio = true;
421 }
422 if (mLastPowerStateFromRadio != powerState) {
423 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700424 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700425 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700426 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700427 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700428 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700429 }
430
431 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
432 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
433
434 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
435 // Report the change in data activity. We don't do this if this is a change
436 // on the mobile network, that is not coming from the radio itself, and we
437 // have previously seen change reports from the radio. In that case only
438 // the radio is the authority for the current state.
439 final int length = mObservers.beginBroadcast();
440 try {
441 for (int i = 0; i < length; i++) {
442 try {
443 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
444 Integer.toString(type), isActive, tsNanos);
445 } catch (RemoteException e) {
446 } catch (RuntimeException e) {
447 }
448 }
449 } finally {
450 mObservers.finishBroadcast();
451 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700452 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800453
454 boolean report = false;
455 synchronized (mIdleTimerLock) {
456 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700457 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800458 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700459 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800460 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700461 if (mNetworkActive != isActive) {
462 mNetworkActive = isActive;
463 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800464 }
465 }
466 if (report) {
467 reportNetworkActive();
468 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700469 }
470
471 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700472 * Prepare native daemon once connected, enabling modules and pushing any
473 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700474 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700475 private void prepareNativeDaemon() {
476 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700477
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700478 // only enable bandwidth control when support exists
479 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
480 if (hasKernelSupport) {
481 Slog.d(TAG, "enabling bandwidth control");
482 try {
483 mConnector.execute("bandwidth", "enable");
484 mBandwidthControlEnabled = true;
485 } catch (NativeDaemonConnectorException e) {
486 Log.wtf(TAG, "problem enabling bandwidth controls", e);
487 }
488 } else {
489 Slog.d(TAG, "not enabling bandwidth control");
490 }
491
492 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
493
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700494 if (mBandwidthControlEnabled) {
495 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800496 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700497 } catch (RemoteException e) {
498 }
499 }
500
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700501 // push any existing quota or UID rules
502 synchronized (mQuotaLock) {
503 int size = mActiveQuotas.size();
504 if (size > 0) {
505 Slog.d(TAG, "pushing " + size + " active quota rules");
506 final HashMap<String, Long> activeQuotas = mActiveQuotas;
507 mActiveQuotas = Maps.newHashMap();
508 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
509 setInterfaceQuota(entry.getKey(), entry.getValue());
510 }
511 }
512
513 size = mActiveAlerts.size();
514 if (size > 0) {
515 Slog.d(TAG, "pushing " + size + " active alert rules");
516 final HashMap<String, Long> activeAlerts = mActiveAlerts;
517 mActiveAlerts = Maps.newHashMap();
518 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
519 setInterfaceAlert(entry.getKey(), entry.getValue());
520 }
521 }
522
523 size = mUidRejectOnQuota.size();
524 if (size > 0) {
525 Slog.d(TAG, "pushing " + size + " active uid rules");
526 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
527 mUidRejectOnQuota = new SparseBooleanArray();
528 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
529 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
530 }
531 }
532 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700533
534 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700535 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700536 }
San Mehat4d02d002010-01-22 16:07:46 -0800537
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900538 /**
539 * Notify our observers of a new or updated interface address.
540 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900541 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900542 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700543 try {
544 for (int i = 0; i < length; i++) {
545 try {
546 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
547 } catch (RemoteException e) {
548 } catch (RuntimeException e) {
549 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900550 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700551 } finally {
552 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900553 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900554 }
555
556 /**
557 * Notify our observers of a deleted interface address.
558 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900559 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900560 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700561 try {
562 for (int i = 0; i < length; i++) {
563 try {
564 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
565 } catch (RemoteException e) {
566 } catch (RuntimeException e) {
567 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900568 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700569 } finally {
570 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900571 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900572 }
573
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900574 /**
575 * Notify our observers of DNS server information received.
576 */
577 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
578 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700579 try {
580 for (int i = 0; i < length; i++) {
581 try {
582 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
583 addresses);
584 } catch (RemoteException e) {
585 } catch (RuntimeException e) {
586 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900587 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700588 } finally {
589 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900590 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900591 }
592
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900593 /**
594 * Notify our observers of a route change.
595 */
596 private void notifyRouteChange(String action, RouteInfo route) {
597 final int length = mObservers.beginBroadcast();
598 try {
599 for (int i = 0; i < length; i++) {
600 try {
601 if (action.equals("updated")) {
602 mObservers.getBroadcastItem(i).routeUpdated(route);
603 } else {
604 mObservers.getBroadcastItem(i).routeRemoved(route);
605 }
606 } catch (RemoteException e) {
607 } catch (RuntimeException e) {
608 }
609 }
610 } finally {
611 mObservers.finishBroadcast();
612 }
613 }
614
San Mehat873f2142010-01-14 10:25:07 -0800615 //
616 // Netd Callback handling
617 //
618
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700619 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
620 @Override
San Mehat873f2142010-01-14 10:25:07 -0800621 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700622 // event is dispatched from internal NDC thread, so we prepare the
623 // daemon back on main thread.
624 if (mConnectedSignal != null) {
625 mConnectedSignal.countDown();
626 mConnectedSignal = null;
627 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700628 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700629 @Override
630 public void run() {
631 prepareNativeDaemon();
632 }
633 });
634 }
San Mehat873f2142010-01-14 10:25:07 -0800635 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700636
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700637 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800638 public boolean onCheckHoldWakeLock(int code) {
639 return code == NetdResponseCode.InterfaceClassActivity;
640 }
641
642 @Override
San Mehat873f2142010-01-14 10:25:07 -0800643 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900644 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700645 switch (code) {
646 case NetdResponseCode.InterfaceChange:
647 /*
648 * a network interface change occured
649 * Format: "NNN Iface added <name>"
650 * "NNN Iface removed <name>"
651 * "NNN Iface changed <name> <up/down>"
652 * "NNN Iface linkstatus <name> <up/down>"
653 */
654 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900655 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700656 }
657 if (cooked[2].equals("added")) {
658 notifyInterfaceAdded(cooked[3]);
659 return true;
660 } else if (cooked[2].equals("removed")) {
661 notifyInterfaceRemoved(cooked[3]);
662 return true;
663 } else if (cooked[2].equals("changed") && cooked.length == 5) {
664 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
665 return true;
666 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
667 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
668 return true;
669 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900670 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700671 // break;
672 case NetdResponseCode.BandwidthControl:
673 /*
674 * Bandwidth control needs some attention
675 * Format: "NNN limit alert <alertName> <ifaceName>"
676 */
677 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900678 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700679 }
680 if (cooked[2].equals("alert")) {
681 notifyLimitReached(cooked[3], cooked[4]);
682 return true;
683 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900684 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700685 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700686 case NetdResponseCode.InterfaceClassActivity:
687 /*
688 * An network interface class state changed (active/idle)
689 * Format: "NNN IfaceClass <active/idle> <label>"
690 */
691 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900692 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700693 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700694 long timestampNanos = 0;
695 if (cooked.length == 5) {
696 try {
697 timestampNanos = Long.parseLong(cooked[4]);
698 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700699 } else {
700 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700701 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700702 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700703 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700704 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
705 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700706 return true;
707 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900708 case NetdResponseCode.InterfaceAddressChange:
709 /*
710 * A network address change occurred
711 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
712 * "NNN Address removed <addr> <iface> <flags> <scope>"
713 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900714 if (cooked.length < 7 || !cooked[1].equals("Address")) {
715 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900716 }
717
Lorenzo Colitti64483942013-11-15 18:43:52 +0900718 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900719 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900720 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900721 int flags = Integer.parseInt(cooked[5]);
722 int scope = Integer.parseInt(cooked[6]);
723 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900724 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
725 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900726 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900727 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900728 }
729
730 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900731 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900732 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900733 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900734 }
735 return true;
736 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900737 case NetdResponseCode.InterfaceDnsServerInfo:
738 /*
739 * Information about available DNS servers has been received.
740 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
741 */
742 long lifetime; // Actually a 32-bit unsigned integer.
743
744 if (cooked.length == 6 &&
745 cooked[1].equals("DnsInfo") &&
746 cooked[2].equals("servers")) {
747 try {
748 lifetime = Long.parseLong(cooked[4]);
749 } catch (NumberFormatException e) {
750 throw new IllegalStateException(errorMessage);
751 }
752 String[] servers = cooked[5].split(",");
753 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
754 }
755 return true;
756 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900757 case NetdResponseCode.RouteChange:
758 /*
759 * A route has been updated or removed.
760 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
761 */
762 if (!cooked[1].equals("Route") || cooked.length < 6) {
763 throw new IllegalStateException(errorMessage);
764 }
765
766 String via = null;
767 String dev = null;
768 boolean valid = true;
769 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
770 if (cooked[i].equals("dev")) {
771 if (dev == null) {
772 dev = cooked[i+1];
773 } else {
774 valid = false; // Duplicate interface.
775 }
776 } else if (cooked[i].equals("via")) {
777 if (via == null) {
778 via = cooked[i+1];
779 } else {
780 valid = false; // Duplicate gateway.
781 }
782 } else {
783 valid = false; // Unknown syntax.
784 }
785 }
786 if (valid) {
787 try {
788 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
789 InetAddress gateway = null;
790 if (via != null) gateway = InetAddress.parseNumericAddress(via);
791 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
792 notifyRouteChange(cooked[2], route);
793 return true;
794 } catch (IllegalArgumentException e) {}
795 }
796 throw new IllegalStateException(errorMessage);
797 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700798 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800799 }
800 return false;
San Mehat873f2142010-01-14 10:25:07 -0800801 }
802 }
803
San Mehated4fc8a2010-01-22 12:28:36 -0800804
San Mehat873f2142010-01-14 10:25:07 -0800805 //
806 // INetworkManagementService members
807 //
808
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800809 @Override
810 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800811 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700812 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800813 return NativeDaemonEvent.filterMessageList(
814 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700815 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800816 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700817 }
San Mehated4fc8a2010-01-22 12:28:36 -0800818 }
819
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800820 @Override
821 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800822 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823
824 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700825 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800826 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700827 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800828 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700829 }
San Mehated4fc8a2010-01-22 12:28:36 -0800830
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800831 event.checkCode(InterfaceGetCfgResult);
832
833 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
834 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800835
Kenny Roota80ce062010-06-01 13:23:53 -0700836 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800837 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700838 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800839 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800840 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800841 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700842 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800843 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800844 } catch (IllegalArgumentException iae) {
845 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700846 }
847
848 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800849 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800850 } catch (NumberFormatException nfe) {
851 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700852 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800853
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800854 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
855 while (st.hasMoreTokens()) {
856 cfg.setFlag(st.nextToken());
857 }
Kenny Roota80ce062010-06-01 13:23:53 -0700858 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800859 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800860 }
San Mehated4fc8a2010-01-22 12:28:36 -0800861 return cfg;
862 }
863
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800864 @Override
865 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800866 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800867 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800868 if (linkAddr == null || linkAddr.getAddress() == null) {
869 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800870 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800871
872 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800873 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900874 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800875 for (String flag : cfg.getFlags()) {
876 cmd.appendArg(flag);
877 }
878
Kenny Roota80ce062010-06-01 13:23:53 -0700879 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800880 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700881 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800882 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700883 }
San Mehat873f2142010-01-14 10:25:07 -0800884 }
885
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800886 @Override
887 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800888 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800889 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800890 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800891 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700892 }
893
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800894 @Override
895 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800896 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800897 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800898 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800899 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700900 }
901
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800902 @Override
903 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800904 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700905 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800906 mConnector.execute(
907 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700908 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800909 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700910 }
911 }
912
Irfan Sherifff5600612011-06-16 10:26:28 -0700913 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
914 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800915 @Override
916 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800917 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700918 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800919 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700920 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800921 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700922 }
923 }
924
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800925 @Override
926 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800927 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700928 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800929 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700930 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800931 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700932 }
933 }
934
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800935 @Override
936 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800937 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700938 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800939 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700940 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800941 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700942 }
943 }
944
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800945 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700946 public void addRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700947 modifyRoute("add", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700948 }
949
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800950 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700951 public void removeRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700952 modifyRoute("remove", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700953 }
954
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700955 private void modifyRoute(String action, String netId, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800956 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700957
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700958 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700959
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700960 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700961 cmd.appendArg(route.getInterface());
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900962 cmd.appendArg(route.getDestination().toString());
963
964 switch (route.getType()) {
965 case RouteInfo.RTN_UNICAST:
966 if (route.hasGateway()) {
967 cmd.appendArg(route.getGateway().getHostAddress());
968 }
969 break;
970 case RouteInfo.RTN_UNREACHABLE:
971 cmd.appendArg("unreachable");
972 break;
973 case RouteInfo.RTN_THROW:
974 cmd.appendArg("throw");
975 break;
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700976 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700977
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800978 try {
979 mConnector.execute(cmd);
980 } catch (NativeDaemonConnectorException e) {
981 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700982 }
983 }
984
985 private ArrayList<String> readRouteList(String filename) {
986 FileInputStream fstream = null;
987 ArrayList<String> list = new ArrayList<String>();
988
989 try {
990 fstream = new FileInputStream(filename);
991 DataInputStream in = new DataInputStream(fstream);
992 BufferedReader br = new BufferedReader(new InputStreamReader(in));
993 String s;
994
995 // throw away the title line
996
997 while (((s = br.readLine()) != null) && (s.length() != 0)) {
998 list.add(s);
999 }
1000 } catch (IOException ex) {
1001 // return current list, possibly empty
1002 } finally {
1003 if (fstream != null) {
1004 try {
1005 fstream.close();
1006 } catch (IOException ex) {}
1007 }
1008 }
1009
1010 return list;
1011 }
1012
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001013 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001014 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001015 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001016 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1017
1018 // v4 routes listed as:
1019 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1020 for (String s : readRouteList("/proc/net/route")) {
1021 String[] fields = s.split("\t");
1022
1023 if (fields.length > 7) {
1024 String iface = fields[0];
1025
1026 if (interfaceName.equals(iface)) {
1027 String dest = fields[1];
1028 String gate = fields[2];
1029 String flags = fields[3]; // future use?
1030 String mask = fields[7];
1031 try {
1032 // address stored as a hex string, ex: 0014A8C0
1033 InetAddress destAddr =
1034 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1035 int prefixLength =
1036 NetworkUtils.netmaskIntToPrefixLength(
1037 (int)Long.parseLong(mask, 16));
1038 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1039
1040 // address stored as a hex string, ex 0014A8C0
1041 InetAddress gatewayAddr =
1042 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1043
Wink Saville7b5fd052013-03-15 05:07:04 +00001044 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001045 routes.add(route);
1046 } catch (Exception e) {
1047 Log.e(TAG, "Error parsing route " + s + " : " + e);
1048 continue;
1049 }
1050 }
1051 }
1052 }
1053
1054 // v6 routes listed as:
1055 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1056 for (String s : readRouteList("/proc/net/ipv6_route")) {
1057 String[]fields = s.split("\\s+");
1058 if (fields.length > 9) {
1059 String iface = fields[9].trim();
1060 if (interfaceName.equals(iface)) {
1061 String dest = fields[0];
1062 String prefix = fields[1];
1063 String gate = fields[4];
1064
1065 try {
1066 // prefix length stored as a hex string, ex 40
1067 int prefixLength = Integer.parseInt(prefix, 16);
1068
1069 // address stored as a 32 char hex string
1070 // ex fe800000000000000000000000000000
1071 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1072 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1073
1074 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1075
Wink Saville7b5fd052013-03-15 05:07:04 +00001076 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001077 routes.add(route);
1078 } catch (Exception e) {
1079 Log.e(TAG, "Error parsing route " + s + " : " + e);
1080 continue;
1081 }
1082 }
1083 }
1084 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001085 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001086 }
1087
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001088 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001089 public void setMtu(String iface, int mtu) {
1090 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1091
1092 final NativeDaemonEvent event;
1093 try {
1094 event = mConnector.execute("interface", "setmtu", iface, mtu);
1095 } catch (NativeDaemonConnectorException e) {
1096 throw e.rethrowAsParcelableException();
1097 }
1098 }
1099
1100 @Override
San Mehat873f2142010-01-14 10:25:07 -08001101 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001102 // TODO: remove from aidl if nobody calls externally
1103 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001104
Joe Onorato8a9b2202010-02-26 18:56:32 -08001105 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001106 }
1107
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001108 @Override
San Mehat873f2142010-01-14 10:25:07 -08001109 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001110 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001111
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001112 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001113 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001114 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001115 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001116 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001117 }
San Mehat873f2142010-01-14 10:25:07 -08001118
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001119 // 211 Forwarding enabled
1120 event.checkCode(IpFwdStatusResult);
1121 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001122 }
1123
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001124 @Override
1125 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001126 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001127 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001128 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001129 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001130 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001131 }
San Mehat873f2142010-01-14 10:25:07 -08001132 }
1133
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001134 @Override
1135 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001136 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001137 // cmd is "tether start first_start first_stop second_start second_stop ..."
1138 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139
1140 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001141 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001142 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001143 }
Kenny Roota80ce062010-06-01 13:23:53 -07001144
1145 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001146 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001147 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001148 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001149 }
San Mehat873f2142010-01-14 10:25:07 -08001150 }
1151
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001152 @Override
1153 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001154 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001155 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001156 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001157 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001158 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001159 }
San Mehat873f2142010-01-14 10:25:07 -08001160 }
1161
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001162 @Override
1163 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001164 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001165
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001166 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001167 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001168 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001169 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001170 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001171 }
San Mehat873f2142010-01-14 10:25:07 -08001172
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001173 // 210 Tethering services started
1174 event.checkCode(TetherStatusResult);
1175 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001176 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001177
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001178 @Override
1179 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001180 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001181 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001182 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001183 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001184 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001185 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001186 List<RouteInfo> routes = new ArrayList<RouteInfo>();
1187 // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it
1188 // suitable to use as a route destination.
1189 routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface));
1190 addInterfaceToLocalNetwork(iface, routes);
San Mehat873f2142010-01-14 10:25:07 -08001191 }
1192
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001193 @Override
San Mehat873f2142010-01-14 10:25:07 -08001194 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001195 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001196 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001197 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001198 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001199 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001200 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001201 removeInterfaceFromLocalNetwork(iface);
San Mehat873f2142010-01-14 10:25:07 -08001202 }
1203
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001204 @Override
1205 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001206 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001207 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001208 return NativeDaemonEvent.filterMessageList(
1209 mConnector.executeForList("tether", "interface", "list"),
1210 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001211 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001212 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001213 }
San Mehat873f2142010-01-14 10:25:07 -08001214 }
1215
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001216 @Override
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001217 public void setDnsForwarders(Network network, String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001218 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001219
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001220 int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET;
1221 final Command cmd = new Command("tether", "dns", "set", netId);
1222
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001223 for (String s : dns) {
1224 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1225 }
1226
San Mehat873f2142010-01-14 10:25:07 -08001227 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001228 mConnector.execute(cmd);
1229 } catch (NativeDaemonConnectorException e) {
1230 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001231 }
1232 }
1233
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001234 @Override
1235 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001236 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001237 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 return NativeDaemonEvent.filterMessageList(
1239 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001240 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001241 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001242 }
San Mehat873f2142010-01-14 10:25:07 -08001243 }
1244
jiaguo1da35f72014-01-09 16:39:59 +08001245 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1246 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1247 for (InterfaceAddress ia : addresses) {
1248 if (!ia.getAddress().isLinkLocalAddress())
1249 filtered.add(ia);
1250 }
1251 return filtered;
1252 }
1253
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001254 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001255 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001256 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001257
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001258 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1259 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001260 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001261 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001262 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001263 // Don't touch link-local routes, as link-local addresses aren't routable,
1264 // kernel creates link-local routes on all interfaces automatically
1265 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1266 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001267 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001268 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001269 InetAddress addr = NetworkUtils.getNetworkPart(
1270 ia.getAddress(), ia.getNetworkPrefixLength());
1271 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001272 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001273 }
1274
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001275 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001276 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001277 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001278 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001279 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001280 }
1281
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001282 @Override
1283 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001284 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001285 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001286 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001287 } catch (SocketException e) {
1288 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001289 }
San Mehat873f2142010-01-14 10:25:07 -08001290 }
1291
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001292 @Override
1293 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001294 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001295 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001296 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001297 } catch (SocketException e) {
1298 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001299 }
San Mehat873f2142010-01-14 10:25:07 -08001300 }
San Mehat72759df2010-01-19 13:50:37 -08001301
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001302 @Override
1303 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001304 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001305 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001306 return NativeDaemonEvent.filterMessageList(
1307 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001308 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001309 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001310 }
San Mehat72759df2010-01-19 13:50:37 -08001311 }
1312
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001313 @Override
1314 public void attachPppd(
1315 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001316 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001317 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001318 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001319 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1320 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1321 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001322 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001323 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001324 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001325 }
1326 }
1327
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001328 @Override
1329 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001330 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001331 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001332 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001333 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001334 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001335 }
San Mehat72759df2010-01-19 13:50:37 -08001336 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001337
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001338 @Override
1339 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001340 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001341 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001342 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001343 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001344 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001345 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001346 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001347 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001348 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001349 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001350 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001351 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001352 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001353 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001354 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001355 }
1356
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001357 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001358 switch (wifiConfig.getAuthType()) {
1359 case KeyMgmt.WPA_PSK:
1360 return "wpa-psk";
1361 case KeyMgmt.WPA2_PSK:
1362 return "wpa2-psk";
1363 default:
1364 return "open";
1365 }
1366 }
1367
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001368 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001369 @Override
1370 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001371 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001372 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001373 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001374 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001375 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001376 }
1377 }
1378
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001379 @Override
1380 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001381 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001382 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001383 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001384 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001385 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001386 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001387 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001388 }
1389
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001390 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001391 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001392 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001393 try {
1394 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001395 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001396 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001397 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001398 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001399 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001400 }
1401 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001402 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001403 }
1404 }
San Mehat91cac642010-03-31 14:31:36 -07001405
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001406 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001407 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001408 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1409
1410 if (DBG) Slog.d(TAG, "Adding idletimer");
1411
1412 synchronized (mIdleTimerLock) {
1413 IdleTimerParams params = mActiveIdleTimers.get(iface);
1414 if (params != null) {
1415 // the interface already has idletimer, update network count
1416 params.networkCount++;
1417 return;
1418 }
1419
1420 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001421 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1422 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001423 } catch (NativeDaemonConnectorException e) {
1424 throw e.rethrowAsParcelableException();
1425 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001426 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1427
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001428 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001429 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1430 mNetworkActive = false;
1431 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001432 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001433 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001434 notifyInterfaceClassActivity(type,
1435 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1436 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001437 }
1438 });
Haoyu Bai04124232012-06-28 15:26:19 -07001439 }
1440 }
1441
1442 @Override
1443 public void removeIdleTimer(String iface) {
1444 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1445
1446 if (DBG) Slog.d(TAG, "Removing idletimer");
1447
1448 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001449 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001450 if (params == null || --(params.networkCount) > 0) {
1451 return;
1452 }
1453
1454 try {
1455 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001456 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001457 } catch (NativeDaemonConnectorException e) {
1458 throw e.rethrowAsParcelableException();
1459 }
1460 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001461 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001462 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001463 notifyInterfaceClassActivity(params.type,
1464 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1465 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001466 }
1467 });
Haoyu Bai04124232012-06-28 15:26:19 -07001468 }
1469 }
1470
1471 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001472 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001473 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001474 try {
1475 return mStatsFactory.readNetworkStatsSummaryDev();
1476 } catch (IOException e) {
1477 throw new IllegalStateException(e);
1478 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001479 }
1480
1481 @Override
1482 public NetworkStats getNetworkStatsSummaryXt() {
1483 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001484 try {
1485 return mStatsFactory.readNetworkStatsSummaryXt();
1486 } catch (IOException e) {
1487 throw new IllegalStateException(e);
1488 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001489 }
1490
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001491 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001492 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001493 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001494 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001495 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001496 } catch (IOException e) {
1497 throw new IllegalStateException(e);
1498 }
San Mehat91cac642010-03-31 14:31:36 -07001499 }
1500
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001501 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001502 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001503 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001504
Jeff Sharkey350083e2011-06-29 10:45:16 -07001505 // silently discard when control disabled
1506 // TODO: eventually migrate to be always enabled
1507 if (!mBandwidthControlEnabled) return;
1508
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001509 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001510 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001511 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001512 }
1513
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001514 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001515 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001516 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001517 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001518 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001519 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001520 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001521 }
1522 }
1523
1524 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001525 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001526 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001527
Jeff Sharkey350083e2011-06-29 10:45:16 -07001528 // silently discard when control disabled
1529 // TODO: eventually migrate to be always enabled
1530 if (!mBandwidthControlEnabled) return;
1531
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001532 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001533 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001534 // TODO: eventually consider throwing
1535 return;
1536 }
1537
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001538 mActiveQuotas.remove(iface);
1539 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001540
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001541 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001542 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001543 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001544 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001545 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001546 }
1547 }
1548 }
1549
1550 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001551 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001552 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001553
1554 // silently discard when control disabled
1555 // TODO: eventually migrate to be always enabled
1556 if (!mBandwidthControlEnabled) return;
1557
1558 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001559 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001560 throw new IllegalStateException("setting alert requires existing quota on iface");
1561 }
1562
1563 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001564 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001565 throw new IllegalStateException("iface " + iface + " already has alert");
1566 }
1567
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001568 try {
1569 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001570 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001571 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001572 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001573 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001574 }
1575 }
1576 }
1577
1578 @Override
1579 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001580 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001581
1582 // silently discard when control disabled
1583 // TODO: eventually migrate to be always enabled
1584 if (!mBandwidthControlEnabled) return;
1585
1586 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001587 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001588 // TODO: eventually consider throwing
1589 return;
1590 }
1591
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001592 try {
1593 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001594 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001595 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001596 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001597 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001598 }
1599 }
1600 }
1601
1602 @Override
1603 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001604 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001605
1606 // silently discard when control disabled
1607 // TODO: eventually migrate to be always enabled
1608 if (!mBandwidthControlEnabled) return;
1609
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001610 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001611 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001612 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001613 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001614 }
1615 }
1616
1617 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001618 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001619 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001620
Jeff Sharkey350083e2011-06-29 10:45:16 -07001621 // silently discard when control disabled
1622 // TODO: eventually migrate to be always enabled
1623 if (!mBandwidthControlEnabled) return;
1624
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001625 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001626 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1627 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1628 // TODO: eventually consider throwing
1629 return;
1630 }
1631
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001632 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001633 mConnector.execute("bandwidth",
1634 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001635 if (rejectOnQuotaInterfaces) {
1636 mUidRejectOnQuota.put(uid, true);
1637 } else {
1638 mUidRejectOnQuota.delete(uid);
1639 }
1640 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001641 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001642 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001643 }
1644 }
1645
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001646 @Override
1647 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001648 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001649 return mBandwidthControlEnabled;
1650 }
1651
1652 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001653 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001654 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001655 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001656 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001657 } catch (IOException e) {
1658 throw new IllegalStateException(e);
1659 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001660 }
1661
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001662 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001663 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001664 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001665
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001666 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001667 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001668 final NativeDaemonEvent[] events = mConnector.executeForList(
1669 "bandwidth", "gettetherstats");
1670 for (NativeDaemonEvent event : events) {
1671 if (event.getCode() != TetheringStatsListResult) continue;
1672
1673 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1674 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1675 try {
1676 final String ifaceIn = tok.nextToken();
1677 final String ifaceOut = tok.nextToken();
1678
1679 final NetworkStats.Entry entry = new NetworkStats.Entry();
1680 entry.iface = ifaceOut;
1681 entry.uid = UID_TETHERING;
1682 entry.set = SET_DEFAULT;
1683 entry.tag = TAG_NONE;
1684 entry.rxBytes = Long.parseLong(tok.nextToken());
1685 entry.rxPackets = Long.parseLong(tok.nextToken());
1686 entry.txBytes = Long.parseLong(tok.nextToken());
1687 entry.txPackets = Long.parseLong(tok.nextToken());
1688 stats.combineValues(entry);
1689 } catch (NoSuchElementException e) {
1690 throw new IllegalStateException("problem parsing tethering stats: " + event);
1691 } catch (NumberFormatException e) {
1692 throw new IllegalStateException("problem parsing tethering stats: " + event);
1693 }
1694 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001695 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001696 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001697 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001698 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001699 }
1700
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001701 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001702 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001703 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001704
Paul Jensen13e817d2014-04-10 14:16:37 -04001705 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001706 (domains == null ? "" : domains));
1707
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001708 for (String s : servers) {
1709 InetAddress a = NetworkUtils.numericToInetAddress(s);
1710 if (a.isAnyLocalAddress() == false) {
1711 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001712 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001713 }
1714
1715 try {
1716 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001717 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001718 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001719 }
1720 }
1721
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001722 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001723 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001724 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001725 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1726 argv[0] = "users";
1727 argv[1] = "add";
1728 argv[2] = netId;
1729 int argc = 3;
1730 // Avoid overly long commands by limiting number of UID ranges per command.
1731 for (int i = 0; i < ranges.length; i++) {
1732 argv[argc++] = ranges[i].toString();
1733 if (i == (ranges.length - 1) || argc == argv.length) {
1734 try {
1735 mConnector.execute("network", Arrays.copyOf(argv, argc));
1736 } catch (NativeDaemonConnectorException e) {
1737 throw e.rethrowAsParcelableException();
1738 }
1739 argc = 3;
1740 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001741 }
1742 }
1743
1744 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001745 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001746 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001747 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1748 argv[0] = "users";
1749 argv[1] = "remove";
1750 argv[2] = netId;
1751 int argc = 3;
1752 // Avoid overly long commands by limiting number of UID ranges per command.
1753 for (int i = 0; i < ranges.length; i++) {
1754 argv[argc++] = ranges[i].toString();
1755 if (i == (ranges.length - 1) || argc == argv.length) {
1756 try {
1757 mConnector.execute("network", Arrays.copyOf(argv, argc));
1758 } catch (NativeDaemonConnectorException e) {
1759 throw e.rethrowAsParcelableException();
1760 }
1761 argc = 3;
1762 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001763 }
1764 }
1765
1766 @Override
Paul Jensenb69a3a82014-08-06 15:34:26 -04001767 public void flushNetworkDnsCache(int netId) {
1768 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1769 try {
1770 mConnector.execute("resolver", "flushnet", netId);
1771 } catch (NativeDaemonConnectorException e) {
1772 throw e.rethrowAsParcelableException();
1773 }
1774 }
1775
1776 @Override
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001777 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001778 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001779 try {
1780 mConnector.execute("firewall", enabled ? "enable" : "disable");
1781 mFirewallEnabled = enabled;
1782 } catch (NativeDaemonConnectorException e) {
1783 throw e.rethrowAsParcelableException();
1784 }
1785 }
1786
1787 @Override
1788 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001789 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001790 return mFirewallEnabled;
1791 }
1792
1793 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001794 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001795 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001796 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001797 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001798 try {
1799 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1800 } catch (NativeDaemonConnectorException e) {
1801 throw e.rethrowAsParcelableException();
1802 }
1803 }
1804
1805 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001806 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001807 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001808 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001809 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001810 try {
1811 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1812 } catch (NativeDaemonConnectorException e) {
1813 throw e.rethrowAsParcelableException();
1814 }
1815 }
1816
1817 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001818 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001819 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001820 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001821 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001822 try {
1823 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1824 } catch (NativeDaemonConnectorException e) {
1825 throw e.rethrowAsParcelableException();
1826 }
1827 }
1828
1829 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001830 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001831 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001832 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001833 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001834 try {
1835 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1836 } catch (NativeDaemonConnectorException e) {
1837 throw e.rethrowAsParcelableException();
1838 }
1839 }
1840
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001841 private static void enforceSystemUid() {
1842 final int uid = Binder.getCallingUid();
1843 if (uid != Process.SYSTEM_UID) {
1844 throw new SecurityException("Only available to AID_SYSTEM");
1845 }
1846 }
1847
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001848 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001849 public void startClatd(String interfaceName) throws IllegalStateException {
1850 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1851
1852 try {
1853 mConnector.execute("clatd", "start", interfaceName);
1854 } catch (NativeDaemonConnectorException e) {
1855 throw e.rethrowAsParcelableException();
1856 }
1857 }
1858
1859 @Override
1860 public void stopClatd() throws IllegalStateException {
1861 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1862
1863 try {
1864 mConnector.execute("clatd", "stop");
1865 } catch (NativeDaemonConnectorException e) {
1866 throw e.rethrowAsParcelableException();
1867 }
1868 }
1869
1870 @Override
1871 public boolean isClatdStarted() {
1872 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1873
1874 final NativeDaemonEvent event;
1875 try {
1876 event = mConnector.execute("clatd", "status");
1877 } catch (NativeDaemonConnectorException e) {
1878 throw e.rethrowAsParcelableException();
1879 }
1880
1881 event.checkCode(ClatdStatusResult);
1882 return event.getMessage().endsWith("started");
1883 }
1884
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001885 @Override
1886 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1887 mNetworkActivityListeners.register(listener);
1888 }
1889
1890 @Override
1891 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1892 mNetworkActivityListeners.unregister(listener);
1893 }
1894
1895 @Override
1896 public boolean isNetworkActive() {
1897 synchronized (mNetworkActivityListeners) {
1898 return mNetworkActive || mActiveIdleTimers.isEmpty();
1899 }
1900 }
1901
1902 private void reportNetworkActive() {
1903 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001904 try {
1905 for (int i = 0; i < length; i++) {
1906 try {
1907 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1908 } catch (RemoteException e) {
1909 } catch (RuntimeException e) {
1910 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001911 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001912 } finally {
1913 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001914 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001915 }
1916
Mattias Falk8b47b362011-08-23 14:15:13 +02001917 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001918 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001919 public void monitor() {
1920 if (mConnector != null) {
1921 mConnector.monitor();
1922 }
1923 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001924
1925 @Override
1926 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1927 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1928
Robert Greenwalt470fd722012-01-18 12:51:15 -08001929 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1930 mConnector.dump(fd, pw, args);
1931 pw.println();
1932
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001933 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001934 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1935 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1936 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001937
1938 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001939 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1940 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001941 }
1942
1943 synchronized (mUidRejectOnQuota) {
1944 pw.print("UID reject on quota ifaces: [");
1945 final int size = mUidRejectOnQuota.size();
1946 for (int i = 0; i < size; i++) {
1947 pw.print(mUidRejectOnQuota.keyAt(i));
1948 if (i < size - 1) pw.print(",");
1949 }
1950 pw.println("]");
1951 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001952
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001953 synchronized (mIdleTimerLock) {
1954 pw.println("Idle timers:");
1955 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1956 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1957 IdleTimerParams params = ent.getValue();
1958 pw.print(" timeout="); pw.print(params.timeout);
1959 pw.print(" type="); pw.print(params.type);
1960 pw.print(" networkCount="); pw.println(params.networkCount);
1961 }
1962 }
1963
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001964 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001965 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001966
Robert Greenwalt568891d2014-04-04 13:38:00 -07001967 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001968 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001969 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1970
1971 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001972 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001973 } catch (NativeDaemonConnectorException e) {
1974 throw e.rethrowAsParcelableException();
1975 }
1976 }
1977
Robert Greenwalt568891d2014-04-04 13:38:00 -07001978 @Override
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001979 public void createVirtualNetwork(int netId, boolean hasDNS, boolean secure) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001980 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1981
1982 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001983 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0",
1984 secure ? "1" : "0");
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001985 } catch (NativeDaemonConnectorException e) {
1986 throw e.rethrowAsParcelableException();
1987 }
1988 }
1989
1990 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001991 public void removeNetwork(int netId) {
1992 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1993
1994 try {
1995 mConnector.execute("network", "destroy", netId);
1996 } catch (NativeDaemonConnectorException e) {
1997 throw e.rethrowAsParcelableException();
1998 }
1999 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002000
2001 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002002 public void addInterfaceToNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002003 modifyInterfaceInNetwork("add", "" + netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002004 }
2005
2006 @Override
2007 public void removeInterfaceFromNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002008 modifyInterfaceInNetwork("remove", "" + netId, iface);
2009 }
Paul Jensen992f2522014-04-28 10:33:11 -04002010
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002011 private void modifyInterfaceInNetwork(String action, String netId, String iface) {
2012 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen992f2522014-04-28 10:33:11 -04002013 try {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002014 mConnector.execute("network", "interface", action, netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002015 } catch (NativeDaemonConnectorException e) {
2016 throw e.rethrowAsParcelableException();
2017 }
2018 }
2019
2020 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002021 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002022 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2023
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07002024 final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002025
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002026 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002027 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002028 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002029 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002030 if (routeInfo.hasGateway()) {
2031 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2032 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002033
2034 try {
2035 mConnector.execute(cmd);
2036 } catch (NativeDaemonConnectorException e) {
2037 throw e.rethrowAsParcelableException();
2038 }
2039 }
2040
2041 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002042 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002043 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2044
2045 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002046 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002047 } catch (NativeDaemonConnectorException e) {
2048 throw e.rethrowAsParcelableException();
2049 }
2050 }
2051
2052 @Override
2053 public void clearDefaultNetId() {
2054 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2055
2056 try {
2057 mConnector.execute("network", "default", "clear");
2058 } catch (NativeDaemonConnectorException e) {
2059 throw e.rethrowAsParcelableException();
2060 }
2061 }
2062
2063 @Override
2064 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2065 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2066
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002067 final Command cmd = new Command("network", "permission", "user", "set");
2068 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2069 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002070 for (int i=0; i<uids.length; i++) {
2071 cmd.appendArg(uids[i]);
2072 }
2073
2074 try {
2075 mConnector.execute(cmd);
2076 } catch (NativeDaemonConnectorException e) {
2077 throw e.rethrowAsParcelableException();
2078 }
2079 }
2080
2081 @Override
2082 public void clearPermission(int[] uids) {
2083 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2084
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002085 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002086 for (int i=0; i<uids.length; i++) {
2087 cmd.appendArg(uids[i]);
2088 }
2089
2090 try {
2091 mConnector.execute(cmd);
2092 } catch (NativeDaemonConnectorException e) {
2093 throw e.rethrowAsParcelableException();
2094 }
2095 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002096
2097 @Override
2098 public void allowProtect(int uid) {
2099 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2100
2101 try {
2102 mConnector.execute("network", "protect", "allow", uid);
2103 } catch (NativeDaemonConnectorException e) {
2104 throw e.rethrowAsParcelableException();
2105 }
2106 }
2107
2108 @Override
2109 public void denyProtect(int uid) {
2110 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2111
2112 try {
2113 mConnector.execute("network", "protect", "deny", uid);
2114 } catch (NativeDaemonConnectorException e) {
2115 throw e.rethrowAsParcelableException();
2116 }
2117 }
2118
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002119 @Override
2120 public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) {
2121 modifyInterfaceInNetwork("add", "local", iface);
2122
2123 for (RouteInfo route : routes) {
2124 if (!route.isDefaultRoute()) {
2125 modifyRoute("add", "local", route);
2126 }
2127 }
2128 }
2129
2130 @Override
2131 public void removeInterfaceFromLocalNetwork(String iface) {
2132 modifyInterfaceInNetwork("remove", "local", iface);
2133 }
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -07002134
2135 @Override
2136 public void blockAddressFamily(int family, int netId, String iface) {
2137 modifyAddressFamily("add", family, netId, iface);
2138 }
2139
2140 @Override
2141 public void unblockAddressFamily(int family, int netId, String iface) {
2142 modifyAddressFamily("remove", family, netId, iface);
2143 }
2144
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +09002145 // TODO: get rid of this and add RTN_UNREACHABLE routes instead.
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -07002146 private void modifyAddressFamily(String action, int family, int netId, String iface) {
2147 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2148
2149 final Command cmd = new Command("network", "route", action, netId, iface);
2150
2151 if (family == AF_INET) {
2152 cmd.appendArg(Inet4Address.ANY.getHostAddress() + "/0");
2153 } else if (family == AF_INET6) {
2154 cmd.appendArg(Inet6Address.ANY.getHostAddress() + "/0");
2155 } else {
2156 throw new IllegalStateException(family + " is neither " + AF_INET + " nor " + AF_INET6);
2157 }
2158
2159 cmd.appendArg("unreachable");
2160
2161 try {
2162 mConnector.execute(cmd);
2163 } catch (NativeDaemonConnectorException e) {
2164 throw e.rethrowAsParcelableException();
2165 }
2166 }
San Mehat873f2142010-01-14 10:25:07 -08002167}