blob: 6f740cd036d3dceb32c4b6fa7dfaee8b5c716ab7 [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;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070047import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070048import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080049import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070050import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080051import android.os.RemoteCallbackList;
52import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070053import android.os.ServiceManager;
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 Sharkey7a1c3fc2013-06-04 12:29:00 -070060import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070061import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070062import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080063import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070064import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070065import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070066import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070067
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070068import java.io.BufferedReader;
69import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080070import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070071import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070072import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070073import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070074import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070075import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070076import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070077import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070078import java.net.InterfaceAddress;
79import java.net.NetworkInterface;
80import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070081import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070082import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070083import java.util.HashMap;
84import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070085import java.util.NoSuchElementException;
86import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070087import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080088
89/**
90 * @hide
91 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070092public class NetworkManagementService extends INetworkManagementService.Stub
93 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070094 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070095 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070096 private static final String NETD_TAG = "NetdConnector";
97
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080098 private static final String ADD = "add";
99 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700100
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700101 private static final String ALLOW = "allow";
102 private static final String DENY = "deny";
103
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700104 private static final String DEFAULT = "default";
105 private static final String SECONDARY = "secondary";
106
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700107 /**
108 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
109 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
110 */
111 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
112
San Mehat873f2142010-01-14 10:25:07 -0800113 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700114 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800115 public static final int InterfaceListResult = 110;
116 public static final int TetherInterfaceListResult = 111;
117 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800118 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800119
120 public static final int TetherStatusResult = 210;
121 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800122 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800123 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700124 public static final int InterfaceRxCounterResult = 216;
125 public static final int InterfaceTxCounterResult = 217;
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;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900129 public static final int ClatdStatusResult = 223;
Robert Greenwalte3253922010-02-18 09:23:25 -0800130
131 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700132 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700133 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800134 }
135
136 /**
137 * Binder context for this service
138 */
139 private Context mContext;
140
141 /**
142 * connector object for communicating with netd
143 */
144 private NativeDaemonConnector mConnector;
145
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700146 private final Handler mMainHandler = new Handler();
147
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700148 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700149 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700150
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800151 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
152 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800153
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700154 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
155
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700156 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700157 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700158 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700159 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700160 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700161 /** Set of UIDs with active reject rules. */
162 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
163
Haoyu Bai04124232012-06-28 15:26:19 -0700164 private Object mIdleTimerLock = new Object();
165 /** Set of interfaces with active idle timers. */
166 private static class IdleTimerParams {
167 public final int timeout;
168 public final String label;
169 public int networkCount;
170
171 IdleTimerParams(int timeout, String label) {
172 this.timeout = timeout;
173 this.label = label;
174 this.networkCount = 1;
175 }
176 }
177 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
178
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700179 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700180 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700181
San Mehat873f2142010-01-14 10:25:07 -0800182 /**
183 * Constructs a new NetworkManagementService instance
184 *
185 * @param context Binder context for this service
186 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700187 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800188 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800189
Marco Nelissen62dbb222010-02-18 10:56:30 -0800190 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
191 return;
192 }
193
San Mehat873f2142010-01-14 10:25:07 -0800194 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700195 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700196 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700197
198 // Add ourself to the Watchdog monitors.
199 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700200 }
201
202 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700203 final NetworkManagementService service = new NetworkManagementService(context);
204 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700205 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
206 service.mThread.start();
207 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700208 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700209 if (DBG) Slog.d(TAG, "Connected");
210 return service;
San Mehat873f2142010-01-14 10:25:07 -0800211 }
212
Jeff Sharkey350083e2011-06-29 10:45:16 -0700213 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700214 prepareNativeDaemon();
215 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700216 }
217
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800218 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800219 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800220 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800221 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800222 }
223
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800224 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800225 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800226 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800227 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800228 }
229
230 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700231 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800232 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700233 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800234 final int length = mObservers.beginBroadcast();
235 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800236 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800237 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
238 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900239 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700240 }
241 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800242 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700243 }
244
245 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700246 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700247 * (typically, an Ethernet cable has been plugged-in or unplugged).
248 */
249 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800250 final int length = mObservers.beginBroadcast();
251 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700252 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800253 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
254 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900255 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800256 }
257 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800258 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800259 }
260
261 /**
262 * Notify our observers of an interface addition.
263 */
264 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800265 final int length = mObservers.beginBroadcast();
266 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800267 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800268 mObservers.getBroadcastItem(i).interfaceAdded(iface);
269 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900270 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800271 }
272 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800273 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800274 }
275
276 /**
277 * Notify our observers of an interface removal.
278 */
279 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700280 // netd already clears out quota and alerts for removed ifaces; update
281 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700282 mActiveAlerts.remove(iface);
283 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700284
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800285 final int length = mObservers.beginBroadcast();
286 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800287 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800288 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
289 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900290 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800291 }
292 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800293 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800294 }
295
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700296 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700297 * Notify our observers of a limit reached.
298 */
299 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800300 final int length = mObservers.beginBroadcast();
301 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700302 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800303 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
304 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900305 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700306 }
307 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800308 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700309 }
310
311 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700312 * Notify our observers of a change in the data activity state of the interface
313 */
314 private void notifyInterfaceClassActivity(String label, boolean active) {
315 final int length = mObservers.beginBroadcast();
316 for (int i = 0; i < length; i++) {
317 try {
318 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
319 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900320 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700321 }
322 }
323 mObservers.finishBroadcast();
324 }
325
326 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700327 * Prepare native daemon once connected, enabling modules and pushing any
328 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700329 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700330 private void prepareNativeDaemon() {
331 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700332
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700333 // only enable bandwidth control when support exists
334 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
335 if (hasKernelSupport) {
336 Slog.d(TAG, "enabling bandwidth control");
337 try {
338 mConnector.execute("bandwidth", "enable");
339 mBandwidthControlEnabled = true;
340 } catch (NativeDaemonConnectorException e) {
341 Log.wtf(TAG, "problem enabling bandwidth controls", e);
342 }
343 } else {
344 Slog.d(TAG, "not enabling bandwidth control");
345 }
346
347 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
348
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700349 if (mBandwidthControlEnabled) {
350 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700351 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700352 .noteNetworkStatsEnabled();
353 } catch (RemoteException e) {
354 }
355 }
356
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700357 // push any existing quota or UID rules
358 synchronized (mQuotaLock) {
359 int size = mActiveQuotas.size();
360 if (size > 0) {
361 Slog.d(TAG, "pushing " + size + " active quota rules");
362 final HashMap<String, Long> activeQuotas = mActiveQuotas;
363 mActiveQuotas = Maps.newHashMap();
364 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
365 setInterfaceQuota(entry.getKey(), entry.getValue());
366 }
367 }
368
369 size = mActiveAlerts.size();
370 if (size > 0) {
371 Slog.d(TAG, "pushing " + size + " active alert rules");
372 final HashMap<String, Long> activeAlerts = mActiveAlerts;
373 mActiveAlerts = Maps.newHashMap();
374 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
375 setInterfaceAlert(entry.getKey(), entry.getValue());
376 }
377 }
378
379 size = mUidRejectOnQuota.size();
380 if (size > 0) {
381 Slog.d(TAG, "pushing " + size + " active uid rules");
382 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
383 mUidRejectOnQuota = new SparseBooleanArray();
384 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
385 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
386 }
387 }
388 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700389
390 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700391 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700392 }
San Mehat4d02d002010-01-22 16:07:46 -0800393
San Mehat873f2142010-01-14 10:25:07 -0800394 //
395 // Netd Callback handling
396 //
397
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700398 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
399 @Override
San Mehat873f2142010-01-14 10:25:07 -0800400 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700401 // event is dispatched from internal NDC thread, so we prepare the
402 // daemon back on main thread.
403 if (mConnectedSignal != null) {
404 mConnectedSignal.countDown();
405 mConnectedSignal = null;
406 } else {
407 mMainHandler.post(new Runnable() {
408 @Override
409 public void run() {
410 prepareNativeDaemon();
411 }
412 });
413 }
San Mehat873f2142010-01-14 10:25:07 -0800414 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700415
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700416 @Override
San Mehat873f2142010-01-14 10:25:07 -0800417 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700418 switch (code) {
419 case NetdResponseCode.InterfaceChange:
420 /*
421 * a network interface change occured
422 * Format: "NNN Iface added <name>"
423 * "NNN Iface removed <name>"
424 * "NNN Iface changed <name> <up/down>"
425 * "NNN Iface linkstatus <name> <up/down>"
426 */
427 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
428 throw new IllegalStateException(
429 String.format("Invalid event from daemon (%s)", raw));
430 }
431 if (cooked[2].equals("added")) {
432 notifyInterfaceAdded(cooked[3]);
433 return true;
434 } else if (cooked[2].equals("removed")) {
435 notifyInterfaceRemoved(cooked[3]);
436 return true;
437 } else if (cooked[2].equals("changed") && cooked.length == 5) {
438 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
439 return true;
440 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
441 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
442 return true;
443 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800444 throw new IllegalStateException(
445 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700446 // break;
447 case NetdResponseCode.BandwidthControl:
448 /*
449 * Bandwidth control needs some attention
450 * Format: "NNN limit alert <alertName> <ifaceName>"
451 */
452 if (cooked.length < 5 || !cooked[1].equals("limit")) {
453 throw new IllegalStateException(
454 String.format("Invalid event from daemon (%s)", raw));
455 }
456 if (cooked[2].equals("alert")) {
457 notifyLimitReached(cooked[3], cooked[4]);
458 return true;
459 }
460 throw new IllegalStateException(
461 String.format("Invalid event from daemon (%s)", raw));
462 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700463 case NetdResponseCode.InterfaceClassActivity:
464 /*
465 * An network interface class state changed (active/idle)
466 * Format: "NNN IfaceClass <active/idle> <label>"
467 */
468 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
469 throw new IllegalStateException(
470 String.format("Invalid event from daemon (%s)", raw));
471 }
472 boolean isActive = cooked[2].equals("active");
473 notifyInterfaceClassActivity(cooked[3], isActive);
474 return true;
475 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700476 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800477 }
478 return false;
San Mehat873f2142010-01-14 10:25:07 -0800479 }
480 }
481
San Mehated4fc8a2010-01-22 12:28:36 -0800482
San Mehat873f2142010-01-14 10:25:07 -0800483 //
484 // INetworkManagementService members
485 //
486
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800487 @Override
488 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800489 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700490 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800491 return NativeDaemonEvent.filterMessageList(
492 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700493 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800494 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700495 }
San Mehated4fc8a2010-01-22 12:28:36 -0800496 }
497
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800498 @Override
499 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800500 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800501
502 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700503 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800504 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700505 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800506 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700507 }
San Mehated4fc8a2010-01-22 12:28:36 -0800508
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800509 event.checkCode(InterfaceGetCfgResult);
510
511 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
512 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800513
Kenny Roota80ce062010-06-01 13:23:53 -0700514 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800515 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700516 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800517 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800518 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800519 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700520 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800521 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800522 } catch (IllegalArgumentException iae) {
523 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700524 }
525
526 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800527 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800528 } catch (NumberFormatException nfe) {
529 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700530 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800531
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800532 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
533 while (st.hasMoreTokens()) {
534 cfg.setFlag(st.nextToken());
535 }
Kenny Roota80ce062010-06-01 13:23:53 -0700536 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800537 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800538 }
San Mehated4fc8a2010-01-22 12:28:36 -0800539 return cfg;
540 }
541
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800542 @Override
543 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800544 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800545 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800546 if (linkAddr == null || linkAddr.getAddress() == null) {
547 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800548 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800549
550 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800551 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800552 linkAddr.getNetworkPrefixLength());
553 for (String flag : cfg.getFlags()) {
554 cmd.appendArg(flag);
555 }
556
Kenny Roota80ce062010-06-01 13:23:53 -0700557 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800558 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700559 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800560 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700561 }
San Mehat873f2142010-01-14 10:25:07 -0800562 }
563
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800564 @Override
565 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800566 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800567 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800568 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800569 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700570 }
571
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800572 @Override
573 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800574 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800575 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800576 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800577 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700578 }
579
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800580 @Override
581 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800582 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700583 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800584 mConnector.execute(
585 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700586 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800587 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700588 }
589 }
590
Irfan Sherifff5600612011-06-16 10:26:28 -0700591 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
592 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800593 @Override
594 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800595 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700596 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800597 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700598 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800599 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700600 }
601 }
602
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800603 @Override
604 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800605 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700606 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800607 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700608 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800609 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700610 }
611 }
612
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800613 @Override
614 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800615 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700616 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800617 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700618 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800619 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700620 }
621 }
622
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800623 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700624 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800625 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700626 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700627 }
628
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800629 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700630 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800631 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700632 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700633 }
634
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800635 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700636 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800637 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700638 modifyRoute(interfaceName, ADD, route, SECONDARY);
639 }
640
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800641 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700642 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800643 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700644 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
645 }
646
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800647 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
648 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700649
650 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800651 final LinkAddress la = route.getDestination();
652 cmd.appendArg(la.getAddress().getHostAddress());
653 cmd.appendArg(la.getNetworkPrefixLength());
654
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700655 if (route.getGateway() == null) {
656 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800657 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700658 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800659 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700660 }
661 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800662 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700663 }
664
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800665 try {
666 mConnector.execute(cmd);
667 } catch (NativeDaemonConnectorException e) {
668 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700669 }
670 }
671
672 private ArrayList<String> readRouteList(String filename) {
673 FileInputStream fstream = null;
674 ArrayList<String> list = new ArrayList<String>();
675
676 try {
677 fstream = new FileInputStream(filename);
678 DataInputStream in = new DataInputStream(fstream);
679 BufferedReader br = new BufferedReader(new InputStreamReader(in));
680 String s;
681
682 // throw away the title line
683
684 while (((s = br.readLine()) != null) && (s.length() != 0)) {
685 list.add(s);
686 }
687 } catch (IOException ex) {
688 // return current list, possibly empty
689 } finally {
690 if (fstream != null) {
691 try {
692 fstream.close();
693 } catch (IOException ex) {}
694 }
695 }
696
697 return list;
698 }
699
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800700 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700701 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800702 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700703 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
704
705 // v4 routes listed as:
706 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
707 for (String s : readRouteList("/proc/net/route")) {
708 String[] fields = s.split("\t");
709
710 if (fields.length > 7) {
711 String iface = fields[0];
712
713 if (interfaceName.equals(iface)) {
714 String dest = fields[1];
715 String gate = fields[2];
716 String flags = fields[3]; // future use?
717 String mask = fields[7];
718 try {
719 // address stored as a hex string, ex: 0014A8C0
720 InetAddress destAddr =
721 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
722 int prefixLength =
723 NetworkUtils.netmaskIntToPrefixLength(
724 (int)Long.parseLong(mask, 16));
725 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
726
727 // address stored as a hex string, ex 0014A8C0
728 InetAddress gatewayAddr =
729 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
730
Wink Saville7b5fd052013-03-15 05:07:04 +0000731 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700732 routes.add(route);
733 } catch (Exception e) {
734 Log.e(TAG, "Error parsing route " + s + " : " + e);
735 continue;
736 }
737 }
738 }
739 }
740
741 // v6 routes listed as:
742 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
743 for (String s : readRouteList("/proc/net/ipv6_route")) {
744 String[]fields = s.split("\\s+");
745 if (fields.length > 9) {
746 String iface = fields[9].trim();
747 if (interfaceName.equals(iface)) {
748 String dest = fields[0];
749 String prefix = fields[1];
750 String gate = fields[4];
751
752 try {
753 // prefix length stored as a hex string, ex 40
754 int prefixLength = Integer.parseInt(prefix, 16);
755
756 // address stored as a 32 char hex string
757 // ex fe800000000000000000000000000000
758 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
759 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
760
761 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
762
Wink Saville7b5fd052013-03-15 05:07:04 +0000763 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700764 routes.add(route);
765 } catch (Exception e) {
766 Log.e(TAG, "Error parsing route " + s + " : " + e);
767 continue;
768 }
769 }
770 }
771 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800772 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700773 }
774
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800775 @Override
San Mehat873f2142010-01-14 10:25:07 -0800776 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800777 // TODO: remove from aidl if nobody calls externally
778 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800779
Joe Onorato8a9b2202010-02-26 18:56:32 -0800780 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800781 }
782
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800783 @Override
San Mehat873f2142010-01-14 10:25:07 -0800784 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800785 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800786
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800787 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700788 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800789 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700790 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800791 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700792 }
San Mehat873f2142010-01-14 10:25:07 -0800793
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800794 // 211 Forwarding enabled
795 event.checkCode(IpFwdStatusResult);
796 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800797 }
798
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800799 @Override
800 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800801 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800802 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800803 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800804 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800805 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800806 }
San Mehat873f2142010-01-14 10:25:07 -0800807 }
808
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800809 @Override
810 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800811 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700812 // cmd is "tether start first_start first_stop second_start second_stop ..."
813 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800814
815 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700816 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800817 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700818 }
Kenny Roota80ce062010-06-01 13:23:53 -0700819
820 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800821 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700822 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800823 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700824 }
San Mehat873f2142010-01-14 10:25:07 -0800825 }
826
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800827 @Override
828 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800829 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700830 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800831 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700832 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800833 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700834 }
San Mehat873f2142010-01-14 10:25:07 -0800835 }
836
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800837 @Override
838 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800839 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800840
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800841 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700842 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800843 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700844 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800845 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700846 }
San Mehat873f2142010-01-14 10:25:07 -0800847
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800848 // 210 Tethering services started
849 event.checkCode(TetherStatusResult);
850 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800851 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700852
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800853 @Override
854 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800855 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700856 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800857 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700858 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800859 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700860 }
San Mehat873f2142010-01-14 10:25:07 -0800861 }
862
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800863 @Override
San Mehat873f2142010-01-14 10:25:07 -0800864 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800865 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700866 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800867 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700868 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800869 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700870 }
San Mehat873f2142010-01-14 10:25:07 -0800871 }
872
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800873 @Override
874 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800875 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700876 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800877 return NativeDaemonEvent.filterMessageList(
878 mConnector.executeForList("tether", "interface", "list"),
879 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700880 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800881 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700882 }
San Mehat873f2142010-01-14 10:25:07 -0800883 }
884
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800885 @Override
886 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800887 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800888
889 final Command cmd = new Command("tether", "dns", "set");
890 for (String s : dns) {
891 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
892 }
893
San Mehat873f2142010-01-14 10:25:07 -0800894 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800895 mConnector.execute(cmd);
896 } catch (NativeDaemonConnectorException e) {
897 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800898 }
899 }
900
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800901 @Override
902 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800903 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700904 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800905 return NativeDaemonEvent.filterMessageList(
906 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700907 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800908 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700909 }
San Mehat873f2142010-01-14 10:25:07 -0800910 }
911
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800912 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700913 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800914 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700915
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800916 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
917 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800918 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800919 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800920 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
922 .getInterfaceAddresses();
923 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800924 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800925 InetAddress addr = NetworkUtils.getNetworkPart(
926 ia.getAddress(), ia.getNetworkPrefixLength());
927 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800928 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700929 }
930
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800931 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800932 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800933 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800934 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800935 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700936 }
937
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800938 @Override
939 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800940 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700941 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700942 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800943 } catch (SocketException e) {
944 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700945 }
San Mehat873f2142010-01-14 10:25:07 -0800946 }
947
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800948 @Override
949 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800950 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700951 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700952 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800953 } catch (SocketException e) {
954 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700955 }
San Mehat873f2142010-01-14 10:25:07 -0800956 }
San Mehat72759df2010-01-19 13:50:37 -0800957
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800958 @Override
959 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800960 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700961 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800962 return NativeDaemonEvent.filterMessageList(
963 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700964 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800965 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700966 }
San Mehat72759df2010-01-19 13:50:37 -0800967 }
968
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800969 @Override
970 public void attachPppd(
971 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800972 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800973 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800974 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800975 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
976 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
977 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800978 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700979 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800980 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800981 }
982 }
983
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800984 @Override
985 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800986 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700987 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800988 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700989 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800990 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700991 }
San Mehat72759df2010-01-19 13:50:37 -0800992 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800993
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800994 @Override
995 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -0700996 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800997 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700998 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -0700999 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001000 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001001 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001002 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001003 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001004 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001005 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001006 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001007 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001008 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001009 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001010 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001011 }
1012
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001013 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001014 switch (wifiConfig.getAuthType()) {
1015 case KeyMgmt.WPA_PSK:
1016 return "wpa-psk";
1017 case KeyMgmt.WPA2_PSK:
1018 return "wpa2-psk";
1019 default:
1020 return "open";
1021 }
1022 }
1023
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001024 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001025 @Override
1026 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001027 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001028 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001029 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001030 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001031 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001032 }
1033 }
1034
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001035 @Override
1036 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001037 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001038 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001039 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001040 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001041 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001042 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001043 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001044 }
1045
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001046 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001047 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001048 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001049 try {
1050 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001051 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001052 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001053 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001054 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001055 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001056 }
1057 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001058 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001059 }
1060 }
San Mehat91cac642010-03-31 14:31:36 -07001061
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001062 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001063 public void addIdleTimer(String iface, int timeout, String label) {
1064 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1065
1066 if (DBG) Slog.d(TAG, "Adding idletimer");
1067
1068 synchronized (mIdleTimerLock) {
1069 IdleTimerParams params = mActiveIdleTimers.get(iface);
1070 if (params != null) {
1071 // the interface already has idletimer, update network count
1072 params.networkCount++;
1073 return;
1074 }
1075
1076 try {
1077 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1078 } catch (NativeDaemonConnectorException e) {
1079 throw e.rethrowAsParcelableException();
1080 }
1081 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1082 }
1083 }
1084
1085 @Override
1086 public void removeIdleTimer(String iface) {
1087 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1088
1089 if (DBG) Slog.d(TAG, "Removing idletimer");
1090
1091 synchronized (mIdleTimerLock) {
1092 IdleTimerParams params = mActiveIdleTimers.get(iface);
1093 if (params == null || --(params.networkCount) > 0) {
1094 return;
1095 }
1096
1097 try {
1098 mConnector.execute("idletimer", "remove", iface,
1099 Integer.toString(params.timeout), params.label);
1100 } catch (NativeDaemonConnectorException e) {
1101 throw e.rethrowAsParcelableException();
1102 }
1103 mActiveIdleTimers.remove(iface);
1104 }
1105 }
1106
1107 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001108 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001109 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001110 try {
1111 return mStatsFactory.readNetworkStatsSummaryDev();
1112 } catch (IOException e) {
1113 throw new IllegalStateException(e);
1114 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001115 }
1116
1117 @Override
1118 public NetworkStats getNetworkStatsSummaryXt() {
1119 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001120 try {
1121 return mStatsFactory.readNetworkStatsSummaryXt();
1122 } catch (IOException e) {
1123 throw new IllegalStateException(e);
1124 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001125 }
1126
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001127 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001128 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001129 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001130 try {
1131 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1132 } catch (IOException e) {
1133 throw new IllegalStateException(e);
1134 }
San Mehat91cac642010-03-31 14:31:36 -07001135 }
1136
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001137 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001138 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001139 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001140
Jeff Sharkey350083e2011-06-29 10:45:16 -07001141 // silently discard when control disabled
1142 // TODO: eventually migrate to be always enabled
1143 if (!mBandwidthControlEnabled) return;
1144
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001145 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001146 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001147 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001148 }
1149
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001150 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001151 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001152 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001153 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001154 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001155 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001156 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001157 }
1158 }
1159
1160 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001161 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001162 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001163
Jeff Sharkey350083e2011-06-29 10:45:16 -07001164 // silently discard when control disabled
1165 // TODO: eventually migrate to be always enabled
1166 if (!mBandwidthControlEnabled) return;
1167
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001168 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001169 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001170 // TODO: eventually consider throwing
1171 return;
1172 }
1173
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001174 mActiveQuotas.remove(iface);
1175 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001176
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001177 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001178 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001179 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001180 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001181 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001182 }
1183 }
1184 }
1185
1186 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001187 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001188 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001189
1190 // silently discard when control disabled
1191 // TODO: eventually migrate to be always enabled
1192 if (!mBandwidthControlEnabled) return;
1193
1194 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001195 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001196 throw new IllegalStateException("setting alert requires existing quota on iface");
1197 }
1198
1199 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001200 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001201 throw new IllegalStateException("iface " + iface + " already has alert");
1202 }
1203
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001204 try {
1205 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001206 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001207 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001208 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001209 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001210 }
1211 }
1212 }
1213
1214 @Override
1215 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001216 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001217
1218 // silently discard when control disabled
1219 // TODO: eventually migrate to be always enabled
1220 if (!mBandwidthControlEnabled) return;
1221
1222 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001223 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001224 // TODO: eventually consider throwing
1225 return;
1226 }
1227
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001228 try {
1229 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001230 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001231 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001232 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001233 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001234 }
1235 }
1236 }
1237
1238 @Override
1239 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001240 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001241
1242 // silently discard when control disabled
1243 // TODO: eventually migrate to be always enabled
1244 if (!mBandwidthControlEnabled) return;
1245
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001246 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001247 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001248 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001249 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001250 }
1251 }
1252
1253 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001254 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001255 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001256
Jeff Sharkey350083e2011-06-29 10:45:16 -07001257 // silently discard when control disabled
1258 // TODO: eventually migrate to be always enabled
1259 if (!mBandwidthControlEnabled) return;
1260
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001261 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001262 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1263 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1264 // TODO: eventually consider throwing
1265 return;
1266 }
1267
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001268 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001269 mConnector.execute("bandwidth",
1270 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001271 if (rejectOnQuotaInterfaces) {
1272 mUidRejectOnQuota.put(uid, true);
1273 } else {
1274 mUidRejectOnQuota.delete(uid);
1275 }
1276 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001277 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001278 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001279 }
1280 }
1281
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001282 @Override
1283 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001284 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001285 return mBandwidthControlEnabled;
1286 }
1287
1288 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001289 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001290 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001291 try {
1292 return mStatsFactory.readNetworkStatsDetail(uid);
1293 } catch (IOException e) {
1294 throw new IllegalStateException(e);
1295 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001296 }
1297
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001298 @Override
1299 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001300 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001301
1302 if (ifacePairs.length % 2 != 0) {
1303 throw new IllegalArgumentException(
1304 "unexpected ifacePairs; length=" + ifacePairs.length);
1305 }
1306
1307 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1308 for (int i = 0; i < ifacePairs.length; i += 2) {
1309 final String ifaceIn = ifacePairs[i];
1310 final String ifaceOut = ifacePairs[i + 1];
1311 if (ifaceIn != null && ifaceOut != null) {
1312 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1313 }
1314 }
1315 return stats;
1316 }
1317
1318 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001319 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001320 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001321 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001322 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001323 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001324 }
1325
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001326 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001327
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001328 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1329 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1330 tok.nextToken();
1331 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001332
1333 try {
1334 final NetworkStats.Entry entry = new NetworkStats.Entry();
1335 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001336 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001337 entry.set = SET_DEFAULT;
1338 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001339 entry.rxBytes = Long.parseLong(tok.nextToken());
1340 entry.rxPackets = Long.parseLong(tok.nextToken());
1341 entry.txBytes = Long.parseLong(tok.nextToken());
1342 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001343 return entry;
1344 } catch (NumberFormatException e) {
1345 throw new IllegalStateException(
1346 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1347 }
1348 }
1349
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001350 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001351 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001352 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001353 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001354 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001355 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001356 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001357 }
1358 }
1359
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001360 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001361 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001362 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001363
Robert Greenwalt8058f622012-11-09 10:52:27 -08001364 final Command cmd = new Command("resolver", "setifdns", iface,
1365 (domains == null ? "" : domains));
1366
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001367 for (String s : servers) {
1368 InetAddress a = NetworkUtils.numericToInetAddress(s);
1369 if (a.isAnyLocalAddress() == false) {
1370 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001371 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001372 }
1373
1374 try {
1375 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001376 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001377 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001378 }
1379 }
1380
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001381 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001382 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1383 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1384 try {
1385 mConnector.execute("interface", "route",
1386 "uid", "add", iface, uid_start, uid_end);
1387 } catch (NativeDaemonConnectorException e) {
1388 throw e.rethrowAsParcelableException();
1389 }
1390 }
1391
1392 @Override
1393 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1394 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1395 try {
1396 mConnector.execute("interface", "route",
1397 "uid", "remove", iface, uid_start, uid_end);
1398 } catch (NativeDaemonConnectorException e) {
1399 throw e.rethrowAsParcelableException();
1400 }
1401 }
1402
1403 @Override
1404 public void setMarkedForwarding(String iface) {
1405 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1406 try {
1407 mConnector.execute("interface", "route", "fwmark", "add", iface);
1408 } catch (NativeDaemonConnectorException e) {
1409 throw e.rethrowAsParcelableException();
1410 }
1411 }
1412
1413 @Override
1414 public void clearMarkedForwarding(String iface) {
1415 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1416 try {
1417 mConnector.execute("interface", "route", "fwmark", "remove", iface);
1418 } catch (NativeDaemonConnectorException e) {
1419 throw e.rethrowAsParcelableException();
1420 }
1421 }
1422
1423 @Override
1424 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1425 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1426 try {
1427 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1428 } catch (NativeDaemonConnectorException e) {
1429 throw e.rethrowAsParcelableException();
1430 }
1431 }
1432
1433 @Override
1434 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1435 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1436 try {
1437 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1438 } catch (NativeDaemonConnectorException e) {
1439 throw e.rethrowAsParcelableException();
1440 }
1441 }
1442
1443 @Override
1444 public void clearDnsInterfaceMaps() {
1445 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1446 try {
1447 mConnector.execute("resolver", "clearifacemapping");
1448 } catch (NativeDaemonConnectorException e) {
1449 throw e.rethrowAsParcelableException();
1450 }
1451 }
1452
1453
1454 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001455 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001456 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001457 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001458 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001459 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001460 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001461 }
1462 }
1463
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001464 @Override
1465 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001466 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001467 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001468 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001469 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001470 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001471 }
1472 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001473
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001474 @Override
1475 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001476 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001477 try {
1478 mConnector.execute("firewall", enabled ? "enable" : "disable");
1479 mFirewallEnabled = enabled;
1480 } catch (NativeDaemonConnectorException e) {
1481 throw e.rethrowAsParcelableException();
1482 }
1483 }
1484
1485 @Override
1486 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001487 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001488 return mFirewallEnabled;
1489 }
1490
1491 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001492 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001493 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001494 Preconditions.checkState(mFirewallEnabled);
1495 final String rule = allow ? ALLOW : DENY;
1496 try {
1497 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1498 } catch (NativeDaemonConnectorException e) {
1499 throw e.rethrowAsParcelableException();
1500 }
1501 }
1502
1503 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001504 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001505 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001506 Preconditions.checkState(mFirewallEnabled);
1507 final String rule = allow ? ALLOW : DENY;
1508 try {
1509 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1510 } catch (NativeDaemonConnectorException e) {
1511 throw e.rethrowAsParcelableException();
1512 }
1513 }
1514
1515 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001516 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001517 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001518 Preconditions.checkState(mFirewallEnabled);
1519 final String rule = allow ? ALLOW : DENY;
1520 try {
1521 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1522 } catch (NativeDaemonConnectorException e) {
1523 throw e.rethrowAsParcelableException();
1524 }
1525 }
1526
1527 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001528 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001529 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001530 Preconditions.checkState(mFirewallEnabled);
1531 final String rule = allow ? ALLOW : DENY;
1532 try {
1533 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1534 } catch (NativeDaemonConnectorException e) {
1535 throw e.rethrowAsParcelableException();
1536 }
1537 }
1538
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001539 private static void enforceSystemUid() {
1540 final int uid = Binder.getCallingUid();
1541 if (uid != Process.SYSTEM_UID) {
1542 throw new SecurityException("Only available to AID_SYSTEM");
1543 }
1544 }
1545
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001546 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001547 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1548 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1549 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001550 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001551 } catch (NativeDaemonConnectorException e) {
1552 throw new IllegalStateException(
1553 "Error communicating with native deamon to set interface for pid" + iface, e);
1554 }
1555 }
1556
1557 @Override
1558 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1559 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1560 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001561 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001562 } catch (NativeDaemonConnectorException e) {
1563 throw new IllegalStateException(
1564 "Error communicating with native deamon to clear interface for pid " + pid, e);
1565 }
1566 }
1567
Lorenzo Colitti79751842013-02-28 16:16:03 +09001568 @Override
1569 public void startClatd(String interfaceName) throws IllegalStateException {
1570 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1571
1572 try {
1573 mConnector.execute("clatd", "start", interfaceName);
1574 } catch (NativeDaemonConnectorException e) {
1575 throw e.rethrowAsParcelableException();
1576 }
1577 }
1578
1579 @Override
1580 public void stopClatd() throws IllegalStateException {
1581 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1582
1583 try {
1584 mConnector.execute("clatd", "stop");
1585 } catch (NativeDaemonConnectorException e) {
1586 throw e.rethrowAsParcelableException();
1587 }
1588 }
1589
1590 @Override
1591 public boolean isClatdStarted() {
1592 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1593
1594 final NativeDaemonEvent event;
1595 try {
1596 event = mConnector.execute("clatd", "status");
1597 } catch (NativeDaemonConnectorException e) {
1598 throw e.rethrowAsParcelableException();
1599 }
1600
1601 event.checkCode(ClatdStatusResult);
1602 return event.getMessage().endsWith("started");
1603 }
1604
Mattias Falk8b47b362011-08-23 14:15:13 +02001605 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001606 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001607 public void monitor() {
1608 if (mConnector != null) {
1609 mConnector.monitor();
1610 }
1611 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001612
1613 @Override
1614 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1615 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1616
Robert Greenwalt470fd722012-01-18 12:51:15 -08001617 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1618 mConnector.dump(fd, pw, args);
1619 pw.println();
1620
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001621 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1622
1623 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001624 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1625 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001626 }
1627
1628 synchronized (mUidRejectOnQuota) {
1629 pw.print("UID reject on quota ifaces: [");
1630 final int size = mUidRejectOnQuota.size();
1631 for (int i = 0; i < size; i++) {
1632 pw.print(mUidRejectOnQuota.keyAt(i));
1633 if (i < size - 1) pw.print(",");
1634 }
1635 pw.println("]");
1636 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001637
1638 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001639 }
San Mehat873f2142010-01-14 10:25:07 -08001640}