blob: f1a3ba5c3ccae93ea47268186d00f9dc8dca01ac [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
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800154 private IBatteryStats mBatteryStats;
155
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700156 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700157 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700158
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800159 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
160 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800161
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700162 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
163
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700164 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700165 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700166 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700167 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700168 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700169 /** Set of UIDs with active reject rules. */
170 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
171
Haoyu Bai04124232012-06-28 15:26:19 -0700172 private Object mIdleTimerLock = new Object();
173 /** Set of interfaces with active idle timers. */
174 private static class IdleTimerParams {
175 public final int timeout;
176 public final String label;
177 public int networkCount;
178
179 IdleTimerParams(int timeout, String label) {
180 this.timeout = timeout;
181 this.label = label;
182 this.networkCount = 1;
183 }
184 }
185 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
186
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700187 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700188 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700189
San Mehat873f2142010-01-14 10:25:07 -0800190 /**
191 * Constructs a new NetworkManagementService instance
192 *
193 * @param context Binder context for this service
194 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900195 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800196 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800197
Marco Nelissen62dbb222010-02-18 10:56:30 -0800198 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
199 return;
200 }
201
San Mehat873f2142010-01-14 10:25:07 -0800202 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900203 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700204 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700205
206 // Add ourself to the Watchdog monitors.
207 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700208 }
209
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900210 static NetworkManagementService create(Context context,
211 String socket) throws InterruptedException {
212 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700213 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700214 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
215 service.mThread.start();
216 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700217 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700218 if (DBG) Slog.d(TAG, "Connected");
219 return service;
San Mehat873f2142010-01-14 10:25:07 -0800220 }
221
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900222 public static NetworkManagementService create(Context context) throws InterruptedException {
223 return create(context, NETD_SOCKET_NAME);
224 }
225
Jeff Sharkey350083e2011-06-29 10:45:16 -0700226 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700227 prepareNativeDaemon();
228 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700229 }
230
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800231 private IBatteryStats getBatteryStats() {
232 synchronized (this) {
233 if (mBatteryStats != null) {
234 return mBatteryStats;
235 }
236 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
237 BatteryStats.SERVICE_NAME));
238 return mBatteryStats;
239 }
240 }
241
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800242 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800243 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800244 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800245 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800246 }
247
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800248 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800249 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800250 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800251 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800252 }
253
254 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700255 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800256 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700257 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800258 final int length = mObservers.beginBroadcast();
259 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800260 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
262 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900263 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700264 }
265 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800266 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700267 }
268
269 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700270 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700271 * (typically, an Ethernet cable has been plugged-in or unplugged).
272 */
273 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800274 final int length = mObservers.beginBroadcast();
275 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700276 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
278 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900279 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800280 }
281 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800282 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800283 }
284
285 /**
286 * Notify our observers of an interface addition.
287 */
288 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800289 final int length = mObservers.beginBroadcast();
290 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800291 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800292 mObservers.getBroadcastItem(i).interfaceAdded(iface);
293 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900294 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800295 }
296 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800298 }
299
300 /**
301 * Notify our observers of an interface removal.
302 */
303 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700304 // netd already clears out quota and alerts for removed ifaces; update
305 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700306 mActiveAlerts.remove(iface);
307 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700308
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800309 final int length = mObservers.beginBroadcast();
310 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800311 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800312 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
313 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900314 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800315 }
316 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800317 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800318 }
319
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700320 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700321 * Notify our observers of a limit reached.
322 */
323 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800324 final int length = mObservers.beginBroadcast();
325 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700326 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800327 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
328 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900329 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700330 }
331 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800332 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700333 }
334
335 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700336 * Notify our observers of a change in the data activity state of the interface
337 */
338 private void notifyInterfaceClassActivity(String label, boolean active) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800339 try {
340 getBatteryStats().noteDataConnectionActive(label, active);
341 } catch (RemoteException e) {
342 }
Haoyu Baidb3c8672012-06-20 14:29:57 -0700343 final int length = mObservers.beginBroadcast();
344 for (int i = 0; i < length; i++) {
345 try {
346 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
347 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900348 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700349 }
350 }
351 mObservers.finishBroadcast();
352 }
353
354 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700355 * Prepare native daemon once connected, enabling modules and pushing any
356 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700357 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700358 private void prepareNativeDaemon() {
359 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700360
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700361 // only enable bandwidth control when support exists
362 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
363 if (hasKernelSupport) {
364 Slog.d(TAG, "enabling bandwidth control");
365 try {
366 mConnector.execute("bandwidth", "enable");
367 mBandwidthControlEnabled = true;
368 } catch (NativeDaemonConnectorException e) {
369 Log.wtf(TAG, "problem enabling bandwidth controls", e);
370 }
371 } else {
372 Slog.d(TAG, "not enabling bandwidth control");
373 }
374
375 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
376
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700377 if (mBandwidthControlEnabled) {
378 try {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800379 getBatteryStats().noteNetworkStatsEnabled();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700380 } catch (RemoteException e) {
381 }
382 }
383
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700384 // push any existing quota or UID rules
385 synchronized (mQuotaLock) {
386 int size = mActiveQuotas.size();
387 if (size > 0) {
388 Slog.d(TAG, "pushing " + size + " active quota rules");
389 final HashMap<String, Long> activeQuotas = mActiveQuotas;
390 mActiveQuotas = Maps.newHashMap();
391 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
392 setInterfaceQuota(entry.getKey(), entry.getValue());
393 }
394 }
395
396 size = mActiveAlerts.size();
397 if (size > 0) {
398 Slog.d(TAG, "pushing " + size + " active alert rules");
399 final HashMap<String, Long> activeAlerts = mActiveAlerts;
400 mActiveAlerts = Maps.newHashMap();
401 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
402 setInterfaceAlert(entry.getKey(), entry.getValue());
403 }
404 }
405
406 size = mUidRejectOnQuota.size();
407 if (size > 0) {
408 Slog.d(TAG, "pushing " + size + " active uid rules");
409 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
410 mUidRejectOnQuota = new SparseBooleanArray();
411 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
412 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
413 }
414 }
415 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700416
417 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700418 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700419 }
San Mehat4d02d002010-01-22 16:07:46 -0800420
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900421 /**
422 * Notify our observers of a new or updated interface address.
423 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900424 private void notifyAddressUpdated(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900425 final int length = mObservers.beginBroadcast();
426 for (int i = 0; i < length; i++) {
427 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900428 mObservers.getBroadcastItem(i).addressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900429 } catch (RemoteException e) {
430 } catch (RuntimeException e) {
431 }
432 }
433 mObservers.finishBroadcast();
434 }
435
436 /**
437 * Notify our observers of a deleted interface address.
438 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900439 private void notifyAddressRemoved(String iface, LinkAddress address) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900440 final int length = mObservers.beginBroadcast();
441 for (int i = 0; i < length; i++) {
442 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900443 mObservers.getBroadcastItem(i).addressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900444 } catch (RemoteException e) {
445 } catch (RuntimeException e) {
446 }
447 }
448 mObservers.finishBroadcast();
449 }
450
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900451 /**
452 * Notify our observers of DNS server information received.
453 */
454 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
455 final int length = mObservers.beginBroadcast();
456 for (int i = 0; i < length; i++) {
457 try {
458 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime, addresses);
459 } catch (RemoteException e) {
460 } catch (RuntimeException e) {
461 }
462 }
463 mObservers.finishBroadcast();
464 }
465
San Mehat873f2142010-01-14 10:25:07 -0800466 //
467 // Netd Callback handling
468 //
469
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700470 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
471 @Override
San Mehat873f2142010-01-14 10:25:07 -0800472 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700473 // event is dispatched from internal NDC thread, so we prepare the
474 // daemon back on main thread.
475 if (mConnectedSignal != null) {
476 mConnectedSignal.countDown();
477 mConnectedSignal = null;
478 } else {
479 mMainHandler.post(new Runnable() {
480 @Override
481 public void run() {
482 prepareNativeDaemon();
483 }
484 });
485 }
San Mehat873f2142010-01-14 10:25:07 -0800486 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700487
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700488 @Override
San Mehat873f2142010-01-14 10:25:07 -0800489 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900490 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700491 switch (code) {
492 case NetdResponseCode.InterfaceChange:
493 /*
494 * a network interface change occured
495 * Format: "NNN Iface added <name>"
496 * "NNN Iface removed <name>"
497 * "NNN Iface changed <name> <up/down>"
498 * "NNN Iface linkstatus <name> <up/down>"
499 */
500 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900501 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700502 }
503 if (cooked[2].equals("added")) {
504 notifyInterfaceAdded(cooked[3]);
505 return true;
506 } else if (cooked[2].equals("removed")) {
507 notifyInterfaceRemoved(cooked[3]);
508 return true;
509 } else if (cooked[2].equals("changed") && cooked.length == 5) {
510 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
511 return true;
512 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
513 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
514 return true;
515 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900516 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700517 // break;
518 case NetdResponseCode.BandwidthControl:
519 /*
520 * Bandwidth control needs some attention
521 * Format: "NNN limit alert <alertName> <ifaceName>"
522 */
523 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900524 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700525 }
526 if (cooked[2].equals("alert")) {
527 notifyLimitReached(cooked[3], cooked[4]);
528 return true;
529 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900530 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700531 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700532 case NetdResponseCode.InterfaceClassActivity:
533 /*
534 * An network interface class state changed (active/idle)
535 * Format: "NNN IfaceClass <active/idle> <label>"
536 */
537 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900538 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700539 }
540 boolean isActive = cooked[2].equals("active");
541 notifyInterfaceClassActivity(cooked[3], isActive);
542 return true;
543 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900544 case NetdResponseCode.InterfaceAddressChange:
545 /*
546 * A network address change occurred
547 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
548 * "NNN Address removed <addr> <iface> <flags> <scope>"
549 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900550 if (cooked.length < 7 || !cooked[1].equals("Address")) {
551 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900552 }
553
Lorenzo Colitti64483942013-11-15 18:43:52 +0900554 String iface = cooked[4];
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900555 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900556 try {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900557 int flags = Integer.parseInt(cooked[5]);
558 int scope = Integer.parseInt(cooked[6]);
559 address = new LinkAddress(cooked[3], flags, scope);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900560 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
561 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900562 } catch(IllegalArgumentException e) { // Malformed/invalid IP address.
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900563 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900564 }
565
566 if (cooked[2].equals("updated")) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900567 notifyAddressUpdated(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900568 } else {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900569 notifyAddressRemoved(iface, address);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900570 }
571 return true;
572 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900573 case NetdResponseCode.InterfaceDnsServerInfo:
574 /*
575 * Information about available DNS servers has been received.
576 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
577 */
578 long lifetime; // Actually a 32-bit unsigned integer.
579
580 if (cooked.length == 6 &&
581 cooked[1].equals("DnsInfo") &&
582 cooked[2].equals("servers")) {
583 try {
584 lifetime = Long.parseLong(cooked[4]);
585 } catch (NumberFormatException e) {
586 throw new IllegalStateException(errorMessage);
587 }
588 String[] servers = cooked[5].split(",");
589 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
590 }
591 return true;
592 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700593 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800594 }
595 return false;
San Mehat873f2142010-01-14 10:25:07 -0800596 }
597 }
598
San Mehated4fc8a2010-01-22 12:28:36 -0800599
San Mehat873f2142010-01-14 10:25:07 -0800600 //
601 // INetworkManagementService members
602 //
603
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800604 @Override
605 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800606 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700607 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800608 return NativeDaemonEvent.filterMessageList(
609 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700610 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800611 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700612 }
San Mehated4fc8a2010-01-22 12:28:36 -0800613 }
614
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800615 @Override
616 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800617 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800618
619 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700620 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800621 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700622 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800623 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700624 }
San Mehated4fc8a2010-01-22 12:28:36 -0800625
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800626 event.checkCode(InterfaceGetCfgResult);
627
628 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
629 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800630
Kenny Roota80ce062010-06-01 13:23:53 -0700631 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800632 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700633 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800634 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800635 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800636 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700637 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800638 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800639 } catch (IllegalArgumentException iae) {
640 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700641 }
642
643 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800644 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800645 } catch (NumberFormatException nfe) {
646 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700647 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800648
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800649 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
650 while (st.hasMoreTokens()) {
651 cfg.setFlag(st.nextToken());
652 }
Kenny Roota80ce062010-06-01 13:23:53 -0700653 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800654 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800655 }
San Mehated4fc8a2010-01-22 12:28:36 -0800656 return cfg;
657 }
658
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800659 @Override
660 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800661 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800662 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800663 if (linkAddr == null || linkAddr.getAddress() == null) {
664 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800665 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800666
667 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800668 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800669 linkAddr.getNetworkPrefixLength());
670 for (String flag : cfg.getFlags()) {
671 cmd.appendArg(flag);
672 }
673
Kenny Roota80ce062010-06-01 13:23:53 -0700674 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800675 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700676 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800677 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700678 }
San Mehat873f2142010-01-14 10:25:07 -0800679 }
680
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800681 @Override
682 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800684 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800685 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800686 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700687 }
688
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800689 @Override
690 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800691 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800692 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800693 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800694 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700695 }
696
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800697 @Override
698 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800699 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700700 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800701 mConnector.execute(
702 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700703 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800704 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700705 }
706 }
707
Irfan Sherifff5600612011-06-16 10:26:28 -0700708 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
709 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800710 @Override
711 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800712 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700713 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800714 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700715 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800716 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700717 }
718 }
719
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800720 @Override
721 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800722 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700723 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800724 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700725 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800726 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700727 }
728 }
729
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800730 @Override
731 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800732 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700733 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800734 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700735 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800736 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700737 }
738 }
739
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800740 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700741 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800742 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700743 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700744 }
745
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800746 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700747 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800748 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700749 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700750 }
751
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800752 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700753 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800754 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700755 modifyRoute(interfaceName, ADD, route, SECONDARY);
756 }
757
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800758 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700759 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800760 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700761 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
762 }
763
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800764 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
765 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700766
767 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768 final LinkAddress la = route.getDestination();
769 cmd.appendArg(la.getAddress().getHostAddress());
770 cmd.appendArg(la.getNetworkPrefixLength());
771
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700772 if (route.getGateway() == null) {
773 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800774 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700775 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800776 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700777 }
778 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800779 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700780 }
781
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800782 try {
783 mConnector.execute(cmd);
784 } catch (NativeDaemonConnectorException e) {
785 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700786 }
787 }
788
789 private ArrayList<String> readRouteList(String filename) {
790 FileInputStream fstream = null;
791 ArrayList<String> list = new ArrayList<String>();
792
793 try {
794 fstream = new FileInputStream(filename);
795 DataInputStream in = new DataInputStream(fstream);
796 BufferedReader br = new BufferedReader(new InputStreamReader(in));
797 String s;
798
799 // throw away the title line
800
801 while (((s = br.readLine()) != null) && (s.length() != 0)) {
802 list.add(s);
803 }
804 } catch (IOException ex) {
805 // return current list, possibly empty
806 } finally {
807 if (fstream != null) {
808 try {
809 fstream.close();
810 } catch (IOException ex) {}
811 }
812 }
813
814 return list;
815 }
816
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800817 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700818 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800819 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700820 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
821
822 // v4 routes listed as:
823 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
824 for (String s : readRouteList("/proc/net/route")) {
825 String[] fields = s.split("\t");
826
827 if (fields.length > 7) {
828 String iface = fields[0];
829
830 if (interfaceName.equals(iface)) {
831 String dest = fields[1];
832 String gate = fields[2];
833 String flags = fields[3]; // future use?
834 String mask = fields[7];
835 try {
836 // address stored as a hex string, ex: 0014A8C0
837 InetAddress destAddr =
838 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
839 int prefixLength =
840 NetworkUtils.netmaskIntToPrefixLength(
841 (int)Long.parseLong(mask, 16));
842 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
843
844 // address stored as a hex string, ex 0014A8C0
845 InetAddress gatewayAddr =
846 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
847
Wink Saville7b5fd052013-03-15 05:07:04 +0000848 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700849 routes.add(route);
850 } catch (Exception e) {
851 Log.e(TAG, "Error parsing route " + s + " : " + e);
852 continue;
853 }
854 }
855 }
856 }
857
858 // v6 routes listed as:
859 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
860 for (String s : readRouteList("/proc/net/ipv6_route")) {
861 String[]fields = s.split("\\s+");
862 if (fields.length > 9) {
863 String iface = fields[9].trim();
864 if (interfaceName.equals(iface)) {
865 String dest = fields[0];
866 String prefix = fields[1];
867 String gate = fields[4];
868
869 try {
870 // prefix length stored as a hex string, ex 40
871 int prefixLength = Integer.parseInt(prefix, 16);
872
873 // address stored as a 32 char hex string
874 // ex fe800000000000000000000000000000
875 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
876 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
877
878 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
879
Wink Saville7b5fd052013-03-15 05:07:04 +0000880 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700881 routes.add(route);
882 } catch (Exception e) {
883 Log.e(TAG, "Error parsing route " + s + " : " + e);
884 continue;
885 }
886 }
887 }
888 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800889 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700890 }
891
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800892 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900893 public void setMtu(String iface, int mtu) {
894 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
895
896 final NativeDaemonEvent event;
897 try {
898 event = mConnector.execute("interface", "setmtu", iface, mtu);
899 } catch (NativeDaemonConnectorException e) {
900 throw e.rethrowAsParcelableException();
901 }
902 }
903
904 @Override
San Mehat873f2142010-01-14 10:25:07 -0800905 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800906 // TODO: remove from aidl if nobody calls externally
907 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800908
Joe Onorato8a9b2202010-02-26 18:56:32 -0800909 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800910 }
911
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800912 @Override
San Mehat873f2142010-01-14 10:25:07 -0800913 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800914 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800915
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800916 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700917 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800918 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700919 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800920 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700921 }
San Mehat873f2142010-01-14 10:25:07 -0800922
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800923 // 211 Forwarding enabled
924 event.checkCode(IpFwdStatusResult);
925 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800926 }
927
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800928 @Override
929 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800930 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800931 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800932 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800933 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800934 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800935 }
San Mehat873f2142010-01-14 10:25:07 -0800936 }
937
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800938 @Override
939 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800940 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700941 // cmd is "tether start first_start first_stop second_start second_stop ..."
942 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800943
944 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700945 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800946 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700947 }
Kenny Roota80ce062010-06-01 13:23:53 -0700948
949 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800950 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700951 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800952 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700953 }
San Mehat873f2142010-01-14 10:25:07 -0800954 }
955
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800956 @Override
957 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800958 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700959 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800960 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700961 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800962 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700963 }
San Mehat873f2142010-01-14 10:25:07 -0800964 }
965
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800966 @Override
967 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800968 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800969
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800970 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700971 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800972 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700973 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800974 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700975 }
San Mehat873f2142010-01-14 10:25:07 -0800976
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977 // 210 Tethering services started
978 event.checkCode(TetherStatusResult);
979 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800980 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700981
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800982 @Override
983 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800984 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700985 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700987 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800988 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700989 }
San Mehat873f2142010-01-14 10:25:07 -0800990 }
991
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800992 @Override
San Mehat873f2142010-01-14 10:25:07 -0800993 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800994 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700995 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800996 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700997 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800998 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700999 }
San Mehat873f2142010-01-14 10:25:07 -08001000 }
1001
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001002 @Override
1003 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001004 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001005 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001006 return NativeDaemonEvent.filterMessageList(
1007 mConnector.executeForList("tether", "interface", "list"),
1008 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001009 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001010 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001011 }
San Mehat873f2142010-01-14 10:25:07 -08001012 }
1013
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001014 @Override
1015 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001016 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001017
1018 final Command cmd = new Command("tether", "dns", "set");
1019 for (String s : dns) {
1020 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1021 }
1022
San Mehat873f2142010-01-14 10:25:07 -08001023 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001024 mConnector.execute(cmd);
1025 } catch (NativeDaemonConnectorException e) {
1026 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001027 }
1028 }
1029
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001030 @Override
1031 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001032 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001033 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001034 return NativeDaemonEvent.filterMessageList(
1035 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001036 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001037 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001038 }
San Mehat873f2142010-01-14 10:25:07 -08001039 }
1040
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001041 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001042 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001043 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001044
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001045 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1046 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001047 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001048 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001049 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001050 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
1051 .getInterfaceAddresses();
1052 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001053 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001054 InetAddress addr = NetworkUtils.getNetworkPart(
1055 ia.getAddress(), ia.getNetworkPrefixLength());
1056 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001057 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001058 }
1059
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001060 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001061 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001062 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001063 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001064 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001065 }
1066
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001067 @Override
1068 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001069 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001070 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001071 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 } catch (SocketException e) {
1073 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001074 }
San Mehat873f2142010-01-14 10:25:07 -08001075 }
1076
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001077 @Override
1078 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001079 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001080 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001081 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001082 } catch (SocketException e) {
1083 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001084 }
San Mehat873f2142010-01-14 10:25:07 -08001085 }
San Mehat72759df2010-01-19 13:50:37 -08001086
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001087 @Override
1088 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001089 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001090 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001091 return NativeDaemonEvent.filterMessageList(
1092 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001093 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001094 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001095 }
San Mehat72759df2010-01-19 13:50:37 -08001096 }
1097
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001098 @Override
1099 public void attachPppd(
1100 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001101 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001102 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001103 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001104 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1105 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1106 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001107 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001108 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001109 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001110 }
1111 }
1112
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001113 @Override
1114 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001116 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001117 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001118 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001119 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001120 }
San Mehat72759df2010-01-19 13:50:37 -08001121 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001122
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001123 @Override
1124 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001125 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001126 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001127 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001128 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001129 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001130 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001131 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001132 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001133 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001134 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001135 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001137 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001138 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001139 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001140 }
1141
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001142 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001143 switch (wifiConfig.getAuthType()) {
1144 case KeyMgmt.WPA_PSK:
1145 return "wpa-psk";
1146 case KeyMgmt.WPA2_PSK:
1147 return "wpa2-psk";
1148 default:
1149 return "open";
1150 }
1151 }
1152
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001153 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001154 @Override
1155 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001157 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001158 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001159 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001160 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001161 }
1162 }
1163
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001164 @Override
1165 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001166 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001167 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001168 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001169 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001170 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001171 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001172 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001173 }
1174
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001175 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001176 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001177 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001178 try {
1179 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001180 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001181 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001182 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001183 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001184 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001185 }
1186 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001187 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001188 }
1189 }
San Mehat91cac642010-03-31 14:31:36 -07001190
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001191 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001192 public void addIdleTimer(String iface, int timeout, String label) {
1193 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1194
1195 if (DBG) Slog.d(TAG, "Adding idletimer");
1196
1197 synchronized (mIdleTimerLock) {
1198 IdleTimerParams params = mActiveIdleTimers.get(iface);
1199 if (params != null) {
1200 // the interface already has idletimer, update network count
1201 params.networkCount++;
1202 return;
1203 }
1204
1205 try {
1206 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1207 } catch (NativeDaemonConnectorException e) {
1208 throw e.rethrowAsParcelableException();
1209 }
1210 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001211 // Networks start up.
1212 notifyInterfaceClassActivity(label, true);
Haoyu Bai04124232012-06-28 15:26:19 -07001213 }
1214 }
1215
1216 @Override
1217 public void removeIdleTimer(String iface) {
1218 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1219
1220 if (DBG) Slog.d(TAG, "Removing idletimer");
1221
1222 synchronized (mIdleTimerLock) {
1223 IdleTimerParams params = mActiveIdleTimers.get(iface);
1224 if (params == null || --(params.networkCount) > 0) {
1225 return;
1226 }
1227
1228 try {
1229 mConnector.execute("idletimer", "remove", iface,
1230 Integer.toString(params.timeout), params.label);
1231 } catch (NativeDaemonConnectorException e) {
1232 throw e.rethrowAsParcelableException();
1233 }
1234 mActiveIdleTimers.remove(iface);
1235 }
1236 }
1237
1238 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001239 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001240 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001241 try {
1242 return mStatsFactory.readNetworkStatsSummaryDev();
1243 } catch (IOException e) {
1244 throw new IllegalStateException(e);
1245 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001246 }
1247
1248 @Override
1249 public NetworkStats getNetworkStatsSummaryXt() {
1250 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001251 try {
1252 return mStatsFactory.readNetworkStatsSummaryXt();
1253 } catch (IOException e) {
1254 throw new IllegalStateException(e);
1255 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001256 }
1257
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001258 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001259 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001260 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001261 try {
1262 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1263 } catch (IOException e) {
1264 throw new IllegalStateException(e);
1265 }
San Mehat91cac642010-03-31 14:31:36 -07001266 }
1267
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001268 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001269 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001270 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001271
Jeff Sharkey350083e2011-06-29 10:45:16 -07001272 // silently discard when control disabled
1273 // TODO: eventually migrate to be always enabled
1274 if (!mBandwidthControlEnabled) return;
1275
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001276 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001277 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001278 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001279 }
1280
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001281 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001282 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001283 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001284 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001285 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001286 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001287 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001288 }
1289 }
1290
1291 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001292 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001293 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001294
Jeff Sharkey350083e2011-06-29 10:45:16 -07001295 // silently discard when control disabled
1296 // TODO: eventually migrate to be always enabled
1297 if (!mBandwidthControlEnabled) return;
1298
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001299 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001300 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001301 // TODO: eventually consider throwing
1302 return;
1303 }
1304
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001305 mActiveQuotas.remove(iface);
1306 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001307
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001308 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001309 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001310 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001311 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001312 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001313 }
1314 }
1315 }
1316
1317 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001318 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001319 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001320
1321 // silently discard when control disabled
1322 // TODO: eventually migrate to be always enabled
1323 if (!mBandwidthControlEnabled) return;
1324
1325 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001326 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001327 throw new IllegalStateException("setting alert requires existing quota on iface");
1328 }
1329
1330 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001331 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001332 throw new IllegalStateException("iface " + iface + " already has alert");
1333 }
1334
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001335 try {
1336 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001337 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001338 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001339 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001340 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001341 }
1342 }
1343 }
1344
1345 @Override
1346 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001347 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001348
1349 // silently discard when control disabled
1350 // TODO: eventually migrate to be always enabled
1351 if (!mBandwidthControlEnabled) return;
1352
1353 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001354 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001355 // TODO: eventually consider throwing
1356 return;
1357 }
1358
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001359 try {
1360 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001361 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001362 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001363 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001364 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001365 }
1366 }
1367 }
1368
1369 @Override
1370 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001371 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001372
1373 // silently discard when control disabled
1374 // TODO: eventually migrate to be always enabled
1375 if (!mBandwidthControlEnabled) return;
1376
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001377 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001378 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001379 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001380 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001381 }
1382 }
1383
1384 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001385 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001386 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001387
Jeff Sharkey350083e2011-06-29 10:45:16 -07001388 // silently discard when control disabled
1389 // TODO: eventually migrate to be always enabled
1390 if (!mBandwidthControlEnabled) return;
1391
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001392 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001393 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1394 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1395 // TODO: eventually consider throwing
1396 return;
1397 }
1398
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001399 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001400 mConnector.execute("bandwidth",
1401 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001402 if (rejectOnQuotaInterfaces) {
1403 mUidRejectOnQuota.put(uid, true);
1404 } else {
1405 mUidRejectOnQuota.delete(uid);
1406 }
1407 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001408 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001409 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001410 }
1411 }
1412
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001413 @Override
1414 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001415 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001416 return mBandwidthControlEnabled;
1417 }
1418
1419 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001420 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001421 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001422 try {
1423 return mStatsFactory.readNetworkStatsDetail(uid);
1424 } catch (IOException e) {
1425 throw new IllegalStateException(e);
1426 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001427 }
1428
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001429 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001430 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001431 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001432
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001433 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001434 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001435 final NativeDaemonEvent[] events = mConnector.executeForList(
1436 "bandwidth", "gettetherstats");
1437 for (NativeDaemonEvent event : events) {
1438 if (event.getCode() != TetheringStatsListResult) continue;
1439
1440 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1441 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1442 try {
1443 final String ifaceIn = tok.nextToken();
1444 final String ifaceOut = tok.nextToken();
1445
1446 final NetworkStats.Entry entry = new NetworkStats.Entry();
1447 entry.iface = ifaceOut;
1448 entry.uid = UID_TETHERING;
1449 entry.set = SET_DEFAULT;
1450 entry.tag = TAG_NONE;
1451 entry.rxBytes = Long.parseLong(tok.nextToken());
1452 entry.rxPackets = Long.parseLong(tok.nextToken());
1453 entry.txBytes = Long.parseLong(tok.nextToken());
1454 entry.txPackets = Long.parseLong(tok.nextToken());
1455 stats.combineValues(entry);
1456 } catch (NoSuchElementException e) {
1457 throw new IllegalStateException("problem parsing tethering stats: " + event);
1458 } catch (NumberFormatException e) {
1459 throw new IllegalStateException("problem parsing tethering stats: " + event);
1460 }
1461 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001462 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001463 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001464 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001465 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001466 }
1467
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001468 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001469 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001470 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001471 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001472 mConnector.execute("resolver", "setdefaultif", iface);
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
Robert Greenwalt8058f622012-11-09 10:52:27 -08001479 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001481
Robert Greenwalt8058f622012-11-09 10:52:27 -08001482 final Command cmd = new Command("resolver", "setifdns", iface,
1483 (domains == null ? "" : domains));
1484
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001485 for (String s : servers) {
1486 InetAddress a = NetworkUtils.numericToInetAddress(s);
1487 if (a.isAnyLocalAddress() == false) {
1488 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001489 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001490 }
1491
1492 try {
1493 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001494 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001495 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001496 }
1497 }
1498
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001499 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001500 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1501 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1502 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001503 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001504 "uid", "add", iface, uid_start, uid_end);
1505 } catch (NativeDaemonConnectorException e) {
1506 throw e.rethrowAsParcelableException();
1507 }
1508 }
1509
1510 @Override
1511 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1513 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001514 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001515 "uid", "remove", iface, uid_start, uid_end);
1516 } catch (NativeDaemonConnectorException e) {
1517 throw e.rethrowAsParcelableException();
1518 }
1519 }
1520
1521 @Override
1522 public void setMarkedForwarding(String iface) {
1523 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1524 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001525 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001526 } catch (NativeDaemonConnectorException e) {
1527 throw e.rethrowAsParcelableException();
1528 }
1529 }
1530
1531 @Override
1532 public void clearMarkedForwarding(String iface) {
1533 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1534 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001535 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1536 } catch (NativeDaemonConnectorException e) {
1537 throw e.rethrowAsParcelableException();
1538 }
1539 }
1540
1541 @Override
1542 public int getMarkForUid(int uid) {
1543 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1544 final NativeDaemonEvent event;
1545 try {
1546 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1547 } catch (NativeDaemonConnectorException e) {
1548 throw e.rethrowAsParcelableException();
1549 }
1550 event.checkCode(GetMarkResult);
1551 return Integer.parseInt(event.getMessage());
1552 }
1553
1554 @Override
1555 public int getMarkForProtect() {
1556 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1557 final NativeDaemonEvent event;
1558 try {
1559 event = mConnector.execute("interface", "fwmark", "get", "protect");
1560 } catch (NativeDaemonConnectorException e) {
1561 throw e.rethrowAsParcelableException();
1562 }
1563 event.checkCode(GetMarkResult);
1564 return Integer.parseInt(event.getMessage());
1565 }
1566
1567 @Override
1568 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1569 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1570 try {
1571 LinkAddress dest = route.getDestination();
1572 mConnector.execute("interface", "fwmark", "route", "add", iface,
1573 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1574 } catch (NativeDaemonConnectorException e) {
1575 throw e.rethrowAsParcelableException();
1576 }
1577 }
1578
1579 @Override
1580 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1581 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1582 try {
1583 LinkAddress dest = route.getDestination();
1584 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1585 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001586 } catch (NativeDaemonConnectorException e) {
1587 throw e.rethrowAsParcelableException();
1588 }
1589 }
1590
1591 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001592 public void setHostExemption(LinkAddress host) {
1593 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1594 try {
1595 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1596 } catch (NativeDaemonConnectorException e) {
1597 throw e.rethrowAsParcelableException();
1598 }
1599 }
1600
1601 @Override
1602 public void clearHostExemption(LinkAddress host) {
1603 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1604 try {
1605 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1606 } catch (NativeDaemonConnectorException e) {
1607 throw e.rethrowAsParcelableException();
1608 }
1609 }
1610
1611 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001612 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1613 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1614 try {
1615 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1616 } catch (NativeDaemonConnectorException e) {
1617 throw e.rethrowAsParcelableException();
1618 }
1619 }
1620
1621 @Override
1622 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1623 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1624 try {
1625 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1626 } catch (NativeDaemonConnectorException e) {
1627 throw e.rethrowAsParcelableException();
1628 }
1629 }
1630
1631 @Override
1632 public void clearDnsInterfaceMaps() {
1633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1634 try {
1635 mConnector.execute("resolver", "clearifacemapping");
1636 } catch (NativeDaemonConnectorException e) {
1637 throw e.rethrowAsParcelableException();
1638 }
1639 }
1640
1641
1642 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001643 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001644 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001645 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001646 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001647 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001648 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001649 }
1650 }
1651
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001652 @Override
1653 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001654 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001655 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001656 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001657 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001658 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001659 }
1660 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001661
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001662 @Override
1663 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001664 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001665 try {
1666 mConnector.execute("firewall", enabled ? "enable" : "disable");
1667 mFirewallEnabled = enabled;
1668 } catch (NativeDaemonConnectorException e) {
1669 throw e.rethrowAsParcelableException();
1670 }
1671 }
1672
1673 @Override
1674 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001675 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001676 return mFirewallEnabled;
1677 }
1678
1679 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001680 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001681 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001682 Preconditions.checkState(mFirewallEnabled);
1683 final String rule = allow ? ALLOW : DENY;
1684 try {
1685 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1686 } catch (NativeDaemonConnectorException e) {
1687 throw e.rethrowAsParcelableException();
1688 }
1689 }
1690
1691 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001692 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001693 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001694 Preconditions.checkState(mFirewallEnabled);
1695 final String rule = allow ? ALLOW : DENY;
1696 try {
1697 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1698 } catch (NativeDaemonConnectorException e) {
1699 throw e.rethrowAsParcelableException();
1700 }
1701 }
1702
1703 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001704 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001705 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001706 Preconditions.checkState(mFirewallEnabled);
1707 final String rule = allow ? ALLOW : DENY;
1708 try {
1709 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1710 } catch (NativeDaemonConnectorException e) {
1711 throw e.rethrowAsParcelableException();
1712 }
1713 }
1714
1715 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001716 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001717 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001718 Preconditions.checkState(mFirewallEnabled);
1719 final String rule = allow ? ALLOW : DENY;
1720 try {
1721 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1722 } catch (NativeDaemonConnectorException e) {
1723 throw e.rethrowAsParcelableException();
1724 }
1725 }
1726
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001727 private static void enforceSystemUid() {
1728 final int uid = Binder.getCallingUid();
1729 if (uid != Process.SYSTEM_UID) {
1730 throw new SecurityException("Only available to AID_SYSTEM");
1731 }
1732 }
1733
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001734 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001735 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1736 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1737 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001738 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001739 } catch (NativeDaemonConnectorException e) {
1740 throw new IllegalStateException(
1741 "Error communicating with native deamon to set interface for pid" + iface, e);
1742 }
1743 }
1744
1745 @Override
1746 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1747 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1748 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001749 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001750 } catch (NativeDaemonConnectorException e) {
1751 throw new IllegalStateException(
1752 "Error communicating with native deamon to clear interface for pid " + pid, e);
1753 }
1754 }
1755
Lorenzo Colitti79751842013-02-28 16:16:03 +09001756 @Override
1757 public void startClatd(String interfaceName) throws IllegalStateException {
1758 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1759
1760 try {
1761 mConnector.execute("clatd", "start", interfaceName);
1762 } catch (NativeDaemonConnectorException e) {
1763 throw e.rethrowAsParcelableException();
1764 }
1765 }
1766
1767 @Override
1768 public void stopClatd() throws IllegalStateException {
1769 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1770
1771 try {
1772 mConnector.execute("clatd", "stop");
1773 } catch (NativeDaemonConnectorException e) {
1774 throw e.rethrowAsParcelableException();
1775 }
1776 }
1777
1778 @Override
1779 public boolean isClatdStarted() {
1780 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1781
1782 final NativeDaemonEvent event;
1783 try {
1784 event = mConnector.execute("clatd", "status");
1785 } catch (NativeDaemonConnectorException e) {
1786 throw e.rethrowAsParcelableException();
1787 }
1788
1789 event.checkCode(ClatdStatusResult);
1790 return event.getMessage().endsWith("started");
1791 }
1792
Mattias Falk8b47b362011-08-23 14:15:13 +02001793 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001794 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001795 public void monitor() {
1796 if (mConnector != null) {
1797 mConnector.monitor();
1798 }
1799 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001800
1801 @Override
1802 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1803 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1804
Robert Greenwalt470fd722012-01-18 12:51:15 -08001805 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1806 mConnector.dump(fd, pw, args);
1807 pw.println();
1808
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001809 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1810
1811 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001812 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1813 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001814 }
1815
1816 synchronized (mUidRejectOnQuota) {
1817 pw.print("UID reject on quota ifaces: [");
1818 final int size = mUidRejectOnQuota.size();
1819 for (int i = 0; i < size; i++) {
1820 pw.print(mUidRejectOnQuota.keyAt(i));
1821 if (i < size - 1) pw.print(",");
1822 }
1823 pw.println("]");
1824 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001825
1826 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001827 }
San Mehat873f2142010-01-14 10:25:07 -08001828}