blob: fbd45a042d53912ba54283865a509845aa9272ea [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
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070038import android.bluetooth.BluetoothTetheringDataTracker;
San Mehat873f2142010-01-14 10:25:07 -080039import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080040import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070041import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080042import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070043import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080044import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070045import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080046import android.net.wifi.WifiConfiguration;
47import android.net.wifi.WifiConfiguration.KeyMgmt;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070048import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080049import android.os.INetworkManagementService;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080050import android.os.RemoteCallbackList;
51import android.os.RemoteException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070052import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080053import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080054import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080055import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070056import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080057
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070058import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070059import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080060import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070061import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070062
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070063import java.io.BufferedReader;
64import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080065import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070066import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070067import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070068import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070069import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070070import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070071import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070072import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070073import java.net.InterfaceAddress;
74import java.net.NetworkInterface;
75import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070076import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070077import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070078import java.util.HashMap;
79import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070080import java.util.NoSuchElementException;
81import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070082import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080083
84/**
85 * @hide
86 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070087public class NetworkManagementService extends INetworkManagementService.Stub
88 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070089 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070090 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070091 private static final String NETD_TAG = "NetdConnector";
92
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080093 private static final String ADD = "add";
94 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070095
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070096 private static final String ALLOW = "allow";
97 private static final String DENY = "deny";
98
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070099 private static final String DEFAULT = "default";
100 private static final String SECONDARY = "secondary";
101
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700102 /**
103 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
104 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
105 */
106 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
107
San Mehat873f2142010-01-14 10:25:07 -0800108 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700109 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800110 public static final int InterfaceListResult = 110;
111 public static final int TetherInterfaceListResult = 111;
112 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800113 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800114
115 public static final int TetherStatusResult = 210;
116 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800117 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800118 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700119 public static final int InterfaceRxCounterResult = 216;
120 public static final int InterfaceTxCounterResult = 217;
121 public static final int InterfaceRxThrottleResult = 218;
122 public static final int InterfaceTxThrottleResult = 219;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700123 public static final int QuotaCounterResult = 220;
124 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800125 public static final int DnsProxyQueryResult = 222;
Robert Greenwalte3253922010-02-18 09:23:25 -0800126
127 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700128 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700129 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800130 }
131
132 /**
133 * Binder context for this service
134 */
135 private Context mContext;
136
137 /**
138 * connector object for communicating with netd
139 */
140 private NativeDaemonConnector mConnector;
141
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700142 private final Handler mMainHandler = new Handler();
143
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700144 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700145 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700146
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800147 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
148 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800149
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700150 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
151
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700152 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700153 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700154 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700155 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700156 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700157 /** Set of UIDs with active reject rules. */
158 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
159
Haoyu Bai04124232012-06-28 15:26:19 -0700160 private Object mIdleTimerLock = new Object();
161 /** Set of interfaces with active idle timers. */
162 private static class IdleTimerParams {
163 public final int timeout;
164 public final String label;
165 public int networkCount;
166
167 IdleTimerParams(int timeout, String label) {
168 this.timeout = timeout;
169 this.label = label;
170 this.networkCount = 1;
171 }
172 }
173 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
174
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700175 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700176 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700177
San Mehat873f2142010-01-14 10:25:07 -0800178 /**
179 * Constructs a new NetworkManagementService instance
180 *
181 * @param context Binder context for this service
182 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700183 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800184 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800185
Marco Nelissen62dbb222010-02-18 10:56:30 -0800186 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
187 return;
188 }
189
San Mehat873f2142010-01-14 10:25:07 -0800190 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700191 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700192 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700193
194 // Add ourself to the Watchdog monitors.
195 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700196 }
197
198 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700199 final NetworkManagementService service = new NetworkManagementService(context);
200 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700201 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
202 service.mThread.start();
203 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700204 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700205 if (DBG) Slog.d(TAG, "Connected");
206 return service;
San Mehat873f2142010-01-14 10:25:07 -0800207 }
208
Jeff Sharkey350083e2011-06-29 10:45:16 -0700209 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700210 prepareNativeDaemon();
211 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700212 }
213
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800214 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800215 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800216 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800217 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800218 }
219
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800220 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800221 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800223 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800224 }
225
226 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700227 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800228 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700229 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800230 final int length = mObservers.beginBroadcast();
231 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800232 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800233 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
234 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700235 }
236 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700238 }
239
240 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700241 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 * (typically, an Ethernet cable has been plugged-in or unplugged).
243 */
244 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800245 final int length = mObservers.beginBroadcast();
246 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700247 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800248 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
249 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800250 }
251 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800252 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800253 }
254
255 /**
256 * Notify our observers of an interface addition.
257 */
258 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800259 final int length = mObservers.beginBroadcast();
260 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800261 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800262 mObservers.getBroadcastItem(i).interfaceAdded(iface);
263 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800264 }
265 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800266 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800267 }
268
269 /**
270 * Notify our observers of an interface removal.
271 */
272 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700273 // netd already clears out quota and alerts for removed ifaces; update
274 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700275 mActiveAlerts.remove(iface);
276 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700277
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800278 final int length = mObservers.beginBroadcast();
279 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800280 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800281 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
282 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800283 }
284 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800286 }
287
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700288 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700289 * Notify our observers of a limit reached.
290 */
291 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800292 final int length = mObservers.beginBroadcast();
293 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700294 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
296 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700297 }
298 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800299 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700300 }
301
302 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700303 * Notify our observers of a change in the data activity state of the interface
304 */
305 private void notifyInterfaceClassActivity(String label, boolean active) {
306 final int length = mObservers.beginBroadcast();
307 for (int i = 0; i < length; i++) {
308 try {
309 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
310 } catch (RemoteException e) {
311 }
312 }
313 mObservers.finishBroadcast();
314 }
315
316 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700317 * Prepare native daemon once connected, enabling modules and pushing any
318 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700319 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700320 private void prepareNativeDaemon() {
321 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700322
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700323 // only enable bandwidth control when support exists
324 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
325 if (hasKernelSupport) {
326 Slog.d(TAG, "enabling bandwidth control");
327 try {
328 mConnector.execute("bandwidth", "enable");
329 mBandwidthControlEnabled = true;
330 } catch (NativeDaemonConnectorException e) {
331 Log.wtf(TAG, "problem enabling bandwidth controls", e);
332 }
333 } else {
334 Slog.d(TAG, "not enabling bandwidth control");
335 }
336
337 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
338
339 // push any existing quota or UID rules
340 synchronized (mQuotaLock) {
341 int size = mActiveQuotas.size();
342 if (size > 0) {
343 Slog.d(TAG, "pushing " + size + " active quota rules");
344 final HashMap<String, Long> activeQuotas = mActiveQuotas;
345 mActiveQuotas = Maps.newHashMap();
346 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
347 setInterfaceQuota(entry.getKey(), entry.getValue());
348 }
349 }
350
351 size = mActiveAlerts.size();
352 if (size > 0) {
353 Slog.d(TAG, "pushing " + size + " active alert rules");
354 final HashMap<String, Long> activeAlerts = mActiveAlerts;
355 mActiveAlerts = Maps.newHashMap();
356 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
357 setInterfaceAlert(entry.getKey(), entry.getValue());
358 }
359 }
360
361 size = mUidRejectOnQuota.size();
362 if (size > 0) {
363 Slog.d(TAG, "pushing " + size + " active uid rules");
364 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
365 mUidRejectOnQuota = new SparseBooleanArray();
366 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
367 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
368 }
369 }
370 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700371
372 // TODO: Push any existing firewall state
373 setFirewallEnabled(mFirewallEnabled);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700374 }
San Mehat4d02d002010-01-22 16:07:46 -0800375
San Mehat873f2142010-01-14 10:25:07 -0800376 //
377 // Netd Callback handling
378 //
379
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700380 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
381 @Override
San Mehat873f2142010-01-14 10:25:07 -0800382 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700383 // event is dispatched from internal NDC thread, so we prepare the
384 // daemon back on main thread.
385 if (mConnectedSignal != null) {
386 mConnectedSignal.countDown();
387 mConnectedSignal = null;
388 } else {
389 mMainHandler.post(new Runnable() {
390 @Override
391 public void run() {
392 prepareNativeDaemon();
393 }
394 });
395 }
San Mehat873f2142010-01-14 10:25:07 -0800396 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700397
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700398 @Override
San Mehat873f2142010-01-14 10:25:07 -0800399 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700400 switch (code) {
401 case NetdResponseCode.InterfaceChange:
402 /*
403 * a network interface change occured
404 * Format: "NNN Iface added <name>"
405 * "NNN Iface removed <name>"
406 * "NNN Iface changed <name> <up/down>"
407 * "NNN Iface linkstatus <name> <up/down>"
408 */
409 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
410 throw new IllegalStateException(
411 String.format("Invalid event from daemon (%s)", raw));
412 }
413 if (cooked[2].equals("added")) {
414 notifyInterfaceAdded(cooked[3]);
415 return true;
416 } else if (cooked[2].equals("removed")) {
417 notifyInterfaceRemoved(cooked[3]);
418 return true;
419 } else if (cooked[2].equals("changed") && cooked.length == 5) {
420 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
421 return true;
422 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
423 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
424 return true;
425 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800426 throw new IllegalStateException(
427 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700428 // break;
429 case NetdResponseCode.BandwidthControl:
430 /*
431 * Bandwidth control needs some attention
432 * Format: "NNN limit alert <alertName> <ifaceName>"
433 */
434 if (cooked.length < 5 || !cooked[1].equals("limit")) {
435 throw new IllegalStateException(
436 String.format("Invalid event from daemon (%s)", raw));
437 }
438 if (cooked[2].equals("alert")) {
439 notifyLimitReached(cooked[3], cooked[4]);
440 return true;
441 }
442 throw new IllegalStateException(
443 String.format("Invalid event from daemon (%s)", raw));
444 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700445 case NetdResponseCode.InterfaceClassActivity:
446 /*
447 * An network interface class state changed (active/idle)
448 * Format: "NNN IfaceClass <active/idle> <label>"
449 */
450 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
451 throw new IllegalStateException(
452 String.format("Invalid event from daemon (%s)", raw));
453 }
454 boolean isActive = cooked[2].equals("active");
455 notifyInterfaceClassActivity(cooked[3], isActive);
456 return true;
457 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700458 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800459 }
460 return false;
San Mehat873f2142010-01-14 10:25:07 -0800461 }
462 }
463
San Mehated4fc8a2010-01-22 12:28:36 -0800464
San Mehat873f2142010-01-14 10:25:07 -0800465 //
466 // INetworkManagementService members
467 //
468
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800469 @Override
470 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800471 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700472 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800473 return NativeDaemonEvent.filterMessageList(
474 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700475 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800476 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700477 }
San Mehated4fc8a2010-01-22 12:28:36 -0800478 }
479
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800480 @Override
481 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800482 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800483
484 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700485 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800486 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700487 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800488 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700489 }
San Mehated4fc8a2010-01-22 12:28:36 -0800490
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800491 event.checkCode(InterfaceGetCfgResult);
492
493 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
494 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800495
Kenny Roota80ce062010-06-01 13:23:53 -0700496 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800497 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700498 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800499 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800500 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800501 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700502 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800503 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800504 } catch (IllegalArgumentException iae) {
505 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700506 }
507
508 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800509 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800510 } catch (NumberFormatException nfe) {
511 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700512 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800513
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800514 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
515 while (st.hasMoreTokens()) {
516 cfg.setFlag(st.nextToken());
517 }
Kenny Roota80ce062010-06-01 13:23:53 -0700518 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800519 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800520 }
San Mehated4fc8a2010-01-22 12:28:36 -0800521 return cfg;
522 }
523
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800524 @Override
525 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800526 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800527 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800528 if (linkAddr == null || linkAddr.getAddress() == null) {
529 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800530 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800531
532 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800533 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800534 linkAddr.getNetworkPrefixLength());
535 for (String flag : cfg.getFlags()) {
536 cmd.appendArg(flag);
537 }
538
Kenny Roota80ce062010-06-01 13:23:53 -0700539 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800540 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700541 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800542 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700543 }
San Mehat873f2142010-01-14 10:25:07 -0800544 }
545
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800546 @Override
547 public void setInterfaceDown(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.setInterfaceDown();
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 setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800556 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800557 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800558 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800559 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700560 }
561
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800562 @Override
563 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800564 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700565 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800566 mConnector.execute(
567 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700568 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800569 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700570 }
571 }
572
Irfan Sherifff5600612011-06-16 10:26:28 -0700573 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
574 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800575 @Override
576 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800577 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700578 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800579 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700580 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800581 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700582 }
583 }
584
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800585 @Override
586 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800587 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700588 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800589 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700590 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800591 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700592 }
593 }
594
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800595 @Override
596 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800597 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700598 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800599 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700600 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800601 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700602 }
603 }
604
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800605 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700606 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800607 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700608 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700609 }
610
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800611 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700612 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800613 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700614 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700615 }
616
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800617 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700618 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800619 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700620 modifyRoute(interfaceName, ADD, route, SECONDARY);
621 }
622
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800623 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700624 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800625 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700626 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
627 }
628
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800629 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
630 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700631
632 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800633 final LinkAddress la = route.getDestination();
634 cmd.appendArg(la.getAddress().getHostAddress());
635 cmd.appendArg(la.getNetworkPrefixLength());
636
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700637 if (route.getGateway() == null) {
638 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800639 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700640 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800641 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700642 }
643 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800644 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700645 }
646
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800647 try {
648 mConnector.execute(cmd);
649 } catch (NativeDaemonConnectorException e) {
650 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700651 }
652 }
653
654 private ArrayList<String> readRouteList(String filename) {
655 FileInputStream fstream = null;
656 ArrayList<String> list = new ArrayList<String>();
657
658 try {
659 fstream = new FileInputStream(filename);
660 DataInputStream in = new DataInputStream(fstream);
661 BufferedReader br = new BufferedReader(new InputStreamReader(in));
662 String s;
663
664 // throw away the title line
665
666 while (((s = br.readLine()) != null) && (s.length() != 0)) {
667 list.add(s);
668 }
669 } catch (IOException ex) {
670 // return current list, possibly empty
671 } finally {
672 if (fstream != null) {
673 try {
674 fstream.close();
675 } catch (IOException ex) {}
676 }
677 }
678
679 return list;
680 }
681
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800682 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700683 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800684 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700685 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
686
687 // v4 routes listed as:
688 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
689 for (String s : readRouteList("/proc/net/route")) {
690 String[] fields = s.split("\t");
691
692 if (fields.length > 7) {
693 String iface = fields[0];
694
695 if (interfaceName.equals(iface)) {
696 String dest = fields[1];
697 String gate = fields[2];
698 String flags = fields[3]; // future use?
699 String mask = fields[7];
700 try {
701 // address stored as a hex string, ex: 0014A8C0
702 InetAddress destAddr =
703 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
704 int prefixLength =
705 NetworkUtils.netmaskIntToPrefixLength(
706 (int)Long.parseLong(mask, 16));
707 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
708
709 // address stored as a hex string, ex 0014A8C0
710 InetAddress gatewayAddr =
711 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
712
713 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
714 routes.add(route);
715 } catch (Exception e) {
716 Log.e(TAG, "Error parsing route " + s + " : " + e);
717 continue;
718 }
719 }
720 }
721 }
722
723 // v6 routes listed as:
724 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
725 for (String s : readRouteList("/proc/net/ipv6_route")) {
726 String[]fields = s.split("\\s+");
727 if (fields.length > 9) {
728 String iface = fields[9].trim();
729 if (interfaceName.equals(iface)) {
730 String dest = fields[0];
731 String prefix = fields[1];
732 String gate = fields[4];
733
734 try {
735 // prefix length stored as a hex string, ex 40
736 int prefixLength = Integer.parseInt(prefix, 16);
737
738 // address stored as a 32 char hex string
739 // ex fe800000000000000000000000000000
740 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
741 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
742
743 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
744
745 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
746 routes.add(route);
747 } catch (Exception e) {
748 Log.e(TAG, "Error parsing route " + s + " : " + e);
749 continue;
750 }
751 }
752 }
753 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800754 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700755 }
756
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800757 @Override
San Mehat873f2142010-01-14 10:25:07 -0800758 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800759 // TODO: remove from aidl if nobody calls externally
760 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800761
Joe Onorato8a9b2202010-02-26 18:56:32 -0800762 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800763 }
764
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800765 @Override
San Mehat873f2142010-01-14 10:25:07 -0800766 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800767 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800768
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800769 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700770 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800771 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700772 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800773 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700774 }
San Mehat873f2142010-01-14 10:25:07 -0800775
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800776 // 211 Forwarding enabled
777 event.checkCode(IpFwdStatusResult);
778 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800779 }
780
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800781 @Override
782 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800783 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800784 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800785 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800786 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800787 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800788 }
San Mehat873f2142010-01-14 10:25:07 -0800789 }
790
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800791 @Override
792 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800793 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700794 // cmd is "tether start first_start first_stop second_start second_stop ..."
795 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800796
797 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700798 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700800 }
Kenny Roota80ce062010-06-01 13:23:53 -0700801
802 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800803 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700804 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800805 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700806 }
San Mehat873f2142010-01-14 10:25:07 -0800807 }
808
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800809 @Override
810 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800811 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700812 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800813 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700814 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800815 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700816 }
San Mehat873f2142010-01-14 10:25:07 -0800817 }
818
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800819 @Override
820 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800821 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800822
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700824 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800825 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700826 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800827 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700828 }
San Mehat873f2142010-01-14 10:25:07 -0800829
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800830 // 210 Tethering services started
831 event.checkCode(TetherStatusResult);
832 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800833 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700834
835 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700836 public void startReverseTethering(String iface)
837 throws IllegalStateException {
838 if (DBG) Slog.d(TAG, "startReverseTethering in");
Matthew Xiefe19f122012-07-12 16:03:32 -0700839 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700840 // cmd is "tether start first_start first_stop second_start second_stop ..."
841 // an odd number of addrs will fail
842 String cmd = "tether start-reverse";
843 cmd += " " + iface;
844 if (DBG) Slog.d(TAG, "startReverseTethering cmd: " + cmd);
845 try {
846 mConnector.doCommand(cmd);
847 } catch (NativeDaemonConnectorException e) {
848 throw new IllegalStateException("Unable to communicate to native daemon");
849 }
850 BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
851
852 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700853
854 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700855 public void stopReverseTethering() throws IllegalStateException {
Matthew Xiefe19f122012-07-12 16:03:32 -0700856 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700857 try {
858 mConnector.doCommand("tether stop-reverse");
859 } catch (NativeDaemonConnectorException e) {
860 throw new IllegalStateException("Unable to communicate to native daemon to stop tether");
861 }
862 BluetoothTetheringDataTracker.getInstance().stopReverseTether();
863 }
San Mehat873f2142010-01-14 10:25:07 -0800864
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800865 @Override
866 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800867 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700868 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800869 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700870 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800871 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700872 }
San Mehat873f2142010-01-14 10:25:07 -0800873 }
874
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800875 @Override
San Mehat873f2142010-01-14 10:25:07 -0800876 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800877 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700878 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800879 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700880 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800881 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700882 }
San Mehat873f2142010-01-14 10:25:07 -0800883 }
884
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800885 @Override
886 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800887 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700888 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800889 return NativeDaemonEvent.filterMessageList(
890 mConnector.executeForList("tether", "interface", "list"),
891 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700892 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800893 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700894 }
San Mehat873f2142010-01-14 10:25:07 -0800895 }
896
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800897 @Override
898 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800899 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800900
901 final Command cmd = new Command("tether", "dns", "set");
902 for (String s : dns) {
903 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
904 }
905
San Mehat873f2142010-01-14 10:25:07 -0800906 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800907 mConnector.execute(cmd);
908 } catch (NativeDaemonConnectorException e) {
909 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800910 }
911 }
912
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800913 @Override
914 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800915 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700916 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800917 return NativeDaemonEvent.filterMessageList(
918 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700919 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800920 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700921 }
San Mehat873f2142010-01-14 10:25:07 -0800922 }
923
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800924 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700925 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800926 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700927
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800928 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
929 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800930 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800932 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800933 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
934 .getInterfaceAddresses();
935 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800936 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800937 InetAddress addr = NetworkUtils.getNetworkPart(
938 ia.getAddress(), ia.getNetworkPrefixLength());
939 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800940 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700941 }
942
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800943 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800944 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800945 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800946 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800947 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700948 }
949
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800950 @Override
951 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700954 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800955 } catch (SocketException e) {
956 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700957 }
San Mehat873f2142010-01-14 10:25:07 -0800958 }
959
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800960 @Override
961 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800962 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700963 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700964 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800965 } catch (SocketException e) {
966 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700967 }
San Mehat873f2142010-01-14 10:25:07 -0800968 }
San Mehat72759df2010-01-19 13:50:37 -0800969
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800970 @Override
971 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800972 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700973 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800974 return NativeDaemonEvent.filterMessageList(
975 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700976 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800977 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700978 }
San Mehat72759df2010-01-19 13:50:37 -0800979 }
980
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800981 @Override
982 public void attachPppd(
983 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800984 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800985 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800987 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
988 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
989 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800990 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700991 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800992 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800993 }
994 }
995
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800996 @Override
997 public void detachPppd(String tty) {
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("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001001 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001002 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001003 }
San Mehat72759df2010-01-19 13:50:37 -08001004 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001005
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001006 @Override
1007 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001008 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 {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001011 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001012 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001013 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001014 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001015 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001016 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001017 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001018 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001019 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001020 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001021 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001022 }
1023
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001024 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001025 switch (wifiConfig.getAuthType()) {
1026 case KeyMgmt.WPA_PSK:
1027 return "wpa-psk";
1028 case KeyMgmt.WPA2_PSK:
1029 return "wpa2-psk";
1030 default:
1031 return "open";
1032 }
1033 }
1034
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001035 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001036 @Override
1037 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001038 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001039 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001040 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001041 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001042 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001043 }
1044 }
1045
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001046 @Override
1047 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001048 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001049 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001050 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001051 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001052 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001053 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001054 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001055 }
1056
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001057 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001058 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001059 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001060 try {
1061 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001062 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001063 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001064 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001065 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001066 }
1067 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001068 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001069 }
1070 }
San Mehat91cac642010-03-31 14:31:36 -07001071
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001072 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001073 public void addIdleTimer(String iface, int timeout, String label) {
1074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1075
1076 if (DBG) Slog.d(TAG, "Adding idletimer");
1077
1078 synchronized (mIdleTimerLock) {
1079 IdleTimerParams params = mActiveIdleTimers.get(iface);
1080 if (params != null) {
1081 // the interface already has idletimer, update network count
1082 params.networkCount++;
1083 return;
1084 }
1085
1086 try {
1087 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1088 } catch (NativeDaemonConnectorException e) {
1089 throw e.rethrowAsParcelableException();
1090 }
1091 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1092 }
1093 }
1094
1095 @Override
1096 public void removeIdleTimer(String iface) {
1097 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1098
1099 if (DBG) Slog.d(TAG, "Removing idletimer");
1100
1101 synchronized (mIdleTimerLock) {
1102 IdleTimerParams params = mActiveIdleTimers.get(iface);
1103 if (params == null || --(params.networkCount) > 0) {
1104 return;
1105 }
1106
1107 try {
1108 mConnector.execute("idletimer", "remove", iface,
1109 Integer.toString(params.timeout), params.label);
1110 } catch (NativeDaemonConnectorException e) {
1111 throw e.rethrowAsParcelableException();
1112 }
1113 mActiveIdleTimers.remove(iface);
1114 }
1115 }
1116
1117 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001118 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001119 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001120 return mStatsFactory.readNetworkStatsSummaryDev();
1121 }
1122
1123 @Override
1124 public NetworkStats getNetworkStatsSummaryXt() {
1125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1126 return mStatsFactory.readNetworkStatsSummaryXt();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001127 }
1128
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001129 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001130 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001131 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001132 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -07001133 }
1134
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001135 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001136 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001137 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001138
Jeff Sharkey350083e2011-06-29 10:45:16 -07001139 // silently discard when control disabled
1140 // TODO: eventually migrate to be always enabled
1141 if (!mBandwidthControlEnabled) return;
1142
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001143 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001144 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001145 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001146 }
1147
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001148 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001149 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001150 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001151 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001152 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001153 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001154 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001155 }
1156 }
1157
1158 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001159 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001160 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001161
Jeff Sharkey350083e2011-06-29 10:45:16 -07001162 // silently discard when control disabled
1163 // TODO: eventually migrate to be always enabled
1164 if (!mBandwidthControlEnabled) return;
1165
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001166 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001167 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001168 // TODO: eventually consider throwing
1169 return;
1170 }
1171
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001172 mActiveQuotas.remove(iface);
1173 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001174
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001175 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001176 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001177 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001178 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001179 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001180 }
1181 }
1182 }
1183
1184 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001185 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001186 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001187
1188 // silently discard when control disabled
1189 // TODO: eventually migrate to be always enabled
1190 if (!mBandwidthControlEnabled) return;
1191
1192 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001193 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001194 throw new IllegalStateException("setting alert requires existing quota on iface");
1195 }
1196
1197 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001198 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001199 throw new IllegalStateException("iface " + iface + " already has alert");
1200 }
1201
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001202 try {
1203 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001204 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001205 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001206 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001207 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001208 }
1209 }
1210 }
1211
1212 @Override
1213 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001214 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001215
1216 // silently discard when control disabled
1217 // TODO: eventually migrate to be always enabled
1218 if (!mBandwidthControlEnabled) return;
1219
1220 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001221 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001222 // TODO: eventually consider throwing
1223 return;
1224 }
1225
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001226 try {
1227 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001228 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001229 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001230 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001231 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001232 }
1233 }
1234 }
1235
1236 @Override
1237 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001238 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001239
1240 // silently discard when control disabled
1241 // TODO: eventually migrate to be always enabled
1242 if (!mBandwidthControlEnabled) return;
1243
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001244 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001245 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001246 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001247 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001248 }
1249 }
1250
1251 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001252 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001253 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001254
Jeff Sharkey350083e2011-06-29 10:45:16 -07001255 // silently discard when control disabled
1256 // TODO: eventually migrate to be always enabled
1257 if (!mBandwidthControlEnabled) return;
1258
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001259 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001260 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1261 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1262 // TODO: eventually consider throwing
1263 return;
1264 }
1265
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001266 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001267 mConnector.execute("bandwidth",
1268 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001269 if (rejectOnQuotaInterfaces) {
1270 mUidRejectOnQuota.put(uid, true);
1271 } else {
1272 mUidRejectOnQuota.delete(uid);
1273 }
1274 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001275 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001276 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001277 }
1278 }
1279
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001280 @Override
1281 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001282 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001283 return mBandwidthControlEnabled;
1284 }
1285
1286 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001287 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001288 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001289 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001290 }
1291
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001292 @Override
1293 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001294 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001295
1296 if (ifacePairs.length % 2 != 0) {
1297 throw new IllegalArgumentException(
1298 "unexpected ifacePairs; length=" + ifacePairs.length);
1299 }
1300
1301 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1302 for (int i = 0; i < ifacePairs.length; i += 2) {
1303 final String ifaceIn = ifacePairs[i];
1304 final String ifaceOut = ifacePairs[i + 1];
1305 if (ifaceIn != null && ifaceOut != null) {
1306 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1307 }
1308 }
1309 return stats;
1310 }
1311
1312 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001313 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001314 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001315 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001316 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001317 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001318 }
1319
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001320 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001321
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001322 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1323 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1324 tok.nextToken();
1325 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001326
1327 try {
1328 final NetworkStats.Entry entry = new NetworkStats.Entry();
1329 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001330 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001331 entry.set = SET_DEFAULT;
1332 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001333 entry.rxBytes = Long.parseLong(tok.nextToken());
1334 entry.rxPackets = Long.parseLong(tok.nextToken());
1335 entry.txBytes = Long.parseLong(tok.nextToken());
1336 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001337 return entry;
1338 } catch (NumberFormatException e) {
1339 throw new IllegalStateException(
1340 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1341 }
1342 }
1343
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001344 @Override
San Mehatf0db6e12010-04-07 15:22:10 -07001345 public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001346 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001347 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001348 mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps);
Kenny Roota80ce062010-06-01 13:23:53 -07001349 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001350 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001351 }
San Mehat91cac642010-03-31 14:31:36 -07001352 }
1353
1354 private int getInterfaceThrottle(String iface, boolean rx) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001355 final NativeDaemonEvent event;
San Mehat91cac642010-03-31 14:31:36 -07001356 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001357 event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx");
1358 } catch (NativeDaemonConnectorException e) {
1359 throw e.rethrowAsParcelableException();
San Mehat91cac642010-03-31 14:31:36 -07001360 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001361
1362 if (rx) {
1363 event.checkCode(InterfaceRxThrottleResult);
1364 } else {
1365 event.checkCode(InterfaceTxThrottleResult);
1366 }
1367
1368 try {
1369 return Integer.parseInt(event.getMessage());
1370 } catch (NumberFormatException e) {
1371 throw new IllegalStateException("unexpected response:" + event);
1372 }
San Mehat91cac642010-03-31 14:31:36 -07001373 }
1374
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001375 @Override
San Mehat91cac642010-03-31 14:31:36 -07001376 public int getInterfaceRxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001377 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001378 return getInterfaceThrottle(iface, true);
1379 }
1380
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001381 @Override
San Mehat91cac642010-03-31 14:31:36 -07001382 public int getInterfaceTxThrottle(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001383 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat91cac642010-03-31 14:31:36 -07001384 return getInterfaceThrottle(iface, false);
1385 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001386
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001387 @Override
1388 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001389 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001390 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001391 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001392 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001393 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001394 }
1395 }
1396
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001397 @Override
1398 public void setDnsServersForInterface(String iface, String[] servers) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001399 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001400
1401 final Command cmd = new Command("resolver", "setifdns", iface);
1402 for (String s : servers) {
1403 InetAddress a = NetworkUtils.numericToInetAddress(s);
1404 if (a.isAnyLocalAddress() == false) {
1405 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001406 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001407 }
1408
1409 try {
1410 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001411 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001412 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001413 }
1414 }
1415
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001416 @Override
1417 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001418 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001419 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001420 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001421 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001422 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001423 }
1424 }
1425
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001426 @Override
1427 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001428 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001429 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001430 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001431 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001432 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001433 }
1434 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001435
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001436 @Override
1437 public void setFirewallEnabled(boolean enabled) {
1438 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1439 try {
1440 mConnector.execute("firewall", enabled ? "enable" : "disable");
1441 mFirewallEnabled = enabled;
1442 } catch (NativeDaemonConnectorException e) {
1443 throw e.rethrowAsParcelableException();
1444 }
1445 }
1446
1447 @Override
1448 public boolean isFirewallEnabled() {
1449 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1450 return mFirewallEnabled;
1451 }
1452
1453 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001454 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001455 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1456 Preconditions.checkState(mFirewallEnabled);
1457 final String rule = allow ? ALLOW : DENY;
1458 try {
1459 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1460 } catch (NativeDaemonConnectorException e) {
1461 throw e.rethrowAsParcelableException();
1462 }
1463 }
1464
1465 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001466 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001467 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1468 Preconditions.checkState(mFirewallEnabled);
1469 final String rule = allow ? ALLOW : DENY;
1470 try {
1471 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1472 } catch (NativeDaemonConnectorException e) {
1473 throw e.rethrowAsParcelableException();
1474 }
1475 }
1476
1477 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001478 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001479 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1480 Preconditions.checkState(mFirewallEnabled);
1481 final String rule = allow ? ALLOW : DENY;
1482 try {
1483 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1484 } catch (NativeDaemonConnectorException e) {
1485 throw e.rethrowAsParcelableException();
1486 }
1487 }
1488
1489 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001490 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001491 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1492 Preconditions.checkState(mFirewallEnabled);
1493 final String rule = allow ? ALLOW : DENY;
1494 try {
1495 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1496 } catch (NativeDaemonConnectorException e) {
1497 throw e.rethrowAsParcelableException();
1498 }
1499 }
1500
1501 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001502 public void monitor() {
1503 if (mConnector != null) {
1504 mConnector.monitor();
1505 }
1506 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001507
1508 @Override
1509 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1510 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1511
Robert Greenwalt470fd722012-01-18 12:51:15 -08001512 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1513 mConnector.dump(fd, pw, args);
1514 pw.println();
1515
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001516 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1517
1518 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001519 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1520 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001521 }
1522
1523 synchronized (mUidRejectOnQuota) {
1524 pw.print("UID reject on quota ifaces: [");
1525 final int size = mUidRejectOnQuota.size();
1526 for (int i = 0; i < size; i++) {
1527 pw.print(mUidRejectOnQuota.keyAt(i));
1528 if (i < size - 1) pw.print(",");
1529 }
1530 pw.println("]");
1531 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001532
1533 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001534 }
San Mehat873f2142010-01-14 10:25:07 -08001535}