blob: dfcab292d653cf30119c49e2e07b0ed5b1eb0d39 [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
San Mehat873f2142010-01-14 10:25:07 -0800841 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800842 // TODO: remove from aidl if nobody calls externally
843 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800844
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800846 }
847
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800848 @Override
San Mehat873f2142010-01-14 10:25:07 -0800849 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800850 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800851
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800852 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700853 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800854 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700855 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800856 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700857 }
San Mehat873f2142010-01-14 10:25:07 -0800858
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800859 // 211 Forwarding enabled
860 event.checkCode(IpFwdStatusResult);
861 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800862 }
863
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800864 @Override
865 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800866 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800867 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800868 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800869 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800870 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800871 }
San Mehat873f2142010-01-14 10:25:07 -0800872 }
873
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800874 @Override
875 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800876 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700877 // cmd is "tether start first_start first_stop second_start second_stop ..."
878 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800879
880 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700881 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800882 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700883 }
Kenny Roota80ce062010-06-01 13:23:53 -0700884
885 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800886 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700887 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800888 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700889 }
San Mehat873f2142010-01-14 10:25:07 -0800890 }
891
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800892 @Override
893 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800894 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700895 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800896 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700897 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800898 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700899 }
San Mehat873f2142010-01-14 10:25:07 -0800900 }
901
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800902 @Override
903 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800904 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800905
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800906 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700907 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800908 event = mConnector.execute("tether", "status");
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
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800913 // 210 Tethering services started
914 event.checkCode(TetherStatusResult);
915 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800916 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700917
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800918 @Override
919 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800920 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700921 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800922 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700923 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800924 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700925 }
San Mehat873f2142010-01-14 10:25:07 -0800926 }
927
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800928 @Override
San Mehat873f2142010-01-14 10:25:07 -0800929 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800930 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700931 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800932 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700933 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800934 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700935 }
San Mehat873f2142010-01-14 10:25:07 -0800936 }
937
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800938 @Override
939 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800940 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700941 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800942 return NativeDaemonEvent.filterMessageList(
943 mConnector.executeForList("tether", "interface", "list"),
944 TetherInterfaceListResult);
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 void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800953
954 final Command cmd = new Command("tether", "dns", "set");
955 for (String s : dns) {
956 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
957 }
958
San Mehat873f2142010-01-14 10:25:07 -0800959 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800960 mConnector.execute(cmd);
961 } catch (NativeDaemonConnectorException e) {
962 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800963 }
964 }
965
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800966 @Override
967 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800968 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700969 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800970 return NativeDaemonEvent.filterMessageList(
971 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700972 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800973 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700974 }
San Mehat873f2142010-01-14 10:25:07 -0800975 }
976
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800977 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700978 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800979 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700980
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800981 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
982 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800983 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800984 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800985 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800986 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
987 .getInterfaceAddresses();
988 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800989 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800990 InetAddress addr = NetworkUtils.getNetworkPart(
991 ia.getAddress(), ia.getNetworkPrefixLength());
992 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800993 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700994 }
995
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800996 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800997 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800998 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800999 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -08001000 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001001 }
1002
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001003 @Override
1004 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001005 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001006 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001007 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001008 } catch (SocketException e) {
1009 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001010 }
San Mehat873f2142010-01-14 10:25:07 -08001011 }
1012
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001013 @Override
1014 public void disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001015 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001016 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -07001017 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001018 } catch (SocketException e) {
1019 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -07001020 }
San Mehat873f2142010-01-14 10:25:07 -08001021 }
San Mehat72759df2010-01-19 13:50:37 -08001022
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001023 @Override
1024 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001025 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001026 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001027 return NativeDaemonEvent.filterMessageList(
1028 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -07001029 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001030 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001031 }
San Mehat72759df2010-01-19 13:50:37 -08001032 }
1033
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001034 @Override
1035 public void attachPppd(
1036 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001037 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -08001038 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001039 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -08001040 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
1041 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
1042 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001043 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -07001044 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001045 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -08001046 }
1047 }
1048
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001049 @Override
1050 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001051 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001052 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001053 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -07001054 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001055 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001056 }
San Mehat72759df2010-01-19 13:50:37 -08001057 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -08001058
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001059 @Override
1060 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -07001061 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001062 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001063 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001064 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001065 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001066 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001067 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001068 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001069 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001070 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001071 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001072 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001073 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001074 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001075 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001076 }
1077
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001078 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001079 switch (wifiConfig.getAuthType()) {
1080 case KeyMgmt.WPA_PSK:
1081 return "wpa-psk";
1082 case KeyMgmt.WPA2_PSK:
1083 return "wpa2-psk";
1084 default:
1085 return "open";
1086 }
1087 }
1088
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001089 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001090 @Override
1091 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001092 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001093 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001094 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001095 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001096 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001097 }
1098 }
1099
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001100 @Override
1101 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001102 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001103 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001104 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001105 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001106 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001107 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001108 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001109 }
1110
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001111 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001112 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001113 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001114 try {
1115 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001116 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001117 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001118 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001119 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001120 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001121 }
1122 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001123 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001124 }
1125 }
San Mehat91cac642010-03-31 14:31:36 -07001126
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001127 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001128 public void addIdleTimer(String iface, int timeout, String label) {
1129 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1130
1131 if (DBG) Slog.d(TAG, "Adding idletimer");
1132
1133 synchronized (mIdleTimerLock) {
1134 IdleTimerParams params = mActiveIdleTimers.get(iface);
1135 if (params != null) {
1136 // the interface already has idletimer, update network count
1137 params.networkCount++;
1138 return;
1139 }
1140
1141 try {
1142 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1143 } catch (NativeDaemonConnectorException e) {
1144 throw e.rethrowAsParcelableException();
1145 }
1146 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1147 }
1148 }
1149
1150 @Override
1151 public void removeIdleTimer(String iface) {
1152 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1153
1154 if (DBG) Slog.d(TAG, "Removing idletimer");
1155
1156 synchronized (mIdleTimerLock) {
1157 IdleTimerParams params = mActiveIdleTimers.get(iface);
1158 if (params == null || --(params.networkCount) > 0) {
1159 return;
1160 }
1161
1162 try {
1163 mConnector.execute("idletimer", "remove", iface,
1164 Integer.toString(params.timeout), params.label);
1165 } catch (NativeDaemonConnectorException e) {
1166 throw e.rethrowAsParcelableException();
1167 }
1168 mActiveIdleTimers.remove(iface);
1169 }
1170 }
1171
1172 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001173 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001174 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001175 try {
1176 return mStatsFactory.readNetworkStatsSummaryDev();
1177 } catch (IOException e) {
1178 throw new IllegalStateException(e);
1179 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001180 }
1181
1182 @Override
1183 public NetworkStats getNetworkStatsSummaryXt() {
1184 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001185 try {
1186 return mStatsFactory.readNetworkStatsSummaryXt();
1187 } catch (IOException e) {
1188 throw new IllegalStateException(e);
1189 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001190 }
1191
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001192 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001193 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001194 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001195 try {
1196 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1197 } catch (IOException e) {
1198 throw new IllegalStateException(e);
1199 }
San Mehat91cac642010-03-31 14:31:36 -07001200 }
1201
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001202 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001203 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001204 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001205
Jeff Sharkey350083e2011-06-29 10:45:16 -07001206 // silently discard when control disabled
1207 // TODO: eventually migrate to be always enabled
1208 if (!mBandwidthControlEnabled) return;
1209
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001210 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001211 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001212 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001213 }
1214
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001215 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001216 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001217 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001218 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001219 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001220 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001221 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001222 }
1223 }
1224
1225 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001226 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001227 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001228
Jeff Sharkey350083e2011-06-29 10:45:16 -07001229 // silently discard when control disabled
1230 // TODO: eventually migrate to be always enabled
1231 if (!mBandwidthControlEnabled) return;
1232
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001233 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001234 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001235 // TODO: eventually consider throwing
1236 return;
1237 }
1238
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001239 mActiveQuotas.remove(iface);
1240 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001241
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001242 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001243 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001244 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001245 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001246 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001247 }
1248 }
1249 }
1250
1251 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001252 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001253 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001254
1255 // silently discard when control disabled
1256 // TODO: eventually migrate to be always enabled
1257 if (!mBandwidthControlEnabled) return;
1258
1259 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001260 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001261 throw new IllegalStateException("setting alert requires existing quota on iface");
1262 }
1263
1264 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001265 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001266 throw new IllegalStateException("iface " + iface + " already has alert");
1267 }
1268
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001269 try {
1270 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001271 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001272 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001273 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001274 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001275 }
1276 }
1277 }
1278
1279 @Override
1280 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001281 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001282
1283 // silently discard when control disabled
1284 // TODO: eventually migrate to be always enabled
1285 if (!mBandwidthControlEnabled) return;
1286
1287 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001288 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001289 // TODO: eventually consider throwing
1290 return;
1291 }
1292
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001293 try {
1294 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001295 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001296 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001297 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001298 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001299 }
1300 }
1301 }
1302
1303 @Override
1304 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001305 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001306
1307 // silently discard when control disabled
1308 // TODO: eventually migrate to be always enabled
1309 if (!mBandwidthControlEnabled) return;
1310
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001311 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001312 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001313 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001314 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001315 }
1316 }
1317
1318 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001319 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001320 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001321
Jeff Sharkey350083e2011-06-29 10:45:16 -07001322 // silently discard when control disabled
1323 // TODO: eventually migrate to be always enabled
1324 if (!mBandwidthControlEnabled) return;
1325
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001326 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001327 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1328 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1329 // TODO: eventually consider throwing
1330 return;
1331 }
1332
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001333 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001334 mConnector.execute("bandwidth",
1335 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001336 if (rejectOnQuotaInterfaces) {
1337 mUidRejectOnQuota.put(uid, true);
1338 } else {
1339 mUidRejectOnQuota.delete(uid);
1340 }
1341 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001342 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001343 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001344 }
1345 }
1346
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001347 @Override
1348 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001349 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001350 return mBandwidthControlEnabled;
1351 }
1352
1353 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001354 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001355 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001356 try {
1357 return mStatsFactory.readNetworkStatsDetail(uid);
1358 } catch (IOException e) {
1359 throw new IllegalStateException(e);
1360 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001361 }
1362
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001363 @Override
1364 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001365 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001366
1367 if (ifacePairs.length % 2 != 0) {
1368 throw new IllegalArgumentException(
1369 "unexpected ifacePairs; length=" + ifacePairs.length);
1370 }
1371
1372 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1373 for (int i = 0; i < ifacePairs.length; i += 2) {
1374 final String ifaceIn = ifacePairs[i];
1375 final String ifaceOut = ifacePairs[i + 1];
1376 if (ifaceIn != null && ifaceOut != null) {
1377 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1378 }
1379 }
1380 return stats;
1381 }
1382
1383 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001384 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001385 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001386 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001387 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001388 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001389 }
1390
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001391 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001392
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001393 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1394 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1395 tok.nextToken();
1396 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001397
1398 try {
1399 final NetworkStats.Entry entry = new NetworkStats.Entry();
1400 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001401 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001402 entry.set = SET_DEFAULT;
1403 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001404 entry.rxBytes = Long.parseLong(tok.nextToken());
1405 entry.rxPackets = Long.parseLong(tok.nextToken());
1406 entry.txBytes = Long.parseLong(tok.nextToken());
1407 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001408 return entry;
1409 } catch (NumberFormatException e) {
1410 throw new IllegalStateException(
1411 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1412 }
1413 }
1414
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001415 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001416 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001417 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001418 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001419 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001420 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001421 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001422 }
1423 }
1424
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001425 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001426 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001427 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001428
Robert Greenwalt8058f622012-11-09 10:52:27 -08001429 final Command cmd = new Command("resolver", "setifdns", iface,
1430 (domains == null ? "" : domains));
1431
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001432 for (String s : servers) {
1433 InetAddress a = NetworkUtils.numericToInetAddress(s);
1434 if (a.isAnyLocalAddress() == false) {
1435 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001436 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001437 }
1438
1439 try {
1440 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001441 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001442 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001443 }
1444 }
1445
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001446 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001447 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1448 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1449 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001450 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001451 "uid", "add", iface, uid_start, uid_end);
1452 } catch (NativeDaemonConnectorException e) {
1453 throw e.rethrowAsParcelableException();
1454 }
1455 }
1456
1457 @Override
1458 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1459 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1460 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001461 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001462 "uid", "remove", iface, uid_start, uid_end);
1463 } catch (NativeDaemonConnectorException e) {
1464 throw e.rethrowAsParcelableException();
1465 }
1466 }
1467
1468 @Override
1469 public void setMarkedForwarding(String iface) {
1470 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1471 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001472 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001473 } catch (NativeDaemonConnectorException e) {
1474 throw e.rethrowAsParcelableException();
1475 }
1476 }
1477
1478 @Override
1479 public void clearMarkedForwarding(String iface) {
1480 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1481 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001482 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1483 } catch (NativeDaemonConnectorException e) {
1484 throw e.rethrowAsParcelableException();
1485 }
1486 }
1487
1488 @Override
1489 public int getMarkForUid(int uid) {
1490 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1491 final NativeDaemonEvent event;
1492 try {
1493 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1494 } catch (NativeDaemonConnectorException e) {
1495 throw e.rethrowAsParcelableException();
1496 }
1497 event.checkCode(GetMarkResult);
1498 return Integer.parseInt(event.getMessage());
1499 }
1500
1501 @Override
1502 public int getMarkForProtect() {
1503 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1504 final NativeDaemonEvent event;
1505 try {
1506 event = mConnector.execute("interface", "fwmark", "get", "protect");
1507 } catch (NativeDaemonConnectorException e) {
1508 throw e.rethrowAsParcelableException();
1509 }
1510 event.checkCode(GetMarkResult);
1511 return Integer.parseInt(event.getMessage());
1512 }
1513
1514 @Override
1515 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1516 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1517 try {
1518 LinkAddress dest = route.getDestination();
1519 mConnector.execute("interface", "fwmark", "route", "add", iface,
1520 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1521 } catch (NativeDaemonConnectorException e) {
1522 throw e.rethrowAsParcelableException();
1523 }
1524 }
1525
1526 @Override
1527 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1528 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1529 try {
1530 LinkAddress dest = route.getDestination();
1531 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1532 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001533 } catch (NativeDaemonConnectorException e) {
1534 throw e.rethrowAsParcelableException();
1535 }
1536 }
1537
1538 @Override
Chad Brubakerf336d722013-07-15 16:34:04 -07001539 public void setHostExemption(LinkAddress host) {
1540 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1541 try {
1542 mConnector.execute("interface", "fwmark", "exempt", "add", host);
1543 } catch (NativeDaemonConnectorException e) {
1544 throw e.rethrowAsParcelableException();
1545 }
1546 }
1547
1548 @Override
1549 public void clearHostExemption(LinkAddress host) {
1550 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1551 try {
1552 mConnector.execute("interface", "fwmark", "exempt", "remove", host);
1553 } catch (NativeDaemonConnectorException e) {
1554 throw e.rethrowAsParcelableException();
1555 }
1556 }
1557
1558 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001559 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1560 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1561 try {
1562 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1563 } catch (NativeDaemonConnectorException e) {
1564 throw e.rethrowAsParcelableException();
1565 }
1566 }
1567
1568 @Override
1569 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1570 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1571 try {
1572 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1573 } catch (NativeDaemonConnectorException e) {
1574 throw e.rethrowAsParcelableException();
1575 }
1576 }
1577
1578 @Override
1579 public void clearDnsInterfaceMaps() {
1580 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1581 try {
1582 mConnector.execute("resolver", "clearifacemapping");
1583 } catch (NativeDaemonConnectorException e) {
1584 throw e.rethrowAsParcelableException();
1585 }
1586 }
1587
1588
1589 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001590 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001591 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001592 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001593 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001594 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001595 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001596 }
1597 }
1598
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001599 @Override
1600 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001601 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001602 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001603 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001604 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001605 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001606 }
1607 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001608
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001609 @Override
1610 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001611 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001612 try {
1613 mConnector.execute("firewall", enabled ? "enable" : "disable");
1614 mFirewallEnabled = enabled;
1615 } catch (NativeDaemonConnectorException e) {
1616 throw e.rethrowAsParcelableException();
1617 }
1618 }
1619
1620 @Override
1621 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001622 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001623 return mFirewallEnabled;
1624 }
1625
1626 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001627 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001628 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001629 Preconditions.checkState(mFirewallEnabled);
1630 final String rule = allow ? ALLOW : DENY;
1631 try {
1632 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1633 } catch (NativeDaemonConnectorException e) {
1634 throw e.rethrowAsParcelableException();
1635 }
1636 }
1637
1638 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001639 public void setFirewallEgressSourceRule(String addr, 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_egress_source_rule", addr, rule);
1645 } catch (NativeDaemonConnectorException e) {
1646 throw e.rethrowAsParcelableException();
1647 }
1648 }
1649
1650 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001651 public void setFirewallEgressDestRule(String addr, int port, 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_dest_rule", addr, port, rule);
1657 } catch (NativeDaemonConnectorException e) {
1658 throw e.rethrowAsParcelableException();
1659 }
1660 }
1661
1662 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001663 public void setFirewallUidRule(int uid, 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_uid_rule", uid, rule);
1669 } catch (NativeDaemonConnectorException e) {
1670 throw e.rethrowAsParcelableException();
1671 }
1672 }
1673
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001674 private static void enforceSystemUid() {
1675 final int uid = Binder.getCallingUid();
1676 if (uid != Process.SYSTEM_UID) {
1677 throw new SecurityException("Only available to AID_SYSTEM");
1678 }
1679 }
1680
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001681 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001682 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1683 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1684 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001685 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001686 } catch (NativeDaemonConnectorException e) {
1687 throw new IllegalStateException(
1688 "Error communicating with native deamon to set interface for pid" + iface, e);
1689 }
1690 }
1691
1692 @Override
1693 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1694 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1695 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001696 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001697 } catch (NativeDaemonConnectorException e) {
1698 throw new IllegalStateException(
1699 "Error communicating with native deamon to clear interface for pid " + pid, e);
1700 }
1701 }
1702
Lorenzo Colitti79751842013-02-28 16:16:03 +09001703 @Override
1704 public void startClatd(String interfaceName) throws IllegalStateException {
1705 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1706
1707 try {
1708 mConnector.execute("clatd", "start", interfaceName);
1709 } catch (NativeDaemonConnectorException e) {
1710 throw e.rethrowAsParcelableException();
1711 }
1712 }
1713
1714 @Override
1715 public void stopClatd() throws IllegalStateException {
1716 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1717
1718 try {
1719 mConnector.execute("clatd", "stop");
1720 } catch (NativeDaemonConnectorException e) {
1721 throw e.rethrowAsParcelableException();
1722 }
1723 }
1724
1725 @Override
1726 public boolean isClatdStarted() {
1727 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1728
1729 final NativeDaemonEvent event;
1730 try {
1731 event = mConnector.execute("clatd", "status");
1732 } catch (NativeDaemonConnectorException e) {
1733 throw e.rethrowAsParcelableException();
1734 }
1735
1736 event.checkCode(ClatdStatusResult);
1737 return event.getMessage().endsWith("started");
1738 }
1739
Mattias Falk8b47b362011-08-23 14:15:13 +02001740 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001741 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001742 public void monitor() {
1743 if (mConnector != null) {
1744 mConnector.monitor();
1745 }
1746 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001747
1748 @Override
1749 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1750 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1751
Robert Greenwalt470fd722012-01-18 12:51:15 -08001752 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1753 mConnector.dump(fd, pw, args);
1754 pw.println();
1755
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001756 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1757
1758 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001759 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1760 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001761 }
1762
1763 synchronized (mUidRejectOnQuota) {
1764 pw.print("UID reject on quota ifaces: [");
1765 final int size = mUidRejectOnQuota.size();
1766 for (int i = 0; i < size; i++) {
1767 pw.print(mUidRejectOnQuota.keyAt(i));
1768 if (i < size - 1) pw.print(",");
1769 }
1770 pw.println("]");
1771 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001772
1773 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001774 }
San Mehat873f2142010-01-14 10:25:07 -08001775}