blob: c9f40cfbb71ed21280e696c42e84876a12557bc5 [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;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +090044import android.net.IpPrefix;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070046import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080047import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070048import android.net.RouteInfo;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040049import android.net.UidRange;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080050import android.net.wifi.WifiConfiguration;
51import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070052import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070053import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070054import android.os.Handler;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080055import android.os.INetworkActivityListener;
San Mehat873f2142010-01-14 10:25:07 -080056import android.os.INetworkManagementService;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080057import android.os.PowerManager;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070058import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080059import android.os.RemoteCallbackList;
60import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070061import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070062import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080063import android.os.SystemProperties;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070064import android.telephony.DataConnectionRealTimeInfo;
65import android.telephony.PhoneStateListener;
Wink Savillefb40dd42014-06-12 17:02:31 -070066import android.telephony.SubscriptionManager;
Wink Saville67e07892014-06-18 16:43:14 -070067import android.telephony.TelephonyManager;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080068import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080069import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070070import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080071
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070072import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070073import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070074import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080075import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070076import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070077import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070078import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070079
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070080import java.io.BufferedReader;
81import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080082import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070083import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070084import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070085import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070086import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070087import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070088import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070089import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070090import java.net.InterfaceAddress;
91import java.net.NetworkInterface;
92import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070093import java.util.ArrayList;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040094import java.util.Arrays;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070095import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080096import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070097import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070098import java.util.NoSuchElementException;
99import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700100import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -0800101
102/**
103 * @hide
104 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700105public class NetworkManagementService extends INetworkManagementService.Stub
106 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700107 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700108 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -0700109 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900110 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700111
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800112 private static final String ADD = "add";
113 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700114
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700115 private static final String ALLOW = "allow";
116 private static final String DENY = "deny";
117
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700118 private static final String DEFAULT = "default";
119 private static final String SECONDARY = "secondary";
120
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400121 private static final int MAX_UID_RANGES_PER_COMMAND = 10;
122
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700123 /**
124 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
125 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
126 */
127 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
128
San Mehat873f2142010-01-14 10:25:07 -0800129 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700130 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800131 public static final int InterfaceListResult = 110;
132 public static final int TetherInterfaceListResult = 111;
133 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800134 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700135 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800136
137 public static final int TetherStatusResult = 210;
138 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800139 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800140 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700141 public static final int InterfaceRxCounterResult = 216;
142 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700143 public static final int QuotaCounterResult = 220;
144 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800145 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900146 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700147 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800148
149 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700150 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700151 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900152 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900153 public static final int InterfaceDnsServerInfo = 615;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900154 public static final int RouteChange = 616;
San Mehat873f2142010-01-14 10:25:07 -0800155 }
156
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700157 static final int DAEMON_MSG_MOBILE_CONN_REAL_TIME_INFO = 1;
158
San Mehat873f2142010-01-14 10:25:07 -0800159 /**
160 * Binder context for this service
161 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700162 private final Context mContext;
San Mehat873f2142010-01-14 10:25:07 -0800163
164 /**
165 * connector object for communicating with netd
166 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700167 private final NativeDaemonConnector mConnector;
San Mehat873f2142010-01-14 10:25:07 -0800168
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700169 private final Handler mFgHandler;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700170 private final Handler mDaemonHandler;
171 private final PhoneStateListener mPhoneStateListener;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700172
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800173 private IBatteryStats mBatteryStats;
174
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700175 private final Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700176 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700177
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800178 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
179 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800180
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700181 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
182
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700183 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700184 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700185 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700186 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700187 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700188 /** Set of UIDs with active reject rules. */
189 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
190
Haoyu Bai04124232012-06-28 15:26:19 -0700191 private Object mIdleTimerLock = new Object();
192 /** Set of interfaces with active idle timers. */
193 private static class IdleTimerParams {
194 public final int timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800195 public final int type;
Haoyu Bai04124232012-06-28 15:26:19 -0700196 public int networkCount;
197
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800198 IdleTimerParams(int timeout, int type) {
Haoyu Bai04124232012-06-28 15:26:19 -0700199 this.timeout = timeout;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800200 this.type = type;
Haoyu Bai04124232012-06-28 15:26:19 -0700201 this.networkCount = 1;
202 }
203 }
204 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
205
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700206 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700207 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700208
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700209 private boolean mMobileActivityFromRadio = false;
210 private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
211
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800212 private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
213 new RemoteCallbackList<INetworkActivityListener>();
214 private boolean mNetworkActive;
215
San Mehat873f2142010-01-14 10:25:07 -0800216 /**
217 * Constructs a new NetworkManagementService instance
218 *
219 * @param context Binder context for this service
220 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900221 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800222 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800223
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700224 // make sure this is on the same looper as our NativeDaemonConnector for sync purposes
225 mFgHandler = new Handler(FgThread.get().getLooper());
226
Marco Nelissen62dbb222010-02-18 10:56:30 -0800227 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700228 mConnector = null;
229 mThread = null;
230 mDaemonHandler = null;
231 mPhoneStateListener = null;
Marco Nelissen62dbb222010-02-18 10:56:30 -0800232 return;
233 }
234
Dianne Hackborn4590e522014-03-24 13:36:46 -0700235 // Don't need this wake lock, since we now have a time stamp for when
236 // the network actually went inactive. (It might be nice to still do this,
237 // but I don't want to do it through the power manager because that pollutes the
238 // battery stats history with pointless noise.)
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700239 //PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn4590e522014-03-24 13:36:46 -0700240 PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800241
San Mehat873f2142010-01-14 10:25:07 -0800242 mConnector = new NativeDaemonConnector(
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700243 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl,
244 FgThread.get().getLooper());
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700245 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700246
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700247 mDaemonHandler = new Handler(FgThread.get().getLooper());
Wink Saville67e07892014-06-18 16:43:14 -0700248
249 mPhoneStateListener = new PhoneStateListener(SubscriptionManager.DEFAULT_SUB_ID,
Wink Savillefb40dd42014-06-12 17:02:31 -0700250 mDaemonHandler.getLooper()) {
Wink Saville67e07892014-06-18 16:43:14 -0700251 @Override
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700252 public void onDataConnectionRealTimeInfoChanged(
253 DataConnectionRealTimeInfo dcRtInfo) {
Wink Saville67e07892014-06-18 16:43:14 -0700254 if (DBG) Slog.d(TAG, "onDataConnectionRealTimeInfoChanged: " + dcRtInfo);
Dianne Hackborn2d4b4ed2014-05-21 15:01:03 -0700255 notifyInterfaceClassActivity(ConnectivityManager.TYPE_MOBILE,
256 dcRtInfo.getDcPowerState(), dcRtInfo.getTime(), true);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700257 }
258 };
Wink Saville67e07892014-06-18 16:43:14 -0700259 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
260 if (tm != null) {
261 tm.listen(mPhoneStateListener,
262 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO);
263 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700264
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700265 // Add ourself to the Watchdog monitors.
266 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700267 }
268
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900269 static NetworkManagementService create(Context context,
270 String socket) throws InterruptedException {
271 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700272 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700273 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
274 service.mThread.start();
275 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700276 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700277 if (DBG) Slog.d(TAG, "Connected");
278 return service;
San Mehat873f2142010-01-14 10:25:07 -0800279 }
280
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900281 public static NetworkManagementService create(Context context) throws InterruptedException {
282 return create(context, NETD_SOCKET_NAME);
283 }
284
Jeff Sharkey350083e2011-06-29 10:45:16 -0700285 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700286 prepareNativeDaemon();
287 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700288 }
289
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800290 private IBatteryStats getBatteryStats() {
291 synchronized (this) {
292 if (mBatteryStats != null) {
293 return mBatteryStats;
294 }
295 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
296 BatteryStats.SERVICE_NAME));
297 return mBatteryStats;
298 }
299 }
300
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800301 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800302 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800303 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800304 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800305 }
306
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800307 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800308 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800309 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800310 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800311 }
312
313 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700314 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800315 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700316 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800317 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700318 try {
319 for (int i = 0; i < length; i++) {
320 try {
321 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
322 } catch (RemoteException e) {
323 } catch (RuntimeException e) {
324 }
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700325 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700326 } finally {
327 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700328 }
329 }
330
331 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700332 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700333 * (typically, an Ethernet cable has been plugged-in or unplugged).
334 */
335 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800336 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700337 try {
338 for (int i = 0; i < length; i++) {
339 try {
340 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
341 } catch (RemoteException e) {
342 } catch (RuntimeException e) {
343 }
San Mehat4d02d002010-01-22 16:07:46 -0800344 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700345 } finally {
346 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800347 }
348 }
349
350 /**
351 * Notify our observers of an interface addition.
352 */
353 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800354 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700355 try {
356 for (int i = 0; i < length; i++) {
357 try {
358 mObservers.getBroadcastItem(i).interfaceAdded(iface);
359 } catch (RemoteException e) {
360 } catch (RuntimeException e) {
361 }
San Mehat4d02d002010-01-22 16:07:46 -0800362 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700363 } finally {
364 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800365 }
366 }
367
368 /**
369 * Notify our observers of an interface removal.
370 */
371 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700372 // netd already clears out quota and alerts for removed ifaces; update
373 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700374 mActiveAlerts.remove(iface);
375 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700376
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800377 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700378 try {
379 for (int i = 0; i < length; i++) {
380 try {
381 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
382 } catch (RemoteException e) {
383 } catch (RuntimeException e) {
384 }
San Mehat4d02d002010-01-22 16:07:46 -0800385 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700386 } finally {
387 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800388 }
389 }
390
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700391 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700392 * Notify our observers of a limit reached.
393 */
394 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800395 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700396 try {
397 for (int i = 0; i < length; i++) {
398 try {
399 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
400 } catch (RemoteException e) {
401 } catch (RuntimeException e) {
402 }
JP Abgrall12b933d2011-07-14 18:09:22 -0700403 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700404 } finally {
405 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700406 }
407 }
408
409 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700410 * Notify our observers of a change in the data activity state of the interface
411 */
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 private void notifyInterfaceClassActivity(int type, int powerState, long tsNanos,
413 boolean fromRadio) {
414 final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
415 if (isMobile) {
416 if (!fromRadio) {
417 if (mMobileActivityFromRadio) {
418 // If this call is not coming from a report from the radio itself, but we
419 // have previously received reports from the radio, then we will take the
420 // power state to just be whatever the radio last reported.
421 powerState = mLastPowerStateFromRadio;
422 }
423 } else {
424 mMobileActivityFromRadio = true;
425 }
426 if (mLastPowerStateFromRadio != powerState) {
427 mLastPowerStateFromRadio = powerState;
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700428 try {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700429 getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos);
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700430 } catch (RemoteException e) {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700431 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700432 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700433 }
434
435 boolean isActive = powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
436 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
437
438 if (!isMobile || fromRadio || !mMobileActivityFromRadio) {
439 // Report the change in data activity. We don't do this if this is a change
440 // on the mobile network, that is not coming from the radio itself, and we
441 // have previously seen change reports from the radio. In that case only
442 // the radio is the authority for the current state.
443 final int length = mObservers.beginBroadcast();
444 try {
445 for (int i = 0; i < length; i++) {
446 try {
447 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
448 Integer.toString(type), isActive, tsNanos);
449 } catch (RemoteException e) {
450 } catch (RuntimeException e) {
451 }
452 }
453 } finally {
454 mObservers.finishBroadcast();
455 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700456 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800457
458 boolean report = false;
459 synchronized (mIdleTimerLock) {
460 if (mActiveIdleTimers.isEmpty()) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700461 // If there are no idle timers, we are not monitoring activity, so we
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800462 // are always considered active.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700463 isActive = true;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800464 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700465 if (mNetworkActive != isActive) {
466 mNetworkActive = isActive;
467 report = isActive;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800468 }
469 }
470 if (report) {
471 reportNetworkActive();
472 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700473 }
474
475 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700476 * Prepare native daemon once connected, enabling modules and pushing any
477 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700478 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700479 private void prepareNativeDaemon() {
480 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700481
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700482 // only enable bandwidth control when support exists
483 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
484 if (hasKernelSupport) {
485 Slog.d(TAG, "enabling bandwidth control");
486 try {
487 mConnector.execute("bandwidth", "enable");
488 mBandwidthControlEnabled = true;
489 } catch (NativeDaemonConnectorException e) {
490 Log.wtf(TAG, "problem enabling bandwidth controls", e);
491 }
492 } else {
493 Slog.d(TAG, "not enabling bandwidth control");
494 }
495
496 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
497
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700498 if (mBandwidthControlEnabled) {
499 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800500 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700501 } catch (RemoteException e) {
502 }
503 }
504
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700505 // push any existing quota or UID rules
506 synchronized (mQuotaLock) {
507 int size = mActiveQuotas.size();
508 if (size > 0) {
509 Slog.d(TAG, "pushing " + size + " active quota rules");
510 final HashMap<String, Long> activeQuotas = mActiveQuotas;
511 mActiveQuotas = Maps.newHashMap();
512 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
513 setInterfaceQuota(entry.getKey(), entry.getValue());
514 }
515 }
516
517 size = mActiveAlerts.size();
518 if (size > 0) {
519 Slog.d(TAG, "pushing " + size + " active alert rules");
520 final HashMap<String, Long> activeAlerts = mActiveAlerts;
521 mActiveAlerts = Maps.newHashMap();
522 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
523 setInterfaceAlert(entry.getKey(), entry.getValue());
524 }
525 }
526
527 size = mUidRejectOnQuota.size();
528 if (size > 0) {
529 Slog.d(TAG, "pushing " + size + " active uid rules");
530 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
531 mUidRejectOnQuota = new SparseBooleanArray();
532 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
533 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
534 }
535 }
536 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700537
538 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700539 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700540 }
San Mehat4d02d002010-01-22 16:07:46 -0800541
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900542 /**
543 * Notify our observers of a new or updated interface address.
544 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900545 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900546 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700547 try {
548 for (int i = 0; i < length; i++) {
549 try {
550 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
551 } catch (RemoteException e) {
552 } catch (RuntimeException e) {
553 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900554 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700555 } finally {
556 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900557 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900558 }
559
560 /**
561 * Notify our observers of a deleted interface address.
562 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900563 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900564 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700565 try {
566 for (int i = 0; i < length; i++) {
567 try {
568 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
569 } catch (RemoteException e) {
570 } catch (RuntimeException e) {
571 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900572 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700573 } finally {
574 mObservers.finishBroadcast();
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900575 }
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900576 }
577
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900578 /**
579 * Notify our observers of DNS server information received.
580 */
581 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
582 final int length = mObservers.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700583 try {
584 for (int i = 0; i < length; i++) {
585 try {
586 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
587 addresses);
588 } catch (RemoteException e) {
589 } catch (RuntimeException e) {
590 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900591 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700592 } finally {
593 mObservers.finishBroadcast();
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900594 }
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900595 }
596
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900597 /**
598 * Notify our observers of a route change.
599 */
600 private void notifyRouteChange(String action, RouteInfo route) {
601 final int length = mObservers.beginBroadcast();
602 try {
603 for (int i = 0; i < length; i++) {
604 try {
605 if (action.equals("updated")) {
606 mObservers.getBroadcastItem(i).routeUpdated(route);
607 } else {
608 mObservers.getBroadcastItem(i).routeRemoved(route);
609 }
610 } catch (RemoteException e) {
611 } catch (RuntimeException e) {
612 }
613 }
614 } finally {
615 mObservers.finishBroadcast();
616 }
617 }
618
San Mehat873f2142010-01-14 10:25:07 -0800619 //
620 // Netd Callback handling
621 //
622
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700623 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
624 @Override
San Mehat873f2142010-01-14 10:25:07 -0800625 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700626 // event is dispatched from internal NDC thread, so we prepare the
627 // daemon back on main thread.
628 if (mConnectedSignal != null) {
629 mConnectedSignal.countDown();
630 mConnectedSignal = null;
631 } else {
Robert Greenwalt2c9f5472014-04-21 14:50:28 -0700632 mFgHandler.post(new Runnable() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700633 @Override
634 public void run() {
635 prepareNativeDaemon();
636 }
637 });
638 }
San Mehat873f2142010-01-14 10:25:07 -0800639 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700640
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700641 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800642 public boolean onCheckHoldWakeLock(int code) {
643 return code == NetdResponseCode.InterfaceClassActivity;
644 }
645
646 @Override
San Mehat873f2142010-01-14 10:25:07 -0800647 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900648 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700649 switch (code) {
650 case NetdResponseCode.InterfaceChange:
651 /*
652 * a network interface change occured
653 * Format: "NNN Iface added <name>"
654 * "NNN Iface removed <name>"
655 * "NNN Iface changed <name> <up/down>"
656 * "NNN Iface linkstatus <name> <up/down>"
657 */
658 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900659 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700660 }
661 if (cooked[2].equals("added")) {
662 notifyInterfaceAdded(cooked[3]);
663 return true;
664 } else if (cooked[2].equals("removed")) {
665 notifyInterfaceRemoved(cooked[3]);
666 return true;
667 } else if (cooked[2].equals("changed") && cooked.length == 5) {
668 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
669 return true;
670 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
671 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
672 return true;
673 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900674 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700675 // break;
676 case NetdResponseCode.BandwidthControl:
677 /*
678 * Bandwidth control needs some attention
679 * Format: "NNN limit alert <alertName> <ifaceName>"
680 */
681 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900682 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700683 }
684 if (cooked[2].equals("alert")) {
685 notifyLimitReached(cooked[3], cooked[4]);
686 return true;
687 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900688 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700689 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700690 case NetdResponseCode.InterfaceClassActivity:
691 /*
692 * An network interface class state changed (active/idle)
693 * Format: "NNN IfaceClass <active/idle> <label>"
694 */
695 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900696 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700697 }
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700698 long timestampNanos = 0;
699 if (cooked.length == 5) {
700 try {
701 timestampNanos = Long.parseLong(cooked[4]);
702 } catch(NumberFormatException ne) {}
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700703 } else {
704 timestampNanos = SystemClock.elapsedRealtimeNanos();
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700705 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700706 boolean isActive = cooked[2].equals("active");
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700707 notifyInterfaceClassActivity(Integer.parseInt(cooked[3]),
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700708 isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
709 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW, timestampNanos, false);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700710 return true;
711 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900712 case NetdResponseCode.InterfaceAddressChange:
713 /*
714 * A network address change occurred
715 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
716 * "NNN Address removed <addr> <iface> <flags> <scope>"
717 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900718 if (cooked.length < 7 || !cooked[1].equals("Address")) {
719 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900720 }
721
Lorenzo Colitti64483942013-11-15 18:43:52 +0900722 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900723 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900724 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900725 int flags = Integer.parseInt(cooked[5]);
726 int scope = Integer.parseInt(cooked[6]);
727 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900728 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
729 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900730 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900731 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900732 }
733
734 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900735 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900736 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900737 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900738 }
739 return true;
740 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900741 case NetdResponseCode.InterfaceDnsServerInfo:
742 /*
743 * Information about available DNS servers has been received.
744 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
745 */
746 long lifetime; // Actually a 32-bit unsigned integer.
747
748 if (cooked.length == 6 &&
749 cooked[1].equals("DnsInfo") &&
750 cooked[2].equals("servers")) {
751 try {
752 lifetime = Long.parseLong(cooked[4]);
753 } catch (NumberFormatException e) {
754 throw new IllegalStateException(errorMessage);
755 }
756 String[] servers = cooked[5].split(",");
757 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
758 }
759 return true;
760 // break;
Lorenzo Colittic18cbfd2014-06-13 21:21:03 +0900761 case NetdResponseCode.RouteChange:
762 /*
763 * A route has been updated or removed.
764 * Format: "NNN Route <updated|removed> <dst> [via <gateway] [dev <iface>]"
765 */
766 if (!cooked[1].equals("Route") || cooked.length < 6) {
767 throw new IllegalStateException(errorMessage);
768 }
769
770 String via = null;
771 String dev = null;
772 boolean valid = true;
773 for (int i = 4; (i + 1) < cooked.length && valid; i += 2) {
774 if (cooked[i].equals("dev")) {
775 if (dev == null) {
776 dev = cooked[i+1];
777 } else {
778 valid = false; // Duplicate interface.
779 }
780 } else if (cooked[i].equals("via")) {
781 if (via == null) {
782 via = cooked[i+1];
783 } else {
784 valid = false; // Duplicate gateway.
785 }
786 } else {
787 valid = false; // Unknown syntax.
788 }
789 }
790 if (valid) {
791 try {
792 // InetAddress.parseNumericAddress(null) inexplicably returns ::1.
793 InetAddress gateway = null;
794 if (via != null) gateway = InetAddress.parseNumericAddress(via);
795 RouteInfo route = new RouteInfo(new IpPrefix(cooked[3]), gateway, dev);
796 notifyRouteChange(cooked[2], route);
797 return true;
798 } catch (IllegalArgumentException e) {}
799 }
800 throw new IllegalStateException(errorMessage);
801 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700802 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800803 }
804 return false;
San Mehat873f2142010-01-14 10:25:07 -0800805 }
806 }
807
San Mehated4fc8a2010-01-22 12:28:36 -0800808
San Mehat873f2142010-01-14 10:25:07 -0800809 //
810 // INetworkManagementService members
811 //
812
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800813 @Override
814 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800815 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700816 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800817 return NativeDaemonEvent.filterMessageList(
818 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700819 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800820 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700821 }
San Mehated4fc8a2010-01-22 12:28:36 -0800822 }
823
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800824 @Override
825 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800826 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800827
828 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700829 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800830 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700831 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800832 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700833 }
San Mehated4fc8a2010-01-22 12:28:36 -0800834
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800835 event.checkCode(InterfaceGetCfgResult);
836
837 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
838 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800839
Kenny Roota80ce062010-06-01 13:23:53 -0700840 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800841 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700842 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800843 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800844 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800845 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700846 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800847 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800848 } catch (IllegalArgumentException iae) {
849 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700850 }
851
852 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800853 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800854 } catch (NumberFormatException nfe) {
855 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700856 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800857
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800858 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
859 while (st.hasMoreTokens()) {
860 cfg.setFlag(st.nextToken());
861 }
Kenny Roota80ce062010-06-01 13:23:53 -0700862 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800863 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800864 }
San Mehated4fc8a2010-01-22 12:28:36 -0800865 return cfg;
866 }
867
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800868 @Override
869 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800870 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800871 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800872 if (linkAddr == null || linkAddr.getAddress() == null) {
873 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800874 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800875
876 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800877 linkAddr.getAddress().getHostAddress(),
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900878 linkAddr.getPrefixLength());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800879 for (String flag : cfg.getFlags()) {
880 cmd.appendArg(flag);
881 }
882
Kenny Roota80ce062010-06-01 13:23:53 -0700883 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800884 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700885 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800886 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700887 }
San Mehat873f2142010-01-14 10:25:07 -0800888 }
889
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800890 @Override
891 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800892 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800893 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800894 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800895 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700896 }
897
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800898 @Override
899 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800900 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800901 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800902 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800903 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700904 }
905
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800906 @Override
907 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800908 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700909 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800910 mConnector.execute(
911 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700912 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800913 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700914 }
915 }
916
Irfan Sherifff5600612011-06-16 10:26:28 -0700917 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
918 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800919 @Override
920 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800921 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700922 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800923 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700924 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800925 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700926 }
927 }
928
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800929 @Override
930 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800931 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700932 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800933 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700934 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800935 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700936 }
937 }
938
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800939 @Override
940 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800941 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700942 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800943 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700944 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800945 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700946 }
947 }
948
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800949 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700950 public void addRoute(int netId, RouteInfo route) {
951 modifyRoute(netId, ADD, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700952 }
953
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800954 @Override
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700955 public void removeRoute(int netId, RouteInfo route) {
956 modifyRoute(netId, REMOVE, route);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700957 }
958
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700959 private void modifyRoute(int netId, String action, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800960 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700961
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700962 final Command cmd = new Command("network", "route", action, netId);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700963
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700964 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700965 final LinkAddress la = route.getDestinationLinkAddress();
Sreeram Ramachandranb2829fa2014-04-15 19:07:12 -0700966 cmd.appendArg(route.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900967 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -0700968 if (route.hasGateway()) {
969 cmd.appendArg(route.getGateway().getHostAddress());
970 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700971
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800972 try {
973 mConnector.execute(cmd);
974 } catch (NativeDaemonConnectorException e) {
975 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700976 }
977 }
978
979 private ArrayList<String> readRouteList(String filename) {
980 FileInputStream fstream = null;
981 ArrayList<String> list = new ArrayList<String>();
982
983 try {
984 fstream = new FileInputStream(filename);
985 DataInputStream in = new DataInputStream(fstream);
986 BufferedReader br = new BufferedReader(new InputStreamReader(in));
987 String s;
988
989 // throw away the title line
990
991 while (((s = br.readLine()) != null) && (s.length() != 0)) {
992 list.add(s);
993 }
994 } catch (IOException ex) {
995 // return current list, possibly empty
996 } finally {
997 if (fstream != null) {
998 try {
999 fstream.close();
1000 } catch (IOException ex) {}
1001 }
1002 }
1003
1004 return list;
1005 }
1006
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001007 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001008 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001009 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001010 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
1011
1012 // v4 routes listed as:
1013 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
1014 for (String s : readRouteList("/proc/net/route")) {
1015 String[] fields = s.split("\t");
1016
1017 if (fields.length > 7) {
1018 String iface = fields[0];
1019
1020 if (interfaceName.equals(iface)) {
1021 String dest = fields[1];
1022 String gate = fields[2];
1023 String flags = fields[3]; // future use?
1024 String mask = fields[7];
1025 try {
1026 // address stored as a hex string, ex: 0014A8C0
1027 InetAddress destAddr =
1028 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
1029 int prefixLength =
1030 NetworkUtils.netmaskIntToPrefixLength(
1031 (int)Long.parseLong(mask, 16));
1032 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1033
1034 // address stored as a hex string, ex 0014A8C0
1035 InetAddress gatewayAddr =
1036 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
1037
Wink Saville7b5fd052013-03-15 05:07:04 +00001038 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001039 routes.add(route);
1040 } catch (Exception e) {
1041 Log.e(TAG, "Error parsing route " + s + " : " + e);
1042 continue;
1043 }
1044 }
1045 }
1046 }
1047
1048 // v6 routes listed as:
1049 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
1050 for (String s : readRouteList("/proc/net/ipv6_route")) {
1051 String[]fields = s.split("\\s+");
1052 if (fields.length > 9) {
1053 String iface = fields[9].trim();
1054 if (interfaceName.equals(iface)) {
1055 String dest = fields[0];
1056 String prefix = fields[1];
1057 String gate = fields[4];
1058
1059 try {
1060 // prefix length stored as a hex string, ex 40
1061 int prefixLength = Integer.parseInt(prefix, 16);
1062
1063 // address stored as a 32 char hex string
1064 // ex fe800000000000000000000000000000
1065 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
1066 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
1067
1068 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
1069
Wink Saville7b5fd052013-03-15 05:07:04 +00001070 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001071 routes.add(route);
1072 } catch (Exception e) {
1073 Log.e(TAG, "Error parsing route " + s + " : " + e);
1074 continue;
1075 }
1076 }
1077 }
1078 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001079 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001080 }
1081
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001082 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +09001083 public void setMtu(String iface, int mtu) {
1084 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1085
1086 final NativeDaemonEvent event;
1087 try {
1088 event = mConnector.execute("interface", "setmtu", iface, mtu);
1089 } catch (NativeDaemonConnectorException e) {
1090 throw e.rethrowAsParcelableException();
1091 }
1092 }
1093
1094 @Override
San Mehat873f2142010-01-14 10:25:07 -08001095 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001096 // TODO: remove from aidl if nobody calls externally
1097 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001098
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -08001100 }
1101
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001102 @Override
San Mehat873f2142010-01-14 10:25:07 -08001103 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001104 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001105
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001107 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001108 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001109 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001110 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001111 }
San Mehat873f2142010-01-14 10:25:07 -08001112
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001113 // 211 Forwarding enabled
1114 event.checkCode(IpFwdStatusResult);
1115 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -08001116 }
1117
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001118 @Override
1119 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001120 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001121 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001122 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001123 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001124 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001125 }
San Mehat873f2142010-01-14 10:25:07 -08001126 }
1127
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001128 @Override
1129 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001130 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001131 // cmd is "tether start first_start first_stop second_start second_stop ..."
1132 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001133
1134 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001135 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -07001137 }
Kenny Roota80ce062010-06-01 13:23:53 -07001138
1139 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001140 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -07001141 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001142 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001143 }
San Mehat873f2142010-01-14 10:25:07 -08001144 }
1145
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001146 @Override
1147 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001148 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001149 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001150 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -07001151 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001152 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001153 }
San Mehat873f2142010-01-14 10:25:07 -08001154 }
1155
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001156 @Override
1157 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001158 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -08001159
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001160 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -07001161 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001162 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -07001163 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001164 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001165 }
San Mehat873f2142010-01-14 10:25:07 -08001166
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001167 // 210 Tethering services started
1168 event.checkCode(TetherStatusResult);
1169 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -08001170 }
Matthew Xiefe19f122012-07-12 16:03:32 -07001171
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001172 @Override
1173 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001174 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001175 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001176 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001177 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001178 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001179 }
San Mehat873f2142010-01-14 10:25:07 -08001180 }
1181
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001182 @Override
San Mehat873f2142010-01-14 10:25:07 -08001183 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001184 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001185 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001186 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -07001187 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001188 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001189 }
San Mehat873f2142010-01-14 10:25:07 -08001190 }
1191
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001192 @Override
1193 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001194 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001195 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001196 return NativeDaemonEvent.filterMessageList(
1197 mConnector.executeForList("tether", "interface", "list"),
1198 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001199 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001200 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001201 }
San Mehat873f2142010-01-14 10:25:07 -08001202 }
1203
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001204 @Override
1205 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001206 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001207
1208 final Command cmd = new Command("tether", "dns", "set");
1209 for (String s : dns) {
1210 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1211 }
1212
San Mehat873f2142010-01-14 10:25:07 -08001213 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001214 mConnector.execute(cmd);
1215 } catch (NativeDaemonConnectorException e) {
1216 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001217 }
1218 }
1219
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001220 @Override
1221 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001223 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001224 return NativeDaemonEvent.filterMessageList(
1225 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001226 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001227 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001228 }
San Mehat873f2142010-01-14 10:25:07 -08001229 }
1230
jiaguo1da35f72014-01-09 16:39:59 +08001231 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1232 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1233 for (InterfaceAddress ia : addresses) {
1234 if (!ia.getAddress().isLinkLocalAddress())
1235 filtered.add(ia);
1236 }
1237 return filtered;
1238 }
1239
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001240 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001241 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001242 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001243
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001244 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1245 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001246 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001247 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001248 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001249 // Don't touch link-local routes, as link-local addresses aren't routable,
1250 // kernel creates link-local routes on all interfaces automatically
1251 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1252 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001254 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001255 InetAddress addr = NetworkUtils.getNetworkPart(
1256 ia.getAddress(), ia.getNetworkPrefixLength());
1257 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001258 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001259 }
1260
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001261 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001262 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001263 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001264 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001265 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001266 }
1267
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001268 @Override
1269 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001270 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001271 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001272 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001273 } catch (SocketException e) {
1274 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001275 }
San Mehat873f2142010-01-14 10:25:07 -08001276 }
1277
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001278 @Override
1279 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001280 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001281 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001282 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001283 } catch (SocketException e) {
1284 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001285 }
San Mehat873f2142010-01-14 10:25:07 -08001286 }
San Mehat72759df2010-01-19 13:50:37 -08001287
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001288 @Override
1289 public String[] listTtys() {
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 return NativeDaemonEvent.filterMessageList(
1293 mConnector.executeForList("list_ttys"), TtyListResult);
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 }
San Mehat72759df2010-01-19 13:50:37 -08001297 }
1298
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001299 @Override
1300 public void attachPppd(
1301 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001302 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001303 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001304 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001305 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1306 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1307 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001308 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001309 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001310 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001311 }
1312 }
1313
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001314 @Override
1315 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001316 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001317 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001318 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001319 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001320 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001321 }
San Mehat72759df2010-01-19 13:50:37 -08001322 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001323
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001324 @Override
1325 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001326 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001327 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001328 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001329 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001330 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001331 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001332 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001333 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001334 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001335 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001336 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001337 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001338 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001339 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001340 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001341 }
1342
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001343 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001344 switch (wifiConfig.getAuthType()) {
1345 case KeyMgmt.WPA_PSK:
1346 return "wpa-psk";
1347 case KeyMgmt.WPA2_PSK:
1348 return "wpa2-psk";
1349 default:
1350 return "open";
1351 }
1352 }
1353
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001354 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001355 @Override
1356 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001357 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001358 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001359 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001360 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001361 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001362 }
1363 }
1364
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001365 @Override
1366 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001367 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001368 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001369 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001370 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001371 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001372 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001373 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001374 }
1375
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001376 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001377 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001378 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001379 try {
1380 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001381 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001382 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001383 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001384 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001385 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001386 }
1387 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001388 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001389 }
1390 }
San Mehat91cac642010-03-31 14:31:36 -07001391
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001392 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001393 public void addIdleTimer(String iface, int timeout, final int type) {
Haoyu Bai04124232012-06-28 15:26:19 -07001394 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1395
1396 if (DBG) Slog.d(TAG, "Adding idletimer");
1397
1398 synchronized (mIdleTimerLock) {
1399 IdleTimerParams params = mActiveIdleTimers.get(iface);
1400 if (params != null) {
1401 // the interface already has idletimer, update network count
1402 params.networkCount++;
1403 return;
1404 }
1405
1406 try {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001407 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout),
1408 Integer.toString(type));
Haoyu Bai04124232012-06-28 15:26:19 -07001409 } catch (NativeDaemonConnectorException e) {
1410 throw e.rethrowAsParcelableException();
1411 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001412 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, type));
1413
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001414 // Networks start up.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001415 if (ConnectivityManager.isNetworkTypeMobile(type)) {
1416 mNetworkActive = false;
1417 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001418 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001419 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001420 notifyInterfaceClassActivity(type,
1421 DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH,
1422 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001423 }
1424 });
Haoyu Bai04124232012-06-28 15:26:19 -07001425 }
1426 }
1427
1428 @Override
1429 public void removeIdleTimer(String iface) {
1430 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1431
1432 if (DBG) Slog.d(TAG, "Removing idletimer");
1433
1434 synchronized (mIdleTimerLock) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001435 final IdleTimerParams params = mActiveIdleTimers.get(iface);
Haoyu Bai04124232012-06-28 15:26:19 -07001436 if (params == null || --(params.networkCount) > 0) {
1437 return;
1438 }
1439
1440 try {
1441 mConnector.execute("idletimer", "remove", iface,
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001442 Integer.toString(params.timeout), Integer.toString(params.type));
Haoyu Bai04124232012-06-28 15:26:19 -07001443 } catch (NativeDaemonConnectorException e) {
1444 throw e.rethrowAsParcelableException();
1445 }
1446 mActiveIdleTimers.remove(iface);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001447 mDaemonHandler.post(new Runnable() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001448 @Override public void run() {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001449 notifyInterfaceClassActivity(params.type,
1450 DataConnectionRealTimeInfo.DC_POWER_STATE_LOW,
1451 SystemClock.elapsedRealtimeNanos(), false);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001452 }
1453 });
Haoyu Bai04124232012-06-28 15:26:19 -07001454 }
1455 }
1456
1457 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001458 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001459 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001460 try {
1461 return mStatsFactory.readNetworkStatsSummaryDev();
1462 } catch (IOException e) {
1463 throw new IllegalStateException(e);
1464 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001465 }
1466
1467 @Override
1468 public NetworkStats getNetworkStatsSummaryXt() {
1469 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001470 try {
1471 return mStatsFactory.readNetworkStatsSummaryXt();
1472 } catch (IOException e) {
1473 throw new IllegalStateException(e);
1474 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001475 }
1476
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001477 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001478 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001479 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001480 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001481 return mStatsFactory.readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001482 } catch (IOException e) {
1483 throw new IllegalStateException(e);
1484 }
San Mehat91cac642010-03-31 14:31:36 -07001485 }
1486
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001487 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001488 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001489 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001490
Jeff Sharkey350083e2011-06-29 10:45:16 -07001491 // silently discard when control disabled
1492 // TODO: eventually migrate to be always enabled
1493 if (!mBandwidthControlEnabled) return;
1494
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001495 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001496 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001497 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001498 }
1499
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001500 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001501 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001502 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001503 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001504 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001505 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001506 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001507 }
1508 }
1509
1510 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001511 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001513
Jeff Sharkey350083e2011-06-29 10:45:16 -07001514 // silently discard when control disabled
1515 // TODO: eventually migrate to be always enabled
1516 if (!mBandwidthControlEnabled) return;
1517
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001518 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001519 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001520 // TODO: eventually consider throwing
1521 return;
1522 }
1523
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001524 mActiveQuotas.remove(iface);
1525 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001526
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001527 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001528 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001529 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001530 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001531 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001532 }
1533 }
1534 }
1535
1536 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001537 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001538 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001539
1540 // silently discard when control disabled
1541 // TODO: eventually migrate to be always enabled
1542 if (!mBandwidthControlEnabled) return;
1543
1544 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001545 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001546 throw new IllegalStateException("setting alert requires existing quota on iface");
1547 }
1548
1549 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001550 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001551 throw new IllegalStateException("iface " + iface + " already has alert");
1552 }
1553
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001554 try {
1555 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001556 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001557 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001558 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001559 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001560 }
1561 }
1562 }
1563
1564 @Override
1565 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001566 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001567
1568 // silently discard when control disabled
1569 // TODO: eventually migrate to be always enabled
1570 if (!mBandwidthControlEnabled) return;
1571
1572 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001573 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001574 // TODO: eventually consider throwing
1575 return;
1576 }
1577
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001578 try {
1579 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001580 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001581 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001582 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001583 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001584 }
1585 }
1586 }
1587
1588 @Override
1589 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001590 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001591
1592 // silently discard when control disabled
1593 // TODO: eventually migrate to be always enabled
1594 if (!mBandwidthControlEnabled) return;
1595
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001596 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001597 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001598 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001599 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001600 }
1601 }
1602
1603 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001604 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001605 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001606
Jeff Sharkey350083e2011-06-29 10:45:16 -07001607 // silently discard when control disabled
1608 // TODO: eventually migrate to be always enabled
1609 if (!mBandwidthControlEnabled) return;
1610
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001611 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001612 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1613 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1614 // TODO: eventually consider throwing
1615 return;
1616 }
1617
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001618 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001619 mConnector.execute("bandwidth",
1620 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001621 if (rejectOnQuotaInterfaces) {
1622 mUidRejectOnQuota.put(uid, true);
1623 } else {
1624 mUidRejectOnQuota.delete(uid);
1625 }
1626 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001627 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001628 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001629 }
1630 }
1631
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001632 @Override
1633 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001634 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001635 return mBandwidthControlEnabled;
1636 }
1637
1638 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001639 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001640 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001641 try {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08001642 return mStatsFactory.readNetworkStatsDetail(uid, null, TAG_ALL, null);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001643 } catch (IOException e) {
1644 throw new IllegalStateException(e);
1645 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001646 }
1647
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001648 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001649 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001650 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001651
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001652 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001653 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001654 final NativeDaemonEvent[] events = mConnector.executeForList(
1655 "bandwidth", "gettetherstats");
1656 for (NativeDaemonEvent event : events) {
1657 if (event.getCode() != TetheringStatsListResult) continue;
1658
1659 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1660 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1661 try {
1662 final String ifaceIn = tok.nextToken();
1663 final String ifaceOut = tok.nextToken();
1664
1665 final NetworkStats.Entry entry = new NetworkStats.Entry();
1666 entry.iface = ifaceOut;
1667 entry.uid = UID_TETHERING;
1668 entry.set = SET_DEFAULT;
1669 entry.tag = TAG_NONE;
1670 entry.rxBytes = Long.parseLong(tok.nextToken());
1671 entry.rxPackets = Long.parseLong(tok.nextToken());
1672 entry.txBytes = Long.parseLong(tok.nextToken());
1673 entry.txPackets = Long.parseLong(tok.nextToken());
1674 stats.combineValues(entry);
1675 } catch (NoSuchElementException e) {
1676 throw new IllegalStateException("problem parsing tethering stats: " + event);
1677 } catch (NumberFormatException e) {
1678 throw new IllegalStateException("problem parsing tethering stats: " + event);
1679 }
1680 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001681 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001682 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001683 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001684 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001685 }
1686
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001687 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001688 public void setDnsServersForNetwork(int netId, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001689 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001690
Paul Jensen13e817d2014-04-10 14:16:37 -04001691 final Command cmd = new Command("resolver", "setnetdns", netId,
Robert Greenwalt8058f622012-11-09 10:52:27 -08001692 (domains == null ? "" : domains));
1693
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001694 for (String s : servers) {
1695 InetAddress a = NetworkUtils.numericToInetAddress(s);
1696 if (a.isAnyLocalAddress() == false) {
1697 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001698 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001699 }
1700
1701 try {
1702 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001703 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001704 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001705 }
1706 }
1707
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001708 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001709 public void addVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001710 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001711 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1712 argv[0] = "users";
1713 argv[1] = "add";
1714 argv[2] = netId;
1715 int argc = 3;
1716 // Avoid overly long commands by limiting number of UID ranges per command.
1717 for (int i = 0; i < ranges.length; i++) {
1718 argv[argc++] = ranges[i].toString();
1719 if (i == (ranges.length - 1) || argc == argv.length) {
1720 try {
1721 mConnector.execute("network", Arrays.copyOf(argv, argc));
1722 } catch (NativeDaemonConnectorException e) {
1723 throw e.rethrowAsParcelableException();
1724 }
1725 argc = 3;
1726 }
Chad Brubaker3277620a2013-06-12 13:37:30 -07001727 }
1728 }
1729
1730 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001731 public void removeVpnUidRanges(int netId, UidRange[] ranges) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001732 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001733 Object[] argv = new Object[3 + MAX_UID_RANGES_PER_COMMAND];
1734 argv[0] = "users";
1735 argv[1] = "remove";
1736 argv[2] = netId;
1737 int argc = 3;
1738 // Avoid overly long commands by limiting number of UID ranges per command.
1739 for (int i = 0; i < ranges.length; i++) {
1740 argv[argc++] = ranges[i].toString();
1741 if (i == (ranges.length - 1) || argc == argv.length) {
1742 try {
1743 mConnector.execute("network", Arrays.copyOf(argv, argc));
1744 } catch (NativeDaemonConnectorException e) {
1745 throw e.rethrowAsParcelableException();
1746 }
1747 argc = 3;
1748 }
Chad Brubakercca54c42013-06-27 17:41:38 -07001749 }
1750 }
1751
1752 @Override
1753 public int getMarkForUid(int uid) {
1754 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1755 final NativeDaemonEvent event;
1756 try {
1757 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1758 } catch (NativeDaemonConnectorException e) {
1759 throw e.rethrowAsParcelableException();
1760 }
1761 event.checkCode(GetMarkResult);
1762 return Integer.parseInt(event.getMessage());
1763 }
1764
1765 @Override
1766 public int getMarkForProtect() {
1767 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1768 final NativeDaemonEvent event;
1769 try {
1770 event = mConnector.execute("interface", "fwmark", "get", "protect");
1771 } catch (NativeDaemonConnectorException e) {
1772 throw e.rethrowAsParcelableException();
1773 }
1774 event.checkCode(GetMarkResult);
1775 return Integer.parseInt(event.getMessage());
1776 }
1777
1778 @Override
1779 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1780 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1781 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001782 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001783 mConnector.execute("interface", "fwmark", "route", "add", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001784 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubakercca54c42013-06-27 17:41:38 -07001785 } catch (NativeDaemonConnectorException e) {
1786 throw e.rethrowAsParcelableException();
1787 }
1788 }
1789
1790 @Override
1791 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1792 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1793 try {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001794 LinkAddress dest = route.getDestinationLinkAddress();
Chad Brubakercca54c42013-06-27 17:41:38 -07001795 mConnector.execute("interface", "fwmark", "route", "remove", iface,
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09001796 dest.getAddress().getHostAddress(), dest.getPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001797 } catch (NativeDaemonConnectorException e) {
1798 throw e.rethrowAsParcelableException();
1799 }
1800 }
1801
1802 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001803 public void setHostExemption(LinkAddress host) {
1804 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1805 try {
1806 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1807 } catch (NativeDaemonConnectorException e) {
1808 throw e.rethrowAsParcelableException();
1809 }
1810 }
1811
1812 @Override
1813 public void clearHostExemption(LinkAddress host) {
1814 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1815 try {
1816 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1817 } catch (NativeDaemonConnectorException e) {
1818 throw e.rethrowAsParcelableException();
1819 }
1820 }
1821
1822 @Override
Paul Jensen13e817d2014-04-10 14:16:37 -04001823 public void flushNetworkDnsCache(int netId) {
Chad Brubaker3277620a2013-06-12 13:37:30 -07001824 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1825 try {
Paul Jensen13e817d2014-04-10 14:16:37 -04001826 mConnector.execute("resolver", "flushnet", netId);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001827 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001828 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001829 }
1830 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001831
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001832 @Override
1833 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001834 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001835 try {
1836 mConnector.execute("firewall", enabled ? "enable" : "disable");
1837 mFirewallEnabled = enabled;
1838 } catch (NativeDaemonConnectorException e) {
1839 throw e.rethrowAsParcelableException();
1840 }
1841 }
1842
1843 @Override
1844 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001845 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001846 return mFirewallEnabled;
1847 }
1848
1849 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001850 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001851 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001852 Preconditions.checkState(mFirewallEnabled);
1853 final String rule = allow ? ALLOW : DENY;
1854 try {
1855 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1856 } catch (NativeDaemonConnectorException e) {
1857 throw e.rethrowAsParcelableException();
1858 }
1859 }
1860
1861 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001862 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001863 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001864 Preconditions.checkState(mFirewallEnabled);
1865 final String rule = allow ? ALLOW : DENY;
1866 try {
1867 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1868 } catch (NativeDaemonConnectorException e) {
1869 throw e.rethrowAsParcelableException();
1870 }
1871 }
1872
1873 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001874 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001875 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001876 Preconditions.checkState(mFirewallEnabled);
1877 final String rule = allow ? ALLOW : DENY;
1878 try {
1879 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1880 } catch (NativeDaemonConnectorException e) {
1881 throw e.rethrowAsParcelableException();
1882 }
1883 }
1884
1885 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001886 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001887 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001888 Preconditions.checkState(mFirewallEnabled);
1889 final String rule = allow ? ALLOW : DENY;
1890 try {
1891 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1892 } catch (NativeDaemonConnectorException e) {
1893 throw e.rethrowAsParcelableException();
1894 }
1895 }
1896
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001897 private static void enforceSystemUid() {
1898 final int uid = Binder.getCallingUid();
1899 if (uid != Process.SYSTEM_UID) {
1900 throw new SecurityException("Only available to AID_SYSTEM");
1901 }
1902 }
1903
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001904 @Override
Lorenzo Colitti79751842013-02-28 16:16:03 +09001905 public void startClatd(String interfaceName) throws IllegalStateException {
1906 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1907
1908 try {
1909 mConnector.execute("clatd", "start", interfaceName);
1910 } catch (NativeDaemonConnectorException e) {
1911 throw e.rethrowAsParcelableException();
1912 }
1913 }
1914
1915 @Override
1916 public void stopClatd() throws IllegalStateException {
1917 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1918
1919 try {
1920 mConnector.execute("clatd", "stop");
1921 } catch (NativeDaemonConnectorException e) {
1922 throw e.rethrowAsParcelableException();
1923 }
1924 }
1925
1926 @Override
1927 public boolean isClatdStarted() {
1928 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1929
1930 final NativeDaemonEvent event;
1931 try {
1932 event = mConnector.execute("clatd", "status");
1933 } catch (NativeDaemonConnectorException e) {
1934 throw e.rethrowAsParcelableException();
1935 }
1936
1937 event.checkCode(ClatdStatusResult);
1938 return event.getMessage().endsWith("started");
1939 }
1940
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001941 @Override
1942 public void registerNetworkActivityListener(INetworkActivityListener listener) {
1943 mNetworkActivityListeners.register(listener);
1944 }
1945
1946 @Override
1947 public void unregisterNetworkActivityListener(INetworkActivityListener listener) {
1948 mNetworkActivityListeners.unregister(listener);
1949 }
1950
1951 @Override
1952 public boolean isNetworkActive() {
1953 synchronized (mNetworkActivityListeners) {
1954 return mNetworkActive || mActiveIdleTimers.isEmpty();
1955 }
1956 }
1957
1958 private void reportNetworkActive() {
1959 final int length = mNetworkActivityListeners.beginBroadcast();
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001960 try {
1961 for (int i = 0; i < length; i++) {
1962 try {
1963 mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
1964 } catch (RemoteException e) {
1965 } catch (RuntimeException e) {
1966 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001967 }
Robert Greenwalt2c9f5472014-04-21 14:50:28 -07001968 } finally {
1969 mNetworkActivityListeners.finishBroadcast();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001970 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001971 }
1972
Mattias Falk8b47b362011-08-23 14:15:13 +02001973 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001974 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001975 public void monitor() {
1976 if (mConnector != null) {
1977 mConnector.monitor();
1978 }
1979 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001980
1981 @Override
1982 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1983 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1984
Robert Greenwalt470fd722012-01-18 12:51:15 -08001985 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1986 mConnector.dump(fd, pw, args);
1987 pw.println();
1988
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001989 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001990 pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
1991 pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
1992 pw.print("mNetworkActive="); pw.println(mNetworkActive);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001993
1994 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001995 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1996 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001997 }
1998
1999 synchronized (mUidRejectOnQuota) {
2000 pw.print("UID reject on quota ifaces: [");
2001 final int size = mUidRejectOnQuota.size();
2002 for (int i = 0; i < size; i++) {
2003 pw.print(mUidRejectOnQuota.keyAt(i));
2004 if (i < size - 1) pw.print(",");
2005 }
2006 pw.println("]");
2007 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002008
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002009 synchronized (mIdleTimerLock) {
2010 pw.println("Idle timers:");
2011 for (HashMap.Entry<String, IdleTimerParams> ent : mActiveIdleTimers.entrySet()) {
2012 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
2013 IdleTimerParams params = ent.getValue();
2014 pw.print(" timeout="); pw.print(params.timeout);
2015 pw.print(" type="); pw.print(params.type);
2016 pw.print(" networkCount="); pw.println(params.networkCount);
2017 }
2018 }
2019
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07002020 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07002021 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002022
Robert Greenwalt568891d2014-04-04 13:38:00 -07002023 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002024 public void createPhysicalNetwork(int netId) {
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2026
2027 try {
Paul Jensen992f2522014-04-28 10:33:11 -04002028 mConnector.execute("network", "create", netId);
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002029 } catch (NativeDaemonConnectorException e) {
2030 throw e.rethrowAsParcelableException();
2031 }
2032 }
2033
Robert Greenwalt568891d2014-04-04 13:38:00 -07002034 @Override
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002035 public void createVirtualNetwork(int netId, boolean hasDNS) {
2036 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2037
2038 try {
2039 mConnector.execute("network", "create", netId, "vpn", hasDNS ? "1" : "0");
2040 } catch (NativeDaemonConnectorException e) {
2041 throw e.rethrowAsParcelableException();
2042 }
2043 }
2044
2045 @Override
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07002046 public void removeNetwork(int netId) {
2047 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2048
2049 try {
2050 mConnector.execute("network", "destroy", netId);
2051 } catch (NativeDaemonConnectorException e) {
2052 throw e.rethrowAsParcelableException();
2053 }
2054 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002055
2056 @Override
Paul Jensen992f2522014-04-28 10:33:11 -04002057 public void addInterfaceToNetwork(String iface, int netId) {
2058 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2059
2060 try {
2061 mConnector.execute("network", "addiface", netId, iface);
2062 } catch (NativeDaemonConnectorException e) {
2063 throw e.rethrowAsParcelableException();
2064 }
2065 }
2066
2067 @Override
2068 public void removeInterfaceFromNetwork(String iface, int netId) {
2069 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2070
2071 try {
2072 mConnector.execute("network", "removeiface", netId, iface);
2073 } catch (NativeDaemonConnectorException e) {
2074 throw e.rethrowAsParcelableException();
2075 }
2076 }
2077
2078 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002079 public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2080 modifyLegacyRouteForNetId(netId, routeInfo, uid, ADD);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002081 }
2082
2083 @Override
Robert Greenwalt913c8952014-04-07 17:36:35 -07002084 public void removeLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
2085 modifyLegacyRouteForNetId(netId, routeInfo, uid, REMOVE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002086 }
2087
Robert Greenwalt913c8952014-04-07 17:36:35 -07002088 private void modifyLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid, String action) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002089 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2090
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002091 final Command cmd = new Command("network", "route", "legacy", uid, action, netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002092
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002093 // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07002094 final LinkAddress la = routeInfo.getDestinationLinkAddress();
Robert Greenwalt568891d2014-04-04 13:38:00 -07002095 cmd.appendArg(routeInfo.getInterface());
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +09002096 cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
Sreeram Ramachandran1fbcb272014-05-22 16:30:48 -07002097 if (routeInfo.hasGateway()) {
2098 cmd.appendArg(routeInfo.getGateway().getHostAddress());
2099 }
Robert Greenwalt568891d2014-04-04 13:38:00 -07002100
2101 try {
2102 mConnector.execute(cmd);
2103 } catch (NativeDaemonConnectorException e) {
2104 throw e.rethrowAsParcelableException();
2105 }
2106 }
2107
2108 @Override
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002109 public void setDefaultNetId(int netId) {
Robert Greenwalt568891d2014-04-04 13:38:00 -07002110 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2111
2112 try {
Sreeram Ramachandranf047f2a2014-04-15 16:04:26 -07002113 mConnector.execute("network", "default", "set", netId);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002114 } catch (NativeDaemonConnectorException e) {
2115 throw e.rethrowAsParcelableException();
2116 }
2117 }
2118
2119 @Override
2120 public void clearDefaultNetId() {
2121 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2122
2123 try {
2124 mConnector.execute("network", "default", "clear");
2125 } catch (NativeDaemonConnectorException e) {
2126 throw e.rethrowAsParcelableException();
2127 }
2128 }
2129
2130 @Override
2131 public void setPermission(boolean internal, boolean changeNetState, int[] uids) {
2132 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2133
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002134 final Command cmd = new Command("network", "permission", "user", "set");
2135 if (internal) cmd.appendArg(CONNECTIVITY_INTERNAL);
2136 if (changeNetState) cmd.appendArg(CHANGE_NETWORK_STATE);
Robert Greenwalt568891d2014-04-04 13:38:00 -07002137 for (int i=0; i<uids.length; i++) {
2138 cmd.appendArg(uids[i]);
2139 }
2140
2141 try {
2142 mConnector.execute(cmd);
2143 } catch (NativeDaemonConnectorException e) {
2144 throw e.rethrowAsParcelableException();
2145 }
2146 }
2147
2148 @Override
2149 public void clearPermission(int[] uids) {
2150 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2151
Sreeram Ramachandrane4bde7d2014-04-10 15:58:39 -07002152 final Command cmd = new Command("network", "permission", "user", "clear");
Robert Greenwalt568891d2014-04-04 13:38:00 -07002153 for (int i=0; i<uids.length; i++) {
2154 cmd.appendArg(uids[i]);
2155 }
2156
2157 try {
2158 mConnector.execute(cmd);
2159 } catch (NativeDaemonConnectorException e) {
2160 throw e.rethrowAsParcelableException();
2161 }
2162 }
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04002163
2164 @Override
2165 public void allowProtect(int uid) {
2166 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2167
2168 try {
2169 mConnector.execute("network", "protect", "allow", uid);
2170 } catch (NativeDaemonConnectorException e) {
2171 throw e.rethrowAsParcelableException();
2172 }
2173 }
2174
2175 @Override
2176 public void denyProtect(int uid) {
2177 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2178
2179 try {
2180 mConnector.execute("network", "protect", "deny", uid);
2181 } catch (NativeDaemonConnectorException e) {
2182 throw e.rethrowAsParcelableException();
2183 }
2184 }
2185
San Mehat873f2142010-01-14 10:25:07 -08002186}