blob: 3b84c732304321940b5751e83b7c175d1f33cc81 [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;
Lorenzo Colitti79751842013-02-28 16:16:03 +090026import static com.android.server.NetworkManagementService.NetdResponseCode.ClatdStatusResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080027import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult;
28import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080029import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult;
30import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult;
31import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult;
32import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult;
33import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult;
34import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070035import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070036
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 Sharkey56cd6462013-06-07 15:09:15 -070061import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070062import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070063import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070064
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070065import java.io.BufferedReader;
66import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080067import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070068import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070069import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070070import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070071import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070072import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070073import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070074import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070075import java.net.InterfaceAddress;
76import java.net.NetworkInterface;
77import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070078import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070079import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070080import java.util.HashMap;
81import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070082import java.util.NoSuchElementException;
83import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070084import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080085
86/**
87 * @hide
88 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070089public class NetworkManagementService extends INetworkManagementService.Stub
90 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070091 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070092 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070093 private static final String NETD_TAG = "NetdConnector";
94
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080095 private static final String ADD = "add";
96 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070097
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070098 private static final String ALLOW = "allow";
99 private static final String DENY = "deny";
100
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700101 private static final String DEFAULT = "default";
102 private static final String SECONDARY = "secondary";
103
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700104 /**
105 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
106 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
107 */
108 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
109
San Mehat873f2142010-01-14 10:25:07 -0800110 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700111 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800112 public static final int InterfaceListResult = 110;
113 public static final int TetherInterfaceListResult = 111;
114 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800115 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800116
117 public static final int TetherStatusResult = 210;
118 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800119 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800120 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700121 public static final int InterfaceRxCounterResult = 216;
122 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700123 public static final int QuotaCounterResult = 220;
124 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800125 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900126 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800127
128 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700129 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700130 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800131 }
132
133 /**
134 * Binder context for this service
135 */
136 private Context mContext;
137
138 /**
139 * connector object for communicating with netd
140 */
141 private NativeDaemonConnector mConnector;
142
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700143 private final Handler mMainHandler = new Handler();
144
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700145 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700146 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700147
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800148 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
149 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800150
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700151 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
152
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700153 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700154 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700155 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700156 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700157 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700158 /** Set of UIDs with active reject rules. */
159 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
160
Haoyu Bai04124232012-06-28 15:26:19 -0700161 private Object mIdleTimerLock = new Object();
162 /** Set of interfaces with active idle timers. */
163 private static class IdleTimerParams {
164 public final int timeout;
165 public final String label;
166 public int networkCount;
167
168 IdleTimerParams(int timeout, String label) {
169 this.timeout = timeout;
170 this.label = label;
171 this.networkCount = 1;
172 }
173 }
174 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
175
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700176 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700177 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700178
San Mehat873f2142010-01-14 10:25:07 -0800179 /**
180 * Constructs a new NetworkManagementService instance
181 *
182 * @param context Binder context for this service
183 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700184 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800185 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800186
Marco Nelissen62dbb222010-02-18 10:56:30 -0800187 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
188 return;
189 }
190
San Mehat873f2142010-01-14 10:25:07 -0800191 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700192 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700193 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700194
195 // Add ourself to the Watchdog monitors.
196 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700197 }
198
199 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700200 final NetworkManagementService service = new NetworkManagementService(context);
201 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700202 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
203 service.mThread.start();
204 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700205 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700206 if (DBG) Slog.d(TAG, "Connected");
207 return service;
San Mehat873f2142010-01-14 10:25:07 -0800208 }
209
Jeff Sharkey350083e2011-06-29 10:45:16 -0700210 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700211 prepareNativeDaemon();
212 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700213 }
214
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800215 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800216 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800217 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800218 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800219 }
220
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800221 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800222 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800223 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800224 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800225 }
226
227 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700228 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800229 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700230 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800231 final int length = mObservers.beginBroadcast();
232 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800233 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800234 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
235 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900236 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700237 }
238 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800239 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700240 }
241
242 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700243 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700244 * (typically, an Ethernet cable has been plugged-in or unplugged).
245 */
246 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800247 final int length = mObservers.beginBroadcast();
248 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700249 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800250 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
251 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900252 } catch (RuntimeException 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) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900267 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800268 }
269 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800270 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800271 }
272
273 /**
274 * Notify our observers of an interface removal.
275 */
276 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700277 // netd already clears out quota and alerts for removed ifaces; update
278 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700279 mActiveAlerts.remove(iface);
280 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700281
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800282 final int length = mObservers.beginBroadcast();
283 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800284 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
286 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900287 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800288 }
289 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800291 }
292
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700293 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700294 * Notify our observers of a limit reached.
295 */
296 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 final int length = mObservers.beginBroadcast();
298 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700299 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
301 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900302 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700303 }
304 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800305 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700306 }
307
308 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700309 * Notify our observers of a change in the data activity state of the interface
310 */
311 private void notifyInterfaceClassActivity(String label, boolean active) {
312 final int length = mObservers.beginBroadcast();
313 for (int i = 0; i < length; i++) {
314 try {
315 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
316 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900317 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700318 }
319 }
320 mObservers.finishBroadcast();
321 }
322
323 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700324 * Prepare native daemon once connected, enabling modules and pushing any
325 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700326 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700327 private void prepareNativeDaemon() {
328 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700329
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700330 // only enable bandwidth control when support exists
331 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
332 if (hasKernelSupport) {
333 Slog.d(TAG, "enabling bandwidth control");
334 try {
335 mConnector.execute("bandwidth", "enable");
336 mBandwidthControlEnabled = true;
337 } catch (NativeDaemonConnectorException e) {
338 Log.wtf(TAG, "problem enabling bandwidth controls", e);
339 }
340 } else {
341 Slog.d(TAG, "not enabling bandwidth control");
342 }
343
344 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
345
346 // push any existing quota or UID rules
347 synchronized (mQuotaLock) {
348 int size = mActiveQuotas.size();
349 if (size > 0) {
350 Slog.d(TAG, "pushing " + size + " active quota rules");
351 final HashMap<String, Long> activeQuotas = mActiveQuotas;
352 mActiveQuotas = Maps.newHashMap();
353 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
354 setInterfaceQuota(entry.getKey(), entry.getValue());
355 }
356 }
357
358 size = mActiveAlerts.size();
359 if (size > 0) {
360 Slog.d(TAG, "pushing " + size + " active alert rules");
361 final HashMap<String, Long> activeAlerts = mActiveAlerts;
362 mActiveAlerts = Maps.newHashMap();
363 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
364 setInterfaceAlert(entry.getKey(), entry.getValue());
365 }
366 }
367
368 size = mUidRejectOnQuota.size();
369 if (size > 0) {
370 Slog.d(TAG, "pushing " + size + " active uid rules");
371 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
372 mUidRejectOnQuota = new SparseBooleanArray();
373 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
374 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
375 }
376 }
377 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700378
379 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700380 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700381 }
San Mehat4d02d002010-01-22 16:07:46 -0800382
San Mehat873f2142010-01-14 10:25:07 -0800383 //
384 // Netd Callback handling
385 //
386
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700387 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
388 @Override
San Mehat873f2142010-01-14 10:25:07 -0800389 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700390 // event is dispatched from internal NDC thread, so we prepare the
391 // daemon back on main thread.
392 if (mConnectedSignal != null) {
393 mConnectedSignal.countDown();
394 mConnectedSignal = null;
395 } else {
396 mMainHandler.post(new Runnable() {
397 @Override
398 public void run() {
399 prepareNativeDaemon();
400 }
401 });
402 }
San Mehat873f2142010-01-14 10:25:07 -0800403 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700404
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700405 @Override
San Mehat873f2142010-01-14 10:25:07 -0800406 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700407 switch (code) {
408 case NetdResponseCode.InterfaceChange:
409 /*
410 * a network interface change occured
411 * Format: "NNN Iface added <name>"
412 * "NNN Iface removed <name>"
413 * "NNN Iface changed <name> <up/down>"
414 * "NNN Iface linkstatus <name> <up/down>"
415 */
416 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
417 throw new IllegalStateException(
418 String.format("Invalid event from daemon (%s)", raw));
419 }
420 if (cooked[2].equals("added")) {
421 notifyInterfaceAdded(cooked[3]);
422 return true;
423 } else if (cooked[2].equals("removed")) {
424 notifyInterfaceRemoved(cooked[3]);
425 return true;
426 } else if (cooked[2].equals("changed") && cooked.length == 5) {
427 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
428 return true;
429 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
430 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
431 return true;
432 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800433 throw new IllegalStateException(
434 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700435 // break;
436 case NetdResponseCode.BandwidthControl:
437 /*
438 * Bandwidth control needs some attention
439 * Format: "NNN limit alert <alertName> <ifaceName>"
440 */
441 if (cooked.length < 5 || !cooked[1].equals("limit")) {
442 throw new IllegalStateException(
443 String.format("Invalid event from daemon (%s)", raw));
444 }
445 if (cooked[2].equals("alert")) {
446 notifyLimitReached(cooked[3], cooked[4]);
447 return true;
448 }
449 throw new IllegalStateException(
450 String.format("Invalid event from daemon (%s)", raw));
451 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700452 case NetdResponseCode.InterfaceClassActivity:
453 /*
454 * An network interface class state changed (active/idle)
455 * Format: "NNN IfaceClass <active/idle> <label>"
456 */
457 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
458 throw new IllegalStateException(
459 String.format("Invalid event from daemon (%s)", raw));
460 }
461 boolean isActive = cooked[2].equals("active");
462 notifyInterfaceClassActivity(cooked[3], isActive);
463 return true;
464 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700465 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800466 }
467 return false;
San Mehat873f2142010-01-14 10:25:07 -0800468 }
469 }
470
San Mehated4fc8a2010-01-22 12:28:36 -0800471
San Mehat873f2142010-01-14 10:25:07 -0800472 //
473 // INetworkManagementService members
474 //
475
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800476 @Override
477 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800478 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700479 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800480 return NativeDaemonEvent.filterMessageList(
481 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700482 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800483 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700484 }
San Mehated4fc8a2010-01-22 12:28:36 -0800485 }
486
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800487 @Override
488 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800489 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800490
491 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700492 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800493 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700494 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800495 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700496 }
San Mehated4fc8a2010-01-22 12:28:36 -0800497
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800498 event.checkCode(InterfaceGetCfgResult);
499
500 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
501 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800502
Kenny Roota80ce062010-06-01 13:23:53 -0700503 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800504 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700505 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800506 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800507 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800508 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700509 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800510 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800511 } catch (IllegalArgumentException iae) {
512 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700513 }
514
515 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800516 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800517 } catch (NumberFormatException nfe) {
518 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700519 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800520
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800521 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
522 while (st.hasMoreTokens()) {
523 cfg.setFlag(st.nextToken());
524 }
Kenny Roota80ce062010-06-01 13:23:53 -0700525 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800526 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800527 }
San Mehated4fc8a2010-01-22 12:28:36 -0800528 return cfg;
529 }
530
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800531 @Override
532 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800533 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800534 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800535 if (linkAddr == null || linkAddr.getAddress() == null) {
536 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800537 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800538
539 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800540 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800541 linkAddr.getNetworkPrefixLength());
542 for (String flag : cfg.getFlags()) {
543 cmd.appendArg(flag);
544 }
545
Kenny Roota80ce062010-06-01 13:23:53 -0700546 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800547 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700548 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800549 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700550 }
San Mehat873f2142010-01-14 10:25:07 -0800551 }
552
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800553 @Override
554 public void setInterfaceDown(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.setInterfaceDown();
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 setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800563 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800564 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800565 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800566 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700567 }
568
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800569 @Override
570 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800571 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700572 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800573 mConnector.execute(
574 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700575 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800576 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700577 }
578 }
579
Irfan Sherifff5600612011-06-16 10:26:28 -0700580 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
581 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800582 @Override
583 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800584 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700585 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800586 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700587 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800588 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700589 }
590 }
591
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800592 @Override
593 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800594 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700595 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800596 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700597 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800598 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700599 }
600 }
601
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800602 @Override
603 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800604 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700605 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800606 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700607 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800608 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700609 }
610 }
611
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800612 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700613 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800614 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700615 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700616 }
617
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800618 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700619 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800620 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700621 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700622 }
623
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800624 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700625 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800626 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700627 modifyRoute(interfaceName, ADD, route, SECONDARY);
628 }
629
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800630 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700631 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800632 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700633 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
634 }
635
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800636 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
637 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700638
639 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800640 final LinkAddress la = route.getDestination();
641 cmd.appendArg(la.getAddress().getHostAddress());
642 cmd.appendArg(la.getNetworkPrefixLength());
643
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700644 if (route.getGateway() == null) {
645 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800646 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700647 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800648 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700649 }
650 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800651 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700652 }
653
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800654 try {
655 mConnector.execute(cmd);
656 } catch (NativeDaemonConnectorException e) {
657 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700658 }
659 }
660
661 private ArrayList<String> readRouteList(String filename) {
662 FileInputStream fstream = null;
663 ArrayList<String> list = new ArrayList<String>();
664
665 try {
666 fstream = new FileInputStream(filename);
667 DataInputStream in = new DataInputStream(fstream);
668 BufferedReader br = new BufferedReader(new InputStreamReader(in));
669 String s;
670
671 // throw away the title line
672
673 while (((s = br.readLine()) != null) && (s.length() != 0)) {
674 list.add(s);
675 }
676 } catch (IOException ex) {
677 // return current list, possibly empty
678 } finally {
679 if (fstream != null) {
680 try {
681 fstream.close();
682 } catch (IOException ex) {}
683 }
684 }
685
686 return list;
687 }
688
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800689 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700690 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800691 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700692 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
693
694 // v4 routes listed as:
695 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
696 for (String s : readRouteList("/proc/net/route")) {
697 String[] fields = s.split("\t");
698
699 if (fields.length > 7) {
700 String iface = fields[0];
701
702 if (interfaceName.equals(iface)) {
703 String dest = fields[1];
704 String gate = fields[2];
705 String flags = fields[3]; // future use?
706 String mask = fields[7];
707 try {
708 // address stored as a hex string, ex: 0014A8C0
709 InetAddress destAddr =
710 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
711 int prefixLength =
712 NetworkUtils.netmaskIntToPrefixLength(
713 (int)Long.parseLong(mask, 16));
714 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
715
716 // address stored as a hex string, ex 0014A8C0
717 InetAddress gatewayAddr =
718 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
719
Wink Saville7b5fd052013-03-15 05:07:04 +0000720 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700721 routes.add(route);
722 } catch (Exception e) {
723 Log.e(TAG, "Error parsing route " + s + " : " + e);
724 continue;
725 }
726 }
727 }
728 }
729
730 // v6 routes listed as:
731 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
732 for (String s : readRouteList("/proc/net/ipv6_route")) {
733 String[]fields = s.split("\\s+");
734 if (fields.length > 9) {
735 String iface = fields[9].trim();
736 if (interfaceName.equals(iface)) {
737 String dest = fields[0];
738 String prefix = fields[1];
739 String gate = fields[4];
740
741 try {
742 // prefix length stored as a hex string, ex 40
743 int prefixLength = Integer.parseInt(prefix, 16);
744
745 // address stored as a 32 char hex string
746 // ex fe800000000000000000000000000000
747 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
748 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
749
750 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
751
Wink Saville7b5fd052013-03-15 05:07:04 +0000752 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700753 routes.add(route);
754 } catch (Exception e) {
755 Log.e(TAG, "Error parsing route " + s + " : " + e);
756 continue;
757 }
758 }
759 }
760 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800761 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700762 }
763
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800764 @Override
San Mehat873f2142010-01-14 10:25:07 -0800765 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800766 // TODO: remove from aidl if nobody calls externally
767 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800768
Joe Onorato8a9b2202010-02-26 18:56:32 -0800769 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800770 }
771
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800772 @Override
San Mehat873f2142010-01-14 10:25:07 -0800773 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800774 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800775
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800776 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700777 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800778 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700779 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800780 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700781 }
San Mehat873f2142010-01-14 10:25:07 -0800782
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800783 // 211 Forwarding enabled
784 event.checkCode(IpFwdStatusResult);
785 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800786 }
787
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800788 @Override
789 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800790 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800791 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800792 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800793 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800794 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800795 }
San Mehat873f2142010-01-14 10:25:07 -0800796 }
797
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800798 @Override
799 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800800 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700801 // cmd is "tether start first_start first_stop second_start second_stop ..."
802 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800803
804 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700805 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800806 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700807 }
Kenny Roota80ce062010-06-01 13:23:53 -0700808
809 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800810 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700811 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800812 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700813 }
San Mehat873f2142010-01-14 10:25:07 -0800814 }
815
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800816 @Override
817 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800818 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700819 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800820 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700821 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800822 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700823 }
San Mehat873f2142010-01-14 10:25:07 -0800824 }
825
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800826 @Override
827 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800828 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800829
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800830 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700831 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800832 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700833 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800834 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700835 }
San Mehat873f2142010-01-14 10:25:07 -0800836
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800837 // 210 Tethering services started
838 event.checkCode(TetherStatusResult);
839 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800840 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700841
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800842 @Override
843 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800844 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700845 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800846 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700847 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800848 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700849 }
San Mehat873f2142010-01-14 10:25:07 -0800850 }
851
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800852 @Override
San Mehat873f2142010-01-14 10:25:07 -0800853 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800854 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700855 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800856 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700857 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800858 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700859 }
San Mehat873f2142010-01-14 10:25:07 -0800860 }
861
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800862 @Override
863 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800864 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700865 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800866 return NativeDaemonEvent.filterMessageList(
867 mConnector.executeForList("tether", "interface", "list"),
868 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700869 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800870 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700871 }
San Mehat873f2142010-01-14 10:25:07 -0800872 }
873
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800874 @Override
875 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800876 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800877
878 final Command cmd = new Command("tether", "dns", "set");
879 for (String s : dns) {
880 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
881 }
882
San Mehat873f2142010-01-14 10:25:07 -0800883 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800884 mConnector.execute(cmd);
885 } catch (NativeDaemonConnectorException e) {
886 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800887 }
888 }
889
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800890 @Override
891 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800892 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700893 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800894 return NativeDaemonEvent.filterMessageList(
895 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700896 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800897 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700898 }
San Mehat873f2142010-01-14 10:25:07 -0800899 }
900
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800901 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700902 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800903 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700904
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800905 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
906 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800907 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800909 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800910 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
911 .getInterfaceAddresses();
912 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800913 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800914 InetAddress addr = NetworkUtils.getNetworkPart(
915 ia.getAddress(), ia.getNetworkPrefixLength());
916 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800917 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700918 }
919
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800920 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800922 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800923 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800924 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700925 }
926
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800927 @Override
928 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800929 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700930 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700931 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800932 } catch (SocketException e) {
933 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700934 }
San Mehat873f2142010-01-14 10:25:07 -0800935 }
936
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800937 @Override
938 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800939 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700940 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700941 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800942 } catch (SocketException e) {
943 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700944 }
San Mehat873f2142010-01-14 10:25:07 -0800945 }
San Mehat72759df2010-01-19 13:50:37 -0800946
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800947 @Override
948 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800949 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700950 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800951 return NativeDaemonEvent.filterMessageList(
952 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800954 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700955 }
San Mehat72759df2010-01-19 13:50:37 -0800956 }
957
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800958 @Override
959 public void attachPppd(
960 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800961 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800962 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800963 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800964 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
965 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
966 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800967 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700968 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800969 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800970 }
971 }
972
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800973 @Override
974 public void detachPppd(String tty) {
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 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700978 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800979 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700980 }
San Mehat72759df2010-01-19 13:50:37 -0800981 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800982
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800983 @Override
984 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -0700985 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800986 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700987 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700988 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -0700989 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -0700990 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -0700991 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -0700992 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkey56cd6462013-06-07 15:09:15 -0700993 getSecurityType(wifiConfig), new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -0700994 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800995 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -0700996 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800997 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -0800998 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -0800999 }
1000
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001001 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001002 switch (wifiConfig.getAuthType()) {
1003 case KeyMgmt.WPA_PSK:
1004 return "wpa-psk";
1005 case KeyMgmt.WPA2_PSK:
1006 return "wpa2-psk";
1007 default:
1008 return "open";
1009 }
1010 }
1011
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001012 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001013 @Override
1014 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001015 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001016 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001017 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001018 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001019 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001020 }
1021 }
1022
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001023 @Override
1024 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001026 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001028 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001029 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001030 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001031 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001032 }
1033
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001034 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001035 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001036 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001037 try {
1038 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001039 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001040 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001041 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Jeff Sharkey56cd6462013-06-07 15:09:15 -07001042 getSecurityType(wifiConfig), new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001043 }
1044 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001045 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001046 }
1047 }
San Mehat91cac642010-03-31 14:31:36 -07001048
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001049 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001050 public void addIdleTimer(String iface, int timeout, String label) {
1051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1052
1053 if (DBG) Slog.d(TAG, "Adding idletimer");
1054
1055 synchronized (mIdleTimerLock) {
1056 IdleTimerParams params = mActiveIdleTimers.get(iface);
1057 if (params != null) {
1058 // the interface already has idletimer, update network count
1059 params.networkCount++;
1060 return;
1061 }
1062
1063 try {
1064 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1065 } catch (NativeDaemonConnectorException e) {
1066 throw e.rethrowAsParcelableException();
1067 }
1068 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1069 }
1070 }
1071
1072 @Override
1073 public void removeIdleTimer(String iface) {
1074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1075
1076 if (DBG) Slog.d(TAG, "Removing idletimer");
1077
1078 synchronized (mIdleTimerLock) {
1079 IdleTimerParams params = mActiveIdleTimers.get(iface);
1080 if (params == null || --(params.networkCount) > 0) {
1081 return;
1082 }
1083
1084 try {
1085 mConnector.execute("idletimer", "remove", iface,
1086 Integer.toString(params.timeout), params.label);
1087 } catch (NativeDaemonConnectorException e) {
1088 throw e.rethrowAsParcelableException();
1089 }
1090 mActiveIdleTimers.remove(iface);
1091 }
1092 }
1093
1094 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001095 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001096 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001097 try {
1098 return mStatsFactory.readNetworkStatsSummaryDev();
1099 } catch (IOException e) {
1100 throw new IllegalStateException(e);
1101 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001102 }
1103
1104 @Override
1105 public NetworkStats getNetworkStatsSummaryXt() {
1106 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001107 try {
1108 return mStatsFactory.readNetworkStatsSummaryXt();
1109 } catch (IOException e) {
1110 throw new IllegalStateException(e);
1111 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001112 }
1113
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001114 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001115 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001116 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001117 try {
1118 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1119 } catch (IOException e) {
1120 throw new IllegalStateException(e);
1121 }
San Mehat91cac642010-03-31 14:31:36 -07001122 }
1123
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001124 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001125 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001126 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001127
Jeff Sharkey350083e2011-06-29 10:45:16 -07001128 // silently discard when control disabled
1129 // TODO: eventually migrate to be always enabled
1130 if (!mBandwidthControlEnabled) return;
1131
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001132 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001133 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001134 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001135 }
1136
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001137 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001138 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001139 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001140 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001141 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001142 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001143 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001144 }
1145 }
1146
1147 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001148 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001149 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001150
Jeff Sharkey350083e2011-06-29 10:45:16 -07001151 // silently discard when control disabled
1152 // TODO: eventually migrate to be always enabled
1153 if (!mBandwidthControlEnabled) return;
1154
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001155 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001156 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001157 // TODO: eventually consider throwing
1158 return;
1159 }
1160
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001161 mActiveQuotas.remove(iface);
1162 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001163
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001164 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001165 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001166 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001167 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001168 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001169 }
1170 }
1171 }
1172
1173 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001174 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001175 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001176
1177 // silently discard when control disabled
1178 // TODO: eventually migrate to be always enabled
1179 if (!mBandwidthControlEnabled) return;
1180
1181 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001182 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001183 throw new IllegalStateException("setting alert requires existing quota on iface");
1184 }
1185
1186 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001187 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001188 throw new IllegalStateException("iface " + iface + " already has alert");
1189 }
1190
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001191 try {
1192 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001193 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001194 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001195 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001196 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001197 }
1198 }
1199 }
1200
1201 @Override
1202 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001203 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001204
1205 // silently discard when control disabled
1206 // TODO: eventually migrate to be always enabled
1207 if (!mBandwidthControlEnabled) return;
1208
1209 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001210 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001211 // TODO: eventually consider throwing
1212 return;
1213 }
1214
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001215 try {
1216 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001217 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001218 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001219 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001220 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001221 }
1222 }
1223 }
1224
1225 @Override
1226 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001227 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001228
1229 // silently discard when control disabled
1230 // TODO: eventually migrate to be always enabled
1231 if (!mBandwidthControlEnabled) return;
1232
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001233 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001234 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001235 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001236 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001237 }
1238 }
1239
1240 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001241 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001242 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001243
Jeff Sharkey350083e2011-06-29 10:45:16 -07001244 // silently discard when control disabled
1245 // TODO: eventually migrate to be always enabled
1246 if (!mBandwidthControlEnabled) return;
1247
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001248 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001249 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1250 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1251 // TODO: eventually consider throwing
1252 return;
1253 }
1254
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001255 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001256 mConnector.execute("bandwidth",
1257 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001258 if (rejectOnQuotaInterfaces) {
1259 mUidRejectOnQuota.put(uid, true);
1260 } else {
1261 mUidRejectOnQuota.delete(uid);
1262 }
1263 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001264 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001265 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001266 }
1267 }
1268
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001269 @Override
1270 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001271 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001272 return mBandwidthControlEnabled;
1273 }
1274
1275 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001276 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001277 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001278 try {
1279 return mStatsFactory.readNetworkStatsDetail(uid);
1280 } catch (IOException e) {
1281 throw new IllegalStateException(e);
1282 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001283 }
1284
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001285 @Override
1286 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001287 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001288
1289 if (ifacePairs.length % 2 != 0) {
1290 throw new IllegalArgumentException(
1291 "unexpected ifacePairs; length=" + ifacePairs.length);
1292 }
1293
1294 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1295 for (int i = 0; i < ifacePairs.length; i += 2) {
1296 final String ifaceIn = ifacePairs[i];
1297 final String ifaceOut = ifacePairs[i + 1];
1298 if (ifaceIn != null && ifaceOut != null) {
1299 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1300 }
1301 }
1302 return stats;
1303 }
1304
1305 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001306 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001307 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001308 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001309 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001310 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001311 }
1312
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001313 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001314
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001315 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1316 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1317 tok.nextToken();
1318 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001319
1320 try {
1321 final NetworkStats.Entry entry = new NetworkStats.Entry();
1322 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001323 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001324 entry.set = SET_DEFAULT;
1325 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001326 entry.rxBytes = Long.parseLong(tok.nextToken());
1327 entry.rxPackets = Long.parseLong(tok.nextToken());
1328 entry.txBytes = Long.parseLong(tok.nextToken());
1329 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001330 return entry;
1331 } catch (NumberFormatException e) {
1332 throw new IllegalStateException(
1333 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1334 }
1335 }
1336
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001337 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001338 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001339 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001340 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001341 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001342 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001343 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001344 }
1345 }
1346
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001347 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001348 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001349 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001350
Robert Greenwalt8058f622012-11-09 10:52:27 -08001351 final Command cmd = new Command("resolver", "setifdns", iface,
1352 (domains == null ? "" : domains));
1353
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001354 for (String s : servers) {
1355 InetAddress a = NetworkUtils.numericToInetAddress(s);
1356 if (a.isAnyLocalAddress() == false) {
1357 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001358 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001359 }
1360
1361 try {
1362 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001363 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001364 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001365 }
1366 }
1367
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001368 @Override
1369 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001370 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001371 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001372 mConnector.execute("resolver", "flushdefaultif");
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 flushInterfaceDnsCache(String iface) {
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", "flushif", iface);
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 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001387
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001388 @Override
1389 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001390 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001391 try {
1392 mConnector.execute("firewall", enabled ? "enable" : "disable");
1393 mFirewallEnabled = enabled;
1394 } catch (NativeDaemonConnectorException e) {
1395 throw e.rethrowAsParcelableException();
1396 }
1397 }
1398
1399 @Override
1400 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001401 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001402 return mFirewallEnabled;
1403 }
1404
1405 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001406 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001407 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001408 Preconditions.checkState(mFirewallEnabled);
1409 final String rule = allow ? ALLOW : DENY;
1410 try {
1411 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1412 } catch (NativeDaemonConnectorException e) {
1413 throw e.rethrowAsParcelableException();
1414 }
1415 }
1416
1417 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001418 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001419 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001420 Preconditions.checkState(mFirewallEnabled);
1421 final String rule = allow ? ALLOW : DENY;
1422 try {
1423 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1424 } catch (NativeDaemonConnectorException e) {
1425 throw e.rethrowAsParcelableException();
1426 }
1427 }
1428
1429 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001430 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001431 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001432 Preconditions.checkState(mFirewallEnabled);
1433 final String rule = allow ? ALLOW : DENY;
1434 try {
1435 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1436 } catch (NativeDaemonConnectorException e) {
1437 throw e.rethrowAsParcelableException();
1438 }
1439 }
1440
1441 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001442 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001443 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001444 Preconditions.checkState(mFirewallEnabled);
1445 final String rule = allow ? ALLOW : DENY;
1446 try {
1447 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1448 } catch (NativeDaemonConnectorException e) {
1449 throw e.rethrowAsParcelableException();
1450 }
1451 }
1452
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001453 private static void enforceSystemUid() {
1454 final int uid = Binder.getCallingUid();
1455 if (uid != Process.SYSTEM_UID) {
1456 throw new SecurityException("Only available to AID_SYSTEM");
1457 }
1458 }
1459
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001460 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001461 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1462 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1463 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001464 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001465 } catch (NativeDaemonConnectorException e) {
1466 throw new IllegalStateException(
1467 "Error communicating with native deamon to set interface for pid" + iface, e);
1468 }
1469 }
1470
1471 @Override
1472 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1473 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1474 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001475 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001476 } catch (NativeDaemonConnectorException e) {
1477 throw new IllegalStateException(
1478 "Error communicating with native deamon to clear interface for pid " + pid, e);
1479 }
1480 }
1481
Lorenzo Colitti79751842013-02-28 16:16:03 +09001482 @Override
1483 public void startClatd(String interfaceName) throws IllegalStateException {
1484 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1485
1486 try {
1487 mConnector.execute("clatd", "start", interfaceName);
1488 } catch (NativeDaemonConnectorException e) {
1489 throw e.rethrowAsParcelableException();
1490 }
1491 }
1492
1493 @Override
1494 public void stopClatd() throws IllegalStateException {
1495 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1496
1497 try {
1498 mConnector.execute("clatd", "stop");
1499 } catch (NativeDaemonConnectorException e) {
1500 throw e.rethrowAsParcelableException();
1501 }
1502 }
1503
1504 @Override
1505 public boolean isClatdStarted() {
1506 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1507
1508 final NativeDaemonEvent event;
1509 try {
1510 event = mConnector.execute("clatd", "status");
1511 } catch (NativeDaemonConnectorException e) {
1512 throw e.rethrowAsParcelableException();
1513 }
1514
1515 event.checkCode(ClatdStatusResult);
1516 return event.getMessage().endsWith("started");
1517 }
1518
Mattias Falk8b47b362011-08-23 14:15:13 +02001519 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001520 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001521 public void monitor() {
1522 if (mConnector != null) {
1523 mConnector.monitor();
1524 }
1525 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001526
1527 @Override
1528 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1529 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1530
Robert Greenwalt470fd722012-01-18 12:51:15 -08001531 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1532 mConnector.dump(fd, pw, args);
1533 pw.println();
1534
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001535 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1536
1537 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001538 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1539 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001540 }
1541
1542 synchronized (mUidRejectOnQuota) {
1543 pw.print("UID reject on quota ifaces: [");
1544 final int size = mUidRejectOnQuota.size();
1545 for (int i = 0; i < size; i++) {
1546 pw.print(mUidRejectOnQuota.keyAt(i));
1547 if (i < size - 1) pw.print(",");
1548 }
1549 pw.println("]");
1550 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001551
1552 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001553 }
San Mehat873f2142010-01-14 10:25:07 -08001554}