blob: 1318f66f36932f36b478b1b33d1f27b6414df549 [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;
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -070028import static android.system.OsConstants.AF_INET;
29import static android.system.OsConstants.AF_INET6;
Lorenzo Colitti79751842013-02-28 16:16:03 +090030import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080031import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080033import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
36import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070037import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080038import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070039import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070040
San Mehat873f2142010-01-14 10:25:07 -080041import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080042import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080043import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070044import android.net.InterfaceConfiguration;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090045import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080046import android.net.LinkAddress;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070047import android.net.Network;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070048import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080049import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070050import android.net.RouteInfo;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040051import android.net.UidRange;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080052import android.net.wifi.WifiConfiguration;
53import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070054import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070055import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070056import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080057import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080058import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080059import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070060import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080061import android.os.RemoteCallbackList;
62import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070063import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070064import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080065import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070066import android.telephony.DataConnectionRealTimeInfo;
67import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070068import android.telephony.SubscriptionManager;
Wink Saville67e07892014-06-18 16:43:14 -070069import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080070import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080071import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070072import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080073
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070074import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070076import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080077import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070078import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070079import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070080import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070081
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070082import java.io.BufferedReader;
83import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080084import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070085import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070086import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070087import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070088import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070089import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070090import java.net.Inet4Address;
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -070091import java.net.Inet6Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070093import java.net.InterfaceAddress;
94import java.net.NetworkInterface;
95import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070096import java.util.ArrayList;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040097import java.util.Arrays;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070098import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080099import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700100import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700101import java.util.NoSuchElementException;
102import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700103import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -0800104
105/**
106 * @hide
107 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700108public class NetworkManagementService extends INetworkManagementService.Stub
109 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700110 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700111 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700112 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900113 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700114
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400115 private static final int MAX_UID_RANGES_PER_COMMAND = 10;
116
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700117 /**
118 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
119 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
120 */
121 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
122
San Mehat873f2142010-01-14 10:25:07 -0800123 class NetdResponseCode {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -0700124 /* Keep in sync with system/netd/server/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800125 public static final int InterfaceListResult = 110;
126 public static final int TetherInterfaceListResult = 111;
127 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800128 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700129 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800130
131 public static final int TetherStatusResult = 210;
132 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800133 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800134 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700135 public static final int InterfaceRxCounterResult = 216;
136 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700137 public static final int QuotaCounterResult = 220;
138 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800139 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900140 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800141
142 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700143 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700144 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900145 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900146 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900147 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800148 }
149
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700150 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
151
San Mehat873f2142010-01-14 10:25:07 -0800152 /**
153 * Binder context for this service
154 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700155 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800156
157 /**
158 * connector object for communicating with netd
159 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700160 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800161
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700162 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700163 private final Handler mDaemonHandler;
164 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700165
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800166 private IBatteryStats mBatteryStats;
167
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700168 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700169 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700170
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800171 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
172 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800173
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700174 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
175
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700176 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700177 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700178 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700179 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700180 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700181 /** Set of UIDs with active reject rules. */
182 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
183
Haoyu Bai04124232012-06-28 15:26:19 -0700184 private Object mIdleTimerLock = new Object();
185 /** Set of interfaces with active idle timers. */
186 private static class IdleTimerParams {
187 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800188 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700189 public int networkCount;
190
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800191 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700192 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800193 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700194 this.networkCount = 1;
195 }
196 }
197 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
198
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700199 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700200 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700201
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700202 private boolean mMobileActivityFromRadio = false;
203 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
204
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800205 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
206 new RemoteCallbackList<INetworkActivityListener>();
207 private boolean mNetworkActive;
208
San Mehat873f2142010-01-14 10:25:07 -0800209 /**
210 * Constructs a new NetworkManagementService instance
211 *
212 * @param context Binder context for this service
213 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900214 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800215 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800216
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700217 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
218 mFgHandler = new Handler(FgThread.get().getLooper());
219
Marco Nelissen62dbb222010-02-18 10:56:30 -0800220 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700221 mConnector = null;
222 mThread = null;
223 mDaemonHandler = null;
224 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800225 return;
226 }
227
Dianne Hackborn4590e522014-03-24 13:36:46 -0700228 // Don't need this wake lock, since we now have a time stamp for when
229 // the network actually went inactive. (It might be nice to still do this,
230 // but I don't want to do it through the power manager because that pollutes the
231 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700232 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700233 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800234
San Mehat873f2142010-01-14 10:25:07 -0800235 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700236 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
237 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700238 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700239
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700240 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Saville67e07892014-06-18 16:43:14 -0700241
242 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUB_ID,
Wink Savillefb40dd42014-06-12 17:02:31 -0700243 mDaemonHandler.getLooper()) {
Wink Saville67e07892014-06-18 16:43:14 -0700244 @Override
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700245 public void onDataConnectionRealTimeInfoChanged(
246 DataConnectionRealTimeInfo dcRtInfo) {
Wink Saville67e07892014-06-18 16:43:14 -0700247 if (DBG) Slog.d(TAG, "onDataConnectionRealTimeInfoChanged: " + dcRtInfo);
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700248 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
249 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700250 }
251 };
Wink Saville67e07892014-06-18 16:43:14 -0700252 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
253 if (tm != null) {
254 tm.listen(mPhoneStateListener,
255 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO);
256 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700257
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700258 // Add ourself to the Watchdog monitors.
259 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700260 }
261
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900262 static NetworkManagementService create(Context context,
263 String socket) throws InterruptedException {
264 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700265 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700266 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
267 service.mThread.start();
268 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700269 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700270 if (DBG) Slog.d(TAG, "Connected");
271 return service;
San Mehat873f2142010-01-14 10:25:07 -0800272 }
273
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900274 public static NetworkManagementService create(Context context) throws InterruptedException {
275 return create(context, NETD_SOCKET_NAME);
276 }
277
Jeff Sharkey350083e2011-06-29 10:45:16 -0700278 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700279 prepareNativeDaemon();
280 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700281 }
282
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800283 private IBatteryStats getBatteryStats() {
284 synchronized (this) {
285 if (mBatteryStats != null) {
286 return mBatteryStats;
287 }
288 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
289 BatteryStats.SERVICE_NAME));
290 return mBatteryStats;
291 }
292 }
293
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800294 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800296 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800298 }
299
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800300 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800301 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800302 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800303 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800304 }
305
306 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700307 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800308 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700309 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800310 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700311 try {
312 for (int i = 0; i < length; i++) {
313 try {
314 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
315 } catch (RemoteException e) {
316 } catch (RuntimeException e) {
317 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700318 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700319 } finally {
320 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700321 }
322 }
323
324 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700325 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700326 * (typically, an Ethernet cable has been plugged-in or unplugged).
327 */
328 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800329 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700330 try {
331 for (int i = 0; i < length; i++) {
332 try {
333 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
334 } catch (RemoteException e) {
335 } catch (RuntimeException e) {
336 }
San Mehat4d02d002010-01-22 16:07:46 -0800337 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700338 } finally {
339 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800340 }
341 }
342
343 /**
344 * Notify our observers of an interface addition.
345 */
346 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800347 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700348 try {
349 for (int i = 0; i < length; i++) {
350 try {
351 mObservers.getBroadcastItem(i).interfaceAdded(iface);
352 } catch (RemoteException e) {
353 } catch (RuntimeException e) {
354 }
San Mehat4d02d002010-01-22 16:07:46 -0800355 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700356 } finally {
357 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800358 }
359 }
360
361 /**
362 * Notify our observers of an interface removal.
363 */
364 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700365 // netd already clears out quota and alerts for removed ifaces; update
366 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700367 mActiveAlerts.remove(iface);
368 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700369
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800370 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700371 try {
372 for (int i = 0; i < length; i++) {
373 try {
374 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
375 } catch (RemoteException e) {
376 } catch (RuntimeException e) {
377 }
San Mehat4d02d002010-01-22 16:07:46 -0800378 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700379 } finally {
380 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800381 }
382 }
383
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700384 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700385 * Notify our observers of a limit reached.
386 */
387 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800388 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700389 try {
390 for (int i = 0; i < length; i++) {
391 try {
392 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
393 } catch (RemoteException e) {
394 } catch (RuntimeException e) {
395 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700396 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700397 } finally {
398 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700399 }
400 }
401
402 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700403 * Notify our observers of a change in the data activity state of the interface
404 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700405 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
406 boolean fromRadio) {
407 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
408 if (isMobile) {
409 if (!fromRadio) {
410 if (mMobileActivityFromRadio) {
411 // If this call is not coming from a report from the radio itself, but we
412 // have previously received reports from the radio, then we will take the
413 // power state to just be whatever the radio last reported.
414 powerState = mLastPowerStateFromRadio;
415 }
416 } else {
417 mMobileActivityFromRadio = true;
418 }
419 if (mLastPowerStateFromRadio != powerState) {
420 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700421 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700422 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700423 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700424 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700425 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700426 }
427
428 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
429 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
430
431 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
432 // Report the change in data activity. We don't do this if this is a change
433 // on the mobile network, that is not coming from the radio itself, and we
434 // have previously seen change reports from the radio. In that case only
435 // the radio is the authority for the current state.
436 final int length = mObservers.beginBroadcast();
437 try {
438 for (int i = 0; i < length; i++) {
439 try {
440 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
441 Integer.toString(type), isActive, tsNanos);
442 } catch (RemoteException e) {
443 } catch (RuntimeException e) {
444 }
445 }
446 } finally {
447 mObservers.finishBroadcast();
448 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700449 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800450
451 boolean report = false;
452 synchronized (mIdleTimerLock) {
453 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700454 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800455 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700456 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800457 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700458 if (mNetworkActive != isActive) {
459 mNetworkActive = isActive;
460 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800461 }
462 }
463 if (report) {
464 reportNetworkActive();
465 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700466 }
467
468 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700469 * Prepare native daemon once connected, enabling modules and pushing any
470 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700471 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700472 private void prepareNativeDaemon() {
473 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700474
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700475 // only enable bandwidth control when support exists
476 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
477 if (hasKernelSupport) {
478 Slog.d(TAG, "enabling bandwidth control");
479 try {
480 mConnector.execute("bandwidth", "enable");
481 mBandwidthControlEnabled = true;
482 } catch (NativeDaemonConnectorException e) {
483 Log.wtf(TAG, "problem enabling bandwidth controls", e);
484 }
485 } else {
486 Slog.d(TAG, "not enabling bandwidth control");
487 }
488
489 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
490
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700491 if (mBandwidthControlEnabled) {
492 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800493 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700494 } catch (RemoteException e) {
495 }
496 }
497
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700498 // push any existing quota or UID rules
499 synchronized (mQuotaLock) {
500 int size = mActiveQuotas.size();
501 if (size > 0) {
502 Slog.d(TAG, "pushing " + size + " active quota rules");
503 final HashMap<String, Long> activeQuotas = mActiveQuotas;
504 mActiveQuotas = Maps.newHashMap();
505 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
506 setInterfaceQuota(entry.getKey(), entry.getValue());
507 }
508 }
509
510 size = mActiveAlerts.size();
511 if (size > 0) {
512 Slog.d(TAG, "pushing " + size + " active alert rules");
513 final HashMap<String, Long> activeAlerts = mActiveAlerts;
514 mActiveAlerts = Maps.newHashMap();
515 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
516 setInterfaceAlert(entry.getKey(), entry.getValue());
517 }
518 }
519
520 size = mUidRejectOnQuota.size();
521 if (size > 0) {
522 Slog.d(TAG, "pushing " + size + " active uid rules");
523 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
524 mUidRejectOnQuota = new SparseBooleanArray();
525 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
526 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
527 }
528 }
529 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700530
531 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700532 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700533 }
San Mehat4d02d002010-01-22 16:07:46 -0800534
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900535 /**
536 * Notify our observers of a new or updated interface address.
537 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900538 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900539 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700540 try {
541 for (int i = 0; i < length; i++) {
542 try {
543 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
544 } catch (RemoteException e) {
545 } catch (RuntimeException e) {
546 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900547 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700548 } finally {
549 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900550 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900551 }
552
553 /**
554 * Notify our observers of a deleted interface address.
555 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900556 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900557 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700558 try {
559 for (int i = 0; i < length; i++) {
560 try {
561 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
562 } catch (RemoteException e) {
563 } catch (RuntimeException e) {
564 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900565 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700566 } finally {
567 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900568 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900569 }
570
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900571 /**
572 * Notify our observers of DNS server information received.
573 */
574 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
575 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700576 try {
577 for (int i = 0; i < length; i++) {
578 try {
579 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
580 addresses);
581 } catch (RemoteException e) {
582 } catch (RuntimeException e) {
583 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900584 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700585 } finally {
586 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900587 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900588 }
589
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900590 /**
591 * Notify our observers of a route change.
592 */
593 private void notifyRouteChange(String action, RouteInfo route) {
594 final int length = mObservers.beginBroadcast();
595 try {
596 for (int i = 0; i < length; i++) {
597 try {
598 if (action.equals("updated")) {
599 mObservers.getBroadcastItem(i).routeUpdated(route);
600 } else {
601 mObservers.getBroadcastItem(i).routeRemoved(route);
602 }
603 } catch (RemoteException e) {
604 } catch (RuntimeException e) {
605 }
606 }
607 } finally {
608 mObservers.finishBroadcast();
609 }
610 }
611
San Mehat873f2142010-01-14 10:25:07 -0800612 //
613 // Netd Callback handling
614 //
615
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700616 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
617 @Override
San Mehat873f2142010-01-14 10:25:07 -0800618 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700619 // event is dispatched from internal NDC thread, so we prepare the
620 // daemon back on main thread.
621 if (mConnectedSignal != null) {
622 mConnectedSignal.countDown();
623 mConnectedSignal = null;
624 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700625 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700626 @Override
627 public void run() {
628 prepareNativeDaemon();
629 }
630 });
631 }
San Mehat873f2142010-01-14 10:25:07 -0800632 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700633
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700634 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800635 public boolean onCheckHoldWakeLock(int code) {
636 return code == NetdResponseCode.InterfaceClassActivity;
637 }
638
639 @Override
San Mehat873f2142010-01-14 10:25:07 -0800640 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900641 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700642 switch (code) {
643 case NetdResponseCode.InterfaceChange:
644 /*
645 * a network interface change occured
646 * Format: "NNN Iface added <name>"
647 * "NNN Iface removed <name>"
648 * "NNN Iface changed <name> <up/down>"
649 * "NNN Iface linkstatus <name> <up/down>"
650 */
651 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900652 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700653 }
654 if (cooked[2].equals("added")) {
655 notifyInterfaceAdded(cooked[3]);
656 return true;
657 } else if (cooked[2].equals("removed")) {
658 notifyInterfaceRemoved(cooked[3]);
659 return true;
660 } else if (cooked[2].equals("changed") && cooked.length == 5) {
661 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
662 return true;
663 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
664 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
665 return true;
666 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900667 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700668 // break;
669 case NetdResponseCode.BandwidthControl:
670 /*
671 * Bandwidth control needs some attention
672 * Format: "NNN limit alert <alertName> <ifaceName>"
673 */
674 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900675 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700676 }
677 if (cooked[2].equals("alert")) {
678 notifyLimitReached(cooked[3], cooked[4]);
679 return true;
680 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900681 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700682 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700683 case NetdResponseCode.InterfaceClassActivity:
684 /*
685 * An network interface class state changed (active/idle)
686 * Format: "NNN IfaceClass <active/idle> <label>"
687 */
688 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900689 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700690 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700691 long timestampNanos = 0;
692 if (cooked.length == 5) {
693 try {
694 timestampNanos = Long.parseLong(cooked[4]);
695 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700696 } else {
697 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700698 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700699 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700700 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700701 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
702 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700703 return true;
704 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900705 case NetdResponseCode.InterfaceAddressChange:
706 /*
707 * A network address change occurred
708 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
709 * "NNN Address removed <addr> <iface> <flags> <scope>"
710 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900711 if (cooked.length < 7 || !cooked[1].equals("Address")) {
712 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900713 }
714
Lorenzo Colitti64483942013-11-15 18:43:52 +0900715 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900716 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900717 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900718 int flags = Integer.parseInt(cooked[5]);
719 int scope = Integer.parseInt(cooked[6]);
720 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900721 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
722 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900723 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900724 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900725 }
726
727 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900728 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900729 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900730 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900731 }
732 return true;
733 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900734 case NetdResponseCode.InterfaceDnsServerInfo:
735 /*
736 * Information about available DNS servers has been received.
737 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
738 */
739 long lifetime; // Actually a 32-bit unsigned integer.
740
741 if (cooked.length == 6 &&
742 cooked[1].equals("DnsInfo") &&
743 cooked[2].equals("servers")) {
744 try {
745 lifetime = Long.parseLong(cooked[4]);
746 } catch (NumberFormatException e) {
747 throw new IllegalStateException(errorMessage);
748 }
749 String[] servers = cooked[5].split(",");
750 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
751 }
752 return true;
753 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900754 case NetdResponseCode.RouteChange:
755 /*
756 * A route has been updated or removed.
757 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
758 */
759 if (!cooked[1].equals("Route") || cooked.length < 6) {
760 throw new IllegalStateException(errorMessage);
761 }
762
763 String via = null;
764 String dev = null;
765 boolean valid = true;
766 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
767 if (cooked[i].equals("dev")) {
768 if (dev == null) {
769 dev = cooked[i+1];
770 } else {
771 valid = false; // Duplicate interface.
772 }
773 } else if (cooked[i].equals("via")) {
774 if (via == null) {
775 via = cooked[i+1];
776 } else {
777 valid = false; // Duplicate gateway.
778 }
779 } else {
780 valid = false; // Unknown syntax.
781 }
782 }
783 if (valid) {
784 try {
785 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
786 InetAddress gateway = null;
787 if (via != null) gateway = InetAddress.parseNumericAddress(via);
788 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
789 notifyRouteChange(cooked[2], route);
790 return true;
791 } catch (IllegalArgumentException e) {}
792 }
793 throw new IllegalStateException(errorMessage);
794 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700795 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800796 }
797 return false;
San Mehat873f2142010-01-14 10:25:07 -0800798 }
799 }
800
San Mehated4fc8a2010-01-22 12:28:36 -0800801
San Mehat873f2142010-01-14 10:25:07 -0800802 //
803 // INetworkManagementService members
804 //
805
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800806 @Override
807 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800808 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700809 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800810 return NativeDaemonEvent.filterMessageList(
811 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700812 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800813 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700814 }
San Mehated4fc8a2010-01-22 12:28:36 -0800815 }
816
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800817 @Override
818 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800819 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800820
821 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700822 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700824 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800825 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700826 }
San Mehated4fc8a2010-01-22 12:28:36 -0800827
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800828 event.checkCode(InterfaceGetCfgResult);
829
830 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
831 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800832
Kenny Roota80ce062010-06-01 13:23:53 -0700833 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800834 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700835 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800836 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800837 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800838 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700839 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800840 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800841 } catch (IllegalArgumentException iae) {
842 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700843 }
844
845 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800846 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800847 } catch (NumberFormatException nfe) {
848 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700849 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800850
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800851 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
852 while (st.hasMoreTokens()) {
853 cfg.setFlag(st.nextToken());
854 }
Kenny Roota80ce062010-06-01 13:23:53 -0700855 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800856 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800857 }
San Mehated4fc8a2010-01-22 12:28:36 -0800858 return cfg;
859 }
860
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800861 @Override
862 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800863 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800864 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800865 if (linkAddr == null || linkAddr.getAddress() == null) {
866 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800867 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800868
869 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800870 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900871 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800872 for (String flag : cfg.getFlags()) {
873 cmd.appendArg(flag);
874 }
875
Kenny Roota80ce062010-06-01 13:23:53 -0700876 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800877 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700878 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800879 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700880 }
San Mehat873f2142010-01-14 10:25:07 -0800881 }
882
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800883 @Override
884 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800885 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800886 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800887 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800888 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700889 }
890
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800891 @Override
892 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800893 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800894 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800895 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800896 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700897 }
898
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800899 @Override
900 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800901 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700902 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 mConnector.execute(
904 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700905 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800906 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700907 }
908 }
909
Irfan Sherifff5600612011-06-16 10:26:28 -0700910 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
911 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800912 @Override
913 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800914 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700915 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800916 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700917 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800918 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700919 }
920 }
921
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800922 @Override
923 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800924 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700925 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800926 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700927 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800928 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700929 }
930 }
931
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800932 @Override
933 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800934 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700935 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700937 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800938 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700939 }
940 }
941
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800942 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700943 public void addRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700944 modifyRoute("add", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700945 }
946
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700948 public void removeRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700949 modifyRoute("remove", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700950 }
951
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700952 private void modifyRoute(String action, String netId, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800953 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700954
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700955 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700956
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700957 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700958 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700959 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900960 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700961 if (route.hasGateway()) {
962 cmd.appendArg(route.getGateway().getHostAddress());
963 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700964
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800965 try {
966 mConnector.execute(cmd);
967 } catch (NativeDaemonConnectorException e) {
968 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700969 }
970 }
971
972 private ArrayList<String> readRouteList(String filename) {
973 FileInputStream fstream = null;
974 ArrayList<String> list = new ArrayList<String>();
975
976 try {
977 fstream = new FileInputStream(filename);
978 DataInputStream in = new DataInputStream(fstream);
979 BufferedReader br = new BufferedReader(new InputStreamReader(in));
980 String s;
981
982 // throw away the title line
983
984 while (((s = br.readLine()) != null) && (s.length() != 0)) {
985 list.add(s);
986 }
987 } catch (IOException ex) {
988 // return current list, possibly empty
989 } finally {
990 if (fstream != null) {
991 try {
992 fstream.close();
993 } catch (IOException ex) {}
994 }
995 }
996
997 return list;
998 }
999
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001000 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001001 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001002 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001003 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1004
1005 // v4 routes listed as:
1006 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1007 for (String s : readRouteList("/proc/net/route")) {
1008 String[] fields = s.split("\t");
1009
1010 if (fields.length > 7) {
1011 String iface = fields[0];
1012
1013 if (interfaceName.equals(iface)) {
1014 String dest = fields[1];
1015 String gate = fields[2];
1016 String flags = fields[3]; // future use?
1017 String mask = fields[7];
1018 try {
1019 // address stored as a hex string, ex: 0014A8C0
1020 InetAddress destAddr =
1021 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1022 int prefixLength =
1023 NetworkUtils.netmaskIntToPrefixLength(
1024 (int)Long.parseLong(mask, 16));
1025 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1026
1027 // address stored as a hex string, ex 0014A8C0
1028 InetAddress gatewayAddr =
1029 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1030
Wink Saville7b5fd052013-03-15 05:07:04 +00001031 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001032 routes.add(route);
1033 } catch (Exception e) {
1034 Log.e(TAG, "Error parsing route " + s + " : " + e);
1035 continue;
1036 }
1037 }
1038 }
1039 }
1040
1041 // v6 routes listed as:
1042 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1043 for (String s : readRouteList("/proc/net/ipv6_route")) {
1044 String[]fields = s.split("\\s+");
1045 if (fields.length > 9) {
1046 String iface = fields[9].trim();
1047 if (interfaceName.equals(iface)) {
1048 String dest = fields[0];
1049 String prefix = fields[1];
1050 String gate = fields[4];
1051
1052 try {
1053 // prefix length stored as a hex string, ex 40
1054 int prefixLength = Integer.parseInt(prefix, 16);
1055
1056 // address stored as a 32 char hex string
1057 // ex fe800000000000000000000000000000
1058 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1059 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1060
1061 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1062
Wink Saville7b5fd052013-03-15 05:07:04 +00001063 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001064 routes.add(route);
1065 } catch (Exception e) {
1066 Log.e(TAG, "Error parsing route " + s + " : " + e);
1067 continue;
1068 }
1069 }
1070 }
1071 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001073 }
1074
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001075 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001076 public void setMtu(String iface, int mtu) {
1077 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1078
1079 final NativeDaemonEvent event;
1080 try {
1081 event = mConnector.execute("interface", "setmtu", iface, mtu);
1082 } catch (NativeDaemonConnectorException e) {
1083 throw e.rethrowAsParcelableException();
1084 }
1085 }
1086
1087 @Override
San Mehat873f2142010-01-14 10:25:07 -08001088 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001089 // TODO: remove from aidl if nobody calls externally
1090 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001091
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001093 }
1094
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001095 @Override
San Mehat873f2142010-01-14 10:25:07 -08001096 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001098
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001099 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001100 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001101 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001102 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001103 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001104 }
San Mehat873f2142010-01-14 10:25:07 -08001105
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 // 211 Forwarding enabled
1107 event.checkCode(IpFwdStatusResult);
1108 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001109 }
1110
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001111 @Override
1112 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001113 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001114 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001115 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001116 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001117 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001118 }
San Mehat873f2142010-01-14 10:25:07 -08001119 }
1120
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001121 @Override
1122 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001123 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001124 // cmd is "tether start first_start first_stop second_start second_stop ..."
1125 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001126
1127 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001128 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001129 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001130 }
Kenny Roota80ce062010-06-01 13:23:53 -07001131
1132 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001133 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001134 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001135 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001136 }
San Mehat873f2142010-01-14 10:25:07 -08001137 }
1138
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001139 @Override
1140 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001142 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001144 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001145 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001146 }
San Mehat873f2142010-01-14 10:25:07 -08001147 }
1148
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001149 @Override
1150 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001151 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001152
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001153 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001154 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001155 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001156 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001157 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001158 }
San Mehat873f2142010-01-14 10:25:07 -08001159
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001160 // 210 Tethering services started
1161 event.checkCode(TetherStatusResult);
1162 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001163 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001164
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001165 @Override
1166 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001167 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001168 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001169 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001170 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001171 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001172 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001173 List<RouteInfo> routes = new ArrayList<RouteInfo>();
1174 // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it
1175 // suitable to use as a route destination.
1176 routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface));
1177 addInterfaceToLocalNetwork(iface, routes);
San Mehat873f2142010-01-14 10:25:07 -08001178 }
1179
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001180 @Override
San Mehat873f2142010-01-14 10:25:07 -08001181 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001182 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001183 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001184 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001185 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001186 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001187 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001188 removeInterfaceFromLocalNetwork(iface);
San Mehat873f2142010-01-14 10:25:07 -08001189 }
1190
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001191 @Override
1192 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001193 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001194 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001195 return NativeDaemonEvent.filterMessageList(
1196 mConnector.executeForList("tether", "interface", "list"),
1197 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001198 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001199 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001200 }
San Mehat873f2142010-01-14 10:25:07 -08001201 }
1202
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001203 @Override
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001204 public void setDnsForwarders(Network network, String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001205 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001206
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001207 int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET;
1208 final Command cmd = new Command("tether", "dns", "set", netId);
1209
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001210 for (String s : dns) {
1211 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1212 }
1213
San Mehat873f2142010-01-14 10:25:07 -08001214 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001215 mConnector.execute(cmd);
1216 } catch (NativeDaemonConnectorException e) {
1217 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001218 }
1219 }
1220
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001221 @Override
1222 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001223 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001224 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001225 return NativeDaemonEvent.filterMessageList(
1226 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001227 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001228 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001229 }
San Mehat873f2142010-01-14 10:25:07 -08001230 }
1231
jiaguo1da35f72014-01-09 16:39:59 +08001232 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1233 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1234 for (InterfaceAddress ia : addresses) {
1235 if (!ia.getAddress().isLinkLocalAddress())
1236 filtered.add(ia);
1237 }
1238 return filtered;
1239 }
1240
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001241 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001242 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001243 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001244
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001245 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1246 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001247 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001248 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001249 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001250 // Don't touch link-local routes, as link-local addresses aren't routable,
1251 // kernel creates link-local routes on all interfaces automatically
1252 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1253 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001254 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001255 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001256 InetAddress addr = NetworkUtils.getNetworkPart(
1257 ia.getAddress(), ia.getNetworkPrefixLength());
1258 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001259 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001260 }
1261
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001262 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001263 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001264 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001265 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001266 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001267 }
1268
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001269 @Override
1270 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001271 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001272 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001273 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001274 } catch (SocketException e) {
1275 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001276 }
San Mehat873f2142010-01-14 10:25:07 -08001277 }
1278
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001279 @Override
1280 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001281 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001282 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001283 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001284 } catch (SocketException e) {
1285 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001286 }
San Mehat873f2142010-01-14 10:25:07 -08001287 }
San Mehat72759df2010-01-19 13:50:37 -08001288
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001289 @Override
1290 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001291 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001292 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001293 return NativeDaemonEvent.filterMessageList(
1294 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001295 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001296 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001297 }
San Mehat72759df2010-01-19 13:50:37 -08001298 }
1299
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001300 @Override
1301 public void attachPppd(
1302 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001303 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001304 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001305 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001306 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1307 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1308 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001309 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001310 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001311 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001312 }
1313 }
1314
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001315 @Override
1316 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001317 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001318 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001319 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001320 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001321 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001322 }
San Mehat72759df2010-01-19 13:50:37 -08001323 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001324
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001325 @Override
1326 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001327 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001328 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001329 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001330 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001331 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001332 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001333 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001334 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001335 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001336 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001337 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001338 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001339 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001340 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001341 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001342 }
1343
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001344 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001345 switch (wifiConfig.getAuthType()) {
1346 case KeyMgmt.WPA_PSK:
1347 return "wpa-psk";
1348 case KeyMgmt.WPA2_PSK:
1349 return "wpa2-psk";
1350 default:
1351 return "open";
1352 }
1353 }
1354
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001355 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001356 @Override
1357 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001358 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001359 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001360 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001361 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001362 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001363 }
1364 }
1365
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001366 @Override
1367 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001368 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001369 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001370 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001371 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001372 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001373 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001374 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001375 }
1376
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001377 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001378 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001379 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001380 try {
1381 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001382 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001383 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001384 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001385 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001386 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001387 }
1388 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001389 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001390 }
1391 }
San Mehat91cac642010-03-31 14:31:36 -07001392
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001393 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001394 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001395 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1396
1397 if (DBG) Slog.d(TAG, "Adding idletimer");
1398
1399 synchronized (mIdleTimerLock) {
1400 IdleTimerParams params = mActiveIdleTimers.get(iface);
1401 if (params != null) {
1402 // the interface already has idletimer, update network count
1403 params.networkCount++;
1404 return;
1405 }
1406
1407 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001408 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1409 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001410 } catch (NativeDaemonConnectorException e) {
1411 throw e.rethrowAsParcelableException();
1412 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001413 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1414
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001415 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001416 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1417 mNetworkActive = false;
1418 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001419 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001420 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001421 notifyInterfaceClassActivity(type,
1422 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1423 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001424 }
1425 });
Haoyu Bai04124232012-06-28 15:26:19 -07001426 }
1427 }
1428
1429 @Override
1430 public void removeIdleTimer(String iface) {
1431 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1432
1433 if (DBG) Slog.d(TAG, "Removing idletimer");
1434
1435 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001436 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001437 if (params == null || --(params.networkCount) > 0) {
1438 return;
1439 }
1440
1441 try {
1442 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001443 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001444 } catch (NativeDaemonConnectorException e) {
1445 throw e.rethrowAsParcelableException();
1446 }
1447 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001448 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001449 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001450 notifyInterfaceClassActivity(params.type,
1451 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1452 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001453 }
1454 });
Haoyu Bai04124232012-06-28 15:26:19 -07001455 }
1456 }
1457
1458 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001459 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001460 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001461 try {
1462 return mStatsFactory.readNetworkStatsSummaryDev();
1463 } catch (IOException e) {
1464 throw new IllegalStateException(e);
1465 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001466 }
1467
1468 @Override
1469 public NetworkStats getNetworkStatsSummaryXt() {
1470 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001471 try {
1472 return mStatsFactory.readNetworkStatsSummaryXt();
1473 } catch (IOException e) {
1474 throw new IllegalStateException(e);
1475 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001476 }
1477
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001478 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001479 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001481 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001482 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001483 } catch (IOException e) {
1484 throw new IllegalStateException(e);
1485 }
San Mehat91cac642010-03-31 14:31:36 -07001486 }
1487
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001488 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001489 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001490 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001491
Jeff Sharkey350083e2011-06-29 10:45:16 -07001492 // silently discard when control disabled
1493 // TODO: eventually migrate to be always enabled
1494 if (!mBandwidthControlEnabled) return;
1495
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001496 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001497 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001498 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001499 }
1500
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001501 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001502 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001503 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001504 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001505 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001506 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001507 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001508 }
1509 }
1510
1511 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001512 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001513 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001514
Jeff Sharkey350083e2011-06-29 10:45:16 -07001515 // silently discard when control disabled
1516 // TODO: eventually migrate to be always enabled
1517 if (!mBandwidthControlEnabled) return;
1518
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001519 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001520 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001521 // TODO: eventually consider throwing
1522 return;
1523 }
1524
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001525 mActiveQuotas.remove(iface);
1526 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001527
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001528 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001529 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001530 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001531 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001532 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001533 }
1534 }
1535 }
1536
1537 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001538 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001539 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001540
1541 // silently discard when control disabled
1542 // TODO: eventually migrate to be always enabled
1543 if (!mBandwidthControlEnabled) return;
1544
1545 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001546 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001547 throw new IllegalStateException("setting alert requires existing quota on iface");
1548 }
1549
1550 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001551 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001552 throw new IllegalStateException("iface " + iface + " already has alert");
1553 }
1554
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001555 try {
1556 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001557 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001558 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001559 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001560 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001561 }
1562 }
1563 }
1564
1565 @Override
1566 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001567 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001568
1569 // silently discard when control disabled
1570 // TODO: eventually migrate to be always enabled
1571 if (!mBandwidthControlEnabled) return;
1572
1573 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001574 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001575 // TODO: eventually consider throwing
1576 return;
1577 }
1578
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001579 try {
1580 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001581 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001582 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001583 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001584 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001585 }
1586 }
1587 }
1588
1589 @Override
1590 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001591 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001592
1593 // silently discard when control disabled
1594 // TODO: eventually migrate to be always enabled
1595 if (!mBandwidthControlEnabled) return;
1596
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001597 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001598 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001599 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001600 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001601 }
1602 }
1603
1604 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001605 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001606 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001607
Jeff Sharkey350083e2011-06-29 10:45:16 -07001608 // silently discard when control disabled
1609 // TODO: eventually migrate to be always enabled
1610 if (!mBandwidthControlEnabled) return;
1611
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001612 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001613 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1614 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1615 // TODO: eventually consider throwing
1616 return;
1617 }
1618
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001619 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001620 mConnector.execute("bandwidth",
1621 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001622 if (rejectOnQuotaInterfaces) {
1623 mUidRejectOnQuota.put(uid, true);
1624 } else {
1625 mUidRejectOnQuota.delete(uid);
1626 }
1627 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001628 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001629 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001630 }
1631 }
1632
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001633 @Override
1634 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001635 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001636 return mBandwidthControlEnabled;
1637 }
1638
1639 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001640 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001641 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001642 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001643 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001644 } catch (IOException e) {
1645 throw new IllegalStateException(e);
1646 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001647 }
1648
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001649 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001650 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001651 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001652
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001653 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001654 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001655 final NativeDaemonEvent[] events = mConnector.executeForList(
1656 "bandwidth", "gettetherstats");
1657 for (NativeDaemonEvent event : events) {
1658 if (event.getCode() != TetheringStatsListResult) continue;
1659
1660 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1661 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1662 try {
1663 final String ifaceIn = tok.nextToken();
1664 final String ifaceOut = tok.nextToken();
1665
1666 final NetworkStats.Entry entry = new NetworkStats.Entry();
1667 entry.iface = ifaceOut;
1668 entry.uid = UID_TETHERING;
1669 entry.set = SET_DEFAULT;
1670 entry.tag = TAG_NONE;
1671 entry.rxBytes = Long.parseLong(tok.nextToken());
1672 entry.rxPackets = Long.parseLong(tok.nextToken());
1673 entry.txBytes = Long.parseLong(tok.nextToken());
1674 entry.txPackets = Long.parseLong(tok.nextToken());
1675 stats.combineValues(entry);
1676 } catch (NoSuchElementException e) {
1677 throw new IllegalStateException("problem parsing tethering stats: " + event);
1678 } catch (NumberFormatException e) {
1679 throw new IllegalStateException("problem parsing tethering stats: " + event);
1680 }
1681 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001682 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001683 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001684 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001685 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001686 }
1687
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001688 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001689 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001690 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001691
Paul Jensen13e817d2014-04-10 14:16:37 -04001692 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001693 (domains == null ? "" : domains));
1694
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001695 for (String s : servers) {
1696 InetAddress a = NetworkUtils.numericToInetAddress(s);
1697 if (a.isAnyLocalAddress() == false) {
1698 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001699 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001700 }
1701
1702 try {
1703 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001704 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001705 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001706 }
1707 }
1708
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001709 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001710 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001711 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001712 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1713 argv[0] = "users";
1714 argv[1] = "add";
1715 argv[2] = netId;
1716 int argc = 3;
1717 // Avoid overly long commands by limiting number of UID ranges per command.
1718 for (int i = 0; i < ranges.length; i++) {
1719 argv[argc++] = ranges[i].toString();
1720 if (i == (ranges.length - 1) || argc == argv.length) {
1721 try {
1722 mConnector.execute("network", Arrays.copyOf(argv, argc));
1723 } catch (NativeDaemonConnectorException e) {
1724 throw e.rethrowAsParcelableException();
1725 }
1726 argc = 3;
1727 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001728 }
1729 }
1730
1731 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001732 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001734 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1735 argv[0] = "users";
1736 argv[1] = "remove";
1737 argv[2] = netId;
1738 int argc = 3;
1739 // Avoid overly long commands by limiting number of UID ranges per command.
1740 for (int i = 0; i < ranges.length; i++) {
1741 argv[argc++] = ranges[i].toString();
1742 if (i == (ranges.length - 1) || argc == argv.length) {
1743 try {
1744 mConnector.execute("network", Arrays.copyOf(argv, argc));
1745 } catch (NativeDaemonConnectorException e) {
1746 throw e.rethrowAsParcelableException();
1747 }
1748 argc = 3;
1749 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001750 }
1751 }
1752
1753 @Override
Paul Jensenb69a3a82014-08-06 15:34:26 -04001754 public void flushNetworkDnsCache(int netId) {
1755 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1756 try {
1757 mConnector.execute("resolver", "flushnet", netId);
1758 } catch (NativeDaemonConnectorException e) {
1759 throw e.rethrowAsParcelableException();
1760 }
1761 }
1762
1763 @Override
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001764 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001765 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001766 try {
1767 mConnector.execute("firewall", enabled ? "enable" : "disable");
1768 mFirewallEnabled = enabled;
1769 } catch (NativeDaemonConnectorException e) {
1770 throw e.rethrowAsParcelableException();
1771 }
1772 }
1773
1774 @Override
1775 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001776 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001777 return mFirewallEnabled;
1778 }
1779
1780 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001781 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001782 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001783 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001784 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001785 try {
1786 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1787 } catch (NativeDaemonConnectorException e) {
1788 throw e.rethrowAsParcelableException();
1789 }
1790 }
1791
1792 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001793 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001794 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001795 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001796 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001797 try {
1798 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1799 } catch (NativeDaemonConnectorException e) {
1800 throw e.rethrowAsParcelableException();
1801 }
1802 }
1803
1804 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001805 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001806 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001807 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001808 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001809 try {
1810 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1811 } catch (NativeDaemonConnectorException e) {
1812 throw e.rethrowAsParcelableException();
1813 }
1814 }
1815
1816 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001817 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001818 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001819 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001820 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001821 try {
1822 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1823 } catch (NativeDaemonConnectorException e) {
1824 throw e.rethrowAsParcelableException();
1825 }
1826 }
1827
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001828 private static void enforceSystemUid() {
1829 final int uid = Binder.getCallingUid();
1830 if (uid != Process.SYSTEM_UID) {
1831 throw new SecurityException("Only available to AID_SYSTEM");
1832 }
1833 }
1834
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001835 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001836 public void startClatd(String interfaceName) throws IllegalStateException {
1837 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1838
1839 try {
1840 mConnector.execute("clatd", "start", interfaceName);
1841 } catch (NativeDaemonConnectorException e) {
1842 throw e.rethrowAsParcelableException();
1843 }
1844 }
1845
1846 @Override
1847 public void stopClatd() throws IllegalStateException {
1848 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1849
1850 try {
1851 mConnector.execute("clatd", "stop");
1852 } catch (NativeDaemonConnectorException e) {
1853 throw e.rethrowAsParcelableException();
1854 }
1855 }
1856
1857 @Override
1858 public boolean isClatdStarted() {
1859 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1860
1861 final NativeDaemonEvent event;
1862 try {
1863 event = mConnector.execute("clatd", "status");
1864 } catch (NativeDaemonConnectorException e) {
1865 throw e.rethrowAsParcelableException();
1866 }
1867
1868 event.checkCode(ClatdStatusResult);
1869 return event.getMessage().endsWith("started");
1870 }
1871
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001872 @Override
1873 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1874 mNetworkActivityListeners.register(listener);
1875 }
1876
1877 @Override
1878 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1879 mNetworkActivityListeners.unregister(listener);
1880 }
1881
1882 @Override
1883 public boolean isNetworkActive() {
1884 synchronized (mNetworkActivityListeners) {
1885 return mNetworkActive || mActiveIdleTimers.isEmpty();
1886 }
1887 }
1888
1889 private void reportNetworkActive() {
1890 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001891 try {
1892 for (int i = 0; i < length; i++) {
1893 try {
1894 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1895 } catch (RemoteException e) {
1896 } catch (RuntimeException e) {
1897 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001898 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001899 } finally {
1900 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001901 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001902 }
1903
Mattias Falk8b47b362011-08-23 14:15:13 +02001904 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001905 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001906 public void monitor() {
1907 if (mConnector != null) {
1908 mConnector.monitor();
1909 }
1910 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001911
1912 @Override
1913 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1914 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1915
Robert Greenwalt470fd722012-01-18 12:51:15 -08001916 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1917 mConnector.dump(fd, pw, args);
1918 pw.println();
1919
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001920 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001921 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1922 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1923 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001924
1925 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001926 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1927 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001928 }
1929
1930 synchronized (mUidRejectOnQuota) {
1931 pw.print("UID reject on quota ifaces: [");
1932 final int size = mUidRejectOnQuota.size();
1933 for (int i = 0; i < size; i++) {
1934 pw.print(mUidRejectOnQuota.keyAt(i));
1935 if (i < size - 1) pw.print(",");
1936 }
1937 pw.println("]");
1938 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001939
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001940 synchronized (mIdleTimerLock) {
1941 pw.println("Idle timers:");
1942 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1943 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1944 IdleTimerParams params = ent.getValue();
1945 pw.print(" timeout="); pw.print(params.timeout);
1946 pw.print(" type="); pw.print(params.type);
1947 pw.print(" networkCount="); pw.println(params.networkCount);
1948 }
1949 }
1950
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001951 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001952 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001953
Robert Greenwalt568891d2014-04-04 13:38:00 -07001954 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001955 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001956 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1957
1958 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001959 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001960 } catch (NativeDaemonConnectorException e) {
1961 throw e.rethrowAsParcelableException();
1962 }
1963 }
1964
Robert Greenwalt568891d2014-04-04 13:38:00 -07001965 @Override
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001966 public void createVirtualNetwork(int netId, boolean hasDNS, boolean secure) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001967 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1968
1969 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001970 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0",
1971 secure ? "1" : "0");
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001972 } catch (NativeDaemonConnectorException e) {
1973 throw e.rethrowAsParcelableException();
1974 }
1975 }
1976
1977 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001978 public void removeNetwork(int netId) {
1979 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1980
1981 try {
1982 mConnector.execute("network", "destroy", netId);
1983 } catch (NativeDaemonConnectorException e) {
1984 throw e.rethrowAsParcelableException();
1985 }
1986 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001987
1988 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001989 public void addInterfaceToNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001990 modifyInterfaceInNetwork("add", "" + netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04001991 }
1992
1993 @Override
1994 public void removeInterfaceFromNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001995 modifyInterfaceInNetwork("remove", "" + netId, iface);
1996 }
Paul Jensen992f2522014-04-28 10:33:11 -04001997
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001998 private void modifyInterfaceInNetwork(String action, String netId, String iface) {
1999 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen992f2522014-04-28 10:33:11 -04002000 try {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002001 mConnector.execute("network", "interface", action, netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002002 } catch (NativeDaemonConnectorException e) {
2003 throw e.rethrowAsParcelableException();
2004 }
2005 }
2006
2007 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002008 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002009 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2010
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07002011 final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002012
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002013 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002014 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002015 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002016 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002017 if (routeInfo.hasGateway()) {
2018 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2019 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002020
2021 try {
2022 mConnector.execute(cmd);
2023 } catch (NativeDaemonConnectorException e) {
2024 throw e.rethrowAsParcelableException();
2025 }
2026 }
2027
2028 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002029 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002030 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2031
2032 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002033 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002034 } catch (NativeDaemonConnectorException e) {
2035 throw e.rethrowAsParcelableException();
2036 }
2037 }
2038
2039 @Override
2040 public void clearDefaultNetId() {
2041 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2042
2043 try {
2044 mConnector.execute("network", "default", "clear");
2045 } catch (NativeDaemonConnectorException e) {
2046 throw e.rethrowAsParcelableException();
2047 }
2048 }
2049
2050 @Override
2051 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2052 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2053
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002054 final Command cmd = new Command("network", "permission", "user", "set");
2055 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2056 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002057 for (int i=0; i<uids.length; i++) {
2058 cmd.appendArg(uids[i]);
2059 }
2060
2061 try {
2062 mConnector.execute(cmd);
2063 } catch (NativeDaemonConnectorException e) {
2064 throw e.rethrowAsParcelableException();
2065 }
2066 }
2067
2068 @Override
2069 public void clearPermission(int[] uids) {
2070 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2071
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002072 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002073 for (int i=0; i<uids.length; i++) {
2074 cmd.appendArg(uids[i]);
2075 }
2076
2077 try {
2078 mConnector.execute(cmd);
2079 } catch (NativeDaemonConnectorException e) {
2080 throw e.rethrowAsParcelableException();
2081 }
2082 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002083
2084 @Override
2085 public void allowProtect(int uid) {
2086 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2087
2088 try {
2089 mConnector.execute("network", "protect", "allow", uid);
2090 } catch (NativeDaemonConnectorException e) {
2091 throw e.rethrowAsParcelableException();
2092 }
2093 }
2094
2095 @Override
2096 public void denyProtect(int uid) {
2097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2098
2099 try {
2100 mConnector.execute("network", "protect", "deny", uid);
2101 } catch (NativeDaemonConnectorException e) {
2102 throw e.rethrowAsParcelableException();
2103 }
2104 }
2105
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002106 @Override
2107 public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) {
2108 modifyInterfaceInNetwork("add", "local", iface);
2109
2110 for (RouteInfo route : routes) {
2111 if (!route.isDefaultRoute()) {
2112 modifyRoute("add", "local", route);
2113 }
2114 }
2115 }
2116
2117 @Override
2118 public void removeInterfaceFromLocalNetwork(String iface) {
2119 modifyInterfaceInNetwork("remove", "local", iface);
2120 }
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -07002121
2122 @Override
2123 public void blockAddressFamily(int family, int netId, String iface) {
2124 modifyAddressFamily("add", family, netId, iface);
2125 }
2126
2127 @Override
2128 public void unblockAddressFamily(int family, int netId, String iface) {
2129 modifyAddressFamily("remove", family, netId, iface);
2130 }
2131
2132 private void modifyAddressFamily(String action, int family, int netId, String iface) {
2133 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2134
2135 final Command cmd = new Command("network", "route", action, netId, iface);
2136
2137 if (family == AF_INET) {
2138 cmd.appendArg(Inet4Address.ANY.getHostAddress() + "/0");
2139 } else if (family == AF_INET6) {
2140 cmd.appendArg(Inet6Address.ANY.getHostAddress() + "/0");
2141 } else {
2142 throw new IllegalStateException(family + " is neither " + AF_INET + " nor " + AF_INET6);
2143 }
2144
2145 cmd.appendArg("unreachable");
2146
2147 try {
2148 mConnector.execute(cmd);
2149 } catch (NativeDaemonConnectorException e) {
2150 throw e.rethrowAsParcelableException();
2151 }
2152 }
San Mehat873f2142010-01-14 10:25:07 -08002153}