blob: d26f3fce816b83d55dae6d1bb3acfb0cb7cae194 [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;
Wink Savillefb40dd42014-06-12 17:02:31 -070064import android.telephony.SubscriptionManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080065import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080066import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070067import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080068
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070069import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070070import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070071import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080072import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070073import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070074import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070075import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070076
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070077import java.io.BufferedReader;
78import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080079import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070080import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070081import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070082import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070084import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070085import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070086import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070087import java.net.InterfaceAddress;
88import java.net.NetworkInterface;
89import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070090import java.util.ArrayList;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070091import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080092import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070093import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070094import java.util.NoSuchElementException;
95import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070096import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080097
98/**
99 * @hide
100 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700101public class NetworkManagementService extends INetworkManagementService.Stub
102 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700103 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700104 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700105 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900106 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700107
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800108 private static final String ADD = "add";
109 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700110
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700111 private static final String ALLOW = "allow";
112 private static final String DENY = "deny";
113
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700114 private static final String DEFAULT = "default";
115 private static final String SECONDARY = "secondary";
116
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700117 /**
118 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
119 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
120 */
121 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
122
San Mehat873f2142010-01-14 10:25:07 -0800123 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700124 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800125 public static final int InterfaceListResult = 110;
126 public static final int TetherInterfaceListResult = 111;
127 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800128 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700129 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800130
131 public static final int TetherStatusResult = 210;
132 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800133 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800134 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700135 public static final int InterfaceRxCounterResult = 216;
136 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700137 public static final int QuotaCounterResult = 220;
138 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800139 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900140 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700141 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800142
143 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700144 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700145 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900146 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900147 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800148 }
149
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700150 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
151
San Mehat873f2142010-01-14 10:25:07 -0800152 /**
153 * Binder context for this service
154 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700155 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800156
157 /**
158 * connector object for communicating with netd
159 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700160 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800161
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700162 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700163 private final Handler mDaemonHandler;
164 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700165
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800166 private IBatteryStats mBatteryStats;
167
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700168 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700169 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700170
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800171 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
172 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800173
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700174 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
175
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700176 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700177 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700178 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700179 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700180 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700181 /** Set of UIDs with active reject rules. */
182 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
183
Haoyu Bai04124232012-06-28 15:26:19 -0700184 private Object mIdleTimerLock = new Object();
185 /** Set of interfaces with active idle timers. */
186 private static class IdleTimerParams {
187 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800188 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700189 public int networkCount;
190
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800191 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700192 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800193 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700194 this.networkCount = 1;
195 }
196 }
197 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
198
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700199 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700200 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700201
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700202 private boolean mMobileActivityFromRadio = false;
203 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
204
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800205 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
206 new RemoteCallbackList<INetworkActivityListener>();
207 private boolean mNetworkActive;
208
San Mehat873f2142010-01-14 10:25:07 -0800209 /**
210 * Constructs a new NetworkManagementService instance
211 *
212 * @param context Binder context for this service
213 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900214 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800215 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800216
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700217 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
218 mFgHandler = new Handler(FgThread.get().getLooper());
219
Marco Nelissen62dbb222010-02-18 10:56:30 -0800220 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700221 mConnector = null;
222 mThread = null;
223 mDaemonHandler = null;
224 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800225 return;
226 }
227
Dianne Hackborn4590e522014-03-24 13:36:46 -0700228 // Don't need this wake lock, since we now have a time stamp for when
229 // the network actually went inactive. (It might be nice to still do this,
230 // but I don't want to do it through the power manager because that pollutes the
231 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700232 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700233 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800234
San Mehat873f2142010-01-14 10:25:07 -0800235 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700236 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
237 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700238 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700239
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700240 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Savillefb40dd42014-06-12 17:02:31 -0700241 mPhoneStateListener = new PhoneStateListener(
242 SubscriptionManager.DEFAULT_SUB_ID, // FIXME: What Subscription should be used??
243 mDaemonHandler.getLooper()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700244 public void onDataConnectionRealTimeInfoChanged(
245 DataConnectionRealTimeInfo dcRtInfo) {
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700246 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
247 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700248 }
249 };
250
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700251 // Add ourself to the Watchdog monitors.
252 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700253 }
254
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900255 static NetworkManagementService create(Context context,
256 String socket) throws InterruptedException {
257 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700258 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700259 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
260 service.mThread.start();
261 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700262 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700263 if (DBG) Slog.d(TAG, "Connected");
264 return service;
San Mehat873f2142010-01-14 10:25:07 -0800265 }
266
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900267 public static NetworkManagementService create(Context context) throws InterruptedException {
268 return create(context, NETD_SOCKET_NAME);
269 }
270
Jeff Sharkey350083e2011-06-29 10:45:16 -0700271 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700272 prepareNativeDaemon();
273 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700274 }
275
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800276 private IBatteryStats getBatteryStats() {
277 synchronized (this) {
278 if (mBatteryStats != null) {
279 return mBatteryStats;
280 }
281 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
282 BatteryStats.SERVICE_NAME));
283 return mBatteryStats;
284 }
285 }
286
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800287 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800288 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800289 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800291 }
292
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800293 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800294 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800295 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800296 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800297 }
298
299 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700300 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800301 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700302 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800303 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700304 try {
305 for (int i = 0; i < length; i++) {
306 try {
307 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
308 } catch (RemoteException e) {
309 } catch (RuntimeException e) {
310 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700311 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700312 } finally {
313 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700314 }
315 }
316
317 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700318 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700319 * (typically, an Ethernet cable has been plugged-in or unplugged).
320 */
321 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800322 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700323 try {
324 for (int i = 0; i < length; i++) {
325 try {
326 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
327 } catch (RemoteException e) {
328 } catch (RuntimeException e) {
329 }
San Mehat4d02d002010-01-22 16:07:46 -0800330 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700331 } finally {
332 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800333 }
334 }
335
336 /**
337 * Notify our observers of an interface addition.
338 */
339 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800340 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700341 try {
342 for (int i = 0; i < length; i++) {
343 try {
344 mObservers.getBroadcastItem(i).interfaceAdded(iface);
345 } catch (RemoteException e) {
346 } catch (RuntimeException e) {
347 }
San Mehat4d02d002010-01-22 16:07:46 -0800348 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700349 } finally {
350 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800351 }
352 }
353
354 /**
355 * Notify our observers of an interface removal.
356 */
357 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700358 // netd already clears out quota and alerts for removed ifaces; update
359 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700360 mActiveAlerts.remove(iface);
361 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700362
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800363 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700364 try {
365 for (int i = 0; i < length; i++) {
366 try {
367 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
368 } catch (RemoteException e) {
369 } catch (RuntimeException e) {
370 }
San Mehat4d02d002010-01-22 16:07:46 -0800371 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700372 } finally {
373 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800374 }
375 }
376
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700377 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700378 * Notify our observers of a limit reached.
379 */
380 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800381 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700382 try {
383 for (int i = 0; i < length; i++) {
384 try {
385 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
386 } catch (RemoteException e) {
387 } catch (RuntimeException e) {
388 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700389 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700390 } finally {
391 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700392 }
393 }
394
395 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700396 * Notify our observers of a change in the data activity state of the interface
397 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700398 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
399 boolean fromRadio) {
400 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
401 if (isMobile) {
402 if (!fromRadio) {
403 if (mMobileActivityFromRadio) {
404 // If this call is not coming from a report from the radio itself, but we
405 // have previously received reports from the radio, then we will take the
406 // power state to just be whatever the radio last reported.
407 powerState = mLastPowerStateFromRadio;
408 }
409 } else {
410 mMobileActivityFromRadio = true;
411 }
412 if (mLastPowerStateFromRadio != powerState) {
413 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700414 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700415 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700416 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700417 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700418 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700419 }
420
421 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
422 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
423
424 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
425 // Report the change in data activity. We don't do this if this is a change
426 // on the mobile network, that is not coming from the radio itself, and we
427 // have previously seen change reports from the radio. In that case only
428 // the radio is the authority for the current state.
429 final int length = mObservers.beginBroadcast();
430 try {
431 for (int i = 0; i < length; i++) {
432 try {
433 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
434 Integer.toString(type), isActive, tsNanos);
435 } catch (RemoteException e) {
436 } catch (RuntimeException e) {
437 }
438 }
439 } finally {
440 mObservers.finishBroadcast();
441 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700442 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800443
444 boolean report = false;
445 synchronized (mIdleTimerLock) {
446 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700447 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800448 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700449 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800450 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700451 if (mNetworkActive != isActive) {
452 mNetworkActive = isActive;
453 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800454 }
455 }
456 if (report) {
457 reportNetworkActive();
458 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700459 }
460
461 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700462 * Prepare native daemon once connected, enabling modules and pushing any
463 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700464 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700465 private void prepareNativeDaemon() {
466 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700467
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700468 // only enable bandwidth control when support exists
469 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
470 if (hasKernelSupport) {
471 Slog.d(TAG, "enabling bandwidth control");
472 try {
473 mConnector.execute("bandwidth", "enable");
474 mBandwidthControlEnabled = true;
475 } catch (NativeDaemonConnectorException e) {
476 Log.wtf(TAG, "problem enabling bandwidth controls", e);
477 }
478 } else {
479 Slog.d(TAG, "not enabling bandwidth control");
480 }
481
482 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
483
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700484 if (mBandwidthControlEnabled) {
485 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800486 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700487 } catch (RemoteException e) {
488 }
489 }
490
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700491 // push any existing quota or UID rules
492 synchronized (mQuotaLock) {
493 int size = mActiveQuotas.size();
494 if (size > 0) {
495 Slog.d(TAG, "pushing " + size + " active quota rules");
496 final HashMap<String, Long> activeQuotas = mActiveQuotas;
497 mActiveQuotas = Maps.newHashMap();
498 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
499 setInterfaceQuota(entry.getKey(), entry.getValue());
500 }
501 }
502
503 size = mActiveAlerts.size();
504 if (size > 0) {
505 Slog.d(TAG, "pushing " + size + " active alert rules");
506 final HashMap<String, Long> activeAlerts = mActiveAlerts;
507 mActiveAlerts = Maps.newHashMap();
508 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
509 setInterfaceAlert(entry.getKey(), entry.getValue());
510 }
511 }
512
513 size = mUidRejectOnQuota.size();
514 if (size > 0) {
515 Slog.d(TAG, "pushing " + size + " active uid rules");
516 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
517 mUidRejectOnQuota = new SparseBooleanArray();
518 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
519 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
520 }
521 }
522 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700523
524 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700525 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700526 }
San Mehat4d02d002010-01-22 16:07:46 -0800527
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900528 /**
529 * Notify our observers of a new or updated interface address.
530 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900531 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900532 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700533 try {
534 for (int i = 0; i < length; i++) {
535 try {
536 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
537 } catch (RemoteException e) {
538 } catch (RuntimeException e) {
539 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900540 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700541 } finally {
542 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900543 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900544 }
545
546 /**
547 * Notify our observers of a deleted interface address.
548 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900549 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900550 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700551 try {
552 for (int i = 0; i < length; i++) {
553 try {
554 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
555 } catch (RemoteException e) {
556 } catch (RuntimeException e) {
557 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900558 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700559 } finally {
560 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900561 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900562 }
563
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900564 /**
565 * Notify our observers of DNS server information received.
566 */
567 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
568 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700569 try {
570 for (int i = 0; i < length; i++) {
571 try {
572 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
573 addresses);
574 } catch (RemoteException e) {
575 } catch (RuntimeException e) {
576 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900577 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700578 } finally {
579 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900580 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900581 }
582
San Mehat873f2142010-01-14 10:25:07 -0800583 //
584 // Netd Callback handling
585 //
586
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700587 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
588 @Override
San Mehat873f2142010-01-14 10:25:07 -0800589 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700590 // event is dispatched from internal NDC thread, so we prepare the
591 // daemon back on main thread.
592 if (mConnectedSignal != null) {
593 mConnectedSignal.countDown();
594 mConnectedSignal = null;
595 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700596 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700597 @Override
598 public void run() {
599 prepareNativeDaemon();
600 }
601 });
602 }
San Mehat873f2142010-01-14 10:25:07 -0800603 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700604
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700605 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800606 public boolean onCheckHoldWakeLock(int code) {
607 return code == NetdResponseCode.InterfaceClassActivity;
608 }
609
610 @Override
San Mehat873f2142010-01-14 10:25:07 -0800611 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900612 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700613 switch (code) {
614 case NetdResponseCode.InterfaceChange:
615 /*
616 * a network interface change occured
617 * Format: "NNN Iface added <name>"
618 * "NNN Iface removed <name>"
619 * "NNN Iface changed <name> <up/down>"
620 * "NNN Iface linkstatus <name> <up/down>"
621 */
622 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900623 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700624 }
625 if (cooked[2].equals("added")) {
626 notifyInterfaceAdded(cooked[3]);
627 return true;
628 } else if (cooked[2].equals("removed")) {
629 notifyInterfaceRemoved(cooked[3]);
630 return true;
631 } else if (cooked[2].equals("changed") && cooked.length == 5) {
632 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
633 return true;
634 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
635 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
636 return true;
637 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900638 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700639 // break;
640 case NetdResponseCode.BandwidthControl:
641 /*
642 * Bandwidth control needs some attention
643 * Format: "NNN limit alert <alertName> <ifaceName>"
644 */
645 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900646 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700647 }
648 if (cooked[2].equals("alert")) {
649 notifyLimitReached(cooked[3], cooked[4]);
650 return true;
651 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900652 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700653 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700654 case NetdResponseCode.InterfaceClassActivity:
655 /*
656 * An network interface class state changed (active/idle)
657 * Format: "NNN IfaceClass <active/idle> <label>"
658 */
659 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900660 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700661 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700662 long timestampNanos = 0;
663 if (cooked.length == 5) {
664 try {
665 timestampNanos = Long.parseLong(cooked[4]);
666 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700667 } else {
668 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700669 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700670 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700671 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700672 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
673 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700674 return true;
675 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900676 case NetdResponseCode.InterfaceAddressChange:
677 /*
678 * A network address change occurred
679 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
680 * "NNN Address removed <addr> <iface> <flags> <scope>"
681 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900682 if (cooked.length < 7 || !cooked[1].equals("Address")) {
683 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900684 }
685
Lorenzo Colitti64483942013-11-15 18:43:52 +0900686 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900687 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900688 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900689 int flags = Integer.parseInt(cooked[5]);
690 int scope = Integer.parseInt(cooked[6]);
691 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900692 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
693 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900694 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900695 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900696 }
697
698 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900699 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900700 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900701 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900702 }
703 return true;
704 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900705 case NetdResponseCode.InterfaceDnsServerInfo:
706 /*
707 * Information about available DNS servers has been received.
708 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
709 */
710 long lifetime; // Actually a 32-bit unsigned integer.
711
712 if (cooked.length == 6 &&
713 cooked[1].equals("DnsInfo") &&
714 cooked[2].equals("servers")) {
715 try {
716 lifetime = Long.parseLong(cooked[4]);
717 } catch (NumberFormatException e) {
718 throw new IllegalStateException(errorMessage);
719 }
720 String[] servers = cooked[5].split(",");
721 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
722 }
723 return true;
724 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700725 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800726 }
727 return false;
San Mehat873f2142010-01-14 10:25:07 -0800728 }
729 }
730
San Mehated4fc8a2010-01-22 12:28:36 -0800731
San Mehat873f2142010-01-14 10:25:07 -0800732 //
733 // INetworkManagementService members
734 //
735
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800736 @Override
737 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800738 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700739 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800740 return NativeDaemonEvent.filterMessageList(
741 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700742 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800743 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700744 }
San Mehated4fc8a2010-01-22 12:28:36 -0800745 }
746
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800747 @Override
748 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800749 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800750
751 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700752 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800753 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700754 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800755 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700756 }
San Mehated4fc8a2010-01-22 12:28:36 -0800757
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800758 event.checkCode(InterfaceGetCfgResult);
759
760 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
761 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800762
Kenny Roota80ce062010-06-01 13:23:53 -0700763 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800764 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700765 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800766 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800767 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800768 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700769 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800770 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800771 } catch (IllegalArgumentException iae) {
772 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700773 }
774
775 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800776 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800777 } catch (NumberFormatException nfe) {
778 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700779 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800780
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800781 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
782 while (st.hasMoreTokens()) {
783 cfg.setFlag(st.nextToken());
784 }
Kenny Roota80ce062010-06-01 13:23:53 -0700785 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800786 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800787 }
San Mehated4fc8a2010-01-22 12:28:36 -0800788 return cfg;
789 }
790
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800791 @Override
792 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800793 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800794 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800795 if (linkAddr == null || linkAddr.getAddress() == null) {
796 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800797 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800798
799 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800800 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900801 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800802 for (String flag : cfg.getFlags()) {
803 cmd.appendArg(flag);
804 }
805
Kenny Roota80ce062010-06-01 13:23:53 -0700806 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800807 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700808 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800809 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700810 }
San Mehat873f2142010-01-14 10:25:07 -0800811 }
812
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800813 @Override
814 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800815 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800816 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800817 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800818 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700819 }
820
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800821 @Override
822 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800823 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800824 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800825 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800826 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700827 }
828
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800829 @Override
830 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800831 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700832 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800833 mConnector.execute(
834 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700835 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800836 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700837 }
838 }
839
Irfan Sherifff5600612011-06-16 10:26:28 -0700840 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
841 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800842 @Override
843 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800844 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700845 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800846 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700847 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800848 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700849 }
850 }
851
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800852 @Override
853 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800854 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700855 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800856 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700857 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800858 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700859 }
860 }
861
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800862 @Override
863 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800864 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700865 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800866 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700867 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800868 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700869 }
870 }
871
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800872 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700873 public void addRoute(int netId, RouteInfo route) {
874 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700875 }
876
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800877 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700878 public void removeRoute(int netId, RouteInfo route) {
879 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700880 }
881
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700882 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800883 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700884
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700885 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700886
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700887 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700888 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700889 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900890 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700891 if (route.hasGateway()) {
892 cmd.appendArg(route.getGateway().getHostAddress());
893 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700894
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800895 try {
896 mConnector.execute(cmd);
897 } catch (NativeDaemonConnectorException e) {
898 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700899 }
900 }
901
902 private ArrayList<String> readRouteList(String filename) {
903 FileInputStream fstream = null;
904 ArrayList<String> list = new ArrayList<String>();
905
906 try {
907 fstream = new FileInputStream(filename);
908 DataInputStream in = new DataInputStream(fstream);
909 BufferedReader br = new BufferedReader(new InputStreamReader(in));
910 String s;
911
912 // throw away the title line
913
914 while (((s = br.readLine()) != null) && (s.length() != 0)) {
915 list.add(s);
916 }
917 } catch (IOException ex) {
918 // return current list, possibly empty
919 } finally {
920 if (fstream != null) {
921 try {
922 fstream.close();
923 } catch (IOException ex) {}
924 }
925 }
926
927 return list;
928 }
929
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800930 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700931 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800932 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700933 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
934
935 // v4 routes listed as:
936 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
937 for (String s : readRouteList("/proc/net/route")) {
938 String[] fields = s.split("\t");
939
940 if (fields.length > 7) {
941 String iface = fields[0];
942
943 if (interfaceName.equals(iface)) {
944 String dest = fields[1];
945 String gate = fields[2];
946 String flags = fields[3]; // future use?
947 String mask = fields[7];
948 try {
949 // address stored as a hex string, ex: 0014A8C0
950 InetAddress destAddr =
951 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
952 int prefixLength =
953 NetworkUtils.netmaskIntToPrefixLength(
954 (int)Long.parseLong(mask, 16));
955 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
956
957 // address stored as a hex string, ex 0014A8C0
958 InetAddress gatewayAddr =
959 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
960
Wink Saville7b5fd052013-03-15 05:07:04 +0000961 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700962 routes.add(route);
963 } catch (Exception e) {
964 Log.e(TAG, "Error parsing route " + s + " : " + e);
965 continue;
966 }
967 }
968 }
969 }
970
971 // v6 routes listed as:
972 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
973 for (String s : readRouteList("/proc/net/ipv6_route")) {
974 String[]fields = s.split("\\s+");
975 if (fields.length > 9) {
976 String iface = fields[9].trim();
977 if (interfaceName.equals(iface)) {
978 String dest = fields[0];
979 String prefix = fields[1];
980 String gate = fields[4];
981
982 try {
983 // prefix length stored as a hex string, ex 40
984 int prefixLength = Integer.parseInt(prefix, 16);
985
986 // address stored as a 32 char hex string
987 // ex fe800000000000000000000000000000
988 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
989 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
990
991 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
992
Wink Saville7b5fd052013-03-15 05:07:04 +0000993 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700994 routes.add(route);
995 } catch (Exception e) {
996 Log.e(TAG, "Error parsing route " + s + " : " + e);
997 continue;
998 }
999 }
1000 }
1001 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001002 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001003 }
1004
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001005 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001006 public void setMtu(String iface, int mtu) {
1007 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1008
1009 final NativeDaemonEvent event;
1010 try {
1011 event = mConnector.execute("interface", "setmtu", iface, mtu);
1012 } catch (NativeDaemonConnectorException e) {
1013 throw e.rethrowAsParcelableException();
1014 }
1015 }
1016
1017 @Override
San Mehat873f2142010-01-14 10:25:07 -08001018 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001019 // TODO: remove from aidl if nobody calls externally
1020 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001021
Joe Onorato8a9b2202010-02-26 18:56:32 -08001022 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001023 }
1024
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001025 @Override
San Mehat873f2142010-01-14 10:25:07 -08001026 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001027 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001028
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001029 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001030 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001031 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001032 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001033 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001034 }
San Mehat873f2142010-01-14 10:25:07 -08001035
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001036 // 211 Forwarding enabled
1037 event.checkCode(IpFwdStatusResult);
1038 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001039 }
1040
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001041 @Override
1042 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001043 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001044 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001045 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001046 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001047 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001048 }
San Mehat873f2142010-01-14 10:25:07 -08001049 }
1050
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001051 @Override
1052 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001053 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001054 // cmd is "tether start first_start first_stop second_start second_stop ..."
1055 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001056
1057 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001058 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001059 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001060 }
Kenny Roota80ce062010-06-01 13:23:53 -07001061
1062 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001063 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001064 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001065 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001066 }
San Mehat873f2142010-01-14 10:25:07 -08001067 }
1068
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001069 @Override
1070 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001071 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001072 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001073 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001074 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001075 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001076 }
San Mehat873f2142010-01-14 10:25:07 -08001077 }
1078
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001079 @Override
1080 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001081 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001082
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001083 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001084 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001085 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001086 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001087 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001088 }
San Mehat873f2142010-01-14 10:25:07 -08001089
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001090 // 210 Tethering services started
1091 event.checkCode(TetherStatusResult);
1092 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001093 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001094
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001095 @Override
1096 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001098 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001099 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001100 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001101 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001102 }
San Mehat873f2142010-01-14 10:25:07 -08001103 }
1104
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001105 @Override
San Mehat873f2142010-01-14 10:25:07 -08001106 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001107 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001108 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001109 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001110 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001111 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001112 }
San Mehat873f2142010-01-14 10:25:07 -08001113 }
1114
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001115 @Override
1116 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001117 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001118 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001119 return NativeDaemonEvent.filterMessageList(
1120 mConnector.executeForList("tether", "interface", "list"),
1121 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001122 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001123 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001124 }
San Mehat873f2142010-01-14 10:25:07 -08001125 }
1126
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001127 @Override
1128 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001129 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001130
1131 final Command cmd = new Command("tether", "dns", "set");
1132 for (String s : dns) {
1133 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1134 }
1135
San Mehat873f2142010-01-14 10:25:07 -08001136 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001137 mConnector.execute(cmd);
1138 } catch (NativeDaemonConnectorException e) {
1139 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001140 }
1141 }
1142
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001143 @Override
1144 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001145 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001146 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001147 return NativeDaemonEvent.filterMessageList(
1148 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001149 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001150 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001151 }
San Mehat873f2142010-01-14 10:25:07 -08001152 }
1153
jiaguo1da35f72014-01-09 16:39:59 +08001154 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1155 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1156 for (InterfaceAddress ia : addresses) {
1157 if (!ia.getAddress().isLinkLocalAddress())
1158 filtered.add(ia);
1159 }
1160 return filtered;
1161 }
1162
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001163 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001164 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001165 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001166
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001167 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1168 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001169 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001170 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001171 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001172 // Don't touch link-local routes, as link-local addresses aren't routable,
1173 // kernel creates link-local routes on all interfaces automatically
1174 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1175 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001176 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001177 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001178 InetAddress addr = NetworkUtils.getNetworkPart(
1179 ia.getAddress(), ia.getNetworkPrefixLength());
1180 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001181 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001182 }
1183
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001184 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001185 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001186 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001187 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001188 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001189 }
1190
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001191 @Override
1192 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001193 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001194 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001195 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001196 } catch (SocketException e) {
1197 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001198 }
San Mehat873f2142010-01-14 10:25:07 -08001199 }
1200
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001201 @Override
1202 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001203 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001204 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001205 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001206 } catch (SocketException e) {
1207 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001208 }
San Mehat873f2142010-01-14 10:25:07 -08001209 }
San Mehat72759df2010-01-19 13:50:37 -08001210
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001211 @Override
1212 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001213 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001214 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001215 return NativeDaemonEvent.filterMessageList(
1216 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001217 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001218 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001219 }
San Mehat72759df2010-01-19 13:50:37 -08001220 }
1221
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001222 @Override
1223 public void attachPppd(
1224 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001225 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001226 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001227 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001228 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1229 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1230 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001231 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001232 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001233 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001234 }
1235 }
1236
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001237 @Override
1238 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001239 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001240 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001241 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001242 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001243 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001244 }
San Mehat72759df2010-01-19 13:50:37 -08001245 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001246
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001247 @Override
1248 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001249 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001250 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001251 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001252 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001253 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001254 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001255 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001256 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001257 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001258 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001259 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001260 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001261 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001262 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001263 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001264 }
1265
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001266 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001267 switch (wifiConfig.getAuthType()) {
1268 case KeyMgmt.WPA_PSK:
1269 return "wpa-psk";
1270 case KeyMgmt.WPA2_PSK:
1271 return "wpa2-psk";
1272 default:
1273 return "open";
1274 }
1275 }
1276
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001277 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001278 @Override
1279 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001280 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001281 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001282 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001283 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001284 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001285 }
1286 }
1287
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001288 @Override
1289 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001290 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001291 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001292 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001293 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001294 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001295 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001296 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001297 }
1298
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001299 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001300 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001301 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001302 try {
1303 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001304 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001305 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001306 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001307 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001308 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001309 }
1310 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001311 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001312 }
1313 }
San Mehat91cac642010-03-31 14:31:36 -07001314
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001315 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001316 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001317 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1318
1319 if (DBG) Slog.d(TAG, "Adding idletimer");
1320
1321 synchronized (mIdleTimerLock) {
1322 IdleTimerParams params = mActiveIdleTimers.get(iface);
1323 if (params != null) {
1324 // the interface already has idletimer, update network count
1325 params.networkCount++;
1326 return;
1327 }
1328
1329 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001330 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1331 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001332 } catch (NativeDaemonConnectorException e) {
1333 throw e.rethrowAsParcelableException();
1334 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001335 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1336
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001337 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001338 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1339 mNetworkActive = false;
1340 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001341 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001342 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001343 notifyInterfaceClassActivity(type,
1344 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1345 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001346 }
1347 });
Haoyu Bai04124232012-06-28 15:26:19 -07001348 }
1349 }
1350
1351 @Override
1352 public void removeIdleTimer(String iface) {
1353 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1354
1355 if (DBG) Slog.d(TAG, "Removing idletimer");
1356
1357 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001358 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001359 if (params == null || --(params.networkCount) > 0) {
1360 return;
1361 }
1362
1363 try {
1364 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001365 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001366 } catch (NativeDaemonConnectorException e) {
1367 throw e.rethrowAsParcelableException();
1368 }
1369 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001370 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001371 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001372 notifyInterfaceClassActivity(params.type,
1373 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1374 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001375 }
1376 });
Haoyu Bai04124232012-06-28 15:26:19 -07001377 }
1378 }
1379
1380 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001381 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001382 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001383 try {
1384 return mStatsFactory.readNetworkStatsSummaryDev();
1385 } catch (IOException e) {
1386 throw new IllegalStateException(e);
1387 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001388 }
1389
1390 @Override
1391 public NetworkStats getNetworkStatsSummaryXt() {
1392 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001393 try {
1394 return mStatsFactory.readNetworkStatsSummaryXt();
1395 } catch (IOException e) {
1396 throw new IllegalStateException(e);
1397 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001398 }
1399
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001400 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001401 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001402 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001403 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001404 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001405 } catch (IOException e) {
1406 throw new IllegalStateException(e);
1407 }
San Mehat91cac642010-03-31 14:31:36 -07001408 }
1409
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001410 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001411 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001412 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001413
Jeff Sharkey350083e2011-06-29 10:45:16 -07001414 // silently discard when control disabled
1415 // TODO: eventually migrate to be always enabled
1416 if (!mBandwidthControlEnabled) return;
1417
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001418 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001419 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001420 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001421 }
1422
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001423 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001424 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001425 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001426 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001427 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001428 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001429 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001430 }
1431 }
1432
1433 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001434 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001435 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001436
Jeff Sharkey350083e2011-06-29 10:45:16 -07001437 // silently discard when control disabled
1438 // TODO: eventually migrate to be always enabled
1439 if (!mBandwidthControlEnabled) return;
1440
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001441 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001442 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001443 // TODO: eventually consider throwing
1444 return;
1445 }
1446
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001447 mActiveQuotas.remove(iface);
1448 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001449
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001450 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001451 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001452 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001453 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001454 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001455 }
1456 }
1457 }
1458
1459 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001460 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001461 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001462
1463 // silently discard when control disabled
1464 // TODO: eventually migrate to be always enabled
1465 if (!mBandwidthControlEnabled) return;
1466
1467 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001468 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001469 throw new IllegalStateException("setting alert requires existing quota on iface");
1470 }
1471
1472 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001473 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001474 throw new IllegalStateException("iface " + iface + " already has alert");
1475 }
1476
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001477 try {
1478 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001479 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001480 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001481 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001482 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001483 }
1484 }
1485 }
1486
1487 @Override
1488 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001489 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001490
1491 // silently discard when control disabled
1492 // TODO: eventually migrate to be always enabled
1493 if (!mBandwidthControlEnabled) return;
1494
1495 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001496 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497 // TODO: eventually consider throwing
1498 return;
1499 }
1500
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001501 try {
1502 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001503 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001504 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001505 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001506 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001507 }
1508 }
1509 }
1510
1511 @Override
1512 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001513 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001514
1515 // silently discard when control disabled
1516 // TODO: eventually migrate to be always enabled
1517 if (!mBandwidthControlEnabled) return;
1518
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001519 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001520 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001521 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001522 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001523 }
1524 }
1525
1526 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001527 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001528 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001529
Jeff Sharkey350083e2011-06-29 10:45:16 -07001530 // silently discard when control disabled
1531 // TODO: eventually migrate to be always enabled
1532 if (!mBandwidthControlEnabled) return;
1533
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001534 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001535 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1536 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1537 // TODO: eventually consider throwing
1538 return;
1539 }
1540
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001541 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001542 mConnector.execute("bandwidth",
1543 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001544 if (rejectOnQuotaInterfaces) {
1545 mUidRejectOnQuota.put(uid, true);
1546 } else {
1547 mUidRejectOnQuota.delete(uid);
1548 }
1549 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001550 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001551 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001552 }
1553 }
1554
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001555 @Override
1556 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001557 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001558 return mBandwidthControlEnabled;
1559 }
1560
1561 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001562 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001563 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001564 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001565 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001566 } catch (IOException e) {
1567 throw new IllegalStateException(e);
1568 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001569 }
1570
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001571 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001572 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001573 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001574
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001575 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001576 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001577 final NativeDaemonEvent[] events = mConnector.executeForList(
1578 "bandwidth", "gettetherstats");
1579 for (NativeDaemonEvent event : events) {
1580 if (event.getCode() != TetheringStatsListResult) continue;
1581
1582 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1583 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1584 try {
1585 final String ifaceIn = tok.nextToken();
1586 final String ifaceOut = tok.nextToken();
1587
1588 final NetworkStats.Entry entry = new NetworkStats.Entry();
1589 entry.iface = ifaceOut;
1590 entry.uid = UID_TETHERING;
1591 entry.set = SET_DEFAULT;
1592 entry.tag = TAG_NONE;
1593 entry.rxBytes = Long.parseLong(tok.nextToken());
1594 entry.rxPackets = Long.parseLong(tok.nextToken());
1595 entry.txBytes = Long.parseLong(tok.nextToken());
1596 entry.txPackets = Long.parseLong(tok.nextToken());
1597 stats.combineValues(entry);
1598 } catch (NoSuchElementException e) {
1599 throw new IllegalStateException("problem parsing tethering stats: " + event);
1600 } catch (NumberFormatException e) {
1601 throw new IllegalStateException("problem parsing tethering stats: " + event);
1602 }
1603 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001604 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001605 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001606 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001607 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001608 }
1609
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001610 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001611 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001612 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001613
Paul Jensen13e817d2014-04-10 14:16:37 -04001614 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001615 (domains == null ? "" : domains));
1616
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001617 for (String s : servers) {
1618 InetAddress a = NetworkUtils.numericToInetAddress(s);
1619 if (a.isAnyLocalAddress() == false) {
1620 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001621 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001622 }
1623
1624 try {
1625 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001626 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001627 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001628 }
1629 }
1630
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001631 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001632 public void setUidRangeRoute(String iface, int uid_start, int uid_end, boolean forward_dns) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1634 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001635 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001636 "uid", "add", iface, uid_start, uid_end, forward_dns ? 1 : 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001637 } catch (NativeDaemonConnectorException e) {
1638 throw e.rethrowAsParcelableException();
1639 }
1640 }
1641
1642 @Override
1643 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1644 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1645 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001646 mConnector.execute("interface", "fwmark",
Paul Jensen13e817d2014-04-10 14:16:37 -04001647 "uid", "remove", iface, uid_start, uid_end, 0);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001648 } catch (NativeDaemonConnectorException e) {
1649 throw e.rethrowAsParcelableException();
1650 }
1651 }
1652
1653 @Override
1654 public void setMarkedForwarding(String iface) {
1655 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1656 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001657 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001658 } catch (NativeDaemonConnectorException e) {
1659 throw e.rethrowAsParcelableException();
1660 }
1661 }
1662
1663 @Override
1664 public void clearMarkedForwarding(String iface) {
1665 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1666 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001667 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1668 } catch (NativeDaemonConnectorException e) {
1669 throw e.rethrowAsParcelableException();
1670 }
1671 }
1672
1673 @Override
1674 public int getMarkForUid(int uid) {
1675 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1676 final NativeDaemonEvent event;
1677 try {
1678 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1679 } catch (NativeDaemonConnectorException e) {
1680 throw e.rethrowAsParcelableException();
1681 }
1682 event.checkCode(GetMarkResult);
1683 return Integer.parseInt(event.getMessage());
1684 }
1685
1686 @Override
1687 public int getMarkForProtect() {
1688 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1689 final NativeDaemonEvent event;
1690 try {
1691 event = mConnector.execute("interface", "fwmark", "get", "protect");
1692 } catch (NativeDaemonConnectorException e) {
1693 throw e.rethrowAsParcelableException();
1694 }
1695 event.checkCode(GetMarkResult);
1696 return Integer.parseInt(event.getMessage());
1697 }
1698
1699 @Override
1700 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1701 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1702 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001703 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001704 mConnector.execute("interface", "fwmark", "route", "add", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001705 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubakercca54c42013-06-27 17:41:38 -07001706 } catch (NativeDaemonConnectorException e) {
1707 throw e.rethrowAsParcelableException();
1708 }
1709 }
1710
1711 @Override
1712 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1713 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1714 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001715 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001716 mConnector.execute("interface", "fwmark", "route", "remove", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001717 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001718 } catch (NativeDaemonConnectorException e) {
1719 throw e.rethrowAsParcelableException();
1720 }
1721 }
1722
1723 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001724 public void setHostExemption(LinkAddress host) {
1725 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1726 try {
1727 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1728 } catch (NativeDaemonConnectorException e) {
1729 throw e.rethrowAsParcelableException();
1730 }
1731 }
1732
1733 @Override
1734 public void clearHostExemption(LinkAddress host) {
1735 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1736 try {
1737 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1738 } catch (NativeDaemonConnectorException e) {
1739 throw e.rethrowAsParcelableException();
1740 }
1741 }
1742
1743 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001744 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001745 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1746 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001747 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001748 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001749 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001750 }
1751 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001752
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001753 @Override
1754 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001755 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001756 try {
1757 mConnector.execute("firewall", enabled ? "enable" : "disable");
1758 mFirewallEnabled = enabled;
1759 } catch (NativeDaemonConnectorException e) {
1760 throw e.rethrowAsParcelableException();
1761 }
1762 }
1763
1764 @Override
1765 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001766 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001767 return mFirewallEnabled;
1768 }
1769
1770 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001771 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001772 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001773 Preconditions.checkState(mFirewallEnabled);
1774 final String rule = allow ? ALLOW : DENY;
1775 try {
1776 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1777 } catch (NativeDaemonConnectorException e) {
1778 throw e.rethrowAsParcelableException();
1779 }
1780 }
1781
1782 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001783 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001784 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001785 Preconditions.checkState(mFirewallEnabled);
1786 final String rule = allow ? ALLOW : DENY;
1787 try {
1788 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1789 } catch (NativeDaemonConnectorException e) {
1790 throw e.rethrowAsParcelableException();
1791 }
1792 }
1793
1794 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001795 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001796 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001797 Preconditions.checkState(mFirewallEnabled);
1798 final String rule = allow ? ALLOW : DENY;
1799 try {
1800 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1801 } catch (NativeDaemonConnectorException e) {
1802 throw e.rethrowAsParcelableException();
1803 }
1804 }
1805
1806 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001807 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001808 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001809 Preconditions.checkState(mFirewallEnabled);
1810 final String rule = allow ? ALLOW : DENY;
1811 try {
1812 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1813 } catch (NativeDaemonConnectorException e) {
1814 throw e.rethrowAsParcelableException();
1815 }
1816 }
1817
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001818 private static void enforceSystemUid() {
1819 final int uid = Binder.getCallingUid();
1820 if (uid != Process.SYSTEM_UID) {
1821 throw new SecurityException("Only available to AID_SYSTEM");
1822 }
1823 }
1824
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001825 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001826 public void startClatd(String interfaceName) throws IllegalStateException {
1827 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1828
1829 try {
1830 mConnector.execute("clatd", "start", interfaceName);
1831 } catch (NativeDaemonConnectorException e) {
1832 throw e.rethrowAsParcelableException();
1833 }
1834 }
1835
1836 @Override
1837 public void stopClatd() throws IllegalStateException {
1838 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1839
1840 try {
1841 mConnector.execute("clatd", "stop");
1842 } catch (NativeDaemonConnectorException e) {
1843 throw e.rethrowAsParcelableException();
1844 }
1845 }
1846
1847 @Override
1848 public boolean isClatdStarted() {
1849 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1850
1851 final NativeDaemonEvent event;
1852 try {
1853 event = mConnector.execute("clatd", "status");
1854 } catch (NativeDaemonConnectorException e) {
1855 throw e.rethrowAsParcelableException();
1856 }
1857
1858 event.checkCode(ClatdStatusResult);
1859 return event.getMessage().endsWith("started");
1860 }
1861
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001862 @Override
1863 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1864 mNetworkActivityListeners.register(listener);
1865 }
1866
1867 @Override
1868 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1869 mNetworkActivityListeners.unregister(listener);
1870 }
1871
1872 @Override
1873 public boolean isNetworkActive() {
1874 synchronized (mNetworkActivityListeners) {
1875 return mNetworkActive || mActiveIdleTimers.isEmpty();
1876 }
1877 }
1878
1879 private void reportNetworkActive() {
1880 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001881 try {
1882 for (int i = 0; i < length; i++) {
1883 try {
1884 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1885 } catch (RemoteException e) {
1886 } catch (RuntimeException e) {
1887 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001888 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001889 } finally {
1890 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001891 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001892 }
1893
Mattias Falk8b47b362011-08-23 14:15:13 +02001894 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001895 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001896 public void monitor() {
1897 if (mConnector != null) {
1898 mConnector.monitor();
1899 }
1900 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001901
1902 @Override
1903 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1904 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1905
Robert Greenwalt470fd722012-01-18 12:51:15 -08001906 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1907 mConnector.dump(fd, pw, args);
1908 pw.println();
1909
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001910 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001911 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1912 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1913 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001914
1915 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001916 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1917 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001918 }
1919
1920 synchronized (mUidRejectOnQuota) {
1921 pw.print("UID reject on quota ifaces: [");
1922 final int size = mUidRejectOnQuota.size();
1923 for (int i = 0; i < size; i++) {
1924 pw.print(mUidRejectOnQuota.keyAt(i));
1925 if (i < size - 1) pw.print(",");
1926 }
1927 pw.println("]");
1928 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001929
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001930 synchronized (mIdleTimerLock) {
1931 pw.println("Idle timers:");
1932 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
1933 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
1934 IdleTimerParams params = ent.getValue();
1935 pw.print(" timeout="); pw.print(params.timeout);
1936 pw.print(" type="); pw.print(params.type);
1937 pw.print(" networkCount="); pw.println(params.networkCount);
1938 }
1939 }
1940
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001941 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001942 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001943
Robert Greenwalt568891d2014-04-04 13:38:00 -07001944 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001945 public void createNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001946 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1947
1948 try {
Paul Jensen992f2522014-04-28 10:33:11 -04001949 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001950 } catch (NativeDaemonConnectorException e) {
1951 throw e.rethrowAsParcelableException();
1952 }
1953 }
1954
Robert Greenwalt568891d2014-04-04 13:38:00 -07001955 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001956 public void removeNetwork(int netId) {
1957 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1958
1959 try {
1960 mConnector.execute("network", "destroy", netId);
1961 } catch (NativeDaemonConnectorException e) {
1962 throw e.rethrowAsParcelableException();
1963 }
1964 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07001965
1966 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04001967 public void addInterfaceToNetwork(String iface, int netId) {
1968 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1969
1970 try {
1971 mConnector.execute("network", "addiface", netId, iface);
1972 } catch (NativeDaemonConnectorException e) {
1973 throw e.rethrowAsParcelableException();
1974 }
1975 }
1976
1977 @Override
1978 public void removeInterfaceFromNetwork(String iface, int netId) {
1979 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1980
1981 try {
1982 mConnector.execute("network", "removeiface", netId, iface);
1983 } catch (NativeDaemonConnectorException e) {
1984 throw e.rethrowAsParcelableException();
1985 }
1986 }
1987
1988 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001989 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1990 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001991 }
1992
1993 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07001994 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
1995 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07001996 }
1997
Robert Greenwalt913c8952014-04-07 17:36:35 -07001998 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07001999 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2000
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002001 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002002
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002003 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002004 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002005 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002006 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002007 if (routeInfo.hasGateway()) {
2008 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2009 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002010
2011 try {
2012 mConnector.execute(cmd);
2013 } catch (NativeDaemonConnectorException e) {
2014 throw e.rethrowAsParcelableException();
2015 }
2016 }
2017
2018 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002019 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002020 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2021
2022 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002023 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002024 } catch (NativeDaemonConnectorException e) {
2025 throw e.rethrowAsParcelableException();
2026 }
2027 }
2028
2029 @Override
2030 public void clearDefaultNetId() {
2031 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2032
2033 try {
2034 mConnector.execute("network", "default", "clear");
2035 } catch (NativeDaemonConnectorException e) {
2036 throw e.rethrowAsParcelableException();
2037 }
2038 }
2039
2040 @Override
2041 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2042 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2043
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002044 final Command cmd = new Command("network", "permission", "user", "set");
2045 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2046 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002047 for (int i=0; i<uids.length; i++) {
2048 cmd.appendArg(uids[i]);
2049 }
2050
2051 try {
2052 mConnector.execute(cmd);
2053 } catch (NativeDaemonConnectorException e) {
2054 throw e.rethrowAsParcelableException();
2055 }
2056 }
2057
2058 @Override
2059 public void clearPermission(int[] uids) {
2060 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2061
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002062 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002063 for (int i=0; i<uids.length; i++) {
2064 cmd.appendArg(uids[i]);
2065 }
2066
2067 try {
2068 mConnector.execute(cmd);
2069 } catch (NativeDaemonConnectorException e) {
2070 throw e.rethrowAsParcelableException();
2071 }
2072 }
San Mehat873f2142010-01-14 10:25:07 -08002073}