blob: e6c5422a8fc119325086a4574e31342256060ba8 [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey4529bb62011-12-14 10:31:54 -080019import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070020import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070022import static android.net.NetworkStats.SET_DEFAULT;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import static android.net.NetworkStats.TAG_NONE;
24import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070025import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090026import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070027import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080028import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
29import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070034import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080035import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070036import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
San Mehat873f2142010-01-14 10:25:07 -080038import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080039import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070040import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080041import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070044import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080045import android.net.wifi.WifiConfiguration;
46import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070047import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070048import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070049import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080050import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080052import android.os.RemoteCallbackList;
53import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070054import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070055import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080056import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080057import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070059import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080060
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070061import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070063import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080064import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070065import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070066import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070067import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070068
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070069import java.io.BufferedReader;
70import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080071import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070072import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070073import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070074import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070075import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070076import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070077import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070078import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070079import java.net.InterfaceAddress;
80import java.net.NetworkInterface;
81import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070082import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070083import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070084import java.util.HashMap;
jiaguo1da35f72014-01-09 16:39:59 +080085import java.util.List;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070086import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070087import java.util.NoSuchElementException;
88import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070089import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080090
91/**
92 * @hide
93 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070094public class NetworkManagementService extends INetworkManagementService.Stub
95 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070096 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070097 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070098 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +090099 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -0700100
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800101 private static final String ADD = "add";
102 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700103
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700104 private static final String ALLOW = "allow";
105 private static final String DENY = "deny";
106
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700107 private static final String DEFAULT = "default";
108 private static final String SECONDARY = "secondary";
109
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700110 /**
111 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
112 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
113 */
114 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
115
San Mehat873f2142010-01-14 10:25:07 -0800116 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700117 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800118 public static final int InterfaceListResult = 110;
119 public static final int TetherInterfaceListResult = 111;
120 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800121 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700122 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800123
124 public static final int TetherStatusResult = 210;
125 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800126 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800127 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700128 public static final int InterfaceRxCounterResult = 216;
129 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700130 public static final int QuotaCounterResult = 220;
131 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800132 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900133 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700134 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800135
136 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700137 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700138 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900139 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900140 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800141 }
142
143 /**
144 * Binder context for this service
145 */
146 private Context mContext;
147
148 /**
149 * connector object for communicating with netd
150 */
151 private NativeDaemonConnector mConnector;
152
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700153 private final Handler mMainHandler = new Handler();
154
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700155 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700156 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700157
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800158 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
159 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800160
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700161 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
162
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700163 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700164 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700165 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700166 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700167 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700168 /** Set of UIDs with active reject rules. */
169 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
170
Haoyu Bai04124232012-06-28 15:26:19 -0700171 private Object mIdleTimerLock = new Object();
172 /** Set of interfaces with active idle timers. */
173 private static class IdleTimerParams {
174 public final int timeout;
175 public final String label;
176 public int networkCount;
177
178 IdleTimerParams(int timeout, String label) {
179 this.timeout = timeout;
180 this.label = label;
181 this.networkCount = 1;
182 }
183 }
184 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
185
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700186 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700187 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700188
San Mehat873f2142010-01-14 10:25:07 -0800189 /**
190 * Constructs a new NetworkManagementService instance
191 *
192 * @param context Binder context for this service
193 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900194 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800195 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800196
Marco Nelissen62dbb222010-02-18 10:56:30 -0800197 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
198 return;
199 }
200
San Mehat873f2142010-01-14 10:25:07 -0800201 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900202 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700203 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700204
205 // Add ourself to the Watchdog monitors.
206 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700207 }
208
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900209 static NetworkManagementService create(Context context,
210 String socket) throws InterruptedException {
211 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700212 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700213 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
214 service.mThread.start();
215 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700216 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700217 if (DBG) Slog.d(TAG, "Connected");
218 return service;
San Mehat873f2142010-01-14 10:25:07 -0800219 }
220
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900221 public static NetworkManagementService create(Context context) throws InterruptedException {
222 return create(context, NETD_SOCKET_NAME);
223 }
224
Jeff Sharkey350083e2011-06-29 10:45:16 -0700225 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700226 prepareNativeDaemon();
227 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700228 }
229
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800230 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800231 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800232 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800233 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800234 }
235
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800236 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800238 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800239 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800240 }
241
242 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700243 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800244 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700245 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800246 final int length = mObservers.beginBroadcast();
247 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800248 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800249 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
250 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900251 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700252 }
253 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800254 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700255 }
256
257 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700258 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700259 * (typically, an Ethernet cable has been plugged-in or unplugged).
260 */
261 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800262 final int length = mObservers.beginBroadcast();
263 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700264 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800265 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
266 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900267 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800268 }
269 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800270 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800271 }
272
273 /**
274 * Notify our observers of an interface addition.
275 */
276 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 final int length = mObservers.beginBroadcast();
278 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800279 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800280 mObservers.getBroadcastItem(i).interfaceAdded(iface);
281 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900282 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800283 }
284 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800286 }
287
288 /**
289 * Notify our observers of an interface removal.
290 */
291 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700292 // netd already clears out quota and alerts for removed ifaces; update
293 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700294 mActiveAlerts.remove(iface);
295 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700296
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 final int length = mObservers.beginBroadcast();
298 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800299 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
301 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900302 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800303 }
304 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800305 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800306 }
307
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700308 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700309 * Notify our observers of a limit reached.
310 */
311 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800312 final int length = mObservers.beginBroadcast();
313 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700314 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800315 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
316 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900317 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700318 }
319 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800320 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700321 }
322
323 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700324 * Notify our observers of a change in the data activity state of the interface
325 */
326 private void notifyInterfaceClassActivity(String label, boolean active) {
327 final int length = mObservers.beginBroadcast();
328 for (int i = 0; i < length; i++) {
329 try {
330 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
331 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900332 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700333 }
334 }
335 mObservers.finishBroadcast();
336 }
337
338 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700339 * Prepare native daemon once connected, enabling modules and pushing any
340 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700341 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700342 private void prepareNativeDaemon() {
343 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700344
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700345 // only enable bandwidth control when support exists
346 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
347 if (hasKernelSupport) {
348 Slog.d(TAG, "enabling bandwidth control");
349 try {
350 mConnector.execute("bandwidth", "enable");
351 mBandwidthControlEnabled = true;
352 } catch (NativeDaemonConnectorException e) {
353 Log.wtf(TAG, "problem enabling bandwidth controls", e);
354 }
355 } else {
356 Slog.d(TAG, "not enabling bandwidth control");
357 }
358
359 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
360
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700361 if (mBandwidthControlEnabled) {
362 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700363 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700364 .noteNetworkStatsEnabled();
365 } catch (RemoteException e) {
366 }
367 }
368
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700369 // push any existing quota or UID rules
370 synchronized (mQuotaLock) {
371 int size = mActiveQuotas.size();
372 if (size > 0) {
373 Slog.d(TAG, "pushing " + size + " active quota rules");
374 final HashMap<String, Long> activeQuotas = mActiveQuotas;
375 mActiveQuotas = Maps.newHashMap();
376 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
377 setInterfaceQuota(entry.getKey(), entry.getValue());
378 }
379 }
380
381 size = mActiveAlerts.size();
382 if (size > 0) {
383 Slog.d(TAG, "pushing " + size + " active alert rules");
384 final HashMap<String, Long> activeAlerts = mActiveAlerts;
385 mActiveAlerts = Maps.newHashMap();
386 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
387 setInterfaceAlert(entry.getKey(), entry.getValue());
388 }
389 }
390
391 size = mUidRejectOnQuota.size();
392 if (size > 0) {
393 Slog.d(TAG, "pushing " + size + " active uid rules");
394 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
395 mUidRejectOnQuota = new SparseBooleanArray();
396 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
397 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
398 }
399 }
400 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700401
402 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700403 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700404 }
San Mehat4d02d002010-01-22 16:07:46 -0800405
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900406 /**
407 * Notify our observers of a new or updated interface address.
408 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900409 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900410 final int length = mObservers.beginBroadcast();
411 for (int i = 0; i < length; i++) {
412 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900413 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900414 } catch (RemoteException e) {
415 } catch (RuntimeException e) {
416 }
417 }
418 mObservers.finishBroadcast();
419 }
420
421 /**
422 * Notify our observers of a deleted interface address.
423 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900424 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900425 final int length = mObservers.beginBroadcast();
426 for (int i = 0; i < length; i++) {
427 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900428 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900429 } catch (RemoteException e) {
430 } catch (RuntimeException e) {
431 }
432 }
433 mObservers.finishBroadcast();
434 }
435
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900436 /**
437 * Notify our observers of DNS server information received.
438 */
439 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
440 final int length = mObservers.beginBroadcast();
441 for (int i = 0; i < length; i++) {
442 try {
443 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime, addresses);
444 } catch (RemoteException e) {
445 } catch (RuntimeException e) {
446 }
447 }
448 mObservers.finishBroadcast();
449 }
450
San Mehat873f2142010-01-14 10:25:07 -0800451 //
452 // Netd Callback handling
453 //
454
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700455 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
456 @Override
San Mehat873f2142010-01-14 10:25:07 -0800457 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700458 // event is dispatched from internal NDC thread, so we prepare the
459 // daemon back on main thread.
460 if (mConnectedSignal != null) {
461 mConnectedSignal.countDown();
462 mConnectedSignal = null;
463 } else {
464 mMainHandler.post(new Runnable() {
465 @Override
466 public void run() {
467 prepareNativeDaemon();
468 }
469 });
470 }
San Mehat873f2142010-01-14 10:25:07 -0800471 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700472
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700473 @Override
San Mehat873f2142010-01-14 10:25:07 -0800474 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900475 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700476 switch (code) {
477 case NetdResponseCode.InterfaceChange:
478 /*
479 * a network interface change occured
480 * Format: "NNN Iface added <name>"
481 * "NNN Iface removed <name>"
482 * "NNN Iface changed <name> <up/down>"
483 * "NNN Iface linkstatus <name> <up/down>"
484 */
485 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900486 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700487 }
488 if (cooked[2].equals("added")) {
489 notifyInterfaceAdded(cooked[3]);
490 return true;
491 } else if (cooked[2].equals("removed")) {
492 notifyInterfaceRemoved(cooked[3]);
493 return true;
494 } else if (cooked[2].equals("changed") && cooked.length == 5) {
495 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
496 return true;
497 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
498 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
499 return true;
500 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900501 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700502 // break;
503 case NetdResponseCode.BandwidthControl:
504 /*
505 * Bandwidth control needs some attention
506 * Format: "NNN limit alert <alertName> <ifaceName>"
507 */
508 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900509 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700510 }
511 if (cooked[2].equals("alert")) {
512 notifyLimitReached(cooked[3], cooked[4]);
513 return true;
514 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900515 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700516 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700517 case NetdResponseCode.InterfaceClassActivity:
518 /*
519 * An network interface class state changed (active/idle)
520 * Format: "NNN IfaceClass <active/idle> <label>"
521 */
522 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900523 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700524 }
525 boolean isActive = cooked[2].equals("active");
526 notifyInterfaceClassActivity(cooked[3], isActive);
527 return true;
528 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900529 case NetdResponseCode.InterfaceAddressChange:
530 /*
531 * A network address change occurred
532 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
533 * "NNN Address removed <addr> <iface> <flags> <scope>"
534 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900535 if (cooked.length < 7 || !cooked[1].equals("Address")) {
536 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900537 }
538
Lorenzo Colitti64483942013-11-15 18:43:52 +0900539 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900540 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900541 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900542 int flags = Integer.parseInt(cooked[5]);
543 int scope = Integer.parseInt(cooked[6]);
544 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900545 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
546 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900547 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900548 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900549 }
550
551 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900552 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900553 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900554 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900555 }
556 return true;
557 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900558 case NetdResponseCode.InterfaceDnsServerInfo:
559 /*
560 * Information about available DNS servers has been received.
561 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
562 */
563 long lifetime; // Actually a 32-bit unsigned integer.
564
565 if (cooked.length == 6 &&
566 cooked[1].equals("DnsInfo") &&
567 cooked[2].equals("servers")) {
568 try {
569 lifetime = Long.parseLong(cooked[4]);
570 } catch (NumberFormatException e) {
571 throw new IllegalStateException(errorMessage);
572 }
573 String[] servers = cooked[5].split(",");
574 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
575 }
576 return true;
577 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700578 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800579 }
580 return false;
San Mehat873f2142010-01-14 10:25:07 -0800581 }
582 }
583
San Mehated4fc8a2010-01-22 12:28:36 -0800584
San Mehat873f2142010-01-14 10:25:07 -0800585 //
586 // INetworkManagementService members
587 //
588
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800589 @Override
590 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800591 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700592 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800593 return NativeDaemonEvent.filterMessageList(
594 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700595 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800596 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700597 }
San Mehated4fc8a2010-01-22 12:28:36 -0800598 }
599
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800600 @Override
601 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800602 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800603
604 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700605 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800606 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700607 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800608 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700609 }
San Mehated4fc8a2010-01-22 12:28:36 -0800610
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800611 event.checkCode(InterfaceGetCfgResult);
612
613 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
614 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800615
Kenny Roota80ce062010-06-01 13:23:53 -0700616 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800617 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700618 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800619 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800620 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800621 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700622 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800623 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800624 } catch (IllegalArgumentException iae) {
625 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700626 }
627
628 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800629 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800630 } catch (NumberFormatException nfe) {
631 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700632 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800633
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800634 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
635 while (st.hasMoreTokens()) {
636 cfg.setFlag(st.nextToken());
637 }
Kenny Roota80ce062010-06-01 13:23:53 -0700638 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800639 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800640 }
San Mehated4fc8a2010-01-22 12:28:36 -0800641 return cfg;
642 }
643
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800644 @Override
645 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800646 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800647 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800648 if (linkAddr == null || linkAddr.getAddress() == null) {
649 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800650 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800651
652 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800653 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800654 linkAddr.getNetworkPrefixLength());
655 for (String flag : cfg.getFlags()) {
656 cmd.appendArg(flag);
657 }
658
Kenny Roota80ce062010-06-01 13:23:53 -0700659 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800660 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700661 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800662 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700663 }
San Mehat873f2142010-01-14 10:25:07 -0800664 }
665
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800666 @Override
667 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800668 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800669 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800670 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800671 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700672 }
673
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800674 @Override
675 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800676 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800677 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800678 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800679 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700680 }
681
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800682 @Override
683 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700685 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800686 mConnector.execute(
687 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700688 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800689 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700690 }
691 }
692
Irfan Sherifff5600612011-06-16 10:26:28 -0700693 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
694 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800695 @Override
696 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800697 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700698 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800699 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700700 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800701 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700702 }
703 }
704
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800705 @Override
706 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800707 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700708 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800709 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700710 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800711 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700712 }
713 }
714
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800715 @Override
716 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700718 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800719 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700720 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800721 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700722 }
723 }
724
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800725 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700726 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800727 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700728 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700729 }
730
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800731 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700732 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700734 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700735 }
736
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800737 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700738 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800739 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700740 modifyRoute(interfaceName, ADD, route, SECONDARY);
741 }
742
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800743 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700744 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800745 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700746 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
747 }
748
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800749 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
750 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700751
752 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800753 final LinkAddress la = route.getDestination();
754 cmd.appendArg(la.getAddress().getHostAddress());
755 cmd.appendArg(la.getNetworkPrefixLength());
756
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700757 if (route.getGateway() == null) {
758 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800759 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700760 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800761 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700762 }
763 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800764 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700765 }
766
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800767 try {
768 mConnector.execute(cmd);
769 } catch (NativeDaemonConnectorException e) {
770 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700771 }
772 }
773
774 private ArrayList<String> readRouteList(String filename) {
775 FileInputStream fstream = null;
776 ArrayList<String> list = new ArrayList<String>();
777
778 try {
779 fstream = new FileInputStream(filename);
780 DataInputStream in = new DataInputStream(fstream);
781 BufferedReader br = new BufferedReader(new InputStreamReader(in));
782 String s;
783
784 // throw away the title line
785
786 while (((s = br.readLine()) != null) && (s.length() != 0)) {
787 list.add(s);
788 }
789 } catch (IOException ex) {
790 // return current list, possibly empty
791 } finally {
792 if (fstream != null) {
793 try {
794 fstream.close();
795 } catch (IOException ex) {}
796 }
797 }
798
799 return list;
800 }
801
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800802 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700803 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800804 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700805 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
806
807 // v4 routes listed as:
808 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
809 for (String s : readRouteList("/proc/net/route")) {
810 String[] fields = s.split("\t");
811
812 if (fields.length > 7) {
813 String iface = fields[0];
814
815 if (interfaceName.equals(iface)) {
816 String dest = fields[1];
817 String gate = fields[2];
818 String flags = fields[3]; // future use?
819 String mask = fields[7];
820 try {
821 // address stored as a hex string, ex: 0014A8C0
822 InetAddress destAddr =
823 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
824 int prefixLength =
825 NetworkUtils.netmaskIntToPrefixLength(
826 (int)Long.parseLong(mask, 16));
827 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
828
829 // address stored as a hex string, ex 0014A8C0
830 InetAddress gatewayAddr =
831 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
832
Wink Saville7b5fd052013-03-15 05:07:04 +0000833 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700834 routes.add(route);
835 } catch (Exception e) {
836 Log.e(TAG, "Error parsing route " + s + " : " + e);
837 continue;
838 }
839 }
840 }
841 }
842
843 // v6 routes listed as:
844 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
845 for (String s : readRouteList("/proc/net/ipv6_route")) {
846 String[]fields = s.split("\\s+");
847 if (fields.length > 9) {
848 String iface = fields[9].trim();
849 if (interfaceName.equals(iface)) {
850 String dest = fields[0];
851 String prefix = fields[1];
852 String gate = fields[4];
853
854 try {
855 // prefix length stored as a hex string, ex 40
856 int prefixLength = Integer.parseInt(prefix, 16);
857
858 // address stored as a 32 char hex string
859 // ex fe800000000000000000000000000000
860 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
861 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
862
863 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
864
Wink Saville7b5fd052013-03-15 05:07:04 +0000865 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700866 routes.add(route);
867 } catch (Exception e) {
868 Log.e(TAG, "Error parsing route " + s + " : " + e);
869 continue;
870 }
871 }
872 }
873 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800874 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700875 }
876
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800877 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900878 public void setMtu(String iface, int mtu) {
879 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
880
881 final NativeDaemonEvent event;
882 try {
883 event = mConnector.execute("interface", "setmtu", iface, mtu);
884 } catch (NativeDaemonConnectorException e) {
885 throw e.rethrowAsParcelableException();
886 }
887 }
888
889 @Override
San Mehat873f2142010-01-14 10:25:07 -0800890 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800891 // TODO: remove from aidl if nobody calls externally
892 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800893
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800895 }
896
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800897 @Override
San Mehat873f2142010-01-14 10:25:07 -0800898 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800899 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800900
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800901 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700902 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700904 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800905 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700906 }
San Mehat873f2142010-01-14 10:25:07 -0800907
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 // 211 Forwarding enabled
909 event.checkCode(IpFwdStatusResult);
910 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800911 }
912
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800913 @Override
914 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800915 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800916 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800917 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800918 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800919 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800920 }
San Mehat873f2142010-01-14 10:25:07 -0800921 }
922
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800923 @Override
924 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800925 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700926 // cmd is "tether start first_start first_stop second_start second_stop ..."
927 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800928
929 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700930 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700932 }
Kenny Roota80ce062010-06-01 13:23:53 -0700933
934 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800935 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700936 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800937 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700938 }
San Mehat873f2142010-01-14 10:25:07 -0800939 }
940
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800941 @Override
942 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800943 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700944 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800945 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700946 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800947 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700948 }
San Mehat873f2142010-01-14 10:25:07 -0800949 }
950
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800951 @Override
952 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800953 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800954
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800955 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700956 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800957 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700958 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800959 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700960 }
San Mehat873f2142010-01-14 10:25:07 -0800961
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800962 // 210 Tethering services started
963 event.checkCode(TetherStatusResult);
964 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800965 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700966
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800967 @Override
968 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800969 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700970 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800971 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700972 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800973 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700974 }
San Mehat873f2142010-01-14 10:25:07 -0800975 }
976
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800977 @Override
San Mehat873f2142010-01-14 10:25:07 -0800978 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800979 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700980 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800981 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700982 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800983 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700984 }
San Mehat873f2142010-01-14 10:25:07 -0800985 }
986
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800987 @Override
988 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800989 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700990 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800991 return NativeDaemonEvent.filterMessageList(
992 mConnector.executeForList("tether", "interface", "list"),
993 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700994 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800995 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700996 }
San Mehat873f2142010-01-14 10:25:07 -0800997 }
998
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800999 @Override
1000 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001001 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001002
1003 final Command cmd = new Command("tether", "dns", "set");
1004 for (String s : dns) {
1005 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1006 }
1007
San Mehat873f2142010-01-14 10:25:07 -08001008 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001009 mConnector.execute(cmd);
1010 } catch (NativeDaemonConnectorException e) {
1011 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001012 }
1013 }
1014
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001015 @Override
1016 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001017 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001018 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001019 return NativeDaemonEvent.filterMessageList(
1020 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001021 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001022 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001023 }
San Mehat873f2142010-01-14 10:25:07 -08001024 }
1025
jiaguo1da35f72014-01-09 16:39:59 +08001026 private List<InterfaceAddress> excludeLinkLocal(List<InterfaceAddress> addresses) {
1027 ArrayList<InterfaceAddress> filtered = new ArrayList<InterfaceAddress>(addresses.size());
1028 for (InterfaceAddress ia : addresses) {
1029 if (!ia.getAddress().isLinkLocalAddress())
1030 filtered.add(ia);
1031 }
1032 return filtered;
1033 }
1034
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001035 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001036 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001037 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001038
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001039 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1040 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001041 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001042 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001043 } else {
jiaguo1da35f72014-01-09 16:39:59 +08001044 // Don't touch link-local routes, as link-local addresses aren't routable,
1045 // kernel creates link-local routes on all interfaces automatically
1046 List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(
1047 internalNetworkInterface.getInterfaceAddresses());
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001048 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001049 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001050 InetAddress addr = NetworkUtils.getNetworkPart(
1051 ia.getAddress(), ia.getNetworkPrefixLength());
1052 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001053 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001054 }
1055
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001056 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001057 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001058 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001059 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001060 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001061 }
1062
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001063 @Override
1064 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001065 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001066 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001067 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001068 } catch (SocketException e) {
1069 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001070 }
San Mehat873f2142010-01-14 10:25:07 -08001071 }
1072
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001073 @Override
1074 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001075 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001076 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001077 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001078 } catch (SocketException e) {
1079 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001080 }
San Mehat873f2142010-01-14 10:25:07 -08001081 }
San Mehat72759df2010-01-19 13:50:37 -08001082
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001083 @Override
1084 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001085 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001086 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001087 return NativeDaemonEvent.filterMessageList(
1088 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001089 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001090 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001091 }
San Mehat72759df2010-01-19 13:50:37 -08001092 }
1093
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001094 @Override
1095 public void attachPppd(
1096 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001098 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001099 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001100 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1101 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1102 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001103 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001104 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001105 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001106 }
1107 }
1108
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001109 @Override
1110 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001111 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001112 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001113 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001114 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001115 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001116 }
San Mehat72759df2010-01-19 13:50:37 -08001117 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001118
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001119 @Override
1120 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001121 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001122 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001123 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001124 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001125 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001126 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001127 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001128 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001129 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001130 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001131 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001132 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001133 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001134 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001135 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001136 }
1137
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001138 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001139 switch (wifiConfig.getAuthType()) {
1140 case KeyMgmt.WPA_PSK:
1141 return "wpa-psk";
1142 case KeyMgmt.WPA2_PSK:
1143 return "wpa2-psk";
1144 default:
1145 return "open";
1146 }
1147 }
1148
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001149 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001150 @Override
1151 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001152 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001153 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001154 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001155 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001156 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001157 }
1158 }
1159
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001160 @Override
1161 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001163 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001164 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001165 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001166 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001167 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001168 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001169 }
1170
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001171 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001172 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001173 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001174 try {
1175 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001176 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001177 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001178 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001179 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001180 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001181 }
1182 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001183 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001184 }
1185 }
San Mehat91cac642010-03-31 14:31:36 -07001186
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001187 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001188 public void addIdleTimer(String iface, int timeout, String label) {
1189 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1190
1191 if (DBG) Slog.d(TAG, "Adding idletimer");
1192
1193 synchronized (mIdleTimerLock) {
1194 IdleTimerParams params = mActiveIdleTimers.get(iface);
1195 if (params != null) {
1196 // the interface already has idletimer, update network count
1197 params.networkCount++;
1198 return;
1199 }
1200
1201 try {
1202 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1203 } catch (NativeDaemonConnectorException e) {
1204 throw e.rethrowAsParcelableException();
1205 }
1206 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1207 }
1208 }
1209
1210 @Override
1211 public void removeIdleTimer(String iface) {
1212 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1213
1214 if (DBG) Slog.d(TAG, "Removing idletimer");
1215
1216 synchronized (mIdleTimerLock) {
1217 IdleTimerParams params = mActiveIdleTimers.get(iface);
1218 if (params == null || --(params.networkCount) > 0) {
1219 return;
1220 }
1221
1222 try {
1223 mConnector.execute("idletimer", "remove", iface,
1224 Integer.toString(params.timeout), params.label);
1225 } catch (NativeDaemonConnectorException e) {
1226 throw e.rethrowAsParcelableException();
1227 }
1228 mActiveIdleTimers.remove(iface);
1229 }
1230 }
1231
1232 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001233 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001234 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001235 try {
1236 return mStatsFactory.readNetworkStatsSummaryDev();
1237 } catch (IOException e) {
1238 throw new IllegalStateException(e);
1239 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001240 }
1241
1242 @Override
1243 public NetworkStats getNetworkStatsSummaryXt() {
1244 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001245 try {
1246 return mStatsFactory.readNetworkStatsSummaryXt();
1247 } catch (IOException e) {
1248 throw new IllegalStateException(e);
1249 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001250 }
1251
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001252 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001253 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001254 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001255 try {
1256 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1257 } catch (IOException e) {
1258 throw new IllegalStateException(e);
1259 }
San Mehat91cac642010-03-31 14:31:36 -07001260 }
1261
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001262 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001263 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001264 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001265
Jeff Sharkey350083e2011-06-29 10:45:16 -07001266 // silently discard when control disabled
1267 // TODO: eventually migrate to be always enabled
1268 if (!mBandwidthControlEnabled) return;
1269
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001270 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001271 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001272 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001273 }
1274
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001275 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001276 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001277 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001278 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001279 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001280 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001281 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001282 }
1283 }
1284
1285 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001286 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001287 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001288
Jeff Sharkey350083e2011-06-29 10:45:16 -07001289 // silently discard when control disabled
1290 // TODO: eventually migrate to be always enabled
1291 if (!mBandwidthControlEnabled) return;
1292
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001293 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001294 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001295 // TODO: eventually consider throwing
1296 return;
1297 }
1298
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001299 mActiveQuotas.remove(iface);
1300 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001301
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001302 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001303 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001304 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001305 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001306 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001307 }
1308 }
1309 }
1310
1311 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001312 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001313 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001314
1315 // silently discard when control disabled
1316 // TODO: eventually migrate to be always enabled
1317 if (!mBandwidthControlEnabled) return;
1318
1319 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001320 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001321 throw new IllegalStateException("setting alert requires existing quota on iface");
1322 }
1323
1324 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001325 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001326 throw new IllegalStateException("iface " + iface + " already has alert");
1327 }
1328
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001329 try {
1330 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001331 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001332 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001333 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001334 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001335 }
1336 }
1337 }
1338
1339 @Override
1340 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001341 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001342
1343 // silently discard when control disabled
1344 // TODO: eventually migrate to be always enabled
1345 if (!mBandwidthControlEnabled) return;
1346
1347 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001348 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001349 // TODO: eventually consider throwing
1350 return;
1351 }
1352
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001353 try {
1354 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001355 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001356 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001357 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001358 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001359 }
1360 }
1361 }
1362
1363 @Override
1364 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001365 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001366
1367 // silently discard when control disabled
1368 // TODO: eventually migrate to be always enabled
1369 if (!mBandwidthControlEnabled) return;
1370
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001371 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001372 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001373 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001374 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001375 }
1376 }
1377
1378 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001379 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001380 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001381
Jeff Sharkey350083e2011-06-29 10:45:16 -07001382 // silently discard when control disabled
1383 // TODO: eventually migrate to be always enabled
1384 if (!mBandwidthControlEnabled) return;
1385
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001386 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001387 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1388 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1389 // TODO: eventually consider throwing
1390 return;
1391 }
1392
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001393 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001394 mConnector.execute("bandwidth",
1395 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001396 if (rejectOnQuotaInterfaces) {
1397 mUidRejectOnQuota.put(uid, true);
1398 } else {
1399 mUidRejectOnQuota.delete(uid);
1400 }
1401 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001402 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001403 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001404 }
1405 }
1406
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001407 @Override
1408 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001409 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001410 return mBandwidthControlEnabled;
1411 }
1412
1413 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001414 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001415 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001416 try {
1417 return mStatsFactory.readNetworkStatsDetail(uid);
1418 } catch (IOException e) {
1419 throw new IllegalStateException(e);
1420 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001421 }
1422
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001423 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001424 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001425 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001426
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001427 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001428 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001429 final NativeDaemonEvent[] events = mConnector.executeForList(
1430 "bandwidth", "gettetherstats");
1431 for (NativeDaemonEvent event : events) {
1432 if (event.getCode() != TetheringStatsListResult) continue;
1433
1434 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1435 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1436 try {
1437 final String ifaceIn = tok.nextToken();
1438 final String ifaceOut = tok.nextToken();
1439
1440 final NetworkStats.Entry entry = new NetworkStats.Entry();
1441 entry.iface = ifaceOut;
1442 entry.uid = UID_TETHERING;
1443 entry.set = SET_DEFAULT;
1444 entry.tag = TAG_NONE;
1445 entry.rxBytes = Long.parseLong(tok.nextToken());
1446 entry.rxPackets = Long.parseLong(tok.nextToken());
1447 entry.txBytes = Long.parseLong(tok.nextToken());
1448 entry.txPackets = Long.parseLong(tok.nextToken());
1449 stats.combineValues(entry);
1450 } catch (NoSuchElementException e) {
1451 throw new IllegalStateException("problem parsing tethering stats: " + event);
1452 } catch (NumberFormatException e) {
1453 throw new IllegalStateException("problem parsing tethering stats: " + event);
1454 }
1455 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001456 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001457 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001458 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001459 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001460 }
1461
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001462 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001463 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001464 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001465 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001466 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001467 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001468 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001469 }
1470 }
1471
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001472 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001473 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001475
Robert Greenwalt8058f622012-11-09 10:52:27 -08001476 final Command cmd = new Command("resolver", "setifdns", iface,
1477 (domains == null ? "" : domains));
1478
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001479 for (String s : servers) {
1480 InetAddress a = NetworkUtils.numericToInetAddress(s);
1481 if (a.isAnyLocalAddress() == false) {
1482 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001483 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001484 }
1485
1486 try {
1487 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001488 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001489 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001490 }
1491 }
1492
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001493 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001494 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1495 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1496 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001497 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001498 "uid", "add", iface, uid_start, uid_end);
1499 } catch (NativeDaemonConnectorException e) {
1500 throw e.rethrowAsParcelableException();
1501 }
1502 }
1503
1504 @Override
1505 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1506 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1507 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001508 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001509 "uid", "remove", iface, uid_start, uid_end);
1510 } catch (NativeDaemonConnectorException e) {
1511 throw e.rethrowAsParcelableException();
1512 }
1513 }
1514
1515 @Override
1516 public void setMarkedForwarding(String iface) {
1517 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1518 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001519 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001520 } catch (NativeDaemonConnectorException e) {
1521 throw e.rethrowAsParcelableException();
1522 }
1523 }
1524
1525 @Override
1526 public void clearMarkedForwarding(String iface) {
1527 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1528 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001529 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1530 } catch (NativeDaemonConnectorException e) {
1531 throw e.rethrowAsParcelableException();
1532 }
1533 }
1534
1535 @Override
1536 public int getMarkForUid(int uid) {
1537 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1538 final NativeDaemonEvent event;
1539 try {
1540 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1541 } catch (NativeDaemonConnectorException e) {
1542 throw e.rethrowAsParcelableException();
1543 }
1544 event.checkCode(GetMarkResult);
1545 return Integer.parseInt(event.getMessage());
1546 }
1547
1548 @Override
1549 public int getMarkForProtect() {
1550 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1551 final NativeDaemonEvent event;
1552 try {
1553 event = mConnector.execute("interface", "fwmark", "get", "protect");
1554 } catch (NativeDaemonConnectorException e) {
1555 throw e.rethrowAsParcelableException();
1556 }
1557 event.checkCode(GetMarkResult);
1558 return Integer.parseInt(event.getMessage());
1559 }
1560
1561 @Override
1562 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1563 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1564 try {
1565 LinkAddress dest = route.getDestination();
1566 mConnector.execute("interface", "fwmark", "route", "add", iface,
1567 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1568 } catch (NativeDaemonConnectorException e) {
1569 throw e.rethrowAsParcelableException();
1570 }
1571 }
1572
1573 @Override
1574 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1575 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1576 try {
1577 LinkAddress dest = route.getDestination();
1578 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1579 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001580 } catch (NativeDaemonConnectorException e) {
1581 throw e.rethrowAsParcelableException();
1582 }
1583 }
1584
1585 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001586 public void setHostExemption(LinkAddress host) {
1587 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1588 try {
1589 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1590 } catch (NativeDaemonConnectorException e) {
1591 throw e.rethrowAsParcelableException();
1592 }
1593 }
1594
1595 @Override
1596 public void clearHostExemption(LinkAddress host) {
1597 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1598 try {
1599 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1600 } catch (NativeDaemonConnectorException e) {
1601 throw e.rethrowAsParcelableException();
1602 }
1603 }
1604
1605 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001606 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1607 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1608 try {
1609 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1610 } catch (NativeDaemonConnectorException e) {
1611 throw e.rethrowAsParcelableException();
1612 }
1613 }
1614
1615 @Override
1616 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1617 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1618 try {
1619 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1620 } catch (NativeDaemonConnectorException e) {
1621 throw e.rethrowAsParcelableException();
1622 }
1623 }
1624
1625 @Override
1626 public void clearDnsInterfaceMaps() {
1627 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1628 try {
1629 mConnector.execute("resolver", "clearifacemapping");
1630 } catch (NativeDaemonConnectorException e) {
1631 throw e.rethrowAsParcelableException();
1632 }
1633 }
1634
1635
1636 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001637 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001638 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001639 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001640 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001641 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001642 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001643 }
1644 }
1645
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001646 @Override
1647 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001648 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001649 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001650 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001651 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001652 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001653 }
1654 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001655
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001656 @Override
1657 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001658 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001659 try {
1660 mConnector.execute("firewall", enabled ? "enable" : "disable");
1661 mFirewallEnabled = enabled;
1662 } catch (NativeDaemonConnectorException e) {
1663 throw e.rethrowAsParcelableException();
1664 }
1665 }
1666
1667 @Override
1668 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001669 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001670 return mFirewallEnabled;
1671 }
1672
1673 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001674 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001675 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001676 Preconditions.checkState(mFirewallEnabled);
1677 final String rule = allow ? ALLOW : DENY;
1678 try {
1679 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1680 } catch (NativeDaemonConnectorException e) {
1681 throw e.rethrowAsParcelableException();
1682 }
1683 }
1684
1685 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001686 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001687 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001688 Preconditions.checkState(mFirewallEnabled);
1689 final String rule = allow ? ALLOW : DENY;
1690 try {
1691 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1692 } catch (NativeDaemonConnectorException e) {
1693 throw e.rethrowAsParcelableException();
1694 }
1695 }
1696
1697 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001698 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001699 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001700 Preconditions.checkState(mFirewallEnabled);
1701 final String rule = allow ? ALLOW : DENY;
1702 try {
1703 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1704 } catch (NativeDaemonConnectorException e) {
1705 throw e.rethrowAsParcelableException();
1706 }
1707 }
1708
1709 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001710 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001711 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001712 Preconditions.checkState(mFirewallEnabled);
1713 final String rule = allow ? ALLOW : DENY;
1714 try {
1715 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1716 } catch (NativeDaemonConnectorException e) {
1717 throw e.rethrowAsParcelableException();
1718 }
1719 }
1720
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001721 private static void enforceSystemUid() {
1722 final int uid = Binder.getCallingUid();
1723 if (uid != Process.SYSTEM_UID) {
1724 throw new SecurityException("Only available to AID_SYSTEM");
1725 }
1726 }
1727
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001728 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001729 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1730 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1731 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001732 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001733 } catch (NativeDaemonConnectorException e) {
1734 throw new IllegalStateException(
1735 "Error communicating with native deamon to set interface for pid" + iface, e);
1736 }
1737 }
1738
1739 @Override
1740 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1741 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1742 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001743 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001744 } catch (NativeDaemonConnectorException e) {
1745 throw new IllegalStateException(
1746 "Error communicating with native deamon to clear interface for pid " + pid, e);
1747 }
1748 }
1749
Lorenzo Colitti79751842013-02-28 16:16:03 +09001750 @Override
1751 public void startClatd(String interfaceName) throws IllegalStateException {
1752 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1753
1754 try {
1755 mConnector.execute("clatd", "start", interfaceName);
1756 } catch (NativeDaemonConnectorException e) {
1757 throw e.rethrowAsParcelableException();
1758 }
1759 }
1760
1761 @Override
1762 public void stopClatd() throws IllegalStateException {
1763 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1764
1765 try {
1766 mConnector.execute("clatd", "stop");
1767 } catch (NativeDaemonConnectorException e) {
1768 throw e.rethrowAsParcelableException();
1769 }
1770 }
1771
1772 @Override
1773 public boolean isClatdStarted() {
1774 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1775
1776 final NativeDaemonEvent event;
1777 try {
1778 event = mConnector.execute("clatd", "status");
1779 } catch (NativeDaemonConnectorException e) {
1780 throw e.rethrowAsParcelableException();
1781 }
1782
1783 event.checkCode(ClatdStatusResult);
1784 return event.getMessage().endsWith("started");
1785 }
1786
Mattias Falk8b47b362011-08-23 14:15:13 +02001787 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001788 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001789 public void monitor() {
1790 if (mConnector != null) {
1791 mConnector.monitor();
1792 }
1793 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001794
1795 @Override
1796 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1797 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1798
Robert Greenwalt470fd722012-01-18 12:51:15 -08001799 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1800 mConnector.dump(fd, pw, args);
1801 pw.println();
1802
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001803 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1804
1805 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001806 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1807 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001808 }
1809
1810 synchronized (mUidRejectOnQuota) {
1811 pw.print("UID reject on quota ifaces: [");
1812 final int size = mUidRejectOnQuota.size();
1813 for (int i = 0; i < size; i++) {
1814 pw.print(mUidRejectOnQuota.keyAt(i));
1815 if (i < size - 1) pw.print(",");
1816 }
1817 pw.println("]");
1818 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001819
1820 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001821 }
San Mehat873f2142010-01-14 10:25:07 -08001822}