blob: 9a0d6485b3152a8d7889581f1f0d1defa9c25825 [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;
San Mehat873f2142010-01-14 10:25:07 -0800139 }
140
141 /**
142 * Binder context for this service
143 */
144 private Context mContext;
145
146 /**
147 * connector object for communicating with netd
148 */
149 private NativeDaemonConnector mConnector;
150
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700151 private final Handler mMainHandler = new Handler();
152
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700153 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700154 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700155
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800156 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
157 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800158
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700159 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
160
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700161 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700162 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700163 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700164 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700165 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700166 /** Set of UIDs with active reject rules. */
167 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
168
Haoyu Bai04124232012-06-28 15:26:19 -0700169 private Object mIdleTimerLock = new Object();
170 /** Set of interfaces with active idle timers. */
171 private static class IdleTimerParams {
172 public final int timeout;
173 public final String label;
174 public int networkCount;
175
176 IdleTimerParams(int timeout, String label) {
177 this.timeout = timeout;
178 this.label = label;
179 this.networkCount = 1;
180 }
181 }
182 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
183
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700184 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700185 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700186
San Mehat873f2142010-01-14 10:25:07 -0800187 /**
188 * Constructs a new NetworkManagementService instance
189 *
190 * @param context Binder context for this service
191 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900192 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800193 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800194
Marco Nelissen62dbb222010-02-18 10:56:30 -0800195 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
196 return;
197 }
198
San Mehat873f2142010-01-14 10:25:07 -0800199 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900200 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700201 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700202
203 // Add ourself to the Watchdog monitors.
204 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700205 }
206
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900207 static NetworkManagementService create(Context context,
208 String socket) throws InterruptedException {
209 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700210 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700211 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
212 service.mThread.start();
213 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700214 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700215 if (DBG) Slog.d(TAG, "Connected");
216 return service;
San Mehat873f2142010-01-14 10:25:07 -0800217 }
218
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900219 public static NetworkManagementService create(Context context) throws InterruptedException {
220 return create(context, NETD_SOCKET_NAME);
221 }
222
Jeff Sharkey350083e2011-06-29 10:45:16 -0700223 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700224 prepareNativeDaemon();
225 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700226 }
227
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800228 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800229 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800230 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800231 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800232 }
233
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800234 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800235 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800236 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800238 }
239
240 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700241 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800242 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700243 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800244 final int length = mObservers.beginBroadcast();
245 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800246 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800247 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
248 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900249 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700250 }
251 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800252 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700253 }
254
255 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700256 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700257 * (typically, an Ethernet cable has been plugged-in or unplugged).
258 */
259 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800260 final int length = mObservers.beginBroadcast();
261 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700262 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800263 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
264 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900265 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800266 }
267 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800268 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800269 }
270
271 /**
272 * Notify our observers of an interface addition.
273 */
274 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800275 final int length = mObservers.beginBroadcast();
276 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800277 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800278 mObservers.getBroadcastItem(i).interfaceAdded(iface);
279 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900280 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800281 }
282 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800283 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800284 }
285
286 /**
287 * Notify our observers of an interface removal.
288 */
289 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700290 // netd already clears out quota and alerts for removed ifaces; update
291 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700292 mActiveAlerts.remove(iface);
293 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700294
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 final int length = mObservers.beginBroadcast();
296 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800297 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800298 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
299 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900300 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800301 }
302 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800303 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800304 }
305
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700306 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700307 * Notify our observers of a limit reached.
308 */
309 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800310 final int length = mObservers.beginBroadcast();
311 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700312 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800313 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
314 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900315 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700316 }
317 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800318 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700319 }
320
321 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700322 * Notify our observers of a change in the data activity state of the interface
323 */
324 private void notifyInterfaceClassActivity(String label, boolean active) {
325 final int length = mObservers.beginBroadcast();
326 for (int i = 0; i < length; i++) {
327 try {
328 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
329 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900330 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700331 }
332 }
333 mObservers.finishBroadcast();
334 }
335
336 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700337 * Prepare native daemon once connected, enabling modules and pushing any
338 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700339 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700340 private void prepareNativeDaemon() {
341 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700342
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700343 // only enable bandwidth control when support exists
344 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
345 if (hasKernelSupport) {
346 Slog.d(TAG, "enabling bandwidth control");
347 try {
348 mConnector.execute("bandwidth", "enable");
349 mBandwidthControlEnabled = true;
350 } catch (NativeDaemonConnectorException e) {
351 Log.wtf(TAG, "problem enabling bandwidth controls", e);
352 }
353 } else {
354 Slog.d(TAG, "not enabling bandwidth control");
355 }
356
357 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
358
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700359 if (mBandwidthControlEnabled) {
360 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700361 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700362 .noteNetworkStatsEnabled();
363 } catch (RemoteException e) {
364 }
365 }
366
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700367 // push any existing quota or UID rules
368 synchronized (mQuotaLock) {
369 int size = mActiveQuotas.size();
370 if (size > 0) {
371 Slog.d(TAG, "pushing " + size + " active quota rules");
372 final HashMap<String, Long> activeQuotas = mActiveQuotas;
373 mActiveQuotas = Maps.newHashMap();
374 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
375 setInterfaceQuota(entry.getKey(), entry.getValue());
376 }
377 }
378
379 size = mActiveAlerts.size();
380 if (size > 0) {
381 Slog.d(TAG, "pushing " + size + " active alert rules");
382 final HashMap<String, Long> activeAlerts = mActiveAlerts;
383 mActiveAlerts = Maps.newHashMap();
384 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
385 setInterfaceAlert(entry.getKey(), entry.getValue());
386 }
387 }
388
389 size = mUidRejectOnQuota.size();
390 if (size > 0) {
391 Slog.d(TAG, "pushing " + size + " active uid rules");
392 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
393 mUidRejectOnQuota = new SparseBooleanArray();
394 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
395 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
396 }
397 }
398 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700399
400 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700401 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700402 }
San Mehat4d02d002010-01-22 16:07:46 -0800403
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900404 /**
405 * Notify our observers of a new or updated interface address.
406 */
407 private void notifyAddressUpdated(String address, String iface, int flags, int scope) {
408 final int length = mObservers.beginBroadcast();
409 for (int i = 0; i < length; i++) {
410 try {
411 mObservers.getBroadcastItem(i).addressUpdated(address, iface, flags, scope);
412 } catch (RemoteException e) {
413 } catch (RuntimeException e) {
414 }
415 }
416 mObservers.finishBroadcast();
417 }
418
419 /**
420 * Notify our observers of a deleted interface address.
421 */
422 private void notifyAddressRemoved(String address, String iface, int flags, int scope) {
423 final int length = mObservers.beginBroadcast();
424 for (int i = 0; i < length; i++) {
425 try {
426 mObservers.getBroadcastItem(i).addressRemoved(address, iface, flags, scope);
427 } catch (RemoteException e) {
428 } catch (RuntimeException e) {
429 }
430 }
431 mObservers.finishBroadcast();
432 }
433
San Mehat873f2142010-01-14 10:25:07 -0800434 //
435 // Netd Callback handling
436 //
437
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700438 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
439 @Override
San Mehat873f2142010-01-14 10:25:07 -0800440 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700441 // event is dispatched from internal NDC thread, so we prepare the
442 // daemon back on main thread.
443 if (mConnectedSignal != null) {
444 mConnectedSignal.countDown();
445 mConnectedSignal = null;
446 } else {
447 mMainHandler.post(new Runnable() {
448 @Override
449 public void run() {
450 prepareNativeDaemon();
451 }
452 });
453 }
San Mehat873f2142010-01-14 10:25:07 -0800454 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700455
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700456 @Override
San Mehat873f2142010-01-14 10:25:07 -0800457 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900458 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700459 switch (code) {
460 case NetdResponseCode.InterfaceChange:
461 /*
462 * a network interface change occured
463 * Format: "NNN Iface added <name>"
464 * "NNN Iface removed <name>"
465 * "NNN Iface changed <name> <up/down>"
466 * "NNN Iface linkstatus <name> <up/down>"
467 */
468 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900469 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700470 }
471 if (cooked[2].equals("added")) {
472 notifyInterfaceAdded(cooked[3]);
473 return true;
474 } else if (cooked[2].equals("removed")) {
475 notifyInterfaceRemoved(cooked[3]);
476 return true;
477 } else if (cooked[2].equals("changed") && cooked.length == 5) {
478 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
479 return true;
480 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
481 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
482 return true;
483 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900484 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700485 // break;
486 case NetdResponseCode.BandwidthControl:
487 /*
488 * Bandwidth control needs some attention
489 * Format: "NNN limit alert <alertName> <ifaceName>"
490 */
491 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900492 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700493 }
494 if (cooked[2].equals("alert")) {
495 notifyLimitReached(cooked[3], cooked[4]);
496 return true;
497 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900498 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700499 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700500 case NetdResponseCode.InterfaceClassActivity:
501 /*
502 * An network interface class state changed (active/idle)
503 * Format: "NNN IfaceClass <active/idle> <label>"
504 */
505 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900506 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700507 }
508 boolean isActive = cooked[2].equals("active");
509 notifyInterfaceClassActivity(cooked[3], isActive);
510 return true;
511 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900512 case NetdResponseCode.InterfaceAddressChange:
513 /*
514 * A network address change occurred
515 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
516 * "NNN Address removed <addr> <iface> <flags> <scope>"
517 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900518 if (cooked.length < 7 || !cooked[1].equals("Address")) {
519 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900520 }
521
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700522 int flags;
523 int scope;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900524 try {
525 flags = Integer.parseInt(cooked[5]);
526 scope = Integer.parseInt(cooked[6]);
527 } catch(NumberFormatException e) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900528 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900529 }
530
531 if (cooked[2].equals("updated")) {
532 notifyAddressUpdated(cooked[3], cooked[4], flags, scope);
533 } else {
534 notifyAddressRemoved(cooked[3], cooked[4], flags, scope);
535 }
536 return true;
537 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700538 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800539 }
540 return false;
San Mehat873f2142010-01-14 10:25:07 -0800541 }
542 }
543
San Mehated4fc8a2010-01-22 12:28:36 -0800544
San Mehat873f2142010-01-14 10:25:07 -0800545 //
546 // INetworkManagementService members
547 //
548
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800549 @Override
550 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800551 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700552 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800553 return NativeDaemonEvent.filterMessageList(
554 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700555 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800556 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700557 }
San Mehated4fc8a2010-01-22 12:28:36 -0800558 }
559
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800560 @Override
561 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800562 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800563
564 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700565 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800566 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700567 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800568 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700569 }
San Mehated4fc8a2010-01-22 12:28:36 -0800570
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800571 event.checkCode(InterfaceGetCfgResult);
572
573 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
574 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800575
Kenny Roota80ce062010-06-01 13:23:53 -0700576 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800577 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700578 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800579 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800580 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800581 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700582 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800583 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800584 } catch (IllegalArgumentException iae) {
585 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700586 }
587
588 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800589 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800590 } catch (NumberFormatException nfe) {
591 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700592 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800593
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800594 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
595 while (st.hasMoreTokens()) {
596 cfg.setFlag(st.nextToken());
597 }
Kenny Roota80ce062010-06-01 13:23:53 -0700598 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800599 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800600 }
San Mehated4fc8a2010-01-22 12:28:36 -0800601 return cfg;
602 }
603
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800604 @Override
605 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800606 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800607 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800608 if (linkAddr == null || linkAddr.getAddress() == null) {
609 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800610 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800611
612 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800613 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800614 linkAddr.getNetworkPrefixLength());
615 for (String flag : cfg.getFlags()) {
616 cmd.appendArg(flag);
617 }
618
Kenny Roota80ce062010-06-01 13:23:53 -0700619 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800620 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700621 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800622 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700623 }
San Mehat873f2142010-01-14 10:25:07 -0800624 }
625
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800626 @Override
627 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800628 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800629 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800630 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800631 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700632 }
633
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800634 @Override
635 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800636 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800637 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800638 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800639 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700640 }
641
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800642 @Override
643 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800644 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700645 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800646 mConnector.execute(
647 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700648 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800649 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700650 }
651 }
652
Irfan Sherifff5600612011-06-16 10:26:28 -0700653 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
654 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800655 @Override
656 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800657 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700658 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800659 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700660 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800661 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700662 }
663 }
664
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800665 @Override
666 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800667 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700668 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800669 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700670 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800671 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700672 }
673 }
674
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800675 @Override
676 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800677 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700678 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800679 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700680 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800681 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700682 }
683 }
684
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800685 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700686 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800687 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700688 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700689 }
690
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800691 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700692 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800693 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700694 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700695 }
696
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800697 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700698 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800699 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700700 modifyRoute(interfaceName, ADD, route, SECONDARY);
701 }
702
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800703 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700704 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800705 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700706 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
707 }
708
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800709 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
710 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700711
712 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800713 final LinkAddress la = route.getDestination();
714 cmd.appendArg(la.getAddress().getHostAddress());
715 cmd.appendArg(la.getNetworkPrefixLength());
716
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700717 if (route.getGateway() == null) {
718 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800719 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700720 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800721 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700722 }
723 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800724 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700725 }
726
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800727 try {
728 mConnector.execute(cmd);
729 } catch (NativeDaemonConnectorException e) {
730 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700731 }
732 }
733
734 private ArrayList<String> readRouteList(String filename) {
735 FileInputStream fstream = null;
736 ArrayList<String> list = new ArrayList<String>();
737
738 try {
739 fstream = new FileInputStream(filename);
740 DataInputStream in = new DataInputStream(fstream);
741 BufferedReader br = new BufferedReader(new InputStreamReader(in));
742 String s;
743
744 // throw away the title line
745
746 while (((s = br.readLine()) != null) && (s.length() != 0)) {
747 list.add(s);
748 }
749 } catch (IOException ex) {
750 // return current list, possibly empty
751 } finally {
752 if (fstream != null) {
753 try {
754 fstream.close();
755 } catch (IOException ex) {}
756 }
757 }
758
759 return list;
760 }
761
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800762 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700763 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800764 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700765 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
766
767 // v4 routes listed as:
768 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
769 for (String s : readRouteList("/proc/net/route")) {
770 String[] fields = s.split("\t");
771
772 if (fields.length > 7) {
773 String iface = fields[0];
774
775 if (interfaceName.equals(iface)) {
776 String dest = fields[1];
777 String gate = fields[2];
778 String flags = fields[3]; // future use?
779 String mask = fields[7];
780 try {
781 // address stored as a hex string, ex: 0014A8C0
782 InetAddress destAddr =
783 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
784 int prefixLength =
785 NetworkUtils.netmaskIntToPrefixLength(
786 (int)Long.parseLong(mask, 16));
787 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
788
789 // address stored as a hex string, ex 0014A8C0
790 InetAddress gatewayAddr =
791 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
792
Wink Saville7b5fd052013-03-15 05:07:04 +0000793 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700794 routes.add(route);
795 } catch (Exception e) {
796 Log.e(TAG, "Error parsing route " + s + " : " + e);
797 continue;
798 }
799 }
800 }
801 }
802
803 // v6 routes listed as:
804 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
805 for (String s : readRouteList("/proc/net/ipv6_route")) {
806 String[]fields = s.split("\\s+");
807 if (fields.length > 9) {
808 String iface = fields[9].trim();
809 if (interfaceName.equals(iface)) {
810 String dest = fields[0];
811 String prefix = fields[1];
812 String gate = fields[4];
813
814 try {
815 // prefix length stored as a hex string, ex 40
816 int prefixLength = Integer.parseInt(prefix, 16);
817
818 // address stored as a 32 char hex string
819 // ex fe800000000000000000000000000000
820 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
821 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
822
823 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
824
Wink Saville7b5fd052013-03-15 05:07:04 +0000825 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700826 routes.add(route);
827 } catch (Exception e) {
828 Log.e(TAG, "Error parsing route " + s + " : " + e);
829 continue;
830 }
831 }
832 }
833 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800834 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700835 }
836
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800837 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900838 public void setMtu(String iface, int mtu) {
839 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
840
841 final NativeDaemonEvent event;
842 try {
843 event = mConnector.execute("interface", "setmtu", iface, mtu);
844 } catch (NativeDaemonConnectorException e) {
845 throw e.rethrowAsParcelableException();
846 }
847 }
848
849 @Override
San Mehat873f2142010-01-14 10:25:07 -0800850 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800851 // TODO: remove from aidl if nobody calls externally
852 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800853
Joe Onorato8a9b2202010-02-26 18:56:32 -0800854 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800855 }
856
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800857 @Override
San Mehat873f2142010-01-14 10:25:07 -0800858 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800859 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800860
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800861 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700862 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800863 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700864 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800865 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700866 }
San Mehat873f2142010-01-14 10:25:07 -0800867
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800868 // 211 Forwarding enabled
869 event.checkCode(IpFwdStatusResult);
870 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800871 }
872
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800873 @Override
874 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800875 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800876 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800877 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800878 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800879 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800880 }
San Mehat873f2142010-01-14 10:25:07 -0800881 }
882
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800883 @Override
884 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800885 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700886 // cmd is "tether start first_start first_stop second_start second_stop ..."
887 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800888
889 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700890 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800891 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700892 }
Kenny Roota80ce062010-06-01 13:23:53 -0700893
894 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800895 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700896 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800897 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700898 }
San Mehat873f2142010-01-14 10:25:07 -0800899 }
900
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800901 @Override
902 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800903 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700904 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800905 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700906 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800907 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700908 }
San Mehat873f2142010-01-14 10:25:07 -0800909 }
910
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800911 @Override
912 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800913 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800914
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800915 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700916 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800917 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700918 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800919 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700920 }
San Mehat873f2142010-01-14 10:25:07 -0800921
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800922 // 210 Tethering services started
923 event.checkCode(TetherStatusResult);
924 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800925 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700926
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800927 @Override
928 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700930 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700932 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800933 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700934 }
San Mehat873f2142010-01-14 10:25:07 -0800935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
San Mehat873f2142010-01-14 10:25:07 -0800938 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800939 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700940 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800941 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700942 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800943 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700944 }
San Mehat873f2142010-01-14 10:25:07 -0800945 }
946
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 @Override
948 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800949 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700950 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800951 return NativeDaemonEvent.filterMessageList(
952 mConnector.executeForList("tether", "interface", "list"),
953 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700954 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800955 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700956 }
San Mehat873f2142010-01-14 10:25:07 -0800957 }
958
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800959 @Override
960 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800961 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800962
963 final Command cmd = new Command("tether", "dns", "set");
964 for (String s : dns) {
965 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
966 }
967
San Mehat873f2142010-01-14 10:25:07 -0800968 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800969 mConnector.execute(cmd);
970 } catch (NativeDaemonConnectorException e) {
971 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800972 }
973 }
974
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800975 @Override
976 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800977 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700978 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800979 return NativeDaemonEvent.filterMessageList(
980 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700981 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800982 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700983 }
San Mehat873f2142010-01-14 10:25:07 -0800984 }
985
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700987 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800988 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700989
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800990 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
991 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800992 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800993 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800994 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800995 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
996 .getInterfaceAddresses();
997 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800998 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800999 InetAddress addr = NetworkUtils.getNetworkPart(
1000 ia.getAddress(), ia.getNetworkPrefixLength());
1001 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001002 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001003 }
1004
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001005 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001006 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001007 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001008 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001009 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001010 }
1011
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001012 @Override
1013 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001014 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001015 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001016 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001017 } catch (SocketException e) {
1018 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001019 }
San Mehat873f2142010-01-14 10:25:07 -08001020 }
1021
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001022 @Override
1023 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001024 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001025 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001026 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 } catch (SocketException e) {
1028 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001029 }
San Mehat873f2142010-01-14 10:25:07 -08001030 }
San Mehat72759df2010-01-19 13:50:37 -08001031
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001032 @Override
1033 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001034 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001035 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001036 return NativeDaemonEvent.filterMessageList(
1037 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001038 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001039 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001040 }
San Mehat72759df2010-01-19 13:50:37 -08001041 }
1042
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001043 @Override
1044 public void attachPppd(
1045 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001046 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001047 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001048 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001049 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1050 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1051 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001052 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001053 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001054 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001055 }
1056 }
1057
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001058 @Override
1059 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001060 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001061 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001062 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001063 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001064 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001065 }
San Mehat72759df2010-01-19 13:50:37 -08001066 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001067
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001068 @Override
1069 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001070 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001071 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001072 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001073 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001074 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001075 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001076 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001077 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001078 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001079 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001080 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001081 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001082 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001083 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001084 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001085 }
1086
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001087 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001088 switch (wifiConfig.getAuthType()) {
1089 case KeyMgmt.WPA_PSK:
1090 return "wpa-psk";
1091 case KeyMgmt.WPA2_PSK:
1092 return "wpa2-psk";
1093 default:
1094 return "open";
1095 }
1096 }
1097
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001098 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001099 @Override
1100 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001101 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001102 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001103 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001104 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001105 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001106 }
1107 }
1108
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001109 @Override
1110 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001111 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001112 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001113 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001114 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001115 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001116 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001117 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001118 }
1119
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001120 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001121 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001122 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001123 try {
1124 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001125 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001126 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001127 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001128 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001129 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001130 }
1131 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001132 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001133 }
1134 }
San Mehat91cac642010-03-31 14:31:36 -07001135
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001136 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001137 public void addIdleTimer(String iface, int timeout, String label) {
1138 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1139
1140 if (DBG) Slog.d(TAG, "Adding idletimer");
1141
1142 synchronized (mIdleTimerLock) {
1143 IdleTimerParams params = mActiveIdleTimers.get(iface);
1144 if (params != null) {
1145 // the interface already has idletimer, update network count
1146 params.networkCount++;
1147 return;
1148 }
1149
1150 try {
1151 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1152 } catch (NativeDaemonConnectorException e) {
1153 throw e.rethrowAsParcelableException();
1154 }
1155 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1156 }
1157 }
1158
1159 @Override
1160 public void removeIdleTimer(String iface) {
1161 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1162
1163 if (DBG) Slog.d(TAG, "Removing idletimer");
1164
1165 synchronized (mIdleTimerLock) {
1166 IdleTimerParams params = mActiveIdleTimers.get(iface);
1167 if (params == null || --(params.networkCount) > 0) {
1168 return;
1169 }
1170
1171 try {
1172 mConnector.execute("idletimer", "remove", iface,
1173 Integer.toString(params.timeout), params.label);
1174 } catch (NativeDaemonConnectorException e) {
1175 throw e.rethrowAsParcelableException();
1176 }
1177 mActiveIdleTimers.remove(iface);
1178 }
1179 }
1180
1181 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001182 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001183 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001184 try {
1185 return mStatsFactory.readNetworkStatsSummaryDev();
1186 } catch (IOException e) {
1187 throw new IllegalStateException(e);
1188 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001189 }
1190
1191 @Override
1192 public NetworkStats getNetworkStatsSummaryXt() {
1193 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001194 try {
1195 return mStatsFactory.readNetworkStatsSummaryXt();
1196 } catch (IOException e) {
1197 throw new IllegalStateException(e);
1198 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001199 }
1200
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001201 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001202 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001203 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001204 try {
1205 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1206 } catch (IOException e) {
1207 throw new IllegalStateException(e);
1208 }
San Mehat91cac642010-03-31 14:31:36 -07001209 }
1210
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001211 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001212 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001213 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001214
Jeff Sharkey350083e2011-06-29 10:45:16 -07001215 // silently discard when control disabled
1216 // TODO: eventually migrate to be always enabled
1217 if (!mBandwidthControlEnabled) return;
1218
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001219 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001220 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001221 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001222 }
1223
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001224 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001225 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001226 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001227 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001228 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001229 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001230 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001231 }
1232 }
1233
1234 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001235 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001236 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001237
Jeff Sharkey350083e2011-06-29 10:45:16 -07001238 // silently discard when control disabled
1239 // TODO: eventually migrate to be always enabled
1240 if (!mBandwidthControlEnabled) return;
1241
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001242 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001243 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001244 // TODO: eventually consider throwing
1245 return;
1246 }
1247
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001248 mActiveQuotas.remove(iface);
1249 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001250
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001251 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001252 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001254 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001255 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001256 }
1257 }
1258 }
1259
1260 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001261 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001262 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001263
1264 // silently discard when control disabled
1265 // TODO: eventually migrate to be always enabled
1266 if (!mBandwidthControlEnabled) return;
1267
1268 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001269 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001270 throw new IllegalStateException("setting alert requires existing quota on iface");
1271 }
1272
1273 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001274 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001275 throw new IllegalStateException("iface " + iface + " already has alert");
1276 }
1277
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001278 try {
1279 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001280 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001281 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001282 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001283 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001284 }
1285 }
1286 }
1287
1288 @Override
1289 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001290 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001291
1292 // silently discard when control disabled
1293 // TODO: eventually migrate to be always enabled
1294 if (!mBandwidthControlEnabled) return;
1295
1296 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001297 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001298 // TODO: eventually consider throwing
1299 return;
1300 }
1301
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001302 try {
1303 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001304 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001305 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001306 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001307 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001308 }
1309 }
1310 }
1311
1312 @Override
1313 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001314 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001315
1316 // silently discard when control disabled
1317 // TODO: eventually migrate to be always enabled
1318 if (!mBandwidthControlEnabled) return;
1319
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001320 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001321 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001322 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001323 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001324 }
1325 }
1326
1327 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001328 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001329 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001330
Jeff Sharkey350083e2011-06-29 10:45:16 -07001331 // silently discard when control disabled
1332 // TODO: eventually migrate to be always enabled
1333 if (!mBandwidthControlEnabled) return;
1334
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001335 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001336 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1337 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1338 // TODO: eventually consider throwing
1339 return;
1340 }
1341
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001342 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001343 mConnector.execute("bandwidth",
1344 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001345 if (rejectOnQuotaInterfaces) {
1346 mUidRejectOnQuota.put(uid, true);
1347 } else {
1348 mUidRejectOnQuota.delete(uid);
1349 }
1350 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001351 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001352 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001353 }
1354 }
1355
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001356 @Override
1357 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001358 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001359 return mBandwidthControlEnabled;
1360 }
1361
1362 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001363 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001364 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001365 try {
1366 return mStatsFactory.readNetworkStatsDetail(uid);
1367 } catch (IOException e) {
1368 throw new IllegalStateException(e);
1369 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001370 }
1371
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001372 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001373 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001374 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001375
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001376 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001377 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001378 final NativeDaemonEvent[] events = mConnector.executeForList(
1379 "bandwidth", "gettetherstats");
1380 for (NativeDaemonEvent event : events) {
1381 if (event.getCode() != TetheringStatsListResult) continue;
1382
1383 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1384 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1385 try {
1386 final String ifaceIn = tok.nextToken();
1387 final String ifaceOut = tok.nextToken();
1388
1389 final NetworkStats.Entry entry = new NetworkStats.Entry();
1390 entry.iface = ifaceOut;
1391 entry.uid = UID_TETHERING;
1392 entry.set = SET_DEFAULT;
1393 entry.tag = TAG_NONE;
1394 entry.rxBytes = Long.parseLong(tok.nextToken());
1395 entry.rxPackets = Long.parseLong(tok.nextToken());
1396 entry.txBytes = Long.parseLong(tok.nextToken());
1397 entry.txPackets = Long.parseLong(tok.nextToken());
1398 stats.combineValues(entry);
1399 } catch (NoSuchElementException e) {
1400 throw new IllegalStateException("problem parsing tethering stats: " + event);
1401 } catch (NumberFormatException e) {
1402 throw new IllegalStateException("problem parsing tethering stats: " + event);
1403 }
1404 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001405 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001406 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001407 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001408 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001409 }
1410
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001411 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001412 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001413 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001414 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001415 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001416 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001417 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001418 }
1419 }
1420
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001421 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001422 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001423 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001424
Robert Greenwalt8058f622012-11-09 10:52:27 -08001425 final Command cmd = new Command("resolver", "setifdns", iface,
1426 (domains == null ? "" : domains));
1427
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001428 for (String s : servers) {
1429 InetAddress a = NetworkUtils.numericToInetAddress(s);
1430 if (a.isAnyLocalAddress() == false) {
1431 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001432 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001433 }
1434
1435 try {
1436 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001437 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001438 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001439 }
1440 }
1441
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001442 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001443 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1444 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1445 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001446 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001447 "uid", "add", iface, uid_start, uid_end);
1448 } catch (NativeDaemonConnectorException e) {
1449 throw e.rethrowAsParcelableException();
1450 }
1451 }
1452
1453 @Override
1454 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1455 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1456 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001457 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001458 "uid", "remove", iface, uid_start, uid_end);
1459 } catch (NativeDaemonConnectorException e) {
1460 throw e.rethrowAsParcelableException();
1461 }
1462 }
1463
1464 @Override
1465 public void setMarkedForwarding(String iface) {
1466 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1467 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001468 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001469 } catch (NativeDaemonConnectorException e) {
1470 throw e.rethrowAsParcelableException();
1471 }
1472 }
1473
1474 @Override
1475 public void clearMarkedForwarding(String iface) {
1476 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1477 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001478 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1479 } catch (NativeDaemonConnectorException e) {
1480 throw e.rethrowAsParcelableException();
1481 }
1482 }
1483
1484 @Override
1485 public int getMarkForUid(int uid) {
1486 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1487 final NativeDaemonEvent event;
1488 try {
1489 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1490 } catch (NativeDaemonConnectorException e) {
1491 throw e.rethrowAsParcelableException();
1492 }
1493 event.checkCode(GetMarkResult);
1494 return Integer.parseInt(event.getMessage());
1495 }
1496
1497 @Override
1498 public int getMarkForProtect() {
1499 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1500 final NativeDaemonEvent event;
1501 try {
1502 event = mConnector.execute("interface", "fwmark", "get", "protect");
1503 } catch (NativeDaemonConnectorException e) {
1504 throw e.rethrowAsParcelableException();
1505 }
1506 event.checkCode(GetMarkResult);
1507 return Integer.parseInt(event.getMessage());
1508 }
1509
1510 @Override
1511 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1513 try {
1514 LinkAddress dest = route.getDestination();
1515 mConnector.execute("interface", "fwmark", "route", "add", iface,
1516 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1517 } catch (NativeDaemonConnectorException e) {
1518 throw e.rethrowAsParcelableException();
1519 }
1520 }
1521
1522 @Override
1523 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1524 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1525 try {
1526 LinkAddress dest = route.getDestination();
1527 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1528 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001529 } catch (NativeDaemonConnectorException e) {
1530 throw e.rethrowAsParcelableException();
1531 }
1532 }
1533
1534 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001535 public void setHostExemption(LinkAddress host) {
1536 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1537 try {
1538 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1539 } catch (NativeDaemonConnectorException e) {
1540 throw e.rethrowAsParcelableException();
1541 }
1542 }
1543
1544 @Override
1545 public void clearHostExemption(LinkAddress host) {
1546 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1547 try {
1548 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1549 } catch (NativeDaemonConnectorException e) {
1550 throw e.rethrowAsParcelableException();
1551 }
1552 }
1553
1554 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001555 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1556 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1557 try {
1558 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1559 } catch (NativeDaemonConnectorException e) {
1560 throw e.rethrowAsParcelableException();
1561 }
1562 }
1563
1564 @Override
1565 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1566 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1567 try {
1568 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1569 } catch (NativeDaemonConnectorException e) {
1570 throw e.rethrowAsParcelableException();
1571 }
1572 }
1573
1574 @Override
1575 public void clearDnsInterfaceMaps() {
1576 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1577 try {
1578 mConnector.execute("resolver", "clearifacemapping");
1579 } catch (NativeDaemonConnectorException e) {
1580 throw e.rethrowAsParcelableException();
1581 }
1582 }
1583
1584
1585 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001586 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001587 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001588 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001589 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001590 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001591 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001592 }
1593 }
1594
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001595 @Override
1596 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001597 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001598 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001599 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001600 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001601 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001602 }
1603 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001604
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001605 @Override
1606 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001607 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001608 try {
1609 mConnector.execute("firewall", enabled ? "enable" : "disable");
1610 mFirewallEnabled = enabled;
1611 } catch (NativeDaemonConnectorException e) {
1612 throw e.rethrowAsParcelableException();
1613 }
1614 }
1615
1616 @Override
1617 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001618 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001619 return mFirewallEnabled;
1620 }
1621
1622 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001623 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001624 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001625 Preconditions.checkState(mFirewallEnabled);
1626 final String rule = allow ? ALLOW : DENY;
1627 try {
1628 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1629 } catch (NativeDaemonConnectorException e) {
1630 throw e.rethrowAsParcelableException();
1631 }
1632 }
1633
1634 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001635 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001636 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001637 Preconditions.checkState(mFirewallEnabled);
1638 final String rule = allow ? ALLOW : DENY;
1639 try {
1640 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1641 } catch (NativeDaemonConnectorException e) {
1642 throw e.rethrowAsParcelableException();
1643 }
1644 }
1645
1646 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001647 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001648 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001649 Preconditions.checkState(mFirewallEnabled);
1650 final String rule = allow ? ALLOW : DENY;
1651 try {
1652 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1653 } catch (NativeDaemonConnectorException e) {
1654 throw e.rethrowAsParcelableException();
1655 }
1656 }
1657
1658 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001659 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001660 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001661 Preconditions.checkState(mFirewallEnabled);
1662 final String rule = allow ? ALLOW : DENY;
1663 try {
1664 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1665 } catch (NativeDaemonConnectorException e) {
1666 throw e.rethrowAsParcelableException();
1667 }
1668 }
1669
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001670 private static void enforceSystemUid() {
1671 final int uid = Binder.getCallingUid();
1672 if (uid != Process.SYSTEM_UID) {
1673 throw new SecurityException("Only available to AID_SYSTEM");
1674 }
1675 }
1676
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001677 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001678 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1679 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1680 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001681 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001682 } catch (NativeDaemonConnectorException e) {
1683 throw new IllegalStateException(
1684 "Error communicating with native deamon to set interface for pid" + iface, e);
1685 }
1686 }
1687
1688 @Override
1689 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1690 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1691 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001692 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001693 } catch (NativeDaemonConnectorException e) {
1694 throw new IllegalStateException(
1695 "Error communicating with native deamon to clear interface for pid " + pid, e);
1696 }
1697 }
1698
Lorenzo Colitti79751842013-02-28 16:16:03 +09001699 @Override
1700 public void startClatd(String interfaceName) throws IllegalStateException {
1701 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1702
1703 try {
1704 mConnector.execute("clatd", "start", interfaceName);
1705 } catch (NativeDaemonConnectorException e) {
1706 throw e.rethrowAsParcelableException();
1707 }
1708 }
1709
1710 @Override
1711 public void stopClatd() throws IllegalStateException {
1712 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1713
1714 try {
1715 mConnector.execute("clatd", "stop");
1716 } catch (NativeDaemonConnectorException e) {
1717 throw e.rethrowAsParcelableException();
1718 }
1719 }
1720
1721 @Override
1722 public boolean isClatdStarted() {
1723 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1724
1725 final NativeDaemonEvent event;
1726 try {
1727 event = mConnector.execute("clatd", "status");
1728 } catch (NativeDaemonConnectorException e) {
1729 throw e.rethrowAsParcelableException();
1730 }
1731
1732 event.checkCode(ClatdStatusResult);
1733 return event.getMessage().endsWith("started");
1734 }
1735
Mattias Falk8b47b362011-08-23 14:15:13 +02001736 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001737 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001738 public void monitor() {
1739 if (mConnector != null) {
1740 mConnector.monitor();
1741 }
1742 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001743
1744 @Override
1745 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1746 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1747
Robert Greenwalt470fd722012-01-18 12:51:15 -08001748 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1749 mConnector.dump(fd, pw, args);
1750 pw.println();
1751
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001752 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1753
1754 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001755 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1756 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001757 }
1758
1759 synchronized (mUidRejectOnQuota) {
1760 pw.print("UID reject on quota ifaces: [");
1761 final int size = mUidRejectOnQuota.size();
1762 for (int i = 0; i < size; i++) {
1763 pw.print(mUidRejectOnQuota.keyAt(i));
1764 if (i < size - 1) pw.print(",");
1765 }
1766 pw.println("]");
1767 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001768
1769 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001770 }
San Mehat873f2142010-01-14 10:25:07 -08001771}