Index: /trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp
===================================================================
--- /trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp	(revision 50760)
+++ /trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp	(revision 50761)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2012 Oracle Corporation
+ * Copyright (C) 2006-2014 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -139,26 +139,36 @@
 static mbuf_tag_id_t g_idTag;
 
-/** the offset of the struct ifnet::if_pcount variable (valid for Lion). */
+/** The offset of the struct ifnet::if_pcount variable.
+ * @remarks Initial value is valid for Lion and earlier. We adjust it on attach
+ *          for later releases.  */
 static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
                                  + sizeof(u_long) /*if_refcnt*/;
 /** Macro for accessing ifnet::if_pcount. */
 #define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
-
 /** The size of area of ifnet structure we try to locate if_pcount in. */
 #define VBOXNETFLT_DARWIN_IFNET_SIZE 256
-
-/*
- * Note that this implementation relies on if_pcount to be aligned on sizeof(int).
+/** Indicates whether g_offIfNetPCount has been adjusted already (no point in
+ * doing it more than once). */
+static bool g_fNetPCountFound  = false;
+
+
+/**
+ * Change the promiscuous setting and try spot the changed in @a pIfNet.
+ *
+ * @returns Offset of potential p_count field.
+ * @param   pIfNet      The interface we're attaching to.
+ * @param   iPromisc    Whether to enable (1) or disable (0) promiscuous mode.
+ *
+ * @note    This implementation relies on if_pcount to be aligned on sizeof(int).
  */
 static unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc)
 {
-    unsigned i;
-    int aSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE/sizeof(int)];
-
-    memcpy(aSavedState, pIfNet, sizeof(aSavedState));
+    int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)];
+    memcpy(aiSavedState, pIfNet, sizeof(aiSavedState));
+
     ifnet_set_promiscuous(pIfNet, iPromisc);
 
-    int offset = 0;
-    int iDiff = iPromisc ? 1 : -1;
+    int const iDiff = iPromisc ? 1 : -1;
+
     /*
      * We assume that ifnet structure will never have less members in front of if_pcount
@@ -166,20 +176,22 @@
      * have to start from zero offset.
      */
-    for (i = g_offIfNetPCount / sizeof(int); i < sizeof(aSavedState) / sizeof(int); i++)
-        if (((int*)pIfNet)[i] - aSavedState[i] == iDiff)
-        {
-            offset = i * sizeof(int);
-            break;
-        }
-
-    return offset;
-}
+    for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++)
+        if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff)
+            return i * sizeof(int);
+
+    return 0;
+}
+
 
 /**
  * Detect and adjust the offset of ifnet::if_pcount.
+ *
+ * @param   pIfNet      The interface we're attaching to.
  */
 static void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet)
 {
-    unsigned offTry1, offTry2, offTry3, offTry4;
+    if (g_fNetPCountFound)
+        return;
+
     /*
      * It would be nice to use locking at this point, but it is not available via KPI.
@@ -187,5 +199,6 @@
      * to rule out false detections.
      */
-    for (int nAttempts = 0; nAttempts < 3; nAttempts++)
+    unsigned offTry1, offTry2, offTry3, offTry4;
+    for (int iAttempt = 0; iAttempt < 3; iAttempt++)
     {
         offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
@@ -193,4 +206,5 @@
         offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
         offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
+
         /* If any attempt has failed we won't continue as our algorithm is flawed. */
         if (!offTry1 || !offTry2 || !offTry3 || !offTry4)
@@ -202,4 +216,5 @@
                 Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount));
                 g_offIfNetPCount = offTry1;
+                g_fNetPCountFound = true;
             }
             break;
@@ -210,4 +225,5 @@
         LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1));
 }
+
 
 /**
