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