blob: 362061e61c166191fc7aed77b4423cf0dbda7eac [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -070019import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkey4529bb62011-12-14 10:31:54 -080020import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070021import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080022import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070023import static android.net.NetworkStats.SET_DEFAULT;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080024import static android.net.NetworkStats.TAG_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.NetworkStats.TAG_NONE;
26import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070027import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090028import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070029import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080032import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070036import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080037import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070038import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070039
San Mehat873f2142010-01-14 10:25:07 -080040import android.content.Context;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080041import android.net.ConnectivityManager;
San Mehat4d02d002010-01-22 16:07:46 -080042import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070045import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080046import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070047import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080048import android.net.wifi.WifiConfiguration;
49import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070050import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070052import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080053import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080054import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080055import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070056import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080057import android.os.RemoteCallbackList;
58import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070059import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070060import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080061import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070062import android.telephony.DataConnectionRealTimeInfo;
63import android.telephony.PhoneStateListener;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080064import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080065import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070066import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080067
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070068import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070069import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070070import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080071import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070072import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070073import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070074import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070075
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070076import java.io.BufferedReader;
77import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080078import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070079import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070080import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070081import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070082import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070083import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070084import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070085import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070086import java.net.InterfaceAddress;
87import java.net.NetworkInterface;
88import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070089import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070090import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080091import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070092import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070093import java.util.NoSuchElementException;
94import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070095import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080096
97/**
98 * @hide
99 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700100public class NetworkManagementService extends INetworkManagementService.Stub
101 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700102 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700103 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700104 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900105 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700106
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800107 private static final String ADD = "add";
108 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700109
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700110 private static final String ALLOW = "allow";
111 private static final String DENY = "deny";
112
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700113 private static final String DEFAULT = "default";
114 private static final String SECONDARY = "secondary";
115
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700116 /**
117 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
118 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
119 */
120 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
121
San Mehat873f2142010-01-14 10:25:07 -0800122 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700123 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800124 public static final int InterfaceListResult = 110;
125 public static final int TetherInterfaceListResult = 111;
126 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800127 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700128 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800129
130 public static final int TetherStatusResult = 210;
131 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800132 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800133 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700134 public static final int InterfaceRxCounterResult = 216;
135 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700136 public static final int QuotaCounterResult = 220;
137 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800138 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900139 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700140 public static final int GetMarkResult = 225;
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;
San Mehat873f2142010-01-14 10:25:07 -0800147 }
148
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700149 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
150
San Mehat873f2142010-01-14 10:25:07 -0800151 /**
152 * Binder context for this service
153 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700154 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800155
156 /**
157 * connector object for communicating with netd
158 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700159 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800160
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700161 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700162 private final Handler mDaemonHandler;
163 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700164
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800165 private IBatteryStats mBatteryStats;
166
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700167 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700168 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700169
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800170 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
171 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800172
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700173 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
174
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700175 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700176 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700177 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700178 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700179 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700180 /** Set of UIDs with active reject rules. */
181 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
182
Haoyu Bai04124232012-06-28 15:26:19 -0700183 private Object mIdleTimerLock = new Object();
184 /** Set of interfaces with active idle timers. */
185 private static class IdleTimerParams {
186 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800187 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700188 public int networkCount;
189
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800190 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700191 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800192 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700193 this.networkCount = 1;
194 }
195 }
196 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
197
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700198 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700199 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700200
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700201 private boolean mMobileActivityFromRadio = false;
202 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
203
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800204 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
205 new RemoteCallbackList<INetworkActivityListener>();
206 private boolean mNetworkActive;
207
San Mehat873f2142010-01-14 10:25:07 -0800208 /**
209 * Constructs a new NetworkManagementService instance
210 *
211 * @param context Binder context for this service
212 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900213 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800214 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800215
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700216 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
217 mFgHandler = new Handler(FgThread.get().getLooper());
218
Marco Nelissen62dbb222010-02-18 10:56:30 -0800219 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700220 mConnector = null;
221 mThread = null;
222 mDaemonHandler = null;
223 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800224 return;
225 }
226
Dianne Hackborn4590e522014-03-24 13:36:46 -0700227 // Don't need this wake lock, since we now have a time stamp for when
228 // the network actually went inactive. (It might be nice to still do this,
229 // but I don't want to do it through the power manager because that pollutes the
230 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700231 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700232 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800233
San Mehat873f2142010-01-14 10:25:07 -0800234 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700235 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
236 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700237 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700238
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700239 mDaemonHandler = new Handler(FgThread.get().getLooper());
240 mPhoneStateListener = new PhoneStateListener(mDaemonHandler.getLooper()) {
241 public void onDataConnectionRealTimeInfoChanged(
242 DataConnectionRealTimeInfo dcRtInfo) {
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700243 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
244 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700245 }
246 };
247
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700248 // Add ourself to the Watchdog monitors.
249 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700250 }
251
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900252 static NetworkManagementService create(Context context,
253 String socket) throws InterruptedException {
254 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700255 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700256 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
257 service.mThread.start();
258 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700259 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700260 if (DBG) Slog.d(TAG, "Connected");
261 return service;
San Mehat873f2142010-01-14 10:25:07 -0800262 }
263
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900264 public static NetworkManagementService create(Context context) throws InterruptedException {
265 return create(context, NETD_SOCKET_NAME);
266 }
267
Jeff Sharkey350083e2011-06-29 10:45:16 -0700268 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700269 prepareNativeDaemon();
270 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700271 }
272
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800273 private IBatteryStats getBatteryStats() {
274 synchronized (this) {
275 if (mBatteryStats != null) {
276 return mBatteryStats;
277 }
278 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
279 BatteryStats.SERVICE_NAME));
280 return mBatteryStats;
281 }
282 }
283
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800284 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800287 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800288 }
289
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800290 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800291 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800292 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800293 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800294 }
295
296 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700297 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800298 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700299 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700301 try {
302 for (int i = 0; i < length; i++) {
303 try {
304 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
305 } catch (RemoteException e) {
306 } catch (RuntimeException e) {
307 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700308 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700309 } finally {
310 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700311 }
312 }
313
314 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700315 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700316 * (typically, an Ethernet cable has been plugged-in or unplugged).
317 */
318 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800319 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700320 try {
321 for (int i = 0; i < length; i++) {
322 try {
323 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
324 } catch (RemoteException e) {
325 } catch (RuntimeException e) {
326 }
San Mehat4d02d002010-01-22 16:07:46 -0800327 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700328 } finally {
329 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800330 }
331 }
332
333 /**
334 * Notify our observers of an interface addition.
335 */
336 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800337 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700338 try {
339 for (int i = 0; i < length; i++) {
340 try {
341 mObservers.getBroadcastItem(i).interfaceAdded(iface);
342 } catch (RemoteException e) {
343 } catch (RuntimeException e) {
344 }
San Mehat4d02d002010-01-22 16:07:46 -0800345 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700346 } finally {
347 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800348 }
349 }
350
351 /**
352 * Notify our observers of an interface removal.
353 */
354 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700355 // netd already clears out quota and alerts for removed ifaces; update
356 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700357 mActiveAlerts.remove(iface);
358 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700359
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800360 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700361 try {
362 for (int i = 0; i < length; i++) {
363 try {
364 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
365 } catch (RemoteException e) {
366 } catch (RuntimeException e) {
367 }
San Mehat4d02d002010-01-22 16:07:46 -0800368 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700369 } finally {
370 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800371 }
372 }
373
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700374 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700375 * Notify our observers of a limit reached.
376 */
377 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800378 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700379 try {
380 for (int i = 0; i < length; i++) {
381 try {
382 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
383 } catch (RemoteException e) {
384 } catch (RuntimeException e) {
385 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700386 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700387 } finally {
388 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700389 }
390 }
391
392 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700393 * Notify our observers of a change in the data activity state of the interface
394 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700395 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
396 boolean fromRadio) {
397 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
398 if (isMobile) {
399 if (!fromRadio) {
400 if (mMobileActivityFromRadio) {
401 // If this call is not coming from a report from the radio itself, but we
402 // have previously received reports from the radio, then we will take the
403 // power state to just be whatever the radio last reported.
404 powerState = mLastPowerStateFromRadio;
405 }
406 } else {
407 mMobileActivityFromRadio = true;
408 }
409 if (mLastPowerStateFromRadio != powerState) {
410 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700411 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700413 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700414 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700415 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700416 }
417
418 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
419 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
420
421 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
422 // Report the change in data activity. We don't do this if this is a change
423 // on the mobile network, that is not coming from the radio itself, and we
424 // have previously seen change reports from the radio. In that case only
425 // the radio is the authority for the current state.
426 final int length = mObservers.beginBroadcast();
427 try {
428 for (int i = 0; i < length; i++) {
429 try {
430 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
431 Integer.toString(type), isActive, tsNanos);
432 } catch (RemoteException e) {
433 } catch (RuntimeException e) {
434 }
435 }
436 } finally {
437 mObservers.finishBroadcast();
438 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700439 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800440
441 boolean report = false;
442 synchronized (mIdleTimerLock) {
443 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700444 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800445 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700446 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800447 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700448 if (mNetworkActive != isActive) {
449 mNetworkActive = isActive;
450 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800451 }
452 }
453 if (report) {
454 reportNetworkActive();
455 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700456 }
457
458 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700459 * Prepare native daemon once connected, enabling modules and pushing any
460 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700461 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700462 private void prepareNativeDaemon() {
463 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700464
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700465 // only enable bandwidth control when support exists
466 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
467 if (hasKernelSupport) {
468 Slog.d(TAG, "enabling bandwidth control");
469 try {
470 mConnector.execute("bandwidth", "enable");
471 mBandwidthControlEnabled = true;
472 } catch (NativeDaemonConnectorException e) {
473 Log.wtf(TAG, "problem enabling bandwidth controls", e);
474 }
475 } else {
476 Slog.d(TAG, "not enabling bandwidth control");
477 }
478
479 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
480
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700481 if (mBandwidthControlEnabled) {
482 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800483 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700484 } catch (RemoteException e) {
485 }
486 }
487
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700488 // push any existing quota or UID rules
489 synchronized (mQuotaLock) {
490 int size = mActiveQuotas.size();
491 if (size > 0) {
492 Slog.d(TAG, "pushing " + size + " active quota rules");
493 final HashMap<String, Long> activeQuotas = mActiveQuotas;
494 mActiveQuotas = Maps.newHashMap();
495 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
496 setInterfaceQuota(entry.getKey(), entry.getValue());
497 }
498 }
499
500 size = mActiveAlerts.size();
501 if (size > 0) {
502 Slog.d(TAG, "pushing " + size + " active alert rules");
503 final HashMap<String, Long> activeAlerts = mActiveAlerts;
504 mActiveAlerts = Maps.newHashMap();
505 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
506 setInterfaceAlert(entry.getKey(), entry.getValue());
507 }
508 }
509
510 size = mUidRejectOnQuota.size();
511 if (size > 0) {
512 Slog.d(TAG, "pushing " + size + " active uid rules");
513 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
514 mUidRejectOnQuota = new SparseBooleanArray();
515 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
516 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
517 }
518 }
519 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700520
521 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700522 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700523 }
San Mehat4d02d002010-01-22 16:07:46 -0800524
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900525 /**
526 * Notify our observers of a new or updated interface address.
527 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900528 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900529 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700530 try {
531 for (int i = 0; i < length; i++) {
532 try {
533 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
534 } catch (RemoteException e) {
535 } catch (RuntimeException e) {
536 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900537 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700538 } finally {
539 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900540 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900541 }
542
543 /**
544 * Notify our observers of a deleted interface address.
545 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900546 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900547 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700548 try {
549 for (int i = 0; i < length; i++) {
550 try {
551 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
552 } catch (RemoteException e) {
553 } catch (RuntimeException e) {
554 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900555 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700556 } finally {
557 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900558 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900559 }
560
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900561 /**
562 * Notify our observers of DNS server information received.
563 */
564 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
565 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700566 try {
567 for (int i = 0; i < length; i++) {
568 try {
569 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
570 addresses);
571 } catch (RemoteException e) {
572 } catch (RuntimeException e) {
573 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900574 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700575 } finally {
576 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900577 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900578 }
579
San Mehat873f2142010-01-14 10:25:07 -0800580 //
581 // Netd Callback handling
582 //
583
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700584 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
585 @Override
San Mehat873f2142010-01-14 10:25:07 -0800586 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700587 // event is dispatched from internal NDC thread, so we prepare the
588 // daemon back on main thread.
589 if (mConnectedSignal != null) {
590 mConnectedSignal.countDown();
591 mConnectedSignal = null;
592 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700593 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700594 @Override
595 public void run() {
596 prepareNativeDaemon();
597 }
598 });
599 }
San Mehat873f2142010-01-14 10:25:07 -0800600 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700601
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700602 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800603 public boolean onCheckHoldWakeLock(int code) {
604 return code == NetdResponseCode.InterfaceClassActivity;
605 }
606
607 @Override
San Mehat873f2142010-01-14 10:25:07 -0800608 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900609 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700610 switch (code) {
611 case NetdResponseCode.InterfaceChange:
612 /*
613 * a network interface change occured
614 * Format: "NNN Iface added <name>"
615 * "NNN Iface removed <name>"
616 * "NNN Iface changed <name> <up/down>"
617 * "NNN Iface linkstatus <name> <up/down>"
618 */
619 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900620 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700621 }
622 if (cooked[2].equals("added")) {
623 notifyInterfaceAdded(cooked[3]);
624 return true;
625 } else if (cooked[2].equals("removed")) {
626 notifyInterfaceRemoved(cooked[3]);
627 return true;
628 } else if (cooked[2].equals("changed") && cooked.length == 5) {
629 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
630 return true;
631 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
632 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
633 return true;
634 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900635 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700636 // break;
637 case NetdResponseCode.BandwidthControl:
638 /*
639 * Bandwidth control needs some attention
640 * Format: "NNN limit alert <alertName> <ifaceName>"
641 */
642 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900643 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700644 }
645 if (cooked[2].equals("alert")) {
646 notifyLimitReached(cooked[3], cooked[4]);
647 return true;
648 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900649 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700650 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700651 case NetdResponseCode.InterfaceClassActivity:
652 /*
653 * An network interface class state changed (active/idle)
654 * Format: "NNN IfaceClass <active/idle> <label>"
655 */
656 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900657 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700658 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700659 long timestampNanos = 0;
660 if (cooked.length == 5) {
661 try {
662 timestampNanos = Long.parseLong(cooked[4]);
663 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700664 } else {
665 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700666 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700667 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700668 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700669 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
670 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700671 return true;
672 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900673 case NetdResponseCode.InterfaceAddressChange:
674 /*
675 * A network address change occurred
676 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
677 * "NNN Address removed <addr> <iface> <flags> <scope>"
678 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900679 if (cooked.length < 7 || !cooked[1].equals("Address")) {
680 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900681 }
682
Lorenzo Colitti64483942013-11-15 18:43:52 +0900683 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900684 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900685 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900686 int flags = Integer.parseInt(cooked[5]);
687 int scope = Integer.parseInt(cooked[6]);
688 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900689 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
690 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900691 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900692 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900693 }
694
695 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900696 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900697 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900698 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900699 }
700 return true;
701 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900702 case NetdResponseCode.InterfaceDnsServerInfo:
703 /*
704 * Information about available DNS servers has been received.
705 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
706 */
707 long lifetime; // Actually a 32-bit unsigned integer.
708
709 if (cooked.length == 6 &&
710 cooked[1].equals("DnsInfo") &&
711 cooked[2].equals("servers")) {
712 try {
713 lifetime = Long.parseLong(cooked[4]);
714 } catch (NumberFormatException e) {
715 throw new IllegalStateException(errorMessage);
716 }
717 String[] servers = cooked[5].split(",");
718 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
719 }
720 return true;
721 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700722 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800723 }
724 return false;
San Mehat873f2142010-01-14 10:25:07 -0800725 }
726 }
727
San Mehated4fc8a2010-01-22 12:28:36 -0800728
San Mehat873f2142010-01-14 10:25:07 -0800729 //
730 // INetworkManagementService members
731 //
732
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800733 @Override
734 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800735 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700736 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800737 return NativeDaemonEvent.filterMessageList(
738 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700739 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800740 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700741 }
San Mehated4fc8a2010-01-22 12:28:36 -0800742 }
743
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800744 @Override
745 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800746 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800747
748 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700749 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800750 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700751 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800752 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700753 }
San Mehated4fc8a2010-01-22 12:28:36 -0800754
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800755 event.checkCode(InterfaceGetCfgResult);
756
757 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
758 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800759
Kenny Roota80ce062010-06-01 13:23:53 -0700760 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800761 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700762 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800763 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800764 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800765 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700766 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800767 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800768 } catch (IllegalArgumentException iae) {
769 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700770 }
771
772 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800773 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800774 } catch (NumberFormatException nfe) {
775 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700776 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800777
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800778 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
779 while (st.hasMoreTokens()) {
780 cfg.setFlag(st.nextToken());
781 }
Kenny Roota80ce062010-06-01 13:23:53 -0700782 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800783 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800784 }
San Mehated4fc8a2010-01-22 12:28:36 -0800785 return cfg;
786 }
787
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800788 @Override
789 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800790 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800791 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800792 if (linkAddr == null || linkAddr.getAddress() == null) {
793 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800794 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800795
796 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800797 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900798 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799 for (String flag : cfg.getFlags()) {
800 cmd.appendArg(flag);
801 }
802
Kenny Roota80ce062010-06-01 13:23:53 -0700803 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800804 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700805 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800806 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700807 }
San Mehat873f2142010-01-14 10:25:07 -0800808 }
809
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800810 @Override
811 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800812 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800813 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800814 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800815 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700816 }
817
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800818 @Override
819 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800820 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800821 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800822 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800823 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700824 }
825
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800826 @Override
827 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800828 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700829 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800830 mConnector.execute(
831 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700832 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800833 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700834 }
835 }
836
Irfan Sherifff5600612011-06-16 10:26:28 -0700837 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
838 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800839 @Override
840 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800841 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700842 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800843 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700844 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800845 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700846 }
847 }
848
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800849 @Override
850 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800851 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700852 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800853 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700854 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800855 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700856 }
857 }
858
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800859 @Override
860 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800861 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700862 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800863 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700864 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800865 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700866 }
867 }
868
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800869 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700870 public void addRoute(int netId, RouteInfo route) {
871 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700872 }
873
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800874 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700875 public void removeRoute(int netId, RouteInfo route) {
876 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700877 }
878
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700879 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800880 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700881
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700882 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700883
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700884 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700885 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700886 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900887 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700888 if (route.hasGateway()) {
889 cmd.appendArg(route.getGateway().getHostAddress());
890 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700891
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800892 try {
893 mConnector.execute(cmd);
894 } catch (NativeDaemonConnectorException e) {
895 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700896 }
897 }
898
899 private ArrayList<String> readRouteList(String filename) {
900 FileInputStream fstream = null;
901 ArrayList<String> list = new ArrayList<String>();
902
903 try {
904 fstream = new FileInputStream(filename);
905 DataInputStream in = new DataInputStream(fstream);
906 BufferedReader br = new BufferedReader(new InputStreamReader(in));
907 String s;
908
909 // throw away the title line
910
911 while (((s = br.readLine()) != null) && (s.length() != 0)) {
912 list.add(s);
913 }
914 } catch (IOException ex) {
915 // return current list, possibly empty
916 } finally {
917 if (fstream != null) {
918 try {
919 fstream.close();
920 } catch (IOException ex) {}
921 }
922 }
923
924 return list;
925 }
926
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800927 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700928 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700930 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
931
932 // v4 routes listed as:
933 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
934 for (String s : readRouteList("/proc/net/route")) {
935 String[] fields = s.split("\t");
936
937 if (fields.length > 7) {
938 String iface = fields[0];
939
940 if (interfaceName.equals(iface)) {
941 String dest = fields[1];
942 String gate = fields[2];
943 String flags = fields[3]; // future use?
944 String mask = fields[7];
945 try {
946 // address stored as a hex string, ex: 0014A8C0
947 InetAddress destAddr =
948 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
949 int prefixLength =
950 NetworkUtils.netmaskIntToPrefixLength(
951 (int)Long.parseLong(mask, 16));
952 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
953
954 // address stored as a hex string, ex 0014A8C0
955 InetAddress gatewayAddr =
956 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
957
Wink Saville7b5fd052013-03-15 05:07:04 +0000958 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700959 routes.add(route);
960 } catch (Exception e) {
961 Log.e(TAG, "Error parsing route " + s + " : " + e);
962 continue;
963 }
964 }
965 }
966 }
967
968 // v6 routes listed as:
969 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
970 for (String s : readRouteList("/proc/net/ipv6_route")) {
971 String[]fields = s.split("\\s+");
972 if (fields.length > 9) {
973 String iface = fields[9].trim();
974 if (interfaceName.equals(iface)) {
975 String dest = fields[0];
976 String prefix = fields[1];
977 String gate = fields[4];
978
979 try {
980 // prefix length stored as a hex string, ex 40
981 int prefixLength = Integer.parseInt(prefix, 16);
982
983 // address stored as a 32 char hex string
984 // ex fe800000000000000000000000000000
985 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
986 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
987
988 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
989
Wink Saville7b5fd052013-03-15 05:07:04 +0000990 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700991 routes.add(route);
992 } catch (Exception e) {
993 Log.e(TAG, "Error parsing route " + s + " : " + e);
994 continue;
995 }
996 }
997 }
998 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800999 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001000 }
1001
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001002 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001003 public void setMtu(String iface, int mtu) {
1004 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1005
1006 final NativeDaemonEvent event;
1007 try {
1008 event = mConnector.execute("interface", "setmtu", iface, mtu);
1009 } catch (NativeDaemonConnectorException e) {
1010 throw e.rethrowAsParcelableException();
1011 }
1012 }
1013
1014 @Override
San Mehat873f2142010-01-14 10:25:07 -08001015 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001016 // TODO: remove from aidl if nobody calls externally
1017 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001018
Joe Onorato8a9b2202010-02-26 18:56:32 -08001019 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001020 }
1021
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001022 @Override
San Mehat873f2142010-01-14 10:25:07 -08001023 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001024 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001025
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001026 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001027 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001028 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001029 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001030 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001031 }
San Mehat873f2142010-01-14 10:25:07 -08001032
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001033 // 211 Forwarding enabled
1034 event.checkCode(IpFwdStatusResult);
1035 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001036 }
1037
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001038 @Override
1039 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001040 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001041 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001042 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001043 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001044 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001045 }
San Mehat873f2142010-01-14 10:25:07 -08001046 }
1047
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001048 @Override
1049 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001050 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001051 // cmd is "tether start first_start first_stop second_start second_stop ..."
1052 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053
1054 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001055 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001056 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001057 }
Kenny Roota80ce062010-06-01 13:23:53 -07001058
1059 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001060 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001061 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001062 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001063 }
San Mehat873f2142010-01-14 10:25:07 -08001064 }
1065
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001066 @Override
1067 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001068 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001069 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001070 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001071 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001072 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001073 }
San Mehat873f2142010-01-14 10:25:07 -08001074 }
1075
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001076 @Override
1077 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001078 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001079
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001080 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001081 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001082 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001083 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001084 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001085 }
San Mehat873f2142010-01-14 10:25:07 -08001086
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001087 // 210 Tethering services started
1088 event.checkCode(TetherStatusResult);
1089 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001090 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001091
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001092 @Override
1093 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001094 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001095 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001096 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001097 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001098 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001099 }
San Mehat873f2142010-01-14 10:25:07 -08001100 }
1101
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001102 @Override
San Mehat873f2142010-01-14 10:25:07 -08001103 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001104 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001105 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001107 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001108 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001109 }
San Mehat873f2142010-01-14 10:25:07 -08001110 }
1111
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001112 @Override
1113 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001115 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001116 return NativeDaemonEvent.filterMessageList(
1117 mConnector.executeForList("tether", "interface", "list"),
1118 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001119 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001120 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001121 }
San Mehat873f2142010-01-14 10:25:07 -08001122 }
1123
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001124 @Override
1125 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001126 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001127
1128 final Command cmd = new Command("tether", "dns", "set");
1129 for (String s : dns) {
1130 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1131 }
1132
San Mehat873f2142010-01-14 10:25:07 -08001133 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001134 mConnector.execute(cmd);
1135 } catch (NativeDaemonConnectorException e) {
1136 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001137 }
1138 }
1139
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001140 @Override
1141 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001142 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001143 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001144 return NativeDaemonEvent.filterMessageList(
1145 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001146 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001147 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001148 }
San Mehat873f2142010-01-14 10:25:07 -08001149 }
1150
jiaguo1da35f72014-01-09 16:39:59 +08001151 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1152 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1153 for (InterfaceAddress ia : addresses) {
1154 if (!ia.getAddress().isLinkLocalAddress())
1155 filtered.add(ia);
1156 }
1157 return filtered;
1158 }
1159
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001160 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001161 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001162 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001163
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001164 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1165 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001166 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001167 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001168 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001169 // Don't touch link-local routes, as link-local addresses aren't routable,
1170 // kernel creates link-local routes on all interfaces automatically
1171 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1172 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001173 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001174 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001175 InetAddress addr = NetworkUtils.getNetworkPart(
1176 ia.getAddress(), ia.getNetworkPrefixLength());
1177 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001178 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001179 }
1180
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001181 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001182 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001183 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001184 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001185 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001186 }
1187
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001188 @Override
1189 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001190 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001191 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001192 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001193 } catch (SocketException e) {
1194 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001195 }
San Mehat873f2142010-01-14 10:25:07 -08001196 }
1197
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001198 @Override
1199 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001200 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001201 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001202 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001203 } catch (SocketException e) {
1204 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001205 }
San Mehat873f2142010-01-14 10:25:07 -08001206 }
San Mehat72759df2010-01-19 13:50:37 -08001207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001208 @Override
1209 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001210 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001211 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 return NativeDaemonEvent.filterMessageList(
1213 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001214 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001215 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001216 }
San Mehat72759df2010-01-19 13:50:37 -08001217 }
1218
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001219 @Override
1220 public void attachPppd(
1221 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001223 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001224 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001225 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1226 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1227 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001228 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001229 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001230 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001231 }
1232 }
1233
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001234 @Override
1235 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001236 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001237 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001239 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001240 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001241 }
San Mehat72759df2010-01-19 13:50:37 -08001242 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001243
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001244 @Override
1245 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001246 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001247 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001248 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001249 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001250 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001251 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001252 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001253 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001254 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001255 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001256 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001257 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001258 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001259 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001260 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001261 }
1262
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001263 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001264 switch (wifiConfig.getAuthType()) {
1265 case KeyMgmt.WPA_PSK:
1266 return "wpa-psk";
1267 case KeyMgmt.WPA2_PSK:
1268 return "wpa2-psk";
1269 default:
1270 return "open";
1271 }
1272 }
1273
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001274 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001275 @Override
1276 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001277 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001278 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001279 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001280 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001281 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001282 }
1283 }
1284
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001285 @Override
1286 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001287 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001288 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001289 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001290 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001291 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001292 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001293 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001294 }
1295
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001296 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001297 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001298 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001299 try {
1300 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001301 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001302 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001303 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001304 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001305 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001306 }
1307 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001308 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001309 }
1310 }
San Mehat91cac642010-03-31 14:31:36 -07001311
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001312 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001313 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001314 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1315
1316 if (DBG) Slog.d(TAG, "Adding idletimer");
1317
1318 synchronized (mIdleTimerLock) {
1319 IdleTimerParams params = mActiveIdleTimers.get(iface);
1320 if (params != null) {
1321 // the interface already has idletimer, update network count
1322 params.networkCount++;
1323 return;
1324 }
1325
1326 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001327 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1328 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001329 } catch (NativeDaemonConnectorException e) {
1330 throw e.rethrowAsParcelableException();
1331 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001332 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1333
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001334 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001335 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1336 mNetworkActive = false;
1337 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001338 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001339 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001340 notifyInterfaceClassActivity(type,
1341 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1342 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001343 }
1344 });
Haoyu Bai04124232012-06-28 15:26:19 -07001345 }
1346 }
1347
1348 @Override
1349 public void removeIdleTimer(String iface) {
1350 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1351
1352 if (DBG) Slog.d(TAG, "Removing idletimer");
1353
1354 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001355 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001356 if (params == null || --(params.networkCount) > 0) {
1357 return;
1358 }
1359
1360 try {
1361 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001362 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001363 } catch (NativeDaemonConnectorException e) {
1364 throw e.rethrowAsParcelableException();
1365 }
1366 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001367 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001368 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001369 notifyInterfaceClassActivity(params.type,
1370 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1371 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001372 }
1373 });
Haoyu Bai04124232012-06-28 15:26:19 -07001374 }
1375 }
1376
1377 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001378 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001379 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001380 try {
1381 return mStatsFactory.readNetworkStatsSummaryDev();
1382 } catch (IOException e) {
1383 throw new IllegalStateException(e);
1384 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001385 }
1386
1387 @Override
1388 public NetworkStats getNetworkStatsSummaryXt() {
1389 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001390 try {
1391 return mStatsFactory.readNetworkStatsSummaryXt();
1392 } catch (IOException e) {
1393 throw new IllegalStateException(e);
1394 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001395 }
1396
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001397 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001398 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001399 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001400 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001401 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001402 } catch (IOException e) {
1403 throw new IllegalStateException(e);
1404 }
San Mehat91cac642010-03-31 14:31:36 -07001405 }
1406
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001407 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001408 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001409 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001410
Jeff Sharkey350083e2011-06-29 10:45:16 -07001411 // silently discard when control disabled
1412 // TODO: eventually migrate to be always enabled
1413 if (!mBandwidthControlEnabled) return;
1414
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001415 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001416 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001417 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001418 }
1419
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001420 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001421 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001422 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001423 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001424 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001425 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001426 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001427 }
1428 }
1429
1430 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001431 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001432 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001433
Jeff Sharkey350083e2011-06-29 10:45:16 -07001434 // silently discard when control disabled
1435 // TODO: eventually migrate to be always enabled
1436 if (!mBandwidthControlEnabled) return;
1437
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001438 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001439 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001440 // TODO: eventually consider throwing
1441 return;
1442 }
1443
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001444 mActiveQuotas.remove(iface);
1445 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001446
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001447 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001448 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001449 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001450 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001451 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001452 }
1453 }
1454 }
1455
1456 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001457 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001458 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001459
1460 // silently discard when control disabled
1461 // TODO: eventually migrate to be always enabled
1462 if (!mBandwidthControlEnabled) return;
1463
1464 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001465 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001466 throw new IllegalStateException("setting alert requires existing quota on iface");
1467 }
1468
1469 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001470 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001471 throw new IllegalStateException("iface " + iface + " already has alert");
1472 }
1473
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001474 try {
1475 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001476 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001477 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001478 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001479 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001480 }
1481 }
1482 }
1483
1484 @Override
1485 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001486 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001487
1488 // silently discard when control disabled
1489 // TODO: eventually migrate to be always enabled
1490 if (!mBandwidthControlEnabled) return;
1491
1492 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001493 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001494 // TODO: eventually consider throwing
1495 return;
1496 }
1497
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001498 try {
1499 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001500 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001501 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001502 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001503 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001504 }
1505 }
1506 }
1507
1508 @Override
1509 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001510 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001511
1512 // silently discard when control disabled
1513 // TODO: eventually migrate to be always enabled
1514 if (!mBandwidthControlEnabled) return;
1515
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001516 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001517 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001518 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001519 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001520 }
1521 }
1522
1523 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001524 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001525 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001526
Jeff Sharkey350083e2011-06-29 10:45:16 -07001527 // silently discard when control disabled
1528 // TODO: eventually migrate to be always enabled
1529 if (!mBandwidthControlEnabled) return;
1530
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001531 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001532 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1533 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1534 // TODO: eventually consider throwing
1535 return;
1536 }
1537
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001538 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001539 mConnector.execute("bandwidth",
1540 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001541 if (rejectOnQuotaInterfaces) {
1542 mUidRejectOnQuota.put(uid, true);
1543 } else {
1544 mUidRejectOnQuota.delete(uid);
1545 }
1546 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001547 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001548 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001549 }
1550 }
1551
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001552 @Override
1553 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001554 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001555 return mBandwidthControlEnabled;
1556 }
1557
1558 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001559 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001560 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001561 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001562 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001563 } catch (IOException e) {
1564 throw new IllegalStateException(e);
1565 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001566 }
1567
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001568 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001569 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001570 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001571
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001572 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001573 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001574 final NativeDaemonEvent[] events = mConnector.executeForList(
1575 "bandwidth", "gettetherstats");
1576 for (NativeDaemonEvent event : events) {
1577 if (event.getCode() != TetheringStatsListResult) continue;
1578
1579 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1580 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1581 try {
1582 final String ifaceIn = tok.nextToken();
1583 final String ifaceOut = tok.nextToken();
1584
1585 final NetworkStats.Entry entry = new NetworkStats.Entry();
1586 entry.iface = ifaceOut;
1587 entry.uid = UID_TETHERING;
1588 entry.set = SET_DEFAULT;
1589 entry.tag = TAG_NONE;
1590 entry.rxBytes = Long.parseLong(tok.nextToken());
1591 entry.rxPackets = Long.parseLong(tok.nextToken());
1592 entry.txBytes = Long.parseLong(tok.nextToken());
1593 entry.txPackets = Long.parseLong(tok.nextToken());
1594 stats.combineValues(entry);
1595 } catch (NoSuchElementException e) {
1596 throw new IllegalStateException("problem parsing tethering stats: " + event);
1597 } catch (NumberFormatException e) {
1598 throw new IllegalStateException("problem parsing tethering stats: " + event);
1599 }
1600 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001601 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001602 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001603 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001604 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001605 }
1606
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001607 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001608 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001609 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001610
Paul Jensen13e817d2014-04-10 14:16:37 -04001611 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001612 (domains == null ? "" : domains));
1613
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001614 for (String s : servers) {
1615 InetAddress a = NetworkUtils.numericToInetAddress(s);
1616 if (a.isAnyLocalAddress() == false) {
1617 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001618 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001619 }
1620
1621 try {
1622 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001623 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001624 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001625 }
1626 }
1627
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001628 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001629 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001630 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1631 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001632 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001633 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001634 } catch (NativeDaemonConnectorException e) {
1635 throw e.rethrowAsParcelableException();
1636 }
1637 }
1638
1639 @Override
1640 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1641 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1642 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001643 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001644 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001645 } catch (NativeDaemonConnectorException e) {
1646 throw e.rethrowAsParcelableException();
1647 }
1648 }
1649
1650 @Override
1651 public void setMarkedForwarding(String iface) {
1652 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1653 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001654 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001655 } catch (NativeDaemonConnectorException e) {
1656 throw e.rethrowAsParcelableException();
1657 }
1658 }
1659
1660 @Override
1661 public void clearMarkedForwarding(String iface) {
1662 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1663 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001664 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1665 } catch (NativeDaemonConnectorException e) {
1666 throw e.rethrowAsParcelableException();
1667 }
1668 }
1669
1670 @Override
1671 public int getMarkForUid(int uid) {
1672 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1673 final NativeDaemonEvent event;
1674 try {
1675 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1676 } catch (NativeDaemonConnectorException e) {
1677 throw e.rethrowAsParcelableException();
1678 }
1679 event.checkCode(GetMarkResult);
1680 return Integer.parseInt(event.getMessage());
1681 }
1682
1683 @Override
1684 public int getMarkForProtect() {
1685 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1686 final NativeDaemonEvent event;
1687 try {
1688 event = mConnector.execute("interface", "fwmark", "get", "protect");
1689 } catch (NativeDaemonConnectorException e) {
1690 throw e.rethrowAsParcelableException();
1691 }
1692 event.checkCode(GetMarkResult);
1693 return Integer.parseInt(event.getMessage());
1694 }
1695
1696 @Override
1697 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1698 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1699 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001700 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001701 mConnector.execute("interface", "fwmark", "route", "add", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001702 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubakercca54c42013-06-27 17:41:38 -07001703 } catch (NativeDaemonConnectorException e) {
1704 throw e.rethrowAsParcelableException();
1705 }
1706 }
1707
1708 @Override
1709 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1710 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1711 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001712 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001713 mConnector.execute("interface", "fwmark", "route", "remove", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001714 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001715 } catch (NativeDaemonConnectorException e) {
1716 throw e.rethrowAsParcelableException();
1717 }
1718 }
1719
1720 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001721 public void setHostExemption(LinkAddress host) {
1722 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1723 try {
1724 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1725 } catch (NativeDaemonConnectorException e) {
1726 throw e.rethrowAsParcelableException();
1727 }
1728 }
1729
1730 @Override
1731 public void clearHostExemption(LinkAddress host) {
1732 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1733 try {
1734 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1735 } catch (NativeDaemonConnectorException e) {
1736 throw e.rethrowAsParcelableException();
1737 }
1738 }
1739
1740 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001741 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001742 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1743 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001744 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001745 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001746 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001747 }
1748 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001749
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001750 @Override
1751 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001752 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001753 try {
1754 mConnector.execute("firewall", enabled ? "enable" : "disable");
1755 mFirewallEnabled = enabled;
1756 } catch (NativeDaemonConnectorException e) {
1757 throw e.rethrowAsParcelableException();
1758 }
1759 }
1760
1761 @Override
1762 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001763 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001764 return mFirewallEnabled;
1765 }
1766
1767 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001768 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001769 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001770 Preconditions.checkState(mFirewallEnabled);
1771 final String rule = allow ? ALLOW : DENY;
1772 try {
1773 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1774 } catch (NativeDaemonConnectorException e) {
1775 throw e.rethrowAsParcelableException();
1776 }
1777 }
1778
1779 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001780 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001781 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001782 Preconditions.checkState(mFirewallEnabled);
1783 final String rule = allow ? ALLOW : DENY;
1784 try {
1785 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1786 } catch (NativeDaemonConnectorException e) {
1787 throw e.rethrowAsParcelableException();
1788 }
1789 }
1790
1791 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001792 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001793 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001794 Preconditions.checkState(mFirewallEnabled);
1795 final String rule = allow ? ALLOW : DENY;
1796 try {
1797 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1798 } catch (NativeDaemonConnectorException e) {
1799 throw e.rethrowAsParcelableException();
1800 }
1801 }
1802
1803 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001804 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001805 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001806 Preconditions.checkState(mFirewallEnabled);
1807 final String rule = allow ? ALLOW : DENY;
1808 try {
1809 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1810 } catch (NativeDaemonConnectorException e) {
1811 throw e.rethrowAsParcelableException();
1812 }
1813 }
1814
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001815 private static void enforceSystemUid() {
1816 final int uid = Binder.getCallingUid();
1817 if (uid != Process.SYSTEM_UID) {
1818 throw new SecurityException("Only available to AID_SYSTEM");
1819 }
1820 }
1821
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001822 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001823 public void startClatd(String interfaceName) throws IllegalStateException {
1824 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1825
1826 try {
1827 mConnector.execute("clatd", "start", interfaceName);
1828 } catch (NativeDaemonConnectorException e) {
1829 throw e.rethrowAsParcelableException();
1830 }
1831 }
1832
1833 @Override
1834 public void stopClatd() throws IllegalStateException {
1835 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1836
1837 try {
1838 mConnector.execute("clatd", "stop");
1839 } catch (NativeDaemonConnectorException e) {
1840 throw e.rethrowAsParcelableException();
1841 }
1842 }
1843
1844 @Override
1845 public boolean isClatdStarted() {
1846 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1847
1848 final NativeDaemonEvent event;
1849 try {
1850 event = mConnector.execute("clatd", "status");
1851 } catch (NativeDaemonConnectorException e) {
1852 throw e.rethrowAsParcelableException();
1853 }
1854
1855 event.checkCode(ClatdStatusResult);
1856 return event.getMessage().endsWith("started");
1857 }
1858
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001859 @Override
1860 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1861 mNetworkActivityListeners.register(listener);
1862 }
1863
1864 @Override
1865 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1866 mNetworkActivityListeners.unregister(listener);
1867 }
1868
1869 @Override
1870 public boolean isNetworkActive() {
1871 synchronized (mNetworkActivityListeners) {
1872 return mNetworkActive || mActiveIdleTimers.isEmpty();
1873 }
1874 }
1875
1876 private void reportNetworkActive() {
1877 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001878 try {
1879 for (int i = 0; i < length; i++) {
1880 try {
1881 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1882 } catch (RemoteException e) {
1883 } catch (RuntimeException e) {
1884 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001885 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001886 } finally {
1887 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001888 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001889 }
1890
Mattias Falk8b47b362011-08-23 14:15:13 +02001891 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001892 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001893 public void monitor() {
1894 if (mConnector != null) {
1895 mConnector.monitor();
1896 }
1897 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001898
1899 @Override
1900 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1901 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1902
Robert Greenwalt470fd722012-01-18 12:51:15 -08001903 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1904 mConnector.dump(fd, pw, args);
1905 pw.println();
1906
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001907 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001908 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1909 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1910 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001911
1912 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001913 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1914 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001915 }
1916
1917 synchronized (mUidRejectOnQuota) {
1918 pw.print("UID reject on quota ifaces: [");
1919 final int size = mUidRejectOnQuota.size();
1920 for (int i = 0; i < size; i++) {
1921 pw.print(mUidRejectOnQuota.keyAt(i));
1922 if (i < size - 1) pw.print(",");
1923 }
1924 pw.println("]");
1925 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001926
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001927 synchronized (mIdleTimerLock) {
1928 pw.println("Idle timers:");
1929 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1930 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1931 IdleTimerParams params = ent.getValue();
1932 pw.print(" timeout="); pw.print(params.timeout);
1933 pw.print(" type="); pw.print(params.type);
1934 pw.print(" networkCount="); pw.println(params.networkCount);
1935 }
1936 }
1937
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001938 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001939 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001940
Robert Greenwalt568891d2014-04-04 13:38:00 -07001941 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001942 public void createNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001943 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1944
1945 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001946 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001947 } catch (NativeDaemonConnectorException e) {
1948 throw e.rethrowAsParcelableException();
1949 }
1950 }
1951
Robert Greenwalt568891d2014-04-04 13:38:00 -07001952 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001953 public void removeNetwork(int netId) {
1954 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1955
1956 try {
1957 mConnector.execute("network", "destroy", netId);
1958 } catch (NativeDaemonConnectorException e) {
1959 throw e.rethrowAsParcelableException();
1960 }
1961 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001962
1963 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001964 public void addInterfaceToNetwork(String iface, int netId) {
1965 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1966
1967 try {
1968 mConnector.execute("network", "addiface", netId, iface);
1969 } catch (NativeDaemonConnectorException e) {
1970 throw e.rethrowAsParcelableException();
1971 }
1972 }
1973
1974 @Override
1975 public void removeInterfaceFromNetwork(String iface, int netId) {
1976 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1977
1978 try {
1979 mConnector.execute("network", "removeiface", netId, iface);
1980 } catch (NativeDaemonConnectorException e) {
1981 throw e.rethrowAsParcelableException();
1982 }
1983 }
1984
1985 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001986 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1987 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001988 }
1989
1990 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001991 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1992 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001993 }
1994
Robert Greenwalt913c8952014-04-07 17:36:35 -07001995 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001996 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1997
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07001998 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001999
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002000 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002001 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002002 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002003 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002004 if (routeInfo.hasGateway()) {
2005 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2006 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002007
2008 try {
2009 mConnector.execute(cmd);
2010 } catch (NativeDaemonConnectorException e) {
2011 throw e.rethrowAsParcelableException();
2012 }
2013 }
2014
2015 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002016 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002017 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2018
2019 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002020 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002021 } catch (NativeDaemonConnectorException e) {
2022 throw e.rethrowAsParcelableException();
2023 }
2024 }
2025
2026 @Override
2027 public void clearDefaultNetId() {
2028 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2029
2030 try {
2031 mConnector.execute("network", "default", "clear");
2032 } catch (NativeDaemonConnectorException e) {
2033 throw e.rethrowAsParcelableException();
2034 }
2035 }
2036
2037 @Override
2038 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2039 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2040
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002041 final Command cmd = new Command("network", "permission", "user", "set");
2042 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2043 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002044 for (int i=0; i<uids.length; i++) {
2045 cmd.appendArg(uids[i]);
2046 }
2047
2048 try {
2049 mConnector.execute(cmd);
2050 } catch (NativeDaemonConnectorException e) {
2051 throw e.rethrowAsParcelableException();
2052 }
2053 }
2054
2055 @Override
2056 public void clearPermission(int[] uids) {
2057 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2058
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002059 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002060 for (int i=0; i<uids.length; i++) {
2061 cmd.appendArg(uids[i]);
2062 }
2063
2064 try {
2065 mConnector.execute(cmd);
2066 } catch (NativeDaemonConnectorException e) {
2067 throw e.rethrowAsParcelableException();
2068 }
2069 }
San Mehat873f2142010-01-14 10:25:07 -08002070}