Merge c41aefe1

Change-Id: I70fc4382f832d1a2bded8a5ee7d9b96fac77879b
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index fbe5379..8a678d6 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -38,32 +38,6 @@
     /** Bring the named network interface down. */
     public native static int disableInterface(String interfaceName);
 
-    /**
-     * Add a route to the routing table.
-     *
-     * @param interfaceName the interface to route through.
-     * @param dst the network or host to route to. May be IPv4 or IPv6, e.g.
-     * "0.0.0.0" or "2001:4860::".
-     * @param prefixLength the prefix length of the route.
-     * @param gw the gateway to use, e.g., "192.168.251.1". If null,
-     * indicates a directly-connected route.
-     */
-    public native static int addRoute(String interfaceName, String dst,
-          int prefixLength, String gw);
-
-    /** Return the gateway address for the default route for the named interface. */
-    public static InetAddress getDefaultRoute(String interfaceName) {
-        int addr = getDefaultRouteNative(interfaceName);
-        return intToInetAddress(addr);
-    }
-    private native static int getDefaultRouteNative(String interfaceName);
-
-    /** Remove host routes that uses the named interface. */
-    public native static int removeHostRoutes(String interfaceName);
-
-    /** Remove the default route for the named interface. */
-    public native static int removeDefaultRoute(String interfaceName);
-
     /** Reset any sockets that are connected via the named interface. */
     public native static int resetConnections(String interfaceName);
 
@@ -160,6 +134,15 @@
     }
 
     /**
+     * Convert a IPv4 netmask integer to a prefix length
+     * @param netmask as an integer in network byte order
+     * @return the network prefix length
+     */
+    public static int netmaskIntToPrefixLength(int netmask) {
+        return Integer.bitCount(netmask);
+    }
+
+    /**
      * Create an InetAddress from a string where the string must be a standard
      * representation of a V4 or V6 address.  Avoids doing a DNS lookup on failure
      * but it will throw an IllegalArgumentException in that case.
@@ -173,60 +156,6 @@
     }
 
     /**
-     * Add a default route through the specified gateway.
-     * @param interfaceName interface on which the route should be added
-     * @param gw the IP address of the gateway to which the route is desired,
-     * @return {@code true} on success, {@code false} on failure
-     */
-    public static boolean addDefaultRoute(String interfaceName, InetAddress gw) {
-        String dstStr;
-        String gwStr = gw.getHostAddress();
-
-        if (gw instanceof Inet4Address) {
-            dstStr = "0.0.0.0";
-        } else if (gw instanceof Inet6Address) {
-            dstStr = "::";
-        } else {
-            Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" +
-                       "(" + gwStr + ")");
-            return false;
-        }
-        return addRoute(interfaceName, dstStr, 0, gwStr) == 0;
-    }
-
-    /**
-     * Add a host route.
-     * @param interfaceName interface on which the route should be added
-     * @param dst the IP address of the host to which the route is desired,
-     * this should not be null.
-     * @param gw the IP address of the gateway to which the route is desired,
-     * if null, indicates a directly-connected route.
-     * @return {@code true} on success, {@code false} on failure
-     */
-    public static boolean addHostRoute(String interfaceName, InetAddress dst,
-          InetAddress gw) {
-        if (dst == null) {
-            Log.w(TAG, "addHostRoute: dst should not be null");
-            return false;
-        }
-
-        int prefixLength;
-        String dstStr = dst.getHostAddress();
-        String gwStr = (gw != null) ? gw.getHostAddress() : null;
-
-        if (dst instanceof Inet4Address) {
-            prefixLength = 32;
-        } else if (dst instanceof Inet6Address) {
-            prefixLength = 128;
-        } else {
-            Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" +
-                       "(" + dst + ")");
-            return false;
-        }
-        return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0;
-    }
-
-    /**
      * Get InetAddress masked with prefixLength.  Will never return null.
      * @param IP address which will be masked with specified prefixLength
      * @param prefixLength the prefixLength used to mask the IP
@@ -271,4 +200,25 @@
         return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) ||
                 ((left instanceof Inet6Address) && (right instanceof Inet6Address)));
     }
+
+    /**
+     * Convert a 32 char hex string into a Inet6Address.
+     * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be
+     * made into an Inet6Address
+     * @param addrHexString a 32 character hex string representing an IPv6 addr
+     * @return addr an InetAddress representation for the string
+     */
+    public static InetAddress hexToInet6Address(String addrHexString)
+            throws IllegalArgumentException {
+        try {
+            return numericToInetAddress(String.format("%s:%s:%s:%s:%s:%s:%s:%s",
+                    addrHexString.substring(0,4),   addrHexString.substring(4,8),
+                    addrHexString.substring(8,12),  addrHexString.substring(12,16),
+                    addrHexString.substring(16,20), addrHexString.substring(20,24),
+                    addrHexString.substring(24,28), addrHexString.substring(28,32)));
+        } catch (Exception e) {
+            Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e);
+            throw new IllegalArgumentException(e);
+        }
+    }
 }
diff --git a/core/java/android/net/RouteInfo.java b/core/java/android/net/RouteInfo.java
index 39e708a..c5288c4 100644
--- a/core/java/android/net/RouteInfo.java
+++ b/core/java/android/net/RouteInfo.java
@@ -46,18 +46,16 @@
 
     public RouteInfo(LinkAddress destination, InetAddress gateway) {
         if (destination == null) {
-            try {
-                if (gateway != null) {
-                    if (gateway instanceof Inet4Address) {
-                        destination = new LinkAddress(Inet4Address.ANY, 0);
-                    } else {
-                        destination = new LinkAddress(Inet6Address.ANY, 0);
-                    }
+            if (gateway != null) {
+                if (gateway instanceof Inet4Address) {
+                    destination = new LinkAddress(Inet4Address.ANY, 0);
                 } else {
-                    // no destination, no gateway. invalid.
-                    throw new RuntimeException("Invalid arguments passed in.");
+                    destination = new LinkAddress(Inet6Address.ANY, 0);
                 }
-            } catch (Exception e) {}
+            } else {
+                // no destination, no gateway. invalid.
+                throw new RuntimeException("Invalid arguments passed in.");
+            }
         }
         if (gateway == null) {
             if (destination.getAddress() instanceof Inet4Address) {
@@ -76,6 +74,20 @@
         this(null, gateway);
     }
 
+    public static RouteInfo makeHostRoute(InetAddress host) {
+        return makeHostRoute(host, null);
+    }
+
+    public static RouteInfo makeHostRoute(InetAddress host, InetAddress gateway) {
+        if (host == null) return null;
+
+        if (host instanceof Inet4Address) {
+            return new RouteInfo(new LinkAddress(host, 32), gateway);
+        } else {
+            return new RouteInfo(new LinkAddress(host, 128), gateway);
+        }
+    }
+
     private boolean isDefault() {
         boolean val = false;
         if (mGateway != null) {
diff --git a/core/java/android/os/INetworkManagementService.aidl b/core/java/android/os/INetworkManagementService.aidl
index fe36786..ecc111b 100644
--- a/core/java/android/os/INetworkManagementService.aidl
+++ b/core/java/android/os/INetworkManagementService.aidl
@@ -20,6 +20,7 @@
 import android.net.InterfaceConfiguration;
 import android.net.INetworkManagementEventObserver;
 import android.net.NetworkStats;
+import android.net.RouteInfo;
 import android.net.wifi.WifiConfiguration;
 
 /**
@@ -58,6 +59,22 @@
     void setInterfaceConfig(String iface, in InterfaceConfiguration cfg);
 
     /**
+     * Retrieves the network routes currently configured on the specified
+     * interface
+     */
+    RouteInfo[] getRoutes(String iface);
+
+    /**
+     * Add the specified route to the interface.
+     */
+    void addRoute(String iface, in RouteInfo route);
+
+    /**
+     * Remove the specified route from the interface.
+     */
+    void removeRoute(String iface, in RouteInfo route);
+
+    /**
      * Shuts down the service
      */
     void shutdown();
diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp
index 548376d..ddae505 100644
--- a/core/jni/android_net_NetUtils.cpp
+++ b/core/jni/android_net_NetUtils.cpp
@@ -26,10 +26,6 @@
 extern "C" {
 int ifc_enable(const char *ifname);
 int ifc_disable(const char *ifname);
-int ifc_add_route(const char *ifname, const char *destStr, uint32_t prefixLen, const char *gwStr);
-int ifc_remove_host_routes(const char *ifname);
-int ifc_get_default_route(const char *ifname);
-int ifc_remove_default_route(const char *ifname);
 int ifc_reset_connections(const char *ifname);
 
 int dhcp_do_request(const char *ifname,
@@ -94,56 +90,6 @@
     return (jint)result;
 }
 
-static jint android_net_utils_addRoute(JNIEnv* env, jobject clazz, jstring ifname,
-          jstring dst, jint prefixLength, jstring gw)
-{
-    int result;
-
-    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
-    const char *dstStr = env->GetStringUTFChars(dst, NULL);
-    const char *gwStr = NULL;
-    if (gw != NULL) {
-        gwStr = env->GetStringUTFChars(gw, NULL);
-    }
-    result = ::ifc_add_route(nameStr, dstStr, prefixLength, gwStr);
-    env->ReleaseStringUTFChars(ifname, nameStr);
-    env->ReleaseStringUTFChars(dst, dstStr);
-    if (gw != NULL) {
-        env->ReleaseStringUTFChars(gw, gwStr);
-    }
-    return (jint)result;
-}
-
-static jint android_net_utils_removeHostRoutes(JNIEnv* env, jobject clazz, jstring ifname)
-{
-    int result;
-
-    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
-    result = ::ifc_remove_host_routes(nameStr);
-    env->ReleaseStringUTFChars(ifname, nameStr);
-    return (jint)result;
-}
-
-static jint android_net_utils_getDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
-{
-    int result;
-
-    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
-    result = ::ifc_get_default_route(nameStr);
-    env->ReleaseStringUTFChars(ifname, nameStr);
-    return (jint)result;
-}
-
-static jint android_net_utils_removeDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
-{
-    int result;
-
-    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
-    result = ::ifc_remove_default_route(nameStr);
-    env->ReleaseStringUTFChars(ifname, nameStr);
-    return (jint)result;
-}
-
 static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz, jstring ifname)
 {
     int result;
@@ -260,12 +206,6 @@
 
     { "enableInterface", "(Ljava/lang/String;)I",  (void *)android_net_utils_enableInterface },
     { "disableInterface", "(Ljava/lang/String;)I",  (void *)android_net_utils_disableInterface },
-    { "addRoute", "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)I",
-       (void *)android_net_utils_addRoute },
-    { "removeHostRoutes", "(Ljava/lang/String;)I",  (void *)android_net_utils_removeHostRoutes },
-    { "getDefaultRouteNative", "(Ljava/lang/String;)I",
-       (void *)android_net_utils_getDefaultRoute },
-    { "removeDefaultRoute", "(Ljava/lang/String;)I",  (void *)android_net_utils_removeDefaultRoute },
     { "resetConnections", "(Ljava/lang/String;)I",  (void *)android_net_utils_resetConnections },
     { "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z",  (void *)android_net_utils_runDhcp },
     { "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z",  (void *)android_net_utils_runDhcpRenew },
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index 090909e..ca2adb4 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -26,6 +26,7 @@
 import android.net.DummyDataStateTracker;
 import android.net.EthernetDataTracker;
 import android.net.IConnectivityManager;
+import android.net.LinkAddress;
 import android.net.LinkProperties;
 import android.net.MobileDataStateTracker;
 import android.net.NetworkConfig;
@@ -61,6 +62,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.InetAddress;
+import java.net.Inet4Address;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -125,6 +127,8 @@
 
     private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
 
+    private INetworkManagementService mNetd;
+
     private static final int ENABLED  = 1;
     private static final int DISABLED = 0;
 
@@ -933,10 +937,6 @@
      * @return {@code true} on success, {@code false} on failure
      */
     private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
-        if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
-            return false;
-        }
-
         LinkProperties lp = nt.getLinkProperties();
         if ((lp == null) || (hostAddress == null)) return false;
 
@@ -951,20 +951,28 @@
         }
 
         RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
-        InetAddress gateway = null;
+        InetAddress gatewayAddress = null;
         if (bestRoute != null) {
-            gateway = bestRoute.getGateway();
+            gatewayAddress = bestRoute.getGateway();
             // if the best route is ourself, don't relf-reference, just add the host route
-            if (hostAddress.equals(gateway)) gateway = null;
+            if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
         }
-        if (gateway != null) {
+        if (gatewayAddress != null) {
             if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
                 loge("Error adding hostroute - too much recursion");
                 return false;
             }
-            if (!addHostRoute(nt, gateway, cycleCount+1)) return false;
+            if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
         }
-        return NetworkUtils.addHostRoute(interfaceName, hostAddress, gateway);
+
+        RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
+
+        try {
+            mNetd.addRoute(interfaceName, route);
+            return true;
+        } catch (Exception ex) {
+            return false;
+        }
     }
 
     // TODO support the removal of single host routes.  Keep a ref count of them so we
@@ -1291,6 +1299,9 @@
     }
 
     void systemReady() {
+        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
+        mNetd = INetworkManagementService.Stub.asInterface(b);
+
         synchronized(this) {
             mSystemReady = true;
             if (mInitialBroadcast != null) {
@@ -1427,7 +1438,6 @@
         if (interfaceName != null && !privateDnsRouteSet) {
             Collection<InetAddress> dnsList = p.getDnses();
             for (InetAddress dns : dnsList) {
-                if (DBG) log("  adding " + dns);
                 addHostRoute(nt, dns, 0);
             }
             nt.privateDnsRouteSet(true);
@@ -1435,8 +1445,6 @@
     }
 
     private void removePrivateDnsRoutes(NetworkStateTracker nt) {
-        // TODO - we should do this explicitly but the NetUtils api doesnt
-        // support this yet - must remove all.  No worse than before
         LinkProperties p = nt.getLinkProperties();
         if (p == null) return;
         String interfaceName = p.getInterfaceName();
@@ -1446,7 +1454,17 @@
                 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
                         " (" + interfaceName + ")");
             }
-            NetworkUtils.removeHostRoutes(interfaceName);
+
+            Collection<InetAddress> dnsList = p.getDnses();
+            for (InetAddress dns : dnsList) {
+                if (DBG) log("  removing " + dns);
+                RouteInfo route = RouteInfo.makeHostRoute(dns);
+                try {
+                    mNetd.removeRoute(interfaceName, route);
+                } catch (Exception ex) {
+                    loge("error (" + ex + ") removing dns route " + route);
+                }
+            }
             nt.privateDnsRouteSet(false);
         }
     }
@@ -1457,19 +1475,27 @@
         if (p == null) return;
         String interfaceName = p.getInterfaceName();
         if (TextUtils.isEmpty(interfaceName)) return;
-        for (RouteInfo route : p.getRoutes()) {
 
+        for (RouteInfo route : p.getRoutes()) {
             //TODO - handle non-default routes
             if (route.isDefaultRoute()) {
+                if (DBG) log("adding default route " + route);
                 InetAddress gateway = route.getGateway();
-                if (addHostRoute(nt, gateway, 0) &&
-                        NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
+                if (addHostRoute(nt, gateway, 0)) {
+                    try {
+                        mNetd.addRoute(interfaceName, route);
+                    } catch (Exception e) {
+                        loge("error adding default route " + route);
+                        continue;
+                    }
                     if (DBG) {
                         NetworkInfo networkInfo = nt.getNetworkInfo();
                         log("addDefaultRoute for " + networkInfo.getTypeName() +
                                 " (" + interfaceName + "), GatewayAddr=" +
                                 gateway.getHostAddress());
                     }
+                } else {
+                    loge("error adding host route for default route " + route);
                 }
             }
         }
@@ -1481,8 +1507,17 @@
         if (p == null) return;
         String interfaceName = p.getInterfaceName();
 
-        if (interfaceName != null) {
-            if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
+        if (interfaceName == null) return;
+
+        for (RouteInfo route : p.getRoutes()) {
+            //TODO - handle non-default routes
+            if (route.isDefaultRoute()) {
+                try {
+                    mNetd.removeRoute(interfaceName, route);
+                } catch (Exception ex) {
+                    loge("error (" + ex + ") removing default route " + route);
+                    continue;
+                }
                 if (DBG) {
                     NetworkInfo networkInfo = nt.getNetworkInfo();
                     log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
diff --git a/services/java/com/android/server/NetworkManagementService.java b/services/java/com/android/server/NetworkManagementService.java
index d931350..7c613c1 100644
--- a/services/java/com/android/server/NetworkManagementService.java
+++ b/services/java/com/android/server/NetworkManagementService.java
@@ -29,6 +29,7 @@
 import android.net.INetworkManagementEventObserver;
 import android.net.LinkAddress;
 import android.net.NetworkUtils;
+import android.net.RouteInfo;
 import android.net.wifi.WifiConfiguration;
 import android.net.wifi.WifiConfiguration.KeyMgmt;
 import android.os.INetworkManagementService;
@@ -46,6 +47,8 @@
 import android.content.ContentResolver;
 import android.database.ContentObserver;
 
+import java.io.BufferedReader;
+import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
@@ -55,8 +58,8 @@
 import java.io.RandomAccessFile;
 import java.io.Reader;
 import java.lang.IllegalStateException;
-
 import java.net.InetAddress;
+import java.net.Inet4Address;
 import java.net.UnknownHostException;
 import java.util.concurrent.CountDownLatch;
 
@@ -71,6 +74,9 @@
     private static final boolean DBG = false;
     private static final String NETD_TAG = "NetdConnector";
 
+    private static final int ADD = 1;
+    private static final int REMOVE = 2;
+
     class NetdResponseCode {
         public static final int InterfaceListResult       = 110;
         public static final int TetherInterfaceListResult = 111;
@@ -320,6 +326,164 @@
         }
     }
 
+    public void addRoute(String interfaceName, RouteInfo route) {
+        modifyRoute(interfaceName, ADD, route);
+    }
+
+    public void removeRoute(String interfaceName, RouteInfo route) {
+        modifyRoute(interfaceName, REMOVE, route);
+    }
+
+    private void modifyRoute(String interfaceName, int action, RouteInfo route) {
+        ArrayList<String> rsp;
+
+        StringBuilder cmd;
+
+        switch (action) {
+            case ADD:
+            {
+                cmd = new StringBuilder("interface route add " + interfaceName);
+                break;
+            }
+            case REMOVE:
+            {
+                cmd = new StringBuilder("interface route remove " + interfaceName);
+                break;
+            }
+            default:
+                throw new IllegalStateException("Unknown action type " + action);
+        }
+
+        // create triplet: dest-ip-addr prefixlength gateway-ip-addr
+        LinkAddress la = route.getDestination();
+        cmd.append(' ');
+        cmd.append(la.getAddress().getHostAddress());
+        cmd.append(' ');
+        cmd.append(la.getNetworkPrefixLength());
+        cmd.append(' ');
+        if (route.getGateway() == null) {
+            if (la.getAddress() instanceof Inet4Address) {
+                cmd.append("0.0.0.0");
+            } else {
+                cmd.append ("::0");
+            }
+        } else {
+            cmd.append(route.getGateway().getHostAddress());
+        }
+        try {
+            rsp = mConnector.doCommand(cmd.toString());
+        } catch (NativeDaemonConnectorException e) {
+            throw new IllegalStateException(
+                    "Unable to communicate with native dameon to add routes - "
+                    + e);
+        }
+
+        for (String line : rsp) {
+            Log.v(TAG, "add route response is " + line);
+        }
+    }
+
+    private ArrayList<String> readRouteList(String filename) {
+        FileInputStream fstream = null;
+        ArrayList<String> list = new ArrayList<String>();
+
+        try {
+            fstream = new FileInputStream(filename);
+            DataInputStream in = new DataInputStream(fstream);
+            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+            String s;
+
+            // throw away the title line
+
+            while (((s = br.readLine()) != null) && (s.length() != 0)) {
+                list.add(s);
+            }
+        } catch (IOException ex) {
+            // return current list, possibly empty
+        } finally {
+            if (fstream != null) {
+                try {
+                    fstream.close();
+                } catch (IOException ex) {}
+            }
+        }
+
+        return list;
+    }
+
+    public RouteInfo[] getRoutes(String interfaceName) {
+        ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>();
+
+        // v4 routes listed as:
+        // iface dest-addr gateway-addr flags refcnt use metric netmask mtu window IRTT
+        for (String s : readRouteList("/proc/net/route")) {
+            String[] fields = s.split("\t");
+
+            if (fields.length > 7) {
+                String iface = fields[0];
+
+                if (interfaceName.equals(iface)) {
+                    String dest = fields[1];
+                    String gate = fields[2];
+                    String flags = fields[3]; // future use?
+                    String mask = fields[7];
+                    try {
+                        // address stored as a hex string, ex: 0014A8C0
+                        InetAddress destAddr =
+                                NetworkUtils.intToInetAddress((int)Long.parseLong(dest, 16));
+                        int prefixLength =
+                                NetworkUtils.netmaskIntToPrefixLength(
+                                (int)Long.parseLong(mask, 16));
+                        LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
+
+                        // address stored as a hex string, ex 0014A8C0
+                        InetAddress gatewayAddr =
+                                NetworkUtils.intToInetAddress((int)Long.parseLong(gate, 16));
+
+                        RouteInfo route = new RouteInfo(linkAddress, gatewayAddr);
+                        routes.add(route);
+                    } catch (Exception e) {
+                        Log.e(TAG, "Error parsing route " + s + " : " + e);
+                        continue;
+                    }
+                }
+            }
+        }
+
+        // v6 routes listed as:
+        // dest-addr prefixlength ?? ?? gateway-addr ?? ?? ?? ?? iface
+        for (String s : readRouteList("/proc/net/ipv6_route")) {
+            String[]fields = s.split("\\s+");
+            if (fields.length > 9) {
+                String iface = fields[9].trim();
+                if (interfaceName.equals(iface)) {
+                    String dest = fields[0];
+                    String prefix = fields[1];
+                    String gate = fields[4];
+
+                    try {
+                        // prefix length stored as a hex string, ex 40
+                        int prefixLength = Integer.parseInt(prefix, 16);
+
+                        // address stored as a 32 char hex string
+                        // ex fe800000000000000000000000000000
+                        InetAddress destAddr = NetworkUtils.hexToInet6Address(dest);
+                        LinkAddress linkAddress = new LinkAddress(destAddr, prefixLength);
+
+                        InetAddress gateAddr = NetworkUtils.hexToInet6Address(gate);
+
+                        RouteInfo route = new RouteInfo(linkAddress, gateAddr);
+                        routes.add(route);
+                    } catch (Exception e) {
+                        Log.e(TAG, "Error parsing route " + s + " : " + e);
+                        continue;
+                    }
+                }
+            }
+        }
+        return (RouteInfo[]) routes.toArray(new RouteInfo[0]);
+    }
+
     public void shutdown() {
         if (mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.SHUTDOWN)