San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 19 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 21 | import static android.Manifest.permission.SHUTDOWN; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 22 | import static android.net.NetworkStats.SET_DEFAULT; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 23 | import static android.net.NetworkStats.TAG_NONE; |
| 24 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | ae2c181 | 2011-10-04 13:11:40 -0700 | [diff] [blame] | 25 | import static android.net.TrafficStats.UID_TETHERING; |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 26 | import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceGetCfgResult; |
| 27 | import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceListResult; |
| 28 | import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceRxThrottleResult; |
| 29 | import static com.android.server.NetworkManagementService.NetdResponseCode.InterfaceTxThrottleResult; |
| 30 | import static com.android.server.NetworkManagementService.NetdResponseCode.IpFwdStatusResult; |
| 31 | import static com.android.server.NetworkManagementService.NetdResponseCode.TetherDnsFwdTgtListResult; |
| 32 | import static com.android.server.NetworkManagementService.NetdResponseCode.TetherInterfaceListResult; |
| 33 | import static com.android.server.NetworkManagementService.NetdResponseCode.TetherStatusResult; |
| 34 | import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsResult; |
| 35 | import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 36 | import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 37 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 38 | import android.content.Context; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 39 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 40 | import android.net.InterfaceConfiguration; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 41 | import android.net.LinkAddress; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 42 | import android.net.NetworkStats; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 43 | import android.net.NetworkUtils; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 44 | import android.net.RouteInfo; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 45 | import android.net.wifi.WifiConfiguration; |
| 46 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 47 | import android.os.Handler; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 48 | import android.os.INetworkManagementService; |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 49 | import android.os.RemoteCallbackList; |
| 50 | import android.os.RemoteException; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 51 | import android.os.SystemClock; |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 52 | import android.os.SystemProperties; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 53 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 54 | import android.util.Slog; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 55 | import android.util.SparseBooleanArray; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 56 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 57 | import com.android.internal.net.NetworkStatsFactory; |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 58 | import com.android.server.NativeDaemonConnector.Command; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 59 | import com.google.android.collect.Maps; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 60 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 61 | import java.io.BufferedReader; |
| 62 | import java.io.DataInputStream; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 63 | import java.io.File; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 65 | import java.io.FileInputStream; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 66 | import java.io.IOException; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 67 | import java.io.InputStreamReader; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 68 | import java.io.PrintWriter; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 69 | import java.net.Inet4Address; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 70 | import java.net.InetAddress; |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 71 | import java.net.InterfaceAddress; |
| 72 | import java.net.NetworkInterface; |
| 73 | import java.net.SocketException; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 74 | import java.util.ArrayList; |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 75 | import java.util.Collection; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 76 | import java.util.HashMap; |
| 77 | import java.util.Map; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 78 | import java.util.NoSuchElementException; |
| 79 | import java.util.StringTokenizer; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 80 | import java.util.concurrent.CountDownLatch; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * @hide |
| 84 | */ |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 85 | public class NetworkManagementService extends INetworkManagementService.Stub |
| 86 | implements Watchdog.Monitor { |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 87 | private static final String TAG = "NetworkManagementService"; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 88 | private static final boolean DBG = false; |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 89 | private static final String NETD_TAG = "NetdConnector"; |
| 90 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 91 | private static final String ADD = "add"; |
| 92 | private static final String REMOVE = "remove"; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 93 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 94 | private static final String DEFAULT = "default"; |
| 95 | private static final String SECONDARY = "secondary"; |
| 96 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Name representing {@link #setGlobalAlert(long)} limit when delivered to |
| 99 | * {@link INetworkManagementEventObserver#limitReached(String, String)}. |
| 100 | */ |
| 101 | public static final String LIMIT_GLOBAL_ALERT = "globalAlert"; |
| 102 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 103 | class NetdResponseCode { |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 104 | /* Keep in sync with system/netd/ResponseCode.h */ |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 105 | public static final int InterfaceListResult = 110; |
| 106 | public static final int TetherInterfaceListResult = 111; |
| 107 | public static final int TetherDnsFwdTgtListResult = 112; |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 108 | public static final int TtyListResult = 113; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 109 | |
| 110 | public static final int TetherStatusResult = 210; |
| 111 | public static final int IpFwdStatusResult = 211; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 112 | public static final int InterfaceGetCfgResult = 213; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 113 | public static final int SoftapStatusResult = 214; |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 114 | public static final int InterfaceRxCounterResult = 216; |
| 115 | public static final int InterfaceTxCounterResult = 217; |
| 116 | public static final int InterfaceRxThrottleResult = 218; |
| 117 | public static final int InterfaceTxThrottleResult = 219; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 118 | public static final int QuotaCounterResult = 220; |
| 119 | public static final int TetheringStatsResult = 221; |
Selim Gurun | 84c00c6 | 2012-02-27 15:42:38 -0800 | [diff] [blame] | 120 | public static final int DnsProxyQueryResult = 222; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 121 | |
| 122 | public static final int InterfaceChange = 600; |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 123 | public static final int BandwidthControl = 601; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Binder context for this service |
| 128 | */ |
| 129 | private Context mContext; |
| 130 | |
| 131 | /** |
| 132 | * connector object for communicating with netd |
| 133 | */ |
| 134 | private NativeDaemonConnector mConnector; |
| 135 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 136 | private final Handler mMainHandler = new Handler(); |
| 137 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 138 | private Thread mThread; |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 139 | private CountDownLatch mConnectedSignal = new CountDownLatch(1); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 140 | |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 141 | private final RemoteCallbackList<INetworkManagementEventObserver> mObservers = |
| 142 | new RemoteCallbackList<INetworkManagementEventObserver>(); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 143 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 144 | private final NetworkStatsFactory mStatsFactory = new NetworkStatsFactory(); |
| 145 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 146 | private Object mQuotaLock = new Object(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 147 | /** Set of interfaces with active quotas. */ |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 148 | private HashMap<String, Long> mActiveQuotas = Maps.newHashMap(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 149 | /** Set of interfaces with active alerts. */ |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 150 | private HashMap<String, Long> mActiveAlerts = Maps.newHashMap(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 151 | /** Set of UIDs with active reject rules. */ |
| 152 | private SparseBooleanArray mUidRejectOnQuota = new SparseBooleanArray(); |
| 153 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 154 | private volatile boolean mBandwidthControlEnabled; |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 155 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Constructs a new NetworkManagementService instance |
| 158 | * |
| 159 | * @param context Binder context for this service |
| 160 | */ |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 161 | private NetworkManagementService(Context context) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 162 | mContext = context; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 163 | |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 164 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 165 | return; |
| 166 | } |
| 167 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 168 | mConnector = new NativeDaemonConnector( |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 169 | new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 160); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 170 | mThread = new Thread(mConnector, NETD_TAG); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 171 | |
| 172 | // Add ourself to the Watchdog monitors. |
| 173 | Watchdog.getInstance().addMonitor(this); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public static NetworkManagementService create(Context context) throws InterruptedException { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 177 | final NetworkManagementService service = new NetworkManagementService(context); |
| 178 | final CountDownLatch connectedSignal = service.mConnectedSignal; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 179 | if (DBG) Slog.d(TAG, "Creating NetworkManagementService"); |
| 180 | service.mThread.start(); |
| 181 | if (DBG) Slog.d(TAG, "Awaiting socket connection"); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 182 | connectedSignal.await(); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 183 | if (DBG) Slog.d(TAG, "Connected"); |
| 184 | return service; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 187 | public void systemReady() { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 188 | prepareNativeDaemon(); |
| 189 | if (DBG) Slog.d(TAG, "Prepared"); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 192 | @Override |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 193 | public void registerObserver(INetworkManagementEventObserver observer) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 194 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 195 | mObservers.register(observer); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 198 | @Override |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 199 | public void unregisterObserver(INetworkManagementEventObserver observer) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 200 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 201 | mObservers.unregister(observer); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 205 | * Notify our observers of an interface status change |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 206 | */ |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 207 | private void notifyInterfaceStatusChanged(String iface, boolean up) { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 208 | final int length = mObservers.beginBroadcast(); |
| 209 | for (int i = 0; i < length; i++) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 210 | try { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 211 | mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up); |
| 212 | } catch (RemoteException e) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 215 | mObservers.finishBroadcast(); |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 219 | * Notify our observers of an interface link state change |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 220 | * (typically, an Ethernet cable has been plugged-in or unplugged). |
| 221 | */ |
| 222 | private void notifyInterfaceLinkStateChanged(String iface, boolean up) { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 223 | final int length = mObservers.beginBroadcast(); |
| 224 | for (int i = 0; i < length; i++) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 225 | try { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 226 | mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up); |
| 227 | } catch (RemoteException e) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 230 | mObservers.finishBroadcast(); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Notify our observers of an interface addition. |
| 235 | */ |
| 236 | private void notifyInterfaceAdded(String iface) { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 237 | final int length = mObservers.beginBroadcast(); |
| 238 | for (int i = 0; i < length; i++) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 239 | try { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 240 | mObservers.getBroadcastItem(i).interfaceAdded(iface); |
| 241 | } catch (RemoteException e) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 244 | mObservers.finishBroadcast(); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Notify our observers of an interface removal. |
| 249 | */ |
| 250 | private void notifyInterfaceRemoved(String iface) { |
Jeff Sharkey | 89b8a21 | 2011-10-11 11:58:11 -0700 | [diff] [blame] | 251 | // netd already clears out quota and alerts for removed ifaces; update |
| 252 | // our sanity-checking state. |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 253 | mActiveAlerts.remove(iface); |
| 254 | mActiveQuotas.remove(iface); |
Jeff Sharkey | 89b8a21 | 2011-10-11 11:58:11 -0700 | [diff] [blame] | 255 | |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 256 | final int length = mObservers.beginBroadcast(); |
| 257 | for (int i = 0; i < length; i++) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 258 | try { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 259 | mObservers.getBroadcastItem(i).interfaceRemoved(iface); |
| 260 | } catch (RemoteException e) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 263 | mObservers.finishBroadcast(); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 266 | /** |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 267 | * Notify our observers of a limit reached. |
| 268 | */ |
| 269 | private void notifyLimitReached(String limitName, String iface) { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 270 | final int length = mObservers.beginBroadcast(); |
| 271 | for (int i = 0; i < length; i++) { |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 272 | try { |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 273 | mObservers.getBroadcastItem(i).limitReached(limitName, iface); |
| 274 | } catch (RemoteException e) { |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
Jeff Sharkey | 3df273e | 2011-12-15 15:47:12 -0800 | [diff] [blame] | 277 | mObservers.finishBroadcast(); |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 281 | * Prepare native daemon once connected, enabling modules and pushing any |
| 282 | * existing in-memory rules. |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 283 | */ |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 284 | private void prepareNativeDaemon() { |
| 285 | mBandwidthControlEnabled = false; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 286 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 287 | // only enable bandwidth control when support exists |
| 288 | final boolean hasKernelSupport = new File("/proc/net/xt_qtaguid/ctrl").exists(); |
| 289 | if (hasKernelSupport) { |
| 290 | Slog.d(TAG, "enabling bandwidth control"); |
| 291 | try { |
| 292 | mConnector.execute("bandwidth", "enable"); |
| 293 | mBandwidthControlEnabled = true; |
| 294 | } catch (NativeDaemonConnectorException e) { |
| 295 | Log.wtf(TAG, "problem enabling bandwidth controls", e); |
| 296 | } |
| 297 | } else { |
| 298 | Slog.d(TAG, "not enabling bandwidth control"); |
| 299 | } |
| 300 | |
| 301 | SystemProperties.set(PROP_QTAGUID_ENABLED, mBandwidthControlEnabled ? "1" : "0"); |
| 302 | |
| 303 | // push any existing quota or UID rules |
| 304 | synchronized (mQuotaLock) { |
| 305 | int size = mActiveQuotas.size(); |
| 306 | if (size > 0) { |
| 307 | Slog.d(TAG, "pushing " + size + " active quota rules"); |
| 308 | final HashMap<String, Long> activeQuotas = mActiveQuotas; |
| 309 | mActiveQuotas = Maps.newHashMap(); |
| 310 | for (Map.Entry<String, Long> entry : activeQuotas.entrySet()) { |
| 311 | setInterfaceQuota(entry.getKey(), entry.getValue()); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | size = mActiveAlerts.size(); |
| 316 | if (size > 0) { |
| 317 | Slog.d(TAG, "pushing " + size + " active alert rules"); |
| 318 | final HashMap<String, Long> activeAlerts = mActiveAlerts; |
| 319 | mActiveAlerts = Maps.newHashMap(); |
| 320 | for (Map.Entry<String, Long> entry : activeAlerts.entrySet()) { |
| 321 | setInterfaceAlert(entry.getKey(), entry.getValue()); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | size = mUidRejectOnQuota.size(); |
| 326 | if (size > 0) { |
| 327 | Slog.d(TAG, "pushing " + size + " active uid rules"); |
| 328 | final SparseBooleanArray uidRejectOnQuota = mUidRejectOnQuota; |
| 329 | mUidRejectOnQuota = new SparseBooleanArray(); |
| 330 | for (int i = 0; i < uidRejectOnQuota.size(); i++) { |
| 331 | setUidNetworkRules(uidRejectOnQuota.keyAt(i), uidRejectOnQuota.valueAt(i)); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 336 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 337 | // |
| 338 | // Netd Callback handling |
| 339 | // |
| 340 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 341 | private class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks { |
| 342 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 343 | public void onDaemonConnected() { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 344 | // event is dispatched from internal NDC thread, so we prepare the |
| 345 | // daemon back on main thread. |
| 346 | if (mConnectedSignal != null) { |
| 347 | mConnectedSignal.countDown(); |
| 348 | mConnectedSignal = null; |
| 349 | } else { |
| 350 | mMainHandler.post(new Runnable() { |
| 351 | @Override |
| 352 | public void run() { |
| 353 | prepareNativeDaemon(); |
| 354 | } |
| 355 | }); |
| 356 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 357 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 358 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 359 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 360 | public boolean onEvent(int code, String raw, String[] cooked) { |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 361 | switch (code) { |
| 362 | case NetdResponseCode.InterfaceChange: |
| 363 | /* |
| 364 | * a network interface change occured |
| 365 | * Format: "NNN Iface added <name>" |
| 366 | * "NNN Iface removed <name>" |
| 367 | * "NNN Iface changed <name> <up/down>" |
| 368 | * "NNN Iface linkstatus <name> <up/down>" |
| 369 | */ |
| 370 | if (cooked.length < 4 || !cooked[1].equals("Iface")) { |
| 371 | throw new IllegalStateException( |
| 372 | String.format("Invalid event from daemon (%s)", raw)); |
| 373 | } |
| 374 | if (cooked[2].equals("added")) { |
| 375 | notifyInterfaceAdded(cooked[3]); |
| 376 | return true; |
| 377 | } else if (cooked[2].equals("removed")) { |
| 378 | notifyInterfaceRemoved(cooked[3]); |
| 379 | return true; |
| 380 | } else if (cooked[2].equals("changed") && cooked.length == 5) { |
| 381 | notifyInterfaceStatusChanged(cooked[3], cooked[4].equals("up")); |
| 382 | return true; |
| 383 | } else if (cooked[2].equals("linkstate") && cooked.length == 5) { |
| 384 | notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up")); |
| 385 | return true; |
| 386 | } |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 387 | throw new IllegalStateException( |
| 388 | String.format("Invalid event from daemon (%s)", raw)); |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 389 | // break; |
| 390 | case NetdResponseCode.BandwidthControl: |
| 391 | /* |
| 392 | * Bandwidth control needs some attention |
| 393 | * Format: "NNN limit alert <alertName> <ifaceName>" |
| 394 | */ |
| 395 | if (cooked.length < 5 || !cooked[1].equals("limit")) { |
| 396 | throw new IllegalStateException( |
| 397 | String.format("Invalid event from daemon (%s)", raw)); |
| 398 | } |
| 399 | if (cooked[2].equals("alert")) { |
| 400 | notifyLimitReached(cooked[3], cooked[4]); |
| 401 | return true; |
| 402 | } |
| 403 | throw new IllegalStateException( |
| 404 | String.format("Invalid event from daemon (%s)", raw)); |
| 405 | // break; |
| 406 | default: break; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 407 | } |
| 408 | return false; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 412 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 413 | // |
| 414 | // INetworkManagementService members |
| 415 | // |
| 416 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 417 | @Override |
| 418 | public String[] listInterfaces() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 419 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 420 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 421 | return NativeDaemonEvent.filterMessageList( |
| 422 | mConnector.executeForList("interface", "list"), InterfaceListResult); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 423 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 424 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 425 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 428 | @Override |
| 429 | public InterfaceConfiguration getInterfaceConfig(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 430 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 431 | |
| 432 | final NativeDaemonEvent event; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 433 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 434 | event = mConnector.execute("interface", "getcfg", iface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 435 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 436 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 437 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 438 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 439 | event.checkCode(InterfaceGetCfgResult); |
| 440 | |
| 441 | // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3 |
| 442 | final StringTokenizer st = new StringTokenizer(event.getMessage()); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 443 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 444 | InterfaceConfiguration cfg; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 445 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 446 | cfg = new InterfaceConfiguration(); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 447 | cfg.setHardwareAddress(st.nextToken(" ")); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 448 | InetAddress addr = null; |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 449 | int prefixLength = 0; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 450 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 451 | addr = NetworkUtils.numericToInetAddress(st.nextToken()); |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 452 | } catch (IllegalArgumentException iae) { |
| 453 | Slog.e(TAG, "Failed to parse ipaddr", iae); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 457 | prefixLength = Integer.parseInt(st.nextToken()); |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 458 | } catch (NumberFormatException nfe) { |
| 459 | Slog.e(TAG, "Failed to parse prefixLength", nfe); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 460 | } |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 461 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 462 | cfg.setLinkAddress(new LinkAddress(addr, prefixLength)); |
| 463 | while (st.hasMoreTokens()) { |
| 464 | cfg.setFlag(st.nextToken()); |
| 465 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 466 | } catch (NoSuchElementException nsee) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 467 | throw new IllegalStateException("Invalid response from daemon: " + event); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 468 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 469 | return cfg; |
| 470 | } |
| 471 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 472 | @Override |
| 473 | public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 474 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 475 | LinkAddress linkAddr = cfg.getLinkAddress(); |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 476 | if (linkAddr == null || linkAddr.getAddress() == null) { |
| 477 | throw new IllegalStateException("Null LinkAddress given"); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 478 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 479 | |
| 480 | final Command cmd = new Command("interface", "setcfg", iface, |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 481 | linkAddr.getAddress().getHostAddress(), |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 482 | linkAddr.getNetworkPrefixLength()); |
| 483 | for (String flag : cfg.getFlags()) { |
| 484 | cmd.appendArg(flag); |
| 485 | } |
| 486 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 487 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 488 | mConnector.execute(cmd); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 489 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 490 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 491 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 494 | @Override |
| 495 | public void setInterfaceDown(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 496 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 497 | final InterfaceConfiguration ifcg = getInterfaceConfig(iface); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 498 | ifcg.setInterfaceDown(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 499 | setInterfaceConfig(iface, ifcg); |
Irfan Sheriff | 7244c97 | 2011-08-05 20:40:45 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 502 | @Override |
| 503 | public void setInterfaceUp(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 504 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 505 | final InterfaceConfiguration ifcg = getInterfaceConfig(iface); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 506 | ifcg.setInterfaceUp(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 507 | setInterfaceConfig(iface, ifcg); |
Irfan Sheriff | 7244c97 | 2011-08-05 20:40:45 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 510 | @Override |
| 511 | public void setInterfaceIpv6PrivacyExtensions(String iface, boolean enable) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 512 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Irfan Sheriff | 7329361 | 2011-09-14 12:31:56 -0700 | [diff] [blame] | 513 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 514 | mConnector.execute( |
| 515 | "interface", "ipv6privacyextensions", iface, enable ? "enable" : "disable"); |
Irfan Sheriff | 7329361 | 2011-09-14 12:31:56 -0700 | [diff] [blame] | 516 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 517 | throw e.rethrowAsParcelableException(); |
Irfan Sheriff | 7329361 | 2011-09-14 12:31:56 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 521 | /* TODO: This is right now a IPv4 only function. Works for wifi which loses its |
| 522 | IPv6 addresses on interface down, but we need to do full clean up here */ |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 523 | @Override |
| 524 | public void clearInterfaceAddresses(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 525 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 526 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 527 | mConnector.execute("interface", "clearaddrs", iface); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 528 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 529 | throw e.rethrowAsParcelableException(); |
Irfan Sheriff | f560061 | 2011-06-16 10:26:28 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 533 | @Override |
| 534 | public void enableIpv6(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 535 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 536 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 537 | mConnector.execute("interface", "ipv6", iface, "enable"); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 538 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 539 | throw e.rethrowAsParcelableException(); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 543 | @Override |
| 544 | public void disableIpv6(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 545 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 546 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 547 | mConnector.execute("interface", "ipv6", iface, "disable"); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 548 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 549 | throw e.rethrowAsParcelableException(); |
repo sync | 7960d9f | 2011-09-29 12:40:02 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 553 | @Override |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 554 | public void addRoute(String interfaceName, RouteInfo route) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 555 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 556 | modifyRoute(interfaceName, ADD, route, DEFAULT); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 559 | @Override |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 560 | public void removeRoute(String interfaceName, RouteInfo route) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 561 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 562 | modifyRoute(interfaceName, REMOVE, route, DEFAULT); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 565 | @Override |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 566 | public void addSecondaryRoute(String interfaceName, RouteInfo route) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 567 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 568 | modifyRoute(interfaceName, ADD, route, SECONDARY); |
| 569 | } |
| 570 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 571 | @Override |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 572 | public void removeSecondaryRoute(String interfaceName, RouteInfo route) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 573 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 574 | modifyRoute(interfaceName, REMOVE, route, SECONDARY); |
| 575 | } |
| 576 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 577 | private void modifyRoute(String interfaceName, String action, RouteInfo route, String type) { |
| 578 | final Command cmd = new Command("interface", "route", action, interfaceName, type); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 579 | |
| 580 | // create triplet: dest-ip-addr prefixlength gateway-ip-addr |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 581 | final LinkAddress la = route.getDestination(); |
| 582 | cmd.appendArg(la.getAddress().getHostAddress()); |
| 583 | cmd.appendArg(la.getNetworkPrefixLength()); |
| 584 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 585 | if (route.getGateway() == null) { |
| 586 | if (la.getAddress() instanceof Inet4Address) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 587 | cmd.appendArg("0.0.0.0"); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 588 | } else { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 589 | cmd.appendArg("::0"); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 590 | } |
| 591 | } else { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 592 | cmd.appendArg(route.getGateway().getHostAddress()); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 595 | try { |
| 596 | mConnector.execute(cmd); |
| 597 | } catch (NativeDaemonConnectorException e) { |
| 598 | throw e.rethrowAsParcelableException(); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | private ArrayList<String> readRouteList(String filename) { |
| 603 | FileInputStream fstream = null; |
| 604 | ArrayList<String> list = new ArrayList<String>(); |
| 605 | |
| 606 | try { |
| 607 | fstream = new FileInputStream(filename); |
| 608 | DataInputStream in = new DataInputStream(fstream); |
| 609 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
| 610 | String s; |
| 611 | |
| 612 | // throw away the title line |
| 613 | |
| 614 | while (((s = br.readLine()) != null) && (s.length() != 0)) { |
| 615 | list.add(s); |
| 616 | } |
| 617 | } catch (IOException ex) { |
| 618 | // return current list, possibly empty |
| 619 | } finally { |
| 620 | if (fstream != null) { |
| 621 | try { |
| 622 | fstream.close(); |
| 623 | } catch (IOException ex) {} |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | return list; |
| 628 | } |
| 629 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 630 | @Override |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 631 | public RouteInfo[] getRoutes(String interfaceName) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 632 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 633 | ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>(); |
| 634 | |
| 635 | // v4 routes listed as: |
| 636 | // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT |
| 637 | for (String s : readRouteList("/proc/net/route")) { |
| 638 | String[] fields = s.split("\t"); |
| 639 | |
| 640 | if (fields.length > 7) { |
| 641 | String iface = fields[0]; |
| 642 | |
| 643 | if (interfaceName.equals(iface)) { |
| 644 | String dest = fields[1]; |
| 645 | String gate = fields[2]; |
| 646 | String flags = fields[3]; // future use? |
| 647 | String mask = fields[7]; |
| 648 | try { |
| 649 | // address stored as a hex string, ex: 0014A8C0 |
| 650 | InetAddress destAddr = |
| 651 | NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16)); |
| 652 | int prefixLength = |
| 653 | NetworkUtils.netmaskIntToPrefixLength( |
| 654 | (int)Long.parseLong(mask, 16)); |
| 655 | LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength); |
| 656 | |
| 657 | // address stored as a hex string, ex 0014A8C0 |
| 658 | InetAddress gatewayAddr = |
| 659 | NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16)); |
| 660 | |
| 661 | RouteInfo route = new RouteInfo(linkAddress, gatewayAddr); |
| 662 | routes.add(route); |
| 663 | } catch (Exception e) { |
| 664 | Log.e(TAG, "Error parsing route " + s + " : " + e); |
| 665 | continue; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // v6 routes listed as: |
| 672 | // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface |
| 673 | for (String s : readRouteList("/proc/net/ipv6_route")) { |
| 674 | String[]fields = s.split("\\s+"); |
| 675 | if (fields.length > 9) { |
| 676 | String iface = fields[9].trim(); |
| 677 | if (interfaceName.equals(iface)) { |
| 678 | String dest = fields[0]; |
| 679 | String prefix = fields[1]; |
| 680 | String gate = fields[4]; |
| 681 | |
| 682 | try { |
| 683 | // prefix length stored as a hex string, ex 40 |
| 684 | int prefixLength = Integer.parseInt(prefix, 16); |
| 685 | |
| 686 | // address stored as a 32 char hex string |
| 687 | // ex fe800000000000000000000000000000 |
| 688 | InetAddress destAddr = NetworkUtils.hexToInet6Address(dest); |
| 689 | LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength); |
| 690 | |
| 691 | InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate); |
| 692 | |
| 693 | RouteInfo route = new RouteInfo(linkAddress, gateAddr); |
| 694 | routes.add(route); |
| 695 | } catch (Exception e) { |
| 696 | Log.e(TAG, "Error parsing route " + s + " : " + e); |
| 697 | continue; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 702 | return routes.toArray(new RouteInfo[routes.size()]); |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 705 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 706 | public void shutdown() { |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 707 | // TODO: remove from aidl if nobody calls externally |
| 708 | mContext.enforceCallingOrSelfPermission(SHUTDOWN, TAG); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 709 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 710 | Slog.d(TAG, "Shutting down"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 713 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 714 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 715 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 716 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 717 | final NativeDaemonEvent event; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 718 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 719 | event = mConnector.execute("ipfwd", "status"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 720 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 721 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 722 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 723 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 724 | // 211 Forwarding enabled |
| 725 | event.checkCode(IpFwdStatusResult); |
| 726 | return event.getMessage().endsWith("enabled"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 729 | @Override |
| 730 | public void setIpForwardingEnabled(boolean enable) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 731 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 732 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 733 | mConnector.execute("ipfwd", enable ? "enable" : "disable"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 734 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 735 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 736 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 739 | @Override |
| 740 | public void startTethering(String[] dhcpRange) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 741 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 742 | // cmd is "tether start first_start first_stop second_start second_stop ..." |
| 743 | // an odd number of addrs will fail |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 744 | |
| 745 | final Command cmd = new Command("tether", "start"); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 746 | for (String d : dhcpRange) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 747 | cmd.appendArg(d); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 748 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 749 | |
| 750 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 751 | mConnector.execute(cmd); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 752 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 753 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 754 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 757 | @Override |
| 758 | public void stopTethering() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 759 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 760 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 761 | mConnector.execute("tether", "stop"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 762 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 763 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 764 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 767 | @Override |
| 768 | public boolean isTetheringStarted() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 769 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 770 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 771 | final NativeDaemonEvent event; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 772 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 773 | event = mConnector.execute("tether", "status"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 774 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 775 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 776 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 777 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 778 | // 210 Tethering services started |
| 779 | event.checkCode(TetherStatusResult); |
| 780 | return event.getMessage().endsWith("started"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 783 | @Override |
| 784 | public void tetherInterface(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 785 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 786 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 787 | mConnector.execute("tether", "interface", "add", iface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 788 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 789 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 790 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 793 | @Override |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 794 | public void untetherInterface(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 795 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 796 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 797 | mConnector.execute("tether", "interface", "remove", iface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 798 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 799 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 800 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 803 | @Override |
| 804 | public String[] listTetheredInterfaces() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 805 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 806 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 807 | return NativeDaemonEvent.filterMessageList( |
| 808 | mConnector.executeForList("tether", "interface", "list"), |
| 809 | TetherInterfaceListResult); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 810 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 811 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 812 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 815 | @Override |
| 816 | public void setDnsForwarders(String[] dns) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 817 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 818 | |
| 819 | final Command cmd = new Command("tether", "dns", "set"); |
| 820 | for (String s : dns) { |
| 821 | cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress()); |
| 822 | } |
| 823 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 824 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 825 | mConnector.execute(cmd); |
| 826 | } catch (NativeDaemonConnectorException e) { |
| 827 | throw e.rethrowAsParcelableException(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 831 | @Override |
| 832 | public String[] getDnsForwarders() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 833 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 834 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 835 | return NativeDaemonEvent.filterMessageList( |
| 836 | mConnector.executeForList("tether", "dns", "list"), TetherDnsFwdTgtListResult); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 837 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 838 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 839 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 842 | private void modifyNat(String action, String internalInterface, String externalInterface) |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 843 | throws SocketException { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 844 | final Command cmd = new Command("nat", action, internalInterface, externalInterface); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 845 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 846 | final NetworkInterface internalNetworkInterface = NetworkInterface.getByName( |
| 847 | internalInterface); |
Robert Greenwalt | e83d181 | 2011-11-21 14:44:39 -0800 | [diff] [blame] | 848 | if (internalNetworkInterface == null) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 849 | cmd.appendArg("0"); |
Robert Greenwalt | e83d181 | 2011-11-21 14:44:39 -0800 | [diff] [blame] | 850 | } else { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 851 | Collection<InterfaceAddress> interfaceAddresses = internalNetworkInterface |
| 852 | .getInterfaceAddresses(); |
| 853 | cmd.appendArg(interfaceAddresses.size()); |
Robert Greenwalt | e83d181 | 2011-11-21 14:44:39 -0800 | [diff] [blame] | 854 | for (InterfaceAddress ia : interfaceAddresses) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 855 | InetAddress addr = NetworkUtils.getNetworkPart( |
| 856 | ia.getAddress(), ia.getNetworkPrefixLength()); |
| 857 | cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength()); |
Robert Greenwalt | e83d181 | 2011-11-21 14:44:39 -0800 | [diff] [blame] | 858 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 861 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 862 | mConnector.execute(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 863 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 864 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 865 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 868 | @Override |
| 869 | public void enableNat(String internalInterface, String externalInterface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 870 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 871 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 872 | modifyNat("enable", internalInterface, externalInterface); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 873 | } catch (SocketException e) { |
| 874 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 875 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 878 | @Override |
| 879 | public void disableNat(String internalInterface, String externalInterface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 880 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 881 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 882 | modifyNat("disable", internalInterface, externalInterface); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 883 | } catch (SocketException e) { |
| 884 | throw new IllegalStateException(e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 885 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 886 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 887 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 888 | @Override |
| 889 | public String[] listTtys() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 890 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 891 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 892 | return NativeDaemonEvent.filterMessageList( |
| 893 | mConnector.executeForList("list_ttys"), TtyListResult); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 894 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 895 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 896 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 899 | @Override |
| 900 | public void attachPppd( |
| 901 | String tty, String localAddr, String remoteAddr, String dns1Addr, String dns2Addr) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 902 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 903 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 904 | mConnector.execute("pppd", "attach", tty, |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 905 | NetworkUtils.numericToInetAddress(localAddr).getHostAddress(), |
| 906 | NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(), |
| 907 | NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(), |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 908 | NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress()); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 909 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 910 | throw e.rethrowAsParcelableException(); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 914 | @Override |
| 915 | public void detachPppd(String tty) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 916 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 917 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 918 | mConnector.execute("pppd", "detach", tty); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 919 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 920 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 921 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 922 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 923 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 924 | @Override |
| 925 | public void startAccessPoint( |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 926 | WifiConfiguration wifiConfig, String wlanIface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 927 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 928 | try { |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 929 | wifiFirmwareReload(wlanIface, "AP"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 930 | if (wifiConfig == null) { |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 931 | mConnector.execute("softap", "set", wlanIface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 932 | } else { |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 933 | mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID, |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 934 | getSecurityType(wifiConfig), wifiConfig.preSharedKey); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 935 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 936 | mConnector.execute("softap", "startap"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 937 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 938 | throw e.rethrowAsParcelableException(); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 939 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 942 | private static String getSecurityType(WifiConfiguration wifiConfig) { |
Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 943 | switch (wifiConfig.getAuthType()) { |
| 944 | case KeyMgmt.WPA_PSK: |
| 945 | return "wpa-psk"; |
| 946 | case KeyMgmt.WPA2_PSK: |
| 947 | return "wpa2-psk"; |
| 948 | default: |
| 949 | return "open"; |
| 950 | } |
| 951 | } |
| 952 | |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 953 | /* @param mode can be "AP", "STA" or "P2P" */ |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 954 | @Override |
| 955 | public void wifiFirmwareReload(String wlanIface, String mode) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 956 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 957 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 958 | mConnector.execute("softap", "fwreload", wlanIface, mode); |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 959 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 960 | throw e.rethrowAsParcelableException(); |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 964 | @Override |
| 965 | public void stopAccessPoint(String wlanIface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 966 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 967 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 968 | mConnector.execute("softap", "stopap"); |
Irfan Sheriff | cb30b22 | 2011-07-29 20:54:52 -0700 | [diff] [blame] | 969 | wifiFirmwareReload(wlanIface, "STA"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 970 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 971 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 972 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 973 | } |
| 974 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 975 | @Override |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 976 | public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 977 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 978 | try { |
| 979 | if (wifiConfig == null) { |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 980 | mConnector.execute("softap", "set", wlanIface); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 981 | } else { |
Irfan Sheriff | 9054275 | 2012-06-19 15:44:35 -0700 | [diff] [blame] | 982 | mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID, |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 983 | getSecurityType(wifiConfig), wifiConfig.preSharedKey); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 984 | } |
| 985 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 986 | throw e.rethrowAsParcelableException(); |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 989 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 990 | @Override |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 991 | public NetworkStats getNetworkStatsSummaryDev() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 992 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 993 | return mStatsFactory.readNetworkStatsSummaryDev(); |
| 994 | } |
| 995 | |
| 996 | @Override |
| 997 | public NetworkStats getNetworkStatsSummaryXt() { |
| 998 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 999 | return mStatsFactory.readNetworkStatsSummaryXt(); |
Jeff Sharkey | ae2c181 | 2011-10-04 13:11:40 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1002 | @Override |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 1003 | public NetworkStats getNetworkStatsDetail() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1004 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1005 | return mStatsFactory.readNetworkStatsDetail(UID_ALL); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1008 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1009 | public void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1010 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1011 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1012 | // silently discard when control disabled |
| 1013 | // TODO: eventually migrate to be always enabled |
| 1014 | if (!mBandwidthControlEnabled) return; |
| 1015 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1016 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1017 | if (mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1018 | throw new IllegalStateException("iface " + iface + " already has quota"); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1021 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1022 | // TODO: support quota shared across interfaces |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1023 | mConnector.execute("bandwidth", "setiquota", iface, quotaBytes); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1024 | mActiveQuotas.put(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1025 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1026 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1027 | } |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1032 | public void removeInterfaceQuota(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1033 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1034 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1035 | // silently discard when control disabled |
| 1036 | // TODO: eventually migrate to be always enabled |
| 1037 | if (!mBandwidthControlEnabled) return; |
| 1038 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1039 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1040 | if (!mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1041 | // TODO: eventually consider throwing |
| 1042 | return; |
| 1043 | } |
| 1044 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1045 | mActiveQuotas.remove(iface); |
| 1046 | mActiveAlerts.remove(iface); |
Jeff Sharkey | 38ddeaa | 2011-11-08 13:04:22 -0800 | [diff] [blame] | 1047 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1048 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1049 | // TODO: support quota shared across interfaces |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1050 | mConnector.execute("bandwidth", "removeiquota", iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1051 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1052 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1058 | public void setInterfaceAlert(String iface, long alertBytes) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1059 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1060 | |
| 1061 | // silently discard when control disabled |
| 1062 | // TODO: eventually migrate to be always enabled |
| 1063 | if (!mBandwidthControlEnabled) return; |
| 1064 | |
| 1065 | // quick sanity check |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1066 | if (!mActiveQuotas.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1067 | throw new IllegalStateException("setting alert requires existing quota on iface"); |
| 1068 | } |
| 1069 | |
| 1070 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1071 | if (mActiveAlerts.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1072 | throw new IllegalStateException("iface " + iface + " already has alert"); |
| 1073 | } |
| 1074 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1075 | try { |
| 1076 | // TODO: support alert shared across interfaces |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1077 | mConnector.execute("bandwidth", "setinterfacealert", iface, alertBytes); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1078 | mActiveAlerts.put(iface, alertBytes); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1079 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1080 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | @Override |
| 1086 | public void removeInterfaceAlert(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1087 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1088 | |
| 1089 | // silently discard when control disabled |
| 1090 | // TODO: eventually migrate to be always enabled |
| 1091 | if (!mBandwidthControlEnabled) return; |
| 1092 | |
| 1093 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1094 | if (!mActiveAlerts.containsKey(iface)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1095 | // TODO: eventually consider throwing |
| 1096 | return; |
| 1097 | } |
| 1098 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1099 | try { |
| 1100 | // TODO: support alert shared across interfaces |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1101 | mConnector.execute("bandwidth", "removeinterfacealert", iface); |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1102 | mActiveAlerts.remove(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1103 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1104 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | @Override |
| 1110 | public void setGlobalAlert(long alertBytes) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1111 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1112 | |
| 1113 | // silently discard when control disabled |
| 1114 | // TODO: eventually migrate to be always enabled |
| 1115 | if (!mBandwidthControlEnabled) return; |
| 1116 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1117 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1118 | mConnector.execute("bandwidth", "setglobalalert", alertBytes); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1119 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1120 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | @Override |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1125 | public void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1126 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1127 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1128 | // silently discard when control disabled |
| 1129 | // TODO: eventually migrate to be always enabled |
| 1130 | if (!mBandwidthControlEnabled) return; |
| 1131 | |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1132 | synchronized (mQuotaLock) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1133 | final boolean oldRejectOnQuota = mUidRejectOnQuota.get(uid, false); |
| 1134 | if (oldRejectOnQuota == rejectOnQuotaInterfaces) { |
| 1135 | // TODO: eventually consider throwing |
| 1136 | return; |
| 1137 | } |
| 1138 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1139 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1140 | mConnector.execute("bandwidth", |
| 1141 | rejectOnQuotaInterfaces ? "addnaughtyapps" : "removenaughtyapps", uid); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1142 | if (rejectOnQuotaInterfaces) { |
| 1143 | mUidRejectOnQuota.put(uid, true); |
| 1144 | } else { |
| 1145 | mUidRejectOnQuota.delete(uid); |
| 1146 | } |
| 1147 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1148 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1149 | } |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 1153 | @Override |
| 1154 | public boolean isBandwidthControlEnabled() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1155 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 1156 | return mBandwidthControlEnabled; |
| 1157 | } |
| 1158 | |
| 1159 | @Override |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1160 | public NetworkStats getNetworkStatsUidDetail(int uid) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1161 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1162 | return mStatsFactory.readNetworkStatsDetail(uid); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1165 | @Override |
| 1166 | public NetworkStats getNetworkStatsTethering(String[] ifacePairs) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1167 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1168 | |
| 1169 | if (ifacePairs.length % 2 != 0) { |
| 1170 | throw new IllegalArgumentException( |
| 1171 | "unexpected ifacePairs; length=" + ifacePairs.length); |
| 1172 | } |
| 1173 | |
| 1174 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1); |
| 1175 | for (int i = 0; i < ifacePairs.length; i += 2) { |
| 1176 | final String ifaceIn = ifacePairs[i]; |
| 1177 | final String ifaceOut = ifacePairs[i + 1]; |
| 1178 | if (ifaceIn != null && ifaceOut != null) { |
| 1179 | stats.combineValues(getNetworkStatsTethering(ifaceIn, ifaceOut)); |
| 1180 | } |
| 1181 | } |
| 1182 | return stats; |
| 1183 | } |
| 1184 | |
| 1185 | private NetworkStats.Entry getNetworkStatsTethering(String ifaceIn, String ifaceOut) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1186 | final NativeDaemonEvent event; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1187 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1188 | event = mConnector.execute("bandwidth", "gettetherstats", ifaceIn, ifaceOut); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1189 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1190 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1193 | event.checkCode(TetheringStatsResult); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1194 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1195 | // 221 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets |
| 1196 | final StringTokenizer tok = new StringTokenizer(event.getMessage()); |
| 1197 | tok.nextToken(); |
| 1198 | tok.nextToken(); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1199 | |
| 1200 | try { |
| 1201 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 1202 | entry.iface = ifaceIn; |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1203 | entry.uid = UID_TETHERING; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1204 | entry.set = SET_DEFAULT; |
| 1205 | entry.tag = TAG_NONE; |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1206 | entry.rxBytes = Long.parseLong(tok.nextToken()); |
| 1207 | entry.rxPackets = Long.parseLong(tok.nextToken()); |
| 1208 | entry.txBytes = Long.parseLong(tok.nextToken()); |
| 1209 | entry.txPackets = Long.parseLong(tok.nextToken()); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 1210 | return entry; |
| 1211 | } catch (NumberFormatException e) { |
| 1212 | throw new IllegalStateException( |
| 1213 | "problem parsing tethering stats for " + ifaceIn + " " + ifaceOut + ": " + e); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1217 | @Override |
San Mehat | f0db6e1 | 2010-04-07 15:22:10 -0700 | [diff] [blame] | 1218 | public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1219 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1220 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1221 | mConnector.execute("interface", "setthrottle", iface, rxKbps, txKbps); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1222 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1223 | throw e.rethrowAsParcelableException(); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1224 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | private int getInterfaceThrottle(String iface, boolean rx) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1228 | final NativeDaemonEvent event; |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1229 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1230 | event = mConnector.execute("interface", "getthrottle", iface, rx ? "rx" : "tx"); |
| 1231 | } catch (NativeDaemonConnectorException e) { |
| 1232 | throw e.rethrowAsParcelableException(); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1233 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1234 | |
| 1235 | if (rx) { |
| 1236 | event.checkCode(InterfaceRxThrottleResult); |
| 1237 | } else { |
| 1238 | event.checkCode(InterfaceTxThrottleResult); |
| 1239 | } |
| 1240 | |
| 1241 | try { |
| 1242 | return Integer.parseInt(event.getMessage()); |
| 1243 | } catch (NumberFormatException e) { |
| 1244 | throw new IllegalStateException("unexpected response:" + event); |
| 1245 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1248 | @Override |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1249 | public int getInterfaceRxThrottle(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1250 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1251 | return getInterfaceThrottle(iface, true); |
| 1252 | } |
| 1253 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1254 | @Override |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1255 | public int getInterfaceTxThrottle(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1256 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 1257 | return getInterfaceThrottle(iface, false); |
| 1258 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 1259 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1260 | @Override |
| 1261 | public void setDefaultInterfaceForDns(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1262 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1263 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1264 | mConnector.execute("resolver", "setdefaultif", iface); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1265 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1266 | throw e.rethrowAsParcelableException(); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1270 | @Override |
| 1271 | public void setDnsServersForInterface(String iface, String[] servers) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1272 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1273 | |
| 1274 | final Command cmd = new Command("resolver", "setifdns", iface); |
| 1275 | for (String s : servers) { |
| 1276 | InetAddress a = NetworkUtils.numericToInetAddress(s); |
| 1277 | if (a.isAnyLocalAddress() == false) { |
| 1278 | cmd.appendArg(a.getHostAddress()); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1279 | } |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | try { |
| 1283 | mConnector.execute(cmd); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1284 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1285 | throw e.rethrowAsParcelableException(); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1289 | @Override |
| 1290 | public void flushDefaultDnsCache() { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1291 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1292 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1293 | mConnector.execute("resolver", "flushdefaultif"); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1294 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1295 | throw e.rethrowAsParcelableException(); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Jeff Sharkey | af75c33 | 2011-11-18 12:41:12 -0800 | [diff] [blame] | 1299 | @Override |
| 1300 | public void flushInterfaceDnsCache(String iface) { |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 1301 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1302 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 1303 | mConnector.execute("resolver", "flushif", iface); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1304 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | 276642b | 2011-12-01 11:24:24 -0800 | [diff] [blame] | 1305 | throw e.rethrowAsParcelableException(); |
Mattias Falk | 7475c0c | 2011-04-04 16:10:36 +0200 | [diff] [blame] | 1306 | } |
| 1307 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 1308 | |
| 1309 | /** {@inheritDoc} */ |
| 1310 | public void monitor() { |
| 1311 | if (mConnector != null) { |
| 1312 | mConnector.monitor(); |
| 1313 | } |
| 1314 | } |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1315 | |
| 1316 | @Override |
| 1317 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1318 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
| 1319 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 1320 | pw.println("NetworkManagementService NativeDaemonConnector Log:"); |
| 1321 | mConnector.dump(fd, pw, args); |
| 1322 | pw.println(); |
| 1323 | |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1324 | pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled); |
| 1325 | |
| 1326 | synchronized (mQuotaLock) { |
Jeff Sharkey | b24a785 | 2012-05-01 15:19:37 -0700 | [diff] [blame] | 1327 | pw.print("Active quota ifaces: "); pw.println(mActiveQuotas.toString()); |
| 1328 | pw.print("Active alert ifaces: "); pw.println(mActiveAlerts.toString()); |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | synchronized (mUidRejectOnQuota) { |
| 1332 | pw.print("UID reject on quota ifaces: ["); |
| 1333 | final int size = mUidRejectOnQuota.size(); |
| 1334 | for (int i = 0; i < size; i++) { |
| 1335 | pw.print(mUidRejectOnQuota.keyAt(i)); |
| 1336 | if (i < size - 1) pw.print(","); |
| 1337 | } |
| 1338 | pw.println("]"); |
| 1339 | } |
| 1340 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1341 | } |