blob: c3a43bb06adaffd26c272e76340fd716c0795ff9 [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";
98
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080099 private static final String ADD = "add";
100 private static final String REMOVE = "remove";
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700101
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700102 private static final String ALLOW = "allow";
103 private static final String DENY = "deny";
104
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700105 private static final String DEFAULT = "default";
106 private static final String SECONDARY = "secondary";
107
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700108 /**
109 * Name representing {@link #setGlobalAlert(long)} limit when delivered to
110 * {@link INetworkManagementEventObserver#limitReached(String, String)}.
111 */
112 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
113
San Mehat873f2142010-01-14 10:25:07 -0800114 class NetdResponseCode {
JP Abgrall12b933d2011-07-14 18:09:22 -0700115 /* Keep in sync with system/netd/ResponseCode.h */
San Mehat873f2142010-01-14 10:25:07 -0800116 public static final int InterfaceListResult = 110;
117 public static final int TetherInterfaceListResult = 111;
118 public static final int TetherDnsFwdTgtListResult = 112;
San Mehat72759df2010-01-19 13:50:37 -0800119 public static final int TtyListResult = 113;
San Mehat873f2142010-01-14 10:25:07 -0800120
121 public static final int TetherStatusResult = 210;
122 public static final int IpFwdStatusResult = 211;
San Mehated4fc8a2010-01-22 12:28:36 -0800123 public static final int InterfaceGetCfgResult = 213;
Robert Greenwalte3253922010-02-18 09:23:25 -0800124 public static final int SoftapStatusResult = 214;
San Mehat91cac642010-03-31 14:31:36 -0700125 public static final int InterfaceRxCounterResult = 216;
126 public static final int InterfaceTxCounterResult = 217;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700127 public static final int QuotaCounterResult = 220;
128 public static final int TetheringStatsResult = 221;
Selim Gurun84c00c62012-02-27 15:42:38 -0800129 public static final int DnsProxyQueryResult = 222;
Lorenzo Colitti79751842013-02-28 16:16:03 +0900130 public static final int ClatdStatusResult = 223;
Chad Brubakercca54c42013-06-27 17:41:38 -0700131 public static final int GetMarkResult = 225;
Robert Greenwalte3253922010-02-18 09:23:25 -0800132
133 public static final int InterfaceChange = 600;
JP Abgrall12b933d2011-07-14 18:09:22 -0700134 public static final int BandwidthControl = 601;
Haoyu Bai6b7358d2012-07-17 16:36:50 -0700135 public static final int InterfaceClassActivity = 613;
San Mehat873f2142010-01-14 10:25:07 -0800136 }
137
138 /**
139 * Binder context for this service
140 */
141 private Context mContext;
142
143 /**
144 * connector object for communicating with netd
145 */
146 private NativeDaemonConnector mConnector;
147
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700148 private final Handler mMainHandler = new Handler();
149
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700150 private Thread mThread;
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700151 private CountDownLatch mConnectedSignal = new CountDownLatch(1);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700152
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800153 private final RemoteCallbackList<INetworkManagementEventObserver> mObservers =
154 new RemoteCallbackList<INetworkManagementEventObserver>();
San Mehat4d02d002010-01-22 16:07:46 -0800155
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700156 private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory();
157
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700158 private Object mQuotaLock = new Object();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700159 /** Set of interfaces with active quotas. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700160 private HashMap<String, Long> mActiveQuotas = Maps.newHashMap();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700161 /** Set of interfaces with active alerts. */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700162 private HashMap<String, Long> mActiveAlerts = Maps.newHashMap();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700163 /** Set of UIDs with active reject rules. */
164 private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray();
165
Haoyu Bai04124232012-06-28 15:26:19 -0700166 private Object mIdleTimerLock = new Object();
167 /** Set of interfaces with active idle timers. */
168 private static class IdleTimerParams {
169 public final int timeout;
170 public final String label;
171 public int networkCount;
172
173 IdleTimerParams(int timeout, String label) {
174 this.timeout = timeout;
175 this.label = label;
176 this.networkCount = 1;
177 }
178 }
179 private HashMap<String, IdleTimerParams> mActiveIdleTimers = Maps.newHashMap();
180
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700181 private volatile boolean mBandwidthControlEnabled;
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700182 private volatile boolean mFirewallEnabled;
Jeff Sharkey350083e2011-06-29 10:45:16 -0700183
San Mehat873f2142010-01-14 10:25:07 -0800184 /**
185 * Constructs a new NetworkManagementService instance
186 *
187 * @param context Binder context for this service
188 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700189 private NetworkManagementService(Context context) {
San Mehat873f2142010-01-14 10:25:07 -0800190 mContext = context;
San Mehat4d02d002010-01-22 16:07:46 -0800191
Marco Nelissen62dbb222010-02-18 10:56:30 -0800192 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
193 return;
194 }
195
San Mehat873f2142010-01-14 10:25:07 -0800196 mConnector = new NativeDaemonConnector(
Robert Greenwalt5a0c3202012-05-22 16:07:46 -0700197 new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700198 mThread = new Thread(mConnector, NETD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700199
200 // Add ourself to the Watchdog monitors.
201 Watchdog.getInstance().addMonitor(this);
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700202 }
203
204 public static NetworkManagementService create(Context context) throws InterruptedException {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700205 final NetworkManagementService service = new NetworkManagementService(context);
206 final CountDownLatch connectedSignal = service.mConnectedSignal;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700207 if (DBG) Slog.d(TAG, "Creating NetworkManagementService");
208 service.mThread.start();
209 if (DBG) Slog.d(TAG, "Awaiting socket connection");
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700210 connectedSignal.await();
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700211 if (DBG) Slog.d(TAG, "Connected");
212 return service;
San Mehat873f2142010-01-14 10:25:07 -0800213 }
214
Jeff Sharkey350083e2011-06-29 10:45:16 -0700215 public void systemReady() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700216 prepareNativeDaemon();
217 if (DBG) Slog.d(TAG, "Prepared");
Jeff Sharkey350083e2011-06-29 10:45:16 -0700218 }
219
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800220 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800221 public void registerObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800222 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800223 mObservers.register(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800224 }
225
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800226 @Override
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800227 public void unregisterObserver(INetworkManagementEventObserver observer) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800228 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800229 mObservers.unregister(observer);
San Mehat4d02d002010-01-22 16:07:46 -0800230 }
231
232 /**
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700233 * Notify our observers of an interface status change
San Mehat4d02d002010-01-22 16:07:46 -0800234 */
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700235 private void notifyInterfaceStatusChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800236 final int length = mObservers.beginBroadcast();
237 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800238 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800239 mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up);
240 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900241 } catch (RuntimeException e) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700242 }
243 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800244 mObservers.finishBroadcast();
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700245 }
246
247 /**
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700248 * Notify our observers of an interface link state change
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700249 * (typically, an Ethernet cable has been plugged-in or unplugged).
250 */
251 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800252 final int length = mObservers.beginBroadcast();
253 for (int i = 0; i < length; i++) {
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700254 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800255 mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
256 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900257 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800258 }
259 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800260 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800261 }
262
263 /**
264 * Notify our observers of an interface addition.
265 */
266 private void notifyInterfaceAdded(String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800267 final int length = mObservers.beginBroadcast();
268 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800269 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800270 mObservers.getBroadcastItem(i).interfaceAdded(iface);
271 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900272 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800273 }
274 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800275 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800276 }
277
278 /**
279 * Notify our observers of an interface removal.
280 */
281 private void notifyInterfaceRemoved(String iface) {
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700282 // netd already clears out quota and alerts for removed ifaces; update
283 // our sanity-checking state.
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700284 mActiveAlerts.remove(iface);
285 mActiveQuotas.remove(iface);
Jeff Sharkey89b8a212011-10-11 11:58:11 -0700286
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800287 final int length = mObservers.beginBroadcast();
288 for (int i = 0; i < length; i++) {
San Mehat4d02d002010-01-22 16:07:46 -0800289 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800290 mObservers.getBroadcastItem(i).interfaceRemoved(iface);
291 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900292 } catch (RuntimeException e) {
San Mehat4d02d002010-01-22 16:07:46 -0800293 }
294 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800295 mObservers.finishBroadcast();
San Mehat4d02d002010-01-22 16:07:46 -0800296 }
297
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700298 /**
JP Abgrall12b933d2011-07-14 18:09:22 -0700299 * Notify our observers of a limit reached.
300 */
301 private void notifyLimitReached(String limitName, String iface) {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800302 final int length = mObservers.beginBroadcast();
303 for (int i = 0; i < length; i++) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700304 try {
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800305 mObservers.getBroadcastItem(i).limitReached(limitName, iface);
306 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900307 } catch (RuntimeException e) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700308 }
309 }
Jeff Sharkey3df273e2011-12-15 15:47:12 -0800310 mObservers.finishBroadcast();
JP Abgrall12b933d2011-07-14 18:09:22 -0700311 }
312
313 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700314 * Notify our observers of a change in the data activity state of the interface
315 */
316 private void notifyInterfaceClassActivity(String label, boolean active) {
317 final int length = mObservers.beginBroadcast();
318 for (int i = 0; i < length; i++) {
319 try {
320 mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
321 } catch (RemoteException e) {
Lorenzo Colittid9b3d552013-03-17 03:21:35 +0900322 } catch (RuntimeException e) {
Haoyu Baidb3c8672012-06-20 14:29:57 -0700323 }
324 }
325 mObservers.finishBroadcast();
326 }
327
328 /**
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700329 * Prepare native daemon once connected, enabling modules and pushing any
330 * existing in-memory rules.
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700331 */
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700332 private void prepareNativeDaemon() {
333 mBandwidthControlEnabled = false;
Robert Greenwalte5c3afb2010-09-22 14:32:35 -0700334
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700335 // only enable bandwidth control when support exists
336 final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists();
337 if (hasKernelSupport) {
338 Slog.d(TAG, "enabling bandwidth control");
339 try {
340 mConnector.execute("bandwidth", "enable");
341 mBandwidthControlEnabled = true;
342 } catch (NativeDaemonConnectorException e) {
343 Log.wtf(TAG, "problem enabling bandwidth controls", e);
344 }
345 } else {
346 Slog.d(TAG, "not enabling bandwidth control");
347 }
348
349 SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0");
350
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700351 if (mBandwidthControlEnabled) {
352 try {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700353 IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700354 .noteNetworkStatsEnabled();
355 } catch (RemoteException e) {
356 }
357 }
358
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700359 // push any existing quota or UID rules
360 synchronized (mQuotaLock) {
361 int size = mActiveQuotas.size();
362 if (size > 0) {
363 Slog.d(TAG, "pushing " + size + " active quota rules");
364 final HashMap<String, Long> activeQuotas = mActiveQuotas;
365 mActiveQuotas = Maps.newHashMap();
366 for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) {
367 setInterfaceQuota(entry.getKey(), entry.getValue());
368 }
369 }
370
371 size = mActiveAlerts.size();
372 if (size > 0) {
373 Slog.d(TAG, "pushing " + size + " active alert rules");
374 final HashMap<String, Long> activeAlerts = mActiveAlerts;
375 mActiveAlerts = Maps.newHashMap();
376 for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) {
377 setInterfaceAlert(entry.getKey(), entry.getValue());
378 }
379 }
380
381 size = mUidRejectOnQuota.size();
382 if (size > 0) {
383 Slog.d(TAG, "pushing " + size + " active uid rules");
384 final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota;
385 mUidRejectOnQuota = new SparseBooleanArray();
386 for (int i = 0; i < uidRejectOnQuota.size(); i++) {
387 setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i));
388 }
389 }
390 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700391
392 // TODO: Push any existing firewall state
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700393 setFirewallEnabled(mFirewallEnabled || LockdownVpnTracker.isEnabled());
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700394 }
San Mehat4d02d002010-01-22 16:07:46 -0800395
San Mehat873f2142010-01-14 10:25:07 -0800396 //
397 // Netd Callback handling
398 //
399
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700400 private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks {
401 @Override
San Mehat873f2142010-01-14 10:25:07 -0800402 public void onDaemonConnected() {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700403 // event is dispatched from internal NDC thread, so we prepare the
404 // daemon back on main thread.
405 if (mConnectedSignal != null) {
406 mConnectedSignal.countDown();
407 mConnectedSignal = null;
408 } else {
409 mMainHandler.post(new Runnable() {
410 @Override
411 public void run() {
412 prepareNativeDaemon();
413 }
414 });
415 }
San Mehat873f2142010-01-14 10:25:07 -0800416 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700417
Jeff Sharkeyb24a7852012-05-01 15:19:37 -0700418 @Override
San Mehat873f2142010-01-14 10:25:07 -0800419 public boolean onEvent(int code, String raw, String[] cooked) {
JP Abgrall12b933d2011-07-14 18:09:22 -0700420 switch (code) {
421 case NetdResponseCode.InterfaceChange:
422 /*
423 * a network interface change occured
424 * Format: "NNN Iface added <name>"
425 * "NNN Iface removed <name>"
426 * "NNN Iface changed <name> <up/down>"
427 * "NNN Iface linkstatus <name> <up/down>"
428 */
429 if (cooked.length < 4 || !cooked[1].equals("Iface")) {
430 throw new IllegalStateException(
431 String.format("Invalid event from daemon (%s)", raw));
432 }
433 if (cooked[2].equals("added")) {
434 notifyInterfaceAdded(cooked[3]);
435 return true;
436 } else if (cooked[2].equals("removed")) {
437 notifyInterfaceRemoved(cooked[3]);
438 return true;
439 } else if (cooked[2].equals("changed") && cooked.length == 5) {
440 notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up"));
441 return true;
442 } else if (cooked[2].equals("linkstate") && cooked.length == 5) {
443 notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
444 return true;
445 }
Robert Greenwalte3253922010-02-18 09:23:25 -0800446 throw new IllegalStateException(
447 String.format("Invalid event from daemon (%s)", raw));
JP Abgrall12b933d2011-07-14 18:09:22 -0700448 // break;
449 case NetdResponseCode.BandwidthControl:
450 /*
451 * Bandwidth control needs some attention
452 * Format: "NNN limit alert <alertName> <ifaceName>"
453 */
454 if (cooked.length < 5 || !cooked[1].equals("limit")) {
455 throw new IllegalStateException(
456 String.format("Invalid event from daemon (%s)", raw));
457 }
458 if (cooked[2].equals("alert")) {
459 notifyLimitReached(cooked[3], cooked[4]);
460 return true;
461 }
462 throw new IllegalStateException(
463 String.format("Invalid event from daemon (%s)", raw));
464 // break;
Haoyu Baidb3c8672012-06-20 14:29:57 -0700465 case NetdResponseCode.InterfaceClassActivity:
466 /*
467 * An network interface class state changed (active/idle)
468 * Format: "NNN IfaceClass <active/idle> <label>"
469 */
470 if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
471 throw new IllegalStateException(
472 String.format("Invalid event from daemon (%s)", raw));
473 }
474 boolean isActive = cooked[2].equals("active");
475 notifyInterfaceClassActivity(cooked[3], isActive);
476 return true;
477 // break;
JP Abgrall12b933d2011-07-14 18:09:22 -0700478 default: break;
Robert Greenwalte3253922010-02-18 09:23:25 -0800479 }
480 return false;
San Mehat873f2142010-01-14 10:25:07 -0800481 }
482 }
483
San Mehated4fc8a2010-01-22 12:28:36 -0800484
San Mehat873f2142010-01-14 10:25:07 -0800485 //
486 // INetworkManagementService members
487 //
488
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800489 @Override
490 public String[] listInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800491 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700492 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800493 return NativeDaemonEvent.filterMessageList(
494 mConnector.executeForList("interface", "list"), InterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700495 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800496 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700497 }
San Mehated4fc8a2010-01-22 12:28:36 -0800498 }
499
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800500 @Override
501 public InterfaceConfiguration getInterfaceConfig(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800502 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800503
504 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700505 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800506 event = mConnector.execute("interface", "getcfg", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700507 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800508 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700509 }
San Mehated4fc8a2010-01-22 12:28:36 -0800510
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800511 event.checkCode(InterfaceGetCfgResult);
512
513 // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
514 final StringTokenizer st = new StringTokenizer(event.getMessage());
San Mehated4fc8a2010-01-22 12:28:36 -0800515
Kenny Roota80ce062010-06-01 13:23:53 -0700516 InterfaceConfiguration cfg;
San Mehated4fc8a2010-01-22 12:28:36 -0800517 try {
Kenny Roota80ce062010-06-01 13:23:53 -0700518 cfg = new InterfaceConfiguration();
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800519 cfg.setHardwareAddress(st.nextToken(" "));
Robert Greenwalted126402011-01-28 15:34:55 -0800520 InetAddress addr = null;
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800521 int prefixLength = 0;
Kenny Roota80ce062010-06-01 13:23:53 -0700522 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800523 addr = NetworkUtils.numericToInetAddress(st.nextToken());
Robert Greenwalte5903732011-02-22 16:00:42 -0800524 } catch (IllegalArgumentException iae) {
525 Slog.e(TAG, "Failed to parse ipaddr", iae);
Kenny Roota80ce062010-06-01 13:23:53 -0700526 }
527
528 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800529 prefixLength = Integer.parseInt(st.nextToken());
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800530 } catch (NumberFormatException nfe) {
531 Slog.e(TAG, "Failed to parse prefixLength", nfe);
Kenny Roota80ce062010-06-01 13:23:53 -0700532 }
Robert Greenwalt04808c22010-12-13 17:01:41 -0800533
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800534 cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
535 while (st.hasMoreTokens()) {
536 cfg.setFlag(st.nextToken());
537 }
Kenny Roota80ce062010-06-01 13:23:53 -0700538 } catch (NoSuchElementException nsee) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800539 throw new IllegalStateException("Invalid response from daemon: " + event);
San Mehated4fc8a2010-01-22 12:28:36 -0800540 }
San Mehated4fc8a2010-01-22 12:28:36 -0800541 return cfg;
542 }
543
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800544 @Override
545 public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800546 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800547 LinkAddress linkAddr = cfg.getLinkAddress();
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800548 if (linkAddr == null || linkAddr.getAddress() == null) {
549 throw new IllegalStateException("Null LinkAddress given");
Robert Greenwalted126402011-01-28 15:34:55 -0800550 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800551
552 final Command cmd = new Command("interface", "setcfg", iface,
Robert Greenwalt2d2afd12011-02-01 15:30:46 -0800553 linkAddr.getAddress().getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800554 linkAddr.getNetworkPrefixLength());
555 for (String flag : cfg.getFlags()) {
556 cmd.appendArg(flag);
557 }
558
Kenny Roota80ce062010-06-01 13:23:53 -0700559 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800560 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700561 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800562 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700563 }
San Mehat873f2142010-01-14 10:25:07 -0800564 }
565
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800566 @Override
567 public void setInterfaceDown(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800568 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800569 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800570 ifcg.setInterfaceDown();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800571 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700572 }
573
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800574 @Override
575 public void setInterfaceUp(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800576 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800577 final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800578 ifcg.setInterfaceUp();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800579 setInterfaceConfig(iface, ifcg);
Irfan Sheriff7244c972011-08-05 20:40:45 -0700580 }
581
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800582 @Override
583 public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800584 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriff73293612011-09-14 12:31:56 -0700585 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800586 mConnector.execute(
587 "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable");
Irfan Sheriff73293612011-09-14 12:31:56 -0700588 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800589 throw e.rethrowAsParcelableException();
Irfan Sheriff73293612011-09-14 12:31:56 -0700590 }
591 }
592
Irfan Sherifff5600612011-06-16 10:26:28 -0700593 /* TODO: This is right now a IPv4 only function. Works for wifi which loses its
594 IPv6 addresses on interface down, but we need to do full clean up here */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800595 @Override
596 public void clearInterfaceAddresses(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800597 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sherifff5600612011-06-16 10:26:28 -0700598 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800599 mConnector.execute("interface", "clearaddrs", iface);
Irfan Sherifff5600612011-06-16 10:26:28 -0700600 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800601 throw e.rethrowAsParcelableException();
Irfan Sherifff5600612011-06-16 10:26:28 -0700602 }
603 }
604
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800605 @Override
606 public void enableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800607 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700608 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800609 mConnector.execute("interface", "ipv6", iface, "enable");
repo sync7960d9f2011-09-29 12:40:02 -0700610 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800611 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700612 }
613 }
614
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800615 @Override
616 public void disableIpv6(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800617 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
repo sync7960d9f2011-09-29 12:40:02 -0700618 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800619 mConnector.execute("interface", "ipv6", iface, "disable");
repo sync7960d9f2011-09-29 12:40:02 -0700620 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800621 throw e.rethrowAsParcelableException();
repo sync7960d9f2011-09-29 12:40:02 -0700622 }
623 }
624
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800625 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700626 public void addRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800627 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700628 modifyRoute(interfaceName, ADD, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700629 }
630
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800631 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700632 public void removeRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700634 modifyRoute(interfaceName, REMOVE, route, DEFAULT);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700635 }
636
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800637 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700638 public void addSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800639 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700640 modifyRoute(interfaceName, ADD, route, SECONDARY);
641 }
642
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800643 @Override
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700644 public void removeSecondaryRoute(String interfaceName, RouteInfo route) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800645 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700646 modifyRoute(interfaceName, REMOVE, route, SECONDARY);
647 }
648
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800649 private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) {
650 final Command cmd = new Command("interface", "route", action, interfaceName, type);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700651
652 // create triplet: dest-ip-addr prefixlength gateway-ip-addr
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800653 final LinkAddress la = route.getDestination();
654 cmd.appendArg(la.getAddress().getHostAddress());
655 cmd.appendArg(la.getNetworkPrefixLength());
656
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700657 if (route.getGateway() == null) {
658 if (la.getAddress() instanceof Inet4Address) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800659 cmd.appendArg("0.0.0.0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700660 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800661 cmd.appendArg("::0");
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700662 }
663 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800664 cmd.appendArg(route.getGateway().getHostAddress());
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700665 }
666
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800667 try {
668 mConnector.execute(cmd);
669 } catch (NativeDaemonConnectorException e) {
670 throw e.rethrowAsParcelableException();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700671 }
672 }
673
674 private ArrayList<String> readRouteList(String filename) {
675 FileInputStream fstream = null;
676 ArrayList<String> list = new ArrayList<String>();
677
678 try {
679 fstream = new FileInputStream(filename);
680 DataInputStream in = new DataInputStream(fstream);
681 BufferedReader br = new BufferedReader(new InputStreamReader(in));
682 String s;
683
684 // throw away the title line
685
686 while (((s = br.readLine()) != null) && (s.length() != 0)) {
687 list.add(s);
688 }
689 } catch (IOException ex) {
690 // return current list, possibly empty
691 } finally {
692 if (fstream != null) {
693 try {
694 fstream.close();
695 } catch (IOException ex) {}
696 }
697 }
698
699 return list;
700 }
701
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800702 @Override
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700703 public RouteInfo[] getRoutes(String interfaceName) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800704 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700705 ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
706
707 // v4 routes listed as:
708 // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
709 for (String s : readRouteList("/proc/net/route")) {
710 String[] fields = s.split("\t");
711
712 if (fields.length > 7) {
713 String iface = fields[0];
714
715 if (interfaceName.equals(iface)) {
716 String dest = fields[1];
717 String gate = fields[2];
718 String flags = fields[3]; // future use?
719 String mask = fields[7];
720 try {
721 // address stored as a hex string, ex: 0014A8C0
722 InetAddress destAddr =
723 NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
724 int prefixLength =
725 NetworkUtils.netmaskIntToPrefixLength(
726 (int)Long.parseLong(mask, 16));
727 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
728
729 // address stored as a hex string, ex 0014A8C0
730 InetAddress gatewayAddr =
731 NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
732
Wink Saville7b5fd052013-03-15 05:07:04 +0000733 RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700734 routes.add(route);
735 } catch (Exception e) {
736 Log.e(TAG, "Error parsing route " + s + " : " + e);
737 continue;
738 }
739 }
740 }
741 }
742
743 // v6 routes listed as:
744 // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
745 for (String s : readRouteList("/proc/net/ipv6_route")) {
746 String[]fields = s.split("\\s+");
747 if (fields.length > 9) {
748 String iface = fields[9].trim();
749 if (interfaceName.equals(iface)) {
750 String dest = fields[0];
751 String prefix = fields[1];
752 String gate = fields[4];
753
754 try {
755 // prefix length stored as a hex string, ex 40
756 int prefixLength = Integer.parseInt(prefix, 16);
757
758 // address stored as a 32 char hex string
759 // ex fe800000000000000000000000000000
760 InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
761 LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
762
763 InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
764
Wink Saville7b5fd052013-03-15 05:07:04 +0000765 RouteInfo route = new RouteInfo(linkAddress, gateAddr);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700766 routes.add(route);
767 } catch (Exception e) {
768 Log.e(TAG, "Error parsing route " + s + " : " + e);
769 continue;
770 }
771 }
772 }
773 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800774 return routes.toArray(new RouteInfo[routes.size()]);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700775 }
776
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800777 @Override
San Mehat873f2142010-01-14 10:25:07 -0800778 public void shutdown() {
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800779 // TODO: remove from aidl if nobody calls externally
780 mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800781
Joe Onorato8a9b2202010-02-26 18:56:32 -0800782 Slog.d(TAG, "Shutting down");
San Mehat873f2142010-01-14 10:25:07 -0800783 }
784
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800785 @Override
San Mehat873f2142010-01-14 10:25:07 -0800786 public boolean getIpForwardingEnabled() throws IllegalStateException{
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800787 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800788
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800789 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700790 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800791 event = mConnector.execute("ipfwd", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700792 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800793 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700794 }
San Mehat873f2142010-01-14 10:25:07 -0800795
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800796 // 211 Forwarding enabled
797 event.checkCode(IpFwdStatusResult);
798 return event.getMessage().endsWith("enabled");
San Mehat873f2142010-01-14 10:25:07 -0800799 }
800
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800801 @Override
802 public void setIpForwardingEnabled(boolean enable) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800803 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800804 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800805 mConnector.execute("ipfwd", enable ? "enable" : "disable");
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800806 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800807 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800808 }
San Mehat873f2142010-01-14 10:25:07 -0800809 }
810
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800811 @Override
812 public void startTethering(String[] dhcpRange) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800813 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700814 // cmd is "tether start first_start first_stop second_start second_stop ..."
815 // an odd number of addrs will fail
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800816
817 final Command cmd = new Command("tether", "start");
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700818 for (String d : dhcpRange) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800819 cmd.appendArg(d);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700820 }
Kenny Roota80ce062010-06-01 13:23:53 -0700821
822 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800823 mConnector.execute(cmd);
Kenny Roota80ce062010-06-01 13:23:53 -0700824 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800825 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700826 }
San Mehat873f2142010-01-14 10:25:07 -0800827 }
828
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800829 @Override
830 public void stopTethering() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800831 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700832 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800833 mConnector.execute("tether", "stop");
Kenny Roota80ce062010-06-01 13:23:53 -0700834 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800835 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700836 }
San Mehat873f2142010-01-14 10:25:07 -0800837 }
838
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800839 @Override
840 public boolean isTetheringStarted() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800841 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat873f2142010-01-14 10:25:07 -0800842
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800843 final NativeDaemonEvent event;
Kenny Roota80ce062010-06-01 13:23:53 -0700844 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800845 event = mConnector.execute("tether", "status");
Kenny Roota80ce062010-06-01 13:23:53 -0700846 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800847 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700848 }
San Mehat873f2142010-01-14 10:25:07 -0800849
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800850 // 210 Tethering services started
851 event.checkCode(TetherStatusResult);
852 return event.getMessage().endsWith("started");
San Mehat873f2142010-01-14 10:25:07 -0800853 }
Matthew Xiefe19f122012-07-12 16:03:32 -0700854
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800855 @Override
856 public void tetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800857 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700858 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800859 mConnector.execute("tether", "interface", "add", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700860 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800861 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700862 }
San Mehat873f2142010-01-14 10:25:07 -0800863 }
864
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800865 @Override
San Mehat873f2142010-01-14 10:25:07 -0800866 public void untetherInterface(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800867 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700868 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800869 mConnector.execute("tether", "interface", "remove", iface);
Kenny Roota80ce062010-06-01 13:23:53 -0700870 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800871 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700872 }
San Mehat873f2142010-01-14 10:25:07 -0800873 }
874
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800875 @Override
876 public String[] listTetheredInterfaces() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800877 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700878 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800879 return NativeDaemonEvent.filterMessageList(
880 mConnector.executeForList("tether", "interface", "list"),
881 TetherInterfaceListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700882 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800883 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700884 }
San Mehat873f2142010-01-14 10:25:07 -0800885 }
886
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800887 @Override
888 public void setDnsForwarders(String[] dns) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800889 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800890
891 final Command cmd = new Command("tether", "dns", "set");
892 for (String s : dns) {
893 cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
894 }
895
San Mehat873f2142010-01-14 10:25:07 -0800896 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800897 mConnector.execute(cmd);
898 } catch (NativeDaemonConnectorException e) {
899 throw e.rethrowAsParcelableException();
San Mehat873f2142010-01-14 10:25:07 -0800900 }
901 }
902
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800903 @Override
904 public String[] getDnsForwarders() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800905 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700906 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800907 return NativeDaemonEvent.filterMessageList(
908 mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700909 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800910 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700911 }
San Mehat873f2142010-01-14 10:25:07 -0800912 }
913
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800914 private void modifyNat(String action, String internalInterface, String externalInterface)
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700915 throws SocketException {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800916 final Command cmd = new Command("nat", action, internalInterface, externalInterface);
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700917
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800918 final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(
919 internalInterface);
Robert Greenwalte83d1812011-11-21 14:44:39 -0800920 if (internalNetworkInterface == null) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800921 cmd.appendArg("0");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800922 } else {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800923 Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface
924 .getInterfaceAddresses();
925 cmd.appendArg(interfaceAddresses.size());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800926 for (InterfaceAddress ia : interfaceAddresses) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800927 InetAddress addr = NetworkUtils.getNetworkPart(
928 ia.getAddress(), ia.getNetworkPrefixLength());
929 cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
Robert Greenwalte83d1812011-11-21 14:44:39 -0800930 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700931 }
932
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800933 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800934 mConnector.execute(cmd);
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800935 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800936 throw e.rethrowAsParcelableException();
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800937 }
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700938 }
939
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800940 @Override
941 public void enableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800942 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700943 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700944 modifyNat("enable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800945 } catch (SocketException e) {
946 throw new IllegalStateException(e);
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 disableNat(String internalInterface, String externalInterface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800952 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 try {
Robert Greenwalt3b28e9a2011-11-02 14:37:19 -0700954 modifyNat("disable", internalInterface, externalInterface);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800955 } catch (SocketException e) {
956 throw new IllegalStateException(e);
Kenny Roota80ce062010-06-01 13:23:53 -0700957 }
San Mehat873f2142010-01-14 10:25:07 -0800958 }
San Mehat72759df2010-01-19 13:50:37 -0800959
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800960 @Override
961 public String[] listTtys() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800962 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700963 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800964 return NativeDaemonEvent.filterMessageList(
965 mConnector.executeForList("list_ttys"), TtyListResult);
Kenny Roota80ce062010-06-01 13:23:53 -0700966 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800967 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700968 }
San Mehat72759df2010-01-19 13:50:37 -0800969 }
970
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800971 @Override
972 public void attachPppd(
973 String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800974 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
San Mehat72759df2010-01-19 13:50:37 -0800975 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800976 mConnector.execute("pppd", "attach", tty,
Robert Greenwalte5903732011-02-22 16:00:42 -0800977 NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
978 NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
979 NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800980 NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress());
Kenny Roota80ce062010-06-01 13:23:53 -0700981 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800982 throw e.rethrowAsParcelableException();
San Mehat72759df2010-01-19 13:50:37 -0800983 }
984 }
985
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800986 @Override
987 public void detachPppd(String tty) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800988 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -0700989 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800990 mConnector.execute("pppd", "detach", tty);
Kenny Roota80ce062010-06-01 13:23:53 -0700991 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -0800992 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -0700993 }
San Mehat72759df2010-01-19 13:50:37 -0800994 }
Robert Greenwaltce1200d2010-02-18 11:25:54 -0800995
Jeff Sharkeyaf75c332011-11-18 12:41:12 -0800996 @Override
997 public void startAccessPoint(
Irfan Sheriff90542752012-06-19 15:44:35 -0700998 WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -0800999 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001000 try {
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001001 wifiFirmwareReload(wlanIface, "AP");
Kenny Roota80ce062010-06-01 13:23:53 -07001002 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001003 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001004 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001005 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001006 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001007 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001008 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001009 mConnector.execute("softap", "startap");
Kenny Roota80ce062010-06-01 13:23:53 -07001010 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001011 throw e.rethrowAsParcelableException();
Irfan Sheriff9ab518ad2010-03-12 15:48:17 -08001012 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001013 }
1014
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001015 private static String getSecurityType(WifiConfiguration wifiConfig) {
Irfan Sheriffec8d23a2011-02-16 17:00:33 -08001016 switch (wifiConfig.getAuthType()) {
1017 case KeyMgmt.WPA_PSK:
1018 return "wpa-psk";
1019 case KeyMgmt.WPA2_PSK:
1020 return "wpa2-psk";
1021 default:
1022 return "open";
1023 }
1024 }
1025
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001026 /* @param mode can be "AP", "STA" or "P2P" */
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001027 @Override
1028 public void wifiFirmwareReload(String wlanIface, String mode) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001029 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001030 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001031 mConnector.execute("softap", "fwreload", wlanIface, mode);
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001032 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001033 throw e.rethrowAsParcelableException();
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001034 }
1035 }
1036
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001037 @Override
1038 public void stopAccessPoint(String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001039 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001040 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001041 mConnector.execute("softap", "stopap");
Irfan Sheriffcb30b222011-07-29 20:54:52 -07001042 wifiFirmwareReload(wlanIface, "STA");
Kenny Roota80ce062010-06-01 13:23:53 -07001043 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001044 throw e.rethrowAsParcelableException();
Kenny Roota80ce062010-06-01 13:23:53 -07001045 }
Irfan Sheriff5321aef2010-02-12 12:35:59 -08001046 }
1047
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001048 @Override
Irfan Sheriff90542752012-06-19 15:44:35 -07001049 public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001050 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Kenny Roota80ce062010-06-01 13:23:53 -07001051 try {
1052 if (wifiConfig == null) {
Irfan Sheriff90542752012-06-19 15:44:35 -07001053 mConnector.execute("softap", "set", wlanIface);
Kenny Roota80ce062010-06-01 13:23:53 -07001054 } else {
Irfan Sheriff90542752012-06-19 15:44:35 -07001055 mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
Dmitry Shmidt28dd15b2013-06-10 14:37:08 -07001056 "broadcast", "6", getSecurityType(wifiConfig),
Kenny Root36062542013-06-10 11:09:28 -07001057 new SensitiveArg(wifiConfig.preSharedKey));
Kenny Roota80ce062010-06-01 13:23:53 -07001058 }
1059 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001060 throw e.rethrowAsParcelableException();
Irfan Sheriffc2f54c22010-03-18 14:02:22 -07001061 }
1062 }
San Mehat91cac642010-03-31 14:31:36 -07001063
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001064 @Override
Haoyu Bai04124232012-06-28 15:26:19 -07001065 public void addIdleTimer(String iface, int timeout, String label) {
1066 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1067
1068 if (DBG) Slog.d(TAG, "Adding idletimer");
1069
1070 synchronized (mIdleTimerLock) {
1071 IdleTimerParams params = mActiveIdleTimers.get(iface);
1072 if (params != null) {
1073 // the interface already has idletimer, update network count
1074 params.networkCount++;
1075 return;
1076 }
1077
1078 try {
1079 mConnector.execute("idletimer", "add", iface, Integer.toString(timeout), label);
1080 } catch (NativeDaemonConnectorException e) {
1081 throw e.rethrowAsParcelableException();
1082 }
1083 mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
1084 }
1085 }
1086
1087 @Override
1088 public void removeIdleTimer(String iface) {
1089 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1090
1091 if (DBG) Slog.d(TAG, "Removing idletimer");
1092
1093 synchronized (mIdleTimerLock) {
1094 IdleTimerParams params = mActiveIdleTimers.get(iface);
1095 if (params == null || --(params.networkCount) > 0) {
1096 return;
1097 }
1098
1099 try {
1100 mConnector.execute("idletimer", "remove", iface,
1101 Integer.toString(params.timeout), params.label);
1102 } catch (NativeDaemonConnectorException e) {
1103 throw e.rethrowAsParcelableException();
1104 }
1105 mActiveIdleTimers.remove(iface);
1106 }
1107 }
1108
1109 @Override
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001110 public NetworkStats getNetworkStatsSummaryDev() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001111 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001112 try {
1113 return mStatsFactory.readNetworkStatsSummaryDev();
1114 } catch (IOException e) {
1115 throw new IllegalStateException(e);
1116 }
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001117 }
1118
1119 @Override
1120 public NetworkStats getNetworkStatsSummaryXt() {
1121 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001122 try {
1123 return mStatsFactory.readNetworkStatsSummaryXt();
1124 } catch (IOException e) {
1125 throw new IllegalStateException(e);
1126 }
Jeff Sharkeyae2c1812011-10-04 13:11:40 -07001127 }
1128
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001129 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001130 public NetworkStats getNetworkStatsDetail() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001131 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001132 try {
1133 return mStatsFactory.readNetworkStatsDetail(UID_ALL);
1134 } catch (IOException e) {
1135 throw new IllegalStateException(e);
1136 }
San Mehat91cac642010-03-31 14:31:36 -07001137 }
1138
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001139 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001140 public void setInterfaceQuota(String iface, long quotaBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001141 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001142
Jeff Sharkey350083e2011-06-29 10:45:16 -07001143 // silently discard when control disabled
1144 // TODO: eventually migrate to be always enabled
1145 if (!mBandwidthControlEnabled) return;
1146
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001147 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001148 if (mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001149 throw new IllegalStateException("iface " + iface + " already has quota");
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001150 }
1151
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001152 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001153 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001154 mConnector.execute("bandwidth", "setiquota", iface, quotaBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001155 mActiveQuotas.put(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001156 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001157 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001158 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001159 }
1160 }
1161
1162 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001163 public void removeInterfaceQuota(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001164 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001165
Jeff Sharkey350083e2011-06-29 10:45:16 -07001166 // silently discard when control disabled
1167 // TODO: eventually migrate to be always enabled
1168 if (!mBandwidthControlEnabled) return;
1169
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001170 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001171 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001172 // TODO: eventually consider throwing
1173 return;
1174 }
1175
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001176 mActiveQuotas.remove(iface);
1177 mActiveAlerts.remove(iface);
Jeff Sharkey38ddeaa2011-11-08 13:04:22 -08001178
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001179 try {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001180 // TODO: support quota shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001181 mConnector.execute("bandwidth", "removeiquota", iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001182 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001183 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001184 }
1185 }
1186 }
1187
1188 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001189 public void setInterfaceAlert(String iface, long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001190 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001191
1192 // silently discard when control disabled
1193 // TODO: eventually migrate to be always enabled
1194 if (!mBandwidthControlEnabled) return;
1195
1196 // quick sanity check
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001197 if (!mActiveQuotas.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001198 throw new IllegalStateException("setting alert requires existing quota on iface");
1199 }
1200
1201 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001202 if (mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001203 throw new IllegalStateException("iface " + iface + " already has alert");
1204 }
1205
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001206 try {
1207 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001208 mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001209 mActiveAlerts.put(iface, alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001210 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001211 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001212 }
1213 }
1214 }
1215
1216 @Override
1217 public void removeInterfaceAlert(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001218 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001219
1220 // silently discard when control disabled
1221 // TODO: eventually migrate to be always enabled
1222 if (!mBandwidthControlEnabled) return;
1223
1224 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001225 if (!mActiveAlerts.containsKey(iface)) {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001226 // TODO: eventually consider throwing
1227 return;
1228 }
1229
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001230 try {
1231 // TODO: support alert shared across interfaces
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001232 mConnector.execute("bandwidth", "removeinterfacealert", iface);
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001233 mActiveAlerts.remove(iface);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001234 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001235 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001236 }
1237 }
1238 }
1239
1240 @Override
1241 public void setGlobalAlert(long alertBytes) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001242 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001243
1244 // silently discard when control disabled
1245 // TODO: eventually migrate to be always enabled
1246 if (!mBandwidthControlEnabled) return;
1247
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001248 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001249 mConnector.execute("bandwidth", "setglobalalert", alertBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001250 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001251 throw e.rethrowAsParcelableException();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001252 }
1253 }
1254
1255 @Override
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001256 public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001257 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001258
Jeff Sharkey350083e2011-06-29 10:45:16 -07001259 // silently discard when control disabled
1260 // TODO: eventually migrate to be always enabled
1261 if (!mBandwidthControlEnabled) return;
1262
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001263 synchronized (mQuotaLock) {
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001264 final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false);
1265 if (oldRejectOnQuota == rejectOnQuotaInterfaces) {
1266 // TODO: eventually consider throwing
1267 return;
1268 }
1269
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001270 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001271 mConnector.execute("bandwidth",
1272 rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001273 if (rejectOnQuotaInterfaces) {
1274 mUidRejectOnQuota.put(uid, true);
1275 } else {
1276 mUidRejectOnQuota.delete(uid);
1277 }
1278 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001279 throw e.rethrowAsParcelableException();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -07001280 }
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001281 }
1282 }
1283
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001284 @Override
1285 public boolean isBandwidthControlEnabled() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001286 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey63d27a92011-08-03 17:04:22 -07001287 return mBandwidthControlEnabled;
1288 }
1289
1290 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001291 public NetworkStats getNetworkStatsUidDetail(int uid) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001292 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001293 try {
1294 return mStatsFactory.readNetworkStatsDetail(uid);
1295 } catch (IOException e) {
1296 throw new IllegalStateException(e);
1297 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -07001298 }
1299
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001300 @Override
1301 public NetworkStats getNetworkStatsTethering(String[] ifacePairs) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001302 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001303
1304 if (ifacePairs.length % 2 != 0) {
1305 throw new IllegalArgumentException(
1306 "unexpected ifacePairs; length=" + ifacePairs.length);
1307 }
1308
1309 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
1310 for (int i = 0; i < ifacePairs.length; i += 2) {
1311 final String ifaceIn = ifacePairs[i];
1312 final String ifaceOut = ifacePairs[i + 1];
1313 if (ifaceIn != null && ifaceOut != null) {
1314 stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut));
1315 }
1316 }
1317 return stats;
1318 }
1319
1320 private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001321 final NativeDaemonEvent event;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001322 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001323 event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001324 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001325 throw e.rethrowAsParcelableException();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001326 }
1327
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001328 event.checkCode(TetheringStatsResult);
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001329
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001330 // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
1331 final StringTokenizer tok = new StringTokenizer(event.getMessage());
1332 tok.nextToken();
1333 tok.nextToken();
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001334
1335 try {
1336 final NetworkStats.Entry entry = new NetworkStats.Entry();
1337 entry.iface = ifaceIn;
Jeff Sharkey905b5892011-09-30 15:19:49 -07001338 entry.uid = UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001339 entry.set = SET_DEFAULT;
1340 entry.tag = TAG_NONE;
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001341 entry.rxBytes = Long.parseLong(tok.nextToken());
1342 entry.rxPackets = Long.parseLong(tok.nextToken());
1343 entry.txBytes = Long.parseLong(tok.nextToken());
1344 entry.txPackets = Long.parseLong(tok.nextToken());
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -07001345 return entry;
1346 } catch (NumberFormatException e) {
1347 throw new IllegalStateException(
1348 "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e);
1349 }
1350 }
1351
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001352 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001353 public void setDefaultInterfaceForDns(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001354 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001355 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001356 mConnector.execute("resolver", "setdefaultif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001357 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001358 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001359 }
1360 }
1361
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001362 @Override
Robert Greenwalt8058f622012-11-09 10:52:27 -08001363 public void setDnsServersForInterface(String iface, String[] servers, String domains) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001364 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001365
Robert Greenwalt8058f622012-11-09 10:52:27 -08001366 final Command cmd = new Command("resolver", "setifdns", iface,
1367 (domains == null ? "" : domains));
1368
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001369 for (String s : servers) {
1370 InetAddress a = NetworkUtils.numericToInetAddress(s);
1371 if (a.isAnyLocalAddress() == false) {
1372 cmd.appendArg(a.getHostAddress());
Mattias Falk7475c0c2011-04-04 16:10:36 +02001373 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001374 }
1375
1376 try {
1377 mConnector.execute(cmd);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001378 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001379 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001380 }
1381 }
1382
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001383 @Override
Chad Brubaker3277620a2013-06-12 13:37:30 -07001384 public void setUidRangeRoute(String iface, int uid_start, int uid_end) {
1385 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1386 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001387 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001388 "uid", "add", iface, uid_start, uid_end);
1389 } catch (NativeDaemonConnectorException e) {
1390 throw e.rethrowAsParcelableException();
1391 }
1392 }
1393
1394 @Override
1395 public void clearUidRangeRoute(String iface, int uid_start, int uid_end) {
1396 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1397 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001398 mConnector.execute("interface", "fwmark",
Chad Brubaker3277620a2013-06-12 13:37:30 -07001399 "uid", "remove", iface, uid_start, uid_end);
1400 } catch (NativeDaemonConnectorException e) {
1401 throw e.rethrowAsParcelableException();
1402 }
1403 }
1404
1405 @Override
1406 public void setMarkedForwarding(String iface) {
1407 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1408 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001409 mConnector.execute("interface", "fwmark", "rule", "add", iface);
Chad Brubaker3277620a2013-06-12 13:37:30 -07001410 } catch (NativeDaemonConnectorException e) {
1411 throw e.rethrowAsParcelableException();
1412 }
1413 }
1414
1415 @Override
1416 public void clearMarkedForwarding(String iface) {
1417 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1418 try {
Chad Brubakercca54c42013-06-27 17:41:38 -07001419 mConnector.execute("interface", "fwmark", "rule", "remove", iface);
1420 } catch (NativeDaemonConnectorException e) {
1421 throw e.rethrowAsParcelableException();
1422 }
1423 }
1424
1425 @Override
1426 public int getMarkForUid(int uid) {
1427 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1428 final NativeDaemonEvent event;
1429 try {
1430 event = mConnector.execute("interface", "fwmark", "get", "mark", uid);
1431 } catch (NativeDaemonConnectorException e) {
1432 throw e.rethrowAsParcelableException();
1433 }
1434 event.checkCode(GetMarkResult);
1435 return Integer.parseInt(event.getMessage());
1436 }
1437
1438 @Override
1439 public int getMarkForProtect() {
1440 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1441 final NativeDaemonEvent event;
1442 try {
1443 event = mConnector.execute("interface", "fwmark", "get", "protect");
1444 } catch (NativeDaemonConnectorException e) {
1445 throw e.rethrowAsParcelableException();
1446 }
1447 event.checkCode(GetMarkResult);
1448 return Integer.parseInt(event.getMessage());
1449 }
1450
1451 @Override
1452 public void setMarkedForwardingRoute(String iface, RouteInfo route) {
1453 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1454 try {
1455 LinkAddress dest = route.getDestination();
1456 mConnector.execute("interface", "fwmark", "route", "add", iface,
1457 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
1458 } catch (NativeDaemonConnectorException e) {
1459 throw e.rethrowAsParcelableException();
1460 }
1461 }
1462
1463 @Override
1464 public void clearMarkedForwardingRoute(String iface, RouteInfo route) {
1465 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1466 try {
1467 LinkAddress dest = route.getDestination();
1468 mConnector.execute("interface", "fwmark", "route", "remove", iface,
1469 dest.getAddress().getHostAddress(), dest.getNetworkPrefixLength());
Chad Brubaker3277620a2013-06-12 13:37:30 -07001470 } catch (NativeDaemonConnectorException e) {
1471 throw e.rethrowAsParcelableException();
1472 }
1473 }
1474
1475 @Override
1476 public void setDnsInterfaceForUidRange(String iface, int uid_start, int uid_end) {
1477 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1478 try {
1479 mConnector.execute("resolver", "setifaceforuidrange", iface, uid_start, uid_end);
1480 } catch (NativeDaemonConnectorException e) {
1481 throw e.rethrowAsParcelableException();
1482 }
1483 }
1484
1485 @Override
1486 public void clearDnsInterfaceForUidRange(int uid_start, int uid_end) {
1487 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1488 try {
1489 mConnector.execute("resolver", "clearifaceforuidrange", uid_start, uid_end);
1490 } catch (NativeDaemonConnectorException e) {
1491 throw e.rethrowAsParcelableException();
1492 }
1493 }
1494
1495 @Override
1496 public void clearDnsInterfaceMaps() {
1497 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1498 try {
1499 mConnector.execute("resolver", "clearifacemapping");
1500 } catch (NativeDaemonConnectorException e) {
1501 throw e.rethrowAsParcelableException();
1502 }
1503 }
1504
1505
1506 @Override
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001507 public void flushDefaultDnsCache() {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001508 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001509 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001510 mConnector.execute("resolver", "flushdefaultif");
Mattias Falk7475c0c2011-04-04 16:10:36 +02001511 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001512 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001513 }
1514 }
1515
Jeff Sharkeyaf75c332011-11-18 12:41:12 -08001516 @Override
1517 public void flushInterfaceDnsCache(String iface) {
Jeff Sharkey4529bb62011-12-14 10:31:54 -08001518 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001519 try {
Jeff Sharkeyba2896e2011-11-30 18:13:54 -08001520 mConnector.execute("resolver", "flushif", iface);
Mattias Falk7475c0c2011-04-04 16:10:36 +02001521 } catch (NativeDaemonConnectorException e) {
Jeff Sharkey276642b2011-12-01 11:24:24 -08001522 throw e.rethrowAsParcelableException();
Mattias Falk7475c0c2011-04-04 16:10:36 +02001523 }
1524 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001525
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001526 @Override
1527 public void setFirewallEnabled(boolean enabled) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001528 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001529 try {
1530 mConnector.execute("firewall", enabled ? "enable" : "disable");
1531 mFirewallEnabled = enabled;
1532 } catch (NativeDaemonConnectorException e) {
1533 throw e.rethrowAsParcelableException();
1534 }
1535 }
1536
1537 @Override
1538 public boolean isFirewallEnabled() {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001539 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001540 return mFirewallEnabled;
1541 }
1542
1543 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001544 public void setFirewallInterfaceRule(String iface, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001545 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001546 Preconditions.checkState(mFirewallEnabled);
1547 final String rule = allow ? ALLOW : DENY;
1548 try {
1549 mConnector.execute("firewall", "set_interface_rule", iface, rule);
1550 } catch (NativeDaemonConnectorException e) {
1551 throw e.rethrowAsParcelableException();
1552 }
1553 }
1554
1555 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001556 public void setFirewallEgressSourceRule(String addr, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001557 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001558 Preconditions.checkState(mFirewallEnabled);
1559 final String rule = allow ? ALLOW : DENY;
1560 try {
1561 mConnector.execute("firewall", "set_egress_source_rule", addr, rule);
1562 } catch (NativeDaemonConnectorException e) {
1563 throw e.rethrowAsParcelableException();
1564 }
1565 }
1566
1567 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001568 public void setFirewallEgressDestRule(String addr, int port, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001569 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001570 Preconditions.checkState(mFirewallEnabled);
1571 final String rule = allow ? ALLOW : DENY;
1572 try {
1573 mConnector.execute("firewall", "set_egress_dest_rule", addr, port, rule);
1574 } catch (NativeDaemonConnectorException e) {
1575 throw e.rethrowAsParcelableException();
1576 }
1577 }
1578
1579 @Override
Jeff Sharkey2c092982012-08-24 11:44:40 -07001580 public void setFirewallUidRule(int uid, boolean allow) {
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001581 enforceSystemUid();
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001582 Preconditions.checkState(mFirewallEnabled);
1583 final String rule = allow ? ALLOW : DENY;
1584 try {
1585 mConnector.execute("firewall", "set_uid_rule", uid, rule);
1586 } catch (NativeDaemonConnectorException e) {
1587 throw e.rethrowAsParcelableException();
1588 }
1589 }
1590
Jeff Sharkeyf56e2432012-09-06 17:54:29 -07001591 private static void enforceSystemUid() {
1592 final int uid = Binder.getCallingUid();
1593 if (uid != Process.SYSTEM_UID) {
1594 throw new SecurityException("Only available to AID_SYSTEM");
1595 }
1596 }
1597
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001598 @Override
Mattias Falk8b47b362011-08-23 14:15:13 +02001599 public void setDnsInterfaceForPid(String iface, int pid) throws IllegalStateException {
1600 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1601 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001602 mConnector.execute("resolver", "setifaceforpid", iface, pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001603 } catch (NativeDaemonConnectorException e) {
1604 throw new IllegalStateException(
1605 "Error communicating with native deamon to set interface for pid" + iface, e);
1606 }
1607 }
1608
1609 @Override
1610 public void clearDnsInterfaceForPid(int pid) throws IllegalStateException {
1611 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1612 try {
Jeff Sharkey0c232f52013-02-13 11:27:24 -08001613 mConnector.execute("resolver", "clearifaceforpid", pid);
Mattias Falk8b47b362011-08-23 14:15:13 +02001614 } catch (NativeDaemonConnectorException e) {
1615 throw new IllegalStateException(
1616 "Error communicating with native deamon to clear interface for pid " + pid, e);
1617 }
1618 }
1619
Lorenzo Colitti79751842013-02-28 16:16:03 +09001620 @Override
1621 public void startClatd(String interfaceName) throws IllegalStateException {
1622 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1623
1624 try {
1625 mConnector.execute("clatd", "start", interfaceName);
1626 } catch (NativeDaemonConnectorException e) {
1627 throw e.rethrowAsParcelableException();
1628 }
1629 }
1630
1631 @Override
1632 public void stopClatd() throws IllegalStateException {
1633 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1634
1635 try {
1636 mConnector.execute("clatd", "stop");
1637 } catch (NativeDaemonConnectorException e) {
1638 throw e.rethrowAsParcelableException();
1639 }
1640 }
1641
1642 @Override
1643 public boolean isClatdStarted() {
1644 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
1645
1646 final NativeDaemonEvent event;
1647 try {
1648 event = mConnector.execute("clatd", "status");
1649 } catch (NativeDaemonConnectorException e) {
1650 throw e.rethrowAsParcelableException();
1651 }
1652
1653 event.checkCode(ClatdStatusResult);
1654 return event.getMessage().endsWith("started");
1655 }
1656
Mattias Falk8b47b362011-08-23 14:15:13 +02001657 /** {@inheritDoc} */
Jeff Sharkey7b4596f2013-02-25 10:55:29 -08001658 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001659 public void monitor() {
1660 if (mConnector != null) {
1661 mConnector.monitor();
1662 }
1663 }
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001664
1665 @Override
1666 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1667 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
1668
Robert Greenwalt470fd722012-01-18 12:51:15 -08001669 pw.println("NetworkManagementService NativeDaemonConnector Log:");
1670 mConnector.dump(fd, pw, args);
1671 pw.println();
1672
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001673 pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);
1674
1675 synchronized (mQuotaLock) {
Jeff Sharkeyb24a7852012-05-01 15:19:37 -07001676 pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString());
1677 pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString());
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001678 }
1679
1680 synchronized (mUidRejectOnQuota) {
1681 pw.print("UID reject on quota ifaces: [");
1682 final int size = mUidRejectOnQuota.size();
1683 for (int i = 0; i < size; i++) {
1684 pw.print(mUidRejectOnQuota.keyAt(i));
1685 if (i < size - 1) pw.print(",");
1686 }
1687 pw.println("]");
1688 }
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -07001689
1690 pw.print("Firewall enabled: "); pw.println(mFirewallEnabled);
Jeff Sharkey47eb1022011-08-25 17:48:52 -07001691 }
San Mehat873f2142010-01-14 10:25:07 -08001692}