blob: 82a7d51a7d54965da3236a27ea07ca5afa5f9648 [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;
Selim Gurun84c00c62012-02-27 15:42:38 -0800120 public static final int DnsProxyQueryResult = 222;
Robert Greenwalte3253922010-02-18 09:23:25 -0800121
122 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700123 public static final int BandwidthControl = 601;
San Mehat873f2142010-01-14 10:25:07 -0800124 }
125
126 /**
127 * Binder context for this service
128 */
129 private Context mContext;
130
131 /**
132 * connector object for communicating with netd
133 */
134 private NativeDaemonConnector mConnector;
135
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700136 private Thread mThread;
137 private final CountDownLatch mConnectedSignal = new CountDownLatch(1);
138
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800139 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
140 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800141
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700142 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
143
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700144 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700145 /** Set of interfaces with active quotas. */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700146 private HashSet<String> mActiveQuotaIfaces = Sets.newHashSet();
147 /** Set of interfaces with active alerts. */
148 private HashSet<String> mActiveAlertIfaces = Sets.newHashSet();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700149 /** Set of UIDs with active reject rules. */
150 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
151
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700152 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700153
San Mehat873f2142010-01-14 10:25:07 -0800154 /**
155 * Constructs a new NetworkManagementService instance
156 *
157 * @param context Binder context for this service
158 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700159 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800160 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800161
Marco Nelissen62dbb222010-02-18 10:56:30 -0800162 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
163 return;
164 }
165
San Mehat873f2142010-01-14 10:25:07 -0800166 mConnector = new NativeDaemonConnector(
Robert Greenwalt470fd722012-01-18 12:51:15 -0800167 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 50);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700168 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700169
170 // Add ourself to the Watchdog monitors.
171 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700172 }
173
174 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700175 NetworkManagementService service = new NetworkManagementService(context);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700176 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
177 service.mThread.start();
178 if (DBG) Slog.d(TAG, "Awaiting socket connection");
179 service.mConnectedSignal.await();
180 if (DBG) Slog.d(TAG, "Connected");
181 return service;
San Mehat873f2142010-01-14 10:25:07 -0800182 }
183
Jeff Sharkey350083e2011-06-29 10:45:16 -0700184 public void systemReady() {
Jeff Sharkey350083e2011-06-29 10:45:16 -0700185 // only enable bandwidth control when support exists, and requested by
186 // system setting.
Jeff Sharkey350083e2011-06-29 10:45:16 -0700187 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
188 final boolean shouldEnable =
Jeff Sharkey05355c32011-08-09 14:44:19 -0700189 Settings.Secure.getInt(mContext.getContentResolver(), NETSTATS_ENABLED, 1) != 0;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700190
Jeff Sharkey350083e2011-06-29 10:45:16 -0700191 if (hasKernelSupport && shouldEnable) {
192 Slog.d(TAG, "enabling bandwidth control");
193 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800194 mConnector.execute("bandwidth", "enable");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700195 mBandwidthControlEnabled = true;
196 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700197 Log.wtf(TAG, "problem enabling bandwidth controls", e);
Jeff Sharkey350083e2011-06-29 10:45:16 -0700198 }
199 } else {
200 Slog.d(TAG, "not enabling bandwidth control");
201 }
Jeff Sharkey62a2c8f2011-07-13 15:24:02 -0700202
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700203 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700204 }
205
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800206 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800207 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800208 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800209 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800210 }
211
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800212 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800213 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800214 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800215 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800216 }
217
218 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700219 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800220 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700221 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800222 final int length = mObservers.beginBroadcast();
223 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800224 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800225 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
226 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700227 }
228 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800229 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700230 }
231
232 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700233 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700234 * (typically, an Ethernet cable has been plugged-in or unplugged).
235 */
236 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 final int length = mObservers.beginBroadcast();
238 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700239 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800240 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
241 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800242 }
243 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800244 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800245 }
246
247 /**
248 * Notify our observers of an interface addition.
249 */
250 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800251 final int length = mObservers.beginBroadcast();
252 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800253 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800254 mObservers.getBroadcastItem(i).interfaceAdded(iface);
255 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800256 }
257 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800258 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800259 }
260
261 /**
262 * Notify our observers of an interface removal.
263 */
264 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700265 // netd already clears out quota and alerts for removed ifaces; update
266 // our sanity-checking state.
267 mActiveAlertIfaces.remove(iface);
268 mActiveQuotaIfaces.remove(iface);
269
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800270 final int length = mObservers.beginBroadcast();
271 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800272 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800273 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
274 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800275 }
276 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800278 }
279
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700280 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700281 * Notify our observers of a limit reached.
282 */
283 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800284 final int length = mObservers.beginBroadcast();
285 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700286 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800287 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
288 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700289 }
290 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800291 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700292 }
293
294 /**
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700295 * Let us know the daemon is connected
296 */
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700297 protected void onDaemonConnected() {
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700298 mConnectedSignal.countDown();
299 }
300
San Mehat4d02d002010-01-22 16:07:46 -0800301
San Mehat873f2142010-01-14 10:25:07 -0800302 //
303 // Netd Callback handling
304 //
305
306 class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700307 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800308 public void onDaemonConnected() {
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700309 NetworkManagementService.this.onDaemonConnected();
San Mehat873f2142010-01-14 10:25:07 -0800310 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700311
312 /** {@inheritDoc} */
San Mehat873f2142010-01-14 10:25:07 -0800313 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700314 switch (code) {
315 case NetdResponseCode.InterfaceChange:
316 /*
317 * a network interface change occured
318 * Format: "NNN Iface added <name>"
319 * "NNN Iface removed <name>"
320 * "NNN Iface changed <name> <up/down>"
321 * "NNN Iface linkstatus <name> <up/down>"
322 */
323 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
324 throw new IllegalStateException(
325 String.format("Invalid event from daemon (%s)", raw));
326 }
327 if (cooked[2].equals("added")) {
328 notifyInterfaceAdded(cooked[3]);
329 return true;
330 } else if (cooked[2].equals("removed")) {
331 notifyInterfaceRemoved(cooked[3]);
332 return true;
333 } else if (cooked[2].equals("changed") && cooked.length == 5) {
334 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
335 return true;
336 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
337 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
338 return true;
339 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800340 throw new IllegalStateException(
341 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700342 // break;
343 case NetdResponseCode.BandwidthControl:
344 /*
345 * Bandwidth control needs some attention
346 * Format: "NNN limit alert <alertName> <ifaceName>"
347 */
348 if (cooked.length < 5 || !cooked[1].equals("limit")) {
349 throw new IllegalStateException(
350 String.format("Invalid event from daemon (%s)", raw));
351 }
352 if (cooked[2].equals("alert")) {
353 notifyLimitReached(cooked[3], cooked[4]);
354 return true;
355 }
356 throw new IllegalStateException(
357 String.format("Invalid event from daemon (%s)", raw));
358 // break;
359 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800360 }
361 return false;
San Mehat873f2142010-01-14 10:25:07 -0800362 }
363 }
364
San Mehated4fc8a2010-01-22 12:28:36 -0800365
San Mehat873f2142010-01-14 10:25:07 -0800366 //
367 // INetworkManagementService members
368 //
369
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800370 @Override
371 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800372 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700373 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800374 return NativeDaemonEvent.filterMessageList(
375 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700376 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800377 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700378 }
San Mehated4fc8a2010-01-22 12:28:36 -0800379 }
380
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800381 @Override
382 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800383 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800384
385 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700386 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800387 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700388 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800389 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700390 }
San Mehated4fc8a2010-01-22 12:28:36 -0800391
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800392 event.checkCode(InterfaceGetCfgResult);
393
394 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
395 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800396
Kenny Roota80ce062010-06-01 13:23:53 -0700397 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800398 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700399 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800400 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800401 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800402 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700403 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800404 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800405 } catch (IllegalArgumentException iae) {
406 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700407 }
408
409 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800410 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800411 } catch (NumberFormatException nfe) {
412 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700413 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800414
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800415 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
416 while (st.hasMoreTokens()) {
417 cfg.setFlag(st.nextToken());
418 }
Kenny Roota80ce062010-06-01 13:23:53 -0700419 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800420 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800421 }
San Mehated4fc8a2010-01-22 12:28:36 -0800422 return cfg;
423 }
424
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800425 @Override
426 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800427 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800428 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800429 if (linkAddr == null || linkAddr.getAddress() == null) {
430 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800431 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800432
433 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800434 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800435 linkAddr.getNetworkPrefixLength());
436 for (String flag : cfg.getFlags()) {
437 cmd.appendArg(flag);
438 }
439
Kenny Roota80ce062010-06-01 13:23:53 -0700440 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800441 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700442 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800443 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700444 }
San Mehat873f2142010-01-14 10:25:07 -0800445 }
446
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800447 @Override
448 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800449 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800450 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800451 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800452 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700453 }
454
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800455 @Override
456 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800457 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800458 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800459 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800460 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700461 }
462
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800463 @Override
464 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800465 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700466 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800467 mConnector.execute(
468 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700469 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800470 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700471 }
472 }
473
Irfan Sherifff5600612011-06-16 10:26:28 -0700474 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
475 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800476 @Override
477 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800478 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700479 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800480 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700481 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800482 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700483 }
484 }
485
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800486 @Override
487 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800488 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700489 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800490 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700491 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800492 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700493 }
494 }
495
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800496 @Override
497 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800498 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700499 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800500 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700501 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800502 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700503 }
504 }
505
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800506 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700507 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800508 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700509 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700510 }
511
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800512 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700513 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800514 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700515 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700516 }
517
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800518 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700519 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800520 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700521 modifyRoute(interfaceName, ADD, route, SECONDARY);
522 }
523
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800524 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700525 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800526 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700527 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
528 }
529
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800530 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
531 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700532
533 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800534 final LinkAddress la = route.getDestination();
535 cmd.appendArg(la.getAddress().getHostAddress());
536 cmd.appendArg(la.getNetworkPrefixLength());
537
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700538 if (route.getGateway() == null) {
539 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800540 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700541 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800542 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700543 }
544 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800545 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700546 }
547
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800548 try {
549 mConnector.execute(cmd);
550 } catch (NativeDaemonConnectorException e) {
551 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700552 }
553 }
554
555 private ArrayList<String> readRouteList(String filename) {
556 FileInputStream fstream = null;
557 ArrayList<String> list = new ArrayList<String>();
558
559 try {
560 fstream = new FileInputStream(filename);
561 DataInputStream in = new DataInputStream(fstream);
562 BufferedReader br = new BufferedReader(new InputStreamReader(in));
563 String s;
564
565 // throw away the title line
566
567 while (((s = br.readLine()) != null) && (s.length() != 0)) {
568 list.add(s);
569 }
570 } catch (IOException ex) {
571 // return current list, possibly empty
572 } finally {
573 if (fstream != null) {
574 try {
575 fstream.close();
576 } catch (IOException ex) {}
577 }
578 }
579
580 return list;
581 }
582
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800583 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700584 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800585 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700586 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
587
588 // v4 routes listed as:
589 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
590 for (String s : readRouteList("/proc/net/route")) {
591 String[] fields = s.split("\t");
592
593 if (fields.length > 7) {
594 String iface = fields[0];
595
596 if (interfaceName.equals(iface)) {
597 String dest = fields[1];
598 String gate = fields[2];
599 String flags = fields[3]; // future use?
600 String mask = fields[7];
601 try {
602 // address stored as a hex string, ex: 0014A8C0
603 InetAddress destAddr =
604 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
605 int prefixLength =
606 NetworkUtils.netmaskIntToPrefixLength(
607 (int)Long.parseLong(mask, 16));
608 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
609
610 // address stored as a hex string, ex 0014A8C0
611 InetAddress gatewayAddr =
612 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
613
614 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
615 routes.add(route);
616 } catch (Exception e) {
617 Log.e(TAG, "Error parsing route " + s + " : " + e);
618 continue;
619 }
620 }
621 }
622 }
623
624 // v6 routes listed as:
625 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
626 for (String s : readRouteList("/proc/net/ipv6_route")) {
627 String[]fields = s.split("\\s+");
628 if (fields.length > 9) {
629 String iface = fields[9].trim();
630 if (interfaceName.equals(iface)) {
631 String dest = fields[0];
632 String prefix = fields[1];
633 String gate = fields[4];
634
635 try {
636 // prefix length stored as a hex string, ex 40
637 int prefixLength = Integer.parseInt(prefix, 16);
638
639 // address stored as a 32 char hex string
640 // ex fe800000000000000000000000000000
641 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
642 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
643
644 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
645
646 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
647 routes.add(route);
648 } catch (Exception e) {
649 Log.e(TAG, "Error parsing route " + s + " : " + e);
650 continue;
651 }
652 }
653 }
654 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800655 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700656 }
657
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800658 @Override
San Mehat873f2142010-01-14 10:25:07 -0800659 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800660 // TODO: remove from aidl if nobody calls externally
661 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800662
Joe Onorato8a9b2202010-02-26 18:56:32 -0800663 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800664 }
665
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800666 @Override
San Mehat873f2142010-01-14 10:25:07 -0800667 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800668 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800669
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800670 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700671 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800672 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700673 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800674 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700675 }
San Mehat873f2142010-01-14 10:25:07 -0800676
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800677 // 211 Forwarding enabled
678 event.checkCode(IpFwdStatusResult);
679 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800680 }
681
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800682 @Override
683 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800685 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800686 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800687 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800688 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800689 }
San Mehat873f2142010-01-14 10:25:07 -0800690 }
691
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800692 @Override
693 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800694 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700695 // cmd is "tether start first_start first_stop second_start second_stop ..."
696 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800697
698 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700699 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800700 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700701 }
Kenny Roota80ce062010-06-01 13:23:53 -0700702
703 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800704 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700705 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800706 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700707 }
San Mehat873f2142010-01-14 10:25:07 -0800708 }
709
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800710 @Override
711 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800712 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700713 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800714 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700715 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800716 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700717 }
San Mehat873f2142010-01-14 10:25:07 -0800718 }
719
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800720 @Override
721 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800722 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800723
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800724 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700725 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800726 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700727 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800728 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700729 }
San Mehat873f2142010-01-14 10:25:07 -0800730
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800731 // 210 Tethering services started
732 event.checkCode(TetherStatusResult);
733 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800734 }
735
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800736 @Override
737 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800738 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700739 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800740 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700741 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800742 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700743 }
San Mehat873f2142010-01-14 10:25:07 -0800744 }
745
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800746 @Override
San Mehat873f2142010-01-14 10:25:07 -0800747 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800748 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700749 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800750 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700751 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800752 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700753 }
San Mehat873f2142010-01-14 10:25:07 -0800754 }
755
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800756 @Override
757 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800758 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700759 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800760 return NativeDaemonEvent.filterMessageList(
761 mConnector.executeForList("tether", "interface", "list"),
762 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700763 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800764 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700765 }
San Mehat873f2142010-01-14 10:25:07 -0800766 }
767
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800768 @Override
769 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800770 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800771
772 final Command cmd = new Command("tether", "dns", "set");
773 for (String s : dns) {
774 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
775 }
776
San Mehat873f2142010-01-14 10:25:07 -0800777 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800778 mConnector.execute(cmd);
779 } catch (NativeDaemonConnectorException e) {
780 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800781 }
782 }
783
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800784 @Override
785 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800786 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700787 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800788 return NativeDaemonEvent.filterMessageList(
789 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700790 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800791 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700792 }
San Mehat873f2142010-01-14 10:25:07 -0800793 }
794
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800795 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700796 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800797 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700798
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
800 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800801 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800802 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800803 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800804 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
805 .getInterfaceAddresses();
806 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800807 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800808 InetAddress addr = NetworkUtils.getNetworkPart(
809 ia.getAddress(), ia.getNetworkPrefixLength());
810 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800811 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700812 }
813
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800814 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800815 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800816 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800817 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800818 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700819 }
820
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800821 @Override
822 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800823 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700824 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700825 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800826 } catch (SocketException e) {
827 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700828 }
San Mehat873f2142010-01-14 10:25:07 -0800829 }
830
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800831 @Override
832 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800833 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700834 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700835 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800836 } catch (SocketException e) {
837 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700838 }
San Mehat873f2142010-01-14 10:25:07 -0800839 }
San Mehat72759df2010-01-19 13:50:37 -0800840
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800841 @Override
842 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800843 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700844 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800845 return NativeDaemonEvent.filterMessageList(
846 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700847 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800848 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700849 }
San Mehat72759df2010-01-19 13:50:37 -0800850 }
851
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800852 @Override
853 public void attachPppd(
854 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800855 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800856 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800857 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800858 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
859 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
860 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800861 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700862 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800863 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800864 }
865 }
866
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800867 @Override
868 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800869 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700870 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800871 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700872 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800873 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700874 }
San Mehat72759df2010-01-19 13:50:37 -0800875 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800876
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800877 @Override
878 public void startAccessPoint(
879 WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800880 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700881 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700882 wifiFirmwareReload(wlanIface, "AP");
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 Sharkeye8914c32012-05-01 16:26:09 -0700945 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800946 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700947 return mStatsFactory.readNetworkStatsSummaryDev();
948 }
949
950 @Override
951 public NetworkStats getNetworkStatsSummaryXt() {
952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
953 return mStatsFactory.readNetworkStatsSummaryXt();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700954 }
955
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700956 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700957 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800958 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700959 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -0700960 }
961
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700962 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700963 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800964 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700965
Jeff Sharkey350083e2011-06-29 10:45:16 -0700966 // silently discard when control disabled
967 // TODO: eventually migrate to be always enabled
968 if (!mBandwidthControlEnabled) return;
969
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700970 synchronized (mQuotaLock) {
971 if (mActiveQuotaIfaces.contains(iface)) {
972 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700973 }
974
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700975 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700976 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700978 mActiveQuotaIfaces.add(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700979 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800980 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700981 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -0700982 }
983 }
984
985 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700986 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800987 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700988
Jeff Sharkey350083e2011-06-29 10:45:16 -0700989 // silently discard when control disabled
990 // TODO: eventually migrate to be always enabled
991 if (!mBandwidthControlEnabled) return;
992
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700993 synchronized (mQuotaLock) {
994 if (!mActiveQuotaIfaces.contains(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700995 // TODO: eventually consider throwing
996 return;
997 }
998
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -0800999 mActiveQuotaIfaces.remove(iface);
1000 mActiveAlertIfaces.remove(iface);
1001
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001002 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001003 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001004 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001005 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001006 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001007 }
1008 }
1009 }
1010
1011 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001012 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001013 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001014
1015 // silently discard when control disabled
1016 // TODO: eventually migrate to be always enabled
1017 if (!mBandwidthControlEnabled) return;
1018
1019 // quick sanity check
1020 if (!mActiveQuotaIfaces.contains(iface)) {
1021 throw new IllegalStateException("setting alert requires existing quota on iface");
1022 }
1023
1024 synchronized (mQuotaLock) {
1025 if (mActiveAlertIfaces.contains(iface)) {
1026 throw new IllegalStateException("iface " + iface + " already has alert");
1027 }
1028
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001029 try {
1030 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001031 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001032 mActiveAlertIfaces.add(iface);
1033 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001034 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001035 }
1036 }
1037 }
1038
1039 @Override
1040 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001041 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001042
1043 // silently discard when control disabled
1044 // TODO: eventually migrate to be always enabled
1045 if (!mBandwidthControlEnabled) return;
1046
1047 synchronized (mQuotaLock) {
1048 if (!mActiveAlertIfaces.contains(iface)) {
1049 // TODO: eventually consider throwing
1050 return;
1051 }
1052
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001053 try {
1054 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001055 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001056 mActiveAlertIfaces.remove(iface);
1057 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001058 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001059 }
1060 }
1061 }
1062
1063 @Override
1064 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001065 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001066
1067 // silently discard when control disabled
1068 // TODO: eventually migrate to be always enabled
1069 if (!mBandwidthControlEnabled) return;
1070
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001071 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001073 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001074 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001075 }
1076 }
1077
1078 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001079 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001080 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001081
Jeff Sharkey350083e2011-06-29 10:45:16 -07001082 // silently discard when control disabled
1083 // TODO: eventually migrate to be always enabled
1084 if (!mBandwidthControlEnabled) return;
1085
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001086 synchronized (mUidRejectOnQuota) {
1087 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1088 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1089 // TODO: eventually consider throwing
1090 return;
1091 }
1092
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001093 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001094 mConnector.execute("bandwidth",
1095 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001096 if (rejectOnQuotaInterfaces) {
1097 mUidRejectOnQuota.put(uid, true);
1098 } else {
1099 mUidRejectOnQuota.delete(uid);
1100 }
1101 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001102 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001103 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001104 }
1105 }
1106
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001107 @Override
1108 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001109 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001110 return mBandwidthControlEnabled;
1111 }
1112
1113 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001114 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001116 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001117 }
1118
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001119 @Override
1120 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001121 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001122
1123 if (ifacePairs.length % 2 != 0) {
1124 throw new IllegalArgumentException(
1125 "unexpected ifacePairs; length=" + ifacePairs.length);
1126 }
1127
1128 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1129 for (int i = 0; i < ifacePairs.length; i += 2) {
1130 final String ifaceIn = ifacePairs[i];
1131 final String ifaceOut = ifacePairs[i + 1];
1132 if (ifaceIn != null && ifaceOut != null) {
1133 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1134 }
1135 }
1136 return stats;
1137 }
1138
1139 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001140 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001141 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001142 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001143 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001144 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001145 }
1146
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001147 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001148
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001149 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1150 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1151 tok.nextToken();
1152 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001153
1154 try {
1155 final NetworkStats.Entry entry = new NetworkStats.Entry();
1156 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001157 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001158 entry.set = SET_DEFAULT;
1159 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001160 entry.rxBytes = Long.parseLong(tok.nextToken());
1161 entry.rxPackets = Long.parseLong(tok.nextToken());
1162 entry.txBytes = Long.parseLong(tok.nextToken());
1163 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001164 return entry;
1165 } catch (NumberFormatException e) {
1166 throw new IllegalStateException(
1167 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1168 }
1169 }
1170
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001171 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001172 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001173 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001174 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001175 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001176 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001177 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001178 }
San Mehat91cac642010-03-31 14:31:36 -07001179 }
1180
1181 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001182 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) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001204 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001205 return getInterfaceThrottle(iface, true);
1206 }
1207
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001208 @Override
San Mehat91cac642010-03-31 14:31:36 -07001209 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001210 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001211 return getInterfaceThrottle(iface, false);
1212 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001213
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001214 @Override
1215 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001216 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001217 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001218 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001219 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001220 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001221 }
1222 }
1223
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001224 @Override
1225 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001226 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001227
1228 final Command cmd = new Command("resolver", "setifdns", iface);
1229 for (String s : servers) {
1230 InetAddress a = NetworkUtils.numericToInetAddress(s);
1231 if (a.isAnyLocalAddress() == false) {
1232 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001233 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001234 }
1235
1236 try {
1237 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001238 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001239 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001240 }
1241 }
1242
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001243 @Override
1244 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001245 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001246 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001247 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001248 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001249 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001250 }
1251 }
1252
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001253 @Override
1254 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001255 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001256 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001257 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001258 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001259 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001260 }
1261 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001262
1263 /** {@inheritDoc} */
1264 public void monitor() {
1265 if (mConnector != null) {
1266 mConnector.monitor();
1267 }
1268 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001269
1270 @Override
1271 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1272 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1273
Robert Greenwalt470fd722012-01-18 12:51:15 -08001274 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1275 mConnector.dump(fd, pw, args);
1276 pw.println();
1277
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001278 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1279
1280 synchronized (mQuotaLock) {
1281 pw.print("Active quota ifaces: "); pw.println(mActiveQuotaIfaces.toString());
1282 pw.print("Active alert ifaces: "); pw.println(mActiveAlertIfaces.toString());
1283 }
1284
1285 synchronized (mUidRejectOnQuota) {
1286 pw.print("UID reject on quota ifaces: [");
1287 final int size = mUidRejectOnQuota.size();
1288 for (int i = 0; i < size; i++) {
1289 pw.print(mUidRejectOnQuota.keyAt(i));
1290 if (i < size - 1) pw.print(",");
1291 }
1292 pw.println("]");
1293 }
1294 }
San Mehat873f2142010-01-14 10:25:07 -08001295}