blob: f9c7a78271c0dd7ef58308e448e91355ef39ebd5 [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -070019import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkey4529bb62011-12-14 10:31:54 -080020import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070021import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080022import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070023import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080024import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.NetworkStats.TAG_NONE;
26import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070027import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090028import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070029import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080032import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070036import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080037import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070038import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070039
San Mehat873f2142010-01-14 10:25:07 -080040import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080041import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080042import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090044import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070046import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080047import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070048import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080049import android.net.wifi.WifiConfiguration;
50import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070051import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070052import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070053import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080054import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080055import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080056import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070057import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080058import android.os.RemoteCallbackList;
59import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070060import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070061import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080062import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070063import android.telephony.DataConnectionRealTimeInfo;
64import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070065import android.telephony.SubscriptionManager;
Wink Saville67e07892014-06-18 16:43:14 -070066import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080067import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070069import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080070
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070071import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070072import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070073import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080074import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070075import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070076import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070077import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070078
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070079import java.io.BufferedReader;
80import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080081import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070082import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070084import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070085import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070086import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070087import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070088import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070089import java.net.InterfaceAddress;
90import java.net.NetworkInterface;
91import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070093import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080094import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070095import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070096import java.util.NoSuchElementException;
97import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070098import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080099
100/**
101 * @hide
102 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700103public class NetworkManagementService extends INetworkManagementService.Stub
104 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700105 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700106 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700107 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900108 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700109
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800110 private static final String ADD = "add";
111 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700112
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700113 private static final String ALLOW = "allow";
114 private static final String DENY = "deny";
115
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700116 private static final String DEFAULT = "default";
117 private static final String SECONDARY = "secondary";
118
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700119 /**
120 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
121 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
122 */
123 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
124
San Mehat873f2142010-01-14 10:25:07 -0800125 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700126 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800127 public static final int InterfaceListResult = 110;
128 public static final int TetherInterfaceListResult = 111;
129 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800130 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700131 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800132
133 public static final int TetherStatusResult = 210;
134 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800135 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800136 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700137 public static final int InterfaceRxCounterResult = 216;
138 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700139 public static final int QuotaCounterResult = 220;
140 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800141 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900142 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700143 public static final int GetMarkResult = 225;
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) {
947 modifyRoute(netId, ADD, 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) {
952 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700953 }
954
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700955 private void modifyRoute(int netId, String action, 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 Ramachandrancc91c7b2014-06-03 18:41:43 -0700961 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700962 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900963 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700964 if (route.hasGateway()) {
965 cmd.appendArg(route.getGateway().getHostAddress());
966 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700967
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800968 try {
969 mConnector.execute(cmd);
970 } catch (NativeDaemonConnectorException e) {
971 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700972 }
973 }
974
975 private ArrayList<String> readRouteList(String filename) {
976 FileInputStream fstream = null;
977 ArrayList<String> list = new ArrayList<String>();
978
979 try {
980 fstream = new FileInputStream(filename);
981 DataInputStream in = new DataInputStream(fstream);
982 BufferedReader br = new BufferedReader(new InputStreamReader(in));
983 String s;
984
985 // throw away the title line
986
987 while (((s = br.readLine()) != null) && (s.length() != 0)) {
988 list.add(s);
989 }
990 } catch (IOException ex) {
991 // return current list, possibly empty
992 } finally {
993 if (fstream != null) {
994 try {
995 fstream.close();
996 } catch (IOException ex) {}
997 }
998 }
999
1000 return list;
1001 }
1002
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001003 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001004 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001005 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001006 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1007
1008 // v4 routes listed as:
1009 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1010 for (String s : readRouteList("/proc/net/route")) {
1011 String[] fields = s.split("\t");
1012
1013 if (fields.length > 7) {
1014 String iface = fields[0];
1015
1016 if (interfaceName.equals(iface)) {
1017 String dest = fields[1];
1018 String gate = fields[2];
1019 String flags = fields[3]; // future use?
1020 String mask = fields[7];
1021 try {
1022 // address stored as a hex string, ex: 0014A8C0
1023 InetAddress destAddr =
1024 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1025 int prefixLength =
1026 NetworkUtils.netmaskIntToPrefixLength(
1027 (int)Long.parseLong(mask, 16));
1028 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1029
1030 // address stored as a hex string, ex 0014A8C0
1031 InetAddress gatewayAddr =
1032 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1033
Wink Saville7b5fd052013-03-15 05:07:04 +00001034 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001035 routes.add(route);
1036 } catch (Exception e) {
1037 Log.e(TAG, "Error parsing route " + s + " : " + e);
1038 continue;
1039 }
1040 }
1041 }
1042 }
1043
1044 // v6 routes listed as:
1045 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1046 for (String s : readRouteList("/proc/net/ipv6_route")) {
1047 String[]fields = s.split("\\s+");
1048 if (fields.length > 9) {
1049 String iface = fields[9].trim();
1050 if (interfaceName.equals(iface)) {
1051 String dest = fields[0];
1052 String prefix = fields[1];
1053 String gate = fields[4];
1054
1055 try {
1056 // prefix length stored as a hex string, ex 40
1057 int prefixLength = Integer.parseInt(prefix, 16);
1058
1059 // address stored as a 32 char hex string
1060 // ex fe800000000000000000000000000000
1061 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1062 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1063
1064 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1065
Wink Saville7b5fd052013-03-15 05:07:04 +00001066 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001067 routes.add(route);
1068 } catch (Exception e) {
1069 Log.e(TAG, "Error parsing route " + s + " : " + e);
1070 continue;
1071 }
1072 }
1073 }
1074 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001075 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001076 }
1077
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001078 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001079 public void setMtu(String iface, int mtu) {
1080 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1081
1082 final NativeDaemonEvent event;
1083 try {
1084 event = mConnector.execute("interface", "setmtu", iface, mtu);
1085 } catch (NativeDaemonConnectorException e) {
1086 throw e.rethrowAsParcelableException();
1087 }
1088 }
1089
1090 @Override
San Mehat873f2142010-01-14 10:25:07 -08001091 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001092 // TODO: remove from aidl if nobody calls externally
1093 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001094
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001096 }
1097
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001098 @Override
San Mehat873f2142010-01-14 10:25:07 -08001099 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001100 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001101
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001102 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001103 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001104 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001105 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001106 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001107 }
San Mehat873f2142010-01-14 10:25:07 -08001108
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001109 // 211 Forwarding enabled
1110 event.checkCode(IpFwdStatusResult);
1111 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001112 }
1113
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001114 @Override
1115 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001116 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001117 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001118 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001119 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001120 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001121 }
San Mehat873f2142010-01-14 10:25:07 -08001122 }
1123
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001124 @Override
1125 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001126 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001127 // cmd is "tether start first_start first_stop second_start second_stop ..."
1128 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001129
1130 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001131 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001132 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001133 }
Kenny Roota80ce062010-06-01 13:23:53 -07001134
1135 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001137 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001138 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001139 }
San Mehat873f2142010-01-14 10:25:07 -08001140 }
1141
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001142 @Override
1143 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001144 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001145 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001146 mConnector.execute("tether", "stop");
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 boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001154 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001155
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001156 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001157 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001158 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001159 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001160 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001161 }
San Mehat873f2142010-01-14 10:25:07 -08001162
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001163 // 210 Tethering services started
1164 event.checkCode(TetherStatusResult);
1165 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001166 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001167
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001168 @Override
1169 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001170 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001171 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001172 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001173 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001174 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001175 }
San Mehat873f2142010-01-14 10:25:07 -08001176 }
1177
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001178 @Override
San Mehat873f2142010-01-14 10:25:07 -08001179 public void untetherInterface(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", "remove", 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 }
San Mehat873f2142010-01-14 10:25:07 -08001186 }
1187
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001188 @Override
1189 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001190 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001191 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001192 return NativeDaemonEvent.filterMessageList(
1193 mConnector.executeForList("tether", "interface", "list"),
1194 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001195 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001196 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001197 }
San Mehat873f2142010-01-14 10:25:07 -08001198 }
1199
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001200 @Override
1201 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001202 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001203
1204 final Command cmd = new Command("tether", "dns", "set");
1205 for (String s : dns) {
1206 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1207 }
1208
San Mehat873f2142010-01-14 10:25:07 -08001209 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001210 mConnector.execute(cmd);
1211 } catch (NativeDaemonConnectorException e) {
1212 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001213 }
1214 }
1215
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001216 @Override
1217 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001218 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001219 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001220 return NativeDaemonEvent.filterMessageList(
1221 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001222 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001223 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001224 }
San Mehat873f2142010-01-14 10:25:07 -08001225 }
1226
jiaguo1da35f72014-01-09 16:39:59 +08001227 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1228 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1229 for (InterfaceAddress ia : addresses) {
1230 if (!ia.getAddress().isLinkLocalAddress())
1231 filtered.add(ia);
1232 }
1233 return filtered;
1234 }
1235
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001236 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001237 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001239
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001240 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1241 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001242 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001243 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001244 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001245 // Don't touch link-local routes, as link-local addresses aren't routable,
1246 // kernel creates link-local routes on all interfaces automatically
1247 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1248 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001249 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001250 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001251 InetAddress addr = NetworkUtils.getNetworkPart(
1252 ia.getAddress(), ia.getNetworkPrefixLength());
1253 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001254 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001255 }
1256
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001257 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001258 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001259 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001260 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001261 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001262 }
1263
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001264 @Override
1265 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001266 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001267 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001268 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001269 } catch (SocketException e) {
1270 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001271 }
San Mehat873f2142010-01-14 10:25:07 -08001272 }
1273
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001274 @Override
1275 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001276 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001277 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001278 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001279 } catch (SocketException e) {
1280 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001281 }
San Mehat873f2142010-01-14 10:25:07 -08001282 }
San Mehat72759df2010-01-19 13:50:37 -08001283
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001284 @Override
1285 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001287 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001288 return NativeDaemonEvent.filterMessageList(
1289 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001290 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001291 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001292 }
San Mehat72759df2010-01-19 13:50:37 -08001293 }
1294
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001295 @Override
1296 public void attachPppd(
1297 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001298 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001299 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001300 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001301 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1302 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1303 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001304 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001305 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001306 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001307 }
1308 }
1309
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001310 @Override
1311 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001312 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001313 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001314 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001315 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001316 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001317 }
San Mehat72759df2010-01-19 13:50:37 -08001318 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001319
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001320 @Override
1321 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001322 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001323 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001324 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001325 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001326 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001327 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001328 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001329 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001330 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001331 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001332 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001333 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001334 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001335 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001336 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001337 }
1338
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001339 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001340 switch (wifiConfig.getAuthType()) {
1341 case KeyMgmt.WPA_PSK:
1342 return "wpa-psk";
1343 case KeyMgmt.WPA2_PSK:
1344 return "wpa2-psk";
1345 default:
1346 return "open";
1347 }
1348 }
1349
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001350 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001351 @Override
1352 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001353 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001354 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001355 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001356 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001357 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001358 }
1359 }
1360
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001361 @Override
1362 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001363 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001364 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001365 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001366 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001367 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001368 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001369 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001370 }
1371
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001372 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001373 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001374 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001375 try {
1376 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001377 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001378 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001379 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001380 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001381 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001382 }
1383 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001384 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001385 }
1386 }
San Mehat91cac642010-03-31 14:31:36 -07001387
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001388 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001389 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001390 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1391
1392 if (DBG) Slog.d(TAG, "Adding idletimer");
1393
1394 synchronized (mIdleTimerLock) {
1395 IdleTimerParams params = mActiveIdleTimers.get(iface);
1396 if (params != null) {
1397 // the interface already has idletimer, update network count
1398 params.networkCount++;
1399 return;
1400 }
1401
1402 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001403 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1404 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001405 } catch (NativeDaemonConnectorException e) {
1406 throw e.rethrowAsParcelableException();
1407 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001408 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1409
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001410 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001411 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1412 mNetworkActive = false;
1413 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001414 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001415 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001416 notifyInterfaceClassActivity(type,
1417 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1418 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001419 }
1420 });
Haoyu Bai04124232012-06-28 15:26:19 -07001421 }
1422 }
1423
1424 @Override
1425 public void removeIdleTimer(String iface) {
1426 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1427
1428 if (DBG) Slog.d(TAG, "Removing idletimer");
1429
1430 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001431 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001432 if (params == null || --(params.networkCount) > 0) {
1433 return;
1434 }
1435
1436 try {
1437 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001438 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001439 } catch (NativeDaemonConnectorException e) {
1440 throw e.rethrowAsParcelableException();
1441 }
1442 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001443 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001444 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001445 notifyInterfaceClassActivity(params.type,
1446 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1447 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001448 }
1449 });
Haoyu Bai04124232012-06-28 15:26:19 -07001450 }
1451 }
1452
1453 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001454 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001455 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001456 try {
1457 return mStatsFactory.readNetworkStatsSummaryDev();
1458 } catch (IOException e) {
1459 throw new IllegalStateException(e);
1460 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001461 }
1462
1463 @Override
1464 public NetworkStats getNetworkStatsSummaryXt() {
1465 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001466 try {
1467 return mStatsFactory.readNetworkStatsSummaryXt();
1468 } catch (IOException e) {
1469 throw new IllegalStateException(e);
1470 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001471 }
1472
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001473 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001474 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001475 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001476 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001477 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001478 } catch (IOException e) {
1479 throw new IllegalStateException(e);
1480 }
San Mehat91cac642010-03-31 14:31:36 -07001481 }
1482
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001483 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001484 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001485 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001486
Jeff Sharkey350083e2011-06-29 10:45:16 -07001487 // silently discard when control disabled
1488 // TODO: eventually migrate to be always enabled
1489 if (!mBandwidthControlEnabled) return;
1490
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001491 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001492 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001493 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001494 }
1495
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001496 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001498 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001499 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001500 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001501 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001502 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001503 }
1504 }
1505
1506 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001507 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001508 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001509
Jeff Sharkey350083e2011-06-29 10:45:16 -07001510 // silently discard when control disabled
1511 // TODO: eventually migrate to be always enabled
1512 if (!mBandwidthControlEnabled) return;
1513
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001514 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001515 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001516 // TODO: eventually consider throwing
1517 return;
1518 }
1519
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001520 mActiveQuotas.remove(iface);
1521 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001522
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001523 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001524 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001525 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001526 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001527 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001528 }
1529 }
1530 }
1531
1532 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001533 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001534 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001535
1536 // silently discard when control disabled
1537 // TODO: eventually migrate to be always enabled
1538 if (!mBandwidthControlEnabled) return;
1539
1540 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001541 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001542 throw new IllegalStateException("setting alert requires existing quota on iface");
1543 }
1544
1545 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001546 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001547 throw new IllegalStateException("iface " + iface + " already has alert");
1548 }
1549
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001550 try {
1551 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001552 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001553 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001554 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001555 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001556 }
1557 }
1558 }
1559
1560 @Override
1561 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001562 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001563
1564 // silently discard when control disabled
1565 // TODO: eventually migrate to be always enabled
1566 if (!mBandwidthControlEnabled) return;
1567
1568 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001569 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001570 // TODO: eventually consider throwing
1571 return;
1572 }
1573
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001574 try {
1575 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001576 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001577 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001578 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001579 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001580 }
1581 }
1582 }
1583
1584 @Override
1585 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001586 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001587
1588 // silently discard when control disabled
1589 // TODO: eventually migrate to be always enabled
1590 if (!mBandwidthControlEnabled) return;
1591
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001592 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001593 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001594 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001595 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001596 }
1597 }
1598
1599 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001600 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001601 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001602
Jeff Sharkey350083e2011-06-29 10:45:16 -07001603 // silently discard when control disabled
1604 // TODO: eventually migrate to be always enabled
1605 if (!mBandwidthControlEnabled) return;
1606
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001607 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001608 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1609 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1610 // TODO: eventually consider throwing
1611 return;
1612 }
1613
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001614 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001615 mConnector.execute("bandwidth",
1616 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001617 if (rejectOnQuotaInterfaces) {
1618 mUidRejectOnQuota.put(uid, true);
1619 } else {
1620 mUidRejectOnQuota.delete(uid);
1621 }
1622 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001623 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001624 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001625 }
1626 }
1627
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001628 @Override
1629 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001630 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001631 return mBandwidthControlEnabled;
1632 }
1633
1634 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001635 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001636 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001637 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001638 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001639 } catch (IOException e) {
1640 throw new IllegalStateException(e);
1641 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001642 }
1643
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001644 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001645 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001646 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001647
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001648 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001649 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001650 final NativeDaemonEvent[] events = mConnector.executeForList(
1651 "bandwidth", "gettetherstats");
1652 for (NativeDaemonEvent event : events) {
1653 if (event.getCode() != TetheringStatsListResult) continue;
1654
1655 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1656 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1657 try {
1658 final String ifaceIn = tok.nextToken();
1659 final String ifaceOut = tok.nextToken();
1660
1661 final NetworkStats.Entry entry = new NetworkStats.Entry();
1662 entry.iface = ifaceOut;
1663 entry.uid = UID_TETHERING;
1664 entry.set = SET_DEFAULT;
1665 entry.tag = TAG_NONE;
1666 entry.rxBytes = Long.parseLong(tok.nextToken());
1667 entry.rxPackets = Long.parseLong(tok.nextToken());
1668 entry.txBytes = Long.parseLong(tok.nextToken());
1669 entry.txPackets = Long.parseLong(tok.nextToken());
1670 stats.combineValues(entry);
1671 } catch (NoSuchElementException e) {
1672 throw new IllegalStateException("problem parsing tethering stats: " + event);
1673 } catch (NumberFormatException e) {
1674 throw new IllegalStateException("problem parsing tethering stats: " + event);
1675 }
1676 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001677 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001678 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001679 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001680 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001681 }
1682
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001683 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001684 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001685 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001686
Paul Jensen13e817d2014-04-10 14:16:37 -04001687 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001688 (domains == null ? "" : domains));
1689
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001690 for (String s : servers) {
1691 InetAddress a = NetworkUtils.numericToInetAddress(s);
1692 if (a.isAnyLocalAddress() == false) {
1693 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001694 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001695 }
1696
1697 try {
1698 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001699 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001700 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001701 }
1702 }
1703
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001704 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001705 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001706 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1707 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001708 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001709 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001710 } catch (NativeDaemonConnectorException e) {
1711 throw e.rethrowAsParcelableException();
1712 }
1713 }
1714
1715 @Override
1716 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1718 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001719 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001720 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001721 } catch (NativeDaemonConnectorException e) {
1722 throw e.rethrowAsParcelableException();
1723 }
1724 }
1725
1726 @Override
1727 public void setMarkedForwarding(String iface) {
1728 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1729 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001730 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001731 } catch (NativeDaemonConnectorException e) {
1732 throw e.rethrowAsParcelableException();
1733 }
1734 }
1735
1736 @Override
1737 public void clearMarkedForwarding(String iface) {
1738 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1739 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001740 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1741 } catch (NativeDaemonConnectorException e) {
1742 throw e.rethrowAsParcelableException();
1743 }
1744 }
1745
1746 @Override
1747 public int getMarkForUid(int uid) {
1748 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1749 final NativeDaemonEvent event;
1750 try {
1751 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1752 } catch (NativeDaemonConnectorException e) {
1753 throw e.rethrowAsParcelableException();
1754 }
1755 event.checkCode(GetMarkResult);
1756 return Integer.parseInt(event.getMessage());
1757 }
1758
1759 @Override
1760 public int getMarkForProtect() {
1761 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1762 final NativeDaemonEvent event;
1763 try {
1764 event = mConnector.execute("interface", "fwmark", "get", "protect");
1765 } catch (NativeDaemonConnectorException e) {
1766 throw e.rethrowAsParcelableException();
1767 }
1768 event.checkCode(GetMarkResult);
1769 return Integer.parseInt(event.getMessage());
1770 }
1771
1772 @Override
1773 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1774 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1775 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001776 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001777 mConnector.execute("interface", "fwmark", "route", "add", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001778 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubakercca54c42013-06-27 17:41:38 -07001779 } catch (NativeDaemonConnectorException e) {
1780 throw e.rethrowAsParcelableException();
1781 }
1782 }
1783
1784 @Override
1785 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1786 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1787 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001788 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001789 mConnector.execute("interface", "fwmark", "route", "remove", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001790 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001791 } catch (NativeDaemonConnectorException e) {
1792 throw e.rethrowAsParcelableException();
1793 }
1794 }
1795
1796 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001797 public void setHostExemption(LinkAddress host) {
1798 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1799 try {
1800 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1801 } catch (NativeDaemonConnectorException e) {
1802 throw e.rethrowAsParcelableException();
1803 }
1804 }
1805
1806 @Override
1807 public void clearHostExemption(LinkAddress host) {
1808 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1809 try {
1810 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1811 } catch (NativeDaemonConnectorException e) {
1812 throw e.rethrowAsParcelableException();
1813 }
1814 }
1815
1816 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001817 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001818 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1819 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001820 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001821 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001822 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001823 }
1824 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001825
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001826 @Override
1827 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001828 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001829 try {
1830 mConnector.execute("firewall", enabled ? "enable" : "disable");
1831 mFirewallEnabled = enabled;
1832 } catch (NativeDaemonConnectorException e) {
1833 throw e.rethrowAsParcelableException();
1834 }
1835 }
1836
1837 @Override
1838 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001839 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001840 return mFirewallEnabled;
1841 }
1842
1843 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001844 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001845 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001846 Preconditions.checkState(mFirewallEnabled);
1847 final String rule = allow ? ALLOW : DENY;
1848 try {
1849 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1850 } catch (NativeDaemonConnectorException e) {
1851 throw e.rethrowAsParcelableException();
1852 }
1853 }
1854
1855 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001856 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001857 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001858 Preconditions.checkState(mFirewallEnabled);
1859 final String rule = allow ? ALLOW : DENY;
1860 try {
1861 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1862 } catch (NativeDaemonConnectorException e) {
1863 throw e.rethrowAsParcelableException();
1864 }
1865 }
1866
1867 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001868 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001869 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001870 Preconditions.checkState(mFirewallEnabled);
1871 final String rule = allow ? ALLOW : DENY;
1872 try {
1873 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1874 } catch (NativeDaemonConnectorException e) {
1875 throw e.rethrowAsParcelableException();
1876 }
1877 }
1878
1879 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001880 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001881 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001882 Preconditions.checkState(mFirewallEnabled);
1883 final String rule = allow ? ALLOW : DENY;
1884 try {
1885 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1886 } catch (NativeDaemonConnectorException e) {
1887 throw e.rethrowAsParcelableException();
1888 }
1889 }
1890
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001891 private static void enforceSystemUid() {
1892 final int uid = Binder.getCallingUid();
1893 if (uid != Process.SYSTEM_UID) {
1894 throw new SecurityException("Only available to AID_SYSTEM");
1895 }
1896 }
1897
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001898 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001899 public void startClatd(String interfaceName) throws IllegalStateException {
1900 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1901
1902 try {
1903 mConnector.execute("clatd", "start", interfaceName);
1904 } catch (NativeDaemonConnectorException e) {
1905 throw e.rethrowAsParcelableException();
1906 }
1907 }
1908
1909 @Override
1910 public void stopClatd() throws IllegalStateException {
1911 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1912
1913 try {
1914 mConnector.execute("clatd", "stop");
1915 } catch (NativeDaemonConnectorException e) {
1916 throw e.rethrowAsParcelableException();
1917 }
1918 }
1919
1920 @Override
1921 public boolean isClatdStarted() {
1922 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1923
1924 final NativeDaemonEvent event;
1925 try {
1926 event = mConnector.execute("clatd", "status");
1927 } catch (NativeDaemonConnectorException e) {
1928 throw e.rethrowAsParcelableException();
1929 }
1930
1931 event.checkCode(ClatdStatusResult);
1932 return event.getMessage().endsWith("started");
1933 }
1934
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001935 @Override
1936 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1937 mNetworkActivityListeners.register(listener);
1938 }
1939
1940 @Override
1941 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1942 mNetworkActivityListeners.unregister(listener);
1943 }
1944
1945 @Override
1946 public boolean isNetworkActive() {
1947 synchronized (mNetworkActivityListeners) {
1948 return mNetworkActive || mActiveIdleTimers.isEmpty();
1949 }
1950 }
1951
1952 private void reportNetworkActive() {
1953 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001954 try {
1955 for (int i = 0; i < length; i++) {
1956 try {
1957 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1958 } catch (RemoteException e) {
1959 } catch (RuntimeException e) {
1960 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001961 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001962 } finally {
1963 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001964 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001965 }
1966
Mattias Falk8b47b362011-08-23 14:15:13 +02001967 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001968 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001969 public void monitor() {
1970 if (mConnector != null) {
1971 mConnector.monitor();
1972 }
1973 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001974
1975 @Override
1976 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1977 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1978
Robert Greenwalt470fd722012-01-18 12:51:15 -08001979 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1980 mConnector.dump(fd, pw, args);
1981 pw.println();
1982
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001983 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001984 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1985 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1986 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001987
1988 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001989 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1990 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001991 }
1992
1993 synchronized (mUidRejectOnQuota) {
1994 pw.print("UID reject on quota ifaces: [");
1995 final int size = mUidRejectOnQuota.size();
1996 for (int i = 0; i < size; i++) {
1997 pw.print(mUidRejectOnQuota.keyAt(i));
1998 if (i < size - 1) pw.print(",");
1999 }
2000 pw.println("]");
2001 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002002
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002003 synchronized (mIdleTimerLock) {
2004 pw.println("Idle timers:");
2005 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
2006 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
2007 IdleTimerParams params = ent.getValue();
2008 pw.print(" timeout="); pw.print(params.timeout);
2009 pw.print(" type="); pw.print(params.type);
2010 pw.print(" networkCount="); pw.println(params.networkCount);
2011 }
2012 }
2013
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002014 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002015 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002016
Robert Greenwalt568891d2014-04-04 13:38:00 -07002017 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002018 public void createNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002019 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2020
2021 try {
Paul Jensen992f2522014-04-28 10:33:11 -04002022 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002023 } catch (NativeDaemonConnectorException e) {
2024 throw e.rethrowAsParcelableException();
2025 }
2026 }
2027
Robert Greenwalt568891d2014-04-04 13:38:00 -07002028 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002029 public void removeNetwork(int netId) {
2030 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2031
2032 try {
2033 mConnector.execute("network", "destroy", netId);
2034 } catch (NativeDaemonConnectorException e) {
2035 throw e.rethrowAsParcelableException();
2036 }
2037 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002038
2039 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002040 public void addInterfaceToNetwork(String iface, int netId) {
2041 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2042
2043 try {
2044 mConnector.execute("network", "addiface", netId, iface);
2045 } catch (NativeDaemonConnectorException e) {
2046 throw e.rethrowAsParcelableException();
2047 }
2048 }
2049
2050 @Override
2051 public void removeInterfaceFromNetwork(String iface, int netId) {
2052 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2053
2054 try {
2055 mConnector.execute("network", "removeiface", netId, iface);
2056 } catch (NativeDaemonConnectorException e) {
2057 throw e.rethrowAsParcelableException();
2058 }
2059 }
2060
2061 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002062 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2063 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002064 }
2065
2066 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002067 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2068 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002069 }
2070
Robert Greenwalt913c8952014-04-07 17:36:35 -07002071 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002072 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2073
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002074 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002075
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002076 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002077 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002078 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002079 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002080 if (routeInfo.hasGateway()) {
2081 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2082 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002083
2084 try {
2085 mConnector.execute(cmd);
2086 } catch (NativeDaemonConnectorException e) {
2087 throw e.rethrowAsParcelableException();
2088 }
2089 }
2090
2091 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002092 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002093 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2094
2095 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002096 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002097 } catch (NativeDaemonConnectorException e) {
2098 throw e.rethrowAsParcelableException();
2099 }
2100 }
2101
2102 @Override
2103 public void clearDefaultNetId() {
2104 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2105
2106 try {
2107 mConnector.execute("network", "default", "clear");
2108 } catch (NativeDaemonConnectorException e) {
2109 throw e.rethrowAsParcelableException();
2110 }
2111 }
2112
2113 @Override
2114 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2116
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002117 final Command cmd = new Command("network", "permission", "user", "set");
2118 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2119 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002120 for (int i=0; i<uids.length; i++) {
2121 cmd.appendArg(uids[i]);
2122 }
2123
2124 try {
2125 mConnector.execute(cmd);
2126 } catch (NativeDaemonConnectorException e) {
2127 throw e.rethrowAsParcelableException();
2128 }
2129 }
2130
2131 @Override
2132 public void clearPermission(int[] uids) {
2133 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2134
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002135 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002136 for (int i=0; i<uids.length; i++) {
2137 cmd.appendArg(uids[i]);
2138 }
2139
2140 try {
2141 mConnector.execute(cmd);
2142 } catch (NativeDaemonConnectorException e) {
2143 throw e.rethrowAsParcelableException();
2144 }
2145 }
San Mehat873f2142010-01-14 10:25:07 -08002146}