blob: 39e518671d9fd0adda10e26c23828b7e050c14dc [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
Haoyu Bai04124232012-06-28 15:26:19 -0700156 private Object mIdleTimerLock = new Object();
157 /** Set of interfaces with active idle timers. */
158 private static class IdleTimerParams {
159 public final int timeout;
160 public final String label;
161 public int networkCount;
162
163 IdleTimerParams(int timeout, String label) {
164 this.timeout = timeout;
165 this.label = label;
166 this.networkCount = 1;
167 }
168 }
169 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
170
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700171 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700172
San Mehat873f2142010-01-14 10:25:07 -0800173 /**
174 * Constructs a new NetworkManagementService instance
175 *
176 * @param context Binder context for this service
177 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700178 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800179 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800180
Marco Nelissen62dbb222010-02-18 10:56:30 -0800181 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
182 return;
183 }
184
San Mehat873f2142010-01-14 10:25:07 -0800185 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700186 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700187 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700188
189 // Add ourself to the Watchdog monitors.
190 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700191 }
192
193 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700194 final NetworkManagementService service = new NetworkManagementService(context);
195 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700196 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
197 service.mThread.start();
198 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700199 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700200 if (DBG) Slog.d(TAG, "Connected");
201 return service;
San Mehat873f2142010-01-14 10:25:07 -0800202 }
203
Jeff Sharkey350083e2011-06-29 10:45:16 -0700204 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700205 prepareNativeDaemon();
206 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700207 }
208
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800209 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800210 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800211 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800212 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800213 }
214
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800215 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800216 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800217 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800218 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800219 }
220
221 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700222 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800223 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700224 private void notifyInterfaceStatusChanged(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++) {
San Mehat4d02d002010-01-22 16:07:46 -0800227 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800228 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
229 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700230 }
231 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800232 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700233 }
234
235 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700236 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700237 * (typically, an Ethernet cable has been plugged-in or unplugged).
238 */
239 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800240 final int length = mObservers.beginBroadcast();
241 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800243 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
244 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800245 }
246 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800247 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800248 }
249
250 /**
251 * Notify our observers of an interface addition.
252 */
253 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800254 final int length = mObservers.beginBroadcast();
255 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800256 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800257 mObservers.getBroadcastItem(i).interfaceAdded(iface);
258 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800259 }
260 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800262 }
263
264 /**
265 * Notify our observers of an interface removal.
266 */
267 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700268 // netd already clears out quota and alerts for removed ifaces; update
269 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700270 mActiveAlerts.remove(iface);
271 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700272
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800273 final int length = mObservers.beginBroadcast();
274 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800275 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800276 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
277 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800278 }
279 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800280 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800281 }
282
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700283 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700284 * Notify our observers of a limit reached.
285 */
286 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800287 final int length = mObservers.beginBroadcast();
288 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700289 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
291 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700292 }
293 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800294 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700295 }
296
297 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700298 * Notify our observers of a change in the data activity state of the interface
299 */
300 private void notifyInterfaceClassActivity(String label, boolean active) {
301 final int length = mObservers.beginBroadcast();
302 for (int i = 0; i < length; i++) {
303 try {
304 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
305 } catch (RemoteException e) {
306 }
307 }
308 mObservers.finishBroadcast();
309 }
310
311 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700312 * Prepare native daemon once connected, enabling modules and pushing any
313 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700314 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700315 private void prepareNativeDaemon() {
316 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700317
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700318 // only enable bandwidth control when support exists
319 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
320 if (hasKernelSupport) {
321 Slog.d(TAG, "enabling bandwidth control");
322 try {
323 mConnector.execute("bandwidth", "enable");
324 mBandwidthControlEnabled = true;
325 } catch (NativeDaemonConnectorException e) {
326 Log.wtf(TAG, "problem enabling bandwidth controls", e);
327 }
328 } else {
329 Slog.d(TAG, "not enabling bandwidth control");
330 }
331
332 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
333
334 // push any existing quota or UID rules
335 synchronized (mQuotaLock) {
336 int size = mActiveQuotas.size();
337 if (size > 0) {
338 Slog.d(TAG, "pushing " + size + " active quota rules");
339 final HashMap<String, Long> activeQuotas = mActiveQuotas;
340 mActiveQuotas = Maps.newHashMap();
341 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
342 setInterfaceQuota(entry.getKey(), entry.getValue());
343 }
344 }
345
346 size = mActiveAlerts.size();
347 if (size > 0) {
348 Slog.d(TAG, "pushing " + size + " active alert rules");
349 final HashMap<String, Long> activeAlerts = mActiveAlerts;
350 mActiveAlerts = Maps.newHashMap();
351 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
352 setInterfaceAlert(entry.getKey(), entry.getValue());
353 }
354 }
355
356 size = mUidRejectOnQuota.size();
357 if (size > 0) {
358 Slog.d(TAG, "pushing " + size + " active uid rules");
359 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
360 mUidRejectOnQuota = new SparseBooleanArray();
361 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
362 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
363 }
364 }
365 }
366 }
San Mehat4d02d002010-01-22 16:07:46 -0800367
San Mehat873f2142010-01-14 10:25:07 -0800368 //
369 // Netd Callback handling
370 //
371
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700372 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
373 @Override
San Mehat873f2142010-01-14 10:25:07 -0800374 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700375 // event is dispatched from internal NDC thread, so we prepare the
376 // daemon back on main thread.
377 if (mConnectedSignal != null) {
378 mConnectedSignal.countDown();
379 mConnectedSignal = null;
380 } else {
381 mMainHandler.post(new Runnable() {
382 @Override
383 public void run() {
384 prepareNativeDaemon();
385 }
386 });
387 }
San Mehat873f2142010-01-14 10:25:07 -0800388 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700389
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700390 @Override
San Mehat873f2142010-01-14 10:25:07 -0800391 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700392 switch (code) {
393 case NetdResponseCode.InterfaceChange:
394 /*
395 * a network interface change occured
396 * Format: "NNN Iface added <name>"
397 * "NNN Iface removed <name>"
398 * "NNN Iface changed <name> <up/down>"
399 * "NNN Iface linkstatus <name> <up/down>"
400 */
401 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
402 throw new IllegalStateException(
403 String.format("Invalid event from daemon (%s)", raw));
404 }
405 if (cooked[2].equals("added")) {
406 notifyInterfaceAdded(cooked[3]);
407 return true;
408 } else if (cooked[2].equals("removed")) {
409 notifyInterfaceRemoved(cooked[3]);
410 return true;
411 } else if (cooked[2].equals("changed") && cooked.length == 5) {
412 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
413 return true;
414 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
415 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
416 return true;
417 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800418 throw new IllegalStateException(
419 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700420 // break;
421 case NetdResponseCode.BandwidthControl:
422 /*
423 * Bandwidth control needs some attention
424 * Format: "NNN limit alert <alertName> <ifaceName>"
425 */
426 if (cooked.length < 5 || !cooked[1].equals("limit")) {
427 throw new IllegalStateException(
428 String.format("Invalid event from daemon (%s)", raw));
429 }
430 if (cooked[2].equals("alert")) {
431 notifyLimitReached(cooked[3], cooked[4]);
432 return true;
433 }
434 throw new IllegalStateException(
435 String.format("Invalid event from daemon (%s)", raw));
436 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700437 case NetdResponseCode.InterfaceClassActivity:
438 /*
439 * An network interface class state changed (active/idle)
440 * Format: "NNN IfaceClass <active/idle> <label>"
441 */
442 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
443 throw new IllegalStateException(
444 String.format("Invalid event from daemon (%s)", raw));
445 }
446 boolean isActive = cooked[2].equals("active");
447 notifyInterfaceClassActivity(cooked[3], isActive);
448 return true;
449 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700450 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800451 }
452 return false;
San Mehat873f2142010-01-14 10:25:07 -0800453 }
454 }
455
San Mehated4fc8a2010-01-22 12:28:36 -0800456
San Mehat873f2142010-01-14 10:25:07 -0800457 //
458 // INetworkManagementService members
459 //
460
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800461 @Override
462 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800463 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700464 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800465 return NativeDaemonEvent.filterMessageList(
466 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700467 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800468 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700469 }
San Mehated4fc8a2010-01-22 12:28:36 -0800470 }
471
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800472 @Override
473 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800475
476 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700477 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800478 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700479 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800480 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700481 }
San Mehated4fc8a2010-01-22 12:28:36 -0800482
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800483 event.checkCode(InterfaceGetCfgResult);
484
485 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
486 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800487
Kenny Roota80ce062010-06-01 13:23:53 -0700488 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800489 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700490 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800491 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800492 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800493 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700494 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800495 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800496 } catch (IllegalArgumentException iae) {
497 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700498 }
499
500 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800501 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800502 } catch (NumberFormatException nfe) {
503 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700504 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800505
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800506 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
507 while (st.hasMoreTokens()) {
508 cfg.setFlag(st.nextToken());
509 }
Kenny Roota80ce062010-06-01 13:23:53 -0700510 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800511 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800512 }
San Mehated4fc8a2010-01-22 12:28:36 -0800513 return cfg;
514 }
515
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800516 @Override
517 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800518 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800519 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800520 if (linkAddr == null || linkAddr.getAddress() == null) {
521 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800522 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800523
524 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800525 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800526 linkAddr.getNetworkPrefixLength());
527 for (String flag : cfg.getFlags()) {
528 cmd.appendArg(flag);
529 }
530
Kenny Roota80ce062010-06-01 13:23:53 -0700531 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800532 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700533 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800534 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700535 }
San Mehat873f2142010-01-14 10:25:07 -0800536 }
537
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800538 @Override
539 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800540 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800541 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800542 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800543 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700544 }
545
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800546 @Override
547 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800548 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800549 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800550 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800551 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700552 }
553
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800554 @Override
555 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800556 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700557 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800558 mConnector.execute(
559 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700560 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800561 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700562 }
563 }
564
Irfan Sherifff5600612011-06-16 10:26:28 -0700565 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
566 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800567 @Override
568 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800569 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700570 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800571 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700572 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800573 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700574 }
575 }
576
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800577 @Override
578 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800579 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700580 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800581 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700582 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800583 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700584 }
585 }
586
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800587 @Override
588 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800589 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700590 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800591 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700592 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800593 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700594 }
595 }
596
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800597 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700598 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800599 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700600 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700601 }
602
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800603 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700604 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800605 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700606 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700607 }
608
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800609 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700610 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800611 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700612 modifyRoute(interfaceName, ADD, route, SECONDARY);
613 }
614
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800615 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700616 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800617 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700618 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
619 }
620
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800621 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
622 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700623
624 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800625 final LinkAddress la = route.getDestination();
626 cmd.appendArg(la.getAddress().getHostAddress());
627 cmd.appendArg(la.getNetworkPrefixLength());
628
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700629 if (route.getGateway() == null) {
630 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800631 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700632 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800633 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700634 }
635 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800636 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700637 }
638
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800639 try {
640 mConnector.execute(cmd);
641 } catch (NativeDaemonConnectorException e) {
642 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700643 }
644 }
645
646 private ArrayList<String> readRouteList(String filename) {
647 FileInputStream fstream = null;
648 ArrayList<String> list = new ArrayList<String>();
649
650 try {
651 fstream = new FileInputStream(filename);
652 DataInputStream in = new DataInputStream(fstream);
653 BufferedReader br = new BufferedReader(new InputStreamReader(in));
654 String s;
655
656 // throw away the title line
657
658 while (((s = br.readLine()) != null) && (s.length() != 0)) {
659 list.add(s);
660 }
661 } catch (IOException ex) {
662 // return current list, possibly empty
663 } finally {
664 if (fstream != null) {
665 try {
666 fstream.close();
667 } catch (IOException ex) {}
668 }
669 }
670
671 return list;
672 }
673
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800674 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700675 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800676 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700677 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
678
679 // v4 routes listed as:
680 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
681 for (String s : readRouteList("/proc/net/route")) {
682 String[] fields = s.split("\t");
683
684 if (fields.length > 7) {
685 String iface = fields[0];
686
687 if (interfaceName.equals(iface)) {
688 String dest = fields[1];
689 String gate = fields[2];
690 String flags = fields[3]; // future use?
691 String mask = fields[7];
692 try {
693 // address stored as a hex string, ex: 0014A8C0
694 InetAddress destAddr =
695 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
696 int prefixLength =
697 NetworkUtils.netmaskIntToPrefixLength(
698 (int)Long.parseLong(mask, 16));
699 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
700
701 // address stored as a hex string, ex 0014A8C0
702 InetAddress gatewayAddr =
703 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
704
705 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
706 routes.add(route);
707 } catch (Exception e) {
708 Log.e(TAG, "Error parsing route " + s + " : " + e);
709 continue;
710 }
711 }
712 }
713 }
714
715 // v6 routes listed as:
716 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
717 for (String s : readRouteList("/proc/net/ipv6_route")) {
718 String[]fields = s.split("\\s+");
719 if (fields.length > 9) {
720 String iface = fields[9].trim();
721 if (interfaceName.equals(iface)) {
722 String dest = fields[0];
723 String prefix = fields[1];
724 String gate = fields[4];
725
726 try {
727 // prefix length stored as a hex string, ex 40
728 int prefixLength = Integer.parseInt(prefix, 16);
729
730 // address stored as a 32 char hex string
731 // ex fe800000000000000000000000000000
732 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
733 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
734
735 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
736
737 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
738 routes.add(route);
739 } catch (Exception e) {
740 Log.e(TAG, "Error parsing route " + s + " : " + e);
741 continue;
742 }
743 }
744 }
745 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800746 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700747 }
748
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800749 @Override
San Mehat873f2142010-01-14 10:25:07 -0800750 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800751 // TODO: remove from aidl if nobody calls externally
752 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800753
Joe Onorato8a9b2202010-02-26 18:56:32 -0800754 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800755 }
756
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800757 @Override
San Mehat873f2142010-01-14 10:25:07 -0800758 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800759 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800760
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800761 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700762 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800763 event = mConnector.execute("ipfwd", "status");
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
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768 // 211 Forwarding enabled
769 event.checkCode(IpFwdStatusResult);
770 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800771 }
772
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800773 @Override
774 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800775 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800776 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800777 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800778 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800779 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800780 }
San Mehat873f2142010-01-14 10:25:07 -0800781 }
782
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800783 @Override
784 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800785 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700786 // cmd is "tether start first_start first_stop second_start second_stop ..."
787 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800788
789 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700790 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800791 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700792 }
Kenny Roota80ce062010-06-01 13:23:53 -0700793
794 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800795 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700796 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800797 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700798 }
San Mehat873f2142010-01-14 10:25:07 -0800799 }
800
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800801 @Override
802 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800803 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700804 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700806 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800807 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700808 }
San Mehat873f2142010-01-14 10:25:07 -0800809 }
810
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800811 @Override
812 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800813 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800814
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800815 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700816 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800817 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700818 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800819 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700820 }
San Mehat873f2142010-01-14 10:25:07 -0800821
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800822 // 210 Tethering services started
823 event.checkCode(TetherStatusResult);
824 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800825 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700826
827 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700828 public void startReverseTethering(String iface)
829 throws IllegalStateException {
830 if (DBG) Slog.d(TAG, "startReverseTethering in");
Matthew Xiefe19f122012-07-12 16:03:32 -0700831 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700832 // cmd is "tether start first_start first_stop second_start second_stop ..."
833 // an odd number of addrs will fail
834 String cmd = "tether start-reverse";
835 cmd += " " + iface;
836 if (DBG) Slog.d(TAG, "startReverseTethering cmd: " + cmd);
837 try {
838 mConnector.doCommand(cmd);
839 } catch (NativeDaemonConnectorException e) {
840 throw new IllegalStateException("Unable to communicate to native daemon");
841 }
842 BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
843
844 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700845
846 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700847 public void stopReverseTethering() throws IllegalStateException {
Matthew Xiefe19f122012-07-12 16:03:32 -0700848 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700849 try {
850 mConnector.doCommand("tether stop-reverse");
851 } catch (NativeDaemonConnectorException e) {
852 throw new IllegalStateException("Unable to communicate to native daemon to stop tether");
853 }
854 BluetoothTetheringDataTracker.getInstance().stopReverseTether();
855 }
San Mehat873f2142010-01-14 10:25:07 -0800856
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800857 @Override
858 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800859 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700860 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800861 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700862 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800863 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700864 }
San Mehat873f2142010-01-14 10:25:07 -0800865 }
866
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800867 @Override
San Mehat873f2142010-01-14 10:25:07 -0800868 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800869 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700870 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800871 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700872 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800873 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700874 }
San Mehat873f2142010-01-14 10:25:07 -0800875 }
876
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800877 @Override
878 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800879 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700880 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800881 return NativeDaemonEvent.filterMessageList(
882 mConnector.executeForList("tether", "interface", "list"),
883 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700884 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800885 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700886 }
San Mehat873f2142010-01-14 10:25:07 -0800887 }
888
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800889 @Override
890 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800891 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800892
893 final Command cmd = new Command("tether", "dns", "set");
894 for (String s : dns) {
895 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
896 }
897
San Mehat873f2142010-01-14 10:25:07 -0800898 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800899 mConnector.execute(cmd);
900 } catch (NativeDaemonConnectorException e) {
901 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800902 }
903 }
904
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800905 @Override
906 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800907 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700908 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800909 return NativeDaemonEvent.filterMessageList(
910 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700911 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800912 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700913 }
San Mehat873f2142010-01-14 10:25:07 -0800914 }
915
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800916 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700917 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800918 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700919
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800920 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
921 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800922 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800923 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800924 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800925 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
926 .getInterfaceAddresses();
927 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800928 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800929 InetAddress addr = NetworkUtils.getNetworkPart(
930 ia.getAddress(), ia.getNetworkPrefixLength());
931 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800932 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700933 }
934
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800935 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800937 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800938 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800939 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700940 }
941
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800942 @Override
943 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800944 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700945 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700946 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800947 } catch (SocketException e) {
948 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700949 }
San Mehat873f2142010-01-14 10:25:07 -0800950 }
951
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800952 @Override
953 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800954 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700955 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700956 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800957 } catch (SocketException e) {
958 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700959 }
San Mehat873f2142010-01-14 10:25:07 -0800960 }
San Mehat72759df2010-01-19 13:50:37 -0800961
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800962 @Override
963 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800964 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700965 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800966 return NativeDaemonEvent.filterMessageList(
967 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700968 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800969 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700970 }
San Mehat72759df2010-01-19 13:50:37 -0800971 }
972
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800973 @Override
974 public void attachPppd(
975 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800976 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800977 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800978 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800979 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
980 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
981 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800982 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700983 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800984 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800985 }
986 }
987
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800988 @Override
989 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800990 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700991 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800992 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700993 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800994 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700995 }
San Mehat72759df2010-01-19 13:50:37 -0800996 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800997
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800998 @Override
999 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001000 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001001 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001002 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001003 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001004 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001005 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001006 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001007 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001008 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001009 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001010 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001011 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001012 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001013 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001014 }
1015
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001016 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001017 switch (wifiConfig.getAuthType()) {
1018 case KeyMgmt.WPA_PSK:
1019 return "wpa-psk";
1020 case KeyMgmt.WPA2_PSK:
1021 return "wpa2-psk";
1022 default:
1023 return "open";
1024 }
1025 }
1026
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001027 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001028 @Override
1029 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001030 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001031 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001032 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001033 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001034 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001035 }
1036 }
1037
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001038 @Override
1039 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001040 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001041 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001042 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001043 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001044 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001045 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001046 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001047 }
1048
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001049 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001050 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001052 try {
1053 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001054 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001055 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001056 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001057 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001058 }
1059 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001060 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001061 }
1062 }
San Mehat91cac642010-03-31 14:31:36 -07001063
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001064 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001065 public void addIdleTimer(String iface, int timeout, String label) {
1066 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1067
1068 if (DBG) Slog.d(TAG, "Adding idletimer");
1069
1070 synchronized (mIdleTimerLock) {
1071 IdleTimerParams params = mActiveIdleTimers.get(iface);
1072 if (params != null) {
1073 // the interface already has idletimer, update network count
1074 params.networkCount++;
1075 return;
1076 }
1077
1078 try {
1079 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1080 } catch (NativeDaemonConnectorException e) {
1081 throw e.rethrowAsParcelableException();
1082 }
1083 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1084 }
1085 }
1086
1087 @Override
1088 public void removeIdleTimer(String iface) {
1089 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1090
1091 if (DBG) Slog.d(TAG, "Removing idletimer");
1092
1093 synchronized (mIdleTimerLock) {
1094 IdleTimerParams params = mActiveIdleTimers.get(iface);
1095 if (params == null || --(params.networkCount) > 0) {
1096 return;
1097 }
1098
1099 try {
1100 mConnector.execute("idletimer", "remove", iface,
1101 Integer.toString(params.timeout), params.label);
1102 } catch (NativeDaemonConnectorException e) {
1103 throw e.rethrowAsParcelableException();
1104 }
1105 mActiveIdleTimers.remove(iface);
1106 }
1107 }
1108
1109 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001110 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001111 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001112 return mStatsFactory.readNetworkStatsSummaryDev();
1113 }
1114
1115 @Override
1116 public NetworkStats getNetworkStatsSummaryXt() {
1117 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1118 return mStatsFactory.readNetworkStatsSummaryXt();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001119 }
1120
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001121 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001122 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001123 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001124 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -07001125 }
1126
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001127 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001128 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001129 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001130
Jeff Sharkey350083e2011-06-29 10:45:16 -07001131 // silently discard when control disabled
1132 // TODO: eventually migrate to be always enabled
1133 if (!mBandwidthControlEnabled) return;
1134
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001135 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001136 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001137 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001138 }
1139
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001140 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001141 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001142 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001143 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001144 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001145 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001146 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001147 }
1148 }
1149
1150 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001151 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001152 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001153
Jeff Sharkey350083e2011-06-29 10:45:16 -07001154 // silently discard when control disabled
1155 // TODO: eventually migrate to be always enabled
1156 if (!mBandwidthControlEnabled) return;
1157
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001158 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001159 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001160 // TODO: eventually consider throwing
1161 return;
1162 }
1163
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001164 mActiveQuotas.remove(iface);
1165 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001166
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001167 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001168 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001169 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001170 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001171 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001172 }
1173 }
1174 }
1175
1176 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001177 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001178 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001179
1180 // silently discard when control disabled
1181 // TODO: eventually migrate to be always enabled
1182 if (!mBandwidthControlEnabled) return;
1183
1184 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001185 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001186 throw new IllegalStateException("setting alert requires existing quota on iface");
1187 }
1188
1189 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001190 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001191 throw new IllegalStateException("iface " + iface + " already has alert");
1192 }
1193
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001194 try {
1195 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001196 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001197 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001198 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001199 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001200 }
1201 }
1202 }
1203
1204 @Override
1205 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001206 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001207
1208 // silently discard when control disabled
1209 // TODO: eventually migrate to be always enabled
1210 if (!mBandwidthControlEnabled) return;
1211
1212 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001213 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001214 // TODO: eventually consider throwing
1215 return;
1216 }
1217
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001218 try {
1219 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001220 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001221 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001222 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001223 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001224 }
1225 }
1226 }
1227
1228 @Override
1229 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001230 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001231
1232 // silently discard when control disabled
1233 // TODO: eventually migrate to be always enabled
1234 if (!mBandwidthControlEnabled) return;
1235
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001236 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001237 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001238 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001239 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001240 }
1241 }
1242
1243 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001244 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001245 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001246
Jeff Sharkey350083e2011-06-29 10:45:16 -07001247 // silently discard when control disabled
1248 // TODO: eventually migrate to be always enabled
1249 if (!mBandwidthControlEnabled) return;
1250
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001251 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001252 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1253 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1254 // TODO: eventually consider throwing
1255 return;
1256 }
1257
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001258 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001259 mConnector.execute("bandwidth",
1260 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001261 if (rejectOnQuotaInterfaces) {
1262 mUidRejectOnQuota.put(uid, true);
1263 } else {
1264 mUidRejectOnQuota.delete(uid);
1265 }
1266 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001267 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001268 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001269 }
1270 }
1271
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001272 @Override
1273 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001274 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001275 return mBandwidthControlEnabled;
1276 }
1277
1278 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001279 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001280 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001281 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001282 }
1283
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001284 @Override
1285 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001287
1288 if (ifacePairs.length % 2 != 0) {
1289 throw new IllegalArgumentException(
1290 "unexpected ifacePairs; length=" + ifacePairs.length);
1291 }
1292
1293 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1294 for (int i = 0; i < ifacePairs.length; i += 2) {
1295 final String ifaceIn = ifacePairs[i];
1296 final String ifaceOut = ifacePairs[i + 1];
1297 if (ifaceIn != null && ifaceOut != null) {
1298 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1299 }
1300 }
1301 return stats;
1302 }
1303
1304 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001305 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001306 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001307 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001308 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001309 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001310 }
1311
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001312 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001313
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001314 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1315 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1316 tok.nextToken();
1317 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001318
1319 try {
1320 final NetworkStats.Entry entry = new NetworkStats.Entry();
1321 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001322 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001323 entry.set = SET_DEFAULT;
1324 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001325 entry.rxBytes = Long.parseLong(tok.nextToken());
1326 entry.rxPackets = Long.parseLong(tok.nextToken());
1327 entry.txBytes = Long.parseLong(tok.nextToken());
1328 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001329 return entry;
1330 } catch (NumberFormatException e) {
1331 throw new IllegalStateException(
1332 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1333 }
1334 }
1335
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001336 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001337 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001338 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001339 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001340 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001341 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001342 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001343 }
San Mehat91cac642010-03-31 14:31:36 -07001344 }
1345
1346 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001347 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001348 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001349 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1350 } catch (NativeDaemonConnectorException e) {
1351 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001352 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001353
1354 if (rx) {
1355 event.checkCode(InterfaceRxThrottleResult);
1356 } else {
1357 event.checkCode(InterfaceTxThrottleResult);
1358 }
1359
1360 try {
1361 return Integer.parseInt(event.getMessage());
1362 } catch (NumberFormatException e) {
1363 throw new IllegalStateException("unexpected response:" + event);
1364 }
San Mehat91cac642010-03-31 14:31:36 -07001365 }
1366
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001367 @Override
San Mehat91cac642010-03-31 14:31:36 -07001368 public int getInterfaceRxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001369 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001370 return getInterfaceThrottle(iface, true);
1371 }
1372
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001373 @Override
San Mehat91cac642010-03-31 14:31:36 -07001374 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001375 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001376 return getInterfaceThrottle(iface, false);
1377 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001378
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001379 @Override
1380 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001381 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001382 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001383 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001384 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001385 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001386 }
1387 }
1388
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001389 @Override
1390 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001391 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001392
1393 final Command cmd = new Command("resolver", "setifdns", iface);
1394 for (String s : servers) {
1395 InetAddress a = NetworkUtils.numericToInetAddress(s);
1396 if (a.isAnyLocalAddress() == false) {
1397 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001398 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001399 }
1400
1401 try {
1402 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001403 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001404 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001405 }
1406 }
1407
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001408 @Override
1409 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001410 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001411 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001412 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001413 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001414 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001415 }
1416 }
1417
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001418 @Override
1419 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001420 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001421 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001422 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001423 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001424 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001425 }
1426 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001427
1428 /** {@inheritDoc} */
1429 public void monitor() {
1430 if (mConnector != null) {
1431 mConnector.monitor();
1432 }
1433 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001434
1435 @Override
1436 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1437 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1438
Robert Greenwalt470fd722012-01-18 12:51:15 -08001439 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1440 mConnector.dump(fd, pw, args);
1441 pw.println();
1442
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001443 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1444
1445 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001446 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1447 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001448 }
1449
1450 synchronized (mUidRejectOnQuota) {
1451 pw.print("UID reject on quota ifaces: [");
1452 final int size = mUidRejectOnQuota.size();
1453 for (int i = 0; i < size; i++) {
1454 pw.print(mUidRejectOnQuota.keyAt(i));
1455 if (i < size - 1) pw.print(",");
1456 }
1457 pw.println("]");
1458 }
1459 }
San Mehat873f2142010-01-14 10:25:07 -08001460}