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 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.res.Resources; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.net.Uri; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 27 | import android.net.InterfaceConfiguration; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 28 | import android.net.INetworkManagementEventObserver; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 29 | import android.os.INetworkManagementService; |
| 30 | import android.os.Handler; |
| 31 | import android.text.TextUtils; |
| 32 | import android.util.Log; |
| 33 | import java.util.ArrayList; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 34 | import java.util.StringTokenizer; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 35 | import android.provider.Settings; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.database.ContentObserver; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.io.FileReader; |
| 41 | import java.lang.IllegalStateException; |
| 42 | |
| 43 | import java.net.InetAddress; |
| 44 | import java.net.UnknownHostException; |
| 45 | |
| 46 | /** |
| 47 | * @hide |
| 48 | */ |
| 49 | class NetworkManagementService extends INetworkManagementService.Stub { |
| 50 | |
| 51 | private static final String TAG = "NetworkManagmentService"; |
| 52 | |
| 53 | class NetdResponseCode { |
| 54 | public static final int InterfaceListResult = 110; |
| 55 | public static final int TetherInterfaceListResult = 111; |
| 56 | public static final int TetherDnsFwdTgtListResult = 112; |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 57 | public static final int TtyListResult = 113; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 58 | |
| 59 | public static final int TetherStatusResult = 210; |
| 60 | public static final int IpFwdStatusResult = 211; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 61 | public static final int InterfaceGetCfgResult = 213; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame^] | 62 | public static final int SoftapStatusResult = 214; |
| 63 | public static final int UsbRNDISStatusResult = 215; |
| 64 | |
| 65 | public static final int InterfaceChange = 600; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Binder context for this service |
| 70 | */ |
| 71 | private Context mContext; |
| 72 | |
| 73 | /** |
| 74 | * connector object for communicating with netd |
| 75 | */ |
| 76 | private NativeDaemonConnector mConnector; |
| 77 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 78 | private ArrayList<INetworkManagementEventObserver> mObservers; |
| 79 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Constructs a new NetworkManagementService instance |
| 82 | * |
| 83 | * @param context Binder context for this service |
| 84 | */ |
San Mehat | d1df8ac | 2010-01-26 06:17:26 -0800 | [diff] [blame] | 85 | public NetworkManagementService(Context context) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 86 | mContext = context; |
| 87 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 88 | mObservers = new ArrayList<INetworkManagementEventObserver>(); |
| 89 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 90 | mConnector = new NativeDaemonConnector( |
| 91 | new NetdCallbackReceiver(), "netd", 10, "NetdConnector"); |
| 92 | Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName()); |
| 93 | thread.start(); |
| 94 | } |
| 95 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 96 | public void registerObserver(INetworkManagementEventObserver obs) { |
| 97 | Log.d(TAG, "Registering observer"); |
| 98 | mObservers.add(obs); |
| 99 | } |
| 100 | |
| 101 | public void unregisterObserver(INetworkManagementEventObserver obs) { |
| 102 | Log.d(TAG, "Unregistering observer"); |
| 103 | mObservers.remove(mObservers.indexOf(obs)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Notify our observers of an interface link status change |
| 108 | */ |
| 109 | private void notifyInterfaceLinkStatusChanged(String iface, boolean link) { |
| 110 | for (INetworkManagementEventObserver obs : mObservers) { |
| 111 | try { |
| 112 | obs.interfaceLinkStatusChanged(iface, link); |
| 113 | } catch (Exception ex) { |
| 114 | Log.w(TAG, "Observer notifier failed", ex); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Notify our observers of an interface addition. |
| 121 | */ |
| 122 | private void notifyInterfaceAdded(String iface) { |
| 123 | for (INetworkManagementEventObserver obs : mObservers) { |
| 124 | try { |
| 125 | obs.interfaceAdded(iface); |
| 126 | } catch (Exception ex) { |
| 127 | Log.w(TAG, "Observer notifier failed", ex); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Notify our observers of an interface removal. |
| 134 | */ |
| 135 | private void notifyInterfaceRemoved(String iface) { |
| 136 | for (INetworkManagementEventObserver obs : mObservers) { |
| 137 | try { |
| 138 | obs.interfaceRemoved(iface); |
| 139 | } catch (Exception ex) { |
| 140 | Log.w(TAG, "Observer notifier failed", ex); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 146 | // |
| 147 | // Netd Callback handling |
| 148 | // |
| 149 | |
| 150 | class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks { |
| 151 | public void onDaemonConnected() { |
| 152 | new Thread() { |
| 153 | public void run() { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 154 | } |
| 155 | }.start(); |
| 156 | } |
| 157 | public boolean onEvent(int code, String raw, String[] cooked) { |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame^] | 158 | if (code == NetdResponseCode.InterfaceChange) { |
| 159 | /* |
| 160 | * a network interface change occured |
| 161 | * Format: "NNN Iface added <name>" |
| 162 | * "NNN Iface removed <name>" |
| 163 | * "NNN Iface changed <name> <up/down>" |
| 164 | */ |
| 165 | if (cooked.length < 4 || !cooked[1].equals("Iface")) { |
| 166 | throw new IllegalStateException( |
| 167 | String.format("Invalid event from daemon (%s)", raw)); |
| 168 | } |
| 169 | if (cooked[2].equals("added")) { |
| 170 | notifyInterfaceAdded(cooked[3]); |
| 171 | return true; |
| 172 | } else if (cooked[2].equals("removed")) { |
| 173 | notifyInterfaceRemoved(cooked[3]); |
| 174 | return true; |
| 175 | } else if (cooked[2].equals("changed") && cooked.length == 5) { |
| 176 | notifyInterfaceLinkStatusChanged(cooked[3], cooked[4].equals("up")); |
| 177 | return true; |
| 178 | } |
| 179 | throw new IllegalStateException( |
| 180 | String.format("Invalid event from daemon (%s)", raw)); |
| 181 | } |
| 182 | return false; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 186 | private static int stringToIpAddr(String addrString) throws UnknownHostException { |
| 187 | try { |
| 188 | String[] parts = addrString.split("\\."); |
| 189 | if (parts.length != 4) { |
| 190 | throw new UnknownHostException(addrString); |
| 191 | } |
| 192 | |
| 193 | int a = Integer.parseInt(parts[0]) ; |
| 194 | int b = Integer.parseInt(parts[1]) << 8; |
| 195 | int c = Integer.parseInt(parts[2]) << 16; |
| 196 | int d = Integer.parseInt(parts[3]) << 24; |
| 197 | |
| 198 | return a | b | c | d; |
| 199 | } catch (NumberFormatException ex) { |
| 200 | throw new UnknownHostException(addrString); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | public static String intToIpString(int i) { |
| 205 | return ((i >> 24 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + |
| 206 | (i & 0xFF); |
| 207 | } |
| 208 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 209 | // |
| 210 | // INetworkManagementService members |
| 211 | // |
| 212 | |
| 213 | public String[] listInterfaces() throws IllegalStateException { |
| 214 | mContext.enforceCallingOrSelfPermission( |
| 215 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 216 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 217 | return mConnector.doListCommand("interface list", NetdResponseCode.InterfaceListResult); |
| 218 | } |
| 219 | |
| 220 | public InterfaceConfiguration getInterfaceConfig(String iface) throws IllegalStateException { |
| 221 | String rsp = mConnector.doCommand("interface getcfg " + iface).get(0); |
| 222 | Log.d(TAG, String.format("rsp <%s>", rsp)); |
| 223 | |
| 224 | // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz.zzz.zzz.zzz [flag1 flag2 flag3] |
| 225 | StringTokenizer st = new StringTokenizer(rsp); |
| 226 | |
| 227 | try { |
| 228 | int code = Integer.parseInt(st.nextToken(" ")); |
| 229 | if (code != NetdResponseCode.InterfaceGetCfgResult) { |
| 230 | throw new IllegalStateException( |
| 231 | String.format("Expected code %d, but got %d", |
| 232 | NetdResponseCode.InterfaceGetCfgResult, code)); |
| 233 | } |
| 234 | } catch (NumberFormatException nfe) { |
| 235 | throw new IllegalStateException( |
| 236 | String.format("Invalid response from daemon (%s)", rsp)); |
| 237 | } |
| 238 | |
| 239 | InterfaceConfiguration cfg = new InterfaceConfiguration(); |
| 240 | cfg.hwAddr = st.nextToken(" "); |
| 241 | try { |
| 242 | cfg.ipAddr = stringToIpAddr(st.nextToken(" ")); |
| 243 | } catch (UnknownHostException uhe) { |
| 244 | Log.e(TAG, "Failed to parse ipaddr", uhe); |
| 245 | cfg.ipAddr = 0; |
| 246 | } |
| 247 | |
| 248 | try { |
| 249 | cfg.netmask = stringToIpAddr(st.nextToken(" ")); |
| 250 | } catch (UnknownHostException uhe) { |
| 251 | Log.e(TAG, "Failed to parse netmask", uhe); |
| 252 | cfg.netmask = 0; |
| 253 | } |
| 254 | cfg.interfaceFlags = st.nextToken("]"); |
| 255 | Log.d(TAG, String.format("flags <%s>", cfg.interfaceFlags)); |
| 256 | return cfg; |
| 257 | } |
| 258 | |
| 259 | public void setInterfaceConfig( |
| 260 | String iface, InterfaceConfiguration cfg) throws IllegalStateException { |
| 261 | String cmd = String.format("interface setcfg %s %s %s", iface, |
| 262 | intToIpString(cfg.ipAddr), intToIpString(cfg.netmask), cfg.interfaceFlags); |
| 263 | mConnector.doCommand(cmd); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | public void shutdown() { |
| 267 | if (mContext.checkCallingOrSelfPermission( |
| 268 | android.Manifest.permission.SHUTDOWN) |
| 269 | != PackageManager.PERMISSION_GRANTED) { |
| 270 | throw new SecurityException("Requires SHUTDOWN permission"); |
| 271 | } |
| 272 | |
| 273 | Log.d(TAG, "Shutting down"); |
| 274 | } |
| 275 | |
| 276 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
| 277 | mContext.enforceCallingOrSelfPermission( |
| 278 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 279 | |
| 280 | ArrayList<String> rsp = mConnector.doCommand("ipfwd status"); |
| 281 | |
| 282 | for (String line : rsp) { |
| 283 | String []tok = line.split(" "); |
| 284 | int code = Integer.parseInt(tok[0]); |
| 285 | if (code == NetdResponseCode.IpFwdStatusResult) { |
| 286 | // 211 Forwarding <enabled/disabled> |
| 287 | if (tok.length !=2) { |
| 288 | throw new IllegalStateException( |
| 289 | String.format("Malformatted list entry '%s'", line)); |
| 290 | } |
| 291 | if (tok[2].equals("enabled")) |
| 292 | return true; |
| 293 | return false; |
| 294 | } else { |
| 295 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 296 | } |
| 297 | } |
| 298 | throw new IllegalStateException("Got an empty response"); |
| 299 | } |
| 300 | |
| 301 | public void setIpForwardingEnabled(boolean enable) throws IllegalStateException { |
| 302 | mContext.enforceCallingOrSelfPermission( |
| 303 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 304 | mConnector.doCommand(String.format("ipfwd %sable", (enable ? "en" : "dis"))); |
| 305 | } |
| 306 | |
| 307 | public void startTethering(String dhcpRangeStart, String dhcpRangeEnd) |
| 308 | throws IllegalStateException { |
| 309 | mContext.enforceCallingOrSelfPermission( |
| 310 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 311 | mConnector.doCommand(String.format("tether start %s %s", dhcpRangeStart, dhcpRangeEnd)); |
| 312 | } |
| 313 | |
| 314 | public void stopTethering() throws IllegalStateException { |
| 315 | mContext.enforceCallingOrSelfPermission( |
| 316 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 317 | mConnector.doCommand("tether stop"); |
| 318 | } |
| 319 | |
| 320 | public boolean isTetheringStarted() throws IllegalStateException { |
| 321 | mContext.enforceCallingOrSelfPermission( |
| 322 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 323 | |
| 324 | ArrayList<String> rsp = mConnector.doCommand("tether status"); |
| 325 | |
| 326 | for (String line : rsp) { |
| 327 | String []tok = line.split(" "); |
| 328 | int code = Integer.parseInt(tok[0]); |
| 329 | if (code == NetdResponseCode.TetherStatusResult) { |
| 330 | // XXX: Tethering services <started/stopped> <TBD>... |
| 331 | if (tok[2].equals("started")) |
| 332 | return true; |
| 333 | return false; |
| 334 | } else { |
| 335 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 336 | } |
| 337 | } |
| 338 | throw new IllegalStateException("Got an empty response"); |
| 339 | } |
| 340 | |
| 341 | public void tetherInterface(String iface) throws IllegalStateException { |
| 342 | mContext.enforceCallingOrSelfPermission( |
| 343 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 344 | mConnector.doCommand("tether interface add " + iface); |
| 345 | } |
| 346 | |
| 347 | public void untetherInterface(String iface) { |
| 348 | mContext.enforceCallingOrSelfPermission( |
| 349 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 350 | mConnector.doCommand("tether interface remove " + iface); |
| 351 | } |
| 352 | |
| 353 | public String[] listTetheredInterfaces() throws IllegalStateException { |
| 354 | mContext.enforceCallingOrSelfPermission( |
| 355 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 356 | return mConnector.doListCommand( |
| 357 | "tether interface list", NetdResponseCode.TetherInterfaceListResult); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | public void setDnsForwarders(String[] dns) throws IllegalStateException { |
| 361 | mContext.enforceCallingOrSelfPermission( |
| 362 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 363 | try { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 364 | String cmd = "tether dns set"; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 365 | for (String s : dns) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 366 | cmd += " " + InetAddress.getByName(s).getHostAddress(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 367 | } |
| 368 | mConnector.doCommand(cmd); |
| 369 | } catch (UnknownHostException e) { |
| 370 | throw new IllegalStateException("Error resolving dns name", e); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | public String[] getDnsForwarders() throws IllegalStateException { |
| 375 | mContext.enforceCallingOrSelfPermission( |
| 376 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 377 | return mConnector.doListCommand( |
| 378 | "tether dns list", NetdResponseCode.TetherDnsFwdTgtListResult); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | public void enableNat(String internalInterface, String externalInterface) |
| 382 | throws IllegalStateException { |
| 383 | mContext.enforceCallingOrSelfPermission( |
| 384 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 385 | mConnector.doCommand( |
| 386 | String.format("nat enable %s %s", internalInterface, externalInterface)); |
| 387 | } |
| 388 | |
| 389 | public void disableNat(String internalInterface, String externalInterface) |
| 390 | throws IllegalStateException { |
| 391 | mContext.enforceCallingOrSelfPermission( |
| 392 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 393 | mConnector.doCommand( |
| 394 | String.format("nat disable %s %s", internalInterface, externalInterface)); |
| 395 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 396 | |
| 397 | public String[] listTtys() throws IllegalStateException { |
| 398 | mContext.enforceCallingOrSelfPermission( |
| 399 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 400 | return mConnector.doListCommand("list_ttys", NetdResponseCode.TtyListResult); |
| 401 | } |
| 402 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 403 | public void attachPppd(String tty, String localAddr, String remoteAddr, String dns1Addr, |
| 404 | String dns2Addr) throws IllegalStateException { |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 405 | try { |
| 406 | mContext.enforceCallingOrSelfPermission( |
| 407 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 408 | mConnector.doCommand(String.format("pppd attach %s %s %s %s %s", tty, |
| 409 | InetAddress.getByName(localAddr).getHostAddress(), |
| 410 | InetAddress.getByName(remoteAddr).getHostAddress(), |
| 411 | InetAddress.getByName(dns1Addr).getHostAddress(), |
| 412 | InetAddress.getByName(dns2Addr).getHostAddress())); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 413 | } catch (UnknownHostException e) { |
| 414 | throw new IllegalStateException("Error resolving addr", e); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | public void detachPppd(String tty) throws IllegalStateException { |
| 419 | mContext.enforceCallingOrSelfPermission( |
| 420 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 421 | mConnector.doCommand(String.format("pppd detach %s", tty)); |
| 422 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 423 | } |