blob: 40ea49e45adcccc3cdd9cdbd2f42098b04b16a00 [file] [log] [blame]
San Mehat873f2142010-01-14 10:25:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey4529bb62011-12-14 10:31:54 -080019import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070020import static android.Manifest.permission.DUMP;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070022import static android.net.NetworkStats.SET_DEFAULT;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import static android.net.NetworkStats.TAG_NONE;
24import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070025import static android.net.TrafficStats.UID_TETHERING;
Lorenzo Colitti79751842013-02-28 16:16:03 +090026import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070027import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080028import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
29import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
Jeff Sharkeye4984be2013-09-10 21:03:27 -070034import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080035import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070036import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
San Mehat873f2142010-01-14 10:25:07 -080038import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080039import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070040import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080041import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070044import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080045import android.net.wifi.WifiConfiguration;
46import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070047import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070048import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070049import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080050import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080052import android.os.RemoteCallbackList;
53import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070054import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070055import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080056import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080057import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070059import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080060
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070061import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070063import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080064import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070065import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070066import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070067import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070068
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070069import java.io.BufferedReader;
70import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080071import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070072import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070073import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070074import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070075import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070076import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070077import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070078import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070079import java.net.InterfaceAddress;
80import java.net.NetworkInterface;
81import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070082import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070083import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070084import java.util.HashMap;
85import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070086import java.util.NoSuchElementException;
87import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070088import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080089
90/**
91 * @hide
92 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070093public class NetworkManagementService extends INetworkManagementService.Stub
94 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070095 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070096 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070097 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +090098 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -070099
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800100 private static final String ADD = "add";
101 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700102
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700103 private static final String ALLOW = "allow";
104 private static final String DENY = "deny";
105
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700106 private static final String DEFAULT = "default";
107 private static final String SECONDARY = "secondary";
108
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700109 /**
110 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
111 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
112 */
113 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
114
San Mehat873f2142010-01-14 10:25:07 -0800115 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700116 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800117 public static final int InterfaceListResult = 110;
118 public static final int TetherInterfaceListResult = 111;
119 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800120 public static final int TtyListResult = 113;
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700121 public static final int TetheringStatsListResult = 114;
San Mehat873f2142010-01-14 10:25:07 -0800122
123 public static final int TetherStatusResult = 210;
124 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800125 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800126 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700127 public static final int InterfaceRxCounterResult = 216;
128 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700129 public static final int QuotaCounterResult = 220;
130 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800131 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900132 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700133 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800134
135 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700136 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700137 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900138 public static final int InterfaceAddressChange = 614;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900139 public static final int InterfaceDnsServerInfo = 615;
San Mehat873f2142010-01-14 10:25:07 -0800140 }
141
142 /**
143 * Binder context for this service
144 */
145 private Context mContext;
146
147 /**
148 * connector object for communicating with netd
149 */
150 private NativeDaemonConnector mConnector;
151
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700152 private final Handler mMainHandler = new Handler();
153
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700154 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700155 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700156
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800157 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
158 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800159
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700160 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
161
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700162 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700163 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700164 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700165 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700166 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700167 /** Set of UIDs with active reject rules. */
168 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
169
Haoyu Bai04124232012-06-28 15:26:19 -0700170 private Object mIdleTimerLock = new Object();
171 /** Set of interfaces with active idle timers. */
172 private static class IdleTimerParams {
173 public final int timeout;
174 public final String label;
175 public int networkCount;
176
177 IdleTimerParams(int timeout, String label) {
178 this.timeout = timeout;
179 this.label = label;
180 this.networkCount = 1;
181 }
182 }
183 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
184
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700185 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700186 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700187
San Mehat873f2142010-01-14 10:25:07 -0800188 /**
189 * Constructs a new NetworkManagementService instance
190 *
191 * @param context Binder context for this service
192 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900193 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800194 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800195
Marco Nelissen62dbb222010-02-18 10:56:30 -0800196 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
197 return;
198 }
199
San Mehat873f2142010-01-14 10:25:07 -0800200 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900201 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700202 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700203
204 // Add ourself to the Watchdog monitors.
205 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700206 }
207
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900208 static NetworkManagementService create(Context context,
209 String socket) throws InterruptedException {
210 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700211 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700212 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
213 service.mThread.start();
214 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700215 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700216 if (DBG) Slog.d(TAG, "Connected");
217 return service;
San Mehat873f2142010-01-14 10:25:07 -0800218 }
219
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900220 public static NetworkManagementService create(Context context) throws InterruptedException {
221 return create(context, NETD_SOCKET_NAME);
222 }
223
Jeff Sharkey350083e2011-06-29 10:45:16 -0700224 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700225 prepareNativeDaemon();
226 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700227 }
228
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800229 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800230 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800231 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800232 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800233 }
234
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800235 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800237 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800238 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800239 }
240
241 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800243 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700244 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800245 final int length = mObservers.beginBroadcast();
246 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800247 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800248 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
249 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900250 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700251 }
252 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800253 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700254 }
255
256 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700257 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700258 * (typically, an Ethernet cable has been plugged-in or unplugged).
259 */
260 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 final int length = mObservers.beginBroadcast();
262 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700263 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800264 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
265 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900266 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800267 }
268 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800270 }
271
272 /**
273 * Notify our observers of an interface addition.
274 */
275 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800276 final int length = mObservers.beginBroadcast();
277 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800278 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800279 mObservers.getBroadcastItem(i).interfaceAdded(iface);
280 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900281 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800282 }
283 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800284 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800285 }
286
287 /**
288 * Notify our observers of an interface removal.
289 */
290 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700291 // netd already clears out quota and alerts for removed ifaces; update
292 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700293 mActiveAlerts.remove(iface);
294 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700295
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800296 final int length = mObservers.beginBroadcast();
297 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800298 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800299 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
300 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900301 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800302 }
303 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800304 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800305 }
306
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700307 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700308 * Notify our observers of a limit reached.
309 */
310 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800311 final int length = mObservers.beginBroadcast();
312 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700313 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800314 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
315 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900316 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700317 }
318 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800319 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700320 }
321
322 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700323 * Notify our observers of a change in the data activity state of the interface
324 */
325 private void notifyInterfaceClassActivity(String label, boolean active) {
326 final int length = mObservers.beginBroadcast();
327 for (int i = 0; i < length; i++) {
328 try {
329 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
330 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900331 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700332 }
333 }
334 mObservers.finishBroadcast();
335 }
336
337 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700338 * Prepare native daemon once connected, enabling modules and pushing any
339 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700340 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700341 private void prepareNativeDaemon() {
342 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700343
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700344 // only enable bandwidth control when support exists
345 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
346 if (hasKernelSupport) {
347 Slog.d(TAG, "enabling bandwidth control");
348 try {
349 mConnector.execute("bandwidth", "enable");
350 mBandwidthControlEnabled = true;
351 } catch (NativeDaemonConnectorException e) {
352 Log.wtf(TAG, "problem enabling bandwidth controls", e);
353 }
354 } else {
355 Slog.d(TAG, "not enabling bandwidth control");
356 }
357
358 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
359
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700360 if (mBandwidthControlEnabled) {
361 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700362 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700363 .noteNetworkStatsEnabled();
364 } catch (RemoteException e) {
365 }
366 }
367
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700368 // push any existing quota or UID rules
369 synchronized (mQuotaLock) {
370 int size = mActiveQuotas.size();
371 if (size > 0) {
372 Slog.d(TAG, "pushing " + size + " active quota rules");
373 final HashMap<String, Long> activeQuotas = mActiveQuotas;
374 mActiveQuotas = Maps.newHashMap();
375 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
376 setInterfaceQuota(entry.getKey(), entry.getValue());
377 }
378 }
379
380 size = mActiveAlerts.size();
381 if (size > 0) {
382 Slog.d(TAG, "pushing " + size + " active alert rules");
383 final HashMap<String, Long> activeAlerts = mActiveAlerts;
384 mActiveAlerts = Maps.newHashMap();
385 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
386 setInterfaceAlert(entry.getKey(), entry.getValue());
387 }
388 }
389
390 size = mUidRejectOnQuota.size();
391 if (size > 0) {
392 Slog.d(TAG, "pushing " + size + " active uid rules");
393 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
394 mUidRejectOnQuota = new SparseBooleanArray();
395 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
396 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
397 }
398 }
399 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700400
401 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700402 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700403 }
San Mehat4d02d002010-01-22 16:07:46 -0800404
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900405 /**
406 * Notify our observers of a new or updated interface address.
407 */
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900408 private void notifyAddressUpdated(LinkAddress address, String iface, int flags, int scope) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900409 final int length = mObservers.beginBroadcast();
410 for (int i = 0; i < length; i++) {
411 try {
412 mObservers.getBroadcastItem(i).addressUpdated(address, iface, flags, scope);
413 } catch (RemoteException e) {
414 } catch (RuntimeException e) {
415 }
416 }
417 mObservers.finishBroadcast();
418 }
419
420 /**
421 * Notify our observers of a deleted interface address.
422 */
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900423 private void notifyAddressRemoved(LinkAddress address, String iface, int flags, int scope) {
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900424 final int length = mObservers.beginBroadcast();
425 for (int i = 0; i < length; i++) {
426 try {
427 mObservers.getBroadcastItem(i).addressRemoved(address, iface, flags, scope);
428 } catch (RemoteException e) {
429 } catch (RuntimeException e) {
430 }
431 }
432 mObservers.finishBroadcast();
433 }
434
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900435 /**
436 * Notify our observers of DNS server information received.
437 */
438 private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
439 final int length = mObservers.beginBroadcast();
440 for (int i = 0; i < length; i++) {
441 try {
442 mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime, addresses);
443 } catch (RemoteException e) {
444 } catch (RuntimeException e) {
445 }
446 }
447 mObservers.finishBroadcast();
448 }
449
San Mehat873f2142010-01-14 10:25:07 -0800450 //
451 // Netd Callback handling
452 //
453
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700454 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
455 @Override
San Mehat873f2142010-01-14 10:25:07 -0800456 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700457 // event is dispatched from internal NDC thread, so we prepare the
458 // daemon back on main thread.
459 if (mConnectedSignal != null) {
460 mConnectedSignal.countDown();
461 mConnectedSignal = null;
462 } else {
463 mMainHandler.post(new Runnable() {
464 @Override
465 public void run() {
466 prepareNativeDaemon();
467 }
468 });
469 }
San Mehat873f2142010-01-14 10:25:07 -0800470 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700471
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700472 @Override
San Mehat873f2142010-01-14 10:25:07 -0800473 public boolean onEvent(int code, String raw, String[] cooked) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900474 String errorMessage = String.format("Invalid event from daemon (%s)", raw);
JP Abgrall12b933d2011-07-14 18:09:22 -0700475 switch (code) {
476 case NetdResponseCode.InterfaceChange:
477 /*
478 * a network interface change occured
479 * Format: "NNN Iface added <name>"
480 * "NNN Iface removed <name>"
481 * "NNN Iface changed <name> <up/down>"
482 * "NNN Iface linkstatus <name> <up/down>"
483 */
484 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900485 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700486 }
487 if (cooked[2].equals("added")) {
488 notifyInterfaceAdded(cooked[3]);
489 return true;
490 } else if (cooked[2].equals("removed")) {
491 notifyInterfaceRemoved(cooked[3]);
492 return true;
493 } else if (cooked[2].equals("changed") && cooked.length == 5) {
494 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
495 return true;
496 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
497 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
498 return true;
499 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900500 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700501 // break;
502 case NetdResponseCode.BandwidthControl:
503 /*
504 * Bandwidth control needs some attention
505 * Format: "NNN limit alert <alertName> <ifaceName>"
506 */
507 if (cooked.length < 5 || !cooked[1].equals("limit")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900508 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700509 }
510 if (cooked[2].equals("alert")) {
511 notifyLimitReached(cooked[3], cooked[4]);
512 return true;
513 }
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900514 throw new IllegalStateException(errorMessage);
JP Abgrall12b933d2011-07-14 18:09:22 -0700515 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700516 case NetdResponseCode.InterfaceClassActivity:
517 /*
518 * An network interface class state changed (active/idle)
519 * Format: "NNN IfaceClass <active/idle> <label>"
520 */
521 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900522 throw new IllegalStateException(errorMessage);
Haoyu Baidb3c8672012-06-20 14:29:57 -0700523 }
524 boolean isActive = cooked[2].equals("active");
525 notifyInterfaceClassActivity(cooked[3], isActive);
526 return true;
527 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900528 case NetdResponseCode.InterfaceAddressChange:
529 /*
530 * A network address change occurred
531 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
532 * "NNN Address removed <addr> <iface> <flags> <scope>"
533 */
Lorenzo Colittia9626c12013-11-04 17:44:09 +0900534 if (cooked.length < 7 || !cooked[1].equals("Address")) {
535 throw new IllegalStateException(errorMessage);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900536 }
537
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700538 int flags;
539 int scope;
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900540 LinkAddress address;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900541 try {
542 flags = Integer.parseInt(cooked[5]);
543 scope = Integer.parseInt(cooked[6]);
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900544 address = new LinkAddress(cooked[3]);
545 } catch(NumberFormatException e) { // Non-numeric lifetime or scope.
546 throw new IllegalStateException(errorMessage, e);
547 } catch(IllegalArgumentException e) { // Malformed IP address.
548 throw new IllegalStateException(errorMessage, e);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900549 }
550
551 if (cooked[2].equals("updated")) {
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900552 notifyAddressUpdated(address, cooked[4], flags, scope);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900553 } else {
Lorenzo Colitti5ad421a2013-11-17 15:05:02 +0900554 notifyAddressRemoved(address, cooked[4], flags, scope);
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900555 }
556 return true;
557 // break;
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900558 case NetdResponseCode.InterfaceDnsServerInfo:
559 /*
560 * Information about available DNS servers has been received.
561 * Format: "NNN DnsInfo servers <interface> <lifetime> <servers>"
562 */
563 long lifetime; // Actually a 32-bit unsigned integer.
564
565 if (cooked.length == 6 &&
566 cooked[1].equals("DnsInfo") &&
567 cooked[2].equals("servers")) {
568 try {
569 lifetime = Long.parseLong(cooked[4]);
570 } catch (NumberFormatException e) {
571 throw new IllegalStateException(errorMessage);
572 }
573 String[] servers = cooked[5].split(",");
574 notifyInterfaceDnsServerInfo(cooked[3], lifetime, servers);
575 }
576 return true;
577 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700578 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800579 }
580 return false;
San Mehat873f2142010-01-14 10:25:07 -0800581 }
582 }
583
San Mehated4fc8a2010-01-22 12:28:36 -0800584
San Mehat873f2142010-01-14 10:25:07 -0800585 //
586 // INetworkManagementService members
587 //
588
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800589 @Override
590 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800591 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700592 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800593 return NativeDaemonEvent.filterMessageList(
594 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700595 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800596 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700597 }
San Mehated4fc8a2010-01-22 12:28:36 -0800598 }
599
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800600 @Override
601 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800602 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800603
604 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700605 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800606 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700607 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800608 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700609 }
San Mehated4fc8a2010-01-22 12:28:36 -0800610
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800611 event.checkCode(InterfaceGetCfgResult);
612
613 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
614 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800615
Kenny Roota80ce062010-06-01 13:23:53 -0700616 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800617 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700618 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800619 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800620 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800621 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700622 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800623 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800624 } catch (IllegalArgumentException iae) {
625 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700626 }
627
628 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800629 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800630 } catch (NumberFormatException nfe) {
631 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700632 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800633
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800634 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
635 while (st.hasMoreTokens()) {
636 cfg.setFlag(st.nextToken());
637 }
Kenny Roota80ce062010-06-01 13:23:53 -0700638 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800639 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800640 }
San Mehated4fc8a2010-01-22 12:28:36 -0800641 return cfg;
642 }
643
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800644 @Override
645 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800646 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800647 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800648 if (linkAddr == null || linkAddr.getAddress() == null) {
649 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800650 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800651
652 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800653 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800654 linkAddr.getNetworkPrefixLength());
655 for (String flag : cfg.getFlags()) {
656 cmd.appendArg(flag);
657 }
658
Kenny Roota80ce062010-06-01 13:23:53 -0700659 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800660 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700661 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800662 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700663 }
San Mehat873f2142010-01-14 10:25:07 -0800664 }
665
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800666 @Override
667 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800668 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800669 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800670 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800671 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700672 }
673
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800674 @Override
675 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800676 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800677 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800678 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800679 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700680 }
681
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800682 @Override
683 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700685 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800686 mConnector.execute(
687 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700688 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800689 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700690 }
691 }
692
Irfan Sherifff5600612011-06-16 10:26:28 -0700693 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
694 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800695 @Override
696 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800697 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700698 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800699 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700700 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800701 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700702 }
703 }
704
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800705 @Override
706 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800707 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700708 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800709 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700710 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800711 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700712 }
713 }
714
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800715 @Override
716 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700718 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800719 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700720 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800721 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700722 }
723 }
724
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800725 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700726 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800727 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700728 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700729 }
730
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800731 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700732 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700734 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700735 }
736
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800737 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700738 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800739 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700740 modifyRoute(interfaceName, ADD, route, SECONDARY);
741 }
742
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800743 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700744 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800745 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700746 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
747 }
748
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800749 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
750 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700751
752 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800753 final LinkAddress la = route.getDestination();
754 cmd.appendArg(la.getAddress().getHostAddress());
755 cmd.appendArg(la.getNetworkPrefixLength());
756
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700757 if (route.getGateway() == null) {
758 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800759 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700760 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800761 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700762 }
763 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800764 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700765 }
766
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800767 try {
768 mConnector.execute(cmd);
769 } catch (NativeDaemonConnectorException e) {
770 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700771 }
772 }
773
774 private ArrayList<String> readRouteList(String filename) {
775 FileInputStream fstream = null;
776 ArrayList<String> list = new ArrayList<String>();
777
778 try {
779 fstream = new FileInputStream(filename);
780 DataInputStream in = new DataInputStream(fstream);
781 BufferedReader br = new BufferedReader(new InputStreamReader(in));
782 String s;
783
784 // throw away the title line
785
786 while (((s = br.readLine()) != null) && (s.length() != 0)) {
787 list.add(s);
788 }
789 } catch (IOException ex) {
790 // return current list, possibly empty
791 } finally {
792 if (fstream != null) {
793 try {
794 fstream.close();
795 } catch (IOException ex) {}
796 }
797 }
798
799 return list;
800 }
801
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800802 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700803 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800804 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700805 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
806
807 // v4 routes listed as:
808 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
809 for (String s : readRouteList("/proc/net/route")) {
810 String[] fields = s.split("\t");
811
812 if (fields.length > 7) {
813 String iface = fields[0];
814
815 if (interfaceName.equals(iface)) {
816 String dest = fields[1];
817 String gate = fields[2];
818 String flags = fields[3]; // future use?
819 String mask = fields[7];
820 try {
821 // address stored as a hex string, ex: 0014A8C0
822 InetAddress destAddr =
823 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
824 int prefixLength =
825 NetworkUtils.netmaskIntToPrefixLength(
826 (int)Long.parseLong(mask, 16));
827 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
828
829 // address stored as a hex string, ex 0014A8C0
830 InetAddress gatewayAddr =
831 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
832
Wink Saville7b5fd052013-03-15 05:07:04 +0000833 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700834 routes.add(route);
835 } catch (Exception e) {
836 Log.e(TAG, "Error parsing route " + s + " : " + e);
837 continue;
838 }
839 }
840 }
841 }
842
843 // v6 routes listed as:
844 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
845 for (String s : readRouteList("/proc/net/ipv6_route")) {
846 String[]fields = s.split("\\s+");
847 if (fields.length > 9) {
848 String iface = fields[9].trim();
849 if (interfaceName.equals(iface)) {
850 String dest = fields[0];
851 String prefix = fields[1];
852 String gate = fields[4];
853
854 try {
855 // prefix length stored as a hex string, ex 40
856 int prefixLength = Integer.parseInt(prefix, 16);
857
858 // address stored as a 32 char hex string
859 // ex fe800000000000000000000000000000
860 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
861 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
862
863 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
864
Wink Saville7b5fd052013-03-15 05:07:04 +0000865 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700866 routes.add(route);
867 } catch (Exception e) {
868 Log.e(TAG, "Error parsing route " + s + " : " + e);
869 continue;
870 }
871 }
872 }
873 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800874 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700875 }
876
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800877 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900878 public void setMtu(String iface, int mtu) {
879 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
880
881 final NativeDaemonEvent event;
882 try {
883 event = mConnector.execute("interface", "setmtu", iface, mtu);
884 } catch (NativeDaemonConnectorException e) {
885 throw e.rethrowAsParcelableException();
886 }
887 }
888
889 @Override
San Mehat873f2142010-01-14 10:25:07 -0800890 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800891 // TODO: remove from aidl if nobody calls externally
892 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800893
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800895 }
896
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800897 @Override
San Mehat873f2142010-01-14 10:25:07 -0800898 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800899 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800900
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800901 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700902 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700904 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800905 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700906 }
San Mehat873f2142010-01-14 10:25:07 -0800907
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 // 211 Forwarding enabled
909 event.checkCode(IpFwdStatusResult);
910 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800911 }
912
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800913 @Override
914 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800915 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800916 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800917 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800918 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800919 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800920 }
San Mehat873f2142010-01-14 10:25:07 -0800921 }
922
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800923 @Override
924 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800925 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700926 // cmd is "tether start first_start first_stop second_start second_stop ..."
927 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800928
929 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700930 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700932 }
Kenny Roota80ce062010-06-01 13:23:53 -0700933
934 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800935 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700936 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800937 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700938 }
San Mehat873f2142010-01-14 10:25:07 -0800939 }
940
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800941 @Override
942 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800943 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700944 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800945 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700946 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800947 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700948 }
San Mehat873f2142010-01-14 10:25:07 -0800949 }
950
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800951 @Override
952 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800953 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800954
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800955 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700956 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800957 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700958 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800959 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700960 }
San Mehat873f2142010-01-14 10:25:07 -0800961
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800962 // 210 Tethering services started
963 event.checkCode(TetherStatusResult);
964 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800965 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700966
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800967 @Override
968 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800969 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700970 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800971 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700972 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800973 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700974 }
San Mehat873f2142010-01-14 10:25:07 -0800975 }
976
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800977 @Override
San Mehat873f2142010-01-14 10:25:07 -0800978 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800979 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700980 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800981 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700982 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800983 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700984 }
San Mehat873f2142010-01-14 10:25:07 -0800985 }
986
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800987 @Override
988 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800989 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700990 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800991 return NativeDaemonEvent.filterMessageList(
992 mConnector.executeForList("tether", "interface", "list"),
993 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700994 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800995 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700996 }
San Mehat873f2142010-01-14 10:25:07 -0800997 }
998
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800999 @Override
1000 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001001 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001002
1003 final Command cmd = new Command("tether", "dns", "set");
1004 for (String s : dns) {
1005 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
1006 }
1007
San Mehat873f2142010-01-14 10:25:07 -08001008 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001009 mConnector.execute(cmd);
1010 } catch (NativeDaemonConnectorException e) {
1011 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -08001012 }
1013 }
1014
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001015 @Override
1016 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001017 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001018 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001019 return NativeDaemonEvent.filterMessageList(
1020 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001021 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001022 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001023 }
San Mehat873f2142010-01-14 10:25:07 -08001024 }
1025
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001026 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001027 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001028 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001029
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001030 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
1031 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -08001032 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001033 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -08001034 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001035 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
1036 .getInterfaceAddresses();
1037 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001038 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001039 InetAddress addr = NetworkUtils.getNetworkPart(
1040 ia.getAddress(), ia.getNetworkPrefixLength());
1041 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001042 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001043 }
1044
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001045 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001046 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001047 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001048 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001049 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001050 }
1051
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001052 @Override
1053 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001054 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001055 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001056 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001057 } catch (SocketException e) {
1058 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001059 }
San Mehat873f2142010-01-14 10:25:07 -08001060 }
1061
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001062 @Override
1063 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001064 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001065 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001066 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001067 } catch (SocketException e) {
1068 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001069 }
San Mehat873f2142010-01-14 10:25:07 -08001070 }
San Mehat72759df2010-01-19 13:50:37 -08001071
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001072 @Override
1073 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001075 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001076 return NativeDaemonEvent.filterMessageList(
1077 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001078 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001079 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001080 }
San Mehat72759df2010-01-19 13:50:37 -08001081 }
1082
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001083 @Override
1084 public void attachPppd(
1085 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001086 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001087 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001088 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001089 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1090 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1091 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001092 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001093 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001094 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001095 }
1096 }
1097
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001098 @Override
1099 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001100 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001101 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001102 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001103 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001104 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001105 }
San Mehat72759df2010-01-19 13:50:37 -08001106 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001107
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001108 @Override
1109 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001110 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001111 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001112 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001113 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001114 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001115 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001116 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001117 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001118 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001119 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001120 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001121 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001122 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001123 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001124 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001125 }
1126
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001127 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001128 switch (wifiConfig.getAuthType()) {
1129 case KeyMgmt.WPA_PSK:
1130 return "wpa-psk";
1131 case KeyMgmt.WPA2_PSK:
1132 return "wpa2-psk";
1133 default:
1134 return "open";
1135 }
1136 }
1137
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001138 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001139 @Override
1140 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001142 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001144 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001145 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001146 }
1147 }
1148
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001149 @Override
1150 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001151 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001152 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001153 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001154 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001155 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001156 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001157 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001158 }
1159
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001160 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001161 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001163 try {
1164 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001165 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001166 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001167 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001168 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001169 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001170 }
1171 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001172 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001173 }
1174 }
San Mehat91cac642010-03-31 14:31:36 -07001175
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001176 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001177 public void addIdleTimer(String iface, int timeout, String label) {
1178 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1179
1180 if (DBG) Slog.d(TAG, "Adding idletimer");
1181
1182 synchronized (mIdleTimerLock) {
1183 IdleTimerParams params = mActiveIdleTimers.get(iface);
1184 if (params != null) {
1185 // the interface already has idletimer, update network count
1186 params.networkCount++;
1187 return;
1188 }
1189
1190 try {
1191 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1192 } catch (NativeDaemonConnectorException e) {
1193 throw e.rethrowAsParcelableException();
1194 }
1195 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1196 }
1197 }
1198
1199 @Override
1200 public void removeIdleTimer(String iface) {
1201 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1202
1203 if (DBG) Slog.d(TAG, "Removing idletimer");
1204
1205 synchronized (mIdleTimerLock) {
1206 IdleTimerParams params = mActiveIdleTimers.get(iface);
1207 if (params == null || --(params.networkCount) > 0) {
1208 return;
1209 }
1210
1211 try {
1212 mConnector.execute("idletimer", "remove", iface,
1213 Integer.toString(params.timeout), params.label);
1214 } catch (NativeDaemonConnectorException e) {
1215 throw e.rethrowAsParcelableException();
1216 }
1217 mActiveIdleTimers.remove(iface);
1218 }
1219 }
1220
1221 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001222 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001223 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001224 try {
1225 return mStatsFactory.readNetworkStatsSummaryDev();
1226 } catch (IOException e) {
1227 throw new IllegalStateException(e);
1228 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001229 }
1230
1231 @Override
1232 public NetworkStats getNetworkStatsSummaryXt() {
1233 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001234 try {
1235 return mStatsFactory.readNetworkStatsSummaryXt();
1236 } catch (IOException e) {
1237 throw new IllegalStateException(e);
1238 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001239 }
1240
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001241 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001242 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001243 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001244 try {
1245 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1246 } catch (IOException e) {
1247 throw new IllegalStateException(e);
1248 }
San Mehat91cac642010-03-31 14:31:36 -07001249 }
1250
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001251 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001252 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001253 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001254
Jeff Sharkey350083e2011-06-29 10:45:16 -07001255 // silently discard when control disabled
1256 // TODO: eventually migrate to be always enabled
1257 if (!mBandwidthControlEnabled) return;
1258
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001259 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001260 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001261 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001262 }
1263
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001264 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001265 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001266 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001267 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001268 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001269 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001270 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001271 }
1272 }
1273
1274 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001275 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001276 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001277
Jeff Sharkey350083e2011-06-29 10:45:16 -07001278 // silently discard when control disabled
1279 // TODO: eventually migrate to be always enabled
1280 if (!mBandwidthControlEnabled) return;
1281
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001282 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001283 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001284 // TODO: eventually consider throwing
1285 return;
1286 }
1287
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001288 mActiveQuotas.remove(iface);
1289 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001290
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001291 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001292 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001293 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001294 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001295 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001296 }
1297 }
1298 }
1299
1300 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001301 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001302 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001303
1304 // silently discard when control disabled
1305 // TODO: eventually migrate to be always enabled
1306 if (!mBandwidthControlEnabled) return;
1307
1308 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001309 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001310 throw new IllegalStateException("setting alert requires existing quota on iface");
1311 }
1312
1313 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001314 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001315 throw new IllegalStateException("iface " + iface + " already has alert");
1316 }
1317
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001318 try {
1319 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001320 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001321 mActiveAlerts.put(iface, 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
1328 @Override
1329 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001330 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001331
1332 // silently discard when control disabled
1333 // TODO: eventually migrate to be always enabled
1334 if (!mBandwidthControlEnabled) return;
1335
1336 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001337 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001338 // TODO: eventually consider throwing
1339 return;
1340 }
1341
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001342 try {
1343 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001344 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001345 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001346 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001347 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001348 }
1349 }
1350 }
1351
1352 @Override
1353 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001354 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001355
1356 // silently discard when control disabled
1357 // TODO: eventually migrate to be always enabled
1358 if (!mBandwidthControlEnabled) return;
1359
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001360 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001361 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001362 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001363 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001364 }
1365 }
1366
1367 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001368 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001369 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001370
Jeff Sharkey350083e2011-06-29 10:45:16 -07001371 // silently discard when control disabled
1372 // TODO: eventually migrate to be always enabled
1373 if (!mBandwidthControlEnabled) return;
1374
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001375 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001376 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1377 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1378 // TODO: eventually consider throwing
1379 return;
1380 }
1381
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001382 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001383 mConnector.execute("bandwidth",
1384 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001385 if (rejectOnQuotaInterfaces) {
1386 mUidRejectOnQuota.put(uid, true);
1387 } else {
1388 mUidRejectOnQuota.delete(uid);
1389 }
1390 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001391 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001392 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001393 }
1394 }
1395
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001396 @Override
1397 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001398 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001399 return mBandwidthControlEnabled;
1400 }
1401
1402 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001403 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001404 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001405 try {
1406 return mStatsFactory.readNetworkStatsDetail(uid);
1407 } catch (IOException e) {
1408 throw new IllegalStateException(e);
1409 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001410 }
1411
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001412 @Override
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001413 public NetworkStats getNetworkStatsTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001414 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001415
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001416 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001417 try {
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001418 final NativeDaemonEvent[] events = mConnector.executeForList(
1419 "bandwidth", "gettetherstats");
1420 for (NativeDaemonEvent event : events) {
1421 if (event.getCode() != TetheringStatsListResult) continue;
1422
1423 // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1424 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1425 try {
1426 final String ifaceIn = tok.nextToken();
1427 final String ifaceOut = tok.nextToken();
1428
1429 final NetworkStats.Entry entry = new NetworkStats.Entry();
1430 entry.iface = ifaceOut;
1431 entry.uid = UID_TETHERING;
1432 entry.set = SET_DEFAULT;
1433 entry.tag = TAG_NONE;
1434 entry.rxBytes = Long.parseLong(tok.nextToken());
1435 entry.rxPackets = Long.parseLong(tok.nextToken());
1436 entry.txBytes = Long.parseLong(tok.nextToken());
1437 entry.txPackets = Long.parseLong(tok.nextToken());
1438 stats.combineValues(entry);
1439 } catch (NoSuchElementException e) {
1440 throw new IllegalStateException("problem parsing tethering stats: " + event);
1441 } catch (NumberFormatException e) {
1442 throw new IllegalStateException("problem parsing tethering stats: " + event);
1443 }
1444 }
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001445 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001446 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001447 }
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001448 return stats;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001449 }
1450
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001451 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001452 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001453 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001454 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001455 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001456 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001457 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001458 }
1459 }
1460
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001461 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001462 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001463 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001464
Robert Greenwalt8058f622012-11-09 10:52:27 -08001465 final Command cmd = new Command("resolver", "setifdns", iface,
1466 (domains == null ? "" : domains));
1467
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001468 for (String s : servers) {
1469 InetAddress a = NetworkUtils.numericToInetAddress(s);
1470 if (a.isAnyLocalAddress() == false) {
1471 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001472 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001473 }
1474
1475 try {
1476 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001477 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001478 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001479 }
1480 }
1481
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001482 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001483 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1484 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1485 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001486 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001487 "uid", "add", iface, uid_start, uid_end);
1488 } catch (NativeDaemonConnectorException e) {
1489 throw e.rethrowAsParcelableException();
1490 }
1491 }
1492
1493 @Override
1494 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1495 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1496 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001497 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001498 "uid", "remove", iface, uid_start, uid_end);
1499 } catch (NativeDaemonConnectorException e) {
1500 throw e.rethrowAsParcelableException();
1501 }
1502 }
1503
1504 @Override
1505 public void setMarkedForwarding(String iface) {
1506 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1507 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001508 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001509 } catch (NativeDaemonConnectorException e) {
1510 throw e.rethrowAsParcelableException();
1511 }
1512 }
1513
1514 @Override
1515 public void clearMarkedForwarding(String iface) {
1516 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1517 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001518 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1519 } catch (NativeDaemonConnectorException e) {
1520 throw e.rethrowAsParcelableException();
1521 }
1522 }
1523
1524 @Override
1525 public int getMarkForUid(int uid) {
1526 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1527 final NativeDaemonEvent event;
1528 try {
1529 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1530 } catch (NativeDaemonConnectorException e) {
1531 throw e.rethrowAsParcelableException();
1532 }
1533 event.checkCode(GetMarkResult);
1534 return Integer.parseInt(event.getMessage());
1535 }
1536
1537 @Override
1538 public int getMarkForProtect() {
1539 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1540 final NativeDaemonEvent event;
1541 try {
1542 event = mConnector.execute("interface", "fwmark", "get", "protect");
1543 } catch (NativeDaemonConnectorException e) {
1544 throw e.rethrowAsParcelableException();
1545 }
1546 event.checkCode(GetMarkResult);
1547 return Integer.parseInt(event.getMessage());
1548 }
1549
1550 @Override
1551 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1552 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1553 try {
1554 LinkAddress dest = route.getDestination();
1555 mConnector.execute("interface", "fwmark", "route", "add", iface,
1556 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1557 } catch (NativeDaemonConnectorException e) {
1558 throw e.rethrowAsParcelableException();
1559 }
1560 }
1561
1562 @Override
1563 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1564 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1565 try {
1566 LinkAddress dest = route.getDestination();
1567 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1568 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001569 } catch (NativeDaemonConnectorException e) {
1570 throw e.rethrowAsParcelableException();
1571 }
1572 }
1573
1574 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001575 public void setHostExemption(LinkAddress host) {
1576 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1577 try {
1578 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1579 } catch (NativeDaemonConnectorException e) {
1580 throw e.rethrowAsParcelableException();
1581 }
1582 }
1583
1584 @Override
1585 public void clearHostExemption(LinkAddress host) {
1586 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1587 try {
1588 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1589 } catch (NativeDaemonConnectorException e) {
1590 throw e.rethrowAsParcelableException();
1591 }
1592 }
1593
1594 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001595 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1596 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1597 try {
1598 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1599 } catch (NativeDaemonConnectorException e) {
1600 throw e.rethrowAsParcelableException();
1601 }
1602 }
1603
1604 @Override
1605 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1606 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1607 try {
1608 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1609 } catch (NativeDaemonConnectorException e) {
1610 throw e.rethrowAsParcelableException();
1611 }
1612 }
1613
1614 @Override
1615 public void clearDnsInterfaceMaps() {
1616 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1617 try {
1618 mConnector.execute("resolver", "clearifacemapping");
1619 } catch (NativeDaemonConnectorException e) {
1620 throw e.rethrowAsParcelableException();
1621 }
1622 }
1623
1624
1625 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001626 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001627 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001628 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001629 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001630 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001631 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001632 }
1633 }
1634
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001635 @Override
1636 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001637 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001638 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001639 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001640 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001641 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001642 }
1643 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001644
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001645 @Override
1646 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001647 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001648 try {
1649 mConnector.execute("firewall", enabled ? "enable" : "disable");
1650 mFirewallEnabled = enabled;
1651 } catch (NativeDaemonConnectorException e) {
1652 throw e.rethrowAsParcelableException();
1653 }
1654 }
1655
1656 @Override
1657 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001658 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001659 return mFirewallEnabled;
1660 }
1661
1662 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001663 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001664 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001665 Preconditions.checkState(mFirewallEnabled);
1666 final String rule = allow ? ALLOW : DENY;
1667 try {
1668 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1669 } catch (NativeDaemonConnectorException e) {
1670 throw e.rethrowAsParcelableException();
1671 }
1672 }
1673
1674 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001675 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001676 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001677 Preconditions.checkState(mFirewallEnabled);
1678 final String rule = allow ? ALLOW : DENY;
1679 try {
1680 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1681 } catch (NativeDaemonConnectorException e) {
1682 throw e.rethrowAsParcelableException();
1683 }
1684 }
1685
1686 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001687 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001688 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001689 Preconditions.checkState(mFirewallEnabled);
1690 final String rule = allow ? ALLOW : DENY;
1691 try {
1692 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1693 } catch (NativeDaemonConnectorException e) {
1694 throw e.rethrowAsParcelableException();
1695 }
1696 }
1697
1698 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001699 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001700 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001701 Preconditions.checkState(mFirewallEnabled);
1702 final String rule = allow ? ALLOW : DENY;
1703 try {
1704 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1705 } catch (NativeDaemonConnectorException e) {
1706 throw e.rethrowAsParcelableException();
1707 }
1708 }
1709
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001710 private static void enforceSystemUid() {
1711 final int uid = Binder.getCallingUid();
1712 if (uid != Process.SYSTEM_UID) {
1713 throw new SecurityException("Only available to AID_SYSTEM");
1714 }
1715 }
1716
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001717 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001718 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1719 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1720 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001721 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001722 } catch (NativeDaemonConnectorException e) {
1723 throw new IllegalStateException(
1724 "Error communicating with native deamon to set interface for pid" + iface, e);
1725 }
1726 }
1727
1728 @Override
1729 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1730 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1731 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001732 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001733 } catch (NativeDaemonConnectorException e) {
1734 throw new IllegalStateException(
1735 "Error communicating with native deamon to clear interface for pid " + pid, e);
1736 }
1737 }
1738
Lorenzo Colitti79751842013-02-28 16:16:03 +09001739 @Override
1740 public void startClatd(String interfaceName) throws IllegalStateException {
1741 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1742
1743 try {
1744 mConnector.execute("clatd", "start", interfaceName);
1745 } catch (NativeDaemonConnectorException e) {
1746 throw e.rethrowAsParcelableException();
1747 }
1748 }
1749
1750 @Override
1751 public void stopClatd() throws IllegalStateException {
1752 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1753
1754 try {
1755 mConnector.execute("clatd", "stop");
1756 } catch (NativeDaemonConnectorException e) {
1757 throw e.rethrowAsParcelableException();
1758 }
1759 }
1760
1761 @Override
1762 public boolean isClatdStarted() {
1763 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1764
1765 final NativeDaemonEvent event;
1766 try {
1767 event = mConnector.execute("clatd", "status");
1768 } catch (NativeDaemonConnectorException e) {
1769 throw e.rethrowAsParcelableException();
1770 }
1771
1772 event.checkCode(ClatdStatusResult);
1773 return event.getMessage().endsWith("started");
1774 }
1775
Mattias Falk8b47b362011-08-23 14:15:13 +02001776 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001777 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001778 public void monitor() {
1779 if (mConnector != null) {
1780 mConnector.monitor();
1781 }
1782 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001783
1784 @Override
1785 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1786 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1787
Robert Greenwalt470fd722012-01-18 12:51:15 -08001788 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1789 mConnector.dump(fd, pw, args);
1790 pw.println();
1791
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001792 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1793
1794 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001795 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1796 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001797 }
1798
1799 synchronized (mUidRejectOnQuota) {
1800 pw.print("UID reject on quota ifaces: [");
1801 final int size = mUidRejectOnQuota.size();
1802 for (int i = 0; i < size; i++) {
1803 pw.print(mUidRejectOnQuota.keyAt(i));
1804 if (i < size - 1) pw.print(",");
1805 }
1806 pw.println("]");
1807 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001808
1809 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001810 }
San Mehat873f2142010-01-14 10:25:07 -08001811}