blob: 5630b083aa07eb7a0a044d10dc8be21e8cafdf36 [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;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080028import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
29import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070034import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070035
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070036import android.bluetooth.BluetoothTetheringDataTracker;
San Mehat873f2142010-01-14 10:25:07 -080037import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080038import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070039import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080040import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070041import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080042import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070043import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080044import android.net.wifi.WifiConfiguration;
45import android.net.wifi.WifiConfiguration.KeyMgmt;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070046import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070047import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080048import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070049import android.os.Process;
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 Sharkey69ddab42012-08-25 00:05:46 -070061import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070062import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070063
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070064import java.io.BufferedReader;
65import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080066import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070067import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070068import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070069import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070070import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070071import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070072import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070073import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070074import java.net.InterfaceAddress;
75import java.net.NetworkInterface;
76import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070077import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070078import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070079import java.util.HashMap;
80import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070081import java.util.NoSuchElementException;
82import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070083import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080084
85/**
86 * @hide
87 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070088public class NetworkManagementService extends INetworkManagementService.Stub
89 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070090 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070091 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070092 private static final String NETD_TAG = "NetdConnector";
93
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080094 private static final String ADD = "add";
95 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070096
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070097 private static final String ALLOW = "allow";
98 private static final String DENY = "deny";
99
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700100 private static final String DEFAULT = "default";
101 private static final String SECONDARY = "secondary";
102
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700103 /**
104 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
105 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
106 */
107 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
108
San Mehat873f2142010-01-14 10:25:07 -0800109 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700110 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800111 public static final int InterfaceListResult = 110;
112 public static final int TetherInterfaceListResult = 111;
113 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800114 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800115
116 public static final int TetherStatusResult = 210;
117 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800118 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800119 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700120 public static final int InterfaceRxCounterResult = 216;
121 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700122 public static final int QuotaCounterResult = 220;
123 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800124 public static final int DnsProxyQueryResult = 222;
Robert Greenwalte3253922010-02-18 09:23:25 -0800125
126 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700127 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700128 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800129 }
130
131 /**
132 * Binder context for this service
133 */
134 private Context mContext;
135
136 /**
137 * connector object for communicating with netd
138 */
139 private NativeDaemonConnector mConnector;
140
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700141 private final Handler mMainHandler = new Handler();
142
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700143 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700144 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700145
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800146 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
147 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800148
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700149 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
150
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700151 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700152 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700153 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700154 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700155 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700156 /** Set of UIDs with active reject rules. */
157 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
158
Haoyu Bai04124232012-06-28 15:26:19 -0700159 private Object mIdleTimerLock = new Object();
160 /** Set of interfaces with active idle timers. */
161 private static class IdleTimerParams {
162 public final int timeout;
163 public final String label;
164 public int networkCount;
165
166 IdleTimerParams(int timeout, String label) {
167 this.timeout = timeout;
168 this.label = label;
169 this.networkCount = 1;
170 }
171 }
172 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
173
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700174 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700175 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700176
San Mehat873f2142010-01-14 10:25:07 -0800177 /**
178 * Constructs a new NetworkManagementService instance
179 *
180 * @param context Binder context for this service
181 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700182 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800183 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800184
Marco Nelissen62dbb222010-02-18 10:56:30 -0800185 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
186 return;
187 }
188
San Mehat873f2142010-01-14 10:25:07 -0800189 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700190 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700191 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700192
193 // Add ourself to the Watchdog monitors.
194 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700195 }
196
197 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700198 final NetworkManagementService service = new NetworkManagementService(context);
199 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700200 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
201 service.mThread.start();
202 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700203 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700204 if (DBG) Slog.d(TAG, "Connected");
205 return service;
San Mehat873f2142010-01-14 10:25:07 -0800206 }
207
Jeff Sharkey350083e2011-06-29 10:45:16 -0700208 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700209 prepareNativeDaemon();
210 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700211 }
212
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800213 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800214 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800215 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800216 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800217 }
218
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800219 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800220 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800221 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800222 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800223 }
224
225 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700226 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800227 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700228 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800229 final int length = mObservers.beginBroadcast();
230 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800231 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800232 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
233 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700234 }
235 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700237 }
238
239 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700240 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700241 * (typically, an Ethernet cable has been plugged-in or unplugged).
242 */
243 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800244 final int length = mObservers.beginBroadcast();
245 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700246 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800247 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
248 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800249 }
250 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800251 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800252 }
253
254 /**
255 * Notify our observers of an interface addition.
256 */
257 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800258 final int length = mObservers.beginBroadcast();
259 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800260 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800261 mObservers.getBroadcastItem(i).interfaceAdded(iface);
262 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800263 }
264 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800265 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800266 }
267
268 /**
269 * Notify our observers of an interface removal.
270 */
271 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700272 // netd already clears out quota and alerts for removed ifaces; update
273 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700274 mActiveAlerts.remove(iface);
275 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700276
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 final int length = mObservers.beginBroadcast();
278 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800279 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800280 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
281 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800282 }
283 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800284 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800285 }
286
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700287 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700288 * Notify our observers of a limit reached.
289 */
290 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800291 final int length = mObservers.beginBroadcast();
292 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700293 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800294 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
295 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700296 }
297 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800298 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700299 }
300
301 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700302 * Notify our observers of a change in the data activity state of the interface
303 */
304 private void notifyInterfaceClassActivity(String label, boolean active) {
305 final int length = mObservers.beginBroadcast();
306 for (int i = 0; i < length; i++) {
307 try {
308 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
309 } catch (RemoteException e) {
310 }
311 }
312 mObservers.finishBroadcast();
313 }
314
315 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700316 * Prepare native daemon once connected, enabling modules and pushing any
317 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700318 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700319 private void prepareNativeDaemon() {
320 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700321
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700322 // only enable bandwidth control when support exists
323 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
324 if (hasKernelSupport) {
325 Slog.d(TAG, "enabling bandwidth control");
326 try {
327 mConnector.execute("bandwidth", "enable");
328 mBandwidthControlEnabled = true;
329 } catch (NativeDaemonConnectorException e) {
330 Log.wtf(TAG, "problem enabling bandwidth controls", e);
331 }
332 } else {
333 Slog.d(TAG, "not enabling bandwidth control");
334 }
335
336 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
337
338 // push any existing quota or UID rules
339 synchronized (mQuotaLock) {
340 int size = mActiveQuotas.size();
341 if (size > 0) {
342 Slog.d(TAG, "pushing " + size + " active quota rules");
343 final HashMap<String, Long> activeQuotas = mActiveQuotas;
344 mActiveQuotas = Maps.newHashMap();
345 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
346 setInterfaceQuota(entry.getKey(), entry.getValue());
347 }
348 }
349
350 size = mActiveAlerts.size();
351 if (size > 0) {
352 Slog.d(TAG, "pushing " + size + " active alert rules");
353 final HashMap<String, Long> activeAlerts = mActiveAlerts;
354 mActiveAlerts = Maps.newHashMap();
355 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
356 setInterfaceAlert(entry.getKey(), entry.getValue());
357 }
358 }
359
360 size = mUidRejectOnQuota.size();
361 if (size > 0) {
362 Slog.d(TAG, "pushing " + size + " active uid rules");
363 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
364 mUidRejectOnQuota = new SparseBooleanArray();
365 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
366 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
367 }
368 }
369 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700370
371 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700372 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700373 }
San Mehat4d02d002010-01-22 16:07:46 -0800374
San Mehat873f2142010-01-14 10:25:07 -0800375 //
376 // Netd Callback handling
377 //
378
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700379 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
380 @Override
San Mehat873f2142010-01-14 10:25:07 -0800381 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700382 // event is dispatched from internal NDC thread, so we prepare the
383 // daemon back on main thread.
384 if (mConnectedSignal != null) {
385 mConnectedSignal.countDown();
386 mConnectedSignal = null;
387 } else {
388 mMainHandler.post(new Runnable() {
389 @Override
390 public void run() {
391 prepareNativeDaemon();
392 }
393 });
394 }
San Mehat873f2142010-01-14 10:25:07 -0800395 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700396
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700397 @Override
San Mehat873f2142010-01-14 10:25:07 -0800398 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700399 switch (code) {
400 case NetdResponseCode.InterfaceChange:
401 /*
402 * a network interface change occured
403 * Format: "NNN Iface added <name>"
404 * "NNN Iface removed <name>"
405 * "NNN Iface changed <name> <up/down>"
406 * "NNN Iface linkstatus <name> <up/down>"
407 */
408 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
409 throw new IllegalStateException(
410 String.format("Invalid event from daemon (%s)", raw));
411 }
412 if (cooked[2].equals("added")) {
413 notifyInterfaceAdded(cooked[3]);
414 return true;
415 } else if (cooked[2].equals("removed")) {
416 notifyInterfaceRemoved(cooked[3]);
417 return true;
418 } else if (cooked[2].equals("changed") && cooked.length == 5) {
419 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
420 return true;
421 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
422 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
423 return true;
424 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800425 throw new IllegalStateException(
426 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700427 // break;
428 case NetdResponseCode.BandwidthControl:
429 /*
430 * Bandwidth control needs some attention
431 * Format: "NNN limit alert <alertName> <ifaceName>"
432 */
433 if (cooked.length < 5 || !cooked[1].equals("limit")) {
434 throw new IllegalStateException(
435 String.format("Invalid event from daemon (%s)", raw));
436 }
437 if (cooked[2].equals("alert")) {
438 notifyLimitReached(cooked[3], cooked[4]);
439 return true;
440 }
441 throw new IllegalStateException(
442 String.format("Invalid event from daemon (%s)", raw));
443 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700444 case NetdResponseCode.InterfaceClassActivity:
445 /*
446 * An network interface class state changed (active/idle)
447 * Format: "NNN IfaceClass <active/idle> <label>"
448 */
449 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
450 throw new IllegalStateException(
451 String.format("Invalid event from daemon (%s)", raw));
452 }
453 boolean isActive = cooked[2].equals("active");
454 notifyInterfaceClassActivity(cooked[3], isActive);
455 return true;
456 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700457 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800458 }
459 return false;
San Mehat873f2142010-01-14 10:25:07 -0800460 }
461 }
462
San Mehated4fc8a2010-01-22 12:28:36 -0800463
San Mehat873f2142010-01-14 10:25:07 -0800464 //
465 // INetworkManagementService members
466 //
467
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800468 @Override
469 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800470 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700471 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800472 return NativeDaemonEvent.filterMessageList(
473 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700474 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800475 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700476 }
San Mehated4fc8a2010-01-22 12:28:36 -0800477 }
478
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800479 @Override
480 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800481 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800482
483 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700484 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800485 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700486 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800487 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700488 }
San Mehated4fc8a2010-01-22 12:28:36 -0800489
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800490 event.checkCode(InterfaceGetCfgResult);
491
492 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
493 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800494
Kenny Roota80ce062010-06-01 13:23:53 -0700495 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800496 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700497 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800498 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800499 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800500 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700501 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800502 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800503 } catch (IllegalArgumentException iae) {
504 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700505 }
506
507 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800508 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800509 } catch (NumberFormatException nfe) {
510 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700511 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800512
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800513 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
514 while (st.hasMoreTokens()) {
515 cfg.setFlag(st.nextToken());
516 }
Kenny Roota80ce062010-06-01 13:23:53 -0700517 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800518 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800519 }
San Mehated4fc8a2010-01-22 12:28:36 -0800520 return cfg;
521 }
522
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800523 @Override
524 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800525 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800526 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800527 if (linkAddr == null || linkAddr.getAddress() == null) {
528 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800529 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800530
531 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800532 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800533 linkAddr.getNetworkPrefixLength());
534 for (String flag : cfg.getFlags()) {
535 cmd.appendArg(flag);
536 }
537
Kenny Roota80ce062010-06-01 13:23:53 -0700538 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800539 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700540 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800541 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700542 }
San Mehat873f2142010-01-14 10:25:07 -0800543 }
544
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800545 @Override
546 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800547 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800548 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800549 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800550 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700551 }
552
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800553 @Override
554 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800555 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800556 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800557 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800558 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700559 }
560
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800561 @Override
562 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800563 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700564 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800565 mConnector.execute(
566 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700567 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800568 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700569 }
570 }
571
Irfan Sherifff5600612011-06-16 10:26:28 -0700572 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
573 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800574 @Override
575 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800576 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700577 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800578 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700579 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800580 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700581 }
582 }
583
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800584 @Override
585 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800586 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700587 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800588 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700589 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800590 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700591 }
592 }
593
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800594 @Override
595 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800596 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700597 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800598 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700599 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800600 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700601 }
602 }
603
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800604 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700605 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800606 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700607 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700608 }
609
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800610 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700611 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800612 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700613 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700614 }
615
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800616 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700617 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800618 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700619 modifyRoute(interfaceName, ADD, route, SECONDARY);
620 }
621
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800622 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700623 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800624 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700625 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
626 }
627
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800628 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
629 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700630
631 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800632 final LinkAddress la = route.getDestination();
633 cmd.appendArg(la.getAddress().getHostAddress());
634 cmd.appendArg(la.getNetworkPrefixLength());
635
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700636 if (route.getGateway() == null) {
637 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800638 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700639 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800640 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700641 }
642 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800643 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700644 }
645
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800646 try {
647 mConnector.execute(cmd);
648 } catch (NativeDaemonConnectorException e) {
649 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700650 }
651 }
652
653 private ArrayList<String> readRouteList(String filename) {
654 FileInputStream fstream = null;
655 ArrayList<String> list = new ArrayList<String>();
656
657 try {
658 fstream = new FileInputStream(filename);
659 DataInputStream in = new DataInputStream(fstream);
660 BufferedReader br = new BufferedReader(new InputStreamReader(in));
661 String s;
662
663 // throw away the title line
664
665 while (((s = br.readLine()) != null) && (s.length() != 0)) {
666 list.add(s);
667 }
668 } catch (IOException ex) {
669 // return current list, possibly empty
670 } finally {
671 if (fstream != null) {
672 try {
673 fstream.close();
674 } catch (IOException ex) {}
675 }
676 }
677
678 return list;
679 }
680
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800681 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700682 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700684 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
685
686 // v4 routes listed as:
687 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
688 for (String s : readRouteList("/proc/net/route")) {
689 String[] fields = s.split("\t");
690
691 if (fields.length > 7) {
692 String iface = fields[0];
693
694 if (interfaceName.equals(iface)) {
695 String dest = fields[1];
696 String gate = fields[2];
697 String flags = fields[3]; // future use?
698 String mask = fields[7];
699 try {
700 // address stored as a hex string, ex: 0014A8C0
701 InetAddress destAddr =
702 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
703 int prefixLength =
704 NetworkUtils.netmaskIntToPrefixLength(
705 (int)Long.parseLong(mask, 16));
706 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
707
708 // address stored as a hex string, ex 0014A8C0
709 InetAddress gatewayAddr =
710 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
711
712 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
713 routes.add(route);
714 } catch (Exception e) {
715 Log.e(TAG, "Error parsing route " + s + " : " + e);
716 continue;
717 }
718 }
719 }
720 }
721
722 // v6 routes listed as:
723 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
724 for (String s : readRouteList("/proc/net/ipv6_route")) {
725 String[]fields = s.split("\\s+");
726 if (fields.length > 9) {
727 String iface = fields[9].trim();
728 if (interfaceName.equals(iface)) {
729 String dest = fields[0];
730 String prefix = fields[1];
731 String gate = fields[4];
732
733 try {
734 // prefix length stored as a hex string, ex 40
735 int prefixLength = Integer.parseInt(prefix, 16);
736
737 // address stored as a 32 char hex string
738 // ex fe800000000000000000000000000000
739 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
740 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
741
742 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
743
744 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
745 routes.add(route);
746 } catch (Exception e) {
747 Log.e(TAG, "Error parsing route " + s + " : " + e);
748 continue;
749 }
750 }
751 }
752 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800753 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700754 }
755
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800756 @Override
San Mehat873f2142010-01-14 10:25:07 -0800757 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800758 // TODO: remove from aidl if nobody calls externally
759 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800760
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800762 }
763
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800764 @Override
San Mehat873f2142010-01-14 10:25:07 -0800765 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800766 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800767
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800768 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700769 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800770 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700771 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800772 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700773 }
San Mehat873f2142010-01-14 10:25:07 -0800774
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800775 // 211 Forwarding enabled
776 event.checkCode(IpFwdStatusResult);
777 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800778 }
779
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800780 @Override
781 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800782 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800783 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800784 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800785 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800786 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800787 }
San Mehat873f2142010-01-14 10:25:07 -0800788 }
789
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800790 @Override
791 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800792 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700793 // cmd is "tether start first_start first_stop second_start second_stop ..."
794 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800795
796 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700797 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800798 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700799 }
Kenny Roota80ce062010-06-01 13:23:53 -0700800
801 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800802 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700803 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800804 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700805 }
San Mehat873f2142010-01-14 10:25:07 -0800806 }
807
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800808 @Override
809 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800810 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700811 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800812 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700813 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800814 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700815 }
San Mehat873f2142010-01-14 10:25:07 -0800816 }
817
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800818 @Override
819 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800820 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800821
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800822 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700823 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800824 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700825 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800826 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700827 }
San Mehat873f2142010-01-14 10:25:07 -0800828
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800829 // 210 Tethering services started
830 event.checkCode(TetherStatusResult);
831 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800832 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700833
834 // TODO(BT) Remove
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800835 @Override
836 public void startReverseTethering(String iface) {
Matthew Xiefe19f122012-07-12 16:03:32 -0700837 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700838 // cmd is "tether start first_start first_stop second_start second_stop ..."
839 // an odd number of addrs will fail
fredc0f420372012-04-12 00:02:00 -0700840 try {
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800841 mConnector.execute("tether", "start-reverse", iface);
fredc0f420372012-04-12 00:02:00 -0700842 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800843 throw e.rethrowAsParcelableException();
fredc0f420372012-04-12 00:02:00 -0700844 }
845 BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
846
847 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700848
849 // TODO(BT) Remove
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800850 @Override
851 public void stopReverseTethering() {
Matthew Xiefe19f122012-07-12 16:03:32 -0700852 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700853 try {
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800854 mConnector.execute("tether", "stop-reverse");
fredc0f420372012-04-12 00:02:00 -0700855 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey7b4596f2013-02-25 10:55:29 -0800856 throw e.rethrowAsParcelableException();
fredc0f420372012-04-12 00:02:00 -0700857 }
858 BluetoothTetheringDataTracker.getInstance().stopReverseTether();
859 }
San Mehat873f2142010-01-14 10:25:07 -0800860
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800861 @Override
862 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800863 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700864 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800865 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700866 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800867 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700868 }
San Mehat873f2142010-01-14 10:25:07 -0800869 }
870
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800871 @Override
San Mehat873f2142010-01-14 10:25:07 -0800872 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800873 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700874 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800875 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700876 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800877 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700878 }
San Mehat873f2142010-01-14 10:25:07 -0800879 }
880
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800881 @Override
882 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800883 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700884 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800885 return NativeDaemonEvent.filterMessageList(
886 mConnector.executeForList("tether", "interface", "list"),
887 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700888 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800889 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700890 }
San Mehat873f2142010-01-14 10:25:07 -0800891 }
892
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800893 @Override
894 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800895 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800896
897 final Command cmd = new Command("tether", "dns", "set");
898 for (String s : dns) {
899 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
900 }
901
San Mehat873f2142010-01-14 10:25:07 -0800902 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 mConnector.execute(cmd);
904 } catch (NativeDaemonConnectorException e) {
905 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800906 }
907 }
908
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800909 @Override
910 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800911 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700912 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800913 return NativeDaemonEvent.filterMessageList(
914 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700915 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800916 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700917 }
San Mehat873f2142010-01-14 10:25:07 -0800918 }
919
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800920 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700921 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800922 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700923
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800924 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
925 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800926 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800927 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800928 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800929 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
930 .getInterfaceAddresses();
931 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800932 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800933 InetAddress addr = NetworkUtils.getNetworkPart(
934 ia.getAddress(), ia.getNetworkPrefixLength());
935 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800936 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700937 }
938
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800939 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800940 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800941 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800942 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800943 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700944 }
945
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800946 @Override
947 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800948 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700949 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700950 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800951 } catch (SocketException e) {
952 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 }
San Mehat873f2142010-01-14 10:25:07 -0800954 }
955
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800956 @Override
957 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800958 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700959 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700960 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800961 } catch (SocketException e) {
962 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700963 }
San Mehat873f2142010-01-14 10:25:07 -0800964 }
San Mehat72759df2010-01-19 13:50:37 -0800965
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800966 @Override
967 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800968 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700969 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800970 return NativeDaemonEvent.filterMessageList(
971 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700972 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800973 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700974 }
San Mehat72759df2010-01-19 13:50:37 -0800975 }
976
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800977 @Override
978 public void attachPppd(
979 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800980 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800981 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800982 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800983 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
984 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
985 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700987 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800988 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800989 }
990 }
991
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800992 @Override
993 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800994 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700995 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800996 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700997 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800998 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700999 }
San Mehat72759df2010-01-19 13:50:37 -08001000 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001001
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001002 @Override
1003 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001004 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001005 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001006 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001007 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001008 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001009 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001010 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001011 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001012 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001013 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001014 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001015 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001016 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001017 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001018 }
1019
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001020 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001021 switch (wifiConfig.getAuthType()) {
1022 case KeyMgmt.WPA_PSK:
1023 return "wpa-psk";
1024 case KeyMgmt.WPA2_PSK:
1025 return "wpa2-psk";
1026 default:
1027 return "open";
1028 }
1029 }
1030
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001031 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001032 @Override
1033 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001034 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001035 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001036 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001037 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001038 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001039 }
1040 }
1041
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001042 @Override
1043 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001044 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001045 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001046 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001047 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001048 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001049 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001050 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001051 }
1052
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001053 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001054 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001055 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001056 try {
1057 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001058 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001059 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001060 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001061 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001062 }
1063 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001064 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001065 }
1066 }
San Mehat91cac642010-03-31 14:31:36 -07001067
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001068 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001069 public void addIdleTimer(String iface, int timeout, String label) {
1070 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1071
1072 if (DBG) Slog.d(TAG, "Adding idletimer");
1073
1074 synchronized (mIdleTimerLock) {
1075 IdleTimerParams params = mActiveIdleTimers.get(iface);
1076 if (params != null) {
1077 // the interface already has idletimer, update network count
1078 params.networkCount++;
1079 return;
1080 }
1081
1082 try {
1083 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1084 } catch (NativeDaemonConnectorException e) {
1085 throw e.rethrowAsParcelableException();
1086 }
1087 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1088 }
1089 }
1090
1091 @Override
1092 public void removeIdleTimer(String iface) {
1093 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1094
1095 if (DBG) Slog.d(TAG, "Removing idletimer");
1096
1097 synchronized (mIdleTimerLock) {
1098 IdleTimerParams params = mActiveIdleTimers.get(iface);
1099 if (params == null || --(params.networkCount) > 0) {
1100 return;
1101 }
1102
1103 try {
1104 mConnector.execute("idletimer", "remove", iface,
1105 Integer.toString(params.timeout), params.label);
1106 } catch (NativeDaemonConnectorException e) {
1107 throw e.rethrowAsParcelableException();
1108 }
1109 mActiveIdleTimers.remove(iface);
1110 }
1111 }
1112
1113 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001114 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001115 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001116 try {
1117 return mStatsFactory.readNetworkStatsSummaryDev();
1118 } catch (IOException e) {
1119 throw new IllegalStateException(e);
1120 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001121 }
1122
1123 @Override
1124 public NetworkStats getNetworkStatsSummaryXt() {
1125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001126 try {
1127 return mStatsFactory.readNetworkStatsSummaryXt();
1128 } catch (IOException e) {
1129 throw new IllegalStateException(e);
1130 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001131 }
1132
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001133 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001134 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001135 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001136 try {
1137 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1138 } catch (IOException e) {
1139 throw new IllegalStateException(e);
1140 }
San Mehat91cac642010-03-31 14:31:36 -07001141 }
1142
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001143 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001144 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001145 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001146
Jeff Sharkey350083e2011-06-29 10:45:16 -07001147 // silently discard when control disabled
1148 // TODO: eventually migrate to be always enabled
1149 if (!mBandwidthControlEnabled) return;
1150
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001151 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001152 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001153 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001154 }
1155
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001156 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001157 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001158 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001159 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001160 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001161 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001162 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001163 }
1164 }
1165
1166 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001167 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001168 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001169
Jeff Sharkey350083e2011-06-29 10:45:16 -07001170 // silently discard when control disabled
1171 // TODO: eventually migrate to be always enabled
1172 if (!mBandwidthControlEnabled) return;
1173
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001174 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001175 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001176 // TODO: eventually consider throwing
1177 return;
1178 }
1179
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001180 mActiveQuotas.remove(iface);
1181 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001182
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001183 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001184 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001185 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001186 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001187 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001188 }
1189 }
1190 }
1191
1192 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001193 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001194 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001195
1196 // silently discard when control disabled
1197 // TODO: eventually migrate to be always enabled
1198 if (!mBandwidthControlEnabled) return;
1199
1200 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001201 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001202 throw new IllegalStateException("setting alert requires existing quota on iface");
1203 }
1204
1205 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001206 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001207 throw new IllegalStateException("iface " + iface + " already has alert");
1208 }
1209
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001210 try {
1211 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001212 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001213 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001214 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001215 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001216 }
1217 }
1218 }
1219
1220 @Override
1221 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001223
1224 // silently discard when control disabled
1225 // TODO: eventually migrate to be always enabled
1226 if (!mBandwidthControlEnabled) return;
1227
1228 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001229 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001230 // TODO: eventually consider throwing
1231 return;
1232 }
1233
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001234 try {
1235 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001236 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001237 mActiveAlerts.remove(iface);
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
1244 @Override
1245 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001246 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001247
1248 // silently discard when control disabled
1249 // TODO: eventually migrate to be always enabled
1250 if (!mBandwidthControlEnabled) return;
1251
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001252 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001253 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001254 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001255 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001256 }
1257 }
1258
1259 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001260 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001261 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001262
Jeff Sharkey350083e2011-06-29 10:45:16 -07001263 // silently discard when control disabled
1264 // TODO: eventually migrate to be always enabled
1265 if (!mBandwidthControlEnabled) return;
1266
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001267 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001268 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1269 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1270 // TODO: eventually consider throwing
1271 return;
1272 }
1273
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001274 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001275 mConnector.execute("bandwidth",
1276 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001277 if (rejectOnQuotaInterfaces) {
1278 mUidRejectOnQuota.put(uid, true);
1279 } else {
1280 mUidRejectOnQuota.delete(uid);
1281 }
1282 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001283 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001284 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001285 }
1286 }
1287
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001288 @Override
1289 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001290 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001291 return mBandwidthControlEnabled;
1292 }
1293
1294 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001295 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001296 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001297 try {
1298 return mStatsFactory.readNetworkStatsDetail(uid);
1299 } catch (IOException e) {
1300 throw new IllegalStateException(e);
1301 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001302 }
1303
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001304 @Override
1305 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001306 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001307
1308 if (ifacePairs.length % 2 != 0) {
1309 throw new IllegalArgumentException(
1310 "unexpected ifacePairs; length=" + ifacePairs.length);
1311 }
1312
1313 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1314 for (int i = 0; i < ifacePairs.length; i += 2) {
1315 final String ifaceIn = ifacePairs[i];
1316 final String ifaceOut = ifacePairs[i + 1];
1317 if (ifaceIn != null && ifaceOut != null) {
1318 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1319 }
1320 }
1321 return stats;
1322 }
1323
1324 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001325 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001326 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001327 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001328 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001329 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001330 }
1331
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001332 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001333
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001334 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1335 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1336 tok.nextToken();
1337 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001338
1339 try {
1340 final NetworkStats.Entry entry = new NetworkStats.Entry();
1341 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001342 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001343 entry.set = SET_DEFAULT;
1344 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001345 entry.rxBytes = Long.parseLong(tok.nextToken());
1346 entry.rxPackets = Long.parseLong(tok.nextToken());
1347 entry.txBytes = Long.parseLong(tok.nextToken());
1348 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001349 return entry;
1350 } catch (NumberFormatException e) {
1351 throw new IllegalStateException(
1352 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1353 }
1354 }
1355
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001356 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001357 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001358 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001359 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001360 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001361 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001362 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001363 }
1364 }
1365
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001366 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001367 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001368 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001369
Robert Greenwalt8058f622012-11-09 10:52:27 -08001370 final Command cmd = new Command("resolver", "setifdns", iface,
1371 (domains == null ? "" : domains));
1372
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001373 for (String s : servers) {
1374 InetAddress a = NetworkUtils.numericToInetAddress(s);
1375 if (a.isAnyLocalAddress() == false) {
1376 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001377 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001378 }
1379
1380 try {
1381 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001382 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001383 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001384 }
1385 }
1386
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001387 @Override
1388 public void flushDefaultDnsCache() {
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", "flushdefaultif");
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 flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001399 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001400 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001401 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001402 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001403 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001404 }
1405 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001406
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001407 @Override
1408 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001409 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001410 try {
1411 mConnector.execute("firewall", enabled ? "enable" : "disable");
1412 mFirewallEnabled = enabled;
1413 } catch (NativeDaemonConnectorException e) {
1414 throw e.rethrowAsParcelableException();
1415 }
1416 }
1417
1418 @Override
1419 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001420 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001421 return mFirewallEnabled;
1422 }
1423
1424 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001425 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001426 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001427 Preconditions.checkState(mFirewallEnabled);
1428 final String rule = allow ? ALLOW : DENY;
1429 try {
1430 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1431 } catch (NativeDaemonConnectorException e) {
1432 throw e.rethrowAsParcelableException();
1433 }
1434 }
1435
1436 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001437 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001438 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001439 Preconditions.checkState(mFirewallEnabled);
1440 final String rule = allow ? ALLOW : DENY;
1441 try {
1442 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1443 } catch (NativeDaemonConnectorException e) {
1444 throw e.rethrowAsParcelableException();
1445 }
1446 }
1447
1448 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001449 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001450 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001451 Preconditions.checkState(mFirewallEnabled);
1452 final String rule = allow ? ALLOW : DENY;
1453 try {
1454 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1455 } catch (NativeDaemonConnectorException e) {
1456 throw e.rethrowAsParcelableException();
1457 }
1458 }
1459
1460 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001461 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001462 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001463 Preconditions.checkState(mFirewallEnabled);
1464 final String rule = allow ? ALLOW : DENY;
1465 try {
1466 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1467 } catch (NativeDaemonConnectorException e) {
1468 throw e.rethrowAsParcelableException();
1469 }
1470 }
1471
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001472 private static void enforceSystemUid() {
1473 final int uid = Binder.getCallingUid();
1474 if (uid != Process.SYSTEM_UID) {
1475 throw new SecurityException("Only available to AID_SYSTEM");
1476 }
1477 }
1478
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001479 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001480 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1481 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1482 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001483 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001484 } catch (NativeDaemonConnectorException e) {
1485 throw new IllegalStateException(
1486 "Error communicating with native deamon to set interface for pid" + iface, e);
1487 }
1488 }
1489
1490 @Override
1491 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1492 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1493 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001494 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001495 } catch (NativeDaemonConnectorException e) {
1496 throw new IllegalStateException(
1497 "Error communicating with native deamon to clear interface for pid " + pid, e);
1498 }
1499 }
1500
1501 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001502 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001503 public void monitor() {
1504 if (mConnector != null) {
1505 mConnector.monitor();
1506 }
1507 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001508
1509 @Override
1510 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1511 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1512
Robert Greenwalt470fd722012-01-18 12:51:15 -08001513 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1514 mConnector.dump(fd, pw, args);
1515 pw.println();
1516
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001517 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1518
1519 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001520 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1521 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001522 }
1523
1524 synchronized (mUidRejectOnQuota) {
1525 pw.print("UID reject on quota ifaces: [");
1526 final int size = mUidRejectOnQuota.size();
1527 for (int i = 0; i < size; i++) {
1528 pw.print(mUidRejectOnQuota.keyAt(i));
1529 if (i < size - 1) pw.print(",");
1530 }
1531 pw.println("]");
1532 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001533
1534 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001535 }
San Mehat873f2142010-01-14 10:25:07 -08001536}