Implement GetNetworkList on Mac/iOS to expose IPv6 address attributes.

Enhance attributes mapping to expose IPv6 temporary and deprecated address attributes on Mac. For iOS, it should only be a non-functional refactor change.

Also refactor the code to facilitate testing and add additional testing.

BUG=413437

Review URL: https://codereview.chromium.org/715513005

Cr-Commit-Position: refs/heads/master@{#304231}
diff --git a/net/BUILD.gn b/net/BUILD.gn
index 1815ea59..53c1ed9 100644
--- a/net/BUILD.gn
+++ b/net/BUILD.gn
@@ -473,6 +473,8 @@
     # doesn't set USE_NSS but needs some of the files.
     set_sources_assignment_filter([])
     sources += [
+      "base/net_util_mac.cc",
+      "base/net_util_mac.h",
       "base/network_change_notifier_mac.cc",
       "base/network_config_watcher_mac.cc",
       "base/platform_mime_util_mac.mm",
@@ -506,6 +508,8 @@
       "base/platform_mime_util_linux.cc",
       "base/address_tracker_linux.cc",
       "base/address_tracker_linux.h",
+      "base/net_util_linux.cc",
+      "base/net_util_linux.h"
     ]
     set_sources_assignment_filter(sources_assignment_filter)
 
diff --git a/net/base/net_util_linux.cc b/net/base/net_util_linux.cc
new file mode 100644
index 0000000..e4e0f7f
--- /dev/null
+++ b/net/base/net_util_linux.cc
@@ -0,0 +1,154 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/net_util_linux.h"
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <set>
+#include <sys/types.h>
+
+#include "base/files/file_path.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_tokenizer.h"
+#include "base/strings/string_util.h"
+#include "base/threading/thread_restrictions.h"
+#include "net/base/address_tracker_linux.h"
+#include "net/base/escape.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_util_posix.h"
+#include "url/gurl.h"
+
+namespace net {
+
+namespace {
+
+// When returning true, the platform native IPv6 address attributes were
+// successfully converted to net IP address attributes. Otherwise, returning
+// false and the caller should drop the IP address which can't be used by the
+// application layer.
+bool TryConvertNativeToNetIPAttributes(int native_attributes,
+                                       int* net_attributes) {
+  // For Linux/ChromeOS/Android, we disallow addresses with attributes
+  // IFA_F_OPTIMISTIC, IFA_F_DADFAILED, and IFA_F_TENTATIVE as these
+  // are still progressing through duplicated address detection (DAD)
+  // and shouldn't be used by the application layer until DAD process
+  // is completed.
+  if (native_attributes & (
+#if !defined(OS_ANDROID)
+                              IFA_F_OPTIMISTIC | IFA_F_DADFAILED |
+#endif  // !OS_ANDROID
+                              IFA_F_TENTATIVE)) {
+    return false;
+  }
+
+  if (native_attributes & IFA_F_TEMPORARY) {
+    *net_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY;
+  }
+
+  if (native_attributes & IFA_F_DEPRECATED) {
+    *net_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED;
+  }
+
+  return true;
+}
+
+}  // namespace
+
+namespace internal {
+
+inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) {
+#if defined(OS_ANDROID)
+  return ip.begin();
+#else
+  return ip.data();
+#endif
+}
+
+bool GetNetworkListImpl(
+    NetworkInterfaceList* networks,
+    int policy,
+    const base::hash_set<int>& online_links,
+    const internal::AddressTrackerLinux::AddressMap& address_map,
+    GetInterfaceNameFunction get_interface_name) {
+  std::map<int, std::string> ifnames;
+
+  for (internal::AddressTrackerLinux::AddressMap::const_iterator it =
+           address_map.begin();
+       it != address_map.end(); ++it) {
+    // Ignore addresses whose links are not online.
+    if (online_links.find(it->second.ifa_index) == online_links.end())
+      continue;
+
+    sockaddr_storage sock_addr;
+    socklen_t sock_len = sizeof(sockaddr_storage);
+
+    // Convert to sockaddr for next check.
+    if (!IPEndPoint(it->first, 0)
+             .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addr), &sock_len)) {
+      continue;
+    }
+
+    // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
+    if (IsLoopbackOrUnspecifiedAddress(reinterpret_cast<sockaddr*>(&sock_addr)))
+      continue;
+
+    int ip_attributes = IP_ADDRESS_ATTRIBUTE_NONE;
+
+    if (it->second.ifa_family == AF_INET6) {
+      // Ignore addresses whose attributes are not actionable by
+      // the application layer.
+      if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags,
+                                             &ip_attributes))
+        continue;
+    }
+
+    // Find the name of this link.
+    std::map<int, std::string>::const_iterator itname =
+        ifnames.find(it->second.ifa_index);
+    std::string ifname;
+    if (itname == ifnames.end()) {
+      char buffer[IF_NAMESIZE] = {0};
+      if (get_interface_name(it->second.ifa_index, buffer)) {
+        ifname = ifnames[it->second.ifa_index] = buffer;
+      } else {
+        // Ignore addresses whose interface name can't be retrieved.
+        continue;
+      }
+    } else {
+      ifname = itname->second;
+    }
+
+    // Based on the interface name and policy, determine whether we
+    // should ignore it.
+    if (ShouldIgnoreInterface(ifname, policy))
+      continue;
+
+    networks->push_back(
+        NetworkInterface(ifname, ifname, it->second.ifa_index,
+                         NetworkChangeNotifier::CONNECTION_UNKNOWN, it->first,
+                         it->second.ifa_prefixlen, ip_attributes));
+  }
+
+  return true;
+}
+
+}  // namespace internal
+
+bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
+  if (networks == NULL)
+    return false;
+
+  internal::AddressTrackerLinux tracker;
+  tracker.Init();
+
+  return internal::GetNetworkListImpl(networks, policy,
+                                      tracker.GetOnlineLinks(),
+                                      tracker.GetAddressMap(), &if_indextoname);
+}
+
+}  // namespace net
diff --git a/net/base/net_util_linux.h b/net/base/net_util_linux.h
new file mode 100644
index 0000000..340c3802
--- /dev/null
+++ b/net/base/net_util_linux.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_NET_UTIL_LINUX_H_
+#define NET_BASE_NET_UTIL_LINUX_H_
+
+// This file is only used to expose some of the internals
+// of net_util_linux.cc to tests.
+
+#include "base/containers/hash_tables.h"
+#include "net/base/address_tracker_linux.h"
+#include "net/base/net_util.h"
+
+namespace net {
+namespace internal {
+
+typedef char* (*GetInterfaceNameFunction)(unsigned int interface_index,
+                                          char* ifname);
+
+NET_EXPORT bool GetNetworkListImpl(
+    NetworkInterfaceList* networks,
+    int policy,
+    const base::hash_set<int>& online_links,
+    const internal::AddressTrackerLinux::AddressMap& address_map,
+    GetInterfaceNameFunction get_interface_name);
+
+}  // namespace internal
+}  // namespace net
+
+#endif  // NET_BASE_NET_UTIL_LINUX_H_
diff --git a/net/base/net_util_mac.cc b/net/base/net_util_mac.cc
new file mode 100644
index 0000000..5dd4630
--- /dev/null
+++ b/net/base/net_util_mac.cc
@@ -0,0 +1,246 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/net_util_mac.h"
+
+#include <ifaddrs.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <set>
+#include <sys/types.h>
+
+#include "base/files/file_path.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_tokenizer.h"
+#include "base/strings/string_util.h"
+#include "base/threading/thread_restrictions.h"
+#include "net/base/escape.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_util_posix.h"
+#include "url/gurl.h"
+
+#if !defined(OS_IOS)
+#include <net/if_media.h>
+#include <netinet/in_var.h>
+#include <sys/ioctl.h>
+#endif  // !OS_IOS
+
+namespace net {
+
+namespace {
+
+#if !defined(OS_IOS)
+
+// MacOSX implementation of IPAttributesGetterMac which calls ioctl on socket to
+// retrieve IP attributes.
+class IPAttributesGetterMacImpl : public internal::IPAttributesGetterMac {
+ public:
+  IPAttributesGetterMacImpl();
+  ~IPAttributesGetterMacImpl() override;
+  bool IsInitialized() const override;
+  bool GetIPAttributes(const char* ifname,
+                       const sockaddr* sock_addr,
+                       int* native_attributes) override;
+
+ private:
+  int ioctl_socket_;
+};
+
+IPAttributesGetterMacImpl::IPAttributesGetterMacImpl()
+    : ioctl_socket_(socket(AF_INET6, SOCK_DGRAM, 0)) {
+  DCHECK_GE(ioctl_socket_, 0);
+}
+
+bool IPAttributesGetterMacImpl::IsInitialized() const {
+  return ioctl_socket_ >= 0;
+}
+
+IPAttributesGetterMacImpl::~IPAttributesGetterMacImpl() {
+  if (ioctl_socket_ >= 0) {
+    close(ioctl_socket_);
+  }
+}
+
+bool IPAttributesGetterMacImpl::GetIPAttributes(const char* ifname,
+                                                const sockaddr* sock_addr,
+                                                int* native_attributes) {
+  struct in6_ifreq ifr = {};
+  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
+  memcpy(&ifr.ifr_ifru.ifru_addr, sock_addr, sock_addr->sa_len);
+  int rv = ioctl(ioctl_socket_, SIOCGIFAFLAG_IN6, &ifr);
+  if (rv >= 0) {
+    *native_attributes = ifr.ifr_ifru.ifru_flags;
+  }
+  return (rv >= 0);
+}
+
+// When returning true, the platform native IPv6 address attributes were
+// successfully converted to net IP address attributes. Otherwise, returning
+// false and the caller should drop the IP address which can't be used by the
+// application layer.
+bool TryConvertNativeToNetIPAttributes(int native_attributes,
+                                       int* net_attributes) {
+  // For MacOSX, we disallow addresses with attributes IN6_IFF_ANYCASE,
+  // IN6_IFF_DUPLICATED, IN6_IFF_TENTATIVE, and IN6_IFF_DETACHED as these are
+  // still progressing through duplicated address detection (DAD) or are not
+  // suitable to be used in an one-to-one communication and shouldn't be used
+  // by the application layer.
+  if (native_attributes & (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED |
+                           IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) {
+    return false;
+  }
+
+  if (native_attributes & IN6_IFF_TEMPORARY) {
+    *net_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY;
+  }
+
+  if (native_attributes & IN6_IFF_DEPRECATED) {
+    *net_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED;
+  }
+
+  return true;
+}
+
+NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
+    int addr_family,
+    const std::string& interface_name) {
+  NetworkChangeNotifier::ConnectionType type =
+      NetworkChangeNotifier::CONNECTION_UNKNOWN;
+
+  struct ifmediareq ifmr = {};
+  strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1);
+
+  int s = socket(addr_family, SOCK_DGRAM, 0);
+  if (s == -1) {
+    return type;
+  }
+
+  if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) {
+    if (ifmr.ifm_current & IFM_IEEE80211) {
+      type = NetworkChangeNotifier::CONNECTION_WIFI;
+    } else if (ifmr.ifm_current & IFM_ETHER) {
+      type = NetworkChangeNotifier::CONNECTION_ETHERNET;
+    }
+  }
+  close(s);
+  return type;
+}
+
+#endif  // !OS_IOS
+}  // namespace
+
+namespace internal {
+
+bool GetNetworkListImpl(NetworkInterfaceList* networks,
+                        int policy,
+                        const ifaddrs* interfaces,
+                        IPAttributesGetterMac* ip_attributes_getter) {
+  // Enumerate the addresses assigned to network interfaces which are up.
+  for (const ifaddrs* interface = interfaces; interface != NULL;
+       interface = interface->ifa_next) {
+    // Skip loopback interfaces, and ones which are down.
+    if (!(IFF_RUNNING & interface->ifa_flags))
+      continue;
+    if (IFF_LOOPBACK & interface->ifa_flags)
+      continue;
+    // Skip interfaces with no address configured.
+    struct sockaddr* addr = interface->ifa_addr;
+    if (!addr)
+      continue;
+
+    // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
+    // configured on non-loopback interfaces.
+    if (IsLoopbackOrUnspecifiedAddress(addr))
+      continue;
+
+    const std::string& name = interface->ifa_name;
+    // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
+    if (ShouldIgnoreInterface(name, policy)) {
+      continue;
+    }
+
+    NetworkChangeNotifier::ConnectionType connection_type =
+        NetworkChangeNotifier::CONNECTION_UNKNOWN;
+
+    int ip_attributes = IP_ADDRESS_ATTRIBUTE_NONE;
+
+#if !defined(OS_IOS)
+    // Retrieve native ip attributes and convert to net version if a getter is
+    // given.
+    if (ip_attributes_getter && ip_attributes_getter->IsInitialized()) {
+      int native_attributes = 0;
+      if (addr->sa_family == AF_INET6 &&
+          ip_attributes_getter->GetIPAttributes(
+              interface->ifa_name, interface->ifa_addr, &native_attributes)) {
+        if (!TryConvertNativeToNetIPAttributes(native_attributes,
+                                               &ip_attributes)) {
+          continue;
+        }
+      }
+    }
+
+    connection_type = GetNetworkInterfaceType(addr->sa_family, name);
+#endif  // !OS_IOS
+
+    IPEndPoint address;
+
+    int addr_size = 0;
+    if (addr->sa_family == AF_INET6) {
+      addr_size = sizeof(sockaddr_in6);
+    } else if (addr->sa_family == AF_INET) {
+      addr_size = sizeof(sockaddr_in);
+    }
+
+    if (address.FromSockAddr(addr, addr_size)) {
+      uint8 prefix_length = 0;
+      if (interface->ifa_netmask) {
+        // If not otherwise set, assume the same sa_family as ifa_addr.
+        if (interface->ifa_netmask->sa_family == 0) {
+          interface->ifa_netmask->sa_family = addr->sa_family;
+        }
+        IPEndPoint netmask;
+        if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
+          prefix_length = MaskPrefixLength(netmask.address());
+        }
+      }
+      networks->push_back(NetworkInterface(
+          name, name, if_nametoindex(name.c_str()), connection_type,
+          address.address(), prefix_length, ip_attributes));
+    }
+  }
+
+  return true;
+}
+
+}  // namespace internal
+
+bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
+  if (networks == NULL)
+    return false;
+
+  // getifaddrs() may require IO operations.
+  base::ThreadRestrictions::AssertIOAllowed();
+
+  ifaddrs* interfaces;
+  if (getifaddrs(&interfaces) < 0) {
+    PLOG(ERROR) << "getifaddrs";
+    return false;
+  }
+
+  scoped_ptr<internal::IPAttributesGetterMac> ip_attributes_getter;
+
+#if !defined(OS_IOS)
+  ip_attributes_getter.reset(new IPAttributesGetterMacImpl());
+#endif
+
+  bool result = internal::GetNetworkListImpl(networks, policy, interfaces,
+                                             ip_attributes_getter.get());
+  freeifaddrs(interfaces);
+  return result;
+}
+
+}  // namespace net
diff --git a/net/base/net_util_mac.h b/net/base/net_util_mac.h
new file mode 100644
index 0000000..178facc
--- /dev/null
+++ b/net/base/net_util_mac.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_NET_UTIL_MAC_H_
+#define NET_BASE_NET_UTIL_MAC_H_
+
+// This file is only used to expose some of the internals
+// of net_util_mac.cc to tests.
+
+#include "base/macros.h"
+#include "net/base/net_export.h"
+#include "net/base/net_util.h"
+
+struct ifaddrs;
+struct sockaddr;
+
+namespace net {
+namespace internal {
+
+class NET_EXPORT IPAttributesGetterMac {
+ public:
+  IPAttributesGetterMac() {}
+  virtual ~IPAttributesGetterMac() {}
+  virtual bool IsInitialized() const = 0;
+  virtual bool GetIPAttributes(const char* ifname,
+                               const sockaddr* sock_addr,
+                               int* native_attributes) = 0;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(IPAttributesGetterMac);
+};
+
+NET_EXPORT bool GetNetworkListImpl(NetworkInterfaceList* networks,
+                                   int policy,
+                                   const ifaddrs* interfaces,
+                                   IPAttributesGetterMac* ip_attributes_getter);
+
+}  // namespace internal
+}  // namespace net
+
+#endif  // NET_BASE_NET_UTIL_MAC_H_
diff --git a/net/base/net_util_posix.cc b/net/base/net_util_posix.cc
index a2fa6cdf..b8e7195 100644
--- a/net/base/net_util_posix.cc
+++ b/net/base/net_util_posix.cc
@@ -7,38 +7,18 @@
 #include <set>
 #include <sys/types.h>
 
-#include "base/files/file_path.h"
-#include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/string_tokenizer.h"
-#include "base/strings/string_util.h"
-#include "base/threading/thread_restrictions.h"
-#include "net/base/escape.h"
-#include "net/base/ip_endpoint.h"
-#include "net/base/net_errors.h"
-#include "url/gurl.h"
 
 #if !defined(OS_NACL)
-#if defined(OS_MACOSX)
-#include <ifaddrs.h>
-#else
-#include "net/base/address_tracker_linux.h"
 #include "net/base/net_util_posix.h"
-#endif  // OS_MACOSX
 #include <net/if.h>
 #include <netinet/in.h>
 #endif  // !defined(OS_NACL)
 
-#if defined(OS_MACOSX) && !defined(OS_IOS)
-#include <net/if_media.h>
-#include <netinet/in_var.h>
-#include <sys/ioctl.h>
-#endif
-
 namespace net {
 
-namespace {
+#if !defined(OS_NACL)
+namespace internal {
 
 // The application layer can pass |policy| defined in net_util.h to
 // request filtering out certain type of interfaces.
@@ -77,330 +57,13 @@
   return false;
 }
 
-#if defined(OS_MACOSX)
-
-struct NetworkInterfaceInfo {
-  NetworkInterfaceInfo() : permanent(true) { }
-
-  bool permanent;  // IPv6 has notion of temporary address. If the address is
-                   // IPv6 and it's temporary this field will be false.
-  NetworkInterface interface;
-};
-
-// This method will remove permanent IPv6 addresses if a temporary address
-// is available for same network interface.
-void RemovePermanentIPv6AddressesWhereTemporaryExists(
-    std::vector<NetworkInterfaceInfo>* infos) {
-  if (!infos || infos->empty())
-    return;
-
-  // Build a set containing the names of interfaces with a temp IPv6 address
-  std::set<std::string> ifaces_with_temp_addrs;
-  std::vector<NetworkInterfaceInfo>::iterator i;
-  for (i = infos->begin(); i != infos->end(); ++i) {
-    if (!i->permanent && i->interface.address.size() == kIPv6AddressSize) {
-      ifaces_with_temp_addrs.insert(i->interface.name);
-    }
-  }
-
-  // If there are no such interfaces then there's no further work.
-  if (ifaces_with_temp_addrs.empty())
-    return;
-
-  // Search for permenent addresses belonging to same network interface.
-  for (i = infos->begin(); i != infos->end(); ) {
-    // If the address is IPv6 and it's permanent and there is temporary
-    // address for it, then we can remove this address.
-    if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent &&
-        (ifaces_with_temp_addrs.find(i->interface.name) !=
-            ifaces_with_temp_addrs.end())) {
-      i = infos->erase(i);
-    } else {
-      ++i;
-    }
-  }
-}
-
-#if !defined(OS_IOS)
-NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
-    int addr_family, const std::string& interface_name) {
-  NetworkChangeNotifier::ConnectionType type =
-      NetworkChangeNotifier::CONNECTION_UNKNOWN;
-
-  struct ifmediareq ifmr = {};
-  strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1);
-
-  int s = socket(addr_family, SOCK_DGRAM, 0);
-  if (s == -1) {
-    return type;
-  }
-
-  if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) {
-    if (ifmr.ifm_current & IFM_IEEE80211) {
-      type = NetworkChangeNotifier::CONNECTION_WIFI;
-    } else if (ifmr.ifm_current & IFM_ETHER) {
-      type = NetworkChangeNotifier::CONNECTION_ETHERNET;
-    }
-  }
-  close(s);
-  return type;
-}
-
-#endif                   // !defined(OS_IOS)
-#elif !defined(OS_NACL)  // OS_MACOSX
-
-// Convert platform native IPv6 address attributes to net IP address
-// attributes and drop ones that can't be used by the application
-// layer.
-bool TryConvertNativeToNetIPAttributes(int native_attributes,
-                                       int* net_attributes) {
-  // For Linux/ChromeOS/Android, we disallow addresses with attributes
-  // IFA_F_OPTIMISTIC, IFA_F_DADFAILED, and IFA_F_TENTATIVE as these
-  // are still progressing through duplicated address detection (DAD)
-  // and shouldn't be used by the application layer until DAD process
-  // is completed.
-  if (native_attributes & (
-#if !defined(OS_ANDROID)
-    IFA_F_OPTIMISTIC | IFA_F_DADFAILED |
-#endif  // !OS_ANDROID
-    IFA_F_TENTATIVE)) {
-    return false;
-  }
-
-  if (native_attributes & IFA_F_TEMPORARY) {
-    *net_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY;
-  }
-
-  if (native_attributes & IFA_F_DEPRECATED) {
-    *net_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED;
-  }
-
-  return true;
-}
-#endif  // OS_MACOSX
-}  // namespace
-
-namespace internal {
-
-#if !defined(OS_MACOSX) && !defined(OS_NACL)
-
-inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) {
-#if defined(OS_ANDROID)
-  return ip.begin();
-#else
-  return ip.data();
-#endif
-}
-
-bool GetNetworkListImpl(
-    NetworkInterfaceList* networks,
-    int policy,
-    const base::hash_set<int>& online_links,
-    const internal::AddressTrackerLinux::AddressMap& address_map,
-    GetInterfaceNameFunction get_interface_name) {
-  std::map<int, std::string> ifnames;
-
-  for (internal::AddressTrackerLinux::AddressMap::const_iterator it =
-           address_map.begin();
-       it != address_map.end();
-       ++it) {
-    // Ignore addresses whose links are not online.
-    if (online_links.find(it->second.ifa_index) == online_links.end())
-      continue;
-
-    sockaddr_storage sock_addr;
-    socklen_t sock_len = sizeof(sockaddr_storage);
-
-    // Convert to sockaddr for next check.
-    if (!IPEndPoint(it->first, 0)
-             .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addr), &sock_len)) {
-      continue;
-    }
-
-    // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
-    if (IsLoopbackOrUnspecifiedAddress(reinterpret_cast<sockaddr*>(&sock_addr)))
-      continue;
-
-    int ip_attributes = IP_ADDRESS_ATTRIBUTE_NONE;
-
-    if (it->second.ifa_family == AF_INET6) {
-      // Ignore addresses whose attributes are not actionable by
-      // the application layer.
-      if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags,
-                                             &ip_attributes))
-        continue;
-    }
-
-    // Find the name of this link.
-    std::map<int, std::string>::const_iterator itname =
-        ifnames.find(it->second.ifa_index);
-    std::string ifname;
-    if (itname == ifnames.end()) {
-      char buffer[IF_NAMESIZE] = {0};
-      if (get_interface_name(it->second.ifa_index, buffer)) {
-        ifname = ifnames[it->second.ifa_index] = buffer;
-      } else {
-        // Ignore addresses whose interface name can't be retrieved.
-        continue;
-      }
-    } else {
-      ifname = itname->second;
-    }
-
-    // Based on the interface name and policy, determine whether we
-    // should ignore it.
-    if (ShouldIgnoreInterface(ifname, policy))
-      continue;
-
-    networks->push_back(
-        NetworkInterface(ifname,
-                         ifname,
-                         it->second.ifa_index,
-                         NetworkChangeNotifier::CONNECTION_UNKNOWN,
-                         it->first,
-                         it->second.ifa_prefixlen,
-                         ip_attributes));
-  }
-
-  return true;
-}
-#endif
-
 }  // namespace internal
-
+#else   // OS_NACL
 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
-  if (networks == NULL)
-    return false;
-#if defined(OS_NACL)
   NOTIMPLEMENTED();
   return false;
-#elif !defined(OS_MACOSX)
-
-  internal::AddressTrackerLinux tracker;
-  tracker.Init();
-
-  return internal::GetNetworkListImpl(networks,
-                                      policy,
-                                      tracker.GetOnlineLinks(),
-                                      tracker.GetAddressMap(),
-                                      &if_indextoname);
-
-#else  // Only OS_MACOSX and OS_IOS will run the code below
-
-  // getifaddrs() may require IO operations.
-  base::ThreadRestrictions::AssertIOAllowed();
-
-#if !defined(OS_IOS)
-  int ioctl_socket = -1;
-  if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
-    // we need a socket to query information about temporary address.
-    ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0);
-    DCHECK_GT(ioctl_socket, 0);
-  }
-#endif
-
-  ifaddrs* interfaces;
-  if (getifaddrs(&interfaces) < 0) {
-    PLOG(ERROR) << "getifaddrs";
-    return false;
-  }
-
-  std::vector<NetworkInterfaceInfo> network_infos;
-
-  // Enumerate the addresses assigned to network interfaces which are up.
-  for (ifaddrs *interface = interfaces;
-       interface != NULL;
-       interface = interface->ifa_next) {
-    // Skip loopback interfaces, and ones which are down.
-    if (!(IFF_UP & interface->ifa_flags))
-      continue;
-    if (IFF_LOOPBACK & interface->ifa_flags)
-      continue;
-    // Skip interfaces with no address configured.
-    struct sockaddr* addr = interface->ifa_addr;
-    if (!addr)
-      continue;
-
-    // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
-    // configured on non-loopback interfaces.
-    if (IsLoopbackOrUnspecifiedAddress(addr))
-      continue;
-
-    int addr_size = 0;
-    if (addr->sa_family == AF_INET6) {
-      addr_size = sizeof(sockaddr_in6);
-    } else if (addr->sa_family == AF_INET) {
-      addr_size = sizeof(sockaddr_in);
-    }
-
-    const std::string& name = interface->ifa_name;
-    // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
-    if (ShouldIgnoreInterface(name, policy)) {
-      continue;
-    }
-
-    NetworkInterfaceInfo network_info;
-    NetworkChangeNotifier::ConnectionType connection_type =
-        NetworkChangeNotifier::CONNECTION_UNKNOWN;
-#if !defined(OS_IOS)
-    // Check if this is a temporary address. Currently this is only supported
-    // on Mac.
-    if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) &&
-        ioctl_socket >= 0 && addr->sa_family == AF_INET6) {
-      struct in6_ifreq ifr = {};
-      strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1);
-      memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr,
-             interface->ifa_addr->sa_len);
-      int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr);
-      if (rv >= 0) {
-        network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY);
-      }
-    }
-
-    connection_type = GetNetworkInterfaceType(addr->sa_family, name);
-#endif
-
-    IPEndPoint address;
-    if (address.FromSockAddr(addr, addr_size)) {
-      uint8 net_mask = 0;
-      if (interface->ifa_netmask) {
-        // If not otherwise set, assume the same sa_family as ifa_addr.
-        if (interface->ifa_netmask->sa_family == 0) {
-          interface->ifa_netmask->sa_family = addr->sa_family;
-        }
-        IPEndPoint netmask;
-        if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
-          net_mask = MaskPrefixLength(netmask.address());
-        }
-      }
-      network_info.interface = NetworkInterface(name,
-                                                name,
-                                                if_nametoindex(name.c_str()),
-                                                connection_type,
-                                                address.address(),
-                                                net_mask,
-                                                IP_ADDRESS_ATTRIBUTE_NONE);
-
-      network_infos.push_back(NetworkInterfaceInfo(network_info));
-    }
-  }
-  freeifaddrs(interfaces);
-#if !defined(OS_IOS)
-  if (ioctl_socket >= 0) {
-    close(ioctl_socket);
-  }
-#endif
-
-  if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
-    RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos);
-  }
-
-  for (size_t i = 0; i < network_infos.size(); ++i) {
-    networks->push_back(network_infos[i].interface);
-  }
-  return true;
-#endif
 }
+#endif  // OS_NACL
 
 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
   return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
diff --git a/net/base/net_util_posix.h b/net/base/net_util_posix.h
index b553d1c..93089c2 100644
--- a/net/base/net_util_posix.h
+++ b/net/base/net_util_posix.h
@@ -5,27 +5,21 @@
 #ifndef NET_BASE_NET_UTIL_POSIX_H_
 #define NET_BASE_NET_UTIL_POSIX_H_
 
-// This file is only used to expose some of the internals
-// of net_util_posix.cc to tests.
+// This file is only used to expose some of the internals of
+// net_util_posix.cc to net_util_linux.cc and net_util_mac.cc.
+
+#include <string>
+
+struct sockaddr;
 
 namespace net {
 namespace internal {
-
-#if !defined(OS_MACOSX) && !defined(OS_NACL)
-typedef char* (*GetInterfaceNameFunction)(unsigned int interface_index,
-                                          char* ifname);
-
-NET_EXPORT bool GetNetworkListImpl(
-    NetworkInterfaceList* networks,
-    int policy,
-    const base::hash_set<int>& online_links,
-    const internal::AddressTrackerLinux::AddressMap& address_map,
-    GetInterfaceNameFunction get_interface_name);
-
-#endif  // !OS_MACOSX && !OS_NACL
+#if !defined(OS_NACL)
+bool ShouldIgnoreInterface(const std::string& name, int policy);
+bool IsLoopbackOrUnspecifiedAddress(const sockaddr* addr);
+#endif  // !OS_NACL
 
 }  // namespace internal
-
 }  // namespace net
 
 #endif  // NET_BASE_NET_UTIL_POSIX_H_
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index 45f0857..a25903a1 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -17,10 +17,17 @@
 #include "base/strings/utf_string_conversions.h"
 #include "base/sys_byteorder.h"
 #include "base/time/time.h"
+#include "net/base/ip_endpoint.h"
 
 #if !defined(OS_NACL) && !defined(OS_WIN)
 #include <net/if.h>
 #include <netinet/in.h>
+#if defined(OS_MACOSX)
+#include <ifaddrs.h>
+#if !defined(OS_IOS)
+#include <netinet/in_var.h>
+#endif  // !OS_IOS
+#endif  // OS_MACOSX
 #endif  // !OS_NACL && !OS_WIN
 #include "testing/gtest/include/gtest/gtest.h"
 #include "url/gurl.h"
@@ -38,6 +45,11 @@
 
 #if !defined(OS_WIN)
 #include "net/base/net_util_posix.h"
+#if defined(OS_MACOSX)
+#include "net/base/net_util_mac.h"
+#else  // OS_MACOSX
+#include "net/base/net_util_linux.h"
+#endif
 #endif  // !OS_WIN
 
 using base::ASCIIToUTF16;
@@ -85,6 +97,58 @@
   return out;
 }
 
+#if defined(OS_MACOSX)
+class IPAttributesGetterTest : public internal::IPAttributesGetterMac {
+ public:
+  IPAttributesGetterTest() : native_attributes_(0) {}
+  bool IsInitialized() const override { return true; }
+  bool GetIPAttributes(const char* ifname,
+                       const sockaddr* sock_addr,
+                       int* native_attributes) override {
+    *native_attributes = native_attributes_;
+    return true;
+  }
+  void set_native_attributes(int native_attributes) {
+    native_attributes_ = native_attributes;
+  }
+
+ private:
+  int native_attributes_;
+};
+
+// Helper function to create a single valid ifaddrs
+bool FillIfaddrs(ifaddrs* interfaces,
+                 const char* ifname,
+                 uint flags,
+                 const IPAddressNumber& ip_address,
+                 const IPAddressNumber& ip_netmask,
+                 sockaddr_storage sock_addrs[2]) {
+  interfaces->ifa_next = NULL;
+  interfaces->ifa_name = const_cast<char*>(ifname);
+  interfaces->ifa_flags = flags;
+
+  socklen_t sock_len = sizeof(sockaddr_storage);
+
+  // Convert to sockaddr for next check.
+  if (!IPEndPoint(ip_address, 0)
+           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[0]),
+                       &sock_len)) {
+    return false;
+  }
+  interfaces->ifa_addr = reinterpret_cast<sockaddr*>(&sock_addrs[0]);
+
+  sock_len = sizeof(sockaddr_storage);
+  if (!IPEndPoint(ip_netmask, 0)
+           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[1]),
+                       &sock_len)) {
+    return false;
+  }
+  interfaces->ifa_netmask = reinterpret_cast<sockaddr*>(&sock_addrs[1]);
+
+  return true;
+}
+#endif  // OS_MACOSX
+
 }  // anonymous namespace
 
 TEST(NetUtilTest, GetIdentityFromURL) {
@@ -812,6 +876,18 @@
   }
 }
 
+static const char ifname_em1[] = "em1";
+static const char ifname_vm[] = "vmnet";
+
+static const unsigned char kIPv6LocalAddr[] = {
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
+static const unsigned char kIPv6Addr[] =
+  {0x24, 0x01, 0xfa, 0x00, 0x00, 0x04, 0x10, 0x00, 0xbe, 0x30, 0x5b, 0xff,
+   0xfe, 0xe5, 0x00, 0xc3};
+static const unsigned char kIPv6Netmask[] =
+  {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+   0x00, 0x00, 0x00, 0x00};
+
 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_NACL)
 
 char* CopyInterfaceName(const char* ifname, int ifname_size, char* output) {
@@ -820,30 +896,24 @@
   return output;
 }
 
-static const char ifname_em1[] = "em1";
 char* GetInterfaceName(unsigned int interface_index, char* ifname) {
   return CopyInterfaceName(ifname_em1, arraysize(ifname_em1), ifname);
 }
 
-static const char ifname_vm[] = "vmnet";
 char* GetInterfaceNameVM(unsigned int interface_index, char* ifname) {
   return CopyInterfaceName(ifname_vm, arraysize(ifname_vm), ifname);
 }
 
 TEST(NetUtilTest, GetNetworkListTrimming) {
-  NetworkInterfaceList results;
-  ::base::hash_set<int> online_links;
-  net::internal::AddressTrackerLinux::AddressMap address_map;
-
-  const unsigned char kIPv6LocalAddr[] = {
-      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
-  const unsigned char kIPv6Addr[] =
-    {0x24, 0x01, 0xfa, 0x00, 0x00, 0x04, 0x10, 0x00, 0xbe, 0x30, 0x5b, 0xff,
-     0xfe, 0xe5, 0x00, 0xc3};
-
   IPAddressNumber ipv6_local_address(
       kIPv6LocalAddr, kIPv6LocalAddr + arraysize(kIPv6LocalAddr));
   IPAddressNumber ipv6_address(kIPv6Addr, kIPv6Addr + arraysize(kIPv6Addr));
+  IPAddressNumber ipv6_netmask(kIPv6Netmask,
+                               kIPv6Netmask + arraysize(kIPv6Netmask));
+
+  NetworkInterfaceList results;
+  ::base::hash_set<int> online_links;
+  net::internal::AddressTrackerLinux::AddressMap address_map;
 
   // Interface 1 is offline.
   struct ifaddrmsg msg = {
@@ -956,7 +1026,101 @@
   results.clear();
 }
 
-#endif
+#elif defined(OS_MACOSX)
+
+TEST(NetUtilTest, GetNetworkListTrimming) {
+  IPAddressNumber ipv6_local_address(
+      kIPv6LocalAddr, kIPv6LocalAddr + arraysize(kIPv6LocalAddr));
+  IPAddressNumber ipv6_address(kIPv6Addr, kIPv6Addr + arraysize(kIPv6Addr));
+  IPAddressNumber ipv6_netmask(kIPv6Netmask,
+                               kIPv6Netmask + arraysize(kIPv6Netmask));
+
+  NetworkInterfaceList results;
+  IPAttributesGetterTest ip_attributes_getter;
+  sockaddr_storage addresses[2];
+  ifaddrs interface;
+
+  // Address of offline links should be ignored.
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_em1, IFF_UP, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 0ul);
+
+  // Local address should be trimmed out.
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_em1, IFF_RUNNING,
+                          ipv6_local_address, ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 0ul);
+
+  // vmware address should return by default.
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_vm, IFF_RUNNING, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 1ul);
+  EXPECT_EQ(results[0].name, ifname_vm);
+  EXPECT_EQ(results[0].network_prefix, 1ul);
+  EXPECT_EQ(results[0].address, ipv6_address);
+  results.clear();
+
+  // vmware address should be trimmed out if policy specified so.
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_vm, IFF_RUNNING, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 0ul);
+  results.clear();
+
+#if !defined(OS_IOS)
+  // Addresses with banned attributes should be ignored.
+  ip_attributes_getter.set_native_attributes(IN6_IFF_ANYCAST);
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_em1, IFF_RUNNING, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 0ul);
+  results.clear();
+
+  // Addresses with allowed attribute IFA_F_TEMPORARY should be returned and
+  // attributes should be translated correctly.
+  ip_attributes_getter.set_native_attributes(IN6_IFF_TEMPORARY);
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_em1, IFF_RUNNING, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 1ul);
+  EXPECT_EQ(results[0].name, ifname_em1);
+  EXPECT_EQ(results[0].network_prefix, 1ul);
+  EXPECT_EQ(results[0].address, ipv6_address);
+  EXPECT_EQ(results[0].ip_address_attributes, IP_ADDRESS_ATTRIBUTE_TEMPORARY);
+  results.clear();
+
+  // Addresses with allowed attribute IFA_F_DEPRECATED should be returned and
+  // attributes should be translated correctly.
+  ip_attributes_getter.set_native_attributes(IN6_IFF_DEPRECATED);
+  ASSERT_TRUE(FillIfaddrs(&interface, ifname_em1, IFF_RUNNING, ipv6_address,
+                          ipv6_netmask, addresses));
+  EXPECT_TRUE(net::internal::GetNetworkListImpl(
+      &results, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES, &interface,
+      &ip_attributes_getter));
+  EXPECT_EQ(results.size(), 1ul);
+  EXPECT_EQ(results[0].name, ifname_em1);
+  EXPECT_EQ(results[0].network_prefix, 1ul);
+  EXPECT_EQ(results[0].address, ipv6_address);
+  EXPECT_EQ(results[0].ip_address_attributes, IP_ADDRESS_ATTRIBUTE_DEPRECATED);
+  results.clear();
+#endif  // !OS_IOS
+}
+
+#endif  // !OS_MACOSX && !OS_WIN && !OS_NACL
 
 namespace {
 
diff --git a/net/net.gyp b/net/net.gyp
index e9379e0..3c9a520a 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -487,10 +487,14 @@
             ['include', '^base/platform_mime_util_linux\\.cc$'],
             ['include', '^base/address_tracker_linux\\.cc$'],
             ['include', '^base/address_tracker_linux\\.h$'],
+            ['include', '^base/net_util_linux\\.cc$'],
+            ['include', '^base/net_util_linux\\.h$'],
           ],
         }],
         ['OS == "ios"', {
           'sources/': [
+            ['include', '^base/net_util_mac\\.cc$'],
+            ['include', '^base/net_util_mac\\.h$'],
             ['include', '^base/network_change_notifier_mac\\.cc$'],
             ['include', '^base/network_config_watcher_mac\\.cc$'],
             ['include', '^base/platform_mime_util_mac\\.mm$'],
diff --git a/net/net.gypi b/net/net.gypi
index a9727838..b51c2ecd 100644
--- a/net/net.gypi
+++ b/net/net.gypi
@@ -246,6 +246,8 @@
       'base/net_log_logger.h',
       'base/net_log_util.cc',
       'base/net_log_util.h',
+      'base/net_util_linux.cc',
+      'base/net_util_mac.cc',
       'base/net_util_win.cc',
       'base/network_change_notifier.cc',
       'base/network_change_notifier.h',