diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 4e23347..592851c 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -3200,7 +3200,7 @@ function ip_address() {
         $forwarded[] = $ip_address;
 
         // Eliminate all trusted IPs.
-        $untrusted = array_diff($forwarded, $reverse_proxy_addresses);
+        $untrusted = drupal_remove_trusted_addresses($forwarded, $reverse_proxy_addresses);
 
         if (!empty($untrusted)) {
           // The right-most IP is the most specific we can trust.
@@ -3219,6 +3219,157 @@ function ip_address() {
 }
 
 /**
+ * Returns an array of untrusted IP address.
+ *
+ * If Drupal is behind a reverse proxy, we can determine the actual remote
+ * address by removing all configured reverse proxy addresses from the list
+ * of IPs in the request.
+ *
+ * This function uses helpers to create an array of "trusted" addresses, then
+ * returns the difference between that array and the array of addresses in the
+ * current request.
+ *
+ * @param $forwarded
+ *   An array of IP addresses from the request headers.
+ * @param $reverse_proxy_addresses
+ *   An array of reverse proxy IP addresses and network ranges.
+ *
+ * @return
+ *   An array of "untrusted" IP addresses.
+ *
+ * @see ip_address()
+ */
+function drupal_remove_trusted_addresses($forwarded, $reverse_proxy_addresses) {
+
+  $trusted = array();
+
+  foreach ($forwarded as $ip) {
+    if (drupal_check_ip($ip, $reverse_proxy_addresses)) {
+      $trusted[] = $ip;
+    }
+  }
+  return array_diff($forwarded, $trusted);
+}
+
+/**
+ * Check if a given IP address matches another IP or network range.
+ *
+ * This function is a clone of Symfony's IpUtils\CheckIp() function.
+ *
+ * @param $ip
+ *   A valid IPv4 or IPv6 address.
+ * @param $network
+ *   List of IPs or subnets (can be a string if only a single one)
+ *
+ * @return
+ *   TRUE if the address is part of the given network range or FALSE if not.
+ *
+ * @see drupal_remove_trusted_addresses()
+ * @see \Symfony\Component\HttpFoundation\IpUtils.php
+ */
+function drupal_check_ip($ip, $networks) {
+
+  if (!is_array($networks)) {
+    $networks = array($networks);
+  }
+
+  foreach ($networks as $network) {
+    $method = substr_count($ip, ':') > 1 ? 'drupal_check_ip6' : 'drupal_check_ip4';
+    if ($method($ip, $network)) {
+      return true;
+    }
+  }
+  return false;
+}
+
+/**
+ * Compares two IPv4 addresses.
+ * In case a subnet is given, it checks if it contains the request IP.
+ *
+ * This function is a clone of Symfony's IpUtils\CheckIp4() function.
+ *
+ * @param string $ip
+ *   IPv4 address to check
+ * @param string $network
+ *   IPv4 address or subnet in CIDR notation
+ *
+ * @return bool
+ *   Whether the IP is valid
+ *
+ * @see drupal_check_ip()
+ * @see \Symfony\Component\HttpFoundation\IpUtils.php
+ */
+function drupal_check_ip4($ip, $network) {
+  if (false !== strpos($network, '/')) {
+    list($address, $netmask) = explode('/', $network, 2);
+
+    if ($netmask < 1 || $netmask > 32) {
+      return false;
+    }
+  } else {
+    $address = $network;
+    $netmask = 32;
+  }
+
+  return 0 === substr_compare(sprintf('%032b', ip2long($ip)), sprintf('%032b', ip2long($address)), 0, $netmask);
+}
+
+/**
+ * Compares two IPv6 addresses.
+ * In case a subnet is given, it checks if it contains the request IP.
+ *
+ * This function is a clone of Symfony's IpUtils\CheckIp4() function.
+ *
+ * @author David Soria Parra <dsp at php dot net>
+ *
+ * @see https://github.com/dsp/v6tools
+ *
+ * @param string $ip
+ *   IPv6 address to check
+ * @param string $network
+ *   IPv6 address or subnet in CIDR notation
+ *
+ * @return bool
+ *   Whether the IP is valid
+ *
+ * @throws \RuntimeException
+ *   When IPV6 support is not enabled
+ *
+ * @see drupal_check_ip()
+ * @see \Symfony\Component\HttpFoundation\IpUtils.php
+ */
+function drupal_check_ip6($ip, $network) {
+  if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
+    throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
+  }
+
+  if (false !== strpos($ip, '/')) {
+    list($address, $netmask) = explode('/', $network, 2);
+
+    if ($netmask < 1 || $netmask > 128) {
+      return false;
+    }
+  } else {
+    $address = $network;
+    $netmask = 128;
+  }
+
+  $bytesAddr = unpack('n*', inet_pton($address));
+  $bytesTest = unpack('n*', inet_pton($ip));
+
+  for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
+    $left = $netmask - 16 * ($i - 1);
+    $left = ($left <= 16) ? $left : 16;
+    $mask = ~(0xffff >> $left) & 0xffff;
+    if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+/**
  * @addtogroup schemaapi
  * @{
  */
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index 16ac171..af5ac1c 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -16,6 +16,10 @@ class BootstrapIPAddressTestCase extends DrupalWebTestCase {
     $this->remote_ip = '127.0.0.1';
     $this->proxy_ip = '127.0.0.2';
     $this->proxy2_ip = '127.0.0.3';
+    $this->proxy3_ip = '10.0.0.1';
+    $this->proxy3_range = '10.0.0.0/8';
+    $this->proxy4_ip = '2404:6800:4003:c02::deca:fbad';
+    $this->proxy4_range = '2404:6800:4003:c02::/64';
     $this->forwarded_ip = '127.0.0.4';
     $this->cluster_ip = '127.0.0.5';
     $this->untrusted_ip = '0.0.0.0';
@@ -53,7 +57,7 @@ class BootstrapIPAddressTestCase extends DrupalWebTestCase {
     );
 
     // Proxy forwarding on and proxy address not trusted.
-    variable_set('reverse_proxy_addresses', array($this->proxy_ip, $this->proxy2_ip));
+    variable_set('reverse_proxy_addresses', array($this->proxy_ip, $this->proxy2_ip, $this->proxy3_range, $this->proxy4_range));
     drupal_static_reset('ip_address');
     $_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
     $this->assertTrue(
@@ -79,6 +83,42 @@ class BootstrapIPAddressTestCase extends DrupalWebTestCase {
       'Visiting from trusted proxy got proxy IP address.'
     );
 
+    // Proxy forwarding on and proxy address in trusted network range.
+    $_SERVER['REMOTE_ADDR'] = $this->proxy3_ip;
+    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
+    drupal_static_reset('ip_address');
+    $this->assertTrue(
+      ip_address() == $this->forwarded_ip,
+      'Proxy forwarding with trusted proxy in network range got forwarded IP address.'
+    );
+
+    // Proxy forwarding on and proxy address in trusted network range and visiting from proxy.
+    $_SERVER['REMOTE_ADDR'] = $this->proxy3_ip;
+    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->proxy3_ip;
+    drupal_static_reset('ip_address');
+    $this->assertTrue(
+      ip_address() == $this->proxy3_ip,
+      'Visiting from trusted proxy in network range got proxy IP address.'
+    );
+
+    // Proxy forwarding on and proxy address in trusted IPv6 network range.
+    $_SERVER['REMOTE_ADDR'] = $this->proxy4_ip;
+    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
+    drupal_static_reset('ip_address');
+    $this->assertTrue(
+      ip_address() == $this->forwarded_ip,
+      'Proxy forwarding with trusted proxy in IPv6 network range got forwarded IP address.'
+    );
+
+    // Proxy forwarding on and proxy address in trusted IPv6 network range and visiting from proxy.
+    $_SERVER['REMOTE_ADDR'] = $this->proxy4_ip;
+    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->proxy4_ip;
+    drupal_static_reset('ip_address');
+    $this->assertTrue(
+      ip_address() == $this->proxy4_ip,
+      'Visiting from trusted proxy in IPv6 network range got proxy IP address.'
+    );
+
     // Multi-tier architecture with comma separated values in header.
     $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
     $_SERVER['HTTP_X_FORWARDED_FOR'] = implode(', ', array($this->untrusted_ip, $this->forwarded_ip, $this->proxy2_ip));
