blob: f7bf8b530b79975c25ee25dcac68d45ecb0f96a4 [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 Sharkeyb0aec072011-10-14 18:32:24 -070019import static android.Manifest.permission.ACCESS_NETWORK_STATE;
20import static android.Manifest.permission.CHANGE_NETWORK_STATE;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080021import static android.Manifest.permission.CHANGE_WIFI_STATE;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070022import static android.Manifest.permission.DUMP;
Jeff Sharkey350083e2011-06-29 10:45:16 -070023import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkeyaf75c332011-11-18 12:41:12 -080024import static android.Manifest.permission.SHUTDOWN;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070025import static android.net.NetworkStats.SET_DEFAULT;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070026import static android.net.NetworkStats.TAG_NONE;
27import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyae2c1812011-10-04 13:11:40 -070028import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey350083e2011-06-29 10:45:16 -070029import static android.provider.Settings.Secure.NETSTATS_ENABLED;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080030import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceRxThrottleResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceTxThrottleResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
36import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
37import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
38import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult;
39import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070040import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070041
San Mehat873f2142010-01-14 10:25:07 -080042import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080043import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070044import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080045import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070046import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080047import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070048import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080049import android.net.wifi.WifiConfiguration;
50import android.net.wifi.WifiConfiguration.KeyMgmt;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070051import android.os.Binder;
San Mehat873f2142010-01-14 10:25:07 -080052import android.os.INetworkManagementService;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070053import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080054import android.os.SystemProperties;
Jeff Sharkey350083e2011-06-29 10:45:16 -070055import android.provider.Settings;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080056import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070058import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080059
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070060import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080061import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070062import com.google.android.collect.Sets;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070063
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070064import java.io.BufferedReader;
65import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080066import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070067import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070068import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070069import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070070import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070071import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070072import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070073import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070074import java.net.InterfaceAddress;
75import java.net.NetworkInterface;
76import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070077import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070078import java.util.Collection;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070079import java.util.HashSet;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070080import java.util.NoSuchElementException;
81import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070082import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080083
84/**
85 * @hide
86 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070087public class NetworkManagementService extends INetworkManagementService.Stub
88 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070089 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070090 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070091 private static final String NETD_TAG = "NetdConnector";
92
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080093 private static final String ADD = "add";
94 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070095
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070096 private static final String DEFAULT = "default";
97 private static final String SECONDARY = "secondary";
98
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070099 /**
100 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
101 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
102 */
103 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
104
San Mehat873f2142010-01-14 10:25:07 -0800105 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700106 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800107 public static final int InterfaceListResult = 110;
108 public static final int TetherInterfaceListResult = 111;
109 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800110 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800111
112 public static final int TetherStatusResult = 210;
113 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800114 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800115 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700116 public static final int InterfaceRxCounterResult = 216;
117 public static final int InterfaceTxCounterResult = 217;
118 public static final int InterfaceRxThrottleResult = 218;
119 public static final int InterfaceTxThrottleResult = 219;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700120 public static final int QuotaCounterResult = 220;
121 public static final int TetheringStatsResult = 221;
Robert Greenwalte3253922010-02-18 09:23:25 -0800122
123 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700124 public static final int BandwidthControl = 601;
San Mehat873f2142010-01-14 10:25:07 -0800125 }
126
127 /**
128 * Binder context for this service
129 */
130 private Context mContext;
131
132 /**
133 * connector object for communicating with netd
134 */
135 private NativeDaemonConnector mConnector;
136
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700137 private Thread mThread;
138 private final CountDownLatch mConnectedSignal = new CountDownLatch(1);
139
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700140 // TODO: replace with RemoteCallbackList
San Mehat4d02d002010-01-22 16:07:46 -0800141 private ArrayList<INetworkManagementEventObserver> mObservers;
142
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700143 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
144
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700145 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700146 /** Set of interfaces with active quotas. */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700147 private HashSet<String> mActiveQuotaIfaces = Sets.newHashSet();
148 /** Set of interfaces with active alerts. */
149 private HashSet<String> mActiveAlertIfaces = Sets.newHashSet();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700150 /** Set of UIDs with active reject rules. */
151 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
152
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700153 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700154
San Mehat873f2142010-01-14 10:25:07 -0800155 /**
156 * Constructs a new NetworkManagementService instance
157 *
158 * @param context Binder context for this service
159 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700160 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800161 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800162 mObservers = new ArrayList<INetworkManagementEventObserver>();
163
Marco Nelissen62dbb222010-02-18 10:56:30 -0800164 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
165 return;
166 }
167
San Mehat873f2142010-01-14 10:25:07 -0800168 mConnector = new NativeDaemonConnector(
Kenny Root305bcbf2010-09-03 07:56:38 -0700169 new NetdCallbackReceiver(), "netd", 10, NETD_TAG);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700170 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700171
172 // Add ourself to the Watchdog monitors.
173 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700174 }
175
176 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700177 NetworkManagementService service = new NetworkManagementService(context);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700178 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
179 service.mThread.start();
180 if (DBG) Slog.d(TAG, "Awaiting socket connection");
181 service.mConnectedSignal.await();
182 if (DBG) Slog.d(TAG, "Connected");
183 return service;
San Mehat873f2142010-01-14 10:25:07 -0800184 }
185
Jeff Sharkey350083e2011-06-29 10:45:16 -0700186 public void systemReady() {
Jeff Sharkey350083e2011-06-29 10:45:16 -0700187 // only enable bandwidth control when support exists, and requested by
188 // system setting.
Jeff Sharkey350083e2011-06-29 10:45:16 -0700189 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
190 final boolean shouldEnable =
Jeff Sharkey05355c32011-08-09 14:44:19 -0700191 Settings.Secure.getInt(mContext.getContentResolver(), NETSTATS_ENABLED, 1) != 0;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700192
Jeff Sharkey350083e2011-06-29 10:45:16 -0700193 if (hasKernelSupport && shouldEnable) {
194 Slog.d(TAG, "enabling bandwidth control");
195 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800196 mConnector.execute("bandwidth", "enable");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700197 mBandwidthControlEnabled = true;
198 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700199 Log.wtf(TAG, "problem enabling bandwidth controls", e);
Jeff Sharkey350083e2011-06-29 10:45:16 -0700200 }
201 } else {
202 Slog.d(TAG, "not enabling bandwidth control");
203 }
Jeff Sharkey62a2c8f2011-07-13 15:24:02 -0700204
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700205 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700206 }
207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800208 @Override
San Mehat4d02d002010-01-22 16:07:46 -0800209 public void registerObserver(INetworkManagementEventObserver obs) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800210 Slog.d(TAG, "Registering observer");
San Mehat4d02d002010-01-22 16:07:46 -0800211 mObservers.add(obs);
212 }
213
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800214 @Override
San Mehat4d02d002010-01-22 16:07:46 -0800215 public void unregisterObserver(INetworkManagementEventObserver obs) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800216 Slog.d(TAG, "Unregistering observer");
San Mehat4d02d002010-01-22 16:07:46 -0800217 mObservers.remove(mObservers.indexOf(obs));
218 }
219
220 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700221 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800222 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700223 private void notifyInterfaceStatusChanged(String iface, boolean up) {
San Mehat4d02d002010-01-22 16:07:46 -0800224 for (INetworkManagementEventObserver obs : mObservers) {
225 try {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700226 obs.interfaceStatusChanged(iface, up);
227 } catch (Exception ex) {
228 Slog.w(TAG, "Observer notifier failed", ex);
229 }
230 }
231 }
232
233 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700234 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700235 * (typically, an Ethernet cable has been plugged-in or unplugged).
236 */
237 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
238 for (INetworkManagementEventObserver obs : mObservers) {
239 try {
240 obs.interfaceLinkStateChanged(iface, up);
San Mehat4d02d002010-01-22 16:07:46 -0800241 } catch (Exception ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800242 Slog.w(TAG, "Observer notifier failed", ex);
San Mehat4d02d002010-01-22 16:07:46 -0800243 }
244 }
245 }
246
247 /**
248 * Notify our observers of an interface addition.
249 */
250 private void notifyInterfaceAdded(String iface) {
251 for (INetworkManagementEventObserver obs : mObservers) {
252 try {
253 obs.interfaceAdded(iface);
254 } catch (Exception ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800255 Slog.w(TAG, "Observer notifier failed", ex);
San Mehat4d02d002010-01-22 16:07:46 -0800256 }
257 }
258 }
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
San Mehat4d02d002010-01-22 16:07:46 -0800269 for (INetworkManagementEventObserver obs : mObservers) {
270 try {
271 obs.interfaceRemoved(iface);
272 } catch (Exception ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800273 Slog.w(TAG, "Observer notifier failed", ex);
San Mehat4d02d002010-01-22 16:07:46 -0800274 }
275 }
276 }
277
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700278 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700279 * Notify our observers of a limit reached.
280 */
281 private void notifyLimitReached(String limitName, String iface) {
282 for (INetworkManagementEventObserver obs : mObservers) {
283 try {
284 obs.limitReached(limitName, iface);
JP Abgrall12b933d2011-07-14 18:09:22 -0700285 } catch (Exception ex) {
286 Slog.w(TAG, "Observer notifier failed", ex);
287 }
288 }
289 }
290
291 /**
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700292 * Let us know the daemon is connected
293 */
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700294 protected void onDaemonConnected() {
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700295 mConnectedSignal.countDown();
296 }
297
San Mehat4d02d002010-01-22 16:07:46 -0800298
San Mehat873f2142010-01-14 10:25:07 -0800299 //
300 // Netd Callback handling
301 //
302
303 class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700304 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800305 public void onDaemonConnected() {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700306 NetworkManagementService.this.onDaemonConnected();
San Mehat873f2142010-01-14 10:25:07 -0800307 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700308
309 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800310 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700311 switch (code) {
312 case NetdResponseCode.InterfaceChange:
313 /*
314 * a network interface change occured
315 * Format: "NNN Iface added <name>"
316 * "NNN Iface removed <name>"
317 * "NNN Iface changed <name> <up/down>"
318 * "NNN Iface linkstatus <name> <up/down>"
319 */
320 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
321 throw new IllegalStateException(
322 String.format("Invalid event from daemon (%s)", raw));
323 }
324 if (cooked[2].equals("added")) {
325 notifyInterfaceAdded(cooked[3]);
326 return true;
327 } else if (cooked[2].equals("removed")) {
328 notifyInterfaceRemoved(cooked[3]);
329 return true;
330 } else if (cooked[2].equals("changed") && cooked.length == 5) {
331 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
332 return true;
333 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
334 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
335 return true;
336 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800337 throw new IllegalStateException(
338 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700339 // break;
340 case NetdResponseCode.BandwidthControl:
341 /*
342 * Bandwidth control needs some attention
343 * Format: "NNN limit alert <alertName> <ifaceName>"
344 */
345 if (cooked.length < 5 || !cooked[1].equals("limit")) {
346 throw new IllegalStateException(
347 String.format("Invalid event from daemon (%s)", raw));
348 }
349 if (cooked[2].equals("alert")) {
350 notifyLimitReached(cooked[3], cooked[4]);
351 return true;
352 }
353 throw new IllegalStateException(
354 String.format("Invalid event from daemon (%s)", raw));
355 // break;
356 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800357 }
358 return false;
San Mehat873f2142010-01-14 10:25:07 -0800359 }
360 }
361
San Mehated4fc8a2010-01-22 12:28:36 -0800362
San Mehat873f2142010-01-14 10:25:07 -0800363 //
364 // INetworkManagementService members
365 //
366
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800367 @Override
368 public String[] listInterfaces() {
369 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700370 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800371 return NativeDaemonEvent.filterMessageList(
372 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700373 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800374 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700375 }
San Mehated4fc8a2010-01-22 12:28:36 -0800376 }
377
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800378 @Override
379 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700380 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800381
382 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700383 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800384 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700385 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800386 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700387 }
San Mehated4fc8a2010-01-22 12:28:36 -0800388
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800389 event.checkCode(InterfaceGetCfgResult);
390
391 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
392 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800393
Kenny Roota80ce062010-06-01 13:23:53 -0700394 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800395 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700396 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800397 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800398 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800399 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700400 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800401 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800402 } catch (IllegalArgumentException iae) {
403 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700404 }
405
406 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800407 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800408 } catch (NumberFormatException nfe) {
409 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700410 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800411
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800412 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
413 while (st.hasMoreTokens()) {
414 cfg.setFlag(st.nextToken());
415 }
Kenny Roota80ce062010-06-01 13:23:53 -0700416 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800417 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800418 }
San Mehated4fc8a2010-01-22 12:28:36 -0800419 return cfg;
420 }
421
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800422 @Override
423 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700424 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800425 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800426 if (linkAddr == null || linkAddr.getAddress() == null) {
427 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800428 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800429
430 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800431 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800432 linkAddr.getNetworkPrefixLength());
433 for (String flag : cfg.getFlags()) {
434 cmd.appendArg(flag);
435 }
436
Kenny Roota80ce062010-06-01 13:23:53 -0700437 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800438 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700439 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800440 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700441 }
San Mehat873f2142010-01-14 10:25:07 -0800442 }
443
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800444 @Override
445 public void setInterfaceDown(String iface) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700446 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800447 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800448 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800449 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700450 }
451
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800452 @Override
453 public void setInterfaceUp(String iface) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700454 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800455 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800456 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800457 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700458 }
459
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800460 @Override
461 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700462 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700463 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800464 mConnector.execute(
465 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700466 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800467 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700468 }
469 }
470
Irfan Sherifff5600612011-06-16 10:26:28 -0700471 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
472 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800473 @Override
474 public void clearInterfaceAddresses(String iface) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700475 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700476 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800477 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700478 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800479 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700480 }
481 }
482
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800483 @Override
484 public void enableIpv6(String iface) {
485 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700486 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800487 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700488 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800489 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700490 }
491 }
492
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800493 @Override
494 public void disableIpv6(String iface) {
495 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700496 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800497 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700498 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800499 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700500 }
501 }
502
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800503 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700504 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700505 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700506 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700507 }
508
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800509 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700510 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700511 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700512 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700513 }
514
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800515 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700516 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
517 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
518 modifyRoute(interfaceName, ADD, route, SECONDARY);
519 }
520
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800521 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700522 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
523 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
524 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
525 }
526
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800527 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
528 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700529
530 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800531 final LinkAddress la = route.getDestination();
532 cmd.appendArg(la.getAddress().getHostAddress());
533 cmd.appendArg(la.getNetworkPrefixLength());
534
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700535 if (route.getGateway() == null) {
536 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800537 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700538 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800539 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700540 }
541 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800542 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700543 }
544
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800545 try {
546 mConnector.execute(cmd);
547 } catch (NativeDaemonConnectorException e) {
548 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700549 }
550 }
551
552 private ArrayList<String> readRouteList(String filename) {
553 FileInputStream fstream = null;
554 ArrayList<String> list = new ArrayList<String>();
555
556 try {
557 fstream = new FileInputStream(filename);
558 DataInputStream in = new DataInputStream(fstream);
559 BufferedReader br = new BufferedReader(new InputStreamReader(in));
560 String s;
561
562 // throw away the title line
563
564 while (((s = br.readLine()) != null) && (s.length() != 0)) {
565 list.add(s);
566 }
567 } catch (IOException ex) {
568 // return current list, possibly empty
569 } finally {
570 if (fstream != null) {
571 try {
572 fstream.close();
573 } catch (IOException ex) {}
574 }
575 }
576
577 return list;
578 }
579
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800580 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700581 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkeyb0aec072011-10-14 18:32:24 -0700582 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700583 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
584
585 // v4 routes listed as:
586 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
587 for (String s : readRouteList("/proc/net/route")) {
588 String[] fields = s.split("\t");
589
590 if (fields.length > 7) {
591 String iface = fields[0];
592
593 if (interfaceName.equals(iface)) {
594 String dest = fields[1];
595 String gate = fields[2];
596 String flags = fields[3]; // future use?
597 String mask = fields[7];
598 try {
599 // address stored as a hex string, ex: 0014A8C0
600 InetAddress destAddr =
601 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
602 int prefixLength =
603 NetworkUtils.netmaskIntToPrefixLength(
604 (int)Long.parseLong(mask, 16));
605 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
606
607 // address stored as a hex string, ex 0014A8C0
608 InetAddress gatewayAddr =
609 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
610
611 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
612 routes.add(route);
613 } catch (Exception e) {
614 Log.e(TAG, "Error parsing route " + s + " : " + e);
615 continue;
616 }
617 }
618 }
619 }
620
621 // v6 routes listed as:
622 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
623 for (String s : readRouteList("/proc/net/ipv6_route")) {
624 String[]fields = s.split("\\s+");
625 if (fields.length > 9) {
626 String iface = fields[9].trim();
627 if (interfaceName.equals(iface)) {
628 String dest = fields[0];
629 String prefix = fields[1];
630 String gate = fields[4];
631
632 try {
633 // prefix length stored as a hex string, ex 40
634 int prefixLength = Integer.parseInt(prefix, 16);
635
636 // address stored as a 32 char hex string
637 // ex fe800000000000000000000000000000
638 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
639 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
640
641 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
642
643 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
644 routes.add(route);
645 } catch (Exception e) {
646 Log.e(TAG, "Error parsing route " + s + " : " + e);
647 continue;
648 }
649 }
650 }
651 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800652 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700653 }
654
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800655 @Override
San Mehat873f2142010-01-14 10:25:07 -0800656 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800657 // TODO: remove from aidl if nobody calls externally
658 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800659
Joe Onorato8a9b2202010-02-26 18:56:32 -0800660 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800661 }
662
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800663 @Override
San Mehat873f2142010-01-14 10:25:07 -0800664 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800665 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800666
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800667 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700668 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800669 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700670 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800671 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700672 }
San Mehat873f2142010-01-14 10:25:07 -0800673
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800674 // 211 Forwarding enabled
675 event.checkCode(IpFwdStatusResult);
676 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800677 }
678
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800679 @Override
680 public void setIpForwardingEnabled(boolean enable) {
681 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800682 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800683 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800684 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800685 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800686 }
San Mehat873f2142010-01-14 10:25:07 -0800687 }
688
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800689 @Override
690 public void startTethering(String[] dhcpRange) {
691 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700692 // cmd is "tether start first_start first_stop second_start second_stop ..."
693 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800694
695 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700696 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800697 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700698 }
Kenny Roota80ce062010-06-01 13:23:53 -0700699
700 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800701 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700702 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800703 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700704 }
San Mehat873f2142010-01-14 10:25:07 -0800705 }
706
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800707 @Override
708 public void stopTethering() {
709 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700710 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800711 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700712 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800713 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700714 }
San Mehat873f2142010-01-14 10:25:07 -0800715 }
716
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800717 @Override
718 public boolean isTetheringStarted() {
719 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800720
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800721 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700722 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800723 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700724 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800725 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700726 }
San Mehat873f2142010-01-14 10:25:07 -0800727
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800728 // 210 Tethering services started
729 event.checkCode(TetherStatusResult);
730 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800731 }
732
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800733 @Override
734 public void tetherInterface(String iface) {
735 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700736 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800737 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700738 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800739 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700740 }
San Mehat873f2142010-01-14 10:25:07 -0800741 }
742
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800743 @Override
San Mehat873f2142010-01-14 10:25:07 -0800744 public void untetherInterface(String iface) {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800745 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700746 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800747 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700748 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800749 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700750 }
San Mehat873f2142010-01-14 10:25:07 -0800751 }
752
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800753 @Override
754 public String[] listTetheredInterfaces() {
755 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700756 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800757 return NativeDaemonEvent.filterMessageList(
758 mConnector.executeForList("tether", "interface", "list"),
759 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700760 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800761 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700762 }
San Mehat873f2142010-01-14 10:25:07 -0800763 }
764
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800765 @Override
766 public void setDnsForwarders(String[] dns) {
767 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768
769 final Command cmd = new Command("tether", "dns", "set");
770 for (String s : dns) {
771 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
772 }
773
San Mehat873f2142010-01-14 10:25:07 -0800774 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800775 mConnector.execute(cmd);
776 } catch (NativeDaemonConnectorException e) {
777 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800778 }
779 }
780
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800781 @Override
782 public String[] getDnsForwarders() {
783 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700784 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800785 return NativeDaemonEvent.filterMessageList(
786 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700787 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800788 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700789 }
San Mehat873f2142010-01-14 10:25:07 -0800790 }
791
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800792 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700793 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800794 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700795
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800796 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
797 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800798 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800800 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800801 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
802 .getInterfaceAddresses();
803 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800804 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 InetAddress addr = NetworkUtils.getNetworkPart(
806 ia.getAddress(), ia.getNetworkPrefixLength());
807 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800808 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700809 }
810
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800811 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800812 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800813 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800814 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800815 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700816 }
817
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800818 @Override
819 public void enableNat(String internalInterface, String externalInterface) {
820 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700821 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700822 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 } catch (SocketException e) {
824 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700825 }
San Mehat873f2142010-01-14 10:25:07 -0800826 }
827
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800828 @Override
829 public void disableNat(String internalInterface, String externalInterface) {
830 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700831 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700832 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800833 } catch (SocketException e) {
834 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700835 }
San Mehat873f2142010-01-14 10:25:07 -0800836 }
San Mehat72759df2010-01-19 13:50:37 -0800837
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800838 @Override
839 public String[] listTtys() {
840 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700841 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800842 return NativeDaemonEvent.filterMessageList(
843 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700844 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800845 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700846 }
San Mehat72759df2010-01-19 13:50:37 -0800847 }
848
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800849 @Override
850 public void attachPppd(
851 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
852 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800853 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800854 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800855 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
856 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
857 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800858 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700859 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800860 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800861 }
862 }
863
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800864 @Override
865 public void detachPppd(String tty) {
866 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700867 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800868 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700869 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800870 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700871 }
San Mehat72759df2010-01-19 13:50:37 -0800872 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800873
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800874 @Override
875 public void startAccessPoint(
876 WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
877 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
878 mContext.enforceCallingOrSelfPermission(CHANGE_WIFI_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700879 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700880 wifiFirmwareReload(wlanIface, "AP");
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800881 mConnector.execute("softap", "start", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700882 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800883 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700884 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800885 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
886 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700887 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800888 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -0700889 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800890 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800891 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800892 }
893
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800894 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800895 switch (wifiConfig.getAuthType()) {
896 case KeyMgmt.WPA_PSK:
897 return "wpa-psk";
898 case KeyMgmt.WPA2_PSK:
899 return "wpa2-psk";
900 default:
901 return "open";
902 }
903 }
904
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700905 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800906 @Override
907 public void wifiFirmwareReload(String wlanIface, String mode) {
908 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
909 mContext.enforceCallingOrSelfPermission(CHANGE_WIFI_STATE, 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) {
919 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
920 mContext.enforceCallingOrSelfPermission(CHANGE_WIFI_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700921 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800922 mConnector.execute("softap", "stopap");
923 mConnector.execute("softap", "stop", wlanIface);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700924 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -0700925 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800926 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700927 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800928 }
929
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800930 @Override
931 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
932 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
933 mContext.enforceCallingOrSelfPermission(CHANGE_WIFI_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700934 try {
935 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700937 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800938 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
939 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700940 }
941 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800942 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700943 }
944 }
San Mehat91cac642010-03-31 14:31:36 -0700945
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700946 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700947 public NetworkStats getNetworkStatsSummary() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800948 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700949 return mStatsFactory.readNetworkStatsSummary();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700950 }
951
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700952 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700953 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800954 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700955 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -0700956 }
957
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700958 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700959 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700960 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
961
Jeff Sharkey350083e2011-06-29 10:45:16 -0700962 // silently discard when control disabled
963 // TODO: eventually migrate to be always enabled
964 if (!mBandwidthControlEnabled) return;
965
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700966 synchronized (mQuotaLock) {
967 if (mActiveQuotaIfaces.contains(iface)) {
968 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700969 }
970
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700971 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700972 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800973 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700974 mActiveQuotaIfaces.add(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700975 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800976 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700977 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -0700978 }
979 }
980
981 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700982 public void removeInterfaceQuota(String iface) {
983 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
984
Jeff Sharkey350083e2011-06-29 10:45:16 -0700985 // silently discard when control disabled
986 // TODO: eventually migrate to be always enabled
987 if (!mBandwidthControlEnabled) return;
988
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700989 synchronized (mQuotaLock) {
990 if (!mActiveQuotaIfaces.contains(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700991 // TODO: eventually consider throwing
992 return;
993 }
994
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -0800995 mActiveQuotaIfaces.remove(iface);
996 mActiveAlertIfaces.remove(iface);
997
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700998 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700999 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001000 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001001 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001002 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001003 }
1004 }
1005 }
1006
1007 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001008 public void setInterfaceAlert(String iface, long alertBytes) {
1009 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1010
1011 // silently discard when control disabled
1012 // TODO: eventually migrate to be always enabled
1013 if (!mBandwidthControlEnabled) return;
1014
1015 // quick sanity check
1016 if (!mActiveQuotaIfaces.contains(iface)) {
1017 throw new IllegalStateException("setting alert requires existing quota on iface");
1018 }
1019
1020 synchronized (mQuotaLock) {
1021 if (mActiveAlertIfaces.contains(iface)) {
1022 throw new IllegalStateException("iface " + iface + " already has alert");
1023 }
1024
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001025 try {
1026 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001028 mActiveAlertIfaces.add(iface);
1029 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001030 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001031 }
1032 }
1033 }
1034
1035 @Override
1036 public void removeInterfaceAlert(String iface) {
1037 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1038
1039 // silently discard when control disabled
1040 // TODO: eventually migrate to be always enabled
1041 if (!mBandwidthControlEnabled) return;
1042
1043 synchronized (mQuotaLock) {
1044 if (!mActiveAlertIfaces.contains(iface)) {
1045 // TODO: eventually consider throwing
1046 return;
1047 }
1048
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001049 try {
1050 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001051 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001052 mActiveAlertIfaces.remove(iface);
1053 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001054 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001055 }
1056 }
1057 }
1058
1059 @Override
1060 public void setGlobalAlert(long alertBytes) {
1061 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1062
1063 // silently discard when control disabled
1064 // TODO: eventually migrate to be always enabled
1065 if (!mBandwidthControlEnabled) return;
1066
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001067 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001068 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001069 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001070 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001071 }
1072 }
1073
1074 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001075 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
1076 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1077
Jeff Sharkey350083e2011-06-29 10:45:16 -07001078 // silently discard when control disabled
1079 // TODO: eventually migrate to be always enabled
1080 if (!mBandwidthControlEnabled) return;
1081
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001082 synchronized (mUidRejectOnQuota) {
1083 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1084 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1085 // TODO: eventually consider throwing
1086 return;
1087 }
1088
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001089 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001090 mConnector.execute("bandwidth",
1091 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001092 if (rejectOnQuotaInterfaces) {
1093 mUidRejectOnQuota.put(uid, true);
1094 } else {
1095 mUidRejectOnQuota.delete(uid);
1096 }
1097 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001098 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001099 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001100 }
1101 }
1102
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001103 @Override
1104 public boolean isBandwidthControlEnabled() {
1105 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1106 return mBandwidthControlEnabled;
1107 }
1108
1109 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001110 public NetworkStats getNetworkStatsUidDetail(int uid) {
1111 if (Binder.getCallingUid() != uid) {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001112 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001113 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001114 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001115 }
1116
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001117 @Override
1118 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001119 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001120
1121 if (ifacePairs.length % 2 != 0) {
1122 throw new IllegalArgumentException(
1123 "unexpected ifacePairs; length=" + ifacePairs.length);
1124 }
1125
1126 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1127 for (int i = 0; i < ifacePairs.length; i += 2) {
1128 final String ifaceIn = ifacePairs[i];
1129 final String ifaceOut = ifacePairs[i + 1];
1130 if (ifaceIn != null && ifaceOut != null) {
1131 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1132 }
1133 }
1134 return stats;
1135 }
1136
1137 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001138 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001139 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001140 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001141 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001142 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001143 }
1144
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001145 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001146
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001147 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1148 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1149 tok.nextToken();
1150 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001151
1152 try {
1153 final NetworkStats.Entry entry = new NetworkStats.Entry();
1154 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001155 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001156 entry.set = SET_DEFAULT;
1157 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001158 entry.rxBytes = Long.parseLong(tok.nextToken());
1159 entry.rxPackets = Long.parseLong(tok.nextToken());
1160 entry.txBytes = Long.parseLong(tok.nextToken());
1161 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001162 return entry;
1163 } catch (NumberFormatException e) {
1164 throw new IllegalStateException(
1165 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1166 }
1167 }
1168
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001169 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001170 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001171 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001172 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001173 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001174 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001175 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001176 }
San Mehat91cac642010-03-31 14:31:36 -07001177 }
1178
1179 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001180 mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181
1182 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001183 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001184 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1185 } catch (NativeDaemonConnectorException e) {
1186 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001187 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001188
1189 if (rx) {
1190 event.checkCode(InterfaceRxThrottleResult);
1191 } else {
1192 event.checkCode(InterfaceTxThrottleResult);
1193 }
1194
1195 try {
1196 return Integer.parseInt(event.getMessage());
1197 } catch (NumberFormatException e) {
1198 throw new IllegalStateException("unexpected response:" + event);
1199 }
San Mehat91cac642010-03-31 14:31:36 -07001200 }
1201
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001202 @Override
San Mehat91cac642010-03-31 14:31:36 -07001203 public int getInterfaceRxThrottle(String iface) {
1204 return getInterfaceThrottle(iface, true);
1205 }
1206
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001207 @Override
San Mehat91cac642010-03-31 14:31:36 -07001208 public int getInterfaceTxThrottle(String iface) {
1209 return getInterfaceThrottle(iface, false);
1210 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001211
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001212 @Override
1213 public void setDefaultInterfaceForDns(String iface) {
1214 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001215 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001216 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001217 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001218 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001219 }
1220 }
1221
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001222 @Override
1223 public void setDnsServersForInterface(String iface, String[] servers) {
1224 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001225
1226 final Command cmd = new Command("resolver", "setifdns", iface);
1227 for (String s : servers) {
1228 InetAddress a = NetworkUtils.numericToInetAddress(s);
1229 if (a.isAnyLocalAddress() == false) {
1230 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001231 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001232 }
1233
1234 try {
1235 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001236 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001237 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001238 }
1239 }
1240
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001241 @Override
1242 public void flushDefaultDnsCache() {
1243 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001244 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001245 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001246 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001247 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001248 }
1249 }
1250
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001251 @Override
1252 public void flushInterfaceDnsCache(String iface) {
1253 mContext.enforceCallingOrSelfPermission(CHANGE_NETWORK_STATE, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001254 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001255 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001256 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001257 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001258 }
1259 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001260
1261 /** {@inheritDoc} */
1262 public void monitor() {
1263 if (mConnector != null) {
1264 mConnector.monitor();
1265 }
1266 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001267
1268 @Override
1269 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1270 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1271
1272 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1273
1274 synchronized (mQuotaLock) {
1275 pw.print("Active quota ifaces: "); pw.println(mActiveQuotaIfaces.toString());
1276 pw.print("Active alert ifaces: "); pw.println(mActiveAlertIfaces.toString());
1277 }
1278
1279 synchronized (mUidRejectOnQuota) {
1280 pw.print("UID reject on quota ifaces: [");
1281 final int size = mUidRejectOnQuota.size();
1282 for (int i = 0; i < size; i++) {
1283 pw.print(mUidRejectOnQuota.keyAt(i));
1284 if (i < size - 1) pw.print(",");
1285 }
1286 pw.println("]");
1287 }
1288 }
San Mehat873f2142010-01-14 10:25:07 -08001289}