diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 104c86f..2e546cb 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2932,7 +2932,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);
 
         // The right-most IP is the most specific we can trust.
         $ip_address = array_pop($untrusted);
@@ -2944,6 +2944,143 @@ 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 ($reverse_proxy_addresses as $proxy) {
+    $list = explode('/', $proxy);
+    $network = $list[0];
+    $netmask = (count($list) == 1) ? NULL : $list[1];
+
+    // Check if we're dealing with a single address.
+    if (empty($netmask)) {
+      if (in_array($network, $forwarded)) {
+        $trusted[] = $network;
+      }
+    }
+    else {
+      // Looking at a network range. Loop over each address in $forwarded
+      // and check if it matches.
+      foreach ($forwarded as $address) {
+        if (drupal_ip_in_network($address, $network, $netmask)) {
+          $trusted[] = $address;
+        }
+      }
+    }
+  }
+  return array_diff($forwarded, $trusted);
+}
+
+/**
+ * Check if a given IPv4 or IPv6 address is part of a network range.
+ *
+ * This function uses helpers to convert the IP and network addresses to
+ * a binary string representation, which are mostly taken from the PHP
+ * documentation page for the ip2long() function.
+ *
+ * @param $address
+ *   A valid IPv4 or IPv6 address.
+ * @param $network
+ *   A valid IPv4 or IPv6 network address.
+ * @param $netmask
+ *   A netmask in dotted quad or CIDR notation.
+ *
+ * @return
+ *   TRUE if the address is part of the given network range or FALSE if not.
+ *
+ * @see drupal_remove_trusted_addresses()
+ * @see http://php.net/manual/en/function.ip2long.php
+ */
+function drupal_ip_in_network($address, $network, $netmask) {
+  // Ensure the IPv4 netmask is in CIDR notation.
+  if (strpos($netmask, '.') !== FALSE) {
+    $long = ip2long($netmask);
+    $base = ip2long('255.255.255.255');
+    $netmask = 32 - log(($long ^ $base) + 1, 2);
+  }
+  $address_bin = drupal_ip_to_bin($address);
+  $network_bin = drupal_ip_to_bin($network);
+
+  return (substr_compare($address_bin, $network_bin, 0, $netmask) === 0);
+}
+
+/**
+ * Convert an IP address to a binary string.
+ *
+ * @param $address.
+ *   A valid human-readble IP address.
+ *
+ * @return
+ *   A binary string representation of the IPv4 address or FALSE if an invalid
+ *   address is passed.
+ *
+ * @see http://php.net/ip2long
+ */
+function drupal_ip_to_bin($address) {
+  // IPv4 addresses are straightforward.
+  if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false)
+    return base_convert(ip2long($address), 10, 2);
+
+  // Early return if not a valid IPv6 address.
+  if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false)
+    return false;
+
+  if (($ip6_addr = inet_pton($address)) === false) {
+    return false;
+  }
+
+  // Convert the ip_adr v6 address into a binary string.
+  $bits = 15; // 16 x 8 bit = 128bit (ipv6)
+  $addr_bin = '';
+  while ($bits >= 0) {
+    $bin = sprintf("%08b",(ord($ip6_addr[$bits])));
+    $addr_bin = $bin.$addr_bin;
+    $bits--;
+  }
+ return $addr_bin;
+}
+
+/**
+ * Convert an IPv6 address to a binary string.
+ *
+ * @param $address.
+ *   An IP or network address in dotted quad notation.
+ *
+ * @return
+ *   A binary string representation of the IPv6 address.
+ */
+function drupal_ipv6_to_bin($address) {
+  $unpacked = unpack('A16', $address);
+  $unpacked = str_split($unpacked[1]);
+  $binary = '';
+  foreach ($unpacked as $char) {
+    $binary .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
+  }
+  return $binary;
+}
+
+/**
  * @addtogroup schemaapi
  * @{
  */
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index ece1cd9..8e885d8 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -16,6 +16,8 @@ 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.0/8';
+    $this->proxy4_ip = '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 +55,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_ip, $this->proxy4_ip));
     drupal_static_reset('ip_address');
     $_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
     $this->assertTrue(
