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