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