blob: 82cc54007e4d7b1151aca02dec8cc9ebac9cc420 [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;
Chad Brubakercca54c42013-06-27 17:41:38 -070035import static com.android.server.NetworkManagementService.NetdResponseCode.GetMarkResult;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070036import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
San Mehat873f2142010-01-14 10:25:07 -080038import android.content.Context;
San Mehat4d02d002010-01-22 16:07:46 -080039import android.net.INetworkManagementEventObserver;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070040import android.net.InterfaceConfiguration;
Robert Greenwalted126402011-01-28 15:34:55 -080041import android.net.LinkAddress;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070042import android.net.NetworkStats;
Robert Greenwalted126402011-01-28 15:34:55 -080043import android.net.NetworkUtils;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070044import android.net.RouteInfo;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080045import android.net.wifi.WifiConfiguration;
46import android.net.wifi.WifiConfiguration.KeyMgmt;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070047import android.os.BatteryStats;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070048import android.os.Binder;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070049import android.os.Handler;
San Mehat873f2142010-01-14 10:25:07 -080050import android.os.INetworkManagementService;
Jeff Sharkeyf56e2432012-09-06 17:54:29 -070051import android.os.Process;
Jeff Sharkey3df273e2011-12-15 15:47:12 -080052import android.os.RemoteCallbackList;
53import android.os.RemoteException;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070054import android.os.ServiceManager;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070055import android.os.SystemClock;
Marco Nelissen62dbb222010-02-18 10:56:30 -080056import android.os.SystemProperties;
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -080057import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070059import android.util.SparseBooleanArray;
San Mehat873f2142010-01-14 10:25:07 -080060
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070061import com.android.internal.app.IBatteryStats;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.net.NetworkStatsFactory;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -070063import com.android.internal.util.Preconditions;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080064import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -070065import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070066import com.android.server.net.LockdownVpnTracker;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070067import com.google.android.collect.Maps;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070068
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070069import java.io.BufferedReader;
70import java.io.DataInputStream;
San Mehat873f2142010-01-14 10:25:07 -080071import java.io.File;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070072import java.io.FileDescriptor;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070073import java.io.FileInputStream;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070074import java.io.IOException;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070075import java.io.InputStreamReader;
Jeff Sharkey47eb1022011-08-25 17:48:52 -070076import java.io.PrintWriter;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070077import java.net.Inet4Address;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070078import java.net.InetAddress;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070079import java.net.InterfaceAddress;
80import java.net.NetworkInterface;
81import java.net.SocketException;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070082import java.util.ArrayList;
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -070083import java.util.Collection;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -070084import java.util.HashMap;
85import java.util.Map;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070086import java.util.NoSuchElementException;
87import java.util.StringTokenizer;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -070088import java.util.concurrent.CountDownLatch;
San Mehat873f2142010-01-14 10:25:07 -080089
90/**
91 * @hide
92 */
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070093public class NetworkManagementService extends INetworkManagementService.Stub
94 implements Watchdog.Monitor {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070095 private static final String TAG = "NetworkManagementService";
Dianne Hackborncef65ee2010-09-30 18:27:22 -070096 private static final boolean DBG = false;
Kenny Root305bcbf2010-09-03 07:56:38 -070097 private static final String NETD_TAG = "NetdConnector";
Lorenzo Colitti7421a012013-08-20 22:51:24 +090098 private static final String NETD_SOCKET_NAME = "netd";
Kenny Root305bcbf2010-09-03 07:56:38 -070099
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800100 private static final String ADD = "add";
101 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700102
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700103 private static final String ALLOW = "allow";
104 private static final String DENY = "deny";
105
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700106 private static final String DEFAULT = "default";
107 private static final String SECONDARY = "secondary";
108
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700109 /**
110 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
111 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
112 */
113 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
114
San Mehat873f2142010-01-14 10:25:07 -0800115 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700116 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800117 public static final int InterfaceListResult = 110;
118 public static final int TetherInterfaceListResult = 111;
119 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800120 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800121
122 public static final int TetherStatusResult = 210;
123 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800124 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800125 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700126 public static final int InterfaceRxCounterResult = 216;
127 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700128 public static final int QuotaCounterResult = 220;
129 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800130 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900131 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700132 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800133
134 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700135 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700136 public static final int InterfaceClassActivity = 613;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900137 public static final int InterfaceAddressChange = 614;
San Mehat873f2142010-01-14 10:25:07 -0800138 }
139
140 /**
141 * Binder context for this service
142 */
143 private Context mContext;
144
145 /**
146 * connector object for communicating with netd
147 */
148 private NativeDaemonConnector mConnector;
149
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700150 private final Handler mMainHandler = new Handler();
151
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700152 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700153 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700154
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800155 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
156 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800157
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700158 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
159
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700160 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700161 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700162 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700163 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700164 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700165 /** Set of UIDs with active reject rules. */
166 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
167
Haoyu Bai04124232012-06-28 15:26:19 -0700168 private Object mIdleTimerLock = new Object();
169 /** Set of interfaces with active idle timers. */
170 private static class IdleTimerParams {
171 public final int timeout;
172 public final String label;
173 public int networkCount;
174
175 IdleTimerParams(int timeout, String label) {
176 this.timeout = timeout;
177 this.label = label;
178 this.networkCount = 1;
179 }
180 }
181 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
182
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700183 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700184 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700185
San Mehat873f2142010-01-14 10:25:07 -0800186 /**
187 * Constructs a new NetworkManagementService instance
188 *
189 * @param context Binder context for this service
190 */
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900191 private NetworkManagementService(Context context, String socket) {
San Mehat873f2142010-01-14 10:25:07 -0800192 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800193
Marco Nelissen62dbb222010-02-18 10:56:30 -0800194 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
195 return;
196 }
197
San Mehat873f2142010-01-14 10:25:07 -0800198 mConnector = new NativeDaemonConnector(
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900199 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700200 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700201
202 // Add ourself to the Watchdog monitors.
203 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700204 }
205
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900206 static NetworkManagementService create(Context context,
207 String socket) throws InterruptedException {
208 final NetworkManagementService service = new NetworkManagementService(context, socket);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700209 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700210 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
211 service.mThread.start();
212 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700213 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700214 if (DBG) Slog.d(TAG, "Connected");
215 return service;
San Mehat873f2142010-01-14 10:25:07 -0800216 }
217
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900218 public static NetworkManagementService create(Context context) throws InterruptedException {
219 return create(context, NETD_SOCKET_NAME);
220 }
221
Jeff Sharkey350083e2011-06-29 10:45:16 -0700222 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700223 prepareNativeDaemon();
224 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700225 }
226
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800227 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800228 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800229 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800230 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800231 }
232
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800233 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800234 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800235 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800237 }
238
239 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700240 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800241 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800243 final int length = mObservers.beginBroadcast();
244 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800245 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800246 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
247 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900248 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700249 }
250 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800251 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700252 }
253
254 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700255 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700256 * (typically, an Ethernet cable has been plugged-in or unplugged).
257 */
258 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800259 final int length = mObservers.beginBroadcast();
260 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700261 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800262 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
263 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900264 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800265 }
266 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800267 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800268 }
269
270 /**
271 * Notify our observers of an interface addition.
272 */
273 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800274 final int length = mObservers.beginBroadcast();
275 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800276 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800277 mObservers.getBroadcastItem(i).interfaceAdded(iface);
278 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900279 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800280 }
281 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800282 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800283 }
284
285 /**
286 * Notify our observers of an interface removal.
287 */
288 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700289 // netd already clears out quota and alerts for removed ifaces; update
290 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700291 mActiveAlerts.remove(iface);
292 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700293
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800294 final int length = mObservers.beginBroadcast();
295 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800296 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800297 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
298 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900299 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800300 }
301 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800302 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800303 }
304
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700305 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700306 * Notify our observers of a limit reached.
307 */
308 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800309 final int length = mObservers.beginBroadcast();
310 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700311 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800312 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
313 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900314 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700315 }
316 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800317 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700318 }
319
320 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700321 * Notify our observers of a change in the data activity state of the interface
322 */
323 private void notifyInterfaceClassActivity(String label, boolean active) {
324 final int length = mObservers.beginBroadcast();
325 for (int i = 0; i < length; i++) {
326 try {
327 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
328 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900329 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700330 }
331 }
332 mObservers.finishBroadcast();
333 }
334
335 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700336 * Prepare native daemon once connected, enabling modules and pushing any
337 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700338 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700339 private void prepareNativeDaemon() {
340 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700341
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700342 // only enable bandwidth control when support exists
343 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
344 if (hasKernelSupport) {
345 Slog.d(TAG, "enabling bandwidth control");
346 try {
347 mConnector.execute("bandwidth", "enable");
348 mBandwidthControlEnabled = true;
349 } catch (NativeDaemonConnectorException e) {
350 Log.wtf(TAG, "problem enabling bandwidth controls", e);
351 }
352 } else {
353 Slog.d(TAG, "not enabling bandwidth control");
354 }
355
356 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
357
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700358 if (mBandwidthControlEnabled) {
359 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700360 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700361 .noteNetworkStatsEnabled();
362 } catch (RemoteException e) {
363 }
364 }
365
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700366 // push any existing quota or UID rules
367 synchronized (mQuotaLock) {
368 int size = mActiveQuotas.size();
369 if (size > 0) {
370 Slog.d(TAG, "pushing " + size + " active quota rules");
371 final HashMap<String, Long> activeQuotas = mActiveQuotas;
372 mActiveQuotas = Maps.newHashMap();
373 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
374 setInterfaceQuota(entry.getKey(), entry.getValue());
375 }
376 }
377
378 size = mActiveAlerts.size();
379 if (size > 0) {
380 Slog.d(TAG, "pushing " + size + " active alert rules");
381 final HashMap<String, Long> activeAlerts = mActiveAlerts;
382 mActiveAlerts = Maps.newHashMap();
383 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
384 setInterfaceAlert(entry.getKey(), entry.getValue());
385 }
386 }
387
388 size = mUidRejectOnQuota.size();
389 if (size > 0) {
390 Slog.d(TAG, "pushing " + size + " active uid rules");
391 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
392 mUidRejectOnQuota = new SparseBooleanArray();
393 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
394 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
395 }
396 }
397 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700398
399 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700400 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700401 }
San Mehat4d02d002010-01-22 16:07:46 -0800402
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900403 /**
404 * Notify our observers of a new or updated interface address.
405 */
406 private void notifyAddressUpdated(String address, String iface, int flags, int scope) {
407 final int length = mObservers.beginBroadcast();
408 for (int i = 0; i < length; i++) {
409 try {
410 mObservers.getBroadcastItem(i).addressUpdated(address, iface, flags, scope);
411 } catch (RemoteException e) {
412 } catch (RuntimeException e) {
413 }
414 }
415 mObservers.finishBroadcast();
416 }
417
418 /**
419 * Notify our observers of a deleted interface address.
420 */
421 private void notifyAddressRemoved(String address, String iface, int flags, int scope) {
422 final int length = mObservers.beginBroadcast();
423 for (int i = 0; i < length; i++) {
424 try {
425 mObservers.getBroadcastItem(i).addressRemoved(address, iface, flags, scope);
426 } catch (RemoteException e) {
427 } catch (RuntimeException e) {
428 }
429 }
430 mObservers.finishBroadcast();
431 }
432
San Mehat873f2142010-01-14 10:25:07 -0800433 //
434 // Netd Callback handling
435 //
436
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700437 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
438 @Override
San Mehat873f2142010-01-14 10:25:07 -0800439 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700440 // event is dispatched from internal NDC thread, so we prepare the
441 // daemon back on main thread.
442 if (mConnectedSignal != null) {
443 mConnectedSignal.countDown();
444 mConnectedSignal = null;
445 } else {
446 mMainHandler.post(new Runnable() {
447 @Override
448 public void run() {
449 prepareNativeDaemon();
450 }
451 });
452 }
San Mehat873f2142010-01-14 10:25:07 -0800453 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700454
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700455 @Override
San Mehat873f2142010-01-14 10:25:07 -0800456 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700457 switch (code) {
458 case NetdResponseCode.InterfaceChange:
459 /*
460 * a network interface change occured
461 * Format: "NNN Iface added <name>"
462 * "NNN Iface removed <name>"
463 * "NNN Iface changed <name> <up/down>"
464 * "NNN Iface linkstatus <name> <up/down>"
465 */
466 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
467 throw new IllegalStateException(
468 String.format("Invalid event from daemon (%s)", raw));
469 }
470 if (cooked[2].equals("added")) {
471 notifyInterfaceAdded(cooked[3]);
472 return true;
473 } else if (cooked[2].equals("removed")) {
474 notifyInterfaceRemoved(cooked[3]);
475 return true;
476 } else if (cooked[2].equals("changed") && cooked.length == 5) {
477 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
478 return true;
479 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
480 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
481 return true;
482 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800483 throw new IllegalStateException(
484 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700485 // break;
486 case NetdResponseCode.BandwidthControl:
487 /*
488 * Bandwidth control needs some attention
489 * Format: "NNN limit alert <alertName> <ifaceName>"
490 */
491 if (cooked.length < 5 || !cooked[1].equals("limit")) {
492 throw new IllegalStateException(
493 String.format("Invalid event from daemon (%s)", raw));
494 }
495 if (cooked[2].equals("alert")) {
496 notifyLimitReached(cooked[3], cooked[4]);
497 return true;
498 }
499 throw new IllegalStateException(
500 String.format("Invalid event from daemon (%s)", raw));
501 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700502 case NetdResponseCode.InterfaceClassActivity:
503 /*
504 * An network interface class state changed (active/idle)
505 * Format: "NNN IfaceClass <active/idle> <label>"
506 */
507 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
508 throw new IllegalStateException(
509 String.format("Invalid event from daemon (%s)", raw));
510 }
511 boolean isActive = cooked[2].equals("active");
512 notifyInterfaceClassActivity(cooked[3], isActive);
513 return true;
514 // break;
Lorenzo Colitti5c7daac2013-08-05 10:39:37 +0900515 case NetdResponseCode.InterfaceAddressChange:
516 /*
517 * A network address change occurred
518 * Format: "NNN Address updated <addr> <iface> <flags> <scope>"
519 * "NNN Address removed <addr> <iface> <flags> <scope>"
520 */
521 String msg = String.format("Invalid event from daemon (%s)", raw);
522 if (cooked.length < 6 || !cooked[1].equals("Address")) {
523 throw new IllegalStateException(msg);
524 }
525
526 int flags, scope;
527 try {
528 flags = Integer.parseInt(cooked[5]);
529 scope = Integer.parseInt(cooked[6]);
530 } catch(NumberFormatException e) {
531 throw new IllegalStateException(msg);
532 }
533
534 if (cooked[2].equals("updated")) {
535 notifyAddressUpdated(cooked[3], cooked[4], flags, scope);
536 } else {
537 notifyAddressRemoved(cooked[3], cooked[4], flags, scope);
538 }
539 return true;
540 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700541 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800542 }
543 return false;
San Mehat873f2142010-01-14 10:25:07 -0800544 }
545 }
546
San Mehated4fc8a2010-01-22 12:28:36 -0800547
San Mehat873f2142010-01-14 10:25:07 -0800548 //
549 // INetworkManagementService members
550 //
551
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800552 @Override
553 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800554 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700555 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800556 return NativeDaemonEvent.filterMessageList(
557 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700558 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800559 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700560 }
San Mehated4fc8a2010-01-22 12:28:36 -0800561 }
562
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800563 @Override
564 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800565 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800566
567 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700568 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800569 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700570 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800571 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700572 }
San Mehated4fc8a2010-01-22 12:28:36 -0800573
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800574 event.checkCode(InterfaceGetCfgResult);
575
576 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
577 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800578
Kenny Roota80ce062010-06-01 13:23:53 -0700579 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800580 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700581 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800582 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800583 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800584 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700585 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800586 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800587 } catch (IllegalArgumentException iae) {
588 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700589 }
590
591 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800592 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800593 } catch (NumberFormatException nfe) {
594 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700595 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800596
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800597 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
598 while (st.hasMoreTokens()) {
599 cfg.setFlag(st.nextToken());
600 }
Kenny Roota80ce062010-06-01 13:23:53 -0700601 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800602 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800603 }
San Mehated4fc8a2010-01-22 12:28:36 -0800604 return cfg;
605 }
606
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800607 @Override
608 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800609 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800610 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800611 if (linkAddr == null || linkAddr.getAddress() == null) {
612 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800613 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800614
615 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800616 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800617 linkAddr.getNetworkPrefixLength());
618 for (String flag : cfg.getFlags()) {
619 cmd.appendArg(flag);
620 }
621
Kenny Roota80ce062010-06-01 13:23:53 -0700622 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800623 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700624 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800625 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700626 }
San Mehat873f2142010-01-14 10:25:07 -0800627 }
628
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800629 @Override
630 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800631 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800632 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800633 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800634 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700635 }
636
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800637 @Override
638 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800639 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800640 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800641 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800642 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700643 }
644
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800645 @Override
646 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800647 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700648 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800649 mConnector.execute(
650 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700651 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800652 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700653 }
654 }
655
Irfan Sherifff5600612011-06-16 10:26:28 -0700656 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
657 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800658 @Override
659 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800660 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700661 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800662 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700663 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800664 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700665 }
666 }
667
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800668 @Override
669 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800670 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700671 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800672 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700673 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800674 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700675 }
676 }
677
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800678 @Override
679 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800680 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700681 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800682 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700683 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800684 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700685 }
686 }
687
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800688 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700689 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800690 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700691 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700692 }
693
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800694 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700695 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800696 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700697 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700698 }
699
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800700 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700701 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800702 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700703 modifyRoute(interfaceName, ADD, route, SECONDARY);
704 }
705
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800706 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700707 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800708 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700709 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
710 }
711
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800712 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
713 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700714
715 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800716 final LinkAddress la = route.getDestination();
717 cmd.appendArg(la.getAddress().getHostAddress());
718 cmd.appendArg(la.getNetworkPrefixLength());
719
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700720 if (route.getGateway() == null) {
721 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800722 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700723 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800724 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700725 }
726 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800727 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700728 }
729
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800730 try {
731 mConnector.execute(cmd);
732 } catch (NativeDaemonConnectorException e) {
733 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700734 }
735 }
736
737 private ArrayList<String> readRouteList(String filename) {
738 FileInputStream fstream = null;
739 ArrayList<String> list = new ArrayList<String>();
740
741 try {
742 fstream = new FileInputStream(filename);
743 DataInputStream in = new DataInputStream(fstream);
744 BufferedReader br = new BufferedReader(new InputStreamReader(in));
745 String s;
746
747 // throw away the title line
748
749 while (((s = br.readLine()) != null) && (s.length() != 0)) {
750 list.add(s);
751 }
752 } catch (IOException ex) {
753 // return current list, possibly empty
754 } finally {
755 if (fstream != null) {
756 try {
757 fstream.close();
758 } catch (IOException ex) {}
759 }
760 }
761
762 return list;
763 }
764
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800765 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700766 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800767 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700768 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
769
770 // v4 routes listed as:
771 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
772 for (String s : readRouteList("/proc/net/route")) {
773 String[] fields = s.split("\t");
774
775 if (fields.length > 7) {
776 String iface = fields[0];
777
778 if (interfaceName.equals(iface)) {
779 String dest = fields[1];
780 String gate = fields[2];
781 String flags = fields[3]; // future use?
782 String mask = fields[7];
783 try {
784 // address stored as a hex string, ex: 0014A8C0
785 InetAddress destAddr =
786 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
787 int prefixLength =
788 NetworkUtils.netmaskIntToPrefixLength(
789 (int)Long.parseLong(mask, 16));
790 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
791
792 // address stored as a hex string, ex 0014A8C0
793 InetAddress gatewayAddr =
794 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
795
Wink Saville7b5fd052013-03-15 05:07:04 +0000796 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700797 routes.add(route);
798 } catch (Exception e) {
799 Log.e(TAG, "Error parsing route " + s + " : " + e);
800 continue;
801 }
802 }
803 }
804 }
805
806 // v6 routes listed as:
807 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
808 for (String s : readRouteList("/proc/net/ipv6_route")) {
809 String[]fields = s.split("\\s+");
810 if (fields.length > 9) {
811 String iface = fields[9].trim();
812 if (interfaceName.equals(iface)) {
813 String dest = fields[0];
814 String prefix = fields[1];
815 String gate = fields[4];
816
817 try {
818 // prefix length stored as a hex string, ex 40
819 int prefixLength = Integer.parseInt(prefix, 16);
820
821 // address stored as a 32 char hex string
822 // ex fe800000000000000000000000000000
823 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
824 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
825
826 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
827
Wink Saville7b5fd052013-03-15 05:07:04 +0000828 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700829 routes.add(route);
830 } catch (Exception e) {
831 Log.e(TAG, "Error parsing route " + s + " : " + e);
832 continue;
833 }
834 }
835 }
836 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800837 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700838 }
839
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800840 @Override
sy.yun9d9b74a2013-09-02 05:24:09 +0900841 public void setMtu(String iface, int mtu) {
842 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
843
844 final NativeDaemonEvent event;
845 try {
846 event = mConnector.execute("interface", "setmtu", iface, mtu);
847 } catch (NativeDaemonConnectorException e) {
848 throw e.rethrowAsParcelableException();
849 }
850 }
851
852 @Override
San Mehat873f2142010-01-14 10:25:07 -0800853 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800854 // TODO: remove from aidl if nobody calls externally
855 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800856
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800858 }
859
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800860 @Override
San Mehat873f2142010-01-14 10:25:07 -0800861 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800862 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800863
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800864 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700865 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800866 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700867 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800868 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700869 }
San Mehat873f2142010-01-14 10:25:07 -0800870
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800871 // 211 Forwarding enabled
872 event.checkCode(IpFwdStatusResult);
873 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800874 }
875
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800876 @Override
877 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800878 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800879 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800880 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800881 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800882 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800883 }
San Mehat873f2142010-01-14 10:25:07 -0800884 }
885
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800886 @Override
887 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800888 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700889 // cmd is "tether start first_start first_stop second_start second_stop ..."
890 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800891
892 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700893 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800894 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700895 }
Kenny Roota80ce062010-06-01 13:23:53 -0700896
897 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800898 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700899 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800900 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700901 }
San Mehat873f2142010-01-14 10:25:07 -0800902 }
903
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800904 @Override
905 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800906 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700907 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700909 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800910 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700911 }
San Mehat873f2142010-01-14 10:25:07 -0800912 }
913
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800914 @Override
915 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800916 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800917
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800918 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700919 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800920 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700921 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800922 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700923 }
San Mehat873f2142010-01-14 10:25:07 -0800924
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800925 // 210 Tethering services started
926 event.checkCode(TetherStatusResult);
927 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800928 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700929
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800930 @Override
931 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800932 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700933 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800934 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700935 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800936 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700937 }
San Mehat873f2142010-01-14 10:25:07 -0800938 }
939
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800940 @Override
San Mehat873f2142010-01-14 10:25:07 -0800941 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800942 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700943 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800944 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700945 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800946 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700947 }
San Mehat873f2142010-01-14 10:25:07 -0800948 }
949
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800950 @Override
951 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800954 return NativeDaemonEvent.filterMessageList(
955 mConnector.executeForList("tether", "interface", "list"),
956 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700957 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800958 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700959 }
San Mehat873f2142010-01-14 10:25:07 -0800960 }
961
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800962 @Override
963 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800964 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800965
966 final Command cmd = new Command("tether", "dns", "set");
967 for (String s : dns) {
968 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
969 }
970
San Mehat873f2142010-01-14 10:25:07 -0800971 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800972 mConnector.execute(cmd);
973 } catch (NativeDaemonConnectorException e) {
974 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800975 }
976 }
977
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800978 @Override
979 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800980 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700981 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800982 return NativeDaemonEvent.filterMessageList(
983 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700984 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800985 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700986 }
San Mehat873f2142010-01-14 10:25:07 -0800987 }
988
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800989 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700990 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800991 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700992
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800993 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
994 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800995 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800996 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800997 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800998 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
999 .getInterfaceAddresses();
1000 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001001 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001002 InetAddress addr = NetworkUtils.getNetworkPart(
1003 ia.getAddress(), ia.getNetworkPrefixLength());
1004 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -08001005 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001006 }
1007
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001008 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001009 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001010 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001011 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001012 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001013 }
1014
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001015 @Override
1016 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001017 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001018 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001019 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001020 } catch (SocketException e) {
1021 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001022 }
San Mehat873f2142010-01-14 10:25:07 -08001023 }
1024
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001025 @Override
1026 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001027 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001028 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001029 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001030 } catch (SocketException e) {
1031 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001032 }
San Mehat873f2142010-01-14 10:25:07 -08001033 }
San Mehat72759df2010-01-19 13:50:37 -08001034
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001035 @Override
1036 public String[] listTtys() {
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 return NativeDaemonEvent.filterMessageList(
1040 mConnector.executeForList("list_ttys"), TtyListResult);
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 }
San Mehat72759df2010-01-19 13:50:37 -08001044 }
1045
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001046 @Override
1047 public void attachPppd(
1048 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001049 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001050 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001051 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001052 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1053 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1054 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001055 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001056 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001057 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001058 }
1059 }
1060
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001061 @Override
1062 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001063 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001064 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001065 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001066 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001067 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001068 }
San Mehat72759df2010-01-19 13:50:37 -08001069 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001070
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001071 @Override
1072 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001073 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001074 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001075 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001076 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001077 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001078 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001079 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001080 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001081 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001082 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001083 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001084 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001085 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001086 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001087 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001088 }
1089
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001090 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001091 switch (wifiConfig.getAuthType()) {
1092 case KeyMgmt.WPA_PSK:
1093 return "wpa-psk";
1094 case KeyMgmt.WPA2_PSK:
1095 return "wpa2-psk";
1096 default:
1097 return "open";
1098 }
1099 }
1100
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001101 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001102 @Override
1103 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001104 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001105 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001106 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001107 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001108 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001109 }
1110 }
1111
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001112 @Override
1113 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001114 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001115 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001116 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001117 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001118 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001119 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001120 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001121 }
1122
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001123 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001124 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001125 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001126 try {
1127 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001128 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001129 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001130 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001131 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001132 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001133 }
1134 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001135 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001136 }
1137 }
San Mehat91cac642010-03-31 14:31:36 -07001138
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001139 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001140 public void addIdleTimer(String iface, int timeout, String label) {
1141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1142
1143 if (DBG) Slog.d(TAG, "Adding idletimer");
1144
1145 synchronized (mIdleTimerLock) {
1146 IdleTimerParams params = mActiveIdleTimers.get(iface);
1147 if (params != null) {
1148 // the interface already has idletimer, update network count
1149 params.networkCount++;
1150 return;
1151 }
1152
1153 try {
1154 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1155 } catch (NativeDaemonConnectorException e) {
1156 throw e.rethrowAsParcelableException();
1157 }
1158 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1159 }
1160 }
1161
1162 @Override
1163 public void removeIdleTimer(String iface) {
1164 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1165
1166 if (DBG) Slog.d(TAG, "Removing idletimer");
1167
1168 synchronized (mIdleTimerLock) {
1169 IdleTimerParams params = mActiveIdleTimers.get(iface);
1170 if (params == null || --(params.networkCount) > 0) {
1171 return;
1172 }
1173
1174 try {
1175 mConnector.execute("idletimer", "remove", iface,
1176 Integer.toString(params.timeout), params.label);
1177 } catch (NativeDaemonConnectorException e) {
1178 throw e.rethrowAsParcelableException();
1179 }
1180 mActiveIdleTimers.remove(iface);
1181 }
1182 }
1183
1184 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001185 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001186 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001187 try {
1188 return mStatsFactory.readNetworkStatsSummaryDev();
1189 } catch (IOException e) {
1190 throw new IllegalStateException(e);
1191 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001192 }
1193
1194 @Override
1195 public NetworkStats getNetworkStatsSummaryXt() {
1196 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001197 try {
1198 return mStatsFactory.readNetworkStatsSummaryXt();
1199 } catch (IOException e) {
1200 throw new IllegalStateException(e);
1201 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001202 }
1203
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001204 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001205 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001206 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001207 try {
1208 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1209 } catch (IOException e) {
1210 throw new IllegalStateException(e);
1211 }
San Mehat91cac642010-03-31 14:31:36 -07001212 }
1213
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001214 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001215 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001216 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001217
Jeff Sharkey350083e2011-06-29 10:45:16 -07001218 // silently discard when control disabled
1219 // TODO: eventually migrate to be always enabled
1220 if (!mBandwidthControlEnabled) return;
1221
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001222 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001223 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001224 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001225 }
1226
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001227 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001228 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001229 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001230 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001231 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001232 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001233 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001234 }
1235 }
1236
1237 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001238 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001239 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001240
Jeff Sharkey350083e2011-06-29 10:45:16 -07001241 // silently discard when control disabled
1242 // TODO: eventually migrate to be always enabled
1243 if (!mBandwidthControlEnabled) return;
1244
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001245 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001246 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001247 // TODO: eventually consider throwing
1248 return;
1249 }
1250
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001251 mActiveQuotas.remove(iface);
1252 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001253
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001254 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001255 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001256 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001257 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001258 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001259 }
1260 }
1261 }
1262
1263 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001264 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001265 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001266
1267 // silently discard when control disabled
1268 // TODO: eventually migrate to be always enabled
1269 if (!mBandwidthControlEnabled) return;
1270
1271 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001272 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001273 throw new IllegalStateException("setting alert requires existing quota on iface");
1274 }
1275
1276 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001277 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001278 throw new IllegalStateException("iface " + iface + " already has alert");
1279 }
1280
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001281 try {
1282 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001283 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001284 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001285 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001286 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001287 }
1288 }
1289 }
1290
1291 @Override
1292 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001293 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001294
1295 // silently discard when control disabled
1296 // TODO: eventually migrate to be always enabled
1297 if (!mBandwidthControlEnabled) return;
1298
1299 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001300 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001301 // TODO: eventually consider throwing
1302 return;
1303 }
1304
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001305 try {
1306 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001307 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001308 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001309 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001310 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001311 }
1312 }
1313 }
1314
1315 @Override
1316 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001317 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001318
1319 // silently discard when control disabled
1320 // TODO: eventually migrate to be always enabled
1321 if (!mBandwidthControlEnabled) return;
1322
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001323 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001324 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001325 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001326 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001327 }
1328 }
1329
1330 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001331 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001332 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001333
Jeff Sharkey350083e2011-06-29 10:45:16 -07001334 // silently discard when control disabled
1335 // TODO: eventually migrate to be always enabled
1336 if (!mBandwidthControlEnabled) return;
1337
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001338 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001339 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1340 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1341 // TODO: eventually consider throwing
1342 return;
1343 }
1344
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001345 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001346 mConnector.execute("bandwidth",
1347 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001348 if (rejectOnQuotaInterfaces) {
1349 mUidRejectOnQuota.put(uid, true);
1350 } else {
1351 mUidRejectOnQuota.delete(uid);
1352 }
1353 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001354 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001355 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001356 }
1357 }
1358
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001359 @Override
1360 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001361 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001362 return mBandwidthControlEnabled;
1363 }
1364
1365 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001366 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001367 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001368 try {
1369 return mStatsFactory.readNetworkStatsDetail(uid);
1370 } catch (IOException e) {
1371 throw new IllegalStateException(e);
1372 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001373 }
1374
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001375 @Override
1376 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001377 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001378
1379 if (ifacePairs.length % 2 != 0) {
1380 throw new IllegalArgumentException(
1381 "unexpected ifacePairs; length=" + ifacePairs.length);
1382 }
1383
1384 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1385 for (int i = 0; i < ifacePairs.length; i += 2) {
1386 final String ifaceIn = ifacePairs[i];
1387 final String ifaceOut = ifacePairs[i + 1];
1388 if (ifaceIn != null && ifaceOut != null) {
1389 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1390 }
1391 }
1392 return stats;
1393 }
1394
1395 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001396 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001397 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001398 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001399 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001400 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001401 }
1402
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001403 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001404
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001405 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1406 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1407 tok.nextToken();
1408 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001409
1410 try {
1411 final NetworkStats.Entry entry = new NetworkStats.Entry();
1412 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001413 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001414 entry.set = SET_DEFAULT;
1415 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001416 entry.rxBytes = Long.parseLong(tok.nextToken());
1417 entry.rxPackets = Long.parseLong(tok.nextToken());
1418 entry.txBytes = Long.parseLong(tok.nextToken());
1419 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001420 return entry;
1421 } catch (NumberFormatException e) {
1422 throw new IllegalStateException(
1423 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1424 }
1425 }
1426
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001427 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001428 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001429 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001430 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001431 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001432 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001433 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001434 }
1435 }
1436
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001437 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001438 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001439 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001440
Robert Greenwalt8058f622012-11-09 10:52:27 -08001441 final Command cmd = new Command("resolver", "setifdns", iface,
1442 (domains == null ? "" : domains));
1443
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001444 for (String s : servers) {
1445 InetAddress a = NetworkUtils.numericToInetAddress(s);
1446 if (a.isAnyLocalAddress() == false) {
1447 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001448 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001449 }
1450
1451 try {
1452 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001453 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001454 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001455 }
1456 }
1457
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001458 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001459 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1460 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1461 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001462 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001463 "uid", "add", iface, uid_start, uid_end);
1464 } catch (NativeDaemonConnectorException e) {
1465 throw e.rethrowAsParcelableException();
1466 }
1467 }
1468
1469 @Override
1470 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1471 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1472 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001473 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001474 "uid", "remove", iface, uid_start, uid_end);
1475 } catch (NativeDaemonConnectorException e) {
1476 throw e.rethrowAsParcelableException();
1477 }
1478 }
1479
1480 @Override
1481 public void setMarkedForwarding(String iface) {
1482 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1483 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001484 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001485 } catch (NativeDaemonConnectorException e) {
1486 throw e.rethrowAsParcelableException();
1487 }
1488 }
1489
1490 @Override
1491 public void clearMarkedForwarding(String iface) {
1492 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1493 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001494 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1495 } catch (NativeDaemonConnectorException e) {
1496 throw e.rethrowAsParcelableException();
1497 }
1498 }
1499
1500 @Override
1501 public int getMarkForUid(int uid) {
1502 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1503 final NativeDaemonEvent event;
1504 try {
1505 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1506 } catch (NativeDaemonConnectorException e) {
1507 throw e.rethrowAsParcelableException();
1508 }
1509 event.checkCode(GetMarkResult);
1510 return Integer.parseInt(event.getMessage());
1511 }
1512
1513 @Override
1514 public int getMarkForProtect() {
1515 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1516 final NativeDaemonEvent event;
1517 try {
1518 event = mConnector.execute("interface", "fwmark", "get", "protect");
1519 } catch (NativeDaemonConnectorException e) {
1520 throw e.rethrowAsParcelableException();
1521 }
1522 event.checkCode(GetMarkResult);
1523 return Integer.parseInt(event.getMessage());
1524 }
1525
1526 @Override
1527 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1528 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1529 try {
1530 LinkAddress dest = route.getDestination();
1531 mConnector.execute("interface", "fwmark", "route", "add", iface,
1532 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1533 } catch (NativeDaemonConnectorException e) {
1534 throw e.rethrowAsParcelableException();
1535 }
1536 }
1537
1538 @Override
1539 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1540 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1541 try {
1542 LinkAddress dest = route.getDestination();
1543 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1544 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001545 } catch (NativeDaemonConnectorException e) {
1546 throw e.rethrowAsParcelableException();
1547 }
1548 }
1549
1550 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001551 public void setHostExemption(LinkAddress host) {
1552 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1553 try {
1554 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1555 } catch (NativeDaemonConnectorException e) {
1556 throw e.rethrowAsParcelableException();
1557 }
1558 }
1559
1560 @Override
1561 public void clearHostExemption(LinkAddress host) {
1562 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1563 try {
1564 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1565 } catch (NativeDaemonConnectorException e) {
1566 throw e.rethrowAsParcelableException();
1567 }
1568 }
1569
1570 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001571 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1572 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1573 try {
1574 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1575 } catch (NativeDaemonConnectorException e) {
1576 throw e.rethrowAsParcelableException();
1577 }
1578 }
1579
1580 @Override
1581 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1582 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1583 try {
1584 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1585 } catch (NativeDaemonConnectorException e) {
1586 throw e.rethrowAsParcelableException();
1587 }
1588 }
1589
1590 @Override
1591 public void clearDnsInterfaceMaps() {
1592 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1593 try {
1594 mConnector.execute("resolver", "clearifacemapping");
1595 } catch (NativeDaemonConnectorException e) {
1596 throw e.rethrowAsParcelableException();
1597 }
1598 }
1599
1600
1601 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001602 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001603 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001604 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001605 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001606 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001607 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001608 }
1609 }
1610
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001611 @Override
1612 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001613 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001614 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001615 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001616 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001617 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001618 }
1619 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001620
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001621 @Override
1622 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001623 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001624 try {
1625 mConnector.execute("firewall", enabled ? "enable" : "disable");
1626 mFirewallEnabled = enabled;
1627 } catch (NativeDaemonConnectorException e) {
1628 throw e.rethrowAsParcelableException();
1629 }
1630 }
1631
1632 @Override
1633 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001634 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001635 return mFirewallEnabled;
1636 }
1637
1638 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001639 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001640 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001641 Preconditions.checkState(mFirewallEnabled);
1642 final String rule = allow ? ALLOW : DENY;
1643 try {
1644 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1645 } catch (NativeDaemonConnectorException e) {
1646 throw e.rethrowAsParcelableException();
1647 }
1648 }
1649
1650 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001651 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001652 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001653 Preconditions.checkState(mFirewallEnabled);
1654 final String rule = allow ? ALLOW : DENY;
1655 try {
1656 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1657 } catch (NativeDaemonConnectorException e) {
1658 throw e.rethrowAsParcelableException();
1659 }
1660 }
1661
1662 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001663 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001664 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001665 Preconditions.checkState(mFirewallEnabled);
1666 final String rule = allow ? ALLOW : DENY;
1667 try {
1668 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1669 } catch (NativeDaemonConnectorException e) {
1670 throw e.rethrowAsParcelableException();
1671 }
1672 }
1673
1674 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001675 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001676 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001677 Preconditions.checkState(mFirewallEnabled);
1678 final String rule = allow ? ALLOW : DENY;
1679 try {
1680 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1681 } catch (NativeDaemonConnectorException e) {
1682 throw e.rethrowAsParcelableException();
1683 }
1684 }
1685
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001686 private static void enforceSystemUid() {
1687 final int uid = Binder.getCallingUid();
1688 if (uid != Process.SYSTEM_UID) {
1689 throw new SecurityException("Only available to AID_SYSTEM");
1690 }
1691 }
1692
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001693 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001694 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1695 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1696 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001697 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001698 } catch (NativeDaemonConnectorException e) {
1699 throw new IllegalStateException(
1700 "Error communicating with native deamon to set interface for pid" + iface, e);
1701 }
1702 }
1703
1704 @Override
1705 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1706 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1707 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001708 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001709 } catch (NativeDaemonConnectorException e) {
1710 throw new IllegalStateException(
1711 "Error communicating with native deamon to clear interface for pid " + pid, e);
1712 }
1713 }
1714
Lorenzo Colitti79751842013-02-28 16:16:03 +09001715 @Override
1716 public void startClatd(String interfaceName) throws IllegalStateException {
1717 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1718
1719 try {
1720 mConnector.execute("clatd", "start", interfaceName);
1721 } catch (NativeDaemonConnectorException e) {
1722 throw e.rethrowAsParcelableException();
1723 }
1724 }
1725
1726 @Override
1727 public void stopClatd() throws IllegalStateException {
1728 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1729
1730 try {
1731 mConnector.execute("clatd", "stop");
1732 } catch (NativeDaemonConnectorException e) {
1733 throw e.rethrowAsParcelableException();
1734 }
1735 }
1736
1737 @Override
1738 public boolean isClatdStarted() {
1739 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1740
1741 final NativeDaemonEvent event;
1742 try {
1743 event = mConnector.execute("clatd", "status");
1744 } catch (NativeDaemonConnectorException e) {
1745 throw e.rethrowAsParcelableException();
1746 }
1747
1748 event.checkCode(ClatdStatusResult);
1749 return event.getMessage().endsWith("started");
1750 }
1751
Mattias Falk8b47b362011-08-23 14:15:13 +02001752 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001753 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001754 public void monitor() {
1755 if (mConnector != null) {
1756 mConnector.monitor();
1757 }
1758 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001759
1760 @Override
1761 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1762 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1763
Robert Greenwalt470fd722012-01-18 12:51:15 -08001764 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1765 mConnector.dump(fd, pw, args);
1766 pw.println();
1767
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001768 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1769
1770 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001771 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1772 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001773 }
1774
1775 synchronized (mUidRejectOnQuota) {
1776 pw.print("UID reject on quota ifaces: [");
1777 final int size = mUidRejectOnQuota.size();
1778 for (int i = 0; i < size; i++) {
1779 pw.print(mUidRejectOnQuota.keyAt(i));
1780 if (i < size - 1) pw.print(",");
1781 }
1782 pw.println("]");
1783 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001784
1785 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001786 }
San Mehat873f2142010-01-14 10:25:07 -08001787}