Bug 1579424 - P2 Calculate network id not only using the main gateway, r=michal
authorJunior Hsu <juhsu@mozilla.com>
Tue, 12 Nov 2019 16:04:38 +0000
changeset 502268 500126439162e6a839f8c0bc0d8355ad0f5386e2
parent 502267 224557872391ae18c2885ca91e98f07e3bb48983
child 502269 1d6992a1811e20062d4813630fe7a4e23c37c142
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1579424
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1579424 - P2 Calculate network id not only using the main gateway, r=michal Differential Revision: https://phabricator.services.mozilla.com/D48804
netwerk/system/mac/nsNetworkLinkService.mm
--- a/netwerk/system/mac/nsNetworkLinkService.mm
+++ b/netwerk/system/mac/nsNetworkLinkService.mm
@@ -402,49 +402,51 @@ static bool scanArp(char* ip, char* mac,
     if (matchIp(sdl, sin2, ip, mac, maclen)) {
       return true;
     }
   }
 
   return false;
 }
 
-/*
- * Fetch the routing table and only return the first gateway,
- * Which is the default gateway.
- *
- * Returns 0 if the default gateway's IP has been found.
- */
-static int routingTable(char* gw, size_t aGwLen) {
+//
+// Figure out the current IPv4 "network identification" string.
+//
+// It detects the IP of the default gateways in the routing table, then the MAC
+// address of that IP in the ARP table before it hashes that string (to avoid
+// information leakage).
+//
+static bool ipv4NetworkId(SHA1Sum* aSHA1) {
   size_t needed;
   int mib[6];
   struct rt_msghdr* rtm;
   struct sockaddr* sa;
   struct sockaddr_in* sockin;
 
   mib[0] = CTL_NET;
   mib[1] = PF_ROUTE;
   mib[2] = 0;
   mib[3] = 0;
   mib[4] = NET_RT_DUMP;
   mib[5] = 0;
 
   if (sysctl(mib, 6, nullptr, &needed, nullptr, 0) < 0) {
-    return 1;
+    return false;
   }
 
   UniquePtr<char[]> buf(new char[needed]);
 
   if (sysctl(mib, 6, &buf[0], &needed, nullptr, 0) < 0) {
-    return 3;
+    return false;
   }
 
   char ip[INET_ADDRSTRLEN];
 
   char* lim = &buf[0] + needed;
+  nsTArray<nsCString> hash;
 
   // `next + 1 < lim` ensures we have valid `rtm->rtm_msglen` which is an
   // unsigned short at the beginning of `rt_msghdr`.
   for (char* next = &buf[0]; next + 1 < lim; next += rtm->rtm_msglen) {
     rtm = reinterpret_cast<struct rt_msghdr*>(next);
 
     if (next + rtm->rtm_msglen > lim) {
       LOG(("Rt msg is truncated..."));
@@ -480,57 +482,40 @@ static int routingTable(char* gw, size_t
 
     if (!gateway) {
       continue;
     }
     if (gateway->sa_family == AF_INET) {
       sockin = reinterpret_cast<struct sockaddr_in*>(gateway);
       inet_ntop(AF_INET, &sockin->sin_addr.s_addr, ip, sizeof(ip) - 1);
       char mac[18];
+
       if (scanArp(ip, mac, sizeof(mac))) {
-        // TODO: use the mac to calculate network id
+        hash.AppendElement(nsCString(mac));
+      } else {
+        // TODO: fail over to ip and interface name
       }
     } else if (gateway->sa_family == AF_LINK) {
       char buf[64];
       struct sockaddr_dl* sockdl = reinterpret_cast<struct sockaddr_dl*>(gateway);
       if (getMac(sockdl, buf, sizeof(buf))) {
-        // TODO: use the mac to calculate network id
+        hash.AppendElement(nsCString(buf));
+      } else {
+        // TODO: fail over to interface name
       }
     }
   }
 
-  // There's no need to iterate over the routing table
-  // We're only looking for the first (default) gateway
-  rtm = reinterpret_cast<struct rt_msghdr*>(&buf[0]);
-  sa = reinterpret_cast<struct sockaddr*>(rtm + 1);
-  sa = reinterpret_cast<struct sockaddr*>(SA_SIZE(sa) + (char*)sa);
-  sockin = reinterpret_cast<struct sockaddr_in*>(sa);
-  inet_ntop(AF_INET, &sockin->sin_addr.s_addr, gw, aGwLen - 1);
-
-  return 0;
-}
+  hash.Sort();
+  for (uint32_t i = 0; i < hash.Length(); ++i) {
+    LOG(("Hashing string for network id: %s", hash[i].get()));
+    aSHA1->update(hash[i].get(), hash[i].Length());
+  }
 
-//
-// Figure out the current IPv4 "network identification" string.
-//
-// It detects the IP of the default gateway in the routing table, then the MAC
-// address of that IP in the ARP table before it hashes that string (to avoid
-// information leakage).
-//
-static bool ipv4NetworkId(SHA1Sum* sha1) {
-  char gw[INET_ADDRSTRLEN];
-  if (!routingTable(gw, sizeof(gw))) {
-    char mac[18];  // big enough for a printable MAC address
-    if (scanArp(gw, mac, sizeof(mac))) {
-      LOG(("networkid: MAC %s\n", mac));
-      sha1->update(mac, strlen(mac));
-      return true;
-    }
-  }
-  return false;
+  return true;
 }
 
 //
 // Sort and hash the prefixes and netmasks
 //
 void nsNetworkLinkService::HashSortedPrefixesAndNetmasks(
     std::vector<prefix_and_netmask> prefixAndNetmaskStore, SHA1Sum* sha1) {
   // getifaddrs does not guarantee the interfaces will always be in the same order.