Changeset 50761 in vbox
- Timestamp:
- Mar 13, 2014 8:30:17 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp
r50747 r50761 5 5 6 6 /* 7 * Copyright (C) 2006-201 2Oracle Corporation7 * Copyright (C) 2006-2014 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 139 139 static mbuf_tag_id_t g_idTag; 140 140 141 /** the offset of the struct ifnet::if_pcount variable (valid for Lion). */ 141 /** The offset of the struct ifnet::if_pcount variable. 142 * @remarks Initial value is valid for Lion and earlier. We adjust it on attach 143 * for later releases. */ 142 144 static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/) 143 145 + sizeof(u_long) /*if_refcnt*/; 144 146 /** Macro for accessing ifnet::if_pcount. */ 145 147 #define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) ) 146 147 148 /** The size of area of ifnet structure we try to locate if_pcount in. */ 148 149 #define VBOXNETFLT_DARWIN_IFNET_SIZE 256 149 150 /* 151 * Note that this implementation relies on if_pcount to be aligned on sizeof(int). 150 /** Indicates whether g_offIfNetPCount has been adjusted already (no point in 151 * doing it more than once). */ 152 static bool g_fNetPCountFound = false; 153 154 155 /** 156 * Change the promiscuous setting and try spot the changed in @a pIfNet. 157 * 158 * @returns Offset of potential p_count field. 159 * @param pIfNet The interface we're attaching to. 160 * @param iPromisc Whether to enable (1) or disable (0) promiscuous mode. 161 * 162 * @note This implementation relies on if_pcount to be aligned on sizeof(int). 152 163 */ 153 164 static unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc) 154 165 { 155 unsigned i; 156 int aSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE/sizeof(int)]; 157 158 memcpy(aSavedState, pIfNet, sizeof(aSavedState)); 166 int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)]; 167 memcpy(aiSavedState, pIfNet, sizeof(aiSavedState)); 168 159 169 ifnet_set_promiscuous(pIfNet, iPromisc); 160 170 161 int offset = 0;162 int iDiff = iPromisc ? 1 : -1; 171 int const iDiff = iPromisc ? 1 : -1; 172 163 173 /* 164 174 * We assume that ifnet structure will never have less members in front of if_pcount … … 166 176 * have to start from zero offset. 167 177 */ 168 for (i = g_offIfNetPCount / sizeof(int); i < sizeof(aSavedState) / sizeof(int); i++) 169 if (((int*)pIfNet)[i] - aSavedState[i] == iDiff) 170 { 171 offset = i * sizeof(int); 172 break; 173 } 174 175 return offset; 176 } 178 for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++) 179 if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff) 180 return i * sizeof(int); 181 182 return 0; 183 } 184 177 185 178 186 /** 179 187 * Detect and adjust the offset of ifnet::if_pcount. 188 * 189 * @param pIfNet The interface we're attaching to. 180 190 */ 181 191 static void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet) 182 192 { 183 unsigned offTry1, offTry2, offTry3, offTry4; 193 if (g_fNetPCountFound) 194 return; 195 184 196 /* 185 197 * It would be nice to use locking at this point, but it is not available via KPI. … … 187 199 * to rule out false detections. 188 200 */ 189 for (int nAttempts = 0; nAttempts < 3; nAttempts++) 201 unsigned offTry1, offTry2, offTry3, offTry4; 202 for (int iAttempt = 0; iAttempt < 3; iAttempt++) 190 203 { 191 204 offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1); … … 193 206 offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0); 194 207 offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0); 208 195 209 /* If any attempt has failed we won't continue as our algorithm is flawed. */ 196 210 if (!offTry1 || !offTry2 || !offTry3 || !offTry4) … … 202 216 Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount)); 203 217 g_offIfNetPCount = offTry1; 218 g_fNetPCountFound = true; 204 219 } 205 220 break; … … 210 225 LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1)); 211 226 } 227 212 228 213 229 /**
Note:
See TracChangeset
for help on using the changeset viewer.