blob: 8c56c8c5f0574e7ad48366ca344eb2ecb52e6005 [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
Wink Savilled09c4ca2014-11-22 10:08:16 -0800242 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUBSCRIPTION_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
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900943 public void setInterfaceIpv6NdOffload(String iface, boolean enable) {
944 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
945 try {
946 mConnector.execute(
947 "interface", "ipv6ndoffload", iface, (enable ? "enable" : "disable"));
948 } catch (NativeDaemonConnectorException e) {
949 throw e.rethrowAsParcelableException();
950 }
951 }
952
953 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700954 public void addRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700955 modifyRoute("add", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700956 }
957
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800958 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700959 public void removeRoute(int netId, RouteInfo route) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700960 modifyRoute("remove", "" + netId, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700961 }
962
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -0700963 private void modifyRoute(String action, String netId, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800964 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700965
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700966 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700967
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700968 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700969 cmd.appendArg(route.getInterface());
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900970 cmd.appendArg(route.getDestination().toString());
971
972 switch (route.getType()) {
973 case RouteInfo.RTN_UNICAST:
974 if (route.hasGateway()) {
975 cmd.appendArg(route.getGateway().getHostAddress());
976 }
977 break;
978 case RouteInfo.RTN_UNREACHABLE:
979 cmd.appendArg("unreachable");
980 break;
981 case RouteInfo.RTN_THROW:
982 cmd.appendArg("throw");
983 break;
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700984 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700985
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 try {
987 mConnector.execute(cmd);
988 } catch (NativeDaemonConnectorException e) {
989 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700990 }
991 }
992
993 private ArrayList<String> readRouteList(String filename) {
994 FileInputStream fstream = null;
995 ArrayList<String> list = new ArrayList<String>();
996
997 try {
998 fstream = new FileInputStream(filename);
999 DataInputStream in = new DataInputStream(fstream);
1000 BufferedReader br = new BufferedReader(new InputStreamReader(in));
1001 String s;
1002
1003 // throw away the title line
1004
1005 while (((s = br.readLine()) != null) && (s.length() != 0)) {
1006 list.add(s);
1007 }
1008 } catch (IOException ex) {
1009 // return current list, possibly empty
1010 } finally {
1011 if (fstream != null) {
1012 try {
1013 fstream.close();
1014 } catch (IOException ex) {}
1015 }
1016 }
1017
1018 return list;
1019 }
1020
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001021 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001022 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001023 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001024 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1025
1026 // v4 routes listed as:
1027 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1028 for (String s : readRouteList("/proc/net/route")) {
1029 String[] fields = s.split("\t");
1030
1031 if (fields.length > 7) {
1032 String iface = fields[0];
1033
1034 if (interfaceName.equals(iface)) {
1035 String dest = fields[1];
1036 String gate = fields[2];
1037 String flags = fields[3]; // future use?
1038 String mask = fields[7];
1039 try {
1040 // address stored as a hex string, ex: 0014A8C0
1041 InetAddress destAddr =
1042 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1043 int prefixLength =
1044 NetworkUtils.netmaskIntToPrefixLength(
1045 (int)Long.parseLong(mask, 16));
1046 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1047
1048 // address stored as a hex string, ex 0014A8C0
1049 InetAddress gatewayAddr =
1050 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1051
Wink Saville7b5fd052013-03-15 05:07:04 +00001052 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001053 routes.add(route);
1054 } catch (Exception e) {
1055 Log.e(TAG, "Error parsing route " + s + " : " + e);
1056 continue;
1057 }
1058 }
1059 }
1060 }
1061
1062 // v6 routes listed as:
1063 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1064 for (String s : readRouteList("/proc/net/ipv6_route")) {
1065 String[]fields = s.split("\\s+");
1066 if (fields.length > 9) {
1067 String iface = fields[9].trim();
1068 if (interfaceName.equals(iface)) {
1069 String dest = fields[0];
1070 String prefix = fields[1];
1071 String gate = fields[4];
1072
1073 try {
1074 // prefix length stored as a hex string, ex 40
1075 int prefixLength = Integer.parseInt(prefix, 16);
1076
1077 // address stored as a 32 char hex string
1078 // ex fe800000000000000000000000000000
1079 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1080 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1081
1082 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1083
Wink Saville7b5fd052013-03-15 05:07:04 +00001084 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001085 routes.add(route);
1086 } catch (Exception e) {
1087 Log.e(TAG, "Error parsing route " + s + " : " + e);
1088 continue;
1089 }
1090 }
1091 }
1092 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001093 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001094 }
1095
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001096 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001097 public void setMtu(String iface, int mtu) {
1098 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1099
1100 final NativeDaemonEvent event;
1101 try {
1102 event = mConnector.execute("interface", "setmtu", iface, mtu);
1103 } catch (NativeDaemonConnectorException e) {
1104 throw e.rethrowAsParcelableException();
1105 }
1106 }
1107
1108 @Override
San Mehat873f2142010-01-14 10:25:07 -08001109 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001110 // TODO: remove from aidl if nobody calls externally
1111 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001112
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001114 }
1115
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001116 @Override
San Mehat873f2142010-01-14 10:25:07 -08001117 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001118 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001119
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001120 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001121 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001122 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001123 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001124 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001125 }
San Mehat873f2142010-01-14 10:25:07 -08001126
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001127 // 211 Forwarding enabled
1128 event.checkCode(IpFwdStatusResult);
1129 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001130 }
1131
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001132 @Override
1133 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001134 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001135 try {
Nilesh Poddarf3d4a582015-02-24 12:11:11 -08001136 mConnector.execute("ipfwd", enable ? "enable" : "disable", "tethering");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001137 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001138 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001139 }
San Mehat873f2142010-01-14 10:25:07 -08001140 }
1141
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001142 @Override
1143 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001144 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001145 // cmd is "tether start first_start first_stop second_start second_stop ..."
1146 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001147
1148 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001149 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001150 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001151 }
Kenny Roota80ce062010-06-01 13:23:53 -07001152
1153 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001154 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001155 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001156 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001157 }
San Mehat873f2142010-01-14 10:25:07 -08001158 }
1159
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001160 @Override
1161 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001163 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001164 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001165 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001166 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001167 }
San Mehat873f2142010-01-14 10:25:07 -08001168 }
1169
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001170 @Override
1171 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001172 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001173
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001174 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001175 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001176 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001177 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001178 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001179 }
San Mehat873f2142010-01-14 10:25:07 -08001180
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 // 210 Tethering services started
1182 event.checkCode(TetherStatusResult);
1183 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001184 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001185
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001186 @Override
1187 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001188 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001189 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001190 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001191 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001192 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001193 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001194 List<RouteInfo> routes = new ArrayList<RouteInfo>();
1195 // The RouteInfo constructor truncates the LinkAddress to a network prefix, thus making it
1196 // suitable to use as a route destination.
1197 routes.add(new RouteInfo(getInterfaceConfig(iface).getLinkAddress(), null, iface));
1198 addInterfaceToLocalNetwork(iface, routes);
San Mehat873f2142010-01-14 10:25:07 -08001199 }
1200
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001201 @Override
San Mehat873f2142010-01-14 10:25:07 -08001202 public void untetherInterface(String iface) {
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 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001206 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001207 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001208 }
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001209 removeInterfaceFromLocalNetwork(iface);
San Mehat873f2142010-01-14 10:25:07 -08001210 }
1211
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001212 @Override
1213 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001214 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001215 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001216 return NativeDaemonEvent.filterMessageList(
1217 mConnector.executeForList("tether", "interface", "list"),
1218 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001219 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001220 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001221 }
San Mehat873f2142010-01-14 10:25:07 -08001222 }
1223
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001224 @Override
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001225 public void setDnsForwarders(Network network, String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001226 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001227
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001228 int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET;
1229 final Command cmd = new Command("tether", "dns", "set", netId);
1230
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001231 for (String s : dns) {
1232 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1233 }
1234
San Mehat873f2142010-01-14 10:25:07 -08001235 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001236 mConnector.execute(cmd);
1237 } catch (NativeDaemonConnectorException e) {
1238 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001239 }
1240 }
1241
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001242 @Override
1243 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001244 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001245 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001246 return NativeDaemonEvent.filterMessageList(
1247 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001248 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001249 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001250 }
San Mehat873f2142010-01-14 10:25:07 -08001251 }
1252
jiaguo1da35f72014-01-09 16:39:59 +08001253 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1254 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1255 for (InterfaceAddress ia : addresses) {
1256 if (!ia.getAddress().isLinkLocalAddress())
1257 filtered.add(ia);
1258 }
1259 return filtered;
1260 }
1261
Lorenzo Colitti35e36db2015-02-26 01:25:36 +09001262 private void modifyInterfaceForward(boolean add, String fromIface, String toIface) {
1263 final Command cmd = new Command("ipfwd", add ? "add" : "remove", fromIface, toIface);
1264 try {
1265 mConnector.execute(cmd);
1266 } catch (NativeDaemonConnectorException e) {
1267 throw e.rethrowAsParcelableException();
1268 }
1269 }
1270
1271 @Override
1272 public void startInterfaceForwarding(String fromIface, String toIface) {
1273 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1274 modifyInterfaceForward(true, fromIface, toIface);
1275 }
1276
1277 @Override
1278 public void stopInterfaceForwarding(String fromIface, String toIface) {
1279 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1280 modifyInterfaceForward(false, fromIface, toIface);
1281 }
1282
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001283 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001284 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001285 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001286
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001287 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1288 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001289 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001290 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001291 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001292 // Don't touch link-local routes, as link-local addresses aren't routable,
1293 // kernel creates link-local routes on all interfaces automatically
1294 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1295 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001296 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001297 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001298 InetAddress addr = NetworkUtils.getNetworkPart(
1299 ia.getAddress(), ia.getNetworkPrefixLength());
1300 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001301 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001302 }
1303
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001304 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001305 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001306 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001307 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001308 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001309 }
1310
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001311 @Override
1312 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001313 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001314 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001315 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001316 } catch (SocketException e) {
1317 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001318 }
San Mehat873f2142010-01-14 10:25:07 -08001319 }
1320
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001321 @Override
1322 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001323 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001324 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001325 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001326 } catch (SocketException e) {
1327 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001328 }
San Mehat873f2142010-01-14 10:25:07 -08001329 }
San Mehat72759df2010-01-19 13:50:37 -08001330
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001331 @Override
1332 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001333 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001334 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001335 return NativeDaemonEvent.filterMessageList(
1336 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001337 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001338 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001339 }
San Mehat72759df2010-01-19 13:50:37 -08001340 }
1341
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001342 @Override
1343 public void attachPppd(
1344 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001345 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001346 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001347 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001348 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1349 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1350 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001351 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001352 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001353 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001354 }
1355 }
1356
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001357 @Override
1358 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001359 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001360 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001361 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001362 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001363 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001364 }
San Mehat72759df2010-01-19 13:50:37 -08001365 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001366
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001367 @Override
1368 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001369 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001370 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001371 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001372 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001373 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001374 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001375 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001376 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001377 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001378 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001379 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001380 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001381 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001382 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001383 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001384 }
1385
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001386 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001387 switch (wifiConfig.getAuthType()) {
1388 case KeyMgmt.WPA_PSK:
1389 return "wpa-psk";
1390 case KeyMgmt.WPA2_PSK:
1391 return "wpa2-psk";
1392 default:
1393 return "open";
1394 }
1395 }
1396
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001397 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001398 @Override
1399 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001400 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001401 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001402 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001403 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001404 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001405 }
1406 }
1407
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001408 @Override
1409 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001410 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001411 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001412 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001413 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001414 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001415 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001416 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001417 }
1418
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001419 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001420 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001421 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001422 try {
1423 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001424 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001425 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001426 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001427 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001428 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001429 }
1430 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001431 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001432 }
1433 }
San Mehat91cac642010-03-31 14:31:36 -07001434
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001435 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001436 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001437 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1438
1439 if (DBG) Slog.d(TAG, "Adding idletimer");
1440
1441 synchronized (mIdleTimerLock) {
1442 IdleTimerParams params = mActiveIdleTimers.get(iface);
1443 if (params != null) {
1444 // the interface already has idletimer, update network count
1445 params.networkCount++;
1446 return;
1447 }
1448
1449 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001450 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1451 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001452 } catch (NativeDaemonConnectorException e) {
1453 throw e.rethrowAsParcelableException();
1454 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001455 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1456
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001457 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001458 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1459 mNetworkActive = false;
1460 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001461 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001462 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001463 notifyInterfaceClassActivity(type,
1464 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1465 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001466 }
1467 });
Haoyu Bai04124232012-06-28 15:26:19 -07001468 }
1469 }
1470
1471 @Override
1472 public void removeIdleTimer(String iface) {
1473 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1474
1475 if (DBG) Slog.d(TAG, "Removing idletimer");
1476
1477 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001478 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001479 if (params == null || --(params.networkCount) > 0) {
1480 return;
1481 }
1482
1483 try {
1484 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001485 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001486 } catch (NativeDaemonConnectorException e) {
1487 throw e.rethrowAsParcelableException();
1488 }
1489 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001490 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001491 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001492 notifyInterfaceClassActivity(params.type,
1493 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1494 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001495 }
1496 });
Haoyu Bai04124232012-06-28 15:26:19 -07001497 }
1498 }
1499
1500 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001501 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001502 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001503 try {
1504 return mStatsFactory.readNetworkStatsSummaryDev();
1505 } catch (IOException e) {
1506 throw new IllegalStateException(e);
1507 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001508 }
1509
1510 @Override
1511 public NetworkStats getNetworkStatsSummaryXt() {
1512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001513 try {
1514 return mStatsFactory.readNetworkStatsSummaryXt();
1515 } catch (IOException e) {
1516 throw new IllegalStateException(e);
1517 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001518 }
1519
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001520 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001521 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001522 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001523 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001524 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001525 } catch (IOException e) {
1526 throw new IllegalStateException(e);
1527 }
San Mehat91cac642010-03-31 14:31:36 -07001528 }
1529
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001530 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001531 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001532 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001533
Jeff Sharkey350083e2011-06-29 10:45:16 -07001534 // silently discard when control disabled
1535 // TODO: eventually migrate to be always enabled
1536 if (!mBandwidthControlEnabled) return;
1537
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001538 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001539 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001540 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001541 }
1542
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001543 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001544 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001545 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001546 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001547 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001548 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001549 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001550 }
1551 }
1552
1553 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001554 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001555 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001556
Jeff Sharkey350083e2011-06-29 10:45:16 -07001557 // silently discard when control disabled
1558 // TODO: eventually migrate to be always enabled
1559 if (!mBandwidthControlEnabled) return;
1560
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001561 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001562 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001563 // TODO: eventually consider throwing
1564 return;
1565 }
1566
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001567 mActiveQuotas.remove(iface);
1568 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001569
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001570 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001571 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001572 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001573 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001574 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001575 }
1576 }
1577 }
1578
1579 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001580 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001581 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001582
1583 // silently discard when control disabled
1584 // TODO: eventually migrate to be always enabled
1585 if (!mBandwidthControlEnabled) return;
1586
1587 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001588 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001589 throw new IllegalStateException("setting alert requires existing quota on iface");
1590 }
1591
1592 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001593 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001594 throw new IllegalStateException("iface " + iface + " already has alert");
1595 }
1596
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001597 try {
1598 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001599 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001600 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001601 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001602 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001603 }
1604 }
1605 }
1606
1607 @Override
1608 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001609 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001610
1611 // silently discard when control disabled
1612 // TODO: eventually migrate to be always enabled
1613 if (!mBandwidthControlEnabled) return;
1614
1615 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001616 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001617 // TODO: eventually consider throwing
1618 return;
1619 }
1620
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001621 try {
1622 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001623 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001624 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001625 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001626 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001627 }
1628 }
1629 }
1630
1631 @Override
1632 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001634
1635 // silently discard when control disabled
1636 // TODO: eventually migrate to be always enabled
1637 if (!mBandwidthControlEnabled) return;
1638
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001639 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001640 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001641 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001642 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001643 }
1644 }
1645
1646 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001647 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001648 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001649
Jeff Sharkey350083e2011-06-29 10:45:16 -07001650 // silently discard when control disabled
1651 // TODO: eventually migrate to be always enabled
1652 if (!mBandwidthControlEnabled) return;
1653
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001654 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001655 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1656 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1657 // TODO: eventually consider throwing
1658 return;
1659 }
1660
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001661 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001662 mConnector.execute("bandwidth",
1663 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001664 if (rejectOnQuotaInterfaces) {
1665 mUidRejectOnQuota.put(uid, true);
1666 } else {
1667 mUidRejectOnQuota.delete(uid);
1668 }
1669 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001670 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001671 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001672 }
1673 }
1674
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001675 @Override
1676 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001677 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001678 return mBandwidthControlEnabled;
1679 }
1680
1681 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001682 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001684 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001685 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001686 } catch (IOException e) {
1687 throw new IllegalStateException(e);
1688 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001689 }
1690
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001691 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001692 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001693 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001694
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001695 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001696 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001697 final NativeDaemonEvent[] events = mConnector.executeForList(
1698 "bandwidth", "gettetherstats");
1699 for (NativeDaemonEvent event : events) {
1700 if (event.getCode() != TetheringStatsListResult) continue;
1701
1702 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1703 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1704 try {
1705 final String ifaceIn = tok.nextToken();
1706 final String ifaceOut = tok.nextToken();
1707
1708 final NetworkStats.Entry entry = new NetworkStats.Entry();
1709 entry.iface = ifaceOut;
1710 entry.uid = UID_TETHERING;
1711 entry.set = SET_DEFAULT;
1712 entry.tag = TAG_NONE;
1713 entry.rxBytes = Long.parseLong(tok.nextToken());
1714 entry.rxPackets = Long.parseLong(tok.nextToken());
1715 entry.txBytes = Long.parseLong(tok.nextToken());
1716 entry.txPackets = Long.parseLong(tok.nextToken());
1717 stats.combineValues(entry);
1718 } catch (NoSuchElementException e) {
1719 throw new IllegalStateException("problem parsing tethering stats: " + event);
1720 } catch (NumberFormatException e) {
1721 throw new IllegalStateException("problem parsing tethering stats: " + event);
1722 }
1723 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001724 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001725 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001726 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001727 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001728 }
1729
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001730 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001731 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001732 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001733
Lorenzo Colitti829dfa72014-11-28 20:07:46 +09001734 Command cmd;
1735 if (servers.length > 0) {
1736 cmd = new Command("resolver", "setnetdns", netId,
1737 (domains == null ? "" : domains));
1738 for (String s : servers) {
1739 InetAddress a = NetworkUtils.numericToInetAddress(s);
1740 if (a.isAnyLocalAddress() == false) {
1741 cmd.appendArg(a.getHostAddress());
1742 }
Mattias Falk7475c0c2011-04-04 16:10:36 +02001743 }
Lorenzo Colitti829dfa72014-11-28 20:07:46 +09001744 } else {
1745 cmd = new Command("resolver", "clearnetdns", netId);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001746 }
1747
1748 try {
1749 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001750 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001751 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001752 }
1753 }
1754
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001755 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001756 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001757 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001758 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1759 argv[0] = "users";
1760 argv[1] = "add";
1761 argv[2] = netId;
1762 int argc = 3;
1763 // Avoid overly long commands by limiting number of UID ranges per command.
1764 for (int i = 0; i < ranges.length; i++) {
1765 argv[argc++] = ranges[i].toString();
1766 if (i == (ranges.length - 1) || argc == argv.length) {
1767 try {
1768 mConnector.execute("network", Arrays.copyOf(argv, argc));
1769 } catch (NativeDaemonConnectorException e) {
1770 throw e.rethrowAsParcelableException();
1771 }
1772 argc = 3;
1773 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001774 }
1775 }
1776
1777 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001778 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001779 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001780 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1781 argv[0] = "users";
1782 argv[1] = "remove";
1783 argv[2] = netId;
1784 int argc = 3;
1785 // Avoid overly long commands by limiting number of UID ranges per command.
1786 for (int i = 0; i < ranges.length; i++) {
1787 argv[argc++] = ranges[i].toString();
1788 if (i == (ranges.length - 1) || argc == argv.length) {
1789 try {
1790 mConnector.execute("network", Arrays.copyOf(argv, argc));
1791 } catch (NativeDaemonConnectorException e) {
1792 throw e.rethrowAsParcelableException();
1793 }
1794 argc = 3;
1795 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001796 }
1797 }
1798
1799 @Override
Paul Jensenb69a3a82014-08-06 15:34:26 -04001800 public void flushNetworkDnsCache(int netId) {
1801 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1802 try {
1803 mConnector.execute("resolver", "flushnet", netId);
1804 } catch (NativeDaemonConnectorException e) {
1805 throw e.rethrowAsParcelableException();
1806 }
1807 }
1808
1809 @Override
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001810 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001811 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001812 try {
1813 mConnector.execute("firewall", enabled ? "enable" : "disable");
1814 mFirewallEnabled = enabled;
1815 } catch (NativeDaemonConnectorException e) {
1816 throw e.rethrowAsParcelableException();
1817 }
1818 }
1819
1820 @Override
1821 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001822 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001823 return mFirewallEnabled;
1824 }
1825
1826 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001827 public void setFirewallInterfaceRule(String iface, 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_interface_rule", iface, rule);
1833 } catch (NativeDaemonConnectorException e) {
1834 throw e.rethrowAsParcelableException();
1835 }
1836 }
1837
1838 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001839 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001840 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001841 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001842 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001843 try {
1844 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1845 } catch (NativeDaemonConnectorException e) {
1846 throw e.rethrowAsParcelableException();
1847 }
1848 }
1849
1850 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001851 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001852 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001853 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001854 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001855 try {
1856 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1857 } catch (NativeDaemonConnectorException e) {
1858 throw e.rethrowAsParcelableException();
1859 }
1860 }
1861
1862 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001863 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001864 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001865 Preconditions.checkState(mFirewallEnabled);
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07001866 final String rule = allow ? "allow" : "deny";
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001867 try {
1868 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1869 } catch (NativeDaemonConnectorException e) {
1870 throw e.rethrowAsParcelableException();
1871 }
1872 }
1873
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001874 private static void enforceSystemUid() {
1875 final int uid = Binder.getCallingUid();
1876 if (uid != Process.SYSTEM_UID) {
1877 throw new SecurityException("Only available to AID_SYSTEM");
1878 }
1879 }
1880
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001881 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001882 public void startClatd(String interfaceName) throws IllegalStateException {
1883 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1884
1885 try {
1886 mConnector.execute("clatd", "start", interfaceName);
1887 } catch (NativeDaemonConnectorException e) {
1888 throw e.rethrowAsParcelableException();
1889 }
1890 }
1891
1892 @Override
Lorenzo Colitti95439462014-10-09 13:44:48 +09001893 public void stopClatd(String interfaceName) throws IllegalStateException {
Lorenzo Colitti79751842013-02-28 16:16:03 +09001894 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1895
1896 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +09001897 mConnector.execute("clatd", "stop", interfaceName);
Lorenzo Colitti79751842013-02-28 16:16:03 +09001898 } catch (NativeDaemonConnectorException e) {
1899 throw e.rethrowAsParcelableException();
1900 }
1901 }
1902
1903 @Override
Lorenzo Colitti95439462014-10-09 13:44:48 +09001904 public boolean isClatdStarted(String interfaceName) {
Lorenzo Colitti79751842013-02-28 16:16:03 +09001905 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1906
1907 final NativeDaemonEvent event;
1908 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +09001909 event = mConnector.execute("clatd", "status", interfaceName);
Lorenzo Colitti79751842013-02-28 16:16:03 +09001910 } catch (NativeDaemonConnectorException e) {
1911 throw e.rethrowAsParcelableException();
1912 }
1913
1914 event.checkCode(ClatdStatusResult);
1915 return event.getMessage().endsWith("started");
1916 }
1917
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001918 @Override
1919 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1920 mNetworkActivityListeners.register(listener);
1921 }
1922
1923 @Override
1924 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1925 mNetworkActivityListeners.unregister(listener);
1926 }
1927
1928 @Override
1929 public boolean isNetworkActive() {
1930 synchronized (mNetworkActivityListeners) {
1931 return mNetworkActive || mActiveIdleTimers.isEmpty();
1932 }
1933 }
1934
1935 private void reportNetworkActive() {
1936 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001937 try {
1938 for (int i = 0; i < length; i++) {
1939 try {
1940 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1941 } catch (RemoteException e) {
1942 } catch (RuntimeException e) {
1943 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001944 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001945 } finally {
1946 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001947 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001948 }
1949
Mattias Falk8b47b362011-08-23 14:15:13 +02001950 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001951 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001952 public void monitor() {
1953 if (mConnector != null) {
1954 mConnector.monitor();
1955 }
1956 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001957
1958 @Override
1959 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1960 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1961
Robert Greenwalt470fd722012-01-18 12:51:15 -08001962 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1963 mConnector.dump(fd, pw, args);
1964 pw.println();
1965
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001966 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001967 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1968 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1969 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001970
1971 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001972 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1973 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001974 }
1975
1976 synchronized (mUidRejectOnQuota) {
1977 pw.print("UID reject on quota ifaces: [");
1978 final int size = mUidRejectOnQuota.size();
1979 for (int i = 0; i < size; i++) {
1980 pw.print(mUidRejectOnQuota.keyAt(i));
1981 if (i < size - 1) pw.print(",");
1982 }
1983 pw.println("]");
1984 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001985
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001986 synchronized (mIdleTimerLock) {
1987 pw.println("Idle timers:");
1988 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1989 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1990 IdleTimerParams params = ent.getValue();
1991 pw.print(" timeout="); pw.print(params.timeout);
1992 pw.print(" type="); pw.print(params.type);
1993 pw.print(" networkCount="); pw.println(params.networkCount);
1994 }
1995 }
1996
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001997 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001998 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001999
Robert Greenwalt568891d2014-04-04 13:38:00 -07002000 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002001 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002002 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2003
2004 try {
Paul Jensen992f2522014-04-28 10:33:11 -04002005 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002006 } catch (NativeDaemonConnectorException e) {
2007 throw e.rethrowAsParcelableException();
2008 }
2009 }
2010
Robert Greenwalt568891d2014-04-04 13:38:00 -07002011 @Override
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002012 public void createVirtualNetwork(int netId, boolean hasDNS, boolean secure) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002013 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2014
2015 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002016 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0",
2017 secure ? "1" : "0");
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002018 } catch (NativeDaemonConnectorException e) {
2019 throw e.rethrowAsParcelableException();
2020 }
2021 }
2022
2023 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002024 public void removeNetwork(int netId) {
2025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2026
2027 try {
2028 mConnector.execute("network", "destroy", netId);
2029 } catch (NativeDaemonConnectorException e) {
2030 throw e.rethrowAsParcelableException();
2031 }
2032 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002033
2034 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002035 public void addInterfaceToNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002036 modifyInterfaceInNetwork("add", "" + netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002037 }
2038
2039 @Override
2040 public void removeInterfaceFromNetwork(String iface, int netId) {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002041 modifyInterfaceInNetwork("remove", "" + netId, iface);
2042 }
Paul Jensen992f2522014-04-28 10:33:11 -04002043
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002044 private void modifyInterfaceInNetwork(String action, String netId, String iface) {
2045 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen992f2522014-04-28 10:33:11 -04002046 try {
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002047 mConnector.execute("network", "interface", action, netId, iface);
Paul Jensen992f2522014-04-28 10:33:11 -04002048 } catch (NativeDaemonConnectorException e) {
2049 throw e.rethrowAsParcelableException();
2050 }
2051 }
2052
2053 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002054 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002055 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2056
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07002057 final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002058
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002059 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002060 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002061 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002062 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002063 if (routeInfo.hasGateway()) {
2064 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2065 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002066
2067 try {
2068 mConnector.execute(cmd);
2069 } catch (NativeDaemonConnectorException e) {
2070 throw e.rethrowAsParcelableException();
2071 }
2072 }
2073
2074 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002075 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002076 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2077
2078 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002079 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002080 } catch (NativeDaemonConnectorException e) {
2081 throw e.rethrowAsParcelableException();
2082 }
2083 }
2084
2085 @Override
2086 public void clearDefaultNetId() {
2087 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2088
2089 try {
2090 mConnector.execute("network", "default", "clear");
2091 } catch (NativeDaemonConnectorException e) {
2092 throw e.rethrowAsParcelableException();
2093 }
2094 }
2095
2096 @Override
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002097 public void setPermission(String permission, int[] uids) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002098 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2099
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002100 Object[] argv = new Object[4 + MAX_UID_RANGES_PER_COMMAND];
2101 argv[0] = "permission";
2102 argv[1] = "user";
2103 argv[2] = "set";
2104 argv[3] = permission;
2105 int argc = 4;
2106 // Avoid overly long commands by limiting number of UIDs per command.
2107 for (int i = 0; i < uids.length; ++i) {
2108 argv[argc++] = uids[i];
2109 if (i == uids.length - 1 || argc == argv.length) {
2110 try {
2111 mConnector.execute("network", Arrays.copyOf(argv, argc));
2112 } catch (NativeDaemonConnectorException e) {
2113 throw e.rethrowAsParcelableException();
2114 }
2115 argc = 4;
2116 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002117 }
2118 }
2119
2120 @Override
2121 public void clearPermission(int[] uids) {
2122 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2123
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07002124 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
2125 argv[0] = "permission";
2126 argv[1] = "user";
2127 argv[2] = "clear";
2128 int argc = 3;
2129 // Avoid overly long commands by limiting number of UIDs per command.
2130 for (int i = 0; i < uids.length; ++i) {
2131 argv[argc++] = uids[i];
2132 if (i == uids.length - 1 || argc == argv.length) {
2133 try {
2134 mConnector.execute("network", Arrays.copyOf(argv, argc));
2135 } catch (NativeDaemonConnectorException e) {
2136 throw e.rethrowAsParcelableException();
2137 }
2138 argc = 3;
2139 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002140 }
2141 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002142
2143 @Override
2144 public void allowProtect(int uid) {
2145 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2146
2147 try {
2148 mConnector.execute("network", "protect", "allow", uid);
2149 } catch (NativeDaemonConnectorException e) {
2150 throw e.rethrowAsParcelableException();
2151 }
2152 }
2153
2154 @Override
2155 public void denyProtect(int uid) {
2156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2157
2158 try {
2159 mConnector.execute("network", "protect", "deny", uid);
2160 } catch (NativeDaemonConnectorException e) {
2161 throw e.rethrowAsParcelableException();
2162 }
2163 }
2164
Sreeram Ramachandrana77760d2014-07-17 17:09:07 -07002165 @Override
2166 public void addInterfaceToLocalNetwork(String iface, List<RouteInfo> routes) {
2167 modifyInterfaceInNetwork("add", "local", iface);
2168
2169 for (RouteInfo route : routes) {
2170 if (!route.isDefaultRoute()) {
2171 modifyRoute("add", "local", route);
2172 }
2173 }
2174 }
2175
2176 @Override
2177 public void removeInterfaceFromLocalNetwork(String iface) {
2178 modifyInterfaceInNetwork("remove", "local", iface);
2179 }
San Mehat873f2142010-01-14 10:25:07 -08002180}