blob: c1d8a13d81a00e0c678300ed3dc419c9d5da9b56 [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;
Jeff Sharkey350083e2011-06-29 10:45:16 -070026import static android.provider.Settings.Secure.NETSTATS_ENABLED;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080027import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
28import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
29import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceRxThrottleResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceTxThrottleResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult;
36import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070037import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
San Mehat873f2142010-01-14 10:25:07 -080039import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080040import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070041import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080042import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070045import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080046import android.net.wifi.WifiConfiguration;
47import android.net.wifi.WifiConfiguration.KeyMgmt;
San Mehat873f2142010-01-14 10:25:07 -080048import android.os.INetworkManagementService;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080049import android.os.RemoteCallbackList;
50import android.os.RemoteException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070051import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080052import android.os.SystemProperties;
Jeff Sharkey350083e2011-06-29 10:45:16 -070053import android.provider.Settings;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080054import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080055import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070056import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080057
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070058import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080059import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070060import com.google.android.collect.Sets;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070061
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070062import java.io.BufferedReader;
63import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080064import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070065import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070066import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070067import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070068import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070069import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070070import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070071import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070072import java.net.InterfaceAddress;
73import java.net.NetworkInterface;
74import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070075import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070076import java.util.Collection;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070077import java.util.HashSet;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070078import java.util.NoSuchElementException;
79import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070080import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080081
82/**
83 * @hide
84 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070085public class NetworkManagementService extends INetworkManagementService.Stub
86 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070087 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070088 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070089 private static final String NETD_TAG = "NetdConnector";
90
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080091 private static final String ADD = "add";
92 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070093
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070094 private static final String DEFAULT = "default";
95 private static final String SECONDARY = "secondary";
96
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070097 /**
98 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
99 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
100 */
101 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
102
San Mehat873f2142010-01-14 10:25:07 -0800103 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700104 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800105 public static final int InterfaceListResult = 110;
106 public static final int TetherInterfaceListResult = 111;
107 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800108 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800109
110 public static final int TetherStatusResult = 210;
111 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800112 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800113 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700114 public static final int InterfaceRxCounterResult = 216;
115 public static final int InterfaceTxCounterResult = 217;
116 public static final int InterfaceRxThrottleResult = 218;
117 public static final int InterfaceTxThrottleResult = 219;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700118 public static final int QuotaCounterResult = 220;
119 public static final int TetheringStatsResult = 221;
Robert Greenwalte3253922010-02-18 09:23:25 -0800120
121 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700122 public static final int BandwidthControl = 601;
San Mehat873f2142010-01-14 10:25:07 -0800123 }
124
125 /**
126 * Binder context for this service
127 */
128 private Context mContext;
129
130 /**
131 * connector object for communicating with netd
132 */
133 private NativeDaemonConnector mConnector;
134
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700135 private Thread mThread;
136 private final CountDownLatch mConnectedSignal = new CountDownLatch(1);
137
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800138 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
139 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800140
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700141 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
142
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700143 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700144 /** Set of interfaces with active quotas. */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700145 private HashSet<String> mActiveQuotaIfaces = Sets.newHashSet();
146 /** Set of interfaces with active alerts. */
147 private HashSet<String> mActiveAlertIfaces = Sets.newHashSet();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700148 /** Set of UIDs with active reject rules. */
149 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
150
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700151 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700152
San Mehat873f2142010-01-14 10:25:07 -0800153 /**
154 * Constructs a new NetworkManagementService instance
155 *
156 * @param context Binder context for this service
157 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700158 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800159 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800160
Marco Nelissen62dbb222010-02-18 10:56:30 -0800161 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
162 return;
163 }
164
San Mehat873f2142010-01-14 10:25:07 -0800165 mConnector = new NativeDaemonConnector(
Kenny Root305bcbf2010-09-03 07:56:38 -0700166 new NetdCallbackReceiver(), "netd", 10, NETD_TAG);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700167 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700168
169 // Add ourself to the Watchdog monitors.
170 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700171 }
172
173 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700174 NetworkManagementService service = new NetworkManagementService(context);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700175 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
176 service.mThread.start();
177 if (DBG) Slog.d(TAG, "Awaiting socket connection");
178 service.mConnectedSignal.await();
179 if (DBG) Slog.d(TAG, "Connected");
180 return service;
San Mehat873f2142010-01-14 10:25:07 -0800181 }
182
Jeff Sharkey350083e2011-06-29 10:45:16 -0700183 public void systemReady() {
Jeff Sharkey350083e2011-06-29 10:45:16 -0700184 // only enable bandwidth control when support exists, and requested by
185 // system setting.
Jeff Sharkey350083e2011-06-29 10:45:16 -0700186 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
187 final boolean shouldEnable =
Jeff Sharkey05355c32011-08-09 14:44:19 -0700188 Settings.Secure.getInt(mContext.getContentResolver(), NETSTATS_ENABLED, 1) != 0;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700189
Jeff Sharkey350083e2011-06-29 10:45:16 -0700190 if (hasKernelSupport && shouldEnable) {
191 Slog.d(TAG, "enabling bandwidth control");
192 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800193 mConnector.execute("bandwidth", "enable");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700194 mBandwidthControlEnabled = true;
195 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700196 Log.wtf(TAG, "problem enabling bandwidth controls", e);
Jeff Sharkey350083e2011-06-29 10:45:16 -0700197 }
198 } else {
199 Slog.d(TAG, "not enabling bandwidth control");
200 }
Jeff Sharkey62a2c8f2011-07-13 15:24:02 -0700201
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700202 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700203 }
204
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800205 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800206 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800207 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800208 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800209 }
210
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800211 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800212 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800213 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800214 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800215 }
216
217 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700218 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800219 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700220 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800221 final int length = mObservers.beginBroadcast();
222 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800223 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800224 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
225 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700226 }
227 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800228 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700229 }
230
231 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700232 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700233 * (typically, an Ethernet cable has been plugged-in or unplugged).
234 */
235 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 final int length = mObservers.beginBroadcast();
237 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700238 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800239 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
240 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800241 }
242 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800243 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800244 }
245
246 /**
247 * Notify our observers of an interface addition.
248 */
249 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800250 final int length = mObservers.beginBroadcast();
251 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800252 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800253 mObservers.getBroadcastItem(i).interfaceAdded(iface);
254 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800255 }
256 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800257 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800258 }
259
260 /**
261 * Notify our observers of an interface removal.
262 */
263 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700264 // netd already clears out quota and alerts for removed ifaces; update
265 // our sanity-checking state.
266 mActiveAlertIfaces.remove(iface);
267 mActiveQuotaIfaces.remove(iface);
268
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 final int length = mObservers.beginBroadcast();
270 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800271 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800272 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
273 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800274 }
275 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800276 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800277 }
278
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700279 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700280 * Notify our observers of a limit reached.
281 */
282 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800283 final int length = mObservers.beginBroadcast();
284 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700285 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800286 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
287 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700288 }
289 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700291 }
292
293 /**
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700294 * Let us know the daemon is connected
295 */
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700296 protected void onDaemonConnected() {
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700297 mConnectedSignal.countDown();
298 }
299
San Mehat4d02d002010-01-22 16:07:46 -0800300
San Mehat873f2142010-01-14 10:25:07 -0800301 //
302 // Netd Callback handling
303 //
304
305 class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700306 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800307 public void onDaemonConnected() {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700308 NetworkManagementService.this.onDaemonConnected();
San Mehat873f2142010-01-14 10:25:07 -0800309 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700310
311 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800312 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700313 switch (code) {
314 case NetdResponseCode.InterfaceChange:
315 /*
316 * a network interface change occured
317 * Format: "NNN Iface added <name>"
318 * "NNN Iface removed <name>"
319 * "NNN Iface changed <name> <up/down>"
320 * "NNN Iface linkstatus <name> <up/down>"
321 */
322 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
323 throw new IllegalStateException(
324 String.format("Invalid event from daemon (%s)", raw));
325 }
326 if (cooked[2].equals("added")) {
327 notifyInterfaceAdded(cooked[3]);
328 return true;
329 } else if (cooked[2].equals("removed")) {
330 notifyInterfaceRemoved(cooked[3]);
331 return true;
332 } else if (cooked[2].equals("changed") && cooked.length == 5) {
333 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
334 return true;
335 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
336 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
337 return true;
338 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800339 throw new IllegalStateException(
340 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700341 // break;
342 case NetdResponseCode.BandwidthControl:
343 /*
344 * Bandwidth control needs some attention
345 * Format: "NNN limit alert <alertName> <ifaceName>"
346 */
347 if (cooked.length < 5 || !cooked[1].equals("limit")) {
348 throw new IllegalStateException(
349 String.format("Invalid event from daemon (%s)", raw));
350 }
351 if (cooked[2].equals("alert")) {
352 notifyLimitReached(cooked[3], cooked[4]);
353 return true;
354 }
355 throw new IllegalStateException(
356 String.format("Invalid event from daemon (%s)", raw));
357 // break;
358 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800359 }
360 return false;
San Mehat873f2142010-01-14 10:25:07 -0800361 }
362 }
363
San Mehated4fc8a2010-01-22 12:28:36 -0800364
San Mehat873f2142010-01-14 10:25:07 -0800365 //
366 // INetworkManagementService members
367 //
368
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800369 @Override
370 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800371 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700372 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800373 return NativeDaemonEvent.filterMessageList(
374 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700375 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800376 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700377 }
San Mehated4fc8a2010-01-22 12:28:36 -0800378 }
379
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800380 @Override
381 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800382 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800383
384 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700385 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800386 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700387 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800388 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700389 }
San Mehated4fc8a2010-01-22 12:28:36 -0800390
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800391 event.checkCode(InterfaceGetCfgResult);
392
393 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
394 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800395
Kenny Roota80ce062010-06-01 13:23:53 -0700396 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800397 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700398 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800399 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800400 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800401 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700402 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800403 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800404 } catch (IllegalArgumentException iae) {
405 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700406 }
407
408 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800409 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800410 } catch (NumberFormatException nfe) {
411 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700412 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800413
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800414 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
415 while (st.hasMoreTokens()) {
416 cfg.setFlag(st.nextToken());
417 }
Kenny Roota80ce062010-06-01 13:23:53 -0700418 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800419 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800420 }
San Mehated4fc8a2010-01-22 12:28:36 -0800421 return cfg;
422 }
423
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800424 @Override
425 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800426 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800427 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800428 if (linkAddr == null || linkAddr.getAddress() == null) {
429 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800430 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800431
432 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800433 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800434 linkAddr.getNetworkPrefixLength());
435 for (String flag : cfg.getFlags()) {
436 cmd.appendArg(flag);
437 }
438
Kenny Roota80ce062010-06-01 13:23:53 -0700439 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800440 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700441 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800442 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700443 }
San Mehat873f2142010-01-14 10:25:07 -0800444 }
445
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800446 @Override
447 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800448 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800449 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800450 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800451 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700452 }
453
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800454 @Override
455 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800456 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800457 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800458 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800459 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700460 }
461
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800462 @Override
463 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800464 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700465 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800466 mConnector.execute(
467 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700468 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800469 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700470 }
471 }
472
Irfan Sherifff5600612011-06-16 10:26:28 -0700473 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
474 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800475 @Override
476 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800477 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700478 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800479 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700480 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800481 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700482 }
483 }
484
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800485 @Override
486 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800487 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700488 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800489 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700490 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800491 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700492 }
493 }
494
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800495 @Override
496 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800497 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700498 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800499 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700500 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800501 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700502 }
503 }
504
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800505 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700506 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800507 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700508 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700509 }
510
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800511 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700512 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800513 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700514 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700515 }
516
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800517 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700518 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800519 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700520 modifyRoute(interfaceName, ADD, route, SECONDARY);
521 }
522
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800523 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700524 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800525 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700526 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
527 }
528
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800529 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
530 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700531
532 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800533 final LinkAddress la = route.getDestination();
534 cmd.appendArg(la.getAddress().getHostAddress());
535 cmd.appendArg(la.getNetworkPrefixLength());
536
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700537 if (route.getGateway() == null) {
538 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800539 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700540 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800541 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700542 }
543 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800544 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700545 }
546
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800547 try {
548 mConnector.execute(cmd);
549 } catch (NativeDaemonConnectorException e) {
550 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700551 }
552 }
553
554 private ArrayList<String> readRouteList(String filename) {
555 FileInputStream fstream = null;
556 ArrayList<String> list = new ArrayList<String>();
557
558 try {
559 fstream = new FileInputStream(filename);
560 DataInputStream in = new DataInputStream(fstream);
561 BufferedReader br = new BufferedReader(new InputStreamReader(in));
562 String s;
563
564 // throw away the title line
565
566 while (((s = br.readLine()) != null) && (s.length() != 0)) {
567 list.add(s);
568 }
569 } catch (IOException ex) {
570 // return current list, possibly empty
571 } finally {
572 if (fstream != null) {
573 try {
574 fstream.close();
575 } catch (IOException ex) {}
576 }
577 }
578
579 return list;
580 }
581
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800582 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700583 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800584 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700585 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
586
587 // v4 routes listed as:
588 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
589 for (String s : readRouteList("/proc/net/route")) {
590 String[] fields = s.split("\t");
591
592 if (fields.length > 7) {
593 String iface = fields[0];
594
595 if (interfaceName.equals(iface)) {
596 String dest = fields[1];
597 String gate = fields[2];
598 String flags = fields[3]; // future use?
599 String mask = fields[7];
600 try {
601 // address stored as a hex string, ex: 0014A8C0
602 InetAddress destAddr =
603 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
604 int prefixLength =
605 NetworkUtils.netmaskIntToPrefixLength(
606 (int)Long.parseLong(mask, 16));
607 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
608
609 // address stored as a hex string, ex 0014A8C0
610 InetAddress gatewayAddr =
611 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
612
613 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
614 routes.add(route);
615 } catch (Exception e) {
616 Log.e(TAG, "Error parsing route " + s + " : " + e);
617 continue;
618 }
619 }
620 }
621 }
622
623 // v6 routes listed as:
624 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
625 for (String s : readRouteList("/proc/net/ipv6_route")) {
626 String[]fields = s.split("\\s+");
627 if (fields.length > 9) {
628 String iface = fields[9].trim();
629 if (interfaceName.equals(iface)) {
630 String dest = fields[0];
631 String prefix = fields[1];
632 String gate = fields[4];
633
634 try {
635 // prefix length stored as a hex string, ex 40
636 int prefixLength = Integer.parseInt(prefix, 16);
637
638 // address stored as a 32 char hex string
639 // ex fe800000000000000000000000000000
640 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
641 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
642
643 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
644
645 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
646 routes.add(route);
647 } catch (Exception e) {
648 Log.e(TAG, "Error parsing route " + s + " : " + e);
649 continue;
650 }
651 }
652 }
653 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800654 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700655 }
656
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800657 @Override
San Mehat873f2142010-01-14 10:25:07 -0800658 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800659 // TODO: remove from aidl if nobody calls externally
660 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800661
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800663 }
664
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800665 @Override
San Mehat873f2142010-01-14 10:25:07 -0800666 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800667 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800668
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800669 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700670 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800671 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700672 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800673 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700674 }
San Mehat873f2142010-01-14 10:25:07 -0800675
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800676 // 211 Forwarding enabled
677 event.checkCode(IpFwdStatusResult);
678 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800679 }
680
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800681 @Override
682 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800684 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800685 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800686 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800687 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800688 }
San Mehat873f2142010-01-14 10:25:07 -0800689 }
690
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800691 @Override
692 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800693 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700694 // cmd is "tether start first_start first_stop second_start second_stop ..."
695 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800696
697 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700698 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800699 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700700 }
Kenny Roota80ce062010-06-01 13:23:53 -0700701
702 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800703 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700704 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800705 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700706 }
San Mehat873f2142010-01-14 10:25:07 -0800707 }
708
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800709 @Override
710 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800711 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700712 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800713 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700714 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800715 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700716 }
San Mehat873f2142010-01-14 10:25:07 -0800717 }
718
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800719 @Override
720 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800721 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800722
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800723 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700724 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800725 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700726 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800727 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700728 }
San Mehat873f2142010-01-14 10:25:07 -0800729
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800730 // 210 Tethering services started
731 event.checkCode(TetherStatusResult);
732 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800733 }
734
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800735 @Override
736 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800737 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700738 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800739 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700740 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800741 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700742 }
San Mehat873f2142010-01-14 10:25:07 -0800743 }
744
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800745 @Override
San Mehat873f2142010-01-14 10:25:07 -0800746 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800747 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700748 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800749 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700750 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800751 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700752 }
San Mehat873f2142010-01-14 10:25:07 -0800753 }
754
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800755 @Override
756 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800757 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700758 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800759 return NativeDaemonEvent.filterMessageList(
760 mConnector.executeForList("tether", "interface", "list"),
761 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700762 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800763 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700764 }
San Mehat873f2142010-01-14 10:25:07 -0800765 }
766
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800767 @Override
768 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800769 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800770
771 final Command cmd = new Command("tether", "dns", "set");
772 for (String s : dns) {
773 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
774 }
775
San Mehat873f2142010-01-14 10:25:07 -0800776 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800777 mConnector.execute(cmd);
778 } catch (NativeDaemonConnectorException e) {
779 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800780 }
781 }
782
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800783 @Override
784 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800785 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700786 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800787 return NativeDaemonEvent.filterMessageList(
788 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700789 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800790 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700791 }
San Mehat873f2142010-01-14 10:25:07 -0800792 }
793
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800794 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700795 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800796 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700797
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800798 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
799 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800800 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800801 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800802 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800803 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
804 .getInterfaceAddresses();
805 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800806 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800807 InetAddress addr = NetworkUtils.getNetworkPart(
808 ia.getAddress(), ia.getNetworkPrefixLength());
809 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800810 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700811 }
812
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800813 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800814 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800815 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800816 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800817 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700818 }
819
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800820 @Override
821 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800822 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700823 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700824 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800825 } catch (SocketException e) {
826 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700827 }
San Mehat873f2142010-01-14 10:25:07 -0800828 }
829
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800830 @Override
831 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800832 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700833 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700834 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800835 } catch (SocketException e) {
836 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700837 }
San Mehat873f2142010-01-14 10:25:07 -0800838 }
San Mehat72759df2010-01-19 13:50:37 -0800839
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800840 @Override
841 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800842 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700843 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800844 return NativeDaemonEvent.filterMessageList(
845 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700846 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800847 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700848 }
San Mehat72759df2010-01-19 13:50:37 -0800849 }
850
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800851 @Override
852 public void attachPppd(
853 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800854 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800855 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800856 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800857 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
858 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
859 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800860 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700861 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800862 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800863 }
864 }
865
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800866 @Override
867 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800868 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700869 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800870 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700871 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800872 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700873 }
San Mehat72759df2010-01-19 13:50:37 -0800874 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800875
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800876 @Override
877 public void startAccessPoint(
878 WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800879 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700880 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700881 wifiFirmwareReload(wlanIface, "AP");
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800882 mConnector.execute("softap", "start", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700883 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800884 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700885 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800886 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
887 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700888 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800889 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -0700890 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800891 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800892 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800893 }
894
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800895 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800896 switch (wifiConfig.getAuthType()) {
897 case KeyMgmt.WPA_PSK:
898 return "wpa-psk";
899 case KeyMgmt.WPA2_PSK:
900 return "wpa2-psk";
901 default:
902 return "open";
903 }
904 }
905
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700906 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800907 @Override
908 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800909 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700910 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800911 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700912 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800913 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700914 }
915 }
916
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800917 @Override
918 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800919 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700920 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 mConnector.execute("softap", "stopap");
922 mConnector.execute("softap", "stop", wlanIface);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700923 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -0700924 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800925 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700926 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800927 }
928
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800929 @Override
930 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800931 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700932 try {
933 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800934 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700935 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
937 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700938 }
939 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800940 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700941 }
942 }
San Mehat91cac642010-03-31 14:31:36 -0700943
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700944 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700945 public NetworkStats getNetworkStatsSummary() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800946 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700947 return mStatsFactory.readNetworkStatsSummary();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700948 }
949
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700950 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700951 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700953 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -0700954 }
955
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700956 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700957 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800958 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700959
Jeff Sharkey350083e2011-06-29 10:45:16 -0700960 // silently discard when control disabled
961 // TODO: eventually migrate to be always enabled
962 if (!mBandwidthControlEnabled) return;
963
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700964 synchronized (mQuotaLock) {
965 if (mActiveQuotaIfaces.contains(iface)) {
966 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700967 }
968
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700969 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700970 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800971 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700972 mActiveQuotaIfaces.add(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700973 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800974 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700975 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -0700976 }
977 }
978
979 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700980 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800981 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700982
Jeff Sharkey350083e2011-06-29 10:45:16 -0700983 // silently discard when control disabled
984 // TODO: eventually migrate to be always enabled
985 if (!mBandwidthControlEnabled) return;
986
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700987 synchronized (mQuotaLock) {
988 if (!mActiveQuotaIfaces.contains(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700989 // TODO: eventually consider throwing
990 return;
991 }
992
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -0800993 mActiveQuotaIfaces.remove(iface);
994 mActiveAlertIfaces.remove(iface);
995
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700996 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700997 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800998 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700999 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001000 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001001 }
1002 }
1003 }
1004
1005 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001006 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001007 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001008
1009 // silently discard when control disabled
1010 // TODO: eventually migrate to be always enabled
1011 if (!mBandwidthControlEnabled) return;
1012
1013 // quick sanity check
1014 if (!mActiveQuotaIfaces.contains(iface)) {
1015 throw new IllegalStateException("setting alert requires existing quota on iface");
1016 }
1017
1018 synchronized (mQuotaLock) {
1019 if (mActiveAlertIfaces.contains(iface)) {
1020 throw new IllegalStateException("iface " + iface + " already has alert");
1021 }
1022
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001023 try {
1024 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001025 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001026 mActiveAlertIfaces.add(iface);
1027 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001028 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001029 }
1030 }
1031 }
1032
1033 @Override
1034 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001035 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001036
1037 // silently discard when control disabled
1038 // TODO: eventually migrate to be always enabled
1039 if (!mBandwidthControlEnabled) return;
1040
1041 synchronized (mQuotaLock) {
1042 if (!mActiveAlertIfaces.contains(iface)) {
1043 // TODO: eventually consider throwing
1044 return;
1045 }
1046
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001047 try {
1048 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001049 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001050 mActiveAlertIfaces.remove(iface);
1051 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001052 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001053 }
1054 }
1055 }
1056
1057 @Override
1058 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001059 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001060
1061 // silently discard when control disabled
1062 // TODO: eventually migrate to be always enabled
1063 if (!mBandwidthControlEnabled) return;
1064
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001065 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001066 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001067 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001068 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001069 }
1070 }
1071
1072 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001073 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001075
Jeff Sharkey350083e2011-06-29 10:45:16 -07001076 // silently discard when control disabled
1077 // TODO: eventually migrate to be always enabled
1078 if (!mBandwidthControlEnabled) return;
1079
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001080 synchronized (mUidRejectOnQuota) {
1081 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1082 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1083 // TODO: eventually consider throwing
1084 return;
1085 }
1086
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001087 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001088 mConnector.execute("bandwidth",
1089 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001090 if (rejectOnQuotaInterfaces) {
1091 mUidRejectOnQuota.put(uid, true);
1092 } else {
1093 mUidRejectOnQuota.delete(uid);
1094 }
1095 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001096 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001097 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001098 }
1099 }
1100
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001101 @Override
1102 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001103 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001104 return mBandwidthControlEnabled;
1105 }
1106
1107 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001108 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001109 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001110 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001111 }
1112
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001113 @Override
1114 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001116
1117 if (ifacePairs.length % 2 != 0) {
1118 throw new IllegalArgumentException(
1119 "unexpected ifacePairs; length=" + ifacePairs.length);
1120 }
1121
1122 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1123 for (int i = 0; i < ifacePairs.length; i += 2) {
1124 final String ifaceIn = ifacePairs[i];
1125 final String ifaceOut = ifacePairs[i + 1];
1126 if (ifaceIn != null && ifaceOut != null) {
1127 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1128 }
1129 }
1130 return stats;
1131 }
1132
1133 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001134 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001135 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001136 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001137 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001138 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001139 }
1140
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001141 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001142
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001143 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1144 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1145 tok.nextToken();
1146 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001147
1148 try {
1149 final NetworkStats.Entry entry = new NetworkStats.Entry();
1150 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001151 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001152 entry.set = SET_DEFAULT;
1153 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001154 entry.rxBytes = Long.parseLong(tok.nextToken());
1155 entry.rxPackets = Long.parseLong(tok.nextToken());
1156 entry.txBytes = Long.parseLong(tok.nextToken());
1157 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001158 return entry;
1159 } catch (NumberFormatException e) {
1160 throw new IllegalStateException(
1161 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1162 }
1163 }
1164
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001165 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001166 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001167 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001168 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001169 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001170 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001171 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001172 }
San Mehat91cac642010-03-31 14:31:36 -07001173 }
1174
1175 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001176 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001177 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001178 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1179 } catch (NativeDaemonConnectorException e) {
1180 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001181 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001182
1183 if (rx) {
1184 event.checkCode(InterfaceRxThrottleResult);
1185 } else {
1186 event.checkCode(InterfaceTxThrottleResult);
1187 }
1188
1189 try {
1190 return Integer.parseInt(event.getMessage());
1191 } catch (NumberFormatException e) {
1192 throw new IllegalStateException("unexpected response:" + event);
1193 }
San Mehat91cac642010-03-31 14:31:36 -07001194 }
1195
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001196 @Override
San Mehat91cac642010-03-31 14:31:36 -07001197 public int getInterfaceRxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001198 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001199 return getInterfaceThrottle(iface, true);
1200 }
1201
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001202 @Override
San Mehat91cac642010-03-31 14:31:36 -07001203 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001204 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001205 return getInterfaceThrottle(iface, false);
1206 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001208 @Override
1209 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001210 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001211 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001213 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001214 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001215 }
1216 }
1217
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001218 @Override
1219 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001220 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001221
1222 final Command cmd = new Command("resolver", "setifdns", iface);
1223 for (String s : servers) {
1224 InetAddress a = NetworkUtils.numericToInetAddress(s);
1225 if (a.isAnyLocalAddress() == false) {
1226 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001227 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001228 }
1229
1230 try {
1231 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001232 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001233 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001234 }
1235 }
1236
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001237 @Override
1238 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001239 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001240 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001241 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001242 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001243 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001244 }
1245 }
1246
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001247 @Override
1248 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001249 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001250 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001251 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001252 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001253 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001254 }
1255 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001256
1257 /** {@inheritDoc} */
1258 public void monitor() {
1259 if (mConnector != null) {
1260 mConnector.monitor();
1261 }
1262 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001263
1264 @Override
1265 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1266 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1267
1268 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1269
1270 synchronized (mQuotaLock) {
1271 pw.print("Active quota ifaces: "); pw.println(mActiveQuotaIfaces.toString());
1272 pw.print("Active alert ifaces: "); pw.println(mActiveAlertIfaces.toString());
1273 }
1274
1275 synchronized (mUidRejectOnQuota) {
1276 pw.print("UID reject on quota ifaces: [");
1277 final int size = mUidRejectOnQuota.size();
1278 for (int i = 0; i < size; i++) {
1279 pw.print(mUidRejectOnQuota.keyAt(i));
1280 if (i < size - 1) pw.print(",");
1281 }
1282 pw.println("]");
1283 }
1284 }
San Mehat873f2142010-01-14 10:25:07 -08001285}