blob: 2b0d8243a7eec63e7879b236bec167ed95e0c174 [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 Sharkeyf56e2432012-09-06 17:54:29 -070048import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070049import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080050import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080052import android.os.RemoteCallbackList;
53import android.os.RemoteException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070054import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080055import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080056import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070058import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080059
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070060import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070061import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080062import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070063import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070064import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070065
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070066import java.io.BufferedReader;
67import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080068import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070069import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070070import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070071import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070072import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070073import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070074import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070075import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070076import java.net.InterfaceAddress;
77import java.net.NetworkInterface;
78import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070079import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070080import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070081import java.util.HashMap;
82import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070083import java.util.NoSuchElementException;
84import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070085import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080086
87/**
88 * @hide
89 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070090public class NetworkManagementService extends INetworkManagementService.Stub
91 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070092 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070093 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070094 private static final String NETD_TAG = "NetdConnector";
95
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080096 private static final String ADD = "add";
97 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070098
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070099 private static final String ALLOW = "allow";
100 private static final String DENY = "deny";
101
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700102 private static final String DEFAULT = "default";
103 private static final String SECONDARY = "secondary";
104
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700105 /**
106 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
107 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
108 */
109 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
110
San Mehat873f2142010-01-14 10:25:07 -0800111 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700112 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800113 public static final int InterfaceListResult = 110;
114 public static final int TetherInterfaceListResult = 111;
115 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800116 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800117
118 public static final int TetherStatusResult = 210;
119 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800120 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800121 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700122 public static final int InterfaceRxCounterResult = 216;
123 public static final int InterfaceTxCounterResult = 217;
124 public static final int InterfaceRxThrottleResult = 218;
125 public static final int InterfaceTxThrottleResult = 219;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700126 public static final int QuotaCounterResult = 220;
127 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800128 public static final int DnsProxyQueryResult = 222;
Robert Greenwalte3253922010-02-18 09:23:25 -0800129
130 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700131 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700132 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800133 }
134
135 /**
136 * Binder context for this service
137 */
138 private Context mContext;
139
140 /**
141 * connector object for communicating with netd
142 */
143 private NativeDaemonConnector mConnector;
144
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700145 private final Handler mMainHandler = new Handler();
146
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700147 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700148 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700149
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800150 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
151 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800152
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700153 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
154
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700155 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700156 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700157 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700158 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700159 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700160 /** Set of UIDs with active reject rules. */
161 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
162
Haoyu Bai04124232012-06-28 15:26:19 -0700163 private Object mIdleTimerLock = new Object();
164 /** Set of interfaces with active idle timers. */
165 private static class IdleTimerParams {
166 public final int timeout;
167 public final String label;
168 public int networkCount;
169
170 IdleTimerParams(int timeout, String label) {
171 this.timeout = timeout;
172 this.label = label;
173 this.networkCount = 1;
174 }
175 }
176 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
177
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700178 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700179 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700180
San Mehat873f2142010-01-14 10:25:07 -0800181 /**
182 * Constructs a new NetworkManagementService instance
183 *
184 * @param context Binder context for this service
185 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700186 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800187 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800188
Marco Nelissen62dbb222010-02-18 10:56:30 -0800189 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
190 return;
191 }
192
San Mehat873f2142010-01-14 10:25:07 -0800193 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700194 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700195 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700196
197 // Add ourself to the Watchdog monitors.
198 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700199 }
200
201 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700202 final NetworkManagementService service = new NetworkManagementService(context);
203 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700204 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
205 service.mThread.start();
206 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700207 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700208 if (DBG) Slog.d(TAG, "Connected");
209 return service;
San Mehat873f2142010-01-14 10:25:07 -0800210 }
211
Jeff Sharkey350083e2011-06-29 10:45:16 -0700212 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700213 prepareNativeDaemon();
214 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700215 }
216
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800217 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800218 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800219 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800220 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800221 }
222
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800223 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800224 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800225 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800226 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800227 }
228
229 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700230 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800231 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700232 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800233 final int length = mObservers.beginBroadcast();
234 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800235 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
237 } catch (RemoteException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700238 }
239 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800240 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700241 }
242
243 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700244 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700245 * (typically, an Ethernet cable has been plugged-in or unplugged).
246 */
247 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800248 final int length = mObservers.beginBroadcast();
249 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700250 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800251 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
252 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800253 }
254 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800255 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800256 }
257
258 /**
259 * Notify our observers of an interface addition.
260 */
261 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800262 final int length = mObservers.beginBroadcast();
263 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800264 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800265 mObservers.getBroadcastItem(i).interfaceAdded(iface);
266 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800267 }
268 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800269 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800270 }
271
272 /**
273 * Notify our observers of an interface removal.
274 */
275 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700276 // netd already clears out quota and alerts for removed ifaces; update
277 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700278 mActiveAlerts.remove(iface);
279 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700280
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800281 final int length = mObservers.beginBroadcast();
282 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800283 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800284 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
285 } catch (RemoteException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800286 }
287 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800288 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800289 }
290
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700291 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700292 * Notify our observers of a limit reached.
293 */
294 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 final int length = mObservers.beginBroadcast();
296 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700297 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800298 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
299 } catch (RemoteException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700300 }
301 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800302 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700303 }
304
305 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700306 * Notify our observers of a change in the data activity state of the interface
307 */
308 private void notifyInterfaceClassActivity(String label, boolean active) {
309 final int length = mObservers.beginBroadcast();
310 for (int i = 0; i < length; i++) {
311 try {
312 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
313 } catch (RemoteException e) {
314 }
315 }
316 mObservers.finishBroadcast();
317 }
318
319 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700320 * Prepare native daemon once connected, enabling modules and pushing any
321 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700322 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700323 private void prepareNativeDaemon() {
324 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700325
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700326 // only enable bandwidth control when support exists
327 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
328 if (hasKernelSupport) {
329 Slog.d(TAG, "enabling bandwidth control");
330 try {
331 mConnector.execute("bandwidth", "enable");
332 mBandwidthControlEnabled = true;
333 } catch (NativeDaemonConnectorException e) {
334 Log.wtf(TAG, "problem enabling bandwidth controls", e);
335 }
336 } else {
337 Slog.d(TAG, "not enabling bandwidth control");
338 }
339
340 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
341
342 // push any existing quota or UID rules
343 synchronized (mQuotaLock) {
344 int size = mActiveQuotas.size();
345 if (size > 0) {
346 Slog.d(TAG, "pushing " + size + " active quota rules");
347 final HashMap<String, Long> activeQuotas = mActiveQuotas;
348 mActiveQuotas = Maps.newHashMap();
349 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
350 setInterfaceQuota(entry.getKey(), entry.getValue());
351 }
352 }
353
354 size = mActiveAlerts.size();
355 if (size > 0) {
356 Slog.d(TAG, "pushing " + size + " active alert rules");
357 final HashMap<String, Long> activeAlerts = mActiveAlerts;
358 mActiveAlerts = Maps.newHashMap();
359 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
360 setInterfaceAlert(entry.getKey(), entry.getValue());
361 }
362 }
363
364 size = mUidRejectOnQuota.size();
365 if (size > 0) {
366 Slog.d(TAG, "pushing " + size + " active uid rules");
367 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
368 mUidRejectOnQuota = new SparseBooleanArray();
369 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
370 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
371 }
372 }
373 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700374
375 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700376 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700377 }
San Mehat4d02d002010-01-22 16:07:46 -0800378
San Mehat873f2142010-01-14 10:25:07 -0800379 //
380 // Netd Callback handling
381 //
382
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700383 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
384 @Override
San Mehat873f2142010-01-14 10:25:07 -0800385 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700386 // event is dispatched from internal NDC thread, so we prepare the
387 // daemon back on main thread.
388 if (mConnectedSignal != null) {
389 mConnectedSignal.countDown();
390 mConnectedSignal = null;
391 } else {
392 mMainHandler.post(new Runnable() {
393 @Override
394 public void run() {
395 prepareNativeDaemon();
396 }
397 });
398 }
San Mehat873f2142010-01-14 10:25:07 -0800399 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700400
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700401 @Override
San Mehat873f2142010-01-14 10:25:07 -0800402 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700403 switch (code) {
404 case NetdResponseCode.InterfaceChange:
405 /*
406 * a network interface change occured
407 * Format: "NNN Iface added <name>"
408 * "NNN Iface removed <name>"
409 * "NNN Iface changed <name> <up/down>"
410 * "NNN Iface linkstatus <name> <up/down>"
411 */
412 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
413 throw new IllegalStateException(
414 String.format("Invalid event from daemon (%s)", raw));
415 }
416 if (cooked[2].equals("added")) {
417 notifyInterfaceAdded(cooked[3]);
418 return true;
419 } else if (cooked[2].equals("removed")) {
420 notifyInterfaceRemoved(cooked[3]);
421 return true;
422 } else if (cooked[2].equals("changed") && cooked.length == 5) {
423 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
424 return true;
425 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
426 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
427 return true;
428 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800429 throw new IllegalStateException(
430 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700431 // break;
432 case NetdResponseCode.BandwidthControl:
433 /*
434 * Bandwidth control needs some attention
435 * Format: "NNN limit alert <alertName> <ifaceName>"
436 */
437 if (cooked.length < 5 || !cooked[1].equals("limit")) {
438 throw new IllegalStateException(
439 String.format("Invalid event from daemon (%s)", raw));
440 }
441 if (cooked[2].equals("alert")) {
442 notifyLimitReached(cooked[3], cooked[4]);
443 return true;
444 }
445 throw new IllegalStateException(
446 String.format("Invalid event from daemon (%s)", raw));
447 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700448 case NetdResponseCode.InterfaceClassActivity:
449 /*
450 * An network interface class state changed (active/idle)
451 * Format: "NNN IfaceClass <active/idle> <label>"
452 */
453 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
454 throw new IllegalStateException(
455 String.format("Invalid event from daemon (%s)", raw));
456 }
457 boolean isActive = cooked[2].equals("active");
458 notifyInterfaceClassActivity(cooked[3], isActive);
459 return true;
460 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700461 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800462 }
463 return false;
San Mehat873f2142010-01-14 10:25:07 -0800464 }
465 }
466
San Mehated4fc8a2010-01-22 12:28:36 -0800467
San Mehat873f2142010-01-14 10:25:07 -0800468 //
469 // INetworkManagementService members
470 //
471
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800472 @Override
473 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800474 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700475 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800476 return NativeDaemonEvent.filterMessageList(
477 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700478 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800479 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700480 }
San Mehated4fc8a2010-01-22 12:28:36 -0800481 }
482
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800483 @Override
484 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800485 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800486
487 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700488 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800489 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700490 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800491 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700492 }
San Mehated4fc8a2010-01-22 12:28:36 -0800493
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800494 event.checkCode(InterfaceGetCfgResult);
495
496 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
497 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800498
Kenny Roota80ce062010-06-01 13:23:53 -0700499 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800500 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700501 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800502 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800503 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800504 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700505 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800506 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800507 } catch (IllegalArgumentException iae) {
508 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700509 }
510
511 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800512 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800513 } catch (NumberFormatException nfe) {
514 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700515 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800516
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800517 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
518 while (st.hasMoreTokens()) {
519 cfg.setFlag(st.nextToken());
520 }
Kenny Roota80ce062010-06-01 13:23:53 -0700521 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800522 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800523 }
San Mehated4fc8a2010-01-22 12:28:36 -0800524 return cfg;
525 }
526
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800527 @Override
528 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800529 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800530 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800531 if (linkAddr == null || linkAddr.getAddress() == null) {
532 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800533 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800534
535 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800536 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800537 linkAddr.getNetworkPrefixLength());
538 for (String flag : cfg.getFlags()) {
539 cmd.appendArg(flag);
540 }
541
Kenny Roota80ce062010-06-01 13:23:53 -0700542 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800543 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700544 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800545 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700546 }
San Mehat873f2142010-01-14 10:25:07 -0800547 }
548
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800549 @Override
550 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800551 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800552 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800553 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800554 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700555 }
556
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800557 @Override
558 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800559 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800560 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800561 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800562 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700563 }
564
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800565 @Override
566 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800567 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700568 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800569 mConnector.execute(
570 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700571 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800572 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700573 }
574 }
575
Irfan Sherifff5600612011-06-16 10:26:28 -0700576 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
577 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800578 @Override
579 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800580 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700581 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800582 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700583 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800584 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700585 }
586 }
587
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800588 @Override
589 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800590 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700591 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800592 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700593 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800594 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700595 }
596 }
597
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800598 @Override
599 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800600 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700601 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800602 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700603 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800604 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700605 }
606 }
607
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800608 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700609 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800610 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700611 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700612 }
613
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800614 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700615 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800616 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700617 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700618 }
619
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800620 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700621 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800622 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700623 modifyRoute(interfaceName, ADD, route, SECONDARY);
624 }
625
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800626 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700627 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800628 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700629 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
630 }
631
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800632 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
633 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700634
635 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800636 final LinkAddress la = route.getDestination();
637 cmd.appendArg(la.getAddress().getHostAddress());
638 cmd.appendArg(la.getNetworkPrefixLength());
639
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700640 if (route.getGateway() == null) {
641 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800642 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700643 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800644 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700645 }
646 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800647 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700648 }
649
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800650 try {
651 mConnector.execute(cmd);
652 } catch (NativeDaemonConnectorException e) {
653 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700654 }
655 }
656
657 private ArrayList<String> readRouteList(String filename) {
658 FileInputStream fstream = null;
659 ArrayList<String> list = new ArrayList<String>();
660
661 try {
662 fstream = new FileInputStream(filename);
663 DataInputStream in = new DataInputStream(fstream);
664 BufferedReader br = new BufferedReader(new InputStreamReader(in));
665 String s;
666
667 // throw away the title line
668
669 while (((s = br.readLine()) != null) && (s.length() != 0)) {
670 list.add(s);
671 }
672 } catch (IOException ex) {
673 // return current list, possibly empty
674 } finally {
675 if (fstream != null) {
676 try {
677 fstream.close();
678 } catch (IOException ex) {}
679 }
680 }
681
682 return list;
683 }
684
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800685 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700686 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800687 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700688 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
689
690 // v4 routes listed as:
691 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
692 for (String s : readRouteList("/proc/net/route")) {
693 String[] fields = s.split("\t");
694
695 if (fields.length > 7) {
696 String iface = fields[0];
697
698 if (interfaceName.equals(iface)) {
699 String dest = fields[1];
700 String gate = fields[2];
701 String flags = fields[3]; // future use?
702 String mask = fields[7];
703 try {
704 // address stored as a hex string, ex: 0014A8C0
705 InetAddress destAddr =
706 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
707 int prefixLength =
708 NetworkUtils.netmaskIntToPrefixLength(
709 (int)Long.parseLong(mask, 16));
710 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
711
712 // address stored as a hex string, ex 0014A8C0
713 InetAddress gatewayAddr =
714 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
715
716 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
717 routes.add(route);
718 } catch (Exception e) {
719 Log.e(TAG, "Error parsing route " + s + " : " + e);
720 continue;
721 }
722 }
723 }
724 }
725
726 // v6 routes listed as:
727 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
728 for (String s : readRouteList("/proc/net/ipv6_route")) {
729 String[]fields = s.split("\\s+");
730 if (fields.length > 9) {
731 String iface = fields[9].trim();
732 if (interfaceName.equals(iface)) {
733 String dest = fields[0];
734 String prefix = fields[1];
735 String gate = fields[4];
736
737 try {
738 // prefix length stored as a hex string, ex 40
739 int prefixLength = Integer.parseInt(prefix, 16);
740
741 // address stored as a 32 char hex string
742 // ex fe800000000000000000000000000000
743 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
744 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
745
746 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
747
748 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
749 routes.add(route);
750 } catch (Exception e) {
751 Log.e(TAG, "Error parsing route " + s + " : " + e);
752 continue;
753 }
754 }
755 }
756 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800757 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700758 }
759
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800760 @Override
San Mehat873f2142010-01-14 10:25:07 -0800761 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800762 // TODO: remove from aidl if nobody calls externally
763 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800764
Joe Onorato8a9b2202010-02-26 18:56:32 -0800765 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800766 }
767
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800768 @Override
San Mehat873f2142010-01-14 10:25:07 -0800769 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800770 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800771
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800772 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700773 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800774 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700775 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800776 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700777 }
San Mehat873f2142010-01-14 10:25:07 -0800778
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800779 // 211 Forwarding enabled
780 event.checkCode(IpFwdStatusResult);
781 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800782 }
783
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800784 @Override
785 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800786 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800787 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800788 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800789 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800790 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800791 }
San Mehat873f2142010-01-14 10:25:07 -0800792 }
793
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800794 @Override
795 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800796 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700797 // cmd is "tether start first_start first_stop second_start second_stop ..."
798 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800799
800 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700801 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800802 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700803 }
Kenny Roota80ce062010-06-01 13:23:53 -0700804
805 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800806 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700807 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800808 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700809 }
San Mehat873f2142010-01-14 10:25:07 -0800810 }
811
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800812 @Override
813 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800814 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700815 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800816 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700817 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800818 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700819 }
San Mehat873f2142010-01-14 10:25:07 -0800820 }
821
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800822 @Override
823 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800824 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800825
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800826 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700827 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800828 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700829 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800830 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700831 }
San Mehat873f2142010-01-14 10:25:07 -0800832
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800833 // 210 Tethering services started
834 event.checkCode(TetherStatusResult);
835 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800836 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700837
838 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700839 public void startReverseTethering(String iface)
840 throws IllegalStateException {
841 if (DBG) Slog.d(TAG, "startReverseTethering in");
Matthew Xiefe19f122012-07-12 16:03:32 -0700842 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700843 // cmd is "tether start first_start first_stop second_start second_stop ..."
844 // an odd number of addrs will fail
845 String cmd = "tether start-reverse";
846 cmd += " " + iface;
847 if (DBG) Slog.d(TAG, "startReverseTethering cmd: " + cmd);
848 try {
849 mConnector.doCommand(cmd);
850 } catch (NativeDaemonConnectorException e) {
851 throw new IllegalStateException("Unable to communicate to native daemon");
852 }
853 BluetoothTetheringDataTracker.getInstance().startReverseTether(iface);
854
855 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700856
857 // TODO(BT) Remove
fredc0f420372012-04-12 00:02:00 -0700858 public void stopReverseTethering() throws IllegalStateException {
Matthew Xiefe19f122012-07-12 16:03:32 -0700859 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
fredc0f420372012-04-12 00:02:00 -0700860 try {
861 mConnector.doCommand("tether stop-reverse");
862 } catch (NativeDaemonConnectorException e) {
863 throw new IllegalStateException("Unable to communicate to native daemon to stop tether");
864 }
865 BluetoothTetheringDataTracker.getInstance().stopReverseTether();
866 }
San Mehat873f2142010-01-14 10:25:07 -0800867
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800868 @Override
869 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800870 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700871 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800872 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700873 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800874 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700875 }
San Mehat873f2142010-01-14 10:25:07 -0800876 }
877
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800878 @Override
San Mehat873f2142010-01-14 10:25:07 -0800879 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800880 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700881 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800882 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700883 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800884 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700885 }
San Mehat873f2142010-01-14 10:25:07 -0800886 }
887
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800888 @Override
889 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800890 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700891 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800892 return NativeDaemonEvent.filterMessageList(
893 mConnector.executeForList("tether", "interface", "list"),
894 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700895 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800896 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700897 }
San Mehat873f2142010-01-14 10:25:07 -0800898 }
899
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800900 @Override
901 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800902 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903
904 final Command cmd = new Command("tether", "dns", "set");
905 for (String s : dns) {
906 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
907 }
908
San Mehat873f2142010-01-14 10:25:07 -0800909 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800910 mConnector.execute(cmd);
911 } catch (NativeDaemonConnectorException e) {
912 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800913 }
914 }
915
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800916 @Override
917 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800918 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700919 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800920 return NativeDaemonEvent.filterMessageList(
921 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700922 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800923 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700924 }
San Mehat873f2142010-01-14 10:25:07 -0800925 }
926
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800927 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700928 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800929 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700930
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800931 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
932 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800933 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800934 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800935 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800936 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
937 .getInterfaceAddresses();
938 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800939 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800940 InetAddress addr = NetworkUtils.getNetworkPart(
941 ia.getAddress(), ia.getNetworkPrefixLength());
942 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800943 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700944 }
945
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800946 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800947 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800948 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800949 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800950 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700951 }
952
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800953 @Override
954 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800955 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700956 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700957 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800958 } catch (SocketException e) {
959 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700960 }
San Mehat873f2142010-01-14 10:25:07 -0800961 }
962
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800963 @Override
964 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800965 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700966 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700967 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800968 } catch (SocketException e) {
969 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700970 }
San Mehat873f2142010-01-14 10:25:07 -0800971 }
San Mehat72759df2010-01-19 13:50:37 -0800972
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800973 @Override
974 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800975 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700976 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977 return NativeDaemonEvent.filterMessageList(
978 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700979 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800980 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700981 }
San Mehat72759df2010-01-19 13:50:37 -0800982 }
983
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800984 @Override
985 public void attachPppd(
986 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800987 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800988 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800989 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800990 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
991 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
992 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800993 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700994 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800995 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800996 }
997 }
998
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800999 @Override
1000 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001001 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001002 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001003 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001004 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001005 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001006 }
San Mehat72759df2010-01-19 13:50:37 -08001007 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001008
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001009 @Override
1010 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001011 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001012 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001013 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001014 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001015 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001016 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001017 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001018 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001019 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001020 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001021 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001022 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001023 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001024 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001025 }
1026
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001028 switch (wifiConfig.getAuthType()) {
1029 case KeyMgmt.WPA_PSK:
1030 return "wpa-psk";
1031 case KeyMgmt.WPA2_PSK:
1032 return "wpa2-psk";
1033 default:
1034 return "open";
1035 }
1036 }
1037
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001038 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001039 @Override
1040 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001041 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001042 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001043 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001044 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001045 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001046 }
1047 }
1048
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001049 @Override
1050 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001052 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001054 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001055 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001056 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001057 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001058 }
1059
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001060 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001061 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001062 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001063 try {
1064 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001065 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001066 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001067 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001068 getSecurityType(wifiConfig), wifiConfig.preSharedKey);
Kenny Roota80ce062010-06-01 13:23:53 -07001069 }
1070 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001071 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001072 }
1073 }
San Mehat91cac642010-03-31 14:31:36 -07001074
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001075 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001076 public void addIdleTimer(String iface, int timeout, String label) {
1077 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1078
1079 if (DBG) Slog.d(TAG, "Adding idletimer");
1080
1081 synchronized (mIdleTimerLock) {
1082 IdleTimerParams params = mActiveIdleTimers.get(iface);
1083 if (params != null) {
1084 // the interface already has idletimer, update network count
1085 params.networkCount++;
1086 return;
1087 }
1088
1089 try {
1090 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1091 } catch (NativeDaemonConnectorException e) {
1092 throw e.rethrowAsParcelableException();
1093 }
1094 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1095 }
1096 }
1097
1098 @Override
1099 public void removeIdleTimer(String iface) {
1100 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1101
1102 if (DBG) Slog.d(TAG, "Removing idletimer");
1103
1104 synchronized (mIdleTimerLock) {
1105 IdleTimerParams params = mActiveIdleTimers.get(iface);
1106 if (params == null || --(params.networkCount) > 0) {
1107 return;
1108 }
1109
1110 try {
1111 mConnector.execute("idletimer", "remove", iface,
1112 Integer.toString(params.timeout), params.label);
1113 } catch (NativeDaemonConnectorException e) {
1114 throw e.rethrowAsParcelableException();
1115 }
1116 mActiveIdleTimers.remove(iface);
1117 }
1118 }
1119
1120 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001121 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001122 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001123 return mStatsFactory.readNetworkStatsSummaryDev();
1124 }
1125
1126 @Override
1127 public NetworkStats getNetworkStatsSummaryXt() {
1128 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1129 return mStatsFactory.readNetworkStatsSummaryXt();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001130 }
1131
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001132 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001133 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001134 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001135 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
San Mehat91cac642010-03-31 14:31:36 -07001136 }
1137
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001138 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001139 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001140 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001141
Jeff Sharkey350083e2011-06-29 10:45:16 -07001142 // silently discard when control disabled
1143 // TODO: eventually migrate to be always enabled
1144 if (!mBandwidthControlEnabled) return;
1145
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001146 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001147 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001148 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001149 }
1150
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001151 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001152 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001153 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001154 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001155 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001156 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001157 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001158 }
1159 }
1160
1161 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001162 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001163 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001164
Jeff Sharkey350083e2011-06-29 10:45:16 -07001165 // silently discard when control disabled
1166 // TODO: eventually migrate to be always enabled
1167 if (!mBandwidthControlEnabled) return;
1168
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001169 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001170 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001171 // TODO: eventually consider throwing
1172 return;
1173 }
1174
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001175 mActiveQuotas.remove(iface);
1176 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001177
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001178 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001179 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001180 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001181 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001182 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001183 }
1184 }
1185 }
1186
1187 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001188 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001189 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001190
1191 // silently discard when control disabled
1192 // TODO: eventually migrate to be always enabled
1193 if (!mBandwidthControlEnabled) return;
1194
1195 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001196 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001197 throw new IllegalStateException("setting alert requires existing quota on iface");
1198 }
1199
1200 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001201 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001202 throw new IllegalStateException("iface " + iface + " already has alert");
1203 }
1204
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001205 try {
1206 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001207 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001208 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001209 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001210 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001211 }
1212 }
1213 }
1214
1215 @Override
1216 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001217 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001218
1219 // silently discard when control disabled
1220 // TODO: eventually migrate to be always enabled
1221 if (!mBandwidthControlEnabled) return;
1222
1223 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001224 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001225 // TODO: eventually consider throwing
1226 return;
1227 }
1228
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001229 try {
1230 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001231 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001232 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001233 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001234 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001235 }
1236 }
1237 }
1238
1239 @Override
1240 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001241 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001242
1243 // silently discard when control disabled
1244 // TODO: eventually migrate to be always enabled
1245 if (!mBandwidthControlEnabled) return;
1246
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001247 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001248 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001249 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001250 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001251 }
1252 }
1253
1254 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001255 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001256 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001257
Jeff Sharkey350083e2011-06-29 10:45:16 -07001258 // silently discard when control disabled
1259 // TODO: eventually migrate to be always enabled
1260 if (!mBandwidthControlEnabled) return;
1261
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001262 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001263 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1264 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1265 // TODO: eventually consider throwing
1266 return;
1267 }
1268
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001269 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001270 mConnector.execute("bandwidth",
1271 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001272 if (rejectOnQuotaInterfaces) {
1273 mUidRejectOnQuota.put(uid, true);
1274 } else {
1275 mUidRejectOnQuota.delete(uid);
1276 }
1277 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001278 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001279 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001280 }
1281 }
1282
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001283 @Override
1284 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001285 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001286 return mBandwidthControlEnabled;
1287 }
1288
1289 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001290 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001291 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001292 return mStatsFactory.readNetworkStatsDetail(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001293 }
1294
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001295 @Override
1296 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001297 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001298
1299 if (ifacePairs.length % 2 != 0) {
1300 throw new IllegalArgumentException(
1301 "unexpected ifacePairs; length=" + ifacePairs.length);
1302 }
1303
1304 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1305 for (int i = 0; i < ifacePairs.length; i += 2) {
1306 final String ifaceIn = ifacePairs[i];
1307 final String ifaceOut = ifacePairs[i + 1];
1308 if (ifaceIn != null && ifaceOut != null) {
1309 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1310 }
1311 }
1312 return stats;
1313 }
1314
1315 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001316 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001317 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001318 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001319 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001320 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001321 }
1322
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001323 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001324
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001325 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1326 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1327 tok.nextToken();
1328 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001329
1330 try {
1331 final NetworkStats.Entry entry = new NetworkStats.Entry();
1332 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001333 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001334 entry.set = SET_DEFAULT;
1335 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001336 entry.rxBytes = Long.parseLong(tok.nextToken());
1337 entry.rxPackets = Long.parseLong(tok.nextToken());
1338 entry.txBytes = Long.parseLong(tok.nextToken());
1339 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001340 return entry;
1341 } catch (NumberFormatException e) {
1342 throw new IllegalStateException(
1343 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1344 }
1345 }
1346
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001347 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001348 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001349 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001350 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001351 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001352 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001353 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001354 }
1355 }
1356
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001357 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001358 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001359 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001360
Robert Greenwalt8058f622012-11-09 10:52:27 -08001361 final Command cmd = new Command("resolver", "setifdns", iface,
1362 (domains == null ? "" : domains));
1363
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001364 for (String s : servers) {
1365 InetAddress a = NetworkUtils.numericToInetAddress(s);
1366 if (a.isAnyLocalAddress() == false) {
1367 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001368 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001369 }
1370
1371 try {
1372 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001373 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001374 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001375 }
1376 }
1377
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001378 @Override
1379 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001380 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001381 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001382 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001383 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001384 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001385 }
1386 }
1387
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001388 @Override
1389 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001390 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001391 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001392 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001393 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001394 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001395 }
1396 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001397
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001398 @Override
1399 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001400 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001401 try {
1402 mConnector.execute("firewall", enabled ? "enable" : "disable");
1403 mFirewallEnabled = enabled;
1404 } catch (NativeDaemonConnectorException e) {
1405 throw e.rethrowAsParcelableException();
1406 }
1407 }
1408
1409 @Override
1410 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001411 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001412 return mFirewallEnabled;
1413 }
1414
1415 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001416 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001417 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001418 Preconditions.checkState(mFirewallEnabled);
1419 final String rule = allow ? ALLOW : DENY;
1420 try {
1421 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1422 } catch (NativeDaemonConnectorException e) {
1423 throw e.rethrowAsParcelableException();
1424 }
1425 }
1426
1427 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001428 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001429 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001430 Preconditions.checkState(mFirewallEnabled);
1431 final String rule = allow ? ALLOW : DENY;
1432 try {
1433 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1434 } catch (NativeDaemonConnectorException e) {
1435 throw e.rethrowAsParcelableException();
1436 }
1437 }
1438
1439 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001440 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001441 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001442 Preconditions.checkState(mFirewallEnabled);
1443 final String rule = allow ? ALLOW : DENY;
1444 try {
1445 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1446 } catch (NativeDaemonConnectorException e) {
1447 throw e.rethrowAsParcelableException();
1448 }
1449 }
1450
1451 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001452 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001453 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001454 Preconditions.checkState(mFirewallEnabled);
1455 final String rule = allow ? ALLOW : DENY;
1456 try {
1457 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1458 } catch (NativeDaemonConnectorException e) {
1459 throw e.rethrowAsParcelableException();
1460 }
1461 }
1462
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001463 private static void enforceSystemUid() {
1464 final int uid = Binder.getCallingUid();
1465 if (uid != Process.SYSTEM_UID) {
1466 throw new SecurityException("Only available to AID_SYSTEM");
1467 }
1468 }
1469
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001470 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001471 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1472 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1473 try {
1474 String cmd = "resolver setifaceforpid " + iface + " " + pid;
1475
1476 mConnector.execute(cmd);
1477 } catch (NativeDaemonConnectorException e) {
1478 throw new IllegalStateException(
1479 "Error communicating with native deamon to set interface for pid" + iface, e);
1480 }
1481 }
1482
1483 @Override
1484 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1485 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1486 try {
1487 String cmd = "resolver clearifaceforpid " + pid;
1488
1489 mConnector.execute(cmd);
1490 } catch (NativeDaemonConnectorException e) {
1491 throw new IllegalStateException(
1492 "Error communicating with native deamon to clear interface for pid " + pid, e);
1493 }
1494 }
1495
1496 /** {@inheritDoc} */
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001497 public void monitor() {
1498 if (mConnector != null) {
1499 mConnector.monitor();
1500 }
1501 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001502
1503 @Override
1504 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1505 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1506
Robert Greenwalt470fd722012-01-18 12:51:15 -08001507 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1508 mConnector.dump(fd, pw, args);
1509 pw.println();
1510
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001511 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1512
1513 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001514 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1515 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001516 }
1517
1518 synchronized (mUidRejectOnQuota) {
1519 pw.print("UID reject on quota ifaces: [");
1520 final int size = mUidRejectOnQuota.size();
1521 for (int i = 0; i < size; i++) {
1522 pw.print(mUidRejectOnQuota.keyAt(i));
1523 if (i < size - 1) pw.print(",");
1524 }
1525 pw.println("]");
1526 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001527
1528 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001529 }
San Mehat873f2142010-01-14 10:25:07 -08001530}