blob: 67bb68072106328c588125d6fb06e45bb14990a2 [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;
fredc0f420372012-04-12 00:02:00 -070081import android.bluetooth.BluetoothTetheringDataTracker;
San Mehat873f2142010-01-14 10:25:07 -080082
83/**
84 * @hide
85 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070086public class NetworkManagementService extends INetworkManagementService.Stub
87 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070088 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070089 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070090 private static final String NETD_TAG = "NetdConnector";
91
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080092 private static final String ADD = "add";
93 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070094
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070095 private static final String DEFAULT = "default";
96 private static final String SECONDARY = "secondary";
97
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070098 /**
99 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
100 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
101 */
102 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
103
San Mehat873f2142010-01-14 10:25:07 -0800104 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700105 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800106 public static final int InterfaceListResult = 110;
107 public static final int TetherInterfaceListResult = 111;
108 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800109 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800110
111 public static final int TetherStatusResult = 210;
112 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800113 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800114 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700115 public static final int InterfaceRxCounterResult = 216;
116 public static final int InterfaceTxCounterResult = 217;
117 public static final int InterfaceRxThrottleResult = 218;
118 public static final int InterfaceTxThrottleResult = 219;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700119 public static final int QuotaCounterResult = 220;
120 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800121 public static final int DnsProxyQueryResult = 222;
Robert Greenwalte3253922010-02-18 09:23:25 -0800122
123 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700124 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700125 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800126 }
127
128 /**
129 * Binder context for this service
130 */
131 private Context mContext;
132
133 /**
134 * connector object for communicating with netd
135 */
136 private NativeDaemonConnector mConnector;
137
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700138 private final Handler mMainHandler = new Handler();
139
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700140 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700141 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700142
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800143 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
144 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800145
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700146 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
147
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700148 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700149 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700150 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700151 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700152 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700153 /** Set of UIDs with active reject rules. */
154 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
155
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700156 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700157
San Mehat873f2142010-01-14 10:25:07 -0800158 /**
159 * Constructs a new NetworkManagementService instance
160 *
161 * @param context Binder context for this service
162 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700163 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800164 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800165
Marco Nelissen62dbb222010-02-18 10:56:30 -0800166 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
167 return;
168 }
169
San Mehat873f2142010-01-14 10:25:07 -0800170 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700171 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700172 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700173
174 // Add ourself to the Watchdog monitors.
175 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700176 }
177
178 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700179 final NetworkManagementService service = new NetworkManagementService(context);
180 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700181 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
182 service.mThread.start();
183 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700184 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700185 if (DBG) Slog.d(TAG, "Connected");
186 return service;
San Mehat873f2142010-01-14 10:25:07 -0800187 }
188
Jeff Sharkey350083e2011-06-29 10:45:16 -0700189 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700190 prepareNativeDaemon();
191 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700192 }
193
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800194 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800195 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800196 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800197 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800198 }
199
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800200 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800201 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800202 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800203 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800204 }
205
206 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700207 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800208 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700209 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800210 final int length = mObservers.beginBroadcast();
211 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800212 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800213 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
214 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700215 }
216 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800217 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700218 }
219
220 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700221 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700222 * (typically, an Ethernet cable has been plugged-in or unplugged).
223 */
224 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800225 final int length = mObservers.beginBroadcast();
226 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700227 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800228 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
229 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800230 }
231 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800232 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800233 }
234
235 /**
236 * Notify our observers of an interface addition.
237 */
238 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800239 final int length = mObservers.beginBroadcast();
240 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800241 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800242 mObservers.getBroadcastItem(i).interfaceAdded(iface);
243 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800244 }
245 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800246 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800247 }
248
249 /**
250 * Notify our observers of an interface removal.
251 */
252 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700253 // netd already clears out quota and alerts for removed ifaces; update
254 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700255 mActiveAlerts.remove(iface);
256 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700257
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800258 final int length = mObservers.beginBroadcast();
259 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800260 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
262 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800263 }
264 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800265 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800266 }
267
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700268 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700269 * Notify our observers of a limit reached.
270 */
271 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800272 final int length = mObservers.beginBroadcast();
273 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700274 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800275 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
276 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700277 }
278 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800279 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700280 }
281
282 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700283 * Prepare native daemon once connected, enabling modules and pushing any
284 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700285 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700286 private void prepareNativeDaemon() {
287 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700288
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700289 // only enable bandwidth control when support exists
290 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
291 if (hasKernelSupport) {
292 Slog.d(TAG, "enabling bandwidth control");
293 try {
294 mConnector.execute("bandwidth", "enable");
295 mBandwidthControlEnabled = true;
296 } catch (NativeDaemonConnectorException e) {
297 Log.wtf(TAG, "problem enabling bandwidth controls", e);
298 }
299 } else {
300 Slog.d(TAG, "not enabling bandwidth control");
301 }
302
303 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
304
305 // push any existing quota or UID rules
306 synchronized (mQuotaLock) {
307 int size = mActiveQuotas.size();
308 if (size > 0) {
309 Slog.d(TAG, "pushing " + size + " active quota rules");
310 final HashMap<String, Long> activeQuotas = mActiveQuotas;
311 mActiveQuotas = Maps.newHashMap();
312 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
313 setInterfaceQuota(entry.getKey(), entry.getValue());
314 }
315 }
316
317 size = mActiveAlerts.size();
318 if (size > 0) {
319 Slog.d(TAG, "pushing " + size + " active alert rules");
320 final HashMap<String, Long> activeAlerts = mActiveAlerts;
321 mActiveAlerts = Maps.newHashMap();
322 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
323 setInterfaceAlert(entry.getKey(), entry.getValue());
324 }
325 }
326
327 size = mUidRejectOnQuota.size();
328 if (size > 0) {
329 Slog.d(TAG, "pushing " + size + " active uid rules");
330 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
331 mUidRejectOnQuota = new SparseBooleanArray();
332 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
333 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
334 }
335 }
336 }
337 }
San Mehat4d02d002010-01-22 16:07:46 -0800338
San Mehat873f2142010-01-14 10:25:07 -0800339 //
340 // Netd Callback handling
341 //
342
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700343 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
344 @Override
San Mehat873f2142010-01-14 10:25:07 -0800345 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700346 // event is dispatched from internal NDC thread, so we prepare the
347 // daemon back on main thread.
348 if (mConnectedSignal != null) {
349 mConnectedSignal.countDown();
350 mConnectedSignal = null;
351 } else {
352 mMainHandler.post(new Runnable() {
353 @Override
354 public void run() {
355 prepareNativeDaemon();
356 }
357 });
358 }
San Mehat873f2142010-01-14 10:25:07 -0800359 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700360
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700361 @Override
San Mehat873f2142010-01-14 10:25:07 -0800362 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700363 switch (code) {
364 case NetdResponseCode.InterfaceChange:
365 /*
366 * a network interface change occured
367 * Format: "NNN Iface added <name>"
368 * "NNN Iface removed <name>"
369 * "NNN Iface changed <name> <up/down>"
370 * "NNN Iface linkstatus <name> <up/down>"
371 */
372 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
373 throw new IllegalStateException(
374 String.format("Invalid event from daemon (%s)", raw));
375 }
376 if (cooked[2].equals("added")) {
377 notifyInterfaceAdded(cooked[3]);
378 return true;
379 } else if (cooked[2].equals("removed")) {
380 notifyInterfaceRemoved(cooked[3]);
381 return true;
382 } else if (cooked[2].equals("changed") && cooked.length == 5) {
383 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
384 return true;
385 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
386 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
387 return true;
388 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800389 throw new IllegalStateException(
390 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700391 // break;
392 case NetdResponseCode.BandwidthControl:
393 /*
394 * Bandwidth control needs some attention
395 * Format: "NNN limit alert <alertName> <ifaceName>"
396 */
397 if (cooked.length < 5 || !cooked[1].equals("limit")) {
398 throw new IllegalStateException(
399 String.format("Invalid event from daemon (%s)", raw));
400 }
401 if (cooked[2].equals("alert")) {
402 notifyLimitReached(cooked[3], cooked[4]);
403 return true;
404 }
405 throw new IllegalStateException(
406 String.format("Invalid event from daemon (%s)", raw));
407 // break;
408 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800409 }
410 return false;
San Mehat873f2142010-01-14 10:25:07 -0800411 }
412 }
413
San Mehated4fc8a2010-01-22 12:28:36 -0800414
San Mehat873f2142010-01-14 10:25:07 -0800415 //
416 // INetworkManagementService members
417 //
418
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800419 @Override
420 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800421 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700422 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800423 return NativeDaemonEvent.filterMessageList(
424 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700425 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800426 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700427 }
San Mehated4fc8a2010-01-22 12:28:36 -0800428 }
429
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800430 @Override
431 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800432 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800433
434 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700435 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800436 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700437 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800438 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700439 }
San Mehated4fc8a2010-01-22 12:28:36 -0800440
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800441 event.checkCode(InterfaceGetCfgResult);
442
443 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
444 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800445
Kenny Roota80ce062010-06-01 13:23:53 -0700446 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800447 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700448 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800449 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800450 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800451 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700452 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800453 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800454 } catch (IllegalArgumentException iae) {
455 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700456 }
457
458 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800459 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800460 } catch (NumberFormatException nfe) {
461 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700462 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800463
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800464 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
465 while (st.hasMoreTokens()) {
466 cfg.setFlag(st.nextToken());
467 }
Kenny Roota80ce062010-06-01 13:23:53 -0700468 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800469 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800470 }
San Mehated4fc8a2010-01-22 12:28:36 -0800471 return cfg;
472 }
473
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800474 @Override
475 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800476 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800477 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800478 if (linkAddr == null || linkAddr.getAddress() == null) {
479 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800480 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800481
482 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800483 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800484 linkAddr.getNetworkPrefixLength());
485 for (String flag : cfg.getFlags()) {
486 cmd.appendArg(flag);
487 }
488
Kenny Roota80ce062010-06-01 13:23:53 -0700489 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800490 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700491 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800492 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700493 }
San Mehat873f2142010-01-14 10:25:07 -0800494 }
495
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800496 @Override
497 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800498 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800499 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800500 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800501 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700502 }
503
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800504 @Override
505 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800506 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800507 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800508 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800509 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700510 }
511
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800512 @Override
513 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800514 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700515 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800516 mConnector.execute(
517 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700518 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800519 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700520 }
521 }
522
Irfan Sherifff5600612011-06-16 10:26:28 -0700523 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
524 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800525 @Override
526 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800527 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700528 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800529 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700530 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800531 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700532 }
533 }
534
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800535 @Override
536 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800537 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700538 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800539 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700540 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800541 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700542 }
543 }
544
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800545 @Override
546 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800547 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700548 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800549 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700550 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800551 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700552 }
553 }
554
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800555 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700556 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800557 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700558 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700559 }
560
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800561 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700562 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800563 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700564 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700565 }
566
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800567 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700568 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800569 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700570 modifyRoute(interfaceName, ADD, route, SECONDARY);
571 }
572
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800573 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700574 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800575 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700576 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
577 }
578
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800579 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
580 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700581
582 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800583 final LinkAddress la = route.getDestination();
584 cmd.appendArg(la.getAddress().getHostAddress());
585 cmd.appendArg(la.getNetworkPrefixLength());
586
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700587 if (route.getGateway() == null) {
588 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800589 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700590 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800591 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700592 }
593 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800594 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700595 }
596
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800597 try {
598 mConnector.execute(cmd);
599 } catch (NativeDaemonConnectorException e) {
600 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700601 }
602 }
603
604 private ArrayList<String> readRouteList(String filename) {
605 FileInputStream fstream = null;
606 ArrayList<String> list = new ArrayList<String>();
607
608 try {
609 fstream = new FileInputStream(filename);
610 DataInputStream in = new DataInputStream(fstream);
611 BufferedReader br = new BufferedReader(new InputStreamReader(in));
612 String s;
613
614 // throw away the title line
615
616 while (((s = br.readLine()) != null) && (s.length() != 0)) {
617 list.add(s);
618 }
619 } catch (IOException ex) {
620 // return current list, possibly empty
621 } finally {
622 if (fstream != null) {
623 try {
624 fstream.close();
625 } catch (IOException ex) {}
626 }
627 }
628
629 return list;
630 }
631
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800632 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700633 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800634 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700635 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
636
637 // v4 routes listed as:
638 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
639 for (String s : readRouteList("/proc/net/route")) {
640 String[] fields = s.split("\t");
641
642 if (fields.length > 7) {
643 String iface = fields[0];
644
645 if (interfaceName.equals(iface)) {
646 String dest = fields[1];
647 String gate = fields[2];
648 String flags = fields[3]; // future use?
649 String mask = fields[7];
650 try {
651 // address stored as a hex string, ex: 0014A8C0
652 InetAddress destAddr =
653 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
654 int prefixLength =
655 NetworkUtils.netmaskIntToPrefixLength(
656 (int)Long.parseLong(mask, 16));
657 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
658
659 // address stored as a hex string, ex 0014A8C0
660 InetAddress gatewayAddr =
661 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
662
663 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
664 routes.add(route);
665 } catch (Exception e) {
666 Log.e(TAG, "Error parsing route " + s + " : " + e);
667 continue;
668 }
669 }
670 }
671 }
672
673 // v6 routes listed as:
674 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
675 for (String s : readRouteList("/proc/net/ipv6_route")) {
676 String[]fields = s.split("\\s+");
677 if (fields.length > 9) {
678 String iface = fields[9].trim();
679 if (interfaceName.equals(iface)) {
680 String dest = fields[0];
681 String prefix = fields[1];
682 String gate = fields[4];
683
684 try {
685 // prefix length stored as a hex string, ex 40
686 int prefixLength = Integer.parseInt(prefix, 16);
687
688 // address stored as a 32 char hex string
689 // ex fe800000000000000000000000000000
690 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
691 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
692
693 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
694
695 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
696 routes.add(route);
697 } catch (Exception e) {
698 Log.e(TAG, "Error parsing route " + s + " : " + e);
699 continue;
700 }
701 }
702 }
703 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800704 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700705 }
706
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800707 @Override
San Mehat873f2142010-01-14 10:25:07 -0800708 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800709 // TODO: remove from aidl if nobody calls externally
710 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800711
Joe Onorato8a9b2202010-02-26 18:56:32 -0800712 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800713 }
714
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800715 @Override
San Mehat873f2142010-01-14 10:25:07 -0800716 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800718
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800719 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700720 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800721 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700722 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800723 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700724 }
San Mehat873f2142010-01-14 10:25:07 -0800725
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800726 // 211 Forwarding enabled
727 event.checkCode(IpFwdStatusResult);
728 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800729 }
730
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800731 @Override
732 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800733 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800734 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800735 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800736 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800737 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800738 }
San Mehat873f2142010-01-14 10:25:07 -0800739 }
740
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800741 @Override
742 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800743 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700744 // cmd is "tether start first_start first_stop second_start second_stop ..."
745 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800746
747 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700748 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800749 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700750 }
Kenny Roota80ce062010-06-01 13:23:53 -0700751
752 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800753 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700754 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800755 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700756 }
San Mehat873f2142010-01-14 10:25:07 -0800757 }
758
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800759 @Override
760 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800761 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700762 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800763 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700764 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800765 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700766 }
San Mehat873f2142010-01-14 10:25:07 -0800767 }
768
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800769 @Override
770 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800771 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800772
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800773 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700774 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800775 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700776 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800777 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700778 }
San Mehat873f2142010-01-14 10:25:07 -0800779
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800780 // 210 Tethering services started
781 event.checkCode(TetherStatusResult);
782 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800783 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700784
785 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700786 public void startReverseTethering(String iface)
787 throws IllegalStateException {
788 if (DBG) Slog.d(TAG, "startReverseTethering in");
Matthew Xiefe19f122012-07-12 16:03:32 -0700789 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700790 // cmd is "tether start first_start first_stop second_start second_stop ..."
791 // an odd number of addrs will fail
792 String cmd = "tether start-reverse";
793 cmd += " " + iface;
794 if (DBG) Slog.d(TAG, "startReverseTethering cmd: " + cmd);
795 try {
796 mConnector.doCommand(cmd);
797 } catch (NativeDaemonConnectorException e) {
798 throw new IllegalStateException("Unable to communicate to native daemon");
799 }
800 BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
801
802 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700803
804 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700805 public void stopReverseTethering() throws IllegalStateException {
Matthew Xiefe19f122012-07-12 16:03:32 -0700806 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700807 try {
808 mConnector.doCommand("tether stop-reverse");
809 } catch (NativeDaemonConnectorException e) {
810 throw new IllegalStateException("Unable to communicate to native daemon to stop tether");
811 }
812 BluetoothTetheringDataTracker.getInstance().stopReverseTether();
813 }
San Mehat873f2142010-01-14 10:25:07 -0800814
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800815 @Override
816 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800817 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700818 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800819 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700820 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800821 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700822 }
San Mehat873f2142010-01-14 10:25:07 -0800823 }
824
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800825 @Override
San Mehat873f2142010-01-14 10:25:07 -0800826 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800827 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700828 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800829 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700830 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800831 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700832 }
San Mehat873f2142010-01-14 10:25:07 -0800833 }
834
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800835 @Override
836 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800837 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700838 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800839 return NativeDaemonEvent.filterMessageList(
840 mConnector.executeForList("tether", "interface", "list"),
841 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700842 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800843 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700844 }
San Mehat873f2142010-01-14 10:25:07 -0800845 }
846
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800847 @Override
848 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800849 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800850
851 final Command cmd = new Command("tether", "dns", "set");
852 for (String s : dns) {
853 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
854 }
855
San Mehat873f2142010-01-14 10:25:07 -0800856 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800857 mConnector.execute(cmd);
858 } catch (NativeDaemonConnectorException e) {
859 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800860 }
861 }
862
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800863 @Override
864 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800865 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700866 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800867 return NativeDaemonEvent.filterMessageList(
868 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700869 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800870 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700871 }
San Mehat873f2142010-01-14 10:25:07 -0800872 }
873
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800874 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700875 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800876 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700877
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800878 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
879 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800880 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800881 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800882 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800883 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
884 .getInterfaceAddresses();
885 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800886 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800887 InetAddress addr = NetworkUtils.getNetworkPart(
888 ia.getAddress(), ia.getNetworkPrefixLength());
889 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800890 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700891 }
892
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800893 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800894 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800895 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800896 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800897 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700898 }
899
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800900 @Override
901 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800902 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700903 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700904 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800905 } catch (SocketException e) {
906 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700907 }
San Mehat873f2142010-01-14 10:25:07 -0800908 }
909
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800910 @Override
911 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800912 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700913 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700914 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800915 } catch (SocketException e) {
916 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700917 }
San Mehat873f2142010-01-14 10:25:07 -0800918 }
San Mehat72759df2010-01-19 13:50:37 -0800919
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800920 @Override
921 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800922 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700923 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800924 return NativeDaemonEvent.filterMessageList(
925 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700926 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800927 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700928 }
San Mehat72759df2010-01-19 13:50:37 -0800929 }
930
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800931 @Override
932 public void attachPppd(
933 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800934 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800935 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800937 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
938 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
939 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800940 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700941 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800942 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800943 }
944 }
945
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800946 @Override
947 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800948 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700949 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800950 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700951 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800952 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700953 }
San Mehat72759df2010-01-19 13:50:37 -0800954 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800955
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800956 @Override
957 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -0700958 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800959 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700960 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700961 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -0700962 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -0700963 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700964 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -0700965 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800966 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -0700967 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800968 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -0700969 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800970 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800971 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800972 }
973
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800974 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -0800975 switch (wifiConfig.getAuthType()) {
976 case KeyMgmt.WPA_PSK:
977 return "wpa-psk";
978 case KeyMgmt.WPA2_PSK:
979 return "wpa2-psk";
980 default:
981 return "open";
982 }
983 }
984
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700985 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800986 @Override
987 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800988 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700989 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800990 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700991 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800992 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700993 }
994 }
995
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800996 @Override
997 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800998 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700999 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001000 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001001 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001002 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001003 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001004 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001005 }
1006
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001007 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001008 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001009 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001010 try {
1011 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001012 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001013 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001014 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001015 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001016 }
1017 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001018 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001019 }
1020 }
San Mehat91cac642010-03-31 14:31:36 -07001021
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001022 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001023 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001024 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001025 return mStatsFactory.readNetworkStatsSummaryDev();
1026 }
1027
1028 @Override
1029 public NetworkStats getNetworkStatsSummaryXt() {
1030 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1031 return mStatsFactory.readNetworkStatsSummaryXt();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001032 }
1033
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001034 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001035 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001036 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001037 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -07001038 }
1039
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001040 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001041 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001042 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001043
Jeff Sharkey350083e2011-06-29 10:45:16 -07001044 // silently discard when control disabled
1045 // TODO: eventually migrate to be always enabled
1046 if (!mBandwidthControlEnabled) return;
1047
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001048 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001049 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001050 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001051 }
1052
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001053 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001054 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001055 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001056 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001057 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001058 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001059 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001060 }
1061 }
1062
1063 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001064 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001065 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001066
Jeff Sharkey350083e2011-06-29 10:45:16 -07001067 // 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 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001072 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001073 // TODO: eventually consider throwing
1074 return;
1075 }
1076
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001077 mActiveQuotas.remove(iface);
1078 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001079
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001080 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001081 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001082 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001083 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001084 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001085 }
1086 }
1087 }
1088
1089 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001090 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001091 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001092
1093 // silently discard when control disabled
1094 // TODO: eventually migrate to be always enabled
1095 if (!mBandwidthControlEnabled) return;
1096
1097 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001098 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001099 throw new IllegalStateException("setting alert requires existing quota on iface");
1100 }
1101
1102 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001103 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001104 throw new IllegalStateException("iface " + iface + " already has alert");
1105 }
1106
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001107 try {
1108 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001109 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001110 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001111 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001112 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001113 }
1114 }
1115 }
1116
1117 @Override
1118 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001119 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001120
1121 // silently discard when control disabled
1122 // TODO: eventually migrate to be always enabled
1123 if (!mBandwidthControlEnabled) return;
1124
1125 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001126 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001127 // TODO: eventually consider throwing
1128 return;
1129 }
1130
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001131 try {
1132 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001133 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001134 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001135 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001136 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001137 }
1138 }
1139 }
1140
1141 @Override
1142 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001143 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001144
1145 // silently discard when control disabled
1146 // TODO: eventually migrate to be always enabled
1147 if (!mBandwidthControlEnabled) return;
1148
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001149 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001150 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001151 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001152 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001153 }
1154 }
1155
1156 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001157 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001158 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001159
Jeff Sharkey350083e2011-06-29 10:45:16 -07001160 // silently discard when control disabled
1161 // TODO: eventually migrate to be always enabled
1162 if (!mBandwidthControlEnabled) return;
1163
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001164 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001165 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1166 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1167 // TODO: eventually consider throwing
1168 return;
1169 }
1170
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001171 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001172 mConnector.execute("bandwidth",
1173 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001174 if (rejectOnQuotaInterfaces) {
1175 mUidRejectOnQuota.put(uid, true);
1176 } else {
1177 mUidRejectOnQuota.delete(uid);
1178 }
1179 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001180 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001181 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001182 }
1183 }
1184
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001185 @Override
1186 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001187 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001188 return mBandwidthControlEnabled;
1189 }
1190
1191 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001192 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001193 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001194 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001195 }
1196
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001197 @Override
1198 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001199 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001200
1201 if (ifacePairs.length % 2 != 0) {
1202 throw new IllegalArgumentException(
1203 "unexpected ifacePairs; length=" + ifacePairs.length);
1204 }
1205
1206 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1207 for (int i = 0; i < ifacePairs.length; i += 2) {
1208 final String ifaceIn = ifacePairs[i];
1209 final String ifaceOut = ifacePairs[i + 1];
1210 if (ifaceIn != null && ifaceOut != null) {
1211 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1212 }
1213 }
1214 return stats;
1215 }
1216
1217 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001218 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001219 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001220 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001221 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001222 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001223 }
1224
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001225 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001226
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001227 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1228 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1229 tok.nextToken();
1230 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001231
1232 try {
1233 final NetworkStats.Entry entry = new NetworkStats.Entry();
1234 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001235 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001236 entry.set = SET_DEFAULT;
1237 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001238 entry.rxBytes = Long.parseLong(tok.nextToken());
1239 entry.rxPackets = Long.parseLong(tok.nextToken());
1240 entry.txBytes = Long.parseLong(tok.nextToken());
1241 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001242 return entry;
1243 } catch (NumberFormatException e) {
1244 throw new IllegalStateException(
1245 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1246 }
1247 }
1248
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001249 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001250 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001251 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001252 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001254 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001255 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001256 }
San Mehat91cac642010-03-31 14:31:36 -07001257 }
1258
1259 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001260 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001261 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001262 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1263 } catch (NativeDaemonConnectorException e) {
1264 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001265 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001266
1267 if (rx) {
1268 event.checkCode(InterfaceRxThrottleResult);
1269 } else {
1270 event.checkCode(InterfaceTxThrottleResult);
1271 }
1272
1273 try {
1274 return Integer.parseInt(event.getMessage());
1275 } catch (NumberFormatException e) {
1276 throw new IllegalStateException("unexpected response:" + event);
1277 }
San Mehat91cac642010-03-31 14:31:36 -07001278 }
1279
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001280 @Override
San Mehat91cac642010-03-31 14:31:36 -07001281 public int getInterfaceRxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001282 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001283 return getInterfaceThrottle(iface, true);
1284 }
1285
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001286 @Override
San Mehat91cac642010-03-31 14:31:36 -07001287 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001288 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001289 return getInterfaceThrottle(iface, false);
1290 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001291
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001292 @Override
1293 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001294 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001295 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001296 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001297 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001298 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001299 }
1300 }
1301
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001302 @Override
1303 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001304 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001305
1306 final Command cmd = new Command("resolver", "setifdns", iface);
1307 for (String s : servers) {
1308 InetAddress a = NetworkUtils.numericToInetAddress(s);
1309 if (a.isAnyLocalAddress() == false) {
1310 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001311 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001312 }
1313
1314 try {
1315 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001316 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001317 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001318 }
1319 }
1320
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001321 @Override
1322 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001323 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001324 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001325 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001326 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001327 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001328 }
1329 }
1330
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001331 @Override
1332 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001333 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001334 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001335 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001336 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001337 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001338 }
1339 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001340
1341 /** {@inheritDoc} */
1342 public void monitor() {
1343 if (mConnector != null) {
1344 mConnector.monitor();
1345 }
1346 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001347
1348 @Override
1349 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1350 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1351
Robert Greenwalt470fd722012-01-18 12:51:15 -08001352 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1353 mConnector.dump(fd, pw, args);
1354 pw.println();
1355
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001356 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1357
1358 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001359 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1360 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001361 }
1362
1363 synchronized (mUidRejectOnQuota) {
1364 pw.print("UID reject on quota ifaces: [");
1365 final int size = mUidRejectOnQuota.size();
1366 for (int i = 0; i < size; i++) {
1367 pw.print(mUidRejectOnQuota.keyAt(i));
1368 if (i < size - 1) pw.print(",");
1369 }
1370 pw.println("]");
1371 }
1372 }
San Mehat873f2142010-01-14 10:25:07 -08001373}