blob: d03a15458b6966dcc4484948064d38c843f3f1a2 [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
Jeff Sharkey4529bb62011-12-14 10:31:54 -080019import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070020import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070022import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080023import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import static android.net.NetworkStats.TAG_NONE;
25import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070026import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +090027import static android.net.RouteInfo.RTN_THROW;
28import static android.net.RouteInfo.RTN_UNICAST;
29import static android.net.RouteInfo.RTN_UNREACHABLE;
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 Ramachandranb2829fa2014-04-15 19:07:12 -0700958 cmd.appendArg(route.getInterface());
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900959 cmd.appendArg(route.getDestination().toString());
960
961 switch (route.getType()) {
962 case RouteInfo.RTN_UNICAST:
963 if (route.hasGateway()) {
964 cmd.appendArg(route.getGateway().getHostAddress());
965 }
966 break;
967 case RouteInfo.RTN_UNREACHABLE:
968 cmd.appendArg("unreachable");
969 break;
970 case RouteInfo.RTN_THROW:
971 cmd.appendArg("throw");
972 break;
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700973 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700974
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800975 try {
976 mConnector.execute(cmd);
977 } catch (NativeDaemonConnectorException e) {
978 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700979 }
980 }
981
982 private ArrayList<String> readRouteList(String filename) {
983 FileInputStream fstream = null;
984 ArrayList<String> list = new ArrayList<String>();
985
986 try {
987 fstream = new FileInputStream(filename);
988 DataInputStream in = new DataInputStream(fstream);
989 BufferedReader br = new BufferedReader(new InputStreamReader(in));
990 String s;
991
992 // throw away the title line
993
994 while (((s = br.readLine()) != null) && (s.length() != 0)) {
995 list.add(s);
996 }
997 } catch (IOException ex) {
998 // return current list, possibly empty
999 } finally {
1000 if (fstream != null) {
1001 try {
1002 fstream.close();
1003 } catch (IOException ex) {}
1004 }
1005 }
1006
1007 return list;
1008 }
1009
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001010 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001011 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001012 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001013 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1014
1015 // v4 routes listed as:
1016 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1017 for (String s : readRouteList("/proc/net/route")) {
1018 String[] fields = s.split("\t");
1019
1020 if (fields.length > 7) {
1021 String iface = fields[0];
1022
1023 if (interfaceName.equals(iface)) {
1024 String dest = fields[1];
1025 String gate = fields[2];
1026 String flags = fields[3]; // future use?
1027 String mask = fields[7];
1028 try {
1029 // address stored as a hex string, ex: 0014A8C0
1030 InetAddress destAddr =
1031 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1032 int prefixLength =
1033 NetworkUtils.netmaskIntToPrefixLength(
1034 (int)Long.parseLong(mask, 16));
1035 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1036
1037 // address stored as a hex string, ex 0014A8C0
1038 InetAddress gatewayAddr =
1039 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1040
Wink Saville7b5fd052013-03-15 05:07:04 +00001041 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001042 routes.add(route);
1043 } catch (Exception e) {
1044 Log.e(TAG, "Error parsing route " + s + " : " + e);
1045 continue;
1046 }
1047 }
1048 }
1049 }
1050
1051 // v6 routes listed as:
1052 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1053 for (String s : readRouteList("/proc/net/ipv6_route")) {
1054 String[]fields = s.split("\\s+");
1055 if (fields.length > 9) {
1056 String iface = fields[9].trim();
1057 if (interfaceName.equals(iface)) {
1058 String dest = fields[0];
1059 String prefix = fields[1];
1060 String gate = fields[4];
1061
1062 try {
1063 // prefix length stored as a hex string, ex 40
1064 int prefixLength = Integer.parseInt(prefix, 16);
1065
1066 // address stored as a 32 char hex string
1067 // ex fe800000000000000000000000000000
1068 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1069 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1070
1071 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1072
Wink Saville7b5fd052013-03-15 05:07:04 +00001073 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001074 routes.add(route);
1075 } catch (Exception e) {
1076 Log.e(TAG, "Error parsing route " + s + " : " + e);
1077 continue;
1078 }
1079 }
1080 }
1081 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001082 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001083 }
1084
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001085 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001086 public void setMtu(String iface, int mtu) {
1087 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1088
1089 final NativeDaemonEvent event;
1090 try {
1091 event = mConnector.execute("interface", "setmtu", iface, mtu);
1092 } catch (NativeDaemonConnectorException e) {
1093 throw e.rethrowAsParcelableException();
1094 }
1095 }
1096
1097 @Override
San Mehat873f2142010-01-14 10:25:07 -08001098 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001099 // TODO: remove from aidl if nobody calls externally
1100 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001101
Joe Onorato8a9b2202010-02-26 18:56:32 -08001102 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001103 }
1104
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001105 @Override
San Mehat873f2142010-01-14 10:25:07 -08001106 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001107 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001108
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001109 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001110 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001111 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001112 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001113 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001114 }
San Mehat873f2142010-01-14 10:25:07 -08001115
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001116 // 211 Forwarding enabled
1117 event.checkCode(IpFwdStatusResult);
1118 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001119 }
1120
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001121 @Override
1122 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001123 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001124 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001125 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001126 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001127 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001128 }
San Mehat873f2142010-01-14 10:25:07 -08001129 }
1130
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001131 @Override
1132 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001133 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001134 // cmd is "tether start first_start first_stop second_start second_stop ..."
1135 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136
1137 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001138 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001140 }
Kenny Roota80ce062010-06-01 13:23:53 -07001141
1142 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 mConnector.execute(cmd);
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 void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001151 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001152 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001153 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001154 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001155 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001156 }
San Mehat873f2142010-01-14 10:25:07 -08001157 }
1158
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001159 @Override
1160 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001161 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001162
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001163 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001164 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001165 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001166 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001167 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001168 }
San Mehat873f2142010-01-14 10:25:07 -08001169
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001170 // 210 Tethering services started
1171 event.checkCode(TetherStatusResult);
1172 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001173 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001174
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001175 @Override
1176 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001177 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001178 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001179 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001180 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001181 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001182 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001183 List<RouteInfo> routes = new ArrayList<RouteInfo>();
1184 // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it
1185 // suitable to use as a route destination.
1186 routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface));
1187 addInterfaceToLocalNetwork(iface, routes);
San Mehat873f2142010-01-14 10:25:07 -08001188 }
1189
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001190 @Override
San Mehat873f2142010-01-14 10:25:07 -08001191 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001192 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001193 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001194 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001195 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001196 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001197 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001198 removeInterfaceFromLocalNetwork(iface);
San Mehat873f2142010-01-14 10:25:07 -08001199 }
1200
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001201 @Override
1202 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001203 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001204 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001205 return NativeDaemonEvent.filterMessageList(
1206 mConnector.executeForList("tether", "interface", "list"),
1207 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001208 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001209 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001210 }
San Mehat873f2142010-01-14 10:25:07 -08001211 }
1212
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001213 @Override
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001214 public void setDnsForwarders(Network network, String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001215 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001216
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001217 int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET;
1218 final Command cmd = new Command("tether", "dns", "set", netId);
1219
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001220 for (String s : dns) {
1221 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1222 }
1223
San Mehat873f2142010-01-14 10:25:07 -08001224 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001225 mConnector.execute(cmd);
1226 } catch (NativeDaemonConnectorException e) {
1227 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001228 }
1229 }
1230
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001231 @Override
1232 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001233 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001234 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001235 return NativeDaemonEvent.filterMessageList(
1236 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001237 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001238 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001239 }
San Mehat873f2142010-01-14 10:25:07 -08001240 }
1241
jiaguo1da35f72014-01-09 16:39:59 +08001242 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1243 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1244 for (InterfaceAddress ia : addresses) {
1245 if (!ia.getAddress().isLinkLocalAddress())
1246 filtered.add(ia);
1247 }
1248 return filtered;
1249 }
1250
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001251 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001252 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001254
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001255 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1256 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001257 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001258 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001259 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001260 // Don't touch link-local routes, as link-local addresses aren't routable,
1261 // kernel creates link-local routes on all interfaces automatically
1262 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1263 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001264 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001265 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001266 InetAddress addr = NetworkUtils.getNetworkPart(
1267 ia.getAddress(), ia.getNetworkPrefixLength());
1268 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001269 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001270 }
1271
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001272 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001273 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001274 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001275 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001276 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001277 }
1278
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001279 @Override
1280 public void enableNat(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("enable", 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 }
1288
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001289 @Override
1290 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001291 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001292 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001293 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001294 } catch (SocketException e) {
1295 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001296 }
San Mehat873f2142010-01-14 10:25:07 -08001297 }
San Mehat72759df2010-01-19 13:50:37 -08001298
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001299 @Override
1300 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001301 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001302 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001303 return NativeDaemonEvent.filterMessageList(
1304 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001305 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001306 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001307 }
San Mehat72759df2010-01-19 13:50:37 -08001308 }
1309
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001310 @Override
1311 public void attachPppd(
1312 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001313 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001314 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001315 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001316 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1317 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1318 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001319 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001320 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001321 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001322 }
1323 }
1324
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001325 @Override
1326 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001327 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001328 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001329 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001330 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001331 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001332 }
San Mehat72759df2010-01-19 13:50:37 -08001333 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001334
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001335 @Override
1336 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001337 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001338 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001339 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001340 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001341 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001342 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001343 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001344 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001345 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001346 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001347 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001348 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001349 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001350 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001351 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001352 }
1353
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001354 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001355 switch (wifiConfig.getAuthType()) {
1356 case KeyMgmt.WPA_PSK:
1357 return "wpa-psk";
1358 case KeyMgmt.WPA2_PSK:
1359 return "wpa2-psk";
1360 default:
1361 return "open";
1362 }
1363 }
1364
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001365 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001366 @Override
1367 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001368 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001369 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001370 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001371 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001372 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001373 }
1374 }
1375
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001376 @Override
1377 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001378 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001379 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001380 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001381 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001382 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001383 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001384 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001385 }
1386
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001387 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001388 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001389 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001390 try {
1391 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001392 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001393 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001394 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001395 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001396 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001397 }
1398 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001399 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001400 }
1401 }
San Mehat91cac642010-03-31 14:31:36 -07001402
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001403 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001404 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001405 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1406
1407 if (DBG) Slog.d(TAG, "Adding idletimer");
1408
1409 synchronized (mIdleTimerLock) {
1410 IdleTimerParams params = mActiveIdleTimers.get(iface);
1411 if (params != null) {
1412 // the interface already has idletimer, update network count
1413 params.networkCount++;
1414 return;
1415 }
1416
1417 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001418 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1419 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001420 } catch (NativeDaemonConnectorException e) {
1421 throw e.rethrowAsParcelableException();
1422 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001423 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1424
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001425 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001426 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1427 mNetworkActive = false;
1428 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001429 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001430 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001431 notifyInterfaceClassActivity(type,
1432 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1433 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001434 }
1435 });
Haoyu Bai04124232012-06-28 15:26:19 -07001436 }
1437 }
1438
1439 @Override
1440 public void removeIdleTimer(String iface) {
1441 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1442
1443 if (DBG) Slog.d(TAG, "Removing idletimer");
1444
1445 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001446 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001447 if (params == null || --(params.networkCount) > 0) {
1448 return;
1449 }
1450
1451 try {
1452 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001453 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001454 } catch (NativeDaemonConnectorException e) {
1455 throw e.rethrowAsParcelableException();
1456 }
1457 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001458 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001459 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001460 notifyInterfaceClassActivity(params.type,
1461 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1462 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001463 }
1464 });
Haoyu Bai04124232012-06-28 15:26:19 -07001465 }
1466 }
1467
1468 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001469 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001470 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001471 try {
1472 return mStatsFactory.readNetworkStatsSummaryDev();
1473 } catch (IOException e) {
1474 throw new IllegalStateException(e);
1475 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001476 }
1477
1478 @Override
1479 public NetworkStats getNetworkStatsSummaryXt() {
1480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001481 try {
1482 return mStatsFactory.readNetworkStatsSummaryXt();
1483 } catch (IOException e) {
1484 throw new IllegalStateException(e);
1485 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001486 }
1487
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001488 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001489 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001490 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001491 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001492 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001493 } catch (IOException e) {
1494 throw new IllegalStateException(e);
1495 }
San Mehat91cac642010-03-31 14:31:36 -07001496 }
1497
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001498 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001499 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001500 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001501
Jeff Sharkey350083e2011-06-29 10:45:16 -07001502 // silently discard when control disabled
1503 // TODO: eventually migrate to be always enabled
1504 if (!mBandwidthControlEnabled) return;
1505
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001506 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001507 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001508 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001509 }
1510
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001511 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001512 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001513 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001514 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001515 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001516 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001517 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001518 }
1519 }
1520
1521 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001522 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001523 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001524
Jeff Sharkey350083e2011-06-29 10:45:16 -07001525 // silently discard when control disabled
1526 // TODO: eventually migrate to be always enabled
1527 if (!mBandwidthControlEnabled) return;
1528
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001529 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001530 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001531 // TODO: eventually consider throwing
1532 return;
1533 }
1534
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001535 mActiveQuotas.remove(iface);
1536 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001537
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001538 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001539 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001540 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001541 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001542 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001543 }
1544 }
1545 }
1546
1547 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001548 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001549 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001550
1551 // silently discard when control disabled
1552 // TODO: eventually migrate to be always enabled
1553 if (!mBandwidthControlEnabled) return;
1554
1555 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001556 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001557 throw new IllegalStateException("setting alert requires existing quota on iface");
1558 }
1559
1560 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001561 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001562 throw new IllegalStateException("iface " + iface + " already has alert");
1563 }
1564
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001565 try {
1566 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001567 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001568 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001569 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001570 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001571 }
1572 }
1573 }
1574
1575 @Override
1576 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001577 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001578
1579 // silently discard when control disabled
1580 // TODO: eventually migrate to be always enabled
1581 if (!mBandwidthControlEnabled) return;
1582
1583 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001584 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001585 // TODO: eventually consider throwing
1586 return;
1587 }
1588
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001589 try {
1590 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001591 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001592 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001593 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001594 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001595 }
1596 }
1597 }
1598
1599 @Override
1600 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001601 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001602
1603 // silently discard when control disabled
1604 // TODO: eventually migrate to be always enabled
1605 if (!mBandwidthControlEnabled) return;
1606
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001607 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001608 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001609 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001610 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001611 }
1612 }
1613
1614 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001615 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001616 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001617
Jeff Sharkey350083e2011-06-29 10:45:16 -07001618 // silently discard when control disabled
1619 // TODO: eventually migrate to be always enabled
1620 if (!mBandwidthControlEnabled) return;
1621
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001622 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001623 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1624 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1625 // TODO: eventually consider throwing
1626 return;
1627 }
1628
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001629 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001630 mConnector.execute("bandwidth",
1631 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001632 if (rejectOnQuotaInterfaces) {
1633 mUidRejectOnQuota.put(uid, true);
1634 } else {
1635 mUidRejectOnQuota.delete(uid);
1636 }
1637 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001638 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001639 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001640 }
1641 }
1642
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001643 @Override
1644 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001645 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001646 return mBandwidthControlEnabled;
1647 }
1648
1649 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001650 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001651 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001652 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001653 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001654 } catch (IOException e) {
1655 throw new IllegalStateException(e);
1656 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001657 }
1658
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001659 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001660 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001661 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001662
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001663 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001664 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001665 final NativeDaemonEvent[] events = mConnector.executeForList(
1666 "bandwidth", "gettetherstats");
1667 for (NativeDaemonEvent event : events) {
1668 if (event.getCode() != TetheringStatsListResult) continue;
1669
1670 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1671 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1672 try {
1673 final String ifaceIn = tok.nextToken();
1674 final String ifaceOut = tok.nextToken();
1675
1676 final NetworkStats.Entry entry = new NetworkStats.Entry();
1677 entry.iface = ifaceOut;
1678 entry.uid = UID_TETHERING;
1679 entry.set = SET_DEFAULT;
1680 entry.tag = TAG_NONE;
1681 entry.rxBytes = Long.parseLong(tok.nextToken());
1682 entry.rxPackets = Long.parseLong(tok.nextToken());
1683 entry.txBytes = Long.parseLong(tok.nextToken());
1684 entry.txPackets = Long.parseLong(tok.nextToken());
1685 stats.combineValues(entry);
1686 } catch (NoSuchElementException e) {
1687 throw new IllegalStateException("problem parsing tethering stats: " + event);
1688 } catch (NumberFormatException e) {
1689 throw new IllegalStateException("problem parsing tethering stats: " + event);
1690 }
1691 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001692 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001693 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001694 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001695 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001696 }
1697
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001698 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001699 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001700 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001701
Paul Jensen13e817d2014-04-10 14:16:37 -04001702 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001703 (domains == null ? "" : domains));
1704
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001705 for (String s : servers) {
1706 InetAddress a = NetworkUtils.numericToInetAddress(s);
1707 if (a.isAnyLocalAddress() == false) {
1708 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001709 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001710 }
1711
1712 try {
1713 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001714 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001715 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001716 }
1717 }
1718
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001719 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001720 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001721 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001722 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1723 argv[0] = "users";
1724 argv[1] = "add";
1725 argv[2] = netId;
1726 int argc = 3;
1727 // Avoid overly long commands by limiting number of UID ranges per command.
1728 for (int i = 0; i < ranges.length; i++) {
1729 argv[argc++] = ranges[i].toString();
1730 if (i == (ranges.length - 1) || argc == argv.length) {
1731 try {
1732 mConnector.execute("network", Arrays.copyOf(argv, argc));
1733 } catch (NativeDaemonConnectorException e) {
1734 throw e.rethrowAsParcelableException();
1735 }
1736 argc = 3;
1737 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001738 }
1739 }
1740
1741 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001742 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001743 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001744 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1745 argv[0] = "users";
1746 argv[1] = "remove";
1747 argv[2] = netId;
1748 int argc = 3;
1749 // Avoid overly long commands by limiting number of UID ranges per command.
1750 for (int i = 0; i < ranges.length; i++) {
1751 argv[argc++] = ranges[i].toString();
1752 if (i == (ranges.length - 1) || argc == argv.length) {
1753 try {
1754 mConnector.execute("network", Arrays.copyOf(argv, argc));
1755 } catch (NativeDaemonConnectorException e) {
1756 throw e.rethrowAsParcelableException();
1757 }
1758 argc = 3;
1759 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001760 }
1761 }
1762
1763 @Override
Paul Jensenb69a3a82014-08-06 15:34:26 -04001764 public void flushNetworkDnsCache(int netId) {
1765 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1766 try {
1767 mConnector.execute("resolver", "flushnet", netId);
1768 } catch (NativeDaemonConnectorException e) {
1769 throw e.rethrowAsParcelableException();
1770 }
1771 }
1772
1773 @Override
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001774 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001775 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001776 try {
1777 mConnector.execute("firewall", enabled ? "enable" : "disable");
1778 mFirewallEnabled = enabled;
1779 } catch (NativeDaemonConnectorException e) {
1780 throw e.rethrowAsParcelableException();
1781 }
1782 }
1783
1784 @Override
1785 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001786 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001787 return mFirewallEnabled;
1788 }
1789
1790 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001791 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001792 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001793 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001794 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001795 try {
1796 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1797 } catch (NativeDaemonConnectorException e) {
1798 throw e.rethrowAsParcelableException();
1799 }
1800 }
1801
1802 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001803 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001804 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001805 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001806 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001807 try {
1808 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1809 } catch (NativeDaemonConnectorException e) {
1810 throw e.rethrowAsParcelableException();
1811 }
1812 }
1813
1814 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001815 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001816 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001817 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001818 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001819 try {
1820 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1821 } catch (NativeDaemonConnectorException e) {
1822 throw e.rethrowAsParcelableException();
1823 }
1824 }
1825
1826 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001827 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001828 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001829 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001830 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001831 try {
1832 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1833 } catch (NativeDaemonConnectorException e) {
1834 throw e.rethrowAsParcelableException();
1835 }
1836 }
1837
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001838 private static void enforceSystemUid() {
1839 final int uid = Binder.getCallingUid();
1840 if (uid != Process.SYSTEM_UID) {
1841 throw new SecurityException("Only available to AID_SYSTEM");
1842 }
1843 }
1844
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001845 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001846 public void startClatd(String interfaceName) throws IllegalStateException {
1847 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1848
1849 try {
1850 mConnector.execute("clatd", "start", interfaceName);
1851 } catch (NativeDaemonConnectorException e) {
1852 throw e.rethrowAsParcelableException();
1853 }
1854 }
1855
1856 @Override
Lorenzo Colitti95439462014-10-09 13:44:48 +09001857 public void stopClatd(String interfaceName) throws IllegalStateException {
Lorenzo Colitti79751842013-02-28 16:16:03 +09001858 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1859
1860 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +09001861 mConnector.execute("clatd", "stop", interfaceName);
Lorenzo Colitti79751842013-02-28 16:16:03 +09001862 } catch (NativeDaemonConnectorException e) {
1863 throw e.rethrowAsParcelableException();
1864 }
1865 }
1866
1867 @Override
Lorenzo Colitti95439462014-10-09 13:44:48 +09001868 public boolean isClatdStarted(String interfaceName) {
Lorenzo Colitti79751842013-02-28 16:16:03 +09001869 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1870
1871 final NativeDaemonEvent event;
1872 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +09001873 event = mConnector.execute("clatd", "status", interfaceName);
Lorenzo Colitti79751842013-02-28 16:16:03 +09001874 } catch (NativeDaemonConnectorException e) {
1875 throw e.rethrowAsParcelableException();
1876 }
1877
1878 event.checkCode(ClatdStatusResult);
1879 return event.getMessage().endsWith("started");
1880 }
1881
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001882 @Override
1883 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1884 mNetworkActivityListeners.register(listener);
1885 }
1886
1887 @Override
1888 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1889 mNetworkActivityListeners.unregister(listener);
1890 }
1891
1892 @Override
1893 public boolean isNetworkActive() {
1894 synchronized (mNetworkActivityListeners) {
1895 return mNetworkActive || mActiveIdleTimers.isEmpty();
1896 }
1897 }
1898
1899 private void reportNetworkActive() {
1900 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001901 try {
1902 for (int i = 0; i < length; i++) {
1903 try {
1904 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1905 } catch (RemoteException e) {
1906 } catch (RuntimeException e) {
1907 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001908 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001909 } finally {
1910 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001911 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001912 }
1913
Mattias Falk8b47b362011-08-23 14:15:13 +02001914 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001915 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001916 public void monitor() {
1917 if (mConnector != null) {
1918 mConnector.monitor();
1919 }
1920 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001921
1922 @Override
1923 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1924 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1925
Robert Greenwalt470fd722012-01-18 12:51:15 -08001926 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1927 mConnector.dump(fd, pw, args);
1928 pw.println();
1929
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001930 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001931 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1932 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1933 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001934
1935 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001936 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1937 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001938 }
1939
1940 synchronized (mUidRejectOnQuota) {
1941 pw.print("UID reject on quota ifaces: [");
1942 final int size = mUidRejectOnQuota.size();
1943 for (int i = 0; i < size; i++) {
1944 pw.print(mUidRejectOnQuota.keyAt(i));
1945 if (i < size - 1) pw.print(",");
1946 }
1947 pw.println("]");
1948 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001949
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001950 synchronized (mIdleTimerLock) {
1951 pw.println("Idle timers:");
1952 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1953 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1954 IdleTimerParams params = ent.getValue();
1955 pw.print(" timeout="); pw.print(params.timeout);
1956 pw.print(" type="); pw.print(params.type);
1957 pw.print(" networkCount="); pw.println(params.networkCount);
1958 }
1959 }
1960
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001961 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001962 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001963
Robert Greenwalt568891d2014-04-04 13:38:00 -07001964 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001965 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001966 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1967
1968 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001969 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001970 } catch (NativeDaemonConnectorException e) {
1971 throw e.rethrowAsParcelableException();
1972 }
1973 }
1974
Robert Greenwalt568891d2014-04-04 13:38:00 -07001975 @Override
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001976 public void createVirtualNetwork(int netId, boolean hasDNS, boolean secure) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001977 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1978
1979 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001980 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0",
1981 secure ? "1" : "0");
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001982 } catch (NativeDaemonConnectorException e) {
1983 throw e.rethrowAsParcelableException();
1984 }
1985 }
1986
1987 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001988 public void removeNetwork(int netId) {
1989 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1990
1991 try {
1992 mConnector.execute("network", "destroy", netId);
1993 } catch (NativeDaemonConnectorException e) {
1994 throw e.rethrowAsParcelableException();
1995 }
1996 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001997
1998 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001999 public void addInterfaceToNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002000 modifyInterfaceInNetwork("add", "" + netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002001 }
2002
2003 @Override
2004 public void removeInterfaceFromNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002005 modifyInterfaceInNetwork("remove", "" + netId, iface);
2006 }
Paul Jensen992f2522014-04-28 10:33:11 -04002007
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002008 private void modifyInterfaceInNetwork(String action, String netId, String iface) {
2009 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen992f2522014-04-28 10:33:11 -04002010 try {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002011 mConnector.execute("network", "interface", action, netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002012 } catch (NativeDaemonConnectorException e) {
2013 throw e.rethrowAsParcelableException();
2014 }
2015 }
2016
2017 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002018 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002019 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2020
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07002021 final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002022
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002023 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002024 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002025 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002026 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002027 if (routeInfo.hasGateway()) {
2028 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2029 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002030
2031 try {
2032 mConnector.execute(cmd);
2033 } catch (NativeDaemonConnectorException e) {
2034 throw e.rethrowAsParcelableException();
2035 }
2036 }
2037
2038 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002039 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002040 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2041
2042 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002043 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002044 } catch (NativeDaemonConnectorException e) {
2045 throw e.rethrowAsParcelableException();
2046 }
2047 }
2048
2049 @Override
2050 public void clearDefaultNetId() {
2051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2052
2053 try {
2054 mConnector.execute("network", "default", "clear");
2055 } catch (NativeDaemonConnectorException e) {
2056 throw e.rethrowAsParcelableException();
2057 }
2058 }
2059
2060 @Override
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002061 public void setPermission(String permission, int[] uids) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002062 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2063
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002064 Object[] argv = new Object[4 + MAX_UID_RANGES_PER_COMMAND];
2065 argv[0] = "permission";
2066 argv[1] = "user";
2067 argv[2] = "set";
2068 argv[3] = permission;
2069 int argc = 4;
2070 // Avoid overly long commands by limiting number of UIDs per command.
2071 for (int i = 0; i < uids.length; ++i) {
2072 argv[argc++] = uids[i];
2073 if (i == uids.length - 1 || argc == argv.length) {
2074 try {
2075 mConnector.execute("network", Arrays.copyOf(argv, argc));
2076 } catch (NativeDaemonConnectorException e) {
2077 throw e.rethrowAsParcelableException();
2078 }
2079 argc = 4;
2080 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002081 }
2082 }
2083
2084 @Override
2085 public void clearPermission(int[] uids) {
2086 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2087
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002088 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
2089 argv[0] = "permission";
2090 argv[1] = "user";
2091 argv[2] = "clear";
2092 int argc = 3;
2093 // Avoid overly long commands by limiting number of UIDs per command.
2094 for (int i = 0; i < uids.length; ++i) {
2095 argv[argc++] = uids[i];
2096 if (i == uids.length - 1 || argc == argv.length) {
2097 try {
2098 mConnector.execute("network", Arrays.copyOf(argv, argc));
2099 } catch (NativeDaemonConnectorException e) {
2100 throw e.rethrowAsParcelableException();
2101 }
2102 argc = 3;
2103 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002104 }
2105 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002106
2107 @Override
2108 public void allowProtect(int uid) {
2109 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2110
2111 try {
2112 mConnector.execute("network", "protect", "allow", uid);
2113 } catch (NativeDaemonConnectorException e) {
2114 throw e.rethrowAsParcelableException();
2115 }
2116 }
2117
2118 @Override
2119 public void denyProtect(int uid) {
2120 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2121
2122 try {
2123 mConnector.execute("network", "protect", "deny", uid);
2124 } catch (NativeDaemonConnectorException e) {
2125 throw e.rethrowAsParcelableException();
2126 }
2127 }
2128
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002129 @Override
2130 public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) {
2131 modifyInterfaceInNetwork("add", "local", iface);
2132
2133 for (RouteInfo route : routes) {
2134 if (!route.isDefaultRoute()) {
2135 modifyRoute("add", "local", route);
2136 }
2137 }
2138 }
2139
2140 @Override
2141 public void removeInterfaceFromLocalNetwork(String iface) {
2142 modifyInterfaceInNetwork("remove", "local", iface);
2143 }
San Mehat873f2142010-01-14 10:25:07 -08002144}