blob: 817d04df99f1c145017f0bbdb62f42b1c45fed9e [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 Sharkeyba2896e2011-11-30 18:13:54 -080026import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
27import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
28import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceRxThrottleResult;
29import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceTxThrottleResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult;
35import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070036import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
San Mehat873f2142010-01-14 10:25:07 -080038import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080039import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070040import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080041import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070044import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080045import android.net.wifi.WifiConfiguration;
46import android.net.wifi.WifiConfiguration.KeyMgmt;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070047import android.os.Handler;
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;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080053import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080054import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070055import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080056
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070057import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080058import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070059import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070060
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070061import java.io.BufferedReader;
62import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080063import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070064import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070065import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070066import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070067import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070068import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070069import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070070import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070071import java.net.InterfaceAddress;
72import java.net.NetworkInterface;
73import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070074import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070075import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070076import java.util.HashMap;
77import java.util.Map;
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
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700136 private final Handler mMainHandler = new Handler();
137
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700138 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700139 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700140
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800141 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
142 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800143
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700144 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
145
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700146 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700147 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700148 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700149 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700150 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700151 /** Set of UIDs with active reject rules. */
152 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
153
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700154 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700155
San Mehat873f2142010-01-14 10:25:07 -0800156 /**
157 * Constructs a new NetworkManagementService instance
158 *
159 * @param context Binder context for this service
160 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700161 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800162 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800163
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(
Robert Greenwalt470fd722012-01-18 12:51:15 -0800169 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 50);
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 Sharkeyb24a7852012-05-01 15:19:37 -0700177 final NetworkManagementService service = new NetworkManagementService(context);
178 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700179 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
180 service.mThread.start();
181 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700182 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700183 if (DBG) Slog.d(TAG, "Connected");
184 return service;
San Mehat873f2142010-01-14 10:25:07 -0800185 }
186
Jeff Sharkey350083e2011-06-29 10:45:16 -0700187 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700188 prepareNativeDaemon();
189 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700190 }
191
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800192 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800193 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800194 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800195 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800196 }
197
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800198 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800199 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800200 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800201 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800202 }
203
204 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700205 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800206 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700207 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800208 final int length = mObservers.beginBroadcast();
209 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800210 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800211 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
212 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700213 }
214 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800215 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700216 }
217
218 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700219 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700220 * (typically, an Ethernet cable has been plugged-in or unplugged).
221 */
222 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800223 final int length = mObservers.beginBroadcast();
224 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700225 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800226 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
227 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800228 }
229 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800230 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800231 }
232
233 /**
234 * Notify our observers of an interface addition.
235 */
236 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 final int length = mObservers.beginBroadcast();
238 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800239 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800240 mObservers.getBroadcastItem(i).interfaceAdded(iface);
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 removal.
249 */
250 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700251 // netd already clears out quota and alerts for removed ifaces; update
252 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700253 mActiveAlerts.remove(iface);
254 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700255
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800256 final int length = mObservers.beginBroadcast();
257 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800258 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800259 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
260 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800261 }
262 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800263 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800264 }
265
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700266 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700267 * Notify our observers of a limit reached.
268 */
269 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800270 final int length = mObservers.beginBroadcast();
271 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700272 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800273 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
274 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700275 }
276 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700278 }
279
280 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700281 * Prepare native daemon once connected, enabling modules and pushing any
282 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700283 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700284 private void prepareNativeDaemon() {
285 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700286
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700287 // only enable bandwidth control when support exists
288 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
289 if (hasKernelSupport) {
290 Slog.d(TAG, "enabling bandwidth control");
291 try {
292 mConnector.execute("bandwidth", "enable");
293 mBandwidthControlEnabled = true;
294 } catch (NativeDaemonConnectorException e) {
295 Log.wtf(TAG, "problem enabling bandwidth controls", e);
296 }
297 } else {
298 Slog.d(TAG, "not enabling bandwidth control");
299 }
300
301 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
302
303 // push any existing quota or UID rules
304 synchronized (mQuotaLock) {
305 int size = mActiveQuotas.size();
306 if (size > 0) {
307 Slog.d(TAG, "pushing " + size + " active quota rules");
308 final HashMap<String, Long> activeQuotas = mActiveQuotas;
309 mActiveQuotas = Maps.newHashMap();
310 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
311 setInterfaceQuota(entry.getKey(), entry.getValue());
312 }
313 }
314
315 size = mActiveAlerts.size();
316 if (size > 0) {
317 Slog.d(TAG, "pushing " + size + " active alert rules");
318 final HashMap<String, Long> activeAlerts = mActiveAlerts;
319 mActiveAlerts = Maps.newHashMap();
320 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
321 setInterfaceAlert(entry.getKey(), entry.getValue());
322 }
323 }
324
325 size = mUidRejectOnQuota.size();
326 if (size > 0) {
327 Slog.d(TAG, "pushing " + size + " active uid rules");
328 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
329 mUidRejectOnQuota = new SparseBooleanArray();
330 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
331 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
332 }
333 }
334 }
335 }
San Mehat4d02d002010-01-22 16:07:46 -0800336
San Mehat873f2142010-01-14 10:25:07 -0800337 //
338 // Netd Callback handling
339 //
340
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700341 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
342 @Override
San Mehat873f2142010-01-14 10:25:07 -0800343 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700344 // event is dispatched from internal NDC thread, so we prepare the
345 // daemon back on main thread.
346 if (mConnectedSignal != null) {
347 mConnectedSignal.countDown();
348 mConnectedSignal = null;
349 } else {
350 mMainHandler.post(new Runnable() {
351 @Override
352 public void run() {
353 prepareNativeDaemon();
354 }
355 });
356 }
San Mehat873f2142010-01-14 10:25:07 -0800357 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700358
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700359 @Override
San Mehat873f2142010-01-14 10:25:07 -0800360 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700361 switch (code) {
362 case NetdResponseCode.InterfaceChange:
363 /*
364 * a network interface change occured
365 * Format: "NNN Iface added <name>"
366 * "NNN Iface removed <name>"
367 * "NNN Iface changed <name> <up/down>"
368 * "NNN Iface linkstatus <name> <up/down>"
369 */
370 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
371 throw new IllegalStateException(
372 String.format("Invalid event from daemon (%s)", raw));
373 }
374 if (cooked[2].equals("added")) {
375 notifyInterfaceAdded(cooked[3]);
376 return true;
377 } else if (cooked[2].equals("removed")) {
378 notifyInterfaceRemoved(cooked[3]);
379 return true;
380 } else if (cooked[2].equals("changed") && cooked.length == 5) {
381 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
382 return true;
383 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
384 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
385 return true;
386 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800387 throw new IllegalStateException(
388 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700389 // break;
390 case NetdResponseCode.BandwidthControl:
391 /*
392 * Bandwidth control needs some attention
393 * Format: "NNN limit alert <alertName> <ifaceName>"
394 */
395 if (cooked.length < 5 || !cooked[1].equals("limit")) {
396 throw new IllegalStateException(
397 String.format("Invalid event from daemon (%s)", raw));
398 }
399 if (cooked[2].equals("alert")) {
400 notifyLimitReached(cooked[3], cooked[4]);
401 return true;
402 }
403 throw new IllegalStateException(
404 String.format("Invalid event from daemon (%s)", raw));
405 // break;
406 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800407 }
408 return false;
San Mehat873f2142010-01-14 10:25:07 -0800409 }
410 }
411
San Mehated4fc8a2010-01-22 12:28:36 -0800412
San Mehat873f2142010-01-14 10:25:07 -0800413 //
414 // INetworkManagementService members
415 //
416
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800417 @Override
418 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800419 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700420 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800421 return NativeDaemonEvent.filterMessageList(
422 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700423 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800424 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700425 }
San Mehated4fc8a2010-01-22 12:28:36 -0800426 }
427
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800428 @Override
429 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800430 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800431
432 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700433 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800434 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700435 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800436 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700437 }
San Mehated4fc8a2010-01-22 12:28:36 -0800438
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800439 event.checkCode(InterfaceGetCfgResult);
440
441 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
442 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800443
Kenny Roota80ce062010-06-01 13:23:53 -0700444 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800445 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700446 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800447 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800448 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800449 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700450 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800451 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800452 } catch (IllegalArgumentException iae) {
453 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700454 }
455
456 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800457 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800458 } catch (NumberFormatException nfe) {
459 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700460 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800461
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800462 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
463 while (st.hasMoreTokens()) {
464 cfg.setFlag(st.nextToken());
465 }
Kenny Roota80ce062010-06-01 13:23:53 -0700466 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800467 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800468 }
San Mehated4fc8a2010-01-22 12:28:36 -0800469 return cfg;
470 }
471
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800472 @Override
473 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800475 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800476 if (linkAddr == null || linkAddr.getAddress() == null) {
477 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800478 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800479
480 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800481 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800482 linkAddr.getNetworkPrefixLength());
483 for (String flag : cfg.getFlags()) {
484 cmd.appendArg(flag);
485 }
486
Kenny Roota80ce062010-06-01 13:23:53 -0700487 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800488 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700489 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800490 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700491 }
San Mehat873f2142010-01-14 10:25:07 -0800492 }
493
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800494 @Override
495 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800496 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800497 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800498 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800499 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700500 }
501
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800502 @Override
503 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800504 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800505 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800506 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800507 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700508 }
509
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800510 @Override
511 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800512 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700513 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800514 mConnector.execute(
515 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700516 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800517 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700518 }
519 }
520
Irfan Sherifff5600612011-06-16 10:26:28 -0700521 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
522 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800523 @Override
524 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800525 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700526 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800527 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700528 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800529 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700530 }
531 }
532
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800533 @Override
534 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800535 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700536 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800537 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700538 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800539 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700540 }
541 }
542
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800543 @Override
544 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800545 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700546 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800547 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700548 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800549 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700550 }
551 }
552
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800553 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700554 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800555 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700556 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700557 }
558
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800559 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700560 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800561 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700562 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700563 }
564
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800565 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700566 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800567 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700568 modifyRoute(interfaceName, ADD, route, SECONDARY);
569 }
570
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800571 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700572 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800573 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700574 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
575 }
576
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800577 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
578 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700579
580 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800581 final LinkAddress la = route.getDestination();
582 cmd.appendArg(la.getAddress().getHostAddress());
583 cmd.appendArg(la.getNetworkPrefixLength());
584
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700585 if (route.getGateway() == null) {
586 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800587 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700588 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800589 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700590 }
591 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800592 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700593 }
594
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800595 try {
596 mConnector.execute(cmd);
597 } catch (NativeDaemonConnectorException e) {
598 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700599 }
600 }
601
602 private ArrayList<String> readRouteList(String filename) {
603 FileInputStream fstream = null;
604 ArrayList<String> list = new ArrayList<String>();
605
606 try {
607 fstream = new FileInputStream(filename);
608 DataInputStream in = new DataInputStream(fstream);
609 BufferedReader br = new BufferedReader(new InputStreamReader(in));
610 String s;
611
612 // throw away the title line
613
614 while (((s = br.readLine()) != null) && (s.length() != 0)) {
615 list.add(s);
616 }
617 } catch (IOException ex) {
618 // return current list, possibly empty
619 } finally {
620 if (fstream != null) {
621 try {
622 fstream.close();
623 } catch (IOException ex) {}
624 }
625 }
626
627 return list;
628 }
629
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800630 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700631 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800632 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700633 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
634
635 // v4 routes listed as:
636 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
637 for (String s : readRouteList("/proc/net/route")) {
638 String[] fields = s.split("\t");
639
640 if (fields.length > 7) {
641 String iface = fields[0];
642
643 if (interfaceName.equals(iface)) {
644 String dest = fields[1];
645 String gate = fields[2];
646 String flags = fields[3]; // future use?
647 String mask = fields[7];
648 try {
649 // address stored as a hex string, ex: 0014A8C0
650 InetAddress destAddr =
651 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
652 int prefixLength =
653 NetworkUtils.netmaskIntToPrefixLength(
654 (int)Long.parseLong(mask, 16));
655 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
656
657 // address stored as a hex string, ex 0014A8C0
658 InetAddress gatewayAddr =
659 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
660
661 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
662 routes.add(route);
663 } catch (Exception e) {
664 Log.e(TAG, "Error parsing route " + s + " : " + e);
665 continue;
666 }
667 }
668 }
669 }
670
671 // v6 routes listed as:
672 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
673 for (String s : readRouteList("/proc/net/ipv6_route")) {
674 String[]fields = s.split("\\s+");
675 if (fields.length > 9) {
676 String iface = fields[9].trim();
677 if (interfaceName.equals(iface)) {
678 String dest = fields[0];
679 String prefix = fields[1];
680 String gate = fields[4];
681
682 try {
683 // prefix length stored as a hex string, ex 40
684 int prefixLength = Integer.parseInt(prefix, 16);
685
686 // address stored as a 32 char hex string
687 // ex fe800000000000000000000000000000
688 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
689 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
690
691 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
692
693 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
694 routes.add(route);
695 } catch (Exception e) {
696 Log.e(TAG, "Error parsing route " + s + " : " + e);
697 continue;
698 }
699 }
700 }
701 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800702 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700703 }
704
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800705 @Override
San Mehat873f2142010-01-14 10:25:07 -0800706 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800707 // TODO: remove from aidl if nobody calls externally
708 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800709
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800711 }
712
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800713 @Override
San Mehat873f2142010-01-14 10:25:07 -0800714 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800715 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800716
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800717 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700718 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800719 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700720 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800721 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700722 }
San Mehat873f2142010-01-14 10:25:07 -0800723
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800724 // 211 Forwarding enabled
725 event.checkCode(IpFwdStatusResult);
726 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800727 }
728
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800729 @Override
730 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800731 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800732 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800733 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800734 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800735 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800736 }
San Mehat873f2142010-01-14 10:25:07 -0800737 }
738
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800739 @Override
740 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800741 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700742 // cmd is "tether start first_start first_stop second_start second_stop ..."
743 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800744
745 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700746 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800747 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700748 }
Kenny Roota80ce062010-06-01 13:23:53 -0700749
750 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800751 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700752 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800753 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700754 }
San Mehat873f2142010-01-14 10:25:07 -0800755 }
756
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800757 @Override
758 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800759 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700760 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800761 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700762 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800763 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700764 }
San Mehat873f2142010-01-14 10:25:07 -0800765 }
766
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800767 @Override
768 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800769 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800770
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800771 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700772 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800773 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700774 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800775 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700776 }
San Mehat873f2142010-01-14 10:25:07 -0800777
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800778 // 210 Tethering services started
779 event.checkCode(TetherStatusResult);
780 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800781 }
782
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800783 @Override
784 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800785 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700786 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800787 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700788 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800789 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700790 }
San Mehat873f2142010-01-14 10:25:07 -0800791 }
792
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800793 @Override
San Mehat873f2142010-01-14 10:25:07 -0800794 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800795 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700796 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800797 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700798 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800799 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700800 }
San Mehat873f2142010-01-14 10:25:07 -0800801 }
802
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800803 @Override
804 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800805 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700806 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800807 return NativeDaemonEvent.filterMessageList(
808 mConnector.executeForList("tether", "interface", "list"),
809 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700810 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800811 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700812 }
San Mehat873f2142010-01-14 10:25:07 -0800813 }
814
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800815 @Override
816 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800817 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800818
819 final Command cmd = new Command("tether", "dns", "set");
820 for (String s : dns) {
821 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
822 }
823
San Mehat873f2142010-01-14 10:25:07 -0800824 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800825 mConnector.execute(cmd);
826 } catch (NativeDaemonConnectorException e) {
827 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800828 }
829 }
830
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800831 @Override
832 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800833 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700834 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800835 return NativeDaemonEvent.filterMessageList(
836 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700837 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800838 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700839 }
San Mehat873f2142010-01-14 10:25:07 -0800840 }
841
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800842 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700843 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800844 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700845
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800846 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
847 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800848 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800849 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800850 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800851 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
852 .getInterfaceAddresses();
853 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800854 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800855 InetAddress addr = NetworkUtils.getNetworkPart(
856 ia.getAddress(), ia.getNetworkPrefixLength());
857 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800858 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700859 }
860
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800861 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800862 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800863 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800864 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800865 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700866 }
867
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800868 @Override
869 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800870 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700871 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700872 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800873 } catch (SocketException e) {
874 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700875 }
San Mehat873f2142010-01-14 10:25:07 -0800876 }
877
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800878 @Override
879 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800880 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700881 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700882 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800883 } catch (SocketException e) {
884 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700885 }
San Mehat873f2142010-01-14 10:25:07 -0800886 }
San Mehat72759df2010-01-19 13:50:37 -0800887
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800888 @Override
889 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800890 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700891 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800892 return NativeDaemonEvent.filterMessageList(
893 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700894 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800895 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700896 }
San Mehat72759df2010-01-19 13:50:37 -0800897 }
898
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800899 @Override
900 public void attachPppd(
901 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800902 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800903 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800904 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800905 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
906 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
907 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700909 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800910 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800911 }
912 }
913
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800914 @Override
915 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800916 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700917 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800918 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700919 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800920 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700921 }
San Mehat72759df2010-01-19 13:50:37 -0800922 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800923
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800924 @Override
925 public void startAccessPoint(
926 WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800927 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700928 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700929 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -0700930 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700932 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800933 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
934 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700935 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -0700937 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800938 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800939 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800940 }
941
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800942 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800943 switch (wifiConfig.getAuthType()) {
944 case KeyMgmt.WPA_PSK:
945 return "wpa-psk";
946 case KeyMgmt.WPA2_PSK:
947 return "wpa2-psk";
948 default:
949 return "open";
950 }
951 }
952
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700953 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800954 @Override
955 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800956 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700957 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800958 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700959 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800960 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700961 }
962 }
963
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800964 @Override
965 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800966 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700967 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800968 mConnector.execute("softap", "stopap");
969 mConnector.execute("softap", "stop", wlanIface);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700970 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -0700971 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800972 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700973 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800974 }
975
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800976 @Override
977 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800978 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700979 try {
980 if (wifiConfig == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800981 mConnector.execute("softap", "set", wlanIface, softapIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700982 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800983 mConnector.execute("softap", "set", wlanIface, softapIface, wifiConfig.SSID,
984 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700985 }
986 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800987 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -0700988 }
989 }
San Mehat91cac642010-03-31 14:31:36 -0700990
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700991 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700992 public NetworkStats getNetworkStatsSummary() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800993 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700994 return mStatsFactory.readNetworkStatsSummary();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700995 }
996
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700997 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700998 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800999 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001000 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -07001001 }
1002
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001003 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001004 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001005 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001006
Jeff Sharkey350083e2011-06-29 10:45:16 -07001007 // silently discard when control disabled
1008 // TODO: eventually migrate to be always enabled
1009 if (!mBandwidthControlEnabled) return;
1010
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001011 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001012 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001013 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001014 }
1015
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001016 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001017 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001018 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001019 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001020 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001021 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001022 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001023 }
1024 }
1025
1026 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001027 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001028 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001029
Jeff Sharkey350083e2011-06-29 10:45:16 -07001030 // silently discard when control disabled
1031 // TODO: eventually migrate to be always enabled
1032 if (!mBandwidthControlEnabled) return;
1033
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001034 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001035 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001036 // TODO: eventually consider throwing
1037 return;
1038 }
1039
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001040 mActiveQuotas.remove(iface);
1041 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001042
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001043 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001044 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001045 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001046 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001047 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001048 }
1049 }
1050 }
1051
1052 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001053 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001054 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001055
1056 // silently discard when control disabled
1057 // TODO: eventually migrate to be always enabled
1058 if (!mBandwidthControlEnabled) return;
1059
1060 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001061 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001062 throw new IllegalStateException("setting alert requires existing quota on iface");
1063 }
1064
1065 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001066 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001067 throw new IllegalStateException("iface " + iface + " already has alert");
1068 }
1069
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001070 try {
1071 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001073 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001074 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001075 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001076 }
1077 }
1078 }
1079
1080 @Override
1081 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001082 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001083
1084 // silently discard when control disabled
1085 // TODO: eventually migrate to be always enabled
1086 if (!mBandwidthControlEnabled) return;
1087
1088 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001089 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001090 // TODO: eventually consider throwing
1091 return;
1092 }
1093
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001094 try {
1095 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001096 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001097 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001098 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001099 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001100 }
1101 }
1102 }
1103
1104 @Override
1105 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001106 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001107
1108 // silently discard when control disabled
1109 // TODO: eventually migrate to be always enabled
1110 if (!mBandwidthControlEnabled) return;
1111
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001112 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001113 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001114 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001115 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001116 }
1117 }
1118
1119 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001120 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001121 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001122
Jeff Sharkey350083e2011-06-29 10:45:16 -07001123 // silently discard when control disabled
1124 // TODO: eventually migrate to be always enabled
1125 if (!mBandwidthControlEnabled) return;
1126
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001127 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001128 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1129 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1130 // TODO: eventually consider throwing
1131 return;
1132 }
1133
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001134 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001135 mConnector.execute("bandwidth",
1136 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001137 if (rejectOnQuotaInterfaces) {
1138 mUidRejectOnQuota.put(uid, true);
1139 } else {
1140 mUidRejectOnQuota.delete(uid);
1141 }
1142 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001143 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001144 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001145 }
1146 }
1147
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001148 @Override
1149 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001150 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001151 return mBandwidthControlEnabled;
1152 }
1153
1154 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001155 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001156 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001157 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001158 }
1159
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001160 @Override
1161 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001163
1164 if (ifacePairs.length % 2 != 0) {
1165 throw new IllegalArgumentException(
1166 "unexpected ifacePairs; length=" + ifacePairs.length);
1167 }
1168
1169 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1170 for (int i = 0; i < ifacePairs.length; i += 2) {
1171 final String ifaceIn = ifacePairs[i];
1172 final String ifaceOut = ifacePairs[i + 1];
1173 if (ifaceIn != null && ifaceOut != null) {
1174 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1175 }
1176 }
1177 return stats;
1178 }
1179
1180 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001182 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001183 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001184 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001185 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001186 }
1187
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001188 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001189
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001190 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1191 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1192 tok.nextToken();
1193 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001194
1195 try {
1196 final NetworkStats.Entry entry = new NetworkStats.Entry();
1197 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001198 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001199 entry.set = SET_DEFAULT;
1200 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001201 entry.rxBytes = Long.parseLong(tok.nextToken());
1202 entry.rxPackets = Long.parseLong(tok.nextToken());
1203 entry.txBytes = Long.parseLong(tok.nextToken());
1204 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001205 return entry;
1206 } catch (NumberFormatException e) {
1207 throw new IllegalStateException(
1208 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1209 }
1210 }
1211
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001212 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001213 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001214 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001215 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001216 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001217 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001218 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001219 }
San Mehat91cac642010-03-31 14:31:36 -07001220 }
1221
1222 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001223 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001224 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001225 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1226 } catch (NativeDaemonConnectorException e) {
1227 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001228 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001229
1230 if (rx) {
1231 event.checkCode(InterfaceRxThrottleResult);
1232 } else {
1233 event.checkCode(InterfaceTxThrottleResult);
1234 }
1235
1236 try {
1237 return Integer.parseInt(event.getMessage());
1238 } catch (NumberFormatException e) {
1239 throw new IllegalStateException("unexpected response:" + event);
1240 }
San Mehat91cac642010-03-31 14:31:36 -07001241 }
1242
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001243 @Override
San Mehat91cac642010-03-31 14:31:36 -07001244 public int getInterfaceRxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001245 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001246 return getInterfaceThrottle(iface, true);
1247 }
1248
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001249 @Override
San Mehat91cac642010-03-31 14:31:36 -07001250 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001251 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001252 return getInterfaceThrottle(iface, false);
1253 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001254
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001255 @Override
1256 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001257 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001258 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001259 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001260 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001261 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001262 }
1263 }
1264
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001265 @Override
1266 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001267 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001268
1269 final Command cmd = new Command("resolver", "setifdns", iface);
1270 for (String s : servers) {
1271 InetAddress a = NetworkUtils.numericToInetAddress(s);
1272 if (a.isAnyLocalAddress() == false) {
1273 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001274 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001275 }
1276
1277 try {
1278 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001279 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001280 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001281 }
1282 }
1283
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001284 @Override
1285 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001287 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001288 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001289 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001290 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001291 }
1292 }
1293
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001294 @Override
1295 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001296 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001297 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001298 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001299 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001300 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001301 }
1302 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001303
1304 /** {@inheritDoc} */
1305 public void monitor() {
1306 if (mConnector != null) {
1307 mConnector.monitor();
1308 }
1309 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001310
1311 @Override
1312 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1313 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1314
Robert Greenwalt470fd722012-01-18 12:51:15 -08001315 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1316 mConnector.dump(fd, pw, args);
1317 pw.println();
1318
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001319 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1320
1321 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001322 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1323 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001324 }
1325
1326 synchronized (mUidRejectOnQuota) {
1327 pw.print("UID reject on quota ifaces: [");
1328 final int size = mUidRejectOnQuota.size();
1329 for (int i = 0; i < size; i++) {
1330 pw.print(mUidRejectOnQuota.keyAt(i));
1331 if (i < size - 1) pw.print(",");
1332 }
1333 pw.println("]");
1334 }
1335 }
San Mehat873f2142010-01-14 10:25:07 -08001336}