blob: 8089fbc2339f01f1caa962f90f95a1f591e0e361 [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;
85import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070086import java.util.NoSuchElementException;
87import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070088import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080089
90/**
91 * @hide
92 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070093public class NetworkManagementService extends INetworkManagementService.Stub
94 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070095 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070096 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070097 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +090098 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -070099
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800100 private static final String ADD = "add";
101 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700102
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700103 private static final String ALLOW = "allow";
104 private static final String DENY = "deny";
105
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700106 private static final String DEFAULT = "default";
107 private static final String SECONDARY = "secondary";
108
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700109 /**
110 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
111 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
112 */
113 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
114
San Mehat873f2142010-01-14 10:25:07 -0800115 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700116 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800117 public static final int InterfaceListResult = 110;
118 public static final int TetherInterfaceListResult = 111;
119 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800120 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700121 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800122
123 public static final int TetherStatusResult = 210;
124 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800125 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800126 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700127 public static final int InterfaceRxCounterResult = 216;
128 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700129 public static final int QuotaCounterResult = 220;
130 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800131 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900132 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700133 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800134
135 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700136 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700137 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900138 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900139 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800140 }
141
142 /**
143 * Binder context for this service
144 */
145 private Context mContext;
146
147 /**
148 * connector object for communicating with netd
149 */
150 private NativeDaemonConnector mConnector;
151
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700152 private final Handler mMainHandler = new Handler();
153
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700154 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700155 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700156
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800157 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
158 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800159
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700160 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
161
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700162 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700163 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700164 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700165 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700166 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700167 /** Set of UIDs with active reject rules. */
168 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
169
Haoyu Bai04124232012-06-28 15:26:19 -0700170 private Object mIdleTimerLock = new Object();
171 /** Set of interfaces with active idle timers. */
172 private static class IdleTimerParams {
173 public final int timeout;
174 public final String label;
175 public int networkCount;
176
177 IdleTimerParams(int timeout, String label) {
178 this.timeout = timeout;
179 this.label = label;
180 this.networkCount = 1;
181 }
182 }
183 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
184
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700185 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700186 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700187
San Mehat873f2142010-01-14 10:25:07 -0800188 /**
189 * Constructs a new NetworkManagementService instance
190 *
191 * @param context Binder context for this service
192 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900193 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800194 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800195
Marco Nelissen62dbb222010-02-18 10:56:30 -0800196 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
197 return;
198 }
199
San Mehat873f2142010-01-14 10:25:07 -0800200 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900201 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700202 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700203
204 // Add ourself to the Watchdog monitors.
205 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700206 }
207
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900208 static NetworkManagementService create(Context context,
209 String socket) throws InterruptedException {
210 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700211 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700212 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
213 service.mThread.start();
214 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700215 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700216 if (DBG) Slog.d(TAG, "Connected");
217 return service;
San Mehat873f2142010-01-14 10:25:07 -0800218 }
219
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900220 public static NetworkManagementService create(Context context) throws InterruptedException {
221 return create(context, NETD_SOCKET_NAME);
222 }
223
Jeff Sharkey350083e2011-06-29 10:45:16 -0700224 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700225 prepareNativeDaemon();
226 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700227 }
228
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800229 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800230 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800231 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800232 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800233 }
234
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800235 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800237 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800238 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800239 }
240
241 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800243 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700244 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800245 final int length = mObservers.beginBroadcast();
246 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800247 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800248 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
249 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900250 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700251 }
252 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800253 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700254 }
255
256 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700257 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700258 * (typically, an Ethernet cable has been plugged-in or unplugged).
259 */
260 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 final int length = mObservers.beginBroadcast();
262 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700263 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800264 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
265 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900266 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800267 }
268 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800270 }
271
272 /**
273 * Notify our observers of an interface addition.
274 */
275 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800276 final int length = mObservers.beginBroadcast();
277 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800278 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800279 mObservers.getBroadcastItem(i).interfaceAdded(iface);
280 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900281 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800282 }
283 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800284 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800285 }
286
287 /**
288 * Notify our observers of an interface removal.
289 */
290 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700291 // netd already clears out quota and alerts for removed ifaces; update
292 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700293 mActiveAlerts.remove(iface);
294 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700295
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800296 final int length = mObservers.beginBroadcast();
297 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800298 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800299 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
300 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900301 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800302 }
303 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800304 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800305 }
306
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700307 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700308 * Notify our observers of a limit reached.
309 */
310 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800311 final int length = mObservers.beginBroadcast();
312 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700313 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800314 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
315 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900316 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700317 }
318 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800319 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700320 }
321
322 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700323 * Notify our observers of a change in the data activity state of the interface
324 */
325 private void notifyInterfaceClassActivity(String label, boolean active) {
326 final int length = mObservers.beginBroadcast();
327 for (int i = 0; i < length; i++) {
328 try {
329 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
330 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900331 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700332 }
333 }
334 mObservers.finishBroadcast();
335 }
336
337 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700338 * Prepare native daemon once connected, enabling modules and pushing any
339 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700340 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700341 private void prepareNativeDaemon() {
342 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700343
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700344 // only enable bandwidth control when support exists
345 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
346 if (hasKernelSupport) {
347 Slog.d(TAG, "enabling bandwidth control");
348 try {
349 mConnector.execute("bandwidth", "enable");
350 mBandwidthControlEnabled = true;
351 } catch (NativeDaemonConnectorException e) {
352 Log.wtf(TAG, "problem enabling bandwidth controls", e);
353 }
354 } else {
355 Slog.d(TAG, "not enabling bandwidth control");
356 }
357
358 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
359
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700360 if (mBandwidthControlEnabled) {
361 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700362 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700363 .noteNetworkStatsEnabled();
364 } catch (RemoteException e) {
365 }
366 }
367
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700368 // push any existing quota or UID rules
369 synchronized (mQuotaLock) {
370 int size = mActiveQuotas.size();
371 if (size > 0) {
372 Slog.d(TAG, "pushing " + size + " active quota rules");
373 final HashMap<String, Long> activeQuotas = mActiveQuotas;
374 mActiveQuotas = Maps.newHashMap();
375 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
376 setInterfaceQuota(entry.getKey(), entry.getValue());
377 }
378 }
379
380 size = mActiveAlerts.size();
381 if (size > 0) {
382 Slog.d(TAG, "pushing " + size + " active alert rules");
383 final HashMap<String, Long> activeAlerts = mActiveAlerts;
384 mActiveAlerts = Maps.newHashMap();
385 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
386 setInterfaceAlert(entry.getKey(), entry.getValue());
387 }
388 }
389
390 size = mUidRejectOnQuota.size();
391 if (size > 0) {
392 Slog.d(TAG, "pushing " + size + " active uid rules");
393 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
394 mUidRejectOnQuota = new SparseBooleanArray();
395 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
396 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
397 }
398 }
399 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700400
401 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700402 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700403 }
San Mehat4d02d002010-01-22 16:07:46 -0800404
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900405 /**
406 * Notify our observers of a new or updated interface address.
407 */
408 private void notifyAddressUpdated(String address, String iface, int flags, int scope) {
409 final int length = mObservers.beginBroadcast();
410 for (int i = 0; i < length; i++) {
411 try {
412 mObservers.getBroadcastItem(i).addressUpdated(address, iface, flags, scope);
413 } catch (RemoteException e) {
414 } catch (RuntimeException e) {
415 }
416 }
417 mObservers.finishBroadcast();
418 }
419
420 /**
421 * Notify our observers of a deleted interface address.
422 */
423 private void notifyAddressRemoved(String address, String iface, int flags, int scope) {
424 final int length = mObservers.beginBroadcast();
425 for (int i = 0; i < length; i++) {
426 try {
427 mObservers.getBroadcastItem(i).addressRemoved(address, iface, flags, scope);
428 } catch (RemoteException e) {
429 } catch (RuntimeException e) {
430 }
431 }
432 mObservers.finishBroadcast();
433 }
434
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900435 /**
436 * Notify our observers of DNS server information received.
437 */
438 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
439 final int length = mObservers.beginBroadcast();
440 for (int i = 0; i < length; i++) {
441 try {
442 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime, addresses);
443 } catch (RemoteException e) {
444 } catch (RuntimeException e) {
445 }
446 }
447 mObservers.finishBroadcast();
448 }
449
San Mehat873f2142010-01-14 10:25:07 -0800450 //
451 // Netd Callback handling
452 //
453
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700454 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
455 @Override
San Mehat873f2142010-01-14 10:25:07 -0800456 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700457 // event is dispatched from internal NDC thread, so we prepare the
458 // daemon back on main thread.
459 if (mConnectedSignal != null) {
460 mConnectedSignal.countDown();
461 mConnectedSignal = null;
462 } else {
463 mMainHandler.post(new Runnable() {
464 @Override
465 public void run() {
466 prepareNativeDaemon();
467 }
468 });
469 }
San Mehat873f2142010-01-14 10:25:07 -0800470 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700471
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700472 @Override
San Mehat873f2142010-01-14 10:25:07 -0800473 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900474 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700475 switch (code) {
476 case NetdResponseCode.InterfaceChange:
477 /*
478 * a network interface change occured
479 * Format: "NNN Iface added <name>"
480 * "NNN Iface removed <name>"
481 * "NNN Iface changed <name> <up/down>"
482 * "NNN Iface linkstatus <name> <up/down>"
483 */
484 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900485 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700486 }
487 if (cooked[2].equals("added")) {
488 notifyInterfaceAdded(cooked[3]);
489 return true;
490 } else if (cooked[2].equals("removed")) {
491 notifyInterfaceRemoved(cooked[3]);
492 return true;
493 } else if (cooked[2].equals("changed") && cooked.length == 5) {
494 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
495 return true;
496 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
497 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
498 return true;
499 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900500 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700501 // break;
502 case NetdResponseCode.BandwidthControl:
503 /*
504 * Bandwidth control needs some attention
505 * Format: "NNN limit alert <alertName> <ifaceName>"
506 */
507 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900508 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700509 }
510 if (cooked[2].equals("alert")) {
511 notifyLimitReached(cooked[3], cooked[4]);
512 return true;
513 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900514 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700515 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700516 case NetdResponseCode.InterfaceClassActivity:
517 /*
518 * An network interface class state changed (active/idle)
519 * Format: "NNN IfaceClass <active/idle> <label>"
520 */
521 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900522 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700523 }
524 boolean isActive = cooked[2].equals("active");
525 notifyInterfaceClassActivity(cooked[3], isActive);
526 return true;
527 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900528 case NetdResponseCode.InterfaceAddressChange:
529 /*
530 * A network address change occurred
531 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
532 * "NNN Address removed <addr> <iface> <flags> <scope>"
533 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900534 if (cooked.length < 7 || !cooked[1].equals("Address")) {
535 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900536 }
537
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700538 int flags;
539 int scope;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900540 try {
541 flags = Integer.parseInt(cooked[5]);
542 scope = Integer.parseInt(cooked[6]);
543 } catch(NumberFormatException e) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900544 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900545 }
546
547 if (cooked[2].equals("updated")) {
548 notifyAddressUpdated(cooked[3], cooked[4], flags, scope);
549 } else {
550 notifyAddressRemoved(cooked[3], cooked[4], flags, scope);
551 }
552 return true;
553 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900554 case NetdResponseCode.InterfaceDnsServerInfo:
555 /*
556 * Information about available DNS servers has been received.
557 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
558 */
559 long lifetime; // Actually a 32-bit unsigned integer.
560
561 if (cooked.length == 6 &&
562 cooked[1].equals("DnsInfo") &&
563 cooked[2].equals("servers")) {
564 try {
565 lifetime = Long.parseLong(cooked[4]);
566 } catch (NumberFormatException e) {
567 throw new IllegalStateException(errorMessage);
568 }
569 String[] servers = cooked[5].split(",");
570 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
571 }
572 return true;
573 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700574 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800575 }
576 return false;
San Mehat873f2142010-01-14 10:25:07 -0800577 }
578 }
579
San Mehated4fc8a2010-01-22 12:28:36 -0800580
San Mehat873f2142010-01-14 10:25:07 -0800581 //
582 // INetworkManagementService members
583 //
584
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800585 @Override
586 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800587 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700588 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800589 return NativeDaemonEvent.filterMessageList(
590 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700591 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800592 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700593 }
San Mehated4fc8a2010-01-22 12:28:36 -0800594 }
595
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800596 @Override
597 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800598 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800599
600 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700601 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800602 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700603 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800604 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700605 }
San Mehated4fc8a2010-01-22 12:28:36 -0800606
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800607 event.checkCode(InterfaceGetCfgResult);
608
609 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
610 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800611
Kenny Roota80ce062010-06-01 13:23:53 -0700612 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800613 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700614 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800615 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800616 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800617 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700618 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800619 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800620 } catch (IllegalArgumentException iae) {
621 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700622 }
623
624 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800625 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800626 } catch (NumberFormatException nfe) {
627 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700628 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800629
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800630 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
631 while (st.hasMoreTokens()) {
632 cfg.setFlag(st.nextToken());
633 }
Kenny Roota80ce062010-06-01 13:23:53 -0700634 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800635 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800636 }
San Mehated4fc8a2010-01-22 12:28:36 -0800637 return cfg;
638 }
639
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800640 @Override
641 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800642 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800643 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800644 if (linkAddr == null || linkAddr.getAddress() == null) {
645 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800646 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800647
648 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800649 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800650 linkAddr.getNetworkPrefixLength());
651 for (String flag : cfg.getFlags()) {
652 cmd.appendArg(flag);
653 }
654
Kenny Roota80ce062010-06-01 13:23:53 -0700655 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800656 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700657 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800658 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700659 }
San Mehat873f2142010-01-14 10:25:07 -0800660 }
661
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800662 @Override
663 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800664 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800665 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800666 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800667 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700668 }
669
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800670 @Override
671 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800672 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800673 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800674 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800675 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700676 }
677
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800678 @Override
679 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800680 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700681 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800682 mConnector.execute(
683 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700684 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800685 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700686 }
687 }
688
Irfan Sherifff5600612011-06-16 10:26:28 -0700689 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
690 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800691 @Override
692 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800693 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700694 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800695 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700696 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800697 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700698 }
699 }
700
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800701 @Override
702 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800703 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700704 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800705 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700706 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800707 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700708 }
709 }
710
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800711 @Override
712 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800713 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700714 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800715 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700716 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800717 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700718 }
719 }
720
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800721 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700722 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800723 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700724 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700725 }
726
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800727 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700728 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800729 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700730 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700731 }
732
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800733 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700734 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800735 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700736 modifyRoute(interfaceName, ADD, route, SECONDARY);
737 }
738
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800739 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700740 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800741 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700742 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
743 }
744
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800745 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
746 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700747
748 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800749 final LinkAddress la = route.getDestination();
750 cmd.appendArg(la.getAddress().getHostAddress());
751 cmd.appendArg(la.getNetworkPrefixLength());
752
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700753 if (route.getGateway() == null) {
754 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800755 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700756 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800757 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700758 }
759 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800760 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700761 }
762
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800763 try {
764 mConnector.execute(cmd);
765 } catch (NativeDaemonConnectorException e) {
766 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700767 }
768 }
769
770 private ArrayList<String> readRouteList(String filename) {
771 FileInputStream fstream = null;
772 ArrayList<String> list = new ArrayList<String>();
773
774 try {
775 fstream = new FileInputStream(filename);
776 DataInputStream in = new DataInputStream(fstream);
777 BufferedReader br = new BufferedReader(new InputStreamReader(in));
778 String s;
779
780 // throw away the title line
781
782 while (((s = br.readLine()) != null) && (s.length() != 0)) {
783 list.add(s);
784 }
785 } catch (IOException ex) {
786 // return current list, possibly empty
787 } finally {
788 if (fstream != null) {
789 try {
790 fstream.close();
791 } catch (IOException ex) {}
792 }
793 }
794
795 return list;
796 }
797
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800798 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700799 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800800 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700801 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
802
803 // v4 routes listed as:
804 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
805 for (String s : readRouteList("/proc/net/route")) {
806 String[] fields = s.split("\t");
807
808 if (fields.length > 7) {
809 String iface = fields[0];
810
811 if (interfaceName.equals(iface)) {
812 String dest = fields[1];
813 String gate = fields[2];
814 String flags = fields[3]; // future use?
815 String mask = fields[7];
816 try {
817 // address stored as a hex string, ex: 0014A8C0
818 InetAddress destAddr =
819 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
820 int prefixLength =
821 NetworkUtils.netmaskIntToPrefixLength(
822 (int)Long.parseLong(mask, 16));
823 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
824
825 // address stored as a hex string, ex 0014A8C0
826 InetAddress gatewayAddr =
827 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
828
Wink Saville7b5fd052013-03-15 05:07:04 +0000829 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700830 routes.add(route);
831 } catch (Exception e) {
832 Log.e(TAG, "Error parsing route " + s + " : " + e);
833 continue;
834 }
835 }
836 }
837 }
838
839 // v6 routes listed as:
840 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
841 for (String s : readRouteList("/proc/net/ipv6_route")) {
842 String[]fields = s.split("\\s+");
843 if (fields.length > 9) {
844 String iface = fields[9].trim();
845 if (interfaceName.equals(iface)) {
846 String dest = fields[0];
847 String prefix = fields[1];
848 String gate = fields[4];
849
850 try {
851 // prefix length stored as a hex string, ex 40
852 int prefixLength = Integer.parseInt(prefix, 16);
853
854 // address stored as a 32 char hex string
855 // ex fe800000000000000000000000000000
856 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
857 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
858
859 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
860
Wink Saville7b5fd052013-03-15 05:07:04 +0000861 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700862 routes.add(route);
863 } catch (Exception e) {
864 Log.e(TAG, "Error parsing route " + s + " : " + e);
865 continue;
866 }
867 }
868 }
869 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800870 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700871 }
872
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800873 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900874 public void setMtu(String iface, int mtu) {
875 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
876
877 final NativeDaemonEvent event;
878 try {
879 event = mConnector.execute("interface", "setmtu", iface, mtu);
880 } catch (NativeDaemonConnectorException e) {
881 throw e.rethrowAsParcelableException();
882 }
883 }
884
885 @Override
San Mehat873f2142010-01-14 10:25:07 -0800886 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800887 // TODO: remove from aidl if nobody calls externally
888 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800889
Joe Onorato8a9b2202010-02-26 18:56:32 -0800890 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800891 }
892
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800893 @Override
San Mehat873f2142010-01-14 10:25:07 -0800894 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800895 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800896
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800897 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700898 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800899 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700900 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800901 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700902 }
San Mehat873f2142010-01-14 10:25:07 -0800903
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800904 // 211 Forwarding enabled
905 event.checkCode(IpFwdStatusResult);
906 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800907 }
908
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800909 @Override
910 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800911 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800912 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800913 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800914 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800915 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800916 }
San Mehat873f2142010-01-14 10:25:07 -0800917 }
918
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800919 @Override
920 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800921 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700922 // cmd is "tether start first_start first_stop second_start second_stop ..."
923 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800924
925 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700926 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800927 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700928 }
Kenny Roota80ce062010-06-01 13:23:53 -0700929
930 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700932 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800933 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700934 }
San Mehat873f2142010-01-14 10:25:07 -0800935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
938 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800939 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700940 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800941 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700942 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800943 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700944 }
San Mehat873f2142010-01-14 10:25:07 -0800945 }
946
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 @Override
948 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800949 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800950
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800951 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700952 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800953 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700954 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800955 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700956 }
San Mehat873f2142010-01-14 10:25:07 -0800957
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800958 // 210 Tethering services started
959 event.checkCode(TetherStatusResult);
960 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800961 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700962
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800963 @Override
964 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800965 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700966 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800967 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700968 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800969 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700970 }
San Mehat873f2142010-01-14 10:25:07 -0800971 }
972
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800973 @Override
San Mehat873f2142010-01-14 10:25:07 -0800974 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800975 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700976 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700978 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800979 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700980 }
San Mehat873f2142010-01-14 10:25:07 -0800981 }
982
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800983 @Override
984 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800985 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700986 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800987 return NativeDaemonEvent.filterMessageList(
988 mConnector.executeForList("tether", "interface", "list"),
989 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700990 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800991 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700992 }
San Mehat873f2142010-01-14 10:25:07 -0800993 }
994
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800995 @Override
996 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800997 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800998
999 final Command cmd = new Command("tether", "dns", "set");
1000 for (String s : dns) {
1001 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1002 }
1003
San Mehat873f2142010-01-14 10:25:07 -08001004 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001005 mConnector.execute(cmd);
1006 } catch (NativeDaemonConnectorException e) {
1007 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001008 }
1009 }
1010
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001011 @Override
1012 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001013 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001014 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001015 return NativeDaemonEvent.filterMessageList(
1016 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001017 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001018 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001019 }
San Mehat873f2142010-01-14 10:25:07 -08001020 }
1021
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001022 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001023 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001024 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001025
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001026 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1027 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001028 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001029 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001030 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001031 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
1032 .getInterfaceAddresses();
1033 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001034 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001035 InetAddress addr = NetworkUtils.getNetworkPart(
1036 ia.getAddress(), ia.getNetworkPrefixLength());
1037 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001038 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001039 }
1040
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001041 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001042 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001043 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001044 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001045 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001046 }
1047
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001048 @Override
1049 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001050 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001051 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001052 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053 } catch (SocketException e) {
1054 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001055 }
San Mehat873f2142010-01-14 10:25:07 -08001056 }
1057
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001058 @Override
1059 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001060 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001061 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001062 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001063 } catch (SocketException e) {
1064 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001065 }
San Mehat873f2142010-01-14 10:25:07 -08001066 }
San Mehat72759df2010-01-19 13:50:37 -08001067
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001068 @Override
1069 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001070 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001071 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 return NativeDaemonEvent.filterMessageList(
1073 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001074 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001075 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001076 }
San Mehat72759df2010-01-19 13:50:37 -08001077 }
1078
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001079 @Override
1080 public void attachPppd(
1081 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001082 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001083 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001084 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001085 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1086 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1087 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001088 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001089 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001090 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001091 }
1092 }
1093
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001094 @Override
1095 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001096 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001097 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001098 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001099 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001100 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001101 }
San Mehat72759df2010-01-19 13:50:37 -08001102 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001103
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001104 @Override
1105 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001106 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001107 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001108 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001109 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001110 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001111 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001112 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001113 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001114 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001115 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001116 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001117 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001118 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001119 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001120 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001121 }
1122
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001123 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001124 switch (wifiConfig.getAuthType()) {
1125 case KeyMgmt.WPA_PSK:
1126 return "wpa-psk";
1127 case KeyMgmt.WPA2_PSK:
1128 return "wpa2-psk";
1129 default:
1130 return "open";
1131 }
1132 }
1133
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001134 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001135 @Override
1136 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001137 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001138 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001140 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001141 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001142 }
1143 }
1144
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001145 @Override
1146 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001147 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001148 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001149 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001150 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001151 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001152 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001153 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001154 }
1155
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001156 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001157 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001158 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001159 try {
1160 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001161 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001162 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001163 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001164 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001165 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001166 }
1167 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001168 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001169 }
1170 }
San Mehat91cac642010-03-31 14:31:36 -07001171
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001172 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001173 public void addIdleTimer(String iface, int timeout, String label) {
1174 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1175
1176 if (DBG) Slog.d(TAG, "Adding idletimer");
1177
1178 synchronized (mIdleTimerLock) {
1179 IdleTimerParams params = mActiveIdleTimers.get(iface);
1180 if (params != null) {
1181 // the interface already has idletimer, update network count
1182 params.networkCount++;
1183 return;
1184 }
1185
1186 try {
1187 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1188 } catch (NativeDaemonConnectorException e) {
1189 throw e.rethrowAsParcelableException();
1190 }
1191 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1192 }
1193 }
1194
1195 @Override
1196 public void removeIdleTimer(String iface) {
1197 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1198
1199 if (DBG) Slog.d(TAG, "Removing idletimer");
1200
1201 synchronized (mIdleTimerLock) {
1202 IdleTimerParams params = mActiveIdleTimers.get(iface);
1203 if (params == null || --(params.networkCount) > 0) {
1204 return;
1205 }
1206
1207 try {
1208 mConnector.execute("idletimer", "remove", iface,
1209 Integer.toString(params.timeout), params.label);
1210 } catch (NativeDaemonConnectorException e) {
1211 throw e.rethrowAsParcelableException();
1212 }
1213 mActiveIdleTimers.remove(iface);
1214 }
1215 }
1216
1217 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001218 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001219 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001220 try {
1221 return mStatsFactory.readNetworkStatsSummaryDev();
1222 } catch (IOException e) {
1223 throw new IllegalStateException(e);
1224 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001225 }
1226
1227 @Override
1228 public NetworkStats getNetworkStatsSummaryXt() {
1229 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001230 try {
1231 return mStatsFactory.readNetworkStatsSummaryXt();
1232 } catch (IOException e) {
1233 throw new IllegalStateException(e);
1234 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001235 }
1236
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001237 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001238 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001239 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001240 try {
1241 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1242 } catch (IOException e) {
1243 throw new IllegalStateException(e);
1244 }
San Mehat91cac642010-03-31 14:31:36 -07001245 }
1246
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001247 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001248 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001249 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001250
Jeff Sharkey350083e2011-06-29 10:45:16 -07001251 // silently discard when control disabled
1252 // TODO: eventually migrate to be always enabled
1253 if (!mBandwidthControlEnabled) return;
1254
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001255 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001256 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001257 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001258 }
1259
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001260 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001261 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001262 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001263 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001264 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001265 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001266 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001267 }
1268 }
1269
1270 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001271 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001272 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001273
Jeff Sharkey350083e2011-06-29 10:45:16 -07001274 // silently discard when control disabled
1275 // TODO: eventually migrate to be always enabled
1276 if (!mBandwidthControlEnabled) return;
1277
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001278 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001279 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001280 // TODO: eventually consider throwing
1281 return;
1282 }
1283
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001284 mActiveQuotas.remove(iface);
1285 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001286
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001287 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001288 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001289 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001290 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001291 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001292 }
1293 }
1294 }
1295
1296 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001297 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001298 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001299
1300 // silently discard when control disabled
1301 // TODO: eventually migrate to be always enabled
1302 if (!mBandwidthControlEnabled) return;
1303
1304 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001305 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001306 throw new IllegalStateException("setting alert requires existing quota on iface");
1307 }
1308
1309 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001310 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001311 throw new IllegalStateException("iface " + iface + " already has alert");
1312 }
1313
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001314 try {
1315 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001316 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001317 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001318 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001319 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001320 }
1321 }
1322 }
1323
1324 @Override
1325 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001326 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001327
1328 // silently discard when control disabled
1329 // TODO: eventually migrate to be always enabled
1330 if (!mBandwidthControlEnabled) return;
1331
1332 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001333 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001334 // TODO: eventually consider throwing
1335 return;
1336 }
1337
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001338 try {
1339 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001340 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001341 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001342 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001343 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001344 }
1345 }
1346 }
1347
1348 @Override
1349 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001350 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001351
1352 // silently discard when control disabled
1353 // TODO: eventually migrate to be always enabled
1354 if (!mBandwidthControlEnabled) return;
1355
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001356 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001357 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001358 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001359 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001360 }
1361 }
1362
1363 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001364 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001365 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001366
Jeff Sharkey350083e2011-06-29 10:45:16 -07001367 // silently discard when control disabled
1368 // TODO: eventually migrate to be always enabled
1369 if (!mBandwidthControlEnabled) return;
1370
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001371 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001372 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1373 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1374 // TODO: eventually consider throwing
1375 return;
1376 }
1377
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001378 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001379 mConnector.execute("bandwidth",
1380 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001381 if (rejectOnQuotaInterfaces) {
1382 mUidRejectOnQuota.put(uid, true);
1383 } else {
1384 mUidRejectOnQuota.delete(uid);
1385 }
1386 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001387 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001388 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001389 }
1390 }
1391
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001392 @Override
1393 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001394 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001395 return mBandwidthControlEnabled;
1396 }
1397
1398 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001399 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001400 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001401 try {
1402 return mStatsFactory.readNetworkStatsDetail(uid);
1403 } catch (IOException e) {
1404 throw new IllegalStateException(e);
1405 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001406 }
1407
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001408 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001409 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001410 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001411
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001412 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001413 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001414 final NativeDaemonEvent[] events = mConnector.executeForList(
1415 "bandwidth", "gettetherstats");
1416 for (NativeDaemonEvent event : events) {
1417 if (event.getCode() != TetheringStatsListResult) continue;
1418
1419 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1420 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1421 try {
1422 final String ifaceIn = tok.nextToken();
1423 final String ifaceOut = tok.nextToken();
1424
1425 final NetworkStats.Entry entry = new NetworkStats.Entry();
1426 entry.iface = ifaceOut;
1427 entry.uid = UID_TETHERING;
1428 entry.set = SET_DEFAULT;
1429 entry.tag = TAG_NONE;
1430 entry.rxBytes = Long.parseLong(tok.nextToken());
1431 entry.rxPackets = Long.parseLong(tok.nextToken());
1432 entry.txBytes = Long.parseLong(tok.nextToken());
1433 entry.txPackets = Long.parseLong(tok.nextToken());
1434 stats.combineValues(entry);
1435 } catch (NoSuchElementException e) {
1436 throw new IllegalStateException("problem parsing tethering stats: " + event);
1437 } catch (NumberFormatException e) {
1438 throw new IllegalStateException("problem parsing tethering stats: " + event);
1439 }
1440 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001441 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001442 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001443 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001444 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001445 }
1446
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001447 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001448 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001449 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001450 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001451 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001452 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001453 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001454 }
1455 }
1456
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001457 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001458 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001459 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001460
Robert Greenwalt8058f622012-11-09 10:52:27 -08001461 final Command cmd = new Command("resolver", "setifdns", iface,
1462 (domains == null ? "" : domains));
1463
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001464 for (String s : servers) {
1465 InetAddress a = NetworkUtils.numericToInetAddress(s);
1466 if (a.isAnyLocalAddress() == false) {
1467 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001468 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001469 }
1470
1471 try {
1472 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001473 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001474 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001475 }
1476 }
1477
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001478 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001479 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1481 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001482 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001483 "uid", "add", iface, uid_start, uid_end);
1484 } catch (NativeDaemonConnectorException e) {
1485 throw e.rethrowAsParcelableException();
1486 }
1487 }
1488
1489 @Override
1490 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1491 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1492 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001493 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001494 "uid", "remove", iface, uid_start, uid_end);
1495 } catch (NativeDaemonConnectorException e) {
1496 throw e.rethrowAsParcelableException();
1497 }
1498 }
1499
1500 @Override
1501 public void setMarkedForwarding(String iface) {
1502 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1503 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001504 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001505 } catch (NativeDaemonConnectorException e) {
1506 throw e.rethrowAsParcelableException();
1507 }
1508 }
1509
1510 @Override
1511 public void clearMarkedForwarding(String iface) {
1512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1513 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001514 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1515 } catch (NativeDaemonConnectorException e) {
1516 throw e.rethrowAsParcelableException();
1517 }
1518 }
1519
1520 @Override
1521 public int getMarkForUid(int uid) {
1522 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1523 final NativeDaemonEvent event;
1524 try {
1525 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1526 } catch (NativeDaemonConnectorException e) {
1527 throw e.rethrowAsParcelableException();
1528 }
1529 event.checkCode(GetMarkResult);
1530 return Integer.parseInt(event.getMessage());
1531 }
1532
1533 @Override
1534 public int getMarkForProtect() {
1535 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1536 final NativeDaemonEvent event;
1537 try {
1538 event = mConnector.execute("interface", "fwmark", "get", "protect");
1539 } catch (NativeDaemonConnectorException e) {
1540 throw e.rethrowAsParcelableException();
1541 }
1542 event.checkCode(GetMarkResult);
1543 return Integer.parseInt(event.getMessage());
1544 }
1545
1546 @Override
1547 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1548 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1549 try {
1550 LinkAddress dest = route.getDestination();
1551 mConnector.execute("interface", "fwmark", "route", "add", iface,
1552 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1553 } catch (NativeDaemonConnectorException e) {
1554 throw e.rethrowAsParcelableException();
1555 }
1556 }
1557
1558 @Override
1559 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1560 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1561 try {
1562 LinkAddress dest = route.getDestination();
1563 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1564 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001565 } catch (NativeDaemonConnectorException e) {
1566 throw e.rethrowAsParcelableException();
1567 }
1568 }
1569
1570 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001571 public void setHostExemption(LinkAddress host) {
1572 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1573 try {
1574 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1575 } catch (NativeDaemonConnectorException e) {
1576 throw e.rethrowAsParcelableException();
1577 }
1578 }
1579
1580 @Override
1581 public void clearHostExemption(LinkAddress host) {
1582 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1583 try {
1584 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1585 } catch (NativeDaemonConnectorException e) {
1586 throw e.rethrowAsParcelableException();
1587 }
1588 }
1589
1590 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001591 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1592 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1593 try {
1594 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1595 } catch (NativeDaemonConnectorException e) {
1596 throw e.rethrowAsParcelableException();
1597 }
1598 }
1599
1600 @Override
1601 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1602 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1603 try {
1604 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1605 } catch (NativeDaemonConnectorException e) {
1606 throw e.rethrowAsParcelableException();
1607 }
1608 }
1609
1610 @Override
1611 public void clearDnsInterfaceMaps() {
1612 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1613 try {
1614 mConnector.execute("resolver", "clearifacemapping");
1615 } catch (NativeDaemonConnectorException e) {
1616 throw e.rethrowAsParcelableException();
1617 }
1618 }
1619
1620
1621 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001622 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001623 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001624 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001625 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001626 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001627 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001628 }
1629 }
1630
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001631 @Override
1632 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001634 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001635 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001636 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001637 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001638 }
1639 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001640
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001641 @Override
1642 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001643 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001644 try {
1645 mConnector.execute("firewall", enabled ? "enable" : "disable");
1646 mFirewallEnabled = enabled;
1647 } catch (NativeDaemonConnectorException e) {
1648 throw e.rethrowAsParcelableException();
1649 }
1650 }
1651
1652 @Override
1653 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001654 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001655 return mFirewallEnabled;
1656 }
1657
1658 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001659 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001660 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001661 Preconditions.checkState(mFirewallEnabled);
1662 final String rule = allow ? ALLOW : DENY;
1663 try {
1664 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1665 } catch (NativeDaemonConnectorException e) {
1666 throw e.rethrowAsParcelableException();
1667 }
1668 }
1669
1670 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001671 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001672 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001673 Preconditions.checkState(mFirewallEnabled);
1674 final String rule = allow ? ALLOW : DENY;
1675 try {
1676 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1677 } catch (NativeDaemonConnectorException e) {
1678 throw e.rethrowAsParcelableException();
1679 }
1680 }
1681
1682 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001683 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001684 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001685 Preconditions.checkState(mFirewallEnabled);
1686 final String rule = allow ? ALLOW : DENY;
1687 try {
1688 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1689 } catch (NativeDaemonConnectorException e) {
1690 throw e.rethrowAsParcelableException();
1691 }
1692 }
1693
1694 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001695 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001696 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001697 Preconditions.checkState(mFirewallEnabled);
1698 final String rule = allow ? ALLOW : DENY;
1699 try {
1700 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1701 } catch (NativeDaemonConnectorException e) {
1702 throw e.rethrowAsParcelableException();
1703 }
1704 }
1705
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001706 private static void enforceSystemUid() {
1707 final int uid = Binder.getCallingUid();
1708 if (uid != Process.SYSTEM_UID) {
1709 throw new SecurityException("Only available to AID_SYSTEM");
1710 }
1711 }
1712
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001713 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001714 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1715 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1716 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001717 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001718 } catch (NativeDaemonConnectorException e) {
1719 throw new IllegalStateException(
1720 "Error communicating with native deamon to set interface for pid" + iface, e);
1721 }
1722 }
1723
1724 @Override
1725 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1726 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1727 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001728 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001729 } catch (NativeDaemonConnectorException e) {
1730 throw new IllegalStateException(
1731 "Error communicating with native deamon to clear interface for pid " + pid, e);
1732 }
1733 }
1734
Lorenzo Colitti79751842013-02-28 16:16:03 +09001735 @Override
1736 public void startClatd(String interfaceName) throws IllegalStateException {
1737 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1738
1739 try {
1740 mConnector.execute("clatd", "start", interfaceName);
1741 } catch (NativeDaemonConnectorException e) {
1742 throw e.rethrowAsParcelableException();
1743 }
1744 }
1745
1746 @Override
1747 public void stopClatd() throws IllegalStateException {
1748 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1749
1750 try {
1751 mConnector.execute("clatd", "stop");
1752 } catch (NativeDaemonConnectorException e) {
1753 throw e.rethrowAsParcelableException();
1754 }
1755 }
1756
1757 @Override
1758 public boolean isClatdStarted() {
1759 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1760
1761 final NativeDaemonEvent event;
1762 try {
1763 event = mConnector.execute("clatd", "status");
1764 } catch (NativeDaemonConnectorException e) {
1765 throw e.rethrowAsParcelableException();
1766 }
1767
1768 event.checkCode(ClatdStatusResult);
1769 return event.getMessage().endsWith("started");
1770 }
1771
Mattias Falk8b47b362011-08-23 14:15:13 +02001772 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001773 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001774 public void monitor() {
1775 if (mConnector != null) {
1776 mConnector.monitor();
1777 }
1778 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001779
1780 @Override
1781 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1782 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1783
Robert Greenwalt470fd722012-01-18 12:51:15 -08001784 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1785 mConnector.dump(fd, pw, args);
1786 pw.println();
1787
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001788 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1789
1790 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001791 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1792 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001793 }
1794
1795 synchronized (mUidRejectOnQuota) {
1796 pw.print("UID reject on quota ifaces: [");
1797 final int size = mUidRejectOnQuota.size();
1798 for (int i = 0; i < size; i++) {
1799 pw.print(mUidRejectOnQuota.keyAt(i));
1800 if (i < size - 1) pw.print(",");
1801 }
1802 pw.println("]");
1803 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001804
1805 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001806 }
San Mehat873f2142010-01-14 10:25:07 -08001807}